org.el 808 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494
  1. ;;; org.el --- Outline-based notes management and organizer
  2. ;; Carstens outline-mode for keeping track of everything.
  3. ;; Copyright (C) 2004-2012 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. ;; Version: 7.8.09
  10. ;;
  11. ;; This file is part of GNU Emacs.
  12. ;;
  13. ;; GNU Emacs is free software: you can redistribute it and/or modify
  14. ;; it under the terms of the GNU General Public License as published by
  15. ;; the Free Software Foundation, either version 3 of the License, or
  16. ;; (at your option) any later version.
  17. ;; GNU Emacs is distributed in the hope that it will be useful,
  18. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. ;; GNU General Public License for more details.
  21. ;; You should have received a copy of the GNU General Public License
  22. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  23. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  24. ;;
  25. ;;; Commentary:
  26. ;;
  27. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  28. ;; project planning with a fast and effective plain-text system.
  29. ;;
  30. ;; Org-mode develops organizational tasks around NOTES files that contain
  31. ;; information about projects as plain text. Org-mode is implemented on
  32. ;; top of outline-mode, which makes it possible to keep the content of
  33. ;; large files well structured. Visibility cycling and structure editing
  34. ;; help to work with the tree. Tables are easily created with a built-in
  35. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  36. ;; and scheduling. It dynamically compiles entries into an agenda that
  37. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  38. ;; Plain text URL-like links connect to websites, emails, Usenet
  39. ;; messages, BBDB entries, and any files related to the projects. For
  40. ;; printing and sharing of notes, an Org-mode file can be exported as a
  41. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  42. ;; iCalendar file. It can also serve as a publishing tool for a set of
  43. ;; linked webpages.
  44. ;;
  45. ;; Installation and Activation
  46. ;; ---------------------------
  47. ;; See the corresponding sections in the manual at
  48. ;;
  49. ;; http://orgmode.org/org.html#Installation
  50. ;;
  51. ;; Documentation
  52. ;; -------------
  53. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  54. ;; distribution also contains a PDF version of it. At the homepage of
  55. ;; Org-mode, you can read the same text online as HTML. There is also an
  56. ;; excellent reference card made by Philip Rooke. This card can be found
  57. ;; in the etc/ directory of Emacs 22.
  58. ;;
  59. ;; A list of recent changes can be found at
  60. ;; http://orgmode.org/Changes.html
  61. ;;
  62. ;;; Code:
  63. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  64. (defvar org-table-formula-constants-local nil
  65. "Local version of `org-table-formula-constants'.")
  66. (make-variable-buffer-local 'org-table-formula-constants-local)
  67. ;;;; Require other packages
  68. (eval-when-compile
  69. (require 'cl)
  70. (require 'gnus-sum))
  71. (require 'calendar)
  72. (require 'format-spec)
  73. ;; Emacs 22 calendar compatibility: Make sure the new variables are available
  74. (when (fboundp 'defvaralias)
  75. (unless (boundp 'calendar-view-holidays-initially-flag)
  76. (defvaralias 'calendar-view-holidays-initially-flag
  77. 'view-calendar-holidays-initially))
  78. (unless (boundp 'calendar-view-diary-initially-flag)
  79. (defvaralias 'calendar-view-diary-initially-flag
  80. 'view-diary-entries-initially))
  81. (unless (boundp 'diary-fancy-buffer)
  82. (defvaralias 'diary-fancy-buffer 'fancy-diary-buffer)))
  83. (require 'outline) (require 'noutline)
  84. ;; Other stuff we need.
  85. (require 'time-date)
  86. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  87. (require 'easymenu)
  88. (require 'overlay)
  89. (require 'org-macs)
  90. (require 'org-entities)
  91. (require 'org-compat)
  92. (require 'org-faces)
  93. (require 'org-list)
  94. (require 'org-pcomplete)
  95. (require 'org-src)
  96. (require 'org-footnote)
  97. (declare-function org-inlinetask-at-task-p "org-inlinetask" ())
  98. (declare-function org-inlinetask-outline-regexp "org-inlinetask" ())
  99. (declare-function org-inlinetask-toggle-visibility "org-inlinetask" ())
  100. (declare-function org-pop-to-buffer-same-window "org-compat" (&optional buffer-or-name norecord label))
  101. (declare-function org-at-clock-log-p "org-clock" ())
  102. (declare-function org-clock-timestamps-up "org-clock" ())
  103. (declare-function org-clock-timestamps-down "org-clock" ())
  104. ;; babel
  105. (require 'ob)
  106. (require 'ob-table)
  107. (require 'ob-lob)
  108. (require 'ob-ref)
  109. (require 'ob-tangle)
  110. (require 'ob-comint)
  111. (require 'ob-keys)
  112. ;; load languages based on value of `org-babel-load-languages'
  113. (defvar org-babel-load-languages)
  114. ;;;###autoload
  115. (defun org-babel-do-load-languages (sym value)
  116. "Load the languages defined in `org-babel-load-languages'."
  117. (set-default sym value)
  118. (mapc (lambda (pair)
  119. (let ((active (cdr pair)) (lang (symbol-name (car pair))))
  120. (if active
  121. (progn
  122. (require (intern (concat "ob-" lang))))
  123. (progn
  124. (funcall 'fmakunbound
  125. (intern (concat "org-babel-execute:" lang)))
  126. (funcall 'fmakunbound
  127. (intern (concat "org-babel-expand-body:" lang)))))))
  128. org-babel-load-languages))
  129. (defcustom org-babel-load-languages '((emacs-lisp . t))
  130. "Languages which can be evaluated in Org-mode buffers.
  131. This list can be used to load support for any of the languages
  132. below, note that each language will depend on a different set of
  133. system executables and/or Emacs modes. When a language is
  134. \"loaded\", then code blocks in that language can be evaluated
  135. with `org-babel-execute-src-block' bound by default to C-c
  136. C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
  137. be set to remove code block evaluation from the C-c C-c
  138. keybinding. By default only Emacs Lisp (which has no
  139. requirements) is loaded."
  140. :group 'org-babel
  141. :set 'org-babel-do-load-languages
  142. :version "24.1"
  143. :type '(alist :tag "Babel Languages"
  144. :key-type
  145. (choice
  146. (const :tag "Awk" awk)
  147. (const :tag "C" C)
  148. (const :tag "R" R)
  149. (const :tag "Asymptote" asymptote)
  150. (const :tag "Calc" calc)
  151. (const :tag "Clojure" clojure)
  152. (const :tag "CSS" css)
  153. (const :tag "Ditaa" ditaa)
  154. (const :tag "Dot" dot)
  155. (const :tag "Emacs Lisp" emacs-lisp)
  156. (const :tag "Fortran" fortran)
  157. (const :tag "Gnuplot" gnuplot)
  158. (const :tag "Haskell" haskell)
  159. (const :tag "IO" io)
  160. (const :tag "Java" java)
  161. (const :tag "Javascript" js)
  162. (const :tag "LaTeX" latex)
  163. (const :tag "Ledger" ledger)
  164. (const :tag "Lilypond" lilypond)
  165. (const :tag "Maxima" maxima)
  166. (const :tag "Matlab" matlab)
  167. (const :tag "Mscgen" mscgen)
  168. (const :tag "Ocaml" ocaml)
  169. (const :tag "Octave" octave)
  170. (const :tag "Org" org)
  171. (const :tag "Perl" perl)
  172. (const :tag "Pico Lisp" picolisp)
  173. (const :tag "PlantUML" plantuml)
  174. (const :tag "Python" python)
  175. (const :tag "Ruby" ruby)
  176. (const :tag "Sass" sass)
  177. (const :tag "Scala" scala)
  178. (const :tag "Scheme" scheme)
  179. (const :tag "Screen" screen)
  180. (const :tag "Shell Script" sh)
  181. (const :tag "Shen" shen)
  182. (const :tag "Sql" sql)
  183. (const :tag "Sqlite" sqlite))
  184. :value-type (boolean :tag "Activate" :value t)))
  185. ;;;; Customization variables
  186. (defcustom org-clone-delete-id nil
  187. "Remove ID property of clones of a subtree.
  188. When non-nil, clones of a subtree don't inherit the ID property.
  189. Otherwise they inherit the ID property with a new unique
  190. identifier."
  191. :type 'boolean
  192. :version "24.1"
  193. :group 'org-id)
  194. ;;; Version
  195. (defconst org-version "7.8.09"
  196. "The version number of the file org.el.")
  197. ;;;###autoload
  198. (defun org-version (&optional here)
  199. "Show the org-mode version in the echo area.
  200. With prefix arg HERE, insert it at point."
  201. (interactive "P")
  202. (let* ((origin default-directory)
  203. (version org-version)
  204. (git-version)
  205. (dir (concat (file-name-directory (locate-library "org")) "../" )))
  206. (when (and (file-exists-p (expand-file-name ".git" dir))
  207. (executable-find "git"))
  208. (unwind-protect
  209. (progn
  210. (cd dir)
  211. (when (eql 0 (shell-command "git describe --abbrev=4 HEAD"))
  212. (with-current-buffer "*Shell Command Output*"
  213. (goto-char (point-min))
  214. (setq git-version (buffer-substring (point) (point-at-eol))))
  215. (subst-char-in-string ?- ?. git-version t)
  216. (when (string-match "\\S-"
  217. (shell-command-to-string
  218. "git diff-index --name-only HEAD --"))
  219. (setq git-version (concat git-version ".dirty")))
  220. (setq version (concat version " (" git-version ")"))))
  221. (cd origin)))
  222. (setq version (format "Org-mode version %s" version))
  223. (if here (insert version))
  224. (message version)))
  225. ;;; Compatibility constants
  226. ;;; The custom variables
  227. (defgroup org nil
  228. "Outline-based notes management and organizer."
  229. :tag "Org"
  230. :group 'outlines
  231. :group 'calendar)
  232. (defcustom org-mode-hook nil
  233. "Mode hook for Org-mode, run after the mode was turned on."
  234. :group 'org
  235. :type 'hook)
  236. (defcustom org-load-hook nil
  237. "Hook that is run after org.el has been loaded."
  238. :group 'org
  239. :type 'hook)
  240. (defcustom org-log-buffer-setup-hook nil
  241. "Hook that is run after an Org log buffer is created."
  242. :group 'org
  243. :version "24.1"
  244. :type 'hook)
  245. (defvar org-modules) ; defined below
  246. (defvar org-modules-loaded nil
  247. "Have the modules been loaded already?")
  248. (defun org-load-modules-maybe (&optional force)
  249. "Load all extensions listed in `org-modules'."
  250. (when (or force (not org-modules-loaded))
  251. (mapc (lambda (ext)
  252. (condition-case nil (require ext)
  253. (error (message "Problems while trying to load feature `%s'" ext))))
  254. org-modules)
  255. (setq org-modules-loaded t)))
  256. (defun org-set-modules (var value)
  257. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  258. (set var value)
  259. (when (featurep 'org)
  260. (org-load-modules-maybe 'force)))
  261. (when (org-bound-and-true-p org-modules)
  262. (let ((a (member 'org-infojs org-modules)))
  263. (and a (setcar a 'org-jsinfo))))
  264. (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)
  265. "Modules that should always be loaded together with org.el.
  266. If a description starts with <C>, the file is not part of Emacs
  267. and loading it will require that you have downloaded and properly installed
  268. the org-mode distribution.
  269. You can also use this system to load external packages (i.e. neither Org
  270. core modules, nor modules from the CONTRIB directory). Just add symbols
  271. to the end of the list. If the package is called org-xyz.el, then you need
  272. to add the symbol `xyz', and the package must have a call to
  273. (provide 'org-xyz)"
  274. :group 'org
  275. :set 'org-set-modules
  276. :type
  277. '(set :greedy t
  278. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  279. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  280. (const :tag " crypt: Encryption of subtrees" org-crypt)
  281. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  282. (const :tag " docview: Links to doc-view buffers" org-docview)
  283. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  284. (const :tag " id: Global IDs for identifying entries" org-id)
  285. (const :tag " info: Links to Info nodes" org-info)
  286. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  287. (const :tag " habit: Track your consistency with habits" org-habit)
  288. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  289. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  290. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  291. (const :tag " mew Links to Mew folders/messages" org-mew)
  292. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  293. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  294. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  295. (const :tag " special-blocks: Turn blocks into LaTeX envs and HTML divs" org-special-blocks)
  296. (const :tag " vm: Links to VM folders/messages" org-vm)
  297. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  298. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  299. (const :tag " mouse: Additional mouse support" org-mouse)
  300. (const :tag " TaskJuggler: Export tasks to a TaskJuggler project" org-taskjuggler)
  301. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  302. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  303. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  304. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  305. (const :tag "C collector: Collect properties into tables" org-collector)
  306. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  307. (const :tag "C drill: Flashcards and spaced repetition for Org-mode" org-drill)
  308. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  309. (const :tag "C eshell Support for links to working directories in eshell" org-eshell)
  310. (const :tag "C eval: Include command output as text" org-eval)
  311. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  312. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  313. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  314. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  315. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  316. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  317. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  318. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  319. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  320. (const :tag "C notmuch: Provide org links to notmuch searches or messages" org-notmuch)
  321. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  322. (const :tag "C mac-link-grabber Grab links and URLs from various Mac applications" org-mac-link-grabber)
  323. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  324. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  325. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  326. (const :tag "C registry: A registry for Org-mode links" org-registry)
  327. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  328. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  329. (const :tag "C secretary: Team management with org-mode" org-secretary)
  330. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  331. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  332. (const :tag "C track: Keep up with Org-mode development" org-track)
  333. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  334. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  335. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  336. (defcustom org-support-shift-select nil
  337. "Non-nil means make shift-cursor commands select text when possible.
  338. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
  339. start selecting a region, or enlarge regions started in this way.
  340. In Org-mode, in special contexts, these same keys are used for
  341. other purposes, important enough to compete with shift selection.
  342. Org tries to balance these needs by supporting `shift-select-mode'
  343. outside these special contexts, under control of this variable.
  344. The default of this variable is nil, to avoid confusing behavior. Shifted
  345. cursor keys will then execute Org commands in the following contexts:
  346. - on a headline, changing TODO state (left/right) and priority (up/down)
  347. - on a time stamp, changing the time
  348. - in a plain list item, changing the bullet type
  349. - in a property definition line, switching between allowed values
  350. - in the BEGIN line of a clock table (changing the time block).
  351. Outside these contexts, the commands will throw an error.
  352. When this variable is t and the cursor is not in a special
  353. context, Org-mode will support shift-selection for making and
  354. enlarging regions. To make this more effective, the bullet
  355. cycling will no longer happen anywhere in an item line, but only
  356. if the cursor is exactly on the bullet.
  357. If you set this variable to the symbol `always', then the keys
  358. will not be special in headlines, property lines, and item lines,
  359. to make shift selection work there as well. If this is what you
  360. want, you can use the following alternative commands: `C-c C-t'
  361. and `C-c ,' to change TODO state and priority, `C-u C-u C-c C-t'
  362. can be used to switch TODO sets, `C-c -' to cycle item bullet
  363. types, and properties can be edited by hand or in column view.
  364. However, when the cursor is on a timestamp, shift-cursor commands
  365. will still edit the time stamp - this is just too good to give up.
  366. XEmacs user should have this variable set to nil, because
  367. `shift-select-mode' is in Emacs 23 or later only."
  368. :group 'org
  369. :type '(choice
  370. (const :tag "Never" nil)
  371. (const :tag "When outside special context" t)
  372. (const :tag "Everywhere except timestamps" always)))
  373. (defcustom org-loop-over-headlines-in-active-region nil
  374. "Shall some commands act upon headlines in the active region?
  375. When set to `t', some commands will be performed in all headlines
  376. within the active region.
  377. When set to `start-level', some commands will be performed in all
  378. headlines within the active region, provided that these headlines
  379. are of the same level than the first one.
  380. When set to a string, those commands will be performed on the
  381. matching headlines within the active region. Such string must be
  382. a tags/property/todo match as it is used in the agenda tags view.
  383. The list of commands is: `org-schedule', `org-deadline',
  384. `org-todo', `org-archive-subtree', `org-archive-set-tag' and
  385. `org-archive-to-archive-sibling'. The archiving commands skip
  386. already archived entries."
  387. :type '(choice (const :tag "Don't loop" nil)
  388. (const :tag "All headlines in active region" t)
  389. (const :tag "In active region, headlines at the same level than the first one" 'start-level)
  390. (string :tag "Tags/Property/Todo matcher"))
  391. :version "24.1"
  392. :group 'org-todo
  393. :group 'org-archive)
  394. (defgroup org-startup nil
  395. "Options concerning startup of Org-mode."
  396. :tag "Org Startup"
  397. :group 'org)
  398. (defcustom org-startup-folded t
  399. "Non-nil means entering Org-mode will switch to OVERVIEW.
  400. This can also be configured on a per-file basis by adding one of
  401. the following lines anywhere in the buffer:
  402. #+STARTUP: fold (or `overview', this is equivalent)
  403. #+STARTUP: nofold (or `showall', this is equivalent)
  404. #+STARTUP: content
  405. #+STARTUP: showeverything"
  406. :group 'org-startup
  407. :type '(choice
  408. (const :tag "nofold: show all" nil)
  409. (const :tag "fold: overview" t)
  410. (const :tag "content: all headlines" content)
  411. (const :tag "show everything, even drawers" showeverything)))
  412. (defcustom org-startup-truncated t
  413. "Non-nil means entering Org-mode will set `truncate-lines'.
  414. This is useful since some lines containing links can be very long and
  415. uninteresting. Also tables look terrible when wrapped."
  416. :group 'org-startup
  417. :type 'boolean)
  418. (defcustom org-startup-indented nil
  419. "Non-nil means turn on `org-indent-mode' on startup.
  420. This can also be configured on a per-file basis by adding one of
  421. the following lines anywhere in the buffer:
  422. #+STARTUP: indent
  423. #+STARTUP: noindent"
  424. :group 'org-structure
  425. :type '(choice
  426. (const :tag "Not" nil)
  427. (const :tag "Globally (slow on startup in large files)" t)))
  428. (defcustom org-use-sub-superscripts t
  429. "Non-nil means interpret \"_\" and \"^\" for export.
  430. When this option is turned on, you can use TeX-like syntax for sub- and
  431. superscripts. Several characters after \"_\" or \"^\" will be
  432. considered as a single item - so grouping with {} is normally not
  433. needed. For example, the following things will be parsed as single
  434. sub- or superscripts.
  435. 10^24 or 10^tau several digits will be considered 1 item.
  436. 10^-12 or 10^-tau a leading sign with digits or a word
  437. x^2-y^3 will be read as x^2 - y^3, because items are
  438. terminated by almost any nonword/nondigit char.
  439. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  440. Still, ambiguity is possible - so when in doubt use {} to enclose the
  441. sub/superscript. If you set this variable to the symbol `{}',
  442. the braces are *required* in order to trigger interpretations as
  443. sub/superscript. This can be helpful in documents that need \"_\"
  444. frequently in plain text.
  445. Not all export backends support this, but HTML does.
  446. This option can also be set with the +OPTIONS line, e.g. \"^:nil\"."
  447. :group 'org-startup
  448. :group 'org-export-translation
  449. :version "24.1"
  450. :type '(choice
  451. (const :tag "Always interpret" t)
  452. (const :tag "Only with braces" {})
  453. (const :tag "Never interpret" nil)))
  454. (if (fboundp 'defvaralias)
  455. (defvaralias 'org-export-with-sub-superscripts 'org-use-sub-superscripts))
  456. (defcustom org-startup-with-beamer-mode nil
  457. "Non-nil means turn on `org-beamer-mode' on startup.
  458. This can also be configured on a per-file basis by adding one of
  459. the following lines anywhere in the buffer:
  460. #+STARTUP: beamer"
  461. :group 'org-startup
  462. :version "24.1"
  463. :type 'boolean)
  464. (defcustom org-startup-align-all-tables nil
  465. "Non-nil means align all tables when visiting a file.
  466. This is useful when the column width in tables is forced with <N> cookies
  467. in table fields. Such tables will look correct only after the first re-align.
  468. This can also be configured on a per-file basis by adding one of
  469. the following lines anywhere in the buffer:
  470. #+STARTUP: align
  471. #+STARTUP: noalign"
  472. :group 'org-startup
  473. :type 'boolean)
  474. (defcustom org-startup-with-inline-images nil
  475. "Non-nil means show inline images when loading a new Org file.
  476. This can also be configured on a per-file basis by adding one of
  477. the following lines anywhere in the buffer:
  478. #+STARTUP: inlineimages
  479. #+STARTUP: noinlineimages"
  480. :group 'org-startup
  481. :version "24.1"
  482. :type 'boolean)
  483. (defcustom org-insert-mode-line-in-empty-file nil
  484. "Non-nil means insert the first line setting Org-mode in empty files.
  485. When the function `org-mode' is called interactively in an empty file, this
  486. normally means that the file name does not automatically trigger Org-mode.
  487. To ensure that the file will always be in Org-mode in the future, a
  488. line enforcing Org-mode will be inserted into the buffer, if this option
  489. has been set."
  490. :group 'org-startup
  491. :type 'boolean)
  492. (defcustom org-replace-disputed-keys nil
  493. "Non-nil means use alternative key bindings for some keys.
  494. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  495. These keys are also used by other packages like shift-selection-mode'
  496. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  497. If you want to use Org-mode together with one of these other modes,
  498. or more generally if you would like to move some Org-mode commands to
  499. other keys, set this variable and configure the keys with the variable
  500. `org-disputed-keys'.
  501. This option is only relevant at load-time of Org-mode, and must be set
  502. *before* org.el is loaded. Changing it requires a restart of Emacs to
  503. become effective."
  504. :group 'org-startup
  505. :type 'boolean)
  506. (defcustom org-use-extra-keys nil
  507. "Non-nil means use extra key sequence definitions for certain commands.
  508. This happens automatically if you run XEmacs or if `window-system'
  509. is nil. This variable lets you do the same manually. You must
  510. set it before loading org.
  511. Example: on Carbon Emacs 22 running graphically, with an external
  512. keyboard on a Powerbook, the default way of setting M-left might
  513. not work for either Alt or ESC. Setting this variable will make
  514. it work for ESC."
  515. :group 'org-startup
  516. :type 'boolean)
  517. (if (fboundp 'defvaralias)
  518. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  519. (defcustom org-disputed-keys
  520. '(([(shift up)] . [(meta p)])
  521. ([(shift down)] . [(meta n)])
  522. ([(shift left)] . [(meta -)])
  523. ([(shift right)] . [(meta +)])
  524. ([(control shift right)] . [(meta shift +)])
  525. ([(control shift left)] . [(meta shift -)]))
  526. "Keys for which Org-mode and other modes compete.
  527. This is an alist, cars are the default keys, second element specifies
  528. the alternative to use when `org-replace-disputed-keys' is t.
  529. Keys can be specified in any syntax supported by `define-key'.
  530. The value of this option takes effect only at Org-mode's startup,
  531. therefore you'll have to restart Emacs to apply it after changing."
  532. :group 'org-startup
  533. :type 'alist)
  534. (defun org-key (key)
  535. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  536. Or return the original if not disputed.
  537. Also apply the translations defined in `org-xemacs-key-equivalents'."
  538. (when org-replace-disputed-keys
  539. (let* ((nkey (key-description key))
  540. (x (org-find-if (lambda (x)
  541. (equal (key-description (car x)) nkey))
  542. org-disputed-keys)))
  543. (setq key (if x (cdr x) key))))
  544. (when (featurep 'xemacs)
  545. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  546. key)
  547. (defun org-find-if (predicate seq)
  548. (catch 'exit
  549. (while seq
  550. (if (funcall predicate (car seq))
  551. (throw 'exit (car seq))
  552. (pop seq)))))
  553. (defun org-defkey (keymap key def)
  554. "Define a key, possibly translated, as returned by `org-key'."
  555. (define-key keymap (org-key key) def))
  556. (defcustom org-ellipsis nil
  557. "The ellipsis to use in the Org-mode outline.
  558. When nil, just use the standard three dots. When a string, use that instead,
  559. When a face, use the standard 3 dots, but with the specified face.
  560. The change affects only Org-mode (which will then use its own display table).
  561. Changing this requires executing `M-x org-mode' in a buffer to become
  562. effective."
  563. :group 'org-startup
  564. :type '(choice (const :tag "Default" nil)
  565. (face :tag "Face" :value org-warning)
  566. (string :tag "String" :value "...#")))
  567. (defvar org-display-table nil
  568. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  569. (defgroup org-keywords nil
  570. "Keywords in Org-mode."
  571. :tag "Org Keywords"
  572. :group 'org)
  573. (defcustom org-deadline-string "DEADLINE:"
  574. "String to mark deadline entries.
  575. A deadline is this string, followed by a time stamp. Should be a word,
  576. terminated by a colon. You can insert a schedule keyword and
  577. a timestamp with \\[org-deadline].
  578. Changes become only effective after restarting Emacs."
  579. :group 'org-keywords
  580. :type 'string)
  581. (defcustom org-scheduled-string "SCHEDULED:"
  582. "String to mark scheduled TODO entries.
  583. A schedule is this string, followed by a time stamp. Should be a word,
  584. terminated by a colon. You can insert a schedule keyword and
  585. a timestamp with \\[org-schedule].
  586. Changes become only effective after restarting Emacs."
  587. :group 'org-keywords
  588. :type 'string)
  589. (defcustom org-closed-string "CLOSED:"
  590. "String used as the prefix for timestamps logging closing a TODO entry."
  591. :group 'org-keywords
  592. :type 'string)
  593. (defcustom org-clock-string "CLOCK:"
  594. "String used as prefix for timestamps clocking work hours on an item."
  595. :group 'org-keywords
  596. :type 'string)
  597. (defcustom org-comment-string "COMMENT"
  598. "Entries starting with this keyword will never be exported.
  599. An entry can be toggled between COMMENT and normal with
  600. \\[org-toggle-comment].
  601. Changes become only effective after restarting Emacs."
  602. :group 'org-keywords
  603. :type 'string)
  604. (defcustom org-quote-string "QUOTE"
  605. "Entries starting with this keyword will be exported in fixed-width font.
  606. Quoting applies only to the text in the entry following the headline, and does
  607. not extend beyond the next headline, even if that is lower level.
  608. An entry can be toggled between QUOTE and normal with
  609. \\[org-toggle-fixed-width-section]."
  610. :group 'org-keywords
  611. :type 'string)
  612. (defconst org-repeat-re
  613. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)"
  614. "Regular expression for specifying repeated events.
  615. After a match, group 1 contains the repeat expression.")
  616. (defgroup org-structure nil
  617. "Options concerning the general structure of Org-mode files."
  618. :tag "Org Structure"
  619. :group 'org)
  620. (defgroup org-reveal-location nil
  621. "Options about how to make context of a location visible."
  622. :tag "Org Reveal Location"
  623. :group 'org-structure)
  624. (defconst org-context-choice
  625. '(choice
  626. (const :tag "Always" t)
  627. (const :tag "Never" nil)
  628. (repeat :greedy t :tag "Individual contexts"
  629. (cons
  630. (choice :tag "Context"
  631. (const agenda)
  632. (const org-goto)
  633. (const occur-tree)
  634. (const tags-tree)
  635. (const link-search)
  636. (const mark-goto)
  637. (const bookmark-jump)
  638. (const isearch)
  639. (const default))
  640. (boolean))))
  641. "Contexts for the reveal options.")
  642. (defcustom org-show-hierarchy-above '((default . t))
  643. "Non-nil means show full hierarchy when revealing a location.
  644. Org-mode often shows locations in an org-mode file which might have
  645. been invisible before. When this is set, the hierarchy of headings
  646. above the exposed location is shown.
  647. Turning this off for example for sparse trees makes them very compact.
  648. Instead of t, this can also be an alist specifying this option for different
  649. contexts. Valid contexts are
  650. agenda when exposing an entry from the agenda
  651. org-goto when using the command `org-goto' on key C-c C-j
  652. occur-tree when using the command `org-occur' on key C-c /
  653. tags-tree when constructing a sparse tree based on tags matches
  654. link-search when exposing search matches associated with a link
  655. mark-goto when exposing the jump goal of a mark
  656. bookmark-jump when exposing a bookmark location
  657. isearch when exiting from an incremental search
  658. default default for all contexts not set explicitly"
  659. :group 'org-reveal-location
  660. :type org-context-choice)
  661. (defcustom org-show-following-heading '((default . nil))
  662. "Non-nil means show following heading when revealing a location.
  663. Org-mode often shows locations in an org-mode file which might have
  664. been invisible before. When this is set, the heading following the
  665. match is shown.
  666. Turning this off for example for sparse trees makes them very compact,
  667. but makes it harder to edit the location of the match. In such a case,
  668. use the command \\[org-reveal] to show more context.
  669. Instead of t, this can also be an alist specifying this option for different
  670. contexts. See `org-show-hierarchy-above' for valid contexts."
  671. :group 'org-reveal-location
  672. :type org-context-choice)
  673. (defcustom org-show-siblings '((default . nil) (isearch t))
  674. "Non-nil means show all sibling heading when revealing a location.
  675. Org-mode often shows locations in an org-mode file which might have
  676. been invisible before. When this is set, the sibling of the current entry
  677. heading are all made visible. If `org-show-hierarchy-above' is t,
  678. the same happens on each level of the hierarchy above the current entry.
  679. By default this is on for the isearch context, off for all other contexts.
  680. Turning this off for example for sparse trees makes them very compact,
  681. but makes it harder to edit the location of the match. In such a case,
  682. use the command \\[org-reveal] to show more context.
  683. Instead of t, this can also be an alist specifying this option for different
  684. contexts. See `org-show-hierarchy-above' for valid contexts."
  685. :group 'org-reveal-location
  686. :type org-context-choice)
  687. (defcustom org-show-entry-below '((default . nil))
  688. "Non-nil means show the entry below a headline when revealing a location.
  689. Org-mode often shows locations in an org-mode file which might have
  690. been invisible before. When this is set, the text below the headline that is
  691. exposed is also shown.
  692. By default this is off for all contexts.
  693. Instead of t, this can also be an alist specifying this option for different
  694. contexts. See `org-show-hierarchy-above' for valid contexts."
  695. :group 'org-reveal-location
  696. :type org-context-choice)
  697. (defcustom org-indirect-buffer-display 'other-window
  698. "How should indirect tree buffers be displayed?
  699. This applies to indirect buffers created with the commands
  700. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  701. Valid values are:
  702. current-window Display in the current window
  703. other-window Just display in another window.
  704. dedicated-frame Create one new frame, and re-use it each time.
  705. new-frame Make a new frame each time. Note that in this case
  706. previously-made indirect buffers are kept, and you need to
  707. kill these buffers yourself."
  708. :group 'org-structure
  709. :group 'org-agenda-windows
  710. :type '(choice
  711. (const :tag "In current window" current-window)
  712. (const :tag "In current frame, other window" other-window)
  713. (const :tag "Each time a new frame" new-frame)
  714. (const :tag "One dedicated frame" dedicated-frame)))
  715. (defcustom org-use-speed-commands nil
  716. "Non-nil means activate single letter commands at beginning of a headline.
  717. This may also be a function to test for appropriate locations where speed
  718. commands should be active."
  719. :group 'org-structure
  720. :type '(choice
  721. (const :tag "Never" nil)
  722. (const :tag "At beginning of headline stars" t)
  723. (function)))
  724. (defcustom org-speed-commands-user nil
  725. "Alist of additional speed commands.
  726. This list will be checked before `org-speed-commands-default'
  727. when the variable `org-use-speed-commands' is non-nil
  728. and when the cursor is at the beginning of a headline.
  729. The car if each entry is a string with a single letter, which must
  730. be assigned to `self-insert-command' in the global map.
  731. The cdr is either a command to be called interactively, a function
  732. to be called, or a form to be evaluated.
  733. An entry that is just a list with a single string will be interpreted
  734. as a descriptive headline that will be added when listing the speed
  735. commands in the Help buffer using the `?' speed command."
  736. :group 'org-structure
  737. :type '(repeat :value ("k" . ignore)
  738. (choice :value ("k" . ignore)
  739. (list :tag "Descriptive Headline" (string :tag "Headline"))
  740. (cons :tag "Letter and Command"
  741. (string :tag "Command letter")
  742. (choice
  743. (function)
  744. (sexp))))))
  745. (defgroup org-cycle nil
  746. "Options concerning visibility cycling in Org-mode."
  747. :tag "Org Cycle"
  748. :group 'org-structure)
  749. (defcustom org-cycle-skip-children-state-if-no-children t
  750. "Non-nil means skip CHILDREN state in entries that don't have any."
  751. :group 'org-cycle
  752. :type 'boolean)
  753. (defcustom org-cycle-max-level nil
  754. "Maximum level which should still be subject to visibility cycling.
  755. Levels higher than this will, for cycling, be treated as text, not a headline.
  756. When `org-odd-levels-only' is set, a value of N in this variable actually
  757. means 2N-1 stars as the limiting headline.
  758. When nil, cycle all levels.
  759. Note that the limiting level of cycling is also influenced by
  760. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  761. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  762. than its value."
  763. :group 'org-cycle
  764. :type '(choice
  765. (const :tag "No limit" nil)
  766. (integer :tag "Maximum level")))
  767. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK" "RESULTS")
  768. "Names of drawers. Drawers are not opened by cycling on the headline above.
  769. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  770. this:
  771. :DRAWERNAME:
  772. .....
  773. :END:
  774. The drawer \"PROPERTIES\" is special for capturing properties through
  775. the property API.
  776. Drawers can be defined on the per-file basis with a line like:
  777. #+DRAWERS: HIDDEN STATE PROPERTIES"
  778. :group 'org-structure
  779. :group 'org-cycle
  780. :type '(repeat (string :tag "Drawer Name")))
  781. (defcustom org-hide-block-startup nil
  782. "Non-nil means entering Org-mode will fold all blocks.
  783. This can also be set in on a per-file basis with
  784. #+STARTUP: hideblocks
  785. #+STARTUP: showblocks"
  786. :group 'org-startup
  787. :group 'org-cycle
  788. :type 'boolean)
  789. (defcustom org-cycle-global-at-bob nil
  790. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  791. This makes it possible to do global cycling without having to use S-TAB or
  792. \\[universal-argument] TAB. For this special case to work, the first line \
  793. of the buffer
  794. must not be a headline - it may be empty or some other text. When used in
  795. this way, `org-cycle-hook' is disables temporarily, to make sure the
  796. cursor stays at the beginning of the buffer.
  797. When this option is nil, don't do anything special at the beginning
  798. of the buffer."
  799. :group 'org-cycle
  800. :type 'boolean)
  801. (defcustom org-cycle-level-after-item/entry-creation t
  802. "Non-nil means cycle entry level or item indentation in new empty entries.
  803. When the cursor is at the end of an empty headline, i.e with only stars
  804. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  805. and then all possible ancestor states, before returning to the original state.
  806. This makes data entry extremely fast: M-RET to create a new headline,
  807. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  808. When the cursor is at the end of an empty plain list item, one TAB will
  809. make it a subitem, two or more tabs will back up to make this an item
  810. higher up in the item hierarchy."
  811. :group 'org-cycle
  812. :type 'boolean)
  813. (defcustom org-cycle-emulate-tab t
  814. "Where should `org-cycle' emulate TAB.
  815. nil Never
  816. white Only in completely white lines
  817. whitestart Only at the beginning of lines, before the first non-white char
  818. t Everywhere except in headlines
  819. exc-hl-bol Everywhere except at the start of a headline
  820. If TAB is used in a place where it does not emulate TAB, the current subtree
  821. visibility is cycled."
  822. :group 'org-cycle
  823. :type '(choice (const :tag "Never" nil)
  824. (const :tag "Only in completely white lines" white)
  825. (const :tag "Before first char in a line" whitestart)
  826. (const :tag "Everywhere except in headlines" t)
  827. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  828. ))
  829. (defcustom org-cycle-separator-lines 2
  830. "Number of empty lines needed to keep an empty line between collapsed trees.
  831. If you leave an empty line between the end of a subtree and the following
  832. headline, this empty line is hidden when the subtree is folded.
  833. Org-mode will leave (exactly) one empty line visible if the number of
  834. empty lines is equal or larger to the number given in this variable.
  835. So the default 2 means at least 2 empty lines after the end of a subtree
  836. are needed to produce free space between a collapsed subtree and the
  837. following headline.
  838. If the number is negative, and the number of empty lines is at least -N,
  839. all empty lines are shown.
  840. Special case: when 0, never leave empty lines in collapsed view."
  841. :group 'org-cycle
  842. :type 'integer)
  843. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  844. (defcustom org-pre-cycle-hook nil
  845. "Hook that is run before visibility cycling is happening.
  846. The function(s) in this hook must accept a single argument which indicates
  847. the new state that will be set right after running this hook. The
  848. argument is a symbol. Before a global state change, it can have the values
  849. `overview', `content', or `all'. Before a local state change, it can have
  850. the values `folded', `children', or `subtree'."
  851. :group 'org-cycle
  852. :type 'hook)
  853. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  854. org-cycle-hide-drawers
  855. org-cycle-show-empty-lines
  856. org-optimize-window-after-visibility-change)
  857. "Hook that is run after `org-cycle' has changed the buffer visibility.
  858. The function(s) in this hook must accept a single argument which indicates
  859. the new state that was set by the most recent `org-cycle' command. The
  860. argument is a symbol. After a global state change, it can have the values
  861. `overview', `content', or `all'. After a local state change, it can have
  862. the values `folded', `children', or `subtree'."
  863. :group 'org-cycle
  864. :type 'hook)
  865. (defgroup org-edit-structure nil
  866. "Options concerning structure editing in Org-mode."
  867. :tag "Org Edit Structure"
  868. :group 'org-structure)
  869. (defcustom org-odd-levels-only nil
  870. "Non-nil means skip even levels and only use odd levels for the outline.
  871. This has the effect that two stars are being added/taken away in
  872. promotion/demotion commands. It also influences how levels are
  873. handled by the exporters.
  874. Changing it requires restart of `font-lock-mode' to become effective
  875. for fontification also in regions already fontified.
  876. You may also set this on a per-file basis by adding one of the following
  877. lines to the buffer:
  878. #+STARTUP: odd
  879. #+STARTUP: oddeven"
  880. :group 'org-edit-structure
  881. :group 'org-appearance
  882. :type 'boolean)
  883. (defcustom org-adapt-indentation t
  884. "Non-nil means adapt indentation to outline node level.
  885. When this variable is set, Org assumes that you write outlines by
  886. indenting text in each node to align with the headline (after the stars).
  887. The following issues are influenced by this variable:
  888. - When this is set and the *entire* text in an entry is indented, the
  889. indentation is increased by one space in a demotion command, and
  890. decreased by one in a promotion command. If any line in the entry
  891. body starts with text at column 0, indentation is not changed at all.
  892. - Property drawers and planning information is inserted indented when
  893. this variable s set. When nil, they will not be indented.
  894. - TAB indents a line relative to context. The lines below a headline
  895. will be indented when this variable is set.
  896. Note that this is all about true indentation, by adding and removing
  897. space characters. See also `org-indent.el' which does level-dependent
  898. indentation in a virtual way, i.e. at display time in Emacs."
  899. :group 'org-edit-structure
  900. :type 'boolean)
  901. (defcustom org-special-ctrl-a/e nil
  902. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  903. When t, `C-a' will bring back the cursor to the beginning of the
  904. headline text, i.e. after the stars and after a possible TODO
  905. keyword. In an item, this will be the position after bullet and
  906. check-box, if any. When the cursor is already at that position,
  907. another `C-a' will bring it to the beginning of the line.
  908. `C-e' will jump to the end of the headline, ignoring the presence
  909. of tags in the headline. A second `C-e' will then jump to the
  910. true end of the line, after any tags. This also means that, when
  911. this variable is non-nil, `C-e' also will never jump beyond the
  912. end of the heading of a folded section, i.e. not after the
  913. ellipses.
  914. When set to the symbol `reversed', the first `C-a' or `C-e' works
  915. normally, going to the true line boundary first. Only a directly
  916. following, identical keypress will bring the cursor to the
  917. special positions.
  918. This may also be a cons cell where the behavior for `C-a' and
  919. `C-e' is set separately."
  920. :group 'org-edit-structure
  921. :type '(choice
  922. (const :tag "off" nil)
  923. (const :tag "on: after stars/bullet and before tags first" t)
  924. (const :tag "reversed: true line boundary first" reversed)
  925. (cons :tag "Set C-a and C-e separately"
  926. (choice :tag "Special C-a"
  927. (const :tag "off" nil)
  928. (const :tag "on: after stars/bullet first" t)
  929. (const :tag "reversed: before stars/bullet first" reversed))
  930. (choice :tag "Special C-e"
  931. (const :tag "off" nil)
  932. (const :tag "on: before tags first" t)
  933. (const :tag "reversed: after tags first" reversed)))))
  934. (if (fboundp 'defvaralias)
  935. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  936. (defcustom org-special-ctrl-k nil
  937. "Non-nil means `C-k' will behave specially in headlines.
  938. When nil, `C-k' will call the default `kill-line' command.
  939. When t, the following will happen while the cursor is in the headline:
  940. - When the cursor is at the beginning of a headline, kill the entire
  941. line and possible the folded subtree below the line.
  942. - When in the middle of the headline text, kill the headline up to the tags.
  943. - When after the headline text, kill the tags."
  944. :group 'org-edit-structure
  945. :type 'boolean)
  946. (defcustom org-ctrl-k-protect-subtree nil
  947. "Non-nil means, do not delete a hidden subtree with C-k.
  948. When set to the symbol `error', simply throw an error when C-k is
  949. used to kill (part-of) a headline that has hidden text behind it.
  950. Any other non-nil value will result in a query to the user, if it is
  951. OK to kill that hidden subtree. When nil, kill without remorse."
  952. :group 'org-edit-structure
  953. :version "24.1"
  954. :type '(choice
  955. (const :tag "Do not protect hidden subtrees" nil)
  956. (const :tag "Protect hidden subtrees with a security query" t)
  957. (const :tag "Never kill a hidden subtree with C-k" error)))
  958. (defcustom org-catch-invisible-edits nil
  959. "Check if in invisible region before inserting or deleting a character.
  960. Valid values are:
  961. nil Do not check, so just do invisible edits.
  962. error Throw an error and do nothing.
  963. show Make point visible, and do the requested edit.
  964. show-and-error Make point visible, then throw an error and abort the edit.
  965. smart Make point visible, and do insertion/deletion if it is
  966. adjacent to visible text and the change feels predictable.
  967. Never delete a previously invisible character or add in the
  968. middle or right after an invisible region. Basically, this
  969. allows insertion and backward-delete right before ellipses.
  970. FIXME: maybe in this case we should not even show?"
  971. :group 'org-edit-structure
  972. :version "24.1"
  973. :type '(choice
  974. (const :tag "Do not check" nil)
  975. (const :tag "Throw error when trying to edit" error)
  976. (const :tag "Unhide, but do not do the edit" show-and-error)
  977. (const :tag "Show invisible part and do the edit" show)
  978. (const :tag "Be smart and do the right thing" smart)))
  979. (defcustom org-yank-folded-subtrees t
  980. "Non-nil means when yanking subtrees, fold them.
  981. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  982. it starts with a heading and all other headings in it are either children
  983. or siblings, then fold all the subtrees. However, do this only if no
  984. text after the yank would be swallowed into a folded tree by this action."
  985. :group 'org-edit-structure
  986. :type 'boolean)
  987. (defcustom org-yank-adjusted-subtrees nil
  988. "Non-nil means when yanking subtrees, adjust the level.
  989. With this setting, `org-paste-subtree' is used to insert the subtree, see
  990. this function for details."
  991. :group 'org-edit-structure
  992. :type 'boolean)
  993. (defcustom org-M-RET-may-split-line '((default . t))
  994. "Non-nil means M-RET will split the line at the cursor position.
  995. When nil, it will go to the end of the line before making a
  996. new line.
  997. You may also set this option in a different way for different
  998. contexts. Valid contexts are:
  999. headline when creating a new headline
  1000. item when creating a new item
  1001. table in a table field
  1002. default the value to be used for all contexts not explicitly
  1003. customized"
  1004. :group 'org-structure
  1005. :group 'org-table
  1006. :type '(choice
  1007. (const :tag "Always" t)
  1008. (const :tag "Never" nil)
  1009. (repeat :greedy t :tag "Individual contexts"
  1010. (cons
  1011. (choice :tag "Context"
  1012. (const headline)
  1013. (const item)
  1014. (const table)
  1015. (const default))
  1016. (boolean)))))
  1017. (defcustom org-insert-heading-respect-content nil
  1018. "Non-nil means insert new headings after the current subtree.
  1019. When nil, the new heading is created directly after the current line.
  1020. The commands \\[org-insert-heading-respect-content] and
  1021. \\[org-insert-todo-heading-respect-content] turn this variable on
  1022. for the duration of the command."
  1023. :group 'org-structure
  1024. :type 'boolean)
  1025. (defcustom org-blank-before-new-entry '((heading . auto)
  1026. (plain-list-item . auto))
  1027. "Should `org-insert-heading' leave a blank line before new heading/item?
  1028. The value is an alist, with `heading' and `plain-list-item' as CAR,
  1029. and a boolean flag as CDR. The cdr may also be the symbol `auto', in
  1030. which case Org will look at the surrounding headings/items and try to
  1031. make an intelligent decision whether to insert a blank line or not.
  1032. For plain lists, if the variable `org-empty-line-terminates-plain-lists' is
  1033. set, the setting here is ignored and no empty line is inserted, to avoid
  1034. breaking the list structure."
  1035. :group 'org-edit-structure
  1036. :type '(list
  1037. (cons (const heading)
  1038. (choice (const :tag "Never" nil)
  1039. (const :tag "Always" t)
  1040. (const :tag "Auto" auto)))
  1041. (cons (const plain-list-item)
  1042. (choice (const :tag "Never" nil)
  1043. (const :tag "Always" t)
  1044. (const :tag "Auto" auto)))))
  1045. (defcustom org-insert-heading-hook nil
  1046. "Hook being run after inserting a new heading."
  1047. :group 'org-edit-structure
  1048. :type 'hook)
  1049. (defcustom org-enable-fixed-width-editor t
  1050. "Non-nil means lines starting with \":\" are treated as fixed-width.
  1051. This currently only means they are never auto-wrapped.
  1052. When nil, such lines will be treated like ordinary lines.
  1053. See also the QUOTE keyword."
  1054. :group 'org-edit-structure
  1055. :type 'boolean)
  1056. (defcustom org-goto-auto-isearch t
  1057. "Non-nil means typing characters in `org-goto' starts incremental search."
  1058. :group 'org-edit-structure
  1059. :type 'boolean)
  1060. (defgroup org-sparse-trees nil
  1061. "Options concerning sparse trees in Org-mode."
  1062. :tag "Org Sparse Trees"
  1063. :group 'org-structure)
  1064. (defcustom org-highlight-sparse-tree-matches t
  1065. "Non-nil means highlight all matches that define a sparse tree.
  1066. The highlights will automatically disappear the next time the buffer is
  1067. changed by an edit command."
  1068. :group 'org-sparse-trees
  1069. :type 'boolean)
  1070. (defcustom org-remove-highlights-with-change t
  1071. "Non-nil means any change to the buffer will remove temporary highlights.
  1072. Such highlights are created by `org-occur' and `org-clock-display'.
  1073. When nil, `C-c C-c needs to be used to get rid of the highlights.
  1074. The highlights created by `org-preview-latex-fragment' always need
  1075. `C-c C-c' to be removed."
  1076. :group 'org-sparse-trees
  1077. :group 'org-time
  1078. :type 'boolean)
  1079. (defcustom org-occur-hook '(org-first-headline-recenter)
  1080. "Hook that is run after `org-occur' has constructed a sparse tree.
  1081. This can be used to recenter the window to show as much of the structure
  1082. as possible."
  1083. :group 'org-sparse-trees
  1084. :type 'hook)
  1085. (defgroup org-imenu-and-speedbar nil
  1086. "Options concerning imenu and speedbar in Org-mode."
  1087. :tag "Org Imenu and Speedbar"
  1088. :group 'org-structure)
  1089. (defcustom org-imenu-depth 2
  1090. "The maximum level for Imenu access to Org-mode headlines.
  1091. This also applied for speedbar access."
  1092. :group 'org-imenu-and-speedbar
  1093. :type 'integer)
  1094. (defgroup org-table nil
  1095. "Options concerning tables in Org-mode."
  1096. :tag "Org Table"
  1097. :group 'org)
  1098. (defcustom org-enable-table-editor 'optimized
  1099. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1100. When nil, such lines will be treated like ordinary lines.
  1101. When equal to the symbol `optimized', the table editor will be optimized to
  1102. do the following:
  1103. - Automatic overwrite mode in front of whitespace in table fields.
  1104. This makes the structure of the table stay in tact as long as the edited
  1105. field does not exceed the column width.
  1106. - Minimize the number of realigns. Normally, the table is aligned each time
  1107. TAB or RET are pressed to move to another field. With optimization this
  1108. happens only if changes to a field might have changed the column width.
  1109. Optimization requires replacing the functions `self-insert-command',
  1110. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1111. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1112. very good at guessing when a re-align will be necessary, but you can always
  1113. force one with \\[org-ctrl-c-ctrl-c].
  1114. If you would like to use the optimized version in Org-mode, but the
  1115. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1116. This variable can be used to turn on and off the table editor during a session,
  1117. but in order to toggle optimization, a restart is required.
  1118. See also the variable `org-table-auto-blank-field'."
  1119. :group 'org-table
  1120. :type '(choice
  1121. (const :tag "off" nil)
  1122. (const :tag "on" t)
  1123. (const :tag "on, optimized" optimized)))
  1124. (defcustom org-self-insert-cluster-for-undo t
  1125. "Non-nil means cluster self-insert commands for undo when possible.
  1126. If this is set, then, like in the Emacs command loop, 20 consecutive
  1127. characters will be undone together.
  1128. This is configurable, because there is some impact on typing performance."
  1129. :group 'org-table
  1130. :type 'boolean)
  1131. (defcustom org-table-tab-recognizes-table.el t
  1132. "Non-nil means TAB will automatically notice a table.el table.
  1133. When it sees such a table, it moves point into it and - if necessary -
  1134. calls `table-recognize-table'."
  1135. :group 'org-table-editing
  1136. :type 'boolean)
  1137. (defgroup org-link nil
  1138. "Options concerning links in Org-mode."
  1139. :tag "Org Link"
  1140. :group 'org)
  1141. (defvar org-link-abbrev-alist-local nil
  1142. "Buffer-local version of `org-link-abbrev-alist', which see.
  1143. The value of this is taken from the #+LINK lines.")
  1144. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1145. (defcustom org-link-abbrev-alist nil
  1146. "Alist of link abbreviations.
  1147. The car of each element is a string, to be replaced at the start of a link.
  1148. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1149. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1150. [[linkkey:tag][description]]
  1151. The 'linkkey' must be a word word, starting with a letter, followed
  1152. by letters, numbers, '-' or '_'.
  1153. If REPLACE is a string, the tag will simply be appended to create the link.
  1154. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  1155. the placeholder \"%h\" will cause a url-encoded version of the tag to
  1156. be inserted at that point (see the function `url-hexify-string').
  1157. REPLACE may also be a function that will be called with the tag as the
  1158. only argument to create the link, which should be returned as a string.
  1159. See the manual for examples."
  1160. :group 'org-link
  1161. :type '(repeat
  1162. (cons
  1163. (string :tag "Protocol")
  1164. (choice
  1165. (string :tag "Format")
  1166. (function)))))
  1167. (defcustom org-descriptive-links t
  1168. "Non-nil means Org will display descriptive links.
  1169. E.g. [[http://orgmode.org][Org website]] will be displayed as
  1170. \"Org Website\", hiding the link itself and just displaying its
  1171. description. When set to `nil', Org will display the full links
  1172. literally.
  1173. You can interactively set the value of this variable by calling
  1174. `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
  1175. :group 'org-link
  1176. :type 'boolean)
  1177. (defcustom org-link-file-path-type 'adaptive
  1178. "How the path name in file links should be stored.
  1179. Valid values are:
  1180. relative Relative to the current directory, i.e. the directory of the file
  1181. into which the link is being inserted.
  1182. absolute Absolute path, if possible with ~ for home directory.
  1183. noabbrev Absolute path, no abbreviation of home directory.
  1184. adaptive Use relative path for files in the current directory and sub-
  1185. directories of it. For other files, use an absolute path."
  1186. :group 'org-link
  1187. :type '(choice
  1188. (const relative)
  1189. (const absolute)
  1190. (const noabbrev)
  1191. (const adaptive)))
  1192. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  1193. "Types of links that should be activated in Org-mode files.
  1194. This is a list of symbols, each leading to the activation of a certain link
  1195. type. In principle, it does not hurt to turn on most link types - there may
  1196. be a small gain when turning off unused link types. The types are:
  1197. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1198. angle Links in angular brackets that may contain whitespace like
  1199. <bbdb:Carsten Dominik>.
  1200. plain Plain links in normal text, no whitespace, like http://google.com.
  1201. radio Text that is matched by a radio target, see manual for details.
  1202. tag Tag settings in a headline (link to tag search).
  1203. date Time stamps (link to calendar).
  1204. footnote Footnote labels.
  1205. Changing this variable requires a restart of Emacs to become effective."
  1206. :group 'org-link
  1207. :type '(set :greedy t
  1208. (const :tag "Double bracket links" bracket)
  1209. (const :tag "Angular bracket links" angle)
  1210. (const :tag "Plain text links" plain)
  1211. (const :tag "Radio target matches" radio)
  1212. (const :tag "Tags" tag)
  1213. (const :tag "Timestamps" date)
  1214. (const :tag "Footnotes" footnote)))
  1215. (defcustom org-make-link-description-function nil
  1216. "Function to use to generate link descriptions from links.
  1217. If nil the link location will be used. This function must take
  1218. two parameters; the first is the link and the second the
  1219. description `org-insert-link' has generated, and should return the
  1220. description to use."
  1221. :group 'org-link
  1222. :type 'function)
  1223. (defgroup org-link-store nil
  1224. "Options concerning storing links in Org-mode."
  1225. :tag "Org Store Link"
  1226. :group 'org-link)
  1227. (defcustom org-email-link-description-format "Email %c: %.30s"
  1228. "Format of the description part of a link to an email or usenet message.
  1229. The following %-escapes will be replaced by corresponding information:
  1230. %F full \"From\" field
  1231. %f name, taken from \"From\" field, address if no name
  1232. %T full \"To\" field
  1233. %t first name in \"To\" field, address if no name
  1234. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1235. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1236. %s subject
  1237. %d date
  1238. %m message-id.
  1239. You may use normal field width specification between the % and the letter.
  1240. This is for example useful to limit the length of the subject.
  1241. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1242. :group 'org-link-store
  1243. :type 'string)
  1244. (defcustom org-from-is-user-regexp
  1245. (let (r1 r2)
  1246. (when (and user-mail-address (not (string= user-mail-address "")))
  1247. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1248. (when (and user-full-name (not (string= user-full-name "")))
  1249. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1250. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1251. "Regexp matched against the \"From:\" header of an email or usenet message.
  1252. It should match if the message is from the user him/herself."
  1253. :group 'org-link-store
  1254. :type 'regexp)
  1255. (defcustom org-link-to-org-use-id 'create-if-interactive-and-no-custom-id
  1256. "Non-nil means storing a link to an Org file will use entry IDs.
  1257. Note that before this variable is even considered, org-id must be loaded,
  1258. so please customize `org-modules' and turn it on.
  1259. The variable can have the following values:
  1260. t Create an ID if needed to make a link to the current entry.
  1261. create-if-interactive
  1262. If `org-store-link' is called directly (interactively, as a user
  1263. command), do create an ID to support the link. But when doing the
  1264. job for remember, only use the ID if it already exists. The
  1265. purpose of this setting is to avoid proliferation of unwanted
  1266. IDs, just because you happen to be in an Org file when you
  1267. call `org-remember' that automatically and preemptively
  1268. creates a link. If you do want to get an ID link in a remember
  1269. template to an entry not having an ID, create it first by
  1270. explicitly creating a link to it, using `C-c C-l' first.
  1271. create-if-interactive-and-no-custom-id
  1272. Like create-if-interactive, but do not create an ID if there is
  1273. a CUSTOM_ID property defined in the entry. This is the default.
  1274. use-existing
  1275. Use existing ID, do not create one.
  1276. nil Never use an ID to make a link, instead link using a text search for
  1277. the headline text."
  1278. :group 'org-link-store
  1279. :type '(choice
  1280. (const :tag "Create ID to make link" t)
  1281. (const :tag "Create if storing link interactively"
  1282. create-if-interactive)
  1283. (const :tag "Create if storing link interactively and no CUSTOM_ID is present"
  1284. create-if-interactive-and-no-custom-id)
  1285. (const :tag "Only use existing" use-existing)
  1286. (const :tag "Do not use ID to create link" nil)))
  1287. (defcustom org-context-in-file-links t
  1288. "Non-nil means file links from `org-store-link' contain context.
  1289. A search string will be added to the file name with :: as separator and
  1290. used to find the context when the link is activated by the command
  1291. `org-open-at-point'. When this option is t, the entire active region
  1292. will be placed in the search string of the file link. If set to a
  1293. positive integer, only the first n lines of context will be stored.
  1294. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1295. negates this setting for the duration of the command."
  1296. :group 'org-link-store
  1297. :type '(choice boolean integer))
  1298. (defcustom org-keep-stored-link-after-insertion nil
  1299. "Non-nil means keep link in list for entire session.
  1300. The command `org-store-link' adds a link pointing to the current
  1301. location to an internal list. These links accumulate during a session.
  1302. The command `org-insert-link' can be used to insert links into any
  1303. Org-mode file (offering completion for all stored links). When this
  1304. option is nil, every link which has been inserted once using \\[org-insert-link]
  1305. will be removed from the list, to make completing the unused links
  1306. more efficient."
  1307. :group 'org-link-store
  1308. :type 'boolean)
  1309. (defgroup org-link-follow nil
  1310. "Options concerning following links in Org-mode."
  1311. :tag "Org Follow Link"
  1312. :group 'org-link)
  1313. (defcustom org-link-translation-function nil
  1314. "Function to translate links with different syntax to Org syntax.
  1315. This can be used to translate links created for example by the Planner
  1316. or emacs-wiki packages to Org syntax.
  1317. The function must accept two parameters, a TYPE containing the link
  1318. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1319. which is everything after the link protocol. It should return a cons
  1320. with possibly modified values of type and path.
  1321. Org contains a function for this, so if you set this variable to
  1322. `org-translate-link-from-planner', you should be able follow many
  1323. links created by planner."
  1324. :group 'org-link-follow
  1325. :type 'function)
  1326. (defcustom org-follow-link-hook nil
  1327. "Hook that is run after a link has been followed."
  1328. :group 'org-link-follow
  1329. :type 'hook)
  1330. (defcustom org-tab-follows-link nil
  1331. "Non-nil means on links TAB will follow the link.
  1332. Needs to be set before org.el is loaded.
  1333. This really should not be used, it does not make sense, and the
  1334. implementation is bad."
  1335. :group 'org-link-follow
  1336. :type 'boolean)
  1337. (defcustom org-return-follows-link nil
  1338. "Non-nil means on links RET will follow the link."
  1339. :group 'org-link-follow
  1340. :type 'boolean)
  1341. (defcustom org-mouse-1-follows-link
  1342. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1343. "Non-nil means mouse-1 on a link will follow the link.
  1344. A longer mouse click will still set point. Does not work on XEmacs.
  1345. Needs to be set before org.el is loaded."
  1346. :group 'org-link-follow
  1347. :type 'boolean)
  1348. (defcustom org-mark-ring-length 4
  1349. "Number of different positions to be recorded in the ring.
  1350. Changing this requires a restart of Emacs to work correctly."
  1351. :group 'org-link-follow
  1352. :type 'integer)
  1353. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1354. "Non-nil means internal links in Org files must exactly match a headline.
  1355. When nil, the link search tries to match a phrase with all words
  1356. in the search text."
  1357. :group 'org-link-follow
  1358. :version "24.1"
  1359. :type '(choice
  1360. (const :tag "Use fuzzy text search" nil)
  1361. (const :tag "Match only exact headline" t)
  1362. (const :tag "Match exact headline or query to create it"
  1363. query-to-create)))
  1364. (defcustom org-link-frame-setup
  1365. '((vm . vm-visit-folder-other-frame)
  1366. (vm-imap . vm-visit-imap-folder-other-frame)
  1367. (gnus . org-gnus-no-new-news)
  1368. (file . find-file-other-window)
  1369. (wl . wl-other-frame))
  1370. "Setup the frame configuration for following links.
  1371. When following a link with Emacs, it may often be useful to display
  1372. this link in another window or frame. This variable can be used to
  1373. set this up for the different types of links.
  1374. For VM, use any of
  1375. `vm-visit-folder'
  1376. `vm-visit-folder-other-window'
  1377. `vm-visit-folder-other-frame'
  1378. For Gnus, use any of
  1379. `gnus'
  1380. `gnus-other-frame'
  1381. `org-gnus-no-new-news'
  1382. For FILE, use any of
  1383. `find-file'
  1384. `find-file-other-window'
  1385. `find-file-other-frame'
  1386. For Wanderlust use any of
  1387. `wl'
  1388. `wl-other-frame'
  1389. For the calendar, use the variable `calendar-setup'.
  1390. For BBDB, it is currently only possible to display the matches in
  1391. another window."
  1392. :group 'org-link-follow
  1393. :type '(list
  1394. (cons (const vm)
  1395. (choice
  1396. (const vm-visit-folder)
  1397. (const vm-visit-folder-other-window)
  1398. (const vm-visit-folder-other-frame)))
  1399. (cons (const gnus)
  1400. (choice
  1401. (const gnus)
  1402. (const gnus-other-frame)
  1403. (const org-gnus-no-new-news)))
  1404. (cons (const file)
  1405. (choice
  1406. (const find-file)
  1407. (const find-file-other-window)
  1408. (const find-file-other-frame)))
  1409. (cons (const wl)
  1410. (choice
  1411. (const wl)
  1412. (const wl-other-frame)))))
  1413. (defcustom org-display-internal-link-with-indirect-buffer nil
  1414. "Non-nil means use indirect buffer to display infile links.
  1415. Activating internal links (from one location in a file to another location
  1416. in the same file) normally just jumps to the location. When the link is
  1417. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1418. is displayed in
  1419. another window. When this option is set, the other window actually displays
  1420. an indirect buffer clone of the current buffer, to avoid any visibility
  1421. changes to the current buffer."
  1422. :group 'org-link-follow
  1423. :type 'boolean)
  1424. (defcustom org-open-non-existing-files nil
  1425. "Non-nil means `org-open-file' will open non-existing files.
  1426. When nil, an error will be generated.
  1427. This variable applies only to external applications because they
  1428. might choke on non-existing files. If the link is to a file that
  1429. will be opened in Emacs, the variable is ignored."
  1430. :group 'org-link-follow
  1431. :type 'boolean)
  1432. (defcustom org-open-directory-means-index-dot-org nil
  1433. "Non-nil means a link to a directory really means to index.org.
  1434. When nil, following a directory link will run dired or open a finder/explorer
  1435. window on that directory."
  1436. :group 'org-link-follow
  1437. :type 'boolean)
  1438. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1439. "Function and arguments to call for following mailto links.
  1440. This is a list with the first element being a Lisp function, and the
  1441. remaining elements being arguments to the function. In string arguments,
  1442. %a will be replaced by the address, and %s will be replaced by the subject
  1443. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1444. :group 'org-link-follow
  1445. :type '(choice
  1446. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1447. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1448. (const :tag "message-mail" (message-mail "%a" "%s"))
  1449. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1450. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1451. "Non-nil means ask for confirmation before executing shell links.
  1452. Shell links can be dangerous: just think about a link
  1453. [[shell:rm -rf ~/*][Google Search]]
  1454. This link would show up in your Org-mode document as \"Google Search\",
  1455. but really it would remove your entire home directory.
  1456. Therefore we advise against setting this variable to nil.
  1457. Just change it to `y-or-n-p' if you want to confirm with a
  1458. single keystroke rather than having to type \"yes\"."
  1459. :group 'org-link-follow
  1460. :type '(choice
  1461. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1462. (const :tag "with y-or-n (faster)" y-or-n-p)
  1463. (const :tag "no confirmation (dangerous)" nil)))
  1464. (put 'org-confirm-shell-link-function
  1465. 'safe-local-variable
  1466. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1467. (defcustom org-confirm-shell-link-not-regexp ""
  1468. "A regexp to skip confirmation for shell links."
  1469. :group 'org-link-follow
  1470. :version "24.1"
  1471. :type 'regexp)
  1472. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1473. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1474. Elisp links can be dangerous: just think about a link
  1475. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1476. This link would show up in your Org-mode document as \"Google Search\",
  1477. but really it would remove your entire home directory.
  1478. Therefore we advise against setting this variable to nil.
  1479. Just change it to `y-or-n-p' if you want to confirm with a
  1480. single keystroke rather than having to type \"yes\"."
  1481. :group 'org-link-follow
  1482. :type '(choice
  1483. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1484. (const :tag "with y-or-n (faster)" y-or-n-p)
  1485. (const :tag "no confirmation (dangerous)" nil)))
  1486. (put 'org-confirm-shell-link-function
  1487. 'safe-local-variable
  1488. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1489. (defcustom org-confirm-elisp-link-not-regexp ""
  1490. "A regexp to skip confirmation for Elisp links."
  1491. :group 'org-link-follow
  1492. :version "24.1"
  1493. :type 'regexp)
  1494. (defconst org-file-apps-defaults-gnu
  1495. '((remote . emacs)
  1496. (system . mailcap)
  1497. (t . mailcap))
  1498. "Default file applications on a UNIX or GNU/Linux system.
  1499. See `org-file-apps'.")
  1500. (defconst org-file-apps-defaults-macosx
  1501. '((remote . emacs)
  1502. (t . "open %s")
  1503. (system . "open %s")
  1504. ("ps.gz" . "gv %s")
  1505. ("eps.gz" . "gv %s")
  1506. ("dvi" . "xdvi %s")
  1507. ("fig" . "xfig %s"))
  1508. "Default file applications on a MacOS X system.
  1509. The system \"open\" is known as a default, but we use X11 applications
  1510. for some files for which the OS does not have a good default.
  1511. See `org-file-apps'.")
  1512. (defconst org-file-apps-defaults-windowsnt
  1513. (list
  1514. '(remote . emacs)
  1515. (cons t
  1516. (list (if (featurep 'xemacs)
  1517. 'mswindows-shell-execute
  1518. 'w32-shell-execute)
  1519. "open" 'file))
  1520. (cons 'system
  1521. (list (if (featurep 'xemacs)
  1522. 'mswindows-shell-execute
  1523. 'w32-shell-execute)
  1524. "open" 'file)))
  1525. "Default file applications on a Windows NT system.
  1526. The system \"open\" is used for most files.
  1527. See `org-file-apps'.")
  1528. (defcustom org-file-apps
  1529. '(
  1530. (auto-mode . emacs)
  1531. ("\\.mm\\'" . default)
  1532. ("\\.x?html?\\'" . default)
  1533. ("\\.pdf\\'" . default)
  1534. )
  1535. "External applications for opening `file:path' items in a document.
  1536. Org-mode uses system defaults for different file types, but
  1537. you can use this variable to set the application for a given file
  1538. extension. The entries in this list are cons cells where the car identifies
  1539. files and the cdr the corresponding command. Possible values for the
  1540. file identifier are
  1541. \"string\" A string as a file identifier can be interpreted in different
  1542. ways, depending on its contents:
  1543. - Alphanumeric characters only:
  1544. Match links with this file extension.
  1545. Example: (\"pdf\" . \"evince %s\")
  1546. to open PDFs with evince.
  1547. - Regular expression: Match links where the
  1548. filename matches the regexp. If you want to
  1549. use groups here, use shy groups.
  1550. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1551. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1552. to open *.html and *.xhtml with firefox.
  1553. - Regular expression which contains (non-shy) groups:
  1554. Match links where the whole link, including \"::\", and
  1555. anything after that, matches the regexp.
  1556. In a custom command string, %1, %2, etc. are replaced with
  1557. the parts of the link that were matched by the groups.
  1558. For backwards compatibility, if a command string is given
  1559. that does not use any of the group matches, this case is
  1560. handled identically to the second one (i.e. match against
  1561. file name only).
  1562. In a custom lisp form, you can access the group matches with
  1563. (match-string n link).
  1564. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1565. to open [[file:document.pdf::5]] with evince at page 5.
  1566. `directory' Matches a directory
  1567. `remote' Matches a remote file, accessible through tramp or efs.
  1568. Remote files most likely should be visited through Emacs
  1569. because external applications cannot handle such paths.
  1570. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1571. so all files Emacs knows how to handle. Using this with
  1572. command `emacs' will open most files in Emacs. Beware that this
  1573. will also open html files inside Emacs, unless you add
  1574. (\"html\" . default) to the list as well.
  1575. t Default for files not matched by any of the other options.
  1576. `system' The system command to open files, like `open' on Windows
  1577. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1578. that will be selected if you call `C-c C-o' with a double
  1579. \\[universal-argument] \\[universal-argument] prefix.
  1580. Possible values for the command are:
  1581. `emacs' The file will be visited by the current Emacs process.
  1582. `default' Use the default application for this file type, which is the
  1583. association for t in the list, most likely in the system-specific
  1584. part.
  1585. This can be used to overrule an unwanted setting in the
  1586. system-specific variable.
  1587. `system' Use the system command for opening files, like \"open\".
  1588. This command is specified by the entry whose car is `system'.
  1589. Most likely, the system-specific version of this variable
  1590. does define this command, but you can overrule/replace it
  1591. here.
  1592. string A command to be executed by a shell; %s will be replaced
  1593. by the path to the file.
  1594. sexp A Lisp form which will be evaluated. The file path will
  1595. be available in the Lisp variable `file'.
  1596. For more examples, see the system specific constants
  1597. `org-file-apps-defaults-macosx'
  1598. `org-file-apps-defaults-windowsnt'
  1599. `org-file-apps-defaults-gnu'."
  1600. :group 'org-link-follow
  1601. :type '(repeat
  1602. (cons (choice :value ""
  1603. (string :tag "Extension")
  1604. (const :tag "System command to open files" system)
  1605. (const :tag "Default for unrecognized files" t)
  1606. (const :tag "Remote file" remote)
  1607. (const :tag "Links to a directory" directory)
  1608. (const :tag "Any files that have Emacs modes"
  1609. auto-mode))
  1610. (choice :value ""
  1611. (const :tag "Visit with Emacs" emacs)
  1612. (const :tag "Use default" default)
  1613. (const :tag "Use the system command" system)
  1614. (string :tag "Command")
  1615. (sexp :tag "Lisp form")))))
  1616. (defgroup org-refile nil
  1617. "Options concerning refiling entries in Org-mode."
  1618. :tag "Org Refile"
  1619. :group 'org)
  1620. (defcustom org-directory "~/org"
  1621. "Directory with org files.
  1622. This is just a default location to look for Org files. There is no need
  1623. at all to put your files into this directory. It is only used in the
  1624. following situations:
  1625. 1. When a remember template specifies a target file that is not an
  1626. absolute path. The path will then be interpreted relative to
  1627. `org-directory'
  1628. 2. When a remember note is filed away in an interactive way (when exiting the
  1629. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1630. with `org-directory' as the default path."
  1631. :group 'org-refile
  1632. :group 'org-remember
  1633. :type 'directory)
  1634. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1635. "Default target for storing notes.
  1636. Used as a fall back file for org-remember.el and org-capture.el, for
  1637. templates that do not specify a target file."
  1638. :group 'org-refile
  1639. :group 'org-remember
  1640. :type '(choice
  1641. (const :tag "Default from remember-data-file" nil)
  1642. file))
  1643. (defcustom org-goto-interface 'outline
  1644. "The default interface to be used for `org-goto'.
  1645. Allowed values are:
  1646. outline The interface shows an outline of the relevant file
  1647. and the correct heading is found by moving through
  1648. the outline or by searching with incremental search.
  1649. outline-path-completion Headlines in the current buffer are offered via
  1650. completion. This is the interface also used by
  1651. the refile command."
  1652. :group 'org-refile
  1653. :type '(choice
  1654. (const :tag "Outline" outline)
  1655. (const :tag "Outline-path-completion" outline-path-completion)))
  1656. (defcustom org-goto-max-level 5
  1657. "Maximum target level when running `org-goto' with refile interface."
  1658. :group 'org-refile
  1659. :type 'integer)
  1660. (defcustom org-reverse-note-order nil
  1661. "Non-nil means store new notes at the beginning of a file or entry.
  1662. When nil, new notes will be filed to the end of a file or entry.
  1663. This can also be a list with cons cells of regular expressions that
  1664. are matched against file names, and values."
  1665. :group 'org-remember
  1666. :group 'org-refile
  1667. :type '(choice
  1668. (const :tag "Reverse always" t)
  1669. (const :tag "Reverse never" nil)
  1670. (repeat :tag "By file name regexp"
  1671. (cons regexp boolean))))
  1672. (defcustom org-log-refile nil
  1673. "Information to record when a task is refiled.
  1674. Possible values are:
  1675. nil Don't add anything
  1676. time Add a time stamp to the task
  1677. note Prompt for a note and add it with template `org-log-note-headings'
  1678. This option can also be set with on a per-file-basis with
  1679. #+STARTUP: nologrefile
  1680. #+STARTUP: logrefile
  1681. #+STARTUP: lognoterefile
  1682. You can have local logging settings for a subtree by setting the LOGGING
  1683. property to one or more of these keywords.
  1684. When bulk-refiling from the agenda, the value `note' is forbidden and
  1685. will temporarily be changed to `time'."
  1686. :group 'org-refile
  1687. :group 'org-progress
  1688. :version "24.1"
  1689. :type '(choice
  1690. (const :tag "No logging" nil)
  1691. (const :tag "Record timestamp" time)
  1692. (const :tag "Record timestamp with note." note)))
  1693. (defcustom org-refile-targets nil
  1694. "Targets for refiling entries with \\[org-refile].
  1695. This is a list of cons cells. Each cell contains:
  1696. - a specification of the files to be considered, either a list of files,
  1697. or a symbol whose function or variable value will be used to retrieve
  1698. a file name or a list of file names. If you use `org-agenda-files' for
  1699. that, all agenda files will be scanned for targets. Nil means consider
  1700. headings in the current buffer.
  1701. - A specification of how to find candidate refile targets. This may be
  1702. any of:
  1703. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1704. This tag has to be present in all target headlines, inheritance will
  1705. not be considered.
  1706. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1707. todo keyword.
  1708. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1709. headlines that are refiling targets.
  1710. - a cons cell (:level . N). Any headline of level N is considered a target.
  1711. Note that, when `org-odd-levels-only' is set, level corresponds to
  1712. order in hierarchy, not to the number of stars.
  1713. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1714. Note that, when `org-odd-levels-only' is set, level corresponds to
  1715. order in hierarchy, not to the number of stars.
  1716. Each element of this list generates a set of possible targets.
  1717. The union of these sets is presented (with completion) to
  1718. the user by `org-refile'.
  1719. You can set the variable `org-refile-target-verify-function' to a function
  1720. to verify each headline found by the simple criteria above.
  1721. When this variable is nil, all top-level headlines in the current buffer
  1722. are used, equivalent to the value `((nil . (:level . 1))'."
  1723. :group 'org-refile
  1724. :type '(repeat
  1725. (cons
  1726. (choice :value org-agenda-files
  1727. (const :tag "All agenda files" org-agenda-files)
  1728. (const :tag "Current buffer" nil)
  1729. (function) (variable) (file))
  1730. (choice :tag "Identify target headline by"
  1731. (cons :tag "Specific tag" (const :value :tag) (string))
  1732. (cons :tag "TODO keyword" (const :value :todo) (string))
  1733. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1734. (cons :tag "Level number" (const :value :level) (integer))
  1735. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1736. (defcustom org-refile-target-verify-function nil
  1737. "Function to verify if the headline at point should be a refile target.
  1738. The function will be called without arguments, with point at the
  1739. beginning of the headline. It should return t and leave point
  1740. where it is if the headline is a valid target for refiling.
  1741. If the target should not be selected, the function must return nil.
  1742. In addition to this, it may move point to a place from where the search
  1743. should be continued. For example, the function may decide that the entire
  1744. subtree of the current entry should be excluded and move point to the end
  1745. of the subtree."
  1746. :group 'org-refile
  1747. :type 'function)
  1748. (defcustom org-refile-use-cache nil
  1749. "Non-nil means cache refile targets to speed up the process.
  1750. The cache for a particular file will be updated automatically when
  1751. the buffer has been killed, or when any of the marker used for flagging
  1752. refile targets no longer points at a live buffer.
  1753. If you have added new entries to a buffer that might themselves be targets,
  1754. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  1755. find that easier, `C-u C-u C-u C-c C-w'."
  1756. :group 'org-refile
  1757. :version "24.1"
  1758. :type 'boolean)
  1759. (defcustom org-refile-use-outline-path nil
  1760. "Non-nil means provide refile targets as paths.
  1761. So a level 3 headline will be available as level1/level2/level3.
  1762. When the value is `file', also include the file name (without directory)
  1763. into the path. In this case, you can also stop the completion after
  1764. the file name, to get entries inserted as top level in the file.
  1765. When `full-file-path', include the full file path."
  1766. :group 'org-refile
  1767. :type '(choice
  1768. (const :tag "Not" nil)
  1769. (const :tag "Yes" t)
  1770. (const :tag "Start with file name" file)
  1771. (const :tag "Start with full file path" full-file-path)))
  1772. (defcustom org-outline-path-complete-in-steps t
  1773. "Non-nil means complete the outline path in hierarchical steps.
  1774. When Org-mode uses the refile interface to select an outline path
  1775. \(see variable `org-refile-use-outline-path'), the completion of
  1776. the path can be done is a single go, or if can be done in steps down
  1777. the headline hierarchy. Going in steps is probably the best if you
  1778. do not use a special completion package like `ido' or `icicles'.
  1779. However, when using these packages, going in one step can be very
  1780. fast, while still showing the whole path to the entry."
  1781. :group 'org-refile
  1782. :type 'boolean)
  1783. (defcustom org-refile-allow-creating-parent-nodes nil
  1784. "Non-nil means allow to create new nodes as refile targets.
  1785. New nodes are then created by adding \"/new node name\" to the completion
  1786. of an existing node. When the value of this variable is `confirm',
  1787. new node creation must be confirmed by the user (recommended)
  1788. When nil, the completion must match an existing entry.
  1789. Note that, if the new heading is not seen by the criteria
  1790. listed in `org-refile-targets', multiple instances of the same
  1791. heading would be created by trying again to file under the new
  1792. heading."
  1793. :group 'org-refile
  1794. :type '(choice
  1795. (const :tag "Never" nil)
  1796. (const :tag "Always" t)
  1797. (const :tag "Prompt for confirmation" confirm)))
  1798. (defcustom org-refile-active-region-within-subtree nil
  1799. "Non-nil means also refile active region within a subtree.
  1800. By default `org-refile' doesn't allow refiling regions if they
  1801. don't contain a set of subtrees, but it might be convenient to
  1802. do so sometimes: in that case, the first line of the region is
  1803. converted to a headline before refiling."
  1804. :group 'org-refile
  1805. :version "24.1"
  1806. :type 'boolean)
  1807. (defgroup org-todo nil
  1808. "Options concerning TODO items in Org-mode."
  1809. :tag "Org TODO"
  1810. :group 'org)
  1811. (defgroup org-progress nil
  1812. "Options concerning Progress logging in Org-mode."
  1813. :tag "Org Progress"
  1814. :group 'org-time)
  1815. (defvar org-todo-interpretation-widgets
  1816. '((:tag "Sequence (cycling hits every state)" sequence)
  1817. (:tag "Type (cycling directly to DONE)" type))
  1818. "The available interpretation symbols for customizing `org-todo-keywords'.
  1819. Interested libraries should add to this list.")
  1820. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1821. "List of TODO entry keyword sequences and their interpretation.
  1822. \\<org-mode-map>This is a list of sequences.
  1823. Each sequence starts with a symbol, either `sequence' or `type',
  1824. indicating if the keywords should be interpreted as a sequence of
  1825. action steps, or as different types of TODO items. The first
  1826. keywords are states requiring action - these states will select a headline
  1827. for inclusion into the global TODO list Org-mode produces. If one of
  1828. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  1829. signify that no further action is necessary. If \"|\" is not found,
  1830. the last keyword is treated as the only DONE state of the sequence.
  1831. The command \\[org-todo] cycles an entry through these states, and one
  1832. additional state where no keyword is present. For details about this
  1833. cycling, see the manual.
  1834. TODO keywords and interpretation can also be set on a per-file basis with
  1835. the special #+SEQ_TODO and #+TYP_TODO lines.
  1836. Each keyword can optionally specify a character for fast state selection
  1837. \(in combination with the variable `org-use-fast-todo-selection')
  1838. and specifiers for state change logging, using the same syntax
  1839. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1840. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1841. indicates to record a time stamp each time this state is selected.
  1842. Each keyword may also specify if a timestamp or a note should be
  1843. recorded when entering or leaving the state, by adding additional
  1844. characters in the parenthesis after the keyword. This looks like this:
  1845. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1846. record only the time of the state change. With X and Y being either
  1847. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1848. Y when leaving the state if and only if the *target* state does not
  1849. define X. You may omit any of the fast-selection key or X or /Y,
  1850. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1851. For backward compatibility, this variable may also be just a list
  1852. of keywords - in this case the interpretation (sequence or type) will be
  1853. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1854. :group 'org-todo
  1855. :group 'org-keywords
  1856. :type '(choice
  1857. (repeat :tag "Old syntax, just keywords"
  1858. (string :tag "Keyword"))
  1859. (repeat :tag "New syntax"
  1860. (cons
  1861. (choice
  1862. :tag "Interpretation"
  1863. ;;Quick and dirty way to see
  1864. ;;`org-todo-interpretations'. This takes the
  1865. ;;place of item arguments
  1866. :convert-widget
  1867. (lambda (widget)
  1868. (widget-put widget
  1869. :args (mapcar
  1870. #'(lambda (x)
  1871. (widget-convert
  1872. (cons 'const x)))
  1873. org-todo-interpretation-widgets))
  1874. widget))
  1875. (repeat
  1876. (string :tag "Keyword"))))))
  1877. (defvar org-todo-keywords-1 nil
  1878. "All TODO and DONE keywords active in a buffer.")
  1879. (make-variable-buffer-local 'org-todo-keywords-1)
  1880. (defvar org-todo-keywords-for-agenda nil)
  1881. (defvar org-done-keywords-for-agenda nil)
  1882. (defvar org-drawers-for-agenda nil)
  1883. (defvar org-todo-keyword-alist-for-agenda nil)
  1884. (defvar org-tag-alist-for-agenda nil)
  1885. (defvar org-agenda-contributing-files nil)
  1886. (defvar org-not-done-keywords nil)
  1887. (make-variable-buffer-local 'org-not-done-keywords)
  1888. (defvar org-done-keywords nil)
  1889. (make-variable-buffer-local 'org-done-keywords)
  1890. (defvar org-todo-heads nil)
  1891. (make-variable-buffer-local 'org-todo-heads)
  1892. (defvar org-todo-sets nil)
  1893. (make-variable-buffer-local 'org-todo-sets)
  1894. (defvar org-todo-log-states nil)
  1895. (make-variable-buffer-local 'org-todo-log-states)
  1896. (defvar org-todo-kwd-alist nil)
  1897. (make-variable-buffer-local 'org-todo-kwd-alist)
  1898. (defvar org-todo-key-alist nil)
  1899. (make-variable-buffer-local 'org-todo-key-alist)
  1900. (defvar org-todo-key-trigger nil)
  1901. (make-variable-buffer-local 'org-todo-key-trigger)
  1902. (defcustom org-todo-interpretation 'sequence
  1903. "Controls how TODO keywords are interpreted.
  1904. This variable is in principle obsolete and is only used for
  1905. backward compatibility, if the interpretation of todo keywords is
  1906. not given already in `org-todo-keywords'. See that variable for
  1907. more information."
  1908. :group 'org-todo
  1909. :group 'org-keywords
  1910. :type '(choice (const sequence)
  1911. (const type)))
  1912. (defcustom org-use-fast-todo-selection t
  1913. "Non-nil means use the fast todo selection scheme with C-c C-t.
  1914. This variable describes if and under what circumstances the cycling
  1915. mechanism for TODO keywords will be replaced by a single-key, direct
  1916. selection scheme.
  1917. When nil, fast selection is never used.
  1918. When the symbol `prefix', it will be used when `org-todo' is called with
  1919. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1920. in an agenda buffer.
  1921. When t, fast selection is used by default. In this case, the prefix
  1922. argument forces cycling instead.
  1923. In all cases, the special interface is only used if access keys have actually
  1924. been assigned by the user, i.e. if keywords in the configuration are followed
  1925. by a letter in parenthesis, like TODO(t)."
  1926. :group 'org-todo
  1927. :type '(choice
  1928. (const :tag "Never" nil)
  1929. (const :tag "By default" t)
  1930. (const :tag "Only with C-u C-c C-t" prefix)))
  1931. (defcustom org-provide-todo-statistics t
  1932. "Non-nil means update todo statistics after insert and toggle.
  1933. ALL-HEADLINES means update todo statistics by including headlines
  1934. with no TODO keyword as well, counting them as not done.
  1935. A list of TODO keywords means the same, but skip keywords that are
  1936. not in this list.
  1937. When this is set, todo statistics is updated in the parent of the
  1938. current entry each time a todo state is changed."
  1939. :group 'org-todo
  1940. :type '(choice
  1941. (const :tag "Yes, only for TODO entries" t)
  1942. (const :tag "Yes, including all entries" 'all-headlines)
  1943. (repeat :tag "Yes, for TODOs in this list"
  1944. (string :tag "TODO keyword"))
  1945. (other :tag "No TODO statistics" nil)))
  1946. (defcustom org-hierarchical-todo-statistics t
  1947. "Non-nil means TODO statistics covers just direct children.
  1948. When nil, all entries in the subtree are considered.
  1949. This has only an effect if `org-provide-todo-statistics' is set.
  1950. To set this to nil for only a single subtree, use a COOKIE_DATA
  1951. property and include the word \"recursive\" into the value."
  1952. :group 'org-todo
  1953. :type 'boolean)
  1954. (defcustom org-after-todo-state-change-hook nil
  1955. "Hook which is run after the state of a TODO item was changed.
  1956. The new state (a string with a TODO keyword, or nil) is available in the
  1957. Lisp variable `org-state'."
  1958. :group 'org-todo
  1959. :type 'hook)
  1960. (defvar org-blocker-hook nil
  1961. "Hook for functions that are allowed to block a state change.
  1962. Each function gets as its single argument a property list, see
  1963. `org-trigger-hook' for more information about this list.
  1964. If any of the functions in this hook returns nil, the state change
  1965. is blocked.")
  1966. (defvar org-trigger-hook nil
  1967. "Hook for functions that are triggered by a state change.
  1968. Each function gets as its single argument a property list with at least
  1969. the following elements:
  1970. (:type type-of-change :position pos-at-entry-start
  1971. :from old-state :to new-state)
  1972. Depending on the type, more properties may be present.
  1973. This mechanism is currently implemented for:
  1974. TODO state changes
  1975. ------------------
  1976. :type todo-state-change
  1977. :from previous state (keyword as a string), or nil, or a symbol
  1978. 'todo' or 'done', to indicate the general type of state.
  1979. :to new state, like in :from")
  1980. (defcustom org-enforce-todo-dependencies nil
  1981. "Non-nil means undone TODO entries will block switching the parent to DONE.
  1982. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1983. be blocked if any prior sibling is not yet done.
  1984. Finally, if the parent is blocked because of ordered siblings of its own,
  1985. the child will also be blocked."
  1986. :set (lambda (var val)
  1987. (set var val)
  1988. (if val
  1989. (add-hook 'org-blocker-hook
  1990. 'org-block-todo-from-children-or-siblings-or-parent)
  1991. (remove-hook 'org-blocker-hook
  1992. 'org-block-todo-from-children-or-siblings-or-parent)))
  1993. :group 'org-todo
  1994. :type 'boolean)
  1995. (defcustom org-enforce-todo-checkbox-dependencies nil
  1996. "Non-nil means unchecked boxes will block switching the parent to DONE.
  1997. When this is nil, checkboxes have no influence on switching TODO states.
  1998. When non-nil, you first need to check off all check boxes before the TODO
  1999. entry can be switched to DONE.
  2000. This variable needs to be set before org.el is loaded, and you need to
  2001. restart Emacs after a change to make the change effective. The only way
  2002. to change is while Emacs is running is through the customize interface."
  2003. :set (lambda (var val)
  2004. (set var val)
  2005. (if val
  2006. (add-hook 'org-blocker-hook
  2007. 'org-block-todo-from-checkboxes)
  2008. (remove-hook 'org-blocker-hook
  2009. 'org-block-todo-from-checkboxes)))
  2010. :group 'org-todo
  2011. :type 'boolean)
  2012. (defcustom org-treat-insert-todo-heading-as-state-change nil
  2013. "Non-nil means inserting a TODO heading is treated as state change.
  2014. So when the command \\[org-insert-todo-heading] is used, state change
  2015. logging will apply if appropriate. When nil, the new TODO item will
  2016. be inserted directly, and no logging will take place."
  2017. :group 'org-todo
  2018. :type 'boolean)
  2019. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  2020. "Non-nil means switching TODO states with S-cursor counts as state change.
  2021. This is the default behavior. However, setting this to nil allows a
  2022. convenient way to select a TODO state and bypass any logging associated
  2023. with that."
  2024. :group 'org-todo
  2025. :type 'boolean)
  2026. (defcustom org-todo-state-tags-triggers nil
  2027. "Tag changes that should be triggered by TODO state changes.
  2028. This is a list. Each entry is
  2029. (state-change (tag . flag) .......)
  2030. State-change can be a string with a state, and empty string to indicate the
  2031. state that has no TODO keyword, or it can be one of the symbols `todo'
  2032. or `done', meaning any not-done or done state, respectively."
  2033. :group 'org-todo
  2034. :group 'org-tags
  2035. :type '(repeat
  2036. (cons (choice :tag "When changing to"
  2037. (const :tag "Not-done state" todo)
  2038. (const :tag "Done state" done)
  2039. (string :tag "State"))
  2040. (repeat
  2041. (cons :tag "Tag action"
  2042. (string :tag "Tag")
  2043. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  2044. (defcustom org-log-done nil
  2045. "Information to record when a task moves to the DONE state.
  2046. Possible values are:
  2047. nil Don't add anything, just change the keyword
  2048. time Add a time stamp to the task
  2049. note Prompt for a note and add it with template `org-log-note-headings'
  2050. This option can also be set with on a per-file-basis with
  2051. #+STARTUP: nologdone
  2052. #+STARTUP: logdone
  2053. #+STARTUP: lognotedone
  2054. You can have local logging settings for a subtree by setting the LOGGING
  2055. property to one or more of these keywords."
  2056. :group 'org-todo
  2057. :group 'org-progress
  2058. :type '(choice
  2059. (const :tag "No logging" nil)
  2060. (const :tag "Record CLOSED timestamp" time)
  2061. (const :tag "Record CLOSED timestamp with note." note)))
  2062. ;; Normalize old uses of org-log-done.
  2063. (cond
  2064. ((eq org-log-done t) (setq org-log-done 'time))
  2065. ((and (listp org-log-done) (memq 'done org-log-done))
  2066. (setq org-log-done 'note)))
  2067. (defcustom org-log-reschedule nil
  2068. "Information to record when the scheduling date of a tasks is modified.
  2069. Possible values are:
  2070. nil Don't add anything, just change the date
  2071. time Add a time stamp to the task
  2072. note Prompt for a note and add it with template `org-log-note-headings'
  2073. This option can also be set with on a per-file-basis with
  2074. #+STARTUP: nologreschedule
  2075. #+STARTUP: logreschedule
  2076. #+STARTUP: lognotereschedule"
  2077. :group 'org-todo
  2078. :group 'org-progress
  2079. :type '(choice
  2080. (const :tag "No logging" nil)
  2081. (const :tag "Record timestamp" time)
  2082. (const :tag "Record timestamp with note." note)))
  2083. (defcustom org-log-redeadline nil
  2084. "Information to record when the deadline date of a tasks is modified.
  2085. Possible values are:
  2086. nil Don't add anything, just change the date
  2087. time Add a time stamp to the task
  2088. note Prompt for a note and add it with template `org-log-note-headings'
  2089. This option can also be set with on a per-file-basis with
  2090. #+STARTUP: nologredeadline
  2091. #+STARTUP: logredeadline
  2092. #+STARTUP: lognoteredeadline
  2093. You can have local logging settings for a subtree by setting the LOGGING
  2094. property to one or more of these keywords."
  2095. :group 'org-todo
  2096. :group 'org-progress
  2097. :type '(choice
  2098. (const :tag "No logging" nil)
  2099. (const :tag "Record timestamp" time)
  2100. (const :tag "Record timestamp with note." note)))
  2101. (defcustom org-log-note-clock-out nil
  2102. "Non-nil means record a note when clocking out of an item.
  2103. This can also be configured on a per-file basis by adding one of
  2104. the following lines anywhere in the buffer:
  2105. #+STARTUP: lognoteclock-out
  2106. #+STARTUP: nolognoteclock-out"
  2107. :group 'org-todo
  2108. :group 'org-progress
  2109. :type 'boolean)
  2110. (defcustom org-log-done-with-time t
  2111. "Non-nil means the CLOSED time stamp will contain date and time.
  2112. When nil, only the date will be recorded."
  2113. :group 'org-progress
  2114. :type 'boolean)
  2115. (defcustom org-log-note-headings
  2116. '((done . "CLOSING NOTE %t")
  2117. (state . "State %-12s from %-12S %t")
  2118. (note . "Note taken on %t")
  2119. (reschedule . "Rescheduled from %S on %t")
  2120. (delschedule . "Not scheduled, was %S on %t")
  2121. (redeadline . "New deadline from %S on %t")
  2122. (deldeadline . "Removed deadline, was %S on %t")
  2123. (refile . "Refiled on %t")
  2124. (clock-out . ""))
  2125. "Headings for notes added to entries.
  2126. The value is an alist, with the car being a symbol indicating the note
  2127. context, and the cdr is the heading to be used. The heading may also be the
  2128. empty string.
  2129. %t in the heading will be replaced by a time stamp.
  2130. %T will be an active time stamp instead the default inactive one
  2131. %d will be replaced by a short-format time stamp.
  2132. %D will be replaced by an active short-format time stamp.
  2133. %s will be replaced by the new TODO state, in double quotes.
  2134. %S will be replaced by the old TODO state, in double quotes.
  2135. %u will be replaced by the user name.
  2136. %U will be replaced by the full user name.
  2137. In fact, it is not a good idea to change the `state' entry, because
  2138. agenda log mode depends on the format of these entries."
  2139. :group 'org-todo
  2140. :group 'org-progress
  2141. :type '(list :greedy t
  2142. (cons (const :tag "Heading when closing an item" done) string)
  2143. (cons (const :tag
  2144. "Heading when changing todo state (todo sequence only)"
  2145. state) string)
  2146. (cons (const :tag "Heading when just taking a note" note) string)
  2147. (cons (const :tag "Heading when clocking out" clock-out) string)
  2148. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2149. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2150. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2151. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2152. (cons (const :tag "Heading when refiling" refile) string)))
  2153. (unless (assq 'note org-log-note-headings)
  2154. (push '(note . "%t") org-log-note-headings))
  2155. (defcustom org-log-into-drawer nil
  2156. "Non-nil means insert state change notes and time stamps into a drawer.
  2157. When nil, state changes notes will be inserted after the headline and
  2158. any scheduling and clock lines, but not inside a drawer.
  2159. The value of this variable should be the name of the drawer to use.
  2160. LOGBOOK is proposed as the default drawer for this purpose, you can
  2161. also set this to a string to define the drawer of your choice.
  2162. A value of t is also allowed, representing \"LOGBOOK\".
  2163. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2164. will be ignored.
  2165. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2166. a subtree."
  2167. :group 'org-todo
  2168. :group 'org-progress
  2169. :type '(choice
  2170. (const :tag "Not into a drawer" nil)
  2171. (const :tag "LOGBOOK" t)
  2172. (string :tag "Other")))
  2173. (if (fboundp 'defvaralias)
  2174. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  2175. (defun org-log-into-drawer ()
  2176. "Return the value of `org-log-into-drawer', but let properties overrule.
  2177. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  2178. used instead of the default value."
  2179. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit)))
  2180. (cond
  2181. ((or (not p) (equal p "nil")) org-log-into-drawer)
  2182. ((equal p "t") "LOGBOOK")
  2183. (t p))))
  2184. (defcustom org-log-state-notes-insert-after-drawers nil
  2185. "Non-nil means insert state change notes after any drawers in entry.
  2186. Only the drawers that *immediately* follow the headline and the
  2187. deadline/scheduled line are skipped.
  2188. When nil, insert notes right after the heading and perhaps the line
  2189. with deadline/scheduling if present.
  2190. This variable will have no effect if `org-log-into-drawer' is
  2191. set."
  2192. :group 'org-todo
  2193. :group 'org-progress
  2194. :type 'boolean)
  2195. (defcustom org-log-states-order-reversed t
  2196. "Non-nil means the latest state note will be directly after heading.
  2197. When nil, the state change notes will be ordered according to time."
  2198. :group 'org-todo
  2199. :group 'org-progress
  2200. :type 'boolean)
  2201. (defcustom org-todo-repeat-to-state nil
  2202. "The TODO state to which a repeater should return the repeating task.
  2203. By default this is the first task in a TODO sequence, or the previous state
  2204. in a TODO_TYP set. But you can specify another task here.
  2205. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2206. :group 'org-todo
  2207. :version "24.1"
  2208. :type '(choice (const :tag "Head of sequence" nil)
  2209. (string :tag "Specific state")))
  2210. (defcustom org-log-repeat 'time
  2211. "Non-nil means record moving through the DONE state when triggering repeat.
  2212. An auto-repeating task is immediately switched back to TODO when
  2213. marked DONE. If you are not logging state changes (by adding \"@\"
  2214. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2215. record a closing note, there will be no record of the task moving
  2216. through DONE. This variable forces taking a note anyway.
  2217. nil Don't force a record
  2218. time Record a time stamp
  2219. note Record a note
  2220. This option can also be set with on a per-file-basis with
  2221. #+STARTUP: logrepeat
  2222. #+STARTUP: lognoterepeat
  2223. #+STARTUP: nologrepeat
  2224. You can have local logging settings for a subtree by setting the LOGGING
  2225. property to one or more of these keywords."
  2226. :group 'org-todo
  2227. :group 'org-progress
  2228. :type '(choice
  2229. (const :tag "Don't force a record" nil)
  2230. (const :tag "Force recording the DONE state" time)
  2231. (const :tag "Force recording a note with the DONE state" note)))
  2232. (defgroup org-priorities nil
  2233. "Priorities in Org-mode."
  2234. :tag "Org Priorities"
  2235. :group 'org-todo)
  2236. (defcustom org-enable-priority-commands t
  2237. "Non-nil means priority commands are active.
  2238. When nil, these commands will be disabled, so that you never accidentally
  2239. set a priority."
  2240. :group 'org-priorities
  2241. :type 'boolean)
  2242. (defcustom org-highest-priority ?A
  2243. "The highest priority of TODO items. A character like ?A, ?B etc.
  2244. Must have a smaller ASCII number than `org-lowest-priority'."
  2245. :group 'org-priorities
  2246. :type 'character)
  2247. (defcustom org-lowest-priority ?C
  2248. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2249. Must have a larger ASCII number than `org-highest-priority'."
  2250. :group 'org-priorities
  2251. :type 'character)
  2252. (defcustom org-default-priority ?B
  2253. "The default priority of TODO items.
  2254. This is the priority an item gets if no explicit priority is given.
  2255. When starting to cycle on an empty priority the first step in the cycle
  2256. depends on `org-priority-start-cycle-with-default'. The resulting first
  2257. step priority must not exceed the range from `org-highest-priority' to
  2258. `org-lowest-priority' which means that `org-default-priority' has to be
  2259. in this range exclusive or inclusive the range boundaries. Else the
  2260. first step refuses to set the default and the second will fall back
  2261. to (depending on the command used) the highest or lowest priority."
  2262. :group 'org-priorities
  2263. :type 'character)
  2264. (defcustom org-priority-start-cycle-with-default t
  2265. "Non-nil means start with default priority when starting to cycle.
  2266. When this is nil, the first step in the cycle will be (depending on the
  2267. command used) one higher or lower than the default priority.
  2268. See also `org-default-priority'."
  2269. :group 'org-priorities
  2270. :type 'boolean)
  2271. (defcustom org-get-priority-function nil
  2272. "Function to extract the priority from a string.
  2273. The string is normally the headline. If this is nil Org computes the
  2274. priority from the priority cookie like [#A] in the headline. It returns
  2275. an integer, increasing by 1000 for each priority level.
  2276. The user can set a different function here, which should take a string
  2277. as an argument and return the numeric priority."
  2278. :group 'org-priorities
  2279. :version "24.1"
  2280. :type 'function)
  2281. (defgroup org-time nil
  2282. "Options concerning time stamps and deadlines in Org-mode."
  2283. :tag "Org Time"
  2284. :group 'org)
  2285. (defcustom org-insert-labeled-timestamps-at-point nil
  2286. "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
  2287. When nil, these labeled time stamps are forces into the second line of an
  2288. entry, just after the headline. When scheduling from the global TODO list,
  2289. the time stamp will always be forced into the second line."
  2290. :group 'org-time
  2291. :type 'boolean)
  2292. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  2293. "Formats for `format-time-string' which are used for time stamps.
  2294. It is not recommended to change this constant.")
  2295. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2296. "Number of minutes to round time stamps to.
  2297. These are two values, the first applies when first creating a time stamp.
  2298. The second applies when changing it with the commands `S-up' and `S-down'.
  2299. When changing the time stamp, this means that it will change in steps
  2300. of N minutes, as given by the second value.
  2301. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2302. numbers should be factors of 60, so for example 5, 10, 15.
  2303. When this is larger than 1, you can still force an exact time stamp by using
  2304. a double prefix argument to a time stamp command like `C-c .' or `C-c !',
  2305. and by using a prefix arg to `S-up/down' to specify the exact number
  2306. of minutes to shift."
  2307. :group 'org-time
  2308. :get #'(lambda (var) ; Make sure both elements are there
  2309. (if (integerp (default-value var))
  2310. (list (default-value var) 5)
  2311. (default-value var)))
  2312. :type '(list
  2313. (integer :tag "when inserting times")
  2314. (integer :tag "when modifying times")))
  2315. ;; Normalize old customizations of this variable.
  2316. (when (integerp org-time-stamp-rounding-minutes)
  2317. (setq org-time-stamp-rounding-minutes
  2318. (list org-time-stamp-rounding-minutes
  2319. org-time-stamp-rounding-minutes)))
  2320. (defcustom org-display-custom-times nil
  2321. "Non-nil means overlay custom formats over all time stamps.
  2322. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2323. To turn this on on a per-file basis, insert anywhere in the file:
  2324. #+STARTUP: customtime"
  2325. :group 'org-time
  2326. :set 'set-default
  2327. :type 'sexp)
  2328. (make-variable-buffer-local 'org-display-custom-times)
  2329. (defcustom org-time-stamp-custom-formats
  2330. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2331. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2332. These are overlaid over the default ISO format if the variable
  2333. `org-display-custom-times' is set. Time like %H:%M should be at the
  2334. end of the second format. The custom formats are also honored by export
  2335. commands, if custom time display is turned on at the time of export."
  2336. :group 'org-time
  2337. :type 'sexp)
  2338. (defun org-time-stamp-format (&optional long inactive)
  2339. "Get the right format for a time string."
  2340. (let ((f (if long (cdr org-time-stamp-formats)
  2341. (car org-time-stamp-formats))))
  2342. (if inactive
  2343. (concat "[" (substring f 1 -1) "]")
  2344. f)))
  2345. (defcustom org-time-clocksum-format "%d:%02d"
  2346. "The format string used when creating CLOCKSUM lines.
  2347. This is also used when org-mode generates a time duration."
  2348. :group 'org-time
  2349. :type 'string)
  2350. (defcustom org-time-clocksum-use-fractional nil
  2351. "If non-nil, \\[org-clock-display] uses fractional times.
  2352. org-mode generates a time duration."
  2353. :group 'org-time
  2354. :type 'boolean)
  2355. (defcustom org-time-clocksum-fractional-format "%.2f"
  2356. "The format string used when creating CLOCKSUM lines, or when
  2357. org-mode generates a time duration."
  2358. :group 'org-time
  2359. :type 'string)
  2360. (defcustom org-deadline-warning-days 14
  2361. "No. of days before expiration during which a deadline becomes active.
  2362. This variable governs the display in sparse trees and in the agenda.
  2363. When 0 or negative, it means use this number (the absolute value of it)
  2364. even if a deadline has a different individual lead time specified.
  2365. Custom commands can set this variable in the options section."
  2366. :group 'org-time
  2367. :group 'org-agenda-daily/weekly
  2368. :type 'integer)
  2369. (defcustom org-read-date-prefer-future t
  2370. "Non-nil means assume future for incomplete date input from user.
  2371. This affects the following situations:
  2372. 1. The user gives a month but not a year.
  2373. For example, if it is April and you enter \"feb 2\", this will be read
  2374. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2375. 2. The user gives a day, but no month.
  2376. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2377. read this as the third of *next* month. However, if you enter \"17\",
  2378. it will be considered as *this* month.
  2379. If you set this variable to the symbol `time', then also the following
  2380. will work:
  2381. 3. If the user gives a time, but no day. If the time is before now,
  2382. to will be interpreted as tomorrow.
  2383. Currently none of this works for ISO week specifications.
  2384. When this option is nil, the current day, month and year will always be
  2385. used as defaults.
  2386. See also `org-agenda-jump-prefer-future'."
  2387. :group 'org-time
  2388. :type '(choice
  2389. (const :tag "Never" nil)
  2390. (const :tag "Check month and day" t)
  2391. (const :tag "Check month, day, and time" time)))
  2392. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2393. "Should the agenda jump command prefer the future for incomplete dates?
  2394. The default is to do the same as configured in `org-read-date-prefer-future'.
  2395. But you can also set a deviating value here.
  2396. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2397. :group 'org-agenda
  2398. :group 'org-time
  2399. :version "24.1"
  2400. :type '(choice
  2401. (const :tag "Use org-read-date-prefer-future"
  2402. org-read-date-prefer-future)
  2403. (const :tag "Never" nil)
  2404. (const :tag "Always" t)))
  2405. (defcustom org-read-date-force-compatible-dates t
  2406. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2407. Depending on the system Emacs is running on, certain dates cannot
  2408. be represented with the type used internally to represent time.
  2409. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2410. correctly. Some systems allow for earlier dates, some for later,
  2411. some for both. One way to find out it to insert any date into an
  2412. Org buffer, putting the cursor on the year and hitting S-up and
  2413. S-down to test the range.
  2414. When this variable is set to t, the date/time prompt will not let
  2415. you specify dates outside the 1970-2037 range, so it is certain that
  2416. these dates will work in whatever version of Emacs you are
  2417. running, and also that you can move a file from one Emacs implementation
  2418. to another. WHenever Org is forcing the year for you, it will display
  2419. a message and beep.
  2420. When this variable is nil, Org will check if the date is
  2421. representable in the specific Emacs implementation you are using.
  2422. If not, it will force a year, usually the current year, and beep
  2423. to remind you. Currently this setting is not recommended because
  2424. the likelihood that you will open your Org files in an Emacs that
  2425. has limited date range is not negligible.
  2426. A workaround for this problem is to use diary sexp dates for time
  2427. stamps outside of this range."
  2428. :group 'org-time
  2429. :version "24.1"
  2430. :type 'boolean)
  2431. (defcustom org-read-date-display-live t
  2432. "Non-nil means display current interpretation of date prompt live.
  2433. This display will be in an overlay, in the minibuffer."
  2434. :group 'org-time
  2435. :type 'boolean)
  2436. (defcustom org-read-date-popup-calendar t
  2437. "Non-nil means pop up a calendar when prompting for a date.
  2438. In the calendar, the date can be selected with mouse-1. However, the
  2439. minibuffer will also be active, and you can simply enter the date as well.
  2440. When nil, only the minibuffer will be available."
  2441. :group 'org-time
  2442. :type 'boolean)
  2443. (if (fboundp 'defvaralias)
  2444. (defvaralias 'org-popup-calendar-for-date-prompt
  2445. 'org-read-date-popup-calendar))
  2446. (defcustom org-read-date-minibuffer-setup-hook nil
  2447. "Hook to be used to set up keys for the date/time interface.
  2448. Add key definitions to `minibuffer-local-map', which will be a temporary
  2449. copy."
  2450. :group 'org-time
  2451. :type 'hook)
  2452. (defcustom org-extend-today-until 0
  2453. "The hour when your day really ends. Must be an integer.
  2454. This has influence for the following applications:
  2455. - When switching the agenda to \"today\". It it is still earlier than
  2456. the time given here, the day recognized as TODAY is actually yesterday.
  2457. - When a date is read from the user and it is still before the time given
  2458. here, the current date and time will be assumed to be yesterday, 23:59.
  2459. Also, timestamps inserted in remember templates follow this rule.
  2460. IMPORTANT: This is a feature whose implementation is and likely will
  2461. remain incomplete. Really, it is only here because past midnight seems to
  2462. be the favorite working time of John Wiegley :-)"
  2463. :group 'org-time
  2464. :type 'integer)
  2465. (defcustom org-use-effective-time nil
  2466. "If non-nil, consider `org-extend-today-until' when creating timestamps.
  2467. For example, if `org-extend-today-until' is 8, and it's 4am, then the
  2468. \"effective time\" of any timestamps between midnight and 8am will be
  2469. 23:59 of the previous day."
  2470. :group 'org-time
  2471. :version "24.1"
  2472. :type 'boolean)
  2473. (defcustom org-edit-timestamp-down-means-later nil
  2474. "Non-nil means S-down will increase the time in a time stamp.
  2475. When nil, S-up will increase."
  2476. :group 'org-time
  2477. :type 'boolean)
  2478. (defcustom org-calendar-follow-timestamp-change t
  2479. "Non-nil means make the calendar window follow timestamp changes.
  2480. When a timestamp is modified and the calendar window is visible, it will be
  2481. moved to the new date."
  2482. :group 'org-time
  2483. :type 'boolean)
  2484. (defgroup org-tags nil
  2485. "Options concerning tags in Org-mode."
  2486. :tag "Org Tags"
  2487. :group 'org)
  2488. (defcustom org-tag-alist nil
  2489. "List of tags allowed in Org-mode files.
  2490. When this list is nil, Org-mode will base TAG input on what is already in the
  2491. buffer.
  2492. The value of this variable is an alist, the car of each entry must be a
  2493. keyword as a string, the cdr may be a character that is used to select
  2494. that tag through the fast-tag-selection interface.
  2495. See the manual for details."
  2496. :group 'org-tags
  2497. :type '(repeat
  2498. (choice
  2499. (cons (string :tag "Tag name")
  2500. (character :tag "Access char"))
  2501. (list :tag "Start radio group"
  2502. (const :startgroup)
  2503. (option (string :tag "Group description")))
  2504. (list :tag "End radio group"
  2505. (const :endgroup)
  2506. (option (string :tag "Group description")))
  2507. (const :tag "New line" (:newline)))))
  2508. (defcustom org-tag-persistent-alist nil
  2509. "List of tags that will always appear in all Org-mode files.
  2510. This is in addition to any in buffer settings or customizations
  2511. of `org-tag-alist'.
  2512. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2513. The value of this variable is an alist, the car of each entry must be a
  2514. keyword as a string, the cdr may be a character that is used to select
  2515. that tag through the fast-tag-selection interface.
  2516. See the manual for details.
  2517. To disable these tags on a per-file basis, insert anywhere in the file:
  2518. #+STARTUP: noptag"
  2519. :group 'org-tags
  2520. :type '(repeat
  2521. (choice
  2522. (cons (string :tag "Tag name")
  2523. (character :tag "Access char"))
  2524. (const :tag "Start radio group" (:startgroup))
  2525. (const :tag "End radio group" (:endgroup))
  2526. (const :tag "New line" (:newline)))))
  2527. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2528. "If non-nil, always offer completion for all tags of all agenda files.
  2529. Instead of customizing this variable directly, you might want to
  2530. set it locally for capture buffers, because there no list of
  2531. tags in that file can be created dynamically (there are none).
  2532. (add-hook 'org-capture-mode-hook
  2533. (lambda ()
  2534. (set (make-local-variable
  2535. 'org-complete-tags-always-offer-all-agenda-tags)
  2536. t)))"
  2537. :group 'org-tags
  2538. :version "24.1"
  2539. :type 'boolean)
  2540. (defvar org-file-tags nil
  2541. "List of tags that can be inherited by all entries in the file.
  2542. The tags will be inherited if the variable `org-use-tag-inheritance'
  2543. says they should be.
  2544. This variable is populated from #+FILETAGS lines.")
  2545. (defcustom org-use-fast-tag-selection 'auto
  2546. "Non-nil means use fast tag selection scheme.
  2547. This is a special interface to select and deselect tags with single keys.
  2548. When nil, fast selection is never used.
  2549. When the symbol `auto', fast selection is used if and only if selection
  2550. characters for tags have been configured, either through the variable
  2551. `org-tag-alist' or through a #+TAGS line in the buffer.
  2552. When t, fast selection is always used and selection keys are assigned
  2553. automatically if necessary."
  2554. :group 'org-tags
  2555. :type '(choice
  2556. (const :tag "Always" t)
  2557. (const :tag "Never" nil)
  2558. (const :tag "When selection characters are configured" 'auto)))
  2559. (defcustom org-fast-tag-selection-single-key nil
  2560. "Non-nil means fast tag selection exits after first change.
  2561. When nil, you have to press RET to exit it.
  2562. During fast tag selection, you can toggle this flag with `C-c'.
  2563. This variable can also have the value `expert'. In this case, the window
  2564. displaying the tags menu is not even shown, until you press C-c again."
  2565. :group 'org-tags
  2566. :type '(choice
  2567. (const :tag "No" nil)
  2568. (const :tag "Yes" t)
  2569. (const :tag "Expert" expert)))
  2570. (defvar org-fast-tag-selection-include-todo nil
  2571. "Non-nil means fast tags selection interface will also offer TODO states.
  2572. This is an undocumented feature, you should not rely on it.")
  2573. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2574. "The column to which tags should be indented in a headline.
  2575. If this number is positive, it specifies the column. If it is negative,
  2576. it means that the tags should be flushright to that column. For example,
  2577. -80 works well for a normal 80 character screen.
  2578. When 0, place tags directly after headline text, with only one space in
  2579. between."
  2580. :group 'org-tags
  2581. :type 'integer)
  2582. (defcustom org-auto-align-tags t
  2583. "Non-nil keeps tags aligned when modifying headlines.
  2584. Some operations (i.e. demoting) change the length of a headline and
  2585. therefore shift the tags around. With this option turned on, after
  2586. each such operation the tags are again aligned to `org-tags-column'."
  2587. :group 'org-tags
  2588. :type 'boolean)
  2589. (defcustom org-use-tag-inheritance t
  2590. "Non-nil means tags in levels apply also for sublevels.
  2591. When nil, only the tags directly given in a specific line apply there.
  2592. This may also be a list of tags that should be inherited, or a regexp that
  2593. matches tags that should be inherited. Additional control is possible
  2594. with the variable `org-tags-exclude-from-inheritance' which gives an
  2595. explicit list of tags to be excluded from inheritance., even if the value of
  2596. `org-use-tag-inheritance' would select it for inheritance.
  2597. If this option is t, a match early-on in a tree can lead to a large
  2598. number of matches in the subtree when constructing the agenda or creating
  2599. a sparse tree. If you only want to see the first match in a tree during
  2600. a search, check out the variable `org-tags-match-list-sublevels'."
  2601. :group 'org-tags
  2602. :type '(choice
  2603. (const :tag "Not" nil)
  2604. (const :tag "Always" t)
  2605. (repeat :tag "Specific tags" (string :tag "Tag"))
  2606. (regexp :tag "Tags matched by regexp")))
  2607. (defcustom org-tags-exclude-from-inheritance nil
  2608. "List of tags that should never be inherited.
  2609. This is a way to exclude a few tags from inheritance. For way to do
  2610. the opposite, to actively allow inheritance for selected tags,
  2611. see the variable `org-use-tag-inheritance'."
  2612. :group 'org-tags
  2613. :type '(repeat (string :tag "Tag")))
  2614. (defun org-tag-inherit-p (tag)
  2615. "Check if TAG is one that should be inherited."
  2616. (cond
  2617. ((member tag org-tags-exclude-from-inheritance) nil)
  2618. ((eq org-use-tag-inheritance t) t)
  2619. ((not org-use-tag-inheritance) nil)
  2620. ((stringp org-use-tag-inheritance)
  2621. (string-match org-use-tag-inheritance tag))
  2622. ((listp org-use-tag-inheritance)
  2623. (member tag org-use-tag-inheritance))
  2624. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2625. (defcustom org-tags-match-list-sublevels t
  2626. "Non-nil means list also sublevels of headlines matching a search.
  2627. This variable applies to tags/property searches, and also to stuck
  2628. projects because this search is based on a tags match as well.
  2629. When set to the symbol `indented', sublevels are indented with
  2630. leading dots.
  2631. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2632. the sublevels of a headline matching a tag search often also match
  2633. the same search. Listing all of them can create very long lists.
  2634. Setting this variable to nil causes subtrees of a match to be skipped.
  2635. This variable is semi-obsolete and probably should always be true. It
  2636. is better to limit inheritance to certain tags using the variables
  2637. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2638. :group 'org-tags
  2639. :type '(choice
  2640. (const :tag "No, don't list them" nil)
  2641. (const :tag "Yes, do list them" t)
  2642. (const :tag "List them, indented with leading dots" indented)))
  2643. (defcustom org-tags-sort-function nil
  2644. "When set, tags are sorted using this function as a comparator."
  2645. :group 'org-tags
  2646. :type '(choice
  2647. (const :tag "No sorting" nil)
  2648. (const :tag "Alphabetical" string<)
  2649. (const :tag "Reverse alphabetical" string>)
  2650. (function :tag "Custom function" nil)))
  2651. (defvar org-tags-history nil
  2652. "History of minibuffer reads for tags.")
  2653. (defvar org-last-tags-completion-table nil
  2654. "The last used completion table for tags.")
  2655. (defvar org-after-tags-change-hook nil
  2656. "Hook that is run after the tags in a line have changed.")
  2657. (defgroup org-properties nil
  2658. "Options concerning properties in Org-mode."
  2659. :tag "Org Properties"
  2660. :group 'org)
  2661. (defcustom org-property-format "%-10s %s"
  2662. "How property key/value pairs should be formatted by `indent-line'.
  2663. When `indent-line' hits a property definition, it will format the line
  2664. according to this format, mainly to make sure that the values are
  2665. lined-up with respect to each other."
  2666. :group 'org-properties
  2667. :type 'string)
  2668. (defcustom org-properties-postprocess-alist nil
  2669. "Alist of properties and functions to adjust inserted values.
  2670. Elements of this alist must be of the form
  2671. ([string] [function])
  2672. where [string] must be a property name and [function] must be a
  2673. lambda expression: this lambda expression must take one argument,
  2674. the value to adjust, and return the new value as a string.
  2675. For example, this element will allow the property \"Remaining\"
  2676. to be updated wrt the relation between the \"Effort\" property
  2677. and the clock summary:
  2678. ((\"Remaining\" (lambda(value)
  2679. (let ((clocksum (org-clock-sum-current-item))
  2680. (effort (org-duration-string-to-minutes
  2681. (org-entry-get (point) \"Effort\"))))
  2682. (org-minutes-to-hh:mm-string (- effort clocksum))))))"
  2683. :group 'org-properties
  2684. :version "24.1"
  2685. :type 'alist)
  2686. (defcustom org-use-property-inheritance nil
  2687. "Non-nil means properties apply also for sublevels.
  2688. This setting is chiefly used during property searches. Turning it on can
  2689. cause significant overhead when doing a search, which is why it is not
  2690. on by default.
  2691. When nil, only the properties directly given in the current entry count.
  2692. When t, every property is inherited. The value may also be a list of
  2693. properties that should have inheritance, or a regular expression matching
  2694. properties that should be inherited.
  2695. However, note that some special properties use inheritance under special
  2696. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2697. and the properties ending in \"_ALL\" when they are used as descriptor
  2698. for valid values of a property.
  2699. Note for programmers:
  2700. When querying an entry with `org-entry-get', you can control if inheritance
  2701. should be used. By default, `org-entry-get' looks only at the local
  2702. properties. You can request inheritance by setting the inherit argument
  2703. to t (to force inheritance) or to `selective' (to respect the setting
  2704. in this variable)."
  2705. :group 'org-properties
  2706. :type '(choice
  2707. (const :tag "Not" nil)
  2708. (const :tag "Always" t)
  2709. (repeat :tag "Specific properties" (string :tag "Property"))
  2710. (regexp :tag "Properties matched by regexp")))
  2711. (defun org-property-inherit-p (property)
  2712. "Check if PROPERTY is one that should be inherited."
  2713. (cond
  2714. ((eq org-use-property-inheritance t) t)
  2715. ((not org-use-property-inheritance) nil)
  2716. ((stringp org-use-property-inheritance)
  2717. (string-match org-use-property-inheritance property))
  2718. ((listp org-use-property-inheritance)
  2719. (member property org-use-property-inheritance))
  2720. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2721. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2722. "The default column format, if no other format has been defined.
  2723. This variable can be set on the per-file basis by inserting a line
  2724. #+COLUMNS: %25ITEM ....."
  2725. :group 'org-properties
  2726. :type 'string)
  2727. (defcustom org-columns-ellipses ".."
  2728. "The ellipses to be used when a field in column view is truncated.
  2729. When this is the empty string, as many characters as possible are shown,
  2730. but then there will be no visual indication that the field has been truncated.
  2731. When this is a string of length N, the last N characters of a truncated
  2732. field are replaced by this string. If the column is narrower than the
  2733. ellipses string, only part of the ellipses string will be shown."
  2734. :group 'org-properties
  2735. :type 'string)
  2736. (defcustom org-columns-modify-value-for-display-function nil
  2737. "Function that modifies values for display in column view.
  2738. For example, it can be used to cut out a certain part from a time stamp.
  2739. The function must take 2 arguments:
  2740. column-title The title of the column (*not* the property name)
  2741. value The value that should be modified.
  2742. The function should return the value that should be displayed,
  2743. or nil if the normal value should be used."
  2744. :group 'org-properties
  2745. :type 'function)
  2746. (defcustom org-effort-property "Effort"
  2747. "The property that is being used to keep track of effort estimates.
  2748. Effort estimates given in this property need to have the format H:MM."
  2749. :group 'org-properties
  2750. :group 'org-progress
  2751. :type '(string :tag "Property"))
  2752. (defconst org-global-properties-fixed
  2753. '(("VISIBILITY_ALL" . "folded children content all")
  2754. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  2755. "List of property/value pairs that can be inherited by any entry.
  2756. These are fixed values, for the preset properties. The user variable
  2757. that can be used to add to this list is `org-global-properties'.
  2758. The entries in this list are cons cells where the car is a property
  2759. name and cdr is a string with the value. If the value represents
  2760. multiple items like an \"_ALL\" property, separate the items by
  2761. spaces.")
  2762. (defcustom org-global-properties nil
  2763. "List of property/value pairs that can be inherited by any entry.
  2764. This list will be combined with the constant `org-global-properties-fixed'.
  2765. The entries in this list are cons cells where the car is a property
  2766. name and cdr is a string with the value.
  2767. You can set buffer-local values for the same purpose in the variable
  2768. `org-file-properties' this by adding lines like
  2769. #+PROPERTY: NAME VALUE"
  2770. :group 'org-properties
  2771. :type '(repeat
  2772. (cons (string :tag "Property")
  2773. (string :tag "Value"))))
  2774. (defvar org-file-properties nil
  2775. "List of property/value pairs that can be inherited by any entry.
  2776. Valid for the current buffer.
  2777. This variable is populated from #+PROPERTY lines.")
  2778. (make-variable-buffer-local 'org-file-properties)
  2779. (defgroup org-agenda nil
  2780. "Options concerning agenda views in Org-mode."
  2781. :tag "Org Agenda"
  2782. :group 'org)
  2783. (defvar org-category nil
  2784. "Variable used by org files to set a category for agenda display.
  2785. Such files should use a file variable to set it, for example
  2786. # -*- mode: org; org-category: \"ELisp\"
  2787. or contain a special line
  2788. #+CATEGORY: ELisp
  2789. If the file does not specify a category, then file's base name
  2790. is used instead.")
  2791. (make-variable-buffer-local 'org-category)
  2792. (put 'org-category 'safe-local-variable #'(lambda (x) (or (symbolp x) (stringp x))))
  2793. (defcustom org-agenda-files nil
  2794. "The files to be used for agenda display.
  2795. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2796. \\[org-remove-file]. You can also use customize to edit the list.
  2797. If an entry is a directory, all files in that directory that are matched by
  2798. `org-agenda-file-regexp' will be part of the file list.
  2799. If the value of the variable is not a list but a single file name, then
  2800. the list of agenda files is actually stored and maintained in that file, one
  2801. agenda file per line. In this file paths can be given relative to
  2802. `org-directory'. Tilde expansion and environment variable substitution
  2803. are also made."
  2804. :group 'org-agenda
  2805. :type '(choice
  2806. (repeat :tag "List of files and directories" file)
  2807. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2808. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2809. "Regular expression to match files for `org-agenda-files'.
  2810. If any element in the list in that variable contains a directory instead
  2811. of a normal file, all files in that directory that are matched by this
  2812. regular expression will be included."
  2813. :group 'org-agenda
  2814. :type 'regexp)
  2815. (defcustom org-agenda-text-search-extra-files nil
  2816. "List of extra files to be searched by text search commands.
  2817. These files will be search in addition to the agenda files by the
  2818. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2819. Note that these files will only be searched for text search commands,
  2820. not for the other agenda views like todo lists, tag searches or the weekly
  2821. agenda. This variable is intended to list notes and possibly archive files
  2822. that should also be searched by these two commands.
  2823. In fact, if the first element in the list is the symbol `agenda-archives',
  2824. than all archive files of all agenda files will be added to the search
  2825. scope."
  2826. :group 'org-agenda
  2827. :type '(set :greedy t
  2828. (const :tag "Agenda Archives" agenda-archives)
  2829. (repeat :inline t (file))))
  2830. (if (fboundp 'defvaralias)
  2831. (defvaralias 'org-agenda-multi-occur-extra-files
  2832. 'org-agenda-text-search-extra-files))
  2833. (defcustom org-agenda-skip-unavailable-files nil
  2834. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2835. A nil value means to remove them, after a query, from the list."
  2836. :group 'org-agenda
  2837. :type 'boolean)
  2838. (defcustom org-calendar-to-agenda-key [?c]
  2839. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2840. The command `org-calendar-goto-agenda' will be bound to this key. The
  2841. default is the character `c' because then `c' can be used to switch back and
  2842. forth between agenda and calendar."
  2843. :group 'org-agenda
  2844. :type 'sexp)
  2845. (defcustom org-calendar-agenda-action-key [?k]
  2846. "The key to be installed in `calendar-mode-map' for agenda-action.
  2847. The command `org-agenda-action' will be bound to this key. The
  2848. default is the character `k' because we use the same key in the agenda."
  2849. :group 'org-agenda
  2850. :type 'sexp)
  2851. (defcustom org-calendar-insert-diary-entry-key [?i]
  2852. "The key to be installed in `calendar-mode-map' for adding diary entries.
  2853. This option is irrelevant until `org-agenda-diary-file' has been configured
  2854. to point to an Org-mode file. When that is the case, the command
  2855. `org-agenda-diary-entry' will be bound to the key given here, by default
  2856. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  2857. if you want to continue doing this, you need to change this to a different
  2858. key."
  2859. :group 'org-agenda
  2860. :type 'sexp)
  2861. (defcustom org-agenda-diary-file 'diary-file
  2862. "File to which to add new entries with the `i' key in agenda and calendar.
  2863. When this is the symbol `diary-file', the functionality in the Emacs
  2864. calendar will be used to add entries to the `diary-file'. But when this
  2865. points to a file, `org-agenda-diary-entry' will be used instead."
  2866. :group 'org-agenda
  2867. :type '(choice
  2868. (const :tag "The standard Emacs diary file" diary-file)
  2869. (file :tag "Special Org file diary entries")))
  2870. (eval-after-load "calendar"
  2871. '(progn
  2872. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2873. 'org-calendar-goto-agenda)
  2874. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2875. 'org-agenda-action)
  2876. (add-hook 'calendar-mode-hook
  2877. (lambda ()
  2878. (unless (eq org-agenda-diary-file 'diary-file)
  2879. (define-key calendar-mode-map
  2880. org-calendar-insert-diary-entry-key
  2881. 'org-agenda-diary-entry))))))
  2882. (defgroup org-latex nil
  2883. "Options for embedding LaTeX code into Org-mode."
  2884. :tag "Org LaTeX"
  2885. :group 'org)
  2886. (defcustom org-format-latex-options
  2887. '(:foreground default :background default :scale 1.0
  2888. :html-foreground "Black" :html-background "Transparent"
  2889. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2890. "Options for creating images from LaTeX fragments.
  2891. This is a property list with the following properties:
  2892. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2893. `default' means use the foreground of the default face.
  2894. :background the background color, or \"Transparent\".
  2895. `default' means use the background of the default face.
  2896. :scale a scaling factor for the size of the images, to get more pixels
  2897. :html-foreground, :html-background, :html-scale
  2898. the same numbers for HTML export.
  2899. :matchers a list indicating which matchers should be used to
  2900. find LaTeX fragments. Valid members of this list are:
  2901. \"begin\" find environments
  2902. \"$1\" find single characters surrounded by $.$
  2903. \"$\" find math expressions surrounded by $...$
  2904. \"$$\" find math expressions surrounded by $$....$$
  2905. \"\\(\" find math expressions surrounded by \\(...\\)
  2906. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2907. :group 'org-latex
  2908. :type 'plist)
  2909. (defcustom org-format-latex-signal-error t
  2910. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  2911. When nil, just push out a message."
  2912. :group 'org-latex
  2913. :version "24.1"
  2914. :type 'boolean)
  2915. (defcustom org-latex-to-mathml-jar-file nil
  2916. "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
  2917. Use this to specify additional executable file say a jar file.
  2918. When using MathToWeb as the converter, specify the full-path to
  2919. your mathtoweb.jar file."
  2920. :group 'org-latex
  2921. :version "24.1"
  2922. :type '(choice
  2923. (const :tag "None" nil)
  2924. (file :tag "JAR file" :must-match t)))
  2925. (defcustom org-latex-to-mathml-convert-command nil
  2926. "Command to convert LaTeX fragments to MathML.
  2927. Replace format-specifiers in the command as noted below and use
  2928. `shell-command' to convert LaTeX to MathML.
  2929. %j: Executable file in fully expanded form as specified by
  2930. `org-latex-to-mathml-jar-file'.
  2931. %I: Input LaTeX file in fully expanded form
  2932. %o: Output MathML file
  2933. This command is used by `org-create-math-formula'.
  2934. When using MathToWeb as the converter, set this to
  2935. \"java -jar %j -unicode -force -df %o %I\"."
  2936. :group 'org-latex
  2937. :version "24.1"
  2938. :type '(choice
  2939. (const :tag "None" nil)
  2940. (string :tag "\nShell command")))
  2941. (defun org-format-latex-mathml-available-p ()
  2942. "Return t if `org-latex-to-mathml-convert-command' is usable."
  2943. (save-match-data
  2944. (when (and (boundp 'org-latex-to-mathml-convert-command)
  2945. org-latex-to-mathml-convert-command)
  2946. (let ((executable (car (split-string
  2947. org-latex-to-mathml-convert-command))))
  2948. (when (executable-find executable)
  2949. (if (string-match
  2950. "%j" org-latex-to-mathml-convert-command)
  2951. (file-readable-p org-latex-to-mathml-jar-file)
  2952. t))))))
  2953. (defcustom org-format-latex-header "\\documentclass{article}
  2954. \\usepackage[usenames]{color}
  2955. \\usepackage{amsmath}
  2956. \\usepackage[mathscr]{eucal}
  2957. \\pagestyle{empty} % do not remove
  2958. \[PACKAGES]
  2959. \[DEFAULT-PACKAGES]
  2960. % The settings below are copied from fullpage.sty
  2961. \\setlength{\\textwidth}{\\paperwidth}
  2962. \\addtolength{\\textwidth}{-3cm}
  2963. \\setlength{\\oddsidemargin}{1.5cm}
  2964. \\addtolength{\\oddsidemargin}{-2.54cm}
  2965. \\setlength{\\evensidemargin}{\\oddsidemargin}
  2966. \\setlength{\\textheight}{\\paperheight}
  2967. \\addtolength{\\textheight}{-\\headheight}
  2968. \\addtolength{\\textheight}{-\\headsep}
  2969. \\addtolength{\\textheight}{-\\footskip}
  2970. \\addtolength{\\textheight}{-3cm}
  2971. \\setlength{\\topmargin}{1.5cm}
  2972. \\addtolength{\\topmargin}{-2.54cm}"
  2973. "The document header used for processing LaTeX fragments.
  2974. It is imperative that this header make sure that no page number
  2975. appears on the page. The package defined in the variables
  2976. `org-export-latex-default-packages-alist' and `org-export-latex-packages-alist'
  2977. will either replace the placeholder \"[PACKAGES]\" in this header, or they
  2978. will be appended."
  2979. :group 'org-latex
  2980. :type 'string)
  2981. (defvar org-format-latex-header-extra nil)
  2982. (defun org-set-packages-alist (var val)
  2983. "Set the packages alist and make sure it has 3 elements per entry."
  2984. (set var (mapcar (lambda (x)
  2985. (if (and (consp x) (= (length x) 2))
  2986. (list (car x) (nth 1 x) t)
  2987. x))
  2988. val)))
  2989. (defun org-get-packages-alist (var)
  2990. "Get the packages alist and make sure it has 3 elements per entry."
  2991. (mapcar (lambda (x)
  2992. (if (and (consp x) (= (length x) 2))
  2993. (list (car x) (nth 1 x) t)
  2994. x))
  2995. (default-value var)))
  2996. ;; The following variables are defined here because is it also used
  2997. ;; when formatting latex fragments. Originally it was part of the
  2998. ;; LaTeX exporter, which is why the name includes "export".
  2999. (defcustom org-export-latex-default-packages-alist
  3000. '(("AUTO" "inputenc" t)
  3001. ("T1" "fontenc" t)
  3002. ("" "fixltx2e" nil)
  3003. ("" "graphicx" t)
  3004. ("" "longtable" nil)
  3005. ("" "float" nil)
  3006. ("" "wrapfig" nil)
  3007. ("" "soul" t)
  3008. ("" "textcomp" t)
  3009. ("" "marvosym" t)
  3010. ("" "wasysym" t)
  3011. ("" "latexsym" t)
  3012. ("" "amssymb" t)
  3013. ("" "hyperref" nil)
  3014. "\\tolerance=1000"
  3015. )
  3016. "Alist of default packages to be inserted in the header.
  3017. Change this only if one of the packages here causes an incompatibility
  3018. with another package you are using.
  3019. The packages in this list are needed by one part or another of Org-mode
  3020. to function properly.
  3021. - inputenc, fontenc: for basic font and character selection
  3022. - textcomp, marvosymb, wasysym, latexsym, amssym: for various symbols used
  3023. for interpreting the entities in `org-entities'. You can skip some of these
  3024. packages if you don't use any of the symbols in it.
  3025. - graphicx: for including images
  3026. - float, wrapfig: for figure placement
  3027. - longtable: for long tables
  3028. - hyperref: for cross references
  3029. Therefore you should not modify this variable unless you know what you
  3030. are doing. The one reason to change it anyway is that you might be loading
  3031. some other package that conflicts with one of the default packages.
  3032. Each cell is of the format \( \"options\" \"package\" snippet-flag\).
  3033. If SNIPPET-FLAG is t, the package also needs to be included when
  3034. compiling LaTeX snippets into images for inclusion into HTML."
  3035. :group 'org-export-latex
  3036. :set 'org-set-packages-alist
  3037. :get 'org-get-packages-alist
  3038. :version "24.1"
  3039. :type '(repeat
  3040. (choice
  3041. (list :tag "options/package pair"
  3042. (string :tag "options")
  3043. (string :tag "package")
  3044. (boolean :tag "Snippet"))
  3045. (string :tag "A line of LaTeX"))))
  3046. (defcustom org-export-latex-packages-alist nil
  3047. "Alist of packages to be inserted in every LaTeX header.
  3048. These will be inserted after `org-export-latex-default-packages-alist'.
  3049. Each cell is of the format \( \"options\" \"package\" snippet-flag \).
  3050. SNIPPET-FLAG, when t, indicates that this package is also needed when
  3051. turning LaTeX snippets into images for inclusion into HTML.
  3052. Make sure that you only list packages here which:
  3053. - you want in every file
  3054. - do not conflict with the default packages in
  3055. `org-export-latex-default-packages-alist'
  3056. - do not conflict with the setup in `org-format-latex-header'."
  3057. :group 'org-export-latex
  3058. :set 'org-set-packages-alist
  3059. :get 'org-get-packages-alist
  3060. :type '(repeat
  3061. (choice
  3062. (list :tag "options/package pair"
  3063. (string :tag "options")
  3064. (string :tag "package")
  3065. (boolean :tag "Snippet"))
  3066. (string :tag "A line of LaTeX"))))
  3067. (defgroup org-appearance nil
  3068. "Settings for Org-mode appearance."
  3069. :tag "Org Appearance"
  3070. :group 'org)
  3071. (defcustom org-level-color-stars-only nil
  3072. "Non-nil means fontify only the stars in each headline.
  3073. When nil, the entire headline is fontified.
  3074. Changing it requires restart of `font-lock-mode' to become effective
  3075. also in regions already fontified."
  3076. :group 'org-appearance
  3077. :type 'boolean)
  3078. (defcustom org-hide-leading-stars nil
  3079. "Non-nil means hide the first N-1 stars in a headline.
  3080. This works by using the face `org-hide' for these stars. This
  3081. face is white for a light background, and black for a dark
  3082. background. You may have to customize the face `org-hide' to
  3083. make this work.
  3084. Changing it requires restart of `font-lock-mode' to become effective
  3085. also in regions already fontified.
  3086. You may also set this on a per-file basis by adding one of the following
  3087. lines to the buffer:
  3088. #+STARTUP: hidestars
  3089. #+STARTUP: showstars"
  3090. :group 'org-appearance
  3091. :type 'boolean)
  3092. (defcustom org-hidden-keywords nil
  3093. "List of symbols corresponding to keywords to be hidden the org buffer.
  3094. For example, a value '(title) for this list will make the document's title
  3095. appear in the buffer without the initial #+TITLE: keyword."
  3096. :group 'org-appearance
  3097. :version "24.1"
  3098. :type '(set (const :tag "#+AUTHOR" author)
  3099. (const :tag "#+DATE" date)
  3100. (const :tag "#+EMAIL" email)
  3101. (const :tag "#+TITLE" title)))
  3102. (defcustom org-fontify-done-headline nil
  3103. "Non-nil means change the face of a headline if it is marked DONE.
  3104. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3105. When this is non-nil, the headline after the keyword is set to the
  3106. `org-headline-done' as an additional indication."
  3107. :group 'org-appearance
  3108. :type 'boolean)
  3109. (defcustom org-fontify-emphasized-text t
  3110. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3111. Changing this variable requires a restart of Emacs to take effect."
  3112. :group 'org-appearance
  3113. :type 'boolean)
  3114. (defcustom org-fontify-whole-heading-line nil
  3115. "Non-nil means fontify the whole line for headings.
  3116. This is useful when setting a background color for the
  3117. org-level-* faces."
  3118. :group 'org-appearance
  3119. :type 'boolean)
  3120. (defcustom org-highlight-latex-fragments-and-specials nil
  3121. "Non-nil means fontify what is treated specially by the exporters."
  3122. :group 'org-appearance
  3123. :type 'boolean)
  3124. (defcustom org-hide-emphasis-markers nil
  3125. "Non-nil mean font-lock should hide the emphasis marker characters."
  3126. :group 'org-appearance
  3127. :type 'boolean)
  3128. (defcustom org-pretty-entities nil
  3129. "Non-nil means show entities as UTF8 characters.
  3130. When nil, the \\name form remains in the buffer."
  3131. :group 'org-appearance
  3132. :version "24.1"
  3133. :type 'boolean)
  3134. (defcustom org-pretty-entities-include-sub-superscripts t
  3135. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  3136. :group 'org-appearance
  3137. :version "24.1"
  3138. :type 'boolean)
  3139. (defvar org-emph-re nil
  3140. "Regular expression for matching emphasis.
  3141. After a match, the match groups contain these elements:
  3142. 0 The match of the full regular expression, including the characters
  3143. before and after the proper match
  3144. 1 The character before the proper match, or empty at beginning of line
  3145. 2 The proper match, including the leading and trailing markers
  3146. 3 The leading marker like * or /, indicating the type of highlighting
  3147. 4 The text between the emphasis markers, not including the markers
  3148. 5 The character after the match, empty at the end of a line")
  3149. (defvar org-verbatim-re nil
  3150. "Regular expression for matching verbatim text.")
  3151. (defvar org-emphasis-regexp-components) ; defined just below
  3152. (defvar org-emphasis-alist) ; defined just below
  3153. (defun org-set-emph-re (var val)
  3154. "Set variable and compute the emphasis regular expression."
  3155. (set var val)
  3156. (when (and (boundp 'org-emphasis-alist)
  3157. (boundp 'org-emphasis-regexp-components)
  3158. org-emphasis-alist org-emphasis-regexp-components)
  3159. (let* ((e org-emphasis-regexp-components)
  3160. (pre (car e))
  3161. (post (nth 1 e))
  3162. (border (nth 2 e))
  3163. (body (nth 3 e))
  3164. (nl (nth 4 e))
  3165. (body1 (concat body "*?"))
  3166. (markers (mapconcat 'car org-emphasis-alist ""))
  3167. (vmarkers (mapconcat
  3168. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  3169. org-emphasis-alist "")))
  3170. ;; make sure special characters appear at the right position in the class
  3171. (if (string-match "\\^" markers)
  3172. (setq markers (concat (replace-match "" t t markers) "^")))
  3173. (if (string-match "-" markers)
  3174. (setq markers (concat (replace-match "" t t markers) "-")))
  3175. (if (string-match "\\^" vmarkers)
  3176. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3177. (if (string-match "-" vmarkers)
  3178. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3179. (if (> nl 0)
  3180. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3181. (int-to-string nl) "\\}")))
  3182. ;; Make the regexp
  3183. (setq org-emph-re
  3184. (concat "\\([" pre "]\\|^\\)"
  3185. "\\("
  3186. "\\([" markers "]\\)"
  3187. "\\("
  3188. "[^" border "]\\|"
  3189. "[^" border "]"
  3190. body1
  3191. "[^" border "]"
  3192. "\\)"
  3193. "\\3\\)"
  3194. "\\([" post "]\\|$\\)"))
  3195. (setq org-verbatim-re
  3196. (concat "\\([" pre "]\\|^\\)"
  3197. "\\("
  3198. "\\([" vmarkers "]\\)"
  3199. "\\("
  3200. "[^" border "]\\|"
  3201. "[^" border "]"
  3202. body1
  3203. "[^" border "]"
  3204. "\\)"
  3205. "\\3\\)"
  3206. "\\([" post "]\\|$\\)")))))
  3207. (defcustom org-emphasis-regexp-components
  3208. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  3209. "Components used to build the regular expression for emphasis.
  3210. This is a list with five entries. Terminology: In an emphasis string
  3211. like \" *strong word* \", we call the initial space PREMATCH, the final
  3212. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3213. and \"trong wor\" is the body. The different components in this variable
  3214. specify what is allowed/forbidden in each part:
  3215. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3216. post Chars allowed as postmatch. End of line will be allowed too.
  3217. border The chars *forbidden* as border characters.
  3218. body-regexp A regexp like \".\" to match a body character. Don't use
  3219. non-shy groups here, and don't allow newline here.
  3220. newline The maximum number of newlines allowed in an emphasis exp.
  3221. Use customize to modify this, or restart Emacs after changing it."
  3222. :group 'org-appearance
  3223. :set 'org-set-emph-re
  3224. :type '(list
  3225. (sexp :tag "Allowed chars in pre ")
  3226. (sexp :tag "Allowed chars in post ")
  3227. (sexp :tag "Forbidden chars in border ")
  3228. (sexp :tag "Regexp for body ")
  3229. (integer :tag "number of newlines allowed")
  3230. (option (boolean :tag "Please ignore this button"))))
  3231. (defcustom org-emphasis-alist
  3232. `(("*" bold "<b>" "</b>")
  3233. ("/" italic "<i>" "</i>")
  3234. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  3235. ("=" org-code "<code>" "</code>" verbatim)
  3236. ("~" org-verbatim "<code>" "</code>" verbatim)
  3237. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  3238. "<del>" "</del>")
  3239. )
  3240. "Special syntax for emphasized text.
  3241. Text starting and ending with a special character will be emphasized, for
  3242. example *bold*, _underlined_ and /italic/. This variable sets the marker
  3243. characters, the face to be used by font-lock for highlighting in Org-mode
  3244. Emacs buffers, and the HTML tags to be used for this.
  3245. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  3246. For DocBook export, see the variable `org-export-docbook-emphasis-alist'.
  3247. Use customize to modify this, or restart Emacs after changing it."
  3248. :group 'org-appearance
  3249. :set 'org-set-emph-re
  3250. :type '(repeat
  3251. (list
  3252. (string :tag "Marker character")
  3253. (choice
  3254. (face :tag "Font-lock-face")
  3255. (plist :tag "Face property list"))
  3256. (string :tag "HTML start tag")
  3257. (string :tag "HTML end tag")
  3258. (option (const verbatim)))))
  3259. (defvar org-protecting-blocks
  3260. '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
  3261. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3262. This is needed for font-lock setup.")
  3263. ;;; Miscellaneous options
  3264. (defgroup org-completion nil
  3265. "Completion in Org-mode."
  3266. :tag "Org Completion"
  3267. :group 'org)
  3268. (defcustom org-completion-use-ido nil
  3269. "Non-nil means use ido completion wherever possible.
  3270. Note that `ido-mode' must be active for this variable to be relevant.
  3271. If you decide to turn this variable on, you might well want to turn off
  3272. `org-outline-path-complete-in-steps'.
  3273. See also `org-completion-use-iswitchb'."
  3274. :group 'org-completion
  3275. :type 'boolean)
  3276. (defcustom org-completion-use-iswitchb nil
  3277. "Non-nil means use iswitchb completion wherever possible.
  3278. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3279. If you decide to turn this variable on, you might well want to turn off
  3280. `org-outline-path-complete-in-steps'.
  3281. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3282. :group 'org-completion
  3283. :type 'boolean)
  3284. (defcustom org-completion-fallback-command 'hippie-expand
  3285. "The expansion command called by \\[pcomplete] in normal context.
  3286. Normal means, no org-mode-specific context."
  3287. :group 'org-completion
  3288. :type 'function)
  3289. ;;; Functions and variables from their packages
  3290. ;; Declared here to avoid compiler warnings
  3291. ;; XEmacs only
  3292. (defvar outline-mode-menu-heading)
  3293. (defvar outline-mode-menu-show)
  3294. (defvar outline-mode-menu-hide)
  3295. (defvar zmacs-regions) ; XEmacs regions
  3296. ;; Emacs only
  3297. (defvar mark-active)
  3298. ;; Various packages
  3299. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  3300. (declare-function calendar-forward-day "cal-move" (arg))
  3301. (declare-function calendar-goto-date "cal-move" (date))
  3302. (declare-function calendar-goto-today "cal-move" ())
  3303. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3304. (defvar calc-embedded-close-formula)
  3305. (defvar calc-embedded-open-formula)
  3306. (declare-function cdlatex-tab "ext:cdlatex" ())
  3307. (declare-function cdlatex-compute-tables "ext:cdlatex" ())
  3308. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3309. (defvar font-lock-unfontify-region-function)
  3310. (declare-function iswitchb-read-buffer "iswitchb"
  3311. (prompt &optional default require-match start matches-set))
  3312. (defvar iswitchb-temp-buflist)
  3313. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3314. (defvar org-agenda-tags-todo-honor-ignore-options)
  3315. (declare-function org-agenda-skip "org-agenda" ())
  3316. (declare-function
  3317. org-agenda-format-item "org-agenda"
  3318. (extra txt &optional category tags dotime noprefix remove-re habitp))
  3319. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3320. (declare-function org-agenda-change-all-lines "org-agenda"
  3321. (newhead hdmarker &optional fixface just-this))
  3322. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3323. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3324. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3325. (beg end))
  3326. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3327. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3328. "org-agenda" (&optional end))
  3329. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3330. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3331. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3332. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3333. (declare-function org-indent-mode "org-indent" (&optional arg))
  3334. (declare-function parse-time-string "parse-time" (string))
  3335. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3336. (declare-function org-export-latex-fix-inputenc "org-latex" ())
  3337. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3338. (defvar remember-data-file)
  3339. (defvar texmathp-why)
  3340. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3341. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3342. (defvar w3m-current-url)
  3343. (defvar w3m-current-title)
  3344. (defvar org-latex-regexps)
  3345. ;;; Autoload and prepare some org modules
  3346. ;; Some table stuff that needs to be defined here, because it is used
  3347. ;; by the functions setting up org-mode or checking for table context.
  3348. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  3349. "Detect an org-type or table-type table.")
  3350. (defconst org-table-line-regexp "^[ \t]*|"
  3351. "Detect an org-type table line.")
  3352. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  3353. "Detect an org-type table line.")
  3354. (defconst org-table-hline-regexp "^[ \t]*|-"
  3355. "Detect an org-type table hline.")
  3356. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  3357. "Detect a table-type table hline.")
  3358. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  3359. "Detect the first line outside a table when searching from within it.
  3360. This works for both table types.")
  3361. ;; Autoload the functions in org-table.el that are needed by functions here.
  3362. (eval-and-compile
  3363. (org-autoload "org-table"
  3364. '(org-table-align org-table-begin org-table-blank-field
  3365. org-table-convert org-table-convert-region org-table-copy-down
  3366. org-table-copy-region org-table-create
  3367. org-table-create-or-convert-from-region
  3368. org-table-create-with-table.el org-table-current-dline
  3369. org-table-cut-region org-table-delete-column org-table-edit-field
  3370. org-table-edit-formulas org-table-end org-table-eval-formula
  3371. org-table-export org-table-field-info
  3372. org-table-get-stored-formulas org-table-goto-column
  3373. org-table-hline-and-move org-table-import org-table-insert-column
  3374. org-table-insert-hline org-table-insert-row org-table-iterate
  3375. org-table-justify-field-maybe org-table-kill-row
  3376. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  3377. org-table-move-column org-table-move-column-left
  3378. org-table-move-column-right org-table-move-row
  3379. org-table-move-row-down org-table-move-row-up
  3380. org-table-next-field org-table-next-row org-table-paste-rectangle
  3381. org-table-previous-field org-table-recalculate
  3382. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  3383. org-table-toggle-coordinate-overlays
  3384. org-table-toggle-formula-debugger org-table-wrap-region
  3385. orgtbl-mode turn-on-orgtbl org-table-to-lisp
  3386. orgtbl-to-generic orgtbl-to-tsv orgtbl-to-csv orgtbl-to-latex
  3387. orgtbl-to-orgtbl orgtbl-to-html orgtbl-to-texinfo)))
  3388. (defun org-at-table-p (&optional table-type)
  3389. "Return t if the cursor is inside an org-type table.
  3390. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  3391. (if org-enable-table-editor
  3392. (save-excursion
  3393. (beginning-of-line 1)
  3394. (looking-at (if table-type org-table-any-line-regexp
  3395. org-table-line-regexp)))
  3396. nil))
  3397. (defsubst org-table-p () (org-at-table-p))
  3398. (defun org-at-table.el-p ()
  3399. "Return t if and only if we are at a table.el table."
  3400. (and (org-at-table-p 'any)
  3401. (save-excursion
  3402. (goto-char (org-table-begin 'any))
  3403. (looking-at org-table1-hline-regexp))))
  3404. (defun org-table-recognize-table.el ()
  3405. "If there is a table.el table nearby, recognize it and move into it."
  3406. (if org-table-tab-recognizes-table.el
  3407. (if (org-at-table.el-p)
  3408. (progn
  3409. (beginning-of-line 1)
  3410. (if (looking-at org-table-dataline-regexp)
  3411. nil
  3412. (if (looking-at org-table1-hline-regexp)
  3413. (progn
  3414. (beginning-of-line 2)
  3415. (if (looking-at org-table-any-border-regexp)
  3416. (beginning-of-line -1)))))
  3417. (if (re-search-forward "|" (org-table-end t) t)
  3418. (progn
  3419. (require 'table)
  3420. (if (table--at-cell-p (point))
  3421. t
  3422. (message "recognizing table.el table...")
  3423. (table-recognize-table)
  3424. (message "recognizing table.el table...done")))
  3425. (error "This should not happen"))
  3426. t)
  3427. nil)
  3428. nil))
  3429. (defun org-at-table-hline-p ()
  3430. "Return t if the cursor is inside a hline in a table."
  3431. (if org-enable-table-editor
  3432. (save-excursion
  3433. (beginning-of-line 1)
  3434. (looking-at org-table-hline-regexp))
  3435. nil))
  3436. (defvar org-table-clean-did-remove-column nil)
  3437. (defun org-table-map-tables (function &optional quietly)
  3438. "Apply FUNCTION to the start of all tables in the buffer."
  3439. (save-excursion
  3440. (save-restriction
  3441. (widen)
  3442. (goto-char (point-min))
  3443. (while (re-search-forward org-table-any-line-regexp nil t)
  3444. (unless quietly
  3445. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
  3446. (beginning-of-line 1)
  3447. (when (and (looking-at org-table-line-regexp)
  3448. ;; Exclude tables in src/example/verbatim/clocktable blocks
  3449. (not (org-in-block-p '("src" "example"))))
  3450. (save-excursion (funcall function))
  3451. (or (looking-at org-table-line-regexp)
  3452. (forward-char 1)))
  3453. (re-search-forward org-table-any-border-regexp nil 1))))
  3454. (unless quietly (message "Mapping tables: done")))
  3455. ;; Declare and autoload functions from org-exp.el & Co
  3456. (declare-function org-default-export-plist "org-exp")
  3457. (declare-function org-infile-export-plist "org-exp")
  3458. (declare-function org-get-current-options "org-exp")
  3459. (eval-and-compile
  3460. (org-autoload "org-exp"
  3461. '(org-export org-export-visible
  3462. org-insert-export-options-template
  3463. org-table-clean-before-export))
  3464. (org-autoload "org-ascii"
  3465. '(org-export-as-ascii org-export-ascii-preprocess
  3466. org-export-as-ascii-to-buffer org-replace-region-by-ascii
  3467. org-export-region-as-ascii))
  3468. (org-autoload "org-latex"
  3469. '(org-export-as-latex-batch org-export-as-latex-to-buffer
  3470. org-replace-region-by-latex org-export-region-as-latex
  3471. org-export-as-latex org-export-as-pdf
  3472. org-export-as-pdf-and-open))
  3473. (org-autoload "org-html"
  3474. '(org-export-as-html-and-open
  3475. org-export-as-html-batch org-export-as-html-to-buffer
  3476. org-replace-region-by-html org-export-region-as-html
  3477. org-export-as-html))
  3478. (org-autoload "org-docbook"
  3479. '(org-export-as-docbook-batch org-export-as-docbook-to-buffer
  3480. org-replace-region-by-docbook org-export-region-as-docbook
  3481. org-export-as-docbook-pdf org-export-as-docbook-pdf-and-open
  3482. org-export-as-docbook))
  3483. (org-autoload "org-icalendar"
  3484. '(org-export-icalendar-this-file
  3485. org-export-icalendar-all-agenda-files
  3486. org-export-icalendar-combine-agenda-files))
  3487. (org-autoload "org-xoxo" '(org-export-as-xoxo))
  3488. (org-autoload "org-beamer" '(org-beamer-mode org-beamer-sectioning)))
  3489. ;; Declare and autoload functions from org-agenda.el
  3490. (eval-and-compile
  3491. (org-autoload "org-agenda"
  3492. '(org-agenda org-agenda-list org-search-view
  3493. org-todo-list org-tags-view org-agenda-list-stuck-projects
  3494. org-diary org-agenda-to-appt
  3495. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  3496. ;; Autoload org-remember
  3497. (eval-and-compile
  3498. (org-autoload "org-remember"
  3499. '(org-remember-insinuate org-remember-annotation
  3500. org-remember-apply-template org-remember org-remember-handler)))
  3501. (eval-and-compile
  3502. (org-autoload "org-capture"
  3503. '(org-capture org-capture-insert-template-here
  3504. org-capture-import-remember-templates)))
  3505. ;; Autoload org-clock.el
  3506. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  3507. (beg end))
  3508. (declare-function org-clock-update-mode-line "org-clock" ())
  3509. (declare-function org-resolve-clocks "org-clock"
  3510. (&optional also-non-dangling-p prompt last-valid))
  3511. (defvar org-clock-start-time)
  3512. (defvar org-clock-marker (make-marker)
  3513. "Marker recording the last clock-in.")
  3514. (defvar org-clock-hd-marker (make-marker)
  3515. "Marker recording the last clock-in, but the headline position.")
  3516. (defvar org-clock-heading ""
  3517. "The heading of the current clock entry.")
  3518. (defun org-clock-is-active ()
  3519. "Return non-nil if clock is currently running.
  3520. The return value is actually the clock marker."
  3521. (marker-buffer org-clock-marker))
  3522. (eval-and-compile
  3523. (org-autoload
  3524. "org-clock"
  3525. '(org-clock-in org-clock-out org-clock-cancel
  3526. org-clock-goto org-clock-sum org-clock-display
  3527. org-clock-remove-overlays org-clock-report
  3528. org-clocktable-shift org-dblock-write:clocktable
  3529. org-get-clocktable org-resolve-clocks)))
  3530. (defun org-clock-update-time-maybe ()
  3531. "If this is a CLOCK line, update it and return t.
  3532. Otherwise, return nil."
  3533. (interactive)
  3534. (save-excursion
  3535. (beginning-of-line 1)
  3536. (skip-chars-forward " \t")
  3537. (when (looking-at org-clock-string)
  3538. (let ((re (concat "[ \t]*" org-clock-string
  3539. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  3540. "\\([ \t]*=>.*\\)?\\)?"))
  3541. ts te h m s neg)
  3542. (cond
  3543. ((not (looking-at re))
  3544. nil)
  3545. ((not (match-end 2))
  3546. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3547. (> org-clock-marker (point))
  3548. (<= org-clock-marker (point-at-eol)))
  3549. ;; The clock is running here
  3550. (setq org-clock-start-time
  3551. (apply 'encode-time
  3552. (org-parse-time-string (match-string 1))))
  3553. (org-clock-update-mode-line)))
  3554. (t
  3555. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  3556. (end-of-line 1)
  3557. (setq ts (match-string 1)
  3558. te (match-string 3))
  3559. (setq s (- (org-float-time
  3560. (apply 'encode-time (org-parse-time-string te)))
  3561. (org-float-time
  3562. (apply 'encode-time (org-parse-time-string ts))))
  3563. neg (< s 0)
  3564. s (abs s)
  3565. h (floor (/ s 3600))
  3566. s (- s (* 3600 h))
  3567. m (floor (/ s 60))
  3568. s (- s (* 60 s)))
  3569. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  3570. t))))))
  3571. (defun org-check-running-clock ()
  3572. "Check if the current buffer contains the running clock.
  3573. If yes, offer to stop it and to save the buffer with the changes."
  3574. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3575. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3576. (buffer-name))))
  3577. (org-clock-out)
  3578. (when (y-or-n-p "Save changed buffer?")
  3579. (save-buffer))))
  3580. (defun org-clocktable-try-shift (dir n)
  3581. "Check if this line starts a clock table, if yes, shift the time block."
  3582. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  3583. (org-clocktable-shift dir n)))
  3584. ;; Autoload org-timer.el
  3585. (eval-and-compile
  3586. (org-autoload
  3587. "org-timer"
  3588. '(org-timer-start org-timer org-timer-item
  3589. org-timer-change-times-in-region
  3590. org-timer-set-timer
  3591. org-timer-reset-timers
  3592. org-timer-show-remaining-time)))
  3593. ;; Autoload org-feed.el
  3594. (eval-and-compile
  3595. (org-autoload
  3596. "org-feed"
  3597. '(org-feed-update org-feed-update-all org-feed-goto-inbox)))
  3598. ;; Autoload org-indent.el
  3599. ;; Define the variable already here, to make sure we have it.
  3600. (defvar org-indent-mode nil
  3601. "Non-nil if Org-Indent mode is enabled.
  3602. Use the command `org-indent-mode' to change this variable.")
  3603. (eval-and-compile
  3604. (org-autoload
  3605. "org-indent"
  3606. '(org-indent-mode)))
  3607. ;; Autoload org-mobile.el
  3608. (eval-and-compile
  3609. (org-autoload
  3610. "org-mobile"
  3611. '(org-mobile-push org-mobile-pull org-mobile-create-sumo-agenda)))
  3612. ;; Autoload archiving code
  3613. ;; The stuff that is needed for cycling and tags has to be defined here.
  3614. (defgroup org-archive nil
  3615. "Options concerning archiving in Org-mode."
  3616. :tag "Org Archive"
  3617. :group 'org-structure)
  3618. (defcustom org-archive-location "%s_archive::"
  3619. "The location where subtrees should be archived.
  3620. The value of this variable is a string, consisting of two parts,
  3621. separated by a double-colon. The first part is a filename and
  3622. the second part is a headline.
  3623. When the filename is omitted, archiving happens in the same file.
  3624. %s in the filename will be replaced by the current file
  3625. name (without the directory part). Archiving to a different file
  3626. is useful to keep archived entries from contributing to the
  3627. Org-mode Agenda.
  3628. The archived entries will be filed as subtrees of the specified
  3629. headline. When the headline is omitted, the subtrees are simply
  3630. filed away at the end of the file, as top-level entries. Also in
  3631. the heading you can use %s to represent the file name, this can be
  3632. useful when using the same archive for a number of different files.
  3633. Here are a few examples:
  3634. \"%s_archive::\"
  3635. If the current file is Projects.org, archive in file
  3636. Projects.org_archive, as top-level trees. This is the default.
  3637. \"::* Archived Tasks\"
  3638. Archive in the current file, under the top-level headline
  3639. \"* Archived Tasks\".
  3640. \"~/org/archive.org::\"
  3641. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3642. \"~/org/archive.org::* From %s\"
  3643. Archive in file ~/org/archive.org (absolute path), under headlines
  3644. \"From FILENAME\" where file name is the current file name.
  3645. \"basement::** Finished Tasks\"
  3646. Archive in file ./basement (relative path), as level 3 trees
  3647. below the level 2 heading \"** Finished Tasks\".
  3648. You may set this option on a per-file basis by adding to the buffer a
  3649. line like
  3650. #+ARCHIVE: basement::** Finished Tasks
  3651. You may also define it locally for a subtree by setting an ARCHIVE property
  3652. in the entry. If such a property is found in an entry, or anywhere up
  3653. the hierarchy, it will be used."
  3654. :group 'org-archive
  3655. :type 'string)
  3656. (defcustom org-archive-tag "ARCHIVE"
  3657. "The tag that marks a subtree as archived.
  3658. An archived subtree does not open during visibility cycling, and does
  3659. not contribute to the agenda listings.
  3660. After changing this, font-lock must be restarted in the relevant buffers to
  3661. get the proper fontification."
  3662. :group 'org-archive
  3663. :group 'org-keywords
  3664. :type 'string)
  3665. (defcustom org-agenda-skip-archived-trees t
  3666. "Non-nil means the agenda will skip any items located in archived trees.
  3667. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3668. variable is no longer recommended, you should leave it at the value t.
  3669. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3670. :group 'org-archive
  3671. :group 'org-agenda-skip
  3672. :type 'boolean)
  3673. (defcustom org-columns-skip-archived-trees t
  3674. "Non-nil means ignore archived trees when creating column view."
  3675. :group 'org-archive
  3676. :group 'org-properties
  3677. :type 'boolean)
  3678. (defcustom org-cycle-open-archived-trees nil
  3679. "Non-nil means `org-cycle' will open archived trees.
  3680. An archived tree is a tree marked with the tag ARCHIVE.
  3681. When nil, archived trees will stay folded. You can still open them with
  3682. normal outline commands like `show-all', but not with the cycling commands."
  3683. :group 'org-archive
  3684. :group 'org-cycle
  3685. :type 'boolean)
  3686. (defcustom org-sparse-tree-open-archived-trees nil
  3687. "Non-nil means sparse tree construction shows matches in archived trees.
  3688. When nil, matches in these trees are highlighted, but the trees are kept in
  3689. collapsed state."
  3690. :group 'org-archive
  3691. :group 'org-sparse-trees
  3692. :type 'boolean)
  3693. (defun org-cycle-hide-archived-subtrees (state)
  3694. "Re-hide all archived subtrees after a visibility state change."
  3695. (when (and (not org-cycle-open-archived-trees)
  3696. (not (memq state '(overview folded))))
  3697. (save-excursion
  3698. (let* ((globalp (memq state '(contents all)))
  3699. (beg (if globalp (point-min) (point)))
  3700. (end (if globalp (point-max) (org-end-of-subtree t))))
  3701. (org-hide-archived-subtrees beg end)
  3702. (goto-char beg)
  3703. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3704. (message "%s" (substitute-command-keys
  3705. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3706. (defun org-force-cycle-archived ()
  3707. "Cycle subtree even if it is archived."
  3708. (interactive)
  3709. (setq this-command 'org-cycle)
  3710. (let ((org-cycle-open-archived-trees t))
  3711. (call-interactively 'org-cycle)))
  3712. (defun org-hide-archived-subtrees (beg end)
  3713. "Re-hide all archived subtrees after a visibility state change."
  3714. (save-excursion
  3715. (let* ((re (concat ":" org-archive-tag ":")))
  3716. (goto-char beg)
  3717. (while (re-search-forward re end t)
  3718. (when (org-at-heading-p)
  3719. (org-flag-subtree t)
  3720. (org-end-of-subtree t))))))
  3721. (defun org-flag-subtree (flag)
  3722. (save-excursion
  3723. (org-back-to-heading t)
  3724. (outline-end-of-heading)
  3725. (outline-flag-region (point)
  3726. (progn (org-end-of-subtree t) (point))
  3727. flag)))
  3728. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3729. (eval-and-compile
  3730. (org-autoload "org-archive"
  3731. '(org-add-archive-files org-archive-subtree
  3732. org-archive-to-archive-sibling org-toggle-archive-tag
  3733. org-archive-subtree-default
  3734. org-archive-subtree-default-with-confirmation)))
  3735. ;; Autoload Column View Code
  3736. (declare-function org-columns-number-to-string "org-colview")
  3737. (declare-function org-columns-get-format-and-top-level "org-colview")
  3738. (declare-function org-columns-compute "org-colview")
  3739. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  3740. '(org-columns-number-to-string org-columns-get-format-and-top-level
  3741. org-columns-compute org-agenda-columns org-columns-remove-overlays
  3742. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  3743. ;; Autoload ID code
  3744. (declare-function org-id-store-link "org-id")
  3745. (declare-function org-id-locations-load "org-id")
  3746. (declare-function org-id-locations-save "org-id")
  3747. (defvar org-id-track-globally)
  3748. (org-autoload "org-id"
  3749. '(org-id-get-create org-id-new org-id-copy org-id-get
  3750. org-id-get-with-outline-path-completion
  3751. org-id-get-with-outline-drilling org-id-store-link
  3752. org-id-goto org-id-find org-id-store-link))
  3753. ;; Autoload Plotting Code
  3754. (org-autoload "org-plot"
  3755. '(org-plot/gnuplot))
  3756. ;;; Variables for pre-computed regular expressions, all buffer local
  3757. (defvar org-drawer-regexp nil
  3758. "Matches first line of a hidden block.")
  3759. (make-variable-buffer-local 'org-drawer-regexp)
  3760. (defvar org-todo-regexp nil
  3761. "Matches any of the TODO state keywords.")
  3762. (make-variable-buffer-local 'org-todo-regexp)
  3763. (defvar org-not-done-regexp nil
  3764. "Matches any of the TODO state keywords except the last one.")
  3765. (make-variable-buffer-local 'org-not-done-regexp)
  3766. (defvar org-not-done-heading-regexp nil
  3767. "Matches a TODO headline that is not done.")
  3768. (make-variable-buffer-local 'org-not-done-regexp)
  3769. (defvar org-todo-line-regexp nil
  3770. "Matches a headline and puts TODO state into group 2 if present.")
  3771. (make-variable-buffer-local 'org-todo-line-regexp)
  3772. (defvar org-complex-heading-regexp nil
  3773. "Matches a headline and puts everything into groups:
  3774. group 1: the stars
  3775. group 2: The todo keyword, maybe
  3776. group 3: Priority cookie
  3777. group 4: True headline
  3778. group 5: Tags")
  3779. (make-variable-buffer-local 'org-complex-heading-regexp)
  3780. (defvar org-complex-heading-regexp-format nil
  3781. "Printf format to make regexp to match an exact headline.
  3782. This regexp will match the headline of any node which has the
  3783. exact headline text that is put into the format, but may have any
  3784. TODO state, priority and tags.")
  3785. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  3786. (defvar org-todo-line-tags-regexp nil
  3787. "Matches a headline and puts TODO state into group 2 if present.
  3788. Also put tags into group 4 if tags are present.")
  3789. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3790. (defvar org-ds-keyword-length 12
  3791. "Maximum length of the Deadline and SCHEDULED keywords.")
  3792. (make-variable-buffer-local 'org-ds-keyword-length)
  3793. (defvar org-deadline-regexp nil
  3794. "Matches the DEADLINE keyword.")
  3795. (make-variable-buffer-local 'org-deadline-regexp)
  3796. (defvar org-deadline-time-regexp nil
  3797. "Matches the DEADLINE keyword together with a time stamp.")
  3798. (make-variable-buffer-local 'org-deadline-time-regexp)
  3799. (defvar org-deadline-line-regexp nil
  3800. "Matches the DEADLINE keyword and the rest of the line.")
  3801. (make-variable-buffer-local 'org-deadline-line-regexp)
  3802. (defvar org-scheduled-regexp nil
  3803. "Matches the SCHEDULED keyword.")
  3804. (make-variable-buffer-local 'org-scheduled-regexp)
  3805. (defvar org-scheduled-time-regexp nil
  3806. "Matches the SCHEDULED keyword together with a time stamp.")
  3807. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3808. (defvar org-closed-time-regexp nil
  3809. "Matches the CLOSED keyword together with a time stamp.")
  3810. (make-variable-buffer-local 'org-closed-time-regexp)
  3811. (defvar org-keyword-time-regexp nil
  3812. "Matches any of the 4 keywords, together with the time stamp.")
  3813. (make-variable-buffer-local 'org-keyword-time-regexp)
  3814. (defvar org-keyword-time-not-clock-regexp nil
  3815. "Matches any of the 3 keywords, together with the time stamp.")
  3816. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3817. (defvar org-maybe-keyword-time-regexp nil
  3818. "Matches a timestamp, possibly preceded by a keyword.")
  3819. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3820. (defvar org-planning-or-clock-line-re nil
  3821. "Matches a line with planning or clock info.")
  3822. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  3823. (defvar org-all-time-keywords nil
  3824. "List of time keywords.")
  3825. (make-variable-buffer-local 'org-all-time-keywords)
  3826. (defconst org-plain-time-of-day-regexp
  3827. (concat
  3828. "\\(\\<[012]?[0-9]"
  3829. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3830. "\\(--?"
  3831. "\\(\\<[012]?[0-9]"
  3832. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3833. "\\)?")
  3834. "Regular expression to match a plain time or time range.
  3835. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3836. groups carry important information:
  3837. 0 the full match
  3838. 1 the first time, range or not
  3839. 8 the second time, if it is a range.")
  3840. (defconst org-plain-time-extension-regexp
  3841. (concat
  3842. "\\(\\<[012]?[0-9]"
  3843. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3844. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  3845. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  3846. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3847. groups carry important information:
  3848. 0 the full match
  3849. 7 hours of duration
  3850. 9 minutes of duration")
  3851. (defconst org-stamp-time-of-day-regexp
  3852. (concat
  3853. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  3854. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  3855. "\\(--?"
  3856. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  3857. "Regular expression to match a timestamp time or time range.
  3858. After a match, the following groups carry important information:
  3859. 0 the full match
  3860. 1 date plus weekday, for back referencing to make sure both times are on the same day
  3861. 2 the first time, range or not
  3862. 4 the second time, if it is a range.")
  3863. (defconst org-startup-options
  3864. '(("fold" org-startup-folded t)
  3865. ("overview" org-startup-folded t)
  3866. ("nofold" org-startup-folded nil)
  3867. ("showall" org-startup-folded nil)
  3868. ("showeverything" org-startup-folded showeverything)
  3869. ("content" org-startup-folded content)
  3870. ("indent" org-startup-indented t)
  3871. ("noindent" org-startup-indented nil)
  3872. ("hidestars" org-hide-leading-stars t)
  3873. ("showstars" org-hide-leading-stars nil)
  3874. ("odd" org-odd-levels-only t)
  3875. ("oddeven" org-odd-levels-only nil)
  3876. ("align" org-startup-align-all-tables t)
  3877. ("noalign" org-startup-align-all-tables nil)
  3878. ("inlineimages" org-startup-with-inline-images t)
  3879. ("noinlineimages" org-startup-with-inline-images nil)
  3880. ("customtime" org-display-custom-times t)
  3881. ("logdone" org-log-done time)
  3882. ("lognotedone" org-log-done note)
  3883. ("nologdone" org-log-done nil)
  3884. ("lognoteclock-out" org-log-note-clock-out t)
  3885. ("nolognoteclock-out" org-log-note-clock-out nil)
  3886. ("logrepeat" org-log-repeat state)
  3887. ("lognoterepeat" org-log-repeat note)
  3888. ("nologrepeat" org-log-repeat nil)
  3889. ("logreschedule" org-log-reschedule time)
  3890. ("lognotereschedule" org-log-reschedule note)
  3891. ("nologreschedule" org-log-reschedule nil)
  3892. ("logredeadline" org-log-redeadline time)
  3893. ("lognoteredeadline" org-log-redeadline note)
  3894. ("nologredeadline" org-log-redeadline nil)
  3895. ("logrefile" org-log-refile time)
  3896. ("lognoterefile" org-log-refile note)
  3897. ("nologrefile" org-log-refile nil)
  3898. ("fninline" org-footnote-define-inline t)
  3899. ("nofninline" org-footnote-define-inline nil)
  3900. ("fnlocal" org-footnote-section nil)
  3901. ("fnauto" org-footnote-auto-label t)
  3902. ("fnprompt" org-footnote-auto-label nil)
  3903. ("fnconfirm" org-footnote-auto-label confirm)
  3904. ("fnplain" org-footnote-auto-label plain)
  3905. ("fnadjust" org-footnote-auto-adjust t)
  3906. ("nofnadjust" org-footnote-auto-adjust nil)
  3907. ("constcgs" constants-unit-system cgs)
  3908. ("constSI" constants-unit-system SI)
  3909. ("noptag" org-tag-persistent-alist nil)
  3910. ("hideblocks" org-hide-block-startup t)
  3911. ("nohideblocks" org-hide-block-startup nil)
  3912. ("beamer" org-startup-with-beamer-mode t)
  3913. ("entitiespretty" org-pretty-entities t)
  3914. ("entitiesplain" org-pretty-entities nil))
  3915. "Variable associated with STARTUP options for org-mode.
  3916. Each element is a list of three items: The startup options as written
  3917. in the #+STARTUP line, the corresponding variable, and the value to
  3918. set this variable to if the option is found. An optional forth element PUSH
  3919. means to push this value onto the list in the variable.")
  3920. (defun org-update-property-plist (key val props)
  3921. "Update PROPS with KEY and VAL."
  3922. (let* ((appending (string= "+" (substring key (- (length key) 1))))
  3923. (key (if appending (substring key 0 (- (length key) 1)) key))
  3924. (remainder (org-remove-if (lambda (p) (string= (car p) key)) props))
  3925. (previous (cdr (assoc key props))))
  3926. (if appending
  3927. (cons (cons key (if previous (concat previous " " val) val)) remainder)
  3928. (cons (cons key val) remainder))))
  3929. (defconst org-block-regexp
  3930. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  3931. "Regular expression for hiding blocks.")
  3932. (defconst org-heading-keyword-regexp-format
  3933. "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  3934. "Printf format for a regexp matching an headline with some keyword.
  3935. This regexp will match the headline of any node which has the
  3936. exact keyword that is put into the format. The keyword isn't in
  3937. any group by default, but the stars and the body are.")
  3938. (defconst org-heading-keyword-maybe-regexp-format
  3939. "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
  3940. "Printf format for a regexp matching an headline, possibly with some keyword.
  3941. This regexp can match any headline with the specified keyword, or
  3942. without a keyword. The keyword isn't in any group by default,
  3943. but the stars and the body are.")
  3944. (defun org-set-regexps-and-options ()
  3945. "Precompute regular expressions for current buffer."
  3946. (when (eq major-mode 'org-mode)
  3947. (org-set-local 'org-todo-kwd-alist nil)
  3948. (org-set-local 'org-todo-key-alist nil)
  3949. (org-set-local 'org-todo-key-trigger nil)
  3950. (org-set-local 'org-todo-keywords-1 nil)
  3951. (org-set-local 'org-done-keywords nil)
  3952. (org-set-local 'org-todo-heads nil)
  3953. (org-set-local 'org-todo-sets nil)
  3954. (org-set-local 'org-todo-log-states nil)
  3955. (org-set-local 'org-file-properties nil)
  3956. (org-set-local 'org-file-tags nil)
  3957. (let ((re (org-make-options-regexp
  3958. '("CATEGORY" "TODO" "COLUMNS"
  3959. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  3960. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE" "LATEX_CLASS"
  3961. "OPTIONS")
  3962. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  3963. (splitre "[ \t]+")
  3964. (scripts org-use-sub-superscripts)
  3965. kwds kws0 kwsa key log value cat arch tags const links hw dws
  3966. tail sep kws1 prio props ftags drawers beamer-p
  3967. ext-setup-or-nil setup-contents (start 0))
  3968. (save-excursion
  3969. (save-restriction
  3970. (widen)
  3971. (goto-char (point-min))
  3972. (while (or (and ext-setup-or-nil
  3973. (string-match re ext-setup-or-nil start)
  3974. (setq start (match-end 0)))
  3975. (and (setq ext-setup-or-nil nil start 0)
  3976. (re-search-forward re nil t)))
  3977. (setq key (upcase (match-string 1 ext-setup-or-nil))
  3978. value (org-match-string-no-properties 2 ext-setup-or-nil))
  3979. (if (stringp value) (setq value (org-trim value)))
  3980. (cond
  3981. ((equal key "CATEGORY")
  3982. (setq cat value))
  3983. ((member key '("SEQ_TODO" "TODO"))
  3984. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3985. ((equal key "TYP_TODO")
  3986. (push (cons 'type (org-split-string value splitre)) kwds))
  3987. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  3988. ;; general TODO-like setup
  3989. (push (cons (intern (downcase (match-string 1 key)))
  3990. (org-split-string value splitre)) kwds))
  3991. ((equal key "TAGS")
  3992. (setq tags (append tags (if tags '("\\n") nil)
  3993. (org-split-string value splitre))))
  3994. ((equal key "COLUMNS")
  3995. (org-set-local 'org-columns-default-format value))
  3996. ((equal key "LINK")
  3997. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3998. (push (cons (match-string 1 value)
  3999. (org-trim (match-string 2 value)))
  4000. links)))
  4001. ((equal key "PRIORITIES")
  4002. (setq prio (org-split-string value " +")))
  4003. ((equal key "PROPERTY")
  4004. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4005. (setq props (org-update-property-plist (match-string 1 value)
  4006. (match-string 2 value)
  4007. props))))
  4008. ((equal key "FILETAGS")
  4009. (when (string-match "\\S-" value)
  4010. (setq ftags
  4011. (append
  4012. ftags
  4013. (apply 'append
  4014. (mapcar (lambda (x) (org-split-string x ":"))
  4015. (org-split-string value)))))))
  4016. ((equal key "DRAWERS")
  4017. (setq drawers (delete-dups (append org-drawers (org-split-string value splitre)))))
  4018. ((equal key "CONSTANTS")
  4019. (setq const (append const (org-split-string value splitre))))
  4020. ((equal key "STARTUP")
  4021. (let ((opts (org-split-string value splitre))
  4022. l var val)
  4023. (while (setq l (pop opts))
  4024. (when (setq l (assoc l org-startup-options))
  4025. (setq var (nth 1 l) val (nth 2 l))
  4026. (if (not (nth 3 l))
  4027. (set (make-local-variable var) val)
  4028. (if (not (listp (symbol-value var)))
  4029. (set (make-local-variable var) nil))
  4030. (set (make-local-variable var) (symbol-value var))
  4031. (add-to-list var val))))))
  4032. ((equal key "ARCHIVE")
  4033. (setq arch value)
  4034. (remove-text-properties 0 (length arch)
  4035. '(face t fontified t) arch))
  4036. ((equal key "LATEX_CLASS")
  4037. (setq beamer-p (equal value "beamer")))
  4038. ((equal key "OPTIONS")
  4039. (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value)
  4040. (setq scripts (read (match-string 2 value)))))
  4041. ((equal key "SETUPFILE")
  4042. (setq setup-contents (org-file-contents
  4043. (expand-file-name
  4044. (org-remove-double-quotes value))
  4045. 'noerror))
  4046. (if (not ext-setup-or-nil)
  4047. (setq ext-setup-or-nil setup-contents start 0)
  4048. (setq ext-setup-or-nil
  4049. (concat (substring ext-setup-or-nil 0 start)
  4050. "\n" setup-contents "\n"
  4051. (substring ext-setup-or-nil start)))))))
  4052. ;; search for property blocks
  4053. (goto-char (point-min))
  4054. (while (re-search-forward org-block-regexp nil t)
  4055. (when (equal "PROPERTY" (upcase (match-string 1)))
  4056. (setq value (replace-regexp-in-string
  4057. "[\n\r]" " " (match-string 4)))
  4058. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4059. (setq props (org-update-property-plist (match-string 1 value)
  4060. (match-string 2 value)
  4061. props)))))))
  4062. (org-set-local 'org-use-sub-superscripts scripts)
  4063. (when cat
  4064. (org-set-local 'org-category (intern cat))
  4065. (push (cons "CATEGORY" cat) props))
  4066. (when prio
  4067. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  4068. (setq prio (mapcar 'string-to-char prio))
  4069. (org-set-local 'org-highest-priority (nth 0 prio))
  4070. (org-set-local 'org-lowest-priority (nth 1 prio))
  4071. (org-set-local 'org-default-priority (nth 2 prio)))
  4072. (and props (org-set-local 'org-file-properties (nreverse props)))
  4073. (and ftags (org-set-local 'org-file-tags
  4074. (mapcar 'org-add-prop-inherited ftags)))
  4075. (and drawers (org-set-local 'org-drawers drawers))
  4076. (and arch (org-set-local 'org-archive-location arch))
  4077. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  4078. ;; Process the TODO keywords
  4079. (unless kwds
  4080. ;; Use the global values as if they had been given locally.
  4081. (setq kwds (default-value 'org-todo-keywords))
  4082. (if (stringp (car kwds))
  4083. (setq kwds (list (cons org-todo-interpretation
  4084. (default-value 'org-todo-keywords)))))
  4085. (setq kwds (reverse kwds)))
  4086. (setq kwds (nreverse kwds))
  4087. (let (inter kws kw)
  4088. (while (setq kws (pop kwds))
  4089. (let ((kws (or
  4090. (run-hook-with-args-until-success
  4091. 'org-todo-setup-filter-hook kws)
  4092. kws)))
  4093. (setq inter (pop kws) sep (member "|" kws)
  4094. kws0 (delete "|" (copy-sequence kws))
  4095. kwsa nil
  4096. kws1 (mapcar
  4097. (lambda (x)
  4098. ;; 1 2
  4099. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  4100. (progn
  4101. (setq kw (match-string 1 x)
  4102. key (and (match-end 2) (match-string 2 x))
  4103. log (org-extract-log-state-settings x))
  4104. (push (cons kw (and key (string-to-char key))) kwsa)
  4105. (and log (push log org-todo-log-states))
  4106. kw)
  4107. (error "Invalid TODO keyword %s" x)))
  4108. kws0)
  4109. kwsa (if kwsa (append '((:startgroup))
  4110. (nreverse kwsa)
  4111. '((:endgroup))))
  4112. hw (car kws1)
  4113. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  4114. tail (list inter hw (car dws) (org-last dws))))
  4115. (add-to-list 'org-todo-heads hw 'append)
  4116. (push kws1 org-todo-sets)
  4117. (setq org-done-keywords (append org-done-keywords dws nil))
  4118. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  4119. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  4120. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  4121. (setq org-todo-sets (nreverse org-todo-sets)
  4122. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4123. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  4124. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  4125. ;; Process the constants
  4126. (when const
  4127. (let (e cst)
  4128. (while (setq e (pop const))
  4129. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  4130. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  4131. (setq org-table-formula-constants-local cst)))
  4132. ;; Process the tags.
  4133. (when tags
  4134. (let (e tgs)
  4135. (while (setq e (pop tags))
  4136. (cond
  4137. ((equal e "{") (push '(:startgroup) tgs))
  4138. ((equal e "}") (push '(:endgroup) tgs))
  4139. ((equal e "\\n") (push '(:newline) tgs))
  4140. ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e)
  4141. (push (cons (match-string 1 e)
  4142. (string-to-char (match-string 2 e)))
  4143. tgs))
  4144. (t (push (list e) tgs))))
  4145. (org-set-local 'org-tag-alist nil)
  4146. (while (setq e (pop tgs))
  4147. (or (and (stringp (car e))
  4148. (assoc (car e) org-tag-alist))
  4149. (push e org-tag-alist)))))
  4150. ;; Compute the regular expressions and other local variables.
  4151. ;; Using `org-outline-regexp-bol' would complicate them much,
  4152. ;; because of the fixed white space at the end of that string.
  4153. (if (not org-done-keywords)
  4154. (setq org-done-keywords (and org-todo-keywords-1
  4155. (list (org-last org-todo-keywords-1)))))
  4156. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  4157. (length org-scheduled-string)
  4158. (length org-clock-string)
  4159. (length org-closed-string)))
  4160. org-drawer-regexp
  4161. (concat "^[ \t]*:\\("
  4162. (mapconcat 'regexp-quote org-drawers "\\|")
  4163. "\\):[ \t]*$")
  4164. org-not-done-keywords
  4165. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  4166. org-todo-regexp
  4167. (concat "\\("
  4168. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4169. "\\)")
  4170. org-not-done-regexp
  4171. (concat "\\("
  4172. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  4173. "\\)")
  4174. org-not-done-heading-regexp
  4175. (format org-heading-keyword-regexp-format org-not-done-regexp)
  4176. org-todo-line-regexp
  4177. (format org-heading-keyword-maybe-regexp-format org-todo-regexp)
  4178. org-complex-heading-regexp
  4179. (concat "^\\(\\*+\\)"
  4180. "\\(?: +" org-todo-regexp "\\)?"
  4181. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4182. "\\(?: +\\(.*?\\)\\)?"
  4183. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
  4184. "[ \t]*$")
  4185. org-complex-heading-regexp-format
  4186. (concat "^\\(\\*+\\)"
  4187. "\\(?: +" org-todo-regexp "\\)?"
  4188. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4189. "\\(?: +"
  4190. ;; Stats cookies can be stuck to body.
  4191. "\\(?:\\[[0-9%%/]+\\] *\\)?"
  4192. "\\(%s\\)"
  4193. "\\(?: *\\[[0-9%%/]+\\]\\)?"
  4194. "\\)"
  4195. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
  4196. "[ \t]*$")
  4197. org-todo-line-tags-regexp
  4198. (concat "^\\(\\*+\\)"
  4199. "\\(?: +" org-todo-regexp "\\)?"
  4200. "\\(?: +\\(.*?\\)\\)?"
  4201. (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
  4202. "[ \t]*$")
  4203. org-deadline-regexp (concat "\\<" org-deadline-string)
  4204. org-deadline-time-regexp
  4205. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  4206. org-deadline-line-regexp
  4207. (concat "\\<\\(" org-deadline-string "\\).*")
  4208. org-scheduled-regexp
  4209. (concat "\\<" org-scheduled-string)
  4210. org-scheduled-time-regexp
  4211. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  4212. org-closed-time-regexp
  4213. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  4214. org-keyword-time-regexp
  4215. (concat "\\<\\(" org-scheduled-string
  4216. "\\|" org-deadline-string
  4217. "\\|" org-closed-string
  4218. "\\|" org-clock-string "\\)"
  4219. " *[[<]\\([^]>]+\\)[]>]")
  4220. org-keyword-time-not-clock-regexp
  4221. (concat "\\<\\(" org-scheduled-string
  4222. "\\|" org-deadline-string
  4223. "\\|" org-closed-string
  4224. "\\)"
  4225. " *[[<]\\([^]>]+\\)[]>]")
  4226. org-maybe-keyword-time-regexp
  4227. (concat "\\(\\<\\(" org-scheduled-string
  4228. "\\|" org-deadline-string
  4229. "\\|" org-closed-string
  4230. "\\|" org-clock-string "\\)\\)?"
  4231. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4232. org-planning-or-clock-line-re
  4233. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  4234. "\\|" org-deadline-string
  4235. "\\|" org-closed-string "\\|" org-clock-string
  4236. "\\)\\>\\)")
  4237. org-all-time-keywords
  4238. (mapcar (lambda (w) (substring w 0 -1))
  4239. (list org-scheduled-string org-deadline-string
  4240. org-clock-string org-closed-string))
  4241. )
  4242. (org-compute-latex-and-specials-regexp)
  4243. (org-set-font-lock-defaults))))
  4244. (defun org-file-contents (file &optional noerror)
  4245. "Return the contents of FILE, as a string."
  4246. (if (or (not file)
  4247. (not (file-readable-p file)))
  4248. (if noerror
  4249. (progn
  4250. (message "Cannot read file \"%s\"" file)
  4251. (ding) (sit-for 2)
  4252. "")
  4253. (error "Cannot read file \"%s\"" file))
  4254. (with-temp-buffer
  4255. (insert-file-contents file)
  4256. (buffer-string))))
  4257. (defun org-extract-log-state-settings (x)
  4258. "Extract the log state setting from a TODO keyword string.
  4259. This will extract info from a string like \"WAIT(w@/!)\"."
  4260. (let (kw key log1 log2)
  4261. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4262. (setq kw (match-string 1 x)
  4263. key (and (match-end 2) (match-string 2 x))
  4264. log1 (and (match-end 3) (match-string 3 x))
  4265. log2 (and (match-end 4) (match-string 4 x)))
  4266. (and (or log1 log2)
  4267. (list kw
  4268. (and log1 (if (equal log1 "!") 'time 'note))
  4269. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4270. (defun org-remove-keyword-keys (list)
  4271. "Remove a pair of parenthesis at the end of each string in LIST."
  4272. (mapcar (lambda (x)
  4273. (if (string-match "(.*)$" x)
  4274. (substring x 0 (match-beginning 0))
  4275. x))
  4276. list))
  4277. (defun org-assign-fast-keys (alist)
  4278. "Assign fast keys to a keyword-key alist.
  4279. Respect keys that are already there."
  4280. (let (new e (alt ?0))
  4281. (while (setq e (pop alist))
  4282. (if (or (memq (car e) '(:newline :endgroup :startgroup))
  4283. (cdr e)) ;; Key already assigned.
  4284. (push e new)
  4285. (let ((clist (string-to-list (downcase (car e))))
  4286. (used (append new alist)))
  4287. (when (= (car clist) ?@)
  4288. (pop clist))
  4289. (while (and clist (rassoc (car clist) used))
  4290. (pop clist))
  4291. (unless clist
  4292. (while (rassoc alt used)
  4293. (incf alt)))
  4294. (push (cons (car e) (or (car clist) alt)) new))))
  4295. (nreverse new)))
  4296. ;;; Some variables used in various places
  4297. (defvar org-window-configuration nil
  4298. "Used in various places to store a window configuration.")
  4299. (defvar org-selected-window nil
  4300. "Used in various places to store a window configuration.")
  4301. (defvar org-finish-function nil
  4302. "Function to be called when `C-c C-c' is used.
  4303. This is for getting out of special buffers like remember.")
  4304. ;; FIXME: Occasionally check by commenting these, to make sure
  4305. ;; no other functions uses these, forgetting to let-bind them.
  4306. (defvar entry)
  4307. (defvar org-last-state)
  4308. (defvar date)
  4309. ;; Defined somewhere in this file, but used before definition.
  4310. (defvar org-entities) ;; defined in org-entities.el
  4311. (defvar org-struct-menu)
  4312. (defvar org-org-menu)
  4313. (defvar org-tbl-menu)
  4314. ;;;; Define the Org-mode
  4315. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4316. (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"))
  4317. ;; We use a before-change function to check if a table might need
  4318. ;; an update.
  4319. (defvar org-table-may-need-update t
  4320. "Indicates that a table might need an update.
  4321. This variable is set by `org-before-change-function'.
  4322. `org-table-align' sets it back to nil.")
  4323. (defun org-before-change-function (beg end)
  4324. "Every change indicates that a table might need an update."
  4325. (setq org-table-may-need-update t))
  4326. (defvar org-mode-map)
  4327. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4328. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4329. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4330. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4331. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4332. (defvar org-table-buffer-is-an nil)
  4333. ;; `org-outline-regexp' ought to be a defconst but is let-binding in
  4334. ;; some places -- e.g. see the macro org-with-limited-levels.
  4335. ;;
  4336. ;; In Org buffers, the value of `outline-regexp' is that of
  4337. ;; `org-outline-regexp'. The only function still directly relying on
  4338. ;; `outline-regexp' is `org-overview' so that `org-cycle' can do its
  4339. ;; job when `orgstruct-mode' is active.
  4340. (defvar org-outline-regexp "\\*+ "
  4341. "Regexp to match Org headlines.")
  4342. (defconst org-outline-regexp-bol "^\\*+ "
  4343. "Regexp to match Org headlines.
  4344. This is similar to `org-outline-regexp' but additionally makes
  4345. sure that we are at the beginning of the line.")
  4346. (defconst org-heading-regexp "^\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  4347. "Matches an headline, putting stars and text into groups.
  4348. Stars are put in group 1 and the trimmed body in group 2.")
  4349. (defvar bidi-paragraph-direction)
  4350. (defvar buffer-face-mode-face)
  4351. ;;;###autoload
  4352. (define-derived-mode org-mode outline-mode "Org"
  4353. "Outline-based notes management and organizer, alias
  4354. \"Carsten's outline-mode for keeping track of everything.\"
  4355. Org-mode develops organizational tasks around a NOTES file which
  4356. contains information about projects as plain text. Org-mode is
  4357. implemented on top of outline-mode, which is ideal to keep the content
  4358. of large files well structured. It supports ToDo items, deadlines and
  4359. time stamps, which magically appear in the diary listing of the Emacs
  4360. calendar. Tables are easily created with a built-in table editor.
  4361. Plain text URL-like links connect to websites, emails (VM), Usenet
  4362. messages (Gnus), BBDB entries, and any files related to the project.
  4363. For printing and sharing of notes, an Org-mode file (or a part of it)
  4364. can be exported as a structured ASCII or HTML file.
  4365. The following commands are available:
  4366. \\{org-mode-map}"
  4367. ;; Get rid of Outline menus, they are not needed
  4368. ;; Need to do this here because define-derived-mode sets up
  4369. ;; the keymap so late. Still, it is a waste to call this each time
  4370. ;; we switch another buffer into org-mode.
  4371. (if (featurep 'xemacs)
  4372. (when (boundp 'outline-mode-menu-heading)
  4373. ;; Assume this is Greg's port, it uses easymenu
  4374. (easy-menu-remove outline-mode-menu-heading)
  4375. (easy-menu-remove outline-mode-menu-show)
  4376. (easy-menu-remove outline-mode-menu-hide))
  4377. (define-key org-mode-map [menu-bar headings] 'undefined)
  4378. (define-key org-mode-map [menu-bar hide] 'undefined)
  4379. (define-key org-mode-map [menu-bar show] 'undefined))
  4380. (org-load-modules-maybe)
  4381. (easy-menu-add org-org-menu)
  4382. (easy-menu-add org-tbl-menu)
  4383. (org-install-agenda-files-menu)
  4384. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4385. (add-to-invisibility-spec '(org-cwidth))
  4386. (add-to-invisibility-spec '(org-hide-block . t))
  4387. (when (featurep 'xemacs)
  4388. (org-set-local 'line-move-ignore-invisible t))
  4389. (org-set-local 'outline-regexp org-outline-regexp)
  4390. (org-set-local 'outline-level 'org-outline-level)
  4391. (setq bidi-paragraph-direction 'left-to-right)
  4392. (when (and org-ellipsis
  4393. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4394. (fboundp 'make-glyph-code))
  4395. (unless org-display-table
  4396. (setq org-display-table (make-display-table)))
  4397. (set-display-table-slot
  4398. org-display-table 4
  4399. (vconcat (mapcar
  4400. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4401. org-ellipsis)))
  4402. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4403. (setq buffer-display-table org-display-table))
  4404. (org-set-regexps-and-options)
  4405. (when (and org-tag-faces (not org-tags-special-faces-re))
  4406. ;; tag faces set outside customize.... force initialization.
  4407. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4408. ;; Calc embedded
  4409. (org-set-local 'calc-embedded-open-mode "# ")
  4410. (modify-syntax-entry ?@ "w")
  4411. (if org-startup-truncated (setq truncate-lines t))
  4412. (org-set-local 'font-lock-unfontify-region-function
  4413. 'org-unfontify-region)
  4414. ;; Activate before-change-function
  4415. (org-set-local 'org-table-may-need-update t)
  4416. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4417. 'local)
  4418. ;; Check for running clock before killing a buffer
  4419. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4420. ;; Paragraphs and auto-filling
  4421. (org-set-autofill-regexps)
  4422. (setq indent-line-function 'org-indent-line-function)
  4423. (org-update-radio-target-regexp)
  4424. ;; Beginning/end of defun
  4425. (org-set-local 'beginning-of-defun-function 'org-beginning-of-defun)
  4426. (org-set-local 'end-of-defun-function 'org-end-of-defun)
  4427. ;; Next error for sparse trees
  4428. (org-set-local 'next-error-function 'org-occur-next-match)
  4429. ;; Make sure dependence stuff works reliably, even for users who set it
  4430. ;; too late :-(
  4431. (if org-enforce-todo-dependencies
  4432. (add-hook 'org-blocker-hook
  4433. 'org-block-todo-from-children-or-siblings-or-parent)
  4434. (remove-hook 'org-blocker-hook
  4435. 'org-block-todo-from-children-or-siblings-or-parent))
  4436. (if org-enforce-todo-checkbox-dependencies
  4437. (add-hook 'org-blocker-hook
  4438. 'org-block-todo-from-checkboxes)
  4439. (remove-hook 'org-blocker-hook
  4440. 'org-block-todo-from-checkboxes))
  4441. ;; Comment characters
  4442. (org-set-local 'comment-start "#")
  4443. (org-set-local 'comment-padding " ")
  4444. ;; Align options lines
  4445. (org-set-local
  4446. 'align-mode-rules-list
  4447. '((org-in-buffer-settings
  4448. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4449. (modes . '(org-mode)))))
  4450. ;; Imenu
  4451. (org-set-local 'imenu-create-index-function
  4452. 'org-imenu-get-tree)
  4453. ;; Make isearch reveal context
  4454. (if (or (featurep 'xemacs)
  4455. (not (boundp 'outline-isearch-open-invisible-function)))
  4456. ;; Emacs 21 and XEmacs make use of the hook
  4457. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4458. ;; Emacs 22 deals with this through a special variable
  4459. (org-set-local 'outline-isearch-open-invisible-function
  4460. (lambda (&rest ignore) (org-show-context 'isearch))))
  4461. ;; Turn on org-beamer-mode?
  4462. (and org-startup-with-beamer-mode (org-beamer-mode 1))
  4463. ;; Setup the pcomplete hooks
  4464. (set (make-local-variable 'pcomplete-command-completion-function)
  4465. 'org-pcomplete-initial)
  4466. (set (make-local-variable 'pcomplete-command-name-function)
  4467. 'org-command-at-point)
  4468. (set (make-local-variable 'pcomplete-default-completion-function)
  4469. 'ignore)
  4470. (set (make-local-variable 'pcomplete-parse-arguments-function)
  4471. 'org-parse-arguments)
  4472. (set (make-local-variable 'pcomplete-termination-string) "")
  4473. (when (>= emacs-major-version 23)
  4474. (set (make-local-variable 'buffer-face-mode-face) 'org-default))
  4475. ;; If empty file that did not turn on org-mode automatically, make it to.
  4476. (if (and org-insert-mode-line-in-empty-file
  4477. (org-called-interactively-p 'any)
  4478. (= (point-min) (point-max)))
  4479. (insert "# -*- mode: org -*-\n\n"))
  4480. (unless org-inhibit-startup
  4481. (when org-startup-align-all-tables
  4482. (let ((bmp (buffer-modified-p)))
  4483. (org-table-map-tables 'org-table-align 'quietly)
  4484. (set-buffer-modified-p bmp)))
  4485. (when org-startup-with-inline-images
  4486. (org-display-inline-images))
  4487. (when org-startup-indented
  4488. (require 'org-indent)
  4489. (org-indent-mode 1))
  4490. (unless org-inhibit-startup-visibility-stuff
  4491. (org-set-startup-visibility))))
  4492. (when (fboundp 'abbrev-table-put)
  4493. (abbrev-table-put org-mode-abbrev-table
  4494. :parents (list text-mode-abbrev-table)))
  4495. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4496. (defun org-current-time ()
  4497. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  4498. (if (> (car org-time-stamp-rounding-minutes) 1)
  4499. (let ((r (car org-time-stamp-rounding-minutes))
  4500. (time (decode-time)))
  4501. (apply 'encode-time
  4502. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4503. (nthcdr 2 time))))
  4504. (current-time)))
  4505. (defun org-today ()
  4506. "Return today date, considering `org-extend-today-until'."
  4507. (time-to-days
  4508. (time-subtract (current-time)
  4509. (list 0 (* 3600 org-extend-today-until) 0))))
  4510. ;;;; Font-Lock stuff, including the activators
  4511. (defvar org-mouse-map (make-sparse-keymap))
  4512. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4513. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4514. (when org-mouse-1-follows-link
  4515. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4516. (when org-tab-follows-link
  4517. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4518. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4519. (require 'font-lock)
  4520. (defconst org-non-link-chars "]\t\n\r<>")
  4521. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  4522. "shell" "elisp" "doi" "message"))
  4523. (defvar org-link-types-re nil
  4524. "Matches a link that has a url-like prefix like \"http:\"")
  4525. (defvar org-link-re-with-space nil
  4526. "Matches a link with spaces, optional angular brackets around it.")
  4527. (defvar org-link-re-with-space2 nil
  4528. "Matches a link with spaces, optional angular brackets around it.")
  4529. (defvar org-link-re-with-space3 nil
  4530. "Matches a link with spaces, only for internal part in bracket links.")
  4531. (defvar org-angle-link-re nil
  4532. "Matches link with angular brackets, spaces are allowed.")
  4533. (defvar org-plain-link-re nil
  4534. "Matches plain link, without spaces.")
  4535. (defvar org-bracket-link-regexp nil
  4536. "Matches a link in double brackets.")
  4537. (defvar org-bracket-link-analytic-regexp nil
  4538. "Regular expression used to analyze links.
  4539. Here is what the match groups contain after a match:
  4540. 1: http:
  4541. 2: http
  4542. 3: path
  4543. 4: [desc]
  4544. 5: desc")
  4545. (defvar org-bracket-link-analytic-regexp++ nil
  4546. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4547. (defvar org-any-link-re nil
  4548. "Regular expression matching any link.")
  4549. (defcustom org-match-sexp-depth 3
  4550. "Number of stacked braces for sub/superscript matching.
  4551. This has to be set before loading org.el to be effective."
  4552. :group 'org-export-translation ; ??????????????????????????/
  4553. :type 'integer)
  4554. (defun org-create-multibrace-regexp (left right n)
  4555. "Create a regular expression which will match a balanced sexp.
  4556. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4557. as single character strings.
  4558. The regexp returned will match the entire expression including the
  4559. delimiters. It will also define a single group which contains the
  4560. match except for the outermost delimiters. The maximum depth of
  4561. stacked delimiters is N. Escaping delimiters is not possible."
  4562. (let* ((nothing (concat "[^" left right "]*?"))
  4563. (or "\\|")
  4564. (re nothing)
  4565. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4566. (while (> n 1)
  4567. (setq n (1- n)
  4568. re (concat re or next)
  4569. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4570. (concat left "\\(" re "\\)" right)))
  4571. (defvar org-match-substring-regexp
  4572. (concat
  4573. "\\([^\\]\\|^\\)\\([_^]\\)\\("
  4574. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4575. "\\|"
  4576. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4577. "\\|"
  4578. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  4579. "The regular expression matching a sub- or superscript.")
  4580. (defvar org-match-substring-with-braces-regexp
  4581. (concat
  4582. "\\([^\\]\\|^\\)\\([_^]\\)\\("
  4583. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4584. "\\)")
  4585. "The regular expression matching a sub- or superscript, forcing braces.")
  4586. (defun org-make-link-regexps ()
  4587. "Update the link regular expressions.
  4588. This should be called after the variable `org-link-types' has changed."
  4589. (setq org-link-types-re
  4590. (concat
  4591. "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
  4592. org-link-re-with-space
  4593. (concat
  4594. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4595. "\\([^" org-non-link-chars " ]"
  4596. "[^" org-non-link-chars "]*"
  4597. "[^" org-non-link-chars " ]\\)>?")
  4598. org-link-re-with-space2
  4599. (concat
  4600. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4601. "\\([^" org-non-link-chars " ]"
  4602. "[^\t\n\r]*"
  4603. "[^" org-non-link-chars " ]\\)>?")
  4604. org-link-re-with-space3
  4605. (concat
  4606. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4607. "\\([^" org-non-link-chars " ]"
  4608. "[^\t\n\r]*\\)")
  4609. org-angle-link-re
  4610. (concat
  4611. "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4612. "\\([^" org-non-link-chars " ]"
  4613. "[^" org-non-link-chars "]*"
  4614. "\\)>")
  4615. org-plain-link-re
  4616. (concat
  4617. "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4618. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  4619. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4620. org-bracket-link-regexp
  4621. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4622. org-bracket-link-analytic-regexp
  4623. (concat
  4624. "\\[\\["
  4625. "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
  4626. "\\([^]]+\\)"
  4627. "\\]"
  4628. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4629. "\\]")
  4630. org-bracket-link-analytic-regexp++
  4631. (concat
  4632. "\\[\\["
  4633. "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  4634. "\\([^]]+\\)"
  4635. "\\]"
  4636. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4637. "\\]")
  4638. org-any-link-re
  4639. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4640. org-angle-link-re "\\)\\|\\("
  4641. org-plain-link-re "\\)")))
  4642. (org-make-link-regexps)
  4643. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
  4644. "Regular expression for fast time stamp matching.")
  4645. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
  4646. "Regular expression for fast time stamp matching.")
  4647. (defconst org-ts-regexp0
  4648. "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4649. "Regular expression matching time strings for analysis.
  4650. This one does not require the space after the date, so it can be used
  4651. on a string that terminates immediately after the date.")
  4652. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4653. "Regular expression matching time strings for analysis.")
  4654. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4655. "Regular expression matching time stamps, with groups.")
  4656. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4657. "Regular expression matching time stamps (also [..]), with groups.")
  4658. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4659. "Regular expression matching a time stamp range.")
  4660. (defconst org-tr-regexp-both
  4661. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4662. "Regular expression matching a time stamp range.")
  4663. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4664. org-ts-regexp "\\)?")
  4665. "Regular expression matching a time stamp or time stamp range.")
  4666. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  4667. org-ts-regexp-both "\\)?")
  4668. "Regular expression matching a time stamp or time stamp range.
  4669. The time stamps may be either active or inactive.")
  4670. (defvar org-emph-face nil)
  4671. (defun org-do-emphasis-faces (limit)
  4672. "Run through the buffer and add overlays to emphasized strings."
  4673. (let (rtn a)
  4674. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4675. (if (not (= (char-after (match-beginning 3))
  4676. (char-after (match-beginning 4))))
  4677. (progn
  4678. (setq rtn t)
  4679. (setq a (assoc (match-string 3) org-emphasis-alist))
  4680. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4681. 'face
  4682. (nth 1 a))
  4683. (and (nth 4 a)
  4684. (org-remove-flyspell-overlays-in
  4685. (match-beginning 0) (match-end 0)))
  4686. (add-text-properties (match-beginning 2) (match-end 2)
  4687. '(font-lock-multiline t org-emphasis t))
  4688. (when org-hide-emphasis-markers
  4689. (add-text-properties (match-end 4) (match-beginning 5)
  4690. '(invisible org-link))
  4691. (add-text-properties (match-beginning 3) (match-end 3)
  4692. '(invisible org-link)))))
  4693. (backward-char 1))
  4694. rtn))
  4695. (defun org-emphasize (&optional char)
  4696. "Insert or change an emphasis, i.e. a font like bold or italic.
  4697. If there is an active region, change that region to a new emphasis.
  4698. If there is no region, just insert the marker characters and position
  4699. the cursor between them.
  4700. CHAR should be either the marker character, or the first character of the
  4701. HTML tag associated with that emphasis. If CHAR is a space, the means
  4702. to remove the emphasis of the selected region.
  4703. If char is not given (for example in an interactive call) it
  4704. will be prompted for."
  4705. (interactive)
  4706. (let ((eal org-emphasis-alist) e det
  4707. (erc org-emphasis-regexp-components)
  4708. (prompt "")
  4709. (string "") beg end move tag c s)
  4710. (if (org-region-active-p)
  4711. (setq beg (region-beginning) end (region-end)
  4712. string (buffer-substring beg end))
  4713. (setq move t))
  4714. (while (setq e (pop eal))
  4715. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4716. c (aref tag 0))
  4717. (push (cons c (string-to-char (car e))) det)
  4718. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4719. (substring tag 1)))))
  4720. (setq det (nreverse det))
  4721. (unless char
  4722. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4723. (setq char (read-char-exclusive)))
  4724. (setq char (or (cdr (assoc char det)) char))
  4725. (if (equal char ?\ )
  4726. (setq s "" move nil)
  4727. (unless (assoc (char-to-string char) org-emphasis-alist)
  4728. (error "No such emphasis marker: \"%c\"" char))
  4729. (setq s (char-to-string char)))
  4730. (while (and (> (length string) 1)
  4731. (equal (substring string 0 1) (substring string -1))
  4732. (assoc (substring string 0 1) org-emphasis-alist))
  4733. (setq string (substring string 1 -1)))
  4734. (setq string (concat s string s))
  4735. (if beg (delete-region beg end))
  4736. (unless (or (bolp)
  4737. (string-match (concat "[" (nth 0 erc) "\n]")
  4738. (char-to-string (char-before (point)))))
  4739. (insert " "))
  4740. (unless (or (eobp)
  4741. (string-match (concat "[" (nth 1 erc) "\n]")
  4742. (char-to-string (char-after (point)))))
  4743. (insert " ") (backward-char 1))
  4744. (insert string)
  4745. (and move (backward-char 1))))
  4746. (defconst org-nonsticky-props
  4747. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  4748. (defsubst org-rear-nonsticky-at (pos)
  4749. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  4750. (defun org-activate-plain-links (limit)
  4751. "Run through the buffer and add overlays to links."
  4752. (catch 'exit
  4753. (let (f)
  4754. (if (re-search-forward org-plain-link-re limit t)
  4755. (progn
  4756. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4757. (setq f (get-text-property (match-beginning 0) 'face))
  4758. (if (or (eq f 'org-tag)
  4759. (and (listp f) (memq 'org-tag f)))
  4760. nil
  4761. (add-text-properties (match-beginning 0) (match-end 0)
  4762. (list 'mouse-face 'highlight
  4763. 'face 'org-link
  4764. 'keymap org-mouse-map))
  4765. (org-rear-nonsticky-at (match-end 0)))
  4766. t)))))
  4767. (defun org-activate-code (limit)
  4768. (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  4769. (progn
  4770. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4771. (remove-text-properties (match-beginning 0) (match-end 0)
  4772. '(display t invisible t intangible t))
  4773. t)))
  4774. (defcustom org-src-fontify-natively nil
  4775. "When non-nil, fontify code in code blocks."
  4776. :type 'boolean
  4777. :version "24.1"
  4778. :group 'org-appearance
  4779. :group 'org-babel)
  4780. (defun org-fontify-meta-lines-and-blocks (limit)
  4781. (condition-case nil
  4782. (org-fontify-meta-lines-and-blocks-1 limit)
  4783. (error (message "org-mode fontification error"))))
  4784. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  4785. "Fontify #+ lines and blocks, in the correct ways."
  4786. (let ((case-fold-search t))
  4787. (if (re-search-forward
  4788. "^\\([ \t]*#\\+\\(\\([a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  4789. limit t)
  4790. (let ((beg (match-beginning 0))
  4791. (block-start (match-end 0))
  4792. (block-end nil)
  4793. (lang (match-string 7))
  4794. (beg1 (line-beginning-position 2))
  4795. (dc1 (downcase (match-string 2)))
  4796. (dc3 (downcase (match-string 3)))
  4797. end end1 quoting block-type ovl)
  4798. (cond
  4799. ((member dc1 '("html:" "ascii:" "latex:" "docbook:"))
  4800. ;; a single line of backend-specific content
  4801. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4802. (remove-text-properties (match-beginning 0) (match-end 0)
  4803. '(display t invisible t intangible t))
  4804. (add-text-properties (match-beginning 1) (match-end 3)
  4805. '(font-lock-fontified t face org-meta-line))
  4806. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  4807. '(font-lock-fontified t face org-block))
  4808. ; for backend-specific code
  4809. t)
  4810. ((and (match-end 4) (equal dc3 "begin"))
  4811. ;; Truly a block
  4812. (setq block-type (downcase (match-string 5))
  4813. quoting (member block-type org-protecting-blocks))
  4814. (when (re-search-forward
  4815. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  4816. nil t) ;; on purpose, we look further than LIMIT
  4817. (setq end (min (point-max) (match-end 0))
  4818. end1 (min (point-max) (1- (match-beginning 0))))
  4819. (setq block-end (match-beginning 0))
  4820. (when quoting
  4821. (remove-text-properties beg end
  4822. '(display t invisible t intangible t)))
  4823. (add-text-properties
  4824. beg end
  4825. '(font-lock-fontified t font-lock-multiline t))
  4826. (add-text-properties beg beg1 '(face org-meta-line))
  4827. (add-text-properties end1 (min (point-max) (1+ end))
  4828. '(face org-meta-line)) ; for end_src
  4829. (cond
  4830. ((and lang (not (string= lang "")) org-src-fontify-natively)
  4831. (org-src-font-lock-fontify-block lang block-start block-end)
  4832. ;; remove old background overlays
  4833. (mapc (lambda (ov)
  4834. (if (eq (overlay-get ov 'face) 'org-block-background)
  4835. (delete-overlay ov)))
  4836. (overlays-at (/ (+ beg1 block-end) 2)))
  4837. ;; add a background overlay
  4838. (setq ovl (make-overlay beg1 block-end))
  4839. (overlay-put ovl 'face 'org-block-background)
  4840. (overlay-put ovl 'evaporate t)) ;; make it go away when empty
  4841. (quoting
  4842. (add-text-properties beg1 (min (point-max) (1+ end1))
  4843. '(face org-block))) ; end of source block
  4844. ((not org-fontify-quote-and-verse-blocks))
  4845. ((string= block-type "quote")
  4846. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-quote)))
  4847. ((string= block-type "verse")
  4848. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-verse))))
  4849. (add-text-properties beg beg1 '(face org-block-begin-line))
  4850. (add-text-properties (min (point-max) (1+ end)) (min (point-max) (1+ end1))
  4851. '(face org-block-end-line))
  4852. t))
  4853. ((member dc1 '("title:" "author:" "email:" "date:"))
  4854. (add-text-properties
  4855. beg (match-end 3)
  4856. (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
  4857. '(font-lock-fontified t invisible t)
  4858. '(font-lock-fontified t face org-document-info-keyword)))
  4859. (add-text-properties
  4860. (match-beginning 6) (match-end 6)
  4861. (if (string-equal dc1 "title:")
  4862. '(font-lock-fontified t face org-document-title)
  4863. '(font-lock-fontified t face org-document-info))))
  4864. ((not (member (char-after beg) '(?\ ?\t)))
  4865. ;; just any other in-buffer setting, but not indented
  4866. (add-text-properties
  4867. beg (match-end 0)
  4868. '(font-lock-fontified t face org-meta-line))
  4869. t)
  4870. ((or (member dc1 '("begin:" "end:" "caption:" "label:"
  4871. "orgtbl:" "tblfm:" "tblname:" "results:"
  4872. "call:" "header:" "headers:" "name:"))
  4873. (and (match-end 4) (equal dc3 "attr")))
  4874. (add-text-properties
  4875. beg (match-end 0)
  4876. '(font-lock-fontified t face org-meta-line))
  4877. t)
  4878. ((member dc3 '(" " ""))
  4879. (add-text-properties
  4880. beg (match-end 0)
  4881. '(font-lock-fontified t face font-lock-comment-face)))
  4882. (t nil))))))
  4883. (defun org-strip-protective-commas (beg end)
  4884. "Strip protective commas between BEG and END in the current buffer."
  4885. (interactive "r")
  4886. (save-excursion
  4887. (save-match-data
  4888. (goto-char beg)
  4889. (let ((front-line (save-excursion
  4890. (re-search-forward
  4891. "[^[:space:]]" end t)
  4892. (goto-char (match-beginning 0))
  4893. (current-column))))
  4894. (while (re-search-forward "^[ \t]*\\(,\\)\\([*]\\|#\\+\\)" end t)
  4895. (goto-char (match-beginning 1))
  4896. (when (= (current-column) front-line)
  4897. (replace-match "" nil nil nil 1)))))))
  4898. (defun org-activate-angle-links (limit)
  4899. "Run through the buffer and add overlays to links."
  4900. (if (re-search-forward org-angle-link-re limit t)
  4901. (progn
  4902. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4903. (add-text-properties (match-beginning 0) (match-end 0)
  4904. (list 'mouse-face 'highlight
  4905. 'keymap org-mouse-map))
  4906. (org-rear-nonsticky-at (match-end 0))
  4907. t)))
  4908. (defun org-activate-footnote-links (limit)
  4909. "Run through the buffer and add overlays to footnotes."
  4910. (let ((fn (org-footnote-next-reference-or-definition limit)))
  4911. (when fn
  4912. (let ((beg (nth 1 fn)) (end (nth 2 fn)))
  4913. (org-remove-flyspell-overlays-in beg end)
  4914. (add-text-properties beg end
  4915. (list 'mouse-face 'highlight
  4916. 'keymap org-mouse-map
  4917. 'help-echo
  4918. (if (= (point-at-bol) beg)
  4919. "Footnote definition"
  4920. "Footnote reference")
  4921. 'font-lock-fontified t
  4922. 'font-lock-multiline t
  4923. 'face 'org-footnote))))))
  4924. (defun org-activate-bracket-links (limit)
  4925. "Run through the buffer and add overlays to bracketed links."
  4926. (if (re-search-forward org-bracket-link-regexp limit t)
  4927. (let* ((help (concat "LINK: "
  4928. (org-match-string-no-properties 1)))
  4929. ;; FIXME: above we should remove the escapes.
  4930. ;; but that requires another match, protecting match data,
  4931. ;; a lot of overhead for font-lock.
  4932. (ip (org-maybe-intangible
  4933. (list 'invisible 'org-link
  4934. 'keymap org-mouse-map 'mouse-face 'highlight
  4935. 'font-lock-multiline t 'help-echo help)))
  4936. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  4937. 'font-lock-multiline t 'help-echo help)))
  4938. ;; We need to remove the invisible property here. Table narrowing
  4939. ;; may have made some of this invisible.
  4940. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4941. (remove-text-properties (match-beginning 0) (match-end 0)
  4942. '(invisible nil))
  4943. (if (match-end 3)
  4944. (progn
  4945. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  4946. (org-rear-nonsticky-at (match-beginning 3))
  4947. (add-text-properties (match-beginning 3) (match-end 3) vp)
  4948. (org-rear-nonsticky-at (match-end 3))
  4949. (add-text-properties (match-end 3) (match-end 0) ip)
  4950. (org-rear-nonsticky-at (match-end 0)))
  4951. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  4952. (org-rear-nonsticky-at (match-beginning 1))
  4953. (add-text-properties (match-beginning 1) (match-end 1) vp)
  4954. (org-rear-nonsticky-at (match-end 1))
  4955. (add-text-properties (match-end 1) (match-end 0) ip)
  4956. (org-rear-nonsticky-at (match-end 0)))
  4957. t)))
  4958. (defun org-activate-dates (limit)
  4959. "Run through the buffer and add overlays to dates."
  4960. (if (re-search-forward org-tsr-regexp-both limit t)
  4961. (progn
  4962. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4963. (add-text-properties (match-beginning 0) (match-end 0)
  4964. (list 'mouse-face 'highlight
  4965. 'keymap org-mouse-map))
  4966. (org-rear-nonsticky-at (match-end 0))
  4967. (when org-display-custom-times
  4968. (if (match-end 3)
  4969. (org-display-custom-time (match-beginning 3) (match-end 3)))
  4970. (org-display-custom-time (match-beginning 1) (match-end 1)))
  4971. t)))
  4972. (defvar org-target-link-regexp nil
  4973. "Regular expression matching radio targets in plain text.")
  4974. (make-variable-buffer-local 'org-target-link-regexp)
  4975. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  4976. "Regular expression matching a link target.")
  4977. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  4978. "Regular expression matching a radio target.")
  4979. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  4980. "Regular expression matching any target.")
  4981. (defun org-activate-target-links (limit)
  4982. "Run through the buffer and add overlays to target matches."
  4983. (when org-target-link-regexp
  4984. (let ((case-fold-search t))
  4985. (if (re-search-forward org-target-link-regexp limit t)
  4986. (progn
  4987. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4988. (add-text-properties (match-beginning 0) (match-end 0)
  4989. (list 'mouse-face 'highlight
  4990. 'keymap org-mouse-map
  4991. 'help-echo "Radio target link"
  4992. 'org-linked-text t))
  4993. (org-rear-nonsticky-at (match-end 0))
  4994. t)))))
  4995. (defun org-update-radio-target-regexp ()
  4996. "Find all radio targets in this file and update the regular expression."
  4997. (interactive)
  4998. (when (memq 'radio org-activate-links)
  4999. (setq org-target-link-regexp
  5000. (org-make-target-link-regexp (org-all-targets 'radio)))
  5001. (org-restart-font-lock)))
  5002. (defun org-hide-wide-columns (limit)
  5003. (let (s e)
  5004. (setq s (text-property-any (point) (or limit (point-max))
  5005. 'org-cwidth t))
  5006. (when s
  5007. (setq e (next-single-property-change s 'org-cwidth))
  5008. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  5009. (goto-char e)
  5010. t)))
  5011. (defvar org-latex-and-specials-regexp nil
  5012. "Regular expression for highlighting export special stuff.")
  5013. (defvar org-match-substring-regexp)
  5014. (defvar org-match-substring-with-braces-regexp)
  5015. ;; This should be with the exporter code, but we also use if for font-locking
  5016. (defconst org-export-html-special-string-regexps
  5017. '(("\\\\-" . "&shy;")
  5018. ("---\\([^-]\\)" . "&mdash;\\1")
  5019. ("--\\([^-]\\)" . "&ndash;\\1")
  5020. ("\\.\\.\\." . "&hellip;"))
  5021. "Regular expressions for special string conversion.")
  5022. (defun org-compute-latex-and-specials-regexp ()
  5023. "Compute regular expression for stuff treated specially by exporters."
  5024. (if (not org-highlight-latex-fragments-and-specials)
  5025. (org-set-local 'org-latex-and-specials-regexp nil)
  5026. (require 'org-exp)
  5027. (let*
  5028. ((matchers (plist-get org-format-latex-options :matchers))
  5029. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  5030. org-latex-regexps)))
  5031. (org-export-allow-BIND nil)
  5032. (options (org-combine-plists (org-default-export-plist)
  5033. (org-infile-export-plist)))
  5034. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  5035. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  5036. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  5037. (org-export-html-expand (plist-get options :expand-quoted-html))
  5038. (org-export-with-special-strings (plist-get options :special-strings))
  5039. (re-sub
  5040. (cond
  5041. ((equal org-export-with-sub-superscripts '{})
  5042. (list org-match-substring-with-braces-regexp))
  5043. (org-export-with-sub-superscripts
  5044. (list org-match-substring-regexp))
  5045. (t nil)))
  5046. (re-latex
  5047. (if org-export-with-LaTeX-fragments
  5048. (mapcar (lambda (x) (nth 1 x)) latexs)))
  5049. (re-macros
  5050. (if org-export-with-TeX-macros
  5051. (list (concat "\\\\"
  5052. (regexp-opt
  5053. (append
  5054. (delq nil
  5055. (mapcar 'car-safe
  5056. (append org-entities-user
  5057. org-entities)))
  5058. (if (boundp 'org-latex-entities)
  5059. (mapcar (lambda (x)
  5060. (or (car-safe x) x))
  5061. org-latex-entities)
  5062. nil))
  5063. 'words))) ; FIXME
  5064. ))
  5065. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  5066. (re-special (if org-export-with-special-strings
  5067. (mapcar (lambda (x) (car x))
  5068. org-export-html-special-string-regexps)))
  5069. (re-rest
  5070. (delq nil
  5071. (list
  5072. (if org-export-html-expand "@<[^>\n]+>")
  5073. ))))
  5074. (org-set-local
  5075. 'org-latex-and-specials-regexp
  5076. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  5077. re-rest) "\\|")))))
  5078. (defun org-do-latex-and-special-faces (limit)
  5079. "Run through the buffer and add overlays to links."
  5080. (when org-latex-and-specials-regexp
  5081. (let (rtn d)
  5082. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  5083. limit t))
  5084. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  5085. 'face))
  5086. '(org-code org-verbatim underline)))
  5087. (progn
  5088. (setq rtn t
  5089. d (cond ((member (char-after (1+ (match-beginning 0)))
  5090. '(?_ ?^)) 1)
  5091. (t 0)))
  5092. (font-lock-prepend-text-property
  5093. (+ d (match-beginning 0)) (match-end 0)
  5094. 'face 'org-latex-and-export-specials)
  5095. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  5096. '(font-lock-multiline t)))))
  5097. rtn)))
  5098. (defun org-restart-font-lock ()
  5099. "Restart `font-lock-mode', to force refontification."
  5100. (when (and (boundp 'font-lock-mode) font-lock-mode)
  5101. (font-lock-mode -1)
  5102. (font-lock-mode 1)))
  5103. (defun org-all-targets (&optional radio)
  5104. "Return a list of all targets in this file.
  5105. With optional argument RADIO, only find radio targets."
  5106. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  5107. rtn)
  5108. (save-excursion
  5109. (goto-char (point-min))
  5110. (while (re-search-forward re nil t)
  5111. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  5112. rtn)))
  5113. (defun org-make-target-link-regexp (targets)
  5114. "Make regular expression matching all strings in TARGETS.
  5115. The regular expression finds the targets also if there is a line break
  5116. between words."
  5117. (and targets
  5118. (concat
  5119. "\\<\\("
  5120. (mapconcat
  5121. (lambda (x)
  5122. (setq x (regexp-quote x))
  5123. (while (string-match " +" x)
  5124. (setq x (replace-match "\\s-+" t t x)))
  5125. x)
  5126. targets
  5127. "\\|")
  5128. "\\)\\>")))
  5129. (defun org-activate-tags (limit)
  5130. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  5131. (progn
  5132. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5133. (add-text-properties (match-beginning 1) (match-end 1)
  5134. (list 'mouse-face 'highlight
  5135. 'keymap org-mouse-map))
  5136. (org-rear-nonsticky-at (match-end 1))
  5137. t)))
  5138. (defun org-outline-level ()
  5139. "Compute the outline level of the heading at point.
  5140. This function assumes that the cursor is at the beginning of a line matched
  5141. by `outline-regexp'. Otherwise it returns garbage.
  5142. If this is called at a normal headline, the level is the number of stars.
  5143. Use `org-reduced-level' to remove the effect of `org-odd-levels'."
  5144. (save-excursion
  5145. (looking-at org-outline-regexp)
  5146. (1- (- (match-end 0) (match-beginning 0)))))
  5147. (defvar org-font-lock-keywords nil)
  5148. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\+?\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  5149. "Regular expression matching a property line.")
  5150. (defvar org-font-lock-hook nil
  5151. "Functions to be called for special font lock stuff.")
  5152. (defvar org-font-lock-set-keywords-hook nil
  5153. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5154. This is called after `org-font-lock-extra-keywords' is defined, but before
  5155. it is installed to be used by font lock. This can be useful if something
  5156. needs to be inserted at a specific position in the font-lock sequence.")
  5157. (defun org-font-lock-hook (limit)
  5158. (run-hook-with-args 'org-font-lock-hook limit))
  5159. (defun org-set-font-lock-defaults ()
  5160. (let* ((em org-fontify-emphasized-text)
  5161. (lk org-activate-links)
  5162. (org-font-lock-extra-keywords
  5163. (list
  5164. ;; Call the hook
  5165. '(org-font-lock-hook)
  5166. ;; Headlines
  5167. `(,(if org-fontify-whole-heading-line
  5168. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5169. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5170. (1 (org-get-level-face 1))
  5171. (2 (org-get-level-face 2))
  5172. (3 (org-get-level-face 3)))
  5173. ;; Table lines
  5174. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5175. (1 'org-table t))
  5176. ;; Table internals
  5177. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5178. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5179. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5180. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5181. ;; Drawers
  5182. (list org-drawer-regexp '(0 'org-special-keyword t))
  5183. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  5184. ;; Properties
  5185. (list org-property-re
  5186. '(1 'org-special-keyword t)
  5187. '(3 'org-property-value t))
  5188. ;; Links
  5189. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5190. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5191. (if (memq 'plain lk) '(org-activate-plain-links))
  5192. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5193. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  5194. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5195. (if (memq 'footnote lk) '(org-activate-footnote-links))
  5196. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5197. '(org-hide-wide-columns (0 nil append))
  5198. ;; TODO keyword
  5199. (list (format org-heading-keyword-regexp-format
  5200. org-todo-regexp)
  5201. '(2 (org-get-todo-face 2) t))
  5202. ;; DONE
  5203. (if org-fontify-done-headline
  5204. (list (format org-heading-keyword-regexp-format
  5205. (concat
  5206. "\\(?:"
  5207. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5208. "\\)"))
  5209. '(2 'org-headline-done t))
  5210. nil)
  5211. ;; Priorities
  5212. '(org-font-lock-add-priority-faces)
  5213. ;; Tags
  5214. '(org-font-lock-add-tag-faces)
  5215. ;; Special keywords
  5216. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5217. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5218. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5219. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5220. ;; Emphasis
  5221. (if em
  5222. (if (featurep 'xemacs)
  5223. '(org-do-emphasis-faces (0 nil append))
  5224. '(org-do-emphasis-faces)))
  5225. ;; Checkboxes
  5226. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5227. 1 'org-checkbox prepend)
  5228. (if (cdr (assq 'checkbox org-list-automatic-rules))
  5229. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5230. (0 (org-get-checkbox-statistics-face) t)))
  5231. ;; Description list items
  5232. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5233. 1 'bold prepend)
  5234. ;; ARCHIVEd headings
  5235. (list (concat
  5236. org-outline-regexp-bol
  5237. "\\(.*:" org-archive-tag ":.*\\)")
  5238. '(1 'org-archived prepend))
  5239. ;; Specials
  5240. '(org-do-latex-and-special-faces)
  5241. '(org-fontify-entities)
  5242. '(org-raise-scripts)
  5243. ;; Code
  5244. '(org-activate-code (1 'org-code t))
  5245. ;; COMMENT
  5246. (list (format org-heading-keyword-regexp-format
  5247. (concat "\\("
  5248. org-comment-string "\\|" org-quote-string
  5249. "\\)"))
  5250. '(2 'org-special-keyword t))
  5251. '("^#.*" (0 'font-lock-comment-face t))
  5252. ;; Blocks and meta lines
  5253. '(org-fontify-meta-lines-and-blocks)
  5254. )))
  5255. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5256. (run-hooks 'org-font-lock-set-keywords-hook)
  5257. ;; Now set the full font-lock-keywords
  5258. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5259. (org-set-local 'font-lock-defaults
  5260. '(org-font-lock-keywords t nil nil backward-paragraph))
  5261. (kill-local-variable 'font-lock-keywords) nil))
  5262. (defun org-toggle-pretty-entities ()
  5263. "Toggle the composition display of entities as UTF8 characters."
  5264. (interactive)
  5265. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5266. (org-restart-font-lock)
  5267. (if org-pretty-entities
  5268. (message "Entities are displayed as UTF8 characters")
  5269. (save-restriction
  5270. (widen)
  5271. (org-decompose-region (point-min) (point-max))
  5272. (message "Entities are displayed plain"))))
  5273. (defun org-fontify-entities (limit)
  5274. "Find an entity to fontify."
  5275. (let (ee)
  5276. (when org-pretty-entities
  5277. (catch 'match
  5278. (while (re-search-forward
  5279. "\\\\\\(frac[13][24]\\|[a-zA-Z]+\\)\\($\\|[^[:alpha:]\n]\\)"
  5280. limit t)
  5281. (if (and (not (org-in-indented-comment-line))
  5282. (setq ee (org-entity-get (match-string 1)))
  5283. (= (length (nth 6 ee)) 1))
  5284. (progn
  5285. (add-text-properties
  5286. (match-beginning 0) (match-end 1)
  5287. (list 'font-lock-fontified t))
  5288. (compose-region (match-beginning 0) (match-end 1)
  5289. (nth 6 ee) nil)
  5290. (backward-char 1)
  5291. (throw 'match t))))
  5292. nil))))
  5293. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5294. "Fontify string S like in Org-mode."
  5295. (with-temp-buffer
  5296. (insert s)
  5297. (let ((org-odd-levels-only odd-levels))
  5298. (org-mode)
  5299. (font-lock-fontify-buffer)
  5300. (buffer-string))))
  5301. (defvar org-m nil)
  5302. (defvar org-l nil)
  5303. (defvar org-f nil)
  5304. (defun org-get-level-face (n)
  5305. "Get the right face for match N in font-lock matching of headlines."
  5306. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5307. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5308. (if org-cycle-level-faces
  5309. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5310. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5311. (cond
  5312. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5313. ((eq n 2) org-f)
  5314. (t (if org-level-color-stars-only nil org-f))))
  5315. (defun org-get-todo-face (kwd)
  5316. "Get the right face for a TODO keyword KWD.
  5317. If KWD is a number, get the corresponding match group."
  5318. (if (numberp kwd) (setq kwd (match-string kwd)))
  5319. (or (org-face-from-face-or-color
  5320. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5321. (and (member kwd org-done-keywords) 'org-done)
  5322. 'org-todo))
  5323. (defun org-face-from-face-or-color (context inherit face-or-color)
  5324. "Create a face list that inherits INHERIT, but sets the foreground color.
  5325. When FACE-OR-COLOR is not a string, just return it."
  5326. (if (stringp face-or-color)
  5327. (list :inherit inherit
  5328. (cdr (assoc context org-faces-easy-properties))
  5329. face-or-color)
  5330. face-or-color))
  5331. (defun org-font-lock-add-tag-faces (limit)
  5332. "Add the special tag faces."
  5333. (when (and org-tag-faces org-tags-special-faces-re)
  5334. (while (re-search-forward org-tags-special-faces-re limit t)
  5335. (add-text-properties (match-beginning 1) (match-end 1)
  5336. (list 'face (org-get-tag-face 1)
  5337. 'font-lock-fontified t))
  5338. (backward-char 1))))
  5339. (defun org-font-lock-add-priority-faces (limit)
  5340. "Add the special priority faces."
  5341. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  5342. (add-text-properties
  5343. (match-beginning 0) (match-end 0)
  5344. (list 'face (or (org-face-from-face-or-color
  5345. 'priority 'org-special-keyword
  5346. (cdr (assoc (char-after (match-beginning 1))
  5347. org-priority-faces)))
  5348. 'org-special-keyword)
  5349. 'font-lock-fontified t))))
  5350. (defun org-get-tag-face (kwd)
  5351. "Get the right face for a TODO keyword KWD.
  5352. If KWD is a number, get the corresponding match group."
  5353. (if (numberp kwd) (setq kwd (match-string kwd)))
  5354. (or (org-face-from-face-or-color
  5355. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  5356. 'org-tag))
  5357. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5358. "Remove fontification and activation overlays from links."
  5359. (font-lock-default-unfontify-region beg end)
  5360. (let* ((buffer-undo-list t)
  5361. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5362. (inhibit-modification-hooks t)
  5363. deactivate-mark buffer-file-name buffer-file-truename)
  5364. (org-decompose-region beg end)
  5365. (remove-text-properties beg end
  5366. '(mouse-face t keymap t org-linked-text t
  5367. invisible t intangible t
  5368. org-no-flyspell t org-emphasis t))
  5369. (org-remove-font-lock-display-properties beg end)))
  5370. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5371. ((raise 0.3) (height 0.7))
  5372. ((raise -0.5))
  5373. ((raise 0.5)))
  5374. "Display properties for showing superscripts and subscripts.")
  5375. (defun org-remove-font-lock-display-properties (beg end)
  5376. "Remove specific display properties that have been added by font lock.
  5377. The will remove the raise properties that are used to show superscripts
  5378. and subscripts."
  5379. (let (next prop)
  5380. (while (< beg end)
  5381. (setq next (next-single-property-change beg 'display nil end)
  5382. prop (get-text-property beg 'display))
  5383. (if (member prop org-script-display)
  5384. (put-text-property beg next 'display nil))
  5385. (setq beg next))))
  5386. (defun org-raise-scripts (limit)
  5387. "Add raise properties to sub/superscripts."
  5388. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5389. (if (re-search-forward
  5390. (if (eq org-use-sub-superscripts t)
  5391. org-match-substring-regexp
  5392. org-match-substring-with-braces-regexp)
  5393. limit t)
  5394. (let* ((pos (point)) table-p comment-p
  5395. (mpos (match-beginning 3))
  5396. (emph-p (get-text-property mpos 'org-emphasis))
  5397. (link-p (get-text-property mpos 'mouse-face))
  5398. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5399. (goto-char (point-at-bol))
  5400. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5401. comment-p (org-looking-at-p "[ \t]*#"))
  5402. (goto-char pos)
  5403. ;; FIXME: Should we go back one character here, for a_b^c
  5404. ;; (goto-char (1- pos)) ;????????????????????
  5405. (if (or comment-p emph-p link-p keyw-p)
  5406. t
  5407. (put-text-property (match-beginning 3) (match-end 0)
  5408. 'display
  5409. (if (equal (char-after (match-beginning 2)) ?^)
  5410. (nth (if table-p 3 1) org-script-display)
  5411. (nth (if table-p 2 0) org-script-display)))
  5412. (add-text-properties (match-beginning 2) (match-end 2)
  5413. (list 'invisible t
  5414. 'org-dwidth t 'org-dwidth-n 1))
  5415. (if (and (eq (char-after (match-beginning 3)) ?{)
  5416. (eq (char-before (match-end 3)) ?}))
  5417. (progn
  5418. (add-text-properties
  5419. (match-beginning 3) (1+ (match-beginning 3))
  5420. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5421. (add-text-properties
  5422. (1- (match-end 3)) (match-end 3)
  5423. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5424. t)))))
  5425. ;;;; Visibility cycling, including org-goto and indirect buffer
  5426. ;;; Cycling
  5427. (defvar org-cycle-global-status nil)
  5428. (make-variable-buffer-local 'org-cycle-global-status)
  5429. (defvar org-cycle-subtree-status nil)
  5430. (make-variable-buffer-local 'org-cycle-subtree-status)
  5431. ;;;###autoload
  5432. (defvar org-inlinetask-min-level)
  5433. (defun org-cycle (&optional arg)
  5434. "TAB-action and visibility cycling for Org-mode.
  5435. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5436. is outline visibility cycling, but it also invokes other actions
  5437. in special contexts.
  5438. - When this function is called with a prefix argument, rotate the entire
  5439. buffer through 3 states (global cycling)
  5440. 1. OVERVIEW: Show only top-level headlines.
  5441. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5442. 3. SHOW ALL: Show everything.
  5443. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5444. determined by the variable `org-startup-folded', and by any VISIBILITY
  5445. properties in the buffer.
  5446. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5447. including any drawers.
  5448. - When inside a table, re-align the table and move to the next field.
  5449. - When point is at the beginning of a headline, rotate the subtree started
  5450. by this line through 3 different states (local cycling)
  5451. 1. FOLDED: Only the main headline is shown.
  5452. 2. CHILDREN: The main headline and the direct children are shown.
  5453. From this state, you can move to one of the children
  5454. and zoom in further.
  5455. 3. SUBTREE: Show the entire subtree, including body text.
  5456. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5457. - When point is at the beginning of an empty headline and the variable
  5458. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5459. of the headline by demoting and promoting it to likely levels. This
  5460. speeds up creation document structure by pressing TAB once or several
  5461. times right after creating a new headline.
  5462. - When there is a numeric prefix, go up to a heading with level ARG, do
  5463. a `show-subtree' and return to the previous cursor position. If ARG
  5464. is negative, go up that many levels.
  5465. - When point is not at the beginning of a headline, execute the global
  5466. binding for TAB, which is re-indenting the line. See the option
  5467. `org-cycle-emulate-tab' for details.
  5468. - Special case: if point is at the beginning of the buffer and there is
  5469. no headline in line 1, this function will act as if called with prefix arg
  5470. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5471. But only if also the variable `org-cycle-global-at-bob' is t."
  5472. (interactive "P")
  5473. (org-load-modules-maybe)
  5474. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5475. (and org-cycle-level-after-item/entry-creation
  5476. (or (org-cycle-level)
  5477. (org-cycle-item-indentation))))
  5478. (let* ((limit-level
  5479. (or org-cycle-max-level
  5480. (and (boundp 'org-inlinetask-min-level)
  5481. org-inlinetask-min-level
  5482. (1- org-inlinetask-min-level))))
  5483. (nstars (and limit-level
  5484. (if org-odd-levels-only
  5485. (and limit-level (1- (* limit-level 2)))
  5486. limit-level)))
  5487. (org-outline-regexp
  5488. (if (not (eq major-mode 'org-mode))
  5489. outline-regexp
  5490. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5491. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5492. (not (looking-at org-outline-regexp))))
  5493. (org-cycle-hook
  5494. (if bob-special
  5495. (delq 'org-optimize-window-after-visibility-change
  5496. (copy-sequence org-cycle-hook))
  5497. org-cycle-hook))
  5498. (pos (point)))
  5499. (if (or bob-special (equal arg '(4)))
  5500. ;; special case: use global cycling
  5501. (setq arg t))
  5502. (cond
  5503. ((equal arg '(16))
  5504. (setq last-command 'dummy)
  5505. (org-set-startup-visibility)
  5506. (message "Startup visibility, plus VISIBILITY properties"))
  5507. ((equal arg '(64))
  5508. (show-all)
  5509. (message "Entire buffer visible, including drawers"))
  5510. ;; Table: enter it or move to the next field.
  5511. ((org-at-table-p 'any)
  5512. (if (org-at-table.el-p)
  5513. (message "Use C-c ' to edit table.el tables")
  5514. (if arg (org-table-edit-field t)
  5515. (org-table-justify-field-maybe)
  5516. (call-interactively 'org-table-next-field))))
  5517. ((run-hook-with-args-until-success
  5518. 'org-tab-after-check-for-table-hook))
  5519. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5520. ((eq arg t) (org-cycle-internal-global))
  5521. ;; Drawers: delegate to `org-flag-drawer'.
  5522. ((and org-drawers org-drawer-regexp
  5523. (save-excursion
  5524. (beginning-of-line 1)
  5525. (looking-at org-drawer-regexp)))
  5526. (org-flag-drawer ; toggle block visibility
  5527. (not (get-char-property (match-end 0) 'invisible))))
  5528. ;; Show-subtree, ARG levels up from here.
  5529. ((integerp arg)
  5530. (save-excursion
  5531. (org-back-to-heading)
  5532. (outline-up-heading (if (< arg 0) (- arg)
  5533. (- (funcall outline-level) arg)))
  5534. (org-show-subtree)))
  5535. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5536. ((and (featurep 'org-inlinetask)
  5537. (org-inlinetask-at-task-p)
  5538. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5539. (org-inlinetask-toggle-visibility))
  5540. ((org-try-cdlatex-tab))
  5541. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5542. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5543. (save-excursion (beginning-of-line 1)
  5544. (looking-at org-outline-regexp)))
  5545. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5546. (org-cycle-internal-local))
  5547. ;; From there: TAB emulation and template completion.
  5548. (buffer-read-only (org-back-to-heading))
  5549. ((run-hook-with-args-until-success
  5550. 'org-tab-after-check-for-cycling-hook))
  5551. ((org-try-structure-completion))
  5552. ((run-hook-with-args-until-success
  5553. 'org-tab-before-tab-emulation-hook))
  5554. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5555. (or (not (bolp))
  5556. (not (looking-at org-outline-regexp))))
  5557. (call-interactively (global-key-binding "\t")))
  5558. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5559. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5560. (or (and (eq org-cycle-emulate-tab 'white)
  5561. (= (match-end 0) (point-at-eol)))
  5562. (and (eq org-cycle-emulate-tab 'whitestart)
  5563. (>= (match-end 0) pos))))
  5564. t
  5565. (eq org-cycle-emulate-tab t))
  5566. (call-interactively (global-key-binding "\t")))
  5567. (t (save-excursion
  5568. (org-back-to-heading)
  5569. (org-cycle)))))))
  5570. (defun org-cycle-internal-global ()
  5571. "Do the global cycling action."
  5572. ;; Hack to avoid display of messages for .org attachments in Gnus
  5573. (let ((ga (string-match "\\*fontification" (buffer-name))))
  5574. (cond
  5575. ((and (eq last-command this-command)
  5576. (eq org-cycle-global-status 'overview))
  5577. ;; We just created the overview - now do table of contents
  5578. ;; This can be slow in very large buffers, so indicate action
  5579. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  5580. (unless ga (message "CONTENTS..."))
  5581. (org-content)
  5582. (unless ga (message "CONTENTS...done"))
  5583. (setq org-cycle-global-status 'contents)
  5584. (run-hook-with-args 'org-cycle-hook 'contents))
  5585. ((and (eq last-command this-command)
  5586. (eq org-cycle-global-status 'contents))
  5587. ;; We just showed the table of contents - now show everything
  5588. (run-hook-with-args 'org-pre-cycle-hook 'all)
  5589. (show-all)
  5590. (unless ga (message "SHOW ALL"))
  5591. (setq org-cycle-global-status 'all)
  5592. (run-hook-with-args 'org-cycle-hook 'all))
  5593. (t
  5594. ;; Default action: go to overview
  5595. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  5596. (org-overview)
  5597. (unless ga (message "OVERVIEW"))
  5598. (setq org-cycle-global-status 'overview)
  5599. (run-hook-with-args 'org-cycle-hook 'overview)))))
  5600. (defun org-cycle-internal-local ()
  5601. "Do the local cycling action."
  5602. (let ((goal-column 0) eoh eol eos has-children children-skipped struct)
  5603. ;; First, determine end of headline (EOH), end of subtree or item
  5604. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  5605. (save-excursion
  5606. (if (org-at-item-p)
  5607. (progn
  5608. (beginning-of-line)
  5609. (setq struct (org-list-struct))
  5610. (setq eoh (point-at-eol))
  5611. (setq eos (org-list-get-item-end-before-blank (point) struct))
  5612. (setq has-children (org-list-has-child-p (point) struct)))
  5613. (org-back-to-heading)
  5614. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  5615. (setq eos (save-excursion
  5616. (org-end-of-subtree t)
  5617. (unless (eobp)
  5618. (skip-chars-forward " \t\n"))
  5619. (if (eobp) (point) (1- (point)))))
  5620. (setq has-children
  5621. (or (save-excursion
  5622. (let ((level (funcall outline-level)))
  5623. (outline-next-heading)
  5624. (and (org-at-heading-p t)
  5625. (> (funcall outline-level) level))))
  5626. (save-excursion
  5627. (org-list-search-forward (org-item-beginning-re) eos t)))))
  5628. ;; Determine end invisible part of buffer (EOL)
  5629. (beginning-of-line 2)
  5630. ;; XEmacs doesn't have `next-single-char-property-change'
  5631. (if (featurep 'xemacs)
  5632. (while (and (not (eobp)) ;; this is like `next-line'
  5633. (get-char-property (1- (point)) 'invisible))
  5634. (beginning-of-line 2))
  5635. (while (and (not (eobp)) ;; this is like `next-line'
  5636. (get-char-property (1- (point)) 'invisible))
  5637. (goto-char (next-single-char-property-change (point) 'invisible))
  5638. (and (eolp) (beginning-of-line 2))))
  5639. (setq eol (point)))
  5640. ;; Find out what to do next and set `this-command'
  5641. (cond
  5642. ((= eos eoh)
  5643. ;; Nothing is hidden behind this heading
  5644. (run-hook-with-args 'org-pre-cycle-hook 'empty)
  5645. (message "EMPTY ENTRY")
  5646. (setq org-cycle-subtree-status nil)
  5647. (save-excursion
  5648. (goto-char eos)
  5649. (outline-next-heading)
  5650. (if (outline-invisible-p) (org-flag-heading nil))))
  5651. ((and (or (>= eol eos)
  5652. (not (string-match "\\S-" (buffer-substring eol eos))))
  5653. (or has-children
  5654. (not (setq children-skipped
  5655. org-cycle-skip-children-state-if-no-children))))
  5656. ;; Entire subtree is hidden in one line: children view
  5657. (run-hook-with-args 'org-pre-cycle-hook 'children)
  5658. (if (org-at-item-p)
  5659. (org-list-set-item-visibility (point-at-bol) struct 'children)
  5660. (org-show-entry)
  5661. (org-with-limited-levels (show-children))
  5662. ;; FIXME: This slows down the func way too much.
  5663. ;; How keep drawers hidden in subtree anyway?
  5664. ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
  5665. ;; (org-cycle-hide-drawers 'subtree))
  5666. ;; Fold every list in subtree to top-level items.
  5667. (when (eq org-cycle-include-plain-lists 'integrate)
  5668. (save-excursion
  5669. (org-back-to-heading)
  5670. (while (org-list-search-forward (org-item-beginning-re) eos t)
  5671. (beginning-of-line 1)
  5672. (let* ((struct (org-list-struct))
  5673. (prevs (org-list-prevs-alist struct))
  5674. (end (org-list-get-bottom-point struct)))
  5675. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  5676. (org-list-get-all-items (point) struct prevs))
  5677. (goto-char end))))))
  5678. (message "CHILDREN")
  5679. (save-excursion
  5680. (goto-char eos)
  5681. (outline-next-heading)
  5682. (if (outline-invisible-p) (org-flag-heading nil)))
  5683. (setq org-cycle-subtree-status 'children)
  5684. (run-hook-with-args 'org-cycle-hook 'children))
  5685. ((or children-skipped
  5686. (and (eq last-command this-command)
  5687. (eq org-cycle-subtree-status 'children)))
  5688. ;; We just showed the children, or no children are there,
  5689. ;; now show everything.
  5690. (run-hook-with-args 'org-pre-cycle-hook 'subtree)
  5691. (outline-flag-region eoh eos nil)
  5692. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  5693. (setq org-cycle-subtree-status 'subtree)
  5694. (run-hook-with-args 'org-cycle-hook 'subtree))
  5695. (t
  5696. ;; Default action: hide the subtree.
  5697. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  5698. (outline-flag-region eoh eos t)
  5699. (message "FOLDED")
  5700. (setq org-cycle-subtree-status 'folded)
  5701. (run-hook-with-args 'org-cycle-hook 'folded)))))
  5702. ;;;###autoload
  5703. (defun org-global-cycle (&optional arg)
  5704. "Cycle the global visibility. For details see `org-cycle'.
  5705. With \\[universal-argument] prefix arg, switch to startup visibility.
  5706. With a numeric prefix, show all headlines up to that level."
  5707. (interactive "P")
  5708. (let ((org-cycle-include-plain-lists
  5709. (if (eq major-mode 'org-mode) org-cycle-include-plain-lists nil)))
  5710. (cond
  5711. ((integerp arg)
  5712. (show-all)
  5713. (hide-sublevels arg)
  5714. (setq org-cycle-global-status 'contents))
  5715. ((equal arg '(4))
  5716. (org-set-startup-visibility)
  5717. (message "Startup visibility, plus VISIBILITY properties."))
  5718. (t
  5719. (org-cycle '(4))))))
  5720. (defun org-set-startup-visibility ()
  5721. "Set the visibility required by startup options and properties."
  5722. (cond
  5723. ((eq org-startup-folded t)
  5724. (org-cycle '(4)))
  5725. ((eq org-startup-folded 'content)
  5726. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  5727. (org-cycle '(4)) (org-cycle '(4)))))
  5728. (unless (eq org-startup-folded 'showeverything)
  5729. (if org-hide-block-startup (org-hide-block-all))
  5730. (org-set-visibility-according-to-property 'no-cleanup)
  5731. (org-cycle-hide-archived-subtrees 'all)
  5732. (org-cycle-hide-drawers 'all)
  5733. (org-cycle-show-empty-lines t)))
  5734. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  5735. "Switch subtree visibilities according to :VISIBILITY: property."
  5736. (interactive)
  5737. (let (org-show-entry-below state)
  5738. (save-excursion
  5739. (goto-char (point-min))
  5740. (while (re-search-forward
  5741. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  5742. nil t)
  5743. (setq state (match-string 1))
  5744. (save-excursion
  5745. (org-back-to-heading t)
  5746. (hide-subtree)
  5747. (org-reveal)
  5748. (cond
  5749. ((equal state '("fold" "folded"))
  5750. (hide-subtree))
  5751. ((equal state "children")
  5752. (org-show-hidden-entry)
  5753. (show-children))
  5754. ((equal state "content")
  5755. (save-excursion
  5756. (save-restriction
  5757. (org-narrow-to-subtree)
  5758. (org-content))))
  5759. ((member state '("all" "showall"))
  5760. (show-subtree)))))
  5761. (unless no-cleanup
  5762. (org-cycle-hide-archived-subtrees 'all)
  5763. (org-cycle-hide-drawers 'all)
  5764. (org-cycle-show-empty-lines 'all)))))
  5765. ;; This function uses outline-regexp instead of the more fundamental
  5766. ;; org-outline-regexp so that org-cycle-global works outside of Org
  5767. ;; buffers, where outline-regexp is needed.
  5768. (defun org-overview ()
  5769. "Switch to overview mode, showing only top-level headlines.
  5770. Really, this shows all headlines with level equal or greater than the level
  5771. of the first headline in the buffer. This is important, because if the
  5772. first headline is not level one, then (hide-sublevels 1) gives confusing
  5773. results."
  5774. (interactive)
  5775. (let ((level (save-excursion
  5776. (goto-char (point-min))
  5777. (if (re-search-forward (concat "^" outline-regexp) nil t)
  5778. (progn
  5779. (goto-char (match-beginning 0))
  5780. (funcall outline-level))))))
  5781. (and level (hide-sublevels level))))
  5782. (defun org-content (&optional arg)
  5783. "Show all headlines in the buffer, like a table of contents.
  5784. With numerical argument N, show content up to level N."
  5785. (interactive "P")
  5786. (save-excursion
  5787. ;; Visit all headings and show their offspring
  5788. (and (integerp arg) (org-overview))
  5789. (goto-char (point-max))
  5790. (catch 'exit
  5791. (while (and (progn (condition-case nil
  5792. (outline-previous-visible-heading 1)
  5793. (error (goto-char (point-min))))
  5794. t)
  5795. (looking-at org-outline-regexp))
  5796. (if (integerp arg)
  5797. (show-children (1- arg))
  5798. (show-branches))
  5799. (if (bobp) (throw 'exit nil))))))
  5800. (defun org-optimize-window-after-visibility-change (state)
  5801. "Adjust the window after a change in outline visibility.
  5802. This function is the default value of the hook `org-cycle-hook'."
  5803. (when (get-buffer-window (current-buffer))
  5804. (cond
  5805. ((eq state 'content) nil)
  5806. ((eq state 'all) nil)
  5807. ((eq state 'folded) nil)
  5808. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  5809. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  5810. (defun org-remove-empty-overlays-at (pos)
  5811. "Remove outline overlays that do not contain non-white stuff."
  5812. (mapc
  5813. (lambda (o)
  5814. (and (eq 'outline (overlay-get o 'invisible))
  5815. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  5816. (overlay-end o))))
  5817. (delete-overlay o)))
  5818. (overlays-at pos)))
  5819. (defun org-clean-visibility-after-subtree-move ()
  5820. "Fix visibility issues after moving a subtree."
  5821. ;; First, find a reasonable region to look at:
  5822. ;; Start two siblings above, end three below
  5823. (let* ((beg (save-excursion
  5824. (and (org-get-last-sibling)
  5825. (org-get-last-sibling))
  5826. (point)))
  5827. (end (save-excursion
  5828. (and (org-get-next-sibling)
  5829. (org-get-next-sibling)
  5830. (org-get-next-sibling))
  5831. (if (org-at-heading-p)
  5832. (point-at-eol)
  5833. (point))))
  5834. (level (looking-at "\\*+"))
  5835. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  5836. (save-excursion
  5837. (save-restriction
  5838. (narrow-to-region beg end)
  5839. (when re
  5840. ;; Properly fold already folded siblings
  5841. (goto-char (point-min))
  5842. (while (re-search-forward re nil t)
  5843. (if (and (not (outline-invisible-p))
  5844. (save-excursion
  5845. (goto-char (point-at-eol)) (outline-invisible-p)))
  5846. (hide-entry))))
  5847. (org-cycle-show-empty-lines 'overview)
  5848. (org-cycle-hide-drawers 'overview)))))
  5849. (defun org-cycle-show-empty-lines (state)
  5850. "Show empty lines above all visible headlines.
  5851. The region to be covered depends on STATE when called through
  5852. `org-cycle-hook'. Lisp program can use t for STATE to get the
  5853. entire buffer covered. Note that an empty line is only shown if there
  5854. are at least `org-cycle-separator-lines' empty lines before the headline."
  5855. (when (not (= org-cycle-separator-lines 0))
  5856. (save-excursion
  5857. (let* ((n (abs org-cycle-separator-lines))
  5858. (re (cond
  5859. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  5860. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  5861. (t (let ((ns (number-to-string (- n 2))))
  5862. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  5863. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  5864. beg end b e)
  5865. (cond
  5866. ((memq state '(overview contents t))
  5867. (setq beg (point-min) end (point-max)))
  5868. ((memq state '(children folded))
  5869. (setq beg (point) end (progn (org-end-of-subtree t t)
  5870. (beginning-of-line 2)
  5871. (point)))))
  5872. (when beg
  5873. (goto-char beg)
  5874. (while (re-search-forward re end t)
  5875. (unless (get-char-property (match-end 1) 'invisible)
  5876. (setq e (match-end 1))
  5877. (if (< org-cycle-separator-lines 0)
  5878. (setq b (save-excursion
  5879. (goto-char (match-beginning 0))
  5880. (org-back-over-empty-lines)
  5881. (if (save-excursion
  5882. (goto-char (max (point-min) (1- (point))))
  5883. (org-at-heading-p))
  5884. (1- (point))
  5885. (point))))
  5886. (setq b (match-beginning 1)))
  5887. (outline-flag-region b e nil)))))))
  5888. ;; Never hide empty lines at the end of the file.
  5889. (save-excursion
  5890. (goto-char (point-max))
  5891. (outline-previous-heading)
  5892. (outline-end-of-heading)
  5893. (if (and (looking-at "[ \t\n]+")
  5894. (= (match-end 0) (point-max)))
  5895. (outline-flag-region (point) (match-end 0) nil))))
  5896. (defun org-show-empty-lines-in-parent ()
  5897. "Move to the parent and re-show empty lines before visible headlines."
  5898. (save-excursion
  5899. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  5900. (org-cycle-show-empty-lines context))))
  5901. (defun org-files-list ()
  5902. "Return `org-agenda-files' list, plus all open org-mode files.
  5903. This is useful for operations that need to scan all of a user's
  5904. open and agenda-wise Org files."
  5905. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  5906. (dolist (buf (buffer-list))
  5907. (with-current-buffer buf
  5908. (if (and (eq major-mode 'org-mode) (buffer-file-name))
  5909. (let ((file (expand-file-name (buffer-file-name))))
  5910. (unless (member file files)
  5911. (push file files))))))
  5912. files))
  5913. (defsubst org-entry-beginning-position ()
  5914. "Return the beginning position of the current entry."
  5915. (save-excursion (outline-back-to-heading t) (point)))
  5916. (defsubst org-entry-end-position ()
  5917. "Return the end position of the current entry."
  5918. (save-excursion (outline-next-heading) (point)))
  5919. (defun org-cycle-hide-drawers (state)
  5920. "Re-hide all drawers after a visibility state change."
  5921. (when (and (eq major-mode 'org-mode)
  5922. (not (memq state '(overview folded contents))))
  5923. (save-excursion
  5924. (let* ((globalp (memq state '(contents all)))
  5925. (beg (if globalp (point-min) (point)))
  5926. (end (if globalp (point-max)
  5927. (if (eq state 'children)
  5928. (save-excursion (outline-next-heading) (point))
  5929. (org-end-of-subtree t)))))
  5930. (goto-char beg)
  5931. (while (re-search-forward org-drawer-regexp end t)
  5932. (org-flag-drawer t))))))
  5933. (defun org-flag-drawer (flag)
  5934. (save-excursion
  5935. (beginning-of-line 1)
  5936. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  5937. (let ((b (match-end 0)))
  5938. (if (re-search-forward
  5939. "^[ \t]*:END:"
  5940. (save-excursion (outline-next-heading) (point)) t)
  5941. (outline-flag-region b (point-at-eol) flag)
  5942. (error ":END: line missing at position %s" b))))))
  5943. (defun org-subtree-end-visible-p ()
  5944. "Is the end of the current subtree visible?"
  5945. (pos-visible-in-window-p
  5946. (save-excursion (org-end-of-subtree t) (point))))
  5947. (defun org-first-headline-recenter (&optional N)
  5948. "Move cursor to the first headline and recenter the headline.
  5949. Optional argument N means put the headline into the Nth line of the window."
  5950. (goto-char (point-min))
  5951. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  5952. (beginning-of-line)
  5953. (recenter (prefix-numeric-value N))))
  5954. ;;; Saving and restoring visibility
  5955. (defun org-outline-overlay-data (&optional use-markers)
  5956. "Return a list of the locations of all outline overlays.
  5957. These are overlays with the `invisible' property value `outline'.
  5958. The return value is a list of cons cells, with start and stop
  5959. positions for each overlay.
  5960. If USE-MARKERS is set, return the positions as markers."
  5961. (let (beg end)
  5962. (save-excursion
  5963. (save-restriction
  5964. (widen)
  5965. (delq nil
  5966. (mapcar (lambda (o)
  5967. (when (eq (overlay-get o 'invisible) 'outline)
  5968. (setq beg (overlay-start o)
  5969. end (overlay-end o))
  5970. (and beg end (> end beg)
  5971. (if use-markers
  5972. (cons (move-marker (make-marker) beg)
  5973. (move-marker (make-marker) end))
  5974. (cons beg end)))))
  5975. (overlays-in (point-min) (point-max))))))))
  5976. (defun org-set-outline-overlay-data (data)
  5977. "Create visibility overlays for all positions in DATA.
  5978. DATA should have been made by `org-outline-overlay-data'."
  5979. (let (o)
  5980. (save-excursion
  5981. (save-restriction
  5982. (widen)
  5983. (show-all)
  5984. (mapc (lambda (c)
  5985. (outline-flag-region (car c) (cdr c) t))
  5986. data)))))
  5987. ;;; Folding of blocks
  5988. (defvar org-hide-block-overlays nil
  5989. "Overlays hiding blocks.")
  5990. (make-variable-buffer-local 'org-hide-block-overlays)
  5991. (defun org-block-map (function &optional start end)
  5992. "Call FUNCTION at the head of all source blocks in the current buffer.
  5993. Optional arguments START and END can be used to limit the range."
  5994. (let ((start (or start (point-min)))
  5995. (end (or end (point-max))))
  5996. (save-excursion
  5997. (goto-char start)
  5998. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  5999. (save-excursion
  6000. (save-match-data
  6001. (goto-char (match-beginning 0))
  6002. (funcall function)))))))
  6003. (defun org-hide-block-toggle-all ()
  6004. "Toggle the visibility of all blocks in the current buffer."
  6005. (org-block-map #'org-hide-block-toggle))
  6006. (defun org-hide-block-all ()
  6007. "Fold all blocks in the current buffer."
  6008. (interactive)
  6009. (org-show-block-all)
  6010. (org-block-map #'org-hide-block-toggle-maybe))
  6011. (defun org-show-block-all ()
  6012. "Unfold all blocks in the current buffer."
  6013. (interactive)
  6014. (mapc 'delete-overlay org-hide-block-overlays)
  6015. (setq org-hide-block-overlays nil))
  6016. (defun org-hide-block-toggle-maybe ()
  6017. "Toggle visibility of block at point."
  6018. (interactive)
  6019. (let ((case-fold-search t))
  6020. (if (save-excursion
  6021. (beginning-of-line 1)
  6022. (looking-at org-block-regexp))
  6023. (progn (org-hide-block-toggle)
  6024. t) ;; to signal that we took action
  6025. nil))) ;; to signal that we did not
  6026. (defun org-hide-block-toggle (&optional force)
  6027. "Toggle the visibility of the current block."
  6028. (interactive)
  6029. (save-excursion
  6030. (beginning-of-line)
  6031. (if (re-search-forward org-block-regexp nil t)
  6032. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  6033. (end (match-end 0)) ;; end of entire body
  6034. ov)
  6035. (if (memq t (mapcar (lambda (overlay)
  6036. (eq (overlay-get overlay 'invisible)
  6037. 'org-hide-block))
  6038. (overlays-at start)))
  6039. (if (or (not force) (eq force 'off))
  6040. (mapc (lambda (ov)
  6041. (when (member ov org-hide-block-overlays)
  6042. (setq org-hide-block-overlays
  6043. (delq ov org-hide-block-overlays)))
  6044. (when (eq (overlay-get ov 'invisible)
  6045. 'org-hide-block)
  6046. (delete-overlay ov)))
  6047. (overlays-at start)))
  6048. (setq ov (make-overlay start end))
  6049. (overlay-put ov 'invisible 'org-hide-block)
  6050. ;; make the block accessible to isearch
  6051. (overlay-put
  6052. ov 'isearch-open-invisible
  6053. (lambda (ov)
  6054. (when (member ov org-hide-block-overlays)
  6055. (setq org-hide-block-overlays
  6056. (delq ov org-hide-block-overlays)))
  6057. (when (eq (overlay-get ov 'invisible)
  6058. 'org-hide-block)
  6059. (delete-overlay ov))))
  6060. (push ov org-hide-block-overlays)))
  6061. (error "Not looking at a source block"))))
  6062. ;; org-tab-after-check-for-cycling-hook
  6063. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  6064. ;; Remove overlays when changing major mode
  6065. (add-hook 'org-mode-hook
  6066. (lambda () (org-add-hook 'change-major-mode-hook
  6067. 'org-show-block-all 'append 'local)))
  6068. ;;; Org-goto
  6069. (defvar org-goto-window-configuration nil)
  6070. (defvar org-goto-marker nil)
  6071. (defvar org-goto-map
  6072. (let ((map (make-sparse-keymap)))
  6073. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  6074. (while (setq cmd (pop cmds))
  6075. (substitute-key-definition cmd cmd map global-map)))
  6076. (suppress-keymap map)
  6077. (org-defkey map "\C-m" 'org-goto-ret)
  6078. (org-defkey map [(return)] 'org-goto-ret)
  6079. (org-defkey map [(left)] 'org-goto-left)
  6080. (org-defkey map [(right)] 'org-goto-right)
  6081. (org-defkey map [(control ?g)] 'org-goto-quit)
  6082. (org-defkey map "\C-i" 'org-cycle)
  6083. (org-defkey map [(tab)] 'org-cycle)
  6084. (org-defkey map [(down)] 'outline-next-visible-heading)
  6085. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6086. (if org-goto-auto-isearch
  6087. (if (fboundp 'define-key-after)
  6088. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6089. nil)
  6090. (org-defkey map "q" 'org-goto-quit)
  6091. (org-defkey map "n" 'outline-next-visible-heading)
  6092. (org-defkey map "p" 'outline-previous-visible-heading)
  6093. (org-defkey map "f" 'outline-forward-same-level)
  6094. (org-defkey map "b" 'outline-backward-same-level)
  6095. (org-defkey map "u" 'outline-up-heading))
  6096. (org-defkey map "/" 'org-occur)
  6097. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6098. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6099. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6100. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6101. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6102. map))
  6103. (defconst org-goto-help
  6104. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  6105. RET=jump to location [Q]uit and return to previous location
  6106. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6107. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6108. ;; FIXME: Docstring does not mention both interfaces
  6109. (defun org-goto (&optional alternative-interface)
  6110. "Look up a different location in the current file, keeping current visibility.
  6111. When you want look-up or go to a different location in a document, the
  6112. fastest way is often to fold the entire buffer and then dive into the tree.
  6113. This method has the disadvantage, that the previous location will be folded,
  6114. which may not be what you want.
  6115. This command works around this by showing a copy of the current buffer
  6116. in an indirect buffer, in overview mode. You can dive into the tree in
  6117. that copy, use org-occur and incremental search to find a location.
  6118. When pressing RET or `Q', the command returns to the original buffer in
  6119. which the visibility is still unchanged. After RET it will also jump to
  6120. the location selected in the indirect buffer and expose the headline
  6121. hierarchy above."
  6122. (interactive "P")
  6123. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6124. (org-refile-use-outline-path t)
  6125. (org-refile-target-verify-function nil)
  6126. (interface
  6127. (if (not alternative-interface)
  6128. org-goto-interface
  6129. (if (eq org-goto-interface 'outline)
  6130. 'outline-path-completion
  6131. 'outline)))
  6132. (org-goto-start-pos (point))
  6133. (selected-point
  6134. (if (eq interface 'outline)
  6135. (car (org-get-location (current-buffer) org-goto-help))
  6136. (let ((pa (org-refile-get-location "Goto" nil nil t)))
  6137. (org-refile-check-position pa)
  6138. (nth 3 pa)))))
  6139. (if selected-point
  6140. (progn
  6141. (org-mark-ring-push org-goto-start-pos)
  6142. (goto-char selected-point)
  6143. (if (or (outline-invisible-p) (org-invisible-p2))
  6144. (org-show-context 'org-goto)))
  6145. (message "Quit"))))
  6146. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6147. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6148. (defvar org-goto-local-auto-isearch-map) ; defined below
  6149. (defun org-get-location (buf help)
  6150. "Let the user select a location in the Org-mode buffer BUF.
  6151. This function uses a recursive edit. It returns the selected position
  6152. or nil."
  6153. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6154. (isearch-hide-immediately nil)
  6155. (isearch-search-fun-function
  6156. (lambda () 'org-goto-local-search-headings))
  6157. (org-goto-selected-point org-goto-exit-command)
  6158. (pop-up-frames nil)
  6159. (special-display-buffer-names nil)
  6160. (special-display-regexps nil)
  6161. (special-display-function nil))
  6162. (save-excursion
  6163. (save-window-excursion
  6164. (delete-other-windows)
  6165. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6166. (org-pop-to-buffer-same-window
  6167. (condition-case nil
  6168. (make-indirect-buffer (current-buffer) "*org-goto*")
  6169. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6170. (with-output-to-temp-buffer "*Help*"
  6171. (princ help))
  6172. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  6173. (setq buffer-read-only nil)
  6174. (let ((org-startup-truncated t)
  6175. (org-startup-folded nil)
  6176. (org-startup-align-all-tables nil))
  6177. (org-mode)
  6178. (org-overview))
  6179. (setq buffer-read-only t)
  6180. (if (and (boundp 'org-goto-start-pos)
  6181. (integer-or-marker-p org-goto-start-pos))
  6182. (let ((org-show-hierarchy-above t)
  6183. (org-show-siblings t)
  6184. (org-show-following-heading t))
  6185. (goto-char org-goto-start-pos)
  6186. (and (outline-invisible-p) (org-show-context)))
  6187. (goto-char (point-min)))
  6188. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6189. (message "Select location and press RET")
  6190. (use-local-map org-goto-map)
  6191. (recursive-edit)
  6192. ))
  6193. (kill-buffer "*org-goto*")
  6194. (cons org-goto-selected-point org-goto-exit-command)))
  6195. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6196. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6197. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6198. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  6199. (defun org-goto-local-search-headings (string bound noerror)
  6200. "Search and make sure that any matches are in headlines."
  6201. (catch 'return
  6202. (while (if isearch-forward
  6203. (search-forward string bound noerror)
  6204. (search-backward string bound noerror))
  6205. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  6206. (and (member :headline context)
  6207. (not (member :tags context))))
  6208. (throw 'return (point))))))
  6209. (defun org-goto-local-auto-isearch ()
  6210. "Start isearch."
  6211. (interactive)
  6212. (goto-char (point-min))
  6213. (let ((keys (this-command-keys)))
  6214. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6215. (isearch-mode t)
  6216. (isearch-process-search-char (string-to-char keys)))))
  6217. (defun org-goto-ret (&optional arg)
  6218. "Finish `org-goto' by going to the new location."
  6219. (interactive "P")
  6220. (setq org-goto-selected-point (point)
  6221. org-goto-exit-command 'return)
  6222. (throw 'exit nil))
  6223. (defun org-goto-left ()
  6224. "Finish `org-goto' by going to the new location."
  6225. (interactive)
  6226. (if (org-at-heading-p)
  6227. (progn
  6228. (beginning-of-line 1)
  6229. (setq org-goto-selected-point (point)
  6230. org-goto-exit-command 'left)
  6231. (throw 'exit nil))
  6232. (error "Not on a heading")))
  6233. (defun org-goto-right ()
  6234. "Finish `org-goto' by going to the new location."
  6235. (interactive)
  6236. (if (org-at-heading-p)
  6237. (progn
  6238. (setq org-goto-selected-point (point)
  6239. org-goto-exit-command 'right)
  6240. (throw 'exit nil))
  6241. (error "Not on a heading")))
  6242. (defun org-goto-quit ()
  6243. "Finish `org-goto' without cursor motion."
  6244. (interactive)
  6245. (setq org-goto-selected-point nil)
  6246. (setq org-goto-exit-command 'quit)
  6247. (throw 'exit nil))
  6248. ;;; Indirect buffer display of subtrees
  6249. (defvar org-indirect-dedicated-frame nil
  6250. "This is the frame being used for indirect tree display.")
  6251. (defvar org-last-indirect-buffer nil)
  6252. (defun org-tree-to-indirect-buffer (&optional arg)
  6253. "Create indirect buffer and narrow it to current subtree.
  6254. With numerical prefix ARG, go up to this level and then take that tree.
  6255. If ARG is negative, go up that many levels.
  6256. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6257. indirect buffer previously made with this command, to avoid proliferation of
  6258. indirect buffers. However, when you call the command with a \
  6259. \\[universal-argument] prefix, or
  6260. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6261. is kept so that you can work with several indirect buffers at the same time.
  6262. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6263. \\[universal-argument] prefix also
  6264. requests that a new frame be made for the new buffer, so that the dedicated
  6265. frame is not changed."
  6266. (interactive "P")
  6267. (let ((cbuf (current-buffer))
  6268. (cwin (selected-window))
  6269. (pos (point))
  6270. beg end level heading ibuf)
  6271. (save-excursion
  6272. (org-back-to-heading t)
  6273. (when (numberp arg)
  6274. (setq level (org-outline-level))
  6275. (if (< arg 0) (setq arg (+ level arg)))
  6276. (while (> (setq level (org-outline-level)) arg)
  6277. (outline-up-heading 1 t)))
  6278. (setq beg (point)
  6279. heading (org-get-heading))
  6280. (org-end-of-subtree t t)
  6281. (if (org-at-heading-p) (backward-char 1))
  6282. (setq end (point)))
  6283. (if (and (buffer-live-p org-last-indirect-buffer)
  6284. (not (eq org-indirect-buffer-display 'new-frame))
  6285. (not arg))
  6286. (kill-buffer org-last-indirect-buffer))
  6287. (setq ibuf (org-get-indirect-buffer cbuf)
  6288. org-last-indirect-buffer ibuf)
  6289. (cond
  6290. ((or (eq org-indirect-buffer-display 'new-frame)
  6291. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6292. (select-frame (make-frame))
  6293. (delete-other-windows)
  6294. (org-pop-to-buffer-same-window ibuf)
  6295. (org-set-frame-title heading))
  6296. ((eq org-indirect-buffer-display 'dedicated-frame)
  6297. (raise-frame
  6298. (select-frame (or (and org-indirect-dedicated-frame
  6299. (frame-live-p org-indirect-dedicated-frame)
  6300. org-indirect-dedicated-frame)
  6301. (setq org-indirect-dedicated-frame (make-frame)))))
  6302. (delete-other-windows)
  6303. (org-pop-to-buffer-same-window ibuf)
  6304. (org-set-frame-title (concat "Indirect: " heading)))
  6305. ((eq org-indirect-buffer-display 'current-window)
  6306. (org-pop-to-buffer-same-window ibuf))
  6307. ((eq org-indirect-buffer-display 'other-window)
  6308. (pop-to-buffer ibuf))
  6309. (t (error "Invalid value")))
  6310. (if (featurep 'xemacs)
  6311. (save-excursion (org-mode) (turn-on-font-lock)))
  6312. (narrow-to-region beg end)
  6313. (show-all)
  6314. (goto-char pos)
  6315. (run-hook-with-args 'org-cycle-hook 'all)
  6316. (and (window-live-p cwin) (select-window cwin))))
  6317. (defun org-get-indirect-buffer (&optional buffer)
  6318. (setq buffer (or buffer (current-buffer)))
  6319. (let ((n 1) (base (buffer-name buffer)) bname)
  6320. (while (buffer-live-p
  6321. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  6322. (setq n (1+ n)))
  6323. (condition-case nil
  6324. (make-indirect-buffer buffer bname 'clone)
  6325. (error (make-indirect-buffer buffer bname)))))
  6326. (defun org-set-frame-title (title)
  6327. "Set the title of the current frame to the string TITLE."
  6328. ;; FIXME: how to name a single frame in XEmacs???
  6329. (unless (featurep 'xemacs)
  6330. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6331. ;;;; Structure editing
  6332. ;;; Inserting headlines
  6333. (defun org-previous-line-empty-p ()
  6334. (save-excursion
  6335. (and (not (bobp))
  6336. (or (beginning-of-line 0) t)
  6337. (save-match-data
  6338. (looking-at "[ \t]*$")))))
  6339. (defun org-insert-heading (&optional force-heading invisible-ok)
  6340. "Insert a new heading or item with same depth at point.
  6341. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  6342. If point is at the beginning of a headline, insert a sibling before the
  6343. current headline. If point is not at the beginning, split the line,
  6344. create the new headline with the text in the current line after point
  6345. \(but see also the variable `org-M-RET-may-split-line').
  6346. When INVISIBLE-OK is set, stop at invisible headlines when going back.
  6347. This is important for non-interactive uses of the command."
  6348. (interactive "P")
  6349. (if (or (= (buffer-size) 0)
  6350. (and (not (save-excursion
  6351. (and (ignore-errors (org-back-to-heading invisible-ok))
  6352. (org-at-heading-p))))
  6353. (or force-heading (not (org-in-item-p)))))
  6354. (progn
  6355. (insert "\n* ")
  6356. (run-hooks 'org-insert-heading-hook))
  6357. (when (or force-heading (not (org-insert-item)))
  6358. (let* ((empty-line-p nil)
  6359. (level nil)
  6360. (on-heading (org-at-heading-p))
  6361. (head (save-excursion
  6362. (condition-case nil
  6363. (progn
  6364. (org-back-to-heading invisible-ok)
  6365. (when (and (not on-heading)
  6366. (featurep 'org-inlinetask)
  6367. (integerp org-inlinetask-min-level)
  6368. (>= (length (match-string 0))
  6369. org-inlinetask-min-level))
  6370. ;; Find a heading level before the inline task
  6371. (while (and (setq level (org-up-heading-safe))
  6372. (>= level org-inlinetask-min-level)))
  6373. (if (org-at-heading-p)
  6374. (org-back-to-heading invisible-ok)
  6375. (error "This should not happen")))
  6376. (setq empty-line-p (org-previous-line-empty-p))
  6377. (match-string 0))
  6378. (error "*"))))
  6379. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6380. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6381. pos hide-previous previous-pos)
  6382. (cond
  6383. ((and (org-at-heading-p) (bolp)
  6384. (or (bobp)
  6385. (save-excursion (backward-char 1) (not (outline-invisible-p)))))
  6386. ;; insert before the current line
  6387. (open-line (if blank 2 1)))
  6388. ((and (bolp)
  6389. (not org-insert-heading-respect-content)
  6390. (or (bobp)
  6391. (save-excursion
  6392. (backward-char 1) (not (outline-invisible-p)))))
  6393. ;; insert right here
  6394. nil)
  6395. (t
  6396. ;; somewhere in the line
  6397. (save-excursion
  6398. (setq previous-pos (point-at-bol))
  6399. (end-of-line)
  6400. (setq hide-previous (outline-invisible-p)))
  6401. (and org-insert-heading-respect-content (org-show-subtree))
  6402. (let ((split
  6403. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  6404. (save-excursion
  6405. (let ((p (point)))
  6406. (goto-char (point-at-bol))
  6407. (and (looking-at org-complex-heading-regexp)
  6408. (match-beginning 4)
  6409. (> p (match-beginning 4)))))))
  6410. tags pos)
  6411. (cond
  6412. (org-insert-heading-respect-content
  6413. (org-end-of-subtree nil t)
  6414. (when (featurep 'org-inlinetask)
  6415. (while (and (not (eobp))
  6416. (looking-at "\\(\\*+\\)[ \t]+")
  6417. (>= (length (match-string 1))
  6418. org-inlinetask-min-level))
  6419. (org-end-of-subtree nil t)))
  6420. (or (bolp) (newline))
  6421. (or (org-previous-line-empty-p)
  6422. (and blank (newline)))
  6423. (open-line 1))
  6424. ((org-at-heading-p)
  6425. (when hide-previous
  6426. (show-children)
  6427. (org-show-entry))
  6428. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
  6429. (setq tags (and (match-end 2) (match-string 2)))
  6430. (and (match-end 1)
  6431. (delete-region (match-beginning 1) (match-end 1)))
  6432. (setq pos (point-at-bol))
  6433. (or split (end-of-line 1))
  6434. (delete-horizontal-space)
  6435. (if (string-match "\\`\\*+\\'"
  6436. (buffer-substring (point-at-bol) (point)))
  6437. (insert " "))
  6438. (newline (if blank 2 1))
  6439. (when tags
  6440. (save-excursion
  6441. (goto-char pos)
  6442. (end-of-line 1)
  6443. (insert " " tags)
  6444. (org-set-tags nil 'align))))
  6445. (t
  6446. (or split (end-of-line 1))
  6447. (newline (if blank 2 1)))))))
  6448. (insert head) (just-one-space)
  6449. (setq pos (point))
  6450. (end-of-line 1)
  6451. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  6452. (when (and org-insert-heading-respect-content hide-previous)
  6453. (save-excursion
  6454. (goto-char previous-pos)
  6455. (hide-subtree)))
  6456. (run-hooks 'org-insert-heading-hook)))))
  6457. (defun org-get-heading (&optional no-tags no-todo)
  6458. "Return the heading of the current entry, without the stars.
  6459. When NO-TAGS is non-nil, don't include tags.
  6460. When NO-TODO is non-nil, don't include TODO keywords."
  6461. (save-excursion
  6462. (org-back-to-heading t)
  6463. (cond
  6464. ((and no-tags no-todo)
  6465. (looking-at org-complex-heading-regexp)
  6466. (match-string 4))
  6467. (no-tags
  6468. (looking-at (concat org-outline-regexp
  6469. "\\(.*?\\)"
  6470. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  6471. (match-string 1))
  6472. (no-todo
  6473. (looking-at org-todo-line-regexp)
  6474. (match-string 3))
  6475. (t (looking-at org-heading-regexp)
  6476. (match-string 2)))))
  6477. (defun org-heading-components ()
  6478. "Return the components of the current heading.
  6479. This is a list with the following elements:
  6480. - the level as an integer
  6481. - the reduced level, different if `org-odd-levels-only' is set.
  6482. - the TODO keyword, or nil
  6483. - the priority character, like ?A, or nil if no priority is given
  6484. - the headline text itself, or the tags string if no headline text
  6485. - the tags string, or nil."
  6486. (save-excursion
  6487. (org-back-to-heading t)
  6488. (if (let (case-fold-search) (looking-at org-complex-heading-regexp))
  6489. (list (length (match-string 1))
  6490. (org-reduced-level (length (match-string 1)))
  6491. (org-match-string-no-properties 2)
  6492. (and (match-end 3) (aref (match-string 3) 2))
  6493. (org-match-string-no-properties 4)
  6494. (org-match-string-no-properties 5)))))
  6495. (defun org-get-entry ()
  6496. "Get the entry text, after heading, entire subtree."
  6497. (save-excursion
  6498. (org-back-to-heading t)
  6499. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  6500. (defun org-insert-heading-after-current ()
  6501. "Insert a new heading with same level as current, after current subtree."
  6502. (interactive)
  6503. (org-back-to-heading)
  6504. (org-insert-heading)
  6505. (org-move-subtree-down)
  6506. (end-of-line 1))
  6507. (defun org-insert-heading-respect-content ()
  6508. (interactive)
  6509. (let ((org-insert-heading-respect-content t))
  6510. (org-insert-heading t)))
  6511. (defun org-insert-todo-heading-respect-content (&optional force-state)
  6512. (interactive "P")
  6513. (let ((org-insert-heading-respect-content t))
  6514. (org-insert-todo-heading force-state t)))
  6515. (defun org-insert-todo-heading (arg &optional force-heading)
  6516. "Insert a new heading with the same level and TODO state as current heading.
  6517. If the heading has no TODO state, or if the state is DONE, use the first
  6518. state (TODO by default). Also with prefix arg, force first state."
  6519. (interactive "P")
  6520. (when (or force-heading (not (org-insert-item 'checkbox)))
  6521. (org-insert-heading force-heading)
  6522. (save-excursion
  6523. (org-back-to-heading)
  6524. (outline-previous-heading)
  6525. (looking-at org-todo-line-regexp))
  6526. (let*
  6527. ((new-mark-x
  6528. (if (or arg
  6529. (not (match-beginning 2))
  6530. (member (match-string 2) org-done-keywords))
  6531. (car org-todo-keywords-1)
  6532. (match-string 2)))
  6533. (new-mark
  6534. (or
  6535. (run-hook-with-args-until-success
  6536. 'org-todo-get-default-hook new-mark-x nil)
  6537. new-mark-x)))
  6538. (beginning-of-line 1)
  6539. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  6540. (if org-treat-insert-todo-heading-as-state-change
  6541. (org-todo new-mark)
  6542. (insert new-mark " "))))
  6543. (when org-provide-todo-statistics
  6544. (org-update-parent-todo-statistics))))
  6545. (defun org-insert-subheading (arg)
  6546. "Insert a new subheading and demote it.
  6547. Works for outline headings and for plain lists alike."
  6548. (interactive "P")
  6549. (org-insert-heading arg)
  6550. (cond
  6551. ((org-at-heading-p) (org-do-demote))
  6552. ((org-at-item-p) (org-indent-item))))
  6553. (defun org-insert-todo-subheading (arg)
  6554. "Insert a new subheading with TODO keyword or checkbox and demote it.
  6555. Works for outline headings and for plain lists alike."
  6556. (interactive "P")
  6557. (org-insert-todo-heading arg)
  6558. (cond
  6559. ((org-at-heading-p) (org-do-demote))
  6560. ((org-at-item-p) (org-indent-item))))
  6561. ;;; Promotion and Demotion
  6562. (defvar org-after-demote-entry-hook nil
  6563. "Hook run after an entry has been demoted.
  6564. The cursor will be at the beginning of the entry.
  6565. When a subtree is being demoted, the hook will be called for each node.")
  6566. (defvar org-after-promote-entry-hook nil
  6567. "Hook run after an entry has been promoted.
  6568. The cursor will be at the beginning of the entry.
  6569. When a subtree is being promoted, the hook will be called for each node.")
  6570. (defun org-promote-subtree ()
  6571. "Promote the entire subtree.
  6572. See also `org-promote'."
  6573. (interactive)
  6574. (save-excursion
  6575. (org-with-limited-levels (org-map-tree 'org-promote)))
  6576. (org-fix-position-after-promote))
  6577. (defun org-demote-subtree ()
  6578. "Demote the entire subtree. See `org-demote'.
  6579. See also `org-promote'."
  6580. (interactive)
  6581. (save-excursion
  6582. (org-with-limited-levels (org-map-tree 'org-demote)))
  6583. (org-fix-position-after-promote))
  6584. (defun org-do-promote ()
  6585. "Promote the current heading higher up the tree.
  6586. If the region is active in `transient-mark-mode', promote all headings
  6587. in the region."
  6588. (interactive)
  6589. (save-excursion
  6590. (if (org-region-active-p)
  6591. (org-map-region 'org-promote (region-beginning) (region-end))
  6592. (org-promote)))
  6593. (org-fix-position-after-promote))
  6594. (defun org-do-demote ()
  6595. "Demote the current heading lower down the tree.
  6596. If the region is active in `transient-mark-mode', demote all headings
  6597. in the region."
  6598. (interactive)
  6599. (save-excursion
  6600. (if (org-region-active-p)
  6601. (org-map-region 'org-demote (region-beginning) (region-end))
  6602. (org-demote)))
  6603. (org-fix-position-after-promote))
  6604. (defun org-fix-position-after-promote ()
  6605. "Make sure that after pro/demotion cursor position is right."
  6606. (let ((pos (point)))
  6607. (when (save-excursion
  6608. (beginning-of-line 1)
  6609. (looking-at org-todo-line-regexp)
  6610. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  6611. (cond ((eobp) (insert " "))
  6612. ((eolp) (insert " "))
  6613. ((equal (char-after) ?\ ) (forward-char 1))))))
  6614. (defun org-current-level ()
  6615. "Return the level of the current entry, or nil if before the first headline.
  6616. The level is the number of stars at the beginning of the headline."
  6617. (save-excursion
  6618. (org-with-limited-levels
  6619. (if (ignore-errors (org-back-to-heading t))
  6620. (funcall outline-level)))))
  6621. (defun org-get-previous-line-level ()
  6622. "Return the outline depth of the last headline before the current line.
  6623. Returns 0 for the first headline in the buffer, and nil if before the
  6624. first headline."
  6625. (let ((current-level (org-current-level))
  6626. (prev-level (when (> (line-number-at-pos) 1)
  6627. (save-excursion
  6628. (beginning-of-line 0)
  6629. (org-current-level)))))
  6630. (cond ((null current-level) nil) ; Before first headline
  6631. ((null prev-level) 0) ; At first headline
  6632. (prev-level))))
  6633. (defun org-reduced-level (l)
  6634. "Compute the effective level of a heading.
  6635. This takes into account the setting of `org-odd-levels-only'."
  6636. (cond
  6637. ((zerop l) 0)
  6638. (org-odd-levels-only (1+ (floor (/ l 2))))
  6639. (t l)))
  6640. (defun org-level-increment ()
  6641. "Return the number of stars that will be added or removed at a
  6642. time to headlines when structure editing, based on the value of
  6643. `org-odd-levels-only'."
  6644. (if org-odd-levels-only 2 1))
  6645. (defun org-get-valid-level (level &optional change)
  6646. "Rectify a level change under the influence of `org-odd-levels-only'
  6647. LEVEL is a current level, CHANGE is by how much the level should be
  6648. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  6649. even level numbers will become the next higher odd number."
  6650. (if org-odd-levels-only
  6651. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  6652. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  6653. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  6654. (max 1 (+ level (or change 0)))))
  6655. (if (boundp 'define-obsolete-function-alias)
  6656. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  6657. (define-obsolete-function-alias 'org-get-legal-level
  6658. 'org-get-valid-level)
  6659. (define-obsolete-function-alias 'org-get-legal-level
  6660. 'org-get-valid-level "23.1")))
  6661. (defun org-promote ()
  6662. "Promote the current heading higher up the tree.
  6663. If the region is active in `transient-mark-mode', promote all headings
  6664. in the region."
  6665. (org-back-to-heading t)
  6666. (let* ((level (save-match-data (funcall outline-level)))
  6667. (after-change-functions (remove 'flyspell-after-change-function
  6668. after-change-functions))
  6669. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  6670. (diff (abs (- level (length up-head) -1))))
  6671. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  6672. (replace-match up-head nil t)
  6673. ;; Fixup tag positioning
  6674. (and org-auto-align-tags (org-set-tags nil t))
  6675. (if org-adapt-indentation (org-fixup-indentation (- diff)))
  6676. (run-hooks 'org-after-promote-entry-hook)))
  6677. (defun org-demote ()
  6678. "Demote the current heading lower down the tree.
  6679. If the region is active in `transient-mark-mode', demote all headings
  6680. in the region."
  6681. (org-back-to-heading t)
  6682. (let* ((level (save-match-data (funcall outline-level)))
  6683. (after-change-functions (remove 'flyspell-after-change-function
  6684. after-change-functions))
  6685. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  6686. (diff (abs (- level (length down-head) -1))))
  6687. (replace-match down-head nil t)
  6688. ;; Fixup tag positioning
  6689. (and org-auto-align-tags (org-set-tags nil t))
  6690. (if org-adapt-indentation (org-fixup-indentation diff))
  6691. (run-hooks 'org-after-demote-entry-hook)))
  6692. (defun org-cycle-level ()
  6693. "Cycle the level of an empty headline through possible states.
  6694. This goes first to child, then to parent, level, then up the hierarchy.
  6695. After top level, it switches back to sibling level."
  6696. (interactive)
  6697. (let ((org-adapt-indentation nil))
  6698. (when (org-point-at-end-of-empty-headline)
  6699. (setq this-command 'org-cycle-level) ; Only needed for caching
  6700. (let ((cur-level (org-current-level))
  6701. (prev-level (org-get-previous-line-level)))
  6702. (cond
  6703. ;; If first headline in file, promote to top-level.
  6704. ((= prev-level 0)
  6705. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6706. do (org-do-promote)))
  6707. ;; If same level as prev, demote one.
  6708. ((= prev-level cur-level)
  6709. (org-do-demote))
  6710. ;; If parent is top-level, promote to top level if not already.
  6711. ((= prev-level 1)
  6712. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6713. do (org-do-promote)))
  6714. ;; If top-level, return to prev-level.
  6715. ((= cur-level 1)
  6716. (loop repeat (/ (- prev-level 1) (org-level-increment))
  6717. do (org-do-demote)))
  6718. ;; If less than prev-level, promote one.
  6719. ((< cur-level prev-level)
  6720. (org-do-promote))
  6721. ;; If deeper than prev-level, promote until higher than
  6722. ;; prev-level.
  6723. ((> cur-level prev-level)
  6724. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  6725. do (org-do-promote))))
  6726. t))))
  6727. (defun org-map-tree (fun)
  6728. "Call FUN for every heading underneath the current one."
  6729. (org-back-to-heading)
  6730. (let ((level (funcall outline-level)))
  6731. (save-excursion
  6732. (funcall fun)
  6733. (while (and (progn
  6734. (outline-next-heading)
  6735. (> (funcall outline-level) level))
  6736. (not (eobp)))
  6737. (funcall fun)))))
  6738. (defun org-map-region (fun beg end)
  6739. "Call FUN for every heading between BEG and END."
  6740. (let ((org-ignore-region t))
  6741. (save-excursion
  6742. (setq end (copy-marker end))
  6743. (goto-char beg)
  6744. (if (and (re-search-forward org-outline-regexp-bol nil t)
  6745. (< (point) end))
  6746. (funcall fun))
  6747. (while (and (progn
  6748. (outline-next-heading)
  6749. (< (point) end))
  6750. (not (eobp)))
  6751. (funcall fun)))))
  6752. (defvar org-property-end-re) ; silence byte-compiler
  6753. (defun org-fixup-indentation (diff)
  6754. "Change the indentation in the current entry by DIFF.
  6755. However, if any line in the current entry has no indentation, or if it
  6756. would end up with no indentation after the change, nothing at all is done."
  6757. (save-excursion
  6758. (let ((end (save-excursion (outline-next-heading)
  6759. (point-marker)))
  6760. (prohibit (if (> diff 0)
  6761. "^\\S-"
  6762. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  6763. col)
  6764. (unless (save-excursion (end-of-line 1)
  6765. (re-search-forward prohibit end t))
  6766. (while (and (< (point) end)
  6767. (re-search-forward "^[ \t]+" end t))
  6768. (goto-char (match-end 0))
  6769. (setq col (current-column))
  6770. (if (< diff 0) (replace-match ""))
  6771. (org-indent-to-column (+ diff col))))
  6772. (move-marker end nil))))
  6773. (defun org-convert-to-odd-levels ()
  6774. "Convert an org-mode file with all levels allowed to one with odd levels.
  6775. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  6776. level 5 etc."
  6777. (interactive)
  6778. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  6779. (let ((outline-level 'org-outline-level)
  6780. (org-odd-levels-only nil) n)
  6781. (save-excursion
  6782. (goto-char (point-min))
  6783. (while (re-search-forward "^\\*\\*+ " nil t)
  6784. (setq n (- (length (match-string 0)) 2))
  6785. (while (>= (setq n (1- n)) 0)
  6786. (org-demote))
  6787. (end-of-line 1))))))
  6788. (defun org-convert-to-oddeven-levels ()
  6789. "Convert an org-mode file with only odd levels to one with odd/even levels.
  6790. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  6791. file contains a section with an even level, conversion would
  6792. destroy the structure of the file. An error is signaled in this
  6793. case."
  6794. (interactive)
  6795. (goto-char (point-min))
  6796. ;; First check if there are no even levels
  6797. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  6798. (org-show-context t)
  6799. (error "Not all levels are odd in this file. Conversion not possible"))
  6800. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  6801. (let ((outline-regexp org-outline-regexp)
  6802. (outline-level 'org-outline-level)
  6803. (org-odd-levels-only nil) n)
  6804. (save-excursion
  6805. (goto-char (point-min))
  6806. (while (re-search-forward "^\\*\\*+ " nil t)
  6807. (setq n (/ (1- (length (match-string 0))) 2))
  6808. (while (>= (setq n (1- n)) 0)
  6809. (org-promote))
  6810. (end-of-line 1))))))
  6811. (defun org-tr-level (n)
  6812. "Make N odd if required."
  6813. (if org-odd-levels-only (1+ (/ n 2)) n))
  6814. ;;; Vertical tree motion, cutting and pasting of subtrees
  6815. (defun org-move-subtree-up (&optional arg)
  6816. "Move the current subtree up past ARG headlines of the same level."
  6817. (interactive "p")
  6818. (org-move-subtree-down (- (prefix-numeric-value arg))))
  6819. (defun org-move-subtree-down (&optional arg)
  6820. "Move the current subtree down past ARG headlines of the same level."
  6821. (interactive "p")
  6822. (setq arg (prefix-numeric-value arg))
  6823. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  6824. 'org-get-last-sibling))
  6825. (ins-point (make-marker))
  6826. (cnt (abs arg))
  6827. (col (current-column))
  6828. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  6829. ;; Select the tree
  6830. (org-back-to-heading)
  6831. (setq beg0 (point))
  6832. (save-excursion
  6833. (setq ne-beg (org-back-over-empty-lines))
  6834. (setq beg (point)))
  6835. (save-match-data
  6836. (save-excursion (outline-end-of-heading)
  6837. (setq folded (outline-invisible-p)))
  6838. (outline-end-of-subtree))
  6839. (outline-next-heading)
  6840. (setq ne-end (org-back-over-empty-lines))
  6841. (setq end (point))
  6842. (goto-char beg0)
  6843. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  6844. ;; include less whitespace
  6845. (save-excursion
  6846. (goto-char beg)
  6847. (forward-line (- ne-beg ne-end))
  6848. (setq beg (point))))
  6849. ;; Find insertion point, with error handling
  6850. (while (> cnt 0)
  6851. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  6852. (progn (goto-char beg0)
  6853. (error "Cannot move past superior level or buffer limit")))
  6854. (setq cnt (1- cnt)))
  6855. (if (> arg 0)
  6856. ;; Moving forward - still need to move over subtree
  6857. (progn (org-end-of-subtree t t)
  6858. (save-excursion
  6859. (org-back-over-empty-lines)
  6860. (or (bolp) (newline)))))
  6861. (setq ne-ins (org-back-over-empty-lines))
  6862. (move-marker ins-point (point))
  6863. (setq txt (buffer-substring beg end))
  6864. (org-save-markers-in-region beg end)
  6865. (delete-region beg end)
  6866. (org-remove-empty-overlays-at beg)
  6867. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  6868. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  6869. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  6870. (let ((bbb (point)))
  6871. (insert-before-markers txt)
  6872. (org-reinstall-markers-in-region bbb)
  6873. (move-marker ins-point bbb))
  6874. (or (bolp) (insert "\n"))
  6875. (setq ins-end (point))
  6876. (goto-char ins-point)
  6877. (org-skip-whitespace)
  6878. (when (and (< arg 0)
  6879. (org-first-sibling-p)
  6880. (> ne-ins ne-beg))
  6881. ;; Move whitespace back to beginning
  6882. (save-excursion
  6883. (goto-char ins-end)
  6884. (let ((kill-whole-line t))
  6885. (kill-line (- ne-ins ne-beg)) (point)))
  6886. (insert (make-string (- ne-ins ne-beg) ?\n)))
  6887. (move-marker ins-point nil)
  6888. (if folded
  6889. (hide-subtree)
  6890. (org-show-entry)
  6891. (show-children)
  6892. (org-cycle-hide-drawers 'children))
  6893. (org-clean-visibility-after-subtree-move)
  6894. ;; move back to the initial column we were at
  6895. (move-to-column col)))
  6896. (defvar org-subtree-clip ""
  6897. "Clipboard for cut and paste of subtrees.
  6898. This is actually only a copy of the kill, because we use the normal kill
  6899. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  6900. (defvar org-subtree-clip-folded nil
  6901. "Was the last copied subtree folded?
  6902. This is used to fold the tree back after pasting.")
  6903. (defun org-cut-subtree (&optional n)
  6904. "Cut the current subtree into the clipboard.
  6905. With prefix arg N, cut this many sequential subtrees.
  6906. This is a short-hand for marking the subtree and then cutting it."
  6907. (interactive "p")
  6908. (org-copy-subtree n 'cut))
  6909. (defun org-copy-subtree (&optional n cut force-store-markers)
  6910. "Cut the current subtree into the clipboard.
  6911. With prefix arg N, cut this many sequential subtrees.
  6912. This is a short-hand for marking the subtree and then copying it.
  6913. If CUT is non-nil, actually cut the subtree.
  6914. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  6915. of some markers in the region, even if CUT is non-nil. This is
  6916. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  6917. (interactive "p")
  6918. (let (beg end folded (beg0 (point)))
  6919. (if (org-called-interactively-p 'any)
  6920. (org-back-to-heading nil) ; take what looks like a subtree
  6921. (org-back-to-heading t)) ; take what is really there
  6922. (org-back-over-empty-lines)
  6923. (setq beg (point))
  6924. (skip-chars-forward " \t\r\n")
  6925. (save-match-data
  6926. (save-excursion (outline-end-of-heading)
  6927. (setq folded (outline-invisible-p)))
  6928. (condition-case nil
  6929. (org-forward-same-level (1- n) t)
  6930. (error nil))
  6931. (org-end-of-subtree t t))
  6932. (org-back-over-empty-lines)
  6933. (setq end (point))
  6934. (goto-char beg0)
  6935. (when (> end beg)
  6936. (setq org-subtree-clip-folded folded)
  6937. (when (or cut force-store-markers)
  6938. (org-save-markers-in-region beg end))
  6939. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  6940. (setq org-subtree-clip (current-kill 0))
  6941. (message "%s: Subtree(s) with %d characters"
  6942. (if cut "Cut" "Copied")
  6943. (length org-subtree-clip)))))
  6944. (defun org-paste-subtree (&optional level tree for-yank)
  6945. "Paste the clipboard as a subtree, with modification of headline level.
  6946. The entire subtree is promoted or demoted in order to match a new headline
  6947. level.
  6948. If the cursor is at the beginning of a headline, the same level as
  6949. that headline is used to paste the tree
  6950. If not, the new level is derived from the *visible* headings
  6951. before and after the insertion point, and taken to be the inferior headline
  6952. level of the two. So if the previous visible heading is level 3 and the
  6953. next is level 4 (or vice versa), level 4 will be used for insertion.
  6954. This makes sure that the subtree remains an independent subtree and does
  6955. not swallow low level entries.
  6956. You can also force a different level, either by using a numeric prefix
  6957. argument, or by inserting the heading marker by hand. For example, if the
  6958. cursor is after \"*****\", then the tree will be shifted to level 5.
  6959. If optional TREE is given, use this text instead of the kill ring.
  6960. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  6961. move back over whitespace before inserting, and move point to the end of
  6962. the inserted text when done."
  6963. (interactive "P")
  6964. (setq tree (or tree (and kill-ring (current-kill 0))))
  6965. (unless (org-kill-is-subtree-p tree)
  6966. (error "%s"
  6967. (substitute-command-keys
  6968. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  6969. (org-with-limited-levels
  6970. (let* ((visp (not (outline-invisible-p)))
  6971. (txt tree)
  6972. (^re_ "\\(\\*+\\)[ \t]*")
  6973. (old-level (if (string-match org-outline-regexp-bol txt)
  6974. (- (match-end 0) (match-beginning 0) 1)
  6975. -1))
  6976. (force-level (cond (level (prefix-numeric-value level))
  6977. ((and (looking-at "[ \t]*$")
  6978. (string-match
  6979. "^\\*+$" (buffer-substring
  6980. (point-at-bol) (point))))
  6981. (- (match-end 1) (match-beginning 1)))
  6982. ((and (bolp)
  6983. (looking-at org-outline-regexp))
  6984. (- (match-end 0) (point) 1))
  6985. (t nil)))
  6986. (previous-level (save-excursion
  6987. (condition-case nil
  6988. (progn
  6989. (outline-previous-visible-heading 1)
  6990. (if (looking-at ^re_)
  6991. (- (match-end 0) (match-beginning 0) 1)
  6992. 1))
  6993. (error 1))))
  6994. (next-level (save-excursion
  6995. (condition-case nil
  6996. (progn
  6997. (or (looking-at org-outline-regexp)
  6998. (outline-next-visible-heading 1))
  6999. (if (looking-at ^re_)
  7000. (- (match-end 0) (match-beginning 0) 1)
  7001. 1))
  7002. (error 1))))
  7003. (new-level (or force-level (max previous-level next-level)))
  7004. (shift (if (or (= old-level -1)
  7005. (= new-level -1)
  7006. (= old-level new-level))
  7007. 0
  7008. (- new-level old-level)))
  7009. (delta (if (> shift 0) -1 1))
  7010. (func (if (> shift 0) 'org-demote 'org-promote))
  7011. (org-odd-levels-only nil)
  7012. beg end newend)
  7013. ;; Remove the forced level indicator
  7014. (if force-level
  7015. (delete-region (point-at-bol) (point)))
  7016. ;; Paste
  7017. (beginning-of-line (if (bolp) 1 2))
  7018. (unless for-yank (org-back-over-empty-lines))
  7019. (setq beg (point))
  7020. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  7021. (insert-before-markers txt)
  7022. (unless (string-match "\n\\'" txt) (insert "\n"))
  7023. (setq newend (point))
  7024. (org-reinstall-markers-in-region beg)
  7025. (setq end (point))
  7026. (goto-char beg)
  7027. (skip-chars-forward " \t\n\r")
  7028. (setq beg (point))
  7029. (if (and (outline-invisible-p) visp)
  7030. (save-excursion (outline-show-heading)))
  7031. ;; Shift if necessary
  7032. (unless (= shift 0)
  7033. (save-restriction
  7034. (narrow-to-region beg end)
  7035. (while (not (= shift 0))
  7036. (org-map-region func (point-min) (point-max))
  7037. (setq shift (+ delta shift)))
  7038. (goto-char (point-min))
  7039. (setq newend (point-max))))
  7040. (when (or (org-called-interactively-p 'interactive) for-yank)
  7041. (message "Clipboard pasted as level %d subtree" new-level))
  7042. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  7043. kill-ring
  7044. (eq org-subtree-clip (current-kill 0))
  7045. org-subtree-clip-folded)
  7046. ;; The tree was folded before it was killed/copied
  7047. (hide-subtree))
  7048. (and for-yank (goto-char newend)))))
  7049. (defun org-kill-is-subtree-p (&optional txt)
  7050. "Check if the current kill is an outline subtree, or a set of trees.
  7051. Returns nil if kill does not start with a headline, or if the first
  7052. headline level is not the largest headline level in the tree.
  7053. So this will actually accept several entries of equal levels as well,
  7054. which is OK for `org-paste-subtree'.
  7055. If optional TXT is given, check this string instead of the current kill."
  7056. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  7057. (re (org-get-limited-outline-regexp))
  7058. (^re (concat "^" re))
  7059. (start-level (and kill
  7060. (string-match
  7061. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7062. kill)
  7063. (- (match-end 2) (match-beginning 2) 1)))
  7064. (start (1+ (or (match-beginning 2) -1))))
  7065. (if (not start-level)
  7066. (progn
  7067. nil) ;; does not even start with a heading
  7068. (catch 'exit
  7069. (while (setq start (string-match ^re kill (1+ start)))
  7070. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7071. (throw 'exit nil)))
  7072. t))))
  7073. (defvar org-markers-to-move nil
  7074. "Markers that should be moved with a cut-and-paste operation.
  7075. Those markers are stored together with their positions relative to
  7076. the start of the region.")
  7077. (defun org-save-markers-in-region (beg end)
  7078. "Check markers in region.
  7079. If these markers are between BEG and END, record their position relative
  7080. to BEG, so that after moving the block of text, we can put the markers back
  7081. into place.
  7082. This function gets called just before an entry or tree gets cut from the
  7083. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7084. called immediately, to move the markers with the entries."
  7085. (setq org-markers-to-move nil)
  7086. (when (featurep 'org-clock)
  7087. (org-clock-save-markers-for-cut-and-paste beg end))
  7088. (when (featurep 'org-agenda)
  7089. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7090. (defun org-check-and-save-marker (marker beg end)
  7091. "Check if MARKER is between BEG and END.
  7092. If yes, remember the marker and the distance to BEG."
  7093. (when (and (marker-buffer marker)
  7094. (equal (marker-buffer marker) (current-buffer)))
  7095. (if (and (>= marker beg) (< marker end))
  7096. (push (cons marker (- marker beg)) org-markers-to-move))))
  7097. (defun org-reinstall-markers-in-region (beg)
  7098. "Move all remembered markers to their position relative to BEG."
  7099. (mapc (lambda (x)
  7100. (move-marker (car x) (+ beg (cdr x))))
  7101. org-markers-to-move)
  7102. (setq org-markers-to-move nil))
  7103. (defun org-narrow-to-subtree ()
  7104. "Narrow buffer to the current subtree."
  7105. (interactive)
  7106. (save-excursion
  7107. (save-match-data
  7108. (org-with-limited-levels
  7109. (narrow-to-region
  7110. (progn (org-back-to-heading t) (point))
  7111. (progn (org-end-of-subtree t t)
  7112. (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7113. (point)))))))
  7114. (defun org-narrow-to-block ()
  7115. "Narrow buffer to the current block."
  7116. (interactive)
  7117. (let* ((case-fold-search t)
  7118. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7119. "^[ \t]*#\\+end_.*")))
  7120. (if blockp
  7121. (narrow-to-region (car blockp) (cdr blockp))
  7122. (error "Not in a block"))))
  7123. (eval-when-compile
  7124. (defvar org-property-drawer-re))
  7125. (defvar org-property-start-re) ;; defined below
  7126. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7127. "Clone the task (subtree) at point N times.
  7128. The clones will be inserted as siblings.
  7129. In interactive use, the user will be prompted for the number of
  7130. clones to be produced, and for a time SHIFT, which may be a
  7131. repeater as used in time stamps, for example `+3d'.
  7132. When a valid repeater is given and the entry contains any time
  7133. stamps, the clones will become a sequence in time, with time
  7134. stamps in the subtree shifted for each clone produced. If SHIFT
  7135. is nil or the empty string, time stamps will be left alone. The
  7136. ID property of the original subtree is removed.
  7137. If the original subtree did contain time stamps with a repeater,
  7138. the following will happen:
  7139. - the repeater will be removed in each clone
  7140. - an additional clone will be produced, with the current, unshifted
  7141. date(s) in the entry.
  7142. - the original entry will be placed *after* all the clones, with
  7143. repeater intact.
  7144. - the start days in the repeater in the original entry will be shifted
  7145. to past the last clone.
  7146. In this way you can spell out a number of instances of a repeating task,
  7147. and still retain the repeater to cover future instances of the task."
  7148. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  7149. (let (beg end template task idprop
  7150. shift-n shift-what doshift nmin nmax (n-no-remove -1)
  7151. (drawer-re org-drawer-regexp))
  7152. (if (not (and (integerp n) (> n 0)))
  7153. (error "Invalid number of replications %s" n))
  7154. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7155. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([dwmy]\\)[ \t]*\\'"
  7156. shift)))
  7157. (error "Invalid shift specification %s" shift))
  7158. (when doshift
  7159. (setq shift-n (string-to-number (match-string 1 shift))
  7160. shift-what (cdr (assoc (match-string 2 shift)
  7161. '(("d" . day) ("w" . week)
  7162. ("m" . month) ("y" . year))))))
  7163. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7164. (setq nmin 1 nmax n)
  7165. (org-back-to-heading t)
  7166. (setq beg (point))
  7167. (setq idprop (org-entry-get nil "ID"))
  7168. (org-end-of-subtree t t)
  7169. (or (bolp) (insert "\n"))
  7170. (setq end (point))
  7171. (setq template (buffer-substring beg end))
  7172. (when (and doshift
  7173. (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[dwmy][^<>\n]*>" template))
  7174. (delete-region beg end)
  7175. (setq end beg)
  7176. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7177. (goto-char end)
  7178. (loop for n from nmin to nmax do
  7179. ;; prepare clone
  7180. (with-temp-buffer
  7181. (insert template)
  7182. (org-mode)
  7183. (goto-char (point-min))
  7184. (org-show-subtree)
  7185. (and idprop (if org-clone-delete-id
  7186. (org-entry-delete nil "ID")
  7187. (org-id-get-create t)))
  7188. (unless (= n 0)
  7189. (while (re-search-forward "^[ \t]*CLOCK:.*$" nil t)
  7190. (kill-whole-line))
  7191. (goto-char (point-min))
  7192. (while (re-search-forward drawer-re nil t)
  7193. (mapc (lambda (d)
  7194. (org-remove-empty-drawer-at d (point))) org-drawers)))
  7195. (goto-char (point-min))
  7196. (when doshift
  7197. (while (re-search-forward org-ts-regexp-both nil t)
  7198. (org-timestamp-change (* n shift-n) shift-what))
  7199. (unless (= n n-no-remove)
  7200. (goto-char (point-min))
  7201. (while (re-search-forward org-ts-regexp nil t)
  7202. (save-excursion
  7203. (goto-char (match-beginning 0))
  7204. (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[dwmy]\\)")
  7205. (delete-region (match-beginning 1) (match-end 1)))))))
  7206. (setq task (buffer-string)))
  7207. (insert task))
  7208. (goto-char beg)))
  7209. ;;; Outline Sorting
  7210. (defun org-sort (with-case)
  7211. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7212. Optional argument WITH-CASE means sort case-sensitively."
  7213. (interactive "P")
  7214. (cond
  7215. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7216. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7217. (t
  7218. (org-call-with-arg 'org-sort-entries with-case))))
  7219. (defun org-sort-remove-invisible (s)
  7220. (remove-text-properties 0 (length s) org-rm-props s)
  7221. (while (string-match org-bracket-link-regexp s)
  7222. (setq s (replace-match (if (match-end 2)
  7223. (match-string 3 s)
  7224. (match-string 1 s)) t t s)))
  7225. s)
  7226. (defvar org-priority-regexp) ; defined later in the file
  7227. (defvar org-after-sorting-entries-or-items-hook nil
  7228. "Hook that is run after a bunch of entries or items have been sorted.
  7229. When children are sorted, the cursor is in the parent line when this
  7230. hook gets called. When a region or a plain list is sorted, the cursor
  7231. will be in the first entry of the sorted region/list.")
  7232. (defun org-sort-entries
  7233. (&optional with-case sorting-type getkey-func compare-func property)
  7234. "Sort entries on a certain level of an outline tree.
  7235. If there is an active region, the entries in the region are sorted.
  7236. Else, if the cursor is before the first entry, sort the top-level items.
  7237. Else, the children of the entry at point are sorted.
  7238. Sorting can be alphabetically, numerically, by date/time as given by
  7239. a time stamp, by a property or by priority.
  7240. The command prompts for the sorting type unless it has been given to the
  7241. function through the SORTING-TYPE argument, which needs to be a character,
  7242. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?r ?R ?f ?F). Here is the
  7243. precise meaning of each character:
  7244. n Numerically, by converting the beginning of the entry/item to a number.
  7245. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7246. t By date/time, either the first active time stamp in the entry, or, if
  7247. none exist, by the first inactive one.
  7248. s By the scheduled date/time.
  7249. d By deadline date/time.
  7250. c By creation time, which is assumed to be the first inactive time stamp
  7251. at the beginning of a line.
  7252. p By priority according to the cookie.
  7253. r By the value of a property.
  7254. Capital letters will reverse the sort order.
  7255. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7256. called with point at the beginning of the record. It must return either
  7257. a string or a number that should serve as the sorting key for that record.
  7258. Comparing entries ignores case by default. However, with an optional argument
  7259. WITH-CASE, the sorting considers case as well."
  7260. (interactive "P")
  7261. (let ((case-func (if with-case 'identity 'downcase))
  7262. start beg end stars re re2
  7263. txt what tmp)
  7264. ;; Find beginning and end of region to sort
  7265. (cond
  7266. ((org-region-active-p)
  7267. ;; we will sort the region
  7268. (setq end (region-end)
  7269. what "region")
  7270. (goto-char (region-beginning))
  7271. (if (not (org-at-heading-p)) (outline-next-heading))
  7272. (setq start (point)))
  7273. ((or (org-at-heading-p)
  7274. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  7275. ;; we will sort the children of the current headline
  7276. (org-back-to-heading)
  7277. (setq start (point)
  7278. end (progn (org-end-of-subtree t t)
  7279. (or (bolp) (insert "\n"))
  7280. (org-back-over-empty-lines)
  7281. (point))
  7282. what "children")
  7283. (goto-char start)
  7284. (show-subtree)
  7285. (outline-next-heading))
  7286. (t
  7287. ;; we will sort the top-level entries in this file
  7288. (goto-char (point-min))
  7289. (or (org-at-heading-p) (outline-next-heading))
  7290. (setq start (point))
  7291. (goto-char (point-max))
  7292. (beginning-of-line 1)
  7293. (when (looking-at ".*?\\S-")
  7294. ;; File ends in a non-white line
  7295. (end-of-line 1)
  7296. (insert "\n"))
  7297. (setq end (point-max))
  7298. (setq what "top-level")
  7299. (goto-char start)
  7300. (show-all)))
  7301. (setq beg (point))
  7302. (if (>= beg end) (error "Nothing to sort"))
  7303. (looking-at "\\(\\*+\\)")
  7304. (setq stars (match-string 1)
  7305. re (concat "^" (regexp-quote stars) " +")
  7306. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  7307. txt (buffer-substring beg end))
  7308. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  7309. (if (and (not (equal stars "*")) (string-match re2 txt))
  7310. (error "Region to sort contains a level above the first entry"))
  7311. (unless sorting-type
  7312. (message
  7313. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  7314. [t]ime [s]cheduled [d]eadline [c]reated
  7315. A/N/T/S/D/C/P/O/F means reversed:"
  7316. what)
  7317. (setq sorting-type (read-char-exclusive))
  7318. (and (= (downcase sorting-type) ?f)
  7319. (setq getkey-func
  7320. (org-icompleting-read "Sort using function: "
  7321. obarray 'fboundp t nil nil))
  7322. (setq getkey-func (intern getkey-func)))
  7323. (and (= (downcase sorting-type) ?r)
  7324. (setq property
  7325. (org-icompleting-read "Property: "
  7326. (mapcar 'list (org-buffer-property-keys t))
  7327. nil t))))
  7328. (message "Sorting entries...")
  7329. (save-restriction
  7330. (narrow-to-region start end)
  7331. (let ((dcst (downcase sorting-type))
  7332. (case-fold-search nil)
  7333. (now (current-time)))
  7334. (sort-subr
  7335. (/= dcst sorting-type)
  7336. ;; This function moves to the beginning character of the "record" to
  7337. ;; be sorted.
  7338. (lambda nil
  7339. (if (re-search-forward re nil t)
  7340. (goto-char (match-beginning 0))
  7341. (goto-char (point-max))))
  7342. ;; This function moves to the last character of the "record" being
  7343. ;; sorted.
  7344. (lambda nil
  7345. (save-match-data
  7346. (condition-case nil
  7347. (outline-forward-same-level 1)
  7348. (error
  7349. (goto-char (point-max))))))
  7350. ;; This function returns the value that gets sorted against.
  7351. (lambda nil
  7352. (cond
  7353. ((= dcst ?n)
  7354. (if (looking-at org-complex-heading-regexp)
  7355. (string-to-number (match-string 4))
  7356. nil))
  7357. ((= dcst ?a)
  7358. (if (looking-at org-complex-heading-regexp)
  7359. (funcall case-func (match-string 4))
  7360. nil))
  7361. ((= dcst ?t)
  7362. (let ((end (save-excursion (outline-next-heading) (point))))
  7363. (if (or (re-search-forward org-ts-regexp end t)
  7364. (re-search-forward org-ts-regexp-both end t))
  7365. (org-time-string-to-seconds (match-string 0))
  7366. (org-float-time now))))
  7367. ((= dcst ?c)
  7368. (let ((end (save-excursion (outline-next-heading) (point))))
  7369. (if (re-search-forward
  7370. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  7371. end t)
  7372. (org-time-string-to-seconds (match-string 0))
  7373. (org-float-time now))))
  7374. ((= dcst ?s)
  7375. (let ((end (save-excursion (outline-next-heading) (point))))
  7376. (if (re-search-forward org-scheduled-time-regexp end t)
  7377. (org-time-string-to-seconds (match-string 1))
  7378. (org-float-time now))))
  7379. ((= dcst ?d)
  7380. (let ((end (save-excursion (outline-next-heading) (point))))
  7381. (if (re-search-forward org-deadline-time-regexp end t)
  7382. (org-time-string-to-seconds (match-string 1))
  7383. (org-float-time now))))
  7384. ((= dcst ?p)
  7385. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  7386. (string-to-char (match-string 2))
  7387. org-default-priority))
  7388. ((= dcst ?r)
  7389. (or (org-entry-get nil property) ""))
  7390. ((= dcst ?o)
  7391. (if (looking-at org-complex-heading-regexp)
  7392. (- 9999 (length (member (match-string 2)
  7393. org-todo-keywords-1)))))
  7394. ((= dcst ?f)
  7395. (if getkey-func
  7396. (progn
  7397. (setq tmp (funcall getkey-func))
  7398. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  7399. tmp)
  7400. (error "Invalid key function `%s'" getkey-func)))
  7401. (t (error "Invalid sorting type `%c'" sorting-type))))
  7402. nil
  7403. (cond
  7404. ((= dcst ?a) 'string<)
  7405. ((= dcst ?f) compare-func)
  7406. ((member dcst '(?p ?t ?s ?d ?c)) '<)
  7407. (t nil)))))
  7408. (run-hooks 'org-after-sorting-entries-or-items-hook)
  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. (defun orgstruct++-mode (&optional arg)
  7502. "Toggle `orgstruct-mode', the enhanced version of it.
  7503. In addition to setting orgstruct-mode, this also exports all indentation
  7504. and autofilling variables from org-mode into the buffer. It will also
  7505. recognize item context in multiline items.
  7506. Note that turning off orgstruct-mode will *not* remove the
  7507. indentation/paragraph settings. This can only be done by refreshing the
  7508. major mode, for example with \\[normal-mode]."
  7509. (interactive "P")
  7510. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  7511. (if (< arg 1)
  7512. (orgstruct-mode -1)
  7513. (orgstruct-mode 1)
  7514. (let (var val)
  7515. (mapc
  7516. (lambda (x)
  7517. (when (string-match
  7518. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  7519. (symbol-name (car x)))
  7520. (setq var (car x) val (nth 1 x))
  7521. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  7522. org-local-vars)
  7523. (org-set-local 'orgstruct-is-++ t))))
  7524. (defvar orgstruct-is-++ nil
  7525. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  7526. (make-variable-buffer-local 'orgstruct-is-++)
  7527. ;;;###autoload
  7528. (defun turn-on-orgstruct++ ()
  7529. "Unconditionally turn on `orgstruct++-mode'."
  7530. (orgstruct++-mode 1))
  7531. (defun orgstruct-error ()
  7532. "Error when there is no default binding for a structure key."
  7533. (interactive)
  7534. (error "This key has no function outside structure elements"))
  7535. (defun orgstruct-setup ()
  7536. "Setup orgstruct keymaps."
  7537. (let ((nfunc 0)
  7538. (bindings
  7539. (list
  7540. '([(meta up)] org-metaup)
  7541. '([(meta down)] org-metadown)
  7542. '([(meta left)] org-metaleft)
  7543. '([(meta right)] org-metaright)
  7544. '([(meta shift up)] org-shiftmetaup)
  7545. '([(meta shift down)] org-shiftmetadown)
  7546. '([(meta shift left)] org-shiftmetaleft)
  7547. '([(meta shift right)] org-shiftmetaright)
  7548. '([?\e (up)] org-metaup)
  7549. '([?\e (down)] org-metadown)
  7550. '([?\e (left)] org-metaleft)
  7551. '([?\e (right)] org-metaright)
  7552. '([?\e (shift up)] org-shiftmetaup)
  7553. '([?\e (shift down)] org-shiftmetadown)
  7554. '([?\e (shift left)] org-shiftmetaleft)
  7555. '([?\e (shift right)] org-shiftmetaright)
  7556. '([(shift up)] org-shiftup)
  7557. '([(shift down)] org-shiftdown)
  7558. '([(shift left)] org-shiftleft)
  7559. '([(shift right)] org-shiftright)
  7560. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  7561. '("\M-q" fill-paragraph)
  7562. '("\C-c^" org-sort)
  7563. '("\C-c-" org-cycle-list-bullet)))
  7564. elt key fun cmd)
  7565. (while (setq elt (pop bindings))
  7566. (setq nfunc (1+ nfunc))
  7567. (setq key (org-key (car elt))
  7568. fun (nth 1 elt)
  7569. cmd (orgstruct-make-binding fun nfunc key))
  7570. (org-defkey orgstruct-mode-map key cmd))
  7571. ;; Special treatment needed for TAB and RET
  7572. (org-defkey orgstruct-mode-map [(tab)]
  7573. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  7574. (org-defkey orgstruct-mode-map "\C-i"
  7575. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  7576. (org-defkey orgstruct-mode-map "\M-\C-m"
  7577. (orgstruct-make-binding 'org-insert-heading 105
  7578. "\M-\C-m" [(meta return)]))
  7579. (org-defkey orgstruct-mode-map [(meta return)]
  7580. (orgstruct-make-binding 'org-insert-heading 106
  7581. [(meta return)] "\M-\C-m"))
  7582. (org-defkey orgstruct-mode-map [(shift meta return)]
  7583. (orgstruct-make-binding 'org-insert-todo-heading 107
  7584. [(meta return)] "\M-\C-m"))
  7585. (org-defkey orgstruct-mode-map "\e\C-m"
  7586. (orgstruct-make-binding 'org-insert-heading 108
  7587. "\e\C-m" [?\e (return)]))
  7588. (org-defkey orgstruct-mode-map [?\e (return)]
  7589. (orgstruct-make-binding 'org-insert-heading 109
  7590. [?\e (return)] "\e\C-m"))
  7591. (org-defkey orgstruct-mode-map [?\e (shift return)]
  7592. (orgstruct-make-binding 'org-insert-todo-heading 110
  7593. [?\e (return)] "\e\C-m"))
  7594. (unless org-local-vars
  7595. (setq org-local-vars (org-get-local-variables)))
  7596. t))
  7597. (defun orgstruct-make-binding (fun n &rest keys)
  7598. "Create a function for binding in the structure minor mode.
  7599. FUN is the command to call inside a table. N is used to create a unique
  7600. command name. KEYS are keys that should be checked in for a command
  7601. to execute outside of tables."
  7602. (eval
  7603. (list 'defun
  7604. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  7605. '(arg)
  7606. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  7607. "Outside of structure, run the binding of `"
  7608. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  7609. "'.")
  7610. '(interactive "p")
  7611. (list 'if
  7612. `(org-context-p 'headline 'item
  7613. (and orgstruct-is-++
  7614. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  7615. 'item-body))
  7616. (list 'org-run-like-in-org-mode (list 'quote fun))
  7617. (list 'let '(orgstruct-mode)
  7618. (list 'call-interactively
  7619. (append '(or)
  7620. (mapcar (lambda (k)
  7621. (list 'key-binding k))
  7622. keys)
  7623. '('orgstruct-error))))))))
  7624. (defun org-context-p (&rest contexts)
  7625. "Check if local context is any of CONTEXTS.
  7626. Possible values in the list of contexts are `table', `headline', and `item'."
  7627. (let ((pos (point)))
  7628. (goto-char (point-at-bol))
  7629. (prog1 (or (and (memq 'table contexts)
  7630. (looking-at "[ \t]*|"))
  7631. (and (memq 'headline contexts)
  7632. (looking-at org-outline-regexp))
  7633. (and (memq 'item contexts)
  7634. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  7635. (and (memq 'item-body contexts)
  7636. (org-in-item-p)))
  7637. (goto-char pos))))
  7638. (defun org-get-local-variables ()
  7639. "Return a list of all local variables in an org-mode buffer."
  7640. (let (varlist)
  7641. (with-current-buffer (get-buffer-create "*Org tmp*")
  7642. (erase-buffer)
  7643. (org-mode)
  7644. (setq varlist (buffer-local-variables)))
  7645. (kill-buffer "*Org tmp*")
  7646. (delq nil
  7647. (mapcar
  7648. (lambda (x)
  7649. (setq x
  7650. (if (symbolp x)
  7651. (list x)
  7652. (list (car x) (list 'quote (cdr x)))))
  7653. (if (string-match
  7654. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  7655. (symbol-name (car x)))
  7656. x nil))
  7657. varlist))))
  7658. (defun org-clone-local-variables (from-buffer &optional regexp)
  7659. "Clone local variables from FROM-BUFFER.
  7660. Optional argument REGEXP selects variables to clone."
  7661. (mapc
  7662. (lambda (pair)
  7663. (and (symbolp (car pair))
  7664. (or (null regexp)
  7665. (string-match regexp (symbol-name (car pair))))
  7666. (set (make-local-variable (car pair))
  7667. (cdr pair))))
  7668. (buffer-local-variables from-buffer)))
  7669. ;;;###autoload
  7670. (defun org-run-like-in-org-mode (cmd)
  7671. "Run a command, pretending that the current buffer is in Org-mode.
  7672. This will temporarily bind local variables that are typically bound in
  7673. Org-mode to the values they have in Org-mode, and then interactively
  7674. call CMD."
  7675. (org-load-modules-maybe)
  7676. (unless org-local-vars
  7677. (setq org-local-vars (org-get-local-variables)))
  7678. (eval (list 'let org-local-vars
  7679. (list 'call-interactively (list 'quote cmd)))))
  7680. ;;;; Archiving
  7681. (defun org-get-category (&optional pos force-refresh)
  7682. "Get the category applying to position POS."
  7683. (save-match-data
  7684. (if force-refresh (org-refresh-category-properties))
  7685. (let ((pos (or pos (point))))
  7686. (or (get-text-property pos 'org-category)
  7687. (progn (org-refresh-category-properties)
  7688. (get-text-property pos 'org-category))))))
  7689. (defun org-refresh-category-properties ()
  7690. "Refresh category text properties in the buffer."
  7691. (let ((def-cat (cond
  7692. ((null org-category)
  7693. (if buffer-file-name
  7694. (file-name-sans-extension
  7695. (file-name-nondirectory buffer-file-name))
  7696. "???"))
  7697. ((symbolp org-category) (symbol-name org-category))
  7698. (t org-category)))
  7699. beg end cat pos optionp)
  7700. (org-unmodified
  7701. (save-excursion
  7702. (save-restriction
  7703. (widen)
  7704. (goto-char (point-min))
  7705. (put-text-property (point) (point-max) 'org-category def-cat)
  7706. (while (re-search-forward
  7707. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  7708. (setq pos (match-end 0)
  7709. optionp (equal (char-after (match-beginning 0)) ?#)
  7710. cat (org-trim (match-string 2)))
  7711. (if optionp
  7712. (setq beg (point-at-bol) end (point-max))
  7713. (org-back-to-heading t)
  7714. (setq beg (point) end (org-end-of-subtree t t)))
  7715. (put-text-property beg end 'org-category cat)
  7716. (put-text-property beg end 'org-category-position beg)
  7717. (goto-char pos)))))))
  7718. ;;;; Link Stuff
  7719. ;;; Link abbreviations
  7720. (defun org-link-expand-abbrev (link)
  7721. "Apply replacements as defined in `org-link-abbrev-alist."
  7722. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  7723. (let* ((key (match-string 1 link))
  7724. (as (or (assoc key org-link-abbrev-alist-local)
  7725. (assoc key org-link-abbrev-alist)))
  7726. (tag (and (match-end 2) (match-string 3 link)))
  7727. rpl)
  7728. (if (not as)
  7729. link
  7730. (setq rpl (cdr as))
  7731. (cond
  7732. ((symbolp rpl) (funcall rpl tag))
  7733. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  7734. ((string-match "%h" rpl)
  7735. (replace-match (url-hexify-string (or tag "")) t t rpl))
  7736. (t (concat rpl tag)))))
  7737. link))
  7738. ;;; Storing and inserting links
  7739. (defvar org-insert-link-history nil
  7740. "Minibuffer history for links inserted with `org-insert-link'.")
  7741. (defvar org-stored-links nil
  7742. "Contains the links stored with `org-store-link'.")
  7743. (defvar org-store-link-plist nil
  7744. "Plist with info about the most recently link created with `org-store-link'.")
  7745. (defvar org-link-protocols nil
  7746. "Link protocols added to Org-mode using `org-add-link-type'.")
  7747. (defvar org-store-link-functions nil
  7748. "List of functions that are called to create and store a link.
  7749. Each function will be called in turn until one returns a non-nil
  7750. value. Each function should check if it is responsible for creating
  7751. this link (for example by looking at the major mode).
  7752. If not, it must exit and return nil.
  7753. If yes, it should return a non-nil value after a calling
  7754. `org-store-link-props' with a list of properties and values.
  7755. Special properties are:
  7756. :type The link prefix, like \"http\". This must be given.
  7757. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  7758. This is obligatory as well.
  7759. :description Optional default description for the second pair
  7760. of brackets in an Org-mode link. The user can still change
  7761. this when inserting this link into an Org-mode buffer.
  7762. In addition to these, any additional properties can be specified
  7763. and then used in remember templates.")
  7764. (defun org-add-link-type (type &optional follow export)
  7765. "Add TYPE to the list of `org-link-types'.
  7766. Re-compute all regular expressions depending on `org-link-types'
  7767. FOLLOW and EXPORT are two functions.
  7768. FOLLOW should take the link path as the single argument and do whatever
  7769. is necessary to follow the link, for example find a file or display
  7770. a mail message.
  7771. EXPORT should format the link path for export to one of the export formats.
  7772. It should be a function accepting three arguments:
  7773. path the path of the link, the text after the prefix (like \"http:\")
  7774. desc the description of the link, if any, or a description added by
  7775. org-export-normalize-links if there is none
  7776. format the export format, a symbol like `html' or `latex' or `ascii'..
  7777. The function may use the FORMAT information to return different values
  7778. depending on the format. The return value will be put literally into
  7779. the exported file. If the return value is nil, this means Org should
  7780. do what it normally does with links which do not have EXPORT defined.
  7781. Org-mode has a built-in default for exporting links. If you are happy with
  7782. this default, there is no need to define an export function for the link
  7783. type. For a simple example of an export function, see `org-bbdb.el'."
  7784. (add-to-list 'org-link-types type t)
  7785. (org-make-link-regexps)
  7786. (if (assoc type org-link-protocols)
  7787. (setcdr (assoc type org-link-protocols) (list follow export))
  7788. (push (list type follow export) org-link-protocols)))
  7789. (defvar org-agenda-buffer-name)
  7790. ;;;###autoload
  7791. (defun org-store-link (arg)
  7792. "\\<org-mode-map>Store an org-link to the current location.
  7793. This link is added to `org-stored-links' and can later be inserted
  7794. into an org-buffer with \\[org-insert-link].
  7795. For some link types, a prefix arg is interpreted:
  7796. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  7797. For file links, arg negates `org-context-in-file-links'."
  7798. (interactive "P")
  7799. (org-load-modules-maybe)
  7800. (setq org-store-link-plist nil) ; reset
  7801. (org-with-limited-levels
  7802. (let (link cpltxt desc description search txt custom-id agenda-link)
  7803. (cond
  7804. ((run-hook-with-args-until-success 'org-store-link-functions)
  7805. (setq link (plist-get org-store-link-plist :link)
  7806. desc (or (plist-get org-store-link-plist :description) link)))
  7807. ((org-src-edit-buffer-p)
  7808. (let (label gc)
  7809. (while (or (not label)
  7810. (save-excursion
  7811. (save-restriction
  7812. (widen)
  7813. (goto-char (point-min))
  7814. (re-search-forward
  7815. (regexp-quote (format org-coderef-label-format label))
  7816. nil t))))
  7817. (when label (message "Label exists already") (sit-for 2))
  7818. (setq label (read-string "Code line label: " label)))
  7819. (end-of-line 1)
  7820. (setq link (format org-coderef-label-format label))
  7821. (setq gc (- 79 (length link)))
  7822. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  7823. (insert link)
  7824. (setq link (concat "(" label ")") desc nil)))
  7825. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  7826. ;; We are in the agenda, link to referenced location
  7827. (let ((m (or (get-text-property (point) 'org-hd-marker)
  7828. (get-text-property (point) 'org-marker))))
  7829. (when m
  7830. (org-with-point-at m
  7831. (setq agenda-link
  7832. (if (org-called-interactively-p 'any)
  7833. (call-interactively 'org-store-link)
  7834. (org-store-link nil)))))))
  7835. ((eq major-mode 'calendar-mode)
  7836. (let ((cd (calendar-cursor-to-date)))
  7837. (setq link
  7838. (format-time-string
  7839. (car org-time-stamp-formats)
  7840. (apply 'encode-time
  7841. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  7842. nil nil nil))))
  7843. (org-store-link-props :type "calendar" :date cd)))
  7844. ((eq major-mode 'w3-mode)
  7845. (setq cpltxt (if (and (buffer-name)
  7846. (not (string-match "Untitled" (buffer-name))))
  7847. (buffer-name)
  7848. (url-view-url t))
  7849. link (org-make-link (url-view-url t)))
  7850. (org-store-link-props :type "w3" :url (url-view-url t)))
  7851. ((eq major-mode 'w3m-mode)
  7852. (setq cpltxt (or w3m-current-title w3m-current-url)
  7853. link (org-make-link w3m-current-url))
  7854. (org-store-link-props :type "w3m" :url (url-view-url t)))
  7855. ((setq search (run-hook-with-args-until-success
  7856. 'org-create-file-search-functions))
  7857. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  7858. "::" search))
  7859. (setq cpltxt (or description link)))
  7860. ((eq major-mode 'image-mode)
  7861. (setq cpltxt (concat "file:"
  7862. (abbreviate-file-name buffer-file-name))
  7863. link (org-make-link cpltxt))
  7864. (org-store-link-props :type "image" :file buffer-file-name))
  7865. ((eq major-mode 'dired-mode)
  7866. ;; link to the file in the current line
  7867. (let ((file (dired-get-filename nil t)))
  7868. (setq file (if file
  7869. (abbreviate-file-name
  7870. (expand-file-name (dired-get-filename nil t)))
  7871. ;; otherwise, no file so use current directory.
  7872. default-directory))
  7873. (setq cpltxt (concat "file:" file)
  7874. link (org-make-link cpltxt))))
  7875. ((and (buffer-file-name (buffer-base-buffer)) (eq major-mode 'org-mode))
  7876. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  7877. (cond
  7878. ((org-in-regexp "<<\\(.*?\\)>>")
  7879. (setq cpltxt
  7880. (concat "file:"
  7881. (abbreviate-file-name
  7882. (buffer-file-name (buffer-base-buffer)))
  7883. "::" (match-string 1))
  7884. link (org-make-link cpltxt)))
  7885. ((and (featurep 'org-id)
  7886. (or (eq org-link-to-org-use-id t)
  7887. (and (eq org-link-to-org-use-id 'create-if-interactive)
  7888. (org-called-interactively-p 'any))
  7889. (and (eq org-link-to-org-use-id
  7890. 'create-if-interactive-and-no-custom-id)
  7891. (org-called-interactively-p 'any)
  7892. (not custom-id))
  7893. (and org-link-to-org-use-id
  7894. (org-entry-get nil "ID"))))
  7895. ;; We can make a link using the ID.
  7896. (setq link (condition-case nil
  7897. (prog1 (org-id-store-link)
  7898. (setq desc (plist-get org-store-link-plist
  7899. :description)))
  7900. (error
  7901. ;; probably before first headline, link to file only
  7902. (concat "file:"
  7903. (abbreviate-file-name
  7904. (buffer-file-name (buffer-base-buffer))))))))
  7905. (t
  7906. ;; Just link to current headline
  7907. (setq cpltxt (concat "file:"
  7908. (abbreviate-file-name
  7909. (buffer-file-name (buffer-base-buffer)))))
  7910. ;; Add a context search string
  7911. (when (org-xor org-context-in-file-links arg)
  7912. (setq txt (cond
  7913. ((org-at-heading-p) nil)
  7914. ((org-region-active-p)
  7915. (buffer-substring (region-beginning) (region-end)))
  7916. (t nil)))
  7917. (when (or (null txt) (string-match "\\S-" txt))
  7918. (setq cpltxt
  7919. (concat cpltxt "::"
  7920. (condition-case nil
  7921. (org-make-org-heading-search-string txt)
  7922. (error "")))
  7923. desc (or (nth 4 (ignore-errors
  7924. (org-heading-components))) "NONE"))))
  7925. (if (string-match "::\\'" cpltxt)
  7926. (setq cpltxt (substring cpltxt 0 -2)))
  7927. (setq link (org-make-link cpltxt)))))
  7928. ((buffer-file-name (buffer-base-buffer))
  7929. ;; Just link to this file here.
  7930. (setq cpltxt (concat "file:"
  7931. (abbreviate-file-name
  7932. (buffer-file-name (buffer-base-buffer)))))
  7933. ;; Add a context string
  7934. (when (org-xor org-context-in-file-links arg)
  7935. (setq txt (if (org-region-active-p)
  7936. (buffer-substring (region-beginning) (region-end))
  7937. (buffer-substring (point-at-bol) (point-at-eol))))
  7938. ;; Only use search option if there is some text.
  7939. (when (string-match "\\S-" txt)
  7940. (setq cpltxt
  7941. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  7942. desc "NONE")))
  7943. (setq link (org-make-link cpltxt)))
  7944. ((org-called-interactively-p 'interactive)
  7945. (error "Cannot link to a buffer which is not visiting a file"))
  7946. (t (setq link nil)))
  7947. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  7948. (setq link (or link cpltxt)
  7949. desc (or desc cpltxt))
  7950. (if (equal desc "NONE") (setq desc nil))
  7951. (if (and (or (org-called-interactively-p 'any) executing-kbd-macro) link)
  7952. (progn
  7953. (setq org-stored-links
  7954. (cons (list link desc) org-stored-links))
  7955. (message "Stored: %s" (or desc link))
  7956. (when custom-id
  7957. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  7958. "::#" custom-id))
  7959. (setq org-stored-links
  7960. (cons (list link desc) org-stored-links))))
  7961. (or agenda-link (and link (org-make-link-string link desc)))))))
  7962. (defun org-store-link-props (&rest plist)
  7963. "Store link properties, extract names and addresses."
  7964. (let (x adr)
  7965. (when (setq x (plist-get plist :from))
  7966. (setq adr (mail-extract-address-components x))
  7967. (setq plist (plist-put plist :fromname (car adr)))
  7968. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  7969. (when (setq x (plist-get plist :to))
  7970. (setq adr (mail-extract-address-components x))
  7971. (setq plist (plist-put plist :toname (car adr)))
  7972. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  7973. (let ((from (plist-get plist :from))
  7974. (to (plist-get plist :to)))
  7975. (when (and from to org-from-is-user-regexp)
  7976. (setq plist
  7977. (plist-put plist :fromto
  7978. (if (string-match org-from-is-user-regexp from)
  7979. (concat "to %t")
  7980. (concat "from %f"))))))
  7981. (setq org-store-link-plist plist))
  7982. (defun org-add-link-props (&rest plist)
  7983. "Add these properties to the link property list."
  7984. (let (key value)
  7985. (while plist
  7986. (setq key (pop plist) value (pop plist))
  7987. (setq org-store-link-plist
  7988. (plist-put org-store-link-plist key value)))))
  7989. (defun org-email-link-description (&optional fmt)
  7990. "Return the description part of an email link.
  7991. This takes information from `org-store-link-plist' and formats it
  7992. according to FMT (default from `org-email-link-description-format')."
  7993. (setq fmt (or fmt org-email-link-description-format))
  7994. (let* ((p org-store-link-plist)
  7995. (to (plist-get p :toaddress))
  7996. (from (plist-get p :fromaddress))
  7997. (table
  7998. (list
  7999. (cons "%c" (plist-get p :fromto))
  8000. (cons "%F" (plist-get p :from))
  8001. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  8002. (cons "%T" (plist-get p :to))
  8003. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  8004. (cons "%s" (plist-get p :subject))
  8005. (cons "%d" (plist-get p :date))
  8006. (cons "%m" (plist-get p :message-id)))))
  8007. (when (string-match "%c" fmt)
  8008. ;; Check if the user wrote this message
  8009. (if (and org-from-is-user-regexp from to
  8010. (save-match-data (string-match org-from-is-user-regexp from)))
  8011. (setq fmt (replace-match "to %t" t t fmt))
  8012. (setq fmt (replace-match "from %f" t t fmt))))
  8013. (org-replace-escapes fmt table)))
  8014. (defun org-make-org-heading-search-string (&optional string heading)
  8015. "Make search string for STRING or current headline."
  8016. (interactive)
  8017. (let ((s (or string (org-get-heading)))
  8018. (lines org-context-in-file-links))
  8019. (unless (and string (not heading))
  8020. ;; We are using a headline, clean up garbage in there.
  8021. (if (string-match org-todo-regexp s)
  8022. (setq s (replace-match "" t t s)))
  8023. (if (string-match (org-re ":[[:alnum:]_@#%:]+:[ \t]*$") s)
  8024. (setq s (replace-match "" t t s)))
  8025. (setq s (org-trim s))
  8026. (if (string-match (concat "^\\(" org-quote-string "\\|"
  8027. org-comment-string "\\)") s)
  8028. (setq s (replace-match "" t t s)))
  8029. (while (string-match org-ts-regexp s)
  8030. (setq s (replace-match "" t t s))))
  8031. (or string (setq s (concat "*" s))) ; Add * for headlines
  8032. (when (and string (integerp lines) (> lines 0))
  8033. (let ((slines (org-split-string s "\n")))
  8034. (when (< lines (length slines))
  8035. (setq s (mapconcat
  8036. 'identity
  8037. (reverse (nthcdr (- (length slines) lines)
  8038. (reverse slines))) "\n")))))
  8039. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  8040. (defun org-make-link (&rest strings)
  8041. "Concatenate STRINGS."
  8042. (apply 'concat strings))
  8043. (defun org-make-link-string (link &optional description)
  8044. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8045. (unless (string-match "\\S-" link)
  8046. (error "Empty link"))
  8047. (when (and description
  8048. (stringp description)
  8049. (not (string-match "\\S-" description)))
  8050. (setq description nil))
  8051. (when (stringp description)
  8052. ;; Remove brackets from the description, they are fatal.
  8053. (while (string-match "\\[" description)
  8054. (setq description (replace-match "{" t t description)))
  8055. (while (string-match "\\]" description)
  8056. (setq description (replace-match "}" t t description))))
  8057. (when (equal link description)
  8058. ;; No description needed, it is identical
  8059. (setq description nil))
  8060. (when (and (not description)
  8061. (not (string-match (org-image-file-name-regexp) link))
  8062. (not (equal link (org-link-escape link))))
  8063. (setq description (org-extract-attributes link)))
  8064. (setq link
  8065. (cond ((string-match (org-image-file-name-regexp) link) link)
  8066. ((string-match org-link-types-re link)
  8067. (concat (match-string 1 link)
  8068. (org-link-escape (substring link (match-end 1)))))
  8069. (t (org-link-escape link))))
  8070. (concat "[[" link "]"
  8071. (if description (concat "[" description "]") "")
  8072. "]"))
  8073. (defconst org-link-escape-chars
  8074. '(?\ ?\[ ?\] ?\; ?\= ?\+)
  8075. "List of characters that should be escaped in link.
  8076. This is the list that is used for internal purposes.")
  8077. (defvar org-url-encoding-use-url-hexify nil)
  8078. (defconst org-link-escape-chars-browser
  8079. '(?\ )
  8080. "List of escapes for characters that are problematic in links.
  8081. This is the list that is used before handing over to the browser.")
  8082. (defun org-link-escape (text &optional table merge)
  8083. "Return percent escaped representation of TEXT.
  8084. TEXT is a string with the text to escape.
  8085. Optional argument TABLE is a list with characters that should be
  8086. escaped. When nil, `org-link-escape-chars' is used.
  8087. If optional argument MERGE is set, merge TABLE into
  8088. `org-link-escape-chars'."
  8089. (if (and org-url-encoding-use-url-hexify (not table))
  8090. (url-hexify-string text)
  8091. (cond
  8092. ((and table merge)
  8093. (mapc (lambda (defchr)
  8094. (unless (member defchr table)
  8095. (setq table (cons defchr table)))) org-link-escape-chars))
  8096. ((null table)
  8097. (setq table org-link-escape-chars)))
  8098. (mapconcat
  8099. (lambda (char)
  8100. (if (or (member char table)
  8101. (< char 32) (= char 37) (> char 126))
  8102. (mapconcat (lambda (sequence-element)
  8103. (format "%%%.2X" sequence-element))
  8104. (or (encode-coding-char char 'utf-8)
  8105. (error "Unable to percent escape character: %s"
  8106. (char-to-string char))) "")
  8107. (char-to-string char))) text "")))
  8108. (defun org-link-unescape (str)
  8109. "Unhex hexified Unicode strings as returned from the JavaScript function
  8110. encodeURIComponent. E.g. `%C3%B6' is the german Umlaut `ö'."
  8111. (unless (and (null str) (string= "" str))
  8112. (let ((pos 0) (case-fold-search t) unhexed)
  8113. (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
  8114. (setq unhexed (org-link-unescape-compound (match-string 0 str)))
  8115. (setq str (replace-match unhexed t t str))
  8116. (setq pos (+ pos (length unhexed))))))
  8117. str)
  8118. (defun org-link-unescape-compound (hex)
  8119. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German Umlaut `ö'.
  8120. Note: this function also decodes single byte encodings like
  8121. `%E1' (\"á\") if not followed by another `%[A-F0-9]{2}' group."
  8122. (save-match-data
  8123. (let* ((bytes (cdr (split-string hex "%")))
  8124. (ret "")
  8125. (eat 0)
  8126. (sum 0))
  8127. (while bytes
  8128. (let* ((val (string-to-number (pop bytes) 16))
  8129. (shift-xor
  8130. (if (= 0 eat)
  8131. (cond
  8132. ((>= val 252) (cons 6 252))
  8133. ((>= val 248) (cons 5 248))
  8134. ((>= val 240) (cons 4 240))
  8135. ((>= val 224) (cons 3 224))
  8136. ((>= val 192) (cons 2 192))
  8137. (t (cons 0 0)))
  8138. (cons 6 128))))
  8139. (if (>= val 192) (setq eat (car shift-xor)))
  8140. (setq val (logxor val (cdr shift-xor)))
  8141. (setq sum (+ (lsh sum (car shift-xor)) val))
  8142. (if (> eat 0) (setq eat (- eat 1)))
  8143. (cond
  8144. ((= 0 eat) ;multi byte
  8145. (setq ret (concat ret (org-char-to-string sum)))
  8146. (setq sum 0))
  8147. ((not bytes) ; single byte(s)
  8148. (setq ret (org-link-unescape-single-byte-sequence hex))))
  8149. )) ;; end (while bytes
  8150. ret )))
  8151. (defun org-link-unescape-single-byte-sequence (hex)
  8152. "Unhexify hex-encoded single byte character sequences."
  8153. (mapconcat (lambda (byte)
  8154. (char-to-string (string-to-number byte 16)))
  8155. (cdr (split-string hex "%")) ""))
  8156. (defun org-xor (a b)
  8157. "Exclusive or."
  8158. (if a (not b) b))
  8159. (defun org-fixup-message-id-for-http (s)
  8160. "Replace special characters in a message id, so it can be used in an http query."
  8161. (when (string-match "%" s)
  8162. (setq s (mapconcat (lambda (c)
  8163. (if (eq c ?%)
  8164. "%25"
  8165. (char-to-string c)))
  8166. s "")))
  8167. (while (string-match "<" s)
  8168. (setq s (replace-match "%3C" t t s)))
  8169. (while (string-match ">" s)
  8170. (setq s (replace-match "%3E" t t s)))
  8171. (while (string-match "@" s)
  8172. (setq s (replace-match "%40" t t s)))
  8173. s)
  8174. ;;;###autoload
  8175. (defun org-insert-link-global ()
  8176. "Insert a link like Org-mode does.
  8177. This command can be called in any mode to insert a link in Org-mode syntax."
  8178. (interactive)
  8179. (org-load-modules-maybe)
  8180. (org-run-like-in-org-mode 'org-insert-link))
  8181. (defun org-insert-link (&optional complete-file link-location default-description)
  8182. "Insert a link. At the prompt, enter the link.
  8183. Completion can be used to insert any of the link protocol prefixes like
  8184. http or ftp in use.
  8185. The history can be used to select a link previously stored with
  8186. `org-store-link'. When the empty string is entered (i.e. if you just
  8187. press RET at the prompt), the link defaults to the most recently
  8188. stored link. As SPC triggers completion in the minibuffer, you need to
  8189. use M-SPC or C-q SPC to force the insertion of a space character.
  8190. You will also be prompted for a description, and if one is given, it will
  8191. be displayed in the buffer instead of the link.
  8192. If there is already a link at point, this command will allow you to edit link
  8193. and description parts.
  8194. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  8195. be selected using completion. The path to the file will be relative to the
  8196. current directory if the file is in the current directory or a subdirectory.
  8197. Otherwise, the link will be the absolute path as completed in the minibuffer
  8198. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  8199. option `org-link-file-path-type'.
  8200. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  8201. the current directory or below.
  8202. With three \\[universal-argument] prefixes, negate the meaning of
  8203. `org-keep-stored-link-after-insertion'.
  8204. If `org-make-link-description-function' is non-nil, this function will be
  8205. called with the link target, and the result will be the default
  8206. link description.
  8207. If the LINK-LOCATION parameter is non-nil, this value will be
  8208. used as the link location instead of reading one interactively.
  8209. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  8210. be used as the default description."
  8211. (interactive "P")
  8212. (let* ((wcf (current-window-configuration))
  8213. (region (if (org-region-active-p)
  8214. (buffer-substring (region-beginning) (region-end))))
  8215. (remove (and region (list (region-beginning) (region-end))))
  8216. (desc region)
  8217. tmphist ; byte-compile incorrectly complains about this
  8218. (link link-location)
  8219. entry file all-prefixes)
  8220. (cond
  8221. (link-location) ; specified by arg, just use it.
  8222. ((org-in-regexp org-bracket-link-regexp 1)
  8223. ;; We do have a link at point, and we are going to edit it.
  8224. (setq remove (list (match-beginning 0) (match-end 0)))
  8225. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  8226. (setq link (read-string "Link: "
  8227. (org-link-unescape
  8228. (org-match-string-no-properties 1)))))
  8229. ((or (org-in-regexp org-angle-link-re)
  8230. (org-in-regexp org-plain-link-re))
  8231. ;; Convert to bracket link
  8232. (setq remove (list (match-beginning 0) (match-end 0))
  8233. link (read-string "Link: "
  8234. (org-remove-angle-brackets (match-string 0)))))
  8235. ((member complete-file '((4) (16)))
  8236. ;; Completing read for file names.
  8237. (setq link (org-file-complete-link complete-file)))
  8238. (t
  8239. ;; Read link, with completion for stored links.
  8240. (with-output-to-temp-buffer "*Org Links*"
  8241. (princ "Insert a link.
  8242. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  8243. (when org-stored-links
  8244. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  8245. (princ (mapconcat
  8246. (lambda (x)
  8247. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  8248. (reverse org-stored-links) "\n"))))
  8249. (let ((cw (selected-window)))
  8250. (select-window (get-buffer-window "*Org Links*" 'visible))
  8251. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  8252. (unless (pos-visible-in-window-p (point-max))
  8253. (org-fit-window-to-buffer))
  8254. (and (window-live-p cw) (select-window cw)))
  8255. ;; Fake a link history, containing the stored links.
  8256. (setq tmphist (append (mapcar 'car org-stored-links)
  8257. org-insert-link-history))
  8258. (setq all-prefixes (append (mapcar 'car org-link-abbrev-alist-local)
  8259. (mapcar 'car org-link-abbrev-alist)
  8260. org-link-types))
  8261. (unwind-protect
  8262. (progn
  8263. (setq link
  8264. (let ((org-completion-use-ido nil)
  8265. (org-completion-use-iswitchb nil))
  8266. (org-completing-read
  8267. "Link: "
  8268. (append
  8269. (mapcar (lambda (x) (list (concat x ":")))
  8270. all-prefixes)
  8271. (mapcar 'car org-stored-links))
  8272. nil nil nil
  8273. 'tmphist
  8274. (car (car org-stored-links)))))
  8275. (if (not (string-match "\\S-" link))
  8276. (error "No link selected"))
  8277. (if (or (member link all-prefixes)
  8278. (and (equal ":" (substring link -1))
  8279. (member (substring link 0 -1) all-prefixes)
  8280. (setq link (substring link 0 -1))))
  8281. (setq link (org-link-try-special-completion link))))
  8282. (set-window-configuration wcf)
  8283. (kill-buffer "*Org Links*"))
  8284. (setq entry (assoc link org-stored-links))
  8285. (or entry (push link org-insert-link-history))
  8286. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  8287. (not org-keep-stored-link-after-insertion))
  8288. (setq org-stored-links (delq (assoc link org-stored-links)
  8289. org-stored-links)))
  8290. (setq desc (or desc (nth 1 entry)))))
  8291. (if (string-match org-plain-link-re link)
  8292. ;; URL-like link, normalize the use of angular brackets.
  8293. (setq link (org-make-link (org-remove-angle-brackets link))))
  8294. ;; Check if we are linking to the current file with a search option
  8295. ;; If yes, simplify the link by using only the search option.
  8296. (when (and buffer-file-name
  8297. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  8298. (let* ((path (match-string 1 link))
  8299. (case-fold-search nil)
  8300. (search (match-string 2 link)))
  8301. (save-match-data
  8302. (if (equal (file-truename buffer-file-name) (file-truename path))
  8303. ;; We are linking to this same file, with a search option
  8304. (setq link search)))))
  8305. ;; Check if we can/should use a relative path. If yes, simplify the link
  8306. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  8307. (let* ((type (match-string 1 link))
  8308. (path (match-string 2 link))
  8309. (origpath path)
  8310. (case-fold-search nil))
  8311. (cond
  8312. ((or (eq org-link-file-path-type 'absolute)
  8313. (equal complete-file '(16)))
  8314. (setq path (abbreviate-file-name (expand-file-name path))))
  8315. ((eq org-link-file-path-type 'noabbrev)
  8316. (setq path (expand-file-name path)))
  8317. ((eq org-link-file-path-type 'relative)
  8318. (setq path (file-relative-name path)))
  8319. (t
  8320. (save-match-data
  8321. (if (string-match (concat "^" (regexp-quote
  8322. (expand-file-name
  8323. (file-name-as-directory
  8324. default-directory))))
  8325. (expand-file-name path))
  8326. ;; We are linking a file with relative path name.
  8327. (setq path (substring (expand-file-name path)
  8328. (match-end 0)))
  8329. (setq path (abbreviate-file-name (expand-file-name path)))))))
  8330. (setq link (concat type path))
  8331. (if (equal desc origpath)
  8332. (setq desc path))))
  8333. (if org-make-link-description-function
  8334. (setq desc (funcall org-make-link-description-function link desc))
  8335. (if default-description (setq desc default-description)))
  8336. (setq desc (read-string "Description: " desc))
  8337. (unless (string-match "\\S-" desc) (setq desc nil))
  8338. (if remove (apply 'delete-region remove))
  8339. (insert (org-make-link-string link desc))))
  8340. (defun org-link-try-special-completion (type)
  8341. "If there is completion support for link type TYPE, offer it."
  8342. (let ((fun (intern (concat "org-" type "-complete-link"))))
  8343. (if (functionp fun)
  8344. (funcall fun)
  8345. (read-string "Link (no completion support): " (concat type ":")))))
  8346. (defun org-file-complete-link (&optional arg)
  8347. "Create a file link using completion."
  8348. (let (file link)
  8349. (setq file (read-file-name "File: "))
  8350. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  8351. (pwd1 (file-name-as-directory (abbreviate-file-name
  8352. (expand-file-name ".")))))
  8353. (cond
  8354. ((equal arg '(16))
  8355. (setq link (org-make-link
  8356. "file:"
  8357. (abbreviate-file-name (expand-file-name file)))))
  8358. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  8359. (setq link (org-make-link "file:" (match-string 1 file))))
  8360. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  8361. (expand-file-name file))
  8362. (setq link (org-make-link
  8363. "file:" (match-string 1 (expand-file-name file)))))
  8364. (t (setq link (org-make-link "file:" file)))))
  8365. link))
  8366. (defun org-completing-read (&rest args)
  8367. "Completing-read with SPACE being a normal character."
  8368. (let ((enable-recursive-minibuffers t)
  8369. (minibuffer-local-completion-map
  8370. (copy-keymap minibuffer-local-completion-map)))
  8371. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  8372. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  8373. (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
  8374. (apply 'org-icompleting-read args)))
  8375. (defun org-completing-read-no-i (&rest args)
  8376. (let (org-completion-use-ido org-completion-use-iswitchb)
  8377. (apply 'org-completing-read args)))
  8378. (defun org-iswitchb-completing-read (prompt choices &rest args)
  8379. "Use iswitch as a completing-read replacement to choose from choices.
  8380. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  8381. from."
  8382. (let* ((iswitchb-use-virtual-buffers nil)
  8383. (iswitchb-make-buflist-hook
  8384. (lambda ()
  8385. (setq iswitchb-temp-buflist choices))))
  8386. (iswitchb-read-buffer prompt)))
  8387. (defun org-icompleting-read (&rest args)
  8388. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  8389. (org-without-partial-completion
  8390. (if (and org-completion-use-ido
  8391. (fboundp 'ido-completing-read)
  8392. (boundp 'ido-mode) ido-mode
  8393. (listp (second args)))
  8394. (let ((ido-enter-matching-directory nil))
  8395. (apply 'ido-completing-read (concat (car args))
  8396. (if (consp (car (nth 1 args)))
  8397. (mapcar 'car (nth 1 args))
  8398. (nth 1 args))
  8399. (cddr args)))
  8400. (if (and org-completion-use-iswitchb
  8401. (boundp 'iswitchb-mode) iswitchb-mode
  8402. (listp (second args)))
  8403. (apply 'org-iswitchb-completing-read (concat (car args))
  8404. (if (consp (car (nth 1 args)))
  8405. (mapcar 'car (nth 1 args))
  8406. (nth 1 args))
  8407. (cddr args))
  8408. (apply 'completing-read args)))))
  8409. (defun org-extract-attributes (s)
  8410. "Extract the attributes cookie from a string and set as text property."
  8411. (let (a attr (start 0) key value)
  8412. (save-match-data
  8413. (when (string-match "{{\\([^}]+\\)}}$" s)
  8414. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  8415. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  8416. (setq key (match-string 1 a) value (match-string 2 a)
  8417. start (match-end 0)
  8418. attr (plist-put attr (intern key) value))))
  8419. (org-add-props s nil 'org-attr attr))
  8420. s))
  8421. (defun org-extract-attributes-from-string (tag)
  8422. (let (key value attr)
  8423. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  8424. (setq key (match-string 1 tag) value (match-string 2 tag)
  8425. tag (replace-match "" t t tag)
  8426. attr (plist-put attr (intern key) value)))
  8427. (cons tag attr)))
  8428. (defun org-attributes-to-string (plist)
  8429. "Format a property list into an HTML attribute list."
  8430. (let ((s "") key value)
  8431. (while plist
  8432. (setq key (pop plist) value (pop plist))
  8433. (and value
  8434. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  8435. s))
  8436. ;;; Opening/following a link
  8437. (defvar org-link-search-failed nil)
  8438. (defvar org-open-link-functions nil
  8439. "Hook for functions finding a plain text link.
  8440. These functions must take a single argument, the link content.
  8441. They will be called for links that look like [[link text][description]]
  8442. when LINK TEXT does not have a protocol like \"http:\" and does not look
  8443. like a filename (e.g. \"./blue.png\").
  8444. These functions will be called *before* Org attempts to resolve the
  8445. link by doing text searches in the current buffer - so if you want a
  8446. link \"[[target]]\" to still find \"<<target>>\", your function should
  8447. handle this as a special case.
  8448. When the function does handle the link, it must return a non-nil value.
  8449. If it decides that it is not responsible for this link, it must return
  8450. nil to indicate that that Org-mode can continue with other options
  8451. like exact and fuzzy text search.")
  8452. (defun org-next-link ()
  8453. "Move forward to the next link.
  8454. If the link is in hidden text, expose it."
  8455. (interactive)
  8456. (when (and org-link-search-failed (eq this-command last-command))
  8457. (goto-char (point-min))
  8458. (message "Link search wrapped back to beginning of buffer"))
  8459. (setq org-link-search-failed nil)
  8460. (let* ((pos (point))
  8461. (ct (org-context))
  8462. (a (assoc :link ct)))
  8463. (if a (goto-char (nth 2 a)))
  8464. (if (re-search-forward org-any-link-re nil t)
  8465. (progn
  8466. (goto-char (match-beginning 0))
  8467. (if (outline-invisible-p) (org-show-context)))
  8468. (goto-char pos)
  8469. (setq org-link-search-failed t)
  8470. (error "No further link found"))))
  8471. (defun org-previous-link ()
  8472. "Move backward to the previous link.
  8473. If the link is in hidden text, expose it."
  8474. (interactive)
  8475. (when (and org-link-search-failed (eq this-command last-command))
  8476. (goto-char (point-max))
  8477. (message "Link search wrapped back to end of buffer"))
  8478. (setq org-link-search-failed nil)
  8479. (let* ((pos (point))
  8480. (ct (org-context))
  8481. (a (assoc :link ct)))
  8482. (if a (goto-char (nth 1 a)))
  8483. (if (re-search-backward org-any-link-re nil t)
  8484. (progn
  8485. (goto-char (match-beginning 0))
  8486. (if (outline-invisible-p) (org-show-context)))
  8487. (goto-char pos)
  8488. (setq org-link-search-failed t)
  8489. (error "No further link found"))))
  8490. (defun org-translate-link (s)
  8491. "Translate a link string if a translation function has been defined."
  8492. (if (and org-link-translation-function
  8493. (fboundp org-link-translation-function)
  8494. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  8495. (progn
  8496. (setq s (funcall org-link-translation-function
  8497. (match-string 1) (match-string 2)))
  8498. (concat (car s) ":" (cdr s)))
  8499. s))
  8500. (defun org-translate-link-from-planner (type path)
  8501. "Translate a link from Emacs Planner syntax so that Org can follow it.
  8502. This is still an experimental function, your mileage may vary."
  8503. (cond
  8504. ((member type '("http" "https" "news" "ftp"))
  8505. ;; standard Internet links are the same.
  8506. nil)
  8507. ((and (equal type "irc") (string-match "^//" path))
  8508. ;; Planner has two / at the beginning of an irc link, we have 1.
  8509. ;; We should have zero, actually....
  8510. (setq path (substring path 1)))
  8511. ((and (equal type "lisp") (string-match "^/" path))
  8512. ;; Planner has a slash, we do not.
  8513. (setq type "elisp" path (substring path 1)))
  8514. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  8515. ;; A typical message link. Planner has the id after the final slash,
  8516. ;; we separate it with a hash mark
  8517. (setq path (concat (match-string 1 path) "#"
  8518. (org-remove-angle-brackets (match-string 2 path)))))
  8519. )
  8520. (cons type path))
  8521. (defun org-find-file-at-mouse (ev)
  8522. "Open file link or URL at mouse."
  8523. (interactive "e")
  8524. (mouse-set-point ev)
  8525. (org-open-at-point 'in-emacs))
  8526. (defun org-open-at-mouse (ev)
  8527. "Open file link or URL at mouse.
  8528. See the docstring of `org-open-file' for details."
  8529. (interactive "e")
  8530. (mouse-set-point ev)
  8531. (if (eq major-mode 'org-agenda-mode)
  8532. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  8533. (org-open-at-point))
  8534. (defvar org-window-config-before-follow-link nil
  8535. "The window configuration before following a link.
  8536. This is saved in case the need arises to restore it.")
  8537. (defvar org-open-link-marker (make-marker)
  8538. "Marker pointing to the location where `org-open-at-point; was called.")
  8539. ;;;###autoload
  8540. (defun org-open-at-point-global ()
  8541. "Follow a link like Org-mode does.
  8542. This command can be called in any mode to follow a link that has
  8543. Org-mode syntax."
  8544. (interactive)
  8545. (org-run-like-in-org-mode 'org-open-at-point))
  8546. ;;;###autoload
  8547. (defun org-open-link-from-string (s &optional arg reference-buffer)
  8548. "Open a link in the string S, as if it was in Org-mode."
  8549. (interactive "sLink: \nP")
  8550. (let ((reference-buffer (or reference-buffer (current-buffer))))
  8551. (with-temp-buffer
  8552. (let ((org-inhibit-startup (not reference-buffer)))
  8553. (org-mode)
  8554. (insert s)
  8555. (goto-char (point-min))
  8556. (when reference-buffer
  8557. (setq org-link-abbrev-alist-local
  8558. (with-current-buffer reference-buffer
  8559. org-link-abbrev-alist-local)))
  8560. (org-open-at-point arg reference-buffer)))))
  8561. (defvar org-open-at-point-functions nil
  8562. "Hook that is run when following a link at point.
  8563. Functions in this hook must return t if they identify and follow
  8564. a link at point. If they don't find anything interesting at point,
  8565. they must return nil.")
  8566. (defun org-open-at-point (&optional arg reference-buffer)
  8567. "Open link at or after point.
  8568. If there is no link at point, this function will search forward up to
  8569. the end of the current line.
  8570. Normally, files will be opened by an appropriate application. If the
  8571. optional prefix argument ARG is non-nil, Emacs will visit the file.
  8572. With a double prefix argument, try to open outside of Emacs, in the
  8573. application the system uses for this file type."
  8574. (interactive "P")
  8575. ;; if in a code block, then open the block's results
  8576. (unless (call-interactively #'org-babel-open-src-block-result)
  8577. (org-load-modules-maybe)
  8578. (move-marker org-open-link-marker (point))
  8579. (setq org-window-config-before-follow-link (current-window-configuration))
  8580. (org-remove-occur-highlights nil nil t)
  8581. (cond
  8582. ((and (org-at-heading-p)
  8583. (not (org-in-regexp
  8584. (concat org-plain-link-re "\\|"
  8585. org-bracket-link-regexp "\\|"
  8586. org-angle-link-re "\\|"
  8587. "[ \t]:[^ \t\n]+:[ \t]*$")))
  8588. (not (get-text-property (point) 'org-linked-text)))
  8589. (or (org-offer-links-in-entry arg)
  8590. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  8591. ((run-hook-with-args-until-success 'org-open-at-point-functions))
  8592. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  8593. ((and (or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  8594. (not (org-in-regexp org-bracket-link-regexp)))
  8595. (org-footnote-action))
  8596. (t
  8597. (let (type path link line search (pos (point)))
  8598. (catch 'match
  8599. (save-excursion
  8600. (skip-chars-forward "^]\n\r")
  8601. (when (org-in-regexp org-bracket-link-regexp 1)
  8602. (setq link (org-extract-attributes
  8603. (org-link-unescape (org-match-string-no-properties 1))))
  8604. (while (string-match " *\n *" link)
  8605. (setq link (replace-match " " t t link)))
  8606. (setq link (org-link-expand-abbrev link))
  8607. (cond
  8608. ((or (file-name-absolute-p link)
  8609. (string-match "^\\.\\.?/" link))
  8610. (setq type "file" path link))
  8611. ((string-match org-link-re-with-space3 link)
  8612. (setq type (match-string 1 link) path (match-string 2 link)))
  8613. (t (setq type "thisfile" path link)))
  8614. (throw 'match t)))
  8615. (when (get-text-property (point) 'org-linked-text)
  8616. (setq type "thisfile"
  8617. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  8618. (1+ (point)) (point))
  8619. path (buffer-substring
  8620. (or (previous-single-property-change pos 'org-linked-text)
  8621. (point-min))
  8622. (or (next-single-property-change pos 'org-linked-text)
  8623. (point-max))))
  8624. (throw 'match t))
  8625. (save-excursion
  8626. (when (or (org-in-regexp org-angle-link-re)
  8627. (org-in-regexp org-plain-link-re))
  8628. (setq type (match-string 1)
  8629. path (org-link-unescape (match-string 2)))
  8630. (throw 'match t)))
  8631. (save-excursion
  8632. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
  8633. (setq type "tags"
  8634. path (match-string 1))
  8635. (while (string-match ":" path)
  8636. (setq path (replace-match "+" t t path)))
  8637. (throw 'match t)))
  8638. (when (org-in-regexp "<\\([^><\n]+\\)>")
  8639. (setq type "tree-match"
  8640. path (match-string 1))
  8641. (throw 'match t)))
  8642. (unless path
  8643. (error "No link found"))
  8644. ;; switch back to reference buffer
  8645. ;; needed when if called in a temporary buffer through
  8646. ;; org-open-link-from-string
  8647. (with-current-buffer (or reference-buffer (current-buffer))
  8648. ;; Remove any trailing spaces in path
  8649. (if (string-match " +\\'" path)
  8650. (setq path (replace-match "" t t path)))
  8651. (if (and org-link-translation-function
  8652. (fboundp org-link-translation-function))
  8653. ;; Check if we need to translate the link
  8654. (let ((tmp (funcall org-link-translation-function type path)))
  8655. (setq type (car tmp) path (cdr tmp))))
  8656. (cond
  8657. ((assoc type org-link-protocols)
  8658. (funcall (nth 1 (assoc type org-link-protocols)) path))
  8659. ((equal type "mailto")
  8660. (let ((cmd (car org-link-mailto-program))
  8661. (args (cdr org-link-mailto-program)) args1
  8662. (address path) (subject "") a)
  8663. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  8664. (setq address (match-string 1 path)
  8665. subject (org-link-escape (match-string 2 path))))
  8666. (while args
  8667. (cond
  8668. ((not (stringp (car args))) (push (pop args) args1))
  8669. (t (setq a (pop args))
  8670. (if (string-match "%a" a)
  8671. (setq a (replace-match address t t a)))
  8672. (if (string-match "%s" a)
  8673. (setq a (replace-match subject t t a)))
  8674. (push a args1))))
  8675. (apply cmd (nreverse args1))))
  8676. ((member type '("http" "https" "ftp" "news"))
  8677. (browse-url (concat type ":" (if (org-string-match-p "[[:nonascii:] ]" path)
  8678. (org-link-escape
  8679. path org-link-escape-chars-browser)
  8680. path))))
  8681. ((string= type "doi")
  8682. (browse-url (concat "http://dx.doi.org/" (if (org-string-match-p "[[:nonascii:] ]" path)
  8683. (org-link-escape
  8684. path org-link-escape-chars-browser)
  8685. path))))
  8686. ((member type '("message"))
  8687. (browse-url (concat type ":" path)))
  8688. ((string= type "tags")
  8689. (org-tags-view arg path))
  8690. ((string= type "tree-match")
  8691. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  8692. ((string= type "file")
  8693. (if (string-match "::\\([0-9]+\\)\\'" path)
  8694. (setq line (string-to-number (match-string 1 path))
  8695. path (substring path 0 (match-beginning 0)))
  8696. (if (string-match "::\\(.+\\)\\'" path)
  8697. (setq search (match-string 1 path)
  8698. path (substring path 0 (match-beginning 0)))))
  8699. (if (string-match "[*?{]" (file-name-nondirectory path))
  8700. (dired path)
  8701. (org-open-file path arg line search)))
  8702. ((string= type "shell")
  8703. (let ((cmd path))
  8704. (if (or (and (not (string= org-confirm-shell-link-not-regexp ""))
  8705. (string-match org-confirm-shell-link-not-regexp cmd))
  8706. (not org-confirm-shell-link-function)
  8707. (funcall org-confirm-shell-link-function
  8708. (format "Execute \"%s\" in shell? "
  8709. (org-add-props cmd nil
  8710. 'face 'org-warning))))
  8711. (progn
  8712. (message "Executing %s" cmd)
  8713. (shell-command cmd))
  8714. (error "Abort"))))
  8715. ((string= type "elisp")
  8716. (let ((cmd path))
  8717. (if (or (and (not (string= org-confirm-elisp-link-not-regexp ""))
  8718. (string-match org-confirm-elisp-link-not-regexp cmd))
  8719. (not org-confirm-elisp-link-function)
  8720. (funcall org-confirm-elisp-link-function
  8721. (format "Execute \"%s\" as elisp? "
  8722. (org-add-props cmd nil
  8723. 'face 'org-warning))))
  8724. (message "%s => %s" cmd
  8725. (if (equal (string-to-char cmd) ?\()
  8726. (eval (read cmd))
  8727. (call-interactively (read cmd))))
  8728. (error "Abort"))))
  8729. ((and (string= type "thisfile")
  8730. (run-hook-with-args-until-success
  8731. 'org-open-link-functions path)))
  8732. ((string= type "thisfile")
  8733. (if arg
  8734. (switch-to-buffer-other-window
  8735. (org-get-buffer-for-internal-link (current-buffer)))
  8736. (org-mark-ring-push))
  8737. (let ((cmd `(org-link-search
  8738. ,path
  8739. ,(cond ((equal arg '(4)) ''occur)
  8740. ((equal arg '(16)) ''org-occur)
  8741. (t nil))
  8742. ,pos)))
  8743. (condition-case nil (let ((org-link-search-inhibit-query t))
  8744. (eval cmd))
  8745. (error (progn (widen) (eval cmd))))))
  8746. (t
  8747. (browse-url-at-point)))))))
  8748. (move-marker org-open-link-marker nil)
  8749. (run-hook-with-args 'org-follow-link-hook)))
  8750. (defun org-offer-links-in-entry (&optional nth zero)
  8751. "Offer links in the current entry and follow the selected link.
  8752. If there is only one link, follow it immediately as well.
  8753. If NTH is an integer, immediately pick the NTH link found.
  8754. If ZERO is a string, check also this string for a link, and if
  8755. there is one, offer it as link number zero."
  8756. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  8757. "\\(" org-angle-link-re "\\)\\|"
  8758. "\\(" org-plain-link-re "\\)"))
  8759. (cnt ?0)
  8760. (in-emacs (if (integerp nth) nil nth))
  8761. have-zero end links link c)
  8762. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  8763. (push (match-string 0 zero) links)
  8764. (setq cnt (1- cnt) have-zero t))
  8765. (save-excursion
  8766. (org-back-to-heading t)
  8767. (setq end (save-excursion (outline-next-heading) (point)))
  8768. (while (re-search-forward re end t)
  8769. (push (match-string 0) links))
  8770. (setq links (org-uniquify (reverse links))))
  8771. (cond
  8772. ((null links)
  8773. (message "No links"))
  8774. ((equal (length links) 1)
  8775. (setq link (list (car links))))
  8776. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  8777. (setq link (list (nth (if have-zero nth (1- nth)) links))))
  8778. (t ; we have to select a link
  8779. (save-excursion
  8780. (save-window-excursion
  8781. (delete-other-windows)
  8782. (with-output-to-temp-buffer "*Select Link*"
  8783. (mapc (lambda (l)
  8784. (if (not (string-match org-bracket-link-regexp l))
  8785. (princ (format "[%c] %s\n" (incf cnt)
  8786. (org-remove-angle-brackets l)))
  8787. (if (match-end 3)
  8788. (princ (format "[%c] %s (%s)\n" (incf cnt)
  8789. (match-string 3 l) (match-string 1 l)))
  8790. (princ (format "[%c] %s\n" (incf cnt)
  8791. (match-string 1 l))))))
  8792. links))
  8793. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  8794. (message "Select link to open, RET to open all:")
  8795. (setq c (read-char-exclusive))
  8796. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  8797. (when (equal c ?q) (error "Abort"))
  8798. (if (equal c ?\C-m)
  8799. (setq link links)
  8800. (setq nth (- c ?0))
  8801. (if have-zero (setq nth (1+ nth)))
  8802. (unless (and (integerp nth) (>= (length links) nth))
  8803. (error "Invalid link selection"))
  8804. (setq link (list (nth (1- nth) links))))))
  8805. (if link
  8806. (let ((buf (current-buffer)))
  8807. (dolist (l link)
  8808. (org-open-link-from-string l in-emacs buf))
  8809. t)
  8810. nil)))
  8811. ;; Add special file links that specify the way of opening
  8812. (org-add-link-type "file+sys" 'org-open-file-with-system)
  8813. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  8814. (defun org-open-file-with-system (path)
  8815. "Open file at PATH using the system way of opening it."
  8816. (org-open-file path 'system))
  8817. (defun org-open-file-with-emacs (path)
  8818. "Open file at PATH in Emacs."
  8819. (org-open-file path 'emacs))
  8820. (defun org-remove-file-link-modifiers ()
  8821. "Remove the file link modifiers in `file+sys:' and `file+emacs:' links."
  8822. (goto-char (point-min))
  8823. (while (re-search-forward "\\<file\\+\\(sys\\|emacs\\):" nil t)
  8824. (org-if-unprotected
  8825. (replace-match "file:" t t))))
  8826. (eval-after-load "org-exp"
  8827. '(add-hook 'org-export-preprocess-before-normalizing-links-hook
  8828. 'org-remove-file-link-modifiers))
  8829. ;;;; Time estimates
  8830. (defun org-get-effort (&optional pom)
  8831. "Get the effort estimate for the current entry."
  8832. (org-entry-get pom org-effort-property))
  8833. ;;; File search
  8834. (defvar org-create-file-search-functions nil
  8835. "List of functions to construct the right search string for a file link.
  8836. These functions are called in turn with point at the location to
  8837. which the link should point.
  8838. A function in the hook should first test if it would like to
  8839. handle this file type, for example by checking the `major-mode'
  8840. or the file extension. If it decides not to handle this file, it
  8841. should just return nil to give other functions a chance. If it
  8842. does handle the file, it must return the search string to be used
  8843. when following the link. The search string will be part of the
  8844. file link, given after a double colon, and `org-open-at-point'
  8845. will automatically search for it. If special measures must be
  8846. taken to make the search successful, another function should be
  8847. added to the companion hook `org-execute-file-search-functions',
  8848. which see.
  8849. A function in this hook may also use `setq' to set the variable
  8850. `description' to provide a suggestion for the descriptive text to
  8851. be used for this link when it gets inserted into an Org-mode
  8852. buffer with \\[org-insert-link].")
  8853. (defvar org-execute-file-search-functions nil
  8854. "List of functions to execute a file search triggered by a link.
  8855. Functions added to this hook must accept a single argument, the
  8856. search string that was part of the file link, the part after the
  8857. double colon. The function must first check if it would like to
  8858. handle this search, for example by checking the `major-mode' or
  8859. the file extension. If it decides not to handle this search, it
  8860. should just return nil to give other functions a chance. If it
  8861. does handle the search, it must return a non-nil value to keep
  8862. other functions from trying.
  8863. Each function can access the current prefix argument through the
  8864. variable `current-prefix-argument'. Note that a single prefix is
  8865. used to force opening a link in Emacs, so it may be good to only
  8866. use a numeric or double prefix to guide the search function.
  8867. In case this is needed, a function in this hook can also restore
  8868. the window configuration before `org-open-at-point' was called using:
  8869. (set-window-configuration org-window-config-before-follow-link)")
  8870. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  8871. (defun org-link-search (s &optional type avoid-pos stealth)
  8872. "Search for a link search option.
  8873. If S is surrounded by forward slashes, it is interpreted as a
  8874. regular expression. In org-mode files, this will create an `org-occur'
  8875. sparse tree. In ordinary files, `occur' will be used to list matches.
  8876. If the current buffer is in `dired-mode', grep will be used to search
  8877. in all files. If AVOID-POS is given, ignore matches near that position.
  8878. When optional argument STEALTH is non-nil, do not modify
  8879. visibility around point, thus ignoring
  8880. `org-show-hierarchy-above', `org-show-following-heading' and
  8881. `org-show-siblings' variables."
  8882. (let ((case-fold-search t)
  8883. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  8884. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  8885. (append '(("") (" ") ("\t") ("\n"))
  8886. org-emphasis-alist)
  8887. "\\|") "\\)"))
  8888. (pos (point))
  8889. (pre nil) (post nil)
  8890. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  8891. (cond
  8892. ;; First check if there are any special search functions
  8893. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  8894. ;; Now try the builtin stuff
  8895. ((and (equal (string-to-char s0) ?#)
  8896. (> (length s0) 1)
  8897. (save-excursion
  8898. (goto-char (point-min))
  8899. (and
  8900. (re-search-forward
  8901. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  8902. (setq type 'dedicated
  8903. pos (match-beginning 0))))
  8904. ;; There is an exact target for this
  8905. (goto-char pos)
  8906. (org-back-to-heading t)))
  8907. ((save-excursion
  8908. (goto-char (point-min))
  8909. (and
  8910. (re-search-forward
  8911. (concat "<<" (regexp-quote s0) ">>") nil t)
  8912. (setq type 'dedicated
  8913. pos (match-beginning 0))))
  8914. ;; There is an exact target for this
  8915. (goto-char pos))
  8916. ((save-excursion
  8917. (goto-char (point-min))
  8918. (and
  8919. (re-search-forward
  8920. (format "^[ \t]*#\\+TARGET: %s" (regexp-quote s0)) nil t)
  8921. (setq type 'dedicated pos (match-beginning 0))))
  8922. ;; Found an invisible target.
  8923. (goto-char pos))
  8924. ((save-excursion
  8925. (goto-char (point-min))
  8926. (and
  8927. (re-search-forward
  8928. (format "^[ \t]*#\\+NAME: %s" (regexp-quote s0)) nil t)
  8929. (setq type 'dedicated pos (match-beginning 0))))
  8930. ;; Found an element with a matching #+name affiliated keyword.
  8931. (goto-char pos))
  8932. ((and (string-match "^(\\(.*\\))$" s0)
  8933. (save-excursion
  8934. (goto-char (point-min))
  8935. (and
  8936. (re-search-forward
  8937. (concat "[^[]" (regexp-quote
  8938. (format org-coderef-label-format
  8939. (match-string 1 s0))))
  8940. nil t)
  8941. (setq type 'dedicated
  8942. pos (1+ (match-beginning 0))))))
  8943. ;; There is a coderef target for this
  8944. (goto-char pos))
  8945. ((string-match "^/\\(.*\\)/$" s)
  8946. ;; A regular expression
  8947. (cond
  8948. ((eq major-mode 'org-mode)
  8949. (org-occur (match-string 1 s)))
  8950. ;;((eq major-mode 'dired-mode)
  8951. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  8952. (t (org-do-occur (match-string 1 s)))))
  8953. ((and (eq major-mode 'org-mode) org-link-search-must-match-exact-headline)
  8954. (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  8955. (goto-char (point-min))
  8956. (cond
  8957. ((let (case-fold-search)
  8958. (re-search-forward (format org-complex-heading-regexp-format
  8959. (regexp-quote s))
  8960. nil t))
  8961. ;; OK, found a match
  8962. (setq type 'dedicated)
  8963. (goto-char (match-beginning 0)))
  8964. ((and (not org-link-search-inhibit-query)
  8965. (eq org-link-search-must-match-exact-headline 'query-to-create)
  8966. (y-or-n-p "No match - create this as a new heading? "))
  8967. (goto-char (point-max))
  8968. (or (bolp) (newline))
  8969. (insert "* " s "\n")
  8970. (beginning-of-line 0))
  8971. (t
  8972. (goto-char pos)
  8973. (error "No match"))))
  8974. (t
  8975. ;; A normal search string
  8976. (when (equal (string-to-char s) ?*)
  8977. ;; Anchor on headlines, post may include tags.
  8978. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  8979. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
  8980. s (substring s 1)))
  8981. (remove-text-properties
  8982. 0 (length s)
  8983. '(face nil mouse-face nil keymap nil fontified nil) s)
  8984. ;; Make a series of regular expressions to find a match
  8985. (setq words (org-split-string s "[ \n\r\t]+")
  8986. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  8987. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  8988. "\\)" markers)
  8989. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  8990. re2a (concat "[ \t\r\n]" re2a_)
  8991. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  8992. re4 (concat "[^a-zA-Z_]" re4_)
  8993. re1 (concat pre re2 post)
  8994. re3 (concat pre (if pre re4_ re4) post)
  8995. re5 (concat pre ".*" re4)
  8996. re2 (concat pre re2)
  8997. re2a (concat pre (if pre re2a_ re2a))
  8998. re4 (concat pre (if pre re4_ re4))
  8999. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  9000. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  9001. re5 "\\)"
  9002. ))
  9003. (cond
  9004. ((eq type 'org-occur) (org-occur reall))
  9005. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  9006. (t (goto-char (point-min))
  9007. (setq type 'fuzzy)
  9008. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  9009. (org-search-not-self 1 re1 nil t)
  9010. (org-search-not-self 1 re2 nil t)
  9011. (org-search-not-self 1 re2a nil t)
  9012. (org-search-not-self 1 re3 nil t)
  9013. (org-search-not-self 1 re4 nil t)
  9014. (org-search-not-self 1 re5 nil t)
  9015. )
  9016. (goto-char (match-beginning 1))
  9017. (goto-char pos)
  9018. (error "No match"))))))
  9019. (and (eq major-mode 'org-mode)
  9020. (not stealth)
  9021. (org-show-context 'link-search))
  9022. type))
  9023. (defun org-search-not-self (group &rest args)
  9024. "Execute `re-search-forward', but only accept matches that do not
  9025. enclose the position of `org-open-link-marker'."
  9026. (let ((m org-open-link-marker))
  9027. (catch 'exit
  9028. (while (apply 're-search-forward args)
  9029. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  9030. (goto-char (match-end group))
  9031. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  9032. (> (match-beginning 0) (marker-position m))
  9033. (< (match-end 0) (marker-position m)))
  9034. (save-match-data
  9035. (or (not (org-in-regexp
  9036. org-bracket-link-analytic-regexp 1))
  9037. (not (match-end 4)) ; no description
  9038. (and (<= (match-beginning 4) (point))
  9039. (>= (match-end 4) (point))))))
  9040. (throw 'exit (point))))))))
  9041. (defun org-get-buffer-for-internal-link (buffer)
  9042. "Return a buffer to be used for displaying the link target of internal links."
  9043. (cond
  9044. ((not org-display-internal-link-with-indirect-buffer)
  9045. buffer)
  9046. ((string-match "(Clone)$" (buffer-name buffer))
  9047. (message "Buffer is already a clone, not making another one")
  9048. ;; we also do not modify visibility in this case
  9049. buffer)
  9050. (t ; make a new indirect buffer for displaying the link
  9051. (let* ((bn (buffer-name buffer))
  9052. (ibn (concat bn "(Clone)"))
  9053. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  9054. (with-current-buffer ib (org-overview))
  9055. ib))))
  9056. (defun org-do-occur (regexp &optional cleanup)
  9057. "Call the Emacs command `occur'.
  9058. If CLEANUP is non-nil, remove the printout of the regular expression
  9059. in the *Occur* buffer. This is useful if the regex is long and not useful
  9060. to read."
  9061. (occur regexp)
  9062. (when cleanup
  9063. (let ((cwin (selected-window)) win beg end)
  9064. (when (setq win (get-buffer-window "*Occur*"))
  9065. (select-window win))
  9066. (goto-char (point-min))
  9067. (when (re-search-forward "match[a-z]+" nil t)
  9068. (setq beg (match-end 0))
  9069. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  9070. (setq end (1- (match-beginning 0)))))
  9071. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  9072. (goto-char (point-min))
  9073. (select-window cwin))))
  9074. ;;; The mark ring for links jumps
  9075. (defvar org-mark-ring nil
  9076. "Mark ring for positions before jumps in Org-mode.")
  9077. (defvar org-mark-ring-last-goto nil
  9078. "Last position in the mark ring used to go back.")
  9079. ;; Fill and close the ring
  9080. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  9081. (loop for i from 1 to org-mark-ring-length do
  9082. (push (make-marker) org-mark-ring))
  9083. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  9084. org-mark-ring)
  9085. (defun org-mark-ring-push (&optional pos buffer)
  9086. "Put the current position or POS into the mark ring and rotate it."
  9087. (interactive)
  9088. (setq pos (or pos (point)))
  9089. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  9090. (move-marker (car org-mark-ring)
  9091. (or pos (point))
  9092. (or buffer (current-buffer)))
  9093. (message "%s"
  9094. (substitute-command-keys
  9095. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  9096. (defun org-mark-ring-goto (&optional n)
  9097. "Jump to the previous position in the mark ring.
  9098. With prefix arg N, jump back that many stored positions. When
  9099. called several times in succession, walk through the entire ring.
  9100. Org-mode commands jumping to a different position in the current file,
  9101. or to another Org-mode file, automatically push the old position
  9102. onto the ring."
  9103. (interactive "p")
  9104. (let (p m)
  9105. (if (eq last-command this-command)
  9106. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  9107. (setq p org-mark-ring))
  9108. (setq org-mark-ring-last-goto p)
  9109. (setq m (car p))
  9110. (org-pop-to-buffer-same-window (marker-buffer m))
  9111. (goto-char m)
  9112. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  9113. (defun org-remove-angle-brackets (s)
  9114. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  9115. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  9116. s)
  9117. (defun org-add-angle-brackets (s)
  9118. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  9119. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  9120. s)
  9121. (defun org-remove-double-quotes (s)
  9122. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  9123. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  9124. s)
  9125. ;;; Following specific links
  9126. (defun org-follow-timestamp-link ()
  9127. (cond
  9128. ((org-at-date-range-p t)
  9129. (let ((org-agenda-start-on-weekday)
  9130. (t1 (match-string 1))
  9131. (t2 (match-string 2)))
  9132. (setq t1 (time-to-days (org-time-string-to-time t1))
  9133. t2 (time-to-days (org-time-string-to-time t2)))
  9134. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  9135. ((org-at-timestamp-p t)
  9136. (org-agenda-list nil (time-to-days (org-time-string-to-time
  9137. (substring (match-string 1) 0 10)))
  9138. 1))
  9139. (t (error "This should not happen"))))
  9140. ;;; Following file links
  9141. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  9142. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  9143. (declare-function mailcap-mime-info
  9144. "mailcap" (string &optional request no-decode))
  9145. (defvar org-wait nil)
  9146. (defun org-open-file (path &optional in-emacs line search)
  9147. "Open the file at PATH.
  9148. First, this expands any special file name abbreviations. Then the
  9149. configuration variable `org-file-apps' is checked if it contains an
  9150. entry for this file type, and if yes, the corresponding command is launched.
  9151. If no application is found, Emacs simply visits the file.
  9152. With optional prefix argument IN-EMACS, Emacs will visit the file.
  9153. With a double \\[universal-argument] \\[universal-argument] \
  9154. prefix arg, Org tries to avoid opening in Emacs
  9155. and to use an external application to visit the file.
  9156. Optional LINE specifies a line to go to, optional SEARCH a string
  9157. to search for. If LINE or SEARCH is given, the file will be
  9158. opened in Emacs, unless an entry from org-file-apps that makes
  9159. use of groups in a regexp matches.
  9160. If you want to change the way frames are used when following a
  9161. link, please customize `org-link-frame-setup'.
  9162. If the file does not exist, an error is thrown."
  9163. (let* ((file (if (equal path "")
  9164. buffer-file-name
  9165. (substitute-in-file-name (expand-file-name path))))
  9166. (file-apps (append org-file-apps (org-default-apps)))
  9167. (apps (org-remove-if
  9168. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9169. (apps-dlink (org-remove-if-not
  9170. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9171. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  9172. (dirp (if remp nil (file-directory-p file)))
  9173. (file (if (and dirp org-open-directory-means-index-dot-org)
  9174. (concat (file-name-as-directory file) "index.org")
  9175. file))
  9176. (a-m-a-p (assq 'auto-mode apps))
  9177. (dfile (downcase file))
  9178. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  9179. (link (cond ((and (eq line nil)
  9180. (eq search nil))
  9181. file)
  9182. (line
  9183. (concat file "::" (number-to-string line)))
  9184. (search
  9185. (concat file "::" search))))
  9186. (dlink (downcase link))
  9187. (old-buffer (current-buffer))
  9188. (old-pos (point))
  9189. (old-mode major-mode)
  9190. ext cmd link-match-data)
  9191. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  9192. (setq ext (match-string 1 dfile))
  9193. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  9194. (setq ext (match-string 1 dfile))))
  9195. (cond
  9196. ((member in-emacs '((16) system))
  9197. (setq cmd (cdr (assoc 'system apps))))
  9198. (in-emacs (setq cmd 'emacs))
  9199. (t
  9200. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  9201. (and dirp (cdr (assoc 'directory apps)))
  9202. ; first, try matching against apps-dlink
  9203. ; if we get a match here, store the match data for later
  9204. (let ((match (assoc-default dlink apps-dlink
  9205. 'string-match)))
  9206. (if match
  9207. (progn (setq link-match-data (match-data))
  9208. match)
  9209. (progn (setq in-emacs (or in-emacs line search))
  9210. nil))) ; if we have no match in apps-dlink,
  9211. ; always open the file in emacs if line or search
  9212. ; is given (for backwards compatibility)
  9213. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  9214. 'string-match)
  9215. (cdr (assoc ext apps))
  9216. (cdr (assoc t apps))))))
  9217. (when (eq cmd 'system)
  9218. (setq cmd (cdr (assoc 'system apps))))
  9219. (when (eq cmd 'default)
  9220. (setq cmd (cdr (assoc t apps))))
  9221. (when (eq cmd 'mailcap)
  9222. (require 'mailcap)
  9223. (mailcap-parse-mailcaps)
  9224. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  9225. (command (mailcap-mime-info mime-type)))
  9226. (if (stringp command)
  9227. (setq cmd command)
  9228. (setq cmd 'emacs))))
  9229. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  9230. (not (file-exists-p file))
  9231. (not org-open-non-existing-files))
  9232. (error "No such file: %s" file))
  9233. (cond
  9234. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  9235. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  9236. (while (string-match "['\"]%s['\"]" cmd)
  9237. (setq cmd (replace-match "%s" t t cmd)))
  9238. (while (string-match "%s" cmd)
  9239. (setq cmd (replace-match
  9240. (save-match-data
  9241. (shell-quote-argument
  9242. (convert-standard-filename file)))
  9243. t t cmd)))
  9244. ;; Replace "%1", "%2" etc. in command with group matches from regex
  9245. (save-match-data
  9246. (let ((match-index 1)
  9247. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  9248. (set-match-data link-match-data)
  9249. (while (<= match-index number-of-groups)
  9250. (let ((regex (concat "%" (number-to-string match-index)))
  9251. (replace-with (match-string match-index dlink)))
  9252. (while (string-match regex cmd)
  9253. (setq cmd (replace-match replace-with t t cmd))))
  9254. (setq match-index (+ match-index 1)))))
  9255. (save-window-excursion
  9256. (start-process-shell-command cmd nil cmd)
  9257. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  9258. ))
  9259. ((or (stringp cmd)
  9260. (eq cmd 'emacs))
  9261. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  9262. (widen)
  9263. (if line (org-goto-line line)
  9264. (if search (org-link-search search))))
  9265. ((consp cmd)
  9266. (let ((file (convert-standard-filename file)))
  9267. (save-match-data
  9268. (set-match-data link-match-data)
  9269. (eval cmd))))
  9270. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  9271. (and (eq major-mode 'org-mode) (eq old-mode 'org-mode)
  9272. (or (not (equal old-buffer (current-buffer)))
  9273. (not (equal old-pos (point))))
  9274. (org-mark-ring-push old-pos old-buffer))))
  9275. (defun org-file-apps-entry-match-against-dlink-p (entry)
  9276. "This function returns non-nil if `entry' uses a regular
  9277. expression which should be matched against the whole link by
  9278. org-open-file.
  9279. It assumes that is the case when the entry uses a regular
  9280. expression which has at least one grouping construct and the
  9281. action is either a lisp form or a command string containing
  9282. '%1', i.e. using at least one subexpression match as a
  9283. parameter."
  9284. (let ((selector (car entry))
  9285. (action (cdr entry)))
  9286. (if (stringp selector)
  9287. (and (> (regexp-opt-depth selector) 0)
  9288. (or (and (stringp action)
  9289. (string-match "%[0-9]" action))
  9290. (consp action)))
  9291. nil)))
  9292. (defun org-default-apps ()
  9293. "Return the default applications for this operating system."
  9294. (cond
  9295. ((eq system-type 'darwin)
  9296. org-file-apps-defaults-macosx)
  9297. ((eq system-type 'windows-nt)
  9298. org-file-apps-defaults-windowsnt)
  9299. (t org-file-apps-defaults-gnu)))
  9300. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  9301. "Convert extensions to regular expressions in the cars of LIST.
  9302. Also, weed out any non-string entries, because the return value is used
  9303. only for regexp matching.
  9304. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  9305. point to the symbol `emacs', indicating that the file should
  9306. be opened in Emacs."
  9307. (append
  9308. (delq nil
  9309. (mapcar (lambda (x)
  9310. (if (not (stringp (car x)))
  9311. nil
  9312. (if (string-match "\\W" (car x))
  9313. x
  9314. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  9315. list))
  9316. (if add-auto-mode
  9317. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  9318. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  9319. (defun org-file-remote-p (file)
  9320. "Test whether FILE specifies a location on a remote system.
  9321. Return non-nil if the location is indeed remote.
  9322. For example, the filename \"/user@host:/foo\" specifies a location
  9323. on the system \"/user@host:\"."
  9324. (cond ((fboundp 'file-remote-p)
  9325. (file-remote-p file))
  9326. ((fboundp 'tramp-handle-file-remote-p)
  9327. (tramp-handle-file-remote-p file))
  9328. ((and (boundp 'ange-ftp-name-format)
  9329. (string-match (car ange-ftp-name-format) file))
  9330. t)
  9331. (t nil)))
  9332. ;;;; Refiling
  9333. (defun org-get-org-file ()
  9334. "Read a filename, with default directory `org-directory'."
  9335. (let ((default (or org-default-notes-file remember-data-file)))
  9336. (read-file-name (format "File name [%s]: " default)
  9337. (file-name-as-directory org-directory)
  9338. default)))
  9339. (defun org-notes-order-reversed-p ()
  9340. "Check if the current file should receive notes in reversed order."
  9341. (cond
  9342. ((not org-reverse-note-order) nil)
  9343. ((eq t org-reverse-note-order) t)
  9344. ((not (listp org-reverse-note-order)) nil)
  9345. (t (catch 'exit
  9346. (let ((all org-reverse-note-order)
  9347. entry)
  9348. (while (setq entry (pop all))
  9349. (if (string-match (car entry) buffer-file-name)
  9350. (throw 'exit (cdr entry))))
  9351. nil)))))
  9352. (defvar org-refile-target-table nil
  9353. "The list of refile targets, created by `org-refile'.")
  9354. (defvar org-agenda-new-buffers nil
  9355. "Buffers created to visit agenda files.")
  9356. (defvar org-refile-cache nil
  9357. "Cache for refile targets.")
  9358. (defvar org-refile-markers nil
  9359. "All the markers used for caching refile locations.")
  9360. (defun org-refile-marker (pos)
  9361. "Get a new refile marker, but only if caching is in use."
  9362. (if (not org-refile-use-cache)
  9363. pos
  9364. (let ((m (make-marker)))
  9365. (move-marker m pos)
  9366. (push m org-refile-markers)
  9367. m)))
  9368. (defun org-refile-cache-clear ()
  9369. "Clear the refile cache and disable all the markers."
  9370. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  9371. (setq org-refile-markers nil)
  9372. (setq org-refile-cache nil)
  9373. (message "Refile cache has been cleared"))
  9374. (defun org-refile-cache-check-set (set)
  9375. "Check if all the markers in the cache still have live buffers."
  9376. (let (marker)
  9377. (catch 'exit
  9378. (while (and set (setq marker (nth 3 (pop set))))
  9379. ;; if org-refile-use-outline-path is 'file, marker may be nil
  9380. (when (and marker (null (marker-buffer marker)))
  9381. (message "not found") (sit-for 3)
  9382. (throw 'exit nil)))
  9383. t)))
  9384. (defun org-refile-cache-put (set &rest identifiers)
  9385. "Push the refile targets SET into the cache, under IDENTIFIERS."
  9386. (let* ((key (sha1 (prin1-to-string identifiers)))
  9387. (entry (assoc key org-refile-cache)))
  9388. (if entry
  9389. (setcdr entry set)
  9390. (push (cons key set) org-refile-cache))))
  9391. (defun org-refile-cache-get (&rest identifiers)
  9392. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  9393. (cond
  9394. ((not org-refile-cache) nil)
  9395. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  9396. (t
  9397. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  9398. org-refile-cache))))
  9399. (and set (org-refile-cache-check-set set) set)))))
  9400. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  9401. "Produce a table with refile targets."
  9402. (let ((case-fold-search nil)
  9403. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  9404. (entries (or org-refile-targets '((nil . (:level . 1)))))
  9405. targets tgs txt re files f desc descre fast-path-p level pos0)
  9406. (message "Getting targets...")
  9407. (with-current-buffer (or default-buffer (current-buffer))
  9408. (while (setq entry (pop entries))
  9409. (setq files (car entry) desc (cdr entry))
  9410. (setq fast-path-p nil)
  9411. (cond
  9412. ((null files) (setq files (list (current-buffer))))
  9413. ((eq files 'org-agenda-files)
  9414. (setq files (org-agenda-files 'unrestricted)))
  9415. ((and (symbolp files) (fboundp files))
  9416. (setq files (funcall files)))
  9417. ((and (symbolp files) (boundp files))
  9418. (setq files (symbol-value files))))
  9419. (if (stringp files) (setq files (list files)))
  9420. (cond
  9421. ((eq (car desc) :tag)
  9422. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  9423. ((eq (car desc) :todo)
  9424. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  9425. ((eq (car desc) :regexp)
  9426. (setq descre (cdr desc)))
  9427. ((eq (car desc) :level)
  9428. (setq descre (concat "^\\*\\{" (number-to-string
  9429. (if org-odd-levels-only
  9430. (1- (* 2 (cdr desc)))
  9431. (cdr desc)))
  9432. "\\}[ \t]")))
  9433. ((eq (car desc) :maxlevel)
  9434. (setq fast-path-p t)
  9435. (setq descre (concat "^\\*\\{1," (number-to-string
  9436. (if org-odd-levels-only
  9437. (1- (* 2 (cdr desc)))
  9438. (cdr desc)))
  9439. "\\}[ \t]")))
  9440. (t (error "Bad refiling target description %s" desc)))
  9441. (while (setq f (pop files))
  9442. (with-current-buffer
  9443. (if (bufferp f) f (org-get-agenda-file-buffer f))
  9444. (or
  9445. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  9446. (progn
  9447. (if (bufferp f) (setq f (buffer-file-name
  9448. (buffer-base-buffer f))))
  9449. (setq f (and f (expand-file-name f)))
  9450. (if (eq org-refile-use-outline-path 'file)
  9451. (push (list (file-name-nondirectory f) f nil nil) tgs))
  9452. (save-excursion
  9453. (save-restriction
  9454. (widen)
  9455. (goto-char (point-min))
  9456. (while (re-search-forward descre nil t)
  9457. (goto-char (setq pos0 (point-at-bol)))
  9458. (catch 'next
  9459. (when org-refile-target-verify-function
  9460. (save-match-data
  9461. (or (funcall org-refile-target-verify-function)
  9462. (throw 'next t))))
  9463. (when (and (looking-at org-complex-heading-regexp)
  9464. (not (member (match-string 4) excluded-entries))
  9465. (match-string 4))
  9466. (setq level (org-reduced-level
  9467. (- (match-end 1) (match-beginning 1)))
  9468. txt (org-link-display-format (match-string 4))
  9469. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  9470. re (format org-complex-heading-regexp-format
  9471. (regexp-quote (match-string 4))))
  9472. (when org-refile-use-outline-path
  9473. (setq txt (mapconcat
  9474. 'org-protect-slash
  9475. (append
  9476. (if (eq org-refile-use-outline-path
  9477. 'file)
  9478. (list (file-name-nondirectory
  9479. (buffer-file-name
  9480. (buffer-base-buffer))))
  9481. (if (eq org-refile-use-outline-path
  9482. 'full-file-path)
  9483. (list (buffer-file-name
  9484. (buffer-base-buffer)))))
  9485. (org-get-outline-path fast-path-p
  9486. level txt)
  9487. (list txt))
  9488. "/")))
  9489. (push (list txt f re (org-refile-marker (point)))
  9490. tgs)))
  9491. (when (= (point) pos0)
  9492. ;; verification function has not moved point
  9493. (goto-char (point-at-eol))))))))
  9494. (when org-refile-use-cache
  9495. (org-refile-cache-put tgs (buffer-file-name) descre))
  9496. (setq targets (append tgs targets))
  9497. ))))
  9498. (message "Getting targets...done")
  9499. (nreverse targets)))
  9500. (defun org-protect-slash (s)
  9501. (while (string-match "/" s)
  9502. (setq s (replace-match "\\" t t s)))
  9503. s)
  9504. (defvar org-olpa (make-vector 20 nil))
  9505. (defun org-get-outline-path (&optional fastp level heading)
  9506. "Return the outline path to the current entry, as a list.
  9507. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  9508. routine which makes outline path derivations for an entire file,
  9509. avoiding backtracing. Refile target collection makes use of that."
  9510. (if fastp
  9511. (progn
  9512. (if (> level 19)
  9513. (error "Outline path failure, more than 19 levels"))
  9514. (loop for i from level upto 19 do
  9515. (aset org-olpa i nil))
  9516. (prog1
  9517. (delq nil (append org-olpa nil))
  9518. (aset org-olpa level heading)))
  9519. (let (rtn case-fold-search)
  9520. (save-excursion
  9521. (save-restriction
  9522. (widen)
  9523. (while (org-up-heading-safe)
  9524. (when (looking-at org-complex-heading-regexp)
  9525. (push (org-match-string-no-properties 4) rtn)))
  9526. rtn)))))
  9527. (defun org-format-outline-path (path &optional width prefix)
  9528. "Format the outline path PATH for display.
  9529. Width is the maximum number of characters that is available.
  9530. Prefix is a prefix to be included in the returned string,
  9531. such as the file name."
  9532. (setq width (or width 79))
  9533. (if prefix (setq width (- width (length prefix))))
  9534. (if (not path)
  9535. (or prefix "")
  9536. (let* ((nsteps (length path))
  9537. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  9538. (maxwidth (if (<= total-width width)
  9539. 10000 ;; everything fits
  9540. ;; we need to shorten the level headings
  9541. (/ (- width nsteps) nsteps)))
  9542. (org-odd-levels-only nil)
  9543. (n 0)
  9544. (total (1+ (length prefix))))
  9545. (setq maxwidth (max maxwidth 10))
  9546. (concat prefix
  9547. (mapconcat
  9548. (lambda (h)
  9549. (setq n (1+ n))
  9550. (if (and (= n nsteps) (< maxwidth 10000))
  9551. (setq maxwidth (- total-width total)))
  9552. (if (< (length h) maxwidth)
  9553. (progn (setq total (+ total (length h) 1)) h)
  9554. (setq h (substring h 0 (- maxwidth 2))
  9555. total (+ total maxwidth 1))
  9556. (if (string-match "[ \t]+\\'" h)
  9557. (setq h (substring h 0 (match-beginning 0))))
  9558. (setq h (concat h "..")))
  9559. (org-add-props h nil 'face
  9560. (nth (% (1- n) org-n-level-faces)
  9561. org-level-faces))
  9562. h)
  9563. path "/")))))
  9564. (defun org-display-outline-path (&optional file current)
  9565. "Display the current outline path in the echo area."
  9566. (interactive "P")
  9567. (let* ((bfn (buffer-file-name (buffer-base-buffer)))
  9568. (case-fold-search nil)
  9569. (path (and (eq major-mode 'org-mode) (org-get-outline-path))))
  9570. (if current (setq path (append path
  9571. (save-excursion
  9572. (org-back-to-heading t)
  9573. (if (looking-at org-complex-heading-regexp)
  9574. (list (match-string 4)))))))
  9575. (message "%s"
  9576. (org-format-outline-path
  9577. path
  9578. (1- (frame-width))
  9579. (and file bfn (concat (file-name-nondirectory bfn) "/"))))))
  9580. (defvar org-refile-history nil
  9581. "History for refiling operations.")
  9582. (defvar org-after-refile-insert-hook nil
  9583. "Hook run after `org-refile' has inserted its stuff at the new location.
  9584. Note that this is still *before* the stuff will be removed from
  9585. the *old* location.")
  9586. (defvar org-capture-last-stored-marker)
  9587. (defun org-refile (&optional goto default-buffer rfloc)
  9588. "Move the entry or entries at point to another heading.
  9589. The list of target headings is compiled using the information in
  9590. `org-refile-targets', which see.
  9591. At the target location, the entry is filed as a subitem of the target
  9592. heading. Depending on `org-reverse-note-order', the new subitem will
  9593. either be the first or the last subitem.
  9594. If there is an active region, all entries in that region will be moved.
  9595. However, the region must fulfill the requirement that the first heading
  9596. is the first one sets the top-level of the moved text - at most siblings
  9597. below it are allowed.
  9598. With prefix arg GOTO, the command will only visit the target location
  9599. and not actually move anything.
  9600. With a double prefix arg \\[universal-argument] \\[universal-argument], \
  9601. go to the location where the last refiling operation has put the subtree.
  9602. With a prefix argument of `2', refile to the running clock.
  9603. RFLOC can be a refile location obtained in a different way.
  9604. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  9605. If you are using target caching (see `org-refile-use-cache'),
  9606. You have to clear the target cache in order to find new targets.
  9607. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  9608. prefix argument (`C-u C-u C-u C-c C-w')."
  9609. (interactive "P")
  9610. (if (member goto '(0 (64)))
  9611. (org-refile-cache-clear)
  9612. (let* ((cbuf (current-buffer))
  9613. (regionp (org-region-active-p))
  9614. (region-start (and regionp (region-beginning)))
  9615. (region-end (and regionp (region-end)))
  9616. (region-length (and regionp (- region-end region-start)))
  9617. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  9618. pos it nbuf file re level reversed)
  9619. (setq last-command nil)
  9620. (when regionp
  9621. (goto-char region-start)
  9622. (or (bolp) (goto-char (point-at-bol)))
  9623. (setq region-start (point))
  9624. (unless (or (org-kill-is-subtree-p
  9625. (buffer-substring region-start region-end))
  9626. (prog1 org-refile-active-region-within-subtree
  9627. (org-toggle-heading)))
  9628. (error "The region is not a (sequence of) subtree(s)")))
  9629. (if (equal goto '(16))
  9630. (org-refile-goto-last-stored)
  9631. (when (or
  9632. (and (equal goto 2)
  9633. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  9634. (prog1
  9635. (setq it (list (or org-clock-heading "running clock")
  9636. (buffer-file-name
  9637. (marker-buffer org-clock-hd-marker))
  9638. ""
  9639. (marker-position org-clock-hd-marker)))
  9640. (setq goto nil)))
  9641. (setq it (or rfloc
  9642. (let (heading-text)
  9643. (save-excursion
  9644. (unless goto
  9645. (org-back-to-heading t)
  9646. (setq heading-text
  9647. (nth 4 (org-heading-components))))
  9648. (org-refile-get-location
  9649. (cond (goto "Goto")
  9650. (regionp "Refile region to")
  9651. (t (concat "Refile subtree \""
  9652. heading-text "\" to")))
  9653. default-buffer
  9654. (and (not (equal '(4) goto))
  9655. org-refile-allow-creating-parent-nodes)
  9656. goto))))))
  9657. (setq file (nth 1 it)
  9658. re (nth 2 it)
  9659. pos (nth 3 it))
  9660. (if (and (not goto)
  9661. pos
  9662. (equal (buffer-file-name) file)
  9663. (if regionp
  9664. (and (>= pos region-start)
  9665. (<= pos region-end))
  9666. (and (>= pos (point))
  9667. (< pos (save-excursion
  9668. (org-end-of-subtree t t))))))
  9669. (error "Cannot refile to position inside the tree or region"))
  9670. (setq nbuf (or (find-buffer-visiting file)
  9671. (find-file-noselect file)))
  9672. (if goto
  9673. (progn
  9674. (org-pop-to-buffer-same-window nbuf)
  9675. (goto-char pos)
  9676. (org-show-context 'org-goto))
  9677. (if regionp
  9678. (progn
  9679. (org-kill-new (buffer-substring region-start region-end))
  9680. (org-save-markers-in-region region-start region-end))
  9681. (org-copy-subtree 1 nil t))
  9682. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  9683. (find-file-noselect file)))
  9684. (setq reversed (org-notes-order-reversed-p))
  9685. (save-excursion
  9686. (save-restriction
  9687. (widen)
  9688. (if pos
  9689. (progn
  9690. (goto-char pos)
  9691. (looking-at org-outline-regexp)
  9692. (setq level (org-get-valid-level (funcall outline-level) 1))
  9693. (goto-char
  9694. (if reversed
  9695. (or (outline-next-heading) (point-max))
  9696. (or (save-excursion (org-get-next-sibling))
  9697. (org-end-of-subtree t t)
  9698. (point-max)))))
  9699. (setq level 1)
  9700. (if (not reversed)
  9701. (goto-char (point-max))
  9702. (goto-char (point-min))
  9703. (or (outline-next-heading) (goto-char (point-max)))))
  9704. (if (not (bolp)) (newline))
  9705. (org-paste-subtree level)
  9706. (when org-log-refile
  9707. (org-add-log-setup 'refile nil nil 'findpos
  9708. org-log-refile)
  9709. (unless (eq org-log-refile 'note)
  9710. (save-excursion (org-add-log-note))))
  9711. (and org-auto-align-tags (org-set-tags nil t))
  9712. (bookmark-set "org-refile-last-stored")
  9713. ;; If we are refiling for capture, make sure that the
  9714. ;; last-capture pointers point here
  9715. (when (org-bound-and-true-p org-refile-for-capture)
  9716. (bookmark-set "org-capture-last-stored-marker")
  9717. (move-marker org-capture-last-stored-marker (point)))
  9718. (if (fboundp 'deactivate-mark) (deactivate-mark))
  9719. (run-hooks 'org-after-refile-insert-hook))))
  9720. (if regionp
  9721. (delete-region (point) (+ (point) region-length))
  9722. (org-cut-subtree))
  9723. (when (featurep 'org-inlinetask)
  9724. (org-inlinetask-remove-END-maybe))
  9725. (setq org-markers-to-move nil)
  9726. (message "Refiled to \"%s\" in file %s" (car it) file)))))))
  9727. (defun org-refile-goto-last-stored ()
  9728. "Go to the location where the last refile was stored."
  9729. (interactive)
  9730. (bookmark-jump "org-refile-last-stored")
  9731. (message "This is the location of the last refile"))
  9732. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  9733. no-exclude)
  9734. "Prompt the user for a refile location, using PROMPT.
  9735. PROMPT should not be suffixed with a colon and a space, because
  9736. this function appends the default value from
  9737. `org-refile-history' automatically, if that is not empty.
  9738. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  9739. this is used for the GOTO interface."
  9740. (let ((org-refile-targets org-refile-targets)
  9741. (org-refile-use-outline-path org-refile-use-outline-path)
  9742. excluded-entries)
  9743. (when (and (eq major-mode 'org-mode)
  9744. (not org-refile-use-cache)
  9745. (not no-exclude))
  9746. (org-map-tree
  9747. (lambda()
  9748. (setq excluded-entries
  9749. (append excluded-entries (list (org-get-heading t t)))))))
  9750. (setq org-refile-target-table
  9751. (org-refile-get-targets default-buffer excluded-entries)))
  9752. (unless org-refile-target-table
  9753. (error "No refile targets"))
  9754. (let* ((prompt (concat prompt
  9755. (and (car org-refile-history)
  9756. (concat " (default " (car org-refile-history) ")"))
  9757. ": "))
  9758. (cbuf (current-buffer))
  9759. (partial-completion-mode nil)
  9760. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  9761. (cfunc (if (and org-refile-use-outline-path
  9762. org-outline-path-complete-in-steps)
  9763. 'org-olpath-completing-read
  9764. 'org-icompleting-read))
  9765. (extra (if org-refile-use-outline-path "/" ""))
  9766. (filename (and cfn (expand-file-name cfn)))
  9767. (tbl (mapcar
  9768. (lambda (x)
  9769. (if (and (not (member org-refile-use-outline-path
  9770. '(file full-file-path)))
  9771. (not (equal filename (nth 1 x))))
  9772. (cons (concat (car x) extra " ("
  9773. (file-name-nondirectory (nth 1 x)) ")")
  9774. (cdr x))
  9775. (cons (concat (car x) extra) (cdr x))))
  9776. org-refile-target-table))
  9777. (completion-ignore-case t)
  9778. pa answ parent-target child parent old-hist)
  9779. (setq old-hist org-refile-history)
  9780. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  9781. nil 'org-refile-history (car org-refile-history)))
  9782. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  9783. (org-refile-check-position pa)
  9784. (if pa
  9785. (progn
  9786. (when (or (not org-refile-history)
  9787. (not (eq old-hist org-refile-history))
  9788. (not (equal (car pa) (car org-refile-history))))
  9789. (setq org-refile-history
  9790. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  9791. org-refile-history
  9792. (cdr org-refile-history))))
  9793. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  9794. (pop org-refile-history)))
  9795. pa)
  9796. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  9797. (progn
  9798. (setq parent (match-string 1 answ)
  9799. child (match-string 2 answ))
  9800. (setq parent-target (or (assoc parent tbl)
  9801. (assoc (concat parent "/") tbl)))
  9802. (when (and parent-target
  9803. (or (eq new-nodes t)
  9804. (and (eq new-nodes 'confirm)
  9805. (y-or-n-p (format "Create new node \"%s\"? "
  9806. child)))))
  9807. (org-refile-new-child parent-target child)))
  9808. (error "Invalid target location")))))
  9809. (declare-function org-string-nw-p "org-macs.el" (s))
  9810. (defun org-refile-check-position (refile-pointer)
  9811. "Check if the refile pointer matches the readline to which it points."
  9812. (let* ((file (nth 1 refile-pointer))
  9813. (re (nth 2 refile-pointer))
  9814. (pos (nth 3 refile-pointer))
  9815. buffer)
  9816. (when (org-string-nw-p re)
  9817. (setq buffer (if (markerp pos)
  9818. (marker-buffer pos)
  9819. (or (find-buffer-visiting file)
  9820. (find-file-noselect file))))
  9821. (with-current-buffer buffer
  9822. (save-excursion
  9823. (save-restriction
  9824. (widen)
  9825. (goto-char pos)
  9826. (beginning-of-line 1)
  9827. (unless (org-looking-at-p re)
  9828. (error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling"))))))))
  9829. (defun org-refile-new-child (parent-target child)
  9830. "Use refile target PARENT-TARGET to add new CHILD below it."
  9831. (unless parent-target
  9832. (error "Cannot find parent for new node"))
  9833. (let ((file (nth 1 parent-target))
  9834. (pos (nth 3 parent-target))
  9835. level)
  9836. (with-current-buffer (or (find-buffer-visiting file)
  9837. (find-file-noselect file))
  9838. (save-excursion
  9839. (save-restriction
  9840. (widen)
  9841. (if pos
  9842. (goto-char pos)
  9843. (goto-char (point-max))
  9844. (if (not (bolp)) (newline)))
  9845. (when (looking-at org-outline-regexp)
  9846. (setq level (funcall outline-level))
  9847. (org-end-of-subtree t t))
  9848. (org-back-over-empty-lines)
  9849. (insert "\n" (make-string
  9850. (if pos (org-get-valid-level level 1) 1) ?*)
  9851. " " child "\n")
  9852. (beginning-of-line 0)
  9853. (list (concat (car parent-target) "/" child) file "" (point)))))))
  9854. (defun org-olpath-completing-read (prompt collection &rest args)
  9855. "Read an outline path like a file name."
  9856. (let ((thetable collection)
  9857. (org-completion-use-ido nil) ; does not work with ido.
  9858. (org-completion-use-iswitchb nil)) ; or iswitchb
  9859. (apply
  9860. 'org-icompleting-read prompt
  9861. (lambda (string predicate &optional flag)
  9862. (let (rtn r f (l (length string)))
  9863. (cond
  9864. ((eq flag nil)
  9865. ;; try completion
  9866. (try-completion string thetable))
  9867. ((eq flag t)
  9868. ;; all-completions
  9869. (setq rtn (all-completions string thetable predicate))
  9870. (mapcar
  9871. (lambda (x)
  9872. (setq r (substring x l))
  9873. (if (string-match " ([^)]*)$" x)
  9874. (setq f (match-string 0 x))
  9875. (setq f ""))
  9876. (if (string-match "/" r)
  9877. (concat string (substring r 0 (match-end 0)) f)
  9878. x))
  9879. rtn))
  9880. ((eq flag 'lambda)
  9881. ;; exact match?
  9882. (assoc string thetable)))
  9883. ))
  9884. args)))
  9885. ;;;; Dynamic blocks
  9886. (defun org-find-dblock (name)
  9887. "Find the first dynamic block with name NAME in the buffer.
  9888. If not found, stay at current position and return nil."
  9889. (let (pos)
  9890. (save-excursion
  9891. (goto-char (point-min))
  9892. (setq pos (and (re-search-forward (concat "^[ \t]*#\\+BEGIN:[ \t]+" name "\\>")
  9893. nil t)
  9894. (match-beginning 0))))
  9895. (if pos (goto-char pos))
  9896. pos))
  9897. (defconst org-dblock-start-re
  9898. "^[ \t]*#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  9899. "Matches the start line of a dynamic block, with parameters.")
  9900. (defconst org-dblock-end-re "^[ \t]*#\\+END\\([: \t\r\n]\\|$\\)"
  9901. "Matches the end of a dynamic block.")
  9902. (defun org-create-dblock (plist)
  9903. "Create a dynamic block section, with parameters taken from PLIST.
  9904. PLIST must contain a :name entry which is used as name of the block."
  9905. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  9906. (end-of-line 1)
  9907. (newline))
  9908. (let ((col (current-column))
  9909. (name (plist-get plist :name)))
  9910. (insert "#+BEGIN: " name)
  9911. (while plist
  9912. (if (eq (car plist) :name)
  9913. (setq plist (cddr plist))
  9914. (insert " " (prin1-to-string (pop plist)))))
  9915. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  9916. (beginning-of-line -2)))
  9917. (defun org-prepare-dblock ()
  9918. "Prepare dynamic block for refresh.
  9919. This empties the block, puts the cursor at the insert position and returns
  9920. the property list including an extra property :name with the block name."
  9921. (unless (looking-at org-dblock-start-re)
  9922. (error "Not at a dynamic block"))
  9923. (let* ((begdel (1+ (match-end 0)))
  9924. (name (org-no-properties (match-string 1)))
  9925. (params (append (list :name name)
  9926. (read (concat "(" (match-string 3) ")")))))
  9927. (save-excursion
  9928. (beginning-of-line 1)
  9929. (skip-chars-forward " \t")
  9930. (setq params (plist-put params :indentation-column (current-column))))
  9931. (unless (re-search-forward org-dblock-end-re nil t)
  9932. (error "Dynamic block not terminated"))
  9933. (setq params
  9934. (append params
  9935. (list :content (buffer-substring
  9936. begdel (match-beginning 0)))))
  9937. (delete-region begdel (match-beginning 0))
  9938. (goto-char begdel)
  9939. (open-line 1)
  9940. params))
  9941. (defun org-map-dblocks (&optional command)
  9942. "Apply COMMAND to all dynamic blocks in the current buffer.
  9943. If COMMAND is not given, use `org-update-dblock'."
  9944. (let ((cmd (or command 'org-update-dblock)))
  9945. (save-excursion
  9946. (goto-char (point-min))
  9947. (while (re-search-forward org-dblock-start-re nil t)
  9948. (goto-char (match-beginning 0))
  9949. (save-excursion
  9950. (condition-case nil
  9951. (funcall cmd)
  9952. (error (message "Error during update of dynamic block"))))
  9953. (unless (re-search-forward org-dblock-end-re nil t)
  9954. (error "Dynamic block not terminated"))))))
  9955. (defun org-dblock-update (&optional arg)
  9956. "User command for updating dynamic blocks.
  9957. Update the dynamic block at point. With prefix ARG, update all dynamic
  9958. blocks in the buffer."
  9959. (interactive "P")
  9960. (if arg
  9961. (org-update-all-dblocks)
  9962. (or (looking-at org-dblock-start-re)
  9963. (org-beginning-of-dblock))
  9964. (org-update-dblock)))
  9965. (defun org-update-dblock ()
  9966. "Update the dynamic block at point.
  9967. This means to empty the block, parse for parameters and then call
  9968. the correct writing function."
  9969. (interactive)
  9970. (save-window-excursion
  9971. (let* ((pos (point))
  9972. (line (org-current-line))
  9973. (params (org-prepare-dblock))
  9974. (name (plist-get params :name))
  9975. (indent (plist-get params :indentation-column))
  9976. (cmd (intern (concat "org-dblock-write:" name))))
  9977. (message "Updating dynamic block `%s' at line %d..." name line)
  9978. (funcall cmd params)
  9979. (message "Updating dynamic block `%s' at line %d...done" name line)
  9980. (goto-char pos)
  9981. (when (and indent (> indent 0))
  9982. (setq indent (make-string indent ?\ ))
  9983. (save-excursion
  9984. (org-beginning-of-dblock)
  9985. (forward-line 1)
  9986. (while (not (looking-at org-dblock-end-re))
  9987. (insert indent)
  9988. (beginning-of-line 2))
  9989. (when (looking-at org-dblock-end-re)
  9990. (and (looking-at "[ \t]+")
  9991. (replace-match ""))
  9992. (insert indent)))))))
  9993. (defun org-beginning-of-dblock ()
  9994. "Find the beginning of the dynamic block at point.
  9995. Error if there is no such block at point."
  9996. (let ((pos (point))
  9997. beg)
  9998. (end-of-line 1)
  9999. (if (and (re-search-backward org-dblock-start-re nil t)
  10000. (setq beg (match-beginning 0))
  10001. (re-search-forward org-dblock-end-re nil t)
  10002. (> (match-end 0) pos))
  10003. (goto-char beg)
  10004. (goto-char pos)
  10005. (error "Not in a dynamic block"))))
  10006. (defun org-update-all-dblocks ()
  10007. "Update all dynamic blocks in the buffer.
  10008. This function can be used in a hook."
  10009. (interactive)
  10010. (when (eq major-mode 'org-mode)
  10011. (org-map-dblocks 'org-update-dblock)))
  10012. ;;;; Completion
  10013. (defconst org-additional-option-like-keywords
  10014. '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML:"
  10015. "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook:"
  10016. "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:"
  10017. "LATEX_CLASS:" "LATEX_CLASS_OPTIONS:" "ATTR_LaTeX:"
  10018. "BEGIN:" "END:"
  10019. "ORGTBL" "TBLFM:" "TBLNAME:"
  10020. "BEGIN_EXAMPLE" "END_EXAMPLE"
  10021. "BEGIN_QUOTE" "END_QUOTE"
  10022. "BEGIN_VERSE" "END_VERSE"
  10023. "BEGIN_CENTER" "END_CENTER"
  10024. "BEGIN_SRC" "END_SRC"
  10025. "BEGIN_RESULT" "END_RESULT"
  10026. "NAME:" "RESULTS:"
  10027. "HEADER:" "HEADERS:"
  10028. "CATEGORY:" "COLUMNS:" "PROPERTY:"
  10029. "CAPTION:" "LABEL:"
  10030. "SETUPFILE:"
  10031. "INCLUDE:"
  10032. "BIND:"
  10033. "MACRO:"))
  10034. (defcustom org-structure-template-alist
  10035. '(
  10036. ("s" "#+BEGIN_SRC ?\n\n#+END_SRC"
  10037. "<src lang=\"?\">\n\n</src>")
  10038. ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE"
  10039. "<example>\n?\n</example>")
  10040. ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE"
  10041. "<quote>\n?\n</quote>")
  10042. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE"
  10043. "<verse>\n?\n</verse>")
  10044. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER"
  10045. "<center>\n?\n</center>")
  10046. ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX"
  10047. "<literal style=\"latex\">\n?\n</literal>")
  10048. ("L" "#+LaTeX: "
  10049. "<literal style=\"latex\">?</literal>")
  10050. ("h" "#+BEGIN_HTML\n?\n#+END_HTML"
  10051. "<literal style=\"html\">\n?\n</literal>")
  10052. ("H" "#+HTML: "
  10053. "<literal style=\"html\">?</literal>")
  10054. ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII")
  10055. ("A" "#+ASCII: ")
  10056. ("i" "#+INDEX: ?"
  10057. "#+INDEX: ?")
  10058. ("I" "#+INCLUDE %file ?"
  10059. "<include file=%file markup=\"?\">")
  10060. )
  10061. "Structure completion elements.
  10062. This is a list of abbreviation keys and values. The value gets inserted
  10063. if you type `<' followed by the key and then press the completion key,
  10064. usually `M-TAB'. %file will be replaced by a file name after prompting
  10065. for the file using completion. The cursor will be placed at the position
  10066. of the `?` in the template.
  10067. There are two templates for each key, the first uses the original Org syntax,
  10068. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  10069. the default when the /org-mtags.el/ module has been loaded. See also the
  10070. variable `org-mtags-prefer-muse-templates'."
  10071. :group 'org-completion
  10072. :type '(repeat
  10073. (string :tag "Key")
  10074. (string :tag "Template")
  10075. (string :tag "Muse Template")))
  10076. (defun org-try-structure-completion ()
  10077. "Try to complete a structure template before point.
  10078. This looks for strings like \"<e\" on an otherwise empty line and
  10079. expands them."
  10080. (let ((l (buffer-substring (point-at-bol) (point)))
  10081. a)
  10082. (when (and (looking-at "[ \t]*$")
  10083. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  10084. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  10085. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  10086. (match-beginning 1)) a)
  10087. t)))
  10088. (defun org-complete-expand-structure-template (start cell)
  10089. "Expand a structure template."
  10090. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  10091. (rpl (nth (if musep 2 1) cell))
  10092. (ind ""))
  10093. (delete-region start (point))
  10094. (when (string-match "\\`#\\+" rpl)
  10095. (cond
  10096. ((bolp))
  10097. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  10098. (setq ind (buffer-substring (point-at-bol) (point))))
  10099. (t (newline))))
  10100. (setq start (point))
  10101. (if (string-match "%file" rpl)
  10102. (setq rpl (replace-match
  10103. (concat
  10104. "\""
  10105. (save-match-data
  10106. (abbreviate-file-name (read-file-name "Include file: ")))
  10107. "\"")
  10108. t t rpl)))
  10109. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  10110. (concat "\n" ind)))
  10111. (insert rpl)
  10112. (if (re-search-backward "\\?" start t) (delete-char 1))))
  10113. ;;;; TODO, DEADLINE, Comments
  10114. (defun org-toggle-comment ()
  10115. "Change the COMMENT state of an entry."
  10116. (interactive)
  10117. (save-excursion
  10118. (org-back-to-heading)
  10119. (let (case-fold-search)
  10120. (cond
  10121. ((looking-at (format org-heading-keyword-regexp-format
  10122. org-comment-string))
  10123. (goto-char (match-end 1))
  10124. (looking-at (concat " +" org-comment-string))
  10125. (replace-match "" t t)
  10126. (when (eolp) (insert " ")))
  10127. ((looking-at org-outline-regexp)
  10128. (goto-char (match-end 0))
  10129. (insert org-comment-string " "))))))
  10130. (defvar org-last-todo-state-is-todo nil
  10131. "This is non-nil when the last TODO state change led to a TODO state.
  10132. If the last change removed the TODO tag or switched to DONE, then
  10133. this is nil.")
  10134. (defvar org-setting-tags nil) ; dynamically skipped
  10135. (defvar org-todo-setup-filter-hook nil
  10136. "Hook for functions that pre-filter todo specs.
  10137. Each function takes a todo spec and returns either nil or the spec
  10138. transformed into canonical form." )
  10139. (defvar org-todo-get-default-hook nil
  10140. "Hook for functions that get a default item for todo.
  10141. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  10142. nil or a string to be used for the todo mark." )
  10143. (defvar org-agenda-headline-snapshot-before-repeat)
  10144. (defun org-current-effective-time ()
  10145. "Return current time adjusted for `org-extend-today-until' variable"
  10146. (let* ((ct (org-current-time))
  10147. (dct (decode-time ct))
  10148. (ct1
  10149. (if (and org-use-effective-time
  10150. (< (nth 2 dct) org-extend-today-until))
  10151. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct))
  10152. ct)))
  10153. ct1))
  10154. (defun org-todo-yesterday (&optional arg)
  10155. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  10156. (interactive "P")
  10157. (if (eq major-mode 'org-agenda-mode)
  10158. (apply 'org-agenda-todo-yesterday arg)
  10159. (let* ((hour (third (decode-time
  10160. (org-current-time))))
  10161. (org-extend-today-until (1+ hour)))
  10162. (org-todo arg))))
  10163. (defun org-todo (&optional arg)
  10164. "Change the TODO state of an item.
  10165. The state of an item is given by a keyword at the start of the heading,
  10166. like
  10167. *** TODO Write paper
  10168. *** DONE Call mom
  10169. The different keywords are specified in the variable `org-todo-keywords'.
  10170. By default the available states are \"TODO\" and \"DONE\".
  10171. So for this example: when the item starts with TODO, it is changed to DONE.
  10172. When it starts with DONE, the DONE is removed. And when neither TODO nor
  10173. DONE are present, add TODO at the beginning of the heading.
  10174. With \\[universal-argument] prefix arg, use completion to determine the new \
  10175. state.
  10176. With numeric prefix arg, switch to that state.
  10177. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  10178. keywords (nextset).
  10179. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  10180. With a numeric prefix arg of 0, inhibit note taking for the change.
  10181. For calling through lisp, arg is also interpreted in the following way:
  10182. 'none -> empty state
  10183. \"\"(empty string) -> switch to empty state
  10184. 'done -> switch to DONE
  10185. 'nextset -> switch to the next set of keywords
  10186. 'previousset -> switch to the previous set of keywords
  10187. \"WAITING\" -> switch to the specified keyword, but only if it
  10188. really is a member of `org-todo-keywords'."
  10189. (interactive "P")
  10190. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  10191. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  10192. 'region-start-level 'region))
  10193. org-loop-over-headlines-in-active-region)
  10194. (org-map-entries
  10195. `(org-todo ,arg)
  10196. org-loop-over-headlines-in-active-region
  10197. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  10198. (if (equal arg '(16)) (setq arg 'nextset))
  10199. (let ((org-blocker-hook org-blocker-hook)
  10200. (case-fold-search nil))
  10201. (when (equal arg '(64))
  10202. (setq arg nil org-blocker-hook nil))
  10203. (when (and org-blocker-hook
  10204. (or org-inhibit-blocking
  10205. (org-entry-get nil "NOBLOCKING")))
  10206. (setq org-blocker-hook nil))
  10207. (save-excursion
  10208. (catch 'exit
  10209. (org-back-to-heading t)
  10210. (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  10211. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  10212. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  10213. (let* ((match-data (match-data))
  10214. (startpos (point-at-bol))
  10215. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  10216. (org-log-done org-log-done)
  10217. (org-log-repeat org-log-repeat)
  10218. (org-todo-log-states org-todo-log-states)
  10219. (org-inhibit-logging
  10220. (if (equal arg 0)
  10221. (progn (setq arg nil) 'note) org-inhibit-logging))
  10222. (this (match-string 1))
  10223. (hl-pos (match-beginning 0))
  10224. (head (org-get-todo-sequence-head this))
  10225. (ass (assoc head org-todo-kwd-alist))
  10226. (interpret (nth 1 ass))
  10227. (done-word (nth 3 ass))
  10228. (final-done-word (nth 4 ass))
  10229. (org-last-state (or this ""))
  10230. (completion-ignore-case t)
  10231. (member (member this org-todo-keywords-1))
  10232. (tail (cdr member))
  10233. (org-state (cond
  10234. ((and org-todo-key-trigger
  10235. (or (and (equal arg '(4))
  10236. (eq org-use-fast-todo-selection 'prefix))
  10237. (and (not arg) org-use-fast-todo-selection
  10238. (not (eq org-use-fast-todo-selection
  10239. 'prefix)))))
  10240. ;; Use fast selection
  10241. (org-fast-todo-selection))
  10242. ((and (equal arg '(4))
  10243. (or (not org-use-fast-todo-selection)
  10244. (not org-todo-key-trigger)))
  10245. ;; Read a state with completion
  10246. (org-icompleting-read
  10247. "State: " (mapcar (lambda(x) (list x))
  10248. org-todo-keywords-1)
  10249. nil t))
  10250. ((eq arg 'right)
  10251. (if this
  10252. (if tail (car tail) nil)
  10253. (car org-todo-keywords-1)))
  10254. ((eq arg 'left)
  10255. (if (equal member org-todo-keywords-1)
  10256. nil
  10257. (if this
  10258. (nth (- (length org-todo-keywords-1)
  10259. (length tail) 2)
  10260. org-todo-keywords-1)
  10261. (org-last org-todo-keywords-1))))
  10262. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  10263. (setq arg nil))) ; hack to fall back to cycling
  10264. (arg
  10265. ;; user or caller requests a specific state
  10266. (cond
  10267. ((equal arg "") nil)
  10268. ((eq arg 'none) nil)
  10269. ((eq arg 'done) (or done-word (car org-done-keywords)))
  10270. ((eq arg 'nextset)
  10271. (or (car (cdr (member head org-todo-heads)))
  10272. (car org-todo-heads)))
  10273. ((eq arg 'previousset)
  10274. (let ((org-todo-heads (reverse org-todo-heads)))
  10275. (or (car (cdr (member head org-todo-heads)))
  10276. (car org-todo-heads))))
  10277. ((car (member arg org-todo-keywords-1)))
  10278. ((stringp arg)
  10279. (error "State `%s' not valid in this file" arg))
  10280. ((nth (1- (prefix-numeric-value arg))
  10281. org-todo-keywords-1))))
  10282. ((null member) (or head (car org-todo-keywords-1)))
  10283. ((equal this final-done-word) nil) ;; -> make empty
  10284. ((null tail) nil) ;; -> first entry
  10285. ((memq interpret '(type priority))
  10286. (if (eq this-command last-command)
  10287. (car tail)
  10288. (if (> (length tail) 0)
  10289. (or done-word (car org-done-keywords))
  10290. nil)))
  10291. (t
  10292. (car tail))))
  10293. (org-state (or
  10294. (run-hook-with-args-until-success
  10295. 'org-todo-get-default-hook org-state org-last-state)
  10296. org-state))
  10297. (next (if org-state (concat " " org-state " ") " "))
  10298. (change-plist (list :type 'todo-state-change :from this :to org-state
  10299. :position startpos))
  10300. dolog now-done-p)
  10301. (when org-blocker-hook
  10302. (setq org-last-todo-state-is-todo
  10303. (not (member this org-done-keywords)))
  10304. (unless (save-excursion
  10305. (save-match-data
  10306. (org-with-wide-buffer
  10307. (run-hook-with-args-until-failure
  10308. 'org-blocker-hook change-plist))))
  10309. (if (org-called-interactively-p 'interactive)
  10310. (error "TODO state change from %s to %s blocked" this org-state)
  10311. ;; fail silently
  10312. (message "TODO state change from %s to %s blocked" this org-state)
  10313. (throw 'exit nil))))
  10314. (store-match-data match-data)
  10315. (replace-match next t t)
  10316. (unless (pos-visible-in-window-p hl-pos)
  10317. (message "TODO state changed to %s" (org-trim next)))
  10318. (unless head
  10319. (setq head (org-get-todo-sequence-head org-state)
  10320. ass (assoc head org-todo-kwd-alist)
  10321. interpret (nth 1 ass)
  10322. done-word (nth 3 ass)
  10323. final-done-word (nth 4 ass)))
  10324. (when (memq arg '(nextset previousset))
  10325. (message "Keyword-Set %d/%d: %s"
  10326. (- (length org-todo-sets) -1
  10327. (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
  10328. (length org-todo-sets)
  10329. (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
  10330. (setq org-last-todo-state-is-todo
  10331. (not (member org-state org-done-keywords)))
  10332. (setq now-done-p (and (member org-state org-done-keywords)
  10333. (not (member this org-done-keywords))))
  10334. (and logging (org-local-logging logging))
  10335. (when (and (or org-todo-log-states org-log-done)
  10336. (not (eq org-inhibit-logging t))
  10337. (not (memq arg '(nextset previousset))))
  10338. ;; we need to look at recording a time and note
  10339. (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
  10340. (nth 2 (assoc this org-todo-log-states))))
  10341. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  10342. (setq dolog 'time))
  10343. (when (and org-state
  10344. (member org-state org-not-done-keywords)
  10345. (not (member this org-not-done-keywords)))
  10346. ;; This is now a todo state and was not one before
  10347. ;; If there was a CLOSED time stamp, get rid of it.
  10348. (org-add-planning-info nil nil 'closed))
  10349. (when (and now-done-p org-log-done)
  10350. ;; It is now done, and it was not done before
  10351. (org-add-planning-info 'closed (org-current-effective-time))
  10352. (if (and (not dolog) (eq 'note org-log-done))
  10353. (org-add-log-setup 'done org-state this 'findpos 'note)))
  10354. (when (and org-state dolog)
  10355. ;; This is a non-nil state, and we need to log it
  10356. (org-add-log-setup 'state org-state this 'findpos dolog)))
  10357. ;; Fixup tag positioning
  10358. (org-todo-trigger-tag-changes org-state)
  10359. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  10360. (when org-provide-todo-statistics
  10361. (org-update-parent-todo-statistics))
  10362. (run-hooks 'org-after-todo-state-change-hook)
  10363. (if (and arg (not (member org-state org-done-keywords)))
  10364. (setq head (org-get-todo-sequence-head org-state)))
  10365. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  10366. ;; Do we need to trigger a repeat?
  10367. (when now-done-p
  10368. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  10369. ;; This is for the agenda, take a snapshot of the headline.
  10370. (save-match-data
  10371. (setq org-agenda-headline-snapshot-before-repeat
  10372. (org-get-heading))))
  10373. (org-auto-repeat-maybe org-state))
  10374. ;; Fixup cursor location if close to the keyword
  10375. (if (and (outline-on-heading-p)
  10376. (not (bolp))
  10377. (save-excursion (beginning-of-line 1)
  10378. (looking-at org-todo-line-regexp))
  10379. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  10380. (progn
  10381. (goto-char (or (match-end 2) (match-end 1)))
  10382. (and (looking-at " ") (just-one-space))))
  10383. (when org-trigger-hook
  10384. (save-excursion
  10385. (run-hook-with-args 'org-trigger-hook change-plist)))))))))
  10386. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  10387. "Block turning an entry into a TODO, using the hierarchy.
  10388. This checks whether the current task should be blocked from state
  10389. changes. Such blocking occurs when:
  10390. 1. The task has children which are not all in a completed state.
  10391. 2. A task has a parent with the property :ORDERED:, and there
  10392. are siblings prior to the current task with incomplete
  10393. status.
  10394. 3. The parent of the task is blocked because it has siblings that should
  10395. be done first, or is child of a block grandparent TODO entry."
  10396. (if (not org-enforce-todo-dependencies)
  10397. t ; if locally turned off don't block
  10398. (catch 'dont-block
  10399. ;; If this is not a todo state change, or if this entry is already DONE,
  10400. ;; do not block
  10401. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10402. (member (plist-get change-plist :from)
  10403. (cons 'done org-done-keywords))
  10404. (member (plist-get change-plist :to)
  10405. (cons 'todo org-not-done-keywords))
  10406. (not (plist-get change-plist :to)))
  10407. (throw 'dont-block t))
  10408. ;; If this task has children, and any are undone, it's blocked
  10409. (save-excursion
  10410. (org-back-to-heading t)
  10411. (let ((this-level (funcall outline-level)))
  10412. (outline-next-heading)
  10413. (let ((child-level (funcall outline-level)))
  10414. (while (and (not (eobp))
  10415. (> child-level this-level))
  10416. ;; this todo has children, check whether they are all
  10417. ;; completed
  10418. (if (and (not (org-entry-is-done-p))
  10419. (org-entry-is-todo-p))
  10420. (throw 'dont-block nil))
  10421. (outline-next-heading)
  10422. (setq child-level (funcall outline-level))))))
  10423. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  10424. ;; any previous siblings are undone, it's blocked
  10425. (save-excursion
  10426. (org-back-to-heading t)
  10427. (let* ((pos (point))
  10428. (parent-pos (and (org-up-heading-safe) (point))))
  10429. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10430. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10431. (forward-line 1)
  10432. (re-search-forward org-not-done-heading-regexp pos t))
  10433. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  10434. ;; Search further up the hierarchy, to see if an ancestor is blocked
  10435. (while t
  10436. (goto-char parent-pos)
  10437. (if (not (looking-at org-not-done-heading-regexp))
  10438. (throw 'dont-block t)) ; do not block, parent is not a TODO
  10439. (setq pos (point))
  10440. (setq parent-pos (and (org-up-heading-safe) (point)))
  10441. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10442. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10443. (forward-line 1)
  10444. (re-search-forward org-not-done-heading-regexp pos t))
  10445. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  10446. (defcustom org-track-ordered-property-with-tag nil
  10447. "Should the ORDERED property also be shown as a tag?
  10448. The ORDERED property decides if an entry should require subtasks to be
  10449. completed in sequence. Since a property is not very visible, setting
  10450. this option means that toggling the ORDERED property with the command
  10451. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  10452. not relevant for the behavior, but it makes things more visible.
  10453. Note that toggling the tag with tags commands will not change the property
  10454. and therefore not influence behavior!
  10455. This can be t, meaning the tag ORDERED should be used, It can also be a
  10456. string to select a different tag for this task."
  10457. :group 'org-todo
  10458. :type '(choice
  10459. (const :tag "No tracking" nil)
  10460. (const :tag "Track with ORDERED tag" t)
  10461. (string :tag "Use other tag")))
  10462. (defun org-toggle-ordered-property ()
  10463. "Toggle the ORDERED property of the current entry.
  10464. For better visibility, you can track the value of this property with a tag.
  10465. See variable `org-track-ordered-property-with-tag'."
  10466. (interactive)
  10467. (let* ((t1 org-track-ordered-property-with-tag)
  10468. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  10469. (save-excursion
  10470. (org-back-to-heading)
  10471. (if (org-entry-get nil "ORDERED")
  10472. (progn
  10473. (org-delete-property "ORDERED")
  10474. (and tag (org-toggle-tag tag 'off))
  10475. (message "Subtasks can be completed in arbitrary order"))
  10476. (org-entry-put nil "ORDERED" "t")
  10477. (and tag (org-toggle-tag tag 'on))
  10478. (message "Subtasks must be completed in sequence")))))
  10479. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  10480. (defun org-block-todo-from-checkboxes (change-plist)
  10481. "Block turning an entry into a TODO, using checkboxes.
  10482. This checks whether the current task should be blocked from state
  10483. changes because there are unchecked boxes in this entry."
  10484. (if (not org-enforce-todo-checkbox-dependencies)
  10485. t ; if locally turned off don't block
  10486. (catch 'dont-block
  10487. ;; If this is not a todo state change, or if this entry is already DONE,
  10488. ;; do not block
  10489. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10490. (member (plist-get change-plist :from)
  10491. (cons 'done org-done-keywords))
  10492. (member (plist-get change-plist :to)
  10493. (cons 'todo org-not-done-keywords))
  10494. (not (plist-get change-plist :to)))
  10495. (throw 'dont-block t))
  10496. ;; If this task has checkboxes that are not checked, it's blocked
  10497. (save-excursion
  10498. (org-back-to-heading t)
  10499. (let ((beg (point)) end)
  10500. (outline-next-heading)
  10501. (setq end (point))
  10502. (goto-char beg)
  10503. (if (org-list-search-forward
  10504. (concat (org-item-beginning-re)
  10505. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  10506. "\\[[- ]\\]")
  10507. end t)
  10508. (progn
  10509. (if (boundp 'org-blocked-by-checkboxes)
  10510. (setq org-blocked-by-checkboxes t))
  10511. (throw 'dont-block nil)))))
  10512. t))) ; do not block
  10513. (defun org-entry-blocked-p ()
  10514. "Is the current entry blocked?"
  10515. (if (org-entry-get nil "NOBLOCKING")
  10516. nil ;; Never block this entry
  10517. (not
  10518. (run-hook-with-args-until-failure
  10519. 'org-blocker-hook
  10520. (list :type 'todo-state-change
  10521. :position (point)
  10522. :from 'todo
  10523. :to 'done)))))
  10524. (defun org-update-statistics-cookies (all)
  10525. "Update the statistics cookie, either from TODO or from checkboxes.
  10526. This should be called with the cursor in a line with a statistics cookie."
  10527. (interactive "P")
  10528. (if all
  10529. (progn
  10530. (org-update-checkbox-count 'all)
  10531. (org-map-entries 'org-update-parent-todo-statistics))
  10532. (if (not (org-at-heading-p))
  10533. (org-update-checkbox-count)
  10534. (let ((pos (move-marker (make-marker) (point)))
  10535. end l1 l2)
  10536. (ignore-errors (org-back-to-heading t))
  10537. (if (not (org-at-heading-p))
  10538. (org-update-checkbox-count)
  10539. (setq l1 (org-outline-level))
  10540. (setq end (save-excursion
  10541. (outline-next-heading)
  10542. (if (org-at-heading-p) (setq l2 (org-outline-level)))
  10543. (point)))
  10544. (if (and (save-excursion
  10545. (re-search-forward
  10546. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  10547. (not (save-excursion (re-search-forward
  10548. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  10549. (org-update-checkbox-count)
  10550. (if (and l2 (> l2 l1))
  10551. (progn
  10552. (goto-char end)
  10553. (org-update-parent-todo-statistics))
  10554. (goto-char pos)
  10555. (beginning-of-line 1)
  10556. (while (re-search-forward
  10557. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  10558. (point-at-eol) t)
  10559. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  10560. (goto-char pos)
  10561. (move-marker pos nil)))))
  10562. (defvar org-entry-property-inherited-from) ;; defined below
  10563. (defun org-update-parent-todo-statistics ()
  10564. "Update any statistics cookie in the parent of the current headline.
  10565. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  10566. the entire subtree and this will travel up the hierarchy and update
  10567. statistics everywhere."
  10568. (let* ((prop (save-excursion (org-up-heading-safe)
  10569. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  10570. (recursive (or (not org-hierarchical-todo-statistics)
  10571. (and prop (string-match "\\<recursive\\>" prop))))
  10572. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  10573. 0))
  10574. (first t)
  10575. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  10576. level ltoggle l1 new ndel
  10577. (cnt-all 0) (cnt-done 0) is-percent kwd
  10578. checkbox-beg ov ovs ove cookie-present)
  10579. (catch 'exit
  10580. (save-excursion
  10581. (beginning-of-line 1)
  10582. (setq ltoggle (funcall outline-level))
  10583. ;; Three situations are to consider:
  10584. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  10585. ;; to the top-level ancestor on the headline;
  10586. ;; 2. If parent has "recursive" property, repeat up to the
  10587. ;; headline setting that property, taking inheritance into
  10588. ;; account;
  10589. ;; 3. Else, move up to direct parent and proceed only once.
  10590. (while (and (setq level (org-up-heading-safe))
  10591. (or recursive first)
  10592. (>= (point) lim))
  10593. (setq first nil cookie-present nil)
  10594. (unless (and level
  10595. (not (string-match
  10596. "\\<checkbox\\>"
  10597. (downcase (or (org-entry-get nil "COOKIE_DATA")
  10598. "")))))
  10599. (throw 'exit nil))
  10600. (while (re-search-forward box-re (point-at-eol) t)
  10601. (setq cnt-all 0 cnt-done 0 cookie-present t)
  10602. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  10603. (save-match-data
  10604. (unless (outline-next-heading) (throw 'exit nil))
  10605. (while (and (looking-at org-complex-heading-regexp)
  10606. (> (setq l1 (length (match-string 1))) level))
  10607. (setq kwd (and (or recursive (= l1 ltoggle))
  10608. (match-string 2)))
  10609. (if (or (eq org-provide-todo-statistics 'all-headlines)
  10610. (and (listp org-provide-todo-statistics)
  10611. (or (member kwd org-provide-todo-statistics)
  10612. (member kwd org-done-keywords))))
  10613. (setq cnt-all (1+ cnt-all))
  10614. (if (eq org-provide-todo-statistics t)
  10615. (and kwd (setq cnt-all (1+ cnt-all)))))
  10616. (and (member kwd org-done-keywords)
  10617. (setq cnt-done (1+ cnt-done)))
  10618. (outline-next-heading)))
  10619. (setq new
  10620. (if is-percent
  10621. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  10622. (format "[%d/%d]" cnt-done cnt-all))
  10623. ndel (- (match-end 0) checkbox-beg))
  10624. ;; handle overlays when updating cookie from column view
  10625. (when (setq ov (car (overlays-at checkbox-beg)))
  10626. (setq ovs (overlay-start ov) ove (overlay-end ov))
  10627. (delete-overlay ov))
  10628. (goto-char checkbox-beg)
  10629. (insert new)
  10630. (delete-region (point) (+ (point) ndel))
  10631. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  10632. (when ov (move-overlay ov ovs ove)))
  10633. (when cookie-present
  10634. (run-hook-with-args 'org-after-todo-statistics-hook
  10635. cnt-done (- cnt-all cnt-done))))))
  10636. (run-hooks 'org-todo-statistics-hook)))
  10637. (defvar org-after-todo-statistics-hook nil
  10638. "Hook that is called after a TODO statistics cookie has been updated.
  10639. Each function is called with two arguments: the number of not-done entries
  10640. and the number of done entries.
  10641. For example, the following function, when added to this hook, will switch
  10642. an entry to DONE when all children are done, and back to TODO when new
  10643. entries are set to a TODO status. Note that this hook is only called
  10644. when there is a statistics cookie in the headline!
  10645. (defun org-summary-todo (n-done n-not-done)
  10646. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  10647. (let (org-log-done org-log-states) ; turn off logging
  10648. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  10649. ")
  10650. (defvar org-todo-statistics-hook nil
  10651. "Hook that is run whenever Org thinks TODO statistics should be updated.
  10652. This hook runs even if there is no statistics cookie present, in which case
  10653. `org-after-todo-statistics-hook' would not run.")
  10654. (defun org-todo-trigger-tag-changes (state)
  10655. "Apply the changes defined in `org-todo-state-tags-triggers'."
  10656. (let ((l org-todo-state-tags-triggers)
  10657. changes)
  10658. (when (or (not state) (equal state ""))
  10659. (setq changes (append changes (cdr (assoc "" l)))))
  10660. (when (and (stringp state) (> (length state) 0))
  10661. (setq changes (append changes (cdr (assoc state l)))))
  10662. (when (member state org-not-done-keywords)
  10663. (setq changes (append changes (cdr (assoc 'todo l)))))
  10664. (when (member state org-done-keywords)
  10665. (setq changes (append changes (cdr (assoc 'done l)))))
  10666. (dolist (c changes)
  10667. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  10668. (defun org-local-logging (value)
  10669. "Get logging settings from a property VALUE."
  10670. (let* (words w a)
  10671. ;; directly set the variables, they are already local.
  10672. (setq org-log-done nil
  10673. org-log-repeat nil
  10674. org-todo-log-states nil)
  10675. (setq words (org-split-string value))
  10676. (while (setq w (pop words))
  10677. (cond
  10678. ((setq a (assoc w org-startup-options))
  10679. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  10680. (set (nth 1 a) (nth 2 a))))
  10681. ((setq a (org-extract-log-state-settings w))
  10682. (and (member (car a) org-todo-keywords-1)
  10683. (push a org-todo-log-states)))))))
  10684. (defun org-get-todo-sequence-head (kwd)
  10685. "Return the head of the TODO sequence to which KWD belongs.
  10686. If KWD is not set, check if there is a text property remembering the
  10687. right sequence."
  10688. (let (p)
  10689. (cond
  10690. ((not kwd)
  10691. (or (get-text-property (point-at-bol) 'org-todo-head)
  10692. (progn
  10693. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  10694. nil (point-at-eol)))
  10695. (get-text-property p 'org-todo-head))))
  10696. ((not (member kwd org-todo-keywords-1))
  10697. (car org-todo-keywords-1))
  10698. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  10699. (defun org-fast-todo-selection ()
  10700. "Fast TODO keyword selection with single keys.
  10701. Returns the new TODO keyword, or nil if no state change should occur."
  10702. (let* ((fulltable org-todo-key-alist)
  10703. (done-keywords org-done-keywords) ;; needed for the faces.
  10704. (maxlen (apply 'max (mapcar
  10705. (lambda (x)
  10706. (if (stringp (car x)) (string-width (car x)) 0))
  10707. fulltable)))
  10708. (expert nil)
  10709. (fwidth (+ maxlen 3 1 3))
  10710. (ncol (/ (- (window-width) 4) fwidth))
  10711. tg cnt e c tbl
  10712. groups ingroup)
  10713. (save-excursion
  10714. (save-window-excursion
  10715. (if expert
  10716. (set-buffer (get-buffer-create " *Org todo*"))
  10717. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  10718. (erase-buffer)
  10719. (org-set-local 'org-done-keywords done-keywords)
  10720. (setq tbl fulltable cnt 0)
  10721. (while (setq e (pop tbl))
  10722. (cond
  10723. ((equal e '(:startgroup))
  10724. (push '() groups) (setq ingroup t)
  10725. (when (not (= cnt 0))
  10726. (setq cnt 0)
  10727. (insert "\n"))
  10728. (insert "{ "))
  10729. ((equal e '(:endgroup))
  10730. (setq ingroup nil cnt 0)
  10731. (insert "}\n"))
  10732. ((equal e '(:newline))
  10733. (when (not (= cnt 0))
  10734. (setq cnt 0)
  10735. (insert "\n")
  10736. (setq e (car tbl))
  10737. (while (equal (car tbl) '(:newline))
  10738. (insert "\n")
  10739. (setq tbl (cdr tbl)))))
  10740. (t
  10741. (setq tg (car e) c (cdr e))
  10742. (if ingroup (push tg (car groups)))
  10743. (setq tg (org-add-props tg nil 'face
  10744. (org-get-todo-face tg)))
  10745. (if (and (= cnt 0) (not ingroup)) (insert " "))
  10746. (insert "[" c "] " tg (make-string
  10747. (- fwidth 4 (length tg)) ?\ ))
  10748. (when (= (setq cnt (1+ cnt)) ncol)
  10749. (insert "\n")
  10750. (if ingroup (insert " "))
  10751. (setq cnt 0)))))
  10752. (insert "\n")
  10753. (goto-char (point-min))
  10754. (if (not expert) (org-fit-window-to-buffer))
  10755. (message "[a-z..]:Set [SPC]:clear")
  10756. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  10757. (cond
  10758. ((or (= c ?\C-g)
  10759. (and (= c ?q) (not (rassoc c fulltable))))
  10760. (setq quit-flag t))
  10761. ((= c ?\ ) nil)
  10762. ((setq e (rassoc c fulltable) tg (car e))
  10763. tg)
  10764. (t (setq quit-flag t)))))))
  10765. (defun org-entry-is-todo-p ()
  10766. (member (org-get-todo-state) org-not-done-keywords))
  10767. (defun org-entry-is-done-p ()
  10768. (member (org-get-todo-state) org-done-keywords))
  10769. (defun org-get-todo-state ()
  10770. (save-excursion
  10771. (org-back-to-heading t)
  10772. (and (looking-at org-todo-line-regexp)
  10773. (match-end 2)
  10774. (match-string 2))))
  10775. (defun org-at-date-range-p (&optional inactive-ok)
  10776. "Is the cursor inside a date range?"
  10777. (interactive)
  10778. (save-excursion
  10779. (catch 'exit
  10780. (let ((pos (point)))
  10781. (skip-chars-backward "^[<\r\n")
  10782. (skip-chars-backward "<[")
  10783. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  10784. (>= (match-end 0) pos)
  10785. (throw 'exit t))
  10786. (skip-chars-backward "^<[\r\n")
  10787. (skip-chars-backward "<[")
  10788. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  10789. (>= (match-end 0) pos)
  10790. (throw 'exit t)))
  10791. nil)))
  10792. (defun org-get-repeat (&optional tagline)
  10793. "Check if there is a deadline/schedule with repeater in this entry."
  10794. (save-match-data
  10795. (save-excursion
  10796. (org-back-to-heading t)
  10797. (and (re-search-forward (if tagline
  10798. (concat tagline "\\s-*" org-repeat-re)
  10799. org-repeat-re)
  10800. (org-entry-end-position) t)
  10801. (match-string-no-properties 1)))))
  10802. (defvar org-last-changed-timestamp)
  10803. (defvar org-last-inserted-timestamp)
  10804. (defvar org-log-post-message)
  10805. (defvar org-log-note-purpose)
  10806. (defvar org-log-note-how)
  10807. (defvar org-log-note-extra)
  10808. (defun org-auto-repeat-maybe (done-word)
  10809. "Check if the current headline contains a repeated deadline/schedule.
  10810. If yes, set TODO state back to what it was and change the base date
  10811. of repeating deadline/scheduled time stamps to new date.
  10812. This function is run automatically after each state change to a DONE state."
  10813. ;; last-state is dynamically scoped into this function
  10814. (let* ((repeat (org-get-repeat))
  10815. (aa (assoc org-last-state org-todo-kwd-alist))
  10816. (interpret (nth 1 aa))
  10817. (head (nth 2 aa))
  10818. (whata '(("d" . day) ("m" . month) ("y" . year)))
  10819. (msg "Entry repeats: ")
  10820. (org-log-done nil)
  10821. (org-todo-log-states nil)
  10822. re type n what ts time to-state)
  10823. (when repeat
  10824. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  10825. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  10826. org-todo-repeat-to-state))
  10827. (unless (and to-state (member to-state org-todo-keywords-1))
  10828. (setq to-state (if (eq interpret 'type) org-last-state head)))
  10829. (org-todo to-state)
  10830. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  10831. (org-entry-put nil "LAST_REPEAT" (format-time-string
  10832. (org-time-stamp-format t t))))
  10833. (when org-log-repeat
  10834. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  10835. (memq 'org-add-log-note post-command-hook))
  10836. ;; OK, we are already setup for some record
  10837. (if (eq org-log-repeat 'note)
  10838. ;; make sure we take a note, not only a time stamp
  10839. (setq org-log-note-how 'note))
  10840. ;; Set up for taking a record
  10841. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  10842. org-last-state
  10843. 'findpos org-log-repeat)))
  10844. (org-back-to-heading t)
  10845. (org-add-planning-info nil nil 'closed)
  10846. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  10847. org-deadline-time-regexp "\\)\\|\\("
  10848. org-ts-regexp "\\)"))
  10849. (while (re-search-forward
  10850. re (save-excursion (outline-next-heading) (point)) t)
  10851. (setq type (if (match-end 1) org-scheduled-string
  10852. (if (match-end 3) org-deadline-string "Plain:"))
  10853. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  10854. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  10855. (setq n (string-to-number (match-string 2 ts))
  10856. what (match-string 3 ts))
  10857. (if (equal what "w") (setq n (* n 7) what "d"))
  10858. ;; Preparation, see if we need to modify the start date for the change
  10859. (when (match-end 1)
  10860. (setq time (save-match-data (org-time-string-to-time ts)))
  10861. (cond
  10862. ((equal (match-string 1 ts) ".")
  10863. ;; Shift starting date to today
  10864. (org-timestamp-change
  10865. (- (org-today) (time-to-days time))
  10866. 'day))
  10867. ((equal (match-string 1 ts) "+")
  10868. (let ((nshiftmax 10) (nshift 0))
  10869. (while (or (= nshift 0)
  10870. (<= (time-to-days time)
  10871. (time-to-days (current-time))))
  10872. (when (= (incf nshift) nshiftmax)
  10873. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  10874. (error "Abort")))
  10875. (org-timestamp-change n (cdr (assoc what whata)))
  10876. (org-at-timestamp-p t)
  10877. (setq ts (match-string 1))
  10878. (setq time (save-match-data (org-time-string-to-time ts)))))
  10879. (org-timestamp-change (- n) (cdr (assoc what whata)))
  10880. ;; rematch, so that we have everything in place for the real shift
  10881. (org-at-timestamp-p t)
  10882. (setq ts (match-string 1))
  10883. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  10884. (org-timestamp-change n (cdr (assoc what whata)))
  10885. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  10886. (setq org-log-post-message msg)
  10887. (message "%s" msg))))
  10888. (defun org-show-todo-tree (arg)
  10889. "Make a compact tree which shows all headlines marked with TODO.
  10890. The tree will show the lines where the regexp matches, and all higher
  10891. headlines above the match.
  10892. With a \\[universal-argument] prefix, prompt for a regexp to match.
  10893. With a numeric prefix N, construct a sparse tree for the Nth element
  10894. of `org-todo-keywords-1'."
  10895. (interactive "P")
  10896. (let ((case-fold-search nil)
  10897. (kwd-re
  10898. (cond ((null arg) org-not-done-regexp)
  10899. ((equal arg '(4))
  10900. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  10901. (mapcar 'list org-todo-keywords-1))))
  10902. (concat "\\("
  10903. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  10904. "\\)\\>")))
  10905. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  10906. (regexp-quote (nth (1- (prefix-numeric-value arg))
  10907. org-todo-keywords-1)))
  10908. (t (error "Invalid prefix argument: %s" arg)))))
  10909. (message "%d TODO entries found"
  10910. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  10911. (defun org-deadline (&optional remove time)
  10912. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  10913. With argument REMOVE, remove any deadline from the item.
  10914. With argument TIME, set the deadline at the corresponding date. TIME
  10915. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  10916. (interactive "P")
  10917. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  10918. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  10919. 'region-start-level 'region))
  10920. org-loop-over-headlines-in-active-region)
  10921. (org-map-entries
  10922. `(org-deadline ',remove ,time)
  10923. org-loop-over-headlines-in-active-region
  10924. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  10925. (let* ((old-date (org-entry-get nil "DEADLINE"))
  10926. (repeater (and old-date
  10927. (string-match
  10928. "\\([.+-]+[0-9]+[dwmy]\\(?:[/ ][-+]?[0-9]+[dwmy]\\)?\\) ?"
  10929. old-date)
  10930. (match-string 1 old-date))))
  10931. (if remove
  10932. (progn
  10933. (when (and old-date org-log-redeadline)
  10934. (org-add-log-setup 'deldeadline nil old-date 'findpos
  10935. org-log-redeadline))
  10936. (org-remove-timestamp-with-keyword org-deadline-string)
  10937. (message "Item no longer has a deadline."))
  10938. (org-add-planning-info 'deadline time 'closed)
  10939. (when (and old-date org-log-redeadline
  10940. (not (equal old-date
  10941. (substring org-last-inserted-timestamp 1 -1))))
  10942. (org-add-log-setup 'redeadline nil old-date 'findpos
  10943. org-log-redeadline))
  10944. (when repeater
  10945. (save-excursion
  10946. (org-back-to-heading t)
  10947. (when (re-search-forward (concat org-deadline-string " "
  10948. org-last-inserted-timestamp)
  10949. (save-excursion
  10950. (outline-next-heading) (point)) t)
  10951. (goto-char (1- (match-end 0)))
  10952. (insert " " repeater)
  10953. (setq org-last-inserted-timestamp
  10954. (concat (substring org-last-inserted-timestamp 0 -1)
  10955. " " repeater
  10956. (substring org-last-inserted-timestamp -1))))))
  10957. (message "Deadline on %s" org-last-inserted-timestamp)))))
  10958. (defun org-schedule (&optional remove time)
  10959. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  10960. With argument REMOVE, remove any scheduling date from the item.
  10961. With argument TIME, scheduled at the corresponding date. TIME can
  10962. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  10963. (interactive "P")
  10964. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  10965. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  10966. 'region-start-level 'region))
  10967. org-loop-over-headlines-in-active-region)
  10968. (org-map-entries
  10969. `(org-schedule ',remove ,time)
  10970. org-loop-over-headlines-in-active-region
  10971. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  10972. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  10973. (repeater (and old-date
  10974. (string-match
  10975. "\\([.+-]+[0-9]+[dwmy]\\(?:[/ ][-+]?[0-9]+[dwmy]\\)?\\) ?"
  10976. old-date)
  10977. (match-string 1 old-date))))
  10978. (if remove
  10979. (progn
  10980. (when (and old-date org-log-reschedule)
  10981. (org-add-log-setup 'delschedule nil old-date 'findpos
  10982. org-log-reschedule))
  10983. (org-remove-timestamp-with-keyword org-scheduled-string)
  10984. (message "Item is no longer scheduled."))
  10985. (org-add-planning-info 'scheduled time 'closed)
  10986. (when (and old-date org-log-reschedule
  10987. (not (equal old-date
  10988. (substring org-last-inserted-timestamp 1 -1))))
  10989. (org-add-log-setup 'reschedule nil old-date 'findpos
  10990. org-log-reschedule))
  10991. (when repeater
  10992. (save-excursion
  10993. (org-back-to-heading t)
  10994. (when (re-search-forward (concat org-scheduled-string " "
  10995. org-last-inserted-timestamp)
  10996. (save-excursion
  10997. (outline-next-heading) (point)) t)
  10998. (goto-char (1- (match-end 0)))
  10999. (insert " " repeater)
  11000. (setq org-last-inserted-timestamp
  11001. (concat (substring org-last-inserted-timestamp 0 -1)
  11002. " " repeater
  11003. (substring org-last-inserted-timestamp -1))))))
  11004. (message "Scheduled to %s" org-last-inserted-timestamp)))))
  11005. (defun org-get-scheduled-time (pom &optional inherit)
  11006. "Get the scheduled time as a time tuple, of a format suitable
  11007. for calling org-schedule with, or if there is no scheduling,
  11008. returns nil."
  11009. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  11010. (when time
  11011. (apply 'encode-time (org-parse-time-string time)))))
  11012. (defun org-get-deadline-time (pom &optional inherit)
  11013. "Get the deadline as a time tuple, of a format suitable for
  11014. calling org-deadline with, or if there is no scheduling, returns
  11015. nil."
  11016. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  11017. (when time
  11018. (apply 'encode-time (org-parse-time-string time)))))
  11019. (defun org-remove-timestamp-with-keyword (keyword)
  11020. "Remove all time stamps with KEYWORD in the current entry."
  11021. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  11022. beg)
  11023. (save-excursion
  11024. (org-back-to-heading t)
  11025. (setq beg (point))
  11026. (outline-next-heading)
  11027. (while (re-search-backward re beg t)
  11028. (replace-match "")
  11029. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  11030. (equal (char-before) ?\ ))
  11031. (backward-delete-char 1)
  11032. (if (string-match "^[ \t]*$" (buffer-substring
  11033. (point-at-bol) (point-at-eol)))
  11034. (delete-region (point-at-bol)
  11035. (min (point-max) (1+ (point-at-eol))))))))))
  11036. (defun org-add-planning-info (what &optional time &rest remove)
  11037. "Insert new timestamp with keyword in the line directly after the headline.
  11038. WHAT indicates what kind of time stamp to add. TIME indicates the time to use.
  11039. If non is given, the user is prompted for a date.
  11040. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  11041. be removed."
  11042. (interactive)
  11043. (let (org-time-was-given org-end-time-was-given ts
  11044. end default-time default-input)
  11045. (catch 'exit
  11046. (when (and (memq what '(scheduled deadline))
  11047. (or (not time)
  11048. (and (stringp time)
  11049. (string-match "^[-+]+[0-9]" time))))
  11050. ;; Try to get a default date/time from existing timestamp
  11051. (save-excursion
  11052. (org-back-to-heading t)
  11053. (setq end (save-excursion (outline-next-heading) (point)))
  11054. (when (re-search-forward (if (eq what 'scheduled)
  11055. org-scheduled-time-regexp
  11056. org-deadline-time-regexp)
  11057. end t)
  11058. (setq ts (match-string 1)
  11059. default-time
  11060. (apply 'encode-time (org-parse-time-string ts))
  11061. default-input (and ts (org-get-compact-tod ts))))))
  11062. (when what
  11063. (setq time
  11064. (if (stringp time)
  11065. ;; This is a string (relative or absolute), set proper date
  11066. (apply 'encode-time
  11067. (org-read-date-analyze
  11068. time default-time (decode-time default-time)))
  11069. ;; If necessary, get the time from the user
  11070. (or time (org-read-date nil 'to-time nil nil
  11071. default-time default-input)))))
  11072. (when (and org-insert-labeled-timestamps-at-point
  11073. (member what '(scheduled deadline)))
  11074. (insert
  11075. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  11076. (org-insert-time-stamp time org-time-was-given
  11077. nil nil nil (list org-end-time-was-given))
  11078. (setq what nil))
  11079. (save-excursion
  11080. (save-restriction
  11081. (let (col list elt ts buffer-invisibility-spec)
  11082. (org-back-to-heading t)
  11083. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
  11084. (goto-char (match-end 1))
  11085. (setq col (current-column))
  11086. (goto-char (match-end 0))
  11087. (if (eobp) (insert "\n") (forward-char 1))
  11088. (when (and (not what)
  11089. (not (looking-at
  11090. (concat "[ \t]*"
  11091. org-keyword-time-not-clock-regexp))))
  11092. ;; Nothing to add, nothing to remove...... :-)
  11093. (throw 'exit nil))
  11094. (if (and (not (looking-at org-outline-regexp))
  11095. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  11096. "[^\r\n]*"))
  11097. (not (equal (match-string 1) org-clock-string)))
  11098. (narrow-to-region (match-beginning 0) (match-end 0))
  11099. (insert-before-markers "\n")
  11100. (backward-char 1)
  11101. (narrow-to-region (point) (point))
  11102. (and org-adapt-indentation (org-indent-to-column col)))
  11103. ;; Check if we have to remove something.
  11104. (setq list (cons what remove))
  11105. (while list
  11106. (setq elt (pop list))
  11107. (when (or (and (eq elt 'scheduled)
  11108. (re-search-forward org-scheduled-time-regexp nil t))
  11109. (and (eq elt 'deadline)
  11110. (re-search-forward org-deadline-time-regexp nil t))
  11111. (and (eq elt 'closed)
  11112. (re-search-forward org-closed-time-regexp nil t)))
  11113. (replace-match "")
  11114. (if (looking-at "--+<[^>]+>") (replace-match ""))))
  11115. (and (looking-at "[ \t]+") (replace-match ""))
  11116. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  11117. (when what
  11118. (insert
  11119. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  11120. (cond ((eq what 'scheduled) org-scheduled-string)
  11121. ((eq what 'deadline) org-deadline-string)
  11122. ((eq what 'closed) org-closed-string))
  11123. " ")
  11124. (setq ts (org-insert-time-stamp
  11125. time
  11126. (or org-time-was-given
  11127. (and (eq what 'closed) org-log-done-with-time))
  11128. (eq what 'closed)
  11129. nil nil (list org-end-time-was-given)))
  11130. (insert
  11131. (if (not (or (bolp) (eq (char-before) ?\ )
  11132. (memq (char-after) '(32 10))
  11133. (eobp))) " " ""))
  11134. (end-of-line 1))
  11135. (goto-char (point-min))
  11136. (widen)
  11137. (if (and (looking-at "[ \t]*\n")
  11138. (equal (char-before) ?\n))
  11139. (delete-region (1- (point)) (point-at-eol)))
  11140. ts))))))
  11141. (defvar org-log-note-marker (make-marker))
  11142. (defvar org-log-note-purpose nil)
  11143. (defvar org-log-note-state nil)
  11144. (defvar org-log-note-previous-state nil)
  11145. (defvar org-log-note-how nil)
  11146. (defvar org-log-note-extra nil)
  11147. (defvar org-log-note-window-configuration nil)
  11148. (defvar org-log-note-return-to (make-marker))
  11149. (defvar org-log-note-effective-time nil
  11150. "Remembered current time so that dynamically scoped
  11151. `org-extend-today-until' affects tha timestamps in state change
  11152. log")
  11153. (defvar org-log-post-message nil
  11154. "Message to be displayed after a log note has been stored.
  11155. The auto-repeater uses this.")
  11156. (defun org-add-note ()
  11157. "Add a note to the current entry.
  11158. This is done in the same way as adding a state change note."
  11159. (interactive)
  11160. (org-add-log-setup 'note nil nil 'findpos nil))
  11161. (defvar org-property-end-re)
  11162. (defun org-add-log-setup (&optional purpose state prev-state
  11163. findpos how extra)
  11164. "Set up the post command hook to take a note.
  11165. If this is about to TODO state change, the new state is expected in STATE.
  11166. When FINDPOS is non-nil, find the correct position for the note in
  11167. the current entry. If not, assume that it can be inserted at point.
  11168. HOW is an indicator what kind of note should be created.
  11169. EXTRA is additional text that will be inserted into the notes buffer."
  11170. (let* ((org-log-into-drawer (org-log-into-drawer))
  11171. (drawer (cond ((stringp org-log-into-drawer)
  11172. org-log-into-drawer)
  11173. (org-log-into-drawer "LOGBOOK")
  11174. (t nil))))
  11175. (save-restriction
  11176. (save-excursion
  11177. (when findpos
  11178. (org-back-to-heading t)
  11179. (narrow-to-region (point) (save-excursion
  11180. (outline-next-heading) (point)))
  11181. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"
  11182. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  11183. "[^\r\n]*\\)?"))
  11184. (goto-char (match-end 0))
  11185. (cond
  11186. (drawer
  11187. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  11188. nil t)
  11189. (progn
  11190. (goto-char (match-end 0))
  11191. (or org-log-states-order-reversed
  11192. (and (re-search-forward org-property-end-re nil t)
  11193. (goto-char (1- (match-beginning 0))))))
  11194. (insert "\n:" drawer ":\n:END:")
  11195. (beginning-of-line 0)
  11196. (org-indent-line-function)
  11197. (beginning-of-line 2)
  11198. (org-indent-line-function)
  11199. (end-of-line 0)))
  11200. ((and org-log-state-notes-insert-after-drawers
  11201. (save-excursion
  11202. (forward-line) (looking-at org-drawer-regexp)))
  11203. (forward-line)
  11204. (while (looking-at org-drawer-regexp)
  11205. (goto-char (match-end 0))
  11206. (re-search-forward org-property-end-re (point-max) t)
  11207. (forward-line))
  11208. (forward-line -1)))
  11209. (unless org-log-states-order-reversed
  11210. (and (= (char-after) ?\n) (forward-char 1))
  11211. (org-skip-over-state-notes)
  11212. (skip-chars-backward " \t\n\r")))
  11213. (move-marker org-log-note-marker (point))
  11214. (setq org-log-note-purpose purpose
  11215. org-log-note-state state
  11216. org-log-note-previous-state prev-state
  11217. org-log-note-how how
  11218. org-log-note-extra extra
  11219. org-log-note-effective-time (org-current-effective-time))
  11220. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  11221. (defun org-skip-over-state-notes ()
  11222. "Skip past the list of State notes in an entry."
  11223. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  11224. (when (ignore-errors (goto-char (org-in-item-p)))
  11225. (let* ((struct (org-list-struct))
  11226. (prevs (org-list-prevs-alist struct)))
  11227. (while (looking-at "[ \t]*- State")
  11228. (goto-char (or (org-list-get-next-item (point) struct prevs)
  11229. (org-list-get-item-end (point) struct)))))))
  11230. (defun org-add-log-note (&optional purpose)
  11231. "Pop up a window for taking a note, and add this note later at point."
  11232. (remove-hook 'post-command-hook 'org-add-log-note)
  11233. (setq org-log-note-window-configuration (current-window-configuration))
  11234. (delete-other-windows)
  11235. (move-marker org-log-note-return-to (point))
  11236. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  11237. (goto-char org-log-note-marker)
  11238. (org-switch-to-buffer-other-window "*Org Note*")
  11239. (erase-buffer)
  11240. (if (memq org-log-note-how '(time state))
  11241. (let (current-prefix-arg) (org-store-log-note))
  11242. (let ((org-inhibit-startup t)) (org-mode))
  11243. (insert (format "# Insert note for %s.
  11244. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  11245. (cond
  11246. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  11247. ((eq org-log-note-purpose 'done) "closed todo item")
  11248. ((eq org-log-note-purpose 'state)
  11249. (format "state change from \"%s\" to \"%s\""
  11250. (or org-log-note-previous-state "")
  11251. (or org-log-note-state "")))
  11252. ((eq org-log-note-purpose 'reschedule)
  11253. "rescheduling")
  11254. ((eq org-log-note-purpose 'delschedule)
  11255. "no longer scheduled")
  11256. ((eq org-log-note-purpose 'redeadline)
  11257. "changing deadline")
  11258. ((eq org-log-note-purpose 'deldeadline)
  11259. "removing deadline")
  11260. ((eq org-log-note-purpose 'refile)
  11261. "refiling")
  11262. ((eq org-log-note-purpose 'note)
  11263. "this entry")
  11264. (t (error "This should not happen")))))
  11265. (if org-log-note-extra (insert org-log-note-extra))
  11266. (org-set-local 'org-finish-function 'org-store-log-note)
  11267. (run-hooks 'org-log-buffer-setup-hook)))
  11268. (defvar org-note-abort nil) ; dynamically scoped
  11269. (defun org-store-log-note ()
  11270. "Finish taking a log note, and insert it to where it belongs."
  11271. (let ((txt (buffer-string))
  11272. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  11273. lines ind bul)
  11274. (kill-buffer (current-buffer))
  11275. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  11276. (setq txt (replace-match "" t t txt)))
  11277. (if (string-match "\\s-+\\'" txt)
  11278. (setq txt (replace-match "" t t txt)))
  11279. (setq lines (org-split-string txt "\n"))
  11280. (when (and note (string-match "\\S-" note))
  11281. (setq note
  11282. (org-replace-escapes
  11283. note
  11284. (list (cons "%u" (user-login-name))
  11285. (cons "%U" user-full-name)
  11286. (cons "%t" (format-time-string
  11287. (org-time-stamp-format 'long 'inactive)
  11288. org-log-note-effective-time))
  11289. (cons "%T" (format-time-string
  11290. (org-time-stamp-format 'long nil)
  11291. org-log-note-effective-time))
  11292. (cons "%d" (format-time-string
  11293. (org-time-stamp-format nil 'inactive)
  11294. org-log-note-effective-time))
  11295. (cons "%D" (format-time-string
  11296. (org-time-stamp-format nil nil)
  11297. org-log-note-effective-time))
  11298. (cons "%s" (if org-log-note-state
  11299. (concat "\"" org-log-note-state "\"")
  11300. ""))
  11301. (cons "%S" (if org-log-note-previous-state
  11302. (concat "\"" org-log-note-previous-state "\"")
  11303. "\"\"")))))
  11304. (if lines (setq note (concat note " \\\\")))
  11305. (push note lines))
  11306. (when (or current-prefix-arg org-note-abort)
  11307. (when org-log-into-drawer
  11308. (org-remove-empty-drawer-at
  11309. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  11310. org-log-note-marker))
  11311. (setq lines nil))
  11312. (when lines
  11313. (with-current-buffer (marker-buffer org-log-note-marker)
  11314. (save-excursion
  11315. (goto-char org-log-note-marker)
  11316. (move-marker org-log-note-marker nil)
  11317. (end-of-line 1)
  11318. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  11319. (setq ind (save-excursion
  11320. (if (ignore-errors (goto-char (org-in-item-p)))
  11321. (let ((struct (org-list-struct)))
  11322. (org-list-get-ind
  11323. (org-list-get-top-point struct) struct))
  11324. (skip-chars-backward " \r\t\n")
  11325. (cond
  11326. ((and (org-at-heading-p)
  11327. org-adapt-indentation)
  11328. (1+ (org-current-level)))
  11329. ((org-at-heading-p) 0)
  11330. (t (org-get-indentation))))))
  11331. (setq bul (org-list-bullet-string "-"))
  11332. (org-indent-line-to ind)
  11333. (insert bul (pop lines))
  11334. (let ((ind-body (+ (length bul) ind)))
  11335. (while lines
  11336. (insert "\n")
  11337. (org-indent-line-to ind-body)
  11338. (insert (pop lines))))
  11339. (message "Note stored")
  11340. (org-back-to-heading t)
  11341. (org-cycle-hide-drawers 'children)))))
  11342. (set-window-configuration org-log-note-window-configuration)
  11343. (with-current-buffer (marker-buffer org-log-note-return-to)
  11344. (goto-char org-log-note-return-to))
  11345. (move-marker org-log-note-return-to nil)
  11346. (and org-log-post-message (message "%s" org-log-post-message)))
  11347. (defun org-remove-empty-drawer-at (drawer pos)
  11348. "Remove an empty drawer DRAWER at position POS.
  11349. POS may also be a marker."
  11350. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  11351. (save-excursion
  11352. (save-restriction
  11353. (widen)
  11354. (goto-char pos)
  11355. (if (org-in-regexp
  11356. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  11357. (replace-match ""))))))
  11358. (defun org-sparse-tree (&optional arg)
  11359. "Create a sparse tree, prompt for the details.
  11360. This command can create sparse trees. You first need to select the type
  11361. of match used to create the tree:
  11362. t Show all TODO entries.
  11363. T Show entries with a specific TODO keyword.
  11364. m Show entries selected by a tags/property match.
  11365. p Enter a property name and its value (both with completion on existing
  11366. names/values) and show entries with that property.
  11367. r Show entries matching a regular expression (`/' can be used as well)
  11368. d Show deadlines due within `org-deadline-warning-days'.
  11369. b Show deadlines and scheduled items before a date.
  11370. a Show deadlines and scheduled items after a date."
  11371. (interactive "P")
  11372. (let (ans kwd value)
  11373. (message "Sparse tree: [r]egexp [/]regexp [t]odo [T]odo-kwd [m]atch [p]roperty\n [d]eadlines [b]efore-date [a]fter-date [D]ates range")
  11374. (setq ans (read-char-exclusive))
  11375. (cond
  11376. ((equal ans ?d)
  11377. (call-interactively 'org-check-deadlines))
  11378. ((equal ans ?b)
  11379. (call-interactively 'org-check-before-date))
  11380. ((equal ans ?a)
  11381. (call-interactively 'org-check-after-date))
  11382. ((equal ans ?D)
  11383. (call-interactively 'org-check-dates-range))
  11384. ((equal ans ?t)
  11385. (org-show-todo-tree nil))
  11386. ((equal ans ?T)
  11387. (org-show-todo-tree '(4)))
  11388. ((member ans '(?T ?m))
  11389. (call-interactively 'org-match-sparse-tree))
  11390. ((member ans '(?p ?P))
  11391. (setq kwd (org-icompleting-read "Property: "
  11392. (mapcar 'list (org-buffer-property-keys))))
  11393. (setq value (org-icompleting-read "Value: "
  11394. (mapcar 'list (org-property-values kwd))))
  11395. (unless (string-match "\\`{.*}\\'" value)
  11396. (setq value (concat "\"" value "\"")))
  11397. (org-match-sparse-tree arg (concat kwd "=" value)))
  11398. ((member ans '(?r ?R ?/))
  11399. (call-interactively 'org-occur))
  11400. (t (error "No such sparse tree command \"%c\"" ans)))))
  11401. (defvar org-occur-highlights nil
  11402. "List of overlays used for occur matches.")
  11403. (make-variable-buffer-local 'org-occur-highlights)
  11404. (defvar org-occur-parameters nil
  11405. "Parameters of the active org-occur calls.
  11406. This is a list, each call to org-occur pushes as cons cell,
  11407. containing the regular expression and the callback, onto the list.
  11408. The list can contain several entries if `org-occur' has been called
  11409. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  11410. will only contain one set of parameters. When the highlights are
  11411. removed (for example with `C-c C-c', or with the next edit (depending
  11412. on `org-remove-highlights-with-change'), this variable is emptied
  11413. as well.")
  11414. (make-variable-buffer-local 'org-occur-parameters)
  11415. (defun org-occur (regexp &optional keep-previous callback)
  11416. "Make a compact tree which shows all matches of REGEXP.
  11417. The tree will show the lines where the regexp matches, and all higher
  11418. headlines above the match. It will also show the heading after the match,
  11419. to make sure editing the matching entry is easy.
  11420. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  11421. call to `org-occur' will be kept, to allow stacking of calls to this
  11422. command.
  11423. If CALLBACK is non-nil, it is a function which is called to confirm
  11424. that the match should indeed be shown."
  11425. (interactive "sRegexp: \nP")
  11426. (when (equal regexp "")
  11427. (error "Regexp cannot be empty"))
  11428. (unless keep-previous
  11429. (org-remove-occur-highlights nil nil t))
  11430. (push (cons regexp callback) org-occur-parameters)
  11431. (let ((cnt 0))
  11432. (save-excursion
  11433. (goto-char (point-min))
  11434. (if (or (not keep-previous) ; do not want to keep
  11435. (not org-occur-highlights)) ; no previous matches
  11436. ;; hide everything
  11437. (org-overview))
  11438. (while (re-search-forward regexp nil t)
  11439. (when (or (not callback)
  11440. (save-match-data (funcall callback)))
  11441. (setq cnt (1+ cnt))
  11442. (when org-highlight-sparse-tree-matches
  11443. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  11444. (org-show-context 'occur-tree))))
  11445. (when org-remove-highlights-with-change
  11446. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  11447. nil 'local))
  11448. (unless org-sparse-tree-open-archived-trees
  11449. (org-hide-archived-subtrees (point-min) (point-max)))
  11450. (run-hooks 'org-occur-hook)
  11451. (if (org-called-interactively-p 'interactive)
  11452. (message "%d match(es) for regexp %s" cnt regexp))
  11453. cnt))
  11454. (defun org-occur-next-match (&optional n reset)
  11455. "Function for `next-error-function' to find sparse tree matches.
  11456. N is the number of matches to move, when negative move backwards.
  11457. RESET is entirely ignored - this function always goes back to the
  11458. starting point when no match is found."
  11459. (let* ((limit (if (< n 0) (point-min) (point-max)))
  11460. (search-func (if (< n 0)
  11461. 'previous-single-char-property-change
  11462. 'next-single-char-property-change))
  11463. (n (abs n))
  11464. (pos (point))
  11465. p1)
  11466. (catch 'exit
  11467. (while (setq p1 (funcall search-func (point) 'org-type))
  11468. (when (equal p1 limit)
  11469. (goto-char pos)
  11470. (error "No more matches"))
  11471. (when (equal (get-char-property p1 'org-type) 'org-occur)
  11472. (setq n (1- n))
  11473. (when (= n 0)
  11474. (goto-char p1)
  11475. (throw 'exit (point))))
  11476. (goto-char p1))
  11477. (goto-char p1)
  11478. (error "No more matches"))))
  11479. (defun org-show-context (&optional key)
  11480. "Make sure point and context are visible.
  11481. How much context is shown depends upon the variables
  11482. `org-show-hierarchy-above', `org-show-following-heading',
  11483. `org-show-entry-below' and `org-show-siblings'."
  11484. (let ((heading-p (org-at-heading-p t))
  11485. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  11486. (following-p (org-get-alist-option org-show-following-heading key))
  11487. (entry-p (org-get-alist-option org-show-entry-below key))
  11488. (siblings-p (org-get-alist-option org-show-siblings key)))
  11489. (catch 'exit
  11490. ;; Show heading or entry text
  11491. (if (and heading-p (not entry-p))
  11492. (org-flag-heading nil) ; only show the heading
  11493. (and (or entry-p (outline-invisible-p) (org-invisible-p2))
  11494. (org-show-hidden-entry))) ; show entire entry
  11495. (when following-p
  11496. ;; Show next sibling, or heading below text
  11497. (save-excursion
  11498. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  11499. (org-flag-heading nil))))
  11500. (when siblings-p (org-show-siblings))
  11501. (when hierarchy-p
  11502. ;; show all higher headings, possibly with siblings
  11503. (save-excursion
  11504. (while (and (condition-case nil
  11505. (progn (org-up-heading-all 1) t)
  11506. (error nil))
  11507. (not (bobp)))
  11508. (org-flag-heading nil)
  11509. (when siblings-p (org-show-siblings))))))))
  11510. (defvar org-reveal-start-hook nil
  11511. "Hook run before revealing a location.")
  11512. (defun org-reveal (&optional siblings)
  11513. "Show current entry, hierarchy above it, and the following headline.
  11514. This can be used to show a consistent set of context around locations
  11515. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  11516. not t for the search context.
  11517. With optional argument SIBLINGS, on each level of the hierarchy all
  11518. siblings are shown. This repairs the tree structure to what it would
  11519. look like when opened with hierarchical calls to `org-cycle'.
  11520. With double optional argument \\[universal-argument] \\[universal-argument], \
  11521. go to the parent and show the
  11522. entire tree."
  11523. (interactive "P")
  11524. (run-hooks 'org-reveal-start-hook)
  11525. (let ((org-show-hierarchy-above t)
  11526. (org-show-following-heading t)
  11527. (org-show-siblings (if siblings t org-show-siblings)))
  11528. (org-show-context nil))
  11529. (when (equal siblings '(16))
  11530. (save-excursion
  11531. (when (org-up-heading-safe)
  11532. (org-show-subtree)
  11533. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  11534. (defun org-highlight-new-match (beg end)
  11535. "Highlight from BEG to END and mark the highlight is an occur headline."
  11536. (let ((ov (make-overlay beg end)))
  11537. (overlay-put ov 'face 'secondary-selection)
  11538. (overlay-put ov 'org-type 'org-occur)
  11539. (push ov org-occur-highlights)))
  11540. (defun org-remove-occur-highlights (&optional beg end noremove)
  11541. "Remove the occur highlights from the buffer.
  11542. BEG and END are ignored. If NOREMOVE is nil, remove this function
  11543. from the `before-change-functions' in the current buffer."
  11544. (interactive)
  11545. (unless org-inhibit-highlight-removal
  11546. (mapc 'delete-overlay org-occur-highlights)
  11547. (setq org-occur-highlights nil)
  11548. (setq org-occur-parameters nil)
  11549. (unless noremove
  11550. (remove-hook 'before-change-functions
  11551. 'org-remove-occur-highlights 'local))))
  11552. ;;;; Priorities
  11553. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  11554. "Regular expression matching the priority indicator.")
  11555. (defvar org-remove-priority-next-time nil)
  11556. (defun org-priority-up ()
  11557. "Increase the priority of the current item."
  11558. (interactive)
  11559. (org-priority 'up))
  11560. (defun org-priority-down ()
  11561. "Decrease the priority of the current item."
  11562. (interactive)
  11563. (org-priority 'down))
  11564. (defun org-priority (&optional action)
  11565. "Change the priority of an item by ARG.
  11566. ACTION can be `set', `up', `down', or a character."
  11567. (interactive)
  11568. (unless org-enable-priority-commands
  11569. (error "Priority commands are disabled"))
  11570. (setq action (or action 'set))
  11571. (let (current new news have remove)
  11572. (save-excursion
  11573. (org-back-to-heading t)
  11574. (if (looking-at org-priority-regexp)
  11575. (setq current (string-to-char (match-string 2))
  11576. have t))
  11577. (cond
  11578. ((eq action 'remove)
  11579. (setq remove t new ?\ ))
  11580. ((or (eq action 'set)
  11581. (if (featurep 'xemacs) (characterp action) (integerp action)))
  11582. (if (not (eq action 'set))
  11583. (setq new action)
  11584. (message "Priority %c-%c, SPC to remove: "
  11585. org-highest-priority org-lowest-priority)
  11586. (save-match-data
  11587. (setq new (read-char-exclusive))))
  11588. (if (and (= (upcase org-highest-priority) org-highest-priority)
  11589. (= (upcase org-lowest-priority) org-lowest-priority))
  11590. (setq new (upcase new)))
  11591. (cond ((equal new ?\ ) (setq remove t))
  11592. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  11593. (error "Priority must be between `%c' and `%c'"
  11594. org-highest-priority org-lowest-priority))))
  11595. ((eq action 'up)
  11596. (setq new (if have
  11597. (1- current) ; normal cycling
  11598. ;; last priority was empty
  11599. (if (eq last-command this-command)
  11600. org-lowest-priority ; wrap around empty to lowest
  11601. ;; default
  11602. (if org-priority-start-cycle-with-default
  11603. org-default-priority
  11604. (1- org-default-priority))))))
  11605. ((eq action 'down)
  11606. (setq new (if have
  11607. (1+ current) ; normal cycling
  11608. ;; last priority was empty
  11609. (if (eq last-command this-command)
  11610. org-highest-priority ; wrap around empty to highest
  11611. ;; default
  11612. (if org-priority-start-cycle-with-default
  11613. org-default-priority
  11614. (1+ org-default-priority))))))
  11615. (t (error "Invalid action")))
  11616. (if (or (< (upcase new) org-highest-priority)
  11617. (> (upcase new) org-lowest-priority))
  11618. (if (and (memq action '(up down))
  11619. (not have) (not (eq last-command this-command)))
  11620. ;; `new' is from default priority
  11621. (error
  11622. "The default can not be set, see `org-default-priority' why")
  11623. ;; normal cycling: `new' is beyond highest/lowest priority
  11624. ;; and is wrapped around to the empty priority
  11625. (setq remove t)))
  11626. (setq news (format "%c" new))
  11627. (if have
  11628. (if remove
  11629. (replace-match "" t t nil 1)
  11630. (replace-match news t t nil 2))
  11631. (if remove
  11632. (error "No priority cookie found in line")
  11633. (let ((case-fold-search nil))
  11634. (looking-at org-todo-line-regexp))
  11635. (if (match-end 2)
  11636. (progn
  11637. (goto-char (match-end 2))
  11638. (insert " [#" news "]"))
  11639. (goto-char (match-beginning 3))
  11640. (insert "[#" news "] "))))
  11641. (org-preserve-lc (org-set-tags nil 'align)))
  11642. (if remove
  11643. (message "Priority removed")
  11644. (message "Priority of current item set to %s" news))))
  11645. (defun org-get-priority (s)
  11646. "Find priority cookie and return priority."
  11647. (if (functionp org-get-priority-function)
  11648. (funcall org-get-priority-function)
  11649. (save-match-data
  11650. (if (not (string-match org-priority-regexp s))
  11651. (* 1000 (- org-lowest-priority org-default-priority))
  11652. (* 1000 (- org-lowest-priority
  11653. (string-to-char (match-string 2 s))))))))
  11654. ;;;; Tags
  11655. (defvar org-agenda-archives-mode)
  11656. (defvar org-map-continue-from nil
  11657. "Position from where mapping should continue.
  11658. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  11659. (defvar org-scanner-tags nil
  11660. "The current tag list while the tags scanner is running.")
  11661. (defvar org-trust-scanner-tags nil
  11662. "Should `org-get-tags-at' use the tags for the scanner.
  11663. This is for internal dynamical scoping only.
  11664. When this is non-nil, the function `org-get-tags-at' will return the value
  11665. of `org-scanner-tags' instead of building the list by itself. This
  11666. can lead to large speed-ups when the tags scanner is used in a file with
  11667. many entries, and when the list of tags is retrieved, for example to
  11668. obtain a list of properties. Building the tags list for each entry in such
  11669. a file becomes an N^2 operation - but with this variable set, it scales
  11670. as N.")
  11671. (defun org-scan-tags (action matcher todo-only &optional start-level)
  11672. "Scan headline tags with inheritance and produce output ACTION.
  11673. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  11674. or `agenda' to produce an entry list for an agenda view. It can also be
  11675. a Lisp form or a function that should be called at each matched headline, in
  11676. this case the return value is a list of all return values from these calls.
  11677. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  11678. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  11679. only lines with a not-done TODO keyword are included in the output.
  11680. This should be the same variable that was scoped into
  11681. and set by `org-make-tags-matcher' when it constructed MATCHER.
  11682. START-LEVEL can be a string with asterisks, reducing the scope to
  11683. headlines matching this string."
  11684. (require 'org-agenda)
  11685. (let* ((re (concat "^"
  11686. (if start-level
  11687. ;; Get the correct level to match
  11688. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  11689. org-outline-regexp)
  11690. " *\\(\\<\\("
  11691. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  11692. (org-re
  11693. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  11694. (props (list 'face 'default
  11695. 'done-face 'org-agenda-done
  11696. 'undone-face 'default
  11697. 'mouse-face 'highlight
  11698. 'org-not-done-regexp org-not-done-regexp
  11699. 'org-todo-regexp org-todo-regexp
  11700. 'org-complex-heading-regexp org-complex-heading-regexp
  11701. 'help-echo
  11702. (format "mouse-2 or RET jump to org file %s"
  11703. (abbreviate-file-name
  11704. (or (buffer-file-name (buffer-base-buffer))
  11705. (buffer-name (buffer-base-buffer)))))))
  11706. (case-fold-search nil)
  11707. (org-map-continue-from nil)
  11708. lspos tags tags-list
  11709. (tags-alist (list (cons 0 org-file-tags)))
  11710. (llast 0) rtn rtn1 level category i txt
  11711. todo marker entry priority)
  11712. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  11713. (setq action (list 'lambda nil action)))
  11714. (save-excursion
  11715. (goto-char (point-min))
  11716. (when (eq action 'sparse-tree)
  11717. (org-overview)
  11718. (org-remove-occur-highlights))
  11719. (while (re-search-forward re nil t)
  11720. (setq org-map-continue-from nil)
  11721. (catch :skip
  11722. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  11723. tags (if (match-end 4) (org-match-string-no-properties 4)))
  11724. (goto-char (setq lspos (match-beginning 0)))
  11725. (setq level (org-reduced-level (funcall outline-level))
  11726. category (org-get-category))
  11727. (setq i llast llast level)
  11728. ;; remove tag lists from same and sublevels
  11729. (while (>= i level)
  11730. (when (setq entry (assoc i tags-alist))
  11731. (setq tags-alist (delete entry tags-alist)))
  11732. (setq i (1- i)))
  11733. ;; add the next tags
  11734. (when tags
  11735. (setq tags (org-split-string tags ":")
  11736. tags-alist
  11737. (cons (cons level tags) tags-alist)))
  11738. ;; compile tags for current headline
  11739. (setq tags-list
  11740. (if org-use-tag-inheritance
  11741. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  11742. tags)
  11743. org-scanner-tags tags-list)
  11744. (when org-use-tag-inheritance
  11745. (setcdr (car tags-alist)
  11746. (mapcar (lambda (x)
  11747. (setq x (copy-sequence x))
  11748. (org-add-prop-inherited x))
  11749. (cdar tags-alist))))
  11750. (when (and tags org-use-tag-inheritance
  11751. (or (not (eq t org-use-tag-inheritance))
  11752. org-tags-exclude-from-inheritance))
  11753. ;; selective inheritance, remove uninherited ones
  11754. (setcdr (car tags-alist)
  11755. (org-remove-uninherited-tags (cdar tags-alist))))
  11756. (when (and
  11757. ;; eval matcher only when the todo condition is OK
  11758. (and (or (not todo-only) (member todo org-not-done-keywords))
  11759. (let ((case-fold-search t) (org-trust-scanner-tags t))
  11760. (eval matcher)))
  11761. ;; Call the skipper, but return t if it does not skip,
  11762. ;; so that the `and' form continues evaluating
  11763. (progn
  11764. (unless (eq action 'sparse-tree) (org-agenda-skip))
  11765. t)
  11766. ;; Check if timestamps are deselecting this entry
  11767. (or (not todo-only)
  11768. (and (member todo org-not-done-keywords)
  11769. (or (not org-agenda-tags-todo-honor-ignore-options)
  11770. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  11771. ;; Extra check for the archive tag
  11772. ;; FIXME: Does the skipper already do this????
  11773. (or
  11774. (not (member org-archive-tag tags-list))
  11775. ;; we have an archive tag, should we use this anyway?
  11776. (or (not org-agenda-skip-archived-trees)
  11777. (and (eq action 'agenda) org-agenda-archives-mode))))
  11778. ;; select this headline
  11779. (cond
  11780. ((eq action 'sparse-tree)
  11781. (and org-highlight-sparse-tree-matches
  11782. (org-get-heading) (match-end 0)
  11783. (org-highlight-new-match
  11784. (match-beginning 1) (match-end 1)))
  11785. (org-show-context 'tags-tree))
  11786. ((eq action 'agenda)
  11787. (setq txt (org-agenda-format-item
  11788. ""
  11789. (concat
  11790. (if (eq org-tags-match-list-sublevels 'indented)
  11791. (make-string (1- level) ?.) "")
  11792. (org-get-heading))
  11793. category
  11794. tags-list)
  11795. priority (org-get-priority txt))
  11796. (goto-char lspos)
  11797. (setq marker (org-agenda-new-marker))
  11798. (org-add-props txt props
  11799. 'org-marker marker 'org-hd-marker marker 'org-category category
  11800. 'todo-state todo
  11801. 'priority priority 'type "tagsmatch")
  11802. (push txt rtn))
  11803. ((functionp action)
  11804. (setq org-map-continue-from nil)
  11805. (save-excursion
  11806. (setq rtn1 (funcall action))
  11807. (push rtn1 rtn)))
  11808. (t (error "Invalid action")))
  11809. ;; if we are to skip sublevels, jump to end of subtree
  11810. (unless org-tags-match-list-sublevels
  11811. (org-end-of-subtree t)
  11812. (backward-char 1))))
  11813. ;; Get the correct position from where to continue
  11814. (if org-map-continue-from
  11815. (goto-char org-map-continue-from)
  11816. (and (= (point) lspos) (end-of-line 1)))))
  11817. (when (and (eq action 'sparse-tree)
  11818. (not org-sparse-tree-open-archived-trees))
  11819. (org-hide-archived-subtrees (point-min) (point-max)))
  11820. (nreverse rtn)))
  11821. (defun org-remove-uninherited-tags (tags)
  11822. "Remove all tags that are not inherited from the list TAGS."
  11823. (cond
  11824. ((eq org-use-tag-inheritance t)
  11825. (if org-tags-exclude-from-inheritance
  11826. (org-delete-all org-tags-exclude-from-inheritance tags)
  11827. tags))
  11828. ((not org-use-tag-inheritance) nil)
  11829. ((stringp org-use-tag-inheritance)
  11830. (delq nil (mapcar
  11831. (lambda (x)
  11832. (if (and (string-match org-use-tag-inheritance x)
  11833. (not (member x org-tags-exclude-from-inheritance)))
  11834. x nil))
  11835. tags)))
  11836. ((listp org-use-tag-inheritance)
  11837. (delq nil (mapcar
  11838. (lambda (x)
  11839. (if (member x org-use-tag-inheritance) x nil))
  11840. tags)))))
  11841. (defun org-match-sparse-tree (&optional todo-only match)
  11842. "Create a sparse tree according to tags string MATCH.
  11843. MATCH can contain positive and negative selection of tags, like
  11844. \"+WORK+URGENT-WITHBOSS\".
  11845. If optional argument TODO-ONLY is non-nil, only select lines that are
  11846. also TODO lines."
  11847. (interactive "P")
  11848. (org-prepare-agenda-buffers (list (current-buffer)))
  11849. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  11850. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  11851. (defvar org-cached-props nil)
  11852. (defun org-cached-entry-get (pom property)
  11853. (if (or (eq t org-use-property-inheritance)
  11854. (and (stringp org-use-property-inheritance)
  11855. (string-match org-use-property-inheritance property))
  11856. (and (listp org-use-property-inheritance)
  11857. (member property org-use-property-inheritance)))
  11858. ;; Caching is not possible, check it directly
  11859. (org-entry-get pom property 'inherit)
  11860. ;; Get all properties, so that we can do complicated checks easily
  11861. (cdr (assoc property (or org-cached-props
  11862. (setq org-cached-props
  11863. (org-entry-properties pom)))))))
  11864. (defun org-global-tags-completion-table (&optional files)
  11865. "Return the list of all tags in all agenda buffer/files.
  11866. Optional FILES argument is a list of files to which can be used
  11867. instead of the agenda files."
  11868. (save-excursion
  11869. (org-uniquify
  11870. (delq nil
  11871. (apply 'append
  11872. (mapcar
  11873. (lambda (file)
  11874. (set-buffer (find-file-noselect file))
  11875. (append (org-get-buffer-tags)
  11876. (mapcar (lambda (x) (if (stringp (car-safe x))
  11877. (list (car-safe x)) nil))
  11878. org-tag-alist)))
  11879. (if (and files (car files))
  11880. files
  11881. (org-agenda-files))))))))
  11882. (defun org-make-tags-matcher (match)
  11883. "Create the TAGS/TODO matcher form for the selection string MATCH.
  11884. The variable `todo-only' is scoped dynamically into this function; it will be
  11885. set to t if the matcher restricts matching to TODO entries,
  11886. otherwise will not be touched.
  11887. Returns a cons of the selection string MATCH and the constructed
  11888. lisp form implementing the matcher. The matcher is to be
  11889. evaluated at an Org entry, with point on the headline,
  11890. and returns t if the entry matches the
  11891. selection string MATCH. The returned lisp form references
  11892. two variables with information about the entry, which must be
  11893. bound around the form's evaluation: todo, the TODO keyword at the
  11894. entry (or nil of none); and tags-list, the list of all tags at the
  11895. entry including inherited ones. Additionally, the category
  11896. of the entry (if any) must be specified as the text property
  11897. 'org-category on the headline.
  11898. See also `org-scan-tags'.
  11899. "
  11900. (declare (special todo-only))
  11901. (unless (boundp 'todo-only)
  11902. (error "org-make-tags-matcher expects todo-only to be scoped in"))
  11903. (unless match
  11904. ;; Get a new match request, with completion
  11905. (let ((org-last-tags-completion-table
  11906. (org-global-tags-completion-table)))
  11907. (setq match (org-completing-read-no-i
  11908. "Match: " 'org-tags-completion-function nil nil nil
  11909. 'org-tags-history))))
  11910. ;; Parse the string and create a lisp form
  11911. (let ((match0 match)
  11912. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  11913. minus tag mm
  11914. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  11915. orterms term orlist re-p str-p level-p level-op time-p
  11916. prop-p pn pv po gv rest)
  11917. (if (string-match "/+" match)
  11918. ;; match contains also a todo-matching request
  11919. (progn
  11920. (setq tagsmatch (substring match 0 (match-beginning 0))
  11921. todomatch (substring match (match-end 0)))
  11922. (if (string-match "^!" todomatch)
  11923. (setq todo-only t todomatch (substring todomatch 1)))
  11924. (if (string-match "^\\s-*$" todomatch)
  11925. (setq todomatch nil)))
  11926. ;; only matching tags
  11927. (setq tagsmatch match todomatch nil))
  11928. ;; Make the tags matcher
  11929. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  11930. (setq tagsmatcher t)
  11931. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  11932. (while (setq term (pop orterms))
  11933. (while (and (equal (substring term -1) "\\") orterms)
  11934. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  11935. (while (string-match re term)
  11936. (setq rest (substring term (match-end 0))
  11937. minus (and (match-end 1)
  11938. (equal (match-string 1 term) "-"))
  11939. tag (save-match-data (replace-regexp-in-string
  11940. "\\\\-" "-"
  11941. (match-string 2 term)))
  11942. re-p (equal (string-to-char tag) ?{)
  11943. level-p (match-end 4)
  11944. prop-p (match-end 5)
  11945. mm (cond
  11946. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  11947. (level-p
  11948. (setq level-op (org-op-to-function (match-string 3 term)))
  11949. `(,level-op level ,(string-to-number
  11950. (match-string 4 term))))
  11951. (prop-p
  11952. (setq pn (match-string 5 term)
  11953. po (match-string 6 term)
  11954. pv (match-string 7 term)
  11955. re-p (equal (string-to-char pv) ?{)
  11956. str-p (equal (string-to-char pv) ?\")
  11957. time-p (save-match-data
  11958. (string-match "^\"[[<].*[]>]\"$" pv))
  11959. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  11960. (if time-p (setq pv (org-matcher-time pv)))
  11961. (setq po (org-op-to-function po (if time-p 'time str-p)))
  11962. (cond
  11963. ((equal pn "CATEGORY")
  11964. (setq gv '(get-text-property (point) 'org-category)))
  11965. ((equal pn "TODO")
  11966. (setq gv 'todo))
  11967. (t
  11968. (setq gv `(org-cached-entry-get nil ,pn))))
  11969. (if re-p
  11970. (if (eq po 'org<>)
  11971. `(not (string-match ,pv (or ,gv "")))
  11972. `(string-match ,pv (or ,gv "")))
  11973. (if str-p
  11974. `(,po (or ,gv "") ,pv)
  11975. `(,po (string-to-number (or ,gv ""))
  11976. ,(string-to-number pv) ))))
  11977. (t `(member ,tag tags-list)))
  11978. mm (if minus (list 'not mm) mm)
  11979. term rest)
  11980. (push mm tagsmatcher))
  11981. (push (if (> (length tagsmatcher) 1)
  11982. (cons 'and tagsmatcher)
  11983. (car tagsmatcher))
  11984. orlist)
  11985. (setq tagsmatcher nil))
  11986. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  11987. (setq tagsmatcher
  11988. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  11989. ;; Make the todo matcher
  11990. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  11991. (setq todomatcher t)
  11992. (setq orterms (org-split-string todomatch "|") orlist nil)
  11993. (while (setq term (pop orterms))
  11994. (while (string-match re term)
  11995. (setq minus (and (match-end 1)
  11996. (equal (match-string 1 term) "-"))
  11997. kwd (match-string 2 term)
  11998. re-p (equal (string-to-char kwd) ?{)
  11999. term (substring term (match-end 0))
  12000. mm (if re-p
  12001. `(string-match ,(substring kwd 1 -1) todo)
  12002. (list 'equal 'todo kwd))
  12003. mm (if minus (list 'not mm) mm))
  12004. (push mm todomatcher))
  12005. (push (if (> (length todomatcher) 1)
  12006. (cons 'and todomatcher)
  12007. (car todomatcher))
  12008. orlist)
  12009. (setq todomatcher nil))
  12010. (setq todomatcher (if (> (length orlist) 1)
  12011. (cons 'or orlist) (car orlist))))
  12012. ;; Return the string and lisp forms of the matcher
  12013. (setq matcher (if todomatcher
  12014. (list 'and tagsmatcher todomatcher)
  12015. tagsmatcher))
  12016. (when todo-only
  12017. (setq matcher (list 'and '(member todo org-not-done-keywords)
  12018. matcher)))
  12019. (cons match0 matcher)))
  12020. (defun org-op-to-function (op &optional stringp)
  12021. "Turn an operator into the appropriate function."
  12022. (setq op
  12023. (cond
  12024. ((equal op "<" ) '(< string< org-time<))
  12025. ((equal op ">" ) '(> org-string> org-time>))
  12026. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  12027. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  12028. ((member op '("=" "==")) '(= string= org-time=))
  12029. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  12030. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  12031. (defun org<> (a b) (not (= a b)))
  12032. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  12033. (defun org-string>= (a b) (not (string< a b)))
  12034. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  12035. (defun org-string<> (a b) (not (string= a b)))
  12036. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  12037. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  12038. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  12039. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  12040. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  12041. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  12042. (defun org-2ft (s)
  12043. "Convert S to a floating point time.
  12044. If S is already a number, just return it. If it is a string, parse
  12045. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  12046. (cond
  12047. ((numberp s) s)
  12048. ((stringp s)
  12049. (condition-case nil
  12050. (float-time (apply 'encode-time (org-parse-time-string s)))
  12051. (error 0.)))
  12052. (t 0.)))
  12053. (defun org-time-today ()
  12054. "Time in seconds today at 0:00.
  12055. Returns the float number of seconds since the beginning of the
  12056. epoch to the beginning of today (00:00)."
  12057. (float-time (apply 'encode-time
  12058. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  12059. (defun org-matcher-time (s)
  12060. "Interpret a time comparison value."
  12061. (save-match-data
  12062. (cond
  12063. ((string= s "<now>") (float-time))
  12064. ((string= s "<today>") (org-time-today))
  12065. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  12066. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  12067. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  12068. (+ (org-time-today)
  12069. (* (string-to-number (match-string 1 s))
  12070. (cdr (assoc (match-string 2 s)
  12071. '(("d" . 86400.0) ("w" . 604800.0)
  12072. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  12073. (t (org-2ft s)))))
  12074. (defun org-match-any-p (re list)
  12075. "Does re match any element of list?"
  12076. (setq list (mapcar (lambda (x) (string-match re x)) list))
  12077. (delq nil list))
  12078. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  12079. (defvar org-tags-overlay (make-overlay 1 1))
  12080. (org-detach-overlay org-tags-overlay)
  12081. (defun org-get-local-tags-at (&optional pos)
  12082. "Get a list of tags defined in the current headline."
  12083. (org-get-tags-at pos 'local))
  12084. (defun org-get-local-tags ()
  12085. "Get a list of tags defined in the current headline."
  12086. (org-get-tags-at nil 'local))
  12087. (defun org-get-tags-at (&optional pos local)
  12088. "Get a list of all headline tags applicable at POS.
  12089. POS defaults to point. If tags are inherited, the list contains
  12090. the targets in the same sequence as the headlines appear, i.e.
  12091. the tags of the current headline come last.
  12092. When LOCAL is non-nil, only return tags from the current headline,
  12093. ignore inherited ones."
  12094. (interactive)
  12095. (if (and org-trust-scanner-tags
  12096. (or (not pos) (equal pos (point)))
  12097. (not local))
  12098. org-scanner-tags
  12099. (let (tags ltags lastpos parent)
  12100. (save-excursion
  12101. (save-restriction
  12102. (widen)
  12103. (goto-char (or pos (point)))
  12104. (save-match-data
  12105. (catch 'done
  12106. (condition-case nil
  12107. (progn
  12108. (org-back-to-heading t)
  12109. (while (not (equal lastpos (point)))
  12110. (setq lastpos (point))
  12111. (when (looking-at
  12112. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  12113. (setq ltags (org-split-string
  12114. (org-match-string-no-properties 1) ":"))
  12115. (when parent
  12116. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  12117. (setq tags (append
  12118. (if parent
  12119. (org-remove-uninherited-tags ltags)
  12120. ltags)
  12121. tags)))
  12122. (or org-use-tag-inheritance (throw 'done t))
  12123. (if local (throw 'done t))
  12124. (or (org-up-heading-safe) (error nil))
  12125. (setq parent t)))
  12126. (error nil)))))
  12127. (if local
  12128. tags
  12129. (append (org-remove-uninherited-tags org-file-tags) tags))))))
  12130. (defun org-add-prop-inherited (s)
  12131. (add-text-properties 0 (length s) '(inherited t) s)
  12132. s)
  12133. (defun org-toggle-tag (tag &optional onoff)
  12134. "Toggle the tag TAG for the current line.
  12135. If ONOFF is `on' or `off', don't toggle but set to this state."
  12136. (let (res current)
  12137. (save-excursion
  12138. (org-back-to-heading t)
  12139. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  12140. (point-at-eol) t)
  12141. (progn
  12142. (setq current (match-string 1))
  12143. (replace-match ""))
  12144. (setq current ""))
  12145. (setq current (nreverse (org-split-string current ":")))
  12146. (cond
  12147. ((eq onoff 'on)
  12148. (setq res t)
  12149. (or (member tag current) (push tag current)))
  12150. ((eq onoff 'off)
  12151. (or (not (member tag current)) (setq current (delete tag current))))
  12152. (t (if (member tag current)
  12153. (setq current (delete tag current))
  12154. (setq res t)
  12155. (push tag current))))
  12156. (end-of-line 1)
  12157. (if current
  12158. (progn
  12159. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  12160. (org-set-tags nil t))
  12161. (delete-horizontal-space))
  12162. (run-hooks 'org-after-tags-change-hook))
  12163. res))
  12164. (defun org-align-tags-here (to-col)
  12165. ;; Assumes that this is a headline
  12166. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  12167. (beginning-of-line 1)
  12168. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12169. (< pos (match-beginning 2)))
  12170. (progn
  12171. (setq tags-l (- (match-end 2) (match-beginning 2)))
  12172. (goto-char (match-beginning 1))
  12173. (insert " ")
  12174. (delete-region (point) (1+ (match-beginning 2)))
  12175. (setq ncol (max (current-column)
  12176. (1+ col)
  12177. (if (> to-col 0)
  12178. to-col
  12179. (- (abs to-col) tags-l))))
  12180. (setq p (point))
  12181. (insert (make-string (- ncol (current-column)) ?\ ))
  12182. (setq ncol (current-column))
  12183. (when indent-tabs-mode (tabify p (point-at-eol)))
  12184. (org-move-to-column (min ncol col) t))
  12185. (goto-char pos))))
  12186. (defun org-set-tags-command (&optional arg just-align)
  12187. "Call the set-tags command for the current entry."
  12188. (interactive "P")
  12189. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  12190. (org-set-tags arg just-align)
  12191. (save-excursion
  12192. (org-back-to-heading t)
  12193. (org-set-tags arg just-align))))
  12194. (defun org-set-tags-to (data)
  12195. "Set the tags of the current entry to DATA, replacing the current tags.
  12196. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  12197. If DATA is nil or the empty string, any tags will be removed."
  12198. (interactive "sTags: ")
  12199. (setq data
  12200. (cond
  12201. ((eq data nil) "")
  12202. ((equal data "") "")
  12203. ((stringp data)
  12204. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  12205. ":"))
  12206. ((listp data)
  12207. (concat ":" (mapconcat 'identity data ":") ":"))
  12208. (t nil)))
  12209. (when data
  12210. (save-excursion
  12211. (org-back-to-heading t)
  12212. (when (looking-at org-complex-heading-regexp)
  12213. (if (match-end 5)
  12214. (progn
  12215. (goto-char (match-beginning 5))
  12216. (insert data)
  12217. (delete-region (point) (point-at-eol))
  12218. (org-set-tags nil 'align))
  12219. (goto-char (point-at-eol))
  12220. (insert " " data)
  12221. (org-set-tags nil 'align)))
  12222. (beginning-of-line 1)
  12223. (if (looking-at ".*?\\([ \t]+\\)$")
  12224. (delete-region (match-beginning 1) (match-end 1))))))
  12225. (defun org-align-all-tags ()
  12226. "Align the tags i all headings."
  12227. (interactive)
  12228. (save-excursion
  12229. (or (ignore-errors (org-back-to-heading t))
  12230. (outline-next-heading))
  12231. (if (org-at-heading-p)
  12232. (org-set-tags t)
  12233. (message "No headings"))))
  12234. (defvar org-indent-indentation-per-level)
  12235. (defun org-set-tags (&optional arg just-align)
  12236. "Set the tags for the current headline.
  12237. With prefix ARG, realign all tags in headings in the current buffer."
  12238. (interactive "P")
  12239. (let* ((re org-outline-regexp-bol)
  12240. (current (unless arg (org-get-tags-string)))
  12241. (col (current-column))
  12242. (org-setting-tags t)
  12243. table current-tags inherited-tags ; computed below when needed
  12244. tags p0 c0 c1 rpl di tc level)
  12245. (if arg
  12246. (save-excursion
  12247. (goto-char (point-min))
  12248. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  12249. (while (re-search-forward re nil t)
  12250. (org-set-tags nil t)
  12251. (end-of-line 1)))
  12252. (message "All tags realigned to column %d" org-tags-column))
  12253. (if just-align
  12254. (setq tags current)
  12255. ;; Get a new set of tags from the user
  12256. (save-excursion
  12257. (setq table (append org-tag-persistent-alist
  12258. (or org-tag-alist (org-get-buffer-tags))
  12259. (and
  12260. org-complete-tags-always-offer-all-agenda-tags
  12261. (org-global-tags-completion-table
  12262. (org-agenda-files))))
  12263. org-last-tags-completion-table table
  12264. current-tags (org-split-string current ":")
  12265. inherited-tags (nreverse
  12266. (nthcdr (length current-tags)
  12267. (nreverse (org-get-tags-at))))
  12268. tags
  12269. (if (or (eq t org-use-fast-tag-selection)
  12270. (and org-use-fast-tag-selection
  12271. (delq nil (mapcar 'cdr table))))
  12272. (org-fast-tag-selection
  12273. current-tags inherited-tags table
  12274. (if org-fast-tag-selection-include-todo
  12275. org-todo-key-alist))
  12276. (let ((org-add-colon-after-tag-completion (< 1 (length table))))
  12277. (org-trim
  12278. (org-icompleting-read "Tags: "
  12279. 'org-tags-completion-function
  12280. nil nil current 'org-tags-history))))))
  12281. (while (string-match "[-+&]+" tags)
  12282. ;; No boolean logic, just a list
  12283. (setq tags (replace-match ":" t t tags))))
  12284. (setq tags (replace-regexp-in-string "[,]" ":" tags))
  12285. (if org-tags-sort-function
  12286. (setq tags (mapconcat 'identity
  12287. (sort (org-split-string
  12288. tags (org-re "[^[:alnum:]_@#%]+"))
  12289. org-tags-sort-function) ":")))
  12290. (if (string-match "\\`[\t ]*\\'" tags)
  12291. (setq tags "")
  12292. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  12293. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  12294. ;; Insert new tags at the correct column
  12295. (beginning-of-line 1)
  12296. (setq level (or (and (looking-at org-outline-regexp)
  12297. (- (match-end 0) (point) 1))
  12298. 1))
  12299. (cond
  12300. ((and (equal current "") (equal tags "")))
  12301. ((re-search-forward
  12302. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  12303. (point-at-eol) t)
  12304. (if (equal tags "")
  12305. (setq rpl "")
  12306. (goto-char (match-beginning 0))
  12307. (setq c0 (current-column)
  12308. ;; compute offset for the case of org-indent-mode active
  12309. di (if org-indent-mode
  12310. (* (1- org-indent-indentation-per-level) (1- level))
  12311. 0)
  12312. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  12313. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  12314. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
  12315. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  12316. (replace-match rpl t t)
  12317. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  12318. tags)
  12319. (t (error "Tags alignment failed")))
  12320. (org-move-to-column col)
  12321. (unless just-align
  12322. (run-hooks 'org-after-tags-change-hook)))))
  12323. (defun org-change-tag-in-region (beg end tag off)
  12324. "Add or remove TAG for each entry in the region.
  12325. This works in the agenda, and also in an org-mode buffer."
  12326. (interactive
  12327. (list (region-beginning) (region-end)
  12328. (let ((org-last-tags-completion-table
  12329. (if (eq major-mode 'org-mode)
  12330. (org-get-buffer-tags)
  12331. (org-global-tags-completion-table))))
  12332. (org-icompleting-read
  12333. "Tag: " 'org-tags-completion-function nil nil nil
  12334. 'org-tags-history))
  12335. (progn
  12336. (message "[s]et or [r]emove? ")
  12337. (equal (read-char-exclusive) ?r))))
  12338. (if (fboundp 'deactivate-mark) (deactivate-mark))
  12339. (let ((agendap (equal major-mode 'org-agenda-mode))
  12340. l1 l2 m buf pos newhead (cnt 0))
  12341. (goto-char end)
  12342. (setq l2 (1- (org-current-line)))
  12343. (goto-char beg)
  12344. (setq l1 (org-current-line))
  12345. (loop for l from l1 to l2 do
  12346. (org-goto-line l)
  12347. (setq m (get-text-property (point) 'org-hd-marker))
  12348. (when (or (and (eq major-mode 'org-mode) (org-at-heading-p))
  12349. (and agendap m))
  12350. (setq buf (if agendap (marker-buffer m) (current-buffer))
  12351. pos (if agendap m (point)))
  12352. (with-current-buffer buf
  12353. (save-excursion
  12354. (save-restriction
  12355. (goto-char pos)
  12356. (setq cnt (1+ cnt))
  12357. (org-toggle-tag tag (if off 'off 'on))
  12358. (setq newhead (org-get-heading)))))
  12359. (and agendap (org-agenda-change-all-lines newhead m))))
  12360. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  12361. (defun org-tags-completion-function (string predicate &optional flag)
  12362. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  12363. (confirm (lambda (x) (stringp (car x)))))
  12364. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  12365. (setq s1 (match-string 1 string)
  12366. s2 (match-string 2 string))
  12367. (setq s1 "" s2 string))
  12368. (cond
  12369. ((eq flag nil)
  12370. ;; try completion
  12371. (setq rtn (try-completion s2 ctable confirm))
  12372. (if (stringp rtn)
  12373. (setq rtn
  12374. (concat s1 s2 (substring rtn (length s2))
  12375. (if (and org-add-colon-after-tag-completion
  12376. (assoc rtn ctable))
  12377. ":" ""))))
  12378. rtn)
  12379. ((eq flag t)
  12380. ;; all-completions
  12381. (all-completions s2 ctable confirm)
  12382. )
  12383. ((eq flag 'lambda)
  12384. ;; exact match?
  12385. (assoc s2 ctable)))
  12386. ))
  12387. (defun org-fast-tag-insert (kwd tags face &optional end)
  12388. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  12389. (insert (format "%-12s" (concat kwd ":"))
  12390. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  12391. (or end "")))
  12392. (defun org-fast-tag-show-exit (flag)
  12393. (save-excursion
  12394. (org-goto-line 3)
  12395. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  12396. (replace-match ""))
  12397. (when flag
  12398. (end-of-line 1)
  12399. (org-move-to-column (- (window-width) 19) t)
  12400. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  12401. (defun org-set-current-tags-overlay (current prefix)
  12402. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  12403. (if (featurep 'xemacs)
  12404. (org-overlay-display org-tags-overlay (concat prefix s)
  12405. 'secondary-selection)
  12406. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  12407. (org-overlay-display org-tags-overlay (concat prefix s)))))
  12408. (defvar org-last-tag-selection-key nil)
  12409. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  12410. "Fast tag selection with single keys.
  12411. CURRENT is the current list of tags in the headline, INHERITED is the
  12412. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  12413. possibly with grouping information. TODO-TABLE is a similar table with
  12414. TODO keywords, should these have keys assigned to them.
  12415. If the keys are nil, a-z are automatically assigned.
  12416. Returns the new tags string, or nil to not change the current settings."
  12417. (let* ((fulltable (append table todo-table))
  12418. (maxlen (apply 'max (mapcar
  12419. (lambda (x)
  12420. (if (stringp (car x)) (string-width (car x)) 0))
  12421. fulltable)))
  12422. (buf (current-buffer))
  12423. (expert (eq org-fast-tag-selection-single-key 'expert))
  12424. (buffer-tags nil)
  12425. (fwidth (+ maxlen 3 1 3))
  12426. (ncol (/ (- (window-width) 4) fwidth))
  12427. (i-face 'org-done)
  12428. (c-face 'org-todo)
  12429. tg cnt e c char c1 c2 ntable tbl rtn
  12430. ov-start ov-end ov-prefix
  12431. (exit-after-next org-fast-tag-selection-single-key)
  12432. (done-keywords org-done-keywords)
  12433. groups ingroup)
  12434. (save-excursion
  12435. (beginning-of-line 1)
  12436. (if (looking-at
  12437. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12438. (setq ov-start (match-beginning 1)
  12439. ov-end (match-end 1)
  12440. ov-prefix "")
  12441. (setq ov-start (1- (point-at-eol))
  12442. ov-end (1+ ov-start))
  12443. (skip-chars-forward "^\n\r")
  12444. (setq ov-prefix
  12445. (concat
  12446. (buffer-substring (1- (point)) (point))
  12447. (if (> (current-column) org-tags-column)
  12448. " "
  12449. (make-string (- org-tags-column (current-column)) ?\ ))))))
  12450. (move-overlay org-tags-overlay ov-start ov-end)
  12451. (save-window-excursion
  12452. (if expert
  12453. (set-buffer (get-buffer-create " *Org tags*"))
  12454. (delete-other-windows)
  12455. (split-window-vertically)
  12456. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  12457. (erase-buffer)
  12458. (org-set-local 'org-done-keywords done-keywords)
  12459. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  12460. (org-fast-tag-insert "Current" current c-face "\n\n")
  12461. (org-fast-tag-show-exit exit-after-next)
  12462. (org-set-current-tags-overlay current ov-prefix)
  12463. (setq tbl fulltable char ?a cnt 0)
  12464. (while (setq e (pop tbl))
  12465. (cond
  12466. ((equal (car e) :startgroup)
  12467. (push '() groups) (setq ingroup t)
  12468. (when (not (= cnt 0))
  12469. (setq cnt 0)
  12470. (insert "\n"))
  12471. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  12472. ((equal (car e) :endgroup)
  12473. (setq ingroup nil cnt 0)
  12474. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  12475. ((equal e '(:newline))
  12476. (when (not (= cnt 0))
  12477. (setq cnt 0)
  12478. (insert "\n")
  12479. (setq e (car tbl))
  12480. (while (equal (car tbl) '(:newline))
  12481. (insert "\n")
  12482. (setq tbl (cdr tbl)))))
  12483. (t
  12484. (setq tg (copy-sequence (car e)) c2 nil)
  12485. (if (cdr e)
  12486. (setq c (cdr e))
  12487. ;; automatically assign a character.
  12488. (setq c1 (string-to-char
  12489. (downcase (substring
  12490. tg (if (= (string-to-char tg) ?@) 1 0)))))
  12491. (if (or (rassoc c1 ntable) (rassoc c1 table))
  12492. (while (or (rassoc char ntable) (rassoc char table))
  12493. (setq char (1+ char)))
  12494. (setq c2 c1))
  12495. (setq c (or c2 char)))
  12496. (if ingroup (push tg (car groups)))
  12497. (setq tg (org-add-props tg nil 'face
  12498. (cond
  12499. ((not (assoc tg table))
  12500. (org-get-todo-face tg))
  12501. ((member tg current) c-face)
  12502. ((member tg inherited) i-face)
  12503. (t nil))))
  12504. (if (and (= cnt 0) (not ingroup)) (insert " "))
  12505. (insert "[" c "] " tg (make-string
  12506. (- fwidth 4 (length tg)) ?\ ))
  12507. (push (cons tg c) ntable)
  12508. (when (= (setq cnt (1+ cnt)) ncol)
  12509. (insert "\n")
  12510. (if ingroup (insert " "))
  12511. (setq cnt 0)))))
  12512. (setq ntable (nreverse ntable))
  12513. (insert "\n")
  12514. (goto-char (point-min))
  12515. (if (not expert) (org-fit-window-to-buffer))
  12516. (setq rtn
  12517. (catch 'exit
  12518. (while t
  12519. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  12520. (if (not groups) "no " "")
  12521. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  12522. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  12523. (setq org-last-tag-selection-key c)
  12524. (cond
  12525. ((= c ?\r) (throw 'exit t))
  12526. ((= c ?!)
  12527. (setq groups (not groups))
  12528. (goto-char (point-min))
  12529. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  12530. ((= c ?\C-c)
  12531. (if (not expert)
  12532. (org-fast-tag-show-exit
  12533. (setq exit-after-next (not exit-after-next)))
  12534. (setq expert nil)
  12535. (delete-other-windows)
  12536. (set-window-buffer (split-window-vertically) " *Org tags*")
  12537. (org-switch-to-buffer-other-window " *Org tags*")
  12538. (org-fit-window-to-buffer)))
  12539. ((or (= c ?\C-g)
  12540. (and (= c ?q) (not (rassoc c ntable))))
  12541. (org-detach-overlay org-tags-overlay)
  12542. (setq quit-flag t))
  12543. ((= c ?\ )
  12544. (setq current nil)
  12545. (if exit-after-next (setq exit-after-next 'now)))
  12546. ((= c ?\t)
  12547. (condition-case nil
  12548. (setq tg (org-icompleting-read
  12549. "Tag: "
  12550. (or buffer-tags
  12551. (with-current-buffer buf
  12552. (org-get-buffer-tags)))))
  12553. (quit (setq tg "")))
  12554. (when (string-match "\\S-" tg)
  12555. (add-to-list 'buffer-tags (list tg))
  12556. (if (member tg current)
  12557. (setq current (delete tg current))
  12558. (push tg current)))
  12559. (if exit-after-next (setq exit-after-next 'now)))
  12560. ((setq e (rassoc c todo-table) tg (car e))
  12561. (with-current-buffer buf
  12562. (save-excursion (org-todo tg)))
  12563. (if exit-after-next (setq exit-after-next 'now)))
  12564. ((setq e (rassoc c ntable) tg (car e))
  12565. (if (member tg current)
  12566. (setq current (delete tg current))
  12567. (loop for g in groups do
  12568. (if (member tg g)
  12569. (mapc (lambda (x)
  12570. (setq current (delete x current)))
  12571. g)))
  12572. (push tg current))
  12573. (if exit-after-next (setq exit-after-next 'now))))
  12574. ;; Create a sorted list
  12575. (setq current
  12576. (sort current
  12577. (lambda (a b)
  12578. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  12579. (if (eq exit-after-next 'now) (throw 'exit t))
  12580. (goto-char (point-min))
  12581. (beginning-of-line 2)
  12582. (delete-region (point) (point-at-eol))
  12583. (org-fast-tag-insert "Current" current c-face)
  12584. (org-set-current-tags-overlay current ov-prefix)
  12585. (while (re-search-forward
  12586. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  12587. (setq tg (match-string 1))
  12588. (add-text-properties
  12589. (match-beginning 1) (match-end 1)
  12590. (list 'face
  12591. (cond
  12592. ((member tg current) c-face)
  12593. ((member tg inherited) i-face)
  12594. (t (get-text-property (match-beginning 1) 'face))))))
  12595. (goto-char (point-min)))))
  12596. (org-detach-overlay org-tags-overlay)
  12597. (if rtn
  12598. (mapconcat 'identity current ":")
  12599. nil))))
  12600. (defun org-get-tags-string ()
  12601. "Get the TAGS string in the current headline."
  12602. (unless (org-at-heading-p t)
  12603. (error "Not on a heading"))
  12604. (save-excursion
  12605. (beginning-of-line 1)
  12606. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12607. (org-match-string-no-properties 1)
  12608. "")))
  12609. (defun org-get-tags ()
  12610. "Get the list of tags specified in the current headline."
  12611. (org-split-string (org-get-tags-string) ":"))
  12612. (defun org-get-buffer-tags ()
  12613. "Get a table of all tags used in the buffer, for completion."
  12614. (let (tags)
  12615. (save-excursion
  12616. (goto-char (point-min))
  12617. (while (re-search-forward
  12618. (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
  12619. (when (equal (char-after (point-at-bol 0)) ?*)
  12620. (mapc (lambda (x) (add-to-list 'tags x))
  12621. (org-split-string (org-match-string-no-properties 1) ":")))))
  12622. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  12623. (mapcar 'list tags)))
  12624. ;;;; The mapping API
  12625. ;;;###autoload
  12626. (defun org-map-entries (func &optional match scope &rest skip)
  12627. "Call FUNC at each headline selected by MATCH in SCOPE.
  12628. FUNC is a function or a lisp form. The function will be called without
  12629. arguments, with the cursor positioned at the beginning of the headline.
  12630. The return values of all calls to the function will be collected and
  12631. returned as a list.
  12632. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  12633. does not need to preserve point. After evaluation, the cursor will be
  12634. moved to the end of the line (presumably of the headline of the
  12635. processed entry) and search continues from there. Under some
  12636. circumstances, this may not produce the wanted results. For example,
  12637. if you have removed (e.g. archived) the current (sub)tree it could
  12638. mean that the next entry will be skipped entirely. In such cases, you
  12639. can specify the position from where search should continue by making
  12640. FUNC set the variable `org-map-continue-from' to the desired buffer
  12641. position.
  12642. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  12643. Only headlines that are matched by this query will be considered during
  12644. the iteration. When MATCH is nil or t, all headlines will be
  12645. visited by the iteration.
  12646. SCOPE determines the scope of this command. It can be any of:
  12647. nil The current buffer, respecting the restriction if any
  12648. tree The subtree started with the entry at point
  12649. region The entries within the active region, if any
  12650. region-start-level
  12651. The entries within the active region, but only those at
  12652. the same level than the first one.
  12653. file The current buffer, without restriction
  12654. file-with-archives
  12655. The current buffer, and any archives associated with it
  12656. agenda All agenda files
  12657. agenda-with-archives
  12658. All agenda files with any archive files associated with them
  12659. \(file1 file2 ...)
  12660. If this is a list, all files in the list will be scanned
  12661. The remaining args are treated as settings for the skipping facilities of
  12662. the scanner. The following items can be given here:
  12663. archive skip trees with the archive tag.
  12664. comment skip trees with the COMMENT keyword
  12665. function or Emacs Lisp form:
  12666. will be used as value for `org-agenda-skip-function', so whenever
  12667. the function returns t, FUNC will not be called for that
  12668. entry and search will continue from the point where the
  12669. function leaves it.
  12670. If your function needs to retrieve the tags including inherited tags
  12671. at the *current* entry, you can use the value of the variable
  12672. `org-scanner-tags' which will be much faster than getting the value
  12673. with `org-get-tags-at'. If your function gets properties with
  12674. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  12675. to t around the call to `org-entry-properties' to get the same speedup.
  12676. Note that if your function moves around to retrieve tags and properties at
  12677. a *different* entry, you cannot use these techniques."
  12678. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  12679. (not (org-region-active-p)))
  12680. (let* ((org-agenda-archives-mode nil) ; just to make sure
  12681. (org-agenda-skip-archived-trees (memq 'archive skip))
  12682. (org-agenda-skip-comment-trees (memq 'comment skip))
  12683. (org-agenda-skip-function
  12684. (car (org-delete-all '(comment archive) skip)))
  12685. (org-tags-match-list-sublevels t)
  12686. (start-level (eq scope 'region-start-level))
  12687. matcher file res
  12688. org-todo-keywords-for-agenda
  12689. org-done-keywords-for-agenda
  12690. org-todo-keyword-alist-for-agenda
  12691. org-drawers-for-agenda
  12692. org-tag-alist-for-agenda
  12693. todo-only)
  12694. (cond
  12695. ((eq match t) (setq matcher t))
  12696. ((eq match nil) (setq matcher t))
  12697. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  12698. (save-excursion
  12699. (save-restriction
  12700. (cond ((eq scope 'tree)
  12701. (org-back-to-heading t)
  12702. (org-narrow-to-subtree)
  12703. (setq scope nil))
  12704. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  12705. (org-region-active-p))
  12706. ;; If needed, set start-level to a string like "2"
  12707. (when start-level
  12708. (save-excursion
  12709. (goto-char (region-beginning))
  12710. (unless (org-at-heading-p) (outline-next-heading))
  12711. (setq start-level (org-current-level))))
  12712. (narrow-to-region (region-beginning)
  12713. (save-excursion
  12714. (goto-char (region-end))
  12715. (unless (and (bolp) (org-at-heading-p))
  12716. (outline-next-heading))
  12717. (point)))
  12718. (setq scope nil)))
  12719. (if (not scope)
  12720. (progn
  12721. (org-prepare-agenda-buffers
  12722. (list (buffer-file-name (current-buffer))))
  12723. (setq res (org-scan-tags func matcher todo-only start-level)))
  12724. ;; Get the right scope
  12725. (cond
  12726. ((and scope (listp scope) (symbolp (car scope)))
  12727. (setq scope (eval scope)))
  12728. ((eq scope 'agenda)
  12729. (setq scope (org-agenda-files t)))
  12730. ((eq scope 'agenda-with-archives)
  12731. (setq scope (org-agenda-files t))
  12732. (setq scope (org-add-archive-files scope)))
  12733. ((eq scope 'file)
  12734. (setq scope (list (buffer-file-name))))
  12735. ((eq scope 'file-with-archives)
  12736. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  12737. (org-prepare-agenda-buffers scope)
  12738. (while (setq file (pop scope))
  12739. (with-current-buffer (org-find-base-buffer-visiting file)
  12740. (save-excursion
  12741. (save-restriction
  12742. (widen)
  12743. (goto-char (point-min))
  12744. (setq res (append res (org-scan-tags func matcher todo-only))))))))))
  12745. res)))
  12746. ;;;; Properties
  12747. ;;; Setting and retrieving properties
  12748. (defconst org-special-properties
  12749. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  12750. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM")
  12751. "The special properties valid in Org-mode.
  12752. These are properties that are not defined in the property drawer,
  12753. but in some other way.")
  12754. (defconst org-default-properties
  12755. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  12756. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  12757. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  12758. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  12759. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  12760. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  12761. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  12762. "Some properties that are used by Org-mode for various purposes.
  12763. Being in this list makes sure that they are offered for completion.")
  12764. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  12765. "Regular expression matching the first line of a property drawer.")
  12766. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  12767. "Regular expression matching the last line of a property drawer.")
  12768. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  12769. "Regular expression matching the first line of a property drawer.")
  12770. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  12771. "Regular expression matching the first line of a property drawer.")
  12772. (defconst org-property-drawer-re
  12773. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  12774. org-property-end-re "\\)\n?")
  12775. "Matches an entire property drawer.")
  12776. (defconst org-clock-drawer-re
  12777. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  12778. org-property-end-re "\\)\n?")
  12779. "Matches an entire clock drawer.")
  12780. (defsubst org-re-property (property)
  12781. "Return a regexp matching PROPERTY.
  12782. Match group 1 will be set to the value "
  12783. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)"))
  12784. (defun org-property-action ()
  12785. "Do an action on properties."
  12786. (interactive)
  12787. (let (c)
  12788. (org-at-property-p)
  12789. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  12790. (setq c (read-char-exclusive))
  12791. (cond
  12792. ((equal c ?s)
  12793. (call-interactively 'org-set-property))
  12794. ((equal c ?d)
  12795. (call-interactively 'org-delete-property))
  12796. ((equal c ?D)
  12797. (call-interactively 'org-delete-property-globally))
  12798. ((equal c ?c)
  12799. (call-interactively 'org-compute-property-at-point))
  12800. (t (error "No such property action %c" c)))))
  12801. (defun org-set-effort (&optional value)
  12802. "Set the effort property of the current entry.
  12803. With numerical prefix arg, use the nth allowed value, 0 stands for the 10th
  12804. allowed value."
  12805. (interactive "P")
  12806. (if (equal value 0) (setq value 10))
  12807. (let* ((completion-ignore-case t)
  12808. (prop org-effort-property)
  12809. (cur (org-entry-get nil prop))
  12810. (allowed (org-property-get-allowed-values nil prop 'table))
  12811. (existing (mapcar 'list (org-property-values prop)))
  12812. rpl
  12813. (val (cond
  12814. ((stringp value) value)
  12815. ((and allowed (integerp value))
  12816. (or (car (nth (1- value) allowed))
  12817. (car (org-last allowed))))
  12818. (allowed
  12819. (message "Select 1-9,0, [RET%s]: %s"
  12820. (if cur (concat "=" cur) "")
  12821. (mapconcat 'car allowed " "))
  12822. (setq rpl (read-char-exclusive))
  12823. (if (equal rpl ?\r)
  12824. cur
  12825. (setq rpl (- rpl ?0))
  12826. (if (equal rpl 0) (setq rpl 10))
  12827. (if (and (> rpl 0) (<= rpl (length allowed)))
  12828. (car (nth (1- rpl) allowed))
  12829. (org-completing-read "Effort: " allowed nil))))
  12830. (t
  12831. (let (org-completion-use-ido org-completion-use-iswitchb)
  12832. (org-completing-read
  12833. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  12834. (concat "[" cur "]") "")
  12835. ": ")
  12836. existing nil nil "" nil cur))))))
  12837. (unless (equal (org-entry-get nil prop) val)
  12838. (org-entry-put nil prop val))
  12839. (message "%s is now %s" prop val)))
  12840. (defun org-at-property-p ()
  12841. "Is cursor inside a property drawer?"
  12842. (save-excursion
  12843. (beginning-of-line 1)
  12844. (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
  12845. (save-match-data ;; Used by calling procedures
  12846. (let ((p (point))
  12847. (range (unless (org-before-first-heading-p)
  12848. (org-get-property-block))))
  12849. (and range (<= (car range) p) (< p (cdr range))))))))
  12850. (defun org-get-property-block (&optional beg end force)
  12851. "Return the (beg . end) range of the body of the property drawer.
  12852. BEG and END can be beginning and end of subtree, if not given
  12853. they will be found.
  12854. If the drawer does not exist and FORCE is non-nil, create the drawer."
  12855. (catch 'exit
  12856. (save-excursion
  12857. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  12858. (end (or end (progn (outline-next-heading) (point)))))
  12859. (goto-char beg)
  12860. (if (re-search-forward org-property-start-re end t)
  12861. (setq beg (1+ (match-end 0)))
  12862. (if force
  12863. (save-excursion
  12864. (org-insert-property-drawer)
  12865. (setq end (progn (outline-next-heading) (point))))
  12866. (throw 'exit nil))
  12867. (goto-char beg)
  12868. (if (re-search-forward org-property-start-re end t)
  12869. (setq beg (1+ (match-end 0)))))
  12870. (if (re-search-forward org-property-end-re end t)
  12871. (setq end (match-beginning 0))
  12872. (or force (throw 'exit nil))
  12873. (goto-char beg)
  12874. (setq end beg)
  12875. (org-indent-line-function)
  12876. (insert ":END:\n"))
  12877. (cons beg end)))))
  12878. (defun org-entry-properties (&optional pom which specific)
  12879. "Get all properties of the entry at point-or-marker POM.
  12880. This includes the TODO keyword, the tags, time strings for deadline,
  12881. scheduled, and clocking, and any additional properties defined in the
  12882. entry. The return value is an alist, keys may occur multiple times
  12883. if the property key was used several times.
  12884. POM may also be nil, in which case the current entry is used.
  12885. If WHICH is nil or `all', get all properties. If WHICH is
  12886. `special' or `standard', only get that subclass. If WHICH
  12887. is a string only get exactly this property. SPECIFIC can be a string, the
  12888. specific property we are interested in. Specifying it can speed
  12889. things up because then unnecessary parsing is avoided."
  12890. (setq which (or which 'all))
  12891. (org-with-point-at pom
  12892. (let ((clockstr (substring org-clock-string 0 -1))
  12893. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  12894. (case-fold-search nil)
  12895. beg end range props sum-props key key1 value string clocksum)
  12896. (save-excursion
  12897. (when (condition-case nil
  12898. (and (eq major-mode 'org-mode) (org-back-to-heading t))
  12899. (error nil))
  12900. (setq beg (point))
  12901. (setq sum-props (get-text-property (point) 'org-summaries))
  12902. (setq clocksum (get-text-property (point) :org-clock-minutes))
  12903. (outline-next-heading)
  12904. (setq end (point))
  12905. (when (memq which '(all special))
  12906. ;; Get the special properties, like TODO and tags
  12907. (goto-char beg)
  12908. (when (and (or (not specific) (string= specific "TODO"))
  12909. (looking-at org-todo-line-regexp) (match-end 2))
  12910. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  12911. (when (and (or (not specific) (string= specific "PRIORITY"))
  12912. (looking-at org-priority-regexp))
  12913. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  12914. (when (or (not specific) (string= specific "FILE"))
  12915. (push (cons "FILE" buffer-file-name) props))
  12916. (when (and (or (not specific) (string= specific "TAGS"))
  12917. (setq value (org-get-tags-string))
  12918. (string-match "\\S-" value))
  12919. (push (cons "TAGS" value) props))
  12920. (when (and (or (not specific) (string= specific "ALLTAGS"))
  12921. (setq value (org-get-tags-at)))
  12922. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  12923. ":"))
  12924. props))
  12925. (when (or (not specific) (string= specific "BLOCKED"))
  12926. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  12927. (when (or (not specific)
  12928. (member specific
  12929. '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
  12930. "TIMESTAMP" "TIMESTAMP_IA")))
  12931. (catch 'match
  12932. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  12933. (setq key (if (match-end 1)
  12934. (substring (org-match-string-no-properties 1)
  12935. 0 -1))
  12936. string (if (equal key clockstr)
  12937. (org-no-properties
  12938. (org-trim
  12939. (buffer-substring
  12940. (match-beginning 3) (goto-char
  12941. (point-at-eol)))))
  12942. (substring (org-match-string-no-properties 3)
  12943. 1 -1)))
  12944. ;; Get the correct property name from the key. This is
  12945. ;; necessary if the user has configured time keywords.
  12946. (setq key1 (concat key ":"))
  12947. (cond
  12948. ((not key)
  12949. (setq key
  12950. (if (= (char-after (match-beginning 3)) ?\[)
  12951. "TIMESTAMP_IA" "TIMESTAMP")))
  12952. ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
  12953. ((equal key1 org-deadline-string) (setq key "DEADLINE"))
  12954. ((equal key1 org-closed-string) (setq key "CLOSED"))
  12955. ((equal key1 org-clock-string) (setq key "CLOCK")))
  12956. (if (and specific (equal key specific) (not (equal key "CLOCK")))
  12957. (progn
  12958. (push (cons key string) props)
  12959. ;; no need to search further if match is found
  12960. (throw 'match t))
  12961. (when (or (equal key "CLOCK") (not (assoc key props)))
  12962. (push (cons key string) props))))))
  12963. )
  12964. (when (memq which '(all standard))
  12965. ;; Get the standard properties, like :PROP: ...
  12966. (setq range (org-get-property-block beg end))
  12967. (when range
  12968. (goto-char (car range))
  12969. (while (re-search-forward
  12970. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  12971. (cdr range) t)
  12972. (setq key (org-match-string-no-properties 1)
  12973. value (org-trim (or (org-match-string-no-properties 2) "")))
  12974. (unless (member key excluded)
  12975. (push (cons key (or value "")) props)))))
  12976. (if clocksum
  12977. (push (cons "CLOCKSUM"
  12978. (org-columns-number-to-string (/ (float clocksum) 60.)
  12979. 'add_times))
  12980. props))
  12981. (unless (assoc "CATEGORY" props)
  12982. (push (cons "CATEGORY" (org-get-category)) props))
  12983. (append sum-props (nreverse props)))))))
  12984. (defun org-entry-get (pom property &optional inherit literal-nil)
  12985. "Get value of PROPERTY for entry at point-or-marker POM.
  12986. If INHERIT is non-nil and the entry does not have the property,
  12987. then also check higher levels of the hierarchy.
  12988. If INHERIT is the symbol `selective', use inheritance only if the setting
  12989. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  12990. If the property is present but empty, the return value is the empty string.
  12991. If the property is not present at all, nil is returned.
  12992. If LITERAL-NIL is set, return the string value \"nil\" as a string,
  12993. do not interpret it as the list atom nil. This is used for inheritance
  12994. when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
  12995. (org-with-point-at pom
  12996. (if (and inherit (if (eq inherit 'selective)
  12997. (org-property-inherit-p property)
  12998. t))
  12999. (org-entry-get-with-inheritance property literal-nil)
  13000. (if (member property org-special-properties)
  13001. ;; We need a special property. Use `org-entry-properties' to
  13002. ;; retrieve it, but specify the wanted property
  13003. (cdr (assoc property (org-entry-properties nil 'special property)))
  13004. (let ((range (unless (org-before-first-heading-p)
  13005. (org-get-property-block)))
  13006. (props (list (or (assoc property org-file-properties)
  13007. (assoc property org-global-properties)
  13008. (assoc property org-global-properties-fixed))))
  13009. val)
  13010. (flet ((ap (key)
  13011. (when (re-search-forward
  13012. (org-re-property key) (cdr range) t)
  13013. (setq props
  13014. (org-update-property-plist
  13015. key
  13016. (if (match-end 1)
  13017. (org-match-string-no-properties 1) "")
  13018. props)))))
  13019. (when (and range (goto-char (car range)))
  13020. (ap property)
  13021. (goto-char (car range))
  13022. (while (ap (concat property "+")))
  13023. (setq val (cdr (assoc property props)))
  13024. (when val (if literal-nil val (org-not-nil val))))))))))
  13025. (defun org-property-or-variable-value (var &optional inherit)
  13026. "Check if there is a property fixing the value of VAR.
  13027. If yes, return this value. If not, return the current value of the variable."
  13028. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  13029. (if (and prop (stringp prop) (string-match "\\S-" prop))
  13030. (read prop)
  13031. (symbol-value var))))
  13032. (defun org-entry-delete (pom property)
  13033. "Delete the property PROPERTY from entry at point-or-marker POM."
  13034. (org-with-point-at pom
  13035. (if (member property org-special-properties)
  13036. nil ; cannot delete these properties.
  13037. (let ((range (org-get-property-block)))
  13038. (if (and range
  13039. (goto-char (car range))
  13040. (re-search-forward
  13041. (org-re-property property)
  13042. (cdr range) t))
  13043. (progn
  13044. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  13045. t)
  13046. nil)))))
  13047. ;; Multi-values properties are properties that contain multiple values
  13048. ;; These values are assumed to be single words, separated by whitespace.
  13049. (defun org-entry-add-to-multivalued-property (pom property value)
  13050. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  13051. (let* ((old (org-entry-get pom property))
  13052. (values (and old (org-split-string old "[ \t]"))))
  13053. (setq value (org-entry-protect-space value))
  13054. (unless (member value values)
  13055. (setq values (cons value values))
  13056. (org-entry-put pom property
  13057. (mapconcat 'identity values " ")))))
  13058. (defun org-entry-remove-from-multivalued-property (pom property value)
  13059. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  13060. (let* ((old (org-entry-get pom property))
  13061. (values (and old (org-split-string old "[ \t]"))))
  13062. (setq value (org-entry-protect-space value))
  13063. (when (member value values)
  13064. (setq values (delete value values))
  13065. (org-entry-put pom property
  13066. (mapconcat 'identity values " ")))))
  13067. (defun org-entry-member-in-multivalued-property (pom property value)
  13068. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  13069. (let* ((old (org-entry-get pom property))
  13070. (values (and old (org-split-string old "[ \t]"))))
  13071. (setq value (org-entry-protect-space value))
  13072. (member value values)))
  13073. (defun org-entry-get-multivalued-property (pom property)
  13074. "Return a list of values in a multivalued property."
  13075. (let* ((value (org-entry-get pom property))
  13076. (values (and value (org-split-string value "[ \t]"))))
  13077. (mapcar 'org-entry-restore-space values)))
  13078. (defun org-entry-put-multivalued-property (pom property &rest values)
  13079. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  13080. VALUES should be a list of strings. Spaces will be protected."
  13081. (org-entry-put pom property
  13082. (mapconcat 'org-entry-protect-space values " "))
  13083. (let* ((value (org-entry-get pom property))
  13084. (values (and value (org-split-string value "[ \t]"))))
  13085. (mapcar 'org-entry-restore-space values)))
  13086. (defun org-entry-protect-space (s)
  13087. "Protect spaces and newline in string S."
  13088. (while (string-match " " s)
  13089. (setq s (replace-match "%20" t t s)))
  13090. (while (string-match "\n" s)
  13091. (setq s (replace-match "%0A" t t s)))
  13092. s)
  13093. (defun org-entry-restore-space (s)
  13094. "Restore spaces and newline in string S."
  13095. (while (string-match "%20" s)
  13096. (setq s (replace-match " " t t s)))
  13097. (while (string-match "%0A" s)
  13098. (setq s (replace-match "\n" t t s)))
  13099. s)
  13100. (defvar org-entry-property-inherited-from (make-marker)
  13101. "Marker pointing to the entry from where a property was inherited.
  13102. Each call to `org-entry-get-with-inheritance' will set this marker to the
  13103. location of the entry where the inheritance search matched. If there was
  13104. no match, the marker will point nowhere.
  13105. Note that also `org-entry-get' calls this function, if the INHERIT flag
  13106. is set.")
  13107. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  13108. "Get entry property, and search higher levels if not present.
  13109. The search will stop at the first ancestor which has the property defined.
  13110. If the value found is \"nil\", return nil to show that the property
  13111. should be considered as undefined (this is the meaning of nil here).
  13112. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  13113. (move-marker org-entry-property-inherited-from nil)
  13114. (let (tmp)
  13115. (unless (org-before-first-heading-p)
  13116. (save-excursion
  13117. (save-restriction
  13118. (widen)
  13119. (catch 'ex
  13120. (while t
  13121. (when (setq tmp (org-entry-get nil property nil 'literal-nil))
  13122. (org-back-to-heading t)
  13123. (move-marker org-entry-property-inherited-from (point))
  13124. (throw 'ex tmp))
  13125. (or (org-up-heading-safe) (throw 'ex nil)))))))
  13126. (setq tmp (or tmp
  13127. (cdr (assoc property org-file-properties))
  13128. (cdr (assoc property org-global-properties))
  13129. (cdr (assoc property org-global-properties-fixed))))
  13130. (if literal-nil tmp (org-not-nil tmp))))
  13131. (defvar org-property-changed-functions nil
  13132. "Hook called when the value of a property has changed.
  13133. Each hook function should accept two arguments, the name of the property
  13134. and the new value.")
  13135. (defun org-entry-put (pom property value)
  13136. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  13137. (org-with-point-at pom
  13138. (org-back-to-heading t)
  13139. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  13140. range)
  13141. (cond
  13142. ((equal property "TODO")
  13143. (when (and (stringp value) (string-match "\\S-" value)
  13144. (not (member value org-todo-keywords-1)))
  13145. (error "\"%s\" is not a valid TODO state" value))
  13146. (if (or (not value)
  13147. (not (string-match "\\S-" value)))
  13148. (setq value 'none))
  13149. (org-todo value)
  13150. (org-set-tags nil 'align))
  13151. ((equal property "PRIORITY")
  13152. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  13153. (string-to-char value) ?\ ))
  13154. (org-set-tags nil 'align))
  13155. ((equal property "SCHEDULED")
  13156. (if (re-search-forward org-scheduled-time-regexp end t)
  13157. (cond
  13158. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13159. ((eq value 'later) (org-timestamp-change 1 'day))
  13160. (t (call-interactively 'org-schedule)))
  13161. (call-interactively 'org-schedule)))
  13162. ((equal property "DEADLINE")
  13163. (if (re-search-forward org-deadline-time-regexp end t)
  13164. (cond
  13165. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13166. ((eq value 'later) (org-timestamp-change 1 'day))
  13167. (t (call-interactively 'org-deadline)))
  13168. (call-interactively 'org-deadline)))
  13169. ((member property org-special-properties)
  13170. (error "The %s property can not yet be set with `org-entry-put'"
  13171. property))
  13172. (t ; a non-special property
  13173. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  13174. (setq range (org-get-property-block beg end 'force))
  13175. (goto-char (car range))
  13176. (if (re-search-forward
  13177. (org-re-property property) (cdr range) t)
  13178. (progn
  13179. (delete-region (match-beginning 0) (match-end 0))
  13180. (goto-char (match-beginning 0)))
  13181. (goto-char (cdr range))
  13182. (insert "\n")
  13183. (backward-char 1)
  13184. (org-indent-line-function))
  13185. (insert ":" property ":")
  13186. (and value (insert " " value))
  13187. (org-indent-line-function)))))
  13188. (run-hook-with-args 'org-property-changed-functions property value)))
  13189. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  13190. "Get all property keys in the current buffer.
  13191. With INCLUDE-SPECIALS, also list the special properties that reflect things
  13192. like tags and TODO state.
  13193. With INCLUDE-DEFAULTS, also include properties that has special meaning
  13194. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
  13195. and others.
  13196. With INCLUDE-COLUMNS, also include property names given in COLUMN
  13197. formats in the current buffer."
  13198. (let (rtn range cfmt s p)
  13199. (save-excursion
  13200. (save-restriction
  13201. (widen)
  13202. (goto-char (point-min))
  13203. (while (re-search-forward org-property-start-re nil t)
  13204. (setq range (org-get-property-block))
  13205. (goto-char (car range))
  13206. (while (re-search-forward
  13207. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  13208. (cdr range) t)
  13209. (add-to-list 'rtn (org-match-string-no-properties 1)))
  13210. (outline-next-heading))))
  13211. (when include-specials
  13212. (setq rtn (append org-special-properties rtn)))
  13213. (when include-defaults
  13214. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  13215. (add-to-list 'rtn org-effort-property))
  13216. (when include-columns
  13217. (save-excursion
  13218. (save-restriction
  13219. (widen)
  13220. (goto-char (point-min))
  13221. (while (re-search-forward
  13222. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  13223. nil t)
  13224. (setq cfmt (match-string 2) s 0)
  13225. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  13226. cfmt s)
  13227. (setq s (match-end 0)
  13228. p (match-string 1 cfmt))
  13229. (unless (or (equal p "ITEM")
  13230. (member p org-special-properties))
  13231. (add-to-list 'rtn (match-string 1 cfmt))))))))
  13232. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  13233. (defun org-property-values (key)
  13234. "Return a list of all values of property KEY in the current buffer."
  13235. (save-excursion
  13236. (save-restriction
  13237. (widen)
  13238. (goto-char (point-min))
  13239. (let ((re (org-re-property key))
  13240. values)
  13241. (while (re-search-forward re nil t)
  13242. (add-to-list 'values (org-trim (match-string 1))))
  13243. (delete "" values)))))
  13244. (defun org-insert-property-drawer ()
  13245. "Insert a property drawer into the current entry."
  13246. (org-back-to-heading t)
  13247. (looking-at org-outline-regexp)
  13248. (let ((indent (if org-adapt-indentation
  13249. (- (match-end 0) (match-beginning 0))
  13250. 0))
  13251. (beg (point))
  13252. (re (concat "^[ \t]*" org-keyword-time-regexp))
  13253. end hiddenp)
  13254. (outline-next-heading)
  13255. (setq end (point))
  13256. (goto-char beg)
  13257. (while (re-search-forward re end t))
  13258. (setq hiddenp (outline-invisible-p))
  13259. (end-of-line 1)
  13260. (and (equal (char-after) ?\n) (forward-char 1))
  13261. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  13262. (if (member (match-string 1) '("CLOCK:" ":END:"))
  13263. ;; just skip this line
  13264. (beginning-of-line 2)
  13265. ;; Drawer start, find the end
  13266. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  13267. (beginning-of-line 1)))
  13268. (org-skip-over-state-notes)
  13269. (skip-chars-backward " \t\n\r")
  13270. (if (eq (char-before) ?*) (forward-char 1))
  13271. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  13272. (beginning-of-line 0)
  13273. (org-indent-to-column indent)
  13274. (beginning-of-line 2)
  13275. (org-indent-to-column indent)
  13276. (beginning-of-line 0)
  13277. (if hiddenp
  13278. (save-excursion
  13279. (org-back-to-heading t)
  13280. (hide-entry))
  13281. (org-flag-drawer t))))
  13282. (defun org-insert-drawer (&optional arg drawer)
  13283. "Insert a drawer at point.
  13284. Optional argument DRAWER, when non-nil, is a string representing
  13285. drawer's name. Otherwise, the user is prompted for a name.
  13286. If a region is active, insert the drawer around that region
  13287. instead.
  13288. Point is left between drawer's boundaries."
  13289. (interactive "P")
  13290. (let* ((logbook (if (stringp org-log-into-drawer) org-log-into-drawer
  13291. "LOGBOOK"))
  13292. ;; SYSTEM-DRAWERS is a list of drawer names that are used
  13293. ;; internally by Org. They are meant to be inserted
  13294. ;; automatically.
  13295. (system-drawers `("CLOCK" ,logbook "PROPERTIES"))
  13296. ;; Remove system drawers from list. Note: For some reason,
  13297. ;; `org-completing-read' ignores the predicate while
  13298. ;; `completing-read' handles it fine.
  13299. (drawer (if arg "PROPERTIES"
  13300. (or drawer
  13301. (completing-read
  13302. "Drawer: " org-drawers
  13303. (lambda (d) (not (member d system-drawers))))))))
  13304. (cond
  13305. ;; With C-u, fall back on `org-insert-property-drawer'
  13306. (arg (org-insert-property-drawer))
  13307. ;; With an active region, insert a drawer at point.
  13308. ((not (org-region-active-p))
  13309. (progn
  13310. (unless (bolp) (insert "\n"))
  13311. (insert (format ":%s:\n\n:END:\n" drawer))
  13312. (forward-line -2)))
  13313. ;; Otherwise, insert the drawer at point
  13314. (t
  13315. (let ((rbeg (region-beginning))
  13316. (rend (copy-marker (region-end))))
  13317. (unwind-protect
  13318. (progn
  13319. (goto-char rbeg)
  13320. (beginning-of-line)
  13321. (when (save-excursion
  13322. (re-search-forward org-outline-regexp-bol rend t))
  13323. (error "Drawers cannot contain headlines"))
  13324. ;; Position point at the beginning of the first
  13325. ;; non-blank line in region. Insert drawer's opening
  13326. ;; there, then indent it.
  13327. (org-skip-whitespace)
  13328. (beginning-of-line)
  13329. (insert ":" drawer ":\n")
  13330. (forward-line -1)
  13331. (indent-for-tab-command)
  13332. ;; Move point to the beginning of the first blank line
  13333. ;; after the last non-blank line in region. Insert
  13334. ;; drawer's closing, then indent it.
  13335. (goto-char rend)
  13336. (skip-chars-backward " \r\t\n")
  13337. (insert "\n:END:")
  13338. (indent-for-tab-command)
  13339. (unless (eolp) (insert "\n")))
  13340. ;; Clear marker, whatever the outcome of insertion is.
  13341. (set-marker rend nil)))))))
  13342. (defvar org-property-set-functions-alist nil
  13343. "Property set function alist.
  13344. Each entry should have the following format:
  13345. (PROPERTY . READ-FUNCTION)
  13346. The read function will be called with the same argument as
  13347. `org-completing-read'.")
  13348. (defun org-set-property-function (property)
  13349. "Get the function that should be used to set PROPERTY.
  13350. This is computed according to `org-property-set-functions-alist'."
  13351. (or (cdr (assoc property org-property-set-functions-alist))
  13352. 'org-completing-read))
  13353. (defun org-read-property-value (property)
  13354. "Read PROPERTY value from user."
  13355. (let* ((completion-ignore-case t)
  13356. (allowed (org-property-get-allowed-values nil property 'table))
  13357. (cur (org-entry-get nil property))
  13358. (prompt (concat property " value"
  13359. (if (and cur (string-match "\\S-" cur))
  13360. (concat " [" cur "]") "") ": "))
  13361. (set-function (org-set-property-function property))
  13362. (val (if allowed
  13363. (funcall set-function prompt allowed nil
  13364. (not (get-text-property 0 'org-unrestricted
  13365. (caar allowed))))
  13366. (let (org-completion-use-ido org-completion-use-iswitchb)
  13367. (funcall set-function prompt
  13368. (mapcar 'list (org-property-values property))
  13369. nil nil "" nil cur)))))
  13370. (if (equal val "")
  13371. cur
  13372. val)))
  13373. (defvar org-last-set-property nil)
  13374. (defun org-read-property-name ()
  13375. "Read a property name."
  13376. (let* ((completion-ignore-case t)
  13377. (keys (org-buffer-property-keys nil t t))
  13378. (default-prop (or (save-excursion
  13379. (save-match-data
  13380. (beginning-of-line)
  13381. (and (looking-at "^\\s-*:\\([^:\n]+\\):")
  13382. (null (string= (match-string 1) "END"))
  13383. (match-string 1))))
  13384. org-last-set-property))
  13385. (property (org-icompleting-read
  13386. (concat "Property"
  13387. (if default-prop (concat " [" default-prop "]") "")
  13388. ": ")
  13389. (mapcar 'list keys)
  13390. nil nil nil nil
  13391. default-prop
  13392. )))
  13393. (if (member property keys)
  13394. property
  13395. (or (cdr (assoc (downcase property)
  13396. (mapcar (lambda (x) (cons (downcase x) x))
  13397. keys)))
  13398. property))))
  13399. (defun org-set-property (property value)
  13400. "In the current entry, set PROPERTY to VALUE.
  13401. When called interactively, this will prompt for a property name, offering
  13402. completion on existing and default properties. And then it will prompt
  13403. for a value, offering completion either on allowed values (via an inherited
  13404. xxx_ALL property) or on existing values in other instances of this property
  13405. in the current file."
  13406. (interactive (list nil nil))
  13407. (let* ((property (or property (org-read-property-name)))
  13408. (value (or value (org-read-property-value property)))
  13409. (fn (assoc property org-properties-postprocess-alist)))
  13410. (setq org-last-set-property property)
  13411. ;; Possibly postprocess the inserted value:
  13412. (when fn (setq value (funcall (cadr fn) value)))
  13413. (unless (equal (org-entry-get nil property) value)
  13414. (org-entry-put nil property value))))
  13415. (defun org-delete-property (property)
  13416. "In the current entry, delete PROPERTY."
  13417. (interactive
  13418. (let* ((completion-ignore-case t)
  13419. (prop (org-icompleting-read "Property: "
  13420. (org-entry-properties nil 'standard))))
  13421. (list prop)))
  13422. (message "Property %s %s" property
  13423. (if (org-entry-delete nil property)
  13424. "deleted"
  13425. "was not present in the entry")))
  13426. (defun org-delete-property-globally (property)
  13427. "Remove PROPERTY globally, from all entries."
  13428. (interactive
  13429. (let* ((completion-ignore-case t)
  13430. (prop (org-icompleting-read
  13431. "Globally remove property: "
  13432. (mapcar 'list (org-buffer-property-keys)))))
  13433. (list prop)))
  13434. (save-excursion
  13435. (save-restriction
  13436. (widen)
  13437. (goto-char (point-min))
  13438. (let ((cnt 0))
  13439. (while (re-search-forward
  13440. (org-re-property property)
  13441. nil t)
  13442. (setq cnt (1+ cnt))
  13443. (delete-region (match-beginning 0) (1+ (point-at-eol))))
  13444. (message "Property \"%s\" removed from %d entries" property cnt)))))
  13445. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  13446. (defun org-compute-property-at-point ()
  13447. "Compute the property at point.
  13448. This looks for an enclosing column format, extracts the operator and
  13449. then applies it to the property in the column format's scope."
  13450. (interactive)
  13451. (unless (org-at-property-p)
  13452. (error "Not at a property"))
  13453. (let ((prop (org-match-string-no-properties 2)))
  13454. (org-columns-get-format-and-top-level)
  13455. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  13456. (error "No operator defined for property %s" prop))
  13457. (org-columns-compute prop)))
  13458. (defvar org-property-allowed-value-functions nil
  13459. "Hook for functions supplying allowed values for a specific property.
  13460. The functions must take a single argument, the name of the property, and
  13461. return a flat list of allowed values. If \":ETC\" is one of
  13462. the values, this means that these values are intended as defaults for
  13463. completion, but that other values should be allowed too.
  13464. The functions must return nil if they are not responsible for this
  13465. property.")
  13466. (defun org-property-get-allowed-values (pom property &optional table)
  13467. "Get allowed values for the property PROPERTY.
  13468. When TABLE is non-nil, return an alist that can directly be used for
  13469. completion."
  13470. (let (vals)
  13471. (cond
  13472. ((equal property "TODO")
  13473. (setq vals (org-with-point-at pom
  13474. (append org-todo-keywords-1 '("")))))
  13475. ((equal property "PRIORITY")
  13476. (let ((n org-lowest-priority))
  13477. (while (>= n org-highest-priority)
  13478. (push (char-to-string n) vals)
  13479. (setq n (1- n)))))
  13480. ((member property org-special-properties))
  13481. ((setq vals (run-hook-with-args-until-success
  13482. 'org-property-allowed-value-functions property)))
  13483. (t
  13484. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  13485. (when (and vals (string-match "\\S-" vals))
  13486. (setq vals (car (read-from-string (concat "(" vals ")"))))
  13487. (setq vals (mapcar (lambda (x)
  13488. (cond ((stringp x) x)
  13489. ((numberp x) (number-to-string x))
  13490. ((symbolp x) (symbol-name x))
  13491. (t "???")))
  13492. vals)))))
  13493. (when (member ":ETC" vals)
  13494. (setq vals (remove ":ETC" vals))
  13495. (org-add-props (car vals) '(org-unrestricted t)))
  13496. (if table (mapcar 'list vals) vals)))
  13497. (defun org-property-previous-allowed-value (&optional previous)
  13498. "Switch to the next allowed value for this property."
  13499. (interactive)
  13500. (org-property-next-allowed-value t))
  13501. (defun org-property-next-allowed-value (&optional previous)
  13502. "Switch to the next allowed value for this property."
  13503. (interactive)
  13504. (unless (org-at-property-p)
  13505. (error "Not at a property"))
  13506. (let* ((key (match-string 2))
  13507. (value (match-string 3))
  13508. (allowed (or (org-property-get-allowed-values (point) key)
  13509. (and (member value '("[ ]" "[-]" "[X]"))
  13510. '("[ ]" "[X]"))))
  13511. nval)
  13512. (unless allowed
  13513. (error "Allowed values for this property have not been defined"))
  13514. (if previous (setq allowed (reverse allowed)))
  13515. (if (member value allowed)
  13516. (setq nval (car (cdr (member value allowed)))))
  13517. (setq nval (or nval (car allowed)))
  13518. (if (equal nval value)
  13519. (error "Only one allowed value for this property"))
  13520. (org-at-property-p)
  13521. (replace-match (concat " :" key ": " nval) t t)
  13522. (org-indent-line-function)
  13523. (beginning-of-line 1)
  13524. (skip-chars-forward " \t")
  13525. (run-hook-with-args 'org-property-changed-functions key nval)))
  13526. (defun org-find-olp (path &optional this-buffer)
  13527. "Return a marker pointing to the entry at outline path OLP.
  13528. If anything goes wrong, throw an error.
  13529. You can wrap this call to catch the error like this:
  13530. (condition-case msg
  13531. (org-mobile-locate-entry (match-string 4))
  13532. (error (nth 1 msg)))
  13533. The return value will then be either a string with the error message,
  13534. or a marker if everything is OK.
  13535. If THIS-BUFFER is set, the outline path does not contain a file,
  13536. only headings."
  13537. (let* ((file (if this-buffer buffer-file-name (pop path)))
  13538. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  13539. (level 1)
  13540. (lmin 1)
  13541. (lmax 1)
  13542. limit re end found pos heading cnt flevel)
  13543. (unless buffer (error "File not found :%s" file))
  13544. (with-current-buffer buffer
  13545. (save-excursion
  13546. (save-restriction
  13547. (widen)
  13548. (setq limit (point-max))
  13549. (goto-char (point-min))
  13550. (while (setq heading (pop path))
  13551. (setq re (format org-complex-heading-regexp-format
  13552. (regexp-quote heading)))
  13553. (setq cnt 0 pos (point))
  13554. (while (re-search-forward re end t)
  13555. (setq level (- (match-end 1) (match-beginning 1)))
  13556. (if (and (>= level lmin) (<= level lmax))
  13557. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  13558. (when (= cnt 0) (error "Heading not found on level %d: %s"
  13559. lmax heading))
  13560. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  13561. lmax heading))
  13562. (goto-char found)
  13563. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  13564. (setq end (save-excursion (org-end-of-subtree t t))))
  13565. (when (org-at-heading-p)
  13566. (move-marker (make-marker) (point))))))))
  13567. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  13568. "Find node HEADING in BUFFER.
  13569. Return a marker to the heading if it was found, or nil if not.
  13570. If POS-ONLY is set, return just the position instead of a marker.
  13571. The heading text must match exact, but it may have a TODO keyword,
  13572. a priority cookie and tags in the standard locations."
  13573. (with-current-buffer (or buffer (current-buffer))
  13574. (save-excursion
  13575. (save-restriction
  13576. (widen)
  13577. (goto-char (point-min))
  13578. (let (case-fold-search)
  13579. (if (re-search-forward
  13580. (format org-complex-heading-regexp-format
  13581. (regexp-quote heading)) nil t)
  13582. (if pos-only
  13583. (match-beginning 0)
  13584. (move-marker (make-marker) (match-beginning 0)))))))))
  13585. (defun org-find-exact-heading-in-directory (heading &optional dir)
  13586. "Find Org node headline HEADING in all .org files in directory DIR.
  13587. When the target headline is found, return a marker to this location."
  13588. (let ((files (directory-files (or dir default-directory)
  13589. nil "\\`[^.#].*\\.org\\'"))
  13590. file visiting m buffer)
  13591. (catch 'found
  13592. (while (setq file (pop files))
  13593. (message "trying %s" file)
  13594. (setq visiting (org-find-base-buffer-visiting file))
  13595. (setq buffer (or visiting (find-file-noselect file)))
  13596. (setq m (org-find-exact-headline-in-buffer
  13597. heading buffer))
  13598. (when (and (not m) (not visiting)) (kill-buffer buffer))
  13599. (and m (throw 'found m))))))
  13600. (defun org-find-entry-with-id (ident)
  13601. "Locate the entry that contains the ID property with exact value IDENT.
  13602. IDENT can be a string, a symbol or a number, this function will search for
  13603. the string representation of it.
  13604. Return the position where this entry starts, or nil if there is no such entry."
  13605. (interactive "sID: ")
  13606. (let ((id (cond
  13607. ((stringp ident) ident)
  13608. ((symbol-name ident) (symbol-name ident))
  13609. ((numberp ident) (number-to-string ident))
  13610. (t (error "IDENT %s must be a string, symbol or number" ident))))
  13611. (case-fold-search nil))
  13612. (save-excursion
  13613. (save-restriction
  13614. (widen)
  13615. (goto-char (point-min))
  13616. (when (re-search-forward
  13617. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  13618. nil t)
  13619. (org-back-to-heading t)
  13620. (point))))))
  13621. ;;;; Timestamps
  13622. (defvar org-last-changed-timestamp nil)
  13623. (defvar org-last-inserted-timestamp nil
  13624. "The last time stamp inserted with `org-insert-time-stamp'.")
  13625. (defvar org-time-was-given) ; dynamically scoped parameter
  13626. (defvar org-end-time-was-given) ; dynamically scoped parameter
  13627. (defvar org-ts-what) ; dynamically scoped parameter
  13628. (defun org-time-stamp (arg &optional inactive)
  13629. "Prompt for a date/time and insert a time stamp.
  13630. If the user specifies a time like HH:MM, or if this command is called
  13631. with a prefix argument, the time stamp will contain date and time.
  13632. Otherwise, only the date will be included. All parts of a date not
  13633. specified by the user will be filled in from the current date/time.
  13634. So if you press just return without typing anything, the time stamp
  13635. will represent the current date/time. If there is already a timestamp
  13636. at the cursor, it will be modified."
  13637. (interactive "P")
  13638. (let* ((ts nil)
  13639. (default-time
  13640. ;; Default time is either today, or, when entering a range,
  13641. ;; the range start.
  13642. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  13643. (save-excursion
  13644. (re-search-backward
  13645. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  13646. (- (point) 20) t)))
  13647. (apply 'encode-time (org-parse-time-string (match-string 1)))
  13648. (current-time)))
  13649. (default-input (and ts (org-get-compact-tod ts)))
  13650. (repeater (save-excursion
  13651. (save-match-data
  13652. (beginning-of-line)
  13653. (when (re-search-forward
  13654. "\\([.+-]+[0-9]+[dwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[dwmy]\\)?\\) ?"
  13655. (save-excursion (progn (end-of-line) (point))) t)
  13656. (match-string 0)))))
  13657. org-time-was-given org-end-time-was-given time)
  13658. (cond
  13659. ((and (org-at-timestamp-p t)
  13660. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  13661. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  13662. (insert "--")
  13663. (setq time (let ((this-command this-command))
  13664. (org-read-date arg 'totime nil nil
  13665. default-time default-input inactive)))
  13666. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  13667. ((org-at-timestamp-p t)
  13668. (setq time (let ((this-command this-command))
  13669. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  13670. (when (org-at-timestamp-p t) ; just to get the match data
  13671. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  13672. (replace-match "")
  13673. (setq org-last-changed-timestamp
  13674. (org-insert-time-stamp
  13675. time (or org-time-was-given arg)
  13676. inactive nil nil (list org-end-time-was-given)))
  13677. (when repeater (goto-char (1- (point))) (insert " " repeater)
  13678. (setq org-last-changed-timestamp
  13679. (concat (substring org-last-inserted-timestamp 0 -1)
  13680. " " repeater ">"))))
  13681. (message "Timestamp updated"))
  13682. (t
  13683. (setq time (let ((this-command this-command))
  13684. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  13685. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  13686. nil nil (list org-end-time-was-given))))))
  13687. ;; FIXME: can we use this for something else, like computing time differences?
  13688. (defun org-get-compact-tod (s)
  13689. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  13690. (let* ((t1 (match-string 1 s))
  13691. (h1 (string-to-number (match-string 2 s)))
  13692. (m1 (string-to-number (match-string 3 s)))
  13693. (t2 (and (match-end 4) (match-string 5 s)))
  13694. (h2 (and t2 (string-to-number (match-string 6 s))))
  13695. (m2 (and t2 (string-to-number (match-string 7 s))))
  13696. dh dm)
  13697. (if (not t2)
  13698. t1
  13699. (setq dh (- h2 h1) dm (- m2 m1))
  13700. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  13701. (concat t1 "+" (number-to-string dh)
  13702. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  13703. (defun org-time-stamp-inactive (&optional arg)
  13704. "Insert an inactive time stamp.
  13705. An inactive time stamp is enclosed in square brackets instead of angle
  13706. brackets. It is inactive in the sense that it does not trigger agenda entries,
  13707. does not link to the calendar and cannot be changed with the S-cursor keys.
  13708. So these are more for recording a certain time/date."
  13709. (interactive "P")
  13710. (org-time-stamp arg 'inactive))
  13711. (defvar org-date-ovl (make-overlay 1 1))
  13712. (overlay-put org-date-ovl 'face 'org-warning)
  13713. (org-detach-overlay org-date-ovl)
  13714. (defvar org-ans1) ; dynamically scoped parameter
  13715. (defvar org-ans2) ; dynamically scoped parameter
  13716. (defvar org-plain-time-of-day-regexp) ; defined below
  13717. (defvar org-overriding-default-time nil) ; dynamically scoped
  13718. (defvar org-read-date-overlay nil)
  13719. (defvar org-dcst nil) ; dynamically scoped
  13720. (defvar org-read-date-history nil)
  13721. (defvar org-read-date-final-answer nil)
  13722. (defvar org-read-date-analyze-futurep nil)
  13723. (defvar org-read-date-analyze-forced-year nil)
  13724. (defvar org-read-date-inactive)
  13725. (defun org-read-date (&optional org-with-time to-time from-string prompt
  13726. default-time default-input inactive)
  13727. "Read a date, possibly a time, and make things smooth for the user.
  13728. The prompt will suggest to enter an ISO date, but you can also enter anything
  13729. which will at least partially be understood by `parse-time-string'.
  13730. Unrecognized parts of the date will default to the current day, month, year,
  13731. hour and minute. If this command is called to replace a timestamp at point,
  13732. or to enter the second timestamp of a range, the default time is taken
  13733. from the existing stamp. Furthermore, the command prefers the future,
  13734. so if you are giving a date where the year is not given, and the day-month
  13735. combination is already past in the current year, it will assume you
  13736. mean next year. For details, see the manual. A few examples:
  13737. 3-2-5 --> 2003-02-05
  13738. feb 15 --> currentyear-02-15
  13739. 2/15 --> currentyear-02-15
  13740. sep 12 9 --> 2009-09-12
  13741. 12:45 --> today 12:45
  13742. 22 sept 0:34 --> currentyear-09-22 0:34
  13743. 12 --> currentyear-currentmonth-12
  13744. Fri --> nearest Friday (today or later)
  13745. etc.
  13746. Furthermore you can specify a relative date by giving, as the *first* thing
  13747. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  13748. change in days weeks, months, years.
  13749. With a single plus or minus, the date is relative to today. With a double
  13750. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  13751. +4d --> four days from today
  13752. +4 --> same as above
  13753. +2w --> two weeks from today
  13754. ++5 --> five days from default date
  13755. The function understands only English month and weekday abbreviations.
  13756. While prompting, a calendar is popped up - you can also select the
  13757. date with the mouse (button 1). The calendar shows a period of three
  13758. months. To scroll it to other months, use the keys `>' and `<'.
  13759. If you don't like the calendar, turn it off with
  13760. \(setq org-read-date-popup-calendar nil)
  13761. With optional argument TO-TIME, the date will immediately be converted
  13762. to an internal time.
  13763. With an optional argument WITH-TIME, the prompt will suggest to also
  13764. insert a time. Note that when WITH-TIME is not set, you can still
  13765. enter a time, and this function will inform the calling routine about
  13766. this change. The calling routine may then choose to change the format
  13767. used to insert the time stamp into the buffer to include the time.
  13768. With optional argument FROM-STRING, read from this string instead from
  13769. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  13770. the time/date that is used for everything that is not specified by the
  13771. user."
  13772. (require 'parse-time)
  13773. (let* ((org-time-stamp-rounding-minutes
  13774. (if (equal org-with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  13775. (org-dcst org-display-custom-times)
  13776. (ct (org-current-time))
  13777. (org-def (or org-overriding-default-time default-time ct))
  13778. (org-defdecode (decode-time org-def))
  13779. (dummy (progn
  13780. (when (< (nth 2 org-defdecode) org-extend-today-until)
  13781. (setcar (nthcdr 2 org-defdecode) -1)
  13782. (setcar (nthcdr 1 org-defdecode) 59)
  13783. (setq org-def (apply 'encode-time org-defdecode)
  13784. org-defdecode (decode-time org-def)))))
  13785. (calendar-frame-setup nil)
  13786. (calendar-setup nil)
  13787. (calendar-move-hook nil)
  13788. (calendar-view-diary-initially-flag nil)
  13789. (calendar-view-holidays-initially-flag nil)
  13790. (timestr (format-time-string
  13791. (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") org-def))
  13792. (prompt (concat (if prompt (concat prompt " ") "")
  13793. (format "Date+time [%s]: " timestr)))
  13794. ans (org-ans0 "") org-ans1 org-ans2 final)
  13795. (cond
  13796. (from-string (setq ans from-string))
  13797. (org-read-date-popup-calendar
  13798. (save-excursion
  13799. (save-window-excursion
  13800. (calendar)
  13801. (unwind-protect
  13802. (progn
  13803. (calendar-forward-day (- (time-to-days org-def)
  13804. (calendar-absolute-from-gregorian
  13805. (calendar-current-date))))
  13806. (org-eval-in-calendar nil t)
  13807. (let* ((old-map (current-local-map))
  13808. (map (copy-keymap calendar-mode-map))
  13809. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  13810. (org-defkey map (kbd "RET") 'org-calendar-select)
  13811. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  13812. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  13813. (org-defkey minibuffer-local-map [(meta shift left)]
  13814. (lambda () (interactive)
  13815. (org-eval-in-calendar '(calendar-backward-month 1))))
  13816. (org-defkey minibuffer-local-map [(meta shift right)]
  13817. (lambda () (interactive)
  13818. (org-eval-in-calendar '(calendar-forward-month 1))))
  13819. (org-defkey minibuffer-local-map [(meta shift up)]
  13820. (lambda () (interactive)
  13821. (org-eval-in-calendar '(calendar-backward-year 1))))
  13822. (org-defkey minibuffer-local-map [(meta shift down)]
  13823. (lambda () (interactive)
  13824. (org-eval-in-calendar '(calendar-forward-year 1))))
  13825. (org-defkey minibuffer-local-map [?\e (shift left)]
  13826. (lambda () (interactive)
  13827. (org-eval-in-calendar '(calendar-backward-month 1))))
  13828. (org-defkey minibuffer-local-map [?\e (shift right)]
  13829. (lambda () (interactive)
  13830. (org-eval-in-calendar '(calendar-forward-month 1))))
  13831. (org-defkey minibuffer-local-map [?\e (shift up)]
  13832. (lambda () (interactive)
  13833. (org-eval-in-calendar '(calendar-backward-year 1))))
  13834. (org-defkey minibuffer-local-map [?\e (shift down)]
  13835. (lambda () (interactive)
  13836. (org-eval-in-calendar '(calendar-forward-year 1))))
  13837. (org-defkey minibuffer-local-map [(shift up)]
  13838. (lambda () (interactive)
  13839. (org-eval-in-calendar '(calendar-backward-week 1))))
  13840. (org-defkey minibuffer-local-map [(shift down)]
  13841. (lambda () (interactive)
  13842. (org-eval-in-calendar '(calendar-forward-week 1))))
  13843. (org-defkey minibuffer-local-map [(shift left)]
  13844. (lambda () (interactive)
  13845. (org-eval-in-calendar '(calendar-backward-day 1))))
  13846. (org-defkey minibuffer-local-map [(shift right)]
  13847. (lambda () (interactive)
  13848. (org-eval-in-calendar '(calendar-forward-day 1))))
  13849. (org-defkey minibuffer-local-map ">"
  13850. (lambda () (interactive)
  13851. (org-eval-in-calendar '(scroll-calendar-left 1))))
  13852. (org-defkey minibuffer-local-map "<"
  13853. (lambda () (interactive)
  13854. (org-eval-in-calendar '(scroll-calendar-right 1))))
  13855. (org-defkey minibuffer-local-map "\C-v"
  13856. (lambda () (interactive)
  13857. (org-eval-in-calendar
  13858. '(calendar-scroll-left-three-months 1))))
  13859. (org-defkey minibuffer-local-map "\M-v"
  13860. (lambda () (interactive)
  13861. (org-eval-in-calendar
  13862. '(calendar-scroll-right-three-months 1))))
  13863. (run-hooks 'org-read-date-minibuffer-setup-hook)
  13864. (unwind-protect
  13865. (progn
  13866. (use-local-map map)
  13867. (setq org-read-date-inactive inactive)
  13868. (add-hook 'post-command-hook 'org-read-date-display)
  13869. (setq org-ans0 (read-string prompt default-input
  13870. 'org-read-date-history nil))
  13871. ;; org-ans0: from prompt
  13872. ;; org-ans1: from mouse click
  13873. ;; org-ans2: from calendar motion
  13874. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  13875. (remove-hook 'post-command-hook 'org-read-date-display)
  13876. (use-local-map old-map)
  13877. (when org-read-date-overlay
  13878. (delete-overlay org-read-date-overlay)
  13879. (setq org-read-date-overlay nil)))))
  13880. (bury-buffer "*Calendar*")))))
  13881. (t ; Naked prompt only
  13882. (unwind-protect
  13883. (setq ans (read-string prompt default-input
  13884. 'org-read-date-history timestr))
  13885. (when org-read-date-overlay
  13886. (delete-overlay org-read-date-overlay)
  13887. (setq org-read-date-overlay nil)))))
  13888. (setq final (org-read-date-analyze ans org-def org-defdecode))
  13889. (when org-read-date-analyze-forced-year
  13890. (message "Year was forced into %s"
  13891. (if org-read-date-force-compatible-dates
  13892. "compatible range (1970-2037)"
  13893. "range representable on this machine"))
  13894. (ding))
  13895. ;; One round trip to get rid of 34th of August and stuff like that....
  13896. (setq final (decode-time (apply 'encode-time final)))
  13897. (setq org-read-date-final-answer ans)
  13898. (if to-time
  13899. (apply 'encode-time final)
  13900. (if (and (boundp 'org-time-was-given) org-time-was-given)
  13901. (format "%04d-%02d-%02d %02d:%02d"
  13902. (nth 5 final) (nth 4 final) (nth 3 final)
  13903. (nth 2 final) (nth 1 final))
  13904. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  13905. (defvar org-def)
  13906. (defvar org-defdecode)
  13907. (defvar org-with-time)
  13908. (defun org-read-date-display ()
  13909. "Display the current date prompt interpretation in the minibuffer."
  13910. (when org-read-date-display-live
  13911. (when org-read-date-overlay
  13912. (delete-overlay org-read-date-overlay))
  13913. (when (minibufferp (current-buffer))
  13914. (save-excursion
  13915. (end-of-line 1)
  13916. (while (not (equal (buffer-substring
  13917. (max (point-min) (- (point) 4)) (point))
  13918. " "))
  13919. (insert " ")))
  13920. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  13921. " " (or org-ans1 org-ans2)))
  13922. (org-end-time-was-given nil)
  13923. (f (org-read-date-analyze ans org-def org-defdecode))
  13924. (fmts (if org-dcst
  13925. org-time-stamp-custom-formats
  13926. org-time-stamp-formats))
  13927. (fmt (if (or org-with-time
  13928. (and (boundp 'org-time-was-given) org-time-was-given))
  13929. (cdr fmts)
  13930. (car fmts)))
  13931. (txt (format-time-string fmt (apply 'encode-time f)))
  13932. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  13933. (txt (concat "=> " txt)))
  13934. (when (and org-end-time-was-given
  13935. (string-match org-plain-time-of-day-regexp txt))
  13936. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  13937. org-end-time-was-given
  13938. (substring txt (match-end 0)))))
  13939. (when org-read-date-analyze-futurep
  13940. (setq txt (concat txt " (=>F)")))
  13941. (setq org-read-date-overlay
  13942. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  13943. (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
  13944. (defun org-read-date-analyze (ans org-def org-defdecode)
  13945. "Analyze the combined answer of the date prompt."
  13946. ;; FIXME: cleanup and comment
  13947. (let ((nowdecode (decode-time (current-time)))
  13948. delta deltan deltaw deltadef year month day
  13949. hour minute second wday pm h2 m2 tl wday1
  13950. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  13951. (setq org-read-date-analyze-futurep nil
  13952. org-read-date-analyze-forced-year nil)
  13953. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  13954. (setq ans "+0"))
  13955. (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
  13956. (setq ans (replace-match "" t t ans)
  13957. deltan (car delta)
  13958. deltaw (nth 1 delta)
  13959. deltadef (nth 2 delta)))
  13960. ;; Check if there is an iso week date in there
  13961. ;; If yes, store the info and postpone interpreting it until the rest
  13962. ;; of the parsing is done
  13963. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  13964. (setq iso-year (if (match-end 1)
  13965. (org-small-year-to-year
  13966. (string-to-number (match-string 1 ans))))
  13967. iso-weekday (if (match-end 3)
  13968. (string-to-number (match-string 3 ans)))
  13969. iso-week (string-to-number (match-string 2 ans)))
  13970. (setq ans (replace-match "" t t ans)))
  13971. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  13972. (when (string-match
  13973. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  13974. (setq year (if (match-end 2)
  13975. (string-to-number (match-string 2 ans))
  13976. (progn (setq kill-year t)
  13977. (string-to-number (format-time-string "%Y"))))
  13978. month (string-to-number (match-string 3 ans))
  13979. day (string-to-number (match-string 4 ans)))
  13980. (if (< year 100) (setq year (+ 2000 year)))
  13981. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  13982. t nil ans)))
  13983. ;; Help matching dotted european dates
  13984. (when (string-match
  13985. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\. ?\\([1-9][0-9][0-9][0-9]\\)?" ans)
  13986. (setq year (if (match-end 3)
  13987. (string-to-number (match-string 3 ans))
  13988. (progn (setq kill-year t)
  13989. (string-to-number (format-time-string "%Y"))))
  13990. day (string-to-number (match-string 1 ans))
  13991. month (string-to-number (match-string 2 ans))
  13992. ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  13993. t nil ans)))
  13994. ;; Help matching american dates, like 5/30 or 5/30/7
  13995. (when (string-match
  13996. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  13997. (setq year (if (match-end 4)
  13998. (string-to-number (match-string 4 ans))
  13999. (progn (setq kill-year t)
  14000. (string-to-number (format-time-string "%Y"))))
  14001. month (string-to-number (match-string 1 ans))
  14002. day (string-to-number (match-string 2 ans)))
  14003. (if (< year 100) (setq year (+ 2000 year)))
  14004. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  14005. t nil ans)))
  14006. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  14007. ;; If there is a time with am/pm, and *no* time without it, we convert
  14008. ;; so that matching will be successful.
  14009. (loop for i from 1 to 2 do ; twice, for end time as well
  14010. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  14011. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  14012. (setq hour (string-to-number (match-string 1 ans))
  14013. minute (if (match-end 3)
  14014. (string-to-number (match-string 3 ans))
  14015. 0)
  14016. pm (equal ?p
  14017. (string-to-char (downcase (match-string 4 ans)))))
  14018. (if (and (= hour 12) (not pm))
  14019. (setq hour 0)
  14020. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  14021. (setq ans (replace-match (format "%02d:%02d" hour minute)
  14022. t t ans))))
  14023. ;; Check if a time range is given as a duration
  14024. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  14025. (setq hour (string-to-number (match-string 1 ans))
  14026. h2 (+ hour (string-to-number (match-string 3 ans)))
  14027. minute (string-to-number (match-string 2 ans))
  14028. m2 (+ minute (if (match-end 5) (string-to-number
  14029. (match-string 5 ans))0)))
  14030. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  14031. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  14032. t t ans)))
  14033. ;; Check if there is a time range
  14034. (when (boundp 'org-end-time-was-given)
  14035. (setq org-time-was-given nil)
  14036. (when (and (string-match org-plain-time-of-day-regexp ans)
  14037. (match-end 8))
  14038. (setq org-end-time-was-given (match-string 8 ans))
  14039. (setq ans (concat (substring ans 0 (match-beginning 7))
  14040. (substring ans (match-end 7))))))
  14041. (setq tl (parse-time-string ans)
  14042. day (or (nth 3 tl) (nth 3 org-defdecode))
  14043. month (or (nth 4 tl)
  14044. (if (and org-read-date-prefer-future
  14045. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  14046. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  14047. (nth 4 org-defdecode)))
  14048. year (or (and (not kill-year) (nth 5 tl))
  14049. (if (and org-read-date-prefer-future
  14050. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  14051. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  14052. (nth 5 org-defdecode)))
  14053. hour (or (nth 2 tl) (nth 2 org-defdecode))
  14054. minute (or (nth 1 tl) (nth 1 org-defdecode))
  14055. second (or (nth 0 tl) 0)
  14056. wday (nth 6 tl))
  14057. (when (and (eq org-read-date-prefer-future 'time)
  14058. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  14059. (equal day (nth 3 nowdecode))
  14060. (equal month (nth 4 nowdecode))
  14061. (equal year (nth 5 nowdecode))
  14062. (nth 2 tl)
  14063. (or (< (nth 2 tl) (nth 2 nowdecode))
  14064. (and (= (nth 2 tl) (nth 2 nowdecode))
  14065. (nth 1 tl)
  14066. (< (nth 1 tl) (nth 1 nowdecode)))))
  14067. (setq day (1+ day)
  14068. futurep t))
  14069. ;; Special date definitions below
  14070. (cond
  14071. (iso-week
  14072. ;; There was an iso week
  14073. (require 'cal-iso)
  14074. (setq futurep nil)
  14075. (setq year (or iso-year year)
  14076. day (or iso-weekday wday 1)
  14077. wday nil ; to make sure that the trigger below does not match
  14078. iso-date (calendar-gregorian-from-absolute
  14079. (calendar-absolute-from-iso
  14080. (list iso-week day year))))
  14081. ; FIXME: Should we also push ISO weeks into the future?
  14082. ; (when (and org-read-date-prefer-future
  14083. ; (not iso-year)
  14084. ; (< (calendar-absolute-from-gregorian iso-date)
  14085. ; (time-to-days (current-time))))
  14086. ; (setq year (1+ year)
  14087. ; iso-date (calendar-gregorian-from-absolute
  14088. ; (calendar-absolute-from-iso
  14089. ; (list iso-week day year)))))
  14090. (setq month (car iso-date)
  14091. year (nth 2 iso-date)
  14092. day (nth 1 iso-date)))
  14093. (deltan
  14094. (setq futurep nil)
  14095. (unless deltadef
  14096. (let ((now (decode-time (current-time))))
  14097. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  14098. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  14099. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  14100. ((equal deltaw "m") (setq month (+ month deltan)))
  14101. ((equal deltaw "y") (setq year (+ year deltan)))))
  14102. ((and wday (not (nth 3 tl)))
  14103. (setq futurep nil)
  14104. ;; Weekday was given, but no day, so pick that day in the week
  14105. ;; on or after the derived date.
  14106. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  14107. (unless (equal wday wday1)
  14108. (setq day (+ day (% (- wday wday1 -7) 7))))))
  14109. (if (and (boundp 'org-time-was-given)
  14110. (nth 2 tl))
  14111. (setq org-time-was-given t))
  14112. (if (< year 100) (setq year (+ 2000 year)))
  14113. ;; Check of the date is representable
  14114. (if org-read-date-force-compatible-dates
  14115. (progn
  14116. (if (< year 1970)
  14117. (setq year 1970 org-read-date-analyze-forced-year t))
  14118. (if (> year 2037)
  14119. (setq year 2037 org-read-date-analyze-forced-year t)))
  14120. (condition-case nil
  14121. (ignore (encode-time second minute hour day month year))
  14122. (error
  14123. (setq year (nth 5 org-defdecode))
  14124. (setq org-read-date-analyze-forced-year t))))
  14125. (setq org-read-date-analyze-futurep futurep)
  14126. (list second minute hour day month year)))
  14127. (defvar parse-time-weekdays)
  14128. (defun org-read-date-get-relative (s today default)
  14129. "Check string S for special relative date string.
  14130. TODAY and DEFAULT are internal times, for today and for a default.
  14131. Return shift list (N what def-flag)
  14132. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  14133. N is the number of WHATs to shift.
  14134. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  14135. the DEFAULT date rather than TODAY."
  14136. (require 'parse-time)
  14137. (when (and
  14138. (string-match
  14139. (concat
  14140. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  14141. "\\([0-9]+\\)?"
  14142. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  14143. "\\([ \t]\\|$\\)") s)
  14144. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  14145. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  14146. (string-to-char (substring (match-string 1 s) -1))
  14147. ?+))
  14148. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  14149. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  14150. (what (if (match-end 3) (match-string 3 s) "d"))
  14151. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  14152. (date (if rel default today))
  14153. (wday (nth 6 (decode-time date)))
  14154. delta)
  14155. (if wday1
  14156. (progn
  14157. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  14158. (if (= dir ?-) (setq delta (- delta 7)))
  14159. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  14160. (list delta "d" rel))
  14161. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  14162. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  14163. "Turn a user-specified date into the internal representation.
  14164. The internal representation needed by the calendar is (month day year).
  14165. This is a wrapper to handle the brain-dead convention in calendar that
  14166. user function argument order change dependent on argument order."
  14167. (if (boundp 'calendar-date-style)
  14168. (cond
  14169. ((eq calendar-date-style 'american)
  14170. (list arg1 arg2 arg3))
  14171. ((eq calendar-date-style 'european)
  14172. (list arg2 arg1 arg3))
  14173. ((eq calendar-date-style 'iso)
  14174. (list arg2 arg3 arg1)))
  14175. (with-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  14176. (if (org-bound-and-true-p european-calendar-style)
  14177. (list arg2 arg1 arg3)
  14178. (list arg1 arg2 arg3)))))
  14179. (defun org-eval-in-calendar (form &optional keepdate)
  14180. "Eval FORM in the calendar window and return to current window.
  14181. Also, store the cursor date in variable org-ans2."
  14182. (let ((sf (selected-frame))
  14183. (sw (selected-window)))
  14184. (select-window (get-buffer-window "*Calendar*" t))
  14185. (eval form)
  14186. (when (and (not keepdate) (calendar-cursor-to-date))
  14187. (let* ((date (calendar-cursor-to-date))
  14188. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14189. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  14190. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  14191. (select-window sw)
  14192. (org-select-frame-set-input-focus sf)))
  14193. (defun org-calendar-select ()
  14194. "Return to `org-read-date' with the date currently selected.
  14195. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  14196. (interactive)
  14197. (when (calendar-cursor-to-date)
  14198. (let* ((date (calendar-cursor-to-date))
  14199. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14200. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  14201. (if (active-minibuffer-window) (exit-minibuffer))))
  14202. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  14203. "Insert a date stamp for the date given by the internal TIME.
  14204. WITH-HM means use the stamp format that includes the time of the day.
  14205. INACTIVE means use square brackets instead of angular ones, so that the
  14206. stamp will not contribute to the agenda.
  14207. PRE and POST are optional strings to be inserted before and after the
  14208. stamp.
  14209. The command returns the inserted time stamp."
  14210. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  14211. stamp)
  14212. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  14213. (insert-before-markers (or pre ""))
  14214. (when (listp extra)
  14215. (setq extra (car extra))
  14216. (if (and (stringp extra)
  14217. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  14218. (setq extra (format "-%02d:%02d"
  14219. (string-to-number (match-string 1 extra))
  14220. (string-to-number (match-string 2 extra))))
  14221. (setq extra nil)))
  14222. (when extra
  14223. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  14224. (insert-before-markers (setq stamp (format-time-string fmt time)))
  14225. (insert-before-markers (or post ""))
  14226. (setq org-last-inserted-timestamp stamp)))
  14227. (defun org-toggle-time-stamp-overlays ()
  14228. "Toggle the use of custom time stamp formats."
  14229. (interactive)
  14230. (setq org-display-custom-times (not org-display-custom-times))
  14231. (unless org-display-custom-times
  14232. (let ((p (point-min)) (bmp (buffer-modified-p)))
  14233. (while (setq p (next-single-property-change p 'display))
  14234. (if (and (get-text-property p 'display)
  14235. (eq (get-text-property p 'face) 'org-date))
  14236. (remove-text-properties
  14237. p (setq p (next-single-property-change p 'display))
  14238. '(display t))))
  14239. (set-buffer-modified-p bmp)))
  14240. (if (featurep 'xemacs)
  14241. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  14242. (org-restart-font-lock)
  14243. (setq org-table-may-need-update t)
  14244. (if org-display-custom-times
  14245. (message "Time stamps are overlaid with custom format")
  14246. (message "Time stamp overlays removed")))
  14247. (defun org-display-custom-time (beg end)
  14248. "Overlay modified time stamp format over timestamp between BEG and END."
  14249. (let* ((ts (buffer-substring beg end))
  14250. t1 w1 with-hm tf time str w2 (off 0))
  14251. (save-match-data
  14252. (setq t1 (org-parse-time-string ts t))
  14253. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)?\\'" ts)
  14254. (setq off (- (match-end 0) (match-beginning 0)))))
  14255. (setq end (- end off))
  14256. (setq w1 (- end beg)
  14257. with-hm (and (nth 1 t1) (nth 2 t1))
  14258. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  14259. time (org-fix-decoded-time t1)
  14260. str (org-add-props
  14261. (format-time-string
  14262. (substring tf 1 -1) (apply 'encode-time time))
  14263. nil 'mouse-face 'highlight)
  14264. w2 (length str))
  14265. (if (not (= w2 w1))
  14266. (add-text-properties (1+ beg) (+ 2 beg)
  14267. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  14268. (if (featurep 'xemacs)
  14269. (progn
  14270. (put-text-property beg end 'invisible t)
  14271. (put-text-property beg end 'end-glyph (make-glyph str)))
  14272. (put-text-property beg end 'display str))))
  14273. (defun org-translate-time (string)
  14274. "Translate all timestamps in STRING to custom format.
  14275. But do this only if the variable `org-display-custom-times' is set."
  14276. (when org-display-custom-times
  14277. (save-match-data
  14278. (let* ((start 0)
  14279. (re org-ts-regexp-both)
  14280. t1 with-hm inactive tf time str beg end)
  14281. (while (setq start (string-match re string start))
  14282. (setq beg (match-beginning 0)
  14283. end (match-end 0)
  14284. t1 (save-match-data
  14285. (org-parse-time-string (substring string beg end) t))
  14286. with-hm (and (nth 1 t1) (nth 2 t1))
  14287. inactive (equal (substring string beg (1+ beg)) "[")
  14288. tf (funcall (if with-hm 'cdr 'car)
  14289. org-time-stamp-custom-formats)
  14290. time (org-fix-decoded-time t1)
  14291. str (format-time-string
  14292. (concat
  14293. (if inactive "[" "<") (substring tf 1 -1)
  14294. (if inactive "]" ">"))
  14295. (apply 'encode-time time))
  14296. string (replace-match str t t string)
  14297. start (+ start (length str)))))))
  14298. string)
  14299. (defun org-fix-decoded-time (time)
  14300. "Set 0 instead of nil for the first 6 elements of time.
  14301. Don't touch the rest."
  14302. (let ((n 0))
  14303. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  14304. (defun org-days-to-time (timestamp-string)
  14305. "Difference between TIMESTAMP-STRING and now in days."
  14306. (- (time-to-days (org-time-string-to-time timestamp-string))
  14307. (time-to-days (current-time))))
  14308. (defun org-deadline-close (timestamp-string &optional ndays)
  14309. "Is the time in TIMESTAMP-STRING close to the current date?"
  14310. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  14311. (and (< (org-days-to-time timestamp-string) ndays)
  14312. (not (org-entry-is-done-p))))
  14313. (defun org-get-wdays (ts)
  14314. "Get the deadline lead time appropriate for timestring TS."
  14315. (cond
  14316. ((<= org-deadline-warning-days 0)
  14317. ;; 0 or negative, enforce this value no matter what
  14318. (- org-deadline-warning-days))
  14319. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
  14320. ;; lead time is specified.
  14321. (floor (* (string-to-number (match-string 1 ts))
  14322. (cdr (assoc (match-string 2 ts)
  14323. '(("d" . 1) ("w" . 7)
  14324. ("m" . 30.4) ("y" . 365.25)))))))
  14325. ;; go for the default.
  14326. (t org-deadline-warning-days)))
  14327. (defun org-calendar-select-mouse (ev)
  14328. "Return to `org-read-date' with the date currently selected.
  14329. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  14330. (interactive "e")
  14331. (mouse-set-point ev)
  14332. (when (calendar-cursor-to-date)
  14333. (let* ((date (calendar-cursor-to-date))
  14334. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14335. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  14336. (if (active-minibuffer-window) (exit-minibuffer))))
  14337. (defun org-check-deadlines (ndays)
  14338. "Check if there are any deadlines due or past due.
  14339. A deadline is considered due if it happens within `org-deadline-warning-days'
  14340. days from today's date. If the deadline appears in an entry marked DONE,
  14341. it is not shown. The prefix arg NDAYS can be used to test that many
  14342. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  14343. (interactive "P")
  14344. (let* ((org-warn-days
  14345. (cond
  14346. ((equal ndays '(4)) 100000)
  14347. (ndays (prefix-numeric-value ndays))
  14348. (t (abs org-deadline-warning-days))))
  14349. (case-fold-search nil)
  14350. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  14351. (callback
  14352. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  14353. (message "%d deadlines past-due or due within %d days"
  14354. (org-occur regexp nil callback)
  14355. org-warn-days)))
  14356. (defun org-check-before-date (date)
  14357. "Check if there are deadlines or scheduled entries before DATE."
  14358. (interactive (list (org-read-date)))
  14359. (let ((case-fold-search nil)
  14360. (regexp (concat "\\<\\(" org-deadline-string
  14361. "\\|" org-scheduled-string
  14362. "\\) *<\\([^>]+\\)>"))
  14363. (callback
  14364. (lambda () (time-less-p
  14365. (org-time-string-to-time (match-string 2))
  14366. (org-time-string-to-time date)))))
  14367. (message "%d entries before %s"
  14368. (org-occur regexp nil callback) date)))
  14369. (defun org-check-after-date (date)
  14370. "Check if there are deadlines or scheduled entries after DATE."
  14371. (interactive (list (org-read-date)))
  14372. (let ((case-fold-search nil)
  14373. (regexp (concat "\\<\\(" org-deadline-string
  14374. "\\|" org-scheduled-string
  14375. "\\) *<\\([^>]+\\)>"))
  14376. (callback
  14377. (lambda () (not
  14378. (time-less-p
  14379. (org-time-string-to-time (match-string 2))
  14380. (org-time-string-to-time date))))))
  14381. (message "%d entries after %s"
  14382. (org-occur regexp nil callback) date)))
  14383. (defun org-check-dates-range (start-date end-date)
  14384. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  14385. (interactive (list (org-read-date nil nil nil "Range starts")
  14386. (org-read-date nil nil nil "Range end")))
  14387. (let ((case-fold-search nil)
  14388. (regexp (concat "\\<\\(" org-deadline-string
  14389. "\\|" org-scheduled-string
  14390. "\\) *<\\([^>]+\\)>"))
  14391. (callback
  14392. (lambda ()
  14393. (let ((match (match-string 2)))
  14394. (and
  14395. (not (time-less-p
  14396. (org-time-string-to-time match)
  14397. (org-time-string-to-time start-date)))
  14398. (time-less-p
  14399. (org-time-string-to-time match)
  14400. (org-time-string-to-time end-date)))))))
  14401. (message "%d entries between %s and %s"
  14402. (org-occur regexp nil callback) start-date end-date)))
  14403. (defun org-evaluate-time-range (&optional to-buffer)
  14404. "Evaluate a time range by computing the difference between start and end.
  14405. Normally the result is just printed in the echo area, but with prefix arg
  14406. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  14407. If the time range is actually in a table, the result is inserted into the
  14408. next column.
  14409. For time difference computation, a year is assumed to be exactly 365
  14410. days in order to avoid rounding problems."
  14411. (interactive "P")
  14412. (or
  14413. (org-clock-update-time-maybe)
  14414. (save-excursion
  14415. (unless (org-at-date-range-p t)
  14416. (goto-char (point-at-bol))
  14417. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  14418. (if (not (org-at-date-range-p t))
  14419. (error "Not at a time-stamp range, and none found in current line")))
  14420. (let* ((ts1 (match-string 1))
  14421. (ts2 (match-string 2))
  14422. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  14423. (match-end (match-end 0))
  14424. (time1 (org-time-string-to-time ts1))
  14425. (time2 (org-time-string-to-time ts2))
  14426. (t1 (org-float-time time1))
  14427. (t2 (org-float-time time2))
  14428. (diff (abs (- t2 t1)))
  14429. (negative (< (- t2 t1) 0))
  14430. ;; (ys (floor (* 365 24 60 60)))
  14431. (ds (* 24 60 60))
  14432. (hs (* 60 60))
  14433. (fy "%dy %dd %02d:%02d")
  14434. (fy1 "%dy %dd")
  14435. (fd "%dd %02d:%02d")
  14436. (fd1 "%dd")
  14437. (fh "%02d:%02d")
  14438. y d h m align)
  14439. (if havetime
  14440. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  14441. y 0
  14442. d (floor (/ diff ds)) diff (mod diff ds)
  14443. h (floor (/ diff hs)) diff (mod diff hs)
  14444. m (floor (/ diff 60)))
  14445. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  14446. y 0
  14447. d (floor (+ (/ diff ds) 0.5))
  14448. h 0 m 0))
  14449. (if (not to-buffer)
  14450. (message "%s" (org-make-tdiff-string y d h m))
  14451. (if (org-at-table-p)
  14452. (progn
  14453. (goto-char match-end)
  14454. (setq align t)
  14455. (and (looking-at " *|") (goto-char (match-end 0))))
  14456. (goto-char match-end))
  14457. (if (looking-at
  14458. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  14459. (replace-match ""))
  14460. (if negative (insert " -"))
  14461. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  14462. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  14463. (insert " " (format fh h m))))
  14464. (if align (org-table-align))
  14465. (message "Time difference inserted")))))
  14466. (defun org-make-tdiff-string (y d h m)
  14467. (let ((fmt "")
  14468. (l nil))
  14469. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  14470. l (push y l)))
  14471. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  14472. l (push d l)))
  14473. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  14474. l (push h l)))
  14475. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  14476. l (push m l)))
  14477. (apply 'format fmt (nreverse l))))
  14478. (defun org-time-string-to-time (s &optional buffer pos)
  14479. (condition-case errdata
  14480. (apply 'encode-time (org-parse-time-string s))
  14481. (error (error "Bad timestamp `%s'%s\nError was: %s"
  14482. s (if (not (and buffer pos))
  14483. ""
  14484. (format " at %d in buffer `%s'" pos buffer))
  14485. (cdr errdata)))))
  14486. (defun org-time-string-to-seconds (s)
  14487. (org-float-time (org-time-string-to-time s)))
  14488. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  14489. "Convert a time stamp to an absolute day number.
  14490. If there is a specifier for a cyclic time stamp, get the closest date to
  14491. DAYNR.
  14492. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  14493. The variable date is bound by the calendar when this is called."
  14494. (cond
  14495. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  14496. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  14497. daynr
  14498. (+ daynr 1000)))
  14499. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  14500. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  14501. (time-to-days (current-time))) (match-string 0 s)
  14502. prefer show-all))
  14503. (t (time-to-days
  14504. (condition-case errdata
  14505. (apply 'encode-time (org-parse-time-string s))
  14506. (error (error "Bad timestamp `%s'%s\nError was: %s"
  14507. s (if (not (and buffer pos))
  14508. ""
  14509. (format " at %d in buffer `%s'" pos buffer))
  14510. (cdr errdata))))))))
  14511. (defun org-days-to-iso-week (days)
  14512. "Return the iso week number."
  14513. (require 'cal-iso)
  14514. (car (calendar-iso-from-absolute days)))
  14515. (defun org-small-year-to-year (year)
  14516. "Convert 2-digit years into 4-digit years.
  14517. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  14518. The year 2000 cannot be abbreviated. Any year larger than 99
  14519. is returned unchanged."
  14520. (if (< year 38)
  14521. (setq year (+ 2000 year))
  14522. (if (< year 100)
  14523. (setq year (+ 1900 year))))
  14524. year)
  14525. (defun org-time-from-absolute (d)
  14526. "Return the time corresponding to date D.
  14527. D may be an absolute day number, or a calendar-type list (month day year)."
  14528. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  14529. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  14530. (defun org-calendar-holiday ()
  14531. "List of holidays, for Diary display in Org-mode."
  14532. (require 'holidays)
  14533. (let ((hl (funcall
  14534. (if (fboundp 'calendar-check-holidays)
  14535. 'calendar-check-holidays 'check-calendar-holidays) date)))
  14536. (if hl (mapconcat 'identity hl "; "))))
  14537. (defun org-diary-sexp-entry (sexp entry date)
  14538. "Process a SEXP diary ENTRY for DATE."
  14539. (require 'diary-lib)
  14540. (let ((result (if calendar-debug-sexp
  14541. (let ((stack-trace-on-error t))
  14542. (eval (car (read-from-string sexp))))
  14543. (condition-case nil
  14544. (eval (car (read-from-string sexp)))
  14545. (error
  14546. (beep)
  14547. (message "Bad sexp at line %d in %s: %s"
  14548. (org-current-line)
  14549. (buffer-file-name) sexp)
  14550. (sleep-for 2))))))
  14551. (cond ((stringp result) (split-string result "; "))
  14552. ((and (consp result)
  14553. (not (consp (cdr result)))
  14554. (stringp (cdr result))) (cdr result))
  14555. ((and (consp result)
  14556. (stringp (car result))) result)
  14557. (result entry)
  14558. (t nil))))
  14559. (defun org-diary-to-ical-string (frombuf)
  14560. "Get iCalendar entries from diary entries in buffer FROMBUF.
  14561. This uses the icalendar.el library."
  14562. (let* ((tmpdir (if (featurep 'xemacs)
  14563. (temp-directory)
  14564. temporary-file-directory))
  14565. (tmpfile (make-temp-name
  14566. (expand-file-name "orgics" tmpdir)))
  14567. buf rtn b e)
  14568. (with-current-buffer frombuf
  14569. (icalendar-export-region (point-min) (point-max) tmpfile)
  14570. (setq buf (find-buffer-visiting tmpfile))
  14571. (set-buffer buf)
  14572. (goto-char (point-min))
  14573. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  14574. (setq b (match-beginning 0)))
  14575. (goto-char (point-max))
  14576. (if (re-search-backward "^END:VEVENT" nil t)
  14577. (setq e (match-end 0)))
  14578. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  14579. (kill-buffer buf)
  14580. (delete-file tmpfile)
  14581. rtn))
  14582. (defun org-closest-date (start current change prefer show-all)
  14583. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  14584. When PREFER is `past', return a date that is either CURRENT or past.
  14585. When PREFER is `future', return a date that is either CURRENT or future.
  14586. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  14587. ;; Make the proper lists from the dates
  14588. (catch 'exit
  14589. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  14590. dn dw sday cday n1 n2 n0
  14591. d m y y1 y2 date1 date2 nmonths nm ny m2)
  14592. (setq start (org-date-to-gregorian start)
  14593. current (org-date-to-gregorian
  14594. (if show-all
  14595. current
  14596. (time-to-days (current-time))))
  14597. sday (calendar-absolute-from-gregorian start)
  14598. cday (calendar-absolute-from-gregorian current))
  14599. (if (<= cday sday) (throw 'exit sday))
  14600. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  14601. (setq dn (string-to-number (match-string 1 change))
  14602. dw (cdr (assoc (match-string 2 change) a1)))
  14603. (error "Invalid change specifier: %s" change))
  14604. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  14605. (cond
  14606. ((eq dw 'day)
  14607. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  14608. n2 (+ n1 dn)))
  14609. ((eq dw 'year)
  14610. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  14611. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  14612. (setq date1 (list m d y1)
  14613. n1 (calendar-absolute-from-gregorian date1)
  14614. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  14615. n2 (calendar-absolute-from-gregorian date2)))
  14616. ((eq dw 'month)
  14617. ;; approx number of month between the two dates
  14618. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  14619. ;; How often does dn fit in there?
  14620. (setq d (nth 1 start) m (car start) y (nth 2 start)
  14621. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  14622. m (+ m nm)
  14623. ny (floor (/ m 12))
  14624. y (+ y ny)
  14625. m (- m (* ny 12)))
  14626. (while (> m 12) (setq m (- m 12) y (1+ y)))
  14627. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  14628. (setq m2 (+ m dn) y2 y)
  14629. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  14630. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  14631. (while (<= n2 cday)
  14632. (setq n1 n2 m m2 y y2)
  14633. (setq m2 (+ m dn) y2 y)
  14634. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  14635. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  14636. ;; Make sure n1 is the earlier date
  14637. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  14638. (if show-all
  14639. (cond
  14640. ((eq prefer 'past) (if (= cday n2) n2 n1))
  14641. ((eq prefer 'future) (if (= cday n1) n1 n2))
  14642. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  14643. (cond
  14644. ((eq prefer 'past) (if (= cday n2) n2 n1))
  14645. ((eq prefer 'future) (if (= cday n1) n1 n2))
  14646. (t (if (= cday n1) n1 n2)))))))
  14647. (defun org-date-to-gregorian (date)
  14648. "Turn any specification of DATE into a Gregorian date for the calendar."
  14649. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  14650. ((and (listp date) (= (length date) 3)) date)
  14651. ((stringp date)
  14652. (setq date (org-parse-time-string date))
  14653. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  14654. ((listp date)
  14655. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  14656. (defun org-parse-time-string (s &optional nodefault)
  14657. "Parse the standard Org-mode time string.
  14658. This should be a lot faster than the normal `parse-time-string'.
  14659. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  14660. hour and minute fields will be nil if not given."
  14661. (if (string-match org-ts-regexp0 s)
  14662. (list 0
  14663. (if (or (match-beginning 8) (not nodefault))
  14664. (string-to-number (or (match-string 8 s) "0")))
  14665. (if (or (match-beginning 7) (not nodefault))
  14666. (string-to-number (or (match-string 7 s) "0")))
  14667. (string-to-number (match-string 4 s))
  14668. (string-to-number (match-string 3 s))
  14669. (string-to-number (match-string 2 s))
  14670. nil nil nil)
  14671. (error "Not a standard Org-mode time string: %s" s)))
  14672. (defun org-timestamp-up (&optional arg)
  14673. "Increase the date item at the cursor by one.
  14674. If the cursor is on the year, change the year. If it is on the month,
  14675. the day or the time, change that.
  14676. With prefix ARG, change by that many units."
  14677. (interactive "p")
  14678. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  14679. (defun org-timestamp-down (&optional arg)
  14680. "Decrease the date item at the cursor by one.
  14681. If the cursor is on the year, change the year. If it is on the month,
  14682. the day or the time, change that.
  14683. With prefix ARG, change by that many units."
  14684. (interactive "p")
  14685. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  14686. (defun org-timestamp-up-day (&optional arg)
  14687. "Increase the date in the time stamp by one day.
  14688. With prefix ARG, change that many days."
  14689. (interactive "p")
  14690. (if (and (not (org-at-timestamp-p t))
  14691. (org-at-heading-p))
  14692. (org-todo 'up)
  14693. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  14694. (defun org-timestamp-down-day (&optional arg)
  14695. "Decrease the date in the time stamp by one day.
  14696. With prefix ARG, change that many days."
  14697. (interactive "p")
  14698. (if (and (not (org-at-timestamp-p t))
  14699. (org-at-heading-p))
  14700. (org-todo 'down)
  14701. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  14702. (defun org-at-timestamp-p (&optional inactive-ok)
  14703. "Determine if the cursor is in or at a timestamp."
  14704. (interactive)
  14705. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  14706. (pos (point))
  14707. (ans (or (looking-at tsr)
  14708. (save-excursion
  14709. (skip-chars-backward "^[<\n\r\t")
  14710. (if (> (point) (point-min)) (backward-char 1))
  14711. (and (looking-at tsr)
  14712. (> (- (match-end 0) pos) -1))))))
  14713. (and ans
  14714. (boundp 'org-ts-what)
  14715. (setq org-ts-what
  14716. (cond
  14717. ((= pos (match-beginning 0)) 'bracket)
  14718. ;; Point is considered to be "on the bracket" whether
  14719. ;; it's really on it or right after it.
  14720. ((or (= pos (1- (match-end 0)))
  14721. (= pos (match-end 0))) 'bracket)
  14722. ((org-pos-in-match-range pos 2) 'year)
  14723. ((org-pos-in-match-range pos 3) 'month)
  14724. ((org-pos-in-match-range pos 7) 'hour)
  14725. ((org-pos-in-match-range pos 8) 'minute)
  14726. ((or (org-pos-in-match-range pos 4)
  14727. (org-pos-in-match-range pos 5)) 'day)
  14728. ((and (> pos (or (match-end 8) (match-end 5)))
  14729. (< pos (match-end 0)))
  14730. (- pos (or (match-end 8) (match-end 5))))
  14731. (t 'day))))
  14732. ans))
  14733. (defun org-toggle-timestamp-type ()
  14734. "Toggle the type (<active> or [inactive]) of a time stamp."
  14735. (interactive)
  14736. (when (org-at-timestamp-p t)
  14737. (let ((beg (match-beginning 0)) (end (match-end 0))
  14738. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  14739. (save-excursion
  14740. (goto-char beg)
  14741. (while (re-search-forward "[][<>]" end t)
  14742. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  14743. t t)))
  14744. (message "Timestamp is now %sactive"
  14745. (if (equal (char-after beg) ?<) "" "in")))))
  14746. (defun org-timestamp-change (n &optional what updown)
  14747. "Change the date in the time stamp at point.
  14748. The date will be changed by N times WHAT. WHAT can be `day', `month',
  14749. `year', `minute', `second'. If WHAT is not given, the cursor position
  14750. in the timestamp determines what will be changed."
  14751. (let ((origin (point)) origin-cat
  14752. with-hm inactive
  14753. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  14754. org-ts-what
  14755. extra rem
  14756. ts time time0)
  14757. (if (not (org-at-timestamp-p t))
  14758. (error "Not at a timestamp"))
  14759. (if (and (not what) (eq org-ts-what 'bracket))
  14760. (org-toggle-timestamp-type)
  14761. ;; Point isn't on brackets. Remember the part of the time-stamp
  14762. ;; the point was in. Indeed, size of time-stamps may change,
  14763. ;; but point must be kept in the same category nonetheless.
  14764. (setq origin-cat org-ts-what)
  14765. (if (and (not what) (not (eq org-ts-what 'day))
  14766. org-display-custom-times
  14767. (get-text-property (point) 'display)
  14768. (not (get-text-property (1- (point)) 'display)))
  14769. (setq org-ts-what 'day))
  14770. (setq org-ts-what (or what org-ts-what)
  14771. inactive (= (char-after (match-beginning 0)) ?\[)
  14772. ts (match-string 0))
  14773. (replace-match "")
  14774. (if (string-match
  14775. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)*\\)[]>]"
  14776. ts)
  14777. (setq extra (match-string 1 ts)))
  14778. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  14779. (setq with-hm t))
  14780. (setq time0 (org-parse-time-string ts))
  14781. (when (and updown
  14782. (eq org-ts-what 'minute)
  14783. (not current-prefix-arg))
  14784. ;; This looks like s-up and s-down. Change by one rounding step.
  14785. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  14786. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  14787. (setcar (cdr time0) (+ (nth 1 time0)
  14788. (if (> n 0) (- rem) (- dm rem))))))
  14789. (setq time
  14790. (encode-time (or (car time0) 0)
  14791. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  14792. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  14793. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  14794. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  14795. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  14796. (nthcdr 6 time0)))
  14797. (when (and (member org-ts-what '(hour minute))
  14798. extra
  14799. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  14800. (setq extra (org-modify-ts-extra
  14801. extra
  14802. (if (eq org-ts-what 'hour) 2 5)
  14803. n dm)))
  14804. (when (integerp org-ts-what)
  14805. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  14806. (if (eq what 'calendar)
  14807. (let ((cal-date (org-get-date-from-calendar)))
  14808. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  14809. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  14810. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  14811. (setcar time0 (or (car time0) 0))
  14812. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  14813. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  14814. (setq time (apply 'encode-time time0))))
  14815. ;; Insert the new time-stamp, and ensure point stays in the same
  14816. ;; category as before (i.e. not after the last position in that
  14817. ;; category).
  14818. (let ((pos (point)))
  14819. ;; Stay before inserted string. `save-excursion' is of no use.
  14820. (setq org-last-changed-timestamp
  14821. (org-insert-time-stamp time with-hm inactive nil nil extra))
  14822. (goto-char pos))
  14823. (save-match-data
  14824. (looking-at org-ts-regexp3)
  14825. (goto-char (cond
  14826. ;; `day' category ends before `hour' if any, or at
  14827. ;; the end of the day name.
  14828. ((eq origin-cat 'day)
  14829. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  14830. ((eq origin-cat 'hour) (min (match-end 7) origin))
  14831. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  14832. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  14833. ;; `year' and `month' have both fixed size: point
  14834. ;; couldn't have moved into another part.
  14835. (t origin))))
  14836. ;; Update clock if on a CLOCK line.
  14837. (org-clock-update-time-maybe)
  14838. ;; Try to recenter the calendar window, if any.
  14839. (if (and org-calendar-follow-timestamp-change
  14840. (get-buffer-window "*Calendar*" t)
  14841. (memq org-ts-what '(day month year)))
  14842. (org-recenter-calendar (time-to-days time))))))
  14843. (defun org-modify-ts-extra (s pos n dm)
  14844. "Change the different parts of the lead-time and repeat fields in timestamp."
  14845. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  14846. ng h m new rem)
  14847. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  14848. (cond
  14849. ((or (org-pos-in-match-range pos 2)
  14850. (org-pos-in-match-range pos 3))
  14851. (setq m (string-to-number (match-string 3 s))
  14852. h (string-to-number (match-string 2 s)))
  14853. (if (org-pos-in-match-range pos 2)
  14854. (setq h (+ h n))
  14855. (setq n (* dm (org-no-warnings (signum n))))
  14856. (when (not (= 0 (setq rem (% m dm))))
  14857. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  14858. (setq m (+ m n)))
  14859. (if (< m 0) (setq m (+ m 60) h (1- h)))
  14860. (if (> m 59) (setq m (- m 60) h (1+ h)))
  14861. (setq h (min 24 (max 0 h)))
  14862. (setq ng 1 new (format "-%02d:%02d" h m)))
  14863. ((org-pos-in-match-range pos 6)
  14864. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  14865. ((org-pos-in-match-range pos 5)
  14866. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  14867. ((org-pos-in-match-range pos 9)
  14868. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  14869. ((org-pos-in-match-range pos 8)
  14870. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  14871. (when ng
  14872. (setq s (concat
  14873. (substring s 0 (match-beginning ng))
  14874. new
  14875. (substring s (match-end ng))))))
  14876. s))
  14877. (defun org-recenter-calendar (date)
  14878. "If the calendar is visible, recenter it to DATE."
  14879. (let ((cwin (get-buffer-window "*Calendar*" t)))
  14880. (when cwin
  14881. (let ((calendar-move-hook nil))
  14882. (with-selected-window cwin
  14883. (calendar-goto-date (if (listp date) date
  14884. (calendar-gregorian-from-absolute date))))))))
  14885. (defun org-goto-calendar (&optional arg)
  14886. "Go to the Emacs calendar at the current date.
  14887. If there is a time stamp in the current line, go to that date.
  14888. A prefix ARG can be used to force the current date."
  14889. (interactive "P")
  14890. (let ((tsr org-ts-regexp) diff
  14891. (calendar-move-hook nil)
  14892. (calendar-view-holidays-initially-flag nil)
  14893. (calendar-view-diary-initially-flag nil))
  14894. (if (or (org-at-timestamp-p)
  14895. (save-excursion
  14896. (beginning-of-line 1)
  14897. (looking-at (concat ".*" tsr))))
  14898. (let ((d1 (time-to-days (current-time)))
  14899. (d2 (time-to-days
  14900. (org-time-string-to-time (match-string 1)))))
  14901. (setq diff (- d2 d1))))
  14902. (calendar)
  14903. (calendar-goto-today)
  14904. (if (and diff (not arg)) (calendar-forward-day diff))))
  14905. (defun org-get-date-from-calendar ()
  14906. "Return a list (month day year) of date at point in calendar."
  14907. (with-current-buffer "*Calendar*"
  14908. (save-match-data
  14909. (calendar-cursor-to-date))))
  14910. (defun org-date-from-calendar ()
  14911. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  14912. If there is already a time stamp at the cursor position, update it."
  14913. (interactive)
  14914. (if (org-at-timestamp-p t)
  14915. (org-timestamp-change 0 'calendar)
  14916. (let ((cal-date (org-get-date-from-calendar)))
  14917. (org-insert-time-stamp
  14918. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  14919. (defun org-minutes-to-hh:mm-string (m)
  14920. "Compute H:MM from a number of minutes."
  14921. (let ((h (/ m 60)))
  14922. (setq m (- m (* 60 h)))
  14923. (format org-time-clocksum-format h m)))
  14924. (defun org-hh:mm-string-to-minutes (s)
  14925. "Convert a string H:MM to a number of minutes.
  14926. If the string is just a number, interpret it as minutes.
  14927. In fact, the first hh:mm or number in the string will be taken,
  14928. there can be extra stuff in the string.
  14929. If no number is found, the return value is 0."
  14930. (cond
  14931. ((integerp s) s)
  14932. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  14933. (+ (* (string-to-number (match-string 1 s)) 60)
  14934. (string-to-number (match-string 2 s))))
  14935. ((string-match "\\([0-9]+\\)" s)
  14936. (string-to-number (match-string 1 s)))
  14937. (t 0)))
  14938. (defcustom org-effort-durations
  14939. `(("h" . 60)
  14940. ("d" . ,(* 60 8))
  14941. ("w" . ,(* 60 8 5))
  14942. ("m" . ,(* 60 8 5 4))
  14943. ("y" . ,(* 60 8 5 40)))
  14944. "Conversion factor to minutes for an effort modifier.
  14945. Each entry has the form (MODIFIER . MINUTES).
  14946. In an effort string, a number followed by MODIFIER is multiplied
  14947. by the specified number of MINUTES to obtain an effort in
  14948. minutes.
  14949. For example, if the value of this variable is ((\"hours\" . 60)), then an
  14950. effort string \"2hours\" is equivalent to 120 minutes."
  14951. :group 'org-agenda
  14952. :version "24.1"
  14953. :type '(alist :key-type (string :tag "Modifier")
  14954. :value-type (number :tag "Minutes")))
  14955. (defun org-duration-string-to-minutes (s)
  14956. "Convert a duration string S to minutes.
  14957. A bare number is interpreted as minutes, modifiers can be set by
  14958. customizing `org-effort-durations' (which see).
  14959. Entries containing a colon are interpreted as H:MM by
  14960. `org-hh:mm-string-to-minutes'."
  14961. (let ((result 0)
  14962. (re (concat "\\([0-9]+\\) *\\("
  14963. (regexp-opt (mapcar 'car org-effort-durations))
  14964. "\\)")))
  14965. (while (string-match re s)
  14966. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  14967. (string-to-number (match-string 1 s))))
  14968. (setq s (replace-match "" nil t s)))
  14969. (incf result (org-hh:mm-string-to-minutes s))
  14970. result))
  14971. ;;;; Files
  14972. (defun org-save-all-org-buffers ()
  14973. "Save all Org-mode buffers without user confirmation."
  14974. (interactive)
  14975. (message "Saving all Org-mode buffers...")
  14976. (save-some-buffers t (lambda () (eq major-mode 'org-mode)))
  14977. (when (featurep 'org-id) (org-id-locations-save))
  14978. (message "Saving all Org-mode buffers... done"))
  14979. (defun org-revert-all-org-buffers ()
  14980. "Revert all Org-mode buffers.
  14981. Prompt for confirmation when there are unsaved changes.
  14982. Be sure you know what you are doing before letting this function
  14983. overwrite your changes.
  14984. This function is useful in a setup where one tracks org files
  14985. with a version control system, to revert on one machine after pulling
  14986. changes from another. I believe the procedure must be like this:
  14987. 1. M-x org-save-all-org-buffers
  14988. 2. Pull changes from the other machine, resolve conflicts
  14989. 3. M-x org-revert-all-org-buffers"
  14990. (interactive)
  14991. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  14992. (error "Abort"))
  14993. (save-excursion
  14994. (save-window-excursion
  14995. (mapc
  14996. (lambda (b)
  14997. (when (and (with-current-buffer b (eq major-mode 'org-mode))
  14998. (with-current-buffer b buffer-file-name))
  14999. (org-pop-to-buffer-same-window b)
  15000. (revert-buffer t 'no-confirm)))
  15001. (buffer-list))
  15002. (when (and (featurep 'org-id) org-id-track-globally)
  15003. (org-id-locations-load)))))
  15004. ;;;; Agenda files
  15005. ;;;###autoload
  15006. (defun org-switchb (&optional arg)
  15007. "Switch between Org buffers.
  15008. With one prefix argument, restrict available buffers to files.
  15009. With two prefix arguments, restrict available buffers to agenda files.
  15010. Defaults to `iswitchb' for buffer name completion.
  15011. Set `org-completion-use-ido' to make it use ido instead."
  15012. (interactive "P")
  15013. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  15014. ((equal arg '(16)) (org-buffer-list 'agenda))
  15015. (t (org-buffer-list))))
  15016. (org-completion-use-iswitchb org-completion-use-iswitchb)
  15017. (org-completion-use-ido org-completion-use-ido))
  15018. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  15019. (setq org-completion-use-iswitchb t))
  15020. (org-pop-to-buffer-same-window
  15021. (org-icompleting-read "Org buffer: "
  15022. (mapcar 'list (mapcar 'buffer-name blist))
  15023. nil t))))
  15024. ;;; Define some older names previously used for this functionality
  15025. ;;;###autoload
  15026. (defalias 'org-ido-switchb 'org-switchb)
  15027. ;;;###autoload
  15028. (defalias 'org-iswitchb 'org-switchb)
  15029. (defun org-buffer-list (&optional predicate exclude-tmp)
  15030. "Return a list of Org buffers.
  15031. PREDICATE can be `export', `files' or `agenda'.
  15032. export restrict the list to Export buffers.
  15033. files restrict the list to buffers visiting Org files.
  15034. agenda restrict the list to buffers visiting agenda files.
  15035. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  15036. (let* ((bfn nil)
  15037. (agenda-files (and (eq predicate 'agenda)
  15038. (mapcar 'file-truename (org-agenda-files t))))
  15039. (filter
  15040. (cond
  15041. ((eq predicate 'files)
  15042. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  15043. ((eq predicate 'export)
  15044. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  15045. ((eq predicate 'agenda)
  15046. (lambda (b)
  15047. (with-current-buffer b
  15048. (and (eq major-mode 'org-mode)
  15049. (setq bfn (buffer-file-name b))
  15050. (member (file-truename bfn) agenda-files)))))
  15051. (t (lambda (b) (with-current-buffer b
  15052. (or (eq major-mode 'org-mode)
  15053. (string-match "\*Org .*Export"
  15054. (buffer-name b)))))))))
  15055. (delq nil
  15056. (mapcar
  15057. (lambda(b)
  15058. (if (and (funcall filter b)
  15059. (or (not exclude-tmp)
  15060. (not (string-match "tmp" (buffer-name b)))))
  15061. b
  15062. nil))
  15063. (buffer-list)))))
  15064. (defun org-agenda-files (&optional unrestricted archives)
  15065. "Get the list of agenda files.
  15066. Optional UNRESTRICTED means return the full list even if a restriction
  15067. is currently in place.
  15068. When ARCHIVES is t, include all archive files that are really being
  15069. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  15070. `org-agenda-archives-mode' is t."
  15071. (let ((files
  15072. (cond
  15073. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  15074. ((stringp org-agenda-files) (org-read-agenda-file-list))
  15075. ((listp org-agenda-files) org-agenda-files)
  15076. (t (error "Invalid value of `org-agenda-files'")))))
  15077. (setq files (apply 'append
  15078. (mapcar (lambda (f)
  15079. (if (file-directory-p f)
  15080. (directory-files
  15081. f t org-agenda-file-regexp)
  15082. (list f)))
  15083. files)))
  15084. (when org-agenda-skip-unavailable-files
  15085. (setq files (delq nil
  15086. (mapcar (function
  15087. (lambda (file)
  15088. (and (file-readable-p file) file)))
  15089. files))))
  15090. (when (or (eq archives t)
  15091. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  15092. (setq files (org-add-archive-files files)))
  15093. files))
  15094. (defun org-agenda-file-p (&optional file)
  15095. "Return non-nil, if FILE is an agenda file.
  15096. If FILE is omitted, use the file associated with the current
  15097. buffer."
  15098. (member (or file (buffer-file-name))
  15099. (org-agenda-files t)))
  15100. (defun org-edit-agenda-file-list ()
  15101. "Edit the list of agenda files.
  15102. Depending on setup, this either uses customize to edit the variable
  15103. `org-agenda-files', or it visits the file that is holding the list. In the
  15104. latter case, the buffer is set up in a way that saving it automatically kills
  15105. the buffer and restores the previous window configuration."
  15106. (interactive)
  15107. (if (stringp org-agenda-files)
  15108. (let ((cw (current-window-configuration)))
  15109. (find-file org-agenda-files)
  15110. (org-set-local 'org-window-configuration cw)
  15111. (org-add-hook 'after-save-hook
  15112. (lambda ()
  15113. (set-window-configuration
  15114. (prog1 org-window-configuration
  15115. (kill-buffer (current-buffer))))
  15116. (org-install-agenda-files-menu)
  15117. (message "New agenda file list installed"))
  15118. nil 'local)
  15119. (message "%s" (substitute-command-keys
  15120. "Edit list and finish with \\[save-buffer]")))
  15121. (customize-variable 'org-agenda-files)))
  15122. (defun org-store-new-agenda-file-list (list)
  15123. "Set new value for the agenda file list and save it correctly."
  15124. (if (stringp org-agenda-files)
  15125. (let ((fe (org-read-agenda-file-list t)) b u)
  15126. (while (setq b (find-buffer-visiting org-agenda-files))
  15127. (kill-buffer b))
  15128. (with-temp-file org-agenda-files
  15129. (insert
  15130. (mapconcat
  15131. (lambda (f) ;; Keep un-expanded entries.
  15132. (if (setq u (assoc f fe))
  15133. (cdr u)
  15134. f))
  15135. list "\n")
  15136. "\n")))
  15137. (let ((org-mode-hook nil) (org-inhibit-startup t)
  15138. (org-insert-mode-line-in-empty-file nil))
  15139. (setq org-agenda-files list)
  15140. (customize-save-variable 'org-agenda-files org-agenda-files))))
  15141. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  15142. "Read the list of agenda files from a file.
  15143. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  15144. filenames, used by `org-store-new-agenda-file-list' to write back
  15145. un-expanded file names."
  15146. (when (file-directory-p org-agenda-files)
  15147. (error "`org-agenda-files' cannot be a single directory"))
  15148. (when (stringp org-agenda-files)
  15149. (with-temp-buffer
  15150. (insert-file-contents org-agenda-files)
  15151. (mapcar
  15152. (lambda (f)
  15153. (let ((e (expand-file-name (substitute-in-file-name f)
  15154. org-directory)))
  15155. (if pair-with-expansion
  15156. (cons e f)
  15157. e)))
  15158. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  15159. ;;;###autoload
  15160. (defun org-cycle-agenda-files ()
  15161. "Cycle through the files in `org-agenda-files'.
  15162. If the current buffer visits an agenda file, find the next one in the list.
  15163. If the current buffer does not, find the first agenda file."
  15164. (interactive)
  15165. (let* ((fs (org-agenda-files t))
  15166. (files (append fs (list (car fs))))
  15167. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  15168. file)
  15169. (unless files (error "No agenda files"))
  15170. (catch 'exit
  15171. (while (setq file (pop files))
  15172. (if (equal (file-truename file) tcf)
  15173. (when (car files)
  15174. (find-file (car files))
  15175. (throw 'exit t))))
  15176. (find-file (car fs)))
  15177. (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  15178. (defun org-agenda-file-to-front (&optional to-end)
  15179. "Move/add the current file to the top of the agenda file list.
  15180. If the file is not present in the list, it is added to the front. If it is
  15181. present, it is moved there. With optional argument TO-END, add/move to the
  15182. end of the list."
  15183. (interactive "P")
  15184. (let ((org-agenda-skip-unavailable-files nil)
  15185. (file-alist (mapcar (lambda (x)
  15186. (cons (file-truename x) x))
  15187. (org-agenda-files t)))
  15188. (ctf (file-truename buffer-file-name))
  15189. x had)
  15190. (setq x (assoc ctf file-alist) had x)
  15191. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  15192. (if to-end
  15193. (setq file-alist (append (delq x file-alist) (list x)))
  15194. (setq file-alist (cons x (delq x file-alist))))
  15195. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  15196. (org-install-agenda-files-menu)
  15197. (message "File %s to %s of agenda file list"
  15198. (if had "moved" "added") (if to-end "end" "front"))))
  15199. (defun org-remove-file (&optional file)
  15200. "Remove current file from the list of files in variable `org-agenda-files'.
  15201. These are the files which are being checked for agenda entries.
  15202. Optional argument FILE means use this file instead of the current."
  15203. (interactive)
  15204. (let* ((org-agenda-skip-unavailable-files nil)
  15205. (file (or file buffer-file-name))
  15206. (true-file (file-truename file))
  15207. (afile (abbreviate-file-name file))
  15208. (files (delq nil (mapcar
  15209. (lambda (x)
  15210. (if (equal true-file
  15211. (file-truename x))
  15212. nil x))
  15213. (org-agenda-files t)))))
  15214. (if (not (= (length files) (length (org-agenda-files t))))
  15215. (progn
  15216. (org-store-new-agenda-file-list files)
  15217. (org-install-agenda-files-menu)
  15218. (message "Removed file: %s" afile))
  15219. (message "File was not in list: %s (not removed)" afile))))
  15220. (defun org-file-menu-entry (file)
  15221. (vector file (list 'find-file file) t))
  15222. (defun org-check-agenda-file (file)
  15223. "Make sure FILE exists. If not, ask user what to do."
  15224. (when (not (file-exists-p file))
  15225. (message "non-existent agenda file %s. [R]emove from list or [A]bort?"
  15226. (abbreviate-file-name file))
  15227. (let ((r (downcase (read-char-exclusive))))
  15228. (cond
  15229. ((equal r ?r)
  15230. (org-remove-file file)
  15231. (throw 'nextfile t))
  15232. (t (error "Abort"))))))
  15233. (defun org-get-agenda-file-buffer (file)
  15234. "Get a buffer visiting FILE. If the buffer needs to be created, add
  15235. it to the list of buffers which might be released later."
  15236. (let ((buf (org-find-base-buffer-visiting file)))
  15237. (if buf
  15238. buf ; just return it
  15239. ;; Make a new buffer and remember it
  15240. (setq buf (find-file-noselect file))
  15241. (if buf (push buf org-agenda-new-buffers))
  15242. buf)))
  15243. (defun org-release-buffers (blist)
  15244. "Release all buffers in list, asking the user for confirmation when needed.
  15245. When a buffer is unmodified, it is just killed. When modified, it is saved
  15246. \(if the user agrees) and then killed."
  15247. (let (buf file)
  15248. (while (setq buf (pop blist))
  15249. (setq file (buffer-file-name buf))
  15250. (when (and (buffer-modified-p buf)
  15251. file
  15252. (y-or-n-p (format "Save file %s? " file)))
  15253. (with-current-buffer buf (save-buffer)))
  15254. (kill-buffer buf))))
  15255. (defun org-prepare-agenda-buffers (files)
  15256. "Create buffers for all agenda files, protect archived trees and comments."
  15257. (interactive)
  15258. (let ((pa '(:org-archived t))
  15259. (pc '(:org-comment t))
  15260. (pall '(:org-archived t :org-comment t))
  15261. (inhibit-read-only t)
  15262. (rea (concat ":" org-archive-tag ":"))
  15263. bmp file re)
  15264. (save-excursion
  15265. (save-restriction
  15266. (while (setq file (pop files))
  15267. (catch 'nextfile
  15268. (if (bufferp file)
  15269. (set-buffer file)
  15270. (org-check-agenda-file file)
  15271. (set-buffer (org-get-agenda-file-buffer file)))
  15272. (widen)
  15273. (setq bmp (buffer-modified-p))
  15274. (org-refresh-category-properties)
  15275. (setq org-todo-keywords-for-agenda
  15276. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  15277. (setq org-done-keywords-for-agenda
  15278. (append org-done-keywords-for-agenda org-done-keywords))
  15279. (setq org-todo-keyword-alist-for-agenda
  15280. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  15281. (setq org-drawers-for-agenda
  15282. (append org-drawers-for-agenda org-drawers))
  15283. (setq org-tag-alist-for-agenda
  15284. (append org-tag-alist-for-agenda org-tag-alist))
  15285. (save-excursion
  15286. (remove-text-properties (point-min) (point-max) pall)
  15287. (when org-agenda-skip-archived-trees
  15288. (goto-char (point-min))
  15289. (while (re-search-forward rea nil t)
  15290. (if (org-at-heading-p t)
  15291. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  15292. (goto-char (point-min))
  15293. (setq re (format org-heading-keyword-regexp-format
  15294. org-comment-string))
  15295. (while (re-search-forward re nil t)
  15296. (add-text-properties
  15297. (match-beginning 0) (org-end-of-subtree t) pc)))
  15298. (set-buffer-modified-p bmp)))))
  15299. (setq org-todo-keywords-for-agenda
  15300. (org-uniquify org-todo-keywords-for-agenda))
  15301. (setq org-todo-keyword-alist-for-agenda
  15302. (org-uniquify org-todo-keyword-alist-for-agenda)
  15303. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  15304. ;;;; Embedded LaTeX
  15305. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  15306. "Keymap for the minor `org-cdlatex-mode'.")
  15307. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  15308. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  15309. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  15310. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  15311. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  15312. (defvar org-cdlatex-texmathp-advice-is-done nil
  15313. "Flag remembering if we have applied the advice to texmathp already.")
  15314. (define-minor-mode org-cdlatex-mode
  15315. "Toggle the minor `org-cdlatex-mode'.
  15316. This mode supports entering LaTeX environment and math in LaTeX fragments
  15317. in Org-mode.
  15318. \\{org-cdlatex-mode-map}"
  15319. nil " OCDL" nil
  15320. (when org-cdlatex-mode
  15321. (require 'cdlatex)
  15322. (run-hooks 'cdlatex-mode-hook)
  15323. (cdlatex-compute-tables))
  15324. (unless org-cdlatex-texmathp-advice-is-done
  15325. (setq org-cdlatex-texmathp-advice-is-done t)
  15326. (defadvice texmathp (around org-math-always-on activate)
  15327. "Always return t in org-mode buffers.
  15328. This is because we want to insert math symbols without dollars even outside
  15329. the LaTeX math segments. If Orgmode thinks that point is actually inside
  15330. an embedded LaTeX fragment, let texmathp do its job.
  15331. \\[org-cdlatex-mode-map]"
  15332. (interactive)
  15333. (let (p)
  15334. (cond
  15335. ((not (eq major-mode 'org-mode)) ad-do-it)
  15336. ((eq this-command 'cdlatex-math-symbol)
  15337. (setq ad-return-value t
  15338. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  15339. (t
  15340. (let ((p (org-inside-LaTeX-fragment-p)))
  15341. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  15342. (setq ad-return-value t
  15343. texmathp-why '("Org-mode embedded math" . 0))
  15344. (if p ad-do-it)))))))))
  15345. (defun turn-on-org-cdlatex ()
  15346. "Unconditionally turn on `org-cdlatex-mode'."
  15347. (org-cdlatex-mode 1))
  15348. (defun org-inside-LaTeX-fragment-p ()
  15349. "Test if point is inside a LaTeX fragment.
  15350. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  15351. sequence appearing also before point.
  15352. Even though the matchers for math are configurable, this function assumes
  15353. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  15354. delimiters are skipped when they have been removed by customization.
  15355. The return value is nil, or a cons cell with the delimiter and the
  15356. position of this delimiter.
  15357. This function does a reasonably good job, but can locally be fooled by
  15358. for example currency specifications. For example it will assume being in
  15359. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  15360. fragments that are properly closed, but during editing, we have to live
  15361. with the uncertainty caused by missing closing delimiters. This function
  15362. looks only before point, not after."
  15363. (catch 'exit
  15364. (let ((pos (point))
  15365. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  15366. (lim (progn
  15367. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  15368. (point)))
  15369. dd-on str (start 0) m re)
  15370. (goto-char pos)
  15371. (when dodollar
  15372. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  15373. re (nth 1 (assoc "$" org-latex-regexps)))
  15374. (while (string-match re str start)
  15375. (cond
  15376. ((= (match-end 0) (length str))
  15377. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  15378. ((= (match-end 0) (- (length str) 5))
  15379. (throw 'exit nil))
  15380. (t (setq start (match-end 0))))))
  15381. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  15382. (goto-char pos)
  15383. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  15384. (and (match-beginning 2) (throw 'exit nil))
  15385. ;; count $$
  15386. (while (re-search-backward "\\$\\$" lim t)
  15387. (setq dd-on (not dd-on)))
  15388. (goto-char pos)
  15389. (if dd-on (cons "$$" m))))))
  15390. (defun org-inside-latex-macro-p ()
  15391. "Is point inside a LaTeX macro or its arguments?"
  15392. (save-match-data
  15393. (org-in-regexp
  15394. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  15395. (defun org-try-cdlatex-tab ()
  15396. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  15397. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  15398. - inside a LaTeX fragment, or
  15399. - after the first word in a line, where an abbreviation expansion could
  15400. insert a LaTeX environment."
  15401. (when org-cdlatex-mode
  15402. (cond
  15403. ;; Before any word on the line: No expansion possible.
  15404. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  15405. ;; Just after first word on the line: Expand it. Make sure it
  15406. ;; cannot happen on headlines, though.
  15407. ((save-excursion
  15408. (skip-chars-backward "a-zA-Z0-9*")
  15409. (skip-chars-backward " \t")
  15410. (and (bolp) (not (org-at-heading-p))))
  15411. (cdlatex-tab) t)
  15412. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  15413. (defun org-cdlatex-underscore-caret (&optional arg)
  15414. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  15415. Revert to the normal definition outside of these fragments."
  15416. (interactive "P")
  15417. (if (org-inside-LaTeX-fragment-p)
  15418. (call-interactively 'cdlatex-sub-superscript)
  15419. (let (org-cdlatex-mode)
  15420. (call-interactively (key-binding (vector last-input-event))))))
  15421. (defun org-cdlatex-math-modify (&optional arg)
  15422. "Execute `cdlatex-math-modify' in LaTeX fragments.
  15423. Revert to the normal definition outside of these fragments."
  15424. (interactive "P")
  15425. (if (org-inside-LaTeX-fragment-p)
  15426. (call-interactively 'cdlatex-math-modify)
  15427. (let (org-cdlatex-mode)
  15428. (call-interactively (key-binding (vector last-input-event))))))
  15429. (defvar org-latex-fragment-image-overlays nil
  15430. "List of overlays carrying the images of latex fragments.")
  15431. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  15432. (defun org-remove-latex-fragment-image-overlays ()
  15433. "Remove all overlays with LaTeX fragment images in current buffer."
  15434. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  15435. (setq org-latex-fragment-image-overlays nil))
  15436. (defun org-preview-latex-fragment (&optional subtree)
  15437. "Preview the LaTeX fragment at point, or all locally or globally.
  15438. If the cursor is in a LaTeX fragment, create the image and overlay
  15439. it over the source code. If there is no fragment at point, display
  15440. all fragments in the current text, from one headline to the next. With
  15441. prefix SUBTREE, display all fragments in the current subtree. With a
  15442. double prefix arg \\[universal-argument] \\[universal-argument], or when \
  15443. the cursor is before the first headline,
  15444. display all fragments in the buffer.
  15445. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  15446. (interactive "P")
  15447. (unless buffer-file-name
  15448. (error "Can't preview LaTeX fragment in a non-file buffer"))
  15449. (org-remove-latex-fragment-image-overlays)
  15450. (save-excursion
  15451. (save-restriction
  15452. (let (beg end at msg)
  15453. (cond
  15454. ((or (equal subtree '(16))
  15455. (not (save-excursion
  15456. (re-search-backward org-outline-regexp-bol nil t))))
  15457. (setq beg (point-min) end (point-max)
  15458. msg "Creating images for buffer...%s"))
  15459. ((equal subtree '(4))
  15460. (org-back-to-heading)
  15461. (setq beg (point) end (org-end-of-subtree t)
  15462. msg "Creating images for subtree...%s"))
  15463. (t
  15464. (if (setq at (org-inside-LaTeX-fragment-p))
  15465. (goto-char (max (point-min) (- (cdr at) 2)))
  15466. (org-back-to-heading))
  15467. (setq beg (point) end (progn (outline-next-heading) (point))
  15468. msg (if at "Creating image...%s"
  15469. "Creating images for entry...%s"))))
  15470. (message msg "")
  15471. (narrow-to-region beg end)
  15472. (goto-char beg)
  15473. (org-format-latex
  15474. (concat "ltxpng/" (file-name-sans-extension
  15475. (file-name-nondirectory
  15476. buffer-file-name)))
  15477. default-directory 'overlays msg at 'forbuffer 'dvipng)
  15478. (message msg "done. Use `C-c C-c' to remove images.")))))
  15479. (defvar org-latex-regexps
  15480. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  15481. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  15482. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  15483. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  15484. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  15485. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  15486. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  15487. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  15488. "Regular expressions for matching embedded LaTeX.")
  15489. (defvar org-export-have-math nil) ;; dynamic scoping
  15490. (defun org-format-latex (prefix &optional dir overlays msg at
  15491. forbuffer processing-type)
  15492. "Replace LaTeX fragments with links to an image, and produce images.
  15493. Some of the options can be changed using the variable
  15494. `org-format-latex-options'."
  15495. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  15496. (let* ((prefixnodir (file-name-nondirectory prefix))
  15497. (absprefix (expand-file-name prefix dir))
  15498. (todir (file-name-directory absprefix))
  15499. (opt org-format-latex-options)
  15500. (matchers (plist-get opt :matchers))
  15501. (re-list org-latex-regexps)
  15502. (org-format-latex-header-extra
  15503. (plist-get (org-infile-export-plist) :latex-header-extra))
  15504. (cnt 0) txt hash link beg end re e checkdir
  15505. executables-checked string
  15506. m n block-type block linkfile movefile ov)
  15507. ;; Check the different regular expressions
  15508. (while (setq e (pop re-list))
  15509. (setq m (car e) re (nth 1 e) n (nth 2 e) block-type (nth 3 e)
  15510. block (if block-type "\n\n" ""))
  15511. (when (member m matchers)
  15512. (goto-char (point-min))
  15513. (while (re-search-forward re nil t)
  15514. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  15515. (not (get-text-property (match-beginning n)
  15516. 'org-protected))
  15517. (or (not overlays)
  15518. (not (eq (get-char-property (match-beginning n)
  15519. 'org-overlay-type)
  15520. 'org-latex-overlay))))
  15521. (setq org-export-have-math t)
  15522. (cond
  15523. ((eq processing-type 'verbatim)
  15524. ;; Leave the text verbatim, just protect it
  15525. (add-text-properties (match-beginning n) (match-end n)
  15526. '(org-protected t)))
  15527. ((eq processing-type 'mathjax)
  15528. ;; Prepare for MathJax processing
  15529. (setq string (match-string n))
  15530. (if (member m '("$" "$1"))
  15531. (save-excursion
  15532. (delete-region (match-beginning n) (match-end n))
  15533. (goto-char (match-beginning n))
  15534. (insert (org-add-props (concat "\\(" (substring string 1 -1)
  15535. "\\)")
  15536. '(org-protected t))))
  15537. (add-text-properties (match-beginning n) (match-end n)
  15538. '(org-protected t))))
  15539. ((eq processing-type 'dvipng)
  15540. ;; Process to an image
  15541. (setq txt (match-string n)
  15542. beg (match-beginning n) end (match-end n)
  15543. cnt (1+ cnt))
  15544. (let (print-length print-level) ; make sure full list is printed
  15545. (setq hash (sha1 (prin1-to-string
  15546. (list org-format-latex-header
  15547. org-format-latex-header-extra
  15548. org-export-latex-default-packages-alist
  15549. org-export-latex-packages-alist
  15550. org-format-latex-options
  15551. forbuffer txt)))
  15552. linkfile (format "%s_%s.png" prefix hash)
  15553. movefile (format "%s_%s.png" absprefix hash)))
  15554. (setq link (concat block "[[file:" linkfile "]]" block))
  15555. (if msg (message msg cnt))
  15556. (goto-char beg)
  15557. (unless checkdir ; make sure the directory exists
  15558. (setq checkdir t)
  15559. (or (file-directory-p todir) (make-directory todir t)))
  15560. (unless executables-checked
  15561. (org-check-external-command
  15562. "latex" "needed to convert LaTeX fragments to images")
  15563. (org-check-external-command
  15564. "dvipng" "needed to convert LaTeX fragments to images")
  15565. (setq executables-checked t))
  15566. (unless (file-exists-p movefile)
  15567. (org-create-formula-image
  15568. txt movefile opt forbuffer))
  15569. (if overlays
  15570. (progn
  15571. (mapc (lambda (o)
  15572. (if (eq (overlay-get o 'org-overlay-type)
  15573. 'org-latex-overlay)
  15574. (delete-overlay o)))
  15575. (overlays-in beg end))
  15576. (setq ov (make-overlay beg end))
  15577. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  15578. (if (featurep 'xemacs)
  15579. (progn
  15580. (overlay-put ov 'invisible t)
  15581. (overlay-put
  15582. ov 'end-glyph
  15583. (make-glyph (vector 'png :file movefile))))
  15584. (overlay-put
  15585. ov 'display
  15586. (list 'image :type 'png :file movefile :ascent 'center)))
  15587. (push ov org-latex-fragment-image-overlays)
  15588. (goto-char end))
  15589. (delete-region beg end)
  15590. (insert (org-add-props link
  15591. (list 'org-latex-src
  15592. (replace-regexp-in-string
  15593. "\"" "" txt)
  15594. 'org-latex-src-embed-type
  15595. (if block-type 'paragraph 'character))))))
  15596. ((eq processing-type 'mathml)
  15597. ;; Process to MathML
  15598. (unless executables-checked
  15599. (unless (save-match-data (org-format-latex-mathml-available-p))
  15600. (error "LaTeX to MathML converter not configured"))
  15601. (setq executables-checked t))
  15602. (setq txt (match-string n)
  15603. beg (match-beginning n) end (match-end n)
  15604. cnt (1+ cnt))
  15605. (if msg (message msg cnt))
  15606. (goto-char beg)
  15607. (delete-region beg end)
  15608. (insert (org-format-latex-as-mathml
  15609. txt block-type prefix dir)))
  15610. (t
  15611. (error "Unknown conversion type %s for latex fragments"
  15612. processing-type)))))))))
  15613. (defun org-create-math-formula (latex-frag &optional mathml-file)
  15614. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  15615. Use `org-latex-to-mathml-convert-command'. If the conversion is
  15616. sucessful, return the portion between \"<math...> </math>\"
  15617. elements otherwise return nil. When MATHML-FILE is specified,
  15618. write the results in to that file. When invoked as an
  15619. interactive command, prompt for LATEX-FRAG, with initial value
  15620. set to the current active region and echo the results for user
  15621. inspection."
  15622. (interactive (list (let ((frag (when (region-active-p)
  15623. (buffer-substring-no-properties
  15624. (region-beginning) (region-end)))))
  15625. (read-string "LaTeX Fragment: " frag nil frag))))
  15626. (unless latex-frag (error "Invalid latex-frag"))
  15627. (let* ((tmp-in-file (file-relative-name
  15628. (make-temp-name (expand-file-name "ltxmathml-in"))))
  15629. (ignore (write-region latex-frag nil tmp-in-file))
  15630. (tmp-out-file (file-relative-name
  15631. (make-temp-name (expand-file-name "ltxmathml-out"))))
  15632. (cmd (format-spec
  15633. org-latex-to-mathml-convert-command
  15634. `((?j . ,(shell-quote-argument
  15635. (expand-file-name org-latex-to-mathml-jar-file)))
  15636. (?I . ,(shell-quote-argument tmp-in-file))
  15637. (?o . ,(shell-quote-argument tmp-out-file)))))
  15638. mathml shell-command-output)
  15639. (when (org-called-interactively-p 'any)
  15640. (unless (org-format-latex-mathml-available-p)
  15641. (error "LaTeX to MathML converter not configured")))
  15642. (message "Running %s" cmd)
  15643. (setq shell-command-output (shell-command-to-string cmd))
  15644. (setq mathml
  15645. (when (file-readable-p tmp-out-file)
  15646. (with-current-buffer (find-file-noselect tmp-out-file t)
  15647. (goto-char (point-min))
  15648. (when (re-search-forward
  15649. (concat
  15650. (regexp-quote
  15651. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
  15652. "\\(.\\|\n\\)*"
  15653. (regexp-quote "</math>")) nil t)
  15654. (prog1 (match-string 0) (kill-buffer))))))
  15655. (cond
  15656. (mathml
  15657. (setq mathml
  15658. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  15659. (when mathml-file
  15660. (write-region mathml nil mathml-file))
  15661. (when (org-called-interactively-p 'any)
  15662. (message mathml)))
  15663. ((message "LaTeX to MathML conversion failed")
  15664. (message shell-command-output)))
  15665. (delete-file tmp-in-file)
  15666. (when (file-exists-p tmp-out-file)
  15667. (delete-file tmp-out-file))
  15668. mathml))
  15669. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  15670. prefix &optional dir)
  15671. "Use `org-create-math-formula' but check local cache first."
  15672. (let* ((absprefix (expand-file-name prefix dir))
  15673. (print-length nil) (print-level nil)
  15674. (formula-id (concat
  15675. "formula-"
  15676. (sha1
  15677. (prin1-to-string
  15678. (list latex-frag
  15679. org-latex-to-mathml-convert-command)))))
  15680. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  15681. (formula-cache-dir (file-name-directory formula-cache)))
  15682. (unless (file-directory-p formula-cache-dir)
  15683. (make-directory formula-cache-dir t))
  15684. (unless (file-exists-p formula-cache)
  15685. (org-create-math-formula latex-frag formula-cache))
  15686. (if (file-exists-p formula-cache)
  15687. ;; Successful conversion. Return the link to MathML file.
  15688. (org-add-props
  15689. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  15690. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  15691. 'org-latex-src-embed-type (if latex-frag-type
  15692. 'paragraph 'character)))
  15693. ;; Failed conversion. Return the LaTeX fragment verbatim
  15694. (add-text-properties
  15695. 0 (1- (length latex-frag)) '(org-protected t) latex-frag)
  15696. latex-frag)))
  15697. ;; This function borrows from Ganesh Swami's latex2png.el
  15698. (defun org-create-formula-image (string tofile options buffer)
  15699. "This calls dvipng."
  15700. (require 'org-latex)
  15701. (let* ((tmpdir (if (featurep 'xemacs)
  15702. (temp-directory)
  15703. temporary-file-directory))
  15704. (texfilebase (make-temp-name
  15705. (expand-file-name "orgtex" tmpdir)))
  15706. (texfile (concat texfilebase ".tex"))
  15707. (dvifile (concat texfilebase ".dvi"))
  15708. (pngfile (concat texfilebase ".png"))
  15709. (fnh (if (featurep 'xemacs)
  15710. (font-height (face-font 'default))
  15711. (face-attribute 'default :height nil)))
  15712. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  15713. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  15714. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  15715. "Black"))
  15716. (bg (or (plist-get options (if buffer :background :html-background))
  15717. "Transparent")))
  15718. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  15719. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  15720. (with-temp-file texfile
  15721. (insert (org-splice-latex-header
  15722. org-format-latex-header
  15723. org-export-latex-default-packages-alist
  15724. org-export-latex-packages-alist t
  15725. org-format-latex-header-extra))
  15726. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")
  15727. (require 'org-latex)
  15728. (org-export-latex-fix-inputenc))
  15729. (let ((dir default-directory))
  15730. (condition-case nil
  15731. (progn
  15732. (cd tmpdir)
  15733. (call-process "latex" nil nil nil texfile))
  15734. (error nil))
  15735. (cd dir))
  15736. (if (not (file-exists-p dvifile))
  15737. (progn (message "Failed to create dvi file from %s" texfile) nil)
  15738. (condition-case nil
  15739. (if (featurep 'xemacs)
  15740. (call-process "dvipng" nil nil nil
  15741. "-fg" fg "-bg" bg
  15742. "-T" "tight"
  15743. "-o" pngfile
  15744. dvifile)
  15745. (call-process "dvipng" nil nil nil
  15746. "-fg" fg "-bg" bg
  15747. "-D" dpi
  15748. ;;"-x" scale "-y" scale
  15749. "-T" "tight"
  15750. "-o" pngfile
  15751. dvifile))
  15752. (error nil))
  15753. (if (not (file-exists-p pngfile))
  15754. (if org-format-latex-signal-error
  15755. (error "Failed to create png file from %s" texfile)
  15756. (message "Failed to create png file from %s" texfile)
  15757. nil)
  15758. ;; Use the requested file name and clean up
  15759. (copy-file pngfile tofile 'replace)
  15760. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  15761. (delete-file (concat texfilebase e)))
  15762. pngfile))))
  15763. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  15764. "Fill a LaTeX header template TPL.
  15765. In the template, the following place holders will be recognized:
  15766. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  15767. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  15768. [PACKAGES] \\usepackage statements for PKG
  15769. [NO-PACKAGES] do not include PKG
  15770. [EXTRA] the string EXTRA
  15771. [NO-EXTRA] do not include EXTRA
  15772. For backward compatibility, if both the positive and the negative place
  15773. holder is missing, the positive one (without the \"NO-\") will be
  15774. assumed to be present at the end of the template.
  15775. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  15776. EXTRA is a string.
  15777. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  15778. (let (rpl (end ""))
  15779. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  15780. (setq rpl (if (or (match-end 1) (not def-pkg))
  15781. "" (org-latex-packages-to-string def-pkg snippets-p t))
  15782. tpl (replace-match rpl t t tpl))
  15783. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  15784. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  15785. (setq rpl (if (or (match-end 1) (not pkg))
  15786. "" (org-latex-packages-to-string pkg snippets-p t))
  15787. tpl (replace-match rpl t t tpl))
  15788. (if pkg (setq end
  15789. (concat end "\n"
  15790. (org-latex-packages-to-string pkg snippets-p)))))
  15791. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  15792. (setq rpl (if (or (match-end 1) (not extra))
  15793. "" (concat extra "\n"))
  15794. tpl (replace-match rpl t t tpl))
  15795. (if (and extra (string-match "\\S-" extra))
  15796. (setq end (concat end "\n" extra))))
  15797. (if (string-match "\\S-" end)
  15798. (concat tpl "\n" end)
  15799. tpl)))
  15800. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  15801. "Turn an alist of packages into a string with the \\usepackage macros."
  15802. (setq pkg (mapconcat (lambda(p)
  15803. (cond
  15804. ((stringp p) p)
  15805. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  15806. (format "%% Package %s omitted" (cadr p)))
  15807. ((equal "" (car p))
  15808. (format "\\usepackage{%s}" (cadr p)))
  15809. (t
  15810. (format "\\usepackage[%s]{%s}"
  15811. (car p) (cadr p)))))
  15812. pkg
  15813. "\n"))
  15814. (if newline (concat pkg "\n") pkg))
  15815. (defun org-dvipng-color (attr)
  15816. "Return an rgb color specification for dvipng."
  15817. (apply 'format "rgb %s %s %s"
  15818. (mapcar 'org-normalize-color
  15819. (if (featurep 'xemacs)
  15820. (color-rgb-components
  15821. (face-property 'default
  15822. (cond ((eq attr :foreground) 'foreground)
  15823. ((eq attr :background) 'background))))
  15824. (color-values (face-attribute 'default attr nil))))))
  15825. (defun org-normalize-color (value)
  15826. "Return string to be used as color value for an RGB component."
  15827. (format "%g" (/ value 65535.0)))
  15828. ;; Image display
  15829. (defvar org-inline-image-overlays nil)
  15830. (make-variable-buffer-local 'org-inline-image-overlays)
  15831. (defun org-toggle-inline-images (&optional include-linked)
  15832. "Toggle the display of inline images.
  15833. INCLUDE-LINKED is passed to `org-display-inline-images'."
  15834. (interactive "P")
  15835. (if org-inline-image-overlays
  15836. (progn
  15837. (org-remove-inline-images)
  15838. (message "Inline image display turned off"))
  15839. (org-display-inline-images include-linked)
  15840. (if org-inline-image-overlays
  15841. (message "%d images displayed inline"
  15842. (length org-inline-image-overlays))
  15843. (message "No images to display inline"))))
  15844. (defun org-display-inline-images (&optional include-linked refresh beg end)
  15845. "Display inline images.
  15846. Normally only links without a description part are inlined, because this
  15847. is how it will work for export. When INCLUDE-LINKED is set, also links
  15848. with a description part will be inlined. This can be nice for a quick
  15849. look at those images, but it does not reflect what exported files will look
  15850. like.
  15851. When REFRESH is set, refresh existing images between BEG and END.
  15852. This will create new image displays only if necessary.
  15853. BEG and END default to the buffer boundaries."
  15854. (interactive "P")
  15855. (unless refresh
  15856. (org-remove-inline-images)
  15857. (if (fboundp 'clear-image-cache) (clear-image-cache)))
  15858. (save-excursion
  15859. (save-restriction
  15860. (widen)
  15861. (setq beg (or beg (point-min)) end (or end (point-max)))
  15862. (goto-char beg)
  15863. (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
  15864. (substring (org-image-file-name-regexp) 0 -2)
  15865. "\\)\\]" (if include-linked "" "\\]")))
  15866. old file ov img)
  15867. (while (re-search-forward re end t)
  15868. (setq old (get-char-property-and-overlay (match-beginning 1)
  15869. 'org-image-overlay))
  15870. (setq file (expand-file-name
  15871. (concat (or (match-string 3) "") (match-string 4))))
  15872. (when (file-exists-p file)
  15873. (if (and (car-safe old) refresh)
  15874. (image-refresh (overlay-get (cdr old) 'display))
  15875. (setq img (save-match-data (create-image file)))
  15876. (when img
  15877. (setq ov (make-overlay (match-beginning 0) (match-end 0)))
  15878. (overlay-put ov 'display img)
  15879. (overlay-put ov 'face 'default)
  15880. (overlay-put ov 'org-image-overlay t)
  15881. (overlay-put ov 'modification-hooks
  15882. (list 'org-display-inline-modification-hook))
  15883. (push ov org-inline-image-overlays)))))))))
  15884. (defun org-display-inline-modification-hook (ov after beg end &optional len)
  15885. "Remove inline-display overlay if a corresponding region is modified."
  15886. (let ((inhibit-modification-hooks t))
  15887. (when (and ov after)
  15888. (delete ov org-inline-image-overlays)
  15889. (delete-overlay ov))))
  15890. (defun org-remove-inline-images ()
  15891. "Remove inline display of images."
  15892. (interactive)
  15893. (mapc 'delete-overlay org-inline-image-overlays)
  15894. (setq org-inline-image-overlays nil))
  15895. ;;;; Key bindings
  15896. ;; Outline functions from `outline-mode-prefix-map'
  15897. ;; that can be remapped in Org:
  15898. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  15899. (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
  15900. (define-key org-mode-map [remap outline-forward-same-level]
  15901. 'org-forward-same-level)
  15902. (define-key org-mode-map [remap outline-backward-same-level]
  15903. 'org-backward-same-level)
  15904. (define-key org-mode-map [remap show-branches]
  15905. 'org-kill-note-or-show-branches)
  15906. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  15907. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  15908. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  15909. ;; Outline functions from `outline-mode-prefix-map'
  15910. ;; that can not be remapped in Org:
  15911. ;; - the column "key binding" shows whether the Outline function is still
  15912. ;; available in Org mode on the same key that it has been bound to in
  15913. ;; Outline mode:
  15914. ;; - "overridden": key used for a different functionality in Org mode
  15915. ;; - else: key still bound to the same Outline function in Org mode
  15916. ;; | Outline function | key binding | Org replacement |
  15917. ;; |------------------------------------+-------------+-----------------------|
  15918. ;; | `outline-next-visible-heading' | `C-c C-n' | still same function |
  15919. ;; | `outline-previous-visible-heading' | `C-c C-p' | still same function |
  15920. ;; | `show-children' | `C-c C-i' | visibility cycling |
  15921. ;; | `hide-subtree' | overridden | visibility cycling |
  15922. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  15923. ;; | `hide-body' | overridden | no replacement |
  15924. ;; | `show-all' | overridden | no replacement |
  15925. ;; | `hide-entry' | overridden | visibility cycling |
  15926. ;; | `show-entry' | overridden | no replacement |
  15927. ;; | `hide-leaves' | overridden | no replacement |
  15928. ;; | `hide-sublevels' | overridden | no replacement |
  15929. ;; | `hide-other' | overridden | no replacement |
  15930. ;; | `outline-move-subtree-up' | `C-c C-^' | better: org-shiftup |
  15931. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  15932. ;; Make `C-c C-x' a prefix key
  15933. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  15934. ;; TAB key with modifiers
  15935. (org-defkey org-mode-map "\C-i" 'org-cycle)
  15936. (org-defkey org-mode-map [(tab)] 'org-cycle)
  15937. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  15938. (org-defkey org-mode-map "\M-\t" 'pcomplete)
  15939. ;; The following line is necessary under Suse GNU/Linux
  15940. (unless (featurep 'xemacs)
  15941. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  15942. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  15943. (define-key org-mode-map [backtab] 'org-shifttab)
  15944. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  15945. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  15946. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  15947. ;; Cursor keys with modifiers
  15948. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  15949. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  15950. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  15951. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  15952. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  15953. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  15954. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  15955. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  15956. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  15957. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  15958. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  15959. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  15960. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  15961. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  15962. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  15963. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  15964. ;; Babel keys
  15965. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  15966. (mapc (lambda (pair)
  15967. (define-key org-babel-map (car pair) (cdr pair)))
  15968. org-babel-key-bindings)
  15969. ;;; Extra keys for tty access.
  15970. ;; We only set them when really needed because otherwise the
  15971. ;; menus don't show the simple keys
  15972. (when (or org-use-extra-keys
  15973. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  15974. (not window-system))
  15975. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  15976. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  15977. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  15978. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  15979. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  15980. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  15981. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  15982. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  15983. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  15984. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  15985. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  15986. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  15987. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  15988. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  15989. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  15990. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  15991. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  15992. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  15993. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  15994. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  15995. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  15996. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  15997. (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
  15998. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  15999. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  16000. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  16001. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  16002. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  16003. ;; All the other keys
  16004. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  16005. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  16006. (if (boundp 'narrow-map)
  16007. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  16008. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  16009. (if (boundp 'narrow-map)
  16010. (org-defkey narrow-map "b" 'org-narrow-to-block)
  16011. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  16012. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-same-level)
  16013. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-same-level)
  16014. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  16015. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  16016. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  16017. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  16018. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  16019. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  16020. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  16021. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  16022. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  16023. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  16024. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  16025. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  16026. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  16027. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  16028. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  16029. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  16030. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  16031. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  16032. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  16033. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  16034. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  16035. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  16036. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  16037. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  16038. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  16039. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  16040. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  16041. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  16042. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  16043. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  16044. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  16045. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  16046. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  16047. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  16048. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  16049. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  16050. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  16051. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  16052. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  16053. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  16054. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  16055. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  16056. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  16057. (org-defkey org-mode-map "\C-c^" 'org-sort)
  16058. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  16059. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  16060. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  16061. (org-defkey org-mode-map "\C-m" 'org-return)
  16062. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  16063. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  16064. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  16065. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  16066. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  16067. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  16068. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  16069. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  16070. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  16071. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  16072. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  16073. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  16074. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  16075. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  16076. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  16077. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  16078. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  16079. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  16080. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  16081. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  16082. (org-defkey org-mode-map "\C-c\C-@" 'org-mark-list)
  16083. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  16084. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  16085. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  16086. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  16087. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  16088. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  16089. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  16090. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  16091. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  16092. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  16093. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  16094. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  16095. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  16096. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  16097. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  16098. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  16099. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  16100. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  16101. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  16102. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  16103. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  16104. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  16105. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  16106. (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
  16107. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  16108. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  16109. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  16110. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  16111. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  16112. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  16113. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  16114. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  16115. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  16116. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  16117. (when (featurep 'xemacs)
  16118. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  16119. (defconst org-speed-commands-default
  16120. '(
  16121. ("Outline Navigation")
  16122. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  16123. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  16124. ("f" . (org-speed-move-safe 'org-forward-same-level))
  16125. ("b" . (org-speed-move-safe 'org-backward-same-level))
  16126. ("u" . (org-speed-move-safe 'outline-up-heading))
  16127. ("j" . org-goto)
  16128. ("g" . (org-refile t))
  16129. ("Outline Visibility")
  16130. ("c" . org-cycle)
  16131. ("C" . org-shifttab)
  16132. (" " . org-display-outline-path)
  16133. ("Outline Structure Editing")
  16134. ("U" . org-shiftmetaup)
  16135. ("D" . org-shiftmetadown)
  16136. ("r" . org-metaright)
  16137. ("l" . org-metaleft)
  16138. ("R" . org-shiftmetaright)
  16139. ("L" . org-shiftmetaleft)
  16140. ("i" . (progn (forward-char 1) (call-interactively
  16141. 'org-insert-heading-respect-content)))
  16142. ("^" . org-sort)
  16143. ("w" . org-refile)
  16144. ("a" . org-archive-subtree-default-with-confirmation)
  16145. ("." . org-mark-subtree)
  16146. ("Clock Commands")
  16147. ("I" . org-clock-in)
  16148. ("O" . org-clock-out)
  16149. ("Meta Data Editing")
  16150. ("t" . org-todo)
  16151. ("0" . (org-priority ?\ ))
  16152. ("1" . (org-priority ?A))
  16153. ("2" . (org-priority ?B))
  16154. ("3" . (org-priority ?C))
  16155. (";" . org-set-tags-command)
  16156. ("e" . org-set-effort)
  16157. ("Agenda Views etc")
  16158. ("v" . org-agenda)
  16159. ("/" . org-sparse-tree)
  16160. ("Misc")
  16161. ("o" . org-open-at-point)
  16162. ("?" . org-speed-command-help)
  16163. ("<" . (org-agenda-set-restriction-lock 'subtree))
  16164. (">" . (org-agenda-remove-restriction-lock))
  16165. )
  16166. "The default speed commands.")
  16167. (defun org-print-speed-command (e)
  16168. (if (> (length (car e)) 1)
  16169. (progn
  16170. (princ "\n")
  16171. (princ (car e))
  16172. (princ "\n")
  16173. (princ (make-string (length (car e)) ?-))
  16174. (princ "\n"))
  16175. (princ (car e))
  16176. (princ " ")
  16177. (if (symbolp (cdr e))
  16178. (princ (symbol-name (cdr e)))
  16179. (prin1 (cdr e)))
  16180. (princ "\n")))
  16181. (defun org-speed-command-help ()
  16182. "Show the available speed commands."
  16183. (interactive)
  16184. (if (not org-use-speed-commands)
  16185. (error "Speed commands are not activated, customize `org-use-speed-commands'")
  16186. (with-output-to-temp-buffer "*Help*"
  16187. (princ "User-defined Speed commands\n===========================\n")
  16188. (mapc 'org-print-speed-command org-speed-commands-user)
  16189. (princ "\n")
  16190. (princ "Built-in Speed commands\n=======================\n")
  16191. (mapc 'org-print-speed-command org-speed-commands-default))
  16192. (with-current-buffer "*Help*"
  16193. (setq truncate-lines t))))
  16194. (defun org-speed-move-safe (cmd)
  16195. "Execute CMD, but make sure that the cursor always ends up in a headline.
  16196. If not, return to the original position and throw an error."
  16197. (interactive)
  16198. (let ((pos (point)))
  16199. (call-interactively cmd)
  16200. (unless (and (bolp) (org-at-heading-p))
  16201. (goto-char pos)
  16202. (error "Boundary reached while executing %s" cmd))))
  16203. (defvar org-self-insert-command-undo-counter 0)
  16204. (defvar org-table-auto-blank-field) ; defined in org-table.el
  16205. (defvar org-speed-command nil)
  16206. (defun org-speed-command-default-hook (keys)
  16207. "Hook for activating single-letter speed commands.
  16208. `org-speed-commands-default' specifies a minimal command set.
  16209. Use `org-speed-commands-user' for further customization."
  16210. (when (or (and (bolp) (looking-at org-outline-regexp))
  16211. (and (functionp org-use-speed-commands)
  16212. (funcall org-use-speed-commands)))
  16213. (cdr (assoc keys (append org-speed-commands-user
  16214. org-speed-commands-default)))))
  16215. (defun org-babel-speed-command-hook (keys)
  16216. "Hook for activating single-letter code block commands."
  16217. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  16218. (cdr (assoc keys org-babel-key-bindings))))
  16219. (defcustom org-speed-command-hook
  16220. '(org-speed-command-default-hook org-babel-speed-command-hook)
  16221. "Hook for activating speed commands at strategic locations.
  16222. Hook functions are called in sequence until a valid handler is
  16223. found.
  16224. Each hook takes a single argument, a user-pressed command key
  16225. which is also a `self-insert-command' from the global map.
  16226. Within the hook, examine the cursor position and the command key
  16227. and return nil or a valid handler as appropriate. Handler could
  16228. be one of an interactive command, a function, or a form.
  16229. Set `org-use-speed-commands' to non-nil value to enable this
  16230. hook. The default setting is `org-speed-command-default-hook'."
  16231. :group 'org-structure
  16232. :version "24.1"
  16233. :type 'hook)
  16234. (defun org-self-insert-command (N)
  16235. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  16236. If the cursor is in a table looking at whitespace, the whitespace is
  16237. overwritten, and the table is not marked as requiring realignment."
  16238. (interactive "p")
  16239. (org-check-before-invisible-edit 'insert)
  16240. (cond
  16241. ((and org-use-speed-commands
  16242. (setq org-speed-command
  16243. (run-hook-with-args-until-success
  16244. 'org-speed-command-hook (this-command-keys))))
  16245. (cond
  16246. ((commandp org-speed-command)
  16247. (setq this-command org-speed-command)
  16248. (call-interactively org-speed-command))
  16249. ((functionp org-speed-command)
  16250. (funcall org-speed-command))
  16251. ((and org-speed-command (listp org-speed-command))
  16252. (eval org-speed-command))
  16253. (t (let (org-use-speed-commands)
  16254. (call-interactively 'org-self-insert-command)))))
  16255. ((and
  16256. (org-table-p)
  16257. (progn
  16258. ;; check if we blank the field, and if that triggers align
  16259. (and (featurep 'org-table) org-table-auto-blank-field
  16260. (member last-command
  16261. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  16262. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  16263. ;; got extra space, this field does not determine column width
  16264. (let (org-table-may-need-update) (org-table-blank-field))
  16265. ;; no extra space, this field may determine column width
  16266. (org-table-blank-field)))
  16267. t)
  16268. (eq N 1)
  16269. (looking-at "[^|\n]* |"))
  16270. (let (org-table-may-need-update)
  16271. (goto-char (1- (match-end 0)))
  16272. (backward-delete-char 1)
  16273. (goto-char (match-beginning 0))
  16274. (self-insert-command N)))
  16275. (t
  16276. (setq org-table-may-need-update t)
  16277. (self-insert-command N)
  16278. (org-fix-tags-on-the-fly)
  16279. (if org-self-insert-cluster-for-undo
  16280. (if (not (eq last-command 'org-self-insert-command))
  16281. (setq org-self-insert-command-undo-counter 1)
  16282. (if (>= org-self-insert-command-undo-counter 20)
  16283. (setq org-self-insert-command-undo-counter 1)
  16284. (and (> org-self-insert-command-undo-counter 0)
  16285. buffer-undo-list (listp buffer-undo-list)
  16286. (not (cadr buffer-undo-list)) ; remove nil entry
  16287. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  16288. (setq org-self-insert-command-undo-counter
  16289. (1+ org-self-insert-command-undo-counter))))))))
  16290. (defun org-check-before-invisible-edit (kind)
  16291. "Check is editing if kind KIND would be dangerous with invisible text around.
  16292. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  16293. ;; First, try to get out of here as quickly as possible, to reduce overhead
  16294. (if (and org-catch-invisible-edits
  16295. (or (not (boundp 'visible-mode)) (not visible-mode))
  16296. (or (get-char-property (point) 'invisible)
  16297. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  16298. ;; OK, we need to take a closer look
  16299. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  16300. (invisible-before-point (if (bobp) nil (get-char-property
  16301. (1- (point)) 'invisible)))
  16302. (border-and-ok-direction
  16303. (or
  16304. ;; Check if we are acting predictably before invisible text
  16305. (and invisible-at-point (not invisible-before-point)
  16306. (memq kind '(insert delete-backward)))
  16307. ;; Check if we are acting predictably after invisible text
  16308. ;; This works not well, and I have turned it off. It seems
  16309. ;; better to always show and stop after invisible text.
  16310. ;; (and (not invisible-at-point) invisible-before-point
  16311. ;; (memq kind '(insert delete)))
  16312. )))
  16313. (when (or (memq invisible-at-point '(outline org-hide-block))
  16314. (memq invisible-before-point '(outline org-hide-block)))
  16315. (if (eq org-catch-invisible-edits 'error)
  16316. (error "Editing in invisible areas is prohibited - make visible first"))
  16317. ;; Make the area visible
  16318. (save-excursion
  16319. (if invisible-before-point
  16320. (goto-char (previous-single-char-property-change
  16321. (point) 'invisible)))
  16322. (org-cycle))
  16323. (cond
  16324. ((eq org-catch-invisible-edits 'show)
  16325. ;; That's it, we do the edit after showing
  16326. (message
  16327. "Unfolding invisible region around point before editing")
  16328. (sit-for 1))
  16329. ((and (eq org-catch-invisible-edits 'smart)
  16330. border-and-ok-direction)
  16331. (message "Unfolding invisible region around point before editing"))
  16332. (t
  16333. ;; Don't do the edit, make the user repeat it in full visibility
  16334. (error "Edit in invisible region aborted, repeat to confirm with text visible")))))))
  16335. (defun org-fix-tags-on-the-fly ()
  16336. (when (and (equal (char-after (point-at-bol)) ?*)
  16337. (org-at-heading-p))
  16338. (org-align-tags-here org-tags-column)))
  16339. (defun org-delete-backward-char (N)
  16340. "Like `delete-backward-char', insert whitespace at field end in tables.
  16341. When deleting backwards, in tables this function will insert whitespace in
  16342. front of the next \"|\" separator, to keep the table aligned. The table will
  16343. still be marked for re-alignment if the field did fill the entire column,
  16344. because, in this case the deletion might narrow the column."
  16345. (interactive "p")
  16346. (org-check-before-invisible-edit 'delete-backward)
  16347. (if (and (org-table-p)
  16348. (eq N 1)
  16349. (string-match "|" (buffer-substring (point-at-bol) (point)))
  16350. (looking-at ".*?|"))
  16351. (let ((pos (point))
  16352. (noalign (looking-at "[^|\n\r]* |"))
  16353. (c org-table-may-need-update))
  16354. (backward-delete-char N)
  16355. (if (not overwrite-mode)
  16356. (progn
  16357. (skip-chars-forward "^|")
  16358. (insert " ")
  16359. (goto-char (1- pos))))
  16360. ;; noalign: if there were two spaces at the end, this field
  16361. ;; does not determine the width of the column.
  16362. (if noalign (setq org-table-may-need-update c)))
  16363. (backward-delete-char N)
  16364. (org-fix-tags-on-the-fly)))
  16365. (defun org-delete-char (N)
  16366. "Like `delete-char', but insert whitespace at field end in tables.
  16367. When deleting characters, in tables this function will insert whitespace in
  16368. front of the next \"|\" separator, to keep the table aligned. The table will
  16369. still be marked for re-alignment if the field did fill the entire column,
  16370. because, in this case the deletion might narrow the column."
  16371. (interactive "p")
  16372. (org-check-before-invisible-edit 'delete)
  16373. (if (and (org-table-p)
  16374. (not (bolp))
  16375. (not (= (char-after) ?|))
  16376. (eq N 1))
  16377. (if (looking-at ".*?|")
  16378. (let ((pos (point))
  16379. (noalign (looking-at "[^|\n\r]* |"))
  16380. (c org-table-may-need-update))
  16381. (replace-match (concat
  16382. (substring (match-string 0) 1 -1)
  16383. " |"))
  16384. (goto-char pos)
  16385. ;; noalign: if there were two spaces at the end, this field
  16386. ;; does not determine the width of the column.
  16387. (if noalign (setq org-table-may-need-update c)))
  16388. (delete-char N))
  16389. (delete-char N)
  16390. (org-fix-tags-on-the-fly)))
  16391. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  16392. (put 'org-self-insert-command 'delete-selection t)
  16393. (put 'orgtbl-self-insert-command 'delete-selection t)
  16394. (put 'org-delete-char 'delete-selection 'supersede)
  16395. (put 'org-delete-backward-char 'delete-selection 'supersede)
  16396. (put 'org-yank 'delete-selection 'yank)
  16397. ;; Make `flyspell-mode' delay after some commands
  16398. (put 'org-self-insert-command 'flyspell-delayed t)
  16399. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  16400. (put 'org-delete-char 'flyspell-delayed t)
  16401. (put 'org-delete-backward-char 'flyspell-delayed t)
  16402. ;; Make pabbrev-mode expand after org-mode commands
  16403. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  16404. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  16405. ;; How to do this: Measure non-white length of current string
  16406. ;; If equal to column width, we should realign.
  16407. (defun org-remap (map &rest commands)
  16408. "In MAP, remap the functions given in COMMANDS.
  16409. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  16410. (let (new old)
  16411. (while commands
  16412. (setq old (pop commands) new (pop commands))
  16413. (if (fboundp 'command-remapping)
  16414. (org-defkey map (vector 'remap old) new)
  16415. (substitute-key-definition old new map global-map)))))
  16416. (when (eq org-enable-table-editor 'optimized)
  16417. ;; If the user wants maximum table support, we need to hijack
  16418. ;; some standard editing functions
  16419. (org-remap org-mode-map
  16420. 'self-insert-command 'org-self-insert-command
  16421. 'delete-char 'org-delete-char
  16422. 'delete-backward-char 'org-delete-backward-char)
  16423. (org-defkey org-mode-map "|" 'org-force-self-insert))
  16424. (defvar org-ctrl-c-ctrl-c-hook nil
  16425. "Hook for functions attaching themselves to `C-c C-c'.
  16426. This can be used to add additional functionality to the C-c C-c
  16427. key which executes context-dependent commands. This hook is run
  16428. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  16429. run after the last test.
  16430. Each function will be called with no arguments. The function
  16431. must check if the context is appropriate for it to act. If yes,
  16432. it should do its thing and then return a non-nil value. If the
  16433. context is wrong, just do nothing and return nil.")
  16434. (defvar org-ctrl-c-ctrl-c-final-hook nil
  16435. "Hook for functions attaching themselves to `C-c C-c'.
  16436. This can be used to add additional functionality to the C-c C-c
  16437. key which executes context-dependent commands. This hook is run
  16438. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  16439. before the first test.
  16440. Each function will be called with no arguments. The function
  16441. must check if the context is appropriate for it to act. If yes,
  16442. it should do its thing and then return a non-nil value. If the
  16443. context is wrong, just do nothing and return nil.")
  16444. (defvar org-tab-first-hook nil
  16445. "Hook for functions to attach themselves to TAB.
  16446. See `org-ctrl-c-ctrl-c-hook' for more information.
  16447. This hook runs as the first action when TAB is pressed, even before
  16448. `org-cycle' messes around with the `outline-regexp' to cater for
  16449. inline tasks and plain list item folding.
  16450. If any function in this hook returns t, any other actions that
  16451. would have been caused by TAB (such as table field motion or visibility
  16452. cycling) will not occur.")
  16453. (defvar org-tab-after-check-for-table-hook nil
  16454. "Hook for functions to attach themselves to TAB.
  16455. See `org-ctrl-c-ctrl-c-hook' for more information.
  16456. This hook runs after it has been established that the cursor is not in a
  16457. table, but before checking if the cursor is in a headline or if global cycling
  16458. should be done.
  16459. If any function in this hook returns t, not other actions like visibility
  16460. cycling will be done.")
  16461. (defvar org-tab-after-check-for-cycling-hook nil
  16462. "Hook for functions to attach themselves to TAB.
  16463. See `org-ctrl-c-ctrl-c-hook' for more information.
  16464. This hook runs after it has been established that not table field motion and
  16465. not visibility should be done because of current context. This is probably
  16466. the place where a package like yasnippets can hook in.")
  16467. (defvar org-tab-before-tab-emulation-hook nil
  16468. "Hook for functions to attach themselves to TAB.
  16469. See `org-ctrl-c-ctrl-c-hook' for more information.
  16470. This hook runs after every other options for TAB have been exhausted, but
  16471. before indentation and \t insertion takes place.")
  16472. (defvar org-metaleft-hook nil
  16473. "Hook for functions attaching themselves to `M-left'.
  16474. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16475. (defvar org-metaright-hook nil
  16476. "Hook for functions attaching themselves to `M-right'.
  16477. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16478. (defvar org-metaup-hook nil
  16479. "Hook for functions attaching themselves to `M-up'.
  16480. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16481. (defvar org-metadown-hook nil
  16482. "Hook for functions attaching themselves to `M-down'.
  16483. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16484. (defvar org-shiftmetaleft-hook nil
  16485. "Hook for functions attaching themselves to `M-S-left'.
  16486. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16487. (defvar org-shiftmetaright-hook nil
  16488. "Hook for functions attaching themselves to `M-S-right'.
  16489. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16490. (defvar org-shiftmetaup-hook nil
  16491. "Hook for functions attaching themselves to `M-S-up'.
  16492. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16493. (defvar org-shiftmetadown-hook nil
  16494. "Hook for functions attaching themselves to `M-S-down'.
  16495. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16496. (defvar org-metareturn-hook nil
  16497. "Hook for functions attaching themselves to `M-RET'.
  16498. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16499. (defvar org-shiftup-hook nil
  16500. "Hook for functions attaching themselves to `S-up'.
  16501. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16502. (defvar org-shiftup-final-hook nil
  16503. "Hook for functions attaching themselves to `S-up'.
  16504. This one runs after all other options except shift-select have been excluded.
  16505. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16506. (defvar org-shiftdown-hook nil
  16507. "Hook for functions attaching themselves to `S-down'.
  16508. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16509. (defvar org-shiftdown-final-hook nil
  16510. "Hook for functions attaching themselves to `S-down'.
  16511. This one runs after all other options except shift-select have been excluded.
  16512. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16513. (defvar org-shiftleft-hook nil
  16514. "Hook for functions attaching themselves to `S-left'.
  16515. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16516. (defvar org-shiftleft-final-hook nil
  16517. "Hook for functions attaching themselves to `S-left'.
  16518. This one runs after all other options except shift-select have been excluded.
  16519. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16520. (defvar org-shiftright-hook nil
  16521. "Hook for functions attaching themselves to `S-right'.
  16522. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16523. (defvar org-shiftright-final-hook nil
  16524. "Hook for functions attaching themselves to `S-right'.
  16525. This one runs after all other options except shift-select have been excluded.
  16526. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16527. (defun org-modifier-cursor-error ()
  16528. "Throw an error, a modified cursor command was applied in wrong context."
  16529. (error "This command is active in special context like tables, headlines or items"))
  16530. (defun org-shiftselect-error ()
  16531. "Throw an error because Shift-Cursor command was applied in wrong context."
  16532. (if (and (boundp 'shift-select-mode) shift-select-mode)
  16533. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  16534. (error "This command works only in special context like headlines or timestamps")))
  16535. (defun org-call-for-shift-select (cmd)
  16536. (let ((this-command-keys-shift-translated t))
  16537. (call-interactively cmd)))
  16538. (defun org-shifttab (&optional arg)
  16539. "Global visibility cycling or move to previous table field.
  16540. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  16541. on context.
  16542. See the individual commands for more information."
  16543. (interactive "P")
  16544. (cond
  16545. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  16546. ((integerp arg)
  16547. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  16548. (message "Content view to level: %d" arg)
  16549. (org-content (prefix-numeric-value arg2))
  16550. (setq org-cycle-global-status 'overview)))
  16551. (t (call-interactively 'org-global-cycle))))
  16552. (defun org-shiftmetaleft ()
  16553. "Promote subtree or delete table column.
  16554. Calls `org-promote-subtree', `org-outdent-item-tree', or
  16555. `org-table-delete-column', depending on context. See the
  16556. individual commands for more information."
  16557. (interactive)
  16558. (cond
  16559. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  16560. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  16561. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  16562. ((if (not (org-region-active-p)) (org-at-item-p)
  16563. (save-excursion (goto-char (region-beginning))
  16564. (org-at-item-p)))
  16565. (call-interactively 'org-outdent-item-tree))
  16566. (t (org-modifier-cursor-error))))
  16567. (defun org-shiftmetaright ()
  16568. "Demote subtree or insert table column.
  16569. Calls `org-demote-subtree', `org-indent-item-tree', or
  16570. `org-table-insert-column', depending on context. See the
  16571. individual commands for more information."
  16572. (interactive)
  16573. (cond
  16574. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  16575. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  16576. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  16577. ((if (not (org-region-active-p)) (org-at-item-p)
  16578. (save-excursion (goto-char (region-beginning))
  16579. (org-at-item-p)))
  16580. (call-interactively 'org-indent-item-tree))
  16581. (t (org-modifier-cursor-error))))
  16582. (defun org-shiftmetaup (&optional arg)
  16583. "Move subtree up or kill table row.
  16584. Calls `org-move-subtree-up' or `org-table-kill-row' or
  16585. `org-move-item-up' depending on context. See the individual commands
  16586. for more information."
  16587. (interactive "P")
  16588. (cond
  16589. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  16590. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  16591. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  16592. ((org-at-item-p) (call-interactively 'org-move-item-up))
  16593. (t (org-modifier-cursor-error))))
  16594. (defun org-shiftmetadown (&optional arg)
  16595. "Move subtree down or insert table row.
  16596. Calls `org-move-subtree-down' or `org-table-insert-row' or
  16597. `org-move-item-down', depending on context. See the individual
  16598. commands for more information."
  16599. (interactive "P")
  16600. (cond
  16601. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  16602. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  16603. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  16604. ((org-at-item-p) (call-interactively 'org-move-item-down))
  16605. (t (org-modifier-cursor-error))))
  16606. (defsubst org-hidden-tree-error ()
  16607. (error
  16608. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  16609. (defun org-metaleft (&optional arg)
  16610. "Promote heading or move table column to left.
  16611. Calls `org-do-promote' or `org-table-move-column', depending on context.
  16612. With no specific context, calls the Emacs default `backward-word'.
  16613. See the individual commands for more information."
  16614. (interactive "P")
  16615. (cond
  16616. ((run-hook-with-args-until-success 'org-metaleft-hook))
  16617. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  16618. ((org-with-limited-levels
  16619. (or (org-at-heading-p)
  16620. (and (org-region-active-p)
  16621. (save-excursion
  16622. (goto-char (region-beginning))
  16623. (org-at-heading-p)))))
  16624. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  16625. (call-interactively 'org-do-promote))
  16626. ;; At an inline task.
  16627. ((org-at-heading-p)
  16628. (call-interactively 'org-inlinetask-promote))
  16629. ((or (org-at-item-p)
  16630. (and (org-region-active-p)
  16631. (save-excursion
  16632. (goto-char (region-beginning))
  16633. (org-at-item-p))))
  16634. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  16635. (call-interactively 'org-outdent-item))
  16636. (t (call-interactively 'backward-word))))
  16637. (defun org-metaright (&optional arg)
  16638. "Demote a subtree, a list item or move table column to right.
  16639. In front of a drawer or a block keyword, indent it correctly.
  16640. With no specific context, calls the Emacs default `forward-word'.
  16641. See the individual commands for more information."
  16642. (interactive "P")
  16643. (cond
  16644. ((run-hook-with-args-until-success 'org-metaright-hook))
  16645. ((org-at-table-p) (call-interactively 'org-table-move-column))
  16646. ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
  16647. ((org-at-block-p) (call-interactively 'org-indent-block))
  16648. ((org-with-limited-levels
  16649. (or (org-at-heading-p)
  16650. (and (org-region-active-p)
  16651. (save-excursion
  16652. (goto-char (region-beginning))
  16653. (org-at-heading-p)))))
  16654. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  16655. (call-interactively 'org-do-demote))
  16656. ;; At an inline task.
  16657. ((org-at-heading-p)
  16658. (call-interactively 'org-inlinetask-demote))
  16659. ((or (org-at-item-p)
  16660. (and (org-region-active-p)
  16661. (save-excursion
  16662. (goto-char (region-beginning))
  16663. (org-at-item-p))))
  16664. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  16665. (call-interactively 'org-indent-item))
  16666. (t (call-interactively 'forward-word))))
  16667. (defun org-check-for-hidden (what)
  16668. "Check if there are hidden headlines/items in the current visual line.
  16669. WHAT can be either `headlines' or `items'. If the current line is
  16670. an outline or item heading and it has a folded subtree below it,
  16671. this function returns t, nil otherwise."
  16672. (let ((re (cond
  16673. ((eq what 'headlines) org-outline-regexp-bol)
  16674. ((eq what 'items) (org-item-beginning-re))
  16675. (t (error "This should not happen"))))
  16676. beg end)
  16677. (save-excursion
  16678. (catch 'exit
  16679. (unless (org-region-active-p)
  16680. (setq beg (point-at-bol))
  16681. (beginning-of-line 2)
  16682. (while (and (not (eobp)) ;; this is like `next-line'
  16683. (get-char-property (1- (point)) 'invisible))
  16684. (beginning-of-line 2))
  16685. (setq end (point))
  16686. (goto-char beg)
  16687. (goto-char (point-at-eol))
  16688. (setq end (max end (point)))
  16689. (while (re-search-forward re end t)
  16690. (if (get-char-property (match-beginning 0) 'invisible)
  16691. (throw 'exit t))))
  16692. nil))))
  16693. (defun org-metaup (&optional arg)
  16694. "Move subtree up or move table row up.
  16695. Calls `org-move-subtree-up' or `org-table-move-row' or
  16696. `org-move-item-up', depending on context. See the individual commands
  16697. for more information."
  16698. (interactive "P")
  16699. (cond
  16700. ((run-hook-with-args-until-success 'org-metaup-hook))
  16701. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  16702. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  16703. ((org-at-item-p) (call-interactively 'org-move-item-up))
  16704. (t (transpose-lines 1) (beginning-of-line -1))))
  16705. (defun org-metadown (&optional arg)
  16706. "Move subtree down or move table row down.
  16707. Calls `org-move-subtree-down' or `org-table-move-row' or
  16708. `org-move-item-down', depending on context. See the individual
  16709. commands for more information."
  16710. (interactive "P")
  16711. (cond
  16712. ((run-hook-with-args-until-success 'org-metadown-hook))
  16713. ((org-at-table-p) (call-interactively 'org-table-move-row))
  16714. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  16715. ((org-at-item-p) (call-interactively 'org-move-item-down))
  16716. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  16717. (defun org-shiftup (&optional arg)
  16718. "Increase item in timestamp or increase priority of current headline.
  16719. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  16720. depending on context. See the individual commands for more information."
  16721. (interactive "P")
  16722. (cond
  16723. ((run-hook-with-args-until-success 'org-shiftup-hook))
  16724. ((and org-support-shift-select (org-region-active-p))
  16725. (org-call-for-shift-select 'previous-line))
  16726. ((org-at-timestamp-p t)
  16727. (call-interactively (if org-edit-timestamp-down-means-later
  16728. 'org-timestamp-down 'org-timestamp-up)))
  16729. ((and (not (eq org-support-shift-select 'always))
  16730. org-enable-priority-commands
  16731. (org-at-heading-p))
  16732. (call-interactively 'org-priority-up))
  16733. ((and (not org-support-shift-select) (org-at-item-p))
  16734. (call-interactively 'org-previous-item))
  16735. ((org-clocktable-try-shift 'up arg))
  16736. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  16737. (org-support-shift-select
  16738. (org-call-for-shift-select 'previous-line))
  16739. (t (org-shiftselect-error))))
  16740. (defun org-shiftdown (&optional arg)
  16741. "Decrease item in timestamp or decrease priority of current headline.
  16742. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  16743. depending on context. See the individual commands for more information."
  16744. (interactive "P")
  16745. (cond
  16746. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  16747. ((and org-support-shift-select (org-region-active-p))
  16748. (org-call-for-shift-select 'next-line))
  16749. ((org-at-timestamp-p t)
  16750. (call-interactively (if org-edit-timestamp-down-means-later
  16751. 'org-timestamp-up 'org-timestamp-down)))
  16752. ((and (not (eq org-support-shift-select 'always))
  16753. org-enable-priority-commands
  16754. (org-at-heading-p))
  16755. (call-interactively 'org-priority-down))
  16756. ((and (not org-support-shift-select) (org-at-item-p))
  16757. (call-interactively 'org-next-item))
  16758. ((org-clocktable-try-shift 'down arg))
  16759. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  16760. (org-support-shift-select
  16761. (org-call-for-shift-select 'next-line))
  16762. (t (org-shiftselect-error))))
  16763. (defun org-shiftright (&optional arg)
  16764. "Cycle the thing at point or in the current line, depending on context.
  16765. Depending on context, this does one of the following:
  16766. - switch a timestamp at point one day into the future
  16767. - on a headline, switch to the next TODO keyword.
  16768. - on an item, switch entire list to the next bullet type
  16769. - on a property line, switch to the next allowed value
  16770. - on a clocktable definition line, move time block into the future"
  16771. (interactive "P")
  16772. (cond
  16773. ((run-hook-with-args-until-success 'org-shiftright-hook))
  16774. ((and org-support-shift-select (org-region-active-p))
  16775. (org-call-for-shift-select 'forward-char))
  16776. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  16777. ((and (not (eq org-support-shift-select 'always))
  16778. (org-at-heading-p))
  16779. (let ((org-inhibit-logging
  16780. (not org-treat-S-cursor-todo-selection-as-state-change))
  16781. (org-inhibit-blocking
  16782. (not org-treat-S-cursor-todo-selection-as-state-change)))
  16783. (org-call-with-arg 'org-todo 'right)))
  16784. ((or (and org-support-shift-select
  16785. (not (eq org-support-shift-select 'always))
  16786. (org-at-item-bullet-p))
  16787. (and (not org-support-shift-select) (org-at-item-p)))
  16788. (org-call-with-arg 'org-cycle-list-bullet nil))
  16789. ((and (not (eq org-support-shift-select 'always))
  16790. (org-at-property-p))
  16791. (call-interactively 'org-property-next-allowed-value))
  16792. ((org-clocktable-try-shift 'right arg))
  16793. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  16794. (org-support-shift-select
  16795. (org-call-for-shift-select 'forward-char))
  16796. (t (org-shiftselect-error))))
  16797. (defun org-shiftleft (&optional arg)
  16798. "Cycle the thing at point or in the current line, depending on context.
  16799. Depending on context, this does one of the following:
  16800. - switch a timestamp at point one day into the past
  16801. - on a headline, switch to the previous TODO keyword.
  16802. - on an item, switch entire list to the previous bullet type
  16803. - on a property line, switch to the previous allowed value
  16804. - on a clocktable definition line, move time block into the past"
  16805. (interactive "P")
  16806. (cond
  16807. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  16808. ((and org-support-shift-select (org-region-active-p))
  16809. (org-call-for-shift-select 'backward-char))
  16810. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  16811. ((and (not (eq org-support-shift-select 'always))
  16812. (org-at-heading-p))
  16813. (let ((org-inhibit-logging
  16814. (not org-treat-S-cursor-todo-selection-as-state-change))
  16815. (org-inhibit-blocking
  16816. (not org-treat-S-cursor-todo-selection-as-state-change)))
  16817. (org-call-with-arg 'org-todo 'left)))
  16818. ((or (and org-support-shift-select
  16819. (not (eq org-support-shift-select 'always))
  16820. (org-at-item-bullet-p))
  16821. (and (not org-support-shift-select) (org-at-item-p)))
  16822. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  16823. ((and (not (eq org-support-shift-select 'always))
  16824. (org-at-property-p))
  16825. (call-interactively 'org-property-previous-allowed-value))
  16826. ((org-clocktable-try-shift 'left arg))
  16827. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  16828. (org-support-shift-select
  16829. (org-call-for-shift-select 'backward-char))
  16830. (t (org-shiftselect-error))))
  16831. (defun org-shiftcontrolright ()
  16832. "Switch to next TODO set."
  16833. (interactive)
  16834. (cond
  16835. ((and org-support-shift-select (org-region-active-p))
  16836. (org-call-for-shift-select 'forward-word))
  16837. ((and (not (eq org-support-shift-select 'always))
  16838. (org-at-heading-p))
  16839. (org-call-with-arg 'org-todo 'nextset))
  16840. (org-support-shift-select
  16841. (org-call-for-shift-select 'forward-word))
  16842. (t (org-shiftselect-error))))
  16843. (defun org-shiftcontrolleft ()
  16844. "Switch to previous TODO set."
  16845. (interactive)
  16846. (cond
  16847. ((and org-support-shift-select (org-region-active-p))
  16848. (org-call-for-shift-select 'backward-word))
  16849. ((and (not (eq org-support-shift-select 'always))
  16850. (org-at-heading-p))
  16851. (org-call-with-arg 'org-todo 'previousset))
  16852. (org-support-shift-select
  16853. (org-call-for-shift-select 'backward-word))
  16854. (t (org-shiftselect-error))))
  16855. (defun org-shiftcontrolup ()
  16856. "Change timestamps synchronously up in CLOCK log lines."
  16857. (interactive)
  16858. (cond ((and (not org-support-shift-select)
  16859. (org-at-clock-log-p)
  16860. (org-at-timestamp-p t))
  16861. (org-clock-timestamps-up))
  16862. (t (org-shiftselect-error))))
  16863. (defun org-shiftcontroldown ()
  16864. "Change timestamps synchronously down in CLOCK log lines."
  16865. (interactive)
  16866. (cond ((and (not org-support-shift-select)
  16867. (org-at-clock-log-p)
  16868. (org-at-timestamp-p t))
  16869. (org-clock-timestamps-down))
  16870. (t (org-shiftselect-error))))
  16871. (defun org-ctrl-c-ret ()
  16872. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  16873. (interactive)
  16874. (cond
  16875. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  16876. (t (call-interactively 'org-insert-heading))))
  16877. (defun org-find-visible ()
  16878. (let ((s (point)))
  16879. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  16880. (get-char-property s 'invisible)))
  16881. s))
  16882. (defun org-find-invisible ()
  16883. (let ((s (point)))
  16884. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  16885. (not (get-char-property s 'invisible))))
  16886. s))
  16887. (defun org-copy-visible (beg end)
  16888. "Copy the visible parts of the region."
  16889. (interactive "r")
  16890. (let (snippets s)
  16891. (save-excursion
  16892. (save-restriction
  16893. (narrow-to-region beg end)
  16894. (setq s (goto-char (point-min)))
  16895. (while (not (= (point) (point-max)))
  16896. (goto-char (org-find-invisible))
  16897. (push (buffer-substring s (point)) snippets)
  16898. (setq s (goto-char (org-find-visible))))))
  16899. (kill-new (apply 'concat (nreverse snippets)))))
  16900. (defun org-copy-special ()
  16901. "Copy region in table or copy current subtree.
  16902. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  16903. See the individual commands for more information."
  16904. (interactive)
  16905. (call-interactively
  16906. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  16907. (defun org-cut-special ()
  16908. "Cut region in table or cut current subtree.
  16909. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  16910. See the individual commands for more information."
  16911. (interactive)
  16912. (call-interactively
  16913. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  16914. (defun org-paste-special (arg)
  16915. "Paste rectangular region into table, or past subtree relative to level.
  16916. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  16917. See the individual commands for more information."
  16918. (interactive "P")
  16919. (if (org-at-table-p)
  16920. (org-table-paste-rectangle)
  16921. (org-paste-subtree arg)))
  16922. (defun org-edit-special (&optional arg)
  16923. "Call a special editor for the stuff at point.
  16924. When at a table, call the formula editor with `org-table-edit-formulas'.
  16925. When at the first line of an src example, call `org-edit-src-code'.
  16926. When in an #+include line, visit the include file. Otherwise call
  16927. `ffap' to visit the file at point."
  16928. (interactive)
  16929. ;; possibly prep session before editing source
  16930. (when arg
  16931. (let* ((info (org-babel-get-src-block-info))
  16932. (lang (nth 0 info))
  16933. (params (nth 2 info))
  16934. (session (cdr (assoc :session params))))
  16935. (when (and info session) ;; we are in a source-code block with a session
  16936. (funcall
  16937. (intern (concat "org-babel-prep-session:" lang)) session params))))
  16938. (cond ;; proceed with `org-edit-special'
  16939. ((save-excursion
  16940. (beginning-of-line 1)
  16941. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  16942. (find-file (org-trim (match-string 1))))
  16943. ((org-edit-src-code))
  16944. ((org-edit-fixed-width-region))
  16945. ((org-at-table.el-p)
  16946. (org-edit-src-code))
  16947. ((or (org-at-table-p)
  16948. (save-excursion
  16949. (beginning-of-line 1)
  16950. (looking-at "[ \t]*#\\+TBLFM:")))
  16951. (call-interactively 'org-table-edit-formulas))
  16952. (t (call-interactively 'ffap))))
  16953. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  16954. (defun org-ctrl-c-ctrl-c (&optional arg)
  16955. "Set tags in headline, or update according to changed information at point.
  16956. This command does many different things, depending on context:
  16957. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  16958. this is what we do.
  16959. - If the cursor is on a statistics cookie, update it.
  16960. - If the cursor is in a headline, prompt for tags and insert them
  16961. into the current line, aligned to `org-tags-column'. When called
  16962. with prefix arg, realign all tags in the current buffer.
  16963. - If the cursor is in one of the special #+KEYWORD lines, this
  16964. triggers scanning the buffer for these lines and updating the
  16965. information.
  16966. - If the cursor is inside a table, realign the table. This command
  16967. works even if the automatic table editor has been turned off.
  16968. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  16969. the entire table.
  16970. - If the cursor is at a footnote reference or definition, jump to
  16971. the corresponding definition or references, respectively.
  16972. - If the cursor is a the beginning of a dynamic block, update it.
  16973. - If the current buffer is a capture buffer, close note and file it.
  16974. - If the cursor is on a <<<target>>>, update radio targets and
  16975. corresponding links in this buffer.
  16976. - If the cursor is on a numbered item in a plain list, renumber the
  16977. ordered list.
  16978. - If the cursor is on a checkbox, toggle it.
  16979. - If the cursor is on a code block, evaluate it. The variable
  16980. `org-confirm-babel-evaluate' can be used to control prompting
  16981. before code block evaluation, by default every code block
  16982. evaluation requires confirmation. Code block evaluation can be
  16983. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  16984. (interactive "P")
  16985. (let ((org-enable-table-editor t))
  16986. (cond
  16987. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  16988. org-occur-highlights
  16989. org-latex-fragment-image-overlays)
  16990. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  16991. (org-remove-occur-highlights)
  16992. (org-remove-latex-fragment-image-overlays)
  16993. (message "Temporary highlights/overlays removed from current buffer"))
  16994. ((and (local-variable-p 'org-finish-function (current-buffer))
  16995. (fboundp org-finish-function))
  16996. (funcall org-finish-function))
  16997. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  16998. ((org-in-regexp org-ts-regexp-both)
  16999. (org-timestamp-change 0 'day))
  17000. ((or (looking-at org-property-start-re)
  17001. (org-at-property-p))
  17002. (call-interactively 'org-property-action))
  17003. ((org-at-target-p) (call-interactively 'org-update-radio-target-regexp))
  17004. ((and (org-in-regexp "\\[\\([0-9]*%\\|[0-9]*/[0-9]*\\)\\]")
  17005. (or (org-at-heading-p) (org-at-item-p)))
  17006. (call-interactively 'org-update-statistics-cookies))
  17007. ((org-at-heading-p) (call-interactively 'org-set-tags))
  17008. ((org-at-table.el-p)
  17009. (message "Use C-c ' to edit table.el tables"))
  17010. ((org-at-table-p)
  17011. (org-table-maybe-eval-formula)
  17012. (if arg
  17013. (call-interactively 'org-table-recalculate)
  17014. (org-table-maybe-recalculate-line))
  17015. (call-interactively 'org-table-align)
  17016. (orgtbl-send-table 'maybe))
  17017. ((or (org-footnote-at-reference-p)
  17018. (org-footnote-at-definition-p))
  17019. (call-interactively 'org-footnote-action))
  17020. ((org-at-item-checkbox-p)
  17021. ;; Cursor at a checkbox: repair list and update checkboxes. Send
  17022. ;; list only if at top item.
  17023. (let* ((cbox (match-string 1))
  17024. (struct (org-list-struct))
  17025. (old-struct (copy-tree struct))
  17026. (parents (org-list-parents-alist struct))
  17027. (orderedp (org-entry-get nil "ORDERED"))
  17028. (firstp (= (org-list-get-top-point struct) (point-at-bol)))
  17029. block-item)
  17030. ;; Use a light version of `org-toggle-checkbox' to avoid
  17031. ;; computing list structure twice.
  17032. (let ((new-box (cond
  17033. ((equal arg '(16)) "[-]")
  17034. ((equal arg '(4)) nil)
  17035. ((equal "[X]" cbox) "[ ]")
  17036. (t "[X]"))))
  17037. (if (and firstp arg)
  17038. ;; If at first item of sub-list, remove check-box from
  17039. ;; every item at the same level.
  17040. (mapc
  17041. (lambda (pos) (org-list-set-checkbox pos struct new-box))
  17042. (org-list-get-all-items
  17043. (point-at-bol) struct (org-list-prevs-alist struct)))
  17044. (org-list-set-checkbox (point-at-bol) struct new-box)))
  17045. ;; Replicate `org-list-write-struct', while grabbing a return
  17046. ;; value from `org-list-struct-fix-box'.
  17047. (org-list-struct-fix-ind struct parents 2)
  17048. (org-list-struct-fix-item-end struct)
  17049. (let ((prevs (org-list-prevs-alist struct)))
  17050. (org-list-struct-fix-bul struct prevs)
  17051. (org-list-struct-fix-ind struct parents)
  17052. (setq block-item
  17053. (org-list-struct-fix-box struct parents prevs orderedp)))
  17054. (org-list-struct-apply-struct struct old-struct)
  17055. (org-update-checkbox-count-maybe)
  17056. (when block-item
  17057. (message
  17058. "Checkboxes were removed due to unchecked box at line %d"
  17059. (org-current-line block-item)))
  17060. (when firstp (org-list-send-list 'maybe))))
  17061. ((org-at-item-p)
  17062. ;; Cursor at an item: repair list. Do checkbox related actions
  17063. ;; only if function was called with an argument. Send list only
  17064. ;; if at top item.
  17065. (let* ((struct (org-list-struct))
  17066. (firstp (= (org-list-get-top-point struct) (point-at-bol)))
  17067. old-struct)
  17068. (when arg
  17069. (setq old-struct (copy-tree struct))
  17070. (if firstp
  17071. ;; If at first item of sub-list, add check-box to every
  17072. ;; item at the same level.
  17073. (mapc
  17074. (lambda (pos)
  17075. (unless (org-list-get-checkbox pos struct)
  17076. (org-list-set-checkbox pos struct "[ ]")))
  17077. (org-list-get-all-items
  17078. (point-at-bol) struct (org-list-prevs-alist struct)))
  17079. (org-list-set-checkbox (point-at-bol) struct "[ ]")))
  17080. (org-list-write-struct
  17081. struct (org-list-parents-alist struct) old-struct)
  17082. (when arg (org-update-checkbox-count-maybe))
  17083. (when firstp (org-list-send-list 'maybe))))
  17084. ((save-excursion (beginning-of-line 1) (looking-at org-dblock-start-re))
  17085. ;; Dynamic block
  17086. (beginning-of-line 1)
  17087. (save-excursion (org-update-dblock)))
  17088. ((save-excursion
  17089. (beginning-of-line 1)
  17090. (looking-at "[ \t]*#\\+\\([A-Z]+\\)"))
  17091. (cond
  17092. ((equal (match-string 1) "TBLFM")
  17093. ;; Recalculate the table before this line
  17094. (save-excursion
  17095. (beginning-of-line 1)
  17096. (skip-chars-backward " \r\n\t")
  17097. (if (org-at-table-p)
  17098. (org-call-with-arg 'org-table-recalculate (or arg t)))))
  17099. (t
  17100. (let ((org-inhibit-startup-visibility-stuff t)
  17101. (org-startup-align-all-tables nil))
  17102. (when (boundp 'org-table-coordinate-overlays)
  17103. (mapc 'delete-overlay org-table-coordinate-overlays)
  17104. (setq org-table-coordinate-overlays nil))
  17105. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  17106. (message "Local setup has been refreshed"))))
  17107. ((org-clock-update-time-maybe))
  17108. (t
  17109. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  17110. (error "C-c C-c can do nothing useful at this location"))))))
  17111. (defun org-mode-restart ()
  17112. "Restart Org-mode, to scan again for special lines.
  17113. Also updates the keyword regular expressions."
  17114. (interactive)
  17115. (org-mode)
  17116. (message "Org-mode restarted"))
  17117. (defun org-kill-note-or-show-branches ()
  17118. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  17119. (interactive)
  17120. (if (not org-finish-function)
  17121. (progn
  17122. (hide-subtree)
  17123. (call-interactively 'show-branches))
  17124. (let ((org-note-abort t))
  17125. (funcall org-finish-function))))
  17126. (defun org-return (&optional indent)
  17127. "Goto next table row or insert a newline.
  17128. Calls `org-table-next-row' or `newline', depending on context.
  17129. See the individual commands for more information."
  17130. (interactive)
  17131. (cond
  17132. ((or (bobp) (org-in-src-block-p))
  17133. (if indent (newline-and-indent) (newline)))
  17134. ((org-at-table-p)
  17135. (org-table-justify-field-maybe)
  17136. (call-interactively 'org-table-next-row))
  17137. ;; when `newline-and-indent' is called within a list, make sure
  17138. ;; text moved stays inside the item.
  17139. ((and (org-in-item-p) indent)
  17140. (if (and (org-at-item-p) (>= (point) (match-end 0)))
  17141. (progn
  17142. (save-match-data (newline))
  17143. (org-indent-line-to (length (match-string 0))))
  17144. (let ((ind (org-get-indentation)))
  17145. (newline)
  17146. (if (org-looking-back org-list-end-re)
  17147. (org-indent-line-function)
  17148. (org-indent-line-to ind)))))
  17149. ((and org-return-follows-link
  17150. (let ((tprop (get-text-property (point) 'face)))
  17151. (or (eq tprop 'org-link)
  17152. (and (listp tprop) (memq 'org-link tprop)))))
  17153. (call-interactively 'org-open-at-point))
  17154. ((and (org-at-heading-p)
  17155. (looking-at
  17156. (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
  17157. (org-show-entry)
  17158. (end-of-line 1)
  17159. (newline))
  17160. (t (if indent (newline-and-indent) (newline)))))
  17161. (defun org-return-indent ()
  17162. "Goto next table row or insert a newline and indent.
  17163. Calls `org-table-next-row' or `newline-and-indent', depending on
  17164. context. See the individual commands for more information."
  17165. (interactive)
  17166. (org-return t))
  17167. (defun org-ctrl-c-star ()
  17168. "Compute table, or change heading status of lines.
  17169. Calls `org-table-recalculate' or `org-toggle-heading',
  17170. depending on context."
  17171. (interactive)
  17172. (cond
  17173. ((org-at-table-p)
  17174. (call-interactively 'org-table-recalculate))
  17175. (t
  17176. ;; Convert all lines in region to list items
  17177. (call-interactively 'org-toggle-heading))))
  17178. (defun org-ctrl-c-minus ()
  17179. "Insert separator line in table or modify bullet status of line.
  17180. Also turns a plain line or a region of lines into list items.
  17181. Calls `org-table-insert-hline', `org-toggle-item', or
  17182. `org-cycle-list-bullet', depending on context."
  17183. (interactive)
  17184. (cond
  17185. ((org-at-table-p)
  17186. (call-interactively 'org-table-insert-hline))
  17187. ((org-region-active-p)
  17188. (call-interactively 'org-toggle-item))
  17189. ((org-in-item-p)
  17190. (call-interactively 'org-cycle-list-bullet))
  17191. (t
  17192. (call-interactively 'org-toggle-item))))
  17193. (defun org-toggle-item (arg)
  17194. "Convert headings or normal lines to items, items to normal lines.
  17195. If there is no active region, only the current line is considered.
  17196. If the first non blank line in the region is an headline, convert
  17197. all headlines to items, shifting text accordingly.
  17198. If it is an item, convert all items to normal lines.
  17199. If it is normal text, change region into an item. With a prefix
  17200. argument ARG, change each line in region into an item."
  17201. (interactive "P")
  17202. (let ((shift-text
  17203. (function
  17204. ;; Shift text in current section to IND, from point to END.
  17205. ;; The function leaves point to END line.
  17206. (lambda (ind end)
  17207. (let ((min-i 1000) (end (copy-marker end)))
  17208. ;; First determine the minimum indentation (MIN-I) of
  17209. ;; the text.
  17210. (save-excursion
  17211. (catch 'exit
  17212. (while (< (point) end)
  17213. (let ((i (org-get-indentation)))
  17214. (cond
  17215. ;; Skip blank lines and inline tasks.
  17216. ((looking-at "^[ \t]*$"))
  17217. ((looking-at org-outline-regexp-bol))
  17218. ;; We can't find less than 0 indentation.
  17219. ((zerop i) (throw 'exit (setq min-i 0)))
  17220. ((< i min-i) (setq min-i i))))
  17221. (forward-line))))
  17222. ;; Then indent each line so that a line indented to
  17223. ;; MIN-I becomes indented to IND. Ignore blank lines
  17224. ;; and inline tasks in the process.
  17225. (let ((delta (- ind min-i)))
  17226. (while (< (point) end)
  17227. (unless (or (looking-at "^[ \t]*$")
  17228. (looking-at org-outline-regexp-bol))
  17229. (org-indent-line-to (+ (org-get-indentation) delta)))
  17230. (forward-line)))))))
  17231. (skip-blanks
  17232. (function
  17233. ;; Return beginning of first non-blank line, starting from
  17234. ;; line at POS.
  17235. (lambda (pos)
  17236. (save-excursion
  17237. (goto-char pos)
  17238. (skip-chars-forward " \r\t\n")
  17239. (point-at-bol)))))
  17240. beg end)
  17241. ;; Determine boundaries of changes.
  17242. (if (org-region-active-p)
  17243. (setq beg (funcall skip-blanks (region-beginning))
  17244. end (copy-marker (region-end)))
  17245. (setq beg (funcall skip-blanks (point-at-bol))
  17246. end (copy-marker (point-at-eol))))
  17247. ;; Depending on the starting line, choose an action on the text
  17248. ;; between BEG and END.
  17249. (org-with-limited-levels
  17250. (save-excursion
  17251. (goto-char beg)
  17252. (cond
  17253. ;; Case 1. Start at an item: de-itemize. Note that it only
  17254. ;; happens when a region is active: `org-ctrl-c-minus'
  17255. ;; would call `org-cycle-list-bullet' otherwise.
  17256. ((org-at-item-p)
  17257. (while (< (point) end)
  17258. (when (org-at-item-p)
  17259. (skip-chars-forward " \t")
  17260. (delete-region (point) (match-end 0)))
  17261. (forward-line)))
  17262. ;; Case 2. Start at an heading: convert to items.
  17263. ((org-at-heading-p)
  17264. (let* ((bul (org-list-bullet-string "-"))
  17265. (bul-len (length bul))
  17266. ;; Indentation of the first heading. It should be
  17267. ;; relative to the indentation of its parent, if any.
  17268. (start-ind (save-excursion
  17269. (cond
  17270. ((not org-adapt-indentation) 0)
  17271. ((not (outline-previous-heading)) 0)
  17272. (t (length (match-string 0))))))
  17273. ;; Level of first heading. Further headings will be
  17274. ;; compared to it to determine hierarchy in the list.
  17275. (ref-level (org-reduced-level (org-outline-level))))
  17276. (while (< (point) end)
  17277. (let* ((level (org-reduced-level (org-outline-level)))
  17278. (delta (max 0 (- level ref-level))))
  17279. ;; If current headline is less indented than the first
  17280. ;; one, set it as reference, in order to preserve
  17281. ;; subtrees.
  17282. (when (< level ref-level) (setq ref-level level))
  17283. (replace-match bul t t)
  17284. (org-indent-line-to (+ start-ind (* delta bul-len)))
  17285. ;; Ensure all text down to END (or SECTION-END) belongs
  17286. ;; to the newly created item.
  17287. (let ((section-end (save-excursion
  17288. (or (outline-next-heading) (point)))))
  17289. (forward-line)
  17290. (funcall shift-text
  17291. (+ start-ind (* (1+ delta) bul-len))
  17292. (min end section-end)))))))
  17293. ;; Case 3. Normal line with ARG: turn each non-item line into
  17294. ;; an item.
  17295. (arg
  17296. (while (< (point) end)
  17297. (unless (or (org-at-heading-p) (org-at-item-p))
  17298. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  17299. (replace-match
  17300. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  17301. (forward-line)))
  17302. ;; Case 4. Normal line without ARG: make the first line of
  17303. ;; region an item, and shift indentation of others
  17304. ;; lines to set them as item's body.
  17305. (t (let* ((bul (org-list-bullet-string "-"))
  17306. (bul-len (length bul))
  17307. (ref-ind (org-get-indentation)))
  17308. (skip-chars-forward " \t")
  17309. (insert bul)
  17310. (forward-line)
  17311. (while (< (point) end)
  17312. ;; Ensure that lines less indented than first one
  17313. ;; still get included in item body.
  17314. (funcall shift-text
  17315. (+ ref-ind bul-len)
  17316. (min end (save-excursion (or (outline-next-heading)
  17317. (point)))))
  17318. (forward-line)))))))))
  17319. (defun org-toggle-heading (&optional nstars)
  17320. "Convert headings to normal text, or items or text to headings.
  17321. If there is no active region, only the current line is considered.
  17322. If the first non blank line is an headline, remove the stars from
  17323. all headlines in the region.
  17324. If it is a plain list item, turn all plain list items into headings.
  17325. If it is a normal line, turn each and every normal line (i.e. not
  17326. an heading or an item) in the region into a heading.
  17327. When converting a line into a heading, the number of stars is chosen
  17328. such that the lines become children of the current entry. However,
  17329. when a prefix argument is given, its value determines the number of
  17330. stars to add."
  17331. (interactive "P")
  17332. (let ((skip-blanks
  17333. (function
  17334. ;; Return beginning of first non-blank line, starting from
  17335. ;; line at POS.
  17336. (lambda (pos)
  17337. (save-excursion
  17338. (goto-char pos)
  17339. (skip-chars-forward " \r\t\n")
  17340. (point-at-bol)))))
  17341. beg end)
  17342. ;; Determine boundaries of changes. If region ends at a bol, do
  17343. ;; not consider the last line to be in the region.
  17344. (if (org-region-active-p)
  17345. (setq beg (funcall skip-blanks (region-beginning))
  17346. end (copy-marker (save-excursion
  17347. (goto-char (region-end))
  17348. (if (bolp) (point) (point-at-eol)))))
  17349. (setq beg (funcall skip-blanks (point-at-bol))
  17350. end (copy-marker (point-at-eol))))
  17351. ;; Ensure inline tasks don't count as headings.
  17352. (org-with-limited-levels
  17353. (save-excursion
  17354. (goto-char beg)
  17355. (cond
  17356. ;; Case 1. Started at an heading: de-star headings.
  17357. ((org-at-heading-p)
  17358. (while (< (point) end)
  17359. (when (org-at-heading-p t)
  17360. (looking-at org-outline-regexp) (replace-match ""))
  17361. (forward-line)))
  17362. ;; Case 2. Started at an item: change items into headlines.
  17363. ;; One star will be added by `org-list-to-subtree'.
  17364. ((org-at-item-p)
  17365. (let* ((stars (make-string
  17366. (if nstars
  17367. ;; subtract the star that will be added again by
  17368. ;; `org-list-to-subtree'
  17369. (1- (prefix-numeric-value current-prefix-arg))
  17370. (or (org-current-level) 0))
  17371. ?*))
  17372. (add-stars
  17373. (cond (nstars "") ; stars from prefix only
  17374. ((equal stars "") "") ; before first heading
  17375. (org-odd-levels-only "*") ; inside heading, odd
  17376. (t "")))) ; inside heading, oddeven
  17377. (while (< (point) end)
  17378. (when (org-at-item-p)
  17379. ;; Pay attention to cases when region ends before list.
  17380. (let* ((struct (org-list-struct))
  17381. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  17382. (save-restriction
  17383. (narrow-to-region (point) list-end)
  17384. (insert
  17385. (org-list-to-subtree
  17386. (org-list-parse-list t)
  17387. '(:istart (concat stars add-stars (funcall get-stars depth))
  17388. :icount (concat stars add-stars (funcall get-stars depth))))))))
  17389. (forward-line))))
  17390. ;; Case 3. Started at normal text: make every line an heading,
  17391. ;; skipping headlines and items.
  17392. (t (let* ((stars (make-string
  17393. (if nstars
  17394. (prefix-numeric-value current-prefix-arg)
  17395. (or (org-current-level) 0))
  17396. ?*))
  17397. (add-stars
  17398. (cond (nstars "") ; stars from prefix only
  17399. ((equal stars "") "*") ; before first heading
  17400. (org-odd-levels-only "**") ; inside heading, odd
  17401. (t "*"))) ; inside heading, oddeven
  17402. (rpl (concat stars add-stars " ")))
  17403. (while (< (point) end)
  17404. (when (and (not (org-at-heading-p)) (not (org-at-item-p))
  17405. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  17406. (replace-match (concat rpl (match-string 2))))
  17407. (forward-line)))))))))
  17408. (defun org-meta-return (&optional arg)
  17409. "Insert a new heading or wrap a region in a table.
  17410. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  17411. See the individual commands for more information."
  17412. (interactive "P")
  17413. (cond
  17414. ((run-hook-with-args-until-success 'org-metareturn-hook))
  17415. ((or (org-at-drawer-p) (org-at-property-p))
  17416. (newline-and-indent))
  17417. ((org-at-table-p)
  17418. (call-interactively 'org-table-wrap-region))
  17419. (t (call-interactively 'org-insert-heading))))
  17420. ;;; Menu entries
  17421. ;; Define the Org-mode menus
  17422. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  17423. '("Tbl"
  17424. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  17425. ["Next Field" org-cycle (org-at-table-p)]
  17426. ["Previous Field" org-shifttab (org-at-table-p)]
  17427. ["Next Row" org-return (org-at-table-p)]
  17428. "--"
  17429. ["Blank Field" org-table-blank-field (org-at-table-p)]
  17430. ["Edit Field" org-table-edit-field (org-at-table-p)]
  17431. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  17432. "--"
  17433. ("Column"
  17434. ["Move Column Left" org-metaleft (org-at-table-p)]
  17435. ["Move Column Right" org-metaright (org-at-table-p)]
  17436. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  17437. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  17438. ("Row"
  17439. ["Move Row Up" org-metaup (org-at-table-p)]
  17440. ["Move Row Down" org-metadown (org-at-table-p)]
  17441. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  17442. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  17443. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  17444. "--"
  17445. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  17446. ("Rectangle"
  17447. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  17448. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  17449. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  17450. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  17451. "--"
  17452. ("Calculate"
  17453. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  17454. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  17455. ["Edit Formulas" org-edit-special (org-at-table-p)]
  17456. "--"
  17457. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  17458. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  17459. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  17460. "--"
  17461. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  17462. "--"
  17463. ["Sum Column/Rectangle" org-table-sum
  17464. (or (org-at-table-p) (org-region-active-p))]
  17465. ["Which Column?" org-table-current-column (org-at-table-p)])
  17466. ["Debug Formulas"
  17467. org-table-toggle-formula-debugger
  17468. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  17469. ["Show Col/Row Numbers"
  17470. org-table-toggle-coordinate-overlays
  17471. :style toggle
  17472. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  17473. "--"
  17474. ["Create" org-table-create (and (not (org-at-table-p))
  17475. org-enable-table-editor)]
  17476. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  17477. ["Import from File" org-table-import (not (org-at-table-p))]
  17478. ["Export to File" org-table-export (org-at-table-p)]
  17479. "--"
  17480. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  17481. (easy-menu-define org-org-menu org-mode-map "Org menu"
  17482. '("Org"
  17483. ("Show/Hide"
  17484. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  17485. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  17486. ["Sparse Tree..." org-sparse-tree t]
  17487. ["Reveal Context" org-reveal t]
  17488. ["Show All" show-all t]
  17489. "--"
  17490. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  17491. "--"
  17492. ["New Heading" org-insert-heading t]
  17493. ("Navigate Headings"
  17494. ["Up" outline-up-heading t]
  17495. ["Next" outline-next-visible-heading t]
  17496. ["Previous" outline-previous-visible-heading t]
  17497. ["Next Same Level" outline-forward-same-level t]
  17498. ["Previous Same Level" outline-backward-same-level t]
  17499. "--"
  17500. ["Jump" org-goto t])
  17501. ("Edit Structure"
  17502. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  17503. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  17504. "--"
  17505. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  17506. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  17507. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  17508. "--"
  17509. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  17510. "--"
  17511. ["Copy visible text" org-copy-visible t]
  17512. "--"
  17513. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  17514. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  17515. ["Demote Heading" org-metaright (not (org-at-table-p))]
  17516. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  17517. "--"
  17518. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  17519. "--"
  17520. ["Convert to odd levels" org-convert-to-odd-levels t]
  17521. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  17522. ("Editing"
  17523. ["Emphasis..." org-emphasize t]
  17524. ["Edit Source Example" org-edit-special t]
  17525. "--"
  17526. ["Footnote new/jump" org-footnote-action t]
  17527. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  17528. ("Archive"
  17529. ["Archive (default method)" org-archive-subtree-default t]
  17530. "--"
  17531. ["Move Subtree to Archive file" org-advertized-archive-subtree t]
  17532. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  17533. ["Move subtree to Archive sibling" org-archive-to-archive-sibling t]
  17534. )
  17535. "--"
  17536. ("Hyperlinks"
  17537. ["Store Link (Global)" org-store-link t]
  17538. ["Find existing link to here" org-occur-link-in-agenda-files t]
  17539. ["Insert Link" org-insert-link t]
  17540. ["Follow Link" org-open-at-point t]
  17541. "--"
  17542. ["Next link" org-next-link t]
  17543. ["Previous link" org-previous-link t]
  17544. "--"
  17545. ["Descriptive Links"
  17546. org-toggle-link-display
  17547. :style radio
  17548. :selected org-descriptive-links
  17549. ]
  17550. ["Literal Links"
  17551. org-toggle-link-display
  17552. :style radio
  17553. :selected (not org-descriptive-links)])
  17554. "--"
  17555. ("TODO Lists"
  17556. ["TODO/DONE/-" org-todo t]
  17557. ("Select keyword"
  17558. ["Next keyword" org-shiftright (org-at-heading-p)]
  17559. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  17560. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  17561. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  17562. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  17563. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  17564. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  17565. "--"
  17566. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  17567. :selected org-enforce-todo-dependencies :style toggle :active t]
  17568. "Settings for tree at point"
  17569. ["Do Children sequentially" org-toggle-ordered-property :style radio
  17570. :selected (org-entry-get nil "ORDERED")
  17571. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  17572. ["Do Children parallel" org-toggle-ordered-property :style radio
  17573. :selected (not (org-entry-get nil "ORDERED"))
  17574. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  17575. "--"
  17576. ["Set Priority" org-priority t]
  17577. ["Priority Up" org-shiftup t]
  17578. ["Priority Down" org-shiftdown t]
  17579. "--"
  17580. ["Get news from all feeds" org-feed-update-all t]
  17581. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  17582. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  17583. ("TAGS and Properties"
  17584. ["Set Tags" org-set-tags-command t]
  17585. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  17586. "--"
  17587. ["Set property" org-set-property t]
  17588. ["Column view of properties" org-columns t]
  17589. ["Insert Column View DBlock" org-insert-columns-dblock t])
  17590. ("Dates and Scheduling"
  17591. ["Timestamp" org-time-stamp t]
  17592. ["Timestamp (inactive)" org-time-stamp-inactive t]
  17593. ("Change Date"
  17594. ["1 Day Later" org-shiftright t]
  17595. ["1 Day Earlier" org-shiftleft t]
  17596. ["1 ... Later" org-shiftup t]
  17597. ["1 ... Earlier" org-shiftdown t])
  17598. ["Compute Time Range" org-evaluate-time-range t]
  17599. ["Schedule Item" org-schedule t]
  17600. ["Deadline" org-deadline t]
  17601. "--"
  17602. ["Custom time format" org-toggle-time-stamp-overlays
  17603. :style radio :selected org-display-custom-times]
  17604. "--"
  17605. ["Goto Calendar" org-goto-calendar t]
  17606. ["Date from Calendar" org-date-from-calendar t]
  17607. "--"
  17608. ["Start/Restart Timer" org-timer-start t]
  17609. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  17610. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  17611. ["Insert Timer String" org-timer t]
  17612. ["Insert Timer Item" org-timer-item t])
  17613. ("Logging work"
  17614. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  17615. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  17616. ["Clock out" org-clock-out t]
  17617. ["Clock cancel" org-clock-cancel t]
  17618. "--"
  17619. ["Mark as default task" org-clock-mark-default-task t]
  17620. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  17621. ["Goto running clock" org-clock-goto t]
  17622. "--"
  17623. ["Display times" org-clock-display t]
  17624. ["Create clock table" org-clock-report t]
  17625. "--"
  17626. ["Record DONE time"
  17627. (progn (setq org-log-done (not org-log-done))
  17628. (message "Switching to %s will %s record a timestamp"
  17629. (car org-done-keywords)
  17630. (if org-log-done "automatically" "not")))
  17631. :style toggle :selected org-log-done])
  17632. "--"
  17633. ["Agenda Command..." org-agenda t]
  17634. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  17635. ("File List for Agenda")
  17636. ("Special views current file"
  17637. ["TODO Tree" org-show-todo-tree t]
  17638. ["Check Deadlines" org-check-deadlines t]
  17639. ["Timeline" org-timeline t]
  17640. ["Tags/Property tree" org-match-sparse-tree t])
  17641. "--"
  17642. ["Export/Publish..." org-export t]
  17643. ("LaTeX"
  17644. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  17645. :selected org-cdlatex-mode]
  17646. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  17647. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  17648. ["Modify math symbol" org-cdlatex-math-modify
  17649. (org-inside-LaTeX-fragment-p)]
  17650. ["Insert citation" org-reftex-citation t]
  17651. "--"
  17652. ["Template for BEAMER" (progn (require 'org-beamer)
  17653. (org-insert-beamer-options-template)) t])
  17654. "--"
  17655. ("MobileOrg"
  17656. ["Push Files and Views" org-mobile-push t]
  17657. ["Get Captured and Flagged" org-mobile-pull t]
  17658. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  17659. "--"
  17660. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  17661. "--"
  17662. ("Documentation"
  17663. ["Show Version" org-version t]
  17664. ["Info Documentation" org-info t])
  17665. ("Customize"
  17666. ["Browse Org Group" org-customize t]
  17667. "--"
  17668. ["Expand This Menu" org-create-customize-menu
  17669. (fboundp 'customize-menu-create)])
  17670. ["Send bug report" org-submit-bug-report t]
  17671. "--"
  17672. ("Refresh/Reload"
  17673. ["Refresh setup current buffer" org-mode-restart t]
  17674. ["Reload Org (after update)" org-reload t]
  17675. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  17676. ))
  17677. (defun org-info (&optional node)
  17678. "Read documentation for Org-mode in the info system.
  17679. With optional NODE, go directly to that node."
  17680. (interactive)
  17681. (info (format "(org)%s" (or node ""))))
  17682. ;;;###autoload
  17683. (defun org-submit-bug-report ()
  17684. "Submit a bug report on Org-mode via mail.
  17685. Don't hesitate to report any problems or inaccurate documentation.
  17686. If you don't have setup sending mail from (X)Emacs, please copy the
  17687. output buffer into your mail program, as it gives us important
  17688. information about your Org-mode version and configuration."
  17689. (interactive)
  17690. (require 'reporter)
  17691. (org-load-modules-maybe)
  17692. (org-require-autoloaded-modules)
  17693. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  17694. (reporter-submit-bug-report
  17695. "emacs-orgmode@gnu.org"
  17696. (org-version)
  17697. (let (list)
  17698. (save-window-excursion
  17699. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  17700. (delete-other-windows)
  17701. (erase-buffer)
  17702. (insert "You are about to submit a bug report to the Org-mode mailing list.
  17703. We would like to add your full Org-mode and Outline configuration to the
  17704. bug report. This greatly simplifies the work of the maintainer and
  17705. other experts on the mailing list.
  17706. HOWEVER, some variables you have customized may contain private
  17707. information. The names of customers, colleagues, or friends, might
  17708. appear in the form of file names, tags, todo states, or search strings.
  17709. If you answer yes to the prompt, you might want to check and remove
  17710. such private information before sending the email.")
  17711. (add-text-properties (point-min) (point-max) '(face org-warning))
  17712. (when (yes-or-no-p "Include your Org-mode configuration ")
  17713. (mapatoms
  17714. (lambda (v)
  17715. (and (boundp v)
  17716. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  17717. (or (and (symbol-value v)
  17718. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  17719. (and
  17720. (get v 'custom-type) (get v 'standard-value)
  17721. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  17722. (push v list)))))
  17723. (kill-buffer (get-buffer "*Warn about privacy*"))
  17724. list))
  17725. nil nil
  17726. "Remember to cover the basics, that is, what you expected to happen and
  17727. what in fact did happen. You don't know how to make a good report? See
  17728. http://orgmode.org/manual/Feedback.html#Feedback
  17729. Your bug report will be posted to the Org-mode mailing list.
  17730. ------------------------------------------------------------------------")
  17731. (save-excursion
  17732. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  17733. (replace-match "\\1Bug: \\3 [\\2]")))))
  17734. (defun org-install-agenda-files-menu ()
  17735. (let ((bl (buffer-list)))
  17736. (save-excursion
  17737. (while bl
  17738. (set-buffer (pop bl))
  17739. (if (eq major-mode 'org-mode) (setq bl nil)))
  17740. (when (eq major-mode 'org-mode)
  17741. (easy-menu-change
  17742. '("Org") "File List for Agenda"
  17743. (append
  17744. (list
  17745. ["Edit File List" (org-edit-agenda-file-list) t]
  17746. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  17747. ["Remove Current File from List" org-remove-file t]
  17748. ["Cycle through agenda files" org-cycle-agenda-files t]
  17749. ["Occur in all agenda files" org-occur-in-agenda-files t]
  17750. "--")
  17751. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  17752. ;;;; Documentation
  17753. ;;;###autoload
  17754. (defun org-require-autoloaded-modules ()
  17755. (interactive)
  17756. (mapc 'require
  17757. '(org-agenda org-archive org-ascii org-attach org-clock org-colview
  17758. org-docbook org-exp org-html org-icalendar
  17759. org-id org-latex
  17760. org-publish org-remember org-table
  17761. org-timer org-xoxo)))
  17762. ;;;###autoload
  17763. (defun org-reload (&optional uncompiled)
  17764. "Reload all org lisp files.
  17765. With prefix arg UNCOMPILED, load the uncompiled versions."
  17766. (interactive "P")
  17767. (require 'find-func)
  17768. (let* ((file-re "^\\(org\\|orgtbl\\)\\(\\.el\\|-.*\\.el\\)")
  17769. (dir-org (file-name-directory (org-find-library-name "org")))
  17770. (dir-org-contrib (ignore-errors
  17771. (file-name-directory
  17772. (org-find-library-name "org-contribdir"))))
  17773. (babel-files
  17774. (mapcar (lambda (el) (concat "ob" (when el (format "-%s" el)) ".el"))
  17775. (append (list nil "comint" "eval" "exp" "keys"
  17776. "lob" "ref" "table" "tangle")
  17777. (delq nil
  17778. (mapcar
  17779. (lambda (lang)
  17780. (when (cdr lang) (symbol-name (car lang))))
  17781. org-babel-load-languages)))))
  17782. (files
  17783. (append (directory-files dir-org t file-re)
  17784. babel-files
  17785. (and dir-org-contrib
  17786. (directory-files dir-org-contrib t file-re))))
  17787. (remove-re (concat (if (featurep 'xemacs)
  17788. "org-colview" "org-colview-xemacs")
  17789. "\\'")))
  17790. (setq files (mapcar 'file-name-sans-extension files))
  17791. (setq files (mapcar
  17792. (lambda (x) (if (string-match remove-re x) nil x))
  17793. files))
  17794. (setq files (delq nil files))
  17795. (mapc
  17796. (lambda (f)
  17797. (when (featurep (intern (file-name-nondirectory f)))
  17798. (if (and (not uncompiled)
  17799. (file-exists-p (concat f ".elc")))
  17800. (load (concat f ".elc") nil nil t)
  17801. (load (concat f ".el") nil nil t))))
  17802. files))
  17803. (org-version))
  17804. ;;;###autoload
  17805. (defun org-customize ()
  17806. "Call the customize function with org as argument."
  17807. (interactive)
  17808. (org-load-modules-maybe)
  17809. (org-require-autoloaded-modules)
  17810. (customize-browse 'org))
  17811. (defun org-create-customize-menu ()
  17812. "Create a full customization menu for Org-mode, insert it into the menu."
  17813. (interactive)
  17814. (org-load-modules-maybe)
  17815. (org-require-autoloaded-modules)
  17816. (if (fboundp 'customize-menu-create)
  17817. (progn
  17818. (easy-menu-change
  17819. '("Org") "Customize"
  17820. `(["Browse Org group" org-customize t]
  17821. "--"
  17822. ,(customize-menu-create 'org)
  17823. ["Set" Custom-set t]
  17824. ["Save" Custom-save t]
  17825. ["Reset to Current" Custom-reset-current t]
  17826. ["Reset to Saved" Custom-reset-saved t]
  17827. ["Reset to Standard Settings" Custom-reset-standard t]))
  17828. (message "\"Org\"-menu now contains full customization menu"))
  17829. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  17830. ;;;; Miscellaneous stuff
  17831. ;;; Generally useful functions
  17832. (defun org-get-at-bol (property)
  17833. "Get text property PROPERTY at beginning of line."
  17834. (get-text-property (point-at-bol) property))
  17835. (defun org-find-text-property-in-string (prop s)
  17836. "Return the first non-nil value of property PROP in string S."
  17837. (or (get-text-property 0 prop s)
  17838. (get-text-property (or (next-single-property-change 0 prop s) 0)
  17839. prop s)))
  17840. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  17841. "Display the given MESSAGE as a warning."
  17842. (if (fboundp 'display-warning)
  17843. (display-warning 'org message
  17844. (if (featurep 'xemacs) 'warning :warning))
  17845. (let ((buf (get-buffer-create "*Org warnings*")))
  17846. (with-current-buffer buf
  17847. (goto-char (point-max))
  17848. (insert "Warning (Org): " message)
  17849. (unless (bolp)
  17850. (newline)))
  17851. (display-buffer buf)
  17852. (sit-for 0))))
  17853. (defun org-eval (form)
  17854. "Eval FORM and return result."
  17855. (condition-case error
  17856. (eval form)
  17857. (error (format "%%![Error: %s]" error))))
  17858. (defun org-in-clocktable-p ()
  17859. "Check if the cursor is in a clocktable."
  17860. (let ((pos (point)) start)
  17861. (save-excursion
  17862. (end-of-line 1)
  17863. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  17864. (setq start (match-beginning 0))
  17865. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  17866. (>= (match-end 0) pos)
  17867. start))))
  17868. (defun org-in-commented-line ()
  17869. "Is point in a line starting with `#'?"
  17870. (equal (char-after (point-at-bol)) ?#))
  17871. (defun org-in-indented-comment-line ()
  17872. "Is point in a line starting with `#' after some white space?"
  17873. (save-excursion
  17874. (save-match-data
  17875. (goto-char (point-at-bol))
  17876. (looking-at "[ \t]*#"))))
  17877. (defun org-in-verbatim-emphasis ()
  17878. (save-match-data
  17879. (and (org-in-regexp org-emph-re 2) (member (match-string 3) '("=" "~")))))
  17880. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  17881. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  17882. (if (and marker (marker-buffer marker)
  17883. (buffer-live-p (marker-buffer marker)))
  17884. (progn
  17885. (org-pop-to-buffer-same-window (marker-buffer marker))
  17886. (if (or (> marker (point-max)) (< marker (point-min)))
  17887. (widen))
  17888. (goto-char marker)
  17889. (org-show-context 'org-goto))
  17890. (if bookmark
  17891. (bookmark-jump bookmark)
  17892. (error "Cannot find location"))))
  17893. (defun org-quote-csv-field (s)
  17894. "Quote field for inclusion in CSV material."
  17895. (if (string-match "[\",]" s)
  17896. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  17897. s))
  17898. (defun org-force-self-insert (N)
  17899. "Needed to enforce self-insert under remapping."
  17900. (interactive "p")
  17901. (self-insert-command N))
  17902. (defun org-string-width (s)
  17903. "Compute width of string, ignoring invisible characters.
  17904. This ignores character with invisibility property `org-link', and also
  17905. characters with property `org-cwidth', because these will become invisible
  17906. upon the next fontification round."
  17907. (let (b l)
  17908. (when (or (eq t buffer-invisibility-spec)
  17909. (assq 'org-link buffer-invisibility-spec))
  17910. (while (setq b (text-property-any 0 (length s)
  17911. 'invisible 'org-link s))
  17912. (setq s (concat (substring s 0 b)
  17913. (substring s (or (next-single-property-change
  17914. b 'invisible s) (length s)))))))
  17915. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  17916. (setq s (concat (substring s 0 b)
  17917. (substring s (or (next-single-property-change
  17918. b 'org-cwidth s) (length s))))))
  17919. (setq l (string-width s) b -1)
  17920. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  17921. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  17922. l))
  17923. (defun org-shorten-string (s maxlength)
  17924. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  17925. If the string is shorter or has length MAXLENGTH, just return the
  17926. original string. If it is longer, the functions finds a space in the
  17927. string, breaks this string off at that locations and adds three dots
  17928. as ellipsis. Including the ellipsis, the string will not be longer
  17929. than MAXLENGTH. If finding a good breaking point in the string does
  17930. not work, the string is just chopped off in the middle of a word
  17931. if necessary."
  17932. (if (<= (length s) maxlength)
  17933. s
  17934. (let* ((n (max (- maxlength 4) 1))
  17935. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  17936. (if (string-match re s)
  17937. (concat (match-string 1 s) "...")
  17938. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  17939. (defun org-get-indentation (&optional line)
  17940. "Get the indentation of the current line, interpreting tabs.
  17941. When LINE is given, assume it represents a line and compute its indentation."
  17942. (if line
  17943. (if (string-match "^ *" (org-remove-tabs line))
  17944. (match-end 0))
  17945. (save-excursion
  17946. (beginning-of-line 1)
  17947. (skip-chars-forward " \t")
  17948. (current-column))))
  17949. (defun org-get-string-indentation (s)
  17950. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  17951. (let ((n -1) (i 0) (w tab-width) c)
  17952. (catch 'exit
  17953. (while (< (setq n (1+ n)) (length s))
  17954. (setq c (aref s n))
  17955. (cond ((= c ?\ ) (setq i (1+ i)))
  17956. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  17957. (t (throw 'exit t)))))
  17958. i))
  17959. (defun org-remove-tabs (s &optional width)
  17960. "Replace tabulators in S with spaces.
  17961. Assumes that s is a single line, starting in column 0."
  17962. (setq width (or width tab-width))
  17963. (while (string-match "\t" s)
  17964. (setq s (replace-match
  17965. (make-string
  17966. (- (* width (/ (+ (match-beginning 0) width) width))
  17967. (match-beginning 0)) ?\ )
  17968. t t s)))
  17969. s)
  17970. (defun org-fix-indentation (line ind)
  17971. "Fix indentation in LINE.
  17972. IND is a cons cell with target and minimum indentation.
  17973. If the current indentation in LINE is smaller than the minimum,
  17974. leave it alone. If it is larger than ind, set it to the target."
  17975. (let* ((l (org-remove-tabs line))
  17976. (i (org-get-indentation l))
  17977. (i1 (car ind)) (i2 (cdr ind)))
  17978. (if (>= i i2) (setq l (substring line i2)))
  17979. (if (> i1 0)
  17980. (concat (make-string i1 ?\ ) l)
  17981. l)))
  17982. (defun org-remove-indentation (code &optional n)
  17983. "Remove the maximum common indentation from the lines in CODE.
  17984. N may optionally be the number of spaces to remove."
  17985. (with-temp-buffer
  17986. (insert code)
  17987. (org-do-remove-indentation n)
  17988. (buffer-string)))
  17989. (defun org-do-remove-indentation (&optional n)
  17990. "Remove the maximum common indentation from the buffer."
  17991. (untabify (point-min) (point-max))
  17992. (let ((min 10000) re)
  17993. (if n
  17994. (setq min n)
  17995. (goto-char (point-min))
  17996. (while (re-search-forward "^ *[^ \n]" nil t)
  17997. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  17998. (unless (or (= min 0) (= min 10000))
  17999. (setq re (format "^ \\{%d\\}" min))
  18000. (goto-char (point-min))
  18001. (while (re-search-forward re nil t)
  18002. (replace-match "")
  18003. (end-of-line 1))
  18004. min)))
  18005. (defun org-fill-template (template alist)
  18006. "Find each %key of ALIST in TEMPLATE and replace it."
  18007. (let ((case-fold-search nil)
  18008. entry key value)
  18009. (setq alist (sort (copy-sequence alist)
  18010. (lambda (a b) (< (length (car a)) (length (car b))))))
  18011. (while (setq entry (pop alist))
  18012. (setq template
  18013. (replace-regexp-in-string
  18014. (concat "%" (regexp-quote (car entry)))
  18015. (cdr entry) template t t)))
  18016. template))
  18017. (defun org-base-buffer (buffer)
  18018. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  18019. (if (not buffer)
  18020. buffer
  18021. (or (buffer-base-buffer buffer)
  18022. buffer)))
  18023. (defun org-trim (s)
  18024. "Remove whitespace at beginning and end of string."
  18025. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  18026. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  18027. s)
  18028. (defun org-wrap (string &optional width lines)
  18029. "Wrap string to either a number of lines, or a width in characters.
  18030. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  18031. that costs. If there is a word longer than WIDTH, the text is actually
  18032. wrapped to the length of that word.
  18033. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  18034. many lines, whatever width that takes.
  18035. The return value is a list of lines, without newlines at the end."
  18036. (let* ((words (org-split-string string "[ \t\n]+"))
  18037. (maxword (apply 'max (mapcar 'org-string-width words)))
  18038. w ll)
  18039. (cond (width
  18040. (org-do-wrap words (max maxword width)))
  18041. (lines
  18042. (setq w maxword)
  18043. (setq ll (org-do-wrap words maxword))
  18044. (if (<= (length ll) lines)
  18045. ll
  18046. (setq ll words)
  18047. (while (> (length ll) lines)
  18048. (setq w (1+ w))
  18049. (setq ll (org-do-wrap words w)))
  18050. ll))
  18051. (t (error "Cannot wrap this")))))
  18052. (defun org-do-wrap (words width)
  18053. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  18054. (let (lines line)
  18055. (while words
  18056. (setq line (pop words))
  18057. (while (and words (< (+ (length line) (length (car words))) width))
  18058. (setq line (concat line " " (pop words))))
  18059. (setq lines (push line lines)))
  18060. (nreverse lines)))
  18061. (defun org-split-string (string &optional separators)
  18062. "Splits STRING into substrings at SEPARATORS.
  18063. No empty strings are returned if there are matches at the beginning
  18064. and end of string."
  18065. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  18066. (start 0)
  18067. notfirst
  18068. (list nil))
  18069. (while (and (string-match rexp string
  18070. (if (and notfirst
  18071. (= start (match-beginning 0))
  18072. (< start (length string)))
  18073. (1+ start) start))
  18074. (< (match-beginning 0) (length string)))
  18075. (setq notfirst t)
  18076. (or (eq (match-beginning 0) 0)
  18077. (and (eq (match-beginning 0) (match-end 0))
  18078. (eq (match-beginning 0) start))
  18079. (setq list
  18080. (cons (substring string start (match-beginning 0))
  18081. list)))
  18082. (setq start (match-end 0)))
  18083. (or (eq start (length string))
  18084. (setq list
  18085. (cons (substring string start)
  18086. list)))
  18087. (nreverse list)))
  18088. (defun org-quote-vert (s)
  18089. "Replace \"|\" with \"\\vert\"."
  18090. (while (string-match "|" s)
  18091. (setq s (replace-match "\\vert" t t s)))
  18092. s)
  18093. (defun org-uuidgen-p (s)
  18094. "Is S an ID created by UUIDGEN?"
  18095. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  18096. (defun org-in-src-block-p nil
  18097. "Whether point is in a code source block."
  18098. (let (ov)
  18099. (when (setq ov (overlays-at (point)))
  18100. (memq 'org-block-background
  18101. (overlay-properties
  18102. (car ov))))))
  18103. (defun org-context ()
  18104. "Return a list of contexts of the current cursor position.
  18105. If several contexts apply, all are returned.
  18106. Each context entry is a list with a symbol naming the context, and
  18107. two positions indicating start and end of the context. Possible
  18108. contexts are:
  18109. :headline anywhere in a headline
  18110. :headline-stars on the leading stars in a headline
  18111. :todo-keyword on a TODO keyword (including DONE) in a headline
  18112. :tags on the TAGS in a headline
  18113. :priority on the priority cookie in a headline
  18114. :item on the first line of a plain list item
  18115. :item-bullet on the bullet/number of a plain list item
  18116. :checkbox on the checkbox in a plain list item
  18117. :table in an org-mode table
  18118. :table-special on a special filed in a table
  18119. :table-table in a table.el table
  18120. :clocktable in a clocktable
  18121. :src-block in a source block
  18122. :link on a hyperlink
  18123. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT, QUOTE.
  18124. :target on a <<target>>
  18125. :radio-target on a <<<radio-target>>>
  18126. :latex-fragment on a LaTeX fragment
  18127. :latex-preview on a LaTeX fragment with overlaid preview image
  18128. This function expects the position to be visible because it uses font-lock
  18129. faces as a help to recognize the following contexts: :table-special, :link,
  18130. and :keyword."
  18131. (let* ((f (get-text-property (point) 'face))
  18132. (faces (if (listp f) f (list f)))
  18133. (case-fold-search t)
  18134. (p (point)) clist o)
  18135. ;; First the large context
  18136. (cond
  18137. ((org-at-heading-p t)
  18138. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  18139. (when (progn
  18140. (beginning-of-line 1)
  18141. (looking-at org-todo-line-tags-regexp))
  18142. (push (org-point-in-group p 1 :headline-stars) clist)
  18143. (push (org-point-in-group p 2 :todo-keyword) clist)
  18144. (push (org-point-in-group p 4 :tags) clist))
  18145. (goto-char p)
  18146. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  18147. (if (looking-at "\\[#[A-Z0-9]\\]")
  18148. (push (org-point-in-group p 0 :priority) clist)))
  18149. ((org-at-item-p)
  18150. (push (org-point-in-group p 2 :item-bullet) clist)
  18151. (push (list :item (point-at-bol)
  18152. (save-excursion (org-end-of-item) (point)))
  18153. clist)
  18154. (and (org-at-item-checkbox-p)
  18155. (push (org-point-in-group p 0 :checkbox) clist)))
  18156. ((org-at-table-p)
  18157. (push (list :table (org-table-begin) (org-table-end)) clist)
  18158. (if (memq 'org-formula faces)
  18159. (push (list :table-special
  18160. (previous-single-property-change p 'face)
  18161. (next-single-property-change p 'face)) clist)))
  18162. ((org-at-table-p 'any)
  18163. (push (list :table-table) clist)))
  18164. (goto-char p)
  18165. ;; New the "medium" contexts: clocktables, source blocks
  18166. (cond ((org-in-clocktable-p)
  18167. (push (list :clocktable
  18168. (and (or (looking-at "#\\+BEGIN: clocktable")
  18169. (search-backward "#+BEGIN: clocktable" nil t))
  18170. (match-beginning 0))
  18171. (and (re-search-forward "#\\+END:?" nil t)
  18172. (match-end 0))) clist))
  18173. ((org-in-src-block-p)
  18174. (push (list :src-block
  18175. (and (or (looking-at "#\\+BEGIN_SRC")
  18176. (search-backward "#+BEGIN_SRC" nil t))
  18177. (match-beginning 0))
  18178. (and (search-forward "#+END_SRC" nil t)
  18179. (match-beginning 0))) clist)))
  18180. (goto-char p)
  18181. ;; Now the small context
  18182. (cond
  18183. ((org-at-timestamp-p)
  18184. (push (org-point-in-group p 0 :timestamp) clist))
  18185. ((memq 'org-link faces)
  18186. (push (list :link
  18187. (previous-single-property-change p 'face)
  18188. (next-single-property-change p 'face)) clist))
  18189. ((memq 'org-special-keyword faces)
  18190. (push (list :keyword
  18191. (previous-single-property-change p 'face)
  18192. (next-single-property-change p 'face)) clist))
  18193. ((org-at-target-p)
  18194. (push (org-point-in-group p 0 :target) clist)
  18195. (goto-char (1- (match-beginning 0)))
  18196. (if (looking-at org-radio-target-regexp)
  18197. (push (org-point-in-group p 0 :radio-target) clist))
  18198. (goto-char p))
  18199. ((setq o (car (delq nil
  18200. (mapcar
  18201. (lambda (x)
  18202. (if (memq x org-latex-fragment-image-overlays) x))
  18203. (overlays-at (point))))))
  18204. (push (list :latex-fragment
  18205. (overlay-start o) (overlay-end o)) clist)
  18206. (push (list :latex-preview
  18207. (overlay-start o) (overlay-end o)) clist))
  18208. ((org-inside-LaTeX-fragment-p)
  18209. ;; FIXME: positions wrong.
  18210. (push (list :latex-fragment (point) (point)) clist)))
  18211. (setq clist (nreverse (delq nil clist)))
  18212. clist))
  18213. ;; FIXME: Compare with at-regexp-p Do we need both?
  18214. (defun org-in-regexp (re &optional nlines visually)
  18215. "Check if point is inside a match of regexp.
  18216. Normally only the current line is checked, but you can include NLINES extra
  18217. lines both before and after point into the search.
  18218. If VISUALLY is set, require that the cursor is not after the match but
  18219. really on, so that the block visually is on the match."
  18220. (catch 'exit
  18221. (let ((pos (point))
  18222. (eol (point-at-eol (+ 1 (or nlines 0))))
  18223. (inc (if visually 1 0)))
  18224. (save-excursion
  18225. (beginning-of-line (- 1 (or nlines 0)))
  18226. (while (re-search-forward re eol t)
  18227. (if (and (<= (match-beginning 0) pos)
  18228. (>= (+ inc (match-end 0)) pos))
  18229. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  18230. (defun org-at-regexp-p (regexp)
  18231. "Is point inside a match of REGEXP in the current line?"
  18232. (catch 'exit
  18233. (save-excursion
  18234. (let ((pos (point)) (end (point-at-eol)))
  18235. (beginning-of-line 1)
  18236. (while (re-search-forward regexp end t)
  18237. (if (and (<= (match-beginning 0) pos)
  18238. (>= (match-end 0) pos))
  18239. (throw 'exit t)))
  18240. nil))))
  18241. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  18242. "Non-nil when point is between matches of START-RE and END-RE.
  18243. Also return a non-nil value when point is on one of the matches.
  18244. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  18245. buffer positions. Default values are the positions of headlines
  18246. surrounding the point.
  18247. The functions returns a cons cell whose car (resp. cdr) is the
  18248. position before START-RE (resp. after END-RE)."
  18249. (save-match-data
  18250. (let ((pos (point))
  18251. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  18252. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  18253. beg end)
  18254. (save-excursion
  18255. ;; Point is on a block when on START-RE or if START-RE can be
  18256. ;; found before it...
  18257. (and (or (org-at-regexp-p start-re)
  18258. (re-search-backward start-re limit-up t))
  18259. (setq beg (match-beginning 0))
  18260. ;; ... and END-RE after it...
  18261. (goto-char (match-end 0))
  18262. (re-search-forward end-re limit-down t)
  18263. (> (setq end (match-end 0)) pos)
  18264. ;; ... without another START-RE in-between.
  18265. (goto-char (match-beginning 0))
  18266. (not (re-search-backward start-re (1+ beg) t))
  18267. ;; Return value.
  18268. (cons beg end))))))
  18269. (defun org-in-block-p (names)
  18270. "Non-nil when point belongs to a block whose name belongs to NAMES.
  18271. NAMES is a list of strings containing names of blocks.
  18272. Return first block name matched, or nil. Beware that in case of
  18273. nested blocks, the returned name may not belong to the closest
  18274. block from point."
  18275. (save-match-data
  18276. (catch 'exit
  18277. (let ((case-fold-search t)
  18278. (lim-up (save-excursion (outline-previous-heading)))
  18279. (lim-down (save-excursion (outline-next-heading))))
  18280. (mapc (lambda (name)
  18281. (let ((n (regexp-quote name)))
  18282. (when (org-between-regexps-p
  18283. (concat "^[ \t]*#\\+begin_" n)
  18284. (concat "^[ \t]*#\\+end_" n)
  18285. lim-up lim-down)
  18286. (throw 'exit n))))
  18287. names))
  18288. nil)))
  18289. (defun org-occur-in-agenda-files (regexp &optional nlines)
  18290. "Call `multi-occur' with buffers for all agenda files."
  18291. (interactive "sOrg-files matching: \np")
  18292. (let* ((files (org-agenda-files))
  18293. (tnames (mapcar 'file-truename files))
  18294. (extra org-agenda-text-search-extra-files)
  18295. f)
  18296. (when (eq (car extra) 'agenda-archives)
  18297. (setq extra (cdr extra))
  18298. (setq files (org-add-archive-files files)))
  18299. (while (setq f (pop extra))
  18300. (unless (member (file-truename f) tnames)
  18301. (add-to-list 'files f 'append)
  18302. (add-to-list 'tnames (file-truename f) 'append)))
  18303. (multi-occur
  18304. (mapcar (lambda (x)
  18305. (with-current-buffer
  18306. (or (get-file-buffer x) (find-file-noselect x))
  18307. (widen)
  18308. (current-buffer)))
  18309. files)
  18310. regexp)))
  18311. (if (boundp 'occur-mode-find-occurrence-hook)
  18312. ;; Emacs 23
  18313. (add-hook 'occur-mode-find-occurrence-hook
  18314. (lambda ()
  18315. (when (eq major-mode 'org-mode)
  18316. (org-reveal))))
  18317. ;; Emacs 22
  18318. (defadvice occur-mode-goto-occurrence
  18319. (after org-occur-reveal activate)
  18320. (and (eq major-mode 'org-mode) (org-reveal)))
  18321. (defadvice occur-mode-goto-occurrence-other-window
  18322. (after org-occur-reveal activate)
  18323. (and (eq major-mode 'org-mode) (org-reveal)))
  18324. (defadvice occur-mode-display-occurrence
  18325. (after org-occur-reveal activate)
  18326. (when (eq major-mode 'org-mode)
  18327. (let ((pos (occur-mode-find-occurrence)))
  18328. (with-current-buffer (marker-buffer pos)
  18329. (save-excursion
  18330. (goto-char pos)
  18331. (org-reveal)))))))
  18332. (defun org-occur-link-in-agenda-files ()
  18333. "Create a link and search for it in the agendas.
  18334. The link is not stored in `org-stored-links', it is just created
  18335. for the search purpose."
  18336. (interactive)
  18337. (let ((link (condition-case nil
  18338. (org-store-link nil)
  18339. (error "Unable to create a link to here"))))
  18340. (org-occur-in-agenda-files (regexp-quote link))))
  18341. (defun org-uniquify (list)
  18342. "Remove duplicate elements from LIST."
  18343. (let (res)
  18344. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  18345. res))
  18346. (defun org-delete-all (elts list)
  18347. "Remove all elements in ELTS from LIST."
  18348. (while elts
  18349. (setq list (delete (pop elts) list)))
  18350. list)
  18351. (defun org-count (cl-item cl-seq)
  18352. "Count the number of occurrences of ITEM in SEQ.
  18353. Taken from `count' in cl-seq.el with all keyword arguments removed."
  18354. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  18355. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  18356. (while (< cl-start cl-end)
  18357. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  18358. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  18359. (setq cl-start (1+ cl-start)))
  18360. cl-count))
  18361. (defun org-remove-if (predicate seq)
  18362. "Remove everything from SEQ that fulfills PREDICATE."
  18363. (let (res e)
  18364. (while seq
  18365. (setq e (pop seq))
  18366. (if (not (funcall predicate e)) (push e res)))
  18367. (nreverse res)))
  18368. (defun org-remove-if-not (predicate seq)
  18369. "Remove everything from SEQ that does not fulfill PREDICATE."
  18370. (let (res e)
  18371. (while seq
  18372. (setq e (pop seq))
  18373. (if (funcall predicate e) (push e res)))
  18374. (nreverse res)))
  18375. (defun org-reduce (cl-func cl-seq &rest cl-keys)
  18376. "Reduce two-argument FUNCTION across SEQ.
  18377. Taken from `reduce' in cl-seq.el with all keyword arguments but
  18378. \":initial-value\" removed."
  18379. (let ((cl-accum (cond ((memq :initial-value cl-keys)
  18380. (cadr (memq :initial-value cl-keys)))
  18381. (cl-seq (pop cl-seq))
  18382. (t (funcall cl-func)))))
  18383. (while cl-seq
  18384. (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
  18385. cl-accum))
  18386. (defun org-back-over-empty-lines ()
  18387. "Move backwards over whitespace, to the beginning of the first empty line.
  18388. Returns the number of empty lines passed."
  18389. (let ((pos (point)))
  18390. (if (cdr (assoc 'heading org-blank-before-new-entry))
  18391. (skip-chars-backward " \t\n\r")
  18392. (unless (eobp)
  18393. (forward-line -1)))
  18394. (beginning-of-line 2)
  18395. (goto-char (min (point) pos))
  18396. (count-lines (point) pos)))
  18397. (defun org-skip-whitespace ()
  18398. (skip-chars-forward " \t\n\r"))
  18399. (defun org-point-in-group (point group &optional context)
  18400. "Check if POINT is in match-group GROUP.
  18401. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  18402. match. If the match group does not exist or point is not inside it,
  18403. return nil."
  18404. (and (match-beginning group)
  18405. (>= point (match-beginning group))
  18406. (<= point (match-end group))
  18407. (if context
  18408. (list context (match-beginning group) (match-end group))
  18409. t)))
  18410. (defun org-switch-to-buffer-other-window (&rest args)
  18411. "Switch to buffer in a second window on the current frame.
  18412. In particular, do not allow pop-up frames.
  18413. Returns the newly created buffer."
  18414. (let (pop-up-frames special-display-buffer-names special-display-regexps
  18415. special-display-function)
  18416. (apply 'switch-to-buffer-other-window args)))
  18417. (defun org-combine-plists (&rest plists)
  18418. "Create a single property list from all plists in PLISTS.
  18419. The process starts by copying the first list, and then setting properties
  18420. from the other lists. Settings in the last list are the most significant
  18421. ones and overrule settings in the other lists."
  18422. (let ((rtn (copy-sequence (pop plists)))
  18423. p v ls)
  18424. (while plists
  18425. (setq ls (pop plists))
  18426. (while ls
  18427. (setq p (pop ls) v (pop ls))
  18428. (setq rtn (plist-put rtn p v))))
  18429. rtn))
  18430. (defun org-move-line-down (arg)
  18431. "Move the current line down. With prefix argument, move it past ARG lines."
  18432. (interactive "p")
  18433. (let ((col (current-column))
  18434. beg end pos)
  18435. (beginning-of-line 1) (setq beg (point))
  18436. (beginning-of-line 2) (setq end (point))
  18437. (beginning-of-line (+ 1 arg))
  18438. (setq pos (move-marker (make-marker) (point)))
  18439. (insert (delete-and-extract-region beg end))
  18440. (goto-char pos)
  18441. (org-move-to-column col)))
  18442. (defun org-move-line-up (arg)
  18443. "Move the current line up. With prefix argument, move it past ARG lines."
  18444. (interactive "p")
  18445. (let ((col (current-column))
  18446. beg end pos)
  18447. (beginning-of-line 1) (setq beg (point))
  18448. (beginning-of-line 2) (setq end (point))
  18449. (beginning-of-line (- arg))
  18450. (setq pos (move-marker (make-marker) (point)))
  18451. (insert (delete-and-extract-region beg end))
  18452. (goto-char pos)
  18453. (org-move-to-column col)))
  18454. (defun org-replace-escapes (string table)
  18455. "Replace %-escapes in STRING with values in TABLE.
  18456. TABLE is an association list with keys like \"%a\" and string values.
  18457. The sequences in STRING may contain normal field width and padding information,
  18458. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  18459. so values can contain further %-escapes if they are define later in TABLE."
  18460. (let ((tbl (copy-alist table))
  18461. (case-fold-search nil)
  18462. (pchg 0)
  18463. e re rpl)
  18464. (while (setq e (pop tbl))
  18465. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  18466. (when (and (cdr e) (string-match re (cdr e)))
  18467. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  18468. (safe "SREF"))
  18469. (add-text-properties 0 3 (list 'sref sref) safe)
  18470. (setcdr e (replace-match safe t t (cdr e)))))
  18471. (while (string-match re string)
  18472. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  18473. (cdr e)))
  18474. (setq string (replace-match rpl t t string))))
  18475. (while (setq pchg (next-property-change pchg string))
  18476. (let ((sref (get-text-property pchg 'sref string)))
  18477. (when (and sref (string-match "SREF" string pchg))
  18478. (setq string (replace-match sref t t string)))))
  18479. string))
  18480. (defun org-sublist (list start end)
  18481. "Return a section of LIST, from START to END.
  18482. Counting starts at 1."
  18483. (let (rtn (c start))
  18484. (setq list (nthcdr (1- start) list))
  18485. (while (and list (<= c end))
  18486. (push (pop list) rtn)
  18487. (setq c (1+ c)))
  18488. (nreverse rtn)))
  18489. (defun org-find-base-buffer-visiting (file)
  18490. "Like `find-buffer-visiting' but always return the base buffer and
  18491. not an indirect buffer."
  18492. (let ((buf (or (get-file-buffer file)
  18493. (find-buffer-visiting file))))
  18494. (if buf
  18495. (or (buffer-base-buffer buf) buf)
  18496. nil)))
  18497. (defun org-image-file-name-regexp (&optional extensions)
  18498. "Return regexp matching the file names of images.
  18499. If EXTENSIONS is given, only match these."
  18500. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  18501. (image-file-name-regexp)
  18502. (let ((image-file-name-extensions
  18503. (or extensions
  18504. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  18505. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  18506. (concat "\\."
  18507. (regexp-opt (nconc (mapcar 'upcase
  18508. image-file-name-extensions)
  18509. image-file-name-extensions)
  18510. t)
  18511. "\\'"))))
  18512. (defun org-file-image-p (file &optional extensions)
  18513. "Return non-nil if FILE is an image."
  18514. (save-match-data
  18515. (string-match (org-image-file-name-regexp extensions) file)))
  18516. (defun org-get-cursor-date ()
  18517. "Return the date at cursor in as a time.
  18518. This works in the calendar and in the agenda, anywhere else it just
  18519. returns the current time."
  18520. (let (date day defd)
  18521. (cond
  18522. ((eq major-mode 'calendar-mode)
  18523. (setq date (calendar-cursor-to-date)
  18524. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  18525. ((eq major-mode 'org-agenda-mode)
  18526. (setq day (get-text-property (point) 'day))
  18527. (if day
  18528. (setq date (calendar-gregorian-from-absolute day)
  18529. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  18530. (nth 2 date))))))
  18531. (or defd (current-time))))
  18532. (defvar org-agenda-action-marker (make-marker)
  18533. "Marker pointing to the entry for the next agenda action.")
  18534. (defun org-mark-entry-for-agenda-action ()
  18535. "Mark the current entry as target of an agenda action.
  18536. Agenda actions are actions executed from the agenda with the key `k',
  18537. which make use of the date at the cursor."
  18538. (interactive)
  18539. (move-marker org-agenda-action-marker
  18540. (save-excursion (org-back-to-heading t) (point))
  18541. (current-buffer))
  18542. (message
  18543. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  18544. (defun org-mark-subtree ()
  18545. "Mark the current subtree.
  18546. This puts point at the start of the current subtree, and mark at the end.
  18547. If point is in an inline task, mark that task instead."
  18548. (interactive)
  18549. (let ((inline-task-p
  18550. (and (featurep 'org-inlinetask)
  18551. (org-inlinetask-in-task-p)))
  18552. (beg))
  18553. ;; Get beginning of subtree
  18554. (cond
  18555. (inline-task-p (org-inlinetask-goto-beginning))
  18556. ((org-at-heading-p) (beginning-of-line))
  18557. (t (org-with-limited-levels (outline-previous-visible-heading 1))))
  18558. (setq beg (point))
  18559. ;; Get end of it
  18560. (if inline-task-p
  18561. (org-inlinetask-goto-end)
  18562. (org-end-of-subtree))
  18563. ;; Mark zone
  18564. (push-mark (point) nil t)
  18565. (goto-char beg)))
  18566. ;;; Paragraph filling stuff.
  18567. ;; We want this to be just right, so use the full arsenal.
  18568. (defun org-indent-line-function ()
  18569. "Indent line depending on context."
  18570. (interactive)
  18571. (let* ((pos (point))
  18572. (itemp (org-at-item-p))
  18573. (case-fold-search t)
  18574. (org-drawer-regexp (or org-drawer-regexp "\000"))
  18575. (inline-task-p (and (featurep 'org-inlinetask)
  18576. (org-inlinetask-in-task-p)))
  18577. (inline-re (and inline-task-p
  18578. (org-inlinetask-outline-regexp)))
  18579. column)
  18580. (beginning-of-line 1)
  18581. (cond
  18582. ;; Comments
  18583. ((looking-at "# ") (setq column 0))
  18584. ;; Headings
  18585. ((looking-at org-outline-regexp) (setq column 0))
  18586. ;; Included files
  18587. ((looking-at "#\\+include:") (setq column 0))
  18588. ;; Footnote definition
  18589. ((looking-at org-footnote-definition-re) (setq column 0))
  18590. ;; Literal examples
  18591. ((looking-at "[ \t]*:\\( \\|$\\)")
  18592. (setq column (org-get-indentation))) ; do nothing
  18593. ;; Lists
  18594. ((ignore-errors (goto-char (org-in-item-p)))
  18595. (setq column (if itemp
  18596. (org-get-indentation)
  18597. (org-list-item-body-column (point))))
  18598. (goto-char pos))
  18599. ;; Drawers
  18600. ((and (looking-at "[ \t]*:END:")
  18601. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  18602. (save-excursion
  18603. (goto-char (1- (match-beginning 1)))
  18604. (setq column (current-column))))
  18605. ;; Special blocks
  18606. ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
  18607. (save-excursion
  18608. (re-search-backward
  18609. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  18610. (setq column (org-get-indentation (match-string 0))))
  18611. ((and (not (looking-at "[ \t]*#\\+begin_"))
  18612. (org-between-regexps-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
  18613. (save-excursion
  18614. (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
  18615. (setq column
  18616. (cond ((equal (downcase (match-string 1)) "src")
  18617. ;; src blocks: let `org-edit-src-exit' handle them
  18618. (org-get-indentation))
  18619. ((equal (downcase (match-string 1)) "example")
  18620. (max (org-get-indentation)
  18621. (org-get-indentation (match-string 0))))
  18622. (t
  18623. (org-get-indentation (match-string 0))))))
  18624. ;; This line has nothing special, look at the previous relevant
  18625. ;; line to compute indentation
  18626. (t
  18627. (beginning-of-line 0)
  18628. (while (and (not (bobp))
  18629. (not (looking-at org-drawer-regexp))
  18630. ;; When point started in an inline task, do not move
  18631. ;; above task starting line.
  18632. (not (and inline-task-p (looking-at inline-re)))
  18633. ;; Skip drawers, blocks, empty lines, verbatim,
  18634. ;; comments, tables, footnotes definitions, lists,
  18635. ;; inline tasks.
  18636. (or (and (looking-at "[ \t]*:END:")
  18637. (re-search-backward org-drawer-regexp nil t))
  18638. (and (looking-at "[ \t]*#\\+end_")
  18639. (re-search-backward "[ \t]*#\\+begin_"nil t))
  18640. (looking-at "[ \t]*[\n:#|]")
  18641. (looking-at org-footnote-definition-re)
  18642. (and (ignore-errors (goto-char (org-in-item-p)))
  18643. (goto-char
  18644. (org-list-get-top-point (org-list-struct))))
  18645. (and (not inline-task-p)
  18646. (featurep 'org-inlinetask)
  18647. (org-inlinetask-in-task-p)
  18648. (or (org-inlinetask-goto-beginning) t))))
  18649. (beginning-of-line 0))
  18650. (cond
  18651. ;; There was an heading above.
  18652. ((looking-at "\\*+[ \t]+")
  18653. (if (not org-adapt-indentation)
  18654. (setq column 0)
  18655. (goto-char (match-end 0))
  18656. (setq column (current-column))))
  18657. ;; A drawer had started and is unfinished
  18658. ((looking-at org-drawer-regexp)
  18659. (goto-char (1- (match-beginning 1)))
  18660. (setq column (current-column)))
  18661. ;; Else, nothing noticeable found: get indentation and go on.
  18662. (t (setq column (org-get-indentation))))))
  18663. ;; Now apply indentation and move cursor accordingly
  18664. (goto-char pos)
  18665. (if (<= (current-column) (current-indentation))
  18666. (org-indent-line-to column)
  18667. (save-excursion (org-indent-line-to column)))
  18668. ;; Special polishing for properties, see `org-property-format'
  18669. (setq column (current-column))
  18670. (beginning-of-line 1)
  18671. (if (looking-at
  18672. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  18673. (replace-match (concat (match-string 1)
  18674. (format org-property-format
  18675. (match-string 2) (match-string 3)))
  18676. t t))
  18677. (org-move-to-column column)))
  18678. (defun org-indent-drawer ()
  18679. "Indent the drawer at point."
  18680. (interactive)
  18681. (let ((p (point))
  18682. (e (and (save-excursion (re-search-forward ":END:" nil t))
  18683. (match-end 0)))
  18684. (folded
  18685. (save-excursion
  18686. (end-of-line)
  18687. (when (overlays-at (point))
  18688. (member 'invisible (overlay-properties
  18689. (car (overlays-at (point)))))))))
  18690. (when folded (org-cycle))
  18691. (indent-for-tab-command)
  18692. (while (and (move-beginning-of-line 2) (< (point) e))
  18693. (indent-for-tab-command))
  18694. (goto-char p)
  18695. (when folded (org-cycle)))
  18696. (message "Drawer at point indented"))
  18697. (defun org-indent-block ()
  18698. "Indent the block at point."
  18699. (interactive)
  18700. (let ((p (point))
  18701. (case-fold-search t)
  18702. (e (and (save-excursion (re-search-forward "#\\+end_?\\(?:[a-z]+\\)?" nil t))
  18703. (match-end 0)))
  18704. (folded
  18705. (save-excursion
  18706. (end-of-line)
  18707. (when (overlays-at (point))
  18708. (member 'invisible (overlay-properties
  18709. (car (overlays-at (point)))))))))
  18710. (when folded (org-cycle))
  18711. (indent-for-tab-command)
  18712. (while (and (move-beginning-of-line 2) (< (point) e))
  18713. (indent-for-tab-command))
  18714. (goto-char p)
  18715. (when folded (org-cycle)))
  18716. (message "Block at point indented"))
  18717. (defvar org-adaptive-fill-regexp-backup adaptive-fill-regexp
  18718. "Variable to store copy of `adaptive-fill-regexp'.
  18719. Since `adaptive-fill-regexp' is set to never match, we need to
  18720. store a backup of its value before entering `org-mode' so that
  18721. the functionality can be provided as a fall-back.")
  18722. (defun org-set-autofill-regexps ()
  18723. (interactive)
  18724. ;; In the paragraph separator we include headlines, because filling
  18725. ;; text in a line directly attached to a headline would otherwise
  18726. ;; fill the headline as well.
  18727. (org-set-local 'comment-start-skip "^#+[ \t]*")
  18728. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|#]")
  18729. ;; The paragraph starter includes hand-formatted lists.
  18730. (org-set-local
  18731. 'paragraph-start
  18732. (concat
  18733. "\f" "\\|"
  18734. "[ ]*$" "\\|"
  18735. org-outline-regexp "\\|"
  18736. "[ \t]*#" "\\|"
  18737. (org-item-re) "\\|"
  18738. "[ \t]*[:|]" "\\|"
  18739. "\\$\\$" "\\|"
  18740. "\\\\\\(begin\\|end\\|[][]\\)"))
  18741. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  18742. ;; But only if the user has not turned off tables or fixed-width regions
  18743. (org-set-local
  18744. 'auto-fill-inhibit-regexp
  18745. (concat org-outline-regexp
  18746. "\\|#\\+"
  18747. "\\|[ \t]*" org-keyword-time-regexp
  18748. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  18749. (concat
  18750. "\\|[ \t]*["
  18751. (if org-enable-table-editor "|" "")
  18752. (if org-enable-fixed-width-editor ":" "")
  18753. "]"))))
  18754. ;; We use our own fill-paragraph function, to make sure that tables
  18755. ;; and fixed-width regions are not wrapped. That function will pass
  18756. ;; through to `fill-paragraph' when appropriate.
  18757. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  18758. ;; Prevent auto-fill from inserting unwanted new items.
  18759. (if (boundp 'fill-nobreak-predicate)
  18760. (org-set-local
  18761. 'fill-nobreak-predicate
  18762. (org-uniquify
  18763. (append fill-nobreak-predicate
  18764. '(org-fill-item-nobreak-p org-fill-line-break-nobreak-p)))))
  18765. ;; Adaptive filling: To get full control, first make sure that
  18766. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  18767. (unless (local-variable-p 'adaptive-fill-regexp (current-buffer))
  18768. (org-set-local 'org-adaptive-fill-regexp-backup
  18769. adaptive-fill-regexp))
  18770. (org-set-local 'adaptive-fill-regexp "\000")
  18771. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  18772. (org-set-local 'adaptive-fill-function
  18773. 'org-adaptive-fill-function)
  18774. (org-set-local
  18775. 'align-mode-rules-list
  18776. '((org-in-buffer-settings
  18777. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  18778. (modes . '(org-mode))))))
  18779. (defun org-fill-item-nobreak-p ()
  18780. "Non-nil when a line break at point would insert a new item."
  18781. (and (looking-at (org-item-re)) (org-list-in-valid-context-p)))
  18782. (defun org-fill-line-break-nobreak-p ()
  18783. "Non-nil when a line break at point would create an Org line break."
  18784. (save-excursion
  18785. (skip-chars-backward "[ \t]")
  18786. (skip-chars-backward "\\\\")
  18787. (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
  18788. (defun org-fill-paragraph (&optional justify)
  18789. "Re-align a table, pass through to fill-paragraph if no table."
  18790. (let ((table-p (org-at-table-p))
  18791. (table.el-p (org-at-table.el-p))
  18792. (itemp (org-in-item-p)))
  18793. (cond ((and (equal (char-after (point-at-bol)) ?*)
  18794. (save-excursion (goto-char (point-at-bol))
  18795. (looking-at org-outline-regexp)))
  18796. t) ; skip headlines
  18797. (table.el-p t) ; skip table.el tables
  18798. (table-p (org-table-align) t) ; align Org tables
  18799. (itemp ; align text in items
  18800. (let* ((struct (save-excursion (goto-char itemp)
  18801. (org-list-struct)))
  18802. (parents (org-list-parents-alist struct))
  18803. (children (org-list-get-children itemp struct parents))
  18804. beg end prev next prefix)
  18805. ;; Determine in which part of item point is: before
  18806. ;; first child, after last child, between two
  18807. ;; sub-lists, or simply in item if there's no child.
  18808. (cond
  18809. ((not children)
  18810. (setq prefix (make-string (org-list-item-body-column itemp) ?\ )
  18811. beg itemp
  18812. end (org-list-get-item-end itemp struct)))
  18813. ((< (point) (setq next (car children)))
  18814. (setq prefix (make-string (org-list-item-body-column itemp) ?\ )
  18815. beg itemp
  18816. end next))
  18817. ((> (point) (setq prev (car (last children))))
  18818. (setq beg (org-list-get-item-end prev struct)
  18819. end (org-list-get-item-end itemp struct)
  18820. prefix (save-excursion
  18821. (goto-char beg)
  18822. (skip-chars-forward " \t")
  18823. (make-string (current-column) ?\ ))))
  18824. (t (catch 'exit
  18825. (while (setq next (pop children))
  18826. (if (> (point) next)
  18827. (setq prev next)
  18828. (setq beg (org-list-get-item-end prev struct)
  18829. end next
  18830. prefix (save-excursion
  18831. (goto-char beg)
  18832. (skip-chars-forward " \t")
  18833. (make-string (current-column) ?\ )))
  18834. (throw 'exit nil))))))
  18835. ;; Use `fill-paragraph' with buffer narrowed to item
  18836. ;; without any child, and with our computed PREFIX.
  18837. (flet ((fill-context-prefix (from to &optional flr) prefix))
  18838. (save-restriction
  18839. (narrow-to-region beg end)
  18840. (save-excursion (fill-paragraph justify)))) t))
  18841. ;; Special case where point is not in a list but is on
  18842. ;; a paragraph adjacent to a list: make sure this paragraph
  18843. ;; doesn't get merged with the end of the list by narrowing
  18844. ;; buffer first.
  18845. ((save-excursion (forward-paragraph -1)
  18846. (setq itemp (org-in-item-p)))
  18847. (let ((struct (save-excursion (goto-char itemp)
  18848. (org-list-struct))))
  18849. (save-restriction
  18850. (narrow-to-region (org-list-get-bottom-point struct)
  18851. (save-excursion (forward-paragraph 1)
  18852. (point)))
  18853. (fill-paragraph justify) t)))
  18854. ;; Don't fill schedule/deadline line before a paragraph
  18855. ((save-excursion (forward-paragraph -1)
  18856. (or (looking-at (concat "^[^\n]*" org-scheduled-regexp ".*$"))
  18857. (looking-at (concat "^[^\n]*" org-deadline-regexp ".*$"))))
  18858. (save-restriction
  18859. (narrow-to-region (1+ (match-end 0))
  18860. (save-excursion (forward-paragraph 1) (point)))
  18861. (fill-paragraph justify) t))
  18862. ;; Else simply call `fill-paragraph'.
  18863. (t nil))))
  18864. ;; For reference, this is the default value of adaptive-fill-regexp
  18865. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  18866. (defun org-adaptive-fill-function ()
  18867. "Return a fill prefix for org-mode files."
  18868. (let (itemp)
  18869. (save-excursion
  18870. (cond
  18871. ;; Comment line
  18872. ((looking-at "#[ \t]+")
  18873. (match-string-no-properties 0))
  18874. ;; Plain list item
  18875. ((org-at-item-p)
  18876. (make-string (org-list-item-body-column (point-at-bol)) ?\ ))
  18877. ;; Point is in a list after `backward-paragraph': original
  18878. ;; point wasn't in the list, or filling would have been taken
  18879. ;; care of by `org-auto-fill-function', but the list and the
  18880. ;; real paragraph are not separated by a blank line. Thus, move
  18881. ;; point after the list to go back to real paragraph and
  18882. ;; determine fill-prefix.
  18883. ((setq itemp (org-in-item-p))
  18884. (goto-char itemp)
  18885. (let* ((struct (org-list-struct))
  18886. (bottom (org-list-get-bottom-point struct)))
  18887. (goto-char bottom)
  18888. (make-string (org-get-indentation) ?\ )))
  18889. ;; Other text
  18890. ((looking-at org-adaptive-fill-regexp-backup)
  18891. (match-string-no-properties 0))))))
  18892. (defun org-auto-fill-function ()
  18893. "Auto-fill function."
  18894. (let (itemp prefix)
  18895. ;; When in a list, compute an appropriate fill-prefix and make
  18896. ;; sure it will be used by `do-auto-fill'.
  18897. (if (setq itemp (org-in-item-p))
  18898. (progn
  18899. (setq prefix (make-string (org-list-item-body-column itemp) ?\ ))
  18900. (flet ((fill-context-prefix (from to &optional flr) prefix))
  18901. (do-auto-fill)))
  18902. ;; Else just use `do-auto-fill'.
  18903. (do-auto-fill))))
  18904. ;;; Other stuff.
  18905. (defun org-toggle-fixed-width-section (arg)
  18906. "Toggle the fixed-width export.
  18907. If there is no active region, the QUOTE keyword at the current headline is
  18908. inserted or removed. When present, it causes the text between this headline
  18909. and the next to be exported as fixed-width text, and unmodified.
  18910. If there is an active region, this command adds or removes a colon as the
  18911. first character of this line. If the first character of a line is a colon,
  18912. this line is also exported in fixed-width font."
  18913. (interactive "P")
  18914. (let* ((cc 0)
  18915. (regionp (org-region-active-p))
  18916. (beg (if regionp (region-beginning) (point)))
  18917. (end (if regionp (region-end)))
  18918. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  18919. (case-fold-search nil)
  18920. (re "[ \t]*\\(:\\(?: \\|$\\)\\)")
  18921. off)
  18922. (if regionp
  18923. (save-excursion
  18924. (goto-char beg)
  18925. (setq cc (current-column))
  18926. (beginning-of-line 1)
  18927. (setq off (looking-at re))
  18928. (while (> nlines 0)
  18929. (setq nlines (1- nlines))
  18930. (beginning-of-line 1)
  18931. (cond
  18932. (arg
  18933. (org-move-to-column cc t)
  18934. (insert ": \n")
  18935. (forward-line -1))
  18936. ((and off (looking-at re))
  18937. (replace-match "" t t nil 1))
  18938. ((not off) (org-move-to-column cc t) (insert ": ")))
  18939. (forward-line 1)))
  18940. (save-excursion
  18941. (org-back-to-heading)
  18942. (cond
  18943. ((looking-at (format org-heading-keyword-regexp-format
  18944. org-quote-string))
  18945. (goto-char (match-end 1))
  18946. (looking-at (concat " +" org-quote-string))
  18947. (replace-match "" t t)
  18948. (when (eolp) (insert " ")))
  18949. ((looking-at org-outline-regexp)
  18950. (goto-char (match-end 0))
  18951. (insert org-quote-string " ")))))))
  18952. (defun org-reftex-citation ()
  18953. "Use reftex-citation to insert a citation into the buffer.
  18954. This looks for a line like
  18955. #+BIBLIOGRAPHY: foo plain option:-d
  18956. and derives from it that foo.bib is the bibliography file relevant
  18957. for this document. It then installs the necessary environment for RefTeX
  18958. to work in this buffer and calls `reftex-citation' to insert a citation
  18959. into the buffer.
  18960. Export of such citations to both LaTeX and HTML is handled by the contributed
  18961. package org-exp-bibtex by Taru Karttunen."
  18962. (interactive)
  18963. (let ((reftex-docstruct-symbol 'rds)
  18964. (reftex-cite-format "\\cite{%l}")
  18965. rds bib)
  18966. (save-excursion
  18967. (save-restriction
  18968. (widen)
  18969. (let ((case-fold-search t)
  18970. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  18971. (if (not (save-excursion
  18972. (or (re-search-forward re nil t)
  18973. (re-search-backward re nil t))))
  18974. (error "No bibliography defined in file")
  18975. (setq bib (concat (match-string 1) ".bib")
  18976. rds (list (list 'bib bib)))))))
  18977. (call-interactively 'reftex-citation)))
  18978. ;;;; Functions extending outline functionality
  18979. (defun org-beginning-of-line (&optional arg)
  18980. "Go to the beginning of the current line. If that is invisible, continue
  18981. to a visible line beginning. This makes the function of C-a more intuitive.
  18982. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  18983. first attempt, and only move to after the tags when the cursor is already
  18984. beyond the end of the headline."
  18985. (interactive "P")
  18986. (let ((pos (point))
  18987. (special (if (consp org-special-ctrl-a/e)
  18988. (car org-special-ctrl-a/e)
  18989. org-special-ctrl-a/e))
  18990. refpos)
  18991. (if (org-bound-and-true-p line-move-visual)
  18992. (beginning-of-visual-line 1)
  18993. (beginning-of-line 1))
  18994. (if (and arg (fboundp 'move-beginning-of-line))
  18995. (call-interactively 'move-beginning-of-line)
  18996. (if (bobp)
  18997. nil
  18998. (backward-char 1)
  18999. (if (org-truely-invisible-p)
  19000. (while (and (not (bobp)) (org-truely-invisible-p))
  19001. (backward-char 1)
  19002. (beginning-of-line 1))
  19003. (forward-char 1))))
  19004. (when special
  19005. (cond
  19006. ((and (looking-at org-complex-heading-regexp)
  19007. (= (char-after (match-end 1)) ?\ ))
  19008. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  19009. (point-at-eol)))
  19010. (goto-char
  19011. (if (eq special t)
  19012. (cond ((> pos refpos) refpos)
  19013. ((= pos (point)) refpos)
  19014. (t (point)))
  19015. (cond ((> pos (point)) (point))
  19016. ((not (eq last-command this-command)) (point))
  19017. (t refpos)))))
  19018. ((org-at-item-p)
  19019. ;; Being at an item and not looking at an the item means point
  19020. ;; was previously moved to beginning of a visual line, which
  19021. ;; doesn't contain the item. Therefore, do nothing special,
  19022. ;; just stay here.
  19023. (when (looking-at org-list-full-item-re)
  19024. ;; Set special position at first white space character after
  19025. ;; bullet, and check-box, if any.
  19026. (let ((after-bullet
  19027. (let ((box (match-end 3)))
  19028. (if (not box) (match-end 1)
  19029. (let ((after (char-after box)))
  19030. (if (and after (= after ? )) (1+ box) box))))))
  19031. ;; Special case: Move point to special position when
  19032. ;; currently after it or at beginning of line.
  19033. (if (eq special t)
  19034. (when (or (> pos after-bullet) (= (point) pos))
  19035. (goto-char after-bullet))
  19036. ;; Reversed case: Move point to special position when
  19037. ;; point was already at beginning of line and command is
  19038. ;; repeated.
  19039. (when (and (= (point) pos) (eq last-command this-command))
  19040. (goto-char after-bullet))))))))
  19041. (org-no-warnings
  19042. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  19043. (defun org-end-of-line (&optional arg)
  19044. "Go to the end of the line.
  19045. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  19046. first attempt, and only move to after the tags when the cursor is already
  19047. beyond the end of the headline."
  19048. (interactive "P")
  19049. (let ((special (if (consp org-special-ctrl-a/e)
  19050. (cdr org-special-ctrl-a/e)
  19051. org-special-ctrl-a/e)))
  19052. (cond
  19053. ((or (not special) arg
  19054. (not (or (org-at-heading-p) (org-at-item-p) (org-at-drawer-p))))
  19055. (call-interactively
  19056. (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
  19057. ((fboundp 'move-end-of-line) 'move-end-of-line)
  19058. (t 'end-of-line))))
  19059. ((org-at-heading-p)
  19060. (let ((pos (point)))
  19061. (beginning-of-line 1)
  19062. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  19063. (if (eq special t)
  19064. (if (or (< pos (match-beginning 1))
  19065. (= pos (match-end 0)))
  19066. (goto-char (match-beginning 1))
  19067. (goto-char (match-end 0)))
  19068. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  19069. (goto-char (match-end 0))
  19070. (goto-char (match-beginning 1))))
  19071. (call-interactively (if (fboundp 'move-end-of-line)
  19072. 'move-end-of-line
  19073. 'end-of-line)))))
  19074. ((org-at-drawer-p)
  19075. (move-end-of-line 1)
  19076. (when (overlays-at (1- (point))) (backward-char 1)))
  19077. ;; At an item: Move before any hidden text.
  19078. (t (call-interactively
  19079. (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
  19080. ((fboundp 'move-end-of-line) 'move-end-of-line)
  19081. (t 'end-of-line)))))
  19082. (org-no-warnings
  19083. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  19084. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  19085. (define-key org-mode-map "\C-e" 'org-end-of-line)
  19086. (defun org-backward-sentence (&optional arg)
  19087. "Go to beginning of sentence, or beginning of table field.
  19088. This will call `backward-sentence' or `org-table-beginning-of-field',
  19089. depending on context."
  19090. (interactive "P")
  19091. (cond
  19092. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  19093. (t (call-interactively 'backward-sentence))))
  19094. (defun org-forward-sentence (&optional arg)
  19095. "Go to end of sentence, or end of table field.
  19096. This will call `forward-sentence' or `org-table-end-of-field',
  19097. depending on context."
  19098. (interactive "P")
  19099. (cond
  19100. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  19101. (t (call-interactively 'forward-sentence))))
  19102. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  19103. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  19104. (defun org-kill-line (&optional arg)
  19105. "Kill line, to tags or end of line."
  19106. (interactive "P")
  19107. (cond
  19108. ((or (not org-special-ctrl-k)
  19109. (bolp)
  19110. (not (org-at-heading-p)))
  19111. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  19112. org-ctrl-k-protect-subtree)
  19113. (if (or (eq org-ctrl-k-protect-subtree 'error)
  19114. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  19115. (error "C-k aborted - would kill hidden subtree")))
  19116. (call-interactively
  19117. (if (and (boundp 'visual-line-mode) visual-line-mode) 'kill-visual-line 'kill-line)))
  19118. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  19119. (kill-region (point) (match-beginning 1))
  19120. (org-set-tags nil t))
  19121. (t (kill-region (point) (point-at-eol)))))
  19122. (define-key org-mode-map "\C-k" 'org-kill-line)
  19123. (defun org-yank (&optional arg)
  19124. "Yank. If the kill is a subtree, treat it specially.
  19125. This command will look at the current kill and check if is a single
  19126. subtree, or a series of subtrees[1]. If it passes the test, and if the
  19127. cursor is at the beginning of a line or after the stars of a currently
  19128. empty headline, then the yank is handled specially. How exactly depends
  19129. on the value of the following variables, both set by default.
  19130. org-yank-folded-subtrees
  19131. When set, the subtree(s) will be folded after insertion, but only
  19132. if doing so would now swallow text after the yanked text.
  19133. org-yank-adjusted-subtrees
  19134. When set, the subtree will be promoted or demoted in order to
  19135. fit into the local outline tree structure, which means that the level
  19136. will be adjusted so that it becomes the smaller one of the two
  19137. *visible* surrounding headings.
  19138. Any prefix to this command will cause `yank' to be called directly with
  19139. no special treatment. In particular, a simple \\[universal-argument] prefix \
  19140. will just
  19141. plainly yank the text as it is.
  19142. \[1] The test checks if the first non-white line is a heading
  19143. and if there are no other headings with fewer stars."
  19144. (interactive "P")
  19145. (org-yank-generic 'yank arg))
  19146. (defun org-yank-generic (command arg)
  19147. "Perform some yank-like command.
  19148. This function implements the behavior described in the `org-yank'
  19149. documentation. However, it has been generalized to work for any
  19150. interactive command with similar behavior."
  19151. ;; pretend to be command COMMAND
  19152. (setq this-command command)
  19153. (if arg
  19154. (call-interactively command)
  19155. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  19156. (and (org-kill-is-subtree-p)
  19157. (or (bolp)
  19158. (and (looking-at "[ \t]*$")
  19159. (string-match
  19160. "\\`\\*+\\'"
  19161. (buffer-substring (point-at-bol) (point)))))))
  19162. swallowp)
  19163. (cond
  19164. ((and subtreep org-yank-folded-subtrees)
  19165. (let ((beg (point))
  19166. end)
  19167. (if (and subtreep org-yank-adjusted-subtrees)
  19168. (org-paste-subtree nil nil 'for-yank)
  19169. (call-interactively command))
  19170. (setq end (point))
  19171. (goto-char beg)
  19172. (when (and (bolp) subtreep
  19173. (not (setq swallowp
  19174. (org-yank-folding-would-swallow-text beg end))))
  19175. (org-with-limited-levels
  19176. (or (looking-at org-outline-regexp)
  19177. (re-search-forward org-outline-regexp-bol end t))
  19178. (while (and (< (point) end) (looking-at org-outline-regexp))
  19179. (hide-subtree)
  19180. (org-cycle-show-empty-lines 'folded)
  19181. (condition-case nil
  19182. (outline-forward-same-level 1)
  19183. (error (goto-char end))))))
  19184. (when swallowp
  19185. (message
  19186. "Inserted text not folded because that would swallow text"))
  19187. (goto-char end)
  19188. (skip-chars-forward " \t\n\r")
  19189. (beginning-of-line 1)
  19190. (push-mark beg 'nomsg)))
  19191. ((and subtreep org-yank-adjusted-subtrees)
  19192. (let ((beg (point-at-bol)))
  19193. (org-paste-subtree nil nil 'for-yank)
  19194. (push-mark beg 'nomsg)))
  19195. (t
  19196. (call-interactively command))))))
  19197. (defun org-yank-folding-would-swallow-text (beg end)
  19198. "Would hide-subtree at BEG swallow any text after END?"
  19199. (let (level)
  19200. (org-with-limited-levels
  19201. (save-excursion
  19202. (goto-char beg)
  19203. (when (or (looking-at org-outline-regexp)
  19204. (re-search-forward org-outline-regexp-bol end t))
  19205. (setq level (org-outline-level)))
  19206. (goto-char end)
  19207. (skip-chars-forward " \t\r\n\v\f")
  19208. (if (or (eobp)
  19209. (and (bolp) (looking-at org-outline-regexp)
  19210. (<= (org-outline-level) level)))
  19211. nil ; Nothing would be swallowed
  19212. t))))) ; something would swallow
  19213. (define-key org-mode-map "\C-y" 'org-yank)
  19214. (defun org-truely-invisible-p ()
  19215. "Check if point is at a character currently not visible.
  19216. This version does not only check the character property, but also
  19217. `visible-mode'."
  19218. ;; Early versions of noutline don't have `outline-invisible-p'.
  19219. (if (org-bound-and-true-p visible-mode)
  19220. nil
  19221. (outline-invisible-p)))
  19222. (defun org-invisible-p2 ()
  19223. "Check if point is at a character currently not visible."
  19224. (save-excursion
  19225. (if (and (eolp) (not (bobp))) (backward-char 1))
  19226. ;; Early versions of noutline don't have `outline-invisible-p'.
  19227. (outline-invisible-p)))
  19228. (defun org-back-to-heading (&optional invisible-ok)
  19229. "Call `outline-back-to-heading', but provide a better error message."
  19230. (condition-case nil
  19231. (outline-back-to-heading invisible-ok)
  19232. (error (error "Before first headline at position %d in buffer %s"
  19233. (point) (current-buffer)))))
  19234. (defun org-beginning-of-defun ()
  19235. "Go to the beginning of the subtree, i.e. back to the heading."
  19236. (org-back-to-heading))
  19237. (defun org-end-of-defun ()
  19238. "Go to the end of the subtree."
  19239. (org-end-of-subtree nil t))
  19240. (defun org-before-first-heading-p ()
  19241. "Before first heading?"
  19242. (save-excursion
  19243. (end-of-line)
  19244. (null (re-search-backward org-outline-regexp-bol nil t))))
  19245. (defun org-at-heading-p (&optional ignored)
  19246. (outline-on-heading-p t))
  19247. ;; Compatibility alias with Org versions < 7.8.03
  19248. (defalias 'org-on-heading-p 'org-at-heading-p)
  19249. (defun org-at-drawer-p nil
  19250. "Is cursor at a drawer keyword?"
  19251. (save-excursion
  19252. (move-beginning-of-line 1)
  19253. (looking-at org-drawer-regexp)))
  19254. (defun org-at-block-p nil
  19255. "Is cursor at a block keyword?"
  19256. (save-excursion
  19257. (move-beginning-of-line 1)
  19258. (looking-at org-block-regexp)))
  19259. (defun org-point-at-end-of-empty-headline ()
  19260. "If point is at the end of an empty headline, return t, else nil.
  19261. If the heading only contains a TODO keyword, it is still still considered
  19262. empty."
  19263. (and (looking-at "[ \t]*$")
  19264. (when org-todo-line-regexp
  19265. (save-excursion
  19266. (beginning-of-line 1)
  19267. (let ((case-fold-search nil))
  19268. (looking-at org-todo-line-regexp)
  19269. (string= (match-string 3) ""))))))
  19270. (defun org-at-heading-or-item-p ()
  19271. (or (org-at-heading-p) (org-at-item-p)))
  19272. (defun org-at-target-p ()
  19273. (or (org-in-regexp org-radio-target-regexp)
  19274. (org-in-regexp org-target-regexp)))
  19275. ;; Compatibility alias with Org versions < 7.8.03
  19276. (defalias 'org-on-target-p 'org-at-target-p)
  19277. (defun org-up-heading-all (arg)
  19278. "Move to the heading line of which the present line is a subheading.
  19279. This function considers both visible and invisible heading lines.
  19280. With argument, move up ARG levels."
  19281. (if (fboundp 'outline-up-heading-all)
  19282. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  19283. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  19284. (defun org-up-heading-safe ()
  19285. "Move to the heading line of which the present line is a subheading.
  19286. This version will not throw an error. It will return the level of the
  19287. headline found, or nil if no higher level is found.
  19288. Also, this function will be a lot faster than `outline-up-heading',
  19289. because it relies on stars being the outline starters. This can really
  19290. make a significant difference in outlines with very many siblings."
  19291. (let (start-level re)
  19292. (org-back-to-heading t)
  19293. (setq start-level (funcall outline-level))
  19294. (if (equal start-level 1)
  19295. nil
  19296. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  19297. (if (re-search-backward re nil t)
  19298. (funcall outline-level)))))
  19299. (defun org-first-sibling-p ()
  19300. "Is this heading the first child of its parents?"
  19301. (interactive)
  19302. (let ((re org-outline-regexp-bol)
  19303. level l)
  19304. (unless (org-at-heading-p t)
  19305. (error "Not at a heading"))
  19306. (setq level (funcall outline-level))
  19307. (save-excursion
  19308. (if (not (re-search-backward re nil t))
  19309. t
  19310. (setq l (funcall outline-level))
  19311. (< l level)))))
  19312. (defun org-goto-sibling (&optional previous)
  19313. "Goto the next sibling, even if it is invisible.
  19314. When PREVIOUS is set, go to the previous sibling instead. Returns t
  19315. when a sibling was found. When none is found, return nil and don't
  19316. move point."
  19317. (let ((fun (if previous 're-search-backward 're-search-forward))
  19318. (pos (point))
  19319. (re org-outline-regexp-bol)
  19320. level l)
  19321. (when (condition-case nil (org-back-to-heading t) (error nil))
  19322. (setq level (funcall outline-level))
  19323. (catch 'exit
  19324. (or previous (forward-char 1))
  19325. (while (funcall fun re nil t)
  19326. (setq l (funcall outline-level))
  19327. (when (< l level) (goto-char pos) (throw 'exit nil))
  19328. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  19329. (goto-char pos)
  19330. nil))))
  19331. (defun org-show-siblings ()
  19332. "Show all siblings of the current headline."
  19333. (save-excursion
  19334. (while (org-goto-sibling) (org-flag-heading nil)))
  19335. (save-excursion
  19336. (while (org-goto-sibling 'previous)
  19337. (org-flag-heading nil))))
  19338. (defun org-goto-first-child ()
  19339. "Goto the first child, even if it is invisible.
  19340. Return t when a child was found. Otherwise don't move point and
  19341. return nil."
  19342. (let (level (pos (point)) (re org-outline-regexp-bol))
  19343. (when (condition-case nil (org-back-to-heading t) (error nil))
  19344. (setq level (outline-level))
  19345. (forward-char 1)
  19346. (if (and (re-search-forward re nil t) (> (outline-level) level))
  19347. (progn (goto-char (match-beginning 0)) t)
  19348. (goto-char pos) nil))))
  19349. (defun org-show-hidden-entry ()
  19350. "Show an entry where even the heading is hidden."
  19351. (save-excursion
  19352. (org-show-entry)))
  19353. (defun org-flag-heading (flag &optional entry)
  19354. "Flag the current heading. FLAG non-nil means make invisible.
  19355. When ENTRY is non-nil, show the entire entry."
  19356. (save-excursion
  19357. (org-back-to-heading t)
  19358. ;; Check if we should show the entire entry
  19359. (if entry
  19360. (progn
  19361. (org-show-entry)
  19362. (save-excursion
  19363. (and (outline-next-heading)
  19364. (org-flag-heading nil))))
  19365. (outline-flag-region (max (point-min) (1- (point)))
  19366. (save-excursion (outline-end-of-heading) (point))
  19367. flag))))
  19368. (defun org-get-next-sibling ()
  19369. "Move to next heading of the same level, and return point.
  19370. If there is no such heading, return nil.
  19371. This is like outline-next-sibling, but invisible headings are ok."
  19372. (let ((level (funcall outline-level)))
  19373. (outline-next-heading)
  19374. (while (and (not (eobp)) (> (funcall outline-level) level))
  19375. (outline-next-heading))
  19376. (if (or (eobp) (< (funcall outline-level) level))
  19377. nil
  19378. (point))))
  19379. (defun org-get-last-sibling ()
  19380. "Move to previous heading of the same level, and return point.
  19381. If there is no such heading, return nil."
  19382. (let ((opoint (point))
  19383. (level (funcall outline-level)))
  19384. (outline-previous-heading)
  19385. (when (and (/= (point) opoint) (outline-on-heading-p t))
  19386. (while (and (> (funcall outline-level) level)
  19387. (not (bobp)))
  19388. (outline-previous-heading))
  19389. (if (< (funcall outline-level) level)
  19390. nil
  19391. (point)))))
  19392. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  19393. ;; This contains an exact copy of the original function, but it uses
  19394. ;; `org-back-to-heading', to make it work also in invisible
  19395. ;; trees. And is uses an invisible-OK argument.
  19396. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  19397. ;; Furthermore, when used inside Org, finding the end of a large subtree
  19398. ;; with many children and grandchildren etc, this can be much faster
  19399. ;; than the outline version.
  19400. (org-back-to-heading invisible-OK)
  19401. (let ((first t)
  19402. (level (funcall outline-level)))
  19403. (if (and (eq major-mode 'org-mode) (< level 1000))
  19404. ;; A true heading (not a plain list item), in Org-mode
  19405. ;; This means we can easily find the end by looking
  19406. ;; only for the right number of stars. Using a regexp to do
  19407. ;; this is so much faster than using a Lisp loop.
  19408. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  19409. (forward-char 1)
  19410. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  19411. ;; something else, do it the slow way
  19412. (while (and (not (eobp))
  19413. (or first (> (funcall outline-level) level)))
  19414. (setq first nil)
  19415. (outline-next-heading)))
  19416. (unless to-heading
  19417. (if (memq (preceding-char) '(?\n ?\^M))
  19418. (progn
  19419. ;; Go to end of line before heading
  19420. (forward-char -1)
  19421. (if (memq (preceding-char) '(?\n ?\^M))
  19422. ;; leave blank line before heading
  19423. (forward-char -1))))))
  19424. (point))
  19425. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  19426. "Use Org version in org-mode, for dramatic speed-up."
  19427. (if (eq major-mode 'org-mode)
  19428. (progn
  19429. (org-end-of-subtree nil t)
  19430. (unless (eobp) (backward-char 1)))
  19431. ad-do-it))
  19432. (defun org-end-of-meta-data-and-drawers ()
  19433. "Jump to the first text after meta data and drawers in the current entry.
  19434. This will move over empty lines, lines with planning time stamps,
  19435. clocking lines, and drawers."
  19436. (org-back-to-heading t)
  19437. (let ((end (save-excursion (outline-next-heading) (point)))
  19438. (re (concat "\\(" org-drawer-regexp "\\)"
  19439. "\\|" "[ \t]*" org-keyword-time-regexp)))
  19440. (forward-line 1)
  19441. (while (re-search-forward re end t)
  19442. (if (not (match-end 1))
  19443. ;; empty or planning line
  19444. (forward-line 1)
  19445. ;; a drawer, find the end
  19446. (re-search-forward "^[ \t]*:END:" end 'move)
  19447. (forward-line 1)))
  19448. (and (re-search-forward "[^\n]" nil t) (backward-char 1))
  19449. (point)))
  19450. (defun org-forward-same-level (arg &optional invisible-ok)
  19451. "Move forward to the arg'th subheading at same level as this one.
  19452. Stop at the first and last subheadings of a superior heading.
  19453. Normally this only looks at visible headings, but when INVISIBLE-OK is non-nil
  19454. it wil also look at invisible ones."
  19455. (interactive "p")
  19456. (org-back-to-heading invisible-ok)
  19457. (org-at-heading-p)
  19458. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  19459. (re (format "^\\*\\{1,%d\\} " level))
  19460. l)
  19461. (forward-char 1)
  19462. (while (> arg 0)
  19463. (while (and (re-search-forward re nil 'move)
  19464. (setq l (- (match-end 0) (match-beginning 0) 1))
  19465. (= l level)
  19466. (not invisible-ok)
  19467. (progn (backward-char 1) (outline-invisible-p)))
  19468. (if (< l level) (setq arg 1)))
  19469. (setq arg (1- arg)))
  19470. (beginning-of-line 1)))
  19471. (defun org-backward-same-level (arg &optional invisible-ok)
  19472. "Move backward to the arg'th subheading at same level as this one.
  19473. Stop at the first and last subheadings of a superior heading."
  19474. (interactive "p")
  19475. (org-back-to-heading)
  19476. (org-at-heading-p)
  19477. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  19478. (re (format "^\\*\\{1,%d\\} " level))
  19479. l)
  19480. (while (> arg 0)
  19481. (while (and (re-search-backward re nil 'move)
  19482. (setq l (- (match-end 0) (match-beginning 0) 1))
  19483. (= l level)
  19484. (not invisible-ok)
  19485. (outline-invisible-p))
  19486. (if (< l level) (setq arg 1)))
  19487. (setq arg (1- arg)))))
  19488. (defun org-show-subtree ()
  19489. "Show everything after this heading at deeper levels."
  19490. (interactive)
  19491. (outline-flag-region
  19492. (point)
  19493. (save-excursion
  19494. (org-end-of-subtree t t))
  19495. nil))
  19496. (defun org-show-entry ()
  19497. "Show the body directly following this heading.
  19498. Show the heading too, if it is currently invisible."
  19499. (interactive)
  19500. (save-excursion
  19501. (condition-case nil
  19502. (progn
  19503. (org-back-to-heading t)
  19504. (outline-flag-region
  19505. (max (point-min) (1- (point)))
  19506. (save-excursion
  19507. (if (re-search-forward
  19508. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  19509. (match-beginning 1)
  19510. (point-max)))
  19511. nil)
  19512. (org-cycle-hide-drawers 'children))
  19513. (error nil))))
  19514. (defun org-make-options-regexp (kwds &optional extra)
  19515. "Make a regular expression for keyword lines."
  19516. (concat
  19517. "^"
  19518. "#?[ \t]*\\+\\("
  19519. (mapconcat 'regexp-quote kwds "\\|")
  19520. (if extra (concat "\\|" extra))
  19521. "\\):[ \t]*"
  19522. "\\(.*\\)"))
  19523. ;; Make isearch reveal the necessary context
  19524. (defun org-isearch-end ()
  19525. "Reveal context after isearch exits."
  19526. (when isearch-success ; only if search was successful
  19527. (if (featurep 'xemacs)
  19528. ;; Under XEmacs, the hook is run in the correct place,
  19529. ;; we directly show the context.
  19530. (org-show-context 'isearch)
  19531. ;; In Emacs the hook runs *before* restoring the overlays.
  19532. ;; So we have to use a one-time post-command-hook to do this.
  19533. ;; (Emacs 22 has a special variable, see function `org-mode')
  19534. (unless (and (boundp 'isearch-mode-end-hook-quit)
  19535. isearch-mode-end-hook-quit)
  19536. ;; Only when the isearch was not quitted.
  19537. (org-add-hook 'post-command-hook 'org-isearch-post-command
  19538. 'append 'local)))))
  19539. (defun org-isearch-post-command ()
  19540. "Remove self from hook, and show context."
  19541. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  19542. (org-show-context 'isearch))
  19543. ;;;; Integration with and fixes for other packages
  19544. ;;; Imenu support
  19545. (defvar org-imenu-markers nil
  19546. "All markers currently used by Imenu.")
  19547. (make-variable-buffer-local 'org-imenu-markers)
  19548. (defun org-imenu-new-marker (&optional pos)
  19549. "Return a new marker for use by Imenu, and remember the marker."
  19550. (let ((m (make-marker)))
  19551. (move-marker m (or pos (point)))
  19552. (push m org-imenu-markers)
  19553. m))
  19554. (defun org-imenu-get-tree ()
  19555. "Produce the index for Imenu."
  19556. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  19557. (setq org-imenu-markers nil)
  19558. (let* ((n org-imenu-depth)
  19559. (re (concat "^" (org-get-limited-outline-regexp)))
  19560. (subs (make-vector (1+ n) nil))
  19561. (last-level 0)
  19562. m level head)
  19563. (save-excursion
  19564. (save-restriction
  19565. (widen)
  19566. (goto-char (point-max))
  19567. (while (re-search-backward re nil t)
  19568. (setq level (org-reduced-level (funcall outline-level)))
  19569. (when (and (<= level n)
  19570. (looking-at org-complex-heading-regexp))
  19571. (setq head (org-link-display-format
  19572. (org-match-string-no-properties 4))
  19573. m (org-imenu-new-marker))
  19574. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  19575. (if (>= level last-level)
  19576. (push (cons head m) (aref subs level))
  19577. (push (cons head (aref subs (1+ level))) (aref subs level))
  19578. (loop for i from (1+ level) to n do (aset subs i nil)))
  19579. (setq last-level level)))))
  19580. (aref subs 1)))
  19581. (eval-after-load "imenu"
  19582. '(progn
  19583. (add-hook 'imenu-after-jump-hook
  19584. (lambda ()
  19585. (if (eq major-mode 'org-mode)
  19586. (org-show-context 'org-goto))))))
  19587. (defun org-link-display-format (link)
  19588. "Replace a link with either the description, or the link target
  19589. if no description is present"
  19590. (save-match-data
  19591. (if (string-match org-bracket-link-analytic-regexp link)
  19592. (replace-match (if (match-end 5)
  19593. (match-string 5 link)
  19594. (concat (match-string 1 link)
  19595. (match-string 3 link)))
  19596. nil t link)
  19597. link)))
  19598. (defun org-toggle-link-display ()
  19599. "Toggle the literal or descriptive display of links."
  19600. (interactive)
  19601. (if org-descriptive-links
  19602. (progn (org-remove-from-invisibility-spec '(org-link))
  19603. (org-restart-font-lock)
  19604. (setq org-descriptive-links nil))
  19605. (progn (add-to-invisibility-spec '(org-link))
  19606. (org-restart-font-lock)
  19607. (setq org-descriptive-links t))))
  19608. ;; Speedbar support
  19609. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  19610. "Overlay marking the agenda restriction line in speedbar.")
  19611. (overlay-put org-speedbar-restriction-lock-overlay
  19612. 'face 'org-agenda-restriction-lock)
  19613. (overlay-put org-speedbar-restriction-lock-overlay
  19614. 'help-echo "Agendas are currently limited to this item.")
  19615. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  19616. (defun org-speedbar-set-agenda-restriction ()
  19617. "Restrict future agenda commands to the location at point in speedbar.
  19618. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  19619. (interactive)
  19620. (require 'org-agenda)
  19621. (let (p m tp np dir txt)
  19622. (cond
  19623. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  19624. 'org-imenu t))
  19625. (setq m (get-text-property p 'org-imenu-marker))
  19626. (with-current-buffer (marker-buffer m)
  19627. (goto-char m)
  19628. (org-agenda-set-restriction-lock 'subtree)))
  19629. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  19630. 'speedbar-function 'speedbar-find-file))
  19631. (setq tp (previous-single-property-change
  19632. (1+ p) 'speedbar-function)
  19633. np (next-single-property-change
  19634. tp 'speedbar-function)
  19635. dir (speedbar-line-directory)
  19636. txt (buffer-substring-no-properties (or tp (point-min))
  19637. (or np (point-max))))
  19638. (with-current-buffer (find-file-noselect
  19639. (let ((default-directory dir))
  19640. (expand-file-name txt)))
  19641. (unless (eq major-mode 'org-mode)
  19642. (error "Cannot restrict to non-Org-mode file"))
  19643. (org-agenda-set-restriction-lock 'file)))
  19644. (t (error "Don't know how to restrict Org-mode's agenda")))
  19645. (move-overlay org-speedbar-restriction-lock-overlay
  19646. (point-at-bol) (point-at-eol))
  19647. (setq current-prefix-arg nil)
  19648. (org-agenda-maybe-redo)))
  19649. (eval-after-load "speedbar"
  19650. '(progn
  19651. (speedbar-add-supported-extension ".org")
  19652. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  19653. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  19654. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  19655. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  19656. (add-hook 'speedbar-visiting-tag-hook
  19657. (lambda () (and (eq major-mode 'org-mode) (org-show-context 'org-goto))))))
  19658. ;;; Fixes and Hacks for problems with other packages
  19659. ;; Make flyspell not check words in links, to not mess up our keymap
  19660. (defun org-mode-flyspell-verify ()
  19661. "Don't let flyspell put overlays at active buttons, or on
  19662. {todo,all-time,additional-option-like}-keywords."
  19663. (let ((pos (max (1- (point)) (point-min)))
  19664. (word (thing-at-point 'word)))
  19665. (and (not (get-text-property pos 'keymap))
  19666. (not (get-text-property pos 'org-no-flyspell))
  19667. (not (member word org-todo-keywords-1))
  19668. (not (member word org-all-time-keywords))
  19669. (not (member word org-additional-option-like-keywords)))))
  19670. (defun org-remove-flyspell-overlays-in (beg end)
  19671. "Remove flyspell overlays in region."
  19672. (and (org-bound-and-true-p flyspell-mode)
  19673. (fboundp 'flyspell-delete-region-overlays)
  19674. (flyspell-delete-region-overlays beg end))
  19675. (add-text-properties beg end '(org-no-flyspell t)))
  19676. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  19677. (eval-after-load "bookmark"
  19678. '(if (boundp 'bookmark-after-jump-hook)
  19679. ;; We can use the hook
  19680. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  19681. ;; Hook not available, use advice
  19682. (defadvice bookmark-jump (after org-make-visible activate)
  19683. "Make the position visible."
  19684. (org-bookmark-jump-unhide))))
  19685. ;; Make sure saveplace shows the location if it was hidden
  19686. (eval-after-load "saveplace"
  19687. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  19688. "Make the position visible."
  19689. (org-bookmark-jump-unhide)))
  19690. ;; Make sure ecb shows the location if it was hidden
  19691. (eval-after-load "ecb"
  19692. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  19693. "Make hierarchy visible when jumping into location from ECB tree buffer."
  19694. (if (eq major-mode 'org-mode)
  19695. (org-show-context))))
  19696. (defun org-bookmark-jump-unhide ()
  19697. "Unhide the current position, to show the bookmark location."
  19698. (and (eq major-mode 'org-mode)
  19699. (or (outline-invisible-p)
  19700. (save-excursion (goto-char (max (point-min) (1- (point))))
  19701. (outline-invisible-p)))
  19702. (org-show-context 'bookmark-jump)))
  19703. ;; Make session.el ignore our circular variable
  19704. (eval-after-load "session"
  19705. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  19706. ;;;; Experimental code
  19707. (defun org-closed-in-range ()
  19708. "Sparse tree of items closed in a certain time range.
  19709. Still experimental, may disappear in the future."
  19710. (interactive)
  19711. ;; Get the time interval from the user.
  19712. (let* ((time1 (org-float-time
  19713. (org-read-date nil 'to-time nil "Starting date: ")))
  19714. (time2 (org-float-time
  19715. (org-read-date nil 'to-time nil "End date:")))
  19716. ;; callback function
  19717. (callback (lambda ()
  19718. (let ((time
  19719. (org-float-time
  19720. (apply 'encode-time
  19721. (org-parse-time-string
  19722. (match-string 1))))))
  19723. ;; check if time in interval
  19724. (and (>= time time1) (<= time time2))))))
  19725. ;; make tree, check each match with the callback
  19726. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  19727. ;;;; Finish up
  19728. (provide 'org)
  19729. (run-hooks 'org-load-hook)
  19730. ;;; org.el ends here