org.el 805 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417
  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.03
  10. ;;
  11. ;; This file is part of GNU Emacs.
  12. ;;
  13. ;; GNU Emacs is free software: you can redistribute it and/or modify
  14. ;; it under the terms of the GNU General Public License as published by
  15. ;; the Free Software Foundation, either version 3 of the License, or
  16. ;; (at your option) any later version.
  17. ;; GNU Emacs is distributed in the hope that it will be useful,
  18. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. ;; GNU General Public License for more details.
  21. ;; You should have received a copy of the GNU General Public License
  22. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  23. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  24. ;;
  25. ;;; Commentary:
  26. ;;
  27. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  28. ;; project planning with a fast and effective plain-text system.
  29. ;;
  30. ;; Org-mode develops organizational tasks around NOTES files that contain
  31. ;; information about projects as plain text. Org-mode is implemented on
  32. ;; top of outline-mode, which makes it possible to keep the content of
  33. ;; large files well structured. Visibility cycling and structure editing
  34. ;; help to work with the tree. Tables are easily created with a built-in
  35. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  36. ;; and scheduling. It dynamically compiles entries into an agenda that
  37. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  38. ;; Plain text URL-like links connect to websites, emails, Usenet
  39. ;; messages, BBDB entries, and any files related to the projects. For
  40. ;; printing and sharing of notes, an Org-mode file can be exported as a
  41. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  42. ;; iCalendar file. It can also serve as a publishing tool for a set of
  43. ;; linked webpages.
  44. ;;
  45. ;; Installation and Activation
  46. ;; ---------------------------
  47. ;; See the corresponding sections in the manual at
  48. ;;
  49. ;; http://orgmode.org/org.html#Installation
  50. ;;
  51. ;; Documentation
  52. ;; -------------
  53. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  54. ;; distribution also contains a PDF version of it. At the homepage of
  55. ;; Org-mode, you can read the same text online as HTML. There is also an
  56. ;; excellent reference card made by Philip Rooke. This card can be found
  57. ;; in the etc/ directory of Emacs 22.
  58. ;;
  59. ;; A list of recent changes can be found at
  60. ;; http://orgmode.org/Changes.html
  61. ;;
  62. ;;; Code:
  63. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  64. (defvar org-table-formula-constants-local nil
  65. "Local version of `org-table-formula-constants'.")
  66. (make-variable-buffer-local 'org-table-formula-constants-local)
  67. ;;;; Require other packages
  68. (eval-when-compile
  69. (require 'cl)
  70. (require 'gnus-sum))
  71. (require 'calendar)
  72. ;; Emacs 22 calendar compatibility: Make sure the new variables are available
  73. (when (fboundp 'defvaralias)
  74. (unless (boundp 'calendar-view-holidays-initially-flag)
  75. (defvaralias 'calendar-view-holidays-initially-flag
  76. 'view-calendar-holidays-initially))
  77. (unless (boundp 'calendar-view-diary-initially-flag)
  78. (defvaralias 'calendar-view-diary-initially-flag
  79. 'view-diary-entries-initially))
  80. (unless (boundp 'diary-fancy-buffer)
  81. (defvaralias 'diary-fancy-buffer 'fancy-diary-buffer)))
  82. (require 'outline) (require 'noutline)
  83. ;; Other stuff we need.
  84. (require 'time-date)
  85. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  86. (require 'easymenu)
  87. (require 'overlay)
  88. (require 'org-macs)
  89. (require 'org-entities)
  90. (require 'org-compat)
  91. (require 'org-faces)
  92. (require 'org-list)
  93. (require 'org-pcomplete)
  94. (require 'org-src)
  95. (require 'org-footnote)
  96. (declare-function org-inlinetask-at-task-p "org-inlinetask" ())
  97. (declare-function org-inlinetask-outline-regexp "org-inlinetask" ())
  98. (declare-function org-inlinetask-toggle-visibility "org-inlinetask" ())
  99. (declare-function org-pop-to-buffer-same-window "org-compat" (&optional buffer-or-name norecord label))
  100. (declare-function org-at-clock-log-p "org-clock" ())
  101. (declare-function org-clock-timestamps-up "org-clock" ())
  102. (declare-function org-clock-timestamps-down "org-clock" ())
  103. ;; babel
  104. (require 'ob)
  105. (require 'ob-table)
  106. (require 'ob-lob)
  107. (require 'ob-ref)
  108. (require 'ob-tangle)
  109. (require 'ob-comint)
  110. (require 'ob-keys)
  111. ;; load languages based on value of `org-babel-load-languages'
  112. (defvar org-babel-load-languages)
  113. ;;;###autoload
  114. (defun org-babel-do-load-languages (sym value)
  115. "Load the languages defined in `org-babel-load-languages'."
  116. (set-default sym value)
  117. (mapc (lambda (pair)
  118. (let ((active (cdr pair)) (lang (symbol-name (car pair))))
  119. (if active
  120. (progn
  121. (require (intern (concat "ob-" lang))))
  122. (progn
  123. (funcall 'fmakunbound
  124. (intern (concat "org-babel-execute:" lang)))
  125. (funcall 'fmakunbound
  126. (intern (concat "org-babel-expand-body:" lang)))))))
  127. org-babel-load-languages))
  128. (defcustom org-babel-load-languages '((emacs-lisp . t))
  129. "Languages which can be evaluated in Org-mode buffers.
  130. This list can be used to load support for any of the languages
  131. below, note that each language will depend on a different set of
  132. system executables and/or Emacs modes. When a language is
  133. \"loaded\", then code blocks in that language can be evaluated
  134. with `org-babel-execute-src-block' bound by default to C-c
  135. C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
  136. be set to remove code block evaluation from the C-c C-c
  137. keybinding. By default only Emacs Lisp (which has no
  138. requirements) is loaded."
  139. :group 'org-babel
  140. :set 'org-babel-do-load-languages
  141. :version "24.1"
  142. :type '(alist :tag "Babel Languages"
  143. :key-type
  144. (choice
  145. (const :tag "Awk" awk)
  146. (const :tag "C" C)
  147. (const :tag "R" R)
  148. (const :tag "Asymptote" asymptote)
  149. (const :tag "Calc" calc)
  150. (const :tag "Clojure" clojure)
  151. (const :tag "CSS" css)
  152. (const :tag "Ditaa" ditaa)
  153. (const :tag "Dot" dot)
  154. (const :tag "Emacs Lisp" emacs-lisp)
  155. (const :tag "Fortran" fortran)
  156. (const :tag "Gnuplot" gnuplot)
  157. (const :tag "Haskell" haskell)
  158. (const :tag "IO" io)
  159. (const :tag "Java" java)
  160. (const :tag "Javascript" js)
  161. (const :tag "LaTeX" latex)
  162. (const :tag "Ledger" ledger)
  163. (const :tag "Lilypond" lilypond)
  164. (const :tag "Maxima" maxima)
  165. (const :tag "Matlab" matlab)
  166. (const :tag "Mscgen" mscgen)
  167. (const :tag "Ocaml" ocaml)
  168. (const :tag "Octave" octave)
  169. (const :tag "Org" org)
  170. (const :tag "Perl" perl)
  171. (const :tag "Pico Lisp" picolisp)
  172. (const :tag "PlantUML" plantuml)
  173. (const :tag "Python" python)
  174. (const :tag "Ruby" ruby)
  175. (const :tag "Sass" sass)
  176. (const :tag "Scala" scala)
  177. (const :tag "Scheme" scheme)
  178. (const :tag "Screen" screen)
  179. (const :tag "Shell Script" sh)
  180. (const :tag "Shen" shen)
  181. (const :tag "Sql" sql)
  182. (const :tag "Sqlite" sqlite))
  183. :value-type (boolean :tag "Activate" :value t)))
  184. ;;;; Customization variables
  185. (defcustom org-clone-delete-id nil
  186. "Remove ID property of clones of a subtree.
  187. When non-nil, clones of a subtree don't inherit the ID property.
  188. Otherwise they inherit the ID property with a new unique
  189. identifier."
  190. :type 'boolean
  191. :version "24.1"
  192. :group 'org-id)
  193. ;;; Version
  194. (defconst org-version "7.8.03"
  195. "The version number of the file org.el.")
  196. (defun org-version (&optional here)
  197. "Show the org-mode version in the echo area.
  198. With prefix arg HERE, insert it at point."
  199. (interactive "P")
  200. (let* ((origin default-directory)
  201. (version org-version)
  202. (git-version)
  203. (dir (concat (file-name-directory (locate-library "org")) "../" )))
  204. (when (and (file-exists-p (expand-file-name ".git" dir))
  205. (executable-find "git"))
  206. (unwind-protect
  207. (progn
  208. (cd dir)
  209. (when (eql 0 (shell-command "git describe --abbrev=4 HEAD"))
  210. (with-current-buffer "*Shell Command Output*"
  211. (goto-char (point-min))
  212. (setq git-version (buffer-substring (point) (point-at-eol))))
  213. (subst-char-in-string ?- ?. git-version t)
  214. (when (string-match "\\S-"
  215. (shell-command-to-string
  216. "git diff-index --name-only HEAD --"))
  217. (setq git-version (concat git-version ".dirty")))
  218. (setq version (concat version " (" git-version ")"))))
  219. (cd origin)))
  220. (setq version (format "Org-mode version %s" version))
  221. (if here (insert version))
  222. (message version)))
  223. ;;; Compatibility constants
  224. ;;; The custom variables
  225. (defgroup org nil
  226. "Outline-based notes management and organizer."
  227. :tag "Org"
  228. :group 'outlines
  229. :group 'calendar)
  230. (defcustom org-mode-hook nil
  231. "Mode hook for Org-mode, run after the mode was turned on."
  232. :group 'org
  233. :type 'hook)
  234. (defcustom org-load-hook nil
  235. "Hook that is run after org.el has been loaded."
  236. :group 'org
  237. :type 'hook)
  238. (defcustom org-log-buffer-setup-hook nil
  239. "Hook that is run after an Org log buffer is created."
  240. :group 'org
  241. :version "24.1"
  242. :type 'hook)
  243. (defvar org-modules) ; defined below
  244. (defvar org-modules-loaded nil
  245. "Have the modules been loaded already?")
  246. (defun org-load-modules-maybe (&optional force)
  247. "Load all extensions listed in `org-modules'."
  248. (when (or force (not org-modules-loaded))
  249. (mapc (lambda (ext)
  250. (condition-case nil (require ext)
  251. (error (message "Problems while trying to load feature `%s'" ext))))
  252. org-modules)
  253. (setq org-modules-loaded t)))
  254. (defun org-set-modules (var value)
  255. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  256. (set var value)
  257. (when (featurep 'org)
  258. (org-load-modules-maybe 'force)))
  259. (when (org-bound-and-true-p org-modules)
  260. (let ((a (member 'org-infojs org-modules)))
  261. (and a (setcar a 'org-jsinfo))))
  262. (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)
  263. "Modules that should always be loaded together with org.el.
  264. If a description starts with <C>, the file is not part of Emacs
  265. and loading it will require that you have downloaded and properly installed
  266. the org-mode distribution.
  267. You can also use this system to load external packages (i.e. neither Org
  268. core modules, nor modules from the CONTRIB directory). Just add symbols
  269. to the end of the list. If the package is called org-xyz.el, then you need
  270. to add the symbol `xyz', and the package must have a call to
  271. (provide 'org-xyz)"
  272. :group 'org
  273. :set 'org-set-modules
  274. :type
  275. '(set :greedy t
  276. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  277. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  278. (const :tag " crypt: Encryption of subtrees" org-crypt)
  279. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  280. (const :tag " docview: Links to doc-view buffers" org-docview)
  281. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  282. (const :tag " id: Global IDs for identifying entries" org-id)
  283. (const :tag " info: Links to Info nodes" org-info)
  284. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  285. (const :tag " habit: Track your consistency with habits" org-habit)
  286. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  287. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  288. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  289. (const :tag " mew Links to Mew folders/messages" org-mew)
  290. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  291. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  292. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  293. (const :tag " special-blocks: Turn blocks into LaTeX envs and HTML divs" org-special-blocks)
  294. (const :tag " vm: Links to VM folders/messages" org-vm)
  295. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  296. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  297. (const :tag " mouse: Additional mouse support" org-mouse)
  298. (const :tag " TaskJuggler: Export tasks to a TaskJuggler project" org-taskjuggler)
  299. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  300. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  301. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  302. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  303. (const :tag "C collector: Collect properties into tables" org-collector)
  304. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  305. (const :tag "C drill: Flashcards and spaced repetition for Org-mode" org-drill)
  306. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  307. (const :tag "C eshell Support for links to working directories in eshell" org-eshell)
  308. (const :tag "C eval: Include command output as text" org-eval)
  309. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  310. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  311. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  312. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  313. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  314. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  315. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  316. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  317. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  318. (const :tag "C notmuch: Provide org links to notmuch searches or messages" org-notmuch)
  319. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  320. (const :tag "C mac-link-grabber Grab links and URLs from various Mac applications" org-mac-link-grabber)
  321. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  322. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  323. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  324. (const :tag "C registry: A registry for Org-mode links" org-registry)
  325. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  326. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  327. (const :tag "C secretary: Team management with org-mode" org-secretary)
  328. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  329. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  330. (const :tag "C track: Keep up with Org-mode development" org-track)
  331. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  332. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  333. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  334. (defcustom org-support-shift-select nil
  335. "Non-nil means make shift-cursor commands select text when possible.
  336. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
  337. start selecting a region, or enlarge regions started in this way.
  338. In Org-mode, in special contexts, these same keys are used for
  339. other purposes, important enough to compete with shift selection.
  340. Org tries to balance these needs by supporting `shift-select-mode'
  341. outside these special contexts, under control of this variable.
  342. The default of this variable is nil, to avoid confusing behavior. Shifted
  343. cursor keys will then execute Org commands in the following contexts:
  344. - on a headline, changing TODO state (left/right) and priority (up/down)
  345. - on a time stamp, changing the time
  346. - in a plain list item, changing the bullet type
  347. - in a property definition line, switching between allowed values
  348. - in the BEGIN line of a clock table (changing the time block).
  349. Outside these contexts, the commands will throw an error.
  350. When this variable is t and the cursor is not in a special
  351. context, Org-mode will support shift-selection for making and
  352. enlarging regions. To make this more effective, the bullet
  353. cycling will no longer happen anywhere in an item line, but only
  354. if the cursor is exactly on the bullet.
  355. If you set this variable to the symbol `always', then the keys
  356. will not be special in headlines, property lines, and item lines,
  357. to make shift selection work there as well. If this is what you
  358. want, you can use the following alternative commands: `C-c C-t'
  359. and `C-c ,' to change TODO state and priority, `C-u C-u C-c C-t'
  360. can be used to switch TODO sets, `C-c -' to cycle item bullet
  361. types, and properties can be edited by hand or in column view.
  362. However, when the cursor is on a timestamp, shift-cursor commands
  363. will still edit the time stamp - this is just too good to give up.
  364. XEmacs user should have this variable set to nil, because
  365. `shift-select-mode' is in Emacs 23 or later only."
  366. :group 'org
  367. :type '(choice
  368. (const :tag "Never" nil)
  369. (const :tag "When outside special context" t)
  370. (const :tag "Everywhere except timestamps" always)))
  371. (defcustom org-loop-over-headlines-in-active-region nil
  372. "Shall some commands act upon headlines in the active region?
  373. When set to `t', some commands will be performed in all headlines
  374. within the active region.
  375. When set to `start-level', some commands will be performed in all
  376. headlines within the active region, provided that these headlines
  377. are of the same level than the first one.
  378. When set to a string, those commands will be performed on the
  379. matching headlines within the active region. Such string must be
  380. a tags/property/todo match as it is used in the agenda tags view.
  381. The list of commands is: `org-schedule', `org-deadline',
  382. `org-todo', `org-archive-subtree', `org-archive-set-tag' and
  383. `org-archive-to-archive-sibling'. The archiving commands skip
  384. already archived entries."
  385. :type '(choice (const :tag "Don't loop" nil)
  386. (const :tag "All headlines in active region" t)
  387. (const :tag "In active region, headlines at the same level than the first one" 'start-level)
  388. (string :tag "Tags/Property/Todo matcher"))
  389. :version "24.1"
  390. :group 'org-todo
  391. :group 'org-archive)
  392. (defgroup org-startup nil
  393. "Options concerning startup of Org-mode."
  394. :tag "Org Startup"
  395. :group 'org)
  396. (defcustom org-startup-folded t
  397. "Non-nil means entering Org-mode will switch to OVERVIEW.
  398. This can also be configured on a per-file basis by adding one of
  399. the following lines anywhere in the buffer:
  400. #+STARTUP: fold (or `overview', this is equivalent)
  401. #+STARTUP: nofold (or `showall', this is equivalent)
  402. #+STARTUP: content
  403. #+STARTUP: showeverything"
  404. :group 'org-startup
  405. :type '(choice
  406. (const :tag "nofold: show all" nil)
  407. (const :tag "fold: overview" t)
  408. (const :tag "content: all headlines" content)
  409. (const :tag "show everything, even drawers" showeverything)))
  410. (defcustom org-startup-truncated t
  411. "Non-nil means entering Org-mode will set `truncate-lines'.
  412. This is useful since some lines containing links can be very long and
  413. uninteresting. Also tables look terrible when wrapped."
  414. :group 'org-startup
  415. :type 'boolean)
  416. (defcustom org-startup-indented nil
  417. "Non-nil means turn on `org-indent-mode' on startup.
  418. This can also be configured on a per-file basis by adding one of
  419. the following lines anywhere in the buffer:
  420. #+STARTUP: indent
  421. #+STARTUP: noindent"
  422. :group 'org-structure
  423. :type '(choice
  424. (const :tag "Not" nil)
  425. (const :tag "Globally (slow on startup in large files)" t)))
  426. (defcustom org-use-sub-superscripts t
  427. "Non-nil means interpret \"_\" and \"^\" for export.
  428. When this option is turned on, you can use TeX-like syntax for sub- and
  429. superscripts. Several characters after \"_\" or \"^\" will be
  430. considered as a single item - so grouping with {} is normally not
  431. needed. For example, the following things will be parsed as single
  432. sub- or superscripts.
  433. 10^24 or 10^tau several digits will be considered 1 item.
  434. 10^-12 or 10^-tau a leading sign with digits or a word
  435. x^2-y^3 will be read as x^2 - y^3, because items are
  436. terminated by almost any nonword/nondigit char.
  437. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  438. Still, ambiguity is possible - so when in doubt use {} to enclose the
  439. sub/superscript. If you set this variable to the symbol `{}',
  440. the braces are *required* in order to trigger interpretations as
  441. sub/superscript. This can be helpful in documents that need \"_\"
  442. frequently in plain text.
  443. Not all export backends support this, but HTML does.
  444. This option can also be set with the +OPTIONS line, e.g. \"^:nil\"."
  445. :group 'org-startup
  446. :group 'org-export-translation
  447. :version "24.1"
  448. :type '(choice
  449. (const :tag "Always interpret" t)
  450. (const :tag "Only with braces" {})
  451. (const :tag "Never interpret" nil)))
  452. (if (fboundp 'defvaralias)
  453. (defvaralias 'org-export-with-sub-superscripts 'org-use-sub-superscripts))
  454. (defcustom org-startup-with-beamer-mode nil
  455. "Non-nil means turn on `org-beamer-mode' on startup.
  456. This can also be configured on a per-file basis by adding one of
  457. the following lines anywhere in the buffer:
  458. #+STARTUP: beamer"
  459. :group 'org-startup
  460. :version "24.1"
  461. :type 'boolean)
  462. (defcustom org-startup-align-all-tables nil
  463. "Non-nil means align all tables when visiting a file.
  464. This is useful when the column width in tables is forced with <N> cookies
  465. in table fields. Such tables will look correct only after the first re-align.
  466. This can also be configured on a per-file basis by adding one of
  467. the following lines anywhere in the buffer:
  468. #+STARTUP: align
  469. #+STARTUP: noalign"
  470. :group 'org-startup
  471. :type 'boolean)
  472. (defcustom org-startup-with-inline-images nil
  473. "Non-nil means show inline images when loading a new Org file.
  474. This can also be configured on a per-file basis by adding one of
  475. the following lines anywhere in the buffer:
  476. #+STARTUP: inlineimages
  477. #+STARTUP: noinlineimages"
  478. :group 'org-startup
  479. :version "24.1"
  480. :type 'boolean)
  481. (defcustom org-insert-mode-line-in-empty-file nil
  482. "Non-nil means insert the first line setting Org-mode in empty files.
  483. When the function `org-mode' is called interactively in an empty file, this
  484. normally means that the file name does not automatically trigger Org-mode.
  485. To ensure that the file will always be in Org-mode in the future, a
  486. line enforcing Org-mode will be inserted into the buffer, if this option
  487. has been set."
  488. :group 'org-startup
  489. :type 'boolean)
  490. (defcustom org-replace-disputed-keys nil
  491. "Non-nil means use alternative key bindings for some keys.
  492. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  493. These keys are also used by other packages like shift-selection-mode'
  494. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  495. If you want to use Org-mode together with one of these other modes,
  496. or more generally if you would like to move some Org-mode commands to
  497. other keys, set this variable and configure the keys with the variable
  498. `org-disputed-keys'.
  499. This option is only relevant at load-time of Org-mode, and must be set
  500. *before* org.el is loaded. Changing it requires a restart of Emacs to
  501. become effective."
  502. :group 'org-startup
  503. :type 'boolean)
  504. (defcustom org-use-extra-keys nil
  505. "Non-nil means use extra key sequence definitions for certain commands.
  506. This happens automatically if you run XEmacs or if `window-system'
  507. is nil. This variable lets you do the same manually. You must
  508. set it before loading org.
  509. Example: on Carbon Emacs 22 running graphically, with an external
  510. keyboard on a Powerbook, the default way of setting M-left might
  511. not work for either Alt or ESC. Setting this variable will make
  512. it work for ESC."
  513. :group 'org-startup
  514. :type 'boolean)
  515. (if (fboundp 'defvaralias)
  516. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  517. (defcustom org-disputed-keys
  518. '(([(shift up)] . [(meta p)])
  519. ([(shift down)] . [(meta n)])
  520. ([(shift left)] . [(meta -)])
  521. ([(shift right)] . [(meta +)])
  522. ([(control shift right)] . [(meta shift +)])
  523. ([(control shift left)] . [(meta shift -)]))
  524. "Keys for which Org-mode and other modes compete.
  525. This is an alist, cars are the default keys, second element specifies
  526. the alternative to use when `org-replace-disputed-keys' is t.
  527. Keys can be specified in any syntax supported by `define-key'.
  528. The value of this option takes effect only at Org-mode's startup,
  529. therefore you'll have to restart Emacs to apply it after changing."
  530. :group 'org-startup
  531. :type 'alist)
  532. (defun org-key (key)
  533. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  534. Or return the original if not disputed.
  535. Also apply the translations defined in `org-xemacs-key-equivalents'."
  536. (when org-replace-disputed-keys
  537. (let* ((nkey (key-description key))
  538. (x (org-find-if (lambda (x)
  539. (equal (key-description (car x)) nkey))
  540. org-disputed-keys)))
  541. (setq key (if x (cdr x) key))))
  542. (when (featurep 'xemacs)
  543. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  544. key)
  545. (defun org-find-if (predicate seq)
  546. (catch 'exit
  547. (while seq
  548. (if (funcall predicate (car seq))
  549. (throw 'exit (car seq))
  550. (pop seq)))))
  551. (defun org-defkey (keymap key def)
  552. "Define a key, possibly translated, as returned by `org-key'."
  553. (define-key keymap (org-key key) def))
  554. (defcustom org-ellipsis nil
  555. "The ellipsis to use in the Org-mode outline.
  556. When nil, just use the standard three dots. When a string, use that instead,
  557. When a face, use the standard 3 dots, but with the specified face.
  558. The change affects only Org-mode (which will then use its own display table).
  559. Changing this requires executing `M-x org-mode' in a buffer to become
  560. effective."
  561. :group 'org-startup
  562. :type '(choice (const :tag "Default" nil)
  563. (face :tag "Face" :value org-warning)
  564. (string :tag "String" :value "...#")))
  565. (defvar org-display-table nil
  566. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  567. (defgroup org-keywords nil
  568. "Keywords in Org-mode."
  569. :tag "Org Keywords"
  570. :group 'org)
  571. (defcustom org-deadline-string "DEADLINE:"
  572. "String to mark deadline entries.
  573. A deadline is this string, followed by a time stamp. Should be a word,
  574. terminated by a colon. You can insert a schedule keyword and
  575. a timestamp with \\[org-deadline].
  576. Changes become only effective after restarting Emacs."
  577. :group 'org-keywords
  578. :type 'string)
  579. (defcustom org-scheduled-string "SCHEDULED:"
  580. "String to mark scheduled TODO entries.
  581. A schedule is this string, followed by a time stamp. Should be a word,
  582. terminated by a colon. You can insert a schedule keyword and
  583. a timestamp with \\[org-schedule].
  584. Changes become only effective after restarting Emacs."
  585. :group 'org-keywords
  586. :type 'string)
  587. (defcustom org-closed-string "CLOSED:"
  588. "String used as the prefix for timestamps logging closing a TODO entry."
  589. :group 'org-keywords
  590. :type 'string)
  591. (defcustom org-clock-string "CLOCK:"
  592. "String used as prefix for timestamps clocking work hours on an item."
  593. :group 'org-keywords
  594. :type 'string)
  595. (defcustom org-comment-string "COMMENT"
  596. "Entries starting with this keyword will never be exported.
  597. An entry can be toggled between COMMENT and normal with
  598. \\[org-toggle-comment].
  599. Changes become only effective after restarting Emacs."
  600. :group 'org-keywords
  601. :type 'string)
  602. (defcustom org-quote-string "QUOTE"
  603. "Entries starting with this keyword will be exported in fixed-width font.
  604. Quoting applies only to the text in the entry following the headline, and does
  605. not extend beyond the next headline, even if that is lower level.
  606. An entry can be toggled between QUOTE and normal with
  607. \\[org-toggle-fixed-width-section]."
  608. :group 'org-keywords
  609. :type 'string)
  610. (defconst org-repeat-re
  611. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)"
  612. "Regular expression for specifying repeated events.
  613. After a match, group 1 contains the repeat expression.")
  614. (defgroup org-structure nil
  615. "Options concerning the general structure of Org-mode files."
  616. :tag "Org Structure"
  617. :group 'org)
  618. (defgroup org-reveal-location nil
  619. "Options about how to make context of a location visible."
  620. :tag "Org Reveal Location"
  621. :group 'org-structure)
  622. (defconst org-context-choice
  623. '(choice
  624. (const :tag "Always" t)
  625. (const :tag "Never" nil)
  626. (repeat :greedy t :tag "Individual contexts"
  627. (cons
  628. (choice :tag "Context"
  629. (const agenda)
  630. (const org-goto)
  631. (const occur-tree)
  632. (const tags-tree)
  633. (const link-search)
  634. (const mark-goto)
  635. (const bookmark-jump)
  636. (const isearch)
  637. (const default))
  638. (boolean))))
  639. "Contexts for the reveal options.")
  640. (defcustom org-show-hierarchy-above '((default . t))
  641. "Non-nil means show full hierarchy when revealing a location.
  642. Org-mode often shows locations in an org-mode file which might have
  643. been invisible before. When this is set, the hierarchy of headings
  644. above the exposed location is shown.
  645. Turning this off for example for sparse trees makes them very compact.
  646. Instead of t, this can also be an alist specifying this option for different
  647. contexts. Valid contexts are
  648. agenda when exposing an entry from the agenda
  649. org-goto when using the command `org-goto' on key C-c C-j
  650. occur-tree when using the command `org-occur' on key C-c /
  651. tags-tree when constructing a sparse tree based on tags matches
  652. link-search when exposing search matches associated with a link
  653. mark-goto when exposing the jump goal of a mark
  654. bookmark-jump when exposing a bookmark location
  655. isearch when exiting from an incremental search
  656. default default for all contexts not set explicitly"
  657. :group 'org-reveal-location
  658. :type org-context-choice)
  659. (defcustom org-show-following-heading '((default . nil))
  660. "Non-nil means show following heading when revealing a location.
  661. Org-mode often shows locations in an org-mode file which might have
  662. been invisible before. When this is set, the heading following the
  663. match is shown.
  664. Turning this off for example for sparse trees makes them very compact,
  665. but makes it harder to edit the location of the match. In such a case,
  666. use the command \\[org-reveal] to show more context.
  667. Instead of t, this can also be an alist specifying this option for different
  668. contexts. See `org-show-hierarchy-above' for valid contexts."
  669. :group 'org-reveal-location
  670. :type org-context-choice)
  671. (defcustom org-show-siblings '((default . nil) (isearch t))
  672. "Non-nil means show all sibling heading when revealing a location.
  673. Org-mode often shows locations in an org-mode file which might have
  674. been invisible before. When this is set, the sibling of the current entry
  675. heading are all made visible. If `org-show-hierarchy-above' is t,
  676. the same happens on each level of the hierarchy above the current entry.
  677. By default this is on for the isearch context, off for all other contexts.
  678. Turning this off for example for sparse trees makes them very compact,
  679. but makes it harder to edit the location of the match. In such a case,
  680. use the command \\[org-reveal] to show more context.
  681. Instead of t, this can also be an alist specifying this option for different
  682. contexts. See `org-show-hierarchy-above' for valid contexts."
  683. :group 'org-reveal-location
  684. :type org-context-choice)
  685. (defcustom org-show-entry-below '((default . nil))
  686. "Non-nil means show the entry below a headline when revealing a location.
  687. Org-mode often shows locations in an org-mode file which might have
  688. been invisible before. When this is set, the text below the headline that is
  689. exposed is also shown.
  690. By default this is off for all contexts.
  691. Instead of t, this can also be an alist specifying this option for different
  692. contexts. See `org-show-hierarchy-above' for valid contexts."
  693. :group 'org-reveal-location
  694. :type org-context-choice)
  695. (defcustom org-indirect-buffer-display 'other-window
  696. "How should indirect tree buffers be displayed?
  697. This applies to indirect buffers created with the commands
  698. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  699. Valid values are:
  700. current-window Display in the current window
  701. other-window Just display in another window.
  702. dedicated-frame Create one new frame, and re-use it each time.
  703. new-frame Make a new frame each time. Note that in this case
  704. previously-made indirect buffers are kept, and you need to
  705. kill these buffers yourself."
  706. :group 'org-structure
  707. :group 'org-agenda-windows
  708. :type '(choice
  709. (const :tag "In current window" current-window)
  710. (const :tag "In current frame, other window" other-window)
  711. (const :tag "Each time a new frame" new-frame)
  712. (const :tag "One dedicated frame" dedicated-frame)))
  713. (defcustom org-use-speed-commands nil
  714. "Non-nil means activate single letter commands at beginning of a headline.
  715. This may also be a function to test for appropriate locations where speed
  716. commands should be active."
  717. :group 'org-structure
  718. :type '(choice
  719. (const :tag "Never" nil)
  720. (const :tag "At beginning of headline stars" t)
  721. (function)))
  722. (defcustom org-speed-commands-user nil
  723. "Alist of additional speed commands.
  724. This list will be checked before `org-speed-commands-default'
  725. when the variable `org-use-speed-commands' is non-nil
  726. and when the cursor is at the beginning of a headline.
  727. The car if each entry is a string with a single letter, which must
  728. be assigned to `self-insert-command' in the global map.
  729. The cdr is either a command to be called interactively, a function
  730. to be called, or a form to be evaluated.
  731. An entry that is just a list with a single string will be interpreted
  732. as a descriptive headline that will be added when listing the speed
  733. commands in the Help buffer using the `?' speed command."
  734. :group 'org-structure
  735. :type '(repeat :value ("k" . ignore)
  736. (choice :value ("k" . ignore)
  737. (list :tag "Descriptive Headline" (string :tag "Headline"))
  738. (cons :tag "Letter and Command"
  739. (string :tag "Command letter")
  740. (choice
  741. (function)
  742. (sexp))))))
  743. (defgroup org-cycle nil
  744. "Options concerning visibility cycling in Org-mode."
  745. :tag "Org Cycle"
  746. :group 'org-structure)
  747. (defcustom org-cycle-skip-children-state-if-no-children t
  748. "Non-nil means skip CHILDREN state in entries that don't have any."
  749. :group 'org-cycle
  750. :type 'boolean)
  751. (defcustom org-cycle-max-level nil
  752. "Maximum level which should still be subject to visibility cycling.
  753. Levels higher than this will, for cycling, be treated as text, not a headline.
  754. When `org-odd-levels-only' is set, a value of N in this variable actually
  755. means 2N-1 stars as the limiting headline.
  756. When nil, cycle all levels.
  757. Note that the limiting level of cycling is also influenced by
  758. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  759. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  760. than its value."
  761. :group 'org-cycle
  762. :type '(choice
  763. (const :tag "No limit" nil)
  764. (integer :tag "Maximum level")))
  765. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK" "RESULTS")
  766. "Names of drawers. Drawers are not opened by cycling on the headline above.
  767. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  768. this:
  769. :DRAWERNAME:
  770. .....
  771. :END:
  772. The drawer \"PROPERTIES\" is special for capturing properties through
  773. the property API.
  774. Drawers can be defined on the per-file basis with a line like:
  775. #+DRAWERS: HIDDEN STATE PROPERTIES"
  776. :group 'org-structure
  777. :group 'org-cycle
  778. :type '(repeat (string :tag "Drawer Name")))
  779. (defcustom org-hide-block-startup nil
  780. "Non-nil means entering Org-mode will fold all blocks.
  781. This can also be set in on a per-file basis with
  782. #+STARTUP: hideblocks
  783. #+STARTUP: showblocks"
  784. :group 'org-startup
  785. :group 'org-cycle
  786. :type 'boolean)
  787. (defcustom org-cycle-global-at-bob nil
  788. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  789. This makes it possible to do global cycling without having to use S-TAB or
  790. \\[universal-argument] TAB. For this special case to work, the first line \
  791. of the buffer
  792. must not be a headline - it may be empty or some other text. When used in
  793. this way, `org-cycle-hook' is disables temporarily, to make sure the
  794. cursor stays at the beginning of the buffer.
  795. When this option is nil, don't do anything special at the beginning
  796. of the buffer."
  797. :group 'org-cycle
  798. :type 'boolean)
  799. (defcustom org-cycle-level-after-item/entry-creation t
  800. "Non-nil means cycle entry level or item indentation in new empty entries.
  801. When the cursor is at the end of an empty headline, i.e with only stars
  802. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  803. and then all possible ancestor states, before returning to the original state.
  804. This makes data entry extremely fast: M-RET to create a new headline,
  805. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  806. When the cursor is at the end of an empty plain list item, one TAB will
  807. make it a subitem, two or more tabs will back up to make this an item
  808. higher up in the item hierarchy."
  809. :group 'org-cycle
  810. :type 'boolean)
  811. (defcustom org-cycle-emulate-tab t
  812. "Where should `org-cycle' emulate TAB.
  813. nil Never
  814. white Only in completely white lines
  815. whitestart Only at the beginning of lines, before the first non-white char
  816. t Everywhere except in headlines
  817. exc-hl-bol Everywhere except at the start of a headline
  818. If TAB is used in a place where it does not emulate TAB, the current subtree
  819. visibility is cycled."
  820. :group 'org-cycle
  821. :type '(choice (const :tag "Never" nil)
  822. (const :tag "Only in completely white lines" white)
  823. (const :tag "Before first char in a line" whitestart)
  824. (const :tag "Everywhere except in headlines" t)
  825. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  826. ))
  827. (defcustom org-cycle-separator-lines 2
  828. "Number of empty lines needed to keep an empty line between collapsed trees.
  829. If you leave an empty line between the end of a subtree and the following
  830. headline, this empty line is hidden when the subtree is folded.
  831. Org-mode will leave (exactly) one empty line visible if the number of
  832. empty lines is equal or larger to the number given in this variable.
  833. So the default 2 means at least 2 empty lines after the end of a subtree
  834. are needed to produce free space between a collapsed subtree and the
  835. following headline.
  836. If the number is negative, and the number of empty lines is at least -N,
  837. all empty lines are shown.
  838. Special case: when 0, never leave empty lines in collapsed view."
  839. :group 'org-cycle
  840. :type 'integer)
  841. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  842. (defcustom org-pre-cycle-hook nil
  843. "Hook that is run before visibility cycling is happening.
  844. The function(s) in this hook must accept a single argument which indicates
  845. the new state that will be set right after running this hook. The
  846. argument is a symbol. Before a global state change, it can have the values
  847. `overview', `content', or `all'. Before a local state change, it can have
  848. the values `folded', `children', or `subtree'."
  849. :group 'org-cycle
  850. :type 'hook)
  851. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  852. org-cycle-hide-drawers
  853. org-cycle-show-empty-lines
  854. org-optimize-window-after-visibility-change)
  855. "Hook that is run after `org-cycle' has changed the buffer visibility.
  856. The function(s) in this hook must accept a single argument which indicates
  857. the new state that was set by the most recent `org-cycle' command. The
  858. argument is a symbol. After a global state change, it can have the values
  859. `overview', `content', or `all'. After a local state change, it can have
  860. the values `folded', `children', or `subtree'."
  861. :group 'org-cycle
  862. :type 'hook)
  863. (defgroup org-edit-structure nil
  864. "Options concerning structure editing in Org-mode."
  865. :tag "Org Edit Structure"
  866. :group 'org-structure)
  867. (defcustom org-odd-levels-only nil
  868. "Non-nil means skip even levels and only use odd levels for the outline.
  869. This has the effect that two stars are being added/taken away in
  870. promotion/demotion commands. It also influences how levels are
  871. handled by the exporters.
  872. Changing it requires restart of `font-lock-mode' to become effective
  873. for fontification also in regions already fontified.
  874. You may also set this on a per-file basis by adding one of the following
  875. lines to the buffer:
  876. #+STARTUP: odd
  877. #+STARTUP: oddeven"
  878. :group 'org-edit-structure
  879. :group 'org-appearance
  880. :type 'boolean)
  881. (defcustom org-adapt-indentation t
  882. "Non-nil means adapt indentation to outline node level.
  883. When this variable is set, Org assumes that you write outlines by
  884. indenting text in each node to align with the headline (after the stars).
  885. The following issues are influenced by this variable:
  886. - When this is set and the *entire* text in an entry is indented, the
  887. indentation is increased by one space in a demotion command, and
  888. decreased by one in a promotion command. If any line in the entry
  889. body starts with text at column 0, indentation is not changed at all.
  890. - Property drawers and planning information is inserted indented when
  891. this variable s set. When nil, they will not be indented.
  892. - TAB indents a line relative to context. The lines below a headline
  893. will be indented when this variable is set.
  894. Note that this is all about true indentation, by adding and removing
  895. space characters. See also `org-indent.el' which does level-dependent
  896. indentation in a virtual way, i.e. at display time in Emacs."
  897. :group 'org-edit-structure
  898. :type 'boolean)
  899. (defcustom org-special-ctrl-a/e nil
  900. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  901. When t, `C-a' will bring back the cursor to the beginning of the
  902. headline text, i.e. after the stars and after a possible TODO
  903. keyword. In an item, this will be the position after bullet and
  904. check-box, if any. When the cursor is already at that position,
  905. another `C-a' will bring it to the beginning of the line.
  906. `C-e' will jump to the end of the headline, ignoring the presence
  907. of tags in the headline. A second `C-e' will then jump to the
  908. true end of the line, after any tags. This also means that, when
  909. this variable is non-nil, `C-e' also will never jump beyond the
  910. end of the heading of a folded section, i.e. not after the
  911. ellipses.
  912. When set to the symbol `reversed', the first `C-a' or `C-e' works
  913. normally, going to the true line boundary first. Only a directly
  914. following, identical keypress will bring the cursor to the
  915. special positions.
  916. This may also be a cons cell where the behavior for `C-a' and
  917. `C-e' is set separately."
  918. :group 'org-edit-structure
  919. :type '(choice
  920. (const :tag "off" nil)
  921. (const :tag "on: after stars/bullet and before tags first" t)
  922. (const :tag "reversed: true line boundary first" reversed)
  923. (cons :tag "Set C-a and C-e separately"
  924. (choice :tag "Special C-a"
  925. (const :tag "off" nil)
  926. (const :tag "on: after stars/bullet first" t)
  927. (const :tag "reversed: before stars/bullet first" reversed))
  928. (choice :tag "Special C-e"
  929. (const :tag "off" nil)
  930. (const :tag "on: before tags first" t)
  931. (const :tag "reversed: after tags first" reversed)))))
  932. (if (fboundp 'defvaralias)
  933. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  934. (defcustom org-special-ctrl-k nil
  935. "Non-nil means `C-k' will behave specially in headlines.
  936. When nil, `C-k' will call the default `kill-line' command.
  937. When t, the following will happen while the cursor is in the headline:
  938. - When the cursor is at the beginning of a headline, kill the entire
  939. line and possible the folded subtree below the line.
  940. - When in the middle of the headline text, kill the headline up to the tags.
  941. - When after the headline text, kill the tags."
  942. :group 'org-edit-structure
  943. :type 'boolean)
  944. (defcustom org-ctrl-k-protect-subtree nil
  945. "Non-nil means, do not delete a hidden subtree with C-k.
  946. When set to the symbol `error', simply throw an error when C-k is
  947. used to kill (part-of) a headline that has hidden text behind it.
  948. Any other non-nil value will result in a query to the user, if it is
  949. OK to kill that hidden subtree. When nil, kill without remorse."
  950. :group 'org-edit-structure
  951. :version "24.1"
  952. :type '(choice
  953. (const :tag "Do not protect hidden subtrees" nil)
  954. (const :tag "Protect hidden subtrees with a security query" t)
  955. (const :tag "Never kill a hidden subtree with C-k" error)))
  956. (defcustom org-catch-invisible-edits nil
  957. "Check if in invisible region before inserting or deleting a character.
  958. Valid values are:
  959. nil Do not check, so just do invisible edits.
  960. error Throw an error and do nothing.
  961. show Make point visible, and do the requested edit.
  962. show-and-error Make point visible, then throw an error and abort the edit.
  963. smart Make point visible, and do insertion/deletion if it is
  964. adjacent to visible text and the change feels predictable.
  965. Never delete a previously invisible character or add in the
  966. middle or right after an invisible region. Basically, this
  967. allows insertion and backward-delete right before ellipses.
  968. FIXME: maybe in this case we should not even show?"
  969. :group 'org-edit-structure
  970. :version "24.1"
  971. :type '(choice
  972. (const :tag "Do not check" nil)
  973. (const :tag "Throw error when trying to edit" error)
  974. (const :tag "Unhide, but do not do the edit" show-and-error)
  975. (const :tag "Show invisible part and do the edit" show)
  976. (const :tag "Be smart and do the right thing" smart)))
  977. (defcustom org-yank-folded-subtrees t
  978. "Non-nil means when yanking subtrees, fold them.
  979. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  980. it starts with a heading and all other headings in it are either children
  981. or siblings, then fold all the subtrees. However, do this only if no
  982. text after the yank would be swallowed into a folded tree by this action."
  983. :group 'org-edit-structure
  984. :type 'boolean)
  985. (defcustom org-yank-adjusted-subtrees nil
  986. "Non-nil means when yanking subtrees, adjust the level.
  987. With this setting, `org-paste-subtree' is used to insert the subtree, see
  988. this function for details."
  989. :group 'org-edit-structure
  990. :type 'boolean)
  991. (defcustom org-M-RET-may-split-line '((default . t))
  992. "Non-nil means M-RET will split the line at the cursor position.
  993. When nil, it will go to the end of the line before making a
  994. new line.
  995. You may also set this option in a different way for different
  996. contexts. Valid contexts are:
  997. headline when creating a new headline
  998. item when creating a new item
  999. table in a table field
  1000. default the value to be used for all contexts not explicitly
  1001. customized"
  1002. :group 'org-structure
  1003. :group 'org-table
  1004. :type '(choice
  1005. (const :tag "Always" t)
  1006. (const :tag "Never" nil)
  1007. (repeat :greedy t :tag "Individual contexts"
  1008. (cons
  1009. (choice :tag "Context"
  1010. (const headline)
  1011. (const item)
  1012. (const table)
  1013. (const default))
  1014. (boolean)))))
  1015. (defcustom org-insert-heading-respect-content nil
  1016. "Non-nil means insert new headings after the current subtree.
  1017. When nil, the new heading is created directly after the current line.
  1018. The commands \\[org-insert-heading-respect-content] and
  1019. \\[org-insert-todo-heading-respect-content] turn this variable on
  1020. for the duration of the command."
  1021. :group 'org-structure
  1022. :type 'boolean)
  1023. (defcustom org-blank-before-new-entry '((heading . auto)
  1024. (plain-list-item . auto))
  1025. "Should `org-insert-heading' leave a blank line before new heading/item?
  1026. The value is an alist, with `heading' and `plain-list-item' as CAR,
  1027. and a boolean flag as CDR. The cdr may also be the symbol `auto', in
  1028. which case Org will look at the surrounding headings/items and try to
  1029. make an intelligent decision whether to insert a blank line or not.
  1030. For plain lists, if the variable `org-empty-line-terminates-plain-lists' is
  1031. set, the setting here is ignored and no empty line is inserted, to avoid
  1032. breaking the list structure."
  1033. :group 'org-edit-structure
  1034. :type '(list
  1035. (cons (const heading)
  1036. (choice (const :tag "Never" nil)
  1037. (const :tag "Always" t)
  1038. (const :tag "Auto" auto)))
  1039. (cons (const plain-list-item)
  1040. (choice (const :tag "Never" nil)
  1041. (const :tag "Always" t)
  1042. (const :tag "Auto" auto)))))
  1043. (defcustom org-insert-heading-hook nil
  1044. "Hook being run after inserting a new heading."
  1045. :group 'org-edit-structure
  1046. :type 'hook)
  1047. (defcustom org-enable-fixed-width-editor t
  1048. "Non-nil means lines starting with \":\" are treated as fixed-width.
  1049. This currently only means they are never auto-wrapped.
  1050. When nil, such lines will be treated like ordinary lines.
  1051. See also the QUOTE keyword."
  1052. :group 'org-edit-structure
  1053. :type 'boolean)
  1054. (defcustom org-goto-auto-isearch t
  1055. "Non-nil means typing characters in `org-goto' starts incremental search."
  1056. :group 'org-edit-structure
  1057. :type 'boolean)
  1058. (defgroup org-sparse-trees nil
  1059. "Options concerning sparse trees in Org-mode."
  1060. :tag "Org Sparse Trees"
  1061. :group 'org-structure)
  1062. (defcustom org-highlight-sparse-tree-matches t
  1063. "Non-nil means highlight all matches that define a sparse tree.
  1064. The highlights will automatically disappear the next time the buffer is
  1065. changed by an edit command."
  1066. :group 'org-sparse-trees
  1067. :type 'boolean)
  1068. (defcustom org-remove-highlights-with-change t
  1069. "Non-nil means any change to the buffer will remove temporary highlights.
  1070. Such highlights are created by `org-occur' and `org-clock-display'.
  1071. When nil, `C-c C-c needs to be used to get rid of the highlights.
  1072. The highlights created by `org-preview-latex-fragment' always need
  1073. `C-c C-c' to be removed."
  1074. :group 'org-sparse-trees
  1075. :group 'org-time
  1076. :type 'boolean)
  1077. (defcustom org-occur-hook '(org-first-headline-recenter)
  1078. "Hook that is run after `org-occur' has constructed a sparse tree.
  1079. This can be used to recenter the window to show as much of the structure
  1080. as possible."
  1081. :group 'org-sparse-trees
  1082. :type 'hook)
  1083. (defgroup org-imenu-and-speedbar nil
  1084. "Options concerning imenu and speedbar in Org-mode."
  1085. :tag "Org Imenu and Speedbar"
  1086. :group 'org-structure)
  1087. (defcustom org-imenu-depth 2
  1088. "The maximum level for Imenu access to Org-mode headlines.
  1089. This also applied for speedbar access."
  1090. :group 'org-imenu-and-speedbar
  1091. :type 'integer)
  1092. (defgroup org-table nil
  1093. "Options concerning tables in Org-mode."
  1094. :tag "Org Table"
  1095. :group 'org)
  1096. (defcustom org-enable-table-editor 'optimized
  1097. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1098. When nil, such lines will be treated like ordinary lines.
  1099. When equal to the symbol `optimized', the table editor will be optimized to
  1100. do the following:
  1101. - Automatic overwrite mode in front of whitespace in table fields.
  1102. This makes the structure of the table stay in tact as long as the edited
  1103. field does not exceed the column width.
  1104. - Minimize the number of realigns. Normally, the table is aligned each time
  1105. TAB or RET are pressed to move to another field. With optimization this
  1106. happens only if changes to a field might have changed the column width.
  1107. Optimization requires replacing the functions `self-insert-command',
  1108. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1109. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1110. very good at guessing when a re-align will be necessary, but you can always
  1111. force one with \\[org-ctrl-c-ctrl-c].
  1112. If you would like to use the optimized version in Org-mode, but the
  1113. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1114. This variable can be used to turn on and off the table editor during a session,
  1115. but in order to toggle optimization, a restart is required.
  1116. See also the variable `org-table-auto-blank-field'."
  1117. :group 'org-table
  1118. :type '(choice
  1119. (const :tag "off" nil)
  1120. (const :tag "on" t)
  1121. (const :tag "on, optimized" optimized)))
  1122. (defcustom org-self-insert-cluster-for-undo t
  1123. "Non-nil means cluster self-insert commands for undo when possible.
  1124. If this is set, then, like in the Emacs command loop, 20 consecutive
  1125. characters will be undone together.
  1126. This is configurable, because there is some impact on typing performance."
  1127. :group 'org-table
  1128. :type 'boolean)
  1129. (defcustom org-table-tab-recognizes-table.el t
  1130. "Non-nil means TAB will automatically notice a table.el table.
  1131. When it sees such a table, it moves point into it and - if necessary -
  1132. calls `table-recognize-table'."
  1133. :group 'org-table-editing
  1134. :type 'boolean)
  1135. (defgroup org-link nil
  1136. "Options concerning links in Org-mode."
  1137. :tag "Org Link"
  1138. :group 'org)
  1139. (defvar org-link-abbrev-alist-local nil
  1140. "Buffer-local version of `org-link-abbrev-alist', which see.
  1141. The value of this is taken from the #+LINK lines.")
  1142. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1143. (defcustom org-link-abbrev-alist nil
  1144. "Alist of link abbreviations.
  1145. The car of each element is a string, to be replaced at the start of a link.
  1146. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1147. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1148. [[linkkey:tag][description]]
  1149. The 'linkkey' must be a word word, starting with a letter, followed
  1150. by letters, numbers, '-' or '_'.
  1151. If REPLACE is a string, the tag will simply be appended to create the link.
  1152. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  1153. the placeholder \"%h\" will cause a url-encoded version of the tag to
  1154. be inserted at that point (see the function `url-hexify-string').
  1155. REPLACE may also be a function that will be called with the tag as the
  1156. only argument to create the link, which should be returned as a string.
  1157. See the manual for examples."
  1158. :group 'org-link
  1159. :type '(repeat
  1160. (cons
  1161. (string :tag "Protocol")
  1162. (choice
  1163. (string :tag "Format")
  1164. (function)))))
  1165. (defcustom org-descriptive-links t
  1166. "Non-nil means Org will display descriptive links.
  1167. E.g. [[http://orgmode.org][Org website]] will be displayed as
  1168. \"Org Website\", hiding the link itself and just displaying its
  1169. description. When set to `nil', Org will display the full links
  1170. literally.
  1171. You can interactively set the value of this variable by calling
  1172. `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
  1173. :group 'org-link
  1174. :type 'boolean)
  1175. (defcustom org-link-file-path-type 'adaptive
  1176. "How the path name in file links should be stored.
  1177. Valid values are:
  1178. relative Relative to the current directory, i.e. the directory of the file
  1179. into which the link is being inserted.
  1180. absolute Absolute path, if possible with ~ for home directory.
  1181. noabbrev Absolute path, no abbreviation of home directory.
  1182. adaptive Use relative path for files in the current directory and sub-
  1183. directories of it. For other files, use an absolute path."
  1184. :group 'org-link
  1185. :type '(choice
  1186. (const relative)
  1187. (const absolute)
  1188. (const noabbrev)
  1189. (const adaptive)))
  1190. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  1191. "Types of links that should be activated in Org-mode files.
  1192. This is a list of symbols, each leading to the activation of a certain link
  1193. type. In principle, it does not hurt to turn on most link types - there may
  1194. be a small gain when turning off unused link types. The types are:
  1195. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1196. angle Links in angular brackets that may contain whitespace like
  1197. <bbdb:Carsten Dominik>.
  1198. plain Plain links in normal text, no whitespace, like http://google.com.
  1199. radio Text that is matched by a radio target, see manual for details.
  1200. tag Tag settings in a headline (link to tag search).
  1201. date Time stamps (link to calendar).
  1202. footnote Footnote labels.
  1203. Changing this variable requires a restart of Emacs to become effective."
  1204. :group 'org-link
  1205. :type '(set :greedy t
  1206. (const :tag "Double bracket links" bracket)
  1207. (const :tag "Angular bracket links" angle)
  1208. (const :tag "Plain text links" plain)
  1209. (const :tag "Radio target matches" radio)
  1210. (const :tag "Tags" tag)
  1211. (const :tag "Timestamps" date)
  1212. (const :tag "Footnotes" footnote)))
  1213. (defcustom org-make-link-description-function nil
  1214. "Function to use to generate link descriptions from links.
  1215. If nil the link location will be used. This function must take
  1216. two parameters; the first is the link and the second the
  1217. description `org-insert-link' has generated, and should return the
  1218. description to use."
  1219. :group 'org-link
  1220. :type 'function)
  1221. (defgroup org-link-store nil
  1222. "Options concerning storing links in Org-mode."
  1223. :tag "Org Store Link"
  1224. :group 'org-link)
  1225. (defcustom org-email-link-description-format "Email %c: %.30s"
  1226. "Format of the description part of a link to an email or usenet message.
  1227. The following %-escapes will be replaced by corresponding information:
  1228. %F full \"From\" field
  1229. %f name, taken from \"From\" field, address if no name
  1230. %T full \"To\" field
  1231. %t first name in \"To\" field, address if no name
  1232. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1233. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1234. %s subject
  1235. %d date
  1236. %m message-id.
  1237. You may use normal field width specification between the % and the letter.
  1238. This is for example useful to limit the length of the subject.
  1239. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1240. :group 'org-link-store
  1241. :type 'string)
  1242. (defcustom org-from-is-user-regexp
  1243. (let (r1 r2)
  1244. (when (and user-mail-address (not (string= user-mail-address "")))
  1245. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1246. (when (and user-full-name (not (string= user-full-name "")))
  1247. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1248. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1249. "Regexp matched against the \"From:\" header of an email or usenet message.
  1250. It should match if the message is from the user him/herself."
  1251. :group 'org-link-store
  1252. :type 'regexp)
  1253. (defcustom org-link-to-org-use-id 'create-if-interactive-and-no-custom-id
  1254. "Non-nil means storing a link to an Org file will use entry IDs.
  1255. Note that before this variable is even considered, org-id must be loaded,
  1256. so please customize `org-modules' and turn it on.
  1257. The variable can have the following values:
  1258. t Create an ID if needed to make a link to the current entry.
  1259. create-if-interactive
  1260. If `org-store-link' is called directly (interactively, as a user
  1261. command), do create an ID to support the link. But when doing the
  1262. job for remember, only use the ID if it already exists. The
  1263. purpose of this setting is to avoid proliferation of unwanted
  1264. IDs, just because you happen to be in an Org file when you
  1265. call `org-remember' that automatically and preemptively
  1266. creates a link. If you do want to get an ID link in a remember
  1267. template to an entry not having an ID, create it first by
  1268. explicitly creating a link to it, using `C-c C-l' first.
  1269. create-if-interactive-and-no-custom-id
  1270. Like create-if-interactive, but do not create an ID if there is
  1271. a CUSTOM_ID property defined in the entry. This is the default.
  1272. use-existing
  1273. Use existing ID, do not create one.
  1274. nil Never use an ID to make a link, instead link using a text search for
  1275. the headline text."
  1276. :group 'org-link-store
  1277. :type '(choice
  1278. (const :tag "Create ID to make link" t)
  1279. (const :tag "Create if storing link interactively"
  1280. create-if-interactive)
  1281. (const :tag "Create if storing link interactively and no CUSTOM_ID is present"
  1282. create-if-interactive-and-no-custom-id)
  1283. (const :tag "Only use existing" use-existing)
  1284. (const :tag "Do not use ID to create link" nil)))
  1285. (defcustom org-context-in-file-links t
  1286. "Non-nil means file links from `org-store-link' contain context.
  1287. A search string will be added to the file name with :: as separator and
  1288. used to find the context when the link is activated by the command
  1289. `org-open-at-point'. When this option is t, the entire active region
  1290. will be placed in the search string of the file link. If set to a
  1291. positive integer, only the first n lines of context will be stored.
  1292. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1293. negates this setting for the duration of the command."
  1294. :group 'org-link-store
  1295. :type '(choice boolean integer))
  1296. (defcustom org-keep-stored-link-after-insertion nil
  1297. "Non-nil means keep link in list for entire session.
  1298. The command `org-store-link' adds a link pointing to the current
  1299. location to an internal list. These links accumulate during a session.
  1300. The command `org-insert-link' can be used to insert links into any
  1301. Org-mode file (offering completion for all stored links). When this
  1302. option is nil, every link which has been inserted once using \\[org-insert-link]
  1303. will be removed from the list, to make completing the unused links
  1304. more efficient."
  1305. :group 'org-link-store
  1306. :type 'boolean)
  1307. (defgroup org-link-follow nil
  1308. "Options concerning following links in Org-mode."
  1309. :tag "Org Follow Link"
  1310. :group 'org-link)
  1311. (defcustom org-link-translation-function nil
  1312. "Function to translate links with different syntax to Org syntax.
  1313. This can be used to translate links created for example by the Planner
  1314. or emacs-wiki packages to Org syntax.
  1315. The function must accept two parameters, a TYPE containing the link
  1316. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1317. which is everything after the link protocol. It should return a cons
  1318. with possibly modified values of type and path.
  1319. Org contains a function for this, so if you set this variable to
  1320. `org-translate-link-from-planner', you should be able follow many
  1321. links created by planner."
  1322. :group 'org-link-follow
  1323. :type 'function)
  1324. (defcustom org-follow-link-hook nil
  1325. "Hook that is run after a link has been followed."
  1326. :group 'org-link-follow
  1327. :type 'hook)
  1328. (defcustom org-tab-follows-link nil
  1329. "Non-nil means on links TAB will follow the link.
  1330. Needs to be set before org.el is loaded.
  1331. This really should not be used, it does not make sense, and the
  1332. implementation is bad."
  1333. :group 'org-link-follow
  1334. :type 'boolean)
  1335. (defcustom org-return-follows-link nil
  1336. "Non-nil means on links RET will follow the link."
  1337. :group 'org-link-follow
  1338. :type 'boolean)
  1339. (defcustom org-mouse-1-follows-link
  1340. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1341. "Non-nil means mouse-1 on a link will follow the link.
  1342. A longer mouse click will still set point. Does not work on XEmacs.
  1343. Needs to be set before org.el is loaded."
  1344. :group 'org-link-follow
  1345. :type 'boolean)
  1346. (defcustom org-mark-ring-length 4
  1347. "Number of different positions to be recorded in the ring.
  1348. Changing this requires a restart of Emacs to work correctly."
  1349. :group 'org-link-follow
  1350. :type 'integer)
  1351. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1352. "Non-nil means internal links in Org files must exactly match a headline.
  1353. When nil, the link search tries to match a phrase with all words
  1354. in the search text."
  1355. :group 'org-link-follow
  1356. :version "24.1"
  1357. :type '(choice
  1358. (const :tag "Use fuzzy text search" nil)
  1359. (const :tag "Match only exact headline" t)
  1360. (const :tag "Match exact headline or query to create it"
  1361. query-to-create)))
  1362. (defcustom org-link-frame-setup
  1363. '((vm . vm-visit-folder-other-frame)
  1364. (vm-imap . vm-visit-imap-folder-other-frame)
  1365. (gnus . org-gnus-no-new-news)
  1366. (file . find-file-other-window)
  1367. (wl . wl-other-frame))
  1368. "Setup the frame configuration for following links.
  1369. When following a link with Emacs, it may often be useful to display
  1370. this link in another window or frame. This variable can be used to
  1371. set this up for the different types of links.
  1372. For VM, use any of
  1373. `vm-visit-folder'
  1374. `vm-visit-folder-other-window'
  1375. `vm-visit-folder-other-frame'
  1376. For Gnus, use any of
  1377. `gnus'
  1378. `gnus-other-frame'
  1379. `org-gnus-no-new-news'
  1380. For FILE, use any of
  1381. `find-file'
  1382. `find-file-other-window'
  1383. `find-file-other-frame'
  1384. For Wanderlust use any of
  1385. `wl'
  1386. `wl-other-frame'
  1387. For the calendar, use the variable `calendar-setup'.
  1388. For BBDB, it is currently only possible to display the matches in
  1389. another window."
  1390. :group 'org-link-follow
  1391. :type '(list
  1392. (cons (const vm)
  1393. (choice
  1394. (const vm-visit-folder)
  1395. (const vm-visit-folder-other-window)
  1396. (const vm-visit-folder-other-frame)))
  1397. (cons (const gnus)
  1398. (choice
  1399. (const gnus)
  1400. (const gnus-other-frame)
  1401. (const org-gnus-no-new-news)))
  1402. (cons (const file)
  1403. (choice
  1404. (const find-file)
  1405. (const find-file-other-window)
  1406. (const find-file-other-frame)))
  1407. (cons (const wl)
  1408. (choice
  1409. (const wl)
  1410. (const wl-other-frame)))))
  1411. (defcustom org-display-internal-link-with-indirect-buffer nil
  1412. "Non-nil means use indirect buffer to display infile links.
  1413. Activating internal links (from one location in a file to another location
  1414. in the same file) normally just jumps to the location. When the link is
  1415. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1416. is displayed in
  1417. another window. When this option is set, the other window actually displays
  1418. an indirect buffer clone of the current buffer, to avoid any visibility
  1419. changes to the current buffer."
  1420. :group 'org-link-follow
  1421. :type 'boolean)
  1422. (defcustom org-open-non-existing-files nil
  1423. "Non-nil means `org-open-file' will open non-existing files.
  1424. When nil, an error will be generated.
  1425. This variable applies only to external applications because they
  1426. might choke on non-existing files. If the link is to a file that
  1427. will be opened in Emacs, the variable is ignored."
  1428. :group 'org-link-follow
  1429. :type 'boolean)
  1430. (defcustom org-open-directory-means-index-dot-org nil
  1431. "Non-nil means a link to a directory really means to index.org.
  1432. When nil, following a directory link will run dired or open a finder/explorer
  1433. window on that directory."
  1434. :group 'org-link-follow
  1435. :type 'boolean)
  1436. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1437. "Function and arguments to call for following mailto links.
  1438. This is a list with the first element being a Lisp function, and the
  1439. remaining elements being arguments to the function. In string arguments,
  1440. %a will be replaced by the address, and %s will be replaced by the subject
  1441. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1442. :group 'org-link-follow
  1443. :type '(choice
  1444. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1445. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1446. (const :tag "message-mail" (message-mail "%a" "%s"))
  1447. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1448. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1449. "Non-nil means ask for confirmation before executing shell links.
  1450. Shell links can be dangerous: just think about a link
  1451. [[shell:rm -rf ~/*][Google Search]]
  1452. This link would show up in your Org-mode document as \"Google Search\",
  1453. but really it would remove your entire home directory.
  1454. Therefore we advise against setting this variable to nil.
  1455. Just change it to `y-or-n-p' if you want to confirm with a
  1456. single keystroke rather than having to type \"yes\"."
  1457. :group 'org-link-follow
  1458. :type '(choice
  1459. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1460. (const :tag "with y-or-n (faster)" y-or-n-p)
  1461. (const :tag "no confirmation (dangerous)" nil)))
  1462. (put 'org-confirm-shell-link-function
  1463. 'safe-local-variable
  1464. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1465. (defcustom org-confirm-shell-link-not-regexp ""
  1466. "A regexp to skip confirmation for shell links."
  1467. :group 'org-link-follow
  1468. :version "24.1"
  1469. :type 'regexp)
  1470. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1471. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1472. Elisp links can be dangerous: just think about a link
  1473. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1474. This link would show up in your Org-mode document as \"Google Search\",
  1475. but really it would remove your entire home directory.
  1476. Therefore we advise against setting this variable to nil.
  1477. Just change it to `y-or-n-p' if you want to confirm with a
  1478. single keystroke rather than having to type \"yes\"."
  1479. :group 'org-link-follow
  1480. :type '(choice
  1481. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1482. (const :tag "with y-or-n (faster)" y-or-n-p)
  1483. (const :tag "no confirmation (dangerous)" nil)))
  1484. (put 'org-confirm-shell-link-function
  1485. 'safe-local-variable
  1486. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1487. (defcustom org-confirm-elisp-link-not-regexp ""
  1488. "A regexp to skip confirmation for Elisp links."
  1489. :group 'org-link-follow
  1490. :version "24.1"
  1491. :type 'regexp)
  1492. (defconst org-file-apps-defaults-gnu
  1493. '((remote . emacs)
  1494. (system . mailcap)
  1495. (t . mailcap))
  1496. "Default file applications on a UNIX or GNU/Linux system.
  1497. See `org-file-apps'.")
  1498. (defconst org-file-apps-defaults-macosx
  1499. '((remote . emacs)
  1500. (t . "open %s")
  1501. (system . "open %s")
  1502. ("ps.gz" . "gv %s")
  1503. ("eps.gz" . "gv %s")
  1504. ("dvi" . "xdvi %s")
  1505. ("fig" . "xfig %s"))
  1506. "Default file applications on a MacOS X system.
  1507. The system \"open\" is known as a default, but we use X11 applications
  1508. for some files for which the OS does not have a good default.
  1509. See `org-file-apps'.")
  1510. (defconst org-file-apps-defaults-windowsnt
  1511. (list
  1512. '(remote . emacs)
  1513. (cons t
  1514. (list (if (featurep 'xemacs)
  1515. 'mswindows-shell-execute
  1516. 'w32-shell-execute)
  1517. "open" 'file))
  1518. (cons 'system
  1519. (list (if (featurep 'xemacs)
  1520. 'mswindows-shell-execute
  1521. 'w32-shell-execute)
  1522. "open" 'file)))
  1523. "Default file applications on a Windows NT system.
  1524. The system \"open\" is used for most files.
  1525. See `org-file-apps'.")
  1526. (defcustom org-file-apps
  1527. '(
  1528. (auto-mode . emacs)
  1529. ("\\.mm\\'" . default)
  1530. ("\\.x?html?\\'" . default)
  1531. ("\\.pdf\\'" . default)
  1532. )
  1533. "External applications for opening `file:path' items in a document.
  1534. Org-mode uses system defaults for different file types, but
  1535. you can use this variable to set the application for a given file
  1536. extension. The entries in this list are cons cells where the car identifies
  1537. files and the cdr the corresponding command. Possible values for the
  1538. file identifier are
  1539. \"string\" A string as a file identifier can be interpreted in different
  1540. ways, depending on its contents:
  1541. - Alphanumeric characters only:
  1542. Match links with this file extension.
  1543. Example: (\"pdf\" . \"evince %s\")
  1544. to open PDFs with evince.
  1545. - Regular expression: Match links where the
  1546. filename matches the regexp. If you want to
  1547. use groups here, use shy groups.
  1548. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1549. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1550. to open *.html and *.xhtml with firefox.
  1551. - Regular expression which contains (non-shy) groups:
  1552. Match links where the whole link, including \"::\", and
  1553. anything after that, matches the regexp.
  1554. In a custom command string, %1, %2, etc. are replaced with
  1555. the parts of the link that were matched by the groups.
  1556. For backwards compatibility, if a command string is given
  1557. that does not use any of the group matches, this case is
  1558. handled identically to the second one (i.e. match against
  1559. file name only).
  1560. In a custom lisp form, you can access the group matches with
  1561. (match-string n link).
  1562. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1563. to open [[file:document.pdf::5]] with evince at page 5.
  1564. `directory' Matches a directory
  1565. `remote' Matches a remote file, accessible through tramp or efs.
  1566. Remote files most likely should be visited through Emacs
  1567. because external applications cannot handle such paths.
  1568. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1569. so all files Emacs knows how to handle. Using this with
  1570. command `emacs' will open most files in Emacs. Beware that this
  1571. will also open html files inside Emacs, unless you add
  1572. (\"html\" . default) to the list as well.
  1573. t Default for files not matched by any of the other options.
  1574. `system' The system command to open files, like `open' on Windows
  1575. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1576. that will be selected if you call `C-c C-o' with a double
  1577. \\[universal-argument] \\[universal-argument] prefix.
  1578. Possible values for the command are:
  1579. `emacs' The file will be visited by the current Emacs process.
  1580. `default' Use the default application for this file type, which is the
  1581. association for t in the list, most likely in the system-specific
  1582. part.
  1583. This can be used to overrule an unwanted setting in the
  1584. system-specific variable.
  1585. `system' Use the system command for opening files, like \"open\".
  1586. This command is specified by the entry whose car is `system'.
  1587. Most likely, the system-specific version of this variable
  1588. does define this command, but you can overrule/replace it
  1589. here.
  1590. string A command to be executed by a shell; %s will be replaced
  1591. by the path to the file.
  1592. sexp A Lisp form which will be evaluated. The file path will
  1593. be available in the Lisp variable `file'.
  1594. For more examples, see the system specific constants
  1595. `org-file-apps-defaults-macosx'
  1596. `org-file-apps-defaults-windowsnt'
  1597. `org-file-apps-defaults-gnu'."
  1598. :group 'org-link-follow
  1599. :type '(repeat
  1600. (cons (choice :value ""
  1601. (string :tag "Extension")
  1602. (const :tag "System command to open files" system)
  1603. (const :tag "Default for unrecognized files" t)
  1604. (const :tag "Remote file" remote)
  1605. (const :tag "Links to a directory" directory)
  1606. (const :tag "Any files that have Emacs modes"
  1607. auto-mode))
  1608. (choice :value ""
  1609. (const :tag "Visit with Emacs" emacs)
  1610. (const :tag "Use default" default)
  1611. (const :tag "Use the system command" system)
  1612. (string :tag "Command")
  1613. (sexp :tag "Lisp form")))))
  1614. (defgroup org-refile nil
  1615. "Options concerning refiling entries in Org-mode."
  1616. :tag "Org Refile"
  1617. :group 'org)
  1618. (defcustom org-directory "~/org"
  1619. "Directory with org files.
  1620. This is just a default location to look for Org files. There is no need
  1621. at all to put your files into this directory. It is only used in the
  1622. following situations:
  1623. 1. When a remember template specifies a target file that is not an
  1624. absolute path. The path will then be interpreted relative to
  1625. `org-directory'
  1626. 2. When a remember note is filed away in an interactive way (when exiting the
  1627. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1628. with `org-directory' as the default path."
  1629. :group 'org-refile
  1630. :group 'org-remember
  1631. :type 'directory)
  1632. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1633. "Default target for storing notes.
  1634. Used as a fall back file for org-remember.el and org-capture.el, for
  1635. templates that do not specify a target file."
  1636. :group 'org-refile
  1637. :group 'org-remember
  1638. :type '(choice
  1639. (const :tag "Default from remember-data-file" nil)
  1640. file))
  1641. (defcustom org-goto-interface 'outline
  1642. "The default interface to be used for `org-goto'.
  1643. Allowed values are:
  1644. outline The interface shows an outline of the relevant file
  1645. and the correct heading is found by moving through
  1646. the outline or by searching with incremental search.
  1647. outline-path-completion Headlines in the current buffer are offered via
  1648. completion. This is the interface also used by
  1649. the refile command."
  1650. :group 'org-refile
  1651. :type '(choice
  1652. (const :tag "Outline" outline)
  1653. (const :tag "Outline-path-completion" outline-path-completion)))
  1654. (defcustom org-goto-max-level 5
  1655. "Maximum target level when running `org-goto' with refile interface."
  1656. :group 'org-refile
  1657. :type 'integer)
  1658. (defcustom org-reverse-note-order nil
  1659. "Non-nil means store new notes at the beginning of a file or entry.
  1660. When nil, new notes will be filed to the end of a file or entry.
  1661. This can also be a list with cons cells of regular expressions that
  1662. are matched against file names, and values."
  1663. :group 'org-remember
  1664. :group 'org-refile
  1665. :type '(choice
  1666. (const :tag "Reverse always" t)
  1667. (const :tag "Reverse never" nil)
  1668. (repeat :tag "By file name regexp"
  1669. (cons regexp boolean))))
  1670. (defcustom org-log-refile nil
  1671. "Information to record when a task is refiled.
  1672. Possible values are:
  1673. nil Don't add anything
  1674. time Add a time stamp to the task
  1675. note Prompt for a note and add it with template `org-log-note-headings'
  1676. This option can also be set with on a per-file-basis with
  1677. #+STARTUP: nologrefile
  1678. #+STARTUP: logrefile
  1679. #+STARTUP: lognoterefile
  1680. You can have local logging settings for a subtree by setting the LOGGING
  1681. property to one or more of these keywords.
  1682. When bulk-refiling from the agenda, the value `note' is forbidden and
  1683. will temporarily be changed to `time'."
  1684. :group 'org-refile
  1685. :group 'org-progress
  1686. :version "24.1"
  1687. :type '(choice
  1688. (const :tag "No logging" nil)
  1689. (const :tag "Record timestamp" time)
  1690. (const :tag "Record timestamp with note." note)))
  1691. (defcustom org-refile-targets nil
  1692. "Targets for refiling entries with \\[org-refile].
  1693. This is a list of cons cells. Each cell contains:
  1694. - a specification of the files to be considered, either a list of files,
  1695. or a symbol whose function or variable value will be used to retrieve
  1696. a file name or a list of file names. If you use `org-agenda-files' for
  1697. that, all agenda files will be scanned for targets. Nil means consider
  1698. headings in the current buffer.
  1699. - A specification of how to find candidate refile targets. This may be
  1700. any of:
  1701. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1702. This tag has to be present in all target headlines, inheritance will
  1703. not be considered.
  1704. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1705. todo keyword.
  1706. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1707. headlines that are refiling targets.
  1708. - a cons cell (:level . N). Any headline of level N is considered a target.
  1709. Note that, when `org-odd-levels-only' is set, level corresponds to
  1710. order in hierarchy, not to the number of stars.
  1711. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1712. Note that, when `org-odd-levels-only' is set, level corresponds to
  1713. order in hierarchy, not to the number of stars.
  1714. Each element of this list generates a set of possible targets.
  1715. The union of these sets is presented (with completion) to
  1716. the user by `org-refile'.
  1717. You can set the variable `org-refile-target-verify-function' to a function
  1718. to verify each headline found by the simple criteria above.
  1719. When this variable is nil, all top-level headlines in the current buffer
  1720. are used, equivalent to the value `((nil . (:level . 1))'."
  1721. :group 'org-refile
  1722. :type '(repeat
  1723. (cons
  1724. (choice :value org-agenda-files
  1725. (const :tag "All agenda files" org-agenda-files)
  1726. (const :tag "Current buffer" nil)
  1727. (function) (variable) (file))
  1728. (choice :tag "Identify target headline by"
  1729. (cons :tag "Specific tag" (const :value :tag) (string))
  1730. (cons :tag "TODO keyword" (const :value :todo) (string))
  1731. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1732. (cons :tag "Level number" (const :value :level) (integer))
  1733. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1734. (defcustom org-refile-target-verify-function nil
  1735. "Function to verify if the headline at point should be a refile target.
  1736. The function will be called without arguments, with point at the
  1737. beginning of the headline. It should return t and leave point
  1738. where it is if the headline is a valid target for refiling.
  1739. If the target should not be selected, the function must return nil.
  1740. In addition to this, it may move point to a place from where the search
  1741. should be continued. For example, the function may decide that the entire
  1742. subtree of the current entry should be excluded and move point to the end
  1743. of the subtree."
  1744. :group 'org-refile
  1745. :type 'function)
  1746. (defcustom org-refile-use-cache nil
  1747. "Non-nil means cache refile targets to speed up the process.
  1748. The cache for a particular file will be updated automatically when
  1749. the buffer has been killed, or when any of the marker used for flagging
  1750. refile targets no longer points at a live buffer.
  1751. If you have added new entries to a buffer that might themselves be targets,
  1752. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  1753. find that easier, `C-u C-u C-u C-c C-w'."
  1754. :group 'org-refile
  1755. :version "24.1"
  1756. :type 'boolean)
  1757. (defcustom org-refile-use-outline-path nil
  1758. "Non-nil means provide refile targets as paths.
  1759. So a level 3 headline will be available as level1/level2/level3.
  1760. When the value is `file', also include the file name (without directory)
  1761. into the path. In this case, you can also stop the completion after
  1762. the file name, to get entries inserted as top level in the file.
  1763. When `full-file-path', include the full file path."
  1764. :group 'org-refile
  1765. :type '(choice
  1766. (const :tag "Not" nil)
  1767. (const :tag "Yes" t)
  1768. (const :tag "Start with file name" file)
  1769. (const :tag "Start with full file path" full-file-path)))
  1770. (defcustom org-outline-path-complete-in-steps t
  1771. "Non-nil means complete the outline path in hierarchical steps.
  1772. When Org-mode uses the refile interface to select an outline path
  1773. \(see variable `org-refile-use-outline-path'), the completion of
  1774. the path can be done is a single go, or if can be done in steps down
  1775. the headline hierarchy. Going in steps is probably the best if you
  1776. do not use a special completion package like `ido' or `icicles'.
  1777. However, when using these packages, going in one step can be very
  1778. fast, while still showing the whole path to the entry."
  1779. :group 'org-refile
  1780. :type 'boolean)
  1781. (defcustom org-refile-allow-creating-parent-nodes nil
  1782. "Non-nil means allow to create new nodes as refile targets.
  1783. New nodes are then created by adding \"/new node name\" to the completion
  1784. of an existing node. When the value of this variable is `confirm',
  1785. new node creation must be confirmed by the user (recommended)
  1786. When nil, the completion must match an existing entry.
  1787. Note that, if the new heading is not seen by the criteria
  1788. listed in `org-refile-targets', multiple instances of the same
  1789. heading would be created by trying again to file under the new
  1790. heading."
  1791. :group 'org-refile
  1792. :type '(choice
  1793. (const :tag "Never" nil)
  1794. (const :tag "Always" t)
  1795. (const :tag "Prompt for confirmation" confirm)))
  1796. (defcustom org-refile-active-region-within-subtree nil
  1797. "Non-nil means also refile active region within a subtree.
  1798. By default `org-refile' doesn't allow refiling regions if they
  1799. don't contain a set of subtrees, but it might be convenient to
  1800. do so sometimes: in that case, the first line of the region is
  1801. converted to a headline before refiling."
  1802. :group 'org-refile
  1803. :version "24.1"
  1804. :type 'boolean)
  1805. (defgroup org-todo nil
  1806. "Options concerning TODO items in Org-mode."
  1807. :tag "Org TODO"
  1808. :group 'org)
  1809. (defgroup org-progress nil
  1810. "Options concerning Progress logging in Org-mode."
  1811. :tag "Org Progress"
  1812. :group 'org-time)
  1813. (defvar org-todo-interpretation-widgets
  1814. '((:tag "Sequence (cycling hits every state)" sequence)
  1815. (:tag "Type (cycling directly to DONE)" type))
  1816. "The available interpretation symbols for customizing `org-todo-keywords'.
  1817. Interested libraries should add to this list.")
  1818. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1819. "List of TODO entry keyword sequences and their interpretation.
  1820. \\<org-mode-map>This is a list of sequences.
  1821. Each sequence starts with a symbol, either `sequence' or `type',
  1822. indicating if the keywords should be interpreted as a sequence of
  1823. action steps, or as different types of TODO items. The first
  1824. keywords are states requiring action - these states will select a headline
  1825. for inclusion into the global TODO list Org-mode produces. If one of
  1826. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  1827. signify that no further action is necessary. If \"|\" is not found,
  1828. the last keyword is treated as the only DONE state of the sequence.
  1829. The command \\[org-todo] cycles an entry through these states, and one
  1830. additional state where no keyword is present. For details about this
  1831. cycling, see the manual.
  1832. TODO keywords and interpretation can also be set on a per-file basis with
  1833. the special #+SEQ_TODO and #+TYP_TODO lines.
  1834. Each keyword can optionally specify a character for fast state selection
  1835. \(in combination with the variable `org-use-fast-todo-selection')
  1836. and specifiers for state change logging, using the same syntax
  1837. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1838. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1839. indicates to record a time stamp each time this state is selected.
  1840. Each keyword may also specify if a timestamp or a note should be
  1841. recorded when entering or leaving the state, by adding additional
  1842. characters in the parenthesis after the keyword. This looks like this:
  1843. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1844. record only the time of the state change. With X and Y being either
  1845. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1846. Y when leaving the state if and only if the *target* state does not
  1847. define X. You may omit any of the fast-selection key or X or /Y,
  1848. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1849. For backward compatibility, this variable may also be just a list
  1850. of keywords - in this case the interpretation (sequence or type) will be
  1851. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1852. :group 'org-todo
  1853. :group 'org-keywords
  1854. :type '(choice
  1855. (repeat :tag "Old syntax, just keywords"
  1856. (string :tag "Keyword"))
  1857. (repeat :tag "New syntax"
  1858. (cons
  1859. (choice
  1860. :tag "Interpretation"
  1861. ;;Quick and dirty way to see
  1862. ;;`org-todo-interpretations'. This takes the
  1863. ;;place of item arguments
  1864. :convert-widget
  1865. (lambda (widget)
  1866. (widget-put widget
  1867. :args (mapcar
  1868. #'(lambda (x)
  1869. (widget-convert
  1870. (cons 'const x)))
  1871. org-todo-interpretation-widgets))
  1872. widget))
  1873. (repeat
  1874. (string :tag "Keyword"))))))
  1875. (defvar org-todo-keywords-1 nil
  1876. "All TODO and DONE keywords active in a buffer.")
  1877. (make-variable-buffer-local 'org-todo-keywords-1)
  1878. (defvar org-todo-keywords-for-agenda nil)
  1879. (defvar org-done-keywords-for-agenda nil)
  1880. (defvar org-drawers-for-agenda nil)
  1881. (defvar org-todo-keyword-alist-for-agenda nil)
  1882. (defvar org-tag-alist-for-agenda nil)
  1883. (defvar org-agenda-contributing-files nil)
  1884. (defvar org-not-done-keywords nil)
  1885. (make-variable-buffer-local 'org-not-done-keywords)
  1886. (defvar org-done-keywords nil)
  1887. (make-variable-buffer-local 'org-done-keywords)
  1888. (defvar org-todo-heads nil)
  1889. (make-variable-buffer-local 'org-todo-heads)
  1890. (defvar org-todo-sets nil)
  1891. (make-variable-buffer-local 'org-todo-sets)
  1892. (defvar org-todo-log-states nil)
  1893. (make-variable-buffer-local 'org-todo-log-states)
  1894. (defvar org-todo-kwd-alist nil)
  1895. (make-variable-buffer-local 'org-todo-kwd-alist)
  1896. (defvar org-todo-key-alist nil)
  1897. (make-variable-buffer-local 'org-todo-key-alist)
  1898. (defvar org-todo-key-trigger nil)
  1899. (make-variable-buffer-local 'org-todo-key-trigger)
  1900. (defcustom org-todo-interpretation 'sequence
  1901. "Controls how TODO keywords are interpreted.
  1902. This variable is in principle obsolete and is only used for
  1903. backward compatibility, if the interpretation of todo keywords is
  1904. not given already in `org-todo-keywords'. See that variable for
  1905. more information."
  1906. :group 'org-todo
  1907. :group 'org-keywords
  1908. :type '(choice (const sequence)
  1909. (const type)))
  1910. (defcustom org-use-fast-todo-selection t
  1911. "Non-nil means use the fast todo selection scheme with C-c C-t.
  1912. This variable describes if and under what circumstances the cycling
  1913. mechanism for TODO keywords will be replaced by a single-key, direct
  1914. selection scheme.
  1915. When nil, fast selection is never used.
  1916. When the symbol `prefix', it will be used when `org-todo' is called with
  1917. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1918. in an agenda buffer.
  1919. When t, fast selection is used by default. In this case, the prefix
  1920. argument forces cycling instead.
  1921. In all cases, the special interface is only used if access keys have actually
  1922. been assigned by the user, i.e. if keywords in the configuration are followed
  1923. by a letter in parenthesis, like TODO(t)."
  1924. :group 'org-todo
  1925. :type '(choice
  1926. (const :tag "Never" nil)
  1927. (const :tag "By default" t)
  1928. (const :tag "Only with C-u C-c C-t" prefix)))
  1929. (defcustom org-provide-todo-statistics t
  1930. "Non-nil means update todo statistics after insert and toggle.
  1931. ALL-HEADLINES means update todo statistics by including headlines
  1932. with no TODO keyword as well, counting them as not done.
  1933. A list of TODO keywords means the same, but skip keywords that are
  1934. not in this list.
  1935. When this is set, todo statistics is updated in the parent of the
  1936. current entry each time a todo state is changed."
  1937. :group 'org-todo
  1938. :type '(choice
  1939. (const :tag "Yes, only for TODO entries" t)
  1940. (const :tag "Yes, including all entries" 'all-headlines)
  1941. (repeat :tag "Yes, for TODOs in this list"
  1942. (string :tag "TODO keyword"))
  1943. (other :tag "No TODO statistics" nil)))
  1944. (defcustom org-hierarchical-todo-statistics t
  1945. "Non-nil means TODO statistics covers just direct children.
  1946. When nil, all entries in the subtree are considered.
  1947. This has only an effect if `org-provide-todo-statistics' is set.
  1948. To set this to nil for only a single subtree, use a COOKIE_DATA
  1949. property and include the word \"recursive\" into the value."
  1950. :group 'org-todo
  1951. :type 'boolean)
  1952. (defcustom org-after-todo-state-change-hook nil
  1953. "Hook which is run after the state of a TODO item was changed.
  1954. The new state (a string with a TODO keyword, or nil) is available in the
  1955. Lisp variable `state'."
  1956. :group 'org-todo
  1957. :type 'hook)
  1958. (defvar org-blocker-hook nil
  1959. "Hook for functions that are allowed to block a state change.
  1960. Each function gets as its single argument a property list, see
  1961. `org-trigger-hook' for more information about this list.
  1962. If any of the functions in this hook returns nil, the state change
  1963. is blocked.")
  1964. (defvar org-trigger-hook nil
  1965. "Hook for functions that are triggered by a state change.
  1966. Each function gets as its single argument a property list with at least
  1967. the following elements:
  1968. (:type type-of-change :position pos-at-entry-start
  1969. :from old-state :to new-state)
  1970. Depending on the type, more properties may be present.
  1971. This mechanism is currently implemented for:
  1972. TODO state changes
  1973. ------------------
  1974. :type todo-state-change
  1975. :from previous state (keyword as a string), or nil, or a symbol
  1976. 'todo' or 'done', to indicate the general type of state.
  1977. :to new state, like in :from")
  1978. (defcustom org-enforce-todo-dependencies nil
  1979. "Non-nil means undone TODO entries will block switching the parent to DONE.
  1980. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1981. be blocked if any prior sibling is not yet done.
  1982. Finally, if the parent is blocked because of ordered siblings of its own,
  1983. the child will also be blocked.
  1984. This variable needs to be set before org.el is loaded, and you need to
  1985. restart Emacs after a change to make the change effective. The only way
  1986. to change is while Emacs is running is through the customize interface."
  1987. :set (lambda (var val)
  1988. (set var val)
  1989. (if val
  1990. (add-hook 'org-blocker-hook
  1991. 'org-block-todo-from-children-or-siblings-or-parent)
  1992. (remove-hook 'org-blocker-hook
  1993. 'org-block-todo-from-children-or-siblings-or-parent)))
  1994. :group 'org-todo
  1995. :type 'boolean)
  1996. (defcustom org-enforce-todo-checkbox-dependencies nil
  1997. "Non-nil means unchecked boxes will block switching the parent to DONE.
  1998. When this is nil, checkboxes have no influence on switching TODO states.
  1999. When non-nil, you first need to check off all check boxes before the TODO
  2000. entry can be switched to DONE.
  2001. This variable needs to be set before org.el is loaded, and you need to
  2002. restart Emacs after a change to make the change effective. The only way
  2003. to change is while Emacs is running is through the customize interface."
  2004. :set (lambda (var val)
  2005. (set var val)
  2006. (if val
  2007. (add-hook 'org-blocker-hook
  2008. 'org-block-todo-from-checkboxes)
  2009. (remove-hook 'org-blocker-hook
  2010. 'org-block-todo-from-checkboxes)))
  2011. :group 'org-todo
  2012. :type 'boolean)
  2013. (defcustom org-treat-insert-todo-heading-as-state-change nil
  2014. "Non-nil means inserting a TODO heading is treated as state change.
  2015. So when the command \\[org-insert-todo-heading] is used, state change
  2016. logging will apply if appropriate. When nil, the new TODO item will
  2017. be inserted directly, and no logging will take place."
  2018. :group 'org-todo
  2019. :type 'boolean)
  2020. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  2021. "Non-nil means switching TODO states with S-cursor counts as state change.
  2022. This is the default behavior. However, setting this to nil allows a
  2023. convenient way to select a TODO state and bypass any logging associated
  2024. with that."
  2025. :group 'org-todo
  2026. :type 'boolean)
  2027. (defcustom org-todo-state-tags-triggers nil
  2028. "Tag changes that should be triggered by TODO state changes.
  2029. This is a list. Each entry is
  2030. (state-change (tag . flag) .......)
  2031. State-change can be a string with a state, and empty string to indicate the
  2032. state that has no TODO keyword, or it can be one of the symbols `todo'
  2033. or `done', meaning any not-done or done state, respectively."
  2034. :group 'org-todo
  2035. :group 'org-tags
  2036. :type '(repeat
  2037. (cons (choice :tag "When changing to"
  2038. (const :tag "Not-done state" todo)
  2039. (const :tag "Done state" done)
  2040. (string :tag "State"))
  2041. (repeat
  2042. (cons :tag "Tag action"
  2043. (string :tag "Tag")
  2044. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  2045. (defcustom org-log-done nil
  2046. "Information to record when a task moves to the DONE state.
  2047. Possible values are:
  2048. nil Don't add anything, just change the keyword
  2049. time Add a time stamp to the task
  2050. note Prompt for a note and add it with template `org-log-note-headings'
  2051. This option can also be set with on a per-file-basis with
  2052. #+STARTUP: nologdone
  2053. #+STARTUP: logdone
  2054. #+STARTUP: lognotedone
  2055. You can have local logging settings for a subtree by setting the LOGGING
  2056. property to one or more of these keywords."
  2057. :group 'org-todo
  2058. :group 'org-progress
  2059. :type '(choice
  2060. (const :tag "No logging" nil)
  2061. (const :tag "Record CLOSED timestamp" time)
  2062. (const :tag "Record CLOSED timestamp with note." note)))
  2063. ;; Normalize old uses of org-log-done.
  2064. (cond
  2065. ((eq org-log-done t) (setq org-log-done 'time))
  2066. ((and (listp org-log-done) (memq 'done org-log-done))
  2067. (setq org-log-done 'note)))
  2068. (defcustom org-log-reschedule nil
  2069. "Information to record when the scheduling date of a tasks is modified.
  2070. Possible values are:
  2071. nil Don't add anything, just change the date
  2072. time Add a time stamp to the task
  2073. note Prompt for a note and add it with template `org-log-note-headings'
  2074. This option can also be set with on a per-file-basis with
  2075. #+STARTUP: nologreschedule
  2076. #+STARTUP: logreschedule
  2077. #+STARTUP: lognotereschedule"
  2078. :group 'org-todo
  2079. :group 'org-progress
  2080. :type '(choice
  2081. (const :tag "No logging" nil)
  2082. (const :tag "Record timestamp" time)
  2083. (const :tag "Record timestamp with note." note)))
  2084. (defcustom org-log-redeadline nil
  2085. "Information to record when the deadline date of a tasks is modified.
  2086. Possible values are:
  2087. nil Don't add anything, just change the date
  2088. time Add a time stamp to the task
  2089. note Prompt for a note and add it with template `org-log-note-headings'
  2090. This option can also be set with on a per-file-basis with
  2091. #+STARTUP: nologredeadline
  2092. #+STARTUP: logredeadline
  2093. #+STARTUP: lognoteredeadline
  2094. You can have local logging settings for a subtree by setting the LOGGING
  2095. property to one or more of these keywords."
  2096. :group 'org-todo
  2097. :group 'org-progress
  2098. :type '(choice
  2099. (const :tag "No logging" nil)
  2100. (const :tag "Record timestamp" time)
  2101. (const :tag "Record timestamp with note." note)))
  2102. (defcustom org-log-note-clock-out nil
  2103. "Non-nil means record a note when clocking out of an item.
  2104. This can also be configured on a per-file basis by adding one of
  2105. the following lines anywhere in the buffer:
  2106. #+STARTUP: lognoteclock-out
  2107. #+STARTUP: nolognoteclock-out"
  2108. :group 'org-todo
  2109. :group 'org-progress
  2110. :type 'boolean)
  2111. (defcustom org-log-done-with-time t
  2112. "Non-nil means the CLOSED time stamp will contain date and time.
  2113. When nil, only the date will be recorded."
  2114. :group 'org-progress
  2115. :type 'boolean)
  2116. (defcustom org-log-note-headings
  2117. '((done . "CLOSING NOTE %t")
  2118. (state . "State %-12s from %-12S %t")
  2119. (note . "Note taken on %t")
  2120. (reschedule . "Rescheduled from %S on %t")
  2121. (delschedule . "Not scheduled, was %S on %t")
  2122. (redeadline . "New deadline from %S on %t")
  2123. (deldeadline . "Removed deadline, was %S on %t")
  2124. (refile . "Refiled on %t")
  2125. (clock-out . ""))
  2126. "Headings for notes added to entries.
  2127. The value is an alist, with the car being a symbol indicating the note
  2128. context, and the cdr is the heading to be used. The heading may also be the
  2129. empty string.
  2130. %t in the heading will be replaced by a time stamp.
  2131. %T will be an active time stamp instead the default inactive one
  2132. %d will be replaced by a short-format time stamp.
  2133. %D will be replaced by an active short-format time stamp.
  2134. %s will be replaced by the new TODO state, in double quotes.
  2135. %S will be replaced by the old TODO state, in double quotes.
  2136. %u will be replaced by the user name.
  2137. %U will be replaced by the full user name.
  2138. In fact, it is not a good idea to change the `state' entry, because
  2139. agenda log mode depends on the format of these entries."
  2140. :group 'org-todo
  2141. :group 'org-progress
  2142. :type '(list :greedy t
  2143. (cons (const :tag "Heading when closing an item" done) string)
  2144. (cons (const :tag
  2145. "Heading when changing todo state (todo sequence only)"
  2146. state) string)
  2147. (cons (const :tag "Heading when just taking a note" note) string)
  2148. (cons (const :tag "Heading when clocking out" clock-out) string)
  2149. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2150. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2151. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2152. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2153. (cons (const :tag "Heading when refiling" refile) string)))
  2154. (unless (assq 'note org-log-note-headings)
  2155. (push '(note . "%t") org-log-note-headings))
  2156. (defcustom org-log-into-drawer nil
  2157. "Non-nil means insert state change notes and time stamps into a drawer.
  2158. When nil, state changes notes will be inserted after the headline and
  2159. any scheduling and clock lines, but not inside a drawer.
  2160. The value of this variable should be the name of the drawer to use.
  2161. LOGBOOK is proposed as the default drawer for this purpose, you can
  2162. also set this to a string to define the drawer of your choice.
  2163. A value of t is also allowed, representing \"LOGBOOK\".
  2164. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2165. will be ignored.
  2166. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2167. a subtree."
  2168. :group 'org-todo
  2169. :group 'org-progress
  2170. :type '(choice
  2171. (const :tag "Not into a drawer" nil)
  2172. (const :tag "LOGBOOK" t)
  2173. (string :tag "Other")))
  2174. (if (fboundp 'defvaralias)
  2175. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  2176. (defun org-log-into-drawer ()
  2177. "Return the value of `org-log-into-drawer', but let properties overrule.
  2178. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  2179. used instead of the default value."
  2180. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit)))
  2181. (cond
  2182. ((or (not p) (equal p "nil")) org-log-into-drawer)
  2183. ((equal p "t") "LOGBOOK")
  2184. (t p))))
  2185. (defcustom org-log-state-notes-insert-after-drawers nil
  2186. "Non-nil means insert state change notes after any drawers in entry.
  2187. Only the drawers that *immediately* follow the headline and the
  2188. deadline/scheduled line are skipped.
  2189. When nil, insert notes right after the heading and perhaps the line
  2190. with deadline/scheduling if present.
  2191. This variable will have no effect if `org-log-into-drawer' is
  2192. set."
  2193. :group 'org-todo
  2194. :group 'org-progress
  2195. :type 'boolean)
  2196. (defcustom org-log-states-order-reversed t
  2197. "Non-nil means the latest state note will be directly after heading.
  2198. When nil, the state change notes will be ordered according to time."
  2199. :group 'org-todo
  2200. :group 'org-progress
  2201. :type 'boolean)
  2202. (defcustom org-todo-repeat-to-state nil
  2203. "The TODO state to which a repeater should return the repeating task.
  2204. By default this is the first task in a TODO sequence, or the previous state
  2205. in a TODO_TYP set. But you can specify another task here.
  2206. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2207. :group 'org-todo
  2208. :version "24.1"
  2209. :type '(choice (const :tag "Head of sequence" nil)
  2210. (string :tag "Specific state")))
  2211. (defcustom org-log-repeat 'time
  2212. "Non-nil means record moving through the DONE state when triggering repeat.
  2213. An auto-repeating task is immediately switched back to TODO when
  2214. marked DONE. If you are not logging state changes (by adding \"@\"
  2215. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2216. record a closing note, there will be no record of the task moving
  2217. through DONE. This variable forces taking a note anyway.
  2218. nil Don't force a record
  2219. time Record a time stamp
  2220. note Record a note
  2221. This option can also be set with on a per-file-basis with
  2222. #+STARTUP: logrepeat
  2223. #+STARTUP: lognoterepeat
  2224. #+STARTUP: nologrepeat
  2225. You can have local logging settings for a subtree by setting the LOGGING
  2226. property to one or more of these keywords."
  2227. :group 'org-todo
  2228. :group 'org-progress
  2229. :type '(choice
  2230. (const :tag "Don't force a record" nil)
  2231. (const :tag "Force recording the DONE state" time)
  2232. (const :tag "Force recording a note with the DONE state" note)))
  2233. (defgroup org-priorities nil
  2234. "Priorities in Org-mode."
  2235. :tag "Org Priorities"
  2236. :group 'org-todo)
  2237. (defcustom org-enable-priority-commands t
  2238. "Non-nil means priority commands are active.
  2239. When nil, these commands will be disabled, so that you never accidentally
  2240. set a priority."
  2241. :group 'org-priorities
  2242. :type 'boolean)
  2243. (defcustom org-highest-priority ?A
  2244. "The highest priority of TODO items. A character like ?A, ?B etc.
  2245. Must have a smaller ASCII number than `org-lowest-priority'."
  2246. :group 'org-priorities
  2247. :type 'character)
  2248. (defcustom org-lowest-priority ?C
  2249. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2250. Must have a larger ASCII number than `org-highest-priority'."
  2251. :group 'org-priorities
  2252. :type 'character)
  2253. (defcustom org-default-priority ?B
  2254. "The default priority of TODO items.
  2255. This is the priority an item gets if no explicit priority is given.
  2256. When starting to cycle on an empty priority the first step in the cycle
  2257. depends on `org-priority-start-cycle-with-default'. The resulting first
  2258. step priority must not exceed the range from `org-highest-priority' to
  2259. `org-lowest-priority' which means that `org-default-priority' has to be
  2260. in this range exclusive or inclusive the range boundaries. Else the
  2261. first step refuses to set the default and the second will fall back
  2262. to (depending on the command used) the highest or lowest priority."
  2263. :group 'org-priorities
  2264. :type 'character)
  2265. (defcustom org-priority-start-cycle-with-default t
  2266. "Non-nil means start with default priority when starting to cycle.
  2267. When this is nil, the first step in the cycle will be (depending on the
  2268. command used) one higher or lower than the default priority.
  2269. See also `org-default-priority'."
  2270. :group 'org-priorities
  2271. :type 'boolean)
  2272. (defcustom org-get-priority-function nil
  2273. "Function to extract the priority from a string.
  2274. The string is normally the headline. If this is nil Org computes the
  2275. priority from the priority cookie like [#A] in the headline. It returns
  2276. an integer, increasing by 1000 for each priority level.
  2277. The user can set a different function here, which should take a string
  2278. as an argument and return the numeric priority."
  2279. :group 'org-priorities
  2280. :version "24.1"
  2281. :type 'function)
  2282. (defgroup org-time nil
  2283. "Options concerning time stamps and deadlines in Org-mode."
  2284. :tag "Org Time"
  2285. :group 'org)
  2286. (defcustom org-insert-labeled-timestamps-at-point nil
  2287. "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
  2288. When nil, these labeled time stamps are forces into the second line of an
  2289. entry, just after the headline. When scheduling from the global TODO list,
  2290. the time stamp will always be forced into the second line."
  2291. :group 'org-time
  2292. :type 'boolean)
  2293. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  2294. "Formats for `format-time-string' which are used for time stamps.
  2295. It is not recommended to change this constant.")
  2296. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2297. "Number of minutes to round time stamps to.
  2298. These are two values, the first applies when first creating a time stamp.
  2299. The second applies when changing it with the commands `S-up' and `S-down'.
  2300. When changing the time stamp, this means that it will change in steps
  2301. of N minutes, as given by the second value.
  2302. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2303. numbers should be factors of 60, so for example 5, 10, 15.
  2304. When this is larger than 1, you can still force an exact time stamp by using
  2305. a double prefix argument to a time stamp command like `C-c .' or `C-c !',
  2306. and by using a prefix arg to `S-up/down' to specify the exact number
  2307. of minutes to shift."
  2308. :group 'org-time
  2309. :get #'(lambda (var) ; Make sure both elements are there
  2310. (if (integerp (default-value var))
  2311. (list (default-value var) 5)
  2312. (default-value var)))
  2313. :type '(list
  2314. (integer :tag "when inserting times")
  2315. (integer :tag "when modifying times")))
  2316. ;; Normalize old customizations of this variable.
  2317. (when (integerp org-time-stamp-rounding-minutes)
  2318. (setq org-time-stamp-rounding-minutes
  2319. (list org-time-stamp-rounding-minutes
  2320. org-time-stamp-rounding-minutes)))
  2321. (defcustom org-display-custom-times nil
  2322. "Non-nil means overlay custom formats over all time stamps.
  2323. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2324. To turn this on on a per-file basis, insert anywhere in the file:
  2325. #+STARTUP: customtime"
  2326. :group 'org-time
  2327. :set 'set-default
  2328. :type 'sexp)
  2329. (make-variable-buffer-local 'org-display-custom-times)
  2330. (defcustom org-time-stamp-custom-formats
  2331. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2332. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2333. These are overlaid over the default ISO format if the variable
  2334. `org-display-custom-times' is set. Time like %H:%M should be at the
  2335. end of the second format. The custom formats are also honored by export
  2336. commands, if custom time display is turned on at the time of export."
  2337. :group 'org-time
  2338. :type 'sexp)
  2339. (defun org-time-stamp-format (&optional long inactive)
  2340. "Get the right format for a time string."
  2341. (let ((f (if long (cdr org-time-stamp-formats)
  2342. (car org-time-stamp-formats))))
  2343. (if inactive
  2344. (concat "[" (substring f 1 -1) "]")
  2345. f)))
  2346. (defcustom org-time-clocksum-format "%d:%02d"
  2347. "The format string used when creating CLOCKSUM lines.
  2348. This is also used when org-mode generates a time duration."
  2349. :group 'org-time
  2350. :type 'string)
  2351. (defcustom org-time-clocksum-use-fractional nil
  2352. "If non-nil, \\[org-clock-display] uses fractional times.
  2353. org-mode generates a time duration."
  2354. :group 'org-time
  2355. :type 'boolean)
  2356. (defcustom org-time-clocksum-fractional-format "%.2f"
  2357. "The format string used when creating CLOCKSUM lines, or when
  2358. org-mode generates a time duration."
  2359. :group 'org-time
  2360. :type 'string)
  2361. (defcustom org-deadline-warning-days 14
  2362. "No. of days before expiration during which a deadline becomes active.
  2363. This variable governs the display in sparse trees and in the agenda.
  2364. When 0 or negative, it means use this number (the absolute value of it)
  2365. even if a deadline has a different individual lead time specified.
  2366. Custom commands can set this variable in the options section."
  2367. :group 'org-time
  2368. :group 'org-agenda-daily/weekly
  2369. :type 'integer)
  2370. (defcustom org-read-date-prefer-future t
  2371. "Non-nil means assume future for incomplete date input from user.
  2372. This affects the following situations:
  2373. 1. The user gives a month but not a year.
  2374. For example, if it is April and you enter \"feb 2\", this will be read
  2375. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2376. 2. The user gives a day, but no month.
  2377. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2378. read this as the third of *next* month. However, if you enter \"17\",
  2379. it will be considered as *this* month.
  2380. If you set this variable to the symbol `time', then also the following
  2381. will work:
  2382. 3. If the user gives a time, but no day. If the time is before now,
  2383. to will be interpreted as tomorrow.
  2384. Currently none of this works for ISO week specifications.
  2385. When this option is nil, the current day, month and year will always be
  2386. used as defaults.
  2387. See also `org-agenda-jump-prefer-future'."
  2388. :group 'org-time
  2389. :type '(choice
  2390. (const :tag "Never" nil)
  2391. (const :tag "Check month and day" t)
  2392. (const :tag "Check month, day, and time" time)))
  2393. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2394. "Should the agenda jump command prefer the future for incomplete dates?
  2395. The default is to do the same as configured in `org-read-date-prefer-future'.
  2396. But you can also set a deviating value here.
  2397. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2398. :group 'org-agenda
  2399. :group 'org-time
  2400. :version "24.1"
  2401. :type '(choice
  2402. (const :tag "Use org-read-date-prefer-future"
  2403. org-read-date-prefer-future)
  2404. (const :tag "Never" nil)
  2405. (const :tag "Always" t)))
  2406. (defcustom org-read-date-force-compatible-dates t
  2407. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2408. Depending on the system Emacs is running on, certain dates cannot
  2409. be represented with the type used internally to represent time.
  2410. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2411. correctly. Some systems allow for earlier dates, some for later,
  2412. some for both. One way to find out it to insert any date into an
  2413. Org buffer, putting the cursor on the year and hitting S-up and
  2414. S-down to test the range.
  2415. When this variable is set to t, the date/time prompt will not let
  2416. you specify dates outside the 1970-2037 range, so it is certain that
  2417. these dates will work in whatever version of Emacs you are
  2418. running, and also that you can move a file from one Emacs implementation
  2419. to another. WHenever Org is forcing the year for you, it will display
  2420. a message and beep.
  2421. When this variable is nil, Org will check if the date is
  2422. representable in the specific Emacs implementation you are using.
  2423. If not, it will force a year, usually the current year, and beep
  2424. to remind you. Currently this setting is not recommended because
  2425. the likelihood that you will open your Org files in an Emacs that
  2426. has limited date range is not negligible.
  2427. A workaround for this problem is to use diary sexp dates for time
  2428. stamps outside of this range."
  2429. :group 'org-time
  2430. :version "24.1"
  2431. :type 'boolean)
  2432. (defcustom org-read-date-display-live t
  2433. "Non-nil means display current interpretation of date prompt live.
  2434. This display will be in an overlay, in the minibuffer."
  2435. :group 'org-time
  2436. :type 'boolean)
  2437. (defcustom org-read-date-popup-calendar t
  2438. "Non-nil means pop up a calendar when prompting for a date.
  2439. In the calendar, the date can be selected with mouse-1. However, the
  2440. minibuffer will also be active, and you can simply enter the date as well.
  2441. When nil, only the minibuffer will be available."
  2442. :group 'org-time
  2443. :type 'boolean)
  2444. (if (fboundp 'defvaralias)
  2445. (defvaralias 'org-popup-calendar-for-date-prompt
  2446. 'org-read-date-popup-calendar))
  2447. (defcustom org-read-date-minibuffer-setup-hook nil
  2448. "Hook to be used to set up keys for the date/time interface.
  2449. Add key definitions to `minibuffer-local-map', which will be a temporary
  2450. copy."
  2451. :group 'org-time
  2452. :type 'hook)
  2453. (defcustom org-extend-today-until 0
  2454. "The hour when your day really ends. Must be an integer.
  2455. This has influence for the following applications:
  2456. - When switching the agenda to \"today\". It it is still earlier than
  2457. the time given here, the day recognized as TODAY is actually yesterday.
  2458. - When a date is read from the user and it is still before the time given
  2459. here, the current date and time will be assumed to be yesterday, 23:59.
  2460. Also, timestamps inserted in remember templates follow this rule.
  2461. IMPORTANT: This is a feature whose implementation is and likely will
  2462. remain incomplete. Really, it is only here because past midnight seems to
  2463. be the favorite working time of John Wiegley :-)"
  2464. :group 'org-time
  2465. :type 'integer)
  2466. (defcustom org-use-effective-time nil
  2467. "If non-nil, consider `org-extend-today-until' when creating timestamps.
  2468. For example, if `org-extend-today-until' is 8, and it's 4am, then the
  2469. \"effective time\" of any timestamps between midnight and 8am will be
  2470. 23:59 of the previous day."
  2471. :group 'boolean
  2472. :version "24.1"
  2473. :type 'integer)
  2474. (defcustom org-edit-timestamp-down-means-later nil
  2475. "Non-nil means S-down will increase the time in a time stamp.
  2476. When nil, S-up will increase."
  2477. :group 'org-time
  2478. :type 'boolean)
  2479. (defcustom org-calendar-follow-timestamp-change t
  2480. "Non-nil means make the calendar window follow timestamp changes.
  2481. When a timestamp is modified and the calendar window is visible, it will be
  2482. moved to the new date."
  2483. :group 'org-time
  2484. :type 'boolean)
  2485. (defgroup org-tags nil
  2486. "Options concerning tags in Org-mode."
  2487. :tag "Org Tags"
  2488. :group 'org)
  2489. (defcustom org-tag-alist nil
  2490. "List of tags allowed in Org-mode files.
  2491. When this list is nil, Org-mode will base TAG input on what is already in the
  2492. buffer.
  2493. The value of this variable is an alist, the car of each entry must be a
  2494. keyword as a string, the cdr may be a character that is used to select
  2495. that tag through the fast-tag-selection interface.
  2496. See the manual for details."
  2497. :group 'org-tags
  2498. :type '(repeat
  2499. (choice
  2500. (cons (string :tag "Tag name")
  2501. (character :tag "Access char"))
  2502. (list :tag "Start radio group"
  2503. (const :startgroup)
  2504. (option (string :tag "Group description")))
  2505. (list :tag "End radio group"
  2506. (const :endgroup)
  2507. (option (string :tag "Group description")))
  2508. (const :tag "New line" (:newline)))))
  2509. (defcustom org-tag-persistent-alist nil
  2510. "List of tags that will always appear in all Org-mode files.
  2511. This is in addition to any in buffer settings or customizations
  2512. of `org-tag-alist'.
  2513. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2514. The value of this variable is an alist, the car of each entry must be a
  2515. keyword as a string, the cdr may be a character that is used to select
  2516. that tag through the fast-tag-selection interface.
  2517. See the manual for details.
  2518. To disable these tags on a per-file basis, insert anywhere in the file:
  2519. #+STARTUP: noptag"
  2520. :group 'org-tags
  2521. :type '(repeat
  2522. (choice
  2523. (cons (string :tag "Tag name")
  2524. (character :tag "Access char"))
  2525. (const :tag "Start radio group" (:startgroup))
  2526. (const :tag "End radio group" (:endgroup))
  2527. (const :tag "New line" (:newline)))))
  2528. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2529. "If non-nil, always offer completion for all tags of all agenda files.
  2530. Instead of customizing this variable directly, you might want to
  2531. set it locally for capture buffers, because there no list of
  2532. tags in that file can be created dynamically (there are none).
  2533. (add-hook 'org-capture-mode-hook
  2534. (lambda ()
  2535. (set (make-local-variable
  2536. 'org-complete-tags-always-offer-all-agenda-tags)
  2537. t)))"
  2538. :group 'org-tags
  2539. :version "24.1"
  2540. :type 'boolean)
  2541. (defvar org-file-tags nil
  2542. "List of tags that can be inherited by all entries in the file.
  2543. The tags will be inherited if the variable `org-use-tag-inheritance'
  2544. says they should be.
  2545. This variable is populated from #+FILETAGS lines.")
  2546. (defcustom org-use-fast-tag-selection 'auto
  2547. "Non-nil means use fast tag selection scheme.
  2548. This is a special interface to select and deselect tags with single keys.
  2549. When nil, fast selection is never used.
  2550. When the symbol `auto', fast selection is used if and only if selection
  2551. characters for tags have been configured, either through the variable
  2552. `org-tag-alist' or through a #+TAGS line in the buffer.
  2553. When t, fast selection is always used and selection keys are assigned
  2554. automatically if necessary."
  2555. :group 'org-tags
  2556. :type '(choice
  2557. (const :tag "Always" t)
  2558. (const :tag "Never" nil)
  2559. (const :tag "When selection characters are configured" 'auto)))
  2560. (defcustom org-fast-tag-selection-single-key nil
  2561. "Non-nil means fast tag selection exits after first change.
  2562. When nil, you have to press RET to exit it.
  2563. During fast tag selection, you can toggle this flag with `C-c'.
  2564. This variable can also have the value `expert'. In this case, the window
  2565. displaying the tags menu is not even shown, until you press C-c again."
  2566. :group 'org-tags
  2567. :type '(choice
  2568. (const :tag "No" nil)
  2569. (const :tag "Yes" t)
  2570. (const :tag "Expert" expert)))
  2571. (defvar org-fast-tag-selection-include-todo nil
  2572. "Non-nil means fast tags selection interface will also offer TODO states.
  2573. This is an undocumented feature, you should not rely on it.")
  2574. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2575. "The column to which tags should be indented in a headline.
  2576. If this number is positive, it specifies the column. If it is negative,
  2577. it means that the tags should be flushright to that column. For example,
  2578. -80 works well for a normal 80 character screen.
  2579. When 0, place tags directly after headline text, with only one space in
  2580. between."
  2581. :group 'org-tags
  2582. :type 'integer)
  2583. (defcustom org-auto-align-tags t
  2584. "Non-nil keeps tags aligned when modifying headlines.
  2585. Some operations (i.e. demoting) change the length of a headline and
  2586. therefore shift the tags around. With this option turned on, after
  2587. each such operation the tags are again aligned to `org-tags-column'."
  2588. :group 'org-tags
  2589. :type 'boolean)
  2590. (defcustom org-use-tag-inheritance t
  2591. "Non-nil means tags in levels apply also for sublevels.
  2592. When nil, only the tags directly given in a specific line apply there.
  2593. This may also be a list of tags that should be inherited, or a regexp that
  2594. matches tags that should be inherited. Additional control is possible
  2595. with the variable `org-tags-exclude-from-inheritance' which gives an
  2596. explicit list of tags to be excluded from inheritance., even if the value of
  2597. `org-use-tag-inheritance' would select it for inheritance.
  2598. If this option is t, a match early-on in a tree can lead to a large
  2599. number of matches in the subtree when constructing the agenda or creating
  2600. a sparse tree. If you only want to see the first match in a tree during
  2601. a search, check out the variable `org-tags-match-list-sublevels'."
  2602. :group 'org-tags
  2603. :type '(choice
  2604. (const :tag "Not" nil)
  2605. (const :tag "Always" t)
  2606. (repeat :tag "Specific tags" (string :tag "Tag"))
  2607. (regexp :tag "Tags matched by regexp")))
  2608. (defcustom org-tags-exclude-from-inheritance nil
  2609. "List of tags that should never be inherited.
  2610. This is a way to exclude a few tags from inheritance. For way to do
  2611. the opposite, to actively allow inheritance for selected tags,
  2612. see the variable `org-use-tag-inheritance'."
  2613. :group 'org-tags
  2614. :type '(repeat (string :tag "Tag")))
  2615. (defun org-tag-inherit-p (tag)
  2616. "Check if TAG is one that should be inherited."
  2617. (cond
  2618. ((member tag org-tags-exclude-from-inheritance) nil)
  2619. ((eq org-use-tag-inheritance t) t)
  2620. ((not org-use-tag-inheritance) nil)
  2621. ((stringp org-use-tag-inheritance)
  2622. (string-match org-use-tag-inheritance tag))
  2623. ((listp org-use-tag-inheritance)
  2624. (member tag org-use-tag-inheritance))
  2625. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2626. (defcustom org-tags-match-list-sublevels t
  2627. "Non-nil means list also sublevels of headlines matching a search.
  2628. This variable applies to tags/property searches, and also to stuck
  2629. projects because this search is based on a tags match as well.
  2630. When set to the symbol `indented', sublevels are indented with
  2631. leading dots.
  2632. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2633. the sublevels of a headline matching a tag search often also match
  2634. the same search. Listing all of them can create very long lists.
  2635. Setting this variable to nil causes subtrees of a match to be skipped.
  2636. This variable is semi-obsolete and probably should always be true. It
  2637. is better to limit inheritance to certain tags using the variables
  2638. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2639. :group 'org-tags
  2640. :type '(choice
  2641. (const :tag "No, don't list them" nil)
  2642. (const :tag "Yes, do list them" t)
  2643. (const :tag "List them, indented with leading dots" indented)))
  2644. (defcustom org-tags-sort-function nil
  2645. "When set, tags are sorted using this function as a comparator."
  2646. :group 'org-tags
  2647. :type '(choice
  2648. (const :tag "No sorting" nil)
  2649. (const :tag "Alphabetical" string<)
  2650. (const :tag "Reverse alphabetical" string>)
  2651. (function :tag "Custom function" nil)))
  2652. (defvar org-tags-history nil
  2653. "History of minibuffer reads for tags.")
  2654. (defvar org-last-tags-completion-table nil
  2655. "The last used completion table for tags.")
  2656. (defvar org-after-tags-change-hook nil
  2657. "Hook that is run after the tags in a line have changed.")
  2658. (defgroup org-properties nil
  2659. "Options concerning properties in Org-mode."
  2660. :tag "Org Properties"
  2661. :group 'org)
  2662. (defcustom org-property-format "%-10s %s"
  2663. "How property key/value pairs should be formatted by `indent-line'.
  2664. When `indent-line' hits a property definition, it will format the line
  2665. according to this format, mainly to make sure that the values are
  2666. lined-up with respect to each other."
  2667. :group 'org-properties
  2668. :type 'string)
  2669. (defcustom org-properties-postprocess-alist nil
  2670. "Alist of properties and functions to adjust inserted values.
  2671. Elements of this alist must be of the form
  2672. ([string] [function])
  2673. where [string] must be a property name and [function] must be a
  2674. lambda expression: this lambda expression must take one argument,
  2675. the value to adjust, and return the new value as a string.
  2676. For example, this element will allow the property \"Remaining\"
  2677. to be updated wrt the relation between the \"Effort\" property
  2678. and the clock summary:
  2679. ((\"Remaining\" (lambda(value)
  2680. (let ((clocksum (org-clock-sum-current-item))
  2681. (effort (org-duration-string-to-minutes
  2682. (org-entry-get (point) \"Effort\"))))
  2683. (org-minutes-to-hh:mm-string (- effort clocksum))))))"
  2684. :group 'org-properties
  2685. :version "24.1"
  2686. :type 'alist)
  2687. (defcustom org-use-property-inheritance nil
  2688. "Non-nil means properties apply also for sublevels.
  2689. This setting is chiefly used during property searches. Turning it on can
  2690. cause significant overhead when doing a search, which is why it is not
  2691. on by default.
  2692. When nil, only the properties directly given in the current entry count.
  2693. When t, every property is inherited. The value may also be a list of
  2694. properties that should have inheritance, or a regular expression matching
  2695. properties that should be inherited.
  2696. However, note that some special properties use inheritance under special
  2697. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2698. and the properties ending in \"_ALL\" when they are used as descriptor
  2699. for valid values of a property.
  2700. Note for programmers:
  2701. When querying an entry with `org-entry-get', you can control if inheritance
  2702. should be used. By default, `org-entry-get' looks only at the local
  2703. properties. You can request inheritance by setting the inherit argument
  2704. to t (to force inheritance) or to `selective' (to respect the setting
  2705. in this variable)."
  2706. :group 'org-properties
  2707. :type '(choice
  2708. (const :tag "Not" nil)
  2709. (const :tag "Always" t)
  2710. (repeat :tag "Specific properties" (string :tag "Property"))
  2711. (regexp :tag "Properties matched by regexp")))
  2712. (defun org-property-inherit-p (property)
  2713. "Check if PROPERTY is one that should be inherited."
  2714. (cond
  2715. ((eq org-use-property-inheritance t) t)
  2716. ((not org-use-property-inheritance) nil)
  2717. ((stringp org-use-property-inheritance)
  2718. (string-match org-use-property-inheritance property))
  2719. ((listp org-use-property-inheritance)
  2720. (member property org-use-property-inheritance))
  2721. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2722. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2723. "The default column format, if no other format has been defined.
  2724. This variable can be set on the per-file basis by inserting a line
  2725. #+COLUMNS: %25ITEM ....."
  2726. :group 'org-properties
  2727. :type 'string)
  2728. (defcustom org-columns-ellipses ".."
  2729. "The ellipses to be used when a field in column view is truncated.
  2730. When this is the empty string, as many characters as possible are shown,
  2731. but then there will be no visual indication that the field has been truncated.
  2732. When this is a string of length N, the last N characters of a truncated
  2733. field are replaced by this string. If the column is narrower than the
  2734. ellipses string, only part of the ellipses string will be shown."
  2735. :group 'org-properties
  2736. :type 'string)
  2737. (defcustom org-columns-modify-value-for-display-function nil
  2738. "Function that modifies values for display in column view.
  2739. For example, it can be used to cut out a certain part from a time stamp.
  2740. The function must take 2 arguments:
  2741. column-title The title of the column (*not* the property name)
  2742. value The value that should be modified.
  2743. The function should return the value that should be displayed,
  2744. or nil if the normal value should be used."
  2745. :group 'org-properties
  2746. :type 'function)
  2747. (defcustom org-effort-property "Effort"
  2748. "The property that is being used to keep track of effort estimates.
  2749. Effort estimates given in this property need to have the format H:MM."
  2750. :group 'org-properties
  2751. :group 'org-progress
  2752. :type '(string :tag "Property"))
  2753. (defconst org-global-properties-fixed
  2754. '(("VISIBILITY_ALL" . "folded children content all")
  2755. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  2756. "List of property/value pairs that can be inherited by any entry.
  2757. These are fixed values, for the preset properties. The user variable
  2758. that can be used to add to this list is `org-global-properties'.
  2759. The entries in this list are cons cells where the car is a property
  2760. name and cdr is a string with the value. If the value represents
  2761. multiple items like an \"_ALL\" property, separate the items by
  2762. spaces.")
  2763. (defcustom org-global-properties nil
  2764. "List of property/value pairs that can be inherited by any entry.
  2765. This list will be combined with the constant `org-global-properties-fixed'.
  2766. The entries in this list are cons cells where the car is a property
  2767. name and cdr is a string with the value.
  2768. You can set buffer-local values for the same purpose in the variable
  2769. `org-file-properties' this by adding lines like
  2770. #+PROPERTY: NAME VALUE"
  2771. :group 'org-properties
  2772. :type '(repeat
  2773. (cons (string :tag "Property")
  2774. (string :tag "Value"))))
  2775. (defvar org-file-properties nil
  2776. "List of property/value pairs that can be inherited by any entry.
  2777. Valid for the current buffer.
  2778. This variable is populated from #+PROPERTY lines.")
  2779. (make-variable-buffer-local 'org-file-properties)
  2780. (defgroup org-agenda nil
  2781. "Options concerning agenda views in Org-mode."
  2782. :tag "Org Agenda"
  2783. :group 'org)
  2784. (defvar org-category nil
  2785. "Variable used by org files to set a category for agenda display.
  2786. Such files should use a file variable to set it, for example
  2787. # -*- mode: org; org-category: \"ELisp\"
  2788. or contain a special line
  2789. #+CATEGORY: ELisp
  2790. If the file does not specify a category, then file's base name
  2791. is used instead.")
  2792. (make-variable-buffer-local 'org-category)
  2793. (put 'org-category 'safe-local-variable #'(lambda (x) (or (symbolp x) (stringp x))))
  2794. (defcustom org-agenda-files nil
  2795. "The files to be used for agenda display.
  2796. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2797. \\[org-remove-file]. You can also use customize to edit the list.
  2798. If an entry is a directory, all files in that directory that are matched by
  2799. `org-agenda-file-regexp' will be part of the file list.
  2800. If the value of the variable is not a list but a single file name, then
  2801. the list of agenda files is actually stored and maintained in that file, one
  2802. agenda file per line. In this file paths can be given relative to
  2803. `org-directory'. Tilde expansion and environment variable substitution
  2804. are also made."
  2805. :group 'org-agenda
  2806. :type '(choice
  2807. (repeat :tag "List of files and directories" file)
  2808. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2809. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2810. "Regular expression to match files for `org-agenda-files'.
  2811. If any element in the list in that variable contains a directory instead
  2812. of a normal file, all files in that directory that are matched by this
  2813. regular expression will be included."
  2814. :group 'org-agenda
  2815. :type 'regexp)
  2816. (defcustom org-agenda-text-search-extra-files nil
  2817. "List of extra files to be searched by text search commands.
  2818. These files will be search in addition to the agenda files by the
  2819. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2820. Note that these files will only be searched for text search commands,
  2821. not for the other agenda views like todo lists, tag searches or the weekly
  2822. agenda. This variable is intended to list notes and possibly archive files
  2823. that should also be searched by these two commands.
  2824. In fact, if the first element in the list is the symbol `agenda-archives',
  2825. than all archive files of all agenda files will be added to the search
  2826. scope."
  2827. :group 'org-agenda
  2828. :type '(set :greedy t
  2829. (const :tag "Agenda Archives" agenda-archives)
  2830. (repeat :inline t (file))))
  2831. (if (fboundp 'defvaralias)
  2832. (defvaralias 'org-agenda-multi-occur-extra-files
  2833. 'org-agenda-text-search-extra-files))
  2834. (defcustom org-agenda-skip-unavailable-files nil
  2835. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2836. A nil value means to remove them, after a query, from the list."
  2837. :group 'org-agenda
  2838. :type 'boolean)
  2839. (defcustom org-calendar-to-agenda-key [?c]
  2840. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2841. The command `org-calendar-goto-agenda' will be bound to this key. The
  2842. default is the character `c' because then `c' can be used to switch back and
  2843. forth between agenda and calendar."
  2844. :group 'org-agenda
  2845. :type 'sexp)
  2846. (defcustom org-calendar-agenda-action-key [?k]
  2847. "The key to be installed in `calendar-mode-map' for agenda-action.
  2848. The command `org-agenda-action' will be bound to this key. The
  2849. default is the character `k' because we use the same key in the agenda."
  2850. :group 'org-agenda
  2851. :type 'sexp)
  2852. (defcustom org-calendar-insert-diary-entry-key [?i]
  2853. "The key to be installed in `calendar-mode-map' for adding diary entries.
  2854. This option is irrelevant until `org-agenda-diary-file' has been configured
  2855. to point to an Org-mode file. When that is the case, the command
  2856. `org-agenda-diary-entry' will be bound to the key given here, by default
  2857. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  2858. if you want to continue doing this, you need to change this to a different
  2859. key."
  2860. :group 'org-agenda
  2861. :type 'sexp)
  2862. (defcustom org-agenda-diary-file 'diary-file
  2863. "File to which to add new entries with the `i' key in agenda and calendar.
  2864. When this is the symbol `diary-file', the functionality in the Emacs
  2865. calendar will be used to add entries to the `diary-file'. But when this
  2866. points to a file, `org-agenda-diary-entry' will be used instead."
  2867. :group 'org-agenda
  2868. :type '(choice
  2869. (const :tag "The standard Emacs diary file" diary-file)
  2870. (file :tag "Special Org file diary entries")))
  2871. (eval-after-load "calendar"
  2872. '(progn
  2873. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2874. 'org-calendar-goto-agenda)
  2875. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2876. 'org-agenda-action)
  2877. (add-hook 'calendar-mode-hook
  2878. (lambda ()
  2879. (unless (eq org-agenda-diary-file 'diary-file)
  2880. (define-key calendar-mode-map
  2881. org-calendar-insert-diary-entry-key
  2882. 'org-agenda-diary-entry))))))
  2883. (defgroup org-latex nil
  2884. "Options for embedding LaTeX code into Org-mode."
  2885. :tag "Org LaTeX"
  2886. :group 'org)
  2887. (defcustom org-format-latex-options
  2888. '(:foreground default :background default :scale 1.0
  2889. :html-foreground "Black" :html-background "Transparent"
  2890. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2891. "Options for creating images from LaTeX fragments.
  2892. This is a property list with the following properties:
  2893. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2894. `default' means use the foreground of the default face.
  2895. :background the background color, or \"Transparent\".
  2896. `default' means use the background of the default face.
  2897. :scale a scaling factor for the size of the images, to get more pixels
  2898. :html-foreground, :html-background, :html-scale
  2899. the same numbers for HTML export.
  2900. :matchers a list indicating which matchers should be used to
  2901. find LaTeX fragments. Valid members of this list are:
  2902. \"begin\" find environments
  2903. \"$1\" find single characters surrounded by $.$
  2904. \"$\" find math expressions surrounded by $...$
  2905. \"$$\" find math expressions surrounded by $$....$$
  2906. \"\\(\" find math expressions surrounded by \\(...\\)
  2907. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2908. :group 'org-latex
  2909. :type 'plist)
  2910. (defcustom org-format-latex-signal-error t
  2911. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  2912. When nil, just push out a message."
  2913. :group 'org-latex
  2914. :version "24.1"
  2915. :type 'boolean)
  2916. (defcustom org-latex-to-mathml-jar-file nil
  2917. "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
  2918. Use this to specify additional executable file say a jar file.
  2919. When using MathToWeb as the converter, specify the full-path to
  2920. your mathtoweb.jar file."
  2921. :group 'org-latex
  2922. :version "24.1"
  2923. :type '(choice
  2924. (const :tag "None" nil)
  2925. (file :tag "JAR file" :must-match t)))
  2926. (defcustom org-latex-to-mathml-convert-command nil
  2927. "Command to convert LaTeX fragments to MathML.
  2928. Replace format-specifiers in the command as noted below and use
  2929. `shell-command' to convert LaTeX to MathML.
  2930. %j: Executable file in fully expanded form as specified by
  2931. `org-latex-to-mathml-jar-file'.
  2932. %I: Input LaTeX file in fully expanded form
  2933. %o: Output MathML file
  2934. This command is used by `org-create-math-formula'.
  2935. When using MathToWeb as the converter, set this to
  2936. \"java -jar %j -unicode -force -df %o %I\"."
  2937. :group 'org-latex
  2938. :version "24.1"
  2939. :type '(choice
  2940. (const :tag "None" nil)
  2941. (string :tag "\nShell command")))
  2942. (defun org-format-latex-mathml-available-p ()
  2943. "Return t if `org-latex-to-mathml-convert-command' is usable."
  2944. (save-match-data
  2945. (when (and (boundp 'org-latex-to-mathml-convert-command)
  2946. org-latex-to-mathml-convert-command)
  2947. (let ((executable (car (split-string
  2948. org-latex-to-mathml-convert-command))))
  2949. (when (executable-find executable)
  2950. (if (string-match
  2951. "%j" org-latex-to-mathml-convert-command)
  2952. (file-readable-p org-latex-to-mathml-jar-file)
  2953. t))))))
  2954. (defcustom org-format-latex-header "\\documentclass{article}
  2955. \\usepackage[usenames]{color}
  2956. \\usepackage{amsmath}
  2957. \\usepackage[mathscr]{eucal}
  2958. \\pagestyle{empty} % do not remove
  2959. \[PACKAGES]
  2960. \[DEFAULT-PACKAGES]
  2961. % The settings below are copied from fullpage.sty
  2962. \\setlength{\\textwidth}{\\paperwidth}
  2963. \\addtolength{\\textwidth}{-3cm}
  2964. \\setlength{\\oddsidemargin}{1.5cm}
  2965. \\addtolength{\\oddsidemargin}{-2.54cm}
  2966. \\setlength{\\evensidemargin}{\\oddsidemargin}
  2967. \\setlength{\\textheight}{\\paperheight}
  2968. \\addtolength{\\textheight}{-\\headheight}
  2969. \\addtolength{\\textheight}{-\\headsep}
  2970. \\addtolength{\\textheight}{-\\footskip}
  2971. \\addtolength{\\textheight}{-3cm}
  2972. \\setlength{\\topmargin}{1.5cm}
  2973. \\addtolength{\\topmargin}{-2.54cm}"
  2974. "The document header used for processing LaTeX fragments.
  2975. It is imperative that this header make sure that no page number
  2976. appears on the page. The package defined in the variables
  2977. `org-export-latex-default-packages-alist' and `org-export-latex-packages-alist'
  2978. will either replace the placeholder \"[PACKAGES]\" in this header, or they
  2979. will be appended."
  2980. :group 'org-latex
  2981. :type 'string)
  2982. (defvar org-format-latex-header-extra nil)
  2983. (defun org-set-packages-alist (var val)
  2984. "Set the packages alist and make sure it has 3 elements per entry."
  2985. (set var (mapcar (lambda (x)
  2986. (if (and (consp x) (= (length x) 2))
  2987. (list (car x) (nth 1 x) t)
  2988. x))
  2989. val)))
  2990. (defun org-get-packages-alist (var)
  2991. "Get the packages alist and make sure it has 3 elements per entry."
  2992. (mapcar (lambda (x)
  2993. (if (and (consp x) (= (length x) 2))
  2994. (list (car x) (nth 1 x) t)
  2995. x))
  2996. (default-value var)))
  2997. ;; The following variables are defined here because is it also used
  2998. ;; when formatting latex fragments. Originally it was part of the
  2999. ;; LaTeX exporter, which is why the name includes "export".
  3000. (defcustom org-export-latex-default-packages-alist
  3001. '(("AUTO" "inputenc" t)
  3002. ("T1" "fontenc" t)
  3003. ("" "fixltx2e" nil)
  3004. ("" "graphicx" t)
  3005. ("" "longtable" nil)
  3006. ("" "float" nil)
  3007. ("" "wrapfig" nil)
  3008. ("" "soul" t)
  3009. ("" "textcomp" t)
  3010. ("" "marvosym" t)
  3011. ("" "wasysym" t)
  3012. ("" "latexsym" t)
  3013. ("" "amssymb" t)
  3014. ("" "hyperref" nil)
  3015. "\\tolerance=1000"
  3016. )
  3017. "Alist of default packages to be inserted in the header.
  3018. Change this only if one of the packages here causes an incompatibility
  3019. with another package you are using.
  3020. The packages in this list are needed by one part or another of Org-mode
  3021. to function properly.
  3022. - inputenc, fontenc: for basic font and character selection
  3023. - textcomp, marvosymb, wasysym, latexsym, amssym: for various symbols used
  3024. for interpreting the entities in `org-entities'. You can skip some of these
  3025. packages if you don't use any of the symbols in it.
  3026. - graphicx: for including images
  3027. - float, wrapfig: for figure placement
  3028. - longtable: for long tables
  3029. - hyperref: for cross references
  3030. Therefore you should not modify this variable unless you know what you
  3031. are doing. The one reason to change it anyway is that you might be loading
  3032. some other package that conflicts with one of the default packages.
  3033. Each cell is of the format \( \"options\" \"package\" snippet-flag\).
  3034. If SNIPPET-FLAG is t, the package also needs to be included when
  3035. compiling LaTeX snippets into images for inclusion into HTML."
  3036. :group 'org-export-latex
  3037. :set 'org-set-packages-alist
  3038. :get 'org-get-packages-alist
  3039. :version "24.1"
  3040. :type '(repeat
  3041. (choice
  3042. (list :tag "options/package pair"
  3043. (string :tag "options")
  3044. (string :tag "package")
  3045. (boolean :tag "Snippet"))
  3046. (string :tag "A line of LaTeX"))))
  3047. (defcustom org-export-latex-packages-alist nil
  3048. "Alist of packages to be inserted in every LaTeX header.
  3049. These will be inserted after `org-export-latex-default-packages-alist'.
  3050. Each cell is of the format \( \"options\" \"package\" snippet-flag \).
  3051. SNIPPET-FLAG, when t, indicates that this package is also needed when
  3052. turning LaTeX snippets into images for inclusion into HTML.
  3053. Make sure that you only list packages here which:
  3054. - you want in every file
  3055. - do not conflict with the default packages in
  3056. `org-export-latex-default-packages-alist'
  3057. - do not conflict with the setup in `org-format-latex-header'."
  3058. :group 'org-export-latex
  3059. :set 'org-set-packages-alist
  3060. :get 'org-get-packages-alist
  3061. :type '(repeat
  3062. (choice
  3063. (list :tag "options/package pair"
  3064. (string :tag "options")
  3065. (string :tag "package")
  3066. (boolean :tag "Snippet"))
  3067. (string :tag "A line of LaTeX"))))
  3068. (defgroup org-appearance nil
  3069. "Settings for Org-mode appearance."
  3070. :tag "Org Appearance"
  3071. :group 'org)
  3072. (defcustom org-level-color-stars-only nil
  3073. "Non-nil means fontify only the stars in each headline.
  3074. When nil, the entire headline is fontified.
  3075. Changing it requires restart of `font-lock-mode' to become effective
  3076. also in regions already fontified."
  3077. :group 'org-appearance
  3078. :type 'boolean)
  3079. (defcustom org-hide-leading-stars nil
  3080. "Non-nil means hide the first N-1 stars in a headline.
  3081. This works by using the face `org-hide' for these stars. This
  3082. face is white for a light background, and black for a dark
  3083. background. You may have to customize the face `org-hide' to
  3084. make this work.
  3085. Changing it requires restart of `font-lock-mode' to become effective
  3086. also in regions already fontified.
  3087. You may also set this on a per-file basis by adding one of the following
  3088. lines to the buffer:
  3089. #+STARTUP: hidestars
  3090. #+STARTUP: showstars"
  3091. :group 'org-appearance
  3092. :type 'boolean)
  3093. (defcustom org-hidden-keywords nil
  3094. "List of symbols corresponding to keywords to be hidden the org buffer.
  3095. For example, a value '(title) for this list will make the document's title
  3096. appear in the buffer without the initial #+TITLE: keyword."
  3097. :group 'org-appearance
  3098. :version "24.1"
  3099. :type '(set (const :tag "#+AUTHOR" author)
  3100. (const :tag "#+DATE" date)
  3101. (const :tag "#+EMAIL" email)
  3102. (const :tag "#+TITLE" title)))
  3103. (defcustom org-fontify-done-headline nil
  3104. "Non-nil means change the face of a headline if it is marked DONE.
  3105. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3106. When this is non-nil, the headline after the keyword is set to the
  3107. `org-headline-done' as an additional indication."
  3108. :group 'org-appearance
  3109. :type 'boolean)
  3110. (defcustom org-fontify-emphasized-text t
  3111. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3112. Changing this variable requires a restart of Emacs to take effect."
  3113. :group 'org-appearance
  3114. :type 'boolean)
  3115. (defcustom org-fontify-whole-heading-line nil
  3116. "Non-nil means fontify the whole line for headings.
  3117. This is useful when setting a background color for the
  3118. org-level-* faces."
  3119. :group 'org-appearance
  3120. :type 'boolean)
  3121. (defcustom org-highlight-latex-fragments-and-specials nil
  3122. "Non-nil means fontify what is treated specially by the exporters."
  3123. :group 'org-appearance
  3124. :type 'boolean)
  3125. (defcustom org-hide-emphasis-markers nil
  3126. "Non-nil mean font-lock should hide the emphasis marker characters."
  3127. :group 'org-appearance
  3128. :type 'boolean)
  3129. (defcustom org-pretty-entities nil
  3130. "Non-nil means show entities as UTF8 characters.
  3131. When nil, the \\name form remains in the buffer."
  3132. :group 'org-appearance
  3133. :version "24.1"
  3134. :type 'boolean)
  3135. (defcustom org-pretty-entities-include-sub-superscripts t
  3136. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  3137. :group 'org-appearance
  3138. :version "24.1"
  3139. :type 'boolean)
  3140. (defvar org-emph-re nil
  3141. "Regular expression for matching emphasis.
  3142. After a match, the match groups contain these elements:
  3143. 0 The match of the full regular expression, including the characters
  3144. before and after the proper match
  3145. 1 The character before the proper match, or empty at beginning of line
  3146. 2 The proper match, including the leading and trailing markers
  3147. 3 The leading marker like * or /, indicating the type of highlighting
  3148. 4 The text between the emphasis markers, not including the markers
  3149. 5 The character after the match, empty at the end of a line")
  3150. (defvar org-verbatim-re nil
  3151. "Regular expression for matching verbatim text.")
  3152. (defvar org-emphasis-regexp-components) ; defined just below
  3153. (defvar org-emphasis-alist) ; defined just below
  3154. (defun org-set-emph-re (var val)
  3155. "Set variable and compute the emphasis regular expression."
  3156. (set var val)
  3157. (when (and (boundp 'org-emphasis-alist)
  3158. (boundp 'org-emphasis-regexp-components)
  3159. org-emphasis-alist org-emphasis-regexp-components)
  3160. (let* ((e org-emphasis-regexp-components)
  3161. (pre (car e))
  3162. (post (nth 1 e))
  3163. (border (nth 2 e))
  3164. (body (nth 3 e))
  3165. (nl (nth 4 e))
  3166. (body1 (concat body "*?"))
  3167. (markers (mapconcat 'car org-emphasis-alist ""))
  3168. (vmarkers (mapconcat
  3169. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  3170. org-emphasis-alist "")))
  3171. ;; make sure special characters appear at the right position in the class
  3172. (if (string-match "\\^" markers)
  3173. (setq markers (concat (replace-match "" t t markers) "^")))
  3174. (if (string-match "-" markers)
  3175. (setq markers (concat (replace-match "" t t markers) "-")))
  3176. (if (string-match "\\^" vmarkers)
  3177. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3178. (if (string-match "-" vmarkers)
  3179. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3180. (if (> nl 0)
  3181. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3182. (int-to-string nl) "\\}")))
  3183. ;; Make the regexp
  3184. (setq org-emph-re
  3185. (concat "\\([" pre "]\\|^\\)"
  3186. "\\("
  3187. "\\([" markers "]\\)"
  3188. "\\("
  3189. "[^" border "]\\|"
  3190. "[^" border "]"
  3191. body1
  3192. "[^" border "]"
  3193. "\\)"
  3194. "\\3\\)"
  3195. "\\([" post "]\\|$\\)"))
  3196. (setq org-verbatim-re
  3197. (concat "\\([" pre "]\\|^\\)"
  3198. "\\("
  3199. "\\([" vmarkers "]\\)"
  3200. "\\("
  3201. "[^" border "]\\|"
  3202. "[^" border "]"
  3203. body1
  3204. "[^" border "]"
  3205. "\\)"
  3206. "\\3\\)"
  3207. "\\([" post "]\\|$\\)")))))
  3208. (defcustom org-emphasis-regexp-components
  3209. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  3210. "Components used to build the regular expression for emphasis.
  3211. This is a list with five entries. Terminology: In an emphasis string
  3212. like \" *strong word* \", we call the initial space PREMATCH, the final
  3213. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3214. and \"trong wor\" is the body. The different components in this variable
  3215. specify what is allowed/forbidden in each part:
  3216. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3217. post Chars allowed as postmatch. End of line will be allowed too.
  3218. border The chars *forbidden* as border characters.
  3219. body-regexp A regexp like \".\" to match a body character. Don't use
  3220. non-shy groups here, and don't allow newline here.
  3221. newline The maximum number of newlines allowed in an emphasis exp.
  3222. Use customize to modify this, or restart Emacs after changing it."
  3223. :group 'org-appearance
  3224. :set 'org-set-emph-re
  3225. :type '(list
  3226. (sexp :tag "Allowed chars in pre ")
  3227. (sexp :tag "Allowed chars in post ")
  3228. (sexp :tag "Forbidden chars in border ")
  3229. (sexp :tag "Regexp for body ")
  3230. (integer :tag "number of newlines allowed")
  3231. (option (boolean :tag "Please ignore this button"))))
  3232. (defcustom org-emphasis-alist
  3233. `(("*" bold "<b>" "</b>")
  3234. ("/" italic "<i>" "</i>")
  3235. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  3236. ("=" org-code "<code>" "</code>" verbatim)
  3237. ("~" org-verbatim "<code>" "</code>" verbatim)
  3238. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  3239. "<del>" "</del>")
  3240. )
  3241. "Special syntax for emphasized text.
  3242. Text starting and ending with a special character will be emphasized, for
  3243. example *bold*, _underlined_ and /italic/. This variable sets the marker
  3244. characters, the face to be used by font-lock for highlighting in Org-mode
  3245. Emacs buffers, and the HTML tags to be used for this.
  3246. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  3247. For DocBook export, see the variable `org-export-docbook-emphasis-alist'.
  3248. Use customize to modify this, or restart Emacs after changing it."
  3249. :group 'org-appearance
  3250. :set 'org-set-emph-re
  3251. :type '(repeat
  3252. (list
  3253. (string :tag "Marker character")
  3254. (choice
  3255. (face :tag "Font-lock-face")
  3256. (plist :tag "Face property list"))
  3257. (string :tag "HTML start tag")
  3258. (string :tag "HTML end tag")
  3259. (option (const verbatim)))))
  3260. (defvar org-protecting-blocks
  3261. '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
  3262. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3263. This is needed for font-lock setup.")
  3264. ;;; Miscellaneous options
  3265. (defgroup org-completion nil
  3266. "Completion in Org-mode."
  3267. :tag "Org Completion"
  3268. :group 'org)
  3269. (defcustom org-completion-use-ido nil
  3270. "Non-nil means use ido completion wherever possible.
  3271. Note that `ido-mode' must be active for this variable to be relevant.
  3272. If you decide to turn this variable on, you might well want to turn off
  3273. `org-outline-path-complete-in-steps'.
  3274. See also `org-completion-use-iswitchb'."
  3275. :group 'org-completion
  3276. :type 'boolean)
  3277. (defcustom org-completion-use-iswitchb nil
  3278. "Non-nil means use iswitchb completion wherever possible.
  3279. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3280. If you decide to turn this variable on, you might well want to turn off
  3281. `org-outline-path-complete-in-steps'.
  3282. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3283. :group 'org-completion
  3284. :type 'boolean)
  3285. (defcustom org-completion-fallback-command 'hippie-expand
  3286. "The expansion command called by \\[pcomplete] in normal context.
  3287. Normal means, no org-mode-specific context."
  3288. :group 'org-completion
  3289. :type 'function)
  3290. ;;; Functions and variables from their packages
  3291. ;; Declared here to avoid compiler warnings
  3292. ;; XEmacs only
  3293. (defvar outline-mode-menu-heading)
  3294. (defvar outline-mode-menu-show)
  3295. (defvar outline-mode-menu-hide)
  3296. (defvar zmacs-regions) ; XEmacs regions
  3297. ;; Emacs only
  3298. (defvar mark-active)
  3299. ;; Various packages
  3300. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  3301. (declare-function calendar-forward-day "cal-move" (arg))
  3302. (declare-function calendar-goto-date "cal-move" (date))
  3303. (declare-function calendar-goto-today "cal-move" ())
  3304. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3305. (defvar calc-embedded-close-formula)
  3306. (defvar calc-embedded-open-formula)
  3307. (declare-function cdlatex-tab "ext:cdlatex" ())
  3308. (declare-function cdlatex-compute-tables "ext:cdlatex" ())
  3309. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3310. (defvar font-lock-unfontify-region-function)
  3311. (declare-function iswitchb-read-buffer "iswitchb"
  3312. (prompt &optional default require-match start matches-set))
  3313. (defvar iswitchb-temp-buflist)
  3314. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3315. (defvar org-agenda-tags-todo-honor-ignore-options)
  3316. (declare-function org-agenda-skip "org-agenda" ())
  3317. (declare-function
  3318. org-agenda-format-item "org-agenda"
  3319. (extra txt &optional category tags dotime noprefix remove-re habitp))
  3320. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3321. (declare-function org-agenda-change-all-lines "org-agenda"
  3322. (newhead hdmarker &optional fixface just-this))
  3323. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3324. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3325. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3326. (beg end))
  3327. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3328. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3329. "org-agenda" (&optional end))
  3330. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3331. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3332. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3333. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3334. (declare-function org-indent-mode "org-indent" (&optional arg))
  3335. (declare-function parse-time-string "parse-time" (string))
  3336. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3337. (declare-function org-export-latex-fix-inputenc "org-latex" ())
  3338. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3339. (defvar remember-data-file)
  3340. (defvar texmathp-why)
  3341. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3342. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3343. (defvar w3m-current-url)
  3344. (defvar w3m-current-title)
  3345. (defvar org-latex-regexps)
  3346. ;;; Autoload and prepare some org modules
  3347. ;; Some table stuff that needs to be defined here, because it is used
  3348. ;; by the functions setting up org-mode or checking for table context.
  3349. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  3350. "Detect an org-type or table-type table.")
  3351. (defconst org-table-line-regexp "^[ \t]*|"
  3352. "Detect an org-type table line.")
  3353. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  3354. "Detect an org-type table line.")
  3355. (defconst org-table-hline-regexp "^[ \t]*|-"
  3356. "Detect an org-type table hline.")
  3357. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  3358. "Detect a table-type table hline.")
  3359. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  3360. "Detect the first line outside a table when searching from within it.
  3361. This works for both table types.")
  3362. ;; Autoload the functions in org-table.el that are needed by functions here.
  3363. (eval-and-compile
  3364. (org-autoload "org-table"
  3365. '(org-table-align org-table-begin org-table-blank-field
  3366. org-table-convert org-table-convert-region org-table-copy-down
  3367. org-table-copy-region org-table-create
  3368. org-table-create-or-convert-from-region
  3369. org-table-create-with-table.el org-table-current-dline
  3370. org-table-cut-region org-table-delete-column org-table-edit-field
  3371. org-table-edit-formulas org-table-end org-table-eval-formula
  3372. org-table-export org-table-field-info
  3373. org-table-get-stored-formulas org-table-goto-column
  3374. org-table-hline-and-move org-table-import org-table-insert-column
  3375. org-table-insert-hline org-table-insert-row org-table-iterate
  3376. org-table-justify-field-maybe org-table-kill-row
  3377. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  3378. org-table-move-column org-table-move-column-left
  3379. org-table-move-column-right org-table-move-row
  3380. org-table-move-row-down org-table-move-row-up
  3381. org-table-next-field org-table-next-row org-table-paste-rectangle
  3382. org-table-previous-field org-table-recalculate
  3383. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  3384. org-table-toggle-coordinate-overlays
  3385. org-table-toggle-formula-debugger org-table-wrap-region
  3386. orgtbl-mode turn-on-orgtbl org-table-to-lisp
  3387. orgtbl-to-generic orgtbl-to-tsv orgtbl-to-csv orgtbl-to-latex
  3388. orgtbl-to-orgtbl orgtbl-to-html orgtbl-to-texinfo)))
  3389. (defun org-at-table-p (&optional table-type)
  3390. "Return t if the cursor is inside an org-type table.
  3391. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  3392. (if org-enable-table-editor
  3393. (save-excursion
  3394. (beginning-of-line 1)
  3395. (looking-at (if table-type org-table-any-line-regexp
  3396. org-table-line-regexp)))
  3397. nil))
  3398. (defsubst org-table-p () (org-at-table-p))
  3399. (defun org-at-table.el-p ()
  3400. "Return t if and only if we are at a table.el table."
  3401. (and (org-at-table-p 'any)
  3402. (save-excursion
  3403. (goto-char (org-table-begin 'any))
  3404. (looking-at org-table1-hline-regexp))))
  3405. (defun org-table-recognize-table.el ()
  3406. "If there is a table.el table nearby, recognize it and move into it."
  3407. (if org-table-tab-recognizes-table.el
  3408. (if (org-at-table.el-p)
  3409. (progn
  3410. (beginning-of-line 1)
  3411. (if (looking-at org-table-dataline-regexp)
  3412. nil
  3413. (if (looking-at org-table1-hline-regexp)
  3414. (progn
  3415. (beginning-of-line 2)
  3416. (if (looking-at org-table-any-border-regexp)
  3417. (beginning-of-line -1)))))
  3418. (if (re-search-forward "|" (org-table-end t) t)
  3419. (progn
  3420. (require 'table)
  3421. (if (table--at-cell-p (point))
  3422. t
  3423. (message "recognizing table.el table...")
  3424. (table-recognize-table)
  3425. (message "recognizing table.el table...done")))
  3426. (error "This should not happen"))
  3427. t)
  3428. nil)
  3429. nil))
  3430. (defun org-at-table-hline-p ()
  3431. "Return t if the cursor is inside a hline in a table."
  3432. (if org-enable-table-editor
  3433. (save-excursion
  3434. (beginning-of-line 1)
  3435. (looking-at org-table-hline-regexp))
  3436. nil))
  3437. (defvar org-table-clean-did-remove-column nil)
  3438. (defun org-table-map-tables (function &optional quietly)
  3439. "Apply FUNCTION to the start of all tables in the buffer."
  3440. (save-excursion
  3441. (save-restriction
  3442. (widen)
  3443. (goto-char (point-min))
  3444. (while (re-search-forward org-table-any-line-regexp nil t)
  3445. (unless quietly
  3446. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
  3447. (beginning-of-line 1)
  3448. (when (looking-at org-table-line-regexp)
  3449. (save-excursion (funcall function))
  3450. (or (looking-at org-table-line-regexp)
  3451. (forward-char 1)))
  3452. (re-search-forward org-table-any-border-regexp nil 1))))
  3453. (unless quietly (message "Mapping tables: done")))
  3454. ;; Declare and autoload functions from org-exp.el & Co
  3455. (declare-function org-default-export-plist "org-exp")
  3456. (declare-function org-infile-export-plist "org-exp")
  3457. (declare-function org-get-current-options "org-exp")
  3458. (eval-and-compile
  3459. (org-autoload "org-exp"
  3460. '(org-export org-export-visible
  3461. org-insert-export-options-template
  3462. org-table-clean-before-export))
  3463. (org-autoload "org-ascii"
  3464. '(org-export-as-ascii org-export-ascii-preprocess
  3465. org-export-as-ascii-to-buffer org-replace-region-by-ascii
  3466. org-export-region-as-ascii))
  3467. (org-autoload "org-latex"
  3468. '(org-export-as-latex-batch org-export-as-latex-to-buffer
  3469. org-replace-region-by-latex org-export-region-as-latex
  3470. org-export-as-latex org-export-as-pdf
  3471. org-export-as-pdf-and-open))
  3472. (org-autoload "org-html"
  3473. '(org-export-as-html-and-open
  3474. org-export-as-html-batch org-export-as-html-to-buffer
  3475. org-replace-region-by-html org-export-region-as-html
  3476. org-export-as-html))
  3477. (org-autoload "org-docbook"
  3478. '(org-export-as-docbook-batch org-export-as-docbook-to-buffer
  3479. org-replace-region-by-docbook org-export-region-as-docbook
  3480. org-export-as-docbook-pdf org-export-as-docbook-pdf-and-open
  3481. org-export-as-docbook))
  3482. (org-autoload "org-icalendar"
  3483. '(org-export-icalendar-this-file
  3484. org-export-icalendar-all-agenda-files
  3485. org-export-icalendar-combine-agenda-files))
  3486. (org-autoload "org-xoxo" '(org-export-as-xoxo))
  3487. (org-autoload "org-beamer" '(org-beamer-mode org-beamer-sectioning)))
  3488. ;; Declare and autoload functions from org-agenda.el
  3489. (eval-and-compile
  3490. (org-autoload "org-agenda"
  3491. '(org-agenda org-agenda-list org-search-view
  3492. org-todo-list org-tags-view org-agenda-list-stuck-projects
  3493. org-diary org-agenda-to-appt
  3494. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  3495. ;; Autoload org-remember
  3496. (eval-and-compile
  3497. (org-autoload "org-remember"
  3498. '(org-remember-insinuate org-remember-annotation
  3499. org-remember-apply-template org-remember org-remember-handler)))
  3500. (eval-and-compile
  3501. (org-autoload "org-capture"
  3502. '(org-capture org-capture-insert-template-here
  3503. org-capture-import-remember-templates)))
  3504. ;; Autoload org-clock.el
  3505. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  3506. (beg end))
  3507. (declare-function org-clock-update-mode-line "org-clock" ())
  3508. (declare-function org-resolve-clocks "org-clock"
  3509. (&optional also-non-dangling-p prompt last-valid))
  3510. (defvar org-clock-start-time)
  3511. (defvar org-clock-marker (make-marker)
  3512. "Marker recording the last clock-in.")
  3513. (defvar org-clock-hd-marker (make-marker)
  3514. "Marker recording the last clock-in, but the headline position.")
  3515. (defvar org-clock-heading ""
  3516. "The heading of the current clock entry.")
  3517. (defun org-clock-is-active ()
  3518. "Return non-nil if clock is currently running.
  3519. The return value is actually the clock marker."
  3520. (marker-buffer org-clock-marker))
  3521. (eval-and-compile
  3522. (org-autoload
  3523. "org-clock"
  3524. '(org-clock-in org-clock-out org-clock-cancel
  3525. org-clock-goto org-clock-sum org-clock-display
  3526. org-clock-remove-overlays org-clock-report
  3527. org-clocktable-shift org-dblock-write:clocktable
  3528. org-get-clocktable org-resolve-clocks)))
  3529. (defun org-clock-update-time-maybe ()
  3530. "If this is a CLOCK line, update it and return t.
  3531. Otherwise, return nil."
  3532. (interactive)
  3533. (save-excursion
  3534. (beginning-of-line 1)
  3535. (skip-chars-forward " \t")
  3536. (when (looking-at org-clock-string)
  3537. (let ((re (concat "[ \t]*" org-clock-string
  3538. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  3539. "\\([ \t]*=>.*\\)?\\)?"))
  3540. ts te h m s neg)
  3541. (cond
  3542. ((not (looking-at re))
  3543. nil)
  3544. ((not (match-end 2))
  3545. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3546. (> org-clock-marker (point))
  3547. (<= org-clock-marker (point-at-eol)))
  3548. ;; The clock is running here
  3549. (setq org-clock-start-time
  3550. (apply 'encode-time
  3551. (org-parse-time-string (match-string 1))))
  3552. (org-clock-update-mode-line)))
  3553. (t
  3554. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  3555. (end-of-line 1)
  3556. (setq ts (match-string 1)
  3557. te (match-string 3))
  3558. (setq s (- (org-float-time
  3559. (apply 'encode-time (org-parse-time-string te)))
  3560. (org-float-time
  3561. (apply 'encode-time (org-parse-time-string ts))))
  3562. neg (< s 0)
  3563. s (abs s)
  3564. h (floor (/ s 3600))
  3565. s (- s (* 3600 h))
  3566. m (floor (/ s 60))
  3567. s (- s (* 60 s)))
  3568. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  3569. t))))))
  3570. (defun org-check-running-clock ()
  3571. "Check if the current buffer contains the running clock.
  3572. If yes, offer to stop it and to save the buffer with the changes."
  3573. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3574. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3575. (buffer-name))))
  3576. (org-clock-out)
  3577. (when (y-or-n-p "Save changed buffer?")
  3578. (save-buffer))))
  3579. (defun org-clocktable-try-shift (dir n)
  3580. "Check if this line starts a clock table, if yes, shift the time block."
  3581. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  3582. (org-clocktable-shift dir n)))
  3583. ;; Autoload org-timer.el
  3584. (eval-and-compile
  3585. (org-autoload
  3586. "org-timer"
  3587. '(org-timer-start org-timer org-timer-item
  3588. org-timer-change-times-in-region
  3589. org-timer-set-timer
  3590. org-timer-reset-timers
  3591. org-timer-show-remaining-time)))
  3592. ;; Autoload org-feed.el
  3593. (eval-and-compile
  3594. (org-autoload
  3595. "org-feed"
  3596. '(org-feed-update org-feed-update-all org-feed-goto-inbox)))
  3597. ;; Autoload org-indent.el
  3598. ;; Define the variable already here, to make sure we have it.
  3599. (defvar org-indent-mode nil
  3600. "Non-nil if Org-Indent mode is enabled.
  3601. Use the command `org-indent-mode' to change this variable.")
  3602. (eval-and-compile
  3603. (org-autoload
  3604. "org-indent"
  3605. '(org-indent-mode)))
  3606. ;; Autoload org-mobile.el
  3607. (eval-and-compile
  3608. (org-autoload
  3609. "org-mobile"
  3610. '(org-mobile-push org-mobile-pull org-mobile-create-sumo-agenda)))
  3611. ;; Autoload archiving code
  3612. ;; The stuff that is needed for cycling and tags has to be defined here.
  3613. (defgroup org-archive nil
  3614. "Options concerning archiving in Org-mode."
  3615. :tag "Org Archive"
  3616. :group 'org-structure)
  3617. (defcustom org-archive-location "%s_archive::"
  3618. "The location where subtrees should be archived.
  3619. The value of this variable is a string, consisting of two parts,
  3620. separated by a double-colon. The first part is a filename and
  3621. the second part is a headline.
  3622. When the filename is omitted, archiving happens in the same file.
  3623. %s in the filename will be replaced by the current file
  3624. name (without the directory part). Archiving to a different file
  3625. is useful to keep archived entries from contributing to the
  3626. Org-mode Agenda.
  3627. The archived entries will be filed as subtrees of the specified
  3628. headline. When the headline is omitted, the subtrees are simply
  3629. filed away at the end of the file, as top-level entries. Also in
  3630. the heading you can use %s to represent the file name, this can be
  3631. useful when using the same archive for a number of different files.
  3632. Here are a few examples:
  3633. \"%s_archive::\"
  3634. If the current file is Projects.org, archive in file
  3635. Projects.org_archive, as top-level trees. This is the default.
  3636. \"::* Archived Tasks\"
  3637. Archive in the current file, under the top-level headline
  3638. \"* Archived Tasks\".
  3639. \"~/org/archive.org::\"
  3640. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3641. \"~/org/archive.org::* From %s\"
  3642. Archive in file ~/org/archive.org (absolute path), under headlines
  3643. \"From FILENAME\" where file name is the current file name.
  3644. \"basement::** Finished Tasks\"
  3645. Archive in file ./basement (relative path), as level 3 trees
  3646. below the level 2 heading \"** Finished Tasks\".
  3647. You may set this option on a per-file basis by adding to the buffer a
  3648. line like
  3649. #+ARCHIVE: basement::** Finished Tasks
  3650. You may also define it locally for a subtree by setting an ARCHIVE property
  3651. in the entry. If such a property is found in an entry, or anywhere up
  3652. the hierarchy, it will be used."
  3653. :group 'org-archive
  3654. :type 'string)
  3655. (defcustom org-archive-tag "ARCHIVE"
  3656. "The tag that marks a subtree as archived.
  3657. An archived subtree does not open during visibility cycling, and does
  3658. not contribute to the agenda listings.
  3659. After changing this, font-lock must be restarted in the relevant buffers to
  3660. get the proper fontification."
  3661. :group 'org-archive
  3662. :group 'org-keywords
  3663. :type 'string)
  3664. (defcustom org-agenda-skip-archived-trees t
  3665. "Non-nil means the agenda will skip any items located in archived trees.
  3666. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3667. variable is no longer recommended, you should leave it at the value t.
  3668. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3669. :group 'org-archive
  3670. :group 'org-agenda-skip
  3671. :type 'boolean)
  3672. (defcustom org-columns-skip-archived-trees t
  3673. "Non-nil means ignore archived trees when creating column view."
  3674. :group 'org-archive
  3675. :group 'org-properties
  3676. :type 'boolean)
  3677. (defcustom org-cycle-open-archived-trees nil
  3678. "Non-nil means `org-cycle' will open archived trees.
  3679. An archived tree is a tree marked with the tag ARCHIVE.
  3680. When nil, archived trees will stay folded. You can still open them with
  3681. normal outline commands like `show-all', but not with the cycling commands."
  3682. :group 'org-archive
  3683. :group 'org-cycle
  3684. :type 'boolean)
  3685. (defcustom org-sparse-tree-open-archived-trees nil
  3686. "Non-nil means sparse tree construction shows matches in archived trees.
  3687. When nil, matches in these trees are highlighted, but the trees are kept in
  3688. collapsed state."
  3689. :group 'org-archive
  3690. :group 'org-sparse-trees
  3691. :type 'boolean)
  3692. (defun org-cycle-hide-archived-subtrees (state)
  3693. "Re-hide all archived subtrees after a visibility state change."
  3694. (when (and (not org-cycle-open-archived-trees)
  3695. (not (memq state '(overview folded))))
  3696. (save-excursion
  3697. (let* ((globalp (memq state '(contents all)))
  3698. (beg (if globalp (point-min) (point)))
  3699. (end (if globalp (point-max) (org-end-of-subtree t))))
  3700. (org-hide-archived-subtrees beg end)
  3701. (goto-char beg)
  3702. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3703. (message "%s" (substitute-command-keys
  3704. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3705. (defun org-force-cycle-archived ()
  3706. "Cycle subtree even if it is archived."
  3707. (interactive)
  3708. (setq this-command 'org-cycle)
  3709. (let ((org-cycle-open-archived-trees t))
  3710. (call-interactively 'org-cycle)))
  3711. (defun org-hide-archived-subtrees (beg end)
  3712. "Re-hide all archived subtrees after a visibility state change."
  3713. (save-excursion
  3714. (let* ((re (concat ":" org-archive-tag ":")))
  3715. (goto-char beg)
  3716. (while (re-search-forward re end t)
  3717. (when (org-at-heading-p)
  3718. (org-flag-subtree t)
  3719. (org-end-of-subtree t))))))
  3720. (defun org-flag-subtree (flag)
  3721. (save-excursion
  3722. (org-back-to-heading t)
  3723. (outline-end-of-heading)
  3724. (outline-flag-region (point)
  3725. (progn (org-end-of-subtree t) (point))
  3726. flag)))
  3727. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3728. (eval-and-compile
  3729. (org-autoload "org-archive"
  3730. '(org-add-archive-files org-archive-subtree
  3731. org-archive-to-archive-sibling org-toggle-archive-tag
  3732. org-archive-subtree-default
  3733. org-archive-subtree-default-with-confirmation)))
  3734. ;; Autoload Column View Code
  3735. (declare-function org-columns-number-to-string "org-colview")
  3736. (declare-function org-columns-get-format-and-top-level "org-colview")
  3737. (declare-function org-columns-compute "org-colview")
  3738. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  3739. '(org-columns-number-to-string org-columns-get-format-and-top-level
  3740. org-columns-compute org-agenda-columns org-columns-remove-overlays
  3741. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  3742. ;; Autoload ID code
  3743. (declare-function org-id-store-link "org-id")
  3744. (declare-function org-id-locations-load "org-id")
  3745. (declare-function org-id-locations-save "org-id")
  3746. (defvar org-id-track-globally)
  3747. (org-autoload "org-id"
  3748. '(org-id-get-create org-id-new org-id-copy org-id-get
  3749. org-id-get-with-outline-path-completion
  3750. org-id-get-with-outline-drilling org-id-store-link
  3751. org-id-goto org-id-find org-id-store-link))
  3752. ;; Autoload Plotting Code
  3753. (org-autoload "org-plot"
  3754. '(org-plot/gnuplot))
  3755. ;;; Variables for pre-computed regular expressions, all buffer local
  3756. (defvar org-drawer-regexp nil
  3757. "Matches first line of a hidden block.")
  3758. (make-variable-buffer-local 'org-drawer-regexp)
  3759. (defvar org-todo-regexp nil
  3760. "Matches any of the TODO state keywords.")
  3761. (make-variable-buffer-local 'org-todo-regexp)
  3762. (defvar org-not-done-regexp nil
  3763. "Matches any of the TODO state keywords except the last one.")
  3764. (make-variable-buffer-local 'org-not-done-regexp)
  3765. (defvar org-not-done-heading-regexp nil
  3766. "Matches a TODO headline that is not done.")
  3767. (make-variable-buffer-local 'org-not-done-regexp)
  3768. (defvar org-todo-line-regexp nil
  3769. "Matches a headline and puts TODO state into group 2 if present.")
  3770. (make-variable-buffer-local 'org-todo-line-regexp)
  3771. (defvar org-complex-heading-regexp nil
  3772. "Matches a headline and puts everything into groups:
  3773. group 1: the stars
  3774. group 2: The todo keyword, maybe
  3775. group 3: Priority cookie
  3776. group 4: True headline
  3777. group 5: Tags")
  3778. (make-variable-buffer-local 'org-complex-heading-regexp)
  3779. (defvar org-complex-heading-regexp-format nil
  3780. "Printf format to make regexp to match an exact headline.
  3781. This regexp will match the headline of any node which has the
  3782. exact headline text that is put into the format, but may have any
  3783. TODO state, priority and tags.")
  3784. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  3785. (defvar org-todo-line-tags-regexp nil
  3786. "Matches a headline and puts TODO state into group 2 if present.
  3787. Also put tags into group 4 if tags are present.")
  3788. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3789. (defvar org-ds-keyword-length 12
  3790. "Maximum length of the Deadline and SCHEDULED keywords.")
  3791. (make-variable-buffer-local 'org-ds-keyword-length)
  3792. (defvar org-deadline-regexp nil
  3793. "Matches the DEADLINE keyword.")
  3794. (make-variable-buffer-local 'org-deadline-regexp)
  3795. (defvar org-deadline-time-regexp nil
  3796. "Matches the DEADLINE keyword together with a time stamp.")
  3797. (make-variable-buffer-local 'org-deadline-time-regexp)
  3798. (defvar org-deadline-line-regexp nil
  3799. "Matches the DEADLINE keyword and the rest of the line.")
  3800. (make-variable-buffer-local 'org-deadline-line-regexp)
  3801. (defvar org-scheduled-regexp nil
  3802. "Matches the SCHEDULED keyword.")
  3803. (make-variable-buffer-local 'org-scheduled-regexp)
  3804. (defvar org-scheduled-time-regexp nil
  3805. "Matches the SCHEDULED keyword together with a time stamp.")
  3806. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3807. (defvar org-closed-time-regexp nil
  3808. "Matches the CLOSED keyword together with a time stamp.")
  3809. (make-variable-buffer-local 'org-closed-time-regexp)
  3810. (defvar org-keyword-time-regexp nil
  3811. "Matches any of the 4 keywords, together with the time stamp.")
  3812. (make-variable-buffer-local 'org-keyword-time-regexp)
  3813. (defvar org-keyword-time-not-clock-regexp nil
  3814. "Matches any of the 3 keywords, together with the time stamp.")
  3815. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3816. (defvar org-maybe-keyword-time-regexp nil
  3817. "Matches a timestamp, possibly preceded by a keyword.")
  3818. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3819. (defvar org-planning-or-clock-line-re nil
  3820. "Matches a line with planning or clock info.")
  3821. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  3822. (defvar org-all-time-keywords nil
  3823. "List of time keywords.")
  3824. (make-variable-buffer-local 'org-all-time-keywords)
  3825. (defconst org-plain-time-of-day-regexp
  3826. (concat
  3827. "\\(\\<[012]?[0-9]"
  3828. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3829. "\\(--?"
  3830. "\\(\\<[012]?[0-9]"
  3831. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3832. "\\)?")
  3833. "Regular expression to match a plain time or time range.
  3834. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3835. groups carry important information:
  3836. 0 the full match
  3837. 1 the first time, range or not
  3838. 8 the second time, if it is a range.")
  3839. (defconst org-plain-time-extension-regexp
  3840. (concat
  3841. "\\(\\<[012]?[0-9]"
  3842. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3843. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  3844. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  3845. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3846. groups carry important information:
  3847. 0 the full match
  3848. 7 hours of duration
  3849. 9 minutes of duration")
  3850. (defconst org-stamp-time-of-day-regexp
  3851. (concat
  3852. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  3853. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  3854. "\\(--?"
  3855. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  3856. "Regular expression to match a timestamp time or time range.
  3857. After a match, the following groups carry important information:
  3858. 0 the full match
  3859. 1 date plus weekday, for back referencing to make sure both times are on the same day
  3860. 2 the first time, range or not
  3861. 4 the second time, if it is a range.")
  3862. (defconst org-startup-options
  3863. '(("fold" org-startup-folded t)
  3864. ("overview" org-startup-folded t)
  3865. ("nofold" org-startup-folded nil)
  3866. ("showall" org-startup-folded nil)
  3867. ("showeverything" org-startup-folded showeverything)
  3868. ("content" org-startup-folded content)
  3869. ("indent" org-startup-indented t)
  3870. ("noindent" org-startup-indented nil)
  3871. ("hidestars" org-hide-leading-stars t)
  3872. ("showstars" org-hide-leading-stars nil)
  3873. ("odd" org-odd-levels-only t)
  3874. ("oddeven" org-odd-levels-only nil)
  3875. ("align" org-startup-align-all-tables t)
  3876. ("noalign" org-startup-align-all-tables nil)
  3877. ("inlineimages" org-startup-with-inline-images t)
  3878. ("noinlineimages" org-startup-with-inline-images nil)
  3879. ("customtime" org-display-custom-times t)
  3880. ("logdone" org-log-done time)
  3881. ("lognotedone" org-log-done note)
  3882. ("nologdone" org-log-done nil)
  3883. ("lognoteclock-out" org-log-note-clock-out t)
  3884. ("nolognoteclock-out" org-log-note-clock-out nil)
  3885. ("logrepeat" org-log-repeat state)
  3886. ("lognoterepeat" org-log-repeat note)
  3887. ("nologrepeat" org-log-repeat nil)
  3888. ("logreschedule" org-log-reschedule time)
  3889. ("lognotereschedule" org-log-reschedule note)
  3890. ("nologreschedule" org-log-reschedule nil)
  3891. ("logredeadline" org-log-redeadline time)
  3892. ("lognoteredeadline" org-log-redeadline note)
  3893. ("nologredeadline" org-log-redeadline nil)
  3894. ("logrefile" org-log-refile time)
  3895. ("lognoterefile" org-log-refile note)
  3896. ("nologrefile" org-log-refile nil)
  3897. ("fninline" org-footnote-define-inline t)
  3898. ("nofninline" org-footnote-define-inline nil)
  3899. ("fnlocal" org-footnote-section nil)
  3900. ("fnauto" org-footnote-auto-label t)
  3901. ("fnprompt" org-footnote-auto-label nil)
  3902. ("fnconfirm" org-footnote-auto-label confirm)
  3903. ("fnplain" org-footnote-auto-label plain)
  3904. ("fnadjust" org-footnote-auto-adjust t)
  3905. ("nofnadjust" org-footnote-auto-adjust nil)
  3906. ("constcgs" constants-unit-system cgs)
  3907. ("constSI" constants-unit-system SI)
  3908. ("noptag" org-tag-persistent-alist nil)
  3909. ("hideblocks" org-hide-block-startup t)
  3910. ("nohideblocks" org-hide-block-startup nil)
  3911. ("beamer" org-startup-with-beamer-mode t)
  3912. ("entitiespretty" org-pretty-entities t)
  3913. ("entitiesplain" org-pretty-entities nil))
  3914. "Variable associated with STARTUP options for org-mode.
  3915. Each element is a list of three items: The startup options as written
  3916. in the #+STARTUP line, the corresponding variable, and the value to
  3917. set this variable to if the option is found. An optional forth element PUSH
  3918. means to push this value onto the list in the variable.")
  3919. (defun org-update-property-plist (key val props)
  3920. "Update PROPS with KEY and VAL."
  3921. (let* ((appending (string= "+" (substring key (- (length key) 1))))
  3922. (key (if appending (substring key 0 (- (length key) 1)) key))
  3923. (remainder (org-remove-if (lambda (p) (string= (car p) key)) props))
  3924. (previous (cdr (assoc key props))))
  3925. (if appending
  3926. (cons (cons key (if previous (concat previous " " val) val)) remainder)
  3927. (cons (cons key val) remainder))))
  3928. (defconst org-block-regexp
  3929. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  3930. "Regular expression for hiding blocks.")
  3931. (defconst org-heading-keyword-regexp-format
  3932. "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  3933. "Printf format for a regexp matching an headline with some keyword.
  3934. This regexp will match the headline of any node which has the
  3935. exact keyword that is put into the format. The keyword isn't in
  3936. any group by default, but the stars and the body are.")
  3937. (defconst org-heading-keyword-maybe-regexp-format
  3938. "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
  3939. "Printf format for a regexp matching an headline, possibly with some keyword.
  3940. This regexp can match any headline with the specified keyword, or
  3941. without a keyword. The keyword isn't in any group by default,
  3942. but the stars and the body are.")
  3943. (defun org-set-regexps-and-options ()
  3944. "Precompute regular expressions for current buffer."
  3945. (when (eq major-mode 'org-mode)
  3946. (org-set-local 'org-todo-kwd-alist nil)
  3947. (org-set-local 'org-todo-key-alist nil)
  3948. (org-set-local 'org-todo-key-trigger nil)
  3949. (org-set-local 'org-todo-keywords-1 nil)
  3950. (org-set-local 'org-done-keywords nil)
  3951. (org-set-local 'org-todo-heads nil)
  3952. (org-set-local 'org-todo-sets nil)
  3953. (org-set-local 'org-todo-log-states nil)
  3954. (org-set-local 'org-file-properties nil)
  3955. (org-set-local 'org-file-tags nil)
  3956. (let ((re (org-make-options-regexp
  3957. '("CATEGORY" "TODO" "COLUMNS"
  3958. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  3959. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE" "LATEX_CLASS"
  3960. "OPTIONS")
  3961. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  3962. (splitre "[ \t]+")
  3963. (scripts org-use-sub-superscripts)
  3964. kwds kws0 kwsa key log value cat arch tags const links hw dws
  3965. tail sep kws1 prio props ftags drawers beamer-p
  3966. ext-setup-or-nil setup-contents (start 0))
  3967. (save-excursion
  3968. (save-restriction
  3969. (widen)
  3970. (goto-char (point-min))
  3971. (while (or (and ext-setup-or-nil
  3972. (string-match re ext-setup-or-nil start)
  3973. (setq start (match-end 0)))
  3974. (and (setq ext-setup-or-nil nil start 0)
  3975. (re-search-forward re nil t)))
  3976. (setq key (upcase (match-string 1 ext-setup-or-nil))
  3977. value (org-match-string-no-properties 2 ext-setup-or-nil))
  3978. (if (stringp value) (setq value (org-trim value)))
  3979. (cond
  3980. ((equal key "CATEGORY")
  3981. (setq cat value))
  3982. ((member key '("SEQ_TODO" "TODO"))
  3983. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3984. ((equal key "TYP_TODO")
  3985. (push (cons 'type (org-split-string value splitre)) kwds))
  3986. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  3987. ;; general TODO-like setup
  3988. (push (cons (intern (downcase (match-string 1 key)))
  3989. (org-split-string value splitre)) kwds))
  3990. ((equal key "TAGS")
  3991. (setq tags (append tags (if tags '("\\n") nil)
  3992. (org-split-string value splitre))))
  3993. ((equal key "COLUMNS")
  3994. (org-set-local 'org-columns-default-format value))
  3995. ((equal key "LINK")
  3996. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3997. (push (cons (match-string 1 value)
  3998. (org-trim (match-string 2 value)))
  3999. links)))
  4000. ((equal key "PRIORITIES")
  4001. (setq prio (org-split-string value " +")))
  4002. ((equal key "PROPERTY")
  4003. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4004. (setq props (org-update-property-plist (match-string 1 value)
  4005. (match-string 2 value)
  4006. props))))
  4007. ((equal key "FILETAGS")
  4008. (when (string-match "\\S-" value)
  4009. (setq ftags
  4010. (append
  4011. ftags
  4012. (apply 'append
  4013. (mapcar (lambda (x) (org-split-string x ":"))
  4014. (org-split-string value)))))))
  4015. ((equal key "DRAWERS")
  4016. (setq drawers (delete-dups (append org-drawers (org-split-string value splitre)))))
  4017. ((equal key "CONSTANTS")
  4018. (setq const (append const (org-split-string value splitre))))
  4019. ((equal key "STARTUP")
  4020. (let ((opts (org-split-string value splitre))
  4021. l var val)
  4022. (while (setq l (pop opts))
  4023. (when (setq l (assoc l org-startup-options))
  4024. (setq var (nth 1 l) val (nth 2 l))
  4025. (if (not (nth 3 l))
  4026. (set (make-local-variable var) val)
  4027. (if (not (listp (symbol-value var)))
  4028. (set (make-local-variable var) nil))
  4029. (set (make-local-variable var) (symbol-value var))
  4030. (add-to-list var val))))))
  4031. ((equal key "ARCHIVE")
  4032. (setq arch value)
  4033. (remove-text-properties 0 (length arch)
  4034. '(face t fontified t) arch))
  4035. ((equal key "LATEX_CLASS")
  4036. (setq beamer-p (equal value "beamer")))
  4037. ((equal key "OPTIONS")
  4038. (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value)
  4039. (setq scripts (read (match-string 2 value)))))
  4040. ((equal key "SETUPFILE")
  4041. (setq setup-contents (org-file-contents
  4042. (expand-file-name
  4043. (org-remove-double-quotes value))
  4044. 'noerror))
  4045. (if (not ext-setup-or-nil)
  4046. (setq ext-setup-or-nil setup-contents start 0)
  4047. (setq ext-setup-or-nil
  4048. (concat (substring ext-setup-or-nil 0 start)
  4049. "\n" setup-contents "\n"
  4050. (substring ext-setup-or-nil start)))))))
  4051. ;; search for property blocks
  4052. (goto-char (point-min))
  4053. (while (re-search-forward org-block-regexp nil t)
  4054. (when (equal "PROPERTY" (upcase (match-string 1)))
  4055. (setq value (replace-regexp-in-string
  4056. "[\n\r]" " " (match-string 4)))
  4057. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4058. (setq props (org-update-property-plist (match-string 1 value)
  4059. (match-string 2 value)
  4060. props)))))))
  4061. (org-set-local 'org-use-sub-superscripts scripts)
  4062. (when cat
  4063. (org-set-local 'org-category (intern cat))
  4064. (push (cons "CATEGORY" cat) props))
  4065. (when prio
  4066. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  4067. (setq prio (mapcar 'string-to-char prio))
  4068. (org-set-local 'org-highest-priority (nth 0 prio))
  4069. (org-set-local 'org-lowest-priority (nth 1 prio))
  4070. (org-set-local 'org-default-priority (nth 2 prio)))
  4071. (and props (org-set-local 'org-file-properties (nreverse props)))
  4072. (and ftags (org-set-local 'org-file-tags
  4073. (mapcar 'org-add-prop-inherited ftags)))
  4074. (and drawers (org-set-local 'org-drawers drawers))
  4075. (and arch (org-set-local 'org-archive-location arch))
  4076. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  4077. ;; Process the TODO keywords
  4078. (unless kwds
  4079. ;; Use the global values as if they had been given locally.
  4080. (setq kwds (default-value 'org-todo-keywords))
  4081. (if (stringp (car kwds))
  4082. (setq kwds (list (cons org-todo-interpretation
  4083. (default-value 'org-todo-keywords)))))
  4084. (setq kwds (reverse kwds)))
  4085. (setq kwds (nreverse kwds))
  4086. (let (inter kws kw)
  4087. (while (setq kws (pop kwds))
  4088. (let ((kws (or
  4089. (run-hook-with-args-until-success
  4090. 'org-todo-setup-filter-hook kws)
  4091. kws)))
  4092. (setq inter (pop kws) sep (member "|" kws)
  4093. kws0 (delete "|" (copy-sequence kws))
  4094. kwsa nil
  4095. kws1 (mapcar
  4096. (lambda (x)
  4097. ;; 1 2
  4098. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  4099. (progn
  4100. (setq kw (match-string 1 x)
  4101. key (and (match-end 2) (match-string 2 x))
  4102. log (org-extract-log-state-settings x))
  4103. (push (cons kw (and key (string-to-char key))) kwsa)
  4104. (and log (push log org-todo-log-states))
  4105. kw)
  4106. (error "Invalid TODO keyword %s" x)))
  4107. kws0)
  4108. kwsa (if kwsa (append '((:startgroup))
  4109. (nreverse kwsa)
  4110. '((:endgroup))))
  4111. hw (car kws1)
  4112. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  4113. tail (list inter hw (car dws) (org-last dws))))
  4114. (add-to-list 'org-todo-heads hw 'append)
  4115. (push kws1 org-todo-sets)
  4116. (setq org-done-keywords (append org-done-keywords dws nil))
  4117. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  4118. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  4119. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  4120. (setq org-todo-sets (nreverse org-todo-sets)
  4121. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4122. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  4123. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  4124. ;; Process the constants
  4125. (when const
  4126. (let (e cst)
  4127. (while (setq e (pop const))
  4128. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  4129. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  4130. (setq org-table-formula-constants-local cst)))
  4131. ;; Process the tags.
  4132. (when tags
  4133. (let (e tgs)
  4134. (while (setq e (pop tags))
  4135. (cond
  4136. ((equal e "{") (push '(:startgroup) tgs))
  4137. ((equal e "}") (push '(:endgroup) tgs))
  4138. ((equal e "\\n") (push '(:newline) tgs))
  4139. ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e)
  4140. (push (cons (match-string 1 e)
  4141. (string-to-char (match-string 2 e)))
  4142. tgs))
  4143. (t (push (list e) tgs))))
  4144. (org-set-local 'org-tag-alist nil)
  4145. (while (setq e (pop tgs))
  4146. (or (and (stringp (car e))
  4147. (assoc (car e) org-tag-alist))
  4148. (push e org-tag-alist)))))
  4149. ;; Compute the regular expressions and other local variables.
  4150. ;; Using `org-outline-regexp-bol' would complicate them much,
  4151. ;; because of the fixed white space at the end of that string.
  4152. (if (not org-done-keywords)
  4153. (setq org-done-keywords (and org-todo-keywords-1
  4154. (list (org-last org-todo-keywords-1)))))
  4155. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  4156. (length org-scheduled-string)
  4157. (length org-clock-string)
  4158. (length org-closed-string)))
  4159. org-drawer-regexp
  4160. (concat "^[ \t]*:\\("
  4161. (mapconcat 'regexp-quote org-drawers "\\|")
  4162. "\\):[ \t]*$")
  4163. org-not-done-keywords
  4164. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  4165. org-todo-regexp
  4166. (concat "\\("
  4167. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4168. "\\)")
  4169. org-not-done-regexp
  4170. (concat "\\("
  4171. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  4172. "\\)")
  4173. org-not-done-heading-regexp
  4174. (format org-heading-keyword-regexp-format org-not-done-regexp)
  4175. org-todo-line-regexp
  4176. (format org-heading-keyword-maybe-regexp-format org-todo-regexp)
  4177. org-complex-heading-regexp
  4178. (concat "^\\(\\*+\\)"
  4179. "\\(?: +" org-todo-regexp "\\)?"
  4180. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4181. "\\(?: +\\(.*?\\)\\)?"
  4182. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
  4183. "[ \t]*$")
  4184. org-complex-heading-regexp-format
  4185. (concat "^\\(\\*+\\)"
  4186. "\\(?: +" org-todo-regexp "\\)?"
  4187. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4188. "\\(?: +"
  4189. ;; Stats cookies can be stuck to body.
  4190. "\\(?:\\[[0-9%%/]+\\] *\\)?"
  4191. "\\(%s\\)"
  4192. "\\(?: *\\[[0-9%%/]+\\]\\)?"
  4193. "\\)"
  4194. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
  4195. "[ \t]*$")
  4196. org-todo-line-tags-regexp
  4197. (concat "^\\(\\*+\\)"
  4198. "\\(?: +" org-todo-regexp "\\)?"
  4199. "\\(?: +\\(.*?\\)\\)?"
  4200. (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
  4201. "[ \t]*$")
  4202. org-deadline-regexp (concat "\\<" org-deadline-string)
  4203. org-deadline-time-regexp
  4204. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  4205. org-deadline-line-regexp
  4206. (concat "\\<\\(" org-deadline-string "\\).*")
  4207. org-scheduled-regexp
  4208. (concat "\\<" org-scheduled-string)
  4209. org-scheduled-time-regexp
  4210. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  4211. org-closed-time-regexp
  4212. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  4213. org-keyword-time-regexp
  4214. (concat "\\<\\(" org-scheduled-string
  4215. "\\|" org-deadline-string
  4216. "\\|" org-closed-string
  4217. "\\|" org-clock-string "\\)"
  4218. " *[[<]\\([^]>]+\\)[]>]")
  4219. org-keyword-time-not-clock-regexp
  4220. (concat "\\<\\(" org-scheduled-string
  4221. "\\|" org-deadline-string
  4222. "\\|" org-closed-string
  4223. "\\)"
  4224. " *[[<]\\([^]>]+\\)[]>]")
  4225. org-maybe-keyword-time-regexp
  4226. (concat "\\(\\<\\(" org-scheduled-string
  4227. "\\|" org-deadline-string
  4228. "\\|" org-closed-string
  4229. "\\|" org-clock-string "\\)\\)?"
  4230. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4231. org-planning-or-clock-line-re
  4232. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  4233. "\\|" org-deadline-string
  4234. "\\|" org-closed-string "\\|" org-clock-string
  4235. "\\)\\>\\)")
  4236. org-all-time-keywords
  4237. (mapcar (lambda (w) (substring w 0 -1))
  4238. (list org-scheduled-string org-deadline-string
  4239. org-clock-string org-closed-string))
  4240. )
  4241. (org-compute-latex-and-specials-regexp)
  4242. (org-set-font-lock-defaults))))
  4243. (defun org-file-contents (file &optional noerror)
  4244. "Return the contents of FILE, as a string."
  4245. (if (or (not file)
  4246. (not (file-readable-p file)))
  4247. (if noerror
  4248. (progn
  4249. (message "Cannot read file \"%s\"" file)
  4250. (ding) (sit-for 2)
  4251. "")
  4252. (error "Cannot read file \"%s\"" file))
  4253. (with-temp-buffer
  4254. (insert-file-contents file)
  4255. (buffer-string))))
  4256. (defun org-extract-log-state-settings (x)
  4257. "Extract the log state setting from a TODO keyword string.
  4258. This will extract info from a string like \"WAIT(w@/!)\"."
  4259. (let (kw key log1 log2)
  4260. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4261. (setq kw (match-string 1 x)
  4262. key (and (match-end 2) (match-string 2 x))
  4263. log1 (and (match-end 3) (match-string 3 x))
  4264. log2 (and (match-end 4) (match-string 4 x)))
  4265. (and (or log1 log2)
  4266. (list kw
  4267. (and log1 (if (equal log1 "!") 'time 'note))
  4268. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4269. (defun org-remove-keyword-keys (list)
  4270. "Remove a pair of parenthesis at the end of each string in LIST."
  4271. (mapcar (lambda (x)
  4272. (if (string-match "(.*)$" x)
  4273. (substring x 0 (match-beginning 0))
  4274. x))
  4275. list))
  4276. (defun org-assign-fast-keys (alist)
  4277. "Assign fast keys to a keyword-key alist.
  4278. Respect keys that are already there."
  4279. (let (new e (alt ?0))
  4280. (while (setq e (pop alist))
  4281. (if (or (memq (car e) '(:newline :endgroup :startgroup))
  4282. (cdr e)) ;; Key already assigned.
  4283. (push e new)
  4284. (let ((clist (string-to-list (downcase (car e))))
  4285. (used (append new alist)))
  4286. (when (= (car clist) ?@)
  4287. (pop clist))
  4288. (while (and clist (rassoc (car clist) used))
  4289. (pop clist))
  4290. (unless clist
  4291. (while (rassoc alt used)
  4292. (incf alt)))
  4293. (push (cons (car e) (or (car clist) alt)) new))))
  4294. (nreverse new)))
  4295. ;;; Some variables used in various places
  4296. (defvar org-window-configuration nil
  4297. "Used in various places to store a window configuration.")
  4298. (defvar org-selected-window nil
  4299. "Used in various places to store a window configuration.")
  4300. (defvar org-finish-function nil
  4301. "Function to be called when `C-c C-c' is used.
  4302. This is for getting out of special buffers like remember.")
  4303. ;; FIXME: Occasionally check by commenting these, to make sure
  4304. ;; no other functions uses these, forgetting to let-bind them.
  4305. (defvar entry)
  4306. (defvar last-state)
  4307. (defvar date)
  4308. ;; Defined somewhere in this file, but used before definition.
  4309. (defvar org-entities) ;; defined in org-entities.el
  4310. (defvar org-struct-menu)
  4311. (defvar org-org-menu)
  4312. (defvar org-tbl-menu)
  4313. ;;;; Define the Org-mode
  4314. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4315. (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"))
  4316. ;; We use a before-change function to check if a table might need
  4317. ;; an update.
  4318. (defvar org-table-may-need-update t
  4319. "Indicates that a table might need an update.
  4320. This variable is set by `org-before-change-function'.
  4321. `org-table-align' sets it back to nil.")
  4322. (defun org-before-change-function (beg end)
  4323. "Every change indicates that a table might need an update."
  4324. (setq org-table-may-need-update t))
  4325. (defvar org-mode-map)
  4326. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4327. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4328. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4329. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4330. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4331. (defvar org-table-buffer-is-an nil)
  4332. ;; `org-outline-regexp' ought to be a defconst but is let-binding in
  4333. ;; some places -- e.g. see the macro org-with-limited-levels.
  4334. ;;
  4335. ;; In Org buffers, the value of `outline-regexp' is that of
  4336. ;; `org-outline-regexp'. The only function still directly relying on
  4337. ;; `outline-regexp' is `org-overview' so that `org-cycle' can do its
  4338. ;; job when `orgstruct-mode' is active.
  4339. (defvar org-outline-regexp "\\*+ "
  4340. "Regexp to match Org headlines.")
  4341. (defconst org-outline-regexp-bol "^\\*+ "
  4342. "Regexp to match Org headlines.
  4343. This is similar to `org-outline-regexp' but additionally makes
  4344. sure that we are at the beginning of the line.")
  4345. (defconst org-heading-regexp "^\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  4346. "Matches an headline, putting stars and text into groups.
  4347. Stars are put in group 1 and the trimmed body in group 2.")
  4348. ;;;###autoload
  4349. (define-derived-mode org-mode outline-mode "Org"
  4350. "Outline-based notes management and organizer, alias
  4351. \"Carsten's outline-mode for keeping track of everything.\"
  4352. Org-mode develops organizational tasks around a NOTES file which
  4353. contains information about projects as plain text. Org-mode is
  4354. implemented on top of outline-mode, which is ideal to keep the content
  4355. of large files well structured. It supports ToDo items, deadlines and
  4356. time stamps, which magically appear in the diary listing of the Emacs
  4357. calendar. Tables are easily created with a built-in table editor.
  4358. Plain text URL-like links connect to websites, emails (VM), Usenet
  4359. messages (Gnus), BBDB entries, and any files related to the project.
  4360. For printing and sharing of notes, an Org-mode file (or a part of it)
  4361. can be exported as a structured ASCII or HTML file.
  4362. The following commands are available:
  4363. \\{org-mode-map}"
  4364. ;; Get rid of Outline menus, they are not needed
  4365. ;; Need to do this here because define-derived-mode sets up
  4366. ;; the keymap so late. Still, it is a waste to call this each time
  4367. ;; we switch another buffer into org-mode.
  4368. (if (featurep 'xemacs)
  4369. (when (boundp 'outline-mode-menu-heading)
  4370. ;; Assume this is Greg's port, it uses easymenu
  4371. (easy-menu-remove outline-mode-menu-heading)
  4372. (easy-menu-remove outline-mode-menu-show)
  4373. (easy-menu-remove outline-mode-menu-hide))
  4374. (define-key org-mode-map [menu-bar headings] 'undefined)
  4375. (define-key org-mode-map [menu-bar hide] 'undefined)
  4376. (define-key org-mode-map [menu-bar show] 'undefined))
  4377. (org-load-modules-maybe)
  4378. (easy-menu-add org-org-menu)
  4379. (easy-menu-add org-tbl-menu)
  4380. (org-install-agenda-files-menu)
  4381. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4382. (add-to-invisibility-spec '(org-cwidth))
  4383. (add-to-invisibility-spec '(org-hide-block . t))
  4384. (when (featurep 'xemacs)
  4385. (org-set-local 'line-move-ignore-invisible t))
  4386. (org-set-local 'outline-regexp org-outline-regexp)
  4387. (org-set-local 'outline-level 'org-outline-level)
  4388. (setq bidi-paragraph-direction 'left-to-right)
  4389. (when (and org-ellipsis
  4390. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4391. (fboundp 'make-glyph-code))
  4392. (unless org-display-table
  4393. (setq org-display-table (make-display-table)))
  4394. (set-display-table-slot
  4395. org-display-table 4
  4396. (vconcat (mapcar
  4397. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4398. org-ellipsis)))
  4399. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4400. (setq buffer-display-table org-display-table))
  4401. (org-set-regexps-and-options)
  4402. (when (and org-tag-faces (not org-tags-special-faces-re))
  4403. ;; tag faces set outside customize.... force initialization.
  4404. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4405. ;; Calc embedded
  4406. (org-set-local 'calc-embedded-open-mode "# ")
  4407. (modify-syntax-entry ?@ "w")
  4408. (if org-startup-truncated (setq truncate-lines t))
  4409. (org-set-local 'font-lock-unfontify-region-function
  4410. 'org-unfontify-region)
  4411. ;; Activate before-change-function
  4412. (org-set-local 'org-table-may-need-update t)
  4413. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4414. 'local)
  4415. ;; Check for running clock before killing a buffer
  4416. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4417. ;; Paragraphs and auto-filling
  4418. (org-set-autofill-regexps)
  4419. (setq indent-line-function 'org-indent-line-function)
  4420. (org-update-radio-target-regexp)
  4421. ;; Beginning/end of defun
  4422. (org-set-local 'beginning-of-defun-function 'org-beginning-of-defun)
  4423. (org-set-local 'end-of-defun-function 'org-end-of-defun)
  4424. ;; Next error for sparse trees
  4425. (org-set-local 'next-error-function 'org-occur-next-match)
  4426. ;; Make sure dependence stuff works reliably, even for users who set it
  4427. ;; too late :-(
  4428. (if org-enforce-todo-dependencies
  4429. (add-hook 'org-blocker-hook
  4430. 'org-block-todo-from-children-or-siblings-or-parent)
  4431. (remove-hook 'org-blocker-hook
  4432. 'org-block-todo-from-children-or-siblings-or-parent))
  4433. (if org-enforce-todo-checkbox-dependencies
  4434. (add-hook 'org-blocker-hook
  4435. 'org-block-todo-from-checkboxes)
  4436. (remove-hook 'org-blocker-hook
  4437. 'org-block-todo-from-checkboxes))
  4438. ;; Comment characters
  4439. (org-set-local 'comment-start "#")
  4440. (org-set-local 'comment-padding " ")
  4441. ;; Align options lines
  4442. (org-set-local
  4443. 'align-mode-rules-list
  4444. '((org-in-buffer-settings
  4445. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4446. (modes . '(org-mode)))))
  4447. ;; Imenu
  4448. (org-set-local 'imenu-create-index-function
  4449. 'org-imenu-get-tree)
  4450. ;; Make isearch reveal context
  4451. (if (or (featurep 'xemacs)
  4452. (not (boundp 'outline-isearch-open-invisible-function)))
  4453. ;; Emacs 21 and XEmacs make use of the hook
  4454. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4455. ;; Emacs 22 deals with this through a special variable
  4456. (org-set-local 'outline-isearch-open-invisible-function
  4457. (lambda (&rest ignore) (org-show-context 'isearch))))
  4458. ;; Turn on org-beamer-mode?
  4459. (and org-startup-with-beamer-mode (org-beamer-mode 1))
  4460. ;; Setup the pcomplete hooks
  4461. (set (make-local-variable 'pcomplete-command-completion-function)
  4462. 'org-pcomplete-initial)
  4463. (set (make-local-variable 'pcomplete-command-name-function)
  4464. 'org-command-at-point)
  4465. (set (make-local-variable 'pcomplete-default-completion-function)
  4466. 'ignore)
  4467. (set (make-local-variable 'pcomplete-parse-arguments-function)
  4468. 'org-parse-arguments)
  4469. (set (make-local-variable 'pcomplete-termination-string) "")
  4470. (set (make-local-variable 'face-remapping-alist)
  4471. '((default org-default)))
  4472. ;; If empty file that did not turn on org-mode automatically, make it to.
  4473. (if (and org-insert-mode-line-in-empty-file
  4474. (org-called-interactively-p 'any)
  4475. (= (point-min) (point-max)))
  4476. (insert "# -*- mode: org -*-\n\n"))
  4477. (unless org-inhibit-startup
  4478. (when org-startup-align-all-tables
  4479. (let ((bmp (buffer-modified-p)))
  4480. (org-table-map-tables 'org-table-align 'quietly)
  4481. (set-buffer-modified-p bmp)))
  4482. (when org-startup-with-inline-images
  4483. (org-display-inline-images))
  4484. (when org-startup-indented
  4485. (require 'org-indent)
  4486. (org-indent-mode 1))
  4487. (unless org-inhibit-startup-visibility-stuff
  4488. (org-set-startup-visibility))))
  4489. (when (fboundp 'abbrev-table-put)
  4490. (abbrev-table-put org-mode-abbrev-table
  4491. :parents (list text-mode-abbrev-table)))
  4492. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4493. (defun org-current-time ()
  4494. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  4495. (if (> (car org-time-stamp-rounding-minutes) 1)
  4496. (let ((r (car org-time-stamp-rounding-minutes))
  4497. (time (decode-time)))
  4498. (apply 'encode-time
  4499. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4500. (nthcdr 2 time))))
  4501. (current-time)))
  4502. (defun org-today ()
  4503. "Return today date, considering `org-extend-today-until'."
  4504. (time-to-days
  4505. (time-subtract (current-time)
  4506. (list 0 (* 3600 org-extend-today-until) 0))))
  4507. ;;;; Font-Lock stuff, including the activators
  4508. (defvar org-mouse-map (make-sparse-keymap))
  4509. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4510. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4511. (when org-mouse-1-follows-link
  4512. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4513. (when org-tab-follows-link
  4514. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4515. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4516. (require 'font-lock)
  4517. (defconst org-non-link-chars "]\t\n\r<>")
  4518. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  4519. "shell" "elisp" "doi" "message"))
  4520. (defvar org-link-types-re nil
  4521. "Matches a link that has a url-like prefix like \"http:\"")
  4522. (defvar org-link-re-with-space nil
  4523. "Matches a link with spaces, optional angular brackets around it.")
  4524. (defvar org-link-re-with-space2 nil
  4525. "Matches a link with spaces, optional angular brackets around it.")
  4526. (defvar org-link-re-with-space3 nil
  4527. "Matches a link with spaces, only for internal part in bracket links.")
  4528. (defvar org-angle-link-re nil
  4529. "Matches link with angular brackets, spaces are allowed.")
  4530. (defvar org-plain-link-re nil
  4531. "Matches plain link, without spaces.")
  4532. (defvar org-bracket-link-regexp nil
  4533. "Matches a link in double brackets.")
  4534. (defvar org-bracket-link-analytic-regexp nil
  4535. "Regular expression used to analyze links.
  4536. Here is what the match groups contain after a match:
  4537. 1: http:
  4538. 2: http
  4539. 3: path
  4540. 4: [desc]
  4541. 5: desc")
  4542. (defvar org-bracket-link-analytic-regexp++ nil
  4543. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4544. (defvar org-any-link-re nil
  4545. "Regular expression matching any link.")
  4546. (defcustom org-match-sexp-depth 3
  4547. "Number of stacked braces for sub/superscript matching.
  4548. This has to be set before loading org.el to be effective."
  4549. :group 'org-export-translation ; ??????????????????????????/
  4550. :type 'integer)
  4551. (defun org-create-multibrace-regexp (left right n)
  4552. "Create a regular expression which will match a balanced sexp.
  4553. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4554. as single character strings.
  4555. The regexp returned will match the entire expression including the
  4556. delimiters. It will also define a single group which contains the
  4557. match except for the outermost delimiters. The maximum depth of
  4558. stacked delimiters is N. Escaping delimiters is not possible."
  4559. (let* ((nothing (concat "[^" left right "]*?"))
  4560. (or "\\|")
  4561. (re nothing)
  4562. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4563. (while (> n 1)
  4564. (setq n (1- n)
  4565. re (concat re or next)
  4566. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4567. (concat left "\\(" re "\\)" right)))
  4568. (defvar org-match-substring-regexp
  4569. (concat
  4570. "\\([^\\]\\|^\\)\\([_^]\\)\\("
  4571. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4572. "\\|"
  4573. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4574. "\\|"
  4575. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  4576. "The regular expression matching a sub- or superscript.")
  4577. (defvar org-match-substring-with-braces-regexp
  4578. (concat
  4579. "\\([^\\]\\|^\\)\\([_^]\\)\\("
  4580. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4581. "\\)")
  4582. "The regular expression matching a sub- or superscript, forcing braces.")
  4583. (defun org-make-link-regexps ()
  4584. "Update the link regular expressions.
  4585. This should be called after the variable `org-link-types' has changed."
  4586. (setq org-link-types-re
  4587. (concat
  4588. "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
  4589. org-link-re-with-space
  4590. (concat
  4591. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4592. "\\([^" org-non-link-chars " ]"
  4593. "[^" org-non-link-chars "]*"
  4594. "[^" org-non-link-chars " ]\\)>?")
  4595. org-link-re-with-space2
  4596. (concat
  4597. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4598. "\\([^" org-non-link-chars " ]"
  4599. "[^\t\n\r]*"
  4600. "[^" org-non-link-chars " ]\\)>?")
  4601. org-link-re-with-space3
  4602. (concat
  4603. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4604. "\\([^" org-non-link-chars " ]"
  4605. "[^\t\n\r]*\\)")
  4606. org-angle-link-re
  4607. (concat
  4608. "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4609. "\\([^" org-non-link-chars " ]"
  4610. "[^" org-non-link-chars "]*"
  4611. "\\)>")
  4612. org-plain-link-re
  4613. (concat
  4614. "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4615. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  4616. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4617. org-bracket-link-regexp
  4618. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4619. org-bracket-link-analytic-regexp
  4620. (concat
  4621. "\\[\\["
  4622. "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
  4623. "\\([^]]+\\)"
  4624. "\\]"
  4625. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4626. "\\]")
  4627. org-bracket-link-analytic-regexp++
  4628. (concat
  4629. "\\[\\["
  4630. "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  4631. "\\([^]]+\\)"
  4632. "\\]"
  4633. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4634. "\\]")
  4635. org-any-link-re
  4636. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4637. org-angle-link-re "\\)\\|\\("
  4638. org-plain-link-re "\\)")))
  4639. (org-make-link-regexps)
  4640. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
  4641. "Regular expression for fast time stamp matching.")
  4642. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
  4643. "Regular expression for fast time stamp matching.")
  4644. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4645. "Regular expression matching time strings for analysis.
  4646. This one does not require the space after the date, so it can be used
  4647. on a string that terminates immediately after the date.")
  4648. (defconst org-ts-regexp1 "\\(\\([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. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4651. "Regular expression matching time stamps, with groups.")
  4652. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4653. "Regular expression matching time stamps (also [..]), with groups.")
  4654. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4655. "Regular expression matching a time stamp range.")
  4656. (defconst org-tr-regexp-both
  4657. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4658. "Regular expression matching a time stamp range.")
  4659. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4660. org-ts-regexp "\\)?")
  4661. "Regular expression matching a time stamp or time stamp range.")
  4662. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  4663. org-ts-regexp-both "\\)?")
  4664. "Regular expression matching a time stamp or time stamp range.
  4665. The time stamps may be either active or inactive.")
  4666. (defvar org-emph-face nil)
  4667. (defun org-do-emphasis-faces (limit)
  4668. "Run through the buffer and add overlays to emphasized strings."
  4669. (let (rtn a)
  4670. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4671. (if (not (= (char-after (match-beginning 3))
  4672. (char-after (match-beginning 4))))
  4673. (progn
  4674. (setq rtn t)
  4675. (setq a (assoc (match-string 3) org-emphasis-alist))
  4676. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4677. 'face
  4678. (nth 1 a))
  4679. (and (nth 4 a)
  4680. (org-remove-flyspell-overlays-in
  4681. (match-beginning 0) (match-end 0)))
  4682. (add-text-properties (match-beginning 2) (match-end 2)
  4683. '(font-lock-multiline t org-emphasis t))
  4684. (when org-hide-emphasis-markers
  4685. (add-text-properties (match-end 4) (match-beginning 5)
  4686. '(invisible org-link))
  4687. (add-text-properties (match-beginning 3) (match-end 3)
  4688. '(invisible org-link)))))
  4689. (backward-char 1))
  4690. rtn))
  4691. (defun org-emphasize (&optional char)
  4692. "Insert or change an emphasis, i.e. a font like bold or italic.
  4693. If there is an active region, change that region to a new emphasis.
  4694. If there is no region, just insert the marker characters and position
  4695. the cursor between them.
  4696. CHAR should be either the marker character, or the first character of the
  4697. HTML tag associated with that emphasis. If CHAR is a space, the means
  4698. to remove the emphasis of the selected region.
  4699. If char is not given (for example in an interactive call) it
  4700. will be prompted for."
  4701. (interactive)
  4702. (let ((eal org-emphasis-alist) e det
  4703. (erc org-emphasis-regexp-components)
  4704. (prompt "")
  4705. (string "") beg end move tag c s)
  4706. (if (org-region-active-p)
  4707. (setq beg (region-beginning) end (region-end)
  4708. string (buffer-substring beg end))
  4709. (setq move t))
  4710. (while (setq e (pop eal))
  4711. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4712. c (aref tag 0))
  4713. (push (cons c (string-to-char (car e))) det)
  4714. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4715. (substring tag 1)))))
  4716. (setq det (nreverse det))
  4717. (unless char
  4718. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4719. (setq char (read-char-exclusive)))
  4720. (setq char (or (cdr (assoc char det)) char))
  4721. (if (equal char ?\ )
  4722. (setq s "" move nil)
  4723. (unless (assoc (char-to-string char) org-emphasis-alist)
  4724. (error "No such emphasis marker: \"%c\"" char))
  4725. (setq s (char-to-string char)))
  4726. (while (and (> (length string) 1)
  4727. (equal (substring string 0 1) (substring string -1))
  4728. (assoc (substring string 0 1) org-emphasis-alist))
  4729. (setq string (substring string 1 -1)))
  4730. (setq string (concat s string s))
  4731. (if beg (delete-region beg end))
  4732. (unless (or (bolp)
  4733. (string-match (concat "[" (nth 0 erc) "\n]")
  4734. (char-to-string (char-before (point)))))
  4735. (insert " "))
  4736. (unless (or (eobp)
  4737. (string-match (concat "[" (nth 1 erc) "\n]")
  4738. (char-to-string (char-after (point)))))
  4739. (insert " ") (backward-char 1))
  4740. (insert string)
  4741. (and move (backward-char 1))))
  4742. (defconst org-nonsticky-props
  4743. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  4744. (defsubst org-rear-nonsticky-at (pos)
  4745. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  4746. (defun org-activate-plain-links (limit)
  4747. "Run through the buffer and add overlays to links."
  4748. (catch 'exit
  4749. (let (f)
  4750. (if (re-search-forward org-plain-link-re limit t)
  4751. (progn
  4752. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4753. (setq f (get-text-property (match-beginning 0) 'face))
  4754. (if (or (eq f 'org-tag)
  4755. (and (listp f) (memq 'org-tag f)))
  4756. nil
  4757. (add-text-properties (match-beginning 0) (match-end 0)
  4758. (list 'mouse-face 'highlight
  4759. 'face 'org-link
  4760. 'keymap org-mouse-map))
  4761. (org-rear-nonsticky-at (match-end 0)))
  4762. t)))))
  4763. (defun org-activate-code (limit)
  4764. (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  4765. (progn
  4766. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4767. (remove-text-properties (match-beginning 0) (match-end 0)
  4768. '(display t invisible t intangible t))
  4769. t)))
  4770. (defcustom org-src-fontify-natively nil
  4771. "When non-nil, fontify code in code blocks."
  4772. :type 'boolean
  4773. :version "24.1"
  4774. :group 'org-appearance
  4775. :group 'org-babel)
  4776. (defun org-fontify-meta-lines-and-blocks (limit)
  4777. (condition-case nil
  4778. (org-fontify-meta-lines-and-blocks-1 limit)
  4779. (error (message "org-mode fontification error"))))
  4780. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  4781. "Fontify #+ lines and blocks, in the correct ways."
  4782. (let ((case-fold-search t))
  4783. (if (re-search-forward
  4784. "^\\([ \t]*#\\+\\(\\([a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  4785. limit t)
  4786. (let ((beg (match-beginning 0))
  4787. (block-start (match-end 0))
  4788. (block-end nil)
  4789. (lang (match-string 7))
  4790. (beg1 (line-beginning-position 2))
  4791. (dc1 (downcase (match-string 2)))
  4792. (dc3 (downcase (match-string 3)))
  4793. end end1 quoting block-type ovl)
  4794. (cond
  4795. ((member dc1 '("html:" "ascii:" "latex:" "docbook:"))
  4796. ;; a single line of backend-specific content
  4797. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4798. (remove-text-properties (match-beginning 0) (match-end 0)
  4799. '(display t invisible t intangible t))
  4800. (add-text-properties (match-beginning 1) (match-end 3)
  4801. '(font-lock-fontified t face org-meta-line))
  4802. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  4803. '(font-lock-fontified t face org-block))
  4804. ; for backend-specific code
  4805. t)
  4806. ((and (match-end 4) (equal dc3 "begin"))
  4807. ;; Truly a block
  4808. (setq block-type (downcase (match-string 5))
  4809. quoting (member block-type org-protecting-blocks))
  4810. (when (re-search-forward
  4811. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  4812. nil t) ;; on purpose, we look further than LIMIT
  4813. (setq end (match-end 0) end1 (1- (match-beginning 0)))
  4814. (setq block-end (match-beginning 0))
  4815. (when quoting
  4816. (remove-text-properties beg end
  4817. '(display t invisible t intangible t)))
  4818. (add-text-properties
  4819. beg end
  4820. '(font-lock-fontified t font-lock-multiline t))
  4821. (add-text-properties beg beg1 '(face org-meta-line))
  4822. (add-text-properties end1 (min (point-max) (1+ end))
  4823. '(face org-meta-line)) ; for end_src
  4824. (cond
  4825. ((and lang (not (string= lang "")) org-src-fontify-natively)
  4826. (org-src-font-lock-fontify-block lang block-start block-end)
  4827. ;; remove old background overlays
  4828. (mapc (lambda (ov)
  4829. (if (eq (overlay-get ov 'face) 'org-block-background)
  4830. (delete-overlay ov)))
  4831. (overlays-at (/ (+ beg1 block-end) 2)))
  4832. ;; add a background overlay
  4833. (setq ovl (make-overlay beg1 block-end))
  4834. (overlay-put ovl 'face 'org-block-background)
  4835. (overlay-put ovl 'evaporate t)) ;; make it go away when empty
  4836. (quoting
  4837. (add-text-properties beg1 (min (point-max) (1+ end1))
  4838. '(face org-block))) ; end of source block
  4839. ((not org-fontify-quote-and-verse-blocks))
  4840. ((string= block-type "quote")
  4841. (add-text-properties beg1 (1+ end1) '(face org-quote)))
  4842. ((string= block-type "verse")
  4843. (add-text-properties beg1 (1+ end1) '(face org-verse))))
  4844. (add-text-properties beg beg1 '(face org-block-begin-line))
  4845. (add-text-properties (1+ end) (1+ end1) '(face org-block-end-line))
  4846. t))
  4847. ((member dc1 '("title:" "author:" "email:" "date:"))
  4848. (add-text-properties
  4849. beg (match-end 3)
  4850. (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
  4851. '(font-lock-fontified t invisible t)
  4852. '(font-lock-fontified t face org-document-info-keyword)))
  4853. (add-text-properties
  4854. (match-beginning 6) (match-end 6)
  4855. (if (string-equal dc1 "title:")
  4856. '(font-lock-fontified t face org-document-title)
  4857. '(font-lock-fontified t face org-document-info))))
  4858. ((not (member (char-after beg) '(?\ ?\t)))
  4859. ;; just any other in-buffer setting, but not indented
  4860. (add-text-properties
  4861. beg (1+ (match-end 0))
  4862. '(font-lock-fontified t face org-meta-line))
  4863. t)
  4864. ((or (member dc1 '("begin:" "end:" "caption:" "label:"
  4865. "orgtbl:" "tblfm:" "tblname:" "results:"
  4866. "call:" "header:" "headers:" "name:"))
  4867. (and (match-end 4) (equal dc3 "attr")))
  4868. (add-text-properties
  4869. beg (match-end 0)
  4870. '(font-lock-fontified t face org-meta-line))
  4871. t)
  4872. ((member dc3 '(" " ""))
  4873. (add-text-properties
  4874. beg (match-end 0)
  4875. '(font-lock-fontified t face font-lock-comment-face)))
  4876. (t nil))))))
  4877. (defun org-strip-protective-commas (beg end)
  4878. "Strip protective commas between BEG and END in the current buffer."
  4879. (interactive "r")
  4880. (save-excursion
  4881. (save-match-data
  4882. (goto-char beg)
  4883. (let ((front-line (save-excursion
  4884. (re-search-forward
  4885. "[^[:space:]]" end t)
  4886. (goto-char (match-beginning 0))
  4887. (current-column))))
  4888. (while (re-search-forward "^[ \t]*\\(,\\)\\([*]\\|#\\+\\)" end t)
  4889. (goto-char (match-beginning 1))
  4890. (when (= (current-column) front-line)
  4891. (replace-match "" nil nil nil 1)))))))
  4892. (defun org-activate-angle-links (limit)
  4893. "Run through the buffer and add overlays to links."
  4894. (if (re-search-forward org-angle-link-re limit t)
  4895. (progn
  4896. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4897. (add-text-properties (match-beginning 0) (match-end 0)
  4898. (list 'mouse-face 'highlight
  4899. 'keymap org-mouse-map))
  4900. (org-rear-nonsticky-at (match-end 0))
  4901. t)))
  4902. (defun org-activate-footnote-links (limit)
  4903. "Run through the buffer and add overlays to footnotes."
  4904. (let ((fn (org-footnote-next-reference-or-definition limit)))
  4905. (when fn
  4906. (let ((beg (nth 1 fn)) (end (nth 2 fn)))
  4907. (org-remove-flyspell-overlays-in beg end)
  4908. (add-text-properties beg end
  4909. (list 'mouse-face 'highlight
  4910. 'keymap org-mouse-map
  4911. 'help-echo
  4912. (if (= (point-at-bol) beg)
  4913. "Footnote definition"
  4914. "Footnote reference")
  4915. 'font-lock-fontified t
  4916. 'font-lock-multiline t
  4917. 'face 'org-footnote))))))
  4918. (defun org-activate-bracket-links (limit)
  4919. "Run through the buffer and add overlays to bracketed links."
  4920. (if (re-search-forward org-bracket-link-regexp limit t)
  4921. (let* ((help (concat "LINK: "
  4922. (org-match-string-no-properties 1)))
  4923. ;; FIXME: above we should remove the escapes.
  4924. ;; but that requires another match, protecting match data,
  4925. ;; a lot of overhead for font-lock.
  4926. (ip (org-maybe-intangible
  4927. (list 'invisible 'org-link
  4928. 'keymap org-mouse-map 'mouse-face 'highlight
  4929. 'font-lock-multiline t 'help-echo help)))
  4930. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  4931. 'font-lock-multiline t 'help-echo help)))
  4932. ;; We need to remove the invisible property here. Table narrowing
  4933. ;; may have made some of this invisible.
  4934. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4935. (remove-text-properties (match-beginning 0) (match-end 0)
  4936. '(invisible nil))
  4937. (if (match-end 3)
  4938. (progn
  4939. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  4940. (org-rear-nonsticky-at (match-beginning 3))
  4941. (add-text-properties (match-beginning 3) (match-end 3) vp)
  4942. (org-rear-nonsticky-at (match-end 3))
  4943. (add-text-properties (match-end 3) (match-end 0) ip)
  4944. (org-rear-nonsticky-at (match-end 0)))
  4945. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  4946. (org-rear-nonsticky-at (match-beginning 1))
  4947. (add-text-properties (match-beginning 1) (match-end 1) vp)
  4948. (org-rear-nonsticky-at (match-end 1))
  4949. (add-text-properties (match-end 1) (match-end 0) ip)
  4950. (org-rear-nonsticky-at (match-end 0)))
  4951. t)))
  4952. (defun org-activate-dates (limit)
  4953. "Run through the buffer and add overlays to dates."
  4954. (if (re-search-forward org-tsr-regexp-both limit t)
  4955. (progn
  4956. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4957. (add-text-properties (match-beginning 0) (match-end 0)
  4958. (list 'mouse-face 'highlight
  4959. 'keymap org-mouse-map))
  4960. (org-rear-nonsticky-at (match-end 0))
  4961. (when org-display-custom-times
  4962. (if (match-end 3)
  4963. (org-display-custom-time (match-beginning 3) (match-end 3)))
  4964. (org-display-custom-time (match-beginning 1) (match-end 1)))
  4965. t)))
  4966. (defvar org-target-link-regexp nil
  4967. "Regular expression matching radio targets in plain text.")
  4968. (make-variable-buffer-local 'org-target-link-regexp)
  4969. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  4970. "Regular expression matching a link target.")
  4971. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  4972. "Regular expression matching a radio target.")
  4973. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  4974. "Regular expression matching any target.")
  4975. (defun org-activate-target-links (limit)
  4976. "Run through the buffer and add overlays to target matches."
  4977. (when org-target-link-regexp
  4978. (let ((case-fold-search t))
  4979. (if (re-search-forward org-target-link-regexp limit t)
  4980. (progn
  4981. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4982. (add-text-properties (match-beginning 0) (match-end 0)
  4983. (list 'mouse-face 'highlight
  4984. 'keymap org-mouse-map
  4985. 'help-echo "Radio target link"
  4986. 'org-linked-text t))
  4987. (org-rear-nonsticky-at (match-end 0))
  4988. t)))))
  4989. (defun org-update-radio-target-regexp ()
  4990. "Find all radio targets in this file and update the regular expression."
  4991. (interactive)
  4992. (when (memq 'radio org-activate-links)
  4993. (setq org-target-link-regexp
  4994. (org-make-target-link-regexp (org-all-targets 'radio)))
  4995. (org-restart-font-lock)))
  4996. (defun org-hide-wide-columns (limit)
  4997. (let (s e)
  4998. (setq s (text-property-any (point) (or limit (point-max))
  4999. 'org-cwidth t))
  5000. (when s
  5001. (setq e (next-single-property-change s 'org-cwidth))
  5002. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  5003. (goto-char e)
  5004. t)))
  5005. (defvar org-latex-and-specials-regexp nil
  5006. "Regular expression for highlighting export special stuff.")
  5007. (defvar org-match-substring-regexp)
  5008. (defvar org-match-substring-with-braces-regexp)
  5009. ;; This should be with the exporter code, but we also use if for font-locking
  5010. (defconst org-export-html-special-string-regexps
  5011. '(("\\\\-" . "&shy;")
  5012. ("---\\([^-]\\)" . "&mdash;\\1")
  5013. ("--\\([^-]\\)" . "&ndash;\\1")
  5014. ("\\.\\.\\." . "&hellip;"))
  5015. "Regular expressions for special string conversion.")
  5016. (defun org-compute-latex-and-specials-regexp ()
  5017. "Compute regular expression for stuff treated specially by exporters."
  5018. (if (not org-highlight-latex-fragments-and-specials)
  5019. (org-set-local 'org-latex-and-specials-regexp nil)
  5020. (require 'org-exp)
  5021. (let*
  5022. ((matchers (plist-get org-format-latex-options :matchers))
  5023. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  5024. org-latex-regexps)))
  5025. (org-export-allow-BIND nil)
  5026. (options (org-combine-plists (org-default-export-plist)
  5027. (org-infile-export-plist)))
  5028. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  5029. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  5030. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  5031. (org-export-html-expand (plist-get options :expand-quoted-html))
  5032. (org-export-with-special-strings (plist-get options :special-strings))
  5033. (re-sub
  5034. (cond
  5035. ((equal org-export-with-sub-superscripts '{})
  5036. (list org-match-substring-with-braces-regexp))
  5037. (org-export-with-sub-superscripts
  5038. (list org-match-substring-regexp))
  5039. (t nil)))
  5040. (re-latex
  5041. (if org-export-with-LaTeX-fragments
  5042. (mapcar (lambda (x) (nth 1 x)) latexs)))
  5043. (re-macros
  5044. (if org-export-with-TeX-macros
  5045. (list (concat "\\\\"
  5046. (regexp-opt
  5047. (append
  5048. (delq nil
  5049. (mapcar 'car-safe
  5050. (append org-entities-user
  5051. org-entities)))
  5052. (if (boundp 'org-latex-entities)
  5053. (mapcar (lambda (x)
  5054. (or (car-safe x) x))
  5055. org-latex-entities)
  5056. nil))
  5057. 'words))) ; FIXME
  5058. ))
  5059. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  5060. (re-special (if org-export-with-special-strings
  5061. (mapcar (lambda (x) (car x))
  5062. org-export-html-special-string-regexps)))
  5063. (re-rest
  5064. (delq nil
  5065. (list
  5066. (if org-export-html-expand "@<[^>\n]+>")
  5067. ))))
  5068. (org-set-local
  5069. 'org-latex-and-specials-regexp
  5070. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  5071. re-rest) "\\|")))))
  5072. (defun org-do-latex-and-special-faces (limit)
  5073. "Run through the buffer and add overlays to links."
  5074. (when org-latex-and-specials-regexp
  5075. (let (rtn d)
  5076. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  5077. limit t))
  5078. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  5079. 'face))
  5080. '(org-code org-verbatim underline)))
  5081. (progn
  5082. (setq rtn t
  5083. d (cond ((member (char-after (1+ (match-beginning 0)))
  5084. '(?_ ?^)) 1)
  5085. (t 0)))
  5086. (font-lock-prepend-text-property
  5087. (+ d (match-beginning 0)) (match-end 0)
  5088. 'face 'org-latex-and-export-specials)
  5089. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  5090. '(font-lock-multiline t)))))
  5091. rtn)))
  5092. (defun org-restart-font-lock ()
  5093. "Restart `font-lock-mode', to force refontification."
  5094. (when (and (boundp 'font-lock-mode) font-lock-mode)
  5095. (font-lock-mode -1)
  5096. (font-lock-mode 1)))
  5097. (defun org-all-targets (&optional radio)
  5098. "Return a list of all targets in this file.
  5099. With optional argument RADIO, only find radio targets."
  5100. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  5101. rtn)
  5102. (save-excursion
  5103. (goto-char (point-min))
  5104. (while (re-search-forward re nil t)
  5105. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  5106. rtn)))
  5107. (defun org-make-target-link-regexp (targets)
  5108. "Make regular expression matching all strings in TARGETS.
  5109. The regular expression finds the targets also if there is a line break
  5110. between words."
  5111. (and targets
  5112. (concat
  5113. "\\<\\("
  5114. (mapconcat
  5115. (lambda (x)
  5116. (setq x (regexp-quote x))
  5117. (while (string-match " +" x)
  5118. (setq x (replace-match "\\s-+" t t x)))
  5119. x)
  5120. targets
  5121. "\\|")
  5122. "\\)\\>")))
  5123. (defun org-activate-tags (limit)
  5124. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  5125. (progn
  5126. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5127. (add-text-properties (match-beginning 1) (match-end 1)
  5128. (list 'mouse-face 'highlight
  5129. 'keymap org-mouse-map))
  5130. (org-rear-nonsticky-at (match-end 1))
  5131. t)))
  5132. (defun org-outline-level ()
  5133. "Compute the outline level of the heading at point.
  5134. This function assumes that the cursor is at the beginning of a line matched
  5135. by `outline-regexp'. Otherwise it returns garbage.
  5136. If this is called at a normal headline, the level is the number of stars.
  5137. Use `org-reduced-level' to remove the effect of `org-odd-levels'."
  5138. (save-excursion
  5139. (looking-at org-outline-regexp)
  5140. (1- (- (match-end 0) (match-beginning 0)))))
  5141. (defvar org-font-lock-keywords nil)
  5142. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\+?\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  5143. "Regular expression matching a property line.")
  5144. (defvar org-font-lock-hook nil
  5145. "Functions to be called for special font lock stuff.")
  5146. (defvar org-font-lock-set-keywords-hook nil
  5147. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5148. This is called after `org-font-lock-extra-keywords' is defined, but before
  5149. it is installed to be used by font lock. This can be useful if something
  5150. needs to be inserted at a specific position in the font-lock sequence.")
  5151. (defun org-font-lock-hook (limit)
  5152. (run-hook-with-args 'org-font-lock-hook limit))
  5153. (defun org-set-font-lock-defaults ()
  5154. (let* ((em org-fontify-emphasized-text)
  5155. (lk org-activate-links)
  5156. (org-font-lock-extra-keywords
  5157. (list
  5158. ;; Call the hook
  5159. '(org-font-lock-hook)
  5160. ;; Headlines
  5161. `(,(if org-fontify-whole-heading-line
  5162. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5163. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5164. (1 (org-get-level-face 1))
  5165. (2 (org-get-level-face 2))
  5166. (3 (org-get-level-face 3)))
  5167. ;; Table lines
  5168. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5169. (1 'org-table t))
  5170. ;; Table internals
  5171. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5172. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5173. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5174. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5175. ;; Drawers
  5176. (list org-drawer-regexp '(0 'org-special-keyword t))
  5177. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  5178. ;; Properties
  5179. (list org-property-re
  5180. '(1 'org-special-keyword t)
  5181. '(3 'org-property-value t))
  5182. ;; Links
  5183. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5184. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5185. (if (memq 'plain lk) '(org-activate-plain-links))
  5186. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5187. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  5188. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5189. (if (memq 'footnote lk) '(org-activate-footnote-links))
  5190. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5191. '(org-hide-wide-columns (0 nil append))
  5192. ;; TODO keyword
  5193. (list (format org-heading-keyword-regexp-format
  5194. org-todo-regexp)
  5195. '(2 (org-get-todo-face 2) t))
  5196. ;; DONE
  5197. (if org-fontify-done-headline
  5198. (list (format org-heading-keyword-regexp-format
  5199. (concat
  5200. "\\(?:"
  5201. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5202. "\\)"))
  5203. '(2 'org-headline-done t))
  5204. nil)
  5205. ;; Priorities
  5206. '(org-font-lock-add-priority-faces)
  5207. ;; Tags
  5208. '(org-font-lock-add-tag-faces)
  5209. ;; Special keywords
  5210. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5211. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5212. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5213. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5214. ;; Emphasis
  5215. (if em
  5216. (if (featurep 'xemacs)
  5217. '(org-do-emphasis-faces (0 nil append))
  5218. '(org-do-emphasis-faces)))
  5219. ;; Checkboxes
  5220. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5221. 1 'org-checkbox prepend)
  5222. (if (cdr (assq 'checkbox org-list-automatic-rules))
  5223. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5224. (0 (org-get-checkbox-statistics-face) t)))
  5225. ;; Description list items
  5226. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5227. 1 'bold prepend)
  5228. ;; ARCHIVEd headings
  5229. (list (concat
  5230. org-outline-regexp-bol
  5231. "\\(.*:" org-archive-tag ":.*\\)")
  5232. '(1 'org-archived prepend))
  5233. ;; Specials
  5234. '(org-do-latex-and-special-faces)
  5235. '(org-fontify-entities)
  5236. '(org-raise-scripts)
  5237. ;; Code
  5238. '(org-activate-code (1 'org-code t))
  5239. ;; COMMENT
  5240. (list (format org-heading-keyword-regexp-format
  5241. (concat "\\("
  5242. org-comment-string "\\|" org-quote-string
  5243. "\\)"))
  5244. '(2 'org-special-keyword t))
  5245. '("^#.*" (0 'font-lock-comment-face t))
  5246. ;; Blocks and meta lines
  5247. '(org-fontify-meta-lines-and-blocks)
  5248. )))
  5249. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5250. (run-hooks 'org-font-lock-set-keywords-hook)
  5251. ;; Now set the full font-lock-keywords
  5252. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5253. (org-set-local 'font-lock-defaults
  5254. '(org-font-lock-keywords t nil nil backward-paragraph))
  5255. (kill-local-variable 'font-lock-keywords) nil))
  5256. (defun org-toggle-pretty-entities ()
  5257. "Toggle the composition display of entities as UTF8 characters."
  5258. (interactive)
  5259. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5260. (org-restart-font-lock)
  5261. (if org-pretty-entities
  5262. (message "Entities are displayed as UTF8 characters")
  5263. (save-restriction
  5264. (widen)
  5265. (org-decompose-region (point-min) (point-max))
  5266. (message "Entities are displayed plain"))))
  5267. (defun org-fontify-entities (limit)
  5268. "Find an entity to fontify."
  5269. (let (ee)
  5270. (when org-pretty-entities
  5271. (catch 'match
  5272. (while (re-search-forward
  5273. "\\\\\\(frac[13][24]\\|[a-zA-Z]+\\)\\($\\|[^[:alpha:]\n]\\)"
  5274. limit t)
  5275. (if (and (not (org-in-indented-comment-line))
  5276. (setq ee (org-entity-get (match-string 1)))
  5277. (= (length (nth 6 ee)) 1))
  5278. (progn
  5279. (add-text-properties
  5280. (match-beginning 0) (match-end 1)
  5281. (list 'font-lock-fontified t))
  5282. (compose-region (match-beginning 0) (match-end 1)
  5283. (nth 6 ee) nil)
  5284. (backward-char 1)
  5285. (throw 'match t))))
  5286. nil))))
  5287. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5288. "Fontify string S like in Org-mode."
  5289. (with-temp-buffer
  5290. (insert s)
  5291. (let ((org-odd-levels-only odd-levels))
  5292. (org-mode)
  5293. (font-lock-fontify-buffer)
  5294. (buffer-string))))
  5295. (defvar org-m nil)
  5296. (defvar org-l nil)
  5297. (defvar org-f nil)
  5298. (defun org-get-level-face (n)
  5299. "Get the right face for match N in font-lock matching of headlines."
  5300. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5301. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5302. (if org-cycle-level-faces
  5303. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5304. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5305. (cond
  5306. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5307. ((eq n 2) org-f)
  5308. (t (if org-level-color-stars-only nil org-f))))
  5309. (defun org-get-todo-face (kwd)
  5310. "Get the right face for a TODO keyword KWD.
  5311. If KWD is a number, get the corresponding match group."
  5312. (if (numberp kwd) (setq kwd (match-string kwd)))
  5313. (or (org-face-from-face-or-color
  5314. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5315. (and (member kwd org-done-keywords) 'org-done)
  5316. 'org-todo))
  5317. (defun org-face-from-face-or-color (context inherit face-or-color)
  5318. "Create a face list that inherits INHERIT, but sets the foreground color.
  5319. When FACE-OR-COLOR is not a string, just return it."
  5320. (if (stringp face-or-color)
  5321. (list :inherit inherit
  5322. (cdr (assoc context org-faces-easy-properties))
  5323. face-or-color)
  5324. face-or-color))
  5325. (defun org-font-lock-add-tag-faces (limit)
  5326. "Add the special tag faces."
  5327. (when (and org-tag-faces org-tags-special-faces-re)
  5328. (while (re-search-forward org-tags-special-faces-re limit t)
  5329. (add-text-properties (match-beginning 1) (match-end 1)
  5330. (list 'face (org-get-tag-face 1)
  5331. 'font-lock-fontified t))
  5332. (backward-char 1))))
  5333. (defun org-font-lock-add-priority-faces (limit)
  5334. "Add the special priority faces."
  5335. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  5336. (add-text-properties
  5337. (match-beginning 0) (match-end 0)
  5338. (list 'face (or (org-face-from-face-or-color
  5339. 'priority 'org-special-keyword
  5340. (cdr (assoc (char-after (match-beginning 1))
  5341. org-priority-faces)))
  5342. 'org-special-keyword)
  5343. 'font-lock-fontified t))))
  5344. (defun org-get-tag-face (kwd)
  5345. "Get the right face for a TODO keyword KWD.
  5346. If KWD is a number, get the corresponding match group."
  5347. (if (numberp kwd) (setq kwd (match-string kwd)))
  5348. (or (org-face-from-face-or-color
  5349. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  5350. 'org-tag))
  5351. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5352. "Remove fontification and activation overlays from links."
  5353. (font-lock-default-unfontify-region beg end)
  5354. (let* ((buffer-undo-list t)
  5355. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5356. (inhibit-modification-hooks t)
  5357. deactivate-mark buffer-file-name buffer-file-truename)
  5358. (org-decompose-region beg end)
  5359. (remove-text-properties beg end
  5360. '(mouse-face t keymap t org-linked-text t
  5361. invisible t intangible t
  5362. org-no-flyspell t org-emphasis t))
  5363. (org-remove-font-lock-display-properties beg end)))
  5364. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5365. ((raise 0.3) (height 0.7))
  5366. ((raise -0.5))
  5367. ((raise 0.5)))
  5368. "Display properties for showing superscripts and subscripts.")
  5369. (defun org-remove-font-lock-display-properties (beg end)
  5370. "Remove specific display properties that have been added by font lock.
  5371. The will remove the raise properties that are used to show superscripts
  5372. and subscripts."
  5373. (let (next prop)
  5374. (while (< beg end)
  5375. (setq next (next-single-property-change beg 'display nil end)
  5376. prop (get-text-property beg 'display))
  5377. (if (member prop org-script-display)
  5378. (put-text-property beg next 'display nil))
  5379. (setq beg next))))
  5380. (defun org-raise-scripts (limit)
  5381. "Add raise properties to sub/superscripts."
  5382. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5383. (if (re-search-forward
  5384. (if (eq org-use-sub-superscripts t)
  5385. org-match-substring-regexp
  5386. org-match-substring-with-braces-regexp)
  5387. limit t)
  5388. (let* ((pos (point)) table-p comment-p
  5389. (mpos (match-beginning 3))
  5390. (emph-p (get-text-property mpos 'org-emphasis))
  5391. (link-p (get-text-property mpos 'mouse-face))
  5392. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5393. (goto-char (point-at-bol))
  5394. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5395. comment-p (org-looking-at-p "[ \t]*#"))
  5396. (goto-char pos)
  5397. ;; FIXME: Should we go back one character here, for a_b^c
  5398. ;; (goto-char (1- pos)) ;????????????????????
  5399. (if (or comment-p emph-p link-p keyw-p)
  5400. t
  5401. (put-text-property (match-beginning 3) (match-end 0)
  5402. 'display
  5403. (if (equal (char-after (match-beginning 2)) ?^)
  5404. (nth (if table-p 3 1) org-script-display)
  5405. (nth (if table-p 2 0) org-script-display)))
  5406. (add-text-properties (match-beginning 2) (match-end 2)
  5407. (list 'invisible t
  5408. 'org-dwidth t 'org-dwidth-n 1))
  5409. (if (and (eq (char-after (match-beginning 3)) ?{)
  5410. (eq (char-before (match-end 3)) ?}))
  5411. (progn
  5412. (add-text-properties
  5413. (match-beginning 3) (1+ (match-beginning 3))
  5414. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5415. (add-text-properties
  5416. (1- (match-end 3)) (match-end 3)
  5417. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5418. t)))))
  5419. ;;;; Visibility cycling, including org-goto and indirect buffer
  5420. ;;; Cycling
  5421. (defvar org-cycle-global-status nil)
  5422. (make-variable-buffer-local 'org-cycle-global-status)
  5423. (defvar org-cycle-subtree-status nil)
  5424. (make-variable-buffer-local 'org-cycle-subtree-status)
  5425. ;;;###autoload
  5426. (defvar org-inlinetask-min-level)
  5427. (defun org-cycle (&optional arg)
  5428. "TAB-action and visibility cycling for Org-mode.
  5429. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5430. is outline visibility cycling, but it also invokes other actions
  5431. in special contexts.
  5432. - When this function is called with a prefix argument, rotate the entire
  5433. buffer through 3 states (global cycling)
  5434. 1. OVERVIEW: Show only top-level headlines.
  5435. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5436. 3. SHOW ALL: Show everything.
  5437. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5438. determined by the variable `org-startup-folded', and by any VISIBILITY
  5439. properties in the buffer.
  5440. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5441. including any drawers.
  5442. - When inside a table, re-align the table and move to the next field.
  5443. - When point is at the beginning of a headline, rotate the subtree started
  5444. by this line through 3 different states (local cycling)
  5445. 1. FOLDED: Only the main headline is shown.
  5446. 2. CHILDREN: The main headline and the direct children are shown.
  5447. From this state, you can move to one of the children
  5448. and zoom in further.
  5449. 3. SUBTREE: Show the entire subtree, including body text.
  5450. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5451. - When point is at the beginning of an empty headline and the variable
  5452. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5453. of the headline by demoting and promoting it to likely levels. This
  5454. speeds up creation document structure by pressing TAB once or several
  5455. times right after creating a new headline.
  5456. - When there is a numeric prefix, go up to a heading with level ARG, do
  5457. a `show-subtree' and return to the previous cursor position. If ARG
  5458. is negative, go up that many levels.
  5459. - When point is not at the beginning of a headline, execute the global
  5460. binding for TAB, which is re-indenting the line. See the option
  5461. `org-cycle-emulate-tab' for details.
  5462. - Special case: if point is at the beginning of the buffer and there is
  5463. no headline in line 1, this function will act as if called with prefix arg
  5464. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5465. But only if also the variable `org-cycle-global-at-bob' is t."
  5466. (interactive "P")
  5467. (org-load-modules-maybe)
  5468. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5469. (and org-cycle-level-after-item/entry-creation
  5470. (or (org-cycle-level)
  5471. (org-cycle-item-indentation))))
  5472. (let* ((limit-level
  5473. (or org-cycle-max-level
  5474. (and (boundp 'org-inlinetask-min-level)
  5475. org-inlinetask-min-level
  5476. (1- org-inlinetask-min-level))))
  5477. (nstars (and limit-level
  5478. (if org-odd-levels-only
  5479. (and limit-level (1- (* limit-level 2)))
  5480. limit-level)))
  5481. (org-outline-regexp
  5482. (if (not (eq major-mode 'org-mode))
  5483. outline-regexp
  5484. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5485. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5486. (not (looking-at org-outline-regexp))))
  5487. (org-cycle-hook
  5488. (if bob-special
  5489. (delq 'org-optimize-window-after-visibility-change
  5490. (copy-sequence org-cycle-hook))
  5491. org-cycle-hook))
  5492. (pos (point)))
  5493. (if (or bob-special (equal arg '(4)))
  5494. ;; special case: use global cycling
  5495. (setq arg t))
  5496. (cond
  5497. ((equal arg '(16))
  5498. (setq last-command 'dummy)
  5499. (org-set-startup-visibility)
  5500. (message "Startup visibility, plus VISIBILITY properties"))
  5501. ((equal arg '(64))
  5502. (show-all)
  5503. (message "Entire buffer visible, including drawers"))
  5504. ;; Table: enter it or move to the next field.
  5505. ((org-at-table-p 'any)
  5506. (if (org-at-table.el-p)
  5507. (message "Use C-c ' to edit table.el tables")
  5508. (if arg (org-table-edit-field t)
  5509. (org-table-justify-field-maybe)
  5510. (call-interactively 'org-table-next-field))))
  5511. ((run-hook-with-args-until-success
  5512. 'org-tab-after-check-for-table-hook))
  5513. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5514. ((eq arg t) (org-cycle-internal-global))
  5515. ;; Drawers: delegate to `org-flag-drawer'.
  5516. ((and org-drawers org-drawer-regexp
  5517. (save-excursion
  5518. (beginning-of-line 1)
  5519. (looking-at org-drawer-regexp)))
  5520. (org-flag-drawer ; toggle block visibility
  5521. (not (get-char-property (match-end 0) 'invisible))))
  5522. ;; Show-subtree, ARG levels up from here.
  5523. ((integerp arg)
  5524. (save-excursion
  5525. (org-back-to-heading)
  5526. (outline-up-heading (if (< arg 0) (- arg)
  5527. (- (funcall outline-level) arg)))
  5528. (org-show-subtree)))
  5529. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5530. ((and (featurep 'org-inlinetask)
  5531. (org-inlinetask-at-task-p)
  5532. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5533. (org-inlinetask-toggle-visibility))
  5534. ((org-try-cdlatex-tab))
  5535. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5536. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5537. (save-excursion (beginning-of-line 1)
  5538. (looking-at org-outline-regexp)))
  5539. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5540. (org-cycle-internal-local))
  5541. ;; From there: TAB emulation and template completion.
  5542. (buffer-read-only (org-back-to-heading))
  5543. ((run-hook-with-args-until-success
  5544. 'org-tab-after-check-for-cycling-hook))
  5545. ((org-try-structure-completion))
  5546. ((run-hook-with-args-until-success
  5547. 'org-tab-before-tab-emulation-hook))
  5548. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5549. (or (not (bolp))
  5550. (not (looking-at org-outline-regexp))))
  5551. (call-interactively (global-key-binding "\t")))
  5552. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5553. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5554. (or (and (eq org-cycle-emulate-tab 'white)
  5555. (= (match-end 0) (point-at-eol)))
  5556. (and (eq org-cycle-emulate-tab 'whitestart)
  5557. (>= (match-end 0) pos))))
  5558. t
  5559. (eq org-cycle-emulate-tab t))
  5560. (call-interactively (global-key-binding "\t")))
  5561. (t (save-excursion
  5562. (org-back-to-heading)
  5563. (org-cycle)))))))
  5564. (defun org-cycle-internal-global ()
  5565. "Do the global cycling action."
  5566. ;; Hack to avoid display of messages for .org attachments in Gnus
  5567. (let ((ga (string-match "\\*fontification" (buffer-name))))
  5568. (cond
  5569. ((and (eq last-command this-command)
  5570. (eq org-cycle-global-status 'overview))
  5571. ;; We just created the overview - now do table of contents
  5572. ;; This can be slow in very large buffers, so indicate action
  5573. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  5574. (unless ga (message "CONTENTS..."))
  5575. (org-content)
  5576. (unless ga (message "CONTENTS...done"))
  5577. (setq org-cycle-global-status 'contents)
  5578. (run-hook-with-args 'org-cycle-hook 'contents))
  5579. ((and (eq last-command this-command)
  5580. (eq org-cycle-global-status 'contents))
  5581. ;; We just showed the table of contents - now show everything
  5582. (run-hook-with-args 'org-pre-cycle-hook 'all)
  5583. (show-all)
  5584. (unless ga (message "SHOW ALL"))
  5585. (setq org-cycle-global-status 'all)
  5586. (run-hook-with-args 'org-cycle-hook 'all))
  5587. (t
  5588. ;; Default action: go to overview
  5589. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  5590. (org-overview)
  5591. (unless ga (message "OVERVIEW"))
  5592. (setq org-cycle-global-status 'overview)
  5593. (run-hook-with-args 'org-cycle-hook 'overview)))))
  5594. (defun org-cycle-internal-local ()
  5595. "Do the local cycling action."
  5596. (let ((goal-column 0) eoh eol eos has-children children-skipped struct)
  5597. ;; First, determine end of headline (EOH), end of subtree or item
  5598. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  5599. (save-excursion
  5600. (if (org-at-item-p)
  5601. (progn
  5602. (beginning-of-line)
  5603. (setq struct (org-list-struct))
  5604. (setq eoh (point-at-eol))
  5605. (setq eos (org-list-get-item-end-before-blank (point) struct))
  5606. (setq has-children (org-list-has-child-p (point) struct)))
  5607. (org-back-to-heading)
  5608. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  5609. (setq eos (save-excursion
  5610. (org-end-of-subtree t)
  5611. (unless (eobp)
  5612. (skip-chars-forward " \t\n"))
  5613. (if (eobp) (point) (1- (point)))))
  5614. (setq has-children
  5615. (or (save-excursion
  5616. (let ((level (funcall outline-level)))
  5617. (outline-next-heading)
  5618. (and (org-at-heading-p t)
  5619. (> (funcall outline-level) level))))
  5620. (save-excursion
  5621. (org-list-search-forward (org-item-beginning-re) eos t)))))
  5622. ;; Determine end invisible part of buffer (EOL)
  5623. (beginning-of-line 2)
  5624. ;; XEmacs doesn't have `next-single-char-property-change'
  5625. (if (featurep 'xemacs)
  5626. (while (and (not (eobp)) ;; this is like `next-line'
  5627. (get-char-property (1- (point)) 'invisible))
  5628. (beginning-of-line 2))
  5629. (while (and (not (eobp)) ;; this is like `next-line'
  5630. (get-char-property (1- (point)) 'invisible))
  5631. (goto-char (next-single-char-property-change (point) 'invisible))
  5632. (and (eolp) (beginning-of-line 2))))
  5633. (setq eol (point)))
  5634. ;; Find out what to do next and set `this-command'
  5635. (cond
  5636. ((= eos eoh)
  5637. ;; Nothing is hidden behind this heading
  5638. (run-hook-with-args 'org-pre-cycle-hook 'empty)
  5639. (message "EMPTY ENTRY")
  5640. (setq org-cycle-subtree-status nil)
  5641. (save-excursion
  5642. (goto-char eos)
  5643. (outline-next-heading)
  5644. (if (outline-invisible-p) (org-flag-heading nil))))
  5645. ((and (or (>= eol eos)
  5646. (not (string-match "\\S-" (buffer-substring eol eos))))
  5647. (or has-children
  5648. (not (setq children-skipped
  5649. org-cycle-skip-children-state-if-no-children))))
  5650. ;; Entire subtree is hidden in one line: children view
  5651. (run-hook-with-args 'org-pre-cycle-hook 'children)
  5652. (if (org-at-item-p)
  5653. (org-list-set-item-visibility (point-at-bol) struct 'children)
  5654. (org-show-entry)
  5655. (org-with-limited-levels (show-children))
  5656. (when (memq 'org-cycle-hide-drawers org-cycle-hook)
  5657. (org-cycle-hide-drawers 'subtree))
  5658. ;; Fold every list in subtree to top-level items.
  5659. (when (eq org-cycle-include-plain-lists 'integrate)
  5660. (save-excursion
  5661. (org-back-to-heading)
  5662. (while (org-list-search-forward (org-item-beginning-re) eos t)
  5663. (beginning-of-line 1)
  5664. (let* ((struct (org-list-struct))
  5665. (prevs (org-list-prevs-alist struct))
  5666. (end (org-list-get-bottom-point struct)))
  5667. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  5668. (org-list-get-all-items (point) struct prevs))
  5669. (goto-char end))))))
  5670. (message "CHILDREN")
  5671. (save-excursion
  5672. (goto-char eos)
  5673. (outline-next-heading)
  5674. (if (outline-invisible-p) (org-flag-heading nil)))
  5675. (setq org-cycle-subtree-status 'children)
  5676. (run-hook-with-args 'org-cycle-hook 'children))
  5677. ((or children-skipped
  5678. (and (eq last-command this-command)
  5679. (eq org-cycle-subtree-status 'children)))
  5680. ;; We just showed the children, or no children are there,
  5681. ;; now show everything.
  5682. (run-hook-with-args 'org-pre-cycle-hook 'subtree)
  5683. (outline-flag-region eoh eos nil)
  5684. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  5685. (setq org-cycle-subtree-status 'subtree)
  5686. (run-hook-with-args 'org-cycle-hook 'subtree))
  5687. (t
  5688. ;; Default action: hide the subtree.
  5689. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  5690. (outline-flag-region eoh eos t)
  5691. (message "FOLDED")
  5692. (setq org-cycle-subtree-status 'folded)
  5693. (run-hook-with-args 'org-cycle-hook 'folded)))))
  5694. ;;;###autoload
  5695. (defun org-global-cycle (&optional arg)
  5696. "Cycle the global visibility. For details see `org-cycle'.
  5697. With \\[universal-argument] prefix arg, switch to startup visibility.
  5698. With a numeric prefix, show all headlines up to that level."
  5699. (interactive "P")
  5700. (let ((org-cycle-include-plain-lists
  5701. (if (eq major-mode 'org-mode) org-cycle-include-plain-lists nil)))
  5702. (cond
  5703. ((integerp arg)
  5704. (show-all)
  5705. (hide-sublevels arg)
  5706. (setq org-cycle-global-status 'contents))
  5707. ((equal arg '(4))
  5708. (org-set-startup-visibility)
  5709. (message "Startup visibility, plus VISIBILITY properties."))
  5710. (t
  5711. (org-cycle '(4))))))
  5712. (defun org-set-startup-visibility ()
  5713. "Set the visibility required by startup options and properties."
  5714. (cond
  5715. ((eq org-startup-folded t)
  5716. (org-cycle '(4)))
  5717. ((eq org-startup-folded 'content)
  5718. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  5719. (org-cycle '(4)) (org-cycle '(4)))))
  5720. (unless (eq org-startup-folded 'showeverything)
  5721. (if org-hide-block-startup (org-hide-block-all))
  5722. (org-set-visibility-according-to-property 'no-cleanup)
  5723. (org-cycle-hide-archived-subtrees 'all)
  5724. (org-cycle-hide-drawers 'all)
  5725. (org-cycle-show-empty-lines t)))
  5726. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  5727. "Switch subtree visibilities according to :VISIBILITY: property."
  5728. (interactive)
  5729. (let (org-show-entry-below state)
  5730. (save-excursion
  5731. (goto-char (point-min))
  5732. (while (re-search-forward
  5733. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  5734. nil t)
  5735. (setq state (match-string 1))
  5736. (save-excursion
  5737. (org-back-to-heading t)
  5738. (hide-subtree)
  5739. (org-reveal)
  5740. (cond
  5741. ((equal state '("fold" "folded"))
  5742. (hide-subtree))
  5743. ((equal state "children")
  5744. (org-show-hidden-entry)
  5745. (show-children))
  5746. ((equal state "content")
  5747. (save-excursion
  5748. (save-restriction
  5749. (org-narrow-to-subtree)
  5750. (org-content))))
  5751. ((member state '("all" "showall"))
  5752. (show-subtree)))))
  5753. (unless no-cleanup
  5754. (org-cycle-hide-archived-subtrees 'all)
  5755. (org-cycle-hide-drawers 'all)
  5756. (org-cycle-show-empty-lines 'all)))))
  5757. ;; This function uses outline-regexp instead of the more fundamental
  5758. ;; org-outline-regexp so that org-cycle-global works outside of Org
  5759. ;; buffers, where outline-regexp is needed.
  5760. (defun org-overview ()
  5761. "Switch to overview mode, showing only top-level headlines.
  5762. Really, this shows all headlines with level equal or greater than the level
  5763. of the first headline in the buffer. This is important, because if the
  5764. first headline is not level one, then (hide-sublevels 1) gives confusing
  5765. results."
  5766. (interactive)
  5767. (let ((level (save-excursion
  5768. (goto-char (point-min))
  5769. (if (re-search-forward (concat "^" outline-regexp) nil t)
  5770. (progn
  5771. (goto-char (match-beginning 0))
  5772. (funcall outline-level))))))
  5773. (and level (hide-sublevels level))))
  5774. (defun org-content (&optional arg)
  5775. "Show all headlines in the buffer, like a table of contents.
  5776. With numerical argument N, show content up to level N."
  5777. (interactive "P")
  5778. (save-excursion
  5779. ;; Visit all headings and show their offspring
  5780. (and (integerp arg) (org-overview))
  5781. (goto-char (point-max))
  5782. (catch 'exit
  5783. (while (and (progn (condition-case nil
  5784. (outline-previous-visible-heading 1)
  5785. (error (goto-char (point-min))))
  5786. t)
  5787. (looking-at org-outline-regexp))
  5788. (if (integerp arg)
  5789. (show-children (1- arg))
  5790. (show-branches))
  5791. (if (bobp) (throw 'exit nil))))))
  5792. (defun org-optimize-window-after-visibility-change (state)
  5793. "Adjust the window after a change in outline visibility.
  5794. This function is the default value of the hook `org-cycle-hook'."
  5795. (when (get-buffer-window (current-buffer))
  5796. (cond
  5797. ((eq state 'content) nil)
  5798. ((eq state 'all) nil)
  5799. ((eq state 'folded) nil)
  5800. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  5801. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  5802. (defun org-remove-empty-overlays-at (pos)
  5803. "Remove outline overlays that do not contain non-white stuff."
  5804. (mapc
  5805. (lambda (o)
  5806. (and (eq 'outline (overlay-get o 'invisible))
  5807. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  5808. (overlay-end o))))
  5809. (delete-overlay o)))
  5810. (overlays-at pos)))
  5811. (defun org-clean-visibility-after-subtree-move ()
  5812. "Fix visibility issues after moving a subtree."
  5813. ;; First, find a reasonable region to look at:
  5814. ;; Start two siblings above, end three below
  5815. (let* ((beg (save-excursion
  5816. (and (org-get-last-sibling)
  5817. (org-get-last-sibling))
  5818. (point)))
  5819. (end (save-excursion
  5820. (and (org-get-next-sibling)
  5821. (org-get-next-sibling)
  5822. (org-get-next-sibling))
  5823. (if (org-at-heading-p)
  5824. (point-at-eol)
  5825. (point))))
  5826. (level (looking-at "\\*+"))
  5827. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  5828. (save-excursion
  5829. (save-restriction
  5830. (narrow-to-region beg end)
  5831. (when re
  5832. ;; Properly fold already folded siblings
  5833. (goto-char (point-min))
  5834. (while (re-search-forward re nil t)
  5835. (if (and (not (outline-invisible-p))
  5836. (save-excursion
  5837. (goto-char (point-at-eol)) (outline-invisible-p)))
  5838. (hide-entry))))
  5839. (org-cycle-show-empty-lines 'overview)
  5840. (org-cycle-hide-drawers 'overview)))))
  5841. (defun org-cycle-show-empty-lines (state)
  5842. "Show empty lines above all visible headlines.
  5843. The region to be covered depends on STATE when called through
  5844. `org-cycle-hook'. Lisp program can use t for STATE to get the
  5845. entire buffer covered. Note that an empty line is only shown if there
  5846. are at least `org-cycle-separator-lines' empty lines before the headline."
  5847. (when (not (= org-cycle-separator-lines 0))
  5848. (save-excursion
  5849. (let* ((n (abs org-cycle-separator-lines))
  5850. (re (cond
  5851. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  5852. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  5853. (t (let ((ns (number-to-string (- n 2))))
  5854. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  5855. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  5856. beg end b e)
  5857. (cond
  5858. ((memq state '(overview contents t))
  5859. (setq beg (point-min) end (point-max)))
  5860. ((memq state '(children folded))
  5861. (setq beg (point) end (progn (org-end-of-subtree t t)
  5862. (beginning-of-line 2)
  5863. (point)))))
  5864. (when beg
  5865. (goto-char beg)
  5866. (while (re-search-forward re end t)
  5867. (unless (get-char-property (match-end 1) 'invisible)
  5868. (setq e (match-end 1))
  5869. (if (< org-cycle-separator-lines 0)
  5870. (setq b (save-excursion
  5871. (goto-char (match-beginning 0))
  5872. (org-back-over-empty-lines)
  5873. (if (save-excursion
  5874. (goto-char (max (point-min) (1- (point))))
  5875. (org-at-heading-p))
  5876. (1- (point))
  5877. (point))))
  5878. (setq b (match-beginning 1)))
  5879. (outline-flag-region b e nil)))))))
  5880. ;; Never hide empty lines at the end of the file.
  5881. (save-excursion
  5882. (goto-char (point-max))
  5883. (outline-previous-heading)
  5884. (outline-end-of-heading)
  5885. (if (and (looking-at "[ \t\n]+")
  5886. (= (match-end 0) (point-max)))
  5887. (outline-flag-region (point) (match-end 0) nil))))
  5888. (defun org-show-empty-lines-in-parent ()
  5889. "Move to the parent and re-show empty lines before visible headlines."
  5890. (save-excursion
  5891. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  5892. (org-cycle-show-empty-lines context))))
  5893. (defun org-files-list ()
  5894. "Return `org-agenda-files' list, plus all open org-mode files.
  5895. This is useful for operations that need to scan all of a user's
  5896. open and agenda-wise Org files."
  5897. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  5898. (dolist (buf (buffer-list))
  5899. (with-current-buffer buf
  5900. (if (and (eq major-mode 'org-mode) (buffer-file-name))
  5901. (let ((file (expand-file-name (buffer-file-name))))
  5902. (unless (member file files)
  5903. (push file files))))))
  5904. files))
  5905. (defsubst org-entry-beginning-position ()
  5906. "Return the beginning position of the current entry."
  5907. (save-excursion (outline-back-to-heading t) (point)))
  5908. (defsubst org-entry-end-position ()
  5909. "Return the end position of the current entry."
  5910. (save-excursion (outline-next-heading) (point)))
  5911. (defun org-cycle-hide-drawers (state)
  5912. "Re-hide all drawers after a visibility state change."
  5913. (when (and (eq major-mode 'org-mode)
  5914. (not (memq state '(overview folded contents))))
  5915. (save-excursion
  5916. (let* ((globalp (memq state '(contents all)))
  5917. (beg (if globalp (point-min) (point)))
  5918. (end (if globalp (point-max)
  5919. (if (eq state 'children)
  5920. (save-excursion (outline-next-heading) (point))
  5921. (org-end-of-subtree t)))))
  5922. (goto-char beg)
  5923. (while (re-search-forward org-drawer-regexp end t)
  5924. (org-flag-drawer t))))))
  5925. (defun org-flag-drawer (flag)
  5926. (save-excursion
  5927. (beginning-of-line 1)
  5928. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  5929. (let ((b (match-end 0)))
  5930. (if (re-search-forward
  5931. "^[ \t]*:END:"
  5932. (save-excursion (outline-next-heading) (point)) t)
  5933. (outline-flag-region b (point-at-eol) flag)
  5934. (error ":END: line missing at position %s" b))))))
  5935. (defun org-subtree-end-visible-p ()
  5936. "Is the end of the current subtree visible?"
  5937. (pos-visible-in-window-p
  5938. (save-excursion (org-end-of-subtree t) (point))))
  5939. (defun org-first-headline-recenter (&optional N)
  5940. "Move cursor to the first headline and recenter the headline.
  5941. Optional argument N means put the headline into the Nth line of the window."
  5942. (goto-char (point-min))
  5943. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  5944. (beginning-of-line)
  5945. (recenter (prefix-numeric-value N))))
  5946. ;;; Saving and restoring visibility
  5947. (defun org-outline-overlay-data (&optional use-markers)
  5948. "Return a list of the locations of all outline overlays.
  5949. These are overlays with the `invisible' property value `outline'.
  5950. The return value is a list of cons cells, with start and stop
  5951. positions for each overlay.
  5952. If USE-MARKERS is set, return the positions as markers."
  5953. (let (beg end)
  5954. (save-excursion
  5955. (save-restriction
  5956. (widen)
  5957. (delq nil
  5958. (mapcar (lambda (o)
  5959. (when (eq (overlay-get o 'invisible) 'outline)
  5960. (setq beg (overlay-start o)
  5961. end (overlay-end o))
  5962. (and beg end (> end beg)
  5963. (if use-markers
  5964. (cons (move-marker (make-marker) beg)
  5965. (move-marker (make-marker) end))
  5966. (cons beg end)))))
  5967. (overlays-in (point-min) (point-max))))))))
  5968. (defun org-set-outline-overlay-data (data)
  5969. "Create visibility overlays for all positions in DATA.
  5970. DATA should have been made by `org-outline-overlay-data'."
  5971. (let (o)
  5972. (save-excursion
  5973. (save-restriction
  5974. (widen)
  5975. (show-all)
  5976. (mapc (lambda (c)
  5977. (outline-flag-region (car c) (cdr c) t))
  5978. data)))))
  5979. ;;; Folding of blocks
  5980. (defvar org-hide-block-overlays nil
  5981. "Overlays hiding blocks.")
  5982. (make-variable-buffer-local 'org-hide-block-overlays)
  5983. (defun org-block-map (function &optional start end)
  5984. "Call FUNCTION at the head of all source blocks in the current buffer.
  5985. Optional arguments START and END can be used to limit the range."
  5986. (let ((start (or start (point-min)))
  5987. (end (or end (point-max))))
  5988. (save-excursion
  5989. (goto-char start)
  5990. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  5991. (save-excursion
  5992. (save-match-data
  5993. (goto-char (match-beginning 0))
  5994. (funcall function)))))))
  5995. (defun org-hide-block-toggle-all ()
  5996. "Toggle the visibility of all blocks in the current buffer."
  5997. (org-block-map #'org-hide-block-toggle))
  5998. (defun org-hide-block-all ()
  5999. "Fold all blocks in the current buffer."
  6000. (interactive)
  6001. (org-show-block-all)
  6002. (org-block-map #'org-hide-block-toggle-maybe))
  6003. (defun org-show-block-all ()
  6004. "Unfold all blocks in the current buffer."
  6005. (interactive)
  6006. (mapc 'delete-overlay org-hide-block-overlays)
  6007. (setq org-hide-block-overlays nil))
  6008. (defun org-hide-block-toggle-maybe ()
  6009. "Toggle visibility of block at point."
  6010. (interactive)
  6011. (let ((case-fold-search t))
  6012. (if (save-excursion
  6013. (beginning-of-line 1)
  6014. (looking-at org-block-regexp))
  6015. (progn (org-hide-block-toggle)
  6016. t) ;; to signal that we took action
  6017. nil))) ;; to signal that we did not
  6018. (defun org-hide-block-toggle (&optional force)
  6019. "Toggle the visibility of the current block."
  6020. (interactive)
  6021. (save-excursion
  6022. (beginning-of-line)
  6023. (if (re-search-forward org-block-regexp nil t)
  6024. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  6025. (end (match-end 0)) ;; end of entire body
  6026. ov)
  6027. (if (memq t (mapcar (lambda (overlay)
  6028. (eq (overlay-get overlay 'invisible)
  6029. 'org-hide-block))
  6030. (overlays-at start)))
  6031. (if (or (not force) (eq force 'off))
  6032. (mapc (lambda (ov)
  6033. (when (member ov org-hide-block-overlays)
  6034. (setq org-hide-block-overlays
  6035. (delq ov org-hide-block-overlays)))
  6036. (when (eq (overlay-get ov 'invisible)
  6037. 'org-hide-block)
  6038. (delete-overlay ov)))
  6039. (overlays-at start)))
  6040. (setq ov (make-overlay start end))
  6041. (overlay-put ov 'invisible 'org-hide-block)
  6042. ;; make the block accessible to isearch
  6043. (overlay-put
  6044. ov 'isearch-open-invisible
  6045. (lambda (ov)
  6046. (when (member ov org-hide-block-overlays)
  6047. (setq org-hide-block-overlays
  6048. (delq ov org-hide-block-overlays)))
  6049. (when (eq (overlay-get ov 'invisible)
  6050. 'org-hide-block)
  6051. (delete-overlay ov))))
  6052. (push ov org-hide-block-overlays)))
  6053. (error "Not looking at a source block"))))
  6054. ;; org-tab-after-check-for-cycling-hook
  6055. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  6056. ;; Remove overlays when changing major mode
  6057. (add-hook 'org-mode-hook
  6058. (lambda () (org-add-hook 'change-major-mode-hook
  6059. 'org-show-block-all 'append 'local)))
  6060. ;;; Org-goto
  6061. (defvar org-goto-window-configuration nil)
  6062. (defvar org-goto-marker nil)
  6063. (defvar org-goto-map
  6064. (let ((map (make-sparse-keymap)))
  6065. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  6066. (while (setq cmd (pop cmds))
  6067. (substitute-key-definition cmd cmd map global-map)))
  6068. (suppress-keymap map)
  6069. (org-defkey map "\C-m" 'org-goto-ret)
  6070. (org-defkey map [(return)] 'org-goto-ret)
  6071. (org-defkey map [(left)] 'org-goto-left)
  6072. (org-defkey map [(right)] 'org-goto-right)
  6073. (org-defkey map [(control ?g)] 'org-goto-quit)
  6074. (org-defkey map "\C-i" 'org-cycle)
  6075. (org-defkey map [(tab)] 'org-cycle)
  6076. (org-defkey map [(down)] 'outline-next-visible-heading)
  6077. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6078. (if org-goto-auto-isearch
  6079. (if (fboundp 'define-key-after)
  6080. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6081. nil)
  6082. (org-defkey map "q" 'org-goto-quit)
  6083. (org-defkey map "n" 'outline-next-visible-heading)
  6084. (org-defkey map "p" 'outline-previous-visible-heading)
  6085. (org-defkey map "f" 'outline-forward-same-level)
  6086. (org-defkey map "b" 'outline-backward-same-level)
  6087. (org-defkey map "u" 'outline-up-heading))
  6088. (org-defkey map "/" 'org-occur)
  6089. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6090. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6091. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6092. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6093. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6094. map))
  6095. (defconst org-goto-help
  6096. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  6097. RET=jump to location [Q]uit and return to previous location
  6098. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6099. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6100. ;; FIXME: Docstring does not mention both interfaces
  6101. (defun org-goto (&optional alternative-interface)
  6102. "Look up a different location in the current file, keeping current visibility.
  6103. When you want look-up or go to a different location in a document, the
  6104. fastest way is often to fold the entire buffer and then dive into the tree.
  6105. This method has the disadvantage, that the previous location will be folded,
  6106. which may not be what you want.
  6107. This command works around this by showing a copy of the current buffer
  6108. in an indirect buffer, in overview mode. You can dive into the tree in
  6109. that copy, use org-occur and incremental search to find a location.
  6110. When pressing RET or `Q', the command returns to the original buffer in
  6111. which the visibility is still unchanged. After RET is will also jump to
  6112. the location selected in the indirect buffer and expose the headline
  6113. hierarchy above."
  6114. (interactive "P")
  6115. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6116. (org-refile-use-outline-path t)
  6117. (org-refile-target-verify-function nil)
  6118. (interface
  6119. (if (not alternative-interface)
  6120. org-goto-interface
  6121. (if (eq org-goto-interface 'outline)
  6122. 'outline-path-completion
  6123. 'outline)))
  6124. (org-goto-start-pos (point))
  6125. (selected-point
  6126. (if (eq interface 'outline)
  6127. (car (org-get-location (current-buffer) org-goto-help))
  6128. (let ((pa (org-refile-get-location "Goto" nil nil t)))
  6129. (org-refile-check-position pa)
  6130. (nth 3 pa)))))
  6131. (if selected-point
  6132. (progn
  6133. (org-mark-ring-push org-goto-start-pos)
  6134. (goto-char selected-point)
  6135. (if (or (outline-invisible-p) (org-invisible-p2))
  6136. (org-show-context 'org-goto)))
  6137. (message "Quit"))))
  6138. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6139. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6140. (defvar org-goto-local-auto-isearch-map) ; defined below
  6141. (defun org-get-location (buf help)
  6142. "Let the user select a location in the Org-mode buffer BUF.
  6143. This function uses a recursive edit. It returns the selected position
  6144. or nil."
  6145. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6146. (isearch-hide-immediately nil)
  6147. (isearch-search-fun-function
  6148. (lambda () 'org-goto-local-search-headings))
  6149. (org-goto-selected-point org-goto-exit-command)
  6150. (pop-up-frames nil)
  6151. (special-display-buffer-names nil)
  6152. (special-display-regexps nil)
  6153. (special-display-function nil))
  6154. (save-excursion
  6155. (save-window-excursion
  6156. (delete-other-windows)
  6157. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6158. (org-pop-to-buffer-same-window
  6159. (condition-case nil
  6160. (make-indirect-buffer (current-buffer) "*org-goto*")
  6161. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6162. (with-output-to-temp-buffer "*Help*"
  6163. (princ help))
  6164. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  6165. (setq buffer-read-only nil)
  6166. (let ((org-startup-truncated t)
  6167. (org-startup-folded nil)
  6168. (org-startup-align-all-tables nil))
  6169. (org-mode)
  6170. (org-overview))
  6171. (setq buffer-read-only t)
  6172. (if (and (boundp 'org-goto-start-pos)
  6173. (integer-or-marker-p org-goto-start-pos))
  6174. (let ((org-show-hierarchy-above t)
  6175. (org-show-siblings t)
  6176. (org-show-following-heading t))
  6177. (goto-char org-goto-start-pos)
  6178. (and (outline-invisible-p) (org-show-context)))
  6179. (goto-char (point-min)))
  6180. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6181. (message "Select location and press RET")
  6182. (use-local-map org-goto-map)
  6183. (recursive-edit)
  6184. ))
  6185. (kill-buffer "*org-goto*")
  6186. (cons org-goto-selected-point org-goto-exit-command)))
  6187. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6188. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6189. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6190. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  6191. (defun org-goto-local-search-headings (string bound noerror)
  6192. "Search and make sure that any matches are in headlines."
  6193. (catch 'return
  6194. (while (if isearch-forward
  6195. (search-forward string bound noerror)
  6196. (search-backward string bound noerror))
  6197. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  6198. (and (member :headline context)
  6199. (not (member :tags context))))
  6200. (throw 'return (point))))))
  6201. (defun org-goto-local-auto-isearch ()
  6202. "Start isearch."
  6203. (interactive)
  6204. (goto-char (point-min))
  6205. (let ((keys (this-command-keys)))
  6206. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6207. (isearch-mode t)
  6208. (isearch-process-search-char (string-to-char keys)))))
  6209. (defun org-goto-ret (&optional arg)
  6210. "Finish `org-goto' by going to the new location."
  6211. (interactive "P")
  6212. (setq org-goto-selected-point (point)
  6213. org-goto-exit-command 'return)
  6214. (throw 'exit nil))
  6215. (defun org-goto-left ()
  6216. "Finish `org-goto' by going to the new location."
  6217. (interactive)
  6218. (if (org-at-heading-p)
  6219. (progn
  6220. (beginning-of-line 1)
  6221. (setq org-goto-selected-point (point)
  6222. org-goto-exit-command 'left)
  6223. (throw 'exit nil))
  6224. (error "Not on a heading")))
  6225. (defun org-goto-right ()
  6226. "Finish `org-goto' by going to the new location."
  6227. (interactive)
  6228. (if (org-at-heading-p)
  6229. (progn
  6230. (setq org-goto-selected-point (point)
  6231. org-goto-exit-command 'right)
  6232. (throw 'exit nil))
  6233. (error "Not on a heading")))
  6234. (defun org-goto-quit ()
  6235. "Finish `org-goto' without cursor motion."
  6236. (interactive)
  6237. (setq org-goto-selected-point nil)
  6238. (setq org-goto-exit-command 'quit)
  6239. (throw 'exit nil))
  6240. ;;; Indirect buffer display of subtrees
  6241. (defvar org-indirect-dedicated-frame nil
  6242. "This is the frame being used for indirect tree display.")
  6243. (defvar org-last-indirect-buffer nil)
  6244. (defun org-tree-to-indirect-buffer (&optional arg)
  6245. "Create indirect buffer and narrow it to current subtree.
  6246. With numerical prefix ARG, go up to this level and then take that tree.
  6247. If ARG is negative, go up that many levels.
  6248. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6249. indirect buffer previously made with this command, to avoid proliferation of
  6250. indirect buffers. However, when you call the command with a \
  6251. \\[universal-argument] prefix, or
  6252. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6253. is kept so that you can work with several indirect buffers at the same time.
  6254. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6255. \\[universal-argument] prefix also
  6256. requests that a new frame be made for the new buffer, so that the dedicated
  6257. frame is not changed."
  6258. (interactive "P")
  6259. (let ((cbuf (current-buffer))
  6260. (cwin (selected-window))
  6261. (pos (point))
  6262. beg end level heading ibuf)
  6263. (save-excursion
  6264. (org-back-to-heading t)
  6265. (when (numberp arg)
  6266. (setq level (org-outline-level))
  6267. (if (< arg 0) (setq arg (+ level arg)))
  6268. (while (> (setq level (org-outline-level)) arg)
  6269. (outline-up-heading 1 t)))
  6270. (setq beg (point)
  6271. heading (org-get-heading))
  6272. (org-end-of-subtree t t)
  6273. (if (org-at-heading-p) (backward-char 1))
  6274. (setq end (point)))
  6275. (if (and (buffer-live-p org-last-indirect-buffer)
  6276. (not (eq org-indirect-buffer-display 'new-frame))
  6277. (not arg))
  6278. (kill-buffer org-last-indirect-buffer))
  6279. (setq ibuf (org-get-indirect-buffer cbuf)
  6280. org-last-indirect-buffer ibuf)
  6281. (cond
  6282. ((or (eq org-indirect-buffer-display 'new-frame)
  6283. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6284. (select-frame (make-frame))
  6285. (delete-other-windows)
  6286. (org-pop-to-buffer-same-window ibuf)
  6287. (org-set-frame-title heading))
  6288. ((eq org-indirect-buffer-display 'dedicated-frame)
  6289. (raise-frame
  6290. (select-frame (or (and org-indirect-dedicated-frame
  6291. (frame-live-p org-indirect-dedicated-frame)
  6292. org-indirect-dedicated-frame)
  6293. (setq org-indirect-dedicated-frame (make-frame)))))
  6294. (delete-other-windows)
  6295. (org-pop-to-buffer-same-window ibuf)
  6296. (org-set-frame-title (concat "Indirect: " heading)))
  6297. ((eq org-indirect-buffer-display 'current-window)
  6298. (org-pop-to-buffer-same-window ibuf))
  6299. ((eq org-indirect-buffer-display 'other-window)
  6300. (pop-to-buffer ibuf))
  6301. (t (error "Invalid value")))
  6302. (if (featurep 'xemacs)
  6303. (save-excursion (org-mode) (turn-on-font-lock)))
  6304. (narrow-to-region beg end)
  6305. (show-all)
  6306. (goto-char pos)
  6307. (run-hook-with-args 'org-cycle-hook 'all)
  6308. (and (window-live-p cwin) (select-window cwin))))
  6309. (defun org-get-indirect-buffer (&optional buffer)
  6310. (setq buffer (or buffer (current-buffer)))
  6311. (let ((n 1) (base (buffer-name buffer)) bname)
  6312. (while (buffer-live-p
  6313. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  6314. (setq n (1+ n)))
  6315. (condition-case nil
  6316. (make-indirect-buffer buffer bname 'clone)
  6317. (error (make-indirect-buffer buffer bname)))))
  6318. (defun org-set-frame-title (title)
  6319. "Set the title of the current frame to the string TITLE."
  6320. ;; FIXME: how to name a single frame in XEmacs???
  6321. (unless (featurep 'xemacs)
  6322. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6323. ;;;; Structure editing
  6324. ;;; Inserting headlines
  6325. (defun org-previous-line-empty-p ()
  6326. (save-excursion
  6327. (and (not (bobp))
  6328. (or (beginning-of-line 0) t)
  6329. (save-match-data
  6330. (looking-at "[ \t]*$")))))
  6331. (defun org-insert-heading (&optional force-heading invisible-ok)
  6332. "Insert a new heading or item with same depth at point.
  6333. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  6334. If point is at the beginning of a headline, insert a sibling before the
  6335. current headline. If point is not at the beginning, split the line,
  6336. create the new headline with the text in the current line after point
  6337. \(but see also the variable `org-M-RET-may-split-line').
  6338. When INVISIBLE-OK is set, stop at invisible headlines when going back.
  6339. This is important for non-interactive uses of the command."
  6340. (interactive "P")
  6341. (if (or (= (buffer-size) 0)
  6342. (and (not (save-excursion
  6343. (and (ignore-errors (org-back-to-heading invisible-ok))
  6344. (org-at-heading-p))))
  6345. (or force-heading (not (org-in-item-p)))))
  6346. (progn
  6347. (insert "\n* ")
  6348. (run-hooks 'org-insert-heading-hook))
  6349. (when (or force-heading (not (org-insert-item)))
  6350. (let* ((empty-line-p nil)
  6351. (level nil)
  6352. (on-heading (org-at-heading-p))
  6353. (head (save-excursion
  6354. (condition-case nil
  6355. (progn
  6356. (org-back-to-heading invisible-ok)
  6357. (when (and (not on-heading)
  6358. (featurep 'org-inlinetask)
  6359. (integerp org-inlinetask-min-level)
  6360. (>= (length (match-string 0))
  6361. org-inlinetask-min-level))
  6362. ;; Find a heading level before the inline task
  6363. (while (and (setq level (org-up-heading-safe))
  6364. (>= level org-inlinetask-min-level)))
  6365. (if (org-at-heading-p)
  6366. (org-back-to-heading invisible-ok)
  6367. (error "This should not happen")))
  6368. (setq empty-line-p (org-previous-line-empty-p))
  6369. (match-string 0))
  6370. (error "*"))))
  6371. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6372. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6373. pos hide-previous previous-pos)
  6374. (cond
  6375. ((and (org-at-heading-p) (bolp)
  6376. (or (bobp)
  6377. (save-excursion (backward-char 1) (not (outline-invisible-p)))))
  6378. ;; insert before the current line
  6379. (open-line (if blank 2 1)))
  6380. ((and (bolp)
  6381. (not org-insert-heading-respect-content)
  6382. (or (bobp)
  6383. (save-excursion
  6384. (backward-char 1) (not (outline-invisible-p)))))
  6385. ;; insert right here
  6386. nil)
  6387. (t
  6388. ;; somewhere in the line
  6389. (save-excursion
  6390. (setq previous-pos (point-at-bol))
  6391. (end-of-line)
  6392. (setq hide-previous (outline-invisible-p)))
  6393. (and org-insert-heading-respect-content (org-show-subtree))
  6394. (let ((split
  6395. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  6396. (save-excursion
  6397. (let ((p (point)))
  6398. (goto-char (point-at-bol))
  6399. (and (looking-at org-complex-heading-regexp)
  6400. (match-beginning 4)
  6401. (> p (match-beginning 4)))))))
  6402. tags pos)
  6403. (cond
  6404. (org-insert-heading-respect-content
  6405. (org-end-of-subtree nil t)
  6406. (when (featurep 'org-inlinetask)
  6407. (while (and (not (eobp))
  6408. (looking-at "\\(\\*+\\)[ \t]+")
  6409. (>= (length (match-string 1))
  6410. org-inlinetask-min-level))
  6411. (org-end-of-subtree nil t)))
  6412. (or (bolp) (newline))
  6413. (or (org-previous-line-empty-p)
  6414. (and blank (newline)))
  6415. (open-line 1))
  6416. ((org-at-heading-p)
  6417. (when hide-previous
  6418. (show-children)
  6419. (org-show-entry))
  6420. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
  6421. (setq tags (and (match-end 2) (match-string 2)))
  6422. (and (match-end 1)
  6423. (delete-region (match-beginning 1) (match-end 1)))
  6424. (setq pos (point-at-bol))
  6425. (or split (end-of-line 1))
  6426. (delete-horizontal-space)
  6427. (if (string-match "\\`\\*+\\'"
  6428. (buffer-substring (point-at-bol) (point)))
  6429. (insert " "))
  6430. (newline (if blank 2 1))
  6431. (when tags
  6432. (save-excursion
  6433. (goto-char pos)
  6434. (end-of-line 1)
  6435. (insert " " tags)
  6436. (org-set-tags nil 'align))))
  6437. (t
  6438. (or split (end-of-line 1))
  6439. (newline (if blank 2 1)))))))
  6440. (insert head) (just-one-space)
  6441. (setq pos (point))
  6442. (end-of-line 1)
  6443. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  6444. (when (and org-insert-heading-respect-content hide-previous)
  6445. (save-excursion
  6446. (goto-char previous-pos)
  6447. (hide-subtree)))
  6448. (run-hooks 'org-insert-heading-hook)))))
  6449. (defun org-get-heading (&optional no-tags no-todo)
  6450. "Return the heading of the current entry, without the stars.
  6451. When NO-TAGS is non-nil, don't include tags.
  6452. When NO-TODO is non-nil, don't include TODO keywords."
  6453. (save-excursion
  6454. (org-back-to-heading t)
  6455. (cond
  6456. ((and no-tags no-todo)
  6457. (looking-at org-complex-heading-regexp)
  6458. (match-string 4))
  6459. (no-tags
  6460. (looking-at (concat org-outline-regexp
  6461. "\\(.*?\\)"
  6462. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  6463. (match-string 1))
  6464. (no-todo
  6465. (looking-at org-todo-line-regexp)
  6466. (match-string 3))
  6467. (t (looking-at org-heading-regexp)
  6468. (match-string 2)))))
  6469. (defun org-heading-components ()
  6470. "Return the components of the current heading.
  6471. This is a list with the following elements:
  6472. - the level as an integer
  6473. - the reduced level, different if `org-odd-levels-only' is set.
  6474. - the TODO keyword, or nil
  6475. - the priority character, like ?A, or nil if no priority is given
  6476. - the headline text itself, or the tags string if no headline text
  6477. - the tags string, or nil."
  6478. (save-excursion
  6479. (org-back-to-heading t)
  6480. (if (let (case-fold-search) (looking-at org-complex-heading-regexp))
  6481. (list (length (match-string 1))
  6482. (org-reduced-level (length (match-string 1)))
  6483. (org-match-string-no-properties 2)
  6484. (and (match-end 3) (aref (match-string 3) 2))
  6485. (org-match-string-no-properties 4)
  6486. (org-match-string-no-properties 5)))))
  6487. (defun org-get-entry ()
  6488. "Get the entry text, after heading, entire subtree."
  6489. (save-excursion
  6490. (org-back-to-heading t)
  6491. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  6492. (defun org-insert-heading-after-current ()
  6493. "Insert a new heading with same level as current, after current subtree."
  6494. (interactive)
  6495. (org-back-to-heading)
  6496. (org-insert-heading)
  6497. (org-move-subtree-down)
  6498. (end-of-line 1))
  6499. (defun org-insert-heading-respect-content ()
  6500. (interactive)
  6501. (let ((org-insert-heading-respect-content t))
  6502. (org-insert-heading t)))
  6503. (defun org-insert-todo-heading-respect-content (&optional force-state)
  6504. (interactive "P")
  6505. (let ((org-insert-heading-respect-content t))
  6506. (org-insert-todo-heading force-state t)))
  6507. (defun org-insert-todo-heading (arg &optional force-heading)
  6508. "Insert a new heading with the same level and TODO state as current heading.
  6509. If the heading has no TODO state, or if the state is DONE, use the first
  6510. state (TODO by default). Also with prefix arg, force first state."
  6511. (interactive "P")
  6512. (when (or force-heading (not (org-insert-item 'checkbox)))
  6513. (org-insert-heading force-heading)
  6514. (save-excursion
  6515. (org-back-to-heading)
  6516. (outline-previous-heading)
  6517. (looking-at org-todo-line-regexp))
  6518. (let*
  6519. ((new-mark-x
  6520. (if (or arg
  6521. (not (match-beginning 2))
  6522. (member (match-string 2) org-done-keywords))
  6523. (car org-todo-keywords-1)
  6524. (match-string 2)))
  6525. (new-mark
  6526. (or
  6527. (run-hook-with-args-until-success
  6528. 'org-todo-get-default-hook new-mark-x nil)
  6529. new-mark-x)))
  6530. (beginning-of-line 1)
  6531. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  6532. (if org-treat-insert-todo-heading-as-state-change
  6533. (org-todo new-mark)
  6534. (insert new-mark " "))))
  6535. (when org-provide-todo-statistics
  6536. (org-update-parent-todo-statistics))))
  6537. (defun org-insert-subheading (arg)
  6538. "Insert a new subheading and demote it.
  6539. Works for outline headings and for plain lists alike."
  6540. (interactive "P")
  6541. (org-insert-heading arg)
  6542. (cond
  6543. ((org-at-heading-p) (org-do-demote))
  6544. ((org-at-item-p) (org-indent-item))))
  6545. (defun org-insert-todo-subheading (arg)
  6546. "Insert a new subheading with TODO keyword or checkbox and demote it.
  6547. Works for outline headings and for plain lists alike."
  6548. (interactive "P")
  6549. (org-insert-todo-heading arg)
  6550. (cond
  6551. ((org-at-heading-p) (org-do-demote))
  6552. ((org-at-item-p) (org-indent-item))))
  6553. ;;; Promotion and Demotion
  6554. (defvar org-after-demote-entry-hook nil
  6555. "Hook run after an entry has been demoted.
  6556. The cursor will be at the beginning of the entry.
  6557. When a subtree is being demoted, the hook will be called for each node.")
  6558. (defvar org-after-promote-entry-hook nil
  6559. "Hook run after an entry has been promoted.
  6560. The cursor will be at the beginning of the entry.
  6561. When a subtree is being promoted, the hook will be called for each node.")
  6562. (defun org-promote-subtree ()
  6563. "Promote the entire subtree.
  6564. See also `org-promote'."
  6565. (interactive)
  6566. (save-excursion
  6567. (org-with-limited-levels (org-map-tree 'org-promote)))
  6568. (org-fix-position-after-promote))
  6569. (defun org-demote-subtree ()
  6570. "Demote the entire subtree. See `org-demote'.
  6571. See also `org-promote'."
  6572. (interactive)
  6573. (save-excursion
  6574. (org-with-limited-levels (org-map-tree 'org-demote)))
  6575. (org-fix-position-after-promote))
  6576. (defun org-do-promote ()
  6577. "Promote the current heading higher up the tree.
  6578. If the region is active in `transient-mark-mode', promote all headings
  6579. in the region."
  6580. (interactive)
  6581. (save-excursion
  6582. (if (org-region-active-p)
  6583. (org-map-region 'org-promote (region-beginning) (region-end))
  6584. (org-promote)))
  6585. (org-fix-position-after-promote))
  6586. (defun org-do-demote ()
  6587. "Demote the current heading lower down the tree.
  6588. If the region is active in `transient-mark-mode', demote all headings
  6589. in the region."
  6590. (interactive)
  6591. (save-excursion
  6592. (if (org-region-active-p)
  6593. (org-map-region 'org-demote (region-beginning) (region-end))
  6594. (org-demote)))
  6595. (org-fix-position-after-promote))
  6596. (defun org-fix-position-after-promote ()
  6597. "Make sure that after pro/demotion cursor position is right."
  6598. (let ((pos (point)))
  6599. (when (save-excursion
  6600. (beginning-of-line 1)
  6601. (looking-at org-todo-line-regexp)
  6602. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  6603. (cond ((eobp) (insert " "))
  6604. ((eolp) (insert " "))
  6605. ((equal (char-after) ?\ ) (forward-char 1))))))
  6606. (defun org-current-level ()
  6607. "Return the level of the current entry, or nil if before the first headline.
  6608. The level is the number of stars at the beginning of the headline."
  6609. (save-excursion
  6610. (org-with-limited-levels
  6611. (if (ignore-errors (org-back-to-heading t))
  6612. (funcall outline-level)))))
  6613. (defun org-get-previous-line-level ()
  6614. "Return the outline depth of the last headline before the current line.
  6615. Returns 0 for the first headline in the buffer, and nil if before the
  6616. first headline."
  6617. (let ((current-level (org-current-level))
  6618. (prev-level (when (> (line-number-at-pos) 1)
  6619. (save-excursion
  6620. (beginning-of-line 0)
  6621. (org-current-level)))))
  6622. (cond ((null current-level) nil) ; Before first headline
  6623. ((null prev-level) 0) ; At first headline
  6624. (prev-level))))
  6625. (defun org-reduced-level (l)
  6626. "Compute the effective level of a heading.
  6627. This takes into account the setting of `org-odd-levels-only'."
  6628. (cond
  6629. ((zerop l) 0)
  6630. (org-odd-levels-only (1+ (floor (/ l 2))))
  6631. (t l)))
  6632. (defun org-level-increment ()
  6633. "Return the number of stars that will be added or removed at a
  6634. time to headlines when structure editing, based on the value of
  6635. `org-odd-levels-only'."
  6636. (if org-odd-levels-only 2 1))
  6637. (defun org-get-valid-level (level &optional change)
  6638. "Rectify a level change under the influence of `org-odd-levels-only'
  6639. LEVEL is a current level, CHANGE is by how much the level should be
  6640. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  6641. even level numbers will become the next higher odd number."
  6642. (if org-odd-levels-only
  6643. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  6644. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  6645. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  6646. (max 1 (+ level (or change 0)))))
  6647. (if (boundp 'define-obsolete-function-alias)
  6648. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  6649. (define-obsolete-function-alias 'org-get-legal-level
  6650. 'org-get-valid-level)
  6651. (define-obsolete-function-alias 'org-get-legal-level
  6652. 'org-get-valid-level "23.1")))
  6653. (defun org-promote ()
  6654. "Promote the current heading higher up the tree.
  6655. If the region is active in `transient-mark-mode', promote all headings
  6656. in the region."
  6657. (org-back-to-heading t)
  6658. (let* ((level (save-match-data (funcall outline-level)))
  6659. (after-change-functions (remove 'flyspell-after-change-function
  6660. after-change-functions))
  6661. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  6662. (diff (abs (- level (length up-head) -1))))
  6663. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  6664. (replace-match up-head nil t)
  6665. ;; Fixup tag positioning
  6666. (and org-auto-align-tags (org-set-tags nil t))
  6667. (if org-adapt-indentation (org-fixup-indentation (- diff)))
  6668. (run-hooks 'org-after-promote-entry-hook)))
  6669. (defun org-demote ()
  6670. "Demote the current heading lower down the tree.
  6671. If the region is active in `transient-mark-mode', demote all headings
  6672. in the region."
  6673. (org-back-to-heading t)
  6674. (let* ((level (save-match-data (funcall outline-level)))
  6675. (after-change-functions (remove 'flyspell-after-change-function
  6676. after-change-functions))
  6677. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  6678. (diff (abs (- level (length down-head) -1))))
  6679. (replace-match down-head nil t)
  6680. ;; Fixup tag positioning
  6681. (and org-auto-align-tags (org-set-tags nil t))
  6682. (if org-adapt-indentation (org-fixup-indentation diff))
  6683. (run-hooks 'org-after-demote-entry-hook)))
  6684. (defun org-cycle-level ()
  6685. "Cycle the level of an empty headline through possible states.
  6686. This goes first to child, then to parent, level, then up the hierarchy.
  6687. After top level, it switches back to sibling level."
  6688. (interactive)
  6689. (let ((org-adapt-indentation nil))
  6690. (when (org-point-at-end-of-empty-headline)
  6691. (setq this-command 'org-cycle-level) ; Only needed for caching
  6692. (let ((cur-level (org-current-level))
  6693. (prev-level (org-get-previous-line-level)))
  6694. (cond
  6695. ;; If first headline in file, promote to top-level.
  6696. ((= prev-level 0)
  6697. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6698. do (org-do-promote)))
  6699. ;; If same level as prev, demote one.
  6700. ((= prev-level cur-level)
  6701. (org-do-demote))
  6702. ;; If parent is top-level, promote to top level if not already.
  6703. ((= prev-level 1)
  6704. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6705. do (org-do-promote)))
  6706. ;; If top-level, return to prev-level.
  6707. ((= cur-level 1)
  6708. (loop repeat (/ (- prev-level 1) (org-level-increment))
  6709. do (org-do-demote)))
  6710. ;; If less than prev-level, promote one.
  6711. ((< cur-level prev-level)
  6712. (org-do-promote))
  6713. ;; If deeper than prev-level, promote until higher than
  6714. ;; prev-level.
  6715. ((> cur-level prev-level)
  6716. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  6717. do (org-do-promote))))
  6718. t))))
  6719. (defun org-map-tree (fun)
  6720. "Call FUN for every heading underneath the current one."
  6721. (org-back-to-heading)
  6722. (let ((level (funcall outline-level)))
  6723. (save-excursion
  6724. (funcall fun)
  6725. (while (and (progn
  6726. (outline-next-heading)
  6727. (> (funcall outline-level) level))
  6728. (not (eobp)))
  6729. (funcall fun)))))
  6730. (defun org-map-region (fun beg end)
  6731. "Call FUN for every heading between BEG and END."
  6732. (let ((org-ignore-region t))
  6733. (save-excursion
  6734. (setq end (copy-marker end))
  6735. (goto-char beg)
  6736. (if (and (re-search-forward org-outline-regexp-bol nil t)
  6737. (< (point) end))
  6738. (funcall fun))
  6739. (while (and (progn
  6740. (outline-next-heading)
  6741. (< (point) end))
  6742. (not (eobp)))
  6743. (funcall fun)))))
  6744. (defvar org-property-end-re) ; silence byte-compiler
  6745. (defun org-fixup-indentation (diff)
  6746. "Change the indentation in the current entry by DIFF.
  6747. However, if any line in the current entry has no indentation, or if it
  6748. would end up with no indentation after the change, nothing at all is done."
  6749. (save-excursion
  6750. (let ((end (save-excursion (outline-next-heading)
  6751. (point-marker)))
  6752. (prohibit (if (> diff 0)
  6753. "^\\S-"
  6754. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  6755. col)
  6756. (unless (save-excursion (end-of-line 1)
  6757. (re-search-forward prohibit end t))
  6758. (while (and (< (point) end)
  6759. (re-search-forward "^[ \t]+" end t))
  6760. (goto-char (match-end 0))
  6761. (setq col (current-column))
  6762. (if (< diff 0) (replace-match ""))
  6763. (org-indent-to-column (+ diff col))))
  6764. (move-marker end nil))))
  6765. (defun org-convert-to-odd-levels ()
  6766. "Convert an org-mode file with all levels allowed to one with odd levels.
  6767. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  6768. level 5 etc."
  6769. (interactive)
  6770. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  6771. (let ((outline-level 'org-outline-level)
  6772. (org-odd-levels-only nil) n)
  6773. (save-excursion
  6774. (goto-char (point-min))
  6775. (while (re-search-forward "^\\*\\*+ " nil t)
  6776. (setq n (- (length (match-string 0)) 2))
  6777. (while (>= (setq n (1- n)) 0)
  6778. (org-demote))
  6779. (end-of-line 1))))))
  6780. (defun org-convert-to-oddeven-levels ()
  6781. "Convert an org-mode file with only odd levels to one with odd/even levels.
  6782. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  6783. file contains a section with an even level, conversion would
  6784. destroy the structure of the file. An error is signaled in this
  6785. case."
  6786. (interactive)
  6787. (goto-char (point-min))
  6788. ;; First check if there are no even levels
  6789. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  6790. (org-show-context t)
  6791. (error "Not all levels are odd in this file. Conversion not possible"))
  6792. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  6793. (let ((outline-regexp org-outline-regexp)
  6794. (outline-level 'org-outline-level)
  6795. (org-odd-levels-only nil) n)
  6796. (save-excursion
  6797. (goto-char (point-min))
  6798. (while (re-search-forward "^\\*\\*+ " nil t)
  6799. (setq n (/ (1- (length (match-string 0))) 2))
  6800. (while (>= (setq n (1- n)) 0)
  6801. (org-promote))
  6802. (end-of-line 1))))))
  6803. (defun org-tr-level (n)
  6804. "Make N odd if required."
  6805. (if org-odd-levels-only (1+ (/ n 2)) n))
  6806. ;;; Vertical tree motion, cutting and pasting of subtrees
  6807. (defun org-move-subtree-up (&optional arg)
  6808. "Move the current subtree up past ARG headlines of the same level."
  6809. (interactive "p")
  6810. (org-move-subtree-down (- (prefix-numeric-value arg))))
  6811. (defun org-move-subtree-down (&optional arg)
  6812. "Move the current subtree down past ARG headlines of the same level."
  6813. (interactive "p")
  6814. (setq arg (prefix-numeric-value arg))
  6815. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  6816. 'org-get-last-sibling))
  6817. (ins-point (make-marker))
  6818. (cnt (abs arg))
  6819. (col (current-column))
  6820. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  6821. ;; Select the tree
  6822. (org-back-to-heading)
  6823. (setq beg0 (point))
  6824. (save-excursion
  6825. (setq ne-beg (org-back-over-empty-lines))
  6826. (setq beg (point)))
  6827. (save-match-data
  6828. (save-excursion (outline-end-of-heading)
  6829. (setq folded (outline-invisible-p)))
  6830. (outline-end-of-subtree))
  6831. (outline-next-heading)
  6832. (setq ne-end (org-back-over-empty-lines))
  6833. (setq end (point))
  6834. (goto-char beg0)
  6835. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  6836. ;; include less whitespace
  6837. (save-excursion
  6838. (goto-char beg)
  6839. (forward-line (- ne-beg ne-end))
  6840. (setq beg (point))))
  6841. ;; Find insertion point, with error handling
  6842. (while (> cnt 0)
  6843. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  6844. (progn (goto-char beg0)
  6845. (error "Cannot move past superior level or buffer limit")))
  6846. (setq cnt (1- cnt)))
  6847. (if (> arg 0)
  6848. ;; Moving forward - still need to move over subtree
  6849. (progn (org-end-of-subtree t t)
  6850. (save-excursion
  6851. (org-back-over-empty-lines)
  6852. (or (bolp) (newline)))))
  6853. (setq ne-ins (org-back-over-empty-lines))
  6854. (move-marker ins-point (point))
  6855. (setq txt (buffer-substring beg end))
  6856. (org-save-markers-in-region beg end)
  6857. (delete-region beg end)
  6858. (org-remove-empty-overlays-at beg)
  6859. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  6860. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  6861. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  6862. (let ((bbb (point)))
  6863. (insert-before-markers txt)
  6864. (org-reinstall-markers-in-region bbb)
  6865. (move-marker ins-point bbb))
  6866. (or (bolp) (insert "\n"))
  6867. (setq ins-end (point))
  6868. (goto-char ins-point)
  6869. (org-skip-whitespace)
  6870. (when (and (< arg 0)
  6871. (org-first-sibling-p)
  6872. (> ne-ins ne-beg))
  6873. ;; Move whitespace back to beginning
  6874. (save-excursion
  6875. (goto-char ins-end)
  6876. (let ((kill-whole-line t))
  6877. (kill-line (- ne-ins ne-beg)) (point)))
  6878. (insert (make-string (- ne-ins ne-beg) ?\n)))
  6879. (move-marker ins-point nil)
  6880. (if folded
  6881. (hide-subtree)
  6882. (org-show-entry)
  6883. (show-children)
  6884. (org-cycle-hide-drawers 'children))
  6885. (org-clean-visibility-after-subtree-move)
  6886. ;; move back to the initial column we were at
  6887. (move-to-column col)))
  6888. (defvar org-subtree-clip ""
  6889. "Clipboard for cut and paste of subtrees.
  6890. This is actually only a copy of the kill, because we use the normal kill
  6891. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  6892. (defvar org-subtree-clip-folded nil
  6893. "Was the last copied subtree folded?
  6894. This is used to fold the tree back after pasting.")
  6895. (defun org-cut-subtree (&optional n)
  6896. "Cut the current subtree into the clipboard.
  6897. With prefix arg N, cut this many sequential subtrees.
  6898. This is a short-hand for marking the subtree and then cutting it."
  6899. (interactive "p")
  6900. (org-copy-subtree n 'cut))
  6901. (defun org-copy-subtree (&optional n cut force-store-markers)
  6902. "Cut the current subtree into the clipboard.
  6903. With prefix arg N, cut this many sequential subtrees.
  6904. This is a short-hand for marking the subtree and then copying it.
  6905. If CUT is non-nil, actually cut the subtree.
  6906. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  6907. of some markers in the region, even if CUT is non-nil. This is
  6908. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  6909. (interactive "p")
  6910. (let (beg end folded (beg0 (point)))
  6911. (if (org-called-interactively-p 'any)
  6912. (org-back-to-heading nil) ; take what looks like a subtree
  6913. (org-back-to-heading t)) ; take what is really there
  6914. (org-back-over-empty-lines)
  6915. (setq beg (point))
  6916. (skip-chars-forward " \t\r\n")
  6917. (save-match-data
  6918. (save-excursion (outline-end-of-heading)
  6919. (setq folded (outline-invisible-p)))
  6920. (condition-case nil
  6921. (org-forward-same-level (1- n) t)
  6922. (error nil))
  6923. (org-end-of-subtree t t))
  6924. (org-back-over-empty-lines)
  6925. (setq end (point))
  6926. (goto-char beg0)
  6927. (when (> end beg)
  6928. (setq org-subtree-clip-folded folded)
  6929. (when (or cut force-store-markers)
  6930. (org-save-markers-in-region beg end))
  6931. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  6932. (setq org-subtree-clip (current-kill 0))
  6933. (message "%s: Subtree(s) with %d characters"
  6934. (if cut "Cut" "Copied")
  6935. (length org-subtree-clip)))))
  6936. (defun org-paste-subtree (&optional level tree for-yank)
  6937. "Paste the clipboard as a subtree, with modification of headline level.
  6938. The entire subtree is promoted or demoted in order to match a new headline
  6939. level.
  6940. If the cursor is at the beginning of a headline, the same level as
  6941. that headline is used to paste the tree
  6942. If not, the new level is derived from the *visible* headings
  6943. before and after the insertion point, and taken to be the inferior headline
  6944. level of the two. So if the previous visible heading is level 3 and the
  6945. next is level 4 (or vice versa), level 4 will be used for insertion.
  6946. This makes sure that the subtree remains an independent subtree and does
  6947. not swallow low level entries.
  6948. You can also force a different level, either by using a numeric prefix
  6949. argument, or by inserting the heading marker by hand. For example, if the
  6950. cursor is after \"*****\", then the tree will be shifted to level 5.
  6951. If optional TREE is given, use this text instead of the kill ring.
  6952. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  6953. move back over whitespace before inserting, and move point to the end of
  6954. the inserted text when done."
  6955. (interactive "P")
  6956. (setq tree (or tree (and kill-ring (current-kill 0))))
  6957. (unless (org-kill-is-subtree-p tree)
  6958. (error "%s"
  6959. (substitute-command-keys
  6960. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  6961. (org-with-limited-levels
  6962. (let* ((visp (not (outline-invisible-p)))
  6963. (txt tree)
  6964. (^re_ "\\(\\*+\\)[ \t]*")
  6965. (old-level (if (string-match org-outline-regexp-bol txt)
  6966. (- (match-end 0) (match-beginning 0) 1)
  6967. -1))
  6968. (force-level (cond (level (prefix-numeric-value level))
  6969. ((and (looking-at "[ \t]*$")
  6970. (string-match
  6971. "^\\*+$" (buffer-substring
  6972. (point-at-bol) (point))))
  6973. (- (match-end 1) (match-beginning 1)))
  6974. ((and (bolp)
  6975. (looking-at org-outline-regexp))
  6976. (- (match-end 0) (point) 1))
  6977. (t nil)))
  6978. (previous-level (save-excursion
  6979. (condition-case nil
  6980. (progn
  6981. (outline-previous-visible-heading 1)
  6982. (if (looking-at ^re_)
  6983. (- (match-end 0) (match-beginning 0) 1)
  6984. 1))
  6985. (error 1))))
  6986. (next-level (save-excursion
  6987. (condition-case nil
  6988. (progn
  6989. (or (looking-at org-outline-regexp)
  6990. (outline-next-visible-heading 1))
  6991. (if (looking-at ^re_)
  6992. (- (match-end 0) (match-beginning 0) 1)
  6993. 1))
  6994. (error 1))))
  6995. (new-level (or force-level (max previous-level next-level)))
  6996. (shift (if (or (= old-level -1)
  6997. (= new-level -1)
  6998. (= old-level new-level))
  6999. 0
  7000. (- new-level old-level)))
  7001. (delta (if (> shift 0) -1 1))
  7002. (func (if (> shift 0) 'org-demote 'org-promote))
  7003. (org-odd-levels-only nil)
  7004. beg end newend)
  7005. ;; Remove the forced level indicator
  7006. (if force-level
  7007. (delete-region (point-at-bol) (point)))
  7008. ;; Paste
  7009. (beginning-of-line (if (bolp) 1 2))
  7010. (unless for-yank (org-back-over-empty-lines))
  7011. (setq beg (point))
  7012. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  7013. (insert-before-markers txt)
  7014. (unless (string-match "\n\\'" txt) (insert "\n"))
  7015. (setq newend (point))
  7016. (org-reinstall-markers-in-region beg)
  7017. (setq end (point))
  7018. (goto-char beg)
  7019. (skip-chars-forward " \t\n\r")
  7020. (setq beg (point))
  7021. (if (and (outline-invisible-p) visp)
  7022. (save-excursion (outline-show-heading)))
  7023. ;; Shift if necessary
  7024. (unless (= shift 0)
  7025. (save-restriction
  7026. (narrow-to-region beg end)
  7027. (while (not (= shift 0))
  7028. (org-map-region func (point-min) (point-max))
  7029. (setq shift (+ delta shift)))
  7030. (goto-char (point-min))
  7031. (setq newend (point-max))))
  7032. (when (or (org-called-interactively-p 'interactive) for-yank)
  7033. (message "Clipboard pasted as level %d subtree" new-level))
  7034. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  7035. kill-ring
  7036. (eq org-subtree-clip (current-kill 0))
  7037. org-subtree-clip-folded)
  7038. ;; The tree was folded before it was killed/copied
  7039. (hide-subtree))
  7040. (and for-yank (goto-char newend)))))
  7041. (defun org-kill-is-subtree-p (&optional txt)
  7042. "Check if the current kill is an outline subtree, or a set of trees.
  7043. Returns nil if kill does not start with a headline, or if the first
  7044. headline level is not the largest headline level in the tree.
  7045. So this will actually accept several entries of equal levels as well,
  7046. which is OK for `org-paste-subtree'.
  7047. If optional TXT is given, check this string instead of the current kill."
  7048. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  7049. (re (org-get-limited-outline-regexp))
  7050. (^re (concat "^" re))
  7051. (start-level (and kill
  7052. (string-match
  7053. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7054. kill)
  7055. (- (match-end 2) (match-beginning 2) 1)))
  7056. (start (1+ (or (match-beginning 2) -1))))
  7057. (if (not start-level)
  7058. (progn
  7059. nil) ;; does not even start with a heading
  7060. (catch 'exit
  7061. (while (setq start (string-match ^re kill (1+ start)))
  7062. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7063. (throw 'exit nil)))
  7064. t))))
  7065. (defvar org-markers-to-move nil
  7066. "Markers that should be moved with a cut-and-paste operation.
  7067. Those markers are stored together with their positions relative to
  7068. the start of the region.")
  7069. (defun org-save-markers-in-region (beg end)
  7070. "Check markers in region.
  7071. If these markers are between BEG and END, record their position relative
  7072. to BEG, so that after moving the block of text, we can put the markers back
  7073. into place.
  7074. This function gets called just before an entry or tree gets cut from the
  7075. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7076. called immediately, to move the markers with the entries."
  7077. (setq org-markers-to-move nil)
  7078. (when (featurep 'org-clock)
  7079. (org-clock-save-markers-for-cut-and-paste beg end))
  7080. (when (featurep 'org-agenda)
  7081. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7082. (defun org-check-and-save-marker (marker beg end)
  7083. "Check if MARKER is between BEG and END.
  7084. If yes, remember the marker and the distance to BEG."
  7085. (when (and (marker-buffer marker)
  7086. (equal (marker-buffer marker) (current-buffer)))
  7087. (if (and (>= marker beg) (< marker end))
  7088. (push (cons marker (- marker beg)) org-markers-to-move))))
  7089. (defun org-reinstall-markers-in-region (beg)
  7090. "Move all remembered markers to their position relative to BEG."
  7091. (mapc (lambda (x)
  7092. (move-marker (car x) (+ beg (cdr x))))
  7093. org-markers-to-move)
  7094. (setq org-markers-to-move nil))
  7095. (defun org-narrow-to-subtree ()
  7096. "Narrow buffer to the current subtree."
  7097. (interactive)
  7098. (save-excursion
  7099. (save-match-data
  7100. (org-with-limited-levels
  7101. (narrow-to-region
  7102. (progn (org-back-to-heading t) (point))
  7103. (progn (org-end-of-subtree t t)
  7104. (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7105. (point)))))))
  7106. (defun org-narrow-to-block ()
  7107. "Narrow buffer to the current block."
  7108. (interactive)
  7109. (let* ((case-fold-search t)
  7110. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7111. "^[ \t]*#\\+end_.*")))
  7112. (if blockp
  7113. (narrow-to-region (car blockp) (cdr blockp))
  7114. (error "Not in a block"))))
  7115. (eval-when-compile
  7116. (defvar org-property-drawer-re))
  7117. (defvar org-property-start-re) ;; defined below
  7118. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7119. "Clone the task (subtree) at point N times.
  7120. The clones will be inserted as siblings.
  7121. In interactive use, the user will be prompted for the number of
  7122. clones to be produced, and for a time SHIFT, which may be a
  7123. repeater as used in time stamps, for example `+3d'.
  7124. When a valid repeater is given and the entry contains any time
  7125. stamps, the clones will become a sequence in time, with time
  7126. stamps in the subtree shifted for each clone produced. If SHIFT
  7127. is nil or the empty string, time stamps will be left alone. The
  7128. ID property of the original subtree is removed.
  7129. If the original subtree did contain time stamps with a repeater,
  7130. the following will happen:
  7131. - the repeater will be removed in each clone
  7132. - an additional clone will be produced, with the current, unshifted
  7133. date(s) in the entry.
  7134. - the original entry will be placed *after* all the clones, with
  7135. repeater intact.
  7136. - the start days in the repeater in the original entry will be shifted
  7137. to past the last clone.
  7138. I this way you can spell out a number of instances of a repeating task,
  7139. and still retain the repeater to cover future instances of the task."
  7140. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  7141. (let (beg end template task idprop
  7142. shift-n shift-what doshift nmin nmax (n-no-remove -1)
  7143. (drawer-re org-drawer-regexp))
  7144. (if (not (and (integerp n) (> n 0)))
  7145. (error "Invalid number of replications %s" n))
  7146. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7147. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([dwmy]\\)[ \t]*\\'"
  7148. shift)))
  7149. (error "Invalid shift specification %s" shift))
  7150. (when doshift
  7151. (setq shift-n (string-to-number (match-string 1 shift))
  7152. shift-what (cdr (assoc (match-string 2 shift)
  7153. '(("d" . day) ("w" . week)
  7154. ("m" . month) ("y" . year))))))
  7155. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7156. (setq nmin 1 nmax n)
  7157. (org-back-to-heading t)
  7158. (setq beg (point))
  7159. (setq idprop (org-entry-get nil "ID"))
  7160. (org-end-of-subtree t t)
  7161. (or (bolp) (insert "\n"))
  7162. (setq end (point))
  7163. (setq template (buffer-substring beg end))
  7164. (when (and doshift
  7165. (string-match "<[^<>\n]+ \\+[0-9]+[dwmy][^<>\n]*>" template))
  7166. (delete-region beg end)
  7167. (setq end beg)
  7168. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7169. (goto-char end)
  7170. (loop for n from nmin to nmax do
  7171. ;; prepare clone
  7172. (with-temp-buffer
  7173. (insert template)
  7174. (org-mode)
  7175. (goto-char (point-min))
  7176. (org-show-subtree)
  7177. (and idprop (if org-clone-delete-id
  7178. (org-entry-delete nil "ID")
  7179. (org-id-get-create t)))
  7180. (unless (= n 0)
  7181. (while (re-search-forward "^[ \t]*CLOCK:.*$" nil t)
  7182. (kill-whole-line))
  7183. (goto-char (point-min))
  7184. (while (re-search-forward drawer-re nil t)
  7185. (mapc (lambda (d)
  7186. (org-remove-empty-drawer-at d (point))) org-drawers)))
  7187. (goto-char (point-min))
  7188. (when doshift
  7189. (while (re-search-forward org-ts-regexp-both nil t)
  7190. (org-timestamp-change (* n shift-n) shift-what))
  7191. (unless (= n n-no-remove)
  7192. (goto-char (point-min))
  7193. (while (re-search-forward org-ts-regexp nil t)
  7194. (save-excursion
  7195. (goto-char (match-beginning 0))
  7196. (if (looking-at "<[^<>\n]+\\( +\\+[0-9]+[dwmy]\\)")
  7197. (delete-region (match-beginning 1) (match-end 1)))))))
  7198. (setq task (buffer-string)))
  7199. (insert task))
  7200. (goto-char beg)))
  7201. ;;; Outline Sorting
  7202. (defun org-sort (with-case)
  7203. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7204. Optional argument WITH-CASE means sort case-sensitively."
  7205. (interactive "P")
  7206. (cond
  7207. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7208. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7209. (t
  7210. (org-call-with-arg 'org-sort-entries with-case))))
  7211. (defun org-sort-remove-invisible (s)
  7212. (remove-text-properties 0 (length s) org-rm-props s)
  7213. (while (string-match org-bracket-link-regexp s)
  7214. (setq s (replace-match (if (match-end 2)
  7215. (match-string 3 s)
  7216. (match-string 1 s)) t t s)))
  7217. s)
  7218. (defvar org-priority-regexp) ; defined later in the file
  7219. (defvar org-after-sorting-entries-or-items-hook nil
  7220. "Hook that is run after a bunch of entries or items have been sorted.
  7221. When children are sorted, the cursor is in the parent line when this
  7222. hook gets called. When a region or a plain list is sorted, the cursor
  7223. will be in the first entry of the sorted region/list.")
  7224. (defun org-sort-entries
  7225. (&optional with-case sorting-type getkey-func compare-func property)
  7226. "Sort entries on a certain level of an outline tree.
  7227. If there is an active region, the entries in the region are sorted.
  7228. Else, if the cursor is before the first entry, sort the top-level items.
  7229. Else, the children of the entry at point are sorted.
  7230. Sorting can be alphabetically, numerically, by date/time as given by
  7231. a time stamp, by a property or by priority.
  7232. The command prompts for the sorting type unless it has been given to the
  7233. function through the SORTING-TYPE argument, which needs to be a character,
  7234. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?r ?R ?f ?F). Here is the
  7235. precise meaning of each character:
  7236. n Numerically, by converting the beginning of the entry/item to a number.
  7237. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7238. t By date/time, either the first active time stamp in the entry, or, if
  7239. none exist, by the first inactive one.
  7240. s By the scheduled date/time.
  7241. d By deadline date/time.
  7242. c By creation time, which is assumed to be the first inactive time stamp
  7243. at the beginning of a line.
  7244. p By priority according to the cookie.
  7245. r By the value of a property.
  7246. Capital letters will reverse the sort order.
  7247. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7248. called with point at the beginning of the record. It must return either
  7249. a string or a number that should serve as the sorting key for that record.
  7250. Comparing entries ignores case by default. However, with an optional argument
  7251. WITH-CASE, the sorting considers case as well."
  7252. (interactive "P")
  7253. (let ((case-func (if with-case 'identity 'downcase))
  7254. start beg end stars re re2
  7255. txt what tmp)
  7256. ;; Find beginning and end of region to sort
  7257. (cond
  7258. ((org-region-active-p)
  7259. ;; we will sort the region
  7260. (setq end (region-end)
  7261. what "region")
  7262. (goto-char (region-beginning))
  7263. (if (not (org-at-heading-p)) (outline-next-heading))
  7264. (setq start (point)))
  7265. ((or (org-at-heading-p)
  7266. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  7267. ;; we will sort the children of the current headline
  7268. (org-back-to-heading)
  7269. (setq start (point)
  7270. end (progn (org-end-of-subtree t t)
  7271. (or (bolp) (insert "\n"))
  7272. (org-back-over-empty-lines)
  7273. (point))
  7274. what "children")
  7275. (goto-char start)
  7276. (show-subtree)
  7277. (outline-next-heading))
  7278. (t
  7279. ;; we will sort the top-level entries in this file
  7280. (goto-char (point-min))
  7281. (or (org-at-heading-p) (outline-next-heading))
  7282. (setq start (point))
  7283. (goto-char (point-max))
  7284. (beginning-of-line 1)
  7285. (when (looking-at ".*?\\S-")
  7286. ;; File ends in a non-white line
  7287. (end-of-line 1)
  7288. (insert "\n"))
  7289. (setq end (point-max))
  7290. (setq what "top-level")
  7291. (goto-char start)
  7292. (show-all)))
  7293. (setq beg (point))
  7294. (if (>= beg end) (error "Nothing to sort"))
  7295. (looking-at "\\(\\*+\\)")
  7296. (setq stars (match-string 1)
  7297. re (concat "^" (regexp-quote stars) " +")
  7298. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  7299. txt (buffer-substring beg end))
  7300. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  7301. (if (and (not (equal stars "*")) (string-match re2 txt))
  7302. (error "Region to sort contains a level above the first entry"))
  7303. (unless sorting-type
  7304. (message
  7305. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  7306. [t]ime [s]cheduled [d]eadline [c]reated
  7307. A/N/T/S/D/C/P/O/F means reversed:"
  7308. what)
  7309. (setq sorting-type (read-char-exclusive))
  7310. (and (= (downcase sorting-type) ?f)
  7311. (setq getkey-func
  7312. (org-icompleting-read "Sort using function: "
  7313. obarray 'fboundp t nil nil))
  7314. (setq getkey-func (intern getkey-func)))
  7315. (and (= (downcase sorting-type) ?r)
  7316. (setq property
  7317. (org-icompleting-read "Property: "
  7318. (mapcar 'list (org-buffer-property-keys t))
  7319. nil t))))
  7320. (message "Sorting entries...")
  7321. (save-restriction
  7322. (narrow-to-region start end)
  7323. (let ((dcst (downcase sorting-type))
  7324. (case-fold-search nil)
  7325. (now (current-time)))
  7326. (sort-subr
  7327. (/= dcst sorting-type)
  7328. ;; This function moves to the beginning character of the "record" to
  7329. ;; be sorted.
  7330. (lambda nil
  7331. (if (re-search-forward re nil t)
  7332. (goto-char (match-beginning 0))
  7333. (goto-char (point-max))))
  7334. ;; This function moves to the last character of the "record" being
  7335. ;; sorted.
  7336. (lambda nil
  7337. (save-match-data
  7338. (condition-case nil
  7339. (outline-forward-same-level 1)
  7340. (error
  7341. (goto-char (point-max))))))
  7342. ;; This function returns the value that gets sorted against.
  7343. (lambda nil
  7344. (cond
  7345. ((= dcst ?n)
  7346. (if (looking-at org-complex-heading-regexp)
  7347. (string-to-number (match-string 4))
  7348. nil))
  7349. ((= dcst ?a)
  7350. (if (looking-at org-complex-heading-regexp)
  7351. (funcall case-func (match-string 4))
  7352. nil))
  7353. ((= dcst ?t)
  7354. (let ((end (save-excursion (outline-next-heading) (point))))
  7355. (if (or (re-search-forward org-ts-regexp end t)
  7356. (re-search-forward org-ts-regexp-both end t))
  7357. (org-time-string-to-seconds (match-string 0))
  7358. (org-float-time now))))
  7359. ((= dcst ?c)
  7360. (let ((end (save-excursion (outline-next-heading) (point))))
  7361. (if (re-search-forward
  7362. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  7363. end t)
  7364. (org-time-string-to-seconds (match-string 0))
  7365. (org-float-time now))))
  7366. ((= dcst ?s)
  7367. (let ((end (save-excursion (outline-next-heading) (point))))
  7368. (if (re-search-forward org-scheduled-time-regexp end t)
  7369. (org-time-string-to-seconds (match-string 1))
  7370. (org-float-time now))))
  7371. ((= dcst ?d)
  7372. (let ((end (save-excursion (outline-next-heading) (point))))
  7373. (if (re-search-forward org-deadline-time-regexp end t)
  7374. (org-time-string-to-seconds (match-string 1))
  7375. (org-float-time now))))
  7376. ((= dcst ?p)
  7377. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  7378. (string-to-char (match-string 2))
  7379. org-default-priority))
  7380. ((= dcst ?r)
  7381. (or (org-entry-get nil property) ""))
  7382. ((= dcst ?o)
  7383. (if (looking-at org-complex-heading-regexp)
  7384. (- 9999 (length (member (match-string 2)
  7385. org-todo-keywords-1)))))
  7386. ((= dcst ?f)
  7387. (if getkey-func
  7388. (progn
  7389. (setq tmp (funcall getkey-func))
  7390. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  7391. tmp)
  7392. (error "Invalid key function `%s'" getkey-func)))
  7393. (t (error "Invalid sorting type `%c'" sorting-type))))
  7394. nil
  7395. (cond
  7396. ((= dcst ?a) 'string<)
  7397. ((= dcst ?f) compare-func)
  7398. ((member dcst '(?p ?t ?s ?d ?c)) '<)
  7399. (t nil)))))
  7400. (run-hooks 'org-after-sorting-entries-or-items-hook)
  7401. (message "Sorting entries...done")))
  7402. (defun org-do-sort (table what &optional with-case sorting-type)
  7403. "Sort TABLE of WHAT according to SORTING-TYPE.
  7404. The user will be prompted for the SORTING-TYPE if the call to this
  7405. function does not specify it. WHAT is only for the prompt, to indicate
  7406. what is being sorted. The sorting key will be extracted from
  7407. the car of the elements of the table.
  7408. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  7409. (unless sorting-type
  7410. (message
  7411. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  7412. what)
  7413. (setq sorting-type (read-char-exclusive)))
  7414. (let ((dcst (downcase sorting-type))
  7415. extractfun comparefun)
  7416. ;; Define the appropriate functions
  7417. (cond
  7418. ((= dcst ?n)
  7419. (setq extractfun 'string-to-number
  7420. comparefun (if (= dcst sorting-type) '< '>)))
  7421. ((= dcst ?a)
  7422. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  7423. (lambda(x) (downcase (org-sort-remove-invisible x))))
  7424. comparefun (if (= dcst sorting-type)
  7425. 'string<
  7426. (lambda (a b) (and (not (string< a b))
  7427. (not (string= a b)))))))
  7428. ((= dcst ?t)
  7429. (setq extractfun
  7430. (lambda (x)
  7431. (if (or (string-match org-ts-regexp x)
  7432. (string-match org-ts-regexp-both x))
  7433. (org-float-time
  7434. (org-time-string-to-time (match-string 0 x)))
  7435. 0))
  7436. comparefun (if (= dcst sorting-type) '< '>)))
  7437. (t (error "Invalid sorting type `%c'" sorting-type)))
  7438. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  7439. table)
  7440. (lambda (a b) (funcall comparefun (car a) (car b))))))
  7441. ;;; The orgstruct minor mode
  7442. ;; Define a minor mode which can be used in other modes in order to
  7443. ;; integrate the org-mode structure editing commands.
  7444. ;; This is really a hack, because the org-mode structure commands use
  7445. ;; keys which normally belong to the major mode. Here is how it
  7446. ;; works: The minor mode defines all the keys necessary to operate the
  7447. ;; structure commands, but wraps the commands into a function which
  7448. ;; tests if the cursor is currently at a headline or a plain list
  7449. ;; item. If that is the case, the structure command is used,
  7450. ;; temporarily setting many Org-mode variables like regular
  7451. ;; expressions for filling etc. However, when any of those keys is
  7452. ;; used at a different location, function uses `key-binding' to look
  7453. ;; up if the key has an associated command in another currently active
  7454. ;; keymap (minor modes, major mode, global), and executes that
  7455. ;; command. There might be problems if any of the keys is otherwise
  7456. ;; used as a prefix key.
  7457. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  7458. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  7459. ;; addresses this by checking explicitly for both bindings.
  7460. (defvar orgstruct-mode-map (make-sparse-keymap)
  7461. "Keymap for the minor `orgstruct-mode'.")
  7462. (defvar org-local-vars nil
  7463. "List of local variables, for use by `orgstruct-mode'.")
  7464. ;;;###autoload
  7465. (define-minor-mode orgstruct-mode
  7466. "Toggle the minor mode `orgstruct-mode'.
  7467. This mode is for using Org-mode structure commands in other
  7468. modes. The following keys behave as if Org-mode were active, if
  7469. the cursor is on a headline, or on a plain list item (both as
  7470. defined by Org-mode).
  7471. M-up Move entry/item up
  7472. M-down Move entry/item down
  7473. M-left Promote
  7474. M-right Demote
  7475. M-S-up Move entry/item up
  7476. M-S-down Move entry/item down
  7477. M-S-left Promote subtree
  7478. M-S-right Demote subtree
  7479. M-q Fill paragraph and items like in Org-mode
  7480. C-c ^ Sort entries
  7481. C-c - Cycle list bullet
  7482. TAB Cycle item visibility
  7483. M-RET Insert new heading/item
  7484. S-M-RET Insert new TODO heading / Checkbox item
  7485. C-c C-c Set tags / toggle checkbox"
  7486. nil " OrgStruct" nil
  7487. (org-load-modules-maybe)
  7488. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  7489. ;;;###autoload
  7490. (defun turn-on-orgstruct ()
  7491. "Unconditionally turn on `orgstruct-mode'."
  7492. (orgstruct-mode 1))
  7493. (defun orgstruct++-mode (&optional arg)
  7494. "Toggle `orgstruct-mode', the enhanced version of it.
  7495. In addition to setting orgstruct-mode, this also exports all indentation
  7496. and autofilling variables from org-mode into the buffer. It will also
  7497. recognize item context in multiline items.
  7498. Note that turning off orgstruct-mode will *not* remove the
  7499. indentation/paragraph settings. This can only be done by refreshing the
  7500. major mode, for example with \\[normal-mode]."
  7501. (interactive "P")
  7502. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  7503. (if (< arg 1)
  7504. (orgstruct-mode -1)
  7505. (orgstruct-mode 1)
  7506. (let (var val)
  7507. (mapc
  7508. (lambda (x)
  7509. (when (string-match
  7510. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  7511. (symbol-name (car x)))
  7512. (setq var (car x) val (nth 1 x))
  7513. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  7514. org-local-vars)
  7515. (org-set-local 'orgstruct-is-++ t))))
  7516. (defvar orgstruct-is-++ nil
  7517. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  7518. (make-variable-buffer-local 'orgstruct-is-++)
  7519. ;;;###autoload
  7520. (defun turn-on-orgstruct++ ()
  7521. "Unconditionally turn on `orgstruct++-mode'."
  7522. (orgstruct++-mode 1))
  7523. (defun orgstruct-error ()
  7524. "Error when there is no default binding for a structure key."
  7525. (interactive)
  7526. (error "This key has no function outside structure elements"))
  7527. (defun orgstruct-setup ()
  7528. "Setup orgstruct keymaps."
  7529. (let ((nfunc 0)
  7530. (bindings
  7531. (list
  7532. '([(meta up)] org-metaup)
  7533. '([(meta down)] org-metadown)
  7534. '([(meta left)] org-metaleft)
  7535. '([(meta right)] org-metaright)
  7536. '([(meta shift up)] org-shiftmetaup)
  7537. '([(meta shift down)] org-shiftmetadown)
  7538. '([(meta shift left)] org-shiftmetaleft)
  7539. '([(meta shift right)] org-shiftmetaright)
  7540. '([?\e (up)] org-metaup)
  7541. '([?\e (down)] org-metadown)
  7542. '([?\e (left)] org-metaleft)
  7543. '([?\e (right)] org-metaright)
  7544. '([?\e (shift up)] org-shiftmetaup)
  7545. '([?\e (shift down)] org-shiftmetadown)
  7546. '([?\e (shift left)] org-shiftmetaleft)
  7547. '([?\e (shift right)] org-shiftmetaright)
  7548. '([(shift up)] org-shiftup)
  7549. '([(shift down)] org-shiftdown)
  7550. '([(shift left)] org-shiftleft)
  7551. '([(shift right)] org-shiftright)
  7552. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  7553. '("\M-q" fill-paragraph)
  7554. '("\C-c^" org-sort)
  7555. '("\C-c-" org-cycle-list-bullet)))
  7556. elt key fun cmd)
  7557. (while (setq elt (pop bindings))
  7558. (setq nfunc (1+ nfunc))
  7559. (setq key (org-key (car elt))
  7560. fun (nth 1 elt)
  7561. cmd (orgstruct-make-binding fun nfunc key))
  7562. (org-defkey orgstruct-mode-map key cmd))
  7563. ;; Special treatment needed for TAB and RET
  7564. (org-defkey orgstruct-mode-map [(tab)]
  7565. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  7566. (org-defkey orgstruct-mode-map "\C-i"
  7567. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  7568. (org-defkey orgstruct-mode-map "\M-\C-m"
  7569. (orgstruct-make-binding 'org-insert-heading 105
  7570. "\M-\C-m" [(meta return)]))
  7571. (org-defkey orgstruct-mode-map [(meta return)]
  7572. (orgstruct-make-binding 'org-insert-heading 106
  7573. [(meta return)] "\M-\C-m"))
  7574. (org-defkey orgstruct-mode-map [(shift meta return)]
  7575. (orgstruct-make-binding 'org-insert-todo-heading 107
  7576. [(meta return)] "\M-\C-m"))
  7577. (org-defkey orgstruct-mode-map "\e\C-m"
  7578. (orgstruct-make-binding 'org-insert-heading 108
  7579. "\e\C-m" [?\e (return)]))
  7580. (org-defkey orgstruct-mode-map [?\e (return)]
  7581. (orgstruct-make-binding 'org-insert-heading 109
  7582. [?\e (return)] "\e\C-m"))
  7583. (org-defkey orgstruct-mode-map [?\e (shift return)]
  7584. (orgstruct-make-binding 'org-insert-todo-heading 110
  7585. [?\e (return)] "\e\C-m"))
  7586. (unless org-local-vars
  7587. (setq org-local-vars (org-get-local-variables)))
  7588. t))
  7589. (defun orgstruct-make-binding (fun n &rest keys)
  7590. "Create a function for binding in the structure minor mode.
  7591. FUN is the command to call inside a table. N is used to create a unique
  7592. command name. KEYS are keys that should be checked in for a command
  7593. to execute outside of tables."
  7594. (eval
  7595. (list 'defun
  7596. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  7597. '(arg)
  7598. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  7599. "Outside of structure, run the binding of `"
  7600. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  7601. "'.")
  7602. '(interactive "p")
  7603. (list 'if
  7604. `(org-context-p 'headline 'item
  7605. (and orgstruct-is-++
  7606. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  7607. 'item-body))
  7608. (list 'org-run-like-in-org-mode (list 'quote fun))
  7609. (list 'let '(orgstruct-mode)
  7610. (list 'call-interactively
  7611. (append '(or)
  7612. (mapcar (lambda (k)
  7613. (list 'key-binding k))
  7614. keys)
  7615. '('orgstruct-error))))))))
  7616. (defun org-context-p (&rest contexts)
  7617. "Check if local context is any of CONTEXTS.
  7618. Possible values in the list of contexts are `table', `headline', and `item'."
  7619. (let ((pos (point)))
  7620. (goto-char (point-at-bol))
  7621. (prog1 (or (and (memq 'table contexts)
  7622. (looking-at "[ \t]*|"))
  7623. (and (memq 'headline contexts)
  7624. (looking-at org-outline-regexp))
  7625. (and (memq 'item contexts)
  7626. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  7627. (and (memq 'item-body contexts)
  7628. (org-in-item-p)))
  7629. (goto-char pos))))
  7630. (defun org-get-local-variables ()
  7631. "Return a list of all local variables in an org-mode buffer."
  7632. (let (varlist)
  7633. (with-current-buffer (get-buffer-create "*Org tmp*")
  7634. (erase-buffer)
  7635. (org-mode)
  7636. (setq varlist (buffer-local-variables)))
  7637. (kill-buffer "*Org tmp*")
  7638. (delq nil
  7639. (mapcar
  7640. (lambda (x)
  7641. (setq x
  7642. (if (symbolp x)
  7643. (list x)
  7644. (list (car x) (list 'quote (cdr x)))))
  7645. (if (string-match
  7646. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  7647. (symbol-name (car x)))
  7648. x nil))
  7649. varlist))))
  7650. (defun org-clone-local-variables (from-buffer &optional regexp)
  7651. "Clone local variables from FROM-BUFFER.
  7652. Optional argument REGEXP selects variables to clone."
  7653. (mapc
  7654. (lambda (pair)
  7655. (and (symbolp (car pair))
  7656. (or (null regexp)
  7657. (string-match regexp (symbol-name (car pair))))
  7658. (set (make-local-variable (car pair))
  7659. (cdr pair))))
  7660. (buffer-local-variables from-buffer)))
  7661. ;;;###autoload
  7662. (defun org-run-like-in-org-mode (cmd)
  7663. "Run a command, pretending that the current buffer is in Org-mode.
  7664. This will temporarily bind local variables that are typically bound in
  7665. Org-mode to the values they have in Org-mode, and then interactively
  7666. call CMD."
  7667. (org-load-modules-maybe)
  7668. (unless org-local-vars
  7669. (setq org-local-vars (org-get-local-variables)))
  7670. (eval (list 'let org-local-vars
  7671. (list 'call-interactively (list 'quote cmd)))))
  7672. ;;;; Archiving
  7673. (defun org-get-category (&optional pos force-refresh)
  7674. "Get the category applying to position POS."
  7675. (if force-refresh (org-refresh-category-properties))
  7676. (let ((pos (or pos (point))))
  7677. (or (get-text-property pos 'org-category)
  7678. (progn (org-refresh-category-properties)
  7679. (get-text-property pos 'org-category)))))
  7680. (defun org-refresh-category-properties ()
  7681. "Refresh category text properties in the buffer."
  7682. (let ((def-cat (cond
  7683. ((null org-category)
  7684. (if buffer-file-name
  7685. (file-name-sans-extension
  7686. (file-name-nondirectory buffer-file-name))
  7687. "???"))
  7688. ((symbolp org-category) (symbol-name org-category))
  7689. (t org-category)))
  7690. beg end cat pos optionp)
  7691. (org-unmodified
  7692. (save-excursion
  7693. (save-restriction
  7694. (widen)
  7695. (goto-char (point-min))
  7696. (put-text-property (point) (point-max) 'org-category def-cat)
  7697. (while (re-search-forward
  7698. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  7699. (setq pos (match-end 0)
  7700. optionp (equal (char-after (match-beginning 0)) ?#)
  7701. cat (org-trim (match-string 2)))
  7702. (if optionp
  7703. (setq beg (point-at-bol) end (point-max))
  7704. (org-back-to-heading t)
  7705. (setq beg (point) end (org-end-of-subtree t t)))
  7706. (put-text-property beg end 'org-category cat)
  7707. (put-text-property beg end 'org-category-position beg)
  7708. (goto-char pos)))))))
  7709. ;;;; Link Stuff
  7710. ;;; Link abbreviations
  7711. (defun org-link-expand-abbrev (link)
  7712. "Apply replacements as defined in `org-link-abbrev-alist."
  7713. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  7714. (let* ((key (match-string 1 link))
  7715. (as (or (assoc key org-link-abbrev-alist-local)
  7716. (assoc key org-link-abbrev-alist)))
  7717. (tag (and (match-end 2) (match-string 3 link)))
  7718. rpl)
  7719. (if (not as)
  7720. link
  7721. (setq rpl (cdr as))
  7722. (cond
  7723. ((symbolp rpl) (funcall rpl tag))
  7724. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  7725. ((string-match "%h" rpl)
  7726. (replace-match (url-hexify-string (or tag "")) t t rpl))
  7727. (t (concat rpl tag)))))
  7728. link))
  7729. ;;; Storing and inserting links
  7730. (defvar org-insert-link-history nil
  7731. "Minibuffer history for links inserted with `org-insert-link'.")
  7732. (defvar org-stored-links nil
  7733. "Contains the links stored with `org-store-link'.")
  7734. (defvar org-store-link-plist nil
  7735. "Plist with info about the most recently link created with `org-store-link'.")
  7736. (defvar org-link-protocols nil
  7737. "Link protocols added to Org-mode using `org-add-link-type'.")
  7738. (defvar org-store-link-functions nil
  7739. "List of functions that are called to create and store a link.
  7740. Each function will be called in turn until one returns a non-nil
  7741. value. Each function should check if it is responsible for creating
  7742. this link (for example by looking at the major mode).
  7743. If not, it must exit and return nil.
  7744. If yes, it should return a non-nil value after a calling
  7745. `org-store-link-props' with a list of properties and values.
  7746. Special properties are:
  7747. :type The link prefix, like \"http\". This must be given.
  7748. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  7749. This is obligatory as well.
  7750. :description Optional default description for the second pair
  7751. of brackets in an Org-mode link. The user can still change
  7752. this when inserting this link into an Org-mode buffer.
  7753. In addition to these, any additional properties can be specified
  7754. and then used in remember templates.")
  7755. (defun org-add-link-type (type &optional follow export)
  7756. "Add TYPE to the list of `org-link-types'.
  7757. Re-compute all regular expressions depending on `org-link-types'
  7758. FOLLOW and EXPORT are two functions.
  7759. FOLLOW should take the link path as the single argument and do whatever
  7760. is necessary to follow the link, for example find a file or display
  7761. a mail message.
  7762. EXPORT should format the link path for export to one of the export formats.
  7763. It should be a function accepting three arguments:
  7764. path the path of the link, the text after the prefix (like \"http:\")
  7765. desc the description of the link, if any, or a description added by
  7766. org-export-normalize-links if there is none
  7767. format the export format, a symbol like `html' or `latex' or `ascii'..
  7768. The function may use the FORMAT information to return different values
  7769. depending on the format. The return value will be put literally into
  7770. the exported file. If the return value is nil, this means Org should
  7771. do what it normally does with links which do not have EXPORT defined.
  7772. Org-mode has a built-in default for exporting links. If you are happy with
  7773. this default, there is no need to define an export function for the link
  7774. type. For a simple example of an export function, see `org-bbdb.el'."
  7775. (add-to-list 'org-link-types type t)
  7776. (org-make-link-regexps)
  7777. (if (assoc type org-link-protocols)
  7778. (setcdr (assoc type org-link-protocols) (list follow export))
  7779. (push (list type follow export) org-link-protocols)))
  7780. (defvar org-agenda-buffer-name)
  7781. ;;;###autoload
  7782. (defun org-store-link (arg)
  7783. "\\<org-mode-map>Store an org-link to the current location.
  7784. This link is added to `org-stored-links' and can later be inserted
  7785. into an org-buffer with \\[org-insert-link].
  7786. For some link types, a prefix arg is interpreted:
  7787. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  7788. For file links, arg negates `org-context-in-file-links'."
  7789. (interactive "P")
  7790. (org-load-modules-maybe)
  7791. (setq org-store-link-plist nil) ; reset
  7792. (org-with-limited-levels
  7793. (let (link cpltxt desc description search txt custom-id agenda-link)
  7794. (cond
  7795. ((run-hook-with-args-until-success 'org-store-link-functions)
  7796. (setq link (plist-get org-store-link-plist :link)
  7797. desc (or (plist-get org-store-link-plist :description) link)))
  7798. ((equal (buffer-name) "*Org Edit Src Example*")
  7799. (let (label gc)
  7800. (while (or (not label)
  7801. (save-excursion
  7802. (save-restriction
  7803. (widen)
  7804. (goto-char (point-min))
  7805. (re-search-forward
  7806. (regexp-quote (format org-coderef-label-format label))
  7807. nil t))))
  7808. (when label (message "Label exists already") (sit-for 2))
  7809. (setq label (read-string "Code line label: " label)))
  7810. (end-of-line 1)
  7811. (setq link (format org-coderef-label-format label))
  7812. (setq gc (- 79 (length link)))
  7813. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  7814. (insert link)
  7815. (setq link (concat "(" label ")") desc nil)))
  7816. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  7817. ;; We are in the agenda, link to referenced location
  7818. (let ((m (or (get-text-property (point) 'org-hd-marker)
  7819. (get-text-property (point) 'org-marker))))
  7820. (when m
  7821. (org-with-point-at m
  7822. (setq agenda-link
  7823. (if (org-called-interactively-p 'any)
  7824. (call-interactively 'org-store-link)
  7825. (org-store-link nil)))))))
  7826. ((eq major-mode 'calendar-mode)
  7827. (let ((cd (calendar-cursor-to-date)))
  7828. (setq link
  7829. (format-time-string
  7830. (car org-time-stamp-formats)
  7831. (apply 'encode-time
  7832. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  7833. nil nil nil))))
  7834. (org-store-link-props :type "calendar" :date cd)))
  7835. ((eq major-mode 'w3-mode)
  7836. (setq cpltxt (if (and (buffer-name)
  7837. (not (string-match "Untitled" (buffer-name))))
  7838. (buffer-name)
  7839. (url-view-url t))
  7840. link (org-make-link (url-view-url t)))
  7841. (org-store-link-props :type "w3" :url (url-view-url t)))
  7842. ((eq major-mode 'w3m-mode)
  7843. (setq cpltxt (or w3m-current-title w3m-current-url)
  7844. link (org-make-link w3m-current-url))
  7845. (org-store-link-props :type "w3m" :url (url-view-url t)))
  7846. ((setq search (run-hook-with-args-until-success
  7847. 'org-create-file-search-functions))
  7848. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  7849. "::" search))
  7850. (setq cpltxt (or description link)))
  7851. ((eq major-mode 'image-mode)
  7852. (setq cpltxt (concat "file:"
  7853. (abbreviate-file-name buffer-file-name))
  7854. link (org-make-link cpltxt))
  7855. (org-store-link-props :type "image" :file buffer-file-name))
  7856. ((eq major-mode 'dired-mode)
  7857. ;; link to the file in the current line
  7858. (let ((file (dired-get-filename nil t)))
  7859. (setq file (if file
  7860. (abbreviate-file-name
  7861. (expand-file-name (dired-get-filename nil t)))
  7862. ;; otherwise, no file so use current directory.
  7863. default-directory))
  7864. (setq cpltxt (concat "file:" file)
  7865. link (org-make-link cpltxt))))
  7866. ((and (buffer-file-name (buffer-base-buffer)) (eq major-mode 'org-mode))
  7867. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  7868. (cond
  7869. ((org-in-regexp "<<\\(.*?\\)>>")
  7870. (setq cpltxt
  7871. (concat "file:"
  7872. (abbreviate-file-name
  7873. (buffer-file-name (buffer-base-buffer)))
  7874. "::" (match-string 1))
  7875. link (org-make-link cpltxt)))
  7876. ((and (featurep 'org-id)
  7877. (or (eq org-link-to-org-use-id t)
  7878. (and (eq org-link-to-org-use-id 'create-if-interactive)
  7879. (org-called-interactively-p 'any))
  7880. (and (eq org-link-to-org-use-id
  7881. 'create-if-interactive-and-no-custom-id)
  7882. (org-called-interactively-p 'any)
  7883. (not custom-id))
  7884. (and org-link-to-org-use-id
  7885. (org-entry-get nil "ID"))))
  7886. ;; We can make a link using the ID.
  7887. (setq link (condition-case nil
  7888. (prog1 (org-id-store-link)
  7889. (setq desc (plist-get org-store-link-plist
  7890. :description)))
  7891. (error
  7892. ;; probably before first headline, link to file only
  7893. (concat "file:"
  7894. (abbreviate-file-name
  7895. (buffer-file-name (buffer-base-buffer))))))))
  7896. (t
  7897. ;; Just link to current headline
  7898. (setq cpltxt (concat "file:"
  7899. (abbreviate-file-name
  7900. (buffer-file-name (buffer-base-buffer)))))
  7901. ;; Add a context search string
  7902. (when (org-xor org-context-in-file-links arg)
  7903. (setq txt (cond
  7904. ((org-at-heading-p) nil)
  7905. ((org-region-active-p)
  7906. (buffer-substring (region-beginning) (region-end)))
  7907. (t nil)))
  7908. (when (or (null txt) (string-match "\\S-" txt))
  7909. (setq cpltxt
  7910. (concat cpltxt "::"
  7911. (condition-case nil
  7912. (org-make-org-heading-search-string txt)
  7913. (error "")))
  7914. desc (or (nth 4 (ignore-errors
  7915. (org-heading-components))) "NONE"))))
  7916. (if (string-match "::\\'" cpltxt)
  7917. (setq cpltxt (substring cpltxt 0 -2)))
  7918. (setq link (org-make-link cpltxt)))))
  7919. ((buffer-file-name (buffer-base-buffer))
  7920. ;; Just link to this file here.
  7921. (setq cpltxt (concat "file:"
  7922. (abbreviate-file-name
  7923. (buffer-file-name (buffer-base-buffer)))))
  7924. ;; Add a context string
  7925. (when (org-xor org-context-in-file-links arg)
  7926. (setq txt (if (org-region-active-p)
  7927. (buffer-substring (region-beginning) (region-end))
  7928. (buffer-substring (point-at-bol) (point-at-eol))))
  7929. ;; Only use search option if there is some text.
  7930. (when (string-match "\\S-" txt)
  7931. (setq cpltxt
  7932. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  7933. desc "NONE")))
  7934. (setq link (org-make-link cpltxt)))
  7935. ((org-called-interactively-p 'interactive)
  7936. (error "Cannot link to a buffer which is not visiting a file"))
  7937. (t (setq link nil)))
  7938. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  7939. (setq link (or link cpltxt)
  7940. desc (or desc cpltxt))
  7941. (if (equal desc "NONE") (setq desc nil))
  7942. (if (and (or (org-called-interactively-p 'any) executing-kbd-macro) link)
  7943. (progn
  7944. (setq org-stored-links
  7945. (cons (list link desc) org-stored-links))
  7946. (message "Stored: %s" (or desc link))
  7947. (when custom-id
  7948. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  7949. "::#" custom-id))
  7950. (setq org-stored-links
  7951. (cons (list link desc) org-stored-links))))
  7952. (or agenda-link (and link (org-make-link-string link desc)))))))
  7953. (defun org-store-link-props (&rest plist)
  7954. "Store link properties, extract names and addresses."
  7955. (let (x adr)
  7956. (when (setq x (plist-get plist :from))
  7957. (setq adr (mail-extract-address-components x))
  7958. (setq plist (plist-put plist :fromname (car adr)))
  7959. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  7960. (when (setq x (plist-get plist :to))
  7961. (setq adr (mail-extract-address-components x))
  7962. (setq plist (plist-put plist :toname (car adr)))
  7963. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  7964. (let ((from (plist-get plist :from))
  7965. (to (plist-get plist :to)))
  7966. (when (and from to org-from-is-user-regexp)
  7967. (setq plist
  7968. (plist-put plist :fromto
  7969. (if (string-match org-from-is-user-regexp from)
  7970. (concat "to %t")
  7971. (concat "from %f"))))))
  7972. (setq org-store-link-plist plist))
  7973. (defun org-add-link-props (&rest plist)
  7974. "Add these properties to the link property list."
  7975. (let (key value)
  7976. (while plist
  7977. (setq key (pop plist) value (pop plist))
  7978. (setq org-store-link-plist
  7979. (plist-put org-store-link-plist key value)))))
  7980. (defun org-email-link-description (&optional fmt)
  7981. "Return the description part of an email link.
  7982. This takes information from `org-store-link-plist' and formats it
  7983. according to FMT (default from `org-email-link-description-format')."
  7984. (setq fmt (or fmt org-email-link-description-format))
  7985. (let* ((p org-store-link-plist)
  7986. (to (plist-get p :toaddress))
  7987. (from (plist-get p :fromaddress))
  7988. (table
  7989. (list
  7990. (cons "%c" (plist-get p :fromto))
  7991. (cons "%F" (plist-get p :from))
  7992. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  7993. (cons "%T" (plist-get p :to))
  7994. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  7995. (cons "%s" (plist-get p :subject))
  7996. (cons "%d" (plist-get p :date))
  7997. (cons "%m" (plist-get p :message-id)))))
  7998. (when (string-match "%c" fmt)
  7999. ;; Check if the user wrote this message
  8000. (if (and org-from-is-user-regexp from to
  8001. (save-match-data (string-match org-from-is-user-regexp from)))
  8002. (setq fmt (replace-match "to %t" t t fmt))
  8003. (setq fmt (replace-match "from %f" t t fmt))))
  8004. (org-replace-escapes fmt table)))
  8005. (defun org-make-org-heading-search-string (&optional string heading)
  8006. "Make search string for STRING or current headline."
  8007. (interactive)
  8008. (let ((s (or string (org-get-heading)))
  8009. (lines org-context-in-file-links))
  8010. (unless (and string (not heading))
  8011. ;; We are using a headline, clean up garbage in there.
  8012. (if (string-match org-todo-regexp s)
  8013. (setq s (replace-match "" t t s)))
  8014. (if (string-match (org-re ":[[:alnum:]_@#%:]+:[ \t]*$") s)
  8015. (setq s (replace-match "" t t s)))
  8016. (setq s (org-trim s))
  8017. (if (string-match (concat "^\\(" org-quote-string "\\|"
  8018. org-comment-string "\\)") s)
  8019. (setq s (replace-match "" t t s)))
  8020. (while (string-match org-ts-regexp s)
  8021. (setq s (replace-match "" t t s))))
  8022. (or string (setq s (concat "*" s))) ; Add * for headlines
  8023. (when (and string (integerp lines) (> lines 0))
  8024. (let ((slines (org-split-string s "\n")))
  8025. (when (< lines (length slines))
  8026. (setq s (mapconcat
  8027. 'identity
  8028. (reverse (nthcdr (- (length slines) lines)
  8029. (reverse slines))) "\n")))))
  8030. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  8031. (defun org-make-link (&rest strings)
  8032. "Concatenate STRINGS."
  8033. (apply 'concat strings))
  8034. (defun org-make-link-string (link &optional description)
  8035. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8036. (unless (string-match "\\S-" link)
  8037. (error "Empty link"))
  8038. (when (and description
  8039. (stringp description)
  8040. (not (string-match "\\S-" description)))
  8041. (setq description nil))
  8042. (when (stringp description)
  8043. ;; Remove brackets from the description, they are fatal.
  8044. (while (string-match "\\[" description)
  8045. (setq description (replace-match "{" t t description)))
  8046. (while (string-match "\\]" description)
  8047. (setq description (replace-match "}" t t description))))
  8048. (when (equal link description)
  8049. ;; No description needed, it is identical
  8050. (setq description nil))
  8051. (when (and (not description)
  8052. (not (string-match (org-image-file-name-regexp) link))
  8053. (not (equal link (org-link-escape link))))
  8054. (setq description (org-extract-attributes link)))
  8055. (setq link
  8056. (cond ((string-match (org-image-file-name-regexp) link) link)
  8057. ((string-match org-link-types-re link)
  8058. (concat (match-string 1 link)
  8059. (org-link-escape (substring link (match-end 1)))))
  8060. (t (org-link-escape link))))
  8061. (concat "[[" link "]"
  8062. (if description (concat "[" description "]") "")
  8063. "]"))
  8064. (defconst org-link-escape-chars
  8065. '(?\ ?\[ ?\] ?\; ?\= ?\+)
  8066. "List of characters that should be escaped in link.
  8067. This is the list that is used for internal purposes.")
  8068. (defvar org-url-encoding-use-url-hexify nil)
  8069. (defconst org-link-escape-chars-browser
  8070. '(?\ )
  8071. "List of escapes for characters that are problematic in links.
  8072. This is the list that is used before handing over to the browser.")
  8073. (defun org-link-escape (text &optional table merge)
  8074. "Return percent escaped representation of TEXT.
  8075. TEXT is a string with the text to escape.
  8076. Optional argument TABLE is a list with characters that should be
  8077. escaped. When nil, `org-link-escape-chars' is used.
  8078. If optional argument MERGE is set, merge TABLE into
  8079. `org-link-escape-chars'."
  8080. (if (and org-url-encoding-use-url-hexify (not table))
  8081. (url-hexify-string text)
  8082. (cond
  8083. ((and table merge)
  8084. (mapc (lambda (defchr)
  8085. (unless (member defchr table)
  8086. (setq table (cons defchr table)))) org-link-escape-chars))
  8087. ((null table)
  8088. (setq table org-link-escape-chars)))
  8089. (mapconcat
  8090. (lambda (char)
  8091. (if (or (member char table)
  8092. (< char 32) (= char 37) (> char 126))
  8093. (mapconcat (lambda (sequence-element)
  8094. (format "%%%.2X" sequence-element))
  8095. (or (encode-coding-char char 'utf-8)
  8096. (error "Unable to percent escape character: %s"
  8097. (char-to-string char))) "")
  8098. (char-to-string char))) text "")))
  8099. (defun org-link-unescape (str)
  8100. "Unhex hexified Unicode strings as returned from the JavaScript function
  8101. encodeURIComponent. E.g. `%C3%B6' is the german Umlaut `ö'."
  8102. (unless (and (null str) (string= "" str))
  8103. (let ((pos 0) (case-fold-search t) unhexed)
  8104. (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
  8105. (setq unhexed (org-link-unescape-compound (match-string 0 str)))
  8106. (setq str (replace-match unhexed t t str))
  8107. (setq pos (+ pos (length unhexed))))))
  8108. str)
  8109. (defun org-link-unescape-compound (hex)
  8110. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German Umlaut `ö'.
  8111. Note: this function also decodes single byte encodings like
  8112. `%E1' (\"á\") if not followed by another `%[A-F0-9]{2}' group."
  8113. (save-match-data
  8114. (let* ((bytes (cdr (split-string hex "%")))
  8115. (ret "")
  8116. (eat 0)
  8117. (sum 0))
  8118. (while bytes
  8119. (let* ((val (string-to-number (pop bytes) 16))
  8120. (shift-xor
  8121. (if (= 0 eat)
  8122. (cond
  8123. ((>= val 252) (cons 6 252))
  8124. ((>= val 248) (cons 5 248))
  8125. ((>= val 240) (cons 4 240))
  8126. ((>= val 224) (cons 3 224))
  8127. ((>= val 192) (cons 2 192))
  8128. (t (cons 0 0)))
  8129. (cons 6 128))))
  8130. (if (>= val 192) (setq eat (car shift-xor)))
  8131. (setq val (logxor val (cdr shift-xor)))
  8132. (setq sum (+ (lsh sum (car shift-xor)) val))
  8133. (if (> eat 0) (setq eat (- eat 1)))
  8134. (cond
  8135. ((= 0 eat) ;multi byte
  8136. (setq ret (concat ret (org-char-to-string sum)))
  8137. (setq sum 0))
  8138. ((not bytes) ; single byte(s)
  8139. (setq ret (org-link-unescape-single-byte-sequence hex))))
  8140. )) ;; end (while bytes
  8141. ret )))
  8142. (defun org-link-unescape-single-byte-sequence (hex)
  8143. "Unhexify hex-encoded single byte character sequences."
  8144. (mapconcat (lambda (byte)
  8145. (char-to-string (string-to-number byte 16)))
  8146. (cdr (split-string hex "%")) ""))
  8147. (defun org-xor (a b)
  8148. "Exclusive or."
  8149. (if a (not b) b))
  8150. (defun org-fixup-message-id-for-http (s)
  8151. "Replace special characters in a message id, so it can be used in an http query."
  8152. (when (string-match "%" s)
  8153. (setq s (mapconcat (lambda (c)
  8154. (if (eq c ?%)
  8155. "%25"
  8156. (char-to-string c)))
  8157. s "")))
  8158. (while (string-match "<" s)
  8159. (setq s (replace-match "%3C" t t s)))
  8160. (while (string-match ">" s)
  8161. (setq s (replace-match "%3E" t t s)))
  8162. (while (string-match "@" s)
  8163. (setq s (replace-match "%40" t t s)))
  8164. s)
  8165. ;;;###autoload
  8166. (defun org-insert-link-global ()
  8167. "Insert a link like Org-mode does.
  8168. This command can be called in any mode to insert a link in Org-mode syntax."
  8169. (interactive)
  8170. (org-load-modules-maybe)
  8171. (org-run-like-in-org-mode 'org-insert-link))
  8172. (defun org-insert-link (&optional complete-file link-location default-description)
  8173. "Insert a link. At the prompt, enter the link.
  8174. Completion can be used to insert any of the link protocol prefixes like
  8175. http or ftp in use.
  8176. The history can be used to select a link previously stored with
  8177. `org-store-link'. When the empty string is entered (i.e. if you just
  8178. press RET at the prompt), the link defaults to the most recently
  8179. stored link. As SPC triggers completion in the minibuffer, you need to
  8180. use M-SPC or C-q SPC to force the insertion of a space character.
  8181. You will also be prompted for a description, and if one is given, it will
  8182. be displayed in the buffer instead of the link.
  8183. If there is already a link at point, this command will allow you to edit link
  8184. and description parts.
  8185. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  8186. be selected using completion. The path to the file will be relative to the
  8187. current directory if the file is in the current directory or a subdirectory.
  8188. Otherwise, the link will be the absolute path as completed in the minibuffer
  8189. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  8190. option `org-link-file-path-type'.
  8191. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  8192. the current directory or below.
  8193. With three \\[universal-argument] prefixes, negate the meaning of
  8194. `org-keep-stored-link-after-insertion'.
  8195. If `org-make-link-description-function' is non-nil, this function will be
  8196. called with the link target, and the result will be the default
  8197. link description.
  8198. If the LINK-LOCATION parameter is non-nil, this value will be
  8199. used as the link location instead of reading one interactively.
  8200. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  8201. be used as the default description."
  8202. (interactive "P")
  8203. (let* ((wcf (current-window-configuration))
  8204. (region (if (org-region-active-p)
  8205. (buffer-substring (region-beginning) (region-end))))
  8206. (remove (and region (list (region-beginning) (region-end))))
  8207. (desc region)
  8208. tmphist ; byte-compile incorrectly complains about this
  8209. (link link-location)
  8210. entry file all-prefixes)
  8211. (cond
  8212. (link-location) ; specified by arg, just use it.
  8213. ((org-in-regexp org-bracket-link-regexp 1)
  8214. ;; We do have a link at point, and we are going to edit it.
  8215. (setq remove (list (match-beginning 0) (match-end 0)))
  8216. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  8217. (setq link (read-string "Link: "
  8218. (org-link-unescape
  8219. (org-match-string-no-properties 1)))))
  8220. ((or (org-in-regexp org-angle-link-re)
  8221. (org-in-regexp org-plain-link-re))
  8222. ;; Convert to bracket link
  8223. (setq remove (list (match-beginning 0) (match-end 0))
  8224. link (read-string "Link: "
  8225. (org-remove-angle-brackets (match-string 0)))))
  8226. ((member complete-file '((4) (16)))
  8227. ;; Completing read for file names.
  8228. (setq link (org-file-complete-link complete-file)))
  8229. (t
  8230. ;; Read link, with completion for stored links.
  8231. (with-output-to-temp-buffer "*Org Links*"
  8232. (princ "Insert a link.
  8233. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  8234. (when org-stored-links
  8235. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  8236. (princ (mapconcat
  8237. (lambda (x)
  8238. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  8239. (reverse org-stored-links) "\n"))))
  8240. (let ((cw (selected-window)))
  8241. (select-window (get-buffer-window "*Org Links*" 'visible))
  8242. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  8243. (unless (pos-visible-in-window-p (point-max))
  8244. (org-fit-window-to-buffer))
  8245. (and (window-live-p cw) (select-window cw)))
  8246. ;; Fake a link history, containing the stored links.
  8247. (setq tmphist (append (mapcar 'car org-stored-links)
  8248. org-insert-link-history))
  8249. (setq all-prefixes (append (mapcar 'car org-link-abbrev-alist-local)
  8250. (mapcar 'car org-link-abbrev-alist)
  8251. org-link-types))
  8252. (unwind-protect
  8253. (progn
  8254. (setq link
  8255. (let ((org-completion-use-ido nil)
  8256. (org-completion-use-iswitchb nil))
  8257. (org-completing-read
  8258. "Link: "
  8259. (append
  8260. (mapcar (lambda (x) (list (concat x ":")))
  8261. all-prefixes)
  8262. (mapcar 'car org-stored-links))
  8263. nil nil nil
  8264. 'tmphist
  8265. (car (car org-stored-links)))))
  8266. (if (not (string-match "\\S-" link))
  8267. (error "No link selected"))
  8268. (if (or (member link all-prefixes)
  8269. (and (equal ":" (substring link -1))
  8270. (member (substring link 0 -1) all-prefixes)
  8271. (setq link (substring link 0 -1))))
  8272. (setq link (org-link-try-special-completion link))))
  8273. (set-window-configuration wcf)
  8274. (kill-buffer "*Org Links*"))
  8275. (setq entry (assoc link org-stored-links))
  8276. (or entry (push link org-insert-link-history))
  8277. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  8278. (not org-keep-stored-link-after-insertion))
  8279. (setq org-stored-links (delq (assoc link org-stored-links)
  8280. org-stored-links)))
  8281. (setq desc (or desc (nth 1 entry)))))
  8282. (if (string-match org-plain-link-re link)
  8283. ;; URL-like link, normalize the use of angular brackets.
  8284. (setq link (org-make-link (org-remove-angle-brackets link))))
  8285. ;; Check if we are linking to the current file with a search option
  8286. ;; If yes, simplify the link by using only the search option.
  8287. (when (and buffer-file-name
  8288. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  8289. (let* ((path (match-string 1 link))
  8290. (case-fold-search nil)
  8291. (search (match-string 2 link)))
  8292. (save-match-data
  8293. (if (equal (file-truename buffer-file-name) (file-truename path))
  8294. ;; We are linking to this same file, with a search option
  8295. (setq link search)))))
  8296. ;; Check if we can/should use a relative path. If yes, simplify the link
  8297. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  8298. (let* ((type (match-string 1 link))
  8299. (path (match-string 2 link))
  8300. (origpath path)
  8301. (case-fold-search nil))
  8302. (cond
  8303. ((or (eq org-link-file-path-type 'absolute)
  8304. (equal complete-file '(16)))
  8305. (setq path (abbreviate-file-name (expand-file-name path))))
  8306. ((eq org-link-file-path-type 'noabbrev)
  8307. (setq path (expand-file-name path)))
  8308. ((eq org-link-file-path-type 'relative)
  8309. (setq path (file-relative-name path)))
  8310. (t
  8311. (save-match-data
  8312. (if (string-match (concat "^" (regexp-quote
  8313. (expand-file-name
  8314. (file-name-as-directory
  8315. default-directory))))
  8316. (expand-file-name path))
  8317. ;; We are linking a file with relative path name.
  8318. (setq path (substring (expand-file-name path)
  8319. (match-end 0)))
  8320. (setq path (abbreviate-file-name (expand-file-name path)))))))
  8321. (setq link (concat type path))
  8322. (if (equal desc origpath)
  8323. (setq desc path))))
  8324. (if org-make-link-description-function
  8325. (setq desc (funcall org-make-link-description-function link desc))
  8326. (if default-description (setq desc default-description)))
  8327. (setq desc (read-string "Description: " desc))
  8328. (unless (string-match "\\S-" desc) (setq desc nil))
  8329. (if remove (apply 'delete-region remove))
  8330. (insert (org-make-link-string link desc))))
  8331. (defun org-link-try-special-completion (type)
  8332. "If there is completion support for link type TYPE, offer it."
  8333. (let ((fun (intern (concat "org-" type "-complete-link"))))
  8334. (if (functionp fun)
  8335. (funcall fun)
  8336. (read-string "Link (no completion support): " (concat type ":")))))
  8337. (defun org-file-complete-link (&optional arg)
  8338. "Create a file link using completion."
  8339. (let (file link)
  8340. (setq file (read-file-name "File: "))
  8341. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  8342. (pwd1 (file-name-as-directory (abbreviate-file-name
  8343. (expand-file-name ".")))))
  8344. (cond
  8345. ((equal arg '(16))
  8346. (setq link (org-make-link
  8347. "file:"
  8348. (abbreviate-file-name (expand-file-name file)))))
  8349. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  8350. (setq link (org-make-link "file:" (match-string 1 file))))
  8351. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  8352. (expand-file-name file))
  8353. (setq link (org-make-link
  8354. "file:" (match-string 1 (expand-file-name file)))))
  8355. (t (setq link (org-make-link "file:" file)))))
  8356. link))
  8357. (defun org-completing-read (&rest args)
  8358. "Completing-read with SPACE being a normal character."
  8359. (let ((enable-recursive-minibuffers t)
  8360. (minibuffer-local-completion-map
  8361. (copy-keymap minibuffer-local-completion-map)))
  8362. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  8363. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  8364. (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
  8365. (apply 'org-icompleting-read args)))
  8366. (defun org-completing-read-no-i (&rest args)
  8367. (let (org-completion-use-ido org-completion-use-iswitchb)
  8368. (apply 'org-completing-read args)))
  8369. (defun org-iswitchb-completing-read (prompt choices &rest args)
  8370. "Use iswitch as a completing-read replacement to choose from choices.
  8371. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  8372. from."
  8373. (let* ((iswitchb-use-virtual-buffers nil)
  8374. (iswitchb-make-buflist-hook
  8375. (lambda ()
  8376. (setq iswitchb-temp-buflist choices))))
  8377. (iswitchb-read-buffer prompt)))
  8378. (defun org-icompleting-read (&rest args)
  8379. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  8380. (org-without-partial-completion
  8381. (if (and org-completion-use-ido
  8382. (fboundp 'ido-completing-read)
  8383. (boundp 'ido-mode) ido-mode
  8384. (listp (second args)))
  8385. (let ((ido-enter-matching-directory nil))
  8386. (apply 'ido-completing-read (concat (car args))
  8387. (if (consp (car (nth 1 args)))
  8388. (mapcar 'car (nth 1 args))
  8389. (nth 1 args))
  8390. (cddr args)))
  8391. (if (and org-completion-use-iswitchb
  8392. (boundp 'iswitchb-mode) iswitchb-mode
  8393. (listp (second args)))
  8394. (apply 'org-iswitchb-completing-read (concat (car args))
  8395. (if (consp (car (nth 1 args)))
  8396. (mapcar 'car (nth 1 args))
  8397. (nth 1 args))
  8398. (cddr args))
  8399. (apply 'completing-read args)))))
  8400. (defun org-extract-attributes (s)
  8401. "Extract the attributes cookie from a string and set as text property."
  8402. (let (a attr (start 0) key value)
  8403. (save-match-data
  8404. (when (string-match "{{\\([^}]+\\)}}$" s)
  8405. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  8406. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  8407. (setq key (match-string 1 a) value (match-string 2 a)
  8408. start (match-end 0)
  8409. attr (plist-put attr (intern key) value))))
  8410. (org-add-props s nil 'org-attr attr))
  8411. s))
  8412. (defun org-extract-attributes-from-string (tag)
  8413. (let (key value attr)
  8414. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  8415. (setq key (match-string 1 tag) value (match-string 2 tag)
  8416. tag (replace-match "" t t tag)
  8417. attr (plist-put attr (intern key) value)))
  8418. (cons tag attr)))
  8419. (defun org-attributes-to-string (plist)
  8420. "Format a property list into an HTML attribute list."
  8421. (let ((s "") key value)
  8422. (while plist
  8423. (setq key (pop plist) value (pop plist))
  8424. (and value
  8425. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  8426. s))
  8427. ;;; Opening/following a link
  8428. (defvar org-link-search-failed nil)
  8429. (defvar org-open-link-functions nil
  8430. "Hook for functions finding a plain text link.
  8431. These functions must take a single argument, the link content.
  8432. They will be called for links that look like [[link text][description]]
  8433. when LINK TEXT does not have a protocol like \"http:\" and does not look
  8434. like a filename (e.g. \"./blue.png\").
  8435. These functions will be called *before* Org attempts to resolve the
  8436. link by doing text searches in the current buffer - so if you want a
  8437. link \"[[target]]\" to still find \"<<target>>\", your function should
  8438. handle this as a special case.
  8439. When the function does handle the link, it must return a non-nil value.
  8440. If it decides that it is not responsible for this link, it must return
  8441. nil to indicate that that Org-mode can continue with other options
  8442. like exact and fuzzy text search.")
  8443. (defun org-next-link ()
  8444. "Move forward to the next link.
  8445. If the link is in hidden text, expose it."
  8446. (interactive)
  8447. (when (and org-link-search-failed (eq this-command last-command))
  8448. (goto-char (point-min))
  8449. (message "Link search wrapped back to beginning of buffer"))
  8450. (setq org-link-search-failed nil)
  8451. (let* ((pos (point))
  8452. (ct (org-context))
  8453. (a (assoc :link ct)))
  8454. (if a (goto-char (nth 2 a)))
  8455. (if (re-search-forward org-any-link-re nil t)
  8456. (progn
  8457. (goto-char (match-beginning 0))
  8458. (if (outline-invisible-p) (org-show-context)))
  8459. (goto-char pos)
  8460. (setq org-link-search-failed t)
  8461. (error "No further link found"))))
  8462. (defun org-previous-link ()
  8463. "Move backward to the previous link.
  8464. If the link is in hidden text, expose it."
  8465. (interactive)
  8466. (when (and org-link-search-failed (eq this-command last-command))
  8467. (goto-char (point-max))
  8468. (message "Link search wrapped back to end of buffer"))
  8469. (setq org-link-search-failed nil)
  8470. (let* ((pos (point))
  8471. (ct (org-context))
  8472. (a (assoc :link ct)))
  8473. (if a (goto-char (nth 1 a)))
  8474. (if (re-search-backward org-any-link-re nil t)
  8475. (progn
  8476. (goto-char (match-beginning 0))
  8477. (if (outline-invisible-p) (org-show-context)))
  8478. (goto-char pos)
  8479. (setq org-link-search-failed t)
  8480. (error "No further link found"))))
  8481. (defun org-translate-link (s)
  8482. "Translate a link string if a translation function has been defined."
  8483. (if (and org-link-translation-function
  8484. (fboundp org-link-translation-function)
  8485. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  8486. (progn
  8487. (setq s (funcall org-link-translation-function
  8488. (match-string 1) (match-string 2)))
  8489. (concat (car s) ":" (cdr s)))
  8490. s))
  8491. (defun org-translate-link-from-planner (type path)
  8492. "Translate a link from Emacs Planner syntax so that Org can follow it.
  8493. This is still an experimental function, your mileage may vary."
  8494. (cond
  8495. ((member type '("http" "https" "news" "ftp"))
  8496. ;; standard Internet links are the same.
  8497. nil)
  8498. ((and (equal type "irc") (string-match "^//" path))
  8499. ;; Planner has two / at the beginning of an irc link, we have 1.
  8500. ;; We should have zero, actually....
  8501. (setq path (substring path 1)))
  8502. ((and (equal type "lisp") (string-match "^/" path))
  8503. ;; Planner has a slash, we do not.
  8504. (setq type "elisp" path (substring path 1)))
  8505. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  8506. ;; A typical message link. Planner has the id after the final slash,
  8507. ;; we separate it with a hash mark
  8508. (setq path (concat (match-string 1 path) "#"
  8509. (org-remove-angle-brackets (match-string 2 path)))))
  8510. )
  8511. (cons type path))
  8512. (defun org-find-file-at-mouse (ev)
  8513. "Open file link or URL at mouse."
  8514. (interactive "e")
  8515. (mouse-set-point ev)
  8516. (org-open-at-point 'in-emacs))
  8517. (defun org-open-at-mouse (ev)
  8518. "Open file link or URL at mouse.
  8519. See the docstring of `org-open-file' for details."
  8520. (interactive "e")
  8521. (mouse-set-point ev)
  8522. (if (eq major-mode 'org-agenda-mode)
  8523. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  8524. (org-open-at-point))
  8525. (defvar org-window-config-before-follow-link nil
  8526. "The window configuration before following a link.
  8527. This is saved in case the need arises to restore it.")
  8528. (defvar org-open-link-marker (make-marker)
  8529. "Marker pointing to the location where `org-open-at-point; was called.")
  8530. ;;;###autoload
  8531. (defun org-open-at-point-global ()
  8532. "Follow a link like Org-mode does.
  8533. This command can be called in any mode to follow a link that has
  8534. Org-mode syntax."
  8535. (interactive)
  8536. (org-run-like-in-org-mode 'org-open-at-point))
  8537. ;;;###autoload
  8538. (defun org-open-link-from-string (s &optional arg reference-buffer)
  8539. "Open a link in the string S, as if it was in Org-mode."
  8540. (interactive "sLink: \nP")
  8541. (let ((reference-buffer (or reference-buffer (current-buffer))))
  8542. (with-temp-buffer
  8543. (let ((org-inhibit-startup (not reference-buffer)))
  8544. (org-mode)
  8545. (insert s)
  8546. (goto-char (point-min))
  8547. (when reference-buffer
  8548. (setq org-link-abbrev-alist-local
  8549. (with-current-buffer reference-buffer
  8550. org-link-abbrev-alist-local)))
  8551. (org-open-at-point arg reference-buffer)))))
  8552. (defvar org-open-at-point-functions nil
  8553. "Hook that is run when following a link at point.
  8554. Functions in this hook must return t if they identify and follow
  8555. a link at point. If they don't find anything interesting at point,
  8556. they must return nil.")
  8557. (defun org-open-at-point (&optional arg reference-buffer)
  8558. "Open link at or after point.
  8559. If there is no link at point, this function will search forward up to
  8560. the end of the current line.
  8561. Normally, files will be opened by an appropriate application. If the
  8562. optional prefix argument ARG is non-nil, Emacs will visit the file.
  8563. With a double prefix argument, try to open outside of Emacs, in the
  8564. application the system uses for this file type."
  8565. (interactive "P")
  8566. ;; if in a code block, then open the block's results
  8567. (unless (call-interactively #'org-babel-open-src-block-result)
  8568. (org-load-modules-maybe)
  8569. (move-marker org-open-link-marker (point))
  8570. (setq org-window-config-before-follow-link (current-window-configuration))
  8571. (org-remove-occur-highlights nil nil t)
  8572. (cond
  8573. ((and (org-at-heading-p)
  8574. (not (org-in-regexp
  8575. (concat org-plain-link-re "\\|"
  8576. org-bracket-link-regexp "\\|"
  8577. org-angle-link-re "\\|"
  8578. "[ \t]:[^ \t\n]+:[ \t]*$")))
  8579. (not (get-text-property (point) 'org-linked-text)))
  8580. (or (org-offer-links-in-entry arg)
  8581. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  8582. ((run-hook-with-args-until-success 'org-open-at-point-functions))
  8583. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  8584. ((and (or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  8585. (not (org-in-regexp org-bracket-link-regexp)))
  8586. (org-footnote-action))
  8587. (t
  8588. (let (type path link line search (pos (point)))
  8589. (catch 'match
  8590. (save-excursion
  8591. (skip-chars-forward "^]\n\r")
  8592. (when (org-in-regexp org-bracket-link-regexp 1)
  8593. (setq link (org-extract-attributes
  8594. (org-link-unescape (org-match-string-no-properties 1))))
  8595. (while (string-match " *\n *" link)
  8596. (setq link (replace-match " " t t link)))
  8597. (setq link (org-link-expand-abbrev link))
  8598. (cond
  8599. ((or (file-name-absolute-p link)
  8600. (string-match "^\\.\\.?/" link))
  8601. (setq type "file" path link))
  8602. ((string-match org-link-re-with-space3 link)
  8603. (setq type (match-string 1 link) path (match-string 2 link)))
  8604. (t (setq type "thisfile" path link)))
  8605. (throw 'match t)))
  8606. (when (get-text-property (point) 'org-linked-text)
  8607. (setq type "thisfile"
  8608. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  8609. (1+ (point)) (point))
  8610. path (buffer-substring
  8611. (or (previous-single-property-change pos 'org-linked-text)
  8612. (point-min))
  8613. (or (next-single-property-change pos 'org-linked-text)
  8614. (point-max))))
  8615. (throw 'match t))
  8616. (save-excursion
  8617. (when (or (org-in-regexp org-angle-link-re)
  8618. (org-in-regexp org-plain-link-re))
  8619. (setq type (match-string 1)
  8620. path (org-link-unescape (match-string 2)))
  8621. (throw 'match t)))
  8622. (save-excursion
  8623. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
  8624. (setq type "tags"
  8625. path (match-string 1))
  8626. (while (string-match ":" path)
  8627. (setq path (replace-match "+" t t path)))
  8628. (throw 'match t)))
  8629. (when (org-in-regexp "<\\([^><\n]+\\)>")
  8630. (setq type "tree-match"
  8631. path (match-string 1))
  8632. (throw 'match t)))
  8633. (unless path
  8634. (error "No link found"))
  8635. ;; switch back to reference buffer
  8636. ;; needed when if called in a temporary buffer through
  8637. ;; org-open-link-from-string
  8638. (with-current-buffer (or reference-buffer (current-buffer))
  8639. ;; Remove any trailing spaces in path
  8640. (if (string-match " +\\'" path)
  8641. (setq path (replace-match "" t t path)))
  8642. (if (and org-link-translation-function
  8643. (fboundp org-link-translation-function))
  8644. ;; Check if we need to translate the link
  8645. (let ((tmp (funcall org-link-translation-function type path)))
  8646. (setq type (car tmp) path (cdr tmp))))
  8647. (cond
  8648. ((assoc type org-link-protocols)
  8649. (funcall (nth 1 (assoc type org-link-protocols)) path))
  8650. ((equal type "mailto")
  8651. (let ((cmd (car org-link-mailto-program))
  8652. (args (cdr org-link-mailto-program)) args1
  8653. (address path) (subject "") a)
  8654. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  8655. (setq address (match-string 1 path)
  8656. subject (org-link-escape (match-string 2 path))))
  8657. (while args
  8658. (cond
  8659. ((not (stringp (car args))) (push (pop args) args1))
  8660. (t (setq a (pop args))
  8661. (if (string-match "%a" a)
  8662. (setq a (replace-match address t t a)))
  8663. (if (string-match "%s" a)
  8664. (setq a (replace-match subject t t a)))
  8665. (push a args1))))
  8666. (apply cmd (nreverse args1))))
  8667. ((member type '("http" "https" "ftp" "news"))
  8668. (browse-url (concat type ":" (if (org-string-match-p "[[:nonascii:] ]" path)
  8669. (org-link-escape
  8670. path org-link-escape-chars-browser)
  8671. path))))
  8672. ((string= type "doi")
  8673. (browse-url (concat "http://dx.doi.org/" (if (org-string-match-p "[[:nonascii:] ]" path)
  8674. (org-link-escape
  8675. path org-link-escape-chars-browser)
  8676. path))))
  8677. ((member type '("message"))
  8678. (browse-url (concat type ":" path)))
  8679. ((string= type "tags")
  8680. (org-tags-view arg path))
  8681. ((string= type "tree-match")
  8682. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  8683. ((string= type "file")
  8684. (if (string-match "::\\([0-9]+\\)\\'" path)
  8685. (setq line (string-to-number (match-string 1 path))
  8686. path (substring path 0 (match-beginning 0)))
  8687. (if (string-match "::\\(.+\\)\\'" path)
  8688. (setq search (match-string 1 path)
  8689. path (substring path 0 (match-beginning 0)))))
  8690. (if (string-match "[*?{]" (file-name-nondirectory path))
  8691. (dired path)
  8692. (org-open-file path arg line search)))
  8693. ((string= type "shell")
  8694. (let ((cmd path))
  8695. (if (or (and (not (string= org-confirm-shell-link-not-regexp ""))
  8696. (string-match org-confirm-shell-link-not-regexp cmd))
  8697. (not org-confirm-shell-link-function)
  8698. (funcall org-confirm-shell-link-function
  8699. (format "Execute \"%s\" in shell? "
  8700. (org-add-props cmd nil
  8701. 'face 'org-warning))))
  8702. (progn
  8703. (message "Executing %s" cmd)
  8704. (shell-command cmd))
  8705. (error "Abort"))))
  8706. ((string= type "elisp")
  8707. (let ((cmd path))
  8708. (if (or (and (not (string= org-confirm-elisp-link-not-regexp ""))
  8709. (string-match org-confirm-elisp-link-not-regexp cmd))
  8710. (not org-confirm-elisp-link-function)
  8711. (funcall org-confirm-elisp-link-function
  8712. (format "Execute \"%s\" as elisp? "
  8713. (org-add-props cmd nil
  8714. 'face 'org-warning))))
  8715. (message "%s => %s" cmd
  8716. (if (equal (string-to-char cmd) ?\()
  8717. (eval (read cmd))
  8718. (call-interactively (read cmd))))
  8719. (error "Abort"))))
  8720. ((and (string= type "thisfile")
  8721. (run-hook-with-args-until-success
  8722. 'org-open-link-functions path)))
  8723. ((string= type "thisfile")
  8724. (if arg
  8725. (switch-to-buffer-other-window
  8726. (org-get-buffer-for-internal-link (current-buffer)))
  8727. (org-mark-ring-push))
  8728. (let ((cmd `(org-link-search
  8729. ,path
  8730. ,(cond ((equal arg '(4)) ''occur)
  8731. ((equal arg '(16)) ''org-occur)
  8732. (t nil))
  8733. ,pos)))
  8734. (condition-case nil (let ((org-link-search-inhibit-query t))
  8735. (eval cmd))
  8736. (error (progn (widen) (eval cmd))))))
  8737. (t
  8738. (browse-url-at-point)))))))
  8739. (move-marker org-open-link-marker nil)
  8740. (run-hook-with-args 'org-follow-link-hook)))
  8741. (defun org-offer-links-in-entry (&optional nth zero)
  8742. "Offer links in the current entry and follow the selected link.
  8743. If there is only one link, follow it immediately as well.
  8744. If NTH is an integer, immediately pick the NTH link found.
  8745. If ZERO is a string, check also this string for a link, and if
  8746. there is one, offer it as link number zero."
  8747. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  8748. "\\(" org-angle-link-re "\\)\\|"
  8749. "\\(" org-plain-link-re "\\)"))
  8750. (cnt ?0)
  8751. (in-emacs (if (integerp nth) nil nth))
  8752. have-zero end links link c)
  8753. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  8754. (push (match-string 0 zero) links)
  8755. (setq cnt (1- cnt) have-zero t))
  8756. (save-excursion
  8757. (org-back-to-heading t)
  8758. (setq end (save-excursion (outline-next-heading) (point)))
  8759. (while (re-search-forward re end t)
  8760. (push (match-string 0) links))
  8761. (setq links (org-uniquify (reverse links))))
  8762. (cond
  8763. ((null links)
  8764. (message "No links"))
  8765. ((equal (length links) 1)
  8766. (setq link (list (car links))))
  8767. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  8768. (setq link (list (nth (if have-zero nth (1- nth)) links))))
  8769. (t ; we have to select a link
  8770. (save-excursion
  8771. (save-window-excursion
  8772. (delete-other-windows)
  8773. (with-output-to-temp-buffer "*Select Link*"
  8774. (mapc (lambda (l)
  8775. (if (not (string-match org-bracket-link-regexp l))
  8776. (princ (format "[%c] %s\n" (incf cnt)
  8777. (org-remove-angle-brackets l)))
  8778. (if (match-end 3)
  8779. (princ (format "[%c] %s (%s)\n" (incf cnt)
  8780. (match-string 3 l) (match-string 1 l)))
  8781. (princ (format "[%c] %s\n" (incf cnt)
  8782. (match-string 1 l))))))
  8783. links))
  8784. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  8785. (message "Select link to open, RET to open all:")
  8786. (setq c (read-char-exclusive))
  8787. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  8788. (when (equal c ?q) (error "Abort"))
  8789. (if (equal c ?\C-m)
  8790. (setq link links)
  8791. (setq nth (- c ?0))
  8792. (if have-zero (setq nth (1+ nth)))
  8793. (unless (and (integerp nth) (>= (length links) nth))
  8794. (error "Invalid link selection"))
  8795. (setq link (list (nth (1- nth) links))))))
  8796. (if link
  8797. (let ((buf (current-buffer)))
  8798. (dolist (l link)
  8799. (org-open-link-from-string l in-emacs buf))
  8800. t)
  8801. nil)))
  8802. ;; Add special file links that specify the way of opening
  8803. (org-add-link-type "file+sys" 'org-open-file-with-system)
  8804. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  8805. (defun org-open-file-with-system (path)
  8806. "Open file at PATH using the system way of opening it."
  8807. (org-open-file path 'system))
  8808. (defun org-open-file-with-emacs (path)
  8809. "Open file at PATH in Emacs."
  8810. (org-open-file path 'emacs))
  8811. (defun org-remove-file-link-modifiers ()
  8812. "Remove the file link modifiers in `file+sys:' and `file+emacs:' links."
  8813. (goto-char (point-min))
  8814. (while (re-search-forward "\\<file\\+\\(sys\\|emacs\\):" nil t)
  8815. (org-if-unprotected
  8816. (replace-match "file:" t t))))
  8817. (eval-after-load "org-exp"
  8818. '(add-hook 'org-export-preprocess-before-normalizing-links-hook
  8819. 'org-remove-file-link-modifiers))
  8820. ;;;; Time estimates
  8821. (defun org-get-effort (&optional pom)
  8822. "Get the effort estimate for the current entry."
  8823. (org-entry-get pom org-effort-property))
  8824. ;;; File search
  8825. (defvar org-create-file-search-functions nil
  8826. "List of functions to construct the right search string for a file link.
  8827. These functions are called in turn with point at the location to
  8828. which the link should point.
  8829. A function in the hook should first test if it would like to
  8830. handle this file type, for example by checking the `major-mode'
  8831. or the file extension. If it decides not to handle this file, it
  8832. should just return nil to give other functions a chance. If it
  8833. does handle the file, it must return the search string to be used
  8834. when following the link. The search string will be part of the
  8835. file link, given after a double colon, and `org-open-at-point'
  8836. will automatically search for it. If special measures must be
  8837. taken to make the search successful, another function should be
  8838. added to the companion hook `org-execute-file-search-functions',
  8839. which see.
  8840. A function in this hook may also use `setq' to set the variable
  8841. `description' to provide a suggestion for the descriptive text to
  8842. be used for this link when it gets inserted into an Org-mode
  8843. buffer with \\[org-insert-link].")
  8844. (defvar org-execute-file-search-functions nil
  8845. "List of functions to execute a file search triggered by a link.
  8846. Functions added to this hook must accept a single argument, the
  8847. search string that was part of the file link, the part after the
  8848. double colon. The function must first check if it would like to
  8849. handle this search, for example by checking the `major-mode' or
  8850. the file extension. If it decides not to handle this search, it
  8851. should just return nil to give other functions a chance. If it
  8852. does handle the search, it must return a non-nil value to keep
  8853. other functions from trying.
  8854. Each function can access the current prefix argument through the
  8855. variable `current-prefix-argument'. Note that a single prefix is
  8856. used to force opening a link in Emacs, so it may be good to only
  8857. use a numeric or double prefix to guide the search function.
  8858. In case this is needed, a function in this hook can also restore
  8859. the window configuration before `org-open-at-point' was called using:
  8860. (set-window-configuration org-window-config-before-follow-link)")
  8861. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  8862. (defun org-link-search (s &optional type avoid-pos stealth)
  8863. "Search for a link search option.
  8864. If S is surrounded by forward slashes, it is interpreted as a
  8865. regular expression. In org-mode files, this will create an `org-occur'
  8866. sparse tree. In ordinary files, `occur' will be used to list matches.
  8867. If the current buffer is in `dired-mode', grep will be used to search
  8868. in all files. If AVOID-POS is given, ignore matches near that position.
  8869. When optional argument STEALTH is non-nil, do not modify
  8870. visibility around point, thus ignoring
  8871. `org-show-hierarchy-above', `org-show-following-heading' and
  8872. `org-show-siblings' variables."
  8873. (let ((case-fold-search t)
  8874. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  8875. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  8876. (append '(("") (" ") ("\t") ("\n"))
  8877. org-emphasis-alist)
  8878. "\\|") "\\)"))
  8879. (pos (point))
  8880. (pre nil) (post nil)
  8881. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  8882. (cond
  8883. ;; First check if there are any special search functions
  8884. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  8885. ;; Now try the builtin stuff
  8886. ((and (equal (string-to-char s0) ?#)
  8887. (> (length s0) 1)
  8888. (save-excursion
  8889. (goto-char (point-min))
  8890. (and
  8891. (re-search-forward
  8892. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  8893. (setq type 'dedicated
  8894. pos (match-beginning 0))))
  8895. ;; There is an exact target for this
  8896. (goto-char pos)
  8897. (org-back-to-heading t)))
  8898. ((save-excursion
  8899. (goto-char (point-min))
  8900. (and
  8901. (re-search-forward
  8902. (concat "<<" (regexp-quote s0) ">>") nil t)
  8903. (setq type 'dedicated
  8904. pos (match-beginning 0))))
  8905. ;; There is an exact target for this
  8906. (goto-char pos))
  8907. ((and (string-match "^(\\(.*\\))$" s0)
  8908. (save-excursion
  8909. (goto-char (point-min))
  8910. (and
  8911. (re-search-forward
  8912. (concat "[^[]" (regexp-quote
  8913. (format org-coderef-label-format
  8914. (match-string 1 s0))))
  8915. nil t)
  8916. (setq type 'dedicated
  8917. pos (1+ (match-beginning 0))))))
  8918. ;; There is a coderef target for this
  8919. (goto-char pos))
  8920. ((string-match "^/\\(.*\\)/$" s)
  8921. ;; A regular expression
  8922. (cond
  8923. ((eq major-mode 'org-mode)
  8924. (org-occur (match-string 1 s)))
  8925. ;;((eq major-mode 'dired-mode)
  8926. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  8927. (t (org-do-occur (match-string 1 s)))))
  8928. ((and (eq major-mode 'org-mode) org-link-search-must-match-exact-headline)
  8929. (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  8930. (goto-char (point-min))
  8931. (cond
  8932. ((let (case-fold-search)
  8933. (re-search-forward (format org-complex-heading-regexp-format
  8934. (regexp-quote s))
  8935. nil t))
  8936. ;; OK, found a match
  8937. (setq type 'dedicated)
  8938. (goto-char (match-beginning 0)))
  8939. ((and (not org-link-search-inhibit-query)
  8940. (eq org-link-search-must-match-exact-headline 'query-to-create)
  8941. (y-or-n-p "No match - create this as a new heading? "))
  8942. (goto-char (point-max))
  8943. (or (bolp) (newline))
  8944. (insert "* " s "\n")
  8945. (beginning-of-line 0))
  8946. (t
  8947. (goto-char pos)
  8948. (error "No match"))))
  8949. (t
  8950. ;; A normal search string
  8951. (when (equal (string-to-char s) ?*)
  8952. ;; Anchor on headlines, post may include tags.
  8953. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  8954. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
  8955. s (substring s 1)))
  8956. (remove-text-properties
  8957. 0 (length s)
  8958. '(face nil mouse-face nil keymap nil fontified nil) s)
  8959. ;; Make a series of regular expressions to find a match
  8960. (setq words (org-split-string s "[ \n\r\t]+")
  8961. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  8962. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  8963. "\\)" markers)
  8964. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  8965. re2a (concat "[ \t\r\n]" re2a_)
  8966. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  8967. re4 (concat "[^a-zA-Z_]" re4_)
  8968. re1 (concat pre re2 post)
  8969. re3 (concat pre (if pre re4_ re4) post)
  8970. re5 (concat pre ".*" re4)
  8971. re2 (concat pre re2)
  8972. re2a (concat pre (if pre re2a_ re2a))
  8973. re4 (concat pre (if pre re4_ re4))
  8974. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  8975. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  8976. re5 "\\)"
  8977. ))
  8978. (cond
  8979. ((eq type 'org-occur) (org-occur reall))
  8980. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  8981. (t (goto-char (point-min))
  8982. (setq type 'fuzzy)
  8983. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  8984. (org-search-not-self 1 re1 nil t)
  8985. (org-search-not-self 1 re2 nil t)
  8986. (org-search-not-self 1 re2a nil t)
  8987. (org-search-not-self 1 re3 nil t)
  8988. (org-search-not-self 1 re4 nil t)
  8989. (org-search-not-self 1 re5 nil t)
  8990. )
  8991. (goto-char (match-beginning 1))
  8992. (goto-char pos)
  8993. (error "No match"))))))
  8994. (and (eq major-mode 'org-mode)
  8995. (not stealth)
  8996. (org-show-context 'link-search))
  8997. type))
  8998. (defun org-search-not-self (group &rest args)
  8999. "Execute `re-search-forward', but only accept matches that do not
  9000. enclose the position of `org-open-link-marker'."
  9001. (let ((m org-open-link-marker))
  9002. (catch 'exit
  9003. (while (apply 're-search-forward args)
  9004. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  9005. (goto-char (match-end group))
  9006. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  9007. (> (match-beginning 0) (marker-position m))
  9008. (< (match-end 0) (marker-position m)))
  9009. (save-match-data
  9010. (or (not (org-in-regexp
  9011. org-bracket-link-analytic-regexp 1))
  9012. (not (match-end 4)) ; no description
  9013. (and (<= (match-beginning 4) (point))
  9014. (>= (match-end 4) (point))))))
  9015. (throw 'exit (point))))))))
  9016. (defun org-get-buffer-for-internal-link (buffer)
  9017. "Return a buffer to be used for displaying the link target of internal links."
  9018. (cond
  9019. ((not org-display-internal-link-with-indirect-buffer)
  9020. buffer)
  9021. ((string-match "(Clone)$" (buffer-name buffer))
  9022. (message "Buffer is already a clone, not making another one")
  9023. ;; we also do not modify visibility in this case
  9024. buffer)
  9025. (t ; make a new indirect buffer for displaying the link
  9026. (let* ((bn (buffer-name buffer))
  9027. (ibn (concat bn "(Clone)"))
  9028. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  9029. (with-current-buffer ib (org-overview))
  9030. ib))))
  9031. (defun org-do-occur (regexp &optional cleanup)
  9032. "Call the Emacs command `occur'.
  9033. If CLEANUP is non-nil, remove the printout of the regular expression
  9034. in the *Occur* buffer. This is useful if the regex is long and not useful
  9035. to read."
  9036. (occur regexp)
  9037. (when cleanup
  9038. (let ((cwin (selected-window)) win beg end)
  9039. (when (setq win (get-buffer-window "*Occur*"))
  9040. (select-window win))
  9041. (goto-char (point-min))
  9042. (when (re-search-forward "match[a-z]+" nil t)
  9043. (setq beg (match-end 0))
  9044. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  9045. (setq end (1- (match-beginning 0)))))
  9046. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  9047. (goto-char (point-min))
  9048. (select-window cwin))))
  9049. ;;; The mark ring for links jumps
  9050. (defvar org-mark-ring nil
  9051. "Mark ring for positions before jumps in Org-mode.")
  9052. (defvar org-mark-ring-last-goto nil
  9053. "Last position in the mark ring used to go back.")
  9054. ;; Fill and close the ring
  9055. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  9056. (loop for i from 1 to org-mark-ring-length do
  9057. (push (make-marker) org-mark-ring))
  9058. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  9059. org-mark-ring)
  9060. (defun org-mark-ring-push (&optional pos buffer)
  9061. "Put the current position or POS into the mark ring and rotate it."
  9062. (interactive)
  9063. (setq pos (or pos (point)))
  9064. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  9065. (move-marker (car org-mark-ring)
  9066. (or pos (point))
  9067. (or buffer (current-buffer)))
  9068. (message "%s"
  9069. (substitute-command-keys
  9070. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  9071. (defun org-mark-ring-goto (&optional n)
  9072. "Jump to the previous position in the mark ring.
  9073. With prefix arg N, jump back that many stored positions. When
  9074. called several times in succession, walk through the entire ring.
  9075. Org-mode commands jumping to a different position in the current file,
  9076. or to another Org-mode file, automatically push the old position
  9077. onto the ring."
  9078. (interactive "p")
  9079. (let (p m)
  9080. (if (eq last-command this-command)
  9081. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  9082. (setq p org-mark-ring))
  9083. (setq org-mark-ring-last-goto p)
  9084. (setq m (car p))
  9085. (org-pop-to-buffer-same-window (marker-buffer m))
  9086. (goto-char m)
  9087. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  9088. (defun org-remove-angle-brackets (s)
  9089. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  9090. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  9091. s)
  9092. (defun org-add-angle-brackets (s)
  9093. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  9094. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  9095. s)
  9096. (defun org-remove-double-quotes (s)
  9097. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  9098. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  9099. s)
  9100. ;;; Following specific links
  9101. (defun org-follow-timestamp-link ()
  9102. (cond
  9103. ((org-at-date-range-p t)
  9104. (let ((org-agenda-start-on-weekday)
  9105. (t1 (match-string 1))
  9106. (t2 (match-string 2)))
  9107. (setq t1 (time-to-days (org-time-string-to-time t1))
  9108. t2 (time-to-days (org-time-string-to-time t2)))
  9109. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  9110. ((org-at-timestamp-p t)
  9111. (org-agenda-list nil (time-to-days (org-time-string-to-time
  9112. (substring (match-string 1) 0 10)))
  9113. 1))
  9114. (t (error "This should not happen"))))
  9115. ;;; Following file links
  9116. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  9117. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  9118. (declare-function mailcap-mime-info
  9119. "mailcap" (string &optional request no-decode))
  9120. (defvar org-wait nil)
  9121. (defun org-open-file (path &optional in-emacs line search)
  9122. "Open the file at PATH.
  9123. First, this expands any special file name abbreviations. Then the
  9124. configuration variable `org-file-apps' is checked if it contains an
  9125. entry for this file type, and if yes, the corresponding command is launched.
  9126. If no application is found, Emacs simply visits the file.
  9127. With optional prefix argument IN-EMACS, Emacs will visit the file.
  9128. With a double \\[universal-argument] \\[universal-argument] \
  9129. prefix arg, Org tries to avoid opening in Emacs
  9130. and to use an external application to visit the file.
  9131. Optional LINE specifies a line to go to, optional SEARCH a string
  9132. to search for. If LINE or SEARCH is given, the file will be
  9133. opened in Emacs, unless an entry from org-file-apps that makes
  9134. use of groups in a regexp matches.
  9135. If you want to change the way frames are used when following a
  9136. link, please customize `org-link-frame-setup'.
  9137. If the file does not exist, an error is thrown."
  9138. (let* ((file (if (equal path "")
  9139. buffer-file-name
  9140. (substitute-in-file-name (expand-file-name path))))
  9141. (file-apps (append org-file-apps (org-default-apps)))
  9142. (apps (org-remove-if
  9143. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9144. (apps-dlink (org-remove-if-not
  9145. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9146. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  9147. (dirp (if remp nil (file-directory-p file)))
  9148. (file (if (and dirp org-open-directory-means-index-dot-org)
  9149. (concat (file-name-as-directory file) "index.org")
  9150. file))
  9151. (a-m-a-p (assq 'auto-mode apps))
  9152. (dfile (downcase file))
  9153. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  9154. (link (cond ((and (eq line nil)
  9155. (eq search nil))
  9156. file)
  9157. (line
  9158. (concat file "::" (number-to-string line)))
  9159. (search
  9160. (concat file "::" search))))
  9161. (dlink (downcase link))
  9162. (old-buffer (current-buffer))
  9163. (old-pos (point))
  9164. (old-mode major-mode)
  9165. ext cmd link-match-data)
  9166. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  9167. (setq ext (match-string 1 dfile))
  9168. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  9169. (setq ext (match-string 1 dfile))))
  9170. (cond
  9171. ((member in-emacs '((16) system))
  9172. (setq cmd (cdr (assoc 'system apps))))
  9173. (in-emacs (setq cmd 'emacs))
  9174. (t
  9175. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  9176. (and dirp (cdr (assoc 'directory apps)))
  9177. ; first, try matching against apps-dlink
  9178. ; if we get a match here, store the match data for later
  9179. (let ((match (assoc-default dlink apps-dlink
  9180. 'string-match)))
  9181. (if match
  9182. (progn (setq link-match-data (match-data))
  9183. match)
  9184. (progn (setq in-emacs (or in-emacs line search))
  9185. nil))) ; if we have no match in apps-dlink,
  9186. ; always open the file in emacs if line or search
  9187. ; is given (for backwards compatibility)
  9188. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  9189. 'string-match)
  9190. (cdr (assoc ext apps))
  9191. (cdr (assoc t apps))))))
  9192. (when (eq cmd 'system)
  9193. (setq cmd (cdr (assoc 'system apps))))
  9194. (when (eq cmd 'default)
  9195. (setq cmd (cdr (assoc t apps))))
  9196. (when (eq cmd 'mailcap)
  9197. (require 'mailcap)
  9198. (mailcap-parse-mailcaps)
  9199. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  9200. (command (mailcap-mime-info mime-type)))
  9201. (if (stringp command)
  9202. (setq cmd command)
  9203. (setq cmd 'emacs))))
  9204. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  9205. (not (file-exists-p file))
  9206. (not org-open-non-existing-files))
  9207. (error "No such file: %s" file))
  9208. (cond
  9209. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  9210. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  9211. (while (string-match "['\"]%s['\"]" cmd)
  9212. (setq cmd (replace-match "%s" t t cmd)))
  9213. (while (string-match "%s" cmd)
  9214. (setq cmd (replace-match
  9215. (save-match-data
  9216. (shell-quote-argument
  9217. (convert-standard-filename file)))
  9218. t t cmd)))
  9219. ;; Replace "%1", "%2" etc. in command with group matches from regex
  9220. (save-match-data
  9221. (let ((match-index 1)
  9222. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  9223. (set-match-data link-match-data)
  9224. (while (<= match-index number-of-groups)
  9225. (let ((regex (concat "%" (number-to-string match-index)))
  9226. (replace-with (match-string match-index dlink)))
  9227. (while (string-match regex cmd)
  9228. (setq cmd (replace-match replace-with t t cmd))))
  9229. (setq match-index (+ match-index 1)))))
  9230. (save-window-excursion
  9231. (start-process-shell-command cmd nil cmd)
  9232. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  9233. ))
  9234. ((or (stringp cmd)
  9235. (eq cmd 'emacs))
  9236. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  9237. (widen)
  9238. (if line (org-goto-line line)
  9239. (if search (org-link-search search))))
  9240. ((consp cmd)
  9241. (let ((file (convert-standard-filename file)))
  9242. (save-match-data
  9243. (set-match-data link-match-data)
  9244. (eval cmd))))
  9245. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  9246. (and (eq major-mode 'org-mode) (eq old-mode 'org-mode)
  9247. (or (not (equal old-buffer (current-buffer)))
  9248. (not (equal old-pos (point))))
  9249. (org-mark-ring-push old-pos old-buffer))))
  9250. (defun org-file-apps-entry-match-against-dlink-p (entry)
  9251. "This function returns non-nil if `entry' uses a regular
  9252. expression which should be matched against the whole link by
  9253. org-open-file.
  9254. It assumes that is the case when the entry uses a regular
  9255. expression which has at least one grouping construct and the
  9256. action is either a lisp form or a command string containing
  9257. '%1', i.e. using at least one subexpression match as a
  9258. parameter."
  9259. (let ((selector (car entry))
  9260. (action (cdr entry)))
  9261. (if (stringp selector)
  9262. (and (> (regexp-opt-depth selector) 0)
  9263. (or (and (stringp action)
  9264. (string-match "%[0-9]" action))
  9265. (consp action)))
  9266. nil)))
  9267. (defun org-default-apps ()
  9268. "Return the default applications for this operating system."
  9269. (cond
  9270. ((eq system-type 'darwin)
  9271. org-file-apps-defaults-macosx)
  9272. ((eq system-type 'windows-nt)
  9273. org-file-apps-defaults-windowsnt)
  9274. (t org-file-apps-defaults-gnu)))
  9275. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  9276. "Convert extensions to regular expressions in the cars of LIST.
  9277. Also, weed out any non-string entries, because the return value is used
  9278. only for regexp matching.
  9279. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  9280. point to the symbol `emacs', indicating that the file should
  9281. be opened in Emacs."
  9282. (append
  9283. (delq nil
  9284. (mapcar (lambda (x)
  9285. (if (not (stringp (car x)))
  9286. nil
  9287. (if (string-match "\\W" (car x))
  9288. x
  9289. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  9290. list))
  9291. (if add-auto-mode
  9292. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  9293. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  9294. (defun org-file-remote-p (file)
  9295. "Test whether FILE specifies a location on a remote system.
  9296. Return non-nil if the location is indeed remote.
  9297. For example, the filename \"/user@host:/foo\" specifies a location
  9298. on the system \"/user@host:\"."
  9299. (cond ((fboundp 'file-remote-p)
  9300. (file-remote-p file))
  9301. ((fboundp 'tramp-handle-file-remote-p)
  9302. (tramp-handle-file-remote-p file))
  9303. ((and (boundp 'ange-ftp-name-format)
  9304. (string-match (car ange-ftp-name-format) file))
  9305. t)
  9306. (t nil)))
  9307. ;;;; Refiling
  9308. (defun org-get-org-file ()
  9309. "Read a filename, with default directory `org-directory'."
  9310. (let ((default (or org-default-notes-file remember-data-file)))
  9311. (read-file-name (format "File name [%s]: " default)
  9312. (file-name-as-directory org-directory)
  9313. default)))
  9314. (defun org-notes-order-reversed-p ()
  9315. "Check if the current file should receive notes in reversed order."
  9316. (cond
  9317. ((not org-reverse-note-order) nil)
  9318. ((eq t org-reverse-note-order) t)
  9319. ((not (listp org-reverse-note-order)) nil)
  9320. (t (catch 'exit
  9321. (let ((all org-reverse-note-order)
  9322. entry)
  9323. (while (setq entry (pop all))
  9324. (if (string-match (car entry) buffer-file-name)
  9325. (throw 'exit (cdr entry))))
  9326. nil)))))
  9327. (defvar org-refile-target-table nil
  9328. "The list of refile targets, created by `org-refile'.")
  9329. (defvar org-agenda-new-buffers nil
  9330. "Buffers created to visit agenda files.")
  9331. (defvar org-refile-cache nil
  9332. "Cache for refile targets.")
  9333. (defvar org-refile-markers nil
  9334. "All the markers used for caching refile locations.")
  9335. (defun org-refile-marker (pos)
  9336. "Get a new refile marker, but only if caching is in use."
  9337. (if (not org-refile-use-cache)
  9338. pos
  9339. (let ((m (make-marker)))
  9340. (move-marker m pos)
  9341. (push m org-refile-markers)
  9342. m)))
  9343. (defun org-refile-cache-clear ()
  9344. "Clear the refile cache and disable all the markers."
  9345. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  9346. (setq org-refile-markers nil)
  9347. (setq org-refile-cache nil)
  9348. (message "Refile cache has been cleared"))
  9349. (defun org-refile-cache-check-set (set)
  9350. "Check if all the markers in the cache still have live buffers."
  9351. (let (marker)
  9352. (catch 'exit
  9353. (while (and set (setq marker (nth 3 (pop set))))
  9354. ;; if org-refile-use-outline-path is 'file, marker may be nil
  9355. (when (and marker (null (marker-buffer marker)))
  9356. (message "not found") (sit-for 3)
  9357. (throw 'exit nil)))
  9358. t)))
  9359. (defun org-refile-cache-put (set &rest identifiers)
  9360. "Push the refile targets SET into the cache, under IDENTIFIERS."
  9361. (let* ((key (sha1 (prin1-to-string identifiers)))
  9362. (entry (assoc key org-refile-cache)))
  9363. (if entry
  9364. (setcdr entry set)
  9365. (push (cons key set) org-refile-cache))))
  9366. (defun org-refile-cache-get (&rest identifiers)
  9367. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  9368. (cond
  9369. ((not org-refile-cache) nil)
  9370. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  9371. (t
  9372. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  9373. org-refile-cache))))
  9374. (and set (org-refile-cache-check-set set) set)))))
  9375. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  9376. "Produce a table with refile targets."
  9377. (let ((case-fold-search nil)
  9378. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  9379. (entries (or org-refile-targets '((nil . (:level . 1)))))
  9380. targets tgs txt re files f desc descre fast-path-p level pos0)
  9381. (message "Getting targets...")
  9382. (with-current-buffer (or default-buffer (current-buffer))
  9383. (while (setq entry (pop entries))
  9384. (setq files (car entry) desc (cdr entry))
  9385. (setq fast-path-p nil)
  9386. (cond
  9387. ((null files) (setq files (list (current-buffer))))
  9388. ((eq files 'org-agenda-files)
  9389. (setq files (org-agenda-files 'unrestricted)))
  9390. ((and (symbolp files) (fboundp files))
  9391. (setq files (funcall files)))
  9392. ((and (symbolp files) (boundp files))
  9393. (setq files (symbol-value files))))
  9394. (if (stringp files) (setq files (list files)))
  9395. (cond
  9396. ((eq (car desc) :tag)
  9397. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  9398. ((eq (car desc) :todo)
  9399. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  9400. ((eq (car desc) :regexp)
  9401. (setq descre (cdr desc)))
  9402. ((eq (car desc) :level)
  9403. (setq descre (concat "^\\*\\{" (number-to-string
  9404. (if org-odd-levels-only
  9405. (1- (* 2 (cdr desc)))
  9406. (cdr desc)))
  9407. "\\}[ \t]")))
  9408. ((eq (car desc) :maxlevel)
  9409. (setq fast-path-p t)
  9410. (setq descre (concat "^\\*\\{1," (number-to-string
  9411. (if org-odd-levels-only
  9412. (1- (* 2 (cdr desc)))
  9413. (cdr desc)))
  9414. "\\}[ \t]")))
  9415. (t (error "Bad refiling target description %s" desc)))
  9416. (while (setq f (pop files))
  9417. (with-current-buffer
  9418. (if (bufferp f) f (org-get-agenda-file-buffer f))
  9419. (or
  9420. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  9421. (progn
  9422. (if (bufferp f) (setq f (buffer-file-name
  9423. (buffer-base-buffer f))))
  9424. (setq f (and f (expand-file-name f)))
  9425. (if (eq org-refile-use-outline-path 'file)
  9426. (push (list (file-name-nondirectory f) f nil nil) tgs))
  9427. (save-excursion
  9428. (save-restriction
  9429. (widen)
  9430. (goto-char (point-min))
  9431. (while (re-search-forward descre nil t)
  9432. (goto-char (setq pos0 (point-at-bol)))
  9433. (catch 'next
  9434. (when org-refile-target-verify-function
  9435. (save-match-data
  9436. (or (funcall org-refile-target-verify-function)
  9437. (throw 'next t))))
  9438. (when (and (looking-at org-complex-heading-regexp)
  9439. (not (member (match-string 4) excluded-entries))
  9440. (match-string 4))
  9441. (setq level (org-reduced-level
  9442. (- (match-end 1) (match-beginning 1)))
  9443. txt (org-link-display-format (match-string 4))
  9444. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  9445. re (format org-complex-heading-regexp-format
  9446. (regexp-quote (match-string 4))))
  9447. (when org-refile-use-outline-path
  9448. (setq txt (mapconcat
  9449. 'org-protect-slash
  9450. (append
  9451. (if (eq org-refile-use-outline-path
  9452. 'file)
  9453. (list (file-name-nondirectory
  9454. (buffer-file-name
  9455. (buffer-base-buffer))))
  9456. (if (eq org-refile-use-outline-path
  9457. 'full-file-path)
  9458. (list (buffer-file-name
  9459. (buffer-base-buffer)))))
  9460. (org-get-outline-path fast-path-p
  9461. level txt)
  9462. (list txt))
  9463. "/")))
  9464. (push (list txt f re (org-refile-marker (point)))
  9465. tgs)))
  9466. (when (= (point) pos0)
  9467. ;; verification function has not moved point
  9468. (goto-char (point-at-eol))))))))
  9469. (when org-refile-use-cache
  9470. (org-refile-cache-put tgs (buffer-file-name) descre))
  9471. (setq targets (append tgs targets))
  9472. ))))
  9473. (message "Getting targets...done")
  9474. (nreverse targets)))
  9475. (defun org-protect-slash (s)
  9476. (while (string-match "/" s)
  9477. (setq s (replace-match "\\" t t s)))
  9478. s)
  9479. (defvar org-olpa (make-vector 20 nil))
  9480. (defun org-get-outline-path (&optional fastp level heading)
  9481. "Return the outline path to the current entry, as a list.
  9482. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  9483. routine which makes outline path derivations for an entire file,
  9484. avoiding backtracing. Refile target collection makes use of that."
  9485. (if fastp
  9486. (progn
  9487. (if (> level 19)
  9488. (error "Outline path failure, more than 19 levels"))
  9489. (loop for i from level upto 19 do
  9490. (aset org-olpa i nil))
  9491. (prog1
  9492. (delq nil (append org-olpa nil))
  9493. (aset org-olpa level heading)))
  9494. (let (rtn case-fold-search)
  9495. (save-excursion
  9496. (save-restriction
  9497. (widen)
  9498. (while (org-up-heading-safe)
  9499. (when (looking-at org-complex-heading-regexp)
  9500. (push (org-match-string-no-properties 4) rtn)))
  9501. rtn)))))
  9502. (defun org-format-outline-path (path &optional width prefix)
  9503. "Format the outline path PATH for display.
  9504. Width is the maximum number of characters that is available.
  9505. Prefix is a prefix to be included in the returned string,
  9506. such as the file name."
  9507. (setq width (or width 79))
  9508. (if prefix (setq width (- width (length prefix))))
  9509. (if (not path)
  9510. (or prefix "")
  9511. (let* ((nsteps (length path))
  9512. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  9513. (maxwidth (if (<= total-width width)
  9514. 10000 ;; everything fits
  9515. ;; we need to shorten the level headings
  9516. (/ (- width nsteps) nsteps)))
  9517. (org-odd-levels-only nil)
  9518. (n 0)
  9519. (total (1+ (length prefix))))
  9520. (setq maxwidth (max maxwidth 10))
  9521. (concat prefix
  9522. (mapconcat
  9523. (lambda (h)
  9524. (setq n (1+ n))
  9525. (if (and (= n nsteps) (< maxwidth 10000))
  9526. (setq maxwidth (- total-width total)))
  9527. (if (< (length h) maxwidth)
  9528. (progn (setq total (+ total (length h) 1)) h)
  9529. (setq h (substring h 0 (- maxwidth 2))
  9530. total (+ total maxwidth 1))
  9531. (if (string-match "[ \t]+\\'" h)
  9532. (setq h (substring h 0 (match-beginning 0))))
  9533. (setq h (concat h "..")))
  9534. (org-add-props h nil 'face
  9535. (nth (% (1- n) org-n-level-faces)
  9536. org-level-faces))
  9537. h)
  9538. path "/")))))
  9539. (defun org-display-outline-path (&optional file current)
  9540. "Display the current outline path in the echo area."
  9541. (interactive "P")
  9542. (let* ((bfn (buffer-file-name (buffer-base-buffer)))
  9543. (case-fold-search nil)
  9544. (path (and (eq major-mode 'org-mode) (org-get-outline-path))))
  9545. (if current (setq path (append path
  9546. (save-excursion
  9547. (org-back-to-heading t)
  9548. (if (looking-at org-complex-heading-regexp)
  9549. (list (match-string 4)))))))
  9550. (message "%s"
  9551. (org-format-outline-path
  9552. path
  9553. (1- (frame-width))
  9554. (and file bfn (concat (file-name-nondirectory bfn) "/"))))))
  9555. (defvar org-refile-history nil
  9556. "History for refiling operations.")
  9557. (defvar org-after-refile-insert-hook nil
  9558. "Hook run after `org-refile' has inserted its stuff at the new location.
  9559. Note that this is still *before* the stuff will be removed from
  9560. the *old* location.")
  9561. (defvar org-capture-last-stored-marker)
  9562. (defun org-refile (&optional goto default-buffer rfloc)
  9563. "Move the entry or entries at point to another heading.
  9564. The list of target headings is compiled using the information in
  9565. `org-refile-targets', which see.
  9566. At the target location, the entry is filed as a subitem of the target
  9567. heading. Depending on `org-reverse-note-order', the new subitem will
  9568. either be the first or the last subitem.
  9569. If there is an active region, all entries in that region will be moved.
  9570. However, the region must fulfill the requirement that the first heading
  9571. is the first one sets the top-level of the moved text - at most siblings
  9572. below it are allowed.
  9573. With prefix arg GOTO, the command will only visit the target location
  9574. and not actually move anything.
  9575. With a double prefix arg \\[universal-argument] \\[universal-argument], \
  9576. go to the location where the last refiling operation has put the subtree.
  9577. With a prefix argument of `2', refile to the running clock.
  9578. RFLOC can be a refile location obtained in a different way.
  9579. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  9580. If you are using target caching (see `org-refile-use-cache'),
  9581. You have to clear the target cache in order to find new targets.
  9582. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  9583. prefix argument (`C-u C-u C-u C-c C-w')."
  9584. (interactive "P")
  9585. (if (member goto '(0 (64)))
  9586. (org-refile-cache-clear)
  9587. (let* ((cbuf (current-buffer))
  9588. (regionp (org-region-active-p))
  9589. (region-start (and regionp (region-beginning)))
  9590. (region-end (and regionp (region-end)))
  9591. (region-length (and regionp (- region-end region-start)))
  9592. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  9593. pos it nbuf file re level reversed)
  9594. (setq last-command nil)
  9595. (when regionp
  9596. (goto-char region-start)
  9597. (or (bolp) (goto-char (point-at-bol)))
  9598. (setq region-start (point))
  9599. (unless (or (org-kill-is-subtree-p
  9600. (buffer-substring region-start region-end))
  9601. (prog1 org-refile-active-region-within-subtree
  9602. (org-toggle-heading)))
  9603. (error "The region is not a (sequence of) subtree(s)")))
  9604. (if (equal goto '(16))
  9605. (org-refile-goto-last-stored)
  9606. (when (or
  9607. (and (equal goto 2)
  9608. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  9609. (prog1
  9610. (setq it (list (or org-clock-heading "running clock")
  9611. (buffer-file-name
  9612. (marker-buffer org-clock-hd-marker))
  9613. ""
  9614. (marker-position org-clock-hd-marker)))
  9615. (setq goto nil)))
  9616. (setq it (or rfloc
  9617. (let (heading-text)
  9618. (save-excursion
  9619. (unless goto
  9620. (org-back-to-heading t)
  9621. (setq heading-text
  9622. (nth 4 (org-heading-components))))
  9623. (org-refile-get-location
  9624. (cond (goto "Goto")
  9625. (regionp "Refile region to")
  9626. (t (concat "Refile subtree \""
  9627. heading-text "\" to")))
  9628. default-buffer
  9629. org-refile-allow-creating-parent-nodes
  9630. goto))))))
  9631. (setq file (nth 1 it)
  9632. re (nth 2 it)
  9633. pos (nth 3 it))
  9634. (if (and (not goto)
  9635. pos
  9636. (equal (buffer-file-name) file)
  9637. (if regionp
  9638. (and (>= pos region-start)
  9639. (<= pos region-end))
  9640. (and (>= pos (point))
  9641. (< pos (save-excursion
  9642. (org-end-of-subtree t t))))))
  9643. (error "Cannot refile to position inside the tree or region"))
  9644. (setq nbuf (or (find-buffer-visiting file)
  9645. (find-file-noselect file)))
  9646. (if goto
  9647. (progn
  9648. (org-pop-to-buffer-same-window nbuf)
  9649. (goto-char pos)
  9650. (org-show-context 'org-goto))
  9651. (if regionp
  9652. (progn
  9653. (org-kill-new (buffer-substring region-start region-end))
  9654. (org-save-markers-in-region region-start region-end))
  9655. (org-copy-subtree 1 nil t))
  9656. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  9657. (find-file-noselect file)))
  9658. (setq reversed (org-notes-order-reversed-p))
  9659. (save-excursion
  9660. (save-restriction
  9661. (widen)
  9662. (if pos
  9663. (progn
  9664. (goto-char pos)
  9665. (looking-at org-outline-regexp)
  9666. (setq level (org-get-valid-level (funcall outline-level) 1))
  9667. (goto-char
  9668. (if reversed
  9669. (or (outline-next-heading) (point-max))
  9670. (or (save-excursion (org-get-next-sibling))
  9671. (org-end-of-subtree t t)
  9672. (point-max)))))
  9673. (setq level 1)
  9674. (if (not reversed)
  9675. (goto-char (point-max))
  9676. (goto-char (point-min))
  9677. (or (outline-next-heading) (goto-char (point-max)))))
  9678. (if (not (bolp)) (newline))
  9679. (org-paste-subtree level)
  9680. (when org-log-refile
  9681. (org-add-log-setup 'refile nil nil 'findpos
  9682. org-log-refile)
  9683. (unless (eq org-log-refile 'note)
  9684. (save-excursion (org-add-log-note))))
  9685. (and org-auto-align-tags (org-set-tags nil t))
  9686. (bookmark-set "org-refile-last-stored")
  9687. ;; If we are refiling for capture, make sure that the
  9688. ;; last-capture pointers point here
  9689. (when (org-bound-and-true-p org-refile-for-capture)
  9690. (bookmark-set "org-capture-last-stored-marker")
  9691. (move-marker org-capture-last-stored-marker (point)))
  9692. (if (fboundp 'deactivate-mark) (deactivate-mark))
  9693. (run-hooks 'org-after-refile-insert-hook))))
  9694. (if regionp
  9695. (delete-region (point) (+ (point) region-length))
  9696. (org-cut-subtree))
  9697. (when (featurep 'org-inlinetask)
  9698. (org-inlinetask-remove-END-maybe))
  9699. (setq org-markers-to-move nil)
  9700. (message "Refiled to \"%s\" in file %s" (car it) file)))))))
  9701. (defun org-refile-goto-last-stored ()
  9702. "Go to the location where the last refile was stored."
  9703. (interactive)
  9704. (bookmark-jump "org-refile-last-stored")
  9705. (message "This is the location of the last refile"))
  9706. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  9707. no-exclude)
  9708. "Prompt the user for a refile location, using PROMPT.
  9709. PROMPT should not be suffixed with a colon and a space, because
  9710. this function appends the default value from
  9711. `org-refile-history' automatically, if that is not empty.
  9712. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  9713. this is used for the GOTO interface."
  9714. (let ((org-refile-targets org-refile-targets)
  9715. (org-refile-use-outline-path org-refile-use-outline-path)
  9716. excluded-entries)
  9717. (when (and (eq major-mode 'org-mode)
  9718. (not org-refile-use-cache)
  9719. (not no-exclude))
  9720. (org-map-tree
  9721. (lambda()
  9722. (setq excluded-entries
  9723. (append excluded-entries (list (org-get-heading t t)))))))
  9724. (setq org-refile-target-table
  9725. (org-refile-get-targets default-buffer excluded-entries)))
  9726. (unless org-refile-target-table
  9727. (error "No refile targets"))
  9728. (let* ((prompt (concat prompt
  9729. (and (car org-refile-history)
  9730. (concat " (default " (car org-refile-history) ")"))
  9731. ": "))
  9732. (cbuf (current-buffer))
  9733. (partial-completion-mode nil)
  9734. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  9735. (cfunc (if (and org-refile-use-outline-path
  9736. org-outline-path-complete-in-steps)
  9737. 'org-olpath-completing-read
  9738. 'org-icompleting-read))
  9739. (extra (if org-refile-use-outline-path "/" ""))
  9740. (filename (and cfn (expand-file-name cfn)))
  9741. (tbl (mapcar
  9742. (lambda (x)
  9743. (if (and (not (member org-refile-use-outline-path
  9744. '(file full-file-path)))
  9745. (not (equal filename (nth 1 x))))
  9746. (cons (concat (car x) extra " ("
  9747. (file-name-nondirectory (nth 1 x)) ")")
  9748. (cdr x))
  9749. (cons (concat (car x) extra) (cdr x))))
  9750. org-refile-target-table))
  9751. (completion-ignore-case t)
  9752. pa answ parent-target child parent old-hist)
  9753. (setq old-hist org-refile-history)
  9754. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  9755. nil 'org-refile-history (car org-refile-history)))
  9756. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  9757. (org-refile-check-position pa)
  9758. (if pa
  9759. (progn
  9760. (when (or (not org-refile-history)
  9761. (not (eq old-hist org-refile-history))
  9762. (not (equal (car pa) (car org-refile-history))))
  9763. (setq org-refile-history
  9764. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  9765. org-refile-history
  9766. (cdr org-refile-history))))
  9767. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  9768. (pop org-refile-history)))
  9769. pa)
  9770. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  9771. (progn
  9772. (setq parent (match-string 1 answ)
  9773. child (match-string 2 answ))
  9774. (setq parent-target (or (assoc parent tbl)
  9775. (assoc (concat parent "/") tbl)))
  9776. (when (and parent-target
  9777. (or (eq new-nodes t)
  9778. (and (eq new-nodes 'confirm)
  9779. (y-or-n-p (format "Create new node \"%s\"? "
  9780. child)))))
  9781. (org-refile-new-child parent-target child)))
  9782. (error "Invalid target location")))))
  9783. (defun org-refile-check-position (refile-pointer)
  9784. "Check if the refile pointer matches the readline to which it points."
  9785. (let* ((file (nth 1 refile-pointer))
  9786. (re (nth 2 refile-pointer))
  9787. (pos (nth 3 refile-pointer))
  9788. buffer)
  9789. (when (org-string-nw-p re)
  9790. (setq buffer (if (markerp pos)
  9791. (marker-buffer pos)
  9792. (or (find-buffer-visiting file)
  9793. (find-file-noselect file))))
  9794. (with-current-buffer buffer
  9795. (save-excursion
  9796. (save-restriction
  9797. (widen)
  9798. (goto-char pos)
  9799. (beginning-of-line 1)
  9800. (unless (org-looking-at-p re)
  9801. (error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling"))))))))
  9802. (defun org-refile-new-child (parent-target child)
  9803. "Use refile target PARENT-TARGET to add new CHILD below it."
  9804. (unless parent-target
  9805. (error "Cannot find parent for new node"))
  9806. (let ((file (nth 1 parent-target))
  9807. (pos (nth 3 parent-target))
  9808. level)
  9809. (with-current-buffer (or (find-buffer-visiting file)
  9810. (find-file-noselect file))
  9811. (save-excursion
  9812. (save-restriction
  9813. (widen)
  9814. (if pos
  9815. (goto-char pos)
  9816. (goto-char (point-max))
  9817. (if (not (bolp)) (newline)))
  9818. (when (looking-at org-outline-regexp)
  9819. (setq level (funcall outline-level))
  9820. (org-end-of-subtree t t))
  9821. (org-back-over-empty-lines)
  9822. (insert "\n" (make-string
  9823. (if pos (org-get-valid-level level 1) 1) ?*)
  9824. " " child "\n")
  9825. (beginning-of-line 0)
  9826. (list (concat (car parent-target) "/" child) file "" (point)))))))
  9827. (defun org-olpath-completing-read (prompt collection &rest args)
  9828. "Read an outline path like a file name."
  9829. (let ((thetable collection)
  9830. (org-completion-use-ido nil) ; does not work with ido.
  9831. (org-completion-use-iswitchb nil)) ; or iswitchb
  9832. (apply
  9833. 'org-icompleting-read prompt
  9834. (lambda (string predicate &optional flag)
  9835. (let (rtn r f (l (length string)))
  9836. (cond
  9837. ((eq flag nil)
  9838. ;; try completion
  9839. (try-completion string thetable))
  9840. ((eq flag t)
  9841. ;; all-completions
  9842. (setq rtn (all-completions string thetable predicate))
  9843. (mapcar
  9844. (lambda (x)
  9845. (setq r (substring x l))
  9846. (if (string-match " ([^)]*)$" x)
  9847. (setq f (match-string 0 x))
  9848. (setq f ""))
  9849. (if (string-match "/" r)
  9850. (concat string (substring r 0 (match-end 0)) f)
  9851. x))
  9852. rtn))
  9853. ((eq flag 'lambda)
  9854. ;; exact match?
  9855. (assoc string thetable)))
  9856. ))
  9857. args)))
  9858. ;;;; Dynamic blocks
  9859. (defun org-find-dblock (name)
  9860. "Find the first dynamic block with name NAME in the buffer.
  9861. If not found, stay at current position and return nil."
  9862. (let (pos)
  9863. (save-excursion
  9864. (goto-char (point-min))
  9865. (setq pos (and (re-search-forward (concat "^[ \t]*#\\+BEGIN:[ \t]+" name "\\>")
  9866. nil t)
  9867. (match-beginning 0))))
  9868. (if pos (goto-char pos))
  9869. pos))
  9870. (defconst org-dblock-start-re
  9871. "^[ \t]*#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  9872. "Matches the start line of a dynamic block, with parameters.")
  9873. (defconst org-dblock-end-re "^[ \t]*#\\+END\\([: \t\r\n]\\|$\\)"
  9874. "Matches the end of a dynamic block.")
  9875. (defun org-create-dblock (plist)
  9876. "Create a dynamic block section, with parameters taken from PLIST.
  9877. PLIST must contain a :name entry which is used as name of the block."
  9878. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  9879. (end-of-line 1)
  9880. (newline))
  9881. (let ((col (current-column))
  9882. (name (plist-get plist :name)))
  9883. (insert "#+BEGIN: " name)
  9884. (while plist
  9885. (if (eq (car plist) :name)
  9886. (setq plist (cddr plist))
  9887. (insert " " (prin1-to-string (pop plist)))))
  9888. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  9889. (beginning-of-line -2)))
  9890. (defun org-prepare-dblock ()
  9891. "Prepare dynamic block for refresh.
  9892. This empties the block, puts the cursor at the insert position and returns
  9893. the property list including an extra property :name with the block name."
  9894. (unless (looking-at org-dblock-start-re)
  9895. (error "Not at a dynamic block"))
  9896. (let* ((begdel (1+ (match-end 0)))
  9897. (name (org-no-properties (match-string 1)))
  9898. (params (append (list :name name)
  9899. (read (concat "(" (match-string 3) ")")))))
  9900. (save-excursion
  9901. (beginning-of-line 1)
  9902. (skip-chars-forward " \t")
  9903. (setq params (plist-put params :indentation-column (current-column))))
  9904. (unless (re-search-forward org-dblock-end-re nil t)
  9905. (error "Dynamic block not terminated"))
  9906. (setq params
  9907. (append params
  9908. (list :content (buffer-substring
  9909. begdel (match-beginning 0)))))
  9910. (delete-region begdel (match-beginning 0))
  9911. (goto-char begdel)
  9912. (open-line 1)
  9913. params))
  9914. (defun org-map-dblocks (&optional command)
  9915. "Apply COMMAND to all dynamic blocks in the current buffer.
  9916. If COMMAND is not given, use `org-update-dblock'."
  9917. (let ((cmd (or command 'org-update-dblock)))
  9918. (save-excursion
  9919. (goto-char (point-min))
  9920. (while (re-search-forward org-dblock-start-re nil t)
  9921. (goto-char (match-beginning 0))
  9922. (save-excursion
  9923. (condition-case nil
  9924. (funcall cmd)
  9925. (error (message "Error during update of dynamic block"))))
  9926. (unless (re-search-forward org-dblock-end-re nil t)
  9927. (error "Dynamic block not terminated"))))))
  9928. (defun org-dblock-update (&optional arg)
  9929. "User command for updating dynamic blocks.
  9930. Update the dynamic block at point. With prefix ARG, update all dynamic
  9931. blocks in the buffer."
  9932. (interactive "P")
  9933. (if arg
  9934. (org-update-all-dblocks)
  9935. (or (looking-at org-dblock-start-re)
  9936. (org-beginning-of-dblock))
  9937. (org-update-dblock)))
  9938. (defun org-update-dblock ()
  9939. "Update the dynamic block at point.
  9940. This means to empty the block, parse for parameters and then call
  9941. the correct writing function."
  9942. (interactive)
  9943. (save-window-excursion
  9944. (let* ((pos (point))
  9945. (line (org-current-line))
  9946. (params (org-prepare-dblock))
  9947. (name (plist-get params :name))
  9948. (indent (plist-get params :indentation-column))
  9949. (cmd (intern (concat "org-dblock-write:" name))))
  9950. (message "Updating dynamic block `%s' at line %d..." name line)
  9951. (funcall cmd params)
  9952. (message "Updating dynamic block `%s' at line %d...done" name line)
  9953. (goto-char pos)
  9954. (when (and indent (> indent 0))
  9955. (setq indent (make-string indent ?\ ))
  9956. (save-excursion
  9957. (org-beginning-of-dblock)
  9958. (forward-line 1)
  9959. (while (not (looking-at org-dblock-end-re))
  9960. (insert indent)
  9961. (beginning-of-line 2))
  9962. (when (looking-at org-dblock-end-re)
  9963. (and (looking-at "[ \t]+")
  9964. (replace-match ""))
  9965. (insert indent)))))))
  9966. (defun org-beginning-of-dblock ()
  9967. "Find the beginning of the dynamic block at point.
  9968. Error if there is no such block at point."
  9969. (let ((pos (point))
  9970. beg)
  9971. (end-of-line 1)
  9972. (if (and (re-search-backward org-dblock-start-re nil t)
  9973. (setq beg (match-beginning 0))
  9974. (re-search-forward org-dblock-end-re nil t)
  9975. (> (match-end 0) pos))
  9976. (goto-char beg)
  9977. (goto-char pos)
  9978. (error "Not in a dynamic block"))))
  9979. (defun org-update-all-dblocks ()
  9980. "Update all dynamic blocks in the buffer.
  9981. This function can be used in a hook."
  9982. (interactive)
  9983. (when (eq major-mode 'org-mode)
  9984. (org-map-dblocks 'org-update-dblock)))
  9985. ;;;; Completion
  9986. (defconst org-additional-option-like-keywords
  9987. '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML:"
  9988. "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook:"
  9989. "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:"
  9990. "LATEX_CLASS:" "LATEX_CLASS_OPTIONS:" "ATTR_LaTeX:"
  9991. "BEGIN:" "END:"
  9992. "ORGTBL" "TBLFM:" "TBLNAME:"
  9993. "BEGIN_EXAMPLE" "END_EXAMPLE"
  9994. "BEGIN_QUOTE" "END_QUOTE"
  9995. "BEGIN_VERSE" "END_VERSE"
  9996. "BEGIN_CENTER" "END_CENTER"
  9997. "BEGIN_SRC" "END_SRC"
  9998. "BEGIN_RESULT" "END_RESULT"
  9999. "NAME:" "RESULTS:"
  10000. "HEADER:" "HEADERS:"
  10001. "CATEGORY:" "COLUMNS:" "PROPERTY:"
  10002. "CAPTION:" "LABEL:"
  10003. "SETUPFILE:"
  10004. "INCLUDE:"
  10005. "BIND:"
  10006. "MACRO:"))
  10007. (defcustom org-structure-template-alist
  10008. '(
  10009. ("s" "#+BEGIN_SRC ?\n\n#+END_SRC"
  10010. "<src lang=\"?\">\n\n</src>")
  10011. ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE"
  10012. "<example>\n?\n</example>")
  10013. ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE"
  10014. "<quote>\n?\n</quote>")
  10015. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE"
  10016. "<verse>\n?\n</verse>")
  10017. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER"
  10018. "<center>\n?\n</center>")
  10019. ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX"
  10020. "<literal style=\"latex\">\n?\n</literal>")
  10021. ("L" "#+LaTeX: "
  10022. "<literal style=\"latex\">?</literal>")
  10023. ("h" "#+BEGIN_HTML\n?\n#+END_HTML"
  10024. "<literal style=\"html\">\n?\n</literal>")
  10025. ("H" "#+HTML: "
  10026. "<literal style=\"html\">?</literal>")
  10027. ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII")
  10028. ("A" "#+ASCII: ")
  10029. ("i" "#+INDEX: ?"
  10030. "#+INDEX: ?")
  10031. ("I" "#+INCLUDE %file ?"
  10032. "<include file=%file markup=\"?\">")
  10033. )
  10034. "Structure completion elements.
  10035. This is a list of abbreviation keys and values. The value gets inserted
  10036. if you type `<' followed by the key and then press the completion key,
  10037. usually `M-TAB'. %file will be replaced by a file name after prompting
  10038. for the file using completion. The cursor will be placed at the position
  10039. of the `?` in the template.
  10040. There are two templates for each key, the first uses the original Org syntax,
  10041. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  10042. the default when the /org-mtags.el/ module has been loaded. See also the
  10043. variable `org-mtags-prefer-muse-templates'."
  10044. :group 'org-completion
  10045. :type '(repeat
  10046. (string :tag "Key")
  10047. (string :tag "Template")
  10048. (string :tag "Muse Template")))
  10049. (defun org-try-structure-completion ()
  10050. "Try to complete a structure template before point.
  10051. This looks for strings like \"<e\" on an otherwise empty line and
  10052. expands them."
  10053. (let ((l (buffer-substring (point-at-bol) (point)))
  10054. a)
  10055. (when (and (looking-at "[ \t]*$")
  10056. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  10057. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  10058. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  10059. (match-beginning 1)) a)
  10060. t)))
  10061. (defun org-complete-expand-structure-template (start cell)
  10062. "Expand a structure template."
  10063. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  10064. (rpl (nth (if musep 2 1) cell))
  10065. (ind ""))
  10066. (delete-region start (point))
  10067. (when (string-match "\\`#\\+" rpl)
  10068. (cond
  10069. ((bolp))
  10070. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  10071. (setq ind (buffer-substring (point-at-bol) (point))))
  10072. (t (newline))))
  10073. (setq start (point))
  10074. (if (string-match "%file" rpl)
  10075. (setq rpl (replace-match
  10076. (concat
  10077. "\""
  10078. (save-match-data
  10079. (abbreviate-file-name (read-file-name "Include file: ")))
  10080. "\"")
  10081. t t rpl)))
  10082. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  10083. (concat "\n" ind)))
  10084. (insert rpl)
  10085. (if (re-search-backward "\\?" start t) (delete-char 1))))
  10086. ;;;; TODO, DEADLINE, Comments
  10087. (defun org-toggle-comment ()
  10088. "Change the COMMENT state of an entry."
  10089. (interactive)
  10090. (save-excursion
  10091. (org-back-to-heading)
  10092. (let (case-fold-search)
  10093. (cond
  10094. ((looking-at (format org-heading-keyword-regexp-format
  10095. org-comment-string))
  10096. (goto-char (match-end 1))
  10097. (looking-at (concat " +" org-comment-string))
  10098. (replace-match "" t t)
  10099. (when (eolp) (insert " ")))
  10100. ((looking-at org-outline-regexp)
  10101. (goto-char (match-end 0))
  10102. (insert org-comment-string " "))))))
  10103. (defvar org-last-todo-state-is-todo nil
  10104. "This is non-nil when the last TODO state change led to a TODO state.
  10105. If the last change removed the TODO tag or switched to DONE, then
  10106. this is nil.")
  10107. (defvar org-setting-tags nil) ; dynamically skipped
  10108. (defvar org-todo-setup-filter-hook nil
  10109. "Hook for functions that pre-filter todo specs.
  10110. Each function takes a todo spec and returns either nil or the spec
  10111. transformed into canonical form." )
  10112. (defvar org-todo-get-default-hook nil
  10113. "Hook for functions that get a default item for todo.
  10114. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  10115. nil or a string to be used for the todo mark." )
  10116. (defvar org-agenda-headline-snapshot-before-repeat)
  10117. (defun org-current-effective-time ()
  10118. "Return current time adjusted for `org-extend-today-until' variable"
  10119. (let* ((ct (org-current-time))
  10120. (dct (decode-time ct))
  10121. (ct1
  10122. (if (and org-use-effective-time
  10123. (< (nth 2 dct) org-extend-today-until))
  10124. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct))
  10125. ct)))
  10126. ct1))
  10127. (defun org-todo-yesterday (&optional arg)
  10128. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  10129. (interactive "P")
  10130. (if (eq major-mode 'org-agenda-mode)
  10131. (apply 'org-agenda-todo-yesterday arg)
  10132. (let* ((hour (third (decode-time
  10133. (org-current-time))))
  10134. (org-extend-today-until (1+ hour)))
  10135. (org-todo arg))))
  10136. (defun org-todo (&optional arg)
  10137. "Change the TODO state of an item.
  10138. The state of an item is given by a keyword at the start of the heading,
  10139. like
  10140. *** TODO Write paper
  10141. *** DONE Call mom
  10142. The different keywords are specified in the variable `org-todo-keywords'.
  10143. By default the available states are \"TODO\" and \"DONE\".
  10144. So for this example: when the item starts with TODO, it is changed to DONE.
  10145. When it starts with DONE, the DONE is removed. And when neither TODO nor
  10146. DONE are present, add TODO at the beginning of the heading.
  10147. With \\[universal-argument] prefix arg, use completion to determine the new \
  10148. state.
  10149. With numeric prefix arg, switch to that state.
  10150. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  10151. keywords (nextset).
  10152. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  10153. With a numeric prefix arg of 0, inhibit note taking for the change.
  10154. For calling through lisp, arg is also interpreted in the following way:
  10155. 'none -> empty state
  10156. \"\"(empty string) -> switch to empty state
  10157. 'done -> switch to DONE
  10158. 'nextset -> switch to the next set of keywords
  10159. 'previousset -> switch to the previous set of keywords
  10160. \"WAITING\" -> switch to the specified keyword, but only if it
  10161. really is a member of `org-todo-keywords'."
  10162. (interactive "P")
  10163. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  10164. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  10165. 'region-start-level 'region))
  10166. org-loop-over-headlines-in-active-region)
  10167. (org-map-entries
  10168. `(org-todo ,arg)
  10169. org-loop-over-headlines-in-active-region
  10170. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  10171. (if (equal arg '(16)) (setq arg 'nextset))
  10172. (let ((org-blocker-hook org-blocker-hook)
  10173. (case-fold-search nil))
  10174. (when (equal arg '(64))
  10175. (setq arg nil org-blocker-hook nil))
  10176. (when (and org-blocker-hook
  10177. (or org-inhibit-blocking
  10178. (org-entry-get nil "NOBLOCKING")))
  10179. (setq org-blocker-hook nil))
  10180. (save-excursion
  10181. (catch 'exit
  10182. (org-back-to-heading t)
  10183. (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  10184. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  10185. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  10186. (let* ((match-data (match-data))
  10187. (startpos (point-at-bol))
  10188. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  10189. (org-log-done org-log-done)
  10190. (org-log-repeat org-log-repeat)
  10191. (org-todo-log-states org-todo-log-states)
  10192. (org-inhibit-logging
  10193. (if (equal arg 0)
  10194. (progn (setq arg nil) 'note) org-inhibit-logging))
  10195. (this (match-string 1))
  10196. (hl-pos (match-beginning 0))
  10197. (head (org-get-todo-sequence-head this))
  10198. (ass (assoc head org-todo-kwd-alist))
  10199. (interpret (nth 1 ass))
  10200. (done-word (nth 3 ass))
  10201. (final-done-word (nth 4 ass))
  10202. (last-state (or this ""))
  10203. (completion-ignore-case t)
  10204. (member (member this org-todo-keywords-1))
  10205. (tail (cdr member))
  10206. (state (cond
  10207. ((and org-todo-key-trigger
  10208. (or (and (equal arg '(4))
  10209. (eq org-use-fast-todo-selection 'prefix))
  10210. (and (not arg) org-use-fast-todo-selection
  10211. (not (eq org-use-fast-todo-selection
  10212. 'prefix)))))
  10213. ;; Use fast selection
  10214. (org-fast-todo-selection))
  10215. ((and (equal arg '(4))
  10216. (or (not org-use-fast-todo-selection)
  10217. (not org-todo-key-trigger)))
  10218. ;; Read a state with completion
  10219. (org-icompleting-read
  10220. "State: " (mapcar (lambda(x) (list x))
  10221. org-todo-keywords-1)
  10222. nil t))
  10223. ((eq arg 'right)
  10224. (if this
  10225. (if tail (car tail) nil)
  10226. (car org-todo-keywords-1)))
  10227. ((eq arg 'left)
  10228. (if (equal member org-todo-keywords-1)
  10229. nil
  10230. (if this
  10231. (nth (- (length org-todo-keywords-1)
  10232. (length tail) 2)
  10233. org-todo-keywords-1)
  10234. (org-last org-todo-keywords-1))))
  10235. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  10236. (setq arg nil))) ; hack to fall back to cycling
  10237. (arg
  10238. ;; user or caller requests a specific state
  10239. (cond
  10240. ((equal arg "") nil)
  10241. ((eq arg 'none) nil)
  10242. ((eq arg 'done) (or done-word (car org-done-keywords)))
  10243. ((eq arg 'nextset)
  10244. (or (car (cdr (member head org-todo-heads)))
  10245. (car org-todo-heads)))
  10246. ((eq arg 'previousset)
  10247. (let ((org-todo-heads (reverse org-todo-heads)))
  10248. (or (car (cdr (member head org-todo-heads)))
  10249. (car org-todo-heads))))
  10250. ((car (member arg org-todo-keywords-1)))
  10251. ((stringp arg)
  10252. (error "State `%s' not valid in this file" arg))
  10253. ((nth (1- (prefix-numeric-value arg))
  10254. org-todo-keywords-1))))
  10255. ((null member) (or head (car org-todo-keywords-1)))
  10256. ((equal this final-done-word) nil) ;; -> make empty
  10257. ((null tail) nil) ;; -> first entry
  10258. ((memq interpret '(type priority))
  10259. (if (eq this-command last-command)
  10260. (car tail)
  10261. (if (> (length tail) 0)
  10262. (or done-word (car org-done-keywords))
  10263. nil)))
  10264. (t
  10265. (car tail))))
  10266. (state (or
  10267. (run-hook-with-args-until-success
  10268. 'org-todo-get-default-hook state last-state)
  10269. state))
  10270. (next (if state (concat " " state " ") " "))
  10271. (change-plist (list :type 'todo-state-change :from this :to state
  10272. :position startpos))
  10273. dolog now-done-p)
  10274. (when org-blocker-hook
  10275. (setq org-last-todo-state-is-todo
  10276. (not (member this org-done-keywords)))
  10277. (unless (save-excursion
  10278. (save-match-data
  10279. (org-with-wide-buffer
  10280. (run-hook-with-args-until-failure
  10281. 'org-blocker-hook change-plist))))
  10282. (if (org-called-interactively-p 'interactive)
  10283. (error "TODO state change from %s to %s blocked" this state)
  10284. ;; fail silently
  10285. (message "TODO state change from %s to %s blocked" this state)
  10286. (throw 'exit nil))))
  10287. (store-match-data match-data)
  10288. (replace-match next t t)
  10289. (unless (pos-visible-in-window-p hl-pos)
  10290. (message "TODO state changed to %s" (org-trim next)))
  10291. (unless head
  10292. (setq head (org-get-todo-sequence-head state)
  10293. ass (assoc head org-todo-kwd-alist)
  10294. interpret (nth 1 ass)
  10295. done-word (nth 3 ass)
  10296. final-done-word (nth 4 ass)))
  10297. (when (memq arg '(nextset previousset))
  10298. (message "Keyword-Set %d/%d: %s"
  10299. (- (length org-todo-sets) -1
  10300. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  10301. (length org-todo-sets)
  10302. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  10303. (setq org-last-todo-state-is-todo
  10304. (not (member state org-done-keywords)))
  10305. (setq now-done-p (and (member state org-done-keywords)
  10306. (not (member this org-done-keywords))))
  10307. (and logging (org-local-logging logging))
  10308. (when (and (or org-todo-log-states org-log-done)
  10309. (not (eq org-inhibit-logging t))
  10310. (not (memq arg '(nextset previousset))))
  10311. ;; we need to look at recording a time and note
  10312. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  10313. (nth 2 (assoc this org-todo-log-states))))
  10314. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  10315. (setq dolog 'time))
  10316. (when (and state
  10317. (member state org-not-done-keywords)
  10318. (not (member this org-not-done-keywords)))
  10319. ;; This is now a todo state and was not one before
  10320. ;; If there was a CLOSED time stamp, get rid of it.
  10321. (org-add-planning-info nil nil 'closed))
  10322. (when (and now-done-p org-log-done)
  10323. ;; It is now done, and it was not done before
  10324. (org-add-planning-info 'closed (org-current-effective-time))
  10325. (if (and (not dolog) (eq 'note org-log-done))
  10326. (org-add-log-setup 'done state this 'findpos 'note)))
  10327. (when (and state dolog)
  10328. ;; This is a non-nil state, and we need to log it
  10329. (org-add-log-setup 'state state this 'findpos dolog)))
  10330. ;; Fixup tag positioning
  10331. (org-todo-trigger-tag-changes state)
  10332. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  10333. (when org-provide-todo-statistics
  10334. (org-update-parent-todo-statistics))
  10335. (run-hooks 'org-after-todo-state-change-hook)
  10336. (if (and arg (not (member state org-done-keywords)))
  10337. (setq head (org-get-todo-sequence-head state)))
  10338. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  10339. ;; Do we need to trigger a repeat?
  10340. (when now-done-p
  10341. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  10342. ;; This is for the agenda, take a snapshot of the headline.
  10343. (save-match-data
  10344. (setq org-agenda-headline-snapshot-before-repeat
  10345. (org-get-heading))))
  10346. (org-auto-repeat-maybe state))
  10347. ;; Fixup cursor location if close to the keyword
  10348. (if (and (outline-on-heading-p)
  10349. (not (bolp))
  10350. (save-excursion (beginning-of-line 1)
  10351. (looking-at org-todo-line-regexp))
  10352. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  10353. (progn
  10354. (goto-char (or (match-end 2) (match-end 1)))
  10355. (and (looking-at " ") (just-one-space))))
  10356. (when org-trigger-hook
  10357. (save-excursion
  10358. (run-hook-with-args 'org-trigger-hook change-plist)))))))))
  10359. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  10360. "Block turning an entry into a TODO, using the hierarchy.
  10361. This checks whether the current task should be blocked from state
  10362. changes. Such blocking occurs when:
  10363. 1. The task has children which are not all in a completed state.
  10364. 2. A task has a parent with the property :ORDERED:, and there
  10365. are siblings prior to the current task with incomplete
  10366. status.
  10367. 3. The parent of the task is blocked because it has siblings that should
  10368. be done first, or is child of a block grandparent TODO entry."
  10369. (if (not org-enforce-todo-dependencies)
  10370. t ; if locally turned off don't block
  10371. (catch 'dont-block
  10372. ;; If this is not a todo state change, or if this entry is already DONE,
  10373. ;; do not block
  10374. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10375. (member (plist-get change-plist :from)
  10376. (cons 'done org-done-keywords))
  10377. (member (plist-get change-plist :to)
  10378. (cons 'todo org-not-done-keywords))
  10379. (not (plist-get change-plist :to)))
  10380. (throw 'dont-block t))
  10381. ;; If this task has children, and any are undone, it's blocked
  10382. (save-excursion
  10383. (org-back-to-heading t)
  10384. (let ((this-level (funcall outline-level)))
  10385. (outline-next-heading)
  10386. (let ((child-level (funcall outline-level)))
  10387. (while (and (not (eobp))
  10388. (> child-level this-level))
  10389. ;; this todo has children, check whether they are all
  10390. ;; completed
  10391. (if (and (not (org-entry-is-done-p))
  10392. (org-entry-is-todo-p))
  10393. (throw 'dont-block nil))
  10394. (outline-next-heading)
  10395. (setq child-level (funcall outline-level))))))
  10396. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  10397. ;; any previous siblings are undone, it's blocked
  10398. (save-excursion
  10399. (org-back-to-heading t)
  10400. (let* ((pos (point))
  10401. (parent-pos (and (org-up-heading-safe) (point))))
  10402. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10403. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10404. (forward-line 1)
  10405. (re-search-forward org-not-done-heading-regexp pos t))
  10406. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  10407. ;; Search further up the hierarchy, to see if an ancestor is blocked
  10408. (while t
  10409. (goto-char parent-pos)
  10410. (if (not (looking-at org-not-done-heading-regexp))
  10411. (throw 'dont-block t)) ; do not block, parent is not a TODO
  10412. (setq pos (point))
  10413. (setq parent-pos (and (org-up-heading-safe) (point)))
  10414. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10415. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10416. (forward-line 1)
  10417. (re-search-forward org-not-done-heading-regexp pos t))
  10418. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  10419. (defcustom org-track-ordered-property-with-tag nil
  10420. "Should the ORDERED property also be shown as a tag?
  10421. The ORDERED property decides if an entry should require subtasks to be
  10422. completed in sequence. Since a property is not very visible, setting
  10423. this option means that toggling the ORDERED property with the command
  10424. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  10425. not relevant for the behavior, but it makes things more visible.
  10426. Note that toggling the tag with tags commands will not change the property
  10427. and therefore not influence behavior!
  10428. This can be t, meaning the tag ORDERED should be used, It can also be a
  10429. string to select a different tag for this task."
  10430. :group 'org-todo
  10431. :type '(choice
  10432. (const :tag "No tracking" nil)
  10433. (const :tag "Track with ORDERED tag" t)
  10434. (string :tag "Use other tag")))
  10435. (defun org-toggle-ordered-property ()
  10436. "Toggle the ORDERED property of the current entry.
  10437. For better visibility, you can track the value of this property with a tag.
  10438. See variable `org-track-ordered-property-with-tag'."
  10439. (interactive)
  10440. (let* ((t1 org-track-ordered-property-with-tag)
  10441. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  10442. (save-excursion
  10443. (org-back-to-heading)
  10444. (if (org-entry-get nil "ORDERED")
  10445. (progn
  10446. (org-delete-property "ORDERED")
  10447. (and tag (org-toggle-tag tag 'off))
  10448. (message "Subtasks can be completed in arbitrary order"))
  10449. (org-entry-put nil "ORDERED" "t")
  10450. (and tag (org-toggle-tag tag 'on))
  10451. (message "Subtasks must be completed in sequence")))))
  10452. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  10453. (defun org-block-todo-from-checkboxes (change-plist)
  10454. "Block turning an entry into a TODO, using checkboxes.
  10455. This checks whether the current task should be blocked from state
  10456. changes because there are unchecked boxes in this entry."
  10457. (if (not org-enforce-todo-checkbox-dependencies)
  10458. t ; if locally turned off don't block
  10459. (catch 'dont-block
  10460. ;; If this is not a todo state change, or if this entry is already DONE,
  10461. ;; do not block
  10462. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10463. (member (plist-get change-plist :from)
  10464. (cons 'done org-done-keywords))
  10465. (member (plist-get change-plist :to)
  10466. (cons 'todo org-not-done-keywords))
  10467. (not (plist-get change-plist :to)))
  10468. (throw 'dont-block t))
  10469. ;; If this task has checkboxes that are not checked, it's blocked
  10470. (save-excursion
  10471. (org-back-to-heading t)
  10472. (let ((beg (point)) end)
  10473. (outline-next-heading)
  10474. (setq end (point))
  10475. (goto-char beg)
  10476. (if (org-list-search-forward
  10477. (concat (org-item-beginning-re)
  10478. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  10479. "\\[[- ]\\]")
  10480. end t)
  10481. (progn
  10482. (if (boundp 'org-blocked-by-checkboxes)
  10483. (setq org-blocked-by-checkboxes t))
  10484. (throw 'dont-block nil)))))
  10485. t))) ; do not block
  10486. (defun org-entry-blocked-p ()
  10487. "Is the current entry blocked?"
  10488. (if (org-entry-get nil "NOBLOCKING")
  10489. nil ;; Never block this entry
  10490. (not
  10491. (run-hook-with-args-until-failure
  10492. 'org-blocker-hook
  10493. (list :type 'todo-state-change
  10494. :position (point)
  10495. :from 'todo
  10496. :to 'done)))))
  10497. (defun org-update-statistics-cookies (all)
  10498. "Update the statistics cookie, either from TODO or from checkboxes.
  10499. This should be called with the cursor in a line with a statistics cookie."
  10500. (interactive "P")
  10501. (if all
  10502. (progn
  10503. (org-update-checkbox-count 'all)
  10504. (org-map-entries 'org-update-parent-todo-statistics))
  10505. (if (not (org-at-heading-p))
  10506. (org-update-checkbox-count)
  10507. (let ((pos (move-marker (make-marker) (point)))
  10508. end l1 l2)
  10509. (ignore-errors (org-back-to-heading t))
  10510. (if (not (org-at-heading-p))
  10511. (org-update-checkbox-count)
  10512. (setq l1 (org-outline-level))
  10513. (setq end (save-excursion
  10514. (outline-next-heading)
  10515. (if (org-at-heading-p) (setq l2 (org-outline-level)))
  10516. (point)))
  10517. (if (and (save-excursion
  10518. (re-search-forward
  10519. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  10520. (not (save-excursion (re-search-forward
  10521. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  10522. (org-update-checkbox-count)
  10523. (if (and l2 (> l2 l1))
  10524. (progn
  10525. (goto-char end)
  10526. (org-update-parent-todo-statistics))
  10527. (goto-char pos)
  10528. (beginning-of-line 1)
  10529. (while (re-search-forward
  10530. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  10531. (point-at-eol) t)
  10532. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  10533. (goto-char pos)
  10534. (move-marker pos nil)))))
  10535. (defvar org-entry-property-inherited-from) ;; defined below
  10536. (defun org-update-parent-todo-statistics ()
  10537. "Update any statistics cookie in the parent of the current headline.
  10538. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  10539. the entire subtree and this will travel up the hierarchy and update
  10540. statistics everywhere."
  10541. (let* ((prop (save-excursion (org-up-heading-safe)
  10542. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  10543. (recursive (or (not org-hierarchical-todo-statistics)
  10544. (and prop (string-match "\\<recursive\\>" prop))))
  10545. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  10546. 0))
  10547. (first t)
  10548. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  10549. level ltoggle l1 new ndel
  10550. (cnt-all 0) (cnt-done 0) is-percent kwd
  10551. checkbox-beg ov ovs ove cookie-present)
  10552. (catch 'exit
  10553. (save-excursion
  10554. (beginning-of-line 1)
  10555. (setq ltoggle (funcall outline-level))
  10556. ;; Three situations are to consider:
  10557. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  10558. ;; to the top-level ancestor on the headline;
  10559. ;; 2. If parent has "recursive" property, repeat up to the
  10560. ;; headline setting that property, taking inheritance into
  10561. ;; account;
  10562. ;; 3. Else, move up to direct parent and proceed only once.
  10563. (while (and (setq level (org-up-heading-safe))
  10564. (or recursive first)
  10565. (>= (point) lim))
  10566. (setq first nil cookie-present nil)
  10567. (unless (and level
  10568. (not (string-match
  10569. "\\<checkbox\\>"
  10570. (downcase (or (org-entry-get nil "COOKIE_DATA")
  10571. "")))))
  10572. (throw 'exit nil))
  10573. (while (re-search-forward box-re (point-at-eol) t)
  10574. (setq cnt-all 0 cnt-done 0 cookie-present t)
  10575. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  10576. (save-match-data
  10577. (unless (outline-next-heading) (throw 'exit nil))
  10578. (while (and (looking-at org-complex-heading-regexp)
  10579. (> (setq l1 (length (match-string 1))) level))
  10580. (setq kwd (and (or recursive (= l1 ltoggle))
  10581. (match-string 2)))
  10582. (if (or (eq org-provide-todo-statistics 'all-headlines)
  10583. (and (listp org-provide-todo-statistics)
  10584. (or (member kwd org-provide-todo-statistics)
  10585. (member kwd org-done-keywords))))
  10586. (setq cnt-all (1+ cnt-all))
  10587. (if (eq org-provide-todo-statistics t)
  10588. (and kwd (setq cnt-all (1+ cnt-all)))))
  10589. (and (member kwd org-done-keywords)
  10590. (setq cnt-done (1+ cnt-done)))
  10591. (outline-next-heading)))
  10592. (setq new
  10593. (if is-percent
  10594. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  10595. (format "[%d/%d]" cnt-done cnt-all))
  10596. ndel (- (match-end 0) checkbox-beg))
  10597. ;; handle overlays when updating cookie from column view
  10598. (when (setq ov (car (overlays-at checkbox-beg)))
  10599. (setq ovs (overlay-start ov) ove (overlay-end ov))
  10600. (delete-overlay ov))
  10601. (goto-char checkbox-beg)
  10602. (insert new)
  10603. (delete-region (point) (+ (point) ndel))
  10604. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  10605. (when ov (move-overlay ov ovs ove)))
  10606. (when cookie-present
  10607. (run-hook-with-args 'org-after-todo-statistics-hook
  10608. cnt-done (- cnt-all cnt-done))))))
  10609. (run-hooks 'org-todo-statistics-hook)))
  10610. (defvar org-after-todo-statistics-hook nil
  10611. "Hook that is called after a TODO statistics cookie has been updated.
  10612. Each function is called with two arguments: the number of not-done entries
  10613. and the number of done entries.
  10614. For example, the following function, when added to this hook, will switch
  10615. an entry to DONE when all children are done, and back to TODO when new
  10616. entries are set to a TODO status. Note that this hook is only called
  10617. when there is a statistics cookie in the headline!
  10618. (defun org-summary-todo (n-done n-not-done)
  10619. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  10620. (let (org-log-done org-log-states) ; turn off logging
  10621. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  10622. ")
  10623. (defvar org-todo-statistics-hook nil
  10624. "Hook that is run whenever Org thinks TODO statistics should be updated.
  10625. This hook runs even if there is no statistics cookie present, in which case
  10626. `org-after-todo-statistics-hook' would not run.")
  10627. (defun org-todo-trigger-tag-changes (state)
  10628. "Apply the changes defined in `org-todo-state-tags-triggers'."
  10629. (let ((l org-todo-state-tags-triggers)
  10630. changes)
  10631. (when (or (not state) (equal state ""))
  10632. (setq changes (append changes (cdr (assoc "" l)))))
  10633. (when (and (stringp state) (> (length state) 0))
  10634. (setq changes (append changes (cdr (assoc state l)))))
  10635. (when (member state org-not-done-keywords)
  10636. (setq changes (append changes (cdr (assoc 'todo l)))))
  10637. (when (member state org-done-keywords)
  10638. (setq changes (append changes (cdr (assoc 'done l)))))
  10639. (dolist (c changes)
  10640. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  10641. (defun org-local-logging (value)
  10642. "Get logging settings from a property VALUE."
  10643. (let* (words w a)
  10644. ;; directly set the variables, they are already local.
  10645. (setq org-log-done nil
  10646. org-log-repeat nil
  10647. org-todo-log-states nil)
  10648. (setq words (org-split-string value))
  10649. (while (setq w (pop words))
  10650. (cond
  10651. ((setq a (assoc w org-startup-options))
  10652. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  10653. (set (nth 1 a) (nth 2 a))))
  10654. ((setq a (org-extract-log-state-settings w))
  10655. (and (member (car a) org-todo-keywords-1)
  10656. (push a org-todo-log-states)))))))
  10657. (defun org-get-todo-sequence-head (kwd)
  10658. "Return the head of the TODO sequence to which KWD belongs.
  10659. If KWD is not set, check if there is a text property remembering the
  10660. right sequence."
  10661. (let (p)
  10662. (cond
  10663. ((not kwd)
  10664. (or (get-text-property (point-at-bol) 'org-todo-head)
  10665. (progn
  10666. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  10667. nil (point-at-eol)))
  10668. (get-text-property p 'org-todo-head))))
  10669. ((not (member kwd org-todo-keywords-1))
  10670. (car org-todo-keywords-1))
  10671. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  10672. (defun org-fast-todo-selection ()
  10673. "Fast TODO keyword selection with single keys.
  10674. Returns the new TODO keyword, or nil if no state change should occur."
  10675. (let* ((fulltable org-todo-key-alist)
  10676. (done-keywords org-done-keywords) ;; needed for the faces.
  10677. (maxlen (apply 'max (mapcar
  10678. (lambda (x)
  10679. (if (stringp (car x)) (string-width (car x)) 0))
  10680. fulltable)))
  10681. (expert nil)
  10682. (fwidth (+ maxlen 3 1 3))
  10683. (ncol (/ (- (window-width) 4) fwidth))
  10684. tg cnt e c tbl
  10685. groups ingroup)
  10686. (save-excursion
  10687. (save-window-excursion
  10688. (if expert
  10689. (set-buffer (get-buffer-create " *Org todo*"))
  10690. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  10691. (erase-buffer)
  10692. (org-set-local 'org-done-keywords done-keywords)
  10693. (setq tbl fulltable cnt 0)
  10694. (while (setq e (pop tbl))
  10695. (cond
  10696. ((equal e '(:startgroup))
  10697. (push '() groups) (setq ingroup t)
  10698. (when (not (= cnt 0))
  10699. (setq cnt 0)
  10700. (insert "\n"))
  10701. (insert "{ "))
  10702. ((equal e '(:endgroup))
  10703. (setq ingroup nil cnt 0)
  10704. (insert "}\n"))
  10705. ((equal e '(:newline))
  10706. (when (not (= cnt 0))
  10707. (setq cnt 0)
  10708. (insert "\n")
  10709. (setq e (car tbl))
  10710. (while (equal (car tbl) '(:newline))
  10711. (insert "\n")
  10712. (setq tbl (cdr tbl)))))
  10713. (t
  10714. (setq tg (car e) c (cdr e))
  10715. (if ingroup (push tg (car groups)))
  10716. (setq tg (org-add-props tg nil 'face
  10717. (org-get-todo-face tg)))
  10718. (if (and (= cnt 0) (not ingroup)) (insert " "))
  10719. (insert "[" c "] " tg (make-string
  10720. (- fwidth 4 (length tg)) ?\ ))
  10721. (when (= (setq cnt (1+ cnt)) ncol)
  10722. (insert "\n")
  10723. (if ingroup (insert " "))
  10724. (setq cnt 0)))))
  10725. (insert "\n")
  10726. (goto-char (point-min))
  10727. (if (not expert) (org-fit-window-to-buffer))
  10728. (message "[a-z..]:Set [SPC]:clear")
  10729. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  10730. (cond
  10731. ((or (= c ?\C-g)
  10732. (and (= c ?q) (not (rassoc c fulltable))))
  10733. (setq quit-flag t))
  10734. ((= c ?\ ) nil)
  10735. ((setq e (rassoc c fulltable) tg (car e))
  10736. tg)
  10737. (t (setq quit-flag t)))))))
  10738. (defun org-entry-is-todo-p ()
  10739. (member (org-get-todo-state) org-not-done-keywords))
  10740. (defun org-entry-is-done-p ()
  10741. (member (org-get-todo-state) org-done-keywords))
  10742. (defun org-get-todo-state ()
  10743. (save-excursion
  10744. (org-back-to-heading t)
  10745. (and (looking-at org-todo-line-regexp)
  10746. (match-end 2)
  10747. (match-string 2))))
  10748. (defun org-at-date-range-p (&optional inactive-ok)
  10749. "Is the cursor inside a date range?"
  10750. (interactive)
  10751. (save-excursion
  10752. (catch 'exit
  10753. (let ((pos (point)))
  10754. (skip-chars-backward "^[<\r\n")
  10755. (skip-chars-backward "<[")
  10756. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  10757. (>= (match-end 0) pos)
  10758. (throw 'exit t))
  10759. (skip-chars-backward "^<[\r\n")
  10760. (skip-chars-backward "<[")
  10761. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  10762. (>= (match-end 0) pos)
  10763. (throw 'exit t)))
  10764. nil)))
  10765. (defun org-get-repeat (&optional tagline)
  10766. "Check if there is a deadline/schedule with repeater in this entry."
  10767. (save-match-data
  10768. (save-excursion
  10769. (org-back-to-heading t)
  10770. (and (re-search-forward (if tagline
  10771. (concat tagline "\\s-*" org-repeat-re)
  10772. org-repeat-re)
  10773. (org-entry-end-position) t)
  10774. (match-string-no-properties 1)))))
  10775. (defvar org-last-changed-timestamp)
  10776. (defvar org-last-inserted-timestamp)
  10777. (defvar org-log-post-message)
  10778. (defvar org-log-note-purpose)
  10779. (defvar org-log-note-how)
  10780. (defvar org-log-note-extra)
  10781. (defun org-auto-repeat-maybe (done-word)
  10782. "Check if the current headline contains a repeated deadline/schedule.
  10783. If yes, set TODO state back to what it was and change the base date
  10784. of repeating deadline/scheduled time stamps to new date.
  10785. This function is run automatically after each state change to a DONE state."
  10786. ;; last-state is dynamically scoped into this function
  10787. (let* ((repeat (org-get-repeat))
  10788. (aa (assoc last-state org-todo-kwd-alist))
  10789. (interpret (nth 1 aa))
  10790. (head (nth 2 aa))
  10791. (whata '(("d" . day) ("m" . month) ("y" . year)))
  10792. (msg "Entry repeats: ")
  10793. (org-log-done nil)
  10794. (org-todo-log-states nil)
  10795. re type n what ts time to-state)
  10796. (when repeat
  10797. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  10798. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  10799. org-todo-repeat-to-state))
  10800. (unless (and to-state (member to-state org-todo-keywords-1))
  10801. (setq to-state (if (eq interpret 'type) last-state head)))
  10802. (org-todo to-state)
  10803. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  10804. (org-entry-put nil "LAST_REPEAT" (format-time-string
  10805. (org-time-stamp-format t t))))
  10806. (when org-log-repeat
  10807. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  10808. (memq 'org-add-log-note post-command-hook))
  10809. ;; OK, we are already setup for some record
  10810. (if (eq org-log-repeat 'note)
  10811. ;; make sure we take a note, not only a time stamp
  10812. (setq org-log-note-how 'note))
  10813. ;; Set up for taking a record
  10814. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  10815. last-state
  10816. 'findpos org-log-repeat)))
  10817. (org-back-to-heading t)
  10818. (org-add-planning-info nil nil 'closed)
  10819. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  10820. org-deadline-time-regexp "\\)\\|\\("
  10821. org-ts-regexp "\\)"))
  10822. (while (re-search-forward
  10823. re (save-excursion (outline-next-heading) (point)) t)
  10824. (setq type (if (match-end 1) org-scheduled-string
  10825. (if (match-end 3) org-deadline-string "Plain:"))
  10826. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  10827. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  10828. (setq n (string-to-number (match-string 2 ts))
  10829. what (match-string 3 ts))
  10830. (if (equal what "w") (setq n (* n 7) what "d"))
  10831. ;; Preparation, see if we need to modify the start date for the change
  10832. (when (match-end 1)
  10833. (setq time (save-match-data (org-time-string-to-time ts)))
  10834. (cond
  10835. ((equal (match-string 1 ts) ".")
  10836. ;; Shift starting date to today
  10837. (org-timestamp-change
  10838. (- (org-today) (time-to-days time))
  10839. 'day))
  10840. ((equal (match-string 1 ts) "+")
  10841. (let ((nshiftmax 10) (nshift 0))
  10842. (while (or (= nshift 0)
  10843. (<= (time-to-days time)
  10844. (time-to-days (current-time))))
  10845. (when (= (incf nshift) nshiftmax)
  10846. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  10847. (error "Abort")))
  10848. (org-timestamp-change n (cdr (assoc what whata)))
  10849. (org-at-timestamp-p t)
  10850. (setq ts (match-string 1))
  10851. (setq time (save-match-data (org-time-string-to-time ts)))))
  10852. (org-timestamp-change (- n) (cdr (assoc what whata)))
  10853. ;; rematch, so that we have everything in place for the real shift
  10854. (org-at-timestamp-p t)
  10855. (setq ts (match-string 1))
  10856. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  10857. (org-timestamp-change n (cdr (assoc what whata)))
  10858. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  10859. (setq org-log-post-message msg)
  10860. (message "%s" msg))))
  10861. (defun org-show-todo-tree (arg)
  10862. "Make a compact tree which shows all headlines marked with TODO.
  10863. The tree will show the lines where the regexp matches, and all higher
  10864. headlines above the match.
  10865. With a \\[universal-argument] prefix, prompt for a regexp to match.
  10866. With a numeric prefix N, construct a sparse tree for the Nth element
  10867. of `org-todo-keywords-1'."
  10868. (interactive "P")
  10869. (let ((case-fold-search nil)
  10870. (kwd-re
  10871. (cond ((null arg) org-not-done-regexp)
  10872. ((equal arg '(4))
  10873. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  10874. (mapcar 'list org-todo-keywords-1))))
  10875. (concat "\\("
  10876. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  10877. "\\)\\>")))
  10878. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  10879. (regexp-quote (nth (1- (prefix-numeric-value arg))
  10880. org-todo-keywords-1)))
  10881. (t (error "Invalid prefix argument: %s" arg)))))
  10882. (message "%d TODO entries found"
  10883. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  10884. (defun org-deadline (&optional remove time)
  10885. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  10886. With argument REMOVE, remove any deadline from the item.
  10887. With argument TIME, set the deadline at the corresponding date. TIME
  10888. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  10889. (interactive "P")
  10890. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  10891. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  10892. 'region-start-level 'region))
  10893. org-loop-over-headlines-in-active-region)
  10894. (org-map-entries
  10895. `(org-deadline ',remove ,time)
  10896. org-loop-over-headlines-in-active-region
  10897. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  10898. (let* ((old-date (org-entry-get nil "DEADLINE"))
  10899. (repeater (and old-date
  10900. (string-match
  10901. "\\([.+-]+[0-9]+[dwmy]\\(?:[/ ][-+]?[0-9]+[dwmy]\\)?\\) ?"
  10902. old-date)
  10903. (match-string 1 old-date))))
  10904. (if remove
  10905. (progn
  10906. (when (and old-date org-log-redeadline)
  10907. (org-add-log-setup 'deldeadline nil old-date 'findpos
  10908. org-log-redeadline))
  10909. (org-remove-timestamp-with-keyword org-deadline-string)
  10910. (message "Item no longer has a deadline."))
  10911. (org-add-planning-info 'deadline time 'closed)
  10912. (when (and old-date org-log-redeadline
  10913. (not (equal old-date
  10914. (substring org-last-inserted-timestamp 1 -1))))
  10915. (org-add-log-setup 'redeadline nil old-date 'findpos
  10916. org-log-redeadline))
  10917. (when repeater
  10918. (save-excursion
  10919. (org-back-to-heading t)
  10920. (when (re-search-forward (concat org-deadline-string " "
  10921. org-last-inserted-timestamp)
  10922. (save-excursion
  10923. (outline-next-heading) (point)) t)
  10924. (goto-char (1- (match-end 0)))
  10925. (insert " " repeater)
  10926. (setq org-last-inserted-timestamp
  10927. (concat (substring org-last-inserted-timestamp 0 -1)
  10928. " " repeater
  10929. (substring org-last-inserted-timestamp -1))))))
  10930. (message "Deadline on %s" org-last-inserted-timestamp)))))
  10931. (defun org-schedule (&optional remove time)
  10932. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  10933. With argument REMOVE, remove any scheduling date from the item.
  10934. With argument TIME, scheduled at the corresponding date. TIME can
  10935. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  10936. (interactive "P")
  10937. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  10938. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  10939. 'region-start-level 'region))
  10940. org-loop-over-headlines-in-active-region)
  10941. (org-map-entries
  10942. `(org-schedule ',remove ,time)
  10943. org-loop-over-headlines-in-active-region
  10944. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  10945. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  10946. (repeater (and old-date
  10947. (string-match
  10948. "\\([.+-]+[0-9]+[dwmy]\\(?:[/ ][-+]?[0-9]+[dwmy]\\)?\\) ?"
  10949. old-date)
  10950. (match-string 1 old-date))))
  10951. (if remove
  10952. (progn
  10953. (when (and old-date org-log-reschedule)
  10954. (org-add-log-setup 'delschedule nil old-date 'findpos
  10955. org-log-reschedule))
  10956. (org-remove-timestamp-with-keyword org-scheduled-string)
  10957. (message "Item is no longer scheduled."))
  10958. (org-add-planning-info 'scheduled time 'closed)
  10959. (when (and old-date org-log-reschedule
  10960. (not (equal old-date
  10961. (substring org-last-inserted-timestamp 1 -1))))
  10962. (org-add-log-setup 'reschedule nil old-date 'findpos
  10963. org-log-reschedule))
  10964. (when repeater
  10965. (save-excursion
  10966. (org-back-to-heading t)
  10967. (when (re-search-forward (concat org-scheduled-string " "
  10968. org-last-inserted-timestamp)
  10969. (save-excursion
  10970. (outline-next-heading) (point)) t)
  10971. (goto-char (1- (match-end 0)))
  10972. (insert " " repeater)
  10973. (setq org-last-inserted-timestamp
  10974. (concat (substring org-last-inserted-timestamp 0 -1)
  10975. " " repeater
  10976. (substring org-last-inserted-timestamp -1))))))
  10977. (message "Scheduled to %s" org-last-inserted-timestamp)))))
  10978. (defun org-get-scheduled-time (pom &optional inherit)
  10979. "Get the scheduled time as a time tuple, of a format suitable
  10980. for calling org-schedule with, or if there is no scheduling,
  10981. returns nil."
  10982. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  10983. (when time
  10984. (apply 'encode-time (org-parse-time-string time)))))
  10985. (defun org-get-deadline-time (pom &optional inherit)
  10986. "Get the deadline as a time tuple, of a format suitable for
  10987. calling org-deadline with, or if there is no scheduling, returns
  10988. nil."
  10989. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  10990. (when time
  10991. (apply 'encode-time (org-parse-time-string time)))))
  10992. (defun org-remove-timestamp-with-keyword (keyword)
  10993. "Remove all time stamps with KEYWORD in the current entry."
  10994. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  10995. beg)
  10996. (save-excursion
  10997. (org-back-to-heading t)
  10998. (setq beg (point))
  10999. (outline-next-heading)
  11000. (while (re-search-backward re beg t)
  11001. (replace-match "")
  11002. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  11003. (equal (char-before) ?\ ))
  11004. (backward-delete-char 1)
  11005. (if (string-match "^[ \t]*$" (buffer-substring
  11006. (point-at-bol) (point-at-eol)))
  11007. (delete-region (point-at-bol)
  11008. (min (point-max) (1+ (point-at-eol))))))))))
  11009. (defun org-add-planning-info (what &optional time &rest remove)
  11010. "Insert new timestamp with keyword in the line directly after the headline.
  11011. WHAT indicates what kind of time stamp to add. TIME indicates the time to use.
  11012. If non is given, the user is prompted for a date.
  11013. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  11014. be removed."
  11015. (interactive)
  11016. (let (org-time-was-given org-end-time-was-given ts
  11017. end default-time default-input)
  11018. (catch 'exit
  11019. (when (and (memq what '(scheduled deadline))
  11020. (or (not time)
  11021. (and (stringp time)
  11022. (string-match "^[-+]+[0-9]" time))))
  11023. ;; Try to get a default date/time from existing timestamp
  11024. (save-excursion
  11025. (org-back-to-heading t)
  11026. (setq end (save-excursion (outline-next-heading) (point)))
  11027. (when (re-search-forward (if (eq what 'scheduled)
  11028. org-scheduled-time-regexp
  11029. org-deadline-time-regexp)
  11030. end t)
  11031. (setq ts (match-string 1)
  11032. default-time
  11033. (apply 'encode-time (org-parse-time-string ts))
  11034. default-input (and ts (org-get-compact-tod ts))))))
  11035. (when what
  11036. (setq time
  11037. (if (stringp time)
  11038. ;; This is a string (relative or absolute), set proper date
  11039. (apply 'encode-time
  11040. (org-read-date-analyze
  11041. time default-time (decode-time default-time)))
  11042. ;; If necessary, get the time from the user
  11043. (or time (org-read-date nil 'to-time nil nil
  11044. default-time default-input)))))
  11045. (when (and org-insert-labeled-timestamps-at-point
  11046. (member what '(scheduled deadline)))
  11047. (insert
  11048. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  11049. (org-insert-time-stamp time org-time-was-given
  11050. nil nil nil (list org-end-time-was-given))
  11051. (setq what nil))
  11052. (save-excursion
  11053. (save-restriction
  11054. (let (col list elt ts buffer-invisibility-spec)
  11055. (org-back-to-heading t)
  11056. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
  11057. (goto-char (match-end 1))
  11058. (setq col (current-column))
  11059. (goto-char (match-end 0))
  11060. (if (eobp) (insert "\n") (forward-char 1))
  11061. (when (and (not what)
  11062. (not (looking-at
  11063. (concat "[ \t]*"
  11064. org-keyword-time-not-clock-regexp))))
  11065. ;; Nothing to add, nothing to remove...... :-)
  11066. (throw 'exit nil))
  11067. (if (and (not (looking-at org-outline-regexp))
  11068. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  11069. "[^\r\n]*"))
  11070. (not (equal (match-string 1) org-clock-string)))
  11071. (narrow-to-region (match-beginning 0) (match-end 0))
  11072. (insert-before-markers "\n")
  11073. (backward-char 1)
  11074. (narrow-to-region (point) (point))
  11075. (and org-adapt-indentation (org-indent-to-column col)))
  11076. ;; Check if we have to remove something.
  11077. (setq list (cons what remove))
  11078. (while list
  11079. (setq elt (pop list))
  11080. (when (or (and (eq elt 'scheduled)
  11081. (re-search-forward org-scheduled-time-regexp nil t))
  11082. (and (eq elt 'deadline)
  11083. (re-search-forward org-deadline-time-regexp nil t))
  11084. (and (eq elt 'closed)
  11085. (re-search-forward org-closed-time-regexp nil t)))
  11086. (replace-match "")
  11087. (if (looking-at "--+<[^>]+>") (replace-match ""))))
  11088. (and (looking-at "[ \t]+") (replace-match ""))
  11089. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  11090. (when what
  11091. (insert
  11092. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  11093. (cond ((eq what 'scheduled) org-scheduled-string)
  11094. ((eq what 'deadline) org-deadline-string)
  11095. ((eq what 'closed) org-closed-string))
  11096. " ")
  11097. (setq ts (org-insert-time-stamp
  11098. time
  11099. (or org-time-was-given
  11100. (and (eq what 'closed) org-log-done-with-time))
  11101. (eq what 'closed)
  11102. nil nil (list org-end-time-was-given)))
  11103. (insert
  11104. (if (not (or (bolp) (eq (char-before) ?\ )
  11105. (memq (char-after) '(32 10))
  11106. (eobp))) " " ""))
  11107. (end-of-line 1))
  11108. (goto-char (point-min))
  11109. (widen)
  11110. (if (and (looking-at "[ \t]*\n")
  11111. (equal (char-before) ?\n))
  11112. (delete-region (1- (point)) (point-at-eol)))
  11113. ts))))))
  11114. (defvar org-log-note-marker (make-marker))
  11115. (defvar org-log-note-purpose nil)
  11116. (defvar org-log-note-state nil)
  11117. (defvar org-log-note-previous-state nil)
  11118. (defvar org-log-note-how nil)
  11119. (defvar org-log-note-extra nil)
  11120. (defvar org-log-note-window-configuration nil)
  11121. (defvar org-log-note-return-to (make-marker))
  11122. (defvar org-log-note-effective-time nil
  11123. "Remembered current time so that dynamically scoped
  11124. `org-extend-today-until' affects tha timestamps in state change
  11125. log")
  11126. (defvar org-log-post-message nil
  11127. "Message to be displayed after a log note has been stored.
  11128. The auto-repeater uses this.")
  11129. (defun org-add-note ()
  11130. "Add a note to the current entry.
  11131. This is done in the same way as adding a state change note."
  11132. (interactive)
  11133. (org-add-log-setup 'note nil nil 'findpos nil))
  11134. (defvar org-property-end-re)
  11135. (defun org-add-log-setup (&optional purpose state prev-state
  11136. findpos how extra)
  11137. "Set up the post command hook to take a note.
  11138. If this is about to TODO state change, the new state is expected in STATE.
  11139. When FINDPOS is non-nil, find the correct position for the note in
  11140. the current entry. If not, assume that it can be inserted at point.
  11141. HOW is an indicator what kind of note should be created.
  11142. EXTRA is additional text that will be inserted into the notes buffer."
  11143. (let* ((org-log-into-drawer (org-log-into-drawer))
  11144. (drawer (cond ((stringp org-log-into-drawer)
  11145. org-log-into-drawer)
  11146. (org-log-into-drawer "LOGBOOK")
  11147. (t nil))))
  11148. (save-restriction
  11149. (save-excursion
  11150. (when findpos
  11151. (org-back-to-heading t)
  11152. (narrow-to-region (point) (save-excursion
  11153. (outline-next-heading) (point)))
  11154. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"
  11155. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  11156. "[^\r\n]*\\)?"))
  11157. (goto-char (match-end 0))
  11158. (cond
  11159. (drawer
  11160. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  11161. nil t)
  11162. (progn
  11163. (goto-char (match-end 0))
  11164. (or org-log-states-order-reversed
  11165. (and (re-search-forward org-property-end-re nil t)
  11166. (goto-char (1- (match-beginning 0))))))
  11167. (insert "\n:" drawer ":\n:END:")
  11168. (beginning-of-line 0)
  11169. (org-indent-line-function)
  11170. (beginning-of-line 2)
  11171. (org-indent-line-function)
  11172. (end-of-line 0)))
  11173. ((and org-log-state-notes-insert-after-drawers
  11174. (save-excursion
  11175. (forward-line) (looking-at org-drawer-regexp)))
  11176. (forward-line)
  11177. (while (looking-at org-drawer-regexp)
  11178. (goto-char (match-end 0))
  11179. (re-search-forward org-property-end-re (point-max) t)
  11180. (forward-line))
  11181. (forward-line -1)))
  11182. (unless org-log-states-order-reversed
  11183. (and (= (char-after) ?\n) (forward-char 1))
  11184. (org-skip-over-state-notes)
  11185. (skip-chars-backward " \t\n\r")))
  11186. (move-marker org-log-note-marker (point))
  11187. (setq org-log-note-purpose purpose
  11188. org-log-note-state state
  11189. org-log-note-previous-state prev-state
  11190. org-log-note-how how
  11191. org-log-note-extra extra
  11192. org-log-note-effective-time (org-current-effective-time))
  11193. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  11194. (defun org-skip-over-state-notes ()
  11195. "Skip past the list of State notes in an entry."
  11196. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  11197. (when (ignore-errors (goto-char (org-in-item-p)))
  11198. (let* ((struct (org-list-struct))
  11199. (prevs (org-list-prevs-alist struct)))
  11200. (while (looking-at "[ \t]*- State")
  11201. (goto-char (or (org-list-get-next-item (point) struct prevs)
  11202. (org-list-get-item-end (point) struct)))))))
  11203. (defun org-add-log-note (&optional purpose)
  11204. "Pop up a window for taking a note, and add this note later at point."
  11205. (remove-hook 'post-command-hook 'org-add-log-note)
  11206. (setq org-log-note-window-configuration (current-window-configuration))
  11207. (delete-other-windows)
  11208. (move-marker org-log-note-return-to (point))
  11209. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  11210. (goto-char org-log-note-marker)
  11211. (org-switch-to-buffer-other-window "*Org Note*")
  11212. (erase-buffer)
  11213. (if (memq org-log-note-how '(time state))
  11214. (let (current-prefix-arg) (org-store-log-note))
  11215. (let ((org-inhibit-startup t)) (org-mode))
  11216. (insert (format "# Insert note for %s.
  11217. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  11218. (cond
  11219. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  11220. ((eq org-log-note-purpose 'done) "closed todo item")
  11221. ((eq org-log-note-purpose 'state)
  11222. (format "state change from \"%s\" to \"%s\""
  11223. (or org-log-note-previous-state "")
  11224. (or org-log-note-state "")))
  11225. ((eq org-log-note-purpose 'reschedule)
  11226. "rescheduling")
  11227. ((eq org-log-note-purpose 'delschedule)
  11228. "no longer scheduled")
  11229. ((eq org-log-note-purpose 'redeadline)
  11230. "changing deadline")
  11231. ((eq org-log-note-purpose 'deldeadline)
  11232. "removing deadline")
  11233. ((eq org-log-note-purpose 'refile)
  11234. "refiling")
  11235. ((eq org-log-note-purpose 'note)
  11236. "this entry")
  11237. (t (error "This should not happen")))))
  11238. (if org-log-note-extra (insert org-log-note-extra))
  11239. (org-set-local 'org-finish-function 'org-store-log-note)
  11240. (run-hooks 'org-log-buffer-setup-hook)))
  11241. (defvar org-note-abort nil) ; dynamically scoped
  11242. (defun org-store-log-note ()
  11243. "Finish taking a log note, and insert it to where it belongs."
  11244. (let ((txt (buffer-string))
  11245. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  11246. lines ind bul)
  11247. (kill-buffer (current-buffer))
  11248. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  11249. (setq txt (replace-match "" t t txt)))
  11250. (if (string-match "\\s-+\\'" txt)
  11251. (setq txt (replace-match "" t t txt)))
  11252. (setq lines (org-split-string txt "\n"))
  11253. (when (and note (string-match "\\S-" note))
  11254. (setq note
  11255. (org-replace-escapes
  11256. note
  11257. (list (cons "%u" (user-login-name))
  11258. (cons "%U" user-full-name)
  11259. (cons "%t" (format-time-string
  11260. (org-time-stamp-format 'long 'inactive)
  11261. org-log-note-effective-time))
  11262. (cons "%T" (format-time-string
  11263. (org-time-stamp-format 'long nil)
  11264. org-log-note-effective-time))
  11265. (cons "%d" (format-time-string
  11266. (org-time-stamp-format nil 'inactive)
  11267. org-log-note-effective-time))
  11268. (cons "%D" (format-time-string
  11269. (org-time-stamp-format nil nil)
  11270. org-log-note-effective-time))
  11271. (cons "%s" (if org-log-note-state
  11272. (concat "\"" org-log-note-state "\"")
  11273. ""))
  11274. (cons "%S" (if org-log-note-previous-state
  11275. (concat "\"" org-log-note-previous-state "\"")
  11276. "\"\"")))))
  11277. (if lines (setq note (concat note " \\\\")))
  11278. (push note lines))
  11279. (when (or current-prefix-arg org-note-abort)
  11280. (when org-log-into-drawer
  11281. (org-remove-empty-drawer-at
  11282. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  11283. org-log-note-marker))
  11284. (setq lines nil))
  11285. (when lines
  11286. (with-current-buffer (marker-buffer org-log-note-marker)
  11287. (save-excursion
  11288. (goto-char org-log-note-marker)
  11289. (move-marker org-log-note-marker nil)
  11290. (end-of-line 1)
  11291. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  11292. (setq ind (save-excursion
  11293. (if (ignore-errors (goto-char (org-in-item-p)))
  11294. (let ((struct (org-list-struct)))
  11295. (org-list-get-ind
  11296. (org-list-get-top-point struct) struct))
  11297. (skip-chars-backward " \r\t\n")
  11298. (cond
  11299. ((and (org-at-heading-p)
  11300. org-adapt-indentation)
  11301. (1+ (org-current-level)))
  11302. ((org-at-heading-p) 0)
  11303. (t (org-get-indentation))))))
  11304. (setq bul (org-list-bullet-string "-"))
  11305. (org-indent-line-to ind)
  11306. (insert bul (pop lines))
  11307. (let ((ind-body (+ (length bul) ind)))
  11308. (while lines
  11309. (insert "\n")
  11310. (org-indent-line-to ind-body)
  11311. (insert (pop lines))))
  11312. (message "Note stored")
  11313. (org-back-to-heading t)
  11314. (org-cycle-hide-drawers 'children)))))
  11315. (set-window-configuration org-log-note-window-configuration)
  11316. (with-current-buffer (marker-buffer org-log-note-return-to)
  11317. (goto-char org-log-note-return-to))
  11318. (move-marker org-log-note-return-to nil)
  11319. (and org-log-post-message (message "%s" org-log-post-message)))
  11320. (defun org-remove-empty-drawer-at (drawer pos)
  11321. "Remove an empty drawer DRAWER at position POS.
  11322. POS may also be a marker."
  11323. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  11324. (save-excursion
  11325. (save-restriction
  11326. (widen)
  11327. (goto-char pos)
  11328. (if (org-in-regexp
  11329. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  11330. (replace-match ""))))))
  11331. (defun org-sparse-tree (&optional arg)
  11332. "Create a sparse tree, prompt for the details.
  11333. This command can create sparse trees. You first need to select the type
  11334. of match used to create the tree:
  11335. t Show all TODO entries.
  11336. T Show entries with a specific TODO keyword.
  11337. m Show entries selected by a tags/property match.
  11338. p Enter a property name and its value (both with completion on existing
  11339. names/values) and show entries with that property.
  11340. r Show entries matching a regular expression (`/' can be used as well)
  11341. d Show deadlines due within `org-deadline-warning-days'.
  11342. b Show deadlines and scheduled items before a date.
  11343. a Show deadlines and scheduled items after a date."
  11344. (interactive "P")
  11345. (let (ans kwd value)
  11346. (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")
  11347. (setq ans (read-char-exclusive))
  11348. (cond
  11349. ((equal ans ?d)
  11350. (call-interactively 'org-check-deadlines))
  11351. ((equal ans ?b)
  11352. (call-interactively 'org-check-before-date))
  11353. ((equal ans ?a)
  11354. (call-interactively 'org-check-after-date))
  11355. ((equal ans ?D)
  11356. (call-interactively 'org-check-dates-range))
  11357. ((equal ans ?t)
  11358. (org-show-todo-tree nil))
  11359. ((equal ans ?T)
  11360. (org-show-todo-tree '(4)))
  11361. ((member ans '(?T ?m))
  11362. (call-interactively 'org-match-sparse-tree))
  11363. ((member ans '(?p ?P))
  11364. (setq kwd (org-icompleting-read "Property: "
  11365. (mapcar 'list (org-buffer-property-keys))))
  11366. (setq value (org-icompleting-read "Value: "
  11367. (mapcar 'list (org-property-values kwd))))
  11368. (unless (string-match "\\`{.*}\\'" value)
  11369. (setq value (concat "\"" value "\"")))
  11370. (org-match-sparse-tree arg (concat kwd "=" value)))
  11371. ((member ans '(?r ?R ?/))
  11372. (call-interactively 'org-occur))
  11373. (t (error "No such sparse tree command \"%c\"" ans)))))
  11374. (defvar org-occur-highlights nil
  11375. "List of overlays used for occur matches.")
  11376. (make-variable-buffer-local 'org-occur-highlights)
  11377. (defvar org-occur-parameters nil
  11378. "Parameters of the active org-occur calls.
  11379. This is a list, each call to org-occur pushes as cons cell,
  11380. containing the regular expression and the callback, onto the list.
  11381. The list can contain several entries if `org-occur' has been called
  11382. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  11383. will only contain one set of parameters. When the highlights are
  11384. removed (for example with `C-c C-c', or with the next edit (depending
  11385. on `org-remove-highlights-with-change'), this variable is emptied
  11386. as well.")
  11387. (make-variable-buffer-local 'org-occur-parameters)
  11388. (defun org-occur (regexp &optional keep-previous callback)
  11389. "Make a compact tree which shows all matches of REGEXP.
  11390. The tree will show the lines where the regexp matches, and all higher
  11391. headlines above the match. It will also show the heading after the match,
  11392. to make sure editing the matching entry is easy.
  11393. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  11394. call to `org-occur' will be kept, to allow stacking of calls to this
  11395. command.
  11396. If CALLBACK is non-nil, it is a function which is called to confirm
  11397. that the match should indeed be shown."
  11398. (interactive "sRegexp: \nP")
  11399. (when (equal regexp "")
  11400. (error "Regexp cannot be empty"))
  11401. (unless keep-previous
  11402. (org-remove-occur-highlights nil nil t))
  11403. (push (cons regexp callback) org-occur-parameters)
  11404. (let ((cnt 0))
  11405. (save-excursion
  11406. (goto-char (point-min))
  11407. (if (or (not keep-previous) ; do not want to keep
  11408. (not org-occur-highlights)) ; no previous matches
  11409. ;; hide everything
  11410. (org-overview))
  11411. (while (re-search-forward regexp nil t)
  11412. (when (or (not callback)
  11413. (save-match-data (funcall callback)))
  11414. (setq cnt (1+ cnt))
  11415. (when org-highlight-sparse-tree-matches
  11416. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  11417. (org-show-context 'occur-tree))))
  11418. (when org-remove-highlights-with-change
  11419. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  11420. nil 'local))
  11421. (unless org-sparse-tree-open-archived-trees
  11422. (org-hide-archived-subtrees (point-min) (point-max)))
  11423. (run-hooks 'org-occur-hook)
  11424. (if (org-called-interactively-p 'interactive)
  11425. (message "%d match(es) for regexp %s" cnt regexp))
  11426. cnt))
  11427. (defun org-occur-next-match (&optional n reset)
  11428. "Function for `next-error-function' to find sparse tree matches.
  11429. N is the number of matches to move, when negative move backwards.
  11430. RESET is entirely ignored - this function always goes back to the
  11431. starting point when no match is found."
  11432. (let* ((limit (if (< n 0) (point-min) (point-max)))
  11433. (search-func (if (< n 0)
  11434. 'previous-single-char-property-change
  11435. 'next-single-char-property-change))
  11436. (n (abs n))
  11437. (pos (point))
  11438. p1)
  11439. (catch 'exit
  11440. (while (setq p1 (funcall search-func (point) 'org-type))
  11441. (when (equal p1 limit)
  11442. (goto-char pos)
  11443. (error "No more matches"))
  11444. (when (equal (get-char-property p1 'org-type) 'org-occur)
  11445. (setq n (1- n))
  11446. (when (= n 0)
  11447. (goto-char p1)
  11448. (throw 'exit (point))))
  11449. (goto-char p1))
  11450. (goto-char p1)
  11451. (error "No more matches"))))
  11452. (defun org-show-context (&optional key)
  11453. "Make sure point and context are visible.
  11454. How much context is shown depends upon the variables
  11455. `org-show-hierarchy-above', `org-show-following-heading',
  11456. `org-show-entry-below' and `org-show-siblings'."
  11457. (let ((heading-p (org-at-heading-p t))
  11458. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  11459. (following-p (org-get-alist-option org-show-following-heading key))
  11460. (entry-p (org-get-alist-option org-show-entry-below key))
  11461. (siblings-p (org-get-alist-option org-show-siblings key)))
  11462. (catch 'exit
  11463. ;; Show heading or entry text
  11464. (if (and heading-p (not entry-p))
  11465. (org-flag-heading nil) ; only show the heading
  11466. (and (or entry-p (outline-invisible-p) (org-invisible-p2))
  11467. (org-show-hidden-entry))) ; show entire entry
  11468. (when following-p
  11469. ;; Show next sibling, or heading below text
  11470. (save-excursion
  11471. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  11472. (org-flag-heading nil))))
  11473. (when siblings-p (org-show-siblings))
  11474. (when hierarchy-p
  11475. ;; show all higher headings, possibly with siblings
  11476. (save-excursion
  11477. (while (and (condition-case nil
  11478. (progn (org-up-heading-all 1) t)
  11479. (error nil))
  11480. (not (bobp)))
  11481. (org-flag-heading nil)
  11482. (when siblings-p (org-show-siblings))))))))
  11483. (defvar org-reveal-start-hook nil
  11484. "Hook run before revealing a location.")
  11485. (defun org-reveal (&optional siblings)
  11486. "Show current entry, hierarchy above it, and the following headline.
  11487. This can be used to show a consistent set of context around locations
  11488. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  11489. not t for the search context.
  11490. With optional argument SIBLINGS, on each level of the hierarchy all
  11491. siblings are shown. This repairs the tree structure to what it would
  11492. look like when opened with hierarchical calls to `org-cycle'.
  11493. With double optional argument \\[universal-argument] \\[universal-argument], \
  11494. go to the parent and show the
  11495. entire tree."
  11496. (interactive "P")
  11497. (run-hooks 'org-reveal-start-hook)
  11498. (let ((org-show-hierarchy-above t)
  11499. (org-show-following-heading t)
  11500. (org-show-siblings (if siblings t org-show-siblings)))
  11501. (org-show-context nil))
  11502. (when (equal siblings '(16))
  11503. (save-excursion
  11504. (when (org-up-heading-safe)
  11505. (org-show-subtree)
  11506. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  11507. (defun org-highlight-new-match (beg end)
  11508. "Highlight from BEG to END and mark the highlight is an occur headline."
  11509. (let ((ov (make-overlay beg end)))
  11510. (overlay-put ov 'face 'secondary-selection)
  11511. (overlay-put ov 'org-type 'org-occur)
  11512. (push ov org-occur-highlights)))
  11513. (defun org-remove-occur-highlights (&optional beg end noremove)
  11514. "Remove the occur highlights from the buffer.
  11515. BEG and END are ignored. If NOREMOVE is nil, remove this function
  11516. from the `before-change-functions' in the current buffer."
  11517. (interactive)
  11518. (unless org-inhibit-highlight-removal
  11519. (mapc 'delete-overlay org-occur-highlights)
  11520. (setq org-occur-highlights nil)
  11521. (setq org-occur-parameters nil)
  11522. (unless noremove
  11523. (remove-hook 'before-change-functions
  11524. 'org-remove-occur-highlights 'local))))
  11525. ;;;; Priorities
  11526. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  11527. "Regular expression matching the priority indicator.")
  11528. (defvar org-remove-priority-next-time nil)
  11529. (defun org-priority-up ()
  11530. "Increase the priority of the current item."
  11531. (interactive)
  11532. (org-priority 'up))
  11533. (defun org-priority-down ()
  11534. "Decrease the priority of the current item."
  11535. (interactive)
  11536. (org-priority 'down))
  11537. (defun org-priority (&optional action)
  11538. "Change the priority of an item by ARG.
  11539. ACTION can be `set', `up', `down', or a character."
  11540. (interactive)
  11541. (unless org-enable-priority-commands
  11542. (error "Priority commands are disabled"))
  11543. (setq action (or action 'set))
  11544. (let (current new news have remove)
  11545. (save-excursion
  11546. (org-back-to-heading t)
  11547. (if (looking-at org-priority-regexp)
  11548. (setq current (string-to-char (match-string 2))
  11549. have t))
  11550. (cond
  11551. ((eq action 'remove)
  11552. (setq remove t new ?\ ))
  11553. ((or (eq action 'set)
  11554. (if (featurep 'xemacs) (characterp action) (integerp action)))
  11555. (if (not (eq action 'set))
  11556. (setq new action)
  11557. (message "Priority %c-%c, SPC to remove: "
  11558. org-highest-priority org-lowest-priority)
  11559. (save-match-data
  11560. (setq new (read-char-exclusive))))
  11561. (if (and (= (upcase org-highest-priority) org-highest-priority)
  11562. (= (upcase org-lowest-priority) org-lowest-priority))
  11563. (setq new (upcase new)))
  11564. (cond ((equal new ?\ ) (setq remove t))
  11565. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  11566. (error "Priority must be between `%c' and `%c'"
  11567. org-highest-priority org-lowest-priority))))
  11568. ((eq action 'up)
  11569. (setq new (if have
  11570. (1- current) ; normal cycling
  11571. ;; last priority was empty
  11572. (if (eq last-command this-command)
  11573. org-lowest-priority ; wrap around empty to lowest
  11574. ;; default
  11575. (if org-priority-start-cycle-with-default
  11576. org-default-priority
  11577. (1- org-default-priority))))))
  11578. ((eq action 'down)
  11579. (setq new (if have
  11580. (1+ current) ; normal cycling
  11581. ;; last priority was empty
  11582. (if (eq last-command this-command)
  11583. org-highest-priority ; wrap around empty to highest
  11584. ;; default
  11585. (if org-priority-start-cycle-with-default
  11586. org-default-priority
  11587. (1+ org-default-priority))))))
  11588. (t (error "Invalid action")))
  11589. (if (or (< (upcase new) org-highest-priority)
  11590. (> (upcase new) org-lowest-priority))
  11591. (if (and (memq action '(up down))
  11592. (not have) (not (eq last-command this-command)))
  11593. ;; `new' is from default priority
  11594. (error
  11595. "The default can not be set, see `org-default-priority' why")
  11596. ;; normal cycling: `new' is beyond highest/lowest priority
  11597. ;; and is wrapped around to the empty priority
  11598. (setq remove t)))
  11599. (setq news (format "%c" new))
  11600. (if have
  11601. (if remove
  11602. (replace-match "" t t nil 1)
  11603. (replace-match news t t nil 2))
  11604. (if remove
  11605. (error "No priority cookie found in line")
  11606. (let ((case-fold-search nil))
  11607. (looking-at org-todo-line-regexp))
  11608. (if (match-end 2)
  11609. (progn
  11610. (goto-char (match-end 2))
  11611. (insert " [#" news "]"))
  11612. (goto-char (match-beginning 3))
  11613. (insert "[#" news "] "))))
  11614. (org-preserve-lc (org-set-tags nil 'align)))
  11615. (if remove
  11616. (message "Priority removed")
  11617. (message "Priority of current item set to %s" news))))
  11618. (defun org-get-priority (s)
  11619. "Find priority cookie and return priority."
  11620. (if (functionp org-get-priority-function)
  11621. (funcall org-get-priority-function)
  11622. (save-match-data
  11623. (if (not (string-match org-priority-regexp s))
  11624. (* 1000 (- org-lowest-priority org-default-priority))
  11625. (* 1000 (- org-lowest-priority
  11626. (string-to-char (match-string 2 s))))))))
  11627. ;;;; Tags
  11628. (defvar org-agenda-archives-mode)
  11629. (defvar org-map-continue-from nil
  11630. "Position from where mapping should continue.
  11631. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  11632. (defvar org-scanner-tags nil
  11633. "The current tag list while the tags scanner is running.")
  11634. (defvar org-trust-scanner-tags nil
  11635. "Should `org-get-tags-at' use the tags for the scanner.
  11636. This is for internal dynamical scoping only.
  11637. When this is non-nil, the function `org-get-tags-at' will return the value
  11638. of `org-scanner-tags' instead of building the list by itself. This
  11639. can lead to large speed-ups when the tags scanner is used in a file with
  11640. many entries, and when the list of tags is retrieved, for example to
  11641. obtain a list of properties. Building the tags list for each entry in such
  11642. a file becomes an N^2 operation - but with this variable set, it scales
  11643. as N.")
  11644. (defun org-scan-tags (action matcher &optional todo-only start-level)
  11645. "Scan headline tags with inheritance and produce output ACTION.
  11646. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  11647. or `agenda' to produce an entry list for an agenda view. It can also be
  11648. a Lisp form or a function that should be called at each matched headline, in
  11649. this case the return value is a list of all return values from these calls.
  11650. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  11651. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  11652. only lines with a TODO keyword are included in the output.
  11653. START-LEVEL can be a string with asterisks, reducing the scope to
  11654. headlines matching this string."
  11655. (require 'org-agenda)
  11656. (let* ((re (concat "^"
  11657. (if start-level
  11658. ;; Get the correct level to match
  11659. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  11660. org-outline-regexp)
  11661. " *\\(\\<\\("
  11662. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  11663. (org-re
  11664. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  11665. (props (list 'face 'default
  11666. 'done-face 'org-agenda-done
  11667. 'undone-face 'default
  11668. 'mouse-face 'highlight
  11669. 'org-not-done-regexp org-not-done-regexp
  11670. 'org-todo-regexp org-todo-regexp
  11671. 'org-complex-heading-regexp org-complex-heading-regexp
  11672. 'help-echo
  11673. (format "mouse-2 or RET jump to org file %s"
  11674. (abbreviate-file-name
  11675. (or (buffer-file-name (buffer-base-buffer))
  11676. (buffer-name (buffer-base-buffer)))))))
  11677. (case-fold-search nil)
  11678. (org-map-continue-from nil)
  11679. lspos tags
  11680. (tags-alist (list (cons 0 org-file-tags)))
  11681. (llast 0) rtn rtn1 level category i txt
  11682. todo marker entry priority)
  11683. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  11684. (setq action (list 'lambda nil action)))
  11685. (save-excursion
  11686. (goto-char (point-min))
  11687. (when (eq action 'sparse-tree)
  11688. (org-overview)
  11689. (org-remove-occur-highlights))
  11690. (while (re-search-forward re nil t)
  11691. (setq org-map-continue-from nil)
  11692. (catch :skip
  11693. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  11694. tags (if (match-end 4) (org-match-string-no-properties 4)))
  11695. (goto-char (setq lspos (match-beginning 0)))
  11696. (setq level (org-reduced-level (funcall outline-level))
  11697. category (org-get-category))
  11698. (setq i llast llast level)
  11699. ;; remove tag lists from same and sublevels
  11700. (while (>= i level)
  11701. (when (setq entry (assoc i tags-alist))
  11702. (setq tags-alist (delete entry tags-alist)))
  11703. (setq i (1- i)))
  11704. ;; add the next tags
  11705. (when tags
  11706. (setq tags (org-split-string tags ":")
  11707. tags-alist
  11708. (cons (cons level tags) tags-alist)))
  11709. ;; compile tags for current headline
  11710. (setq tags-list
  11711. (if org-use-tag-inheritance
  11712. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  11713. tags)
  11714. org-scanner-tags tags-list)
  11715. (when org-use-tag-inheritance
  11716. (setcdr (car tags-alist)
  11717. (mapcar (lambda (x)
  11718. (setq x (copy-sequence x))
  11719. (org-add-prop-inherited x))
  11720. (cdar tags-alist))))
  11721. (when (and tags org-use-tag-inheritance
  11722. (or (not (eq t org-use-tag-inheritance))
  11723. org-tags-exclude-from-inheritance))
  11724. ;; selective inheritance, remove uninherited ones
  11725. (setcdr (car tags-alist)
  11726. (org-remove-uninherited-tags (cdar tags-alist))))
  11727. (when (and
  11728. ;; eval matcher only when the todo condition is OK
  11729. (and (or (not todo-only) (member todo org-not-done-keywords))
  11730. (let ((case-fold-search t)) (eval matcher)))
  11731. ;; Call the skipper, but return t if it does not skip,
  11732. ;; so that the `and' form continues evaluating
  11733. (progn
  11734. (unless (eq action 'sparse-tree) (org-agenda-skip))
  11735. t)
  11736. ;; Check if timestamps are deselecting this entry
  11737. (or (not todo-only)
  11738. (and (member todo org-not-done-keywords)
  11739. (or (not org-agenda-tags-todo-honor-ignore-options)
  11740. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  11741. ;; Extra check for the archive tag
  11742. ;; FIXME: Does the skipper already do this????
  11743. (or
  11744. (not (member org-archive-tag tags-list))
  11745. ;; we have an archive tag, should we use this anyway?
  11746. (or (not org-agenda-skip-archived-trees)
  11747. (and (eq action 'agenda) org-agenda-archives-mode))))
  11748. ;; select this headline
  11749. (cond
  11750. ((eq action 'sparse-tree)
  11751. (and org-highlight-sparse-tree-matches
  11752. (org-get-heading) (match-end 0)
  11753. (org-highlight-new-match
  11754. (match-beginning 1) (match-end 1)))
  11755. (org-show-context 'tags-tree))
  11756. ((eq action 'agenda)
  11757. (setq txt (org-agenda-format-item
  11758. ""
  11759. (concat
  11760. (if (eq org-tags-match-list-sublevels 'indented)
  11761. (make-string (1- level) ?.) "")
  11762. (org-get-heading))
  11763. category
  11764. tags-list)
  11765. priority (org-get-priority txt))
  11766. (goto-char lspos)
  11767. (setq marker (org-agenda-new-marker))
  11768. (org-add-props txt props
  11769. 'org-marker marker 'org-hd-marker marker 'org-category category
  11770. 'todo-state todo
  11771. 'priority priority 'type "tagsmatch")
  11772. (push txt rtn))
  11773. ((functionp action)
  11774. (setq org-map-continue-from nil)
  11775. (save-excursion
  11776. (setq rtn1 (funcall action))
  11777. (push rtn1 rtn)))
  11778. (t (error "Invalid action")))
  11779. ;; if we are to skip sublevels, jump to end of subtree
  11780. (unless org-tags-match-list-sublevels
  11781. (org-end-of-subtree t)
  11782. (backward-char 1))))
  11783. ;; Get the correct position from where to continue
  11784. (if org-map-continue-from
  11785. (goto-char org-map-continue-from)
  11786. (and (= (point) lspos) (end-of-line 1)))))
  11787. (when (and (eq action 'sparse-tree)
  11788. (not org-sparse-tree-open-archived-trees))
  11789. (org-hide-archived-subtrees (point-min) (point-max)))
  11790. (nreverse rtn)))
  11791. (defun org-remove-uninherited-tags (tags)
  11792. "Remove all tags that are not inherited from the list TAGS."
  11793. (cond
  11794. ((eq org-use-tag-inheritance t)
  11795. (if org-tags-exclude-from-inheritance
  11796. (org-delete-all org-tags-exclude-from-inheritance tags)
  11797. tags))
  11798. ((not org-use-tag-inheritance) nil)
  11799. ((stringp org-use-tag-inheritance)
  11800. (delq nil (mapcar
  11801. (lambda (x)
  11802. (if (and (string-match org-use-tag-inheritance x)
  11803. (not (member x org-tags-exclude-from-inheritance)))
  11804. x nil))
  11805. tags)))
  11806. ((listp org-use-tag-inheritance)
  11807. (delq nil (mapcar
  11808. (lambda (x)
  11809. (if (member x org-use-tag-inheritance) x nil))
  11810. tags)))))
  11811. (defvar todo-only) ;; dynamically scoped
  11812. (defun org-match-sparse-tree (&optional todo-only match)
  11813. "Create a sparse tree according to tags string MATCH.
  11814. MATCH can contain positive and negative selection of tags, like
  11815. \"+WORK+URGENT-WITHBOSS\".
  11816. If optional argument TODO-ONLY is non-nil, only select lines that are
  11817. also TODO lines."
  11818. (interactive "P")
  11819. (org-prepare-agenda-buffers (list (current-buffer)))
  11820. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  11821. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  11822. (defvar org-cached-props nil)
  11823. (defun org-cached-entry-get (pom property)
  11824. (if (or (eq t org-use-property-inheritance)
  11825. (and (stringp org-use-property-inheritance)
  11826. (string-match org-use-property-inheritance property))
  11827. (and (listp org-use-property-inheritance)
  11828. (member property org-use-property-inheritance)))
  11829. ;; Caching is not possible, check it directly
  11830. (org-entry-get pom property 'inherit)
  11831. ;; Get all properties, so that we can do complicated checks easily
  11832. (cdr (assoc property (or org-cached-props
  11833. (setq org-cached-props
  11834. (org-entry-properties pom)))))))
  11835. (defun org-global-tags-completion-table (&optional files)
  11836. "Return the list of all tags in all agenda buffer/files.
  11837. Optional FILES argument is a list of files to which can be used
  11838. instead of the agenda files."
  11839. (save-excursion
  11840. (org-uniquify
  11841. (delq nil
  11842. (apply 'append
  11843. (mapcar
  11844. (lambda (file)
  11845. (set-buffer (find-file-noselect file))
  11846. (append (org-get-buffer-tags)
  11847. (mapcar (lambda (x) (if (stringp (car-safe x))
  11848. (list (car-safe x)) nil))
  11849. org-tag-alist)))
  11850. (if (and files (car files))
  11851. files
  11852. (org-agenda-files))))))))
  11853. (defun org-make-tags-matcher (match)
  11854. "Create the TAGS/TODO matcher form for the selection string MATCH."
  11855. ;; todo-only is scoped dynamically into this function, and the function
  11856. ;; may change it if the matcher asks for it.
  11857. (unless match
  11858. ;; Get a new match request, with completion
  11859. (let ((org-last-tags-completion-table
  11860. (org-global-tags-completion-table)))
  11861. (setq match (org-completing-read-no-i
  11862. "Match: " 'org-tags-completion-function nil nil nil
  11863. 'org-tags-history))))
  11864. ;; Parse the string and create a lisp form
  11865. (let ((match0 match)
  11866. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  11867. minus tag mm
  11868. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  11869. orterms term orlist re-p str-p level-p level-op time-p
  11870. prop-p pn pv po gv rest)
  11871. (if (string-match "/+" match)
  11872. ;; match contains also a todo-matching request
  11873. (progn
  11874. (setq tagsmatch (substring match 0 (match-beginning 0))
  11875. todomatch (substring match (match-end 0)))
  11876. (if (string-match "^!" todomatch)
  11877. (setq todo-only t todomatch (substring todomatch 1)))
  11878. (if (string-match "^\\s-*$" todomatch)
  11879. (setq todomatch nil)))
  11880. ;; only matching tags
  11881. (setq tagsmatch match todomatch nil))
  11882. ;; Make the tags matcher
  11883. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  11884. (setq tagsmatcher t)
  11885. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  11886. (while (setq term (pop orterms))
  11887. (while (and (equal (substring term -1) "\\") orterms)
  11888. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  11889. (while (string-match re term)
  11890. (setq rest (substring term (match-end 0))
  11891. minus (and (match-end 1)
  11892. (equal (match-string 1 term) "-"))
  11893. tag (save-match-data (replace-regexp-in-string
  11894. "\\\\-" "-"
  11895. (match-string 2 term)))
  11896. re-p (equal (string-to-char tag) ?{)
  11897. level-p (match-end 4)
  11898. prop-p (match-end 5)
  11899. mm (cond
  11900. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  11901. (level-p
  11902. (setq level-op (org-op-to-function (match-string 3 term)))
  11903. `(,level-op level ,(string-to-number
  11904. (match-string 4 term))))
  11905. (prop-p
  11906. (setq pn (match-string 5 term)
  11907. po (match-string 6 term)
  11908. pv (match-string 7 term)
  11909. re-p (equal (string-to-char pv) ?{)
  11910. str-p (equal (string-to-char pv) ?\")
  11911. time-p (save-match-data
  11912. (string-match "^\"[[<].*[]>]\"$" pv))
  11913. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  11914. (if time-p (setq pv (org-matcher-time pv)))
  11915. (setq po (org-op-to-function po (if time-p 'time str-p)))
  11916. (cond
  11917. ((equal pn "CATEGORY")
  11918. (setq gv '(get-text-property (point) 'org-category)))
  11919. ((equal pn "TODO")
  11920. (setq gv 'todo))
  11921. (t
  11922. (setq gv `(org-cached-entry-get nil ,pn))))
  11923. (if re-p
  11924. (if (eq po 'org<>)
  11925. `(not (string-match ,pv (or ,gv "")))
  11926. `(string-match ,pv (or ,gv "")))
  11927. (if str-p
  11928. `(,po (or ,gv "") ,pv)
  11929. `(,po (string-to-number (or ,gv ""))
  11930. ,(string-to-number pv) ))))
  11931. (t `(member ,tag tags-list)))
  11932. mm (if minus (list 'not mm) mm)
  11933. term rest)
  11934. (push mm tagsmatcher))
  11935. (push (if (> (length tagsmatcher) 1)
  11936. (cons 'and tagsmatcher)
  11937. (car tagsmatcher))
  11938. orlist)
  11939. (setq tagsmatcher nil))
  11940. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  11941. (setq tagsmatcher
  11942. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  11943. ;; Make the todo matcher
  11944. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  11945. (setq todomatcher t)
  11946. (setq orterms (org-split-string todomatch "|") orlist nil)
  11947. (while (setq term (pop orterms))
  11948. (while (string-match re term)
  11949. (setq minus (and (match-end 1)
  11950. (equal (match-string 1 term) "-"))
  11951. kwd (match-string 2 term)
  11952. re-p (equal (string-to-char kwd) ?{)
  11953. term (substring term (match-end 0))
  11954. mm (if re-p
  11955. `(string-match ,(substring kwd 1 -1) todo)
  11956. (list 'equal 'todo kwd))
  11957. mm (if minus (list 'not mm) mm))
  11958. (push mm todomatcher))
  11959. (push (if (> (length todomatcher) 1)
  11960. (cons 'and todomatcher)
  11961. (car todomatcher))
  11962. orlist)
  11963. (setq todomatcher nil))
  11964. (setq todomatcher (if (> (length orlist) 1)
  11965. (cons 'or orlist) (car orlist))))
  11966. ;; Return the string and lisp forms of the matcher
  11967. (setq matcher (if todomatcher
  11968. (list 'and tagsmatcher todomatcher)
  11969. tagsmatcher))
  11970. (cons match0 matcher)))
  11971. (defun org-op-to-function (op &optional stringp)
  11972. "Turn an operator into the appropriate function."
  11973. (setq op
  11974. (cond
  11975. ((equal op "<" ) '(< string< org-time<))
  11976. ((equal op ">" ) '(> org-string> org-time>))
  11977. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  11978. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  11979. ((member op '("=" "==")) '(= string= org-time=))
  11980. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  11981. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  11982. (defun org<> (a b) (not (= a b)))
  11983. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  11984. (defun org-string>= (a b) (not (string< a b)))
  11985. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  11986. (defun org-string<> (a b) (not (string= a b)))
  11987. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  11988. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  11989. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  11990. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  11991. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  11992. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  11993. (defun org-2ft (s)
  11994. "Convert S to a floating point time.
  11995. If S is already a number, just return it. If it is a string, parse
  11996. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  11997. (cond
  11998. ((numberp s) s)
  11999. ((stringp s)
  12000. (condition-case nil
  12001. (float-time (apply 'encode-time (org-parse-time-string s)))
  12002. (error 0.)))
  12003. (t 0.)))
  12004. (defun org-time-today ()
  12005. "Time in seconds today at 0:00.
  12006. Returns the float number of seconds since the beginning of the
  12007. epoch to the beginning of today (00:00)."
  12008. (float-time (apply 'encode-time
  12009. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  12010. (defun org-matcher-time (s)
  12011. "Interpret a time comparison value."
  12012. (save-match-data
  12013. (cond
  12014. ((string= s "<now>") (float-time))
  12015. ((string= s "<today>") (org-time-today))
  12016. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  12017. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  12018. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  12019. (+ (org-time-today)
  12020. (* (string-to-number (match-string 1 s))
  12021. (cdr (assoc (match-string 2 s)
  12022. '(("d" . 86400.0) ("w" . 604800.0)
  12023. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  12024. (t (org-2ft s)))))
  12025. (defun org-match-any-p (re list)
  12026. "Does re match any element of list?"
  12027. (setq list (mapcar (lambda (x) (string-match re x)) list))
  12028. (delq nil list))
  12029. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  12030. (defvar org-tags-overlay (make-overlay 1 1))
  12031. (org-detach-overlay org-tags-overlay)
  12032. (defun org-get-local-tags-at (&optional pos)
  12033. "Get a list of tags defined in the current headline."
  12034. (org-get-tags-at pos 'local))
  12035. (defun org-get-local-tags ()
  12036. "Get a list of tags defined in the current headline."
  12037. (org-get-tags-at nil 'local))
  12038. (defun org-get-tags-at (&optional pos local)
  12039. "Get a list of all headline tags applicable at POS.
  12040. POS defaults to point. If tags are inherited, the list contains
  12041. the targets in the same sequence as the headlines appear, i.e.
  12042. the tags of the current headline come last.
  12043. When LOCAL is non-nil, only return tags from the current headline,
  12044. ignore inherited ones."
  12045. (interactive)
  12046. (if (and org-trust-scanner-tags
  12047. (or (not pos) (equal pos (point)))
  12048. (not local))
  12049. org-scanner-tags
  12050. (let (tags ltags lastpos parent)
  12051. (save-excursion
  12052. (save-restriction
  12053. (widen)
  12054. (goto-char (or pos (point)))
  12055. (save-match-data
  12056. (catch 'done
  12057. (condition-case nil
  12058. (progn
  12059. (org-back-to-heading t)
  12060. (while (not (equal lastpos (point)))
  12061. (setq lastpos (point))
  12062. (when (looking-at
  12063. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  12064. (setq ltags (org-split-string
  12065. (org-match-string-no-properties 1) ":"))
  12066. (when parent
  12067. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  12068. (setq tags (append
  12069. (if parent
  12070. (org-remove-uninherited-tags ltags)
  12071. ltags)
  12072. tags)))
  12073. (or org-use-tag-inheritance (throw 'done t))
  12074. (if local (throw 'done t))
  12075. (or (org-up-heading-safe) (error nil))
  12076. (setq parent t)))
  12077. (error nil)))))
  12078. (if local
  12079. tags
  12080. (append (org-remove-uninherited-tags org-file-tags) tags))))))
  12081. (defun org-add-prop-inherited (s)
  12082. (add-text-properties 0 (length s) '(inherited t) s)
  12083. s)
  12084. (defun org-toggle-tag (tag &optional onoff)
  12085. "Toggle the tag TAG for the current line.
  12086. If ONOFF is `on' or `off', don't toggle but set to this state."
  12087. (let (res current)
  12088. (save-excursion
  12089. (org-back-to-heading t)
  12090. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  12091. (point-at-eol) t)
  12092. (progn
  12093. (setq current (match-string 1))
  12094. (replace-match ""))
  12095. (setq current ""))
  12096. (setq current (nreverse (org-split-string current ":")))
  12097. (cond
  12098. ((eq onoff 'on)
  12099. (setq res t)
  12100. (or (member tag current) (push tag current)))
  12101. ((eq onoff 'off)
  12102. (or (not (member tag current)) (setq current (delete tag current))))
  12103. (t (if (member tag current)
  12104. (setq current (delete tag current))
  12105. (setq res t)
  12106. (push tag current))))
  12107. (end-of-line 1)
  12108. (if current
  12109. (progn
  12110. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  12111. (org-set-tags nil t))
  12112. (delete-horizontal-space))
  12113. (run-hooks 'org-after-tags-change-hook))
  12114. res))
  12115. (defun org-align-tags-here (to-col)
  12116. ;; Assumes that this is a headline
  12117. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  12118. (beginning-of-line 1)
  12119. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12120. (< pos (match-beginning 2)))
  12121. (progn
  12122. (setq tags-l (- (match-end 2) (match-beginning 2)))
  12123. (goto-char (match-beginning 1))
  12124. (insert " ")
  12125. (delete-region (point) (1+ (match-beginning 2)))
  12126. (setq ncol (max (current-column)
  12127. (1+ col)
  12128. (if (> to-col 0)
  12129. to-col
  12130. (- (abs to-col) tags-l))))
  12131. (setq p (point))
  12132. (insert (make-string (- ncol (current-column)) ?\ ))
  12133. (setq ncol (current-column))
  12134. (when indent-tabs-mode (tabify p (point-at-eol)))
  12135. (org-move-to-column (min ncol col) t))
  12136. (goto-char pos))))
  12137. (defun org-set-tags-command (&optional arg just-align)
  12138. "Call the set-tags command for the current entry."
  12139. (interactive "P")
  12140. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  12141. (org-set-tags arg just-align)
  12142. (save-excursion
  12143. (org-back-to-heading t)
  12144. (org-set-tags arg just-align))))
  12145. (defun org-set-tags-to (data)
  12146. "Set the tags of the current entry to DATA, replacing the current tags.
  12147. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  12148. If DATA is nil or the empty string, any tags will be removed."
  12149. (interactive "sTags: ")
  12150. (setq data
  12151. (cond
  12152. ((eq data nil) "")
  12153. ((equal data "") "")
  12154. ((stringp data)
  12155. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  12156. ":"))
  12157. ((listp data)
  12158. (concat ":" (mapconcat 'identity data ":") ":"))
  12159. (t nil)))
  12160. (when data
  12161. (save-excursion
  12162. (org-back-to-heading t)
  12163. (when (looking-at org-complex-heading-regexp)
  12164. (if (match-end 5)
  12165. (progn
  12166. (goto-char (match-beginning 5))
  12167. (insert data)
  12168. (delete-region (point) (point-at-eol))
  12169. (org-set-tags nil 'align))
  12170. (goto-char (point-at-eol))
  12171. (insert " " data)
  12172. (org-set-tags nil 'align)))
  12173. (beginning-of-line 1)
  12174. (if (looking-at ".*?\\([ \t]+\\)$")
  12175. (delete-region (match-beginning 1) (match-end 1))))))
  12176. (defun org-align-all-tags ()
  12177. "Align the tags i all headings."
  12178. (interactive)
  12179. (save-excursion
  12180. (or (ignore-errors (org-back-to-heading t))
  12181. (outline-next-heading))
  12182. (if (org-at-heading-p)
  12183. (org-set-tags t)
  12184. (message "No headings"))))
  12185. (defvar org-indent-indentation-per-level)
  12186. (defun org-set-tags (&optional arg just-align)
  12187. "Set the tags for the current headline.
  12188. With prefix ARG, realign all tags in headings in the current buffer."
  12189. (interactive "P")
  12190. (let* ((re org-outline-regexp-bol)
  12191. (current (unless arg (org-get-tags-string)))
  12192. (col (current-column))
  12193. (org-setting-tags t)
  12194. table current-tags inherited-tags ; computed below when needed
  12195. tags p0 c0 c1 rpl di tc level)
  12196. (if arg
  12197. (save-excursion
  12198. (goto-char (point-min))
  12199. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  12200. (while (re-search-forward re nil t)
  12201. (org-set-tags nil t)
  12202. (end-of-line 1)))
  12203. (message "All tags realigned to column %d" org-tags-column))
  12204. (if just-align
  12205. (setq tags current)
  12206. ;; Get a new set of tags from the user
  12207. (save-excursion
  12208. (setq table (append org-tag-persistent-alist
  12209. (or org-tag-alist (org-get-buffer-tags))
  12210. (and
  12211. org-complete-tags-always-offer-all-agenda-tags
  12212. (org-global-tags-completion-table
  12213. (org-agenda-files))))
  12214. org-last-tags-completion-table table
  12215. current-tags (org-split-string current ":")
  12216. inherited-tags (nreverse
  12217. (nthcdr (length current-tags)
  12218. (nreverse (org-get-tags-at))))
  12219. tags
  12220. (if (or (eq t org-use-fast-tag-selection)
  12221. (and org-use-fast-tag-selection
  12222. (delq nil (mapcar 'cdr table))))
  12223. (org-fast-tag-selection
  12224. current-tags inherited-tags table
  12225. (if org-fast-tag-selection-include-todo
  12226. org-todo-key-alist))
  12227. (let ((org-add-colon-after-tag-completion t))
  12228. (org-trim
  12229. (org-icompleting-read "Tags: "
  12230. 'org-tags-completion-function
  12231. nil nil current 'org-tags-history))))))
  12232. (while (string-match "[-+&]+" tags)
  12233. ;; No boolean logic, just a list
  12234. (setq tags (replace-match ":" t t tags))))
  12235. (setq tags (replace-regexp-in-string "[,]" ":" tags))
  12236. (if org-tags-sort-function
  12237. (setq tags (mapconcat 'identity
  12238. (sort (org-split-string
  12239. tags (org-re "[^[:alnum:]_@#%]+"))
  12240. org-tags-sort-function) ":")))
  12241. (if (string-match "\\`[\t ]*\\'" tags)
  12242. (setq tags "")
  12243. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  12244. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  12245. ;; Insert new tags at the correct column
  12246. (beginning-of-line 1)
  12247. (setq level (or (and (looking-at org-outline-regexp)
  12248. (- (match-end 0) (point) 1))
  12249. 1))
  12250. (cond
  12251. ((and (equal current "") (equal tags "")))
  12252. ((re-search-forward
  12253. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  12254. (point-at-eol) t)
  12255. (if (equal tags "")
  12256. (setq rpl "")
  12257. (goto-char (match-beginning 0))
  12258. (setq c0 (current-column)
  12259. ;; compute offset for the case of org-indent-mode active
  12260. di (if org-indent-mode
  12261. (* (1- org-indent-indentation-per-level) (1- level))
  12262. 0)
  12263. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  12264. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  12265. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
  12266. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  12267. (replace-match rpl t t)
  12268. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  12269. tags)
  12270. (t (error "Tags alignment failed")))
  12271. (org-move-to-column col)
  12272. (unless just-align
  12273. (run-hooks 'org-after-tags-change-hook)))))
  12274. (defun org-change-tag-in-region (beg end tag off)
  12275. "Add or remove TAG for each entry in the region.
  12276. This works in the agenda, and also in an org-mode buffer."
  12277. (interactive
  12278. (list (region-beginning) (region-end)
  12279. (let ((org-last-tags-completion-table
  12280. (if (eq major-mode 'org-mode)
  12281. (org-get-buffer-tags)
  12282. (org-global-tags-completion-table))))
  12283. (org-icompleting-read
  12284. "Tag: " 'org-tags-completion-function nil nil nil
  12285. 'org-tags-history))
  12286. (progn
  12287. (message "[s]et or [r]emove? ")
  12288. (equal (read-char-exclusive) ?r))))
  12289. (if (fboundp 'deactivate-mark) (deactivate-mark))
  12290. (let ((agendap (equal major-mode 'org-agenda-mode))
  12291. l1 l2 m buf pos newhead (cnt 0))
  12292. (goto-char end)
  12293. (setq l2 (1- (org-current-line)))
  12294. (goto-char beg)
  12295. (setq l1 (org-current-line))
  12296. (loop for l from l1 to l2 do
  12297. (org-goto-line l)
  12298. (setq m (get-text-property (point) 'org-hd-marker))
  12299. (when (or (and (eq major-mode 'org-mode) (org-at-heading-p))
  12300. (and agendap m))
  12301. (setq buf (if agendap (marker-buffer m) (current-buffer))
  12302. pos (if agendap m (point)))
  12303. (with-current-buffer buf
  12304. (save-excursion
  12305. (save-restriction
  12306. (goto-char pos)
  12307. (setq cnt (1+ cnt))
  12308. (org-toggle-tag tag (if off 'off 'on))
  12309. (setq newhead (org-get-heading)))))
  12310. (and agendap (org-agenda-change-all-lines newhead m))))
  12311. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  12312. (defun org-tags-completion-function (string predicate &optional flag)
  12313. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  12314. (confirm (lambda (x) (stringp (car x)))))
  12315. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  12316. (setq s1 (match-string 1 string)
  12317. s2 (match-string 2 string))
  12318. (setq s1 "" s2 string))
  12319. (cond
  12320. ((eq flag nil)
  12321. ;; try completion
  12322. (setq rtn (try-completion s2 ctable confirm))
  12323. (if (stringp rtn)
  12324. (setq rtn
  12325. (concat s1 s2 (substring rtn (length s2))
  12326. (if (and org-add-colon-after-tag-completion
  12327. (assoc rtn ctable))
  12328. ":" ""))))
  12329. rtn)
  12330. ((eq flag t)
  12331. ;; all-completions
  12332. (all-completions s2 ctable confirm)
  12333. )
  12334. ((eq flag 'lambda)
  12335. ;; exact match?
  12336. (assoc s2 ctable)))
  12337. ))
  12338. (defun org-fast-tag-insert (kwd tags face &optional end)
  12339. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  12340. (insert (format "%-12s" (concat kwd ":"))
  12341. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  12342. (or end "")))
  12343. (defun org-fast-tag-show-exit (flag)
  12344. (save-excursion
  12345. (org-goto-line 3)
  12346. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  12347. (replace-match ""))
  12348. (when flag
  12349. (end-of-line 1)
  12350. (org-move-to-column (- (window-width) 19) t)
  12351. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  12352. (defun org-set-current-tags-overlay (current prefix)
  12353. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  12354. (if (featurep 'xemacs)
  12355. (org-overlay-display org-tags-overlay (concat prefix s)
  12356. 'secondary-selection)
  12357. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  12358. (org-overlay-display org-tags-overlay (concat prefix s)))))
  12359. (defvar org-last-tag-selection-key nil)
  12360. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  12361. "Fast tag selection with single keys.
  12362. CURRENT is the current list of tags in the headline, INHERITED is the
  12363. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  12364. possibly with grouping information. TODO-TABLE is a similar table with
  12365. TODO keywords, should these have keys assigned to them.
  12366. If the keys are nil, a-z are automatically assigned.
  12367. Returns the new tags string, or nil to not change the current settings."
  12368. (let* ((fulltable (append table todo-table))
  12369. (maxlen (apply 'max (mapcar
  12370. (lambda (x)
  12371. (if (stringp (car x)) (string-width (car x)) 0))
  12372. fulltable)))
  12373. (buf (current-buffer))
  12374. (expert (eq org-fast-tag-selection-single-key 'expert))
  12375. (buffer-tags nil)
  12376. (fwidth (+ maxlen 3 1 3))
  12377. (ncol (/ (- (window-width) 4) fwidth))
  12378. (i-face 'org-done)
  12379. (c-face 'org-todo)
  12380. tg cnt e c char c1 c2 ntable tbl rtn
  12381. ov-start ov-end ov-prefix
  12382. (exit-after-next org-fast-tag-selection-single-key)
  12383. (done-keywords org-done-keywords)
  12384. groups ingroup)
  12385. (save-excursion
  12386. (beginning-of-line 1)
  12387. (if (looking-at
  12388. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12389. (setq ov-start (match-beginning 1)
  12390. ov-end (match-end 1)
  12391. ov-prefix "")
  12392. (setq ov-start (1- (point-at-eol))
  12393. ov-end (1+ ov-start))
  12394. (skip-chars-forward "^\n\r")
  12395. (setq ov-prefix
  12396. (concat
  12397. (buffer-substring (1- (point)) (point))
  12398. (if (> (current-column) org-tags-column)
  12399. " "
  12400. (make-string (- org-tags-column (current-column)) ?\ ))))))
  12401. (move-overlay org-tags-overlay ov-start ov-end)
  12402. (save-window-excursion
  12403. (if expert
  12404. (set-buffer (get-buffer-create " *Org tags*"))
  12405. (delete-other-windows)
  12406. (split-window-vertically)
  12407. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  12408. (erase-buffer)
  12409. (org-set-local 'org-done-keywords done-keywords)
  12410. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  12411. (org-fast-tag-insert "Current" current c-face "\n\n")
  12412. (org-fast-tag-show-exit exit-after-next)
  12413. (org-set-current-tags-overlay current ov-prefix)
  12414. (setq tbl fulltable char ?a cnt 0)
  12415. (while (setq e (pop tbl))
  12416. (cond
  12417. ((equal (car e) :startgroup)
  12418. (push '() groups) (setq ingroup t)
  12419. (when (not (= cnt 0))
  12420. (setq cnt 0)
  12421. (insert "\n"))
  12422. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  12423. ((equal (car e) :endgroup)
  12424. (setq ingroup nil cnt 0)
  12425. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  12426. ((equal e '(:newline))
  12427. (when (not (= cnt 0))
  12428. (setq cnt 0)
  12429. (insert "\n")
  12430. (setq e (car tbl))
  12431. (while (equal (car tbl) '(:newline))
  12432. (insert "\n")
  12433. (setq tbl (cdr tbl)))))
  12434. (t
  12435. (setq tg (copy-sequence (car e)) c2 nil)
  12436. (if (cdr e)
  12437. (setq c (cdr e))
  12438. ;; automatically assign a character.
  12439. (setq c1 (string-to-char
  12440. (downcase (substring
  12441. tg (if (= (string-to-char tg) ?@) 1 0)))))
  12442. (if (or (rassoc c1 ntable) (rassoc c1 table))
  12443. (while (or (rassoc char ntable) (rassoc char table))
  12444. (setq char (1+ char)))
  12445. (setq c2 c1))
  12446. (setq c (or c2 char)))
  12447. (if ingroup (push tg (car groups)))
  12448. (setq tg (org-add-props tg nil 'face
  12449. (cond
  12450. ((not (assoc tg table))
  12451. (org-get-todo-face tg))
  12452. ((member tg current) c-face)
  12453. ((member tg inherited) i-face)
  12454. (t nil))))
  12455. (if (and (= cnt 0) (not ingroup)) (insert " "))
  12456. (insert "[" c "] " tg (make-string
  12457. (- fwidth 4 (length tg)) ?\ ))
  12458. (push (cons tg c) ntable)
  12459. (when (= (setq cnt (1+ cnt)) ncol)
  12460. (insert "\n")
  12461. (if ingroup (insert " "))
  12462. (setq cnt 0)))))
  12463. (setq ntable (nreverse ntable))
  12464. (insert "\n")
  12465. (goto-char (point-min))
  12466. (if (not expert) (org-fit-window-to-buffer))
  12467. (setq rtn
  12468. (catch 'exit
  12469. (while t
  12470. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  12471. (if (not groups) "no " "")
  12472. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  12473. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  12474. (setq org-last-tag-selection-key c)
  12475. (cond
  12476. ((= c ?\r) (throw 'exit t))
  12477. ((= c ?!)
  12478. (setq groups (not groups))
  12479. (goto-char (point-min))
  12480. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  12481. ((= c ?\C-c)
  12482. (if (not expert)
  12483. (org-fast-tag-show-exit
  12484. (setq exit-after-next (not exit-after-next)))
  12485. (setq expert nil)
  12486. (delete-other-windows)
  12487. (set-window-buffer (split-window-vertically) " *Org tags*")
  12488. (org-switch-to-buffer-other-window " *Org tags*")
  12489. (org-fit-window-to-buffer)))
  12490. ((or (= c ?\C-g)
  12491. (and (= c ?q) (not (rassoc c ntable))))
  12492. (org-detach-overlay org-tags-overlay)
  12493. (setq quit-flag t))
  12494. ((= c ?\ )
  12495. (setq current nil)
  12496. (if exit-after-next (setq exit-after-next 'now)))
  12497. ((= c ?\t)
  12498. (condition-case nil
  12499. (setq tg (org-icompleting-read
  12500. "Tag: "
  12501. (or buffer-tags
  12502. (with-current-buffer buf
  12503. (org-get-buffer-tags)))))
  12504. (quit (setq tg "")))
  12505. (when (string-match "\\S-" tg)
  12506. (add-to-list 'buffer-tags (list tg))
  12507. (if (member tg current)
  12508. (setq current (delete tg current))
  12509. (push tg current)))
  12510. (if exit-after-next (setq exit-after-next 'now)))
  12511. ((setq e (rassoc c todo-table) tg (car e))
  12512. (with-current-buffer buf
  12513. (save-excursion (org-todo tg)))
  12514. (if exit-after-next (setq exit-after-next 'now)))
  12515. ((setq e (rassoc c ntable) tg (car e))
  12516. (if (member tg current)
  12517. (setq current (delete tg current))
  12518. (loop for g in groups do
  12519. (if (member tg g)
  12520. (mapc (lambda (x)
  12521. (setq current (delete x current)))
  12522. g)))
  12523. (push tg current))
  12524. (if exit-after-next (setq exit-after-next 'now))))
  12525. ;; Create a sorted list
  12526. (setq current
  12527. (sort current
  12528. (lambda (a b)
  12529. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  12530. (if (eq exit-after-next 'now) (throw 'exit t))
  12531. (goto-char (point-min))
  12532. (beginning-of-line 2)
  12533. (delete-region (point) (point-at-eol))
  12534. (org-fast-tag-insert "Current" current c-face)
  12535. (org-set-current-tags-overlay current ov-prefix)
  12536. (while (re-search-forward
  12537. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  12538. (setq tg (match-string 1))
  12539. (add-text-properties
  12540. (match-beginning 1) (match-end 1)
  12541. (list 'face
  12542. (cond
  12543. ((member tg current) c-face)
  12544. ((member tg inherited) i-face)
  12545. (t (get-text-property (match-beginning 1) 'face))))))
  12546. (goto-char (point-min)))))
  12547. (org-detach-overlay org-tags-overlay)
  12548. (if rtn
  12549. (mapconcat 'identity current ":")
  12550. nil))))
  12551. (defun org-get-tags-string ()
  12552. "Get the TAGS string in the current headline."
  12553. (unless (org-at-heading-p t)
  12554. (error "Not on a heading"))
  12555. (save-excursion
  12556. (beginning-of-line 1)
  12557. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12558. (org-match-string-no-properties 1)
  12559. "")))
  12560. (defun org-get-tags ()
  12561. "Get the list of tags specified in the current headline."
  12562. (org-split-string (org-get-tags-string) ":"))
  12563. (defun org-get-buffer-tags ()
  12564. "Get a table of all tags used in the buffer, for completion."
  12565. (let (tags)
  12566. (save-excursion
  12567. (goto-char (point-min))
  12568. (while (re-search-forward
  12569. (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
  12570. (when (equal (char-after (point-at-bol 0)) ?*)
  12571. (mapc (lambda (x) (add-to-list 'tags x))
  12572. (org-split-string (org-match-string-no-properties 1) ":")))))
  12573. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  12574. (mapcar 'list tags)))
  12575. ;;;; The mapping API
  12576. ;;;###autoload
  12577. (defun org-map-entries (func &optional match scope &rest skip)
  12578. "Call FUNC at each headline selected by MATCH in SCOPE.
  12579. FUNC is a function or a lisp form. The function will be called without
  12580. arguments, with the cursor positioned at the beginning of the headline.
  12581. The return values of all calls to the function will be collected and
  12582. returned as a list.
  12583. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  12584. does not need to preserve point. After evaluation, the cursor will be
  12585. moved to the end of the line (presumably of the headline of the
  12586. processed entry) and search continues from there. Under some
  12587. circumstances, this may not produce the wanted results. For example,
  12588. if you have removed (e.g. archived) the current (sub)tree it could
  12589. mean that the next entry will be skipped entirely. In such cases, you
  12590. can specify the position from where search should continue by making
  12591. FUNC set the variable `org-map-continue-from' to the desired buffer
  12592. position.
  12593. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  12594. Only headlines that are matched by this query will be considered during
  12595. the iteration. When MATCH is nil or t, all headlines will be
  12596. visited by the iteration.
  12597. SCOPE determines the scope of this command. It can be any of:
  12598. nil The current buffer, respecting the restriction if any
  12599. tree The subtree started with the entry at point
  12600. region The entries within the active region, if any
  12601. region-start-level
  12602. The entries within the active region, but only those at
  12603. the same level than the first one.
  12604. file The current buffer, without restriction
  12605. file-with-archives
  12606. The current buffer, and any archives associated with it
  12607. agenda All agenda files
  12608. agenda-with-archives
  12609. All agenda files with any archive files associated with them
  12610. \(file1 file2 ...)
  12611. If this is a list, all files in the list will be scanned
  12612. The remaining args are treated as settings for the skipping facilities of
  12613. the scanner. The following items can be given here:
  12614. archive skip trees with the archive tag.
  12615. comment skip trees with the COMMENT keyword
  12616. function or Emacs Lisp form:
  12617. will be used as value for `org-agenda-skip-function', so whenever
  12618. the function returns t, FUNC will not be called for that
  12619. entry and search will continue from the point where the
  12620. function leaves it.
  12621. If your function needs to retrieve the tags including inherited tags
  12622. at the *current* entry, you can use the value of the variable
  12623. `org-scanner-tags' which will be much faster than getting the value
  12624. with `org-get-tags-at'. If your function gets properties with
  12625. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  12626. to t around the call to `org-entry-properties' to get the same speedup.
  12627. Note that if your function moves around to retrieve tags and properties at
  12628. a *different* entry, you cannot use these techniques."
  12629. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  12630. (not (org-region-active-p)))
  12631. (let* ((org-agenda-archives-mode nil) ; just to make sure
  12632. (org-agenda-skip-archived-trees (memq 'archive skip))
  12633. (org-agenda-skip-comment-trees (memq 'comment skip))
  12634. (org-agenda-skip-function
  12635. (car (org-delete-all '(comment archive) skip)))
  12636. (org-tags-match-list-sublevels t)
  12637. (start-level (eq scope 'region-start-level))
  12638. matcher file res
  12639. org-todo-keywords-for-agenda
  12640. org-done-keywords-for-agenda
  12641. org-todo-keyword-alist-for-agenda
  12642. org-drawers-for-agenda
  12643. org-tag-alist-for-agenda)
  12644. (cond
  12645. ((eq match t) (setq matcher t))
  12646. ((eq match nil) (setq matcher t))
  12647. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  12648. (save-excursion
  12649. (save-restriction
  12650. (cond ((eq scope 'tree)
  12651. (org-back-to-heading t)
  12652. (org-narrow-to-subtree)
  12653. (setq scope nil))
  12654. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  12655. (org-region-active-p))
  12656. ;; If needed, set start-level to a string like "2"
  12657. (when start-level
  12658. (save-excursion
  12659. (goto-char (region-beginning))
  12660. (unless (org-at-heading-p) (outline-next-heading))
  12661. (setq start-level (org-current-level))))
  12662. (narrow-to-region (region-beginning)
  12663. (save-excursion
  12664. (goto-char (region-end))
  12665. (unless (and (bolp) (org-at-heading-p))
  12666. (outline-next-heading))
  12667. (point)))
  12668. (setq scope nil)))
  12669. (if (not scope)
  12670. (progn
  12671. (org-prepare-agenda-buffers
  12672. (list (buffer-file-name (current-buffer))))
  12673. (setq res (org-scan-tags func matcher nil start-level)))
  12674. ;; Get the right scope
  12675. (cond
  12676. ((and scope (listp scope) (symbolp (car scope)))
  12677. (setq scope (eval scope)))
  12678. ((eq scope 'agenda)
  12679. (setq scope (org-agenda-files t)))
  12680. ((eq scope 'agenda-with-archives)
  12681. (setq scope (org-agenda-files t))
  12682. (setq scope (org-add-archive-files scope)))
  12683. ((eq scope 'file)
  12684. (setq scope (list (buffer-file-name))))
  12685. ((eq scope 'file-with-archives)
  12686. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  12687. (org-prepare-agenda-buffers scope)
  12688. (while (setq file (pop scope))
  12689. (with-current-buffer (org-find-base-buffer-visiting file)
  12690. (save-excursion
  12691. (save-restriction
  12692. (widen)
  12693. (goto-char (point-min))
  12694. (setq res (append res (org-scan-tags func matcher))))))))))
  12695. res)))
  12696. ;;;; Properties
  12697. ;;; Setting and retrieving properties
  12698. (defconst org-special-properties
  12699. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  12700. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM")
  12701. "The special properties valid in Org-mode.
  12702. These are properties that are not defined in the property drawer,
  12703. but in some other way.")
  12704. (defconst org-default-properties
  12705. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  12706. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  12707. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  12708. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  12709. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  12710. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  12711. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  12712. "Some properties that are used by Org-mode for various purposes.
  12713. Being in this list makes sure that they are offered for completion.")
  12714. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  12715. "Regular expression matching the first line of a property drawer.")
  12716. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  12717. "Regular expression matching the last line of a property drawer.")
  12718. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  12719. "Regular expression matching the first line of a property drawer.")
  12720. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  12721. "Regular expression matching the first line of a property drawer.")
  12722. (defconst org-property-drawer-re
  12723. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  12724. org-property-end-re "\\)\n?")
  12725. "Matches an entire property drawer.")
  12726. (defconst org-clock-drawer-re
  12727. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  12728. org-property-end-re "\\)\n?")
  12729. "Matches an entire clock drawer.")
  12730. (defsubst org-re-property (property)
  12731. "Return a regexp matching PROPERTY.
  12732. Match group 1 will be set to the value "
  12733. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)"))
  12734. (defun org-property-action ()
  12735. "Do an action on properties."
  12736. (interactive)
  12737. (let (c)
  12738. (org-at-property-p)
  12739. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  12740. (setq c (read-char-exclusive))
  12741. (cond
  12742. ((equal c ?s)
  12743. (call-interactively 'org-set-property))
  12744. ((equal c ?d)
  12745. (call-interactively 'org-delete-property))
  12746. ((equal c ?D)
  12747. (call-interactively 'org-delete-property-globally))
  12748. ((equal c ?c)
  12749. (call-interactively 'org-compute-property-at-point))
  12750. (t (error "No such property action %c" c)))))
  12751. (defun org-set-effort (&optional value)
  12752. "Set the effort property of the current entry.
  12753. With numerical prefix arg, use the nth allowed value, 0 stands for the 10th
  12754. allowed value."
  12755. (interactive "P")
  12756. (if (equal value 0) (setq value 10))
  12757. (let* ((completion-ignore-case t)
  12758. (prop org-effort-property)
  12759. (cur (org-entry-get nil prop))
  12760. (allowed (org-property-get-allowed-values nil prop 'table))
  12761. (existing (mapcar 'list (org-property-values prop)))
  12762. rpl
  12763. (val (cond
  12764. ((stringp value) value)
  12765. ((and allowed (integerp value))
  12766. (or (car (nth (1- value) allowed))
  12767. (car (org-last allowed))))
  12768. (allowed
  12769. (message "Select 1-9,0, [RET%s]: %s"
  12770. (if cur (concat "=" cur) "")
  12771. (mapconcat 'car allowed " "))
  12772. (setq rpl (read-char-exclusive))
  12773. (if (equal rpl ?\r)
  12774. cur
  12775. (setq rpl (- rpl ?0))
  12776. (if (equal rpl 0) (setq rpl 10))
  12777. (if (and (> rpl 0) (<= rpl (length allowed)))
  12778. (car (nth (1- rpl) allowed))
  12779. (org-completing-read "Effort: " allowed nil))))
  12780. (t
  12781. (let (org-completion-use-ido org-completion-use-iswitchb)
  12782. (org-completing-read
  12783. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  12784. (concat "[" cur "]") "")
  12785. ": ")
  12786. existing nil nil "" nil cur))))))
  12787. (unless (equal (org-entry-get nil prop) val)
  12788. (org-entry-put nil prop val))
  12789. (message "%s is now %s" prop val)))
  12790. (defun org-at-property-p ()
  12791. "Is cursor inside a property drawer?"
  12792. (save-excursion
  12793. (beginning-of-line 1)
  12794. (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
  12795. (save-match-data ;; Used by calling procedures
  12796. (let ((p (point))
  12797. (range (unless (org-before-first-heading-p)
  12798. (org-get-property-block))))
  12799. (and range (<= (car range) p) (< p (cdr range))))))))
  12800. (defun org-get-property-block (&optional beg end force)
  12801. "Return the (beg . end) range of the body of the property drawer.
  12802. BEG and END can be beginning and end of subtree, if not given
  12803. they will be found.
  12804. If the drawer does not exist and FORCE is non-nil, create the drawer."
  12805. (catch 'exit
  12806. (save-excursion
  12807. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  12808. (end (or end (progn (outline-next-heading) (point)))))
  12809. (goto-char beg)
  12810. (if (re-search-forward org-property-start-re end t)
  12811. (setq beg (1+ (match-end 0)))
  12812. (if force
  12813. (save-excursion
  12814. (org-insert-property-drawer)
  12815. (setq end (progn (outline-next-heading) (point))))
  12816. (throw 'exit nil))
  12817. (goto-char beg)
  12818. (if (re-search-forward org-property-start-re end t)
  12819. (setq beg (1+ (match-end 0)))))
  12820. (if (re-search-forward org-property-end-re end t)
  12821. (setq end (match-beginning 0))
  12822. (or force (throw 'exit nil))
  12823. (goto-char beg)
  12824. (setq end beg)
  12825. (org-indent-line-function)
  12826. (insert ":END:\n"))
  12827. (cons beg end)))))
  12828. (defun org-entry-properties (&optional pom which specific)
  12829. "Get all properties of the entry at point-or-marker POM.
  12830. This includes the TODO keyword, the tags, time strings for deadline,
  12831. scheduled, and clocking, and any additional properties defined in the
  12832. entry. The return value is an alist, keys may occur multiple times
  12833. if the property key was used several times.
  12834. POM may also be nil, in which case the current entry is used.
  12835. If WHICH is nil or `all', get all properties. If WHICH is
  12836. `special' or `standard', only get that subclass. If WHICH
  12837. is a string only get exactly this property. SPECIFIC can be a string, the
  12838. specific property we are interested in. Specifying it can speed
  12839. things up because then unnecessary parsing is avoided."
  12840. (setq which (or which 'all))
  12841. (org-with-point-at pom
  12842. (let ((clockstr (substring org-clock-string 0 -1))
  12843. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  12844. (case-fold-search nil)
  12845. beg end range props sum-props key key1 value string clocksum)
  12846. (save-excursion
  12847. (when (condition-case nil
  12848. (and (eq major-mode 'org-mode) (org-back-to-heading t))
  12849. (error nil))
  12850. (setq beg (point))
  12851. (setq sum-props (get-text-property (point) 'org-summaries))
  12852. (setq clocksum (get-text-property (point) :org-clock-minutes))
  12853. (outline-next-heading)
  12854. (setq end (point))
  12855. (when (memq which '(all special))
  12856. ;; Get the special properties, like TODO and tags
  12857. (goto-char beg)
  12858. (when (and (or (not specific) (string= specific "TODO"))
  12859. (looking-at org-todo-line-regexp) (match-end 2))
  12860. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  12861. (when (and (or (not specific) (string= specific "PRIORITY"))
  12862. (looking-at org-priority-regexp))
  12863. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  12864. (when (or (not specific) (string= specific "FILE"))
  12865. (push (cons "FILE" buffer-file-name) props))
  12866. (when (and (or (not specific) (string= specific "TAGS"))
  12867. (setq value (org-get-tags-string))
  12868. (string-match "\\S-" value))
  12869. (push (cons "TAGS" value) props))
  12870. (when (and (or (not specific) (string= specific "ALLTAGS"))
  12871. (setq value (org-get-tags-at)))
  12872. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  12873. ":"))
  12874. props))
  12875. (when (or (not specific) (string= specific "BLOCKED"))
  12876. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  12877. (when (or (not specific)
  12878. (member specific
  12879. '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
  12880. "TIMESTAMP" "TIMESTAMP_IA")))
  12881. (catch 'match
  12882. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  12883. (setq key (if (match-end 1)
  12884. (substring (org-match-string-no-properties 1)
  12885. 0 -1))
  12886. string (if (equal key clockstr)
  12887. (org-no-properties
  12888. (org-trim
  12889. (buffer-substring
  12890. (match-beginning 3) (goto-char
  12891. (point-at-eol)))))
  12892. (substring (org-match-string-no-properties 3)
  12893. 1 -1)))
  12894. ;; Get the correct property name from the key. This is
  12895. ;; necessary if the user has configured time keywords.
  12896. (setq key1 (concat key ":"))
  12897. (cond
  12898. ((not key)
  12899. (setq key
  12900. (if (= (char-after (match-beginning 3)) ?\[)
  12901. "TIMESTAMP_IA" "TIMESTAMP")))
  12902. ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
  12903. ((equal key1 org-deadline-string) (setq key "DEADLINE"))
  12904. ((equal key1 org-closed-string) (setq key "CLOSED"))
  12905. ((equal key1 org-clock-string) (setq key "CLOCK")))
  12906. (if (and specific (equal key specific) (not (equal key "CLOCK")))
  12907. (progn
  12908. (push (cons key string) props)
  12909. ;; no need to search further if match is found
  12910. (throw 'match t))
  12911. (when (or (equal key "CLOCK") (not (assoc key props)))
  12912. (push (cons key string) props))))))
  12913. )
  12914. (when (memq which '(all standard))
  12915. ;; Get the standard properties, like :PROP: ...
  12916. (setq range (org-get-property-block beg end))
  12917. (when range
  12918. (goto-char (car range))
  12919. (while (re-search-forward
  12920. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  12921. (cdr range) t)
  12922. (setq key (org-match-string-no-properties 1)
  12923. value (org-trim (or (org-match-string-no-properties 2) "")))
  12924. (unless (member key excluded)
  12925. (push (cons key (or value "")) props)))))
  12926. (if clocksum
  12927. (push (cons "CLOCKSUM"
  12928. (org-columns-number-to-string (/ (float clocksum) 60.)
  12929. 'add_times))
  12930. props))
  12931. (unless (assoc "CATEGORY" props)
  12932. (push (cons "CATEGORY" (org-get-category)) props))
  12933. (append sum-props (nreverse props)))))))
  12934. (defun org-entry-get (pom property &optional inherit literal-nil)
  12935. "Get value of PROPERTY for entry at point-or-marker POM.
  12936. If INHERIT is non-nil and the entry does not have the property,
  12937. then also check higher levels of the hierarchy.
  12938. If INHERIT is the symbol `selective', use inheritance only if the setting
  12939. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  12940. If the property is present but empty, the return value is the empty string.
  12941. If the property is not present at all, nil is returned.
  12942. If LITERAL-NIL is set, return the string value \"nil\" as a string,
  12943. do not interpret it as the list atom nil. This is used for inheritance
  12944. when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
  12945. (org-with-point-at pom
  12946. (if (and inherit (if (eq inherit 'selective)
  12947. (org-property-inherit-p property)
  12948. t))
  12949. (org-entry-get-with-inheritance property literal-nil)
  12950. (if (member property org-special-properties)
  12951. ;; We need a special property. Use `org-entry-properties' to
  12952. ;; retrieve it, but specify the wanted property
  12953. (cdr (assoc property (org-entry-properties nil 'special property)))
  12954. (let ((range (unless (org-before-first-heading-p)
  12955. (org-get-property-block)))
  12956. (props (list (or (assoc property org-file-properties)
  12957. (assoc property org-global-properties)
  12958. (assoc property org-global-properties-fixed))))
  12959. val)
  12960. (flet ((ap (key)
  12961. (when (re-search-forward
  12962. (org-re-property key) (cdr range) t)
  12963. (setq props
  12964. (org-update-property-plist
  12965. key
  12966. (if (match-end 1)
  12967. (org-match-string-no-properties 1) "")
  12968. props)))))
  12969. (when (and range (goto-char (car range)))
  12970. (ap property)
  12971. (goto-char (car range))
  12972. (while (ap (concat property "+")))
  12973. (setq val (cdr (assoc property props)))
  12974. (when val (if literal-nil val (org-not-nil val))))))))))
  12975. (defun org-property-or-variable-value (var &optional inherit)
  12976. "Check if there is a property fixing the value of VAR.
  12977. If yes, return this value. If not, return the current value of the variable."
  12978. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  12979. (if (and prop (stringp prop) (string-match "\\S-" prop))
  12980. (read prop)
  12981. (symbol-value var))))
  12982. (defun org-entry-delete (pom property)
  12983. "Delete the property PROPERTY from entry at point-or-marker POM."
  12984. (org-with-point-at pom
  12985. (if (member property org-special-properties)
  12986. nil ; cannot delete these properties.
  12987. (let ((range (org-get-property-block)))
  12988. (if (and range
  12989. (goto-char (car range))
  12990. (re-search-forward
  12991. (org-re-property property)
  12992. (cdr range) t))
  12993. (progn
  12994. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  12995. t)
  12996. nil)))))
  12997. ;; Multi-values properties are properties that contain multiple values
  12998. ;; These values are assumed to be single words, separated by whitespace.
  12999. (defun org-entry-add-to-multivalued-property (pom property value)
  13000. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  13001. (let* ((old (org-entry-get pom property))
  13002. (values (and old (org-split-string old "[ \t]"))))
  13003. (setq value (org-entry-protect-space value))
  13004. (unless (member value values)
  13005. (setq values (cons value values))
  13006. (org-entry-put pom property
  13007. (mapconcat 'identity values " ")))))
  13008. (defun org-entry-remove-from-multivalued-property (pom property value)
  13009. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  13010. (let* ((old (org-entry-get pom property))
  13011. (values (and old (org-split-string old "[ \t]"))))
  13012. (setq value (org-entry-protect-space value))
  13013. (when (member value values)
  13014. (setq values (delete value values))
  13015. (org-entry-put pom property
  13016. (mapconcat 'identity values " ")))))
  13017. (defun org-entry-member-in-multivalued-property (pom property value)
  13018. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  13019. (let* ((old (org-entry-get pom property))
  13020. (values (and old (org-split-string old "[ \t]"))))
  13021. (setq value (org-entry-protect-space value))
  13022. (member value values)))
  13023. (defun org-entry-get-multivalued-property (pom property)
  13024. "Return a list of values in a multivalued property."
  13025. (let* ((value (org-entry-get pom property))
  13026. (values (and value (org-split-string value "[ \t]"))))
  13027. (mapcar 'org-entry-restore-space values)))
  13028. (defun org-entry-put-multivalued-property (pom property &rest values)
  13029. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  13030. VALUES should be a list of strings. Spaces will be protected."
  13031. (org-entry-put pom property
  13032. (mapconcat 'org-entry-protect-space values " "))
  13033. (let* ((value (org-entry-get pom property))
  13034. (values (and value (org-split-string value "[ \t]"))))
  13035. (mapcar 'org-entry-restore-space values)))
  13036. (defun org-entry-protect-space (s)
  13037. "Protect spaces and newline in string S."
  13038. (while (string-match " " s)
  13039. (setq s (replace-match "%20" t t s)))
  13040. (while (string-match "\n" s)
  13041. (setq s (replace-match "%0A" t t s)))
  13042. s)
  13043. (defun org-entry-restore-space (s)
  13044. "Restore spaces and newline in string S."
  13045. (while (string-match "%20" s)
  13046. (setq s (replace-match " " t t s)))
  13047. (while (string-match "%0A" s)
  13048. (setq s (replace-match "\n" t t s)))
  13049. s)
  13050. (defvar org-entry-property-inherited-from (make-marker)
  13051. "Marker pointing to the entry from where a property was inherited.
  13052. Each call to `org-entry-get-with-inheritance' will set this marker to the
  13053. location of the entry where the inheritance search matched. If there was
  13054. no match, the marker will point nowhere.
  13055. Note that also `org-entry-get' calls this function, if the INHERIT flag
  13056. is set.")
  13057. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  13058. "Get entry property, and search higher levels if not present.
  13059. The search will stop at the first ancestor which has the property defined.
  13060. If the value found is \"nil\", return nil to show that the property
  13061. should be considered as undefined (this is the meaning of nil here).
  13062. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  13063. (move-marker org-entry-property-inherited-from nil)
  13064. (let (tmp)
  13065. (unless (org-before-first-heading-p)
  13066. (save-excursion
  13067. (save-restriction
  13068. (widen)
  13069. (catch 'ex
  13070. (while t
  13071. (when (setq tmp (org-entry-get nil property nil 'literal-nil))
  13072. (org-back-to-heading t)
  13073. (move-marker org-entry-property-inherited-from (point))
  13074. (throw 'ex tmp))
  13075. (or (org-up-heading-safe) (throw 'ex nil)))))))
  13076. (setq tmp (or tmp
  13077. (cdr (assoc property org-file-properties))
  13078. (cdr (assoc property org-global-properties))
  13079. (cdr (assoc property org-global-properties-fixed))))
  13080. (if literal-nil tmp (org-not-nil tmp))))
  13081. (defvar org-property-changed-functions nil
  13082. "Hook called when the value of a property has changed.
  13083. Each hook function should accept two arguments, the name of the property
  13084. and the new value.")
  13085. (defun org-entry-put (pom property value)
  13086. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  13087. (org-with-point-at pom
  13088. (org-back-to-heading t)
  13089. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  13090. range)
  13091. (cond
  13092. ((equal property "TODO")
  13093. (when (and (stringp value) (string-match "\\S-" value)
  13094. (not (member value org-todo-keywords-1)))
  13095. (error "\"%s\" is not a valid TODO state" value))
  13096. (if (or (not value)
  13097. (not (string-match "\\S-" value)))
  13098. (setq value 'none))
  13099. (org-todo value)
  13100. (org-set-tags nil 'align))
  13101. ((equal property "PRIORITY")
  13102. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  13103. (string-to-char value) ?\ ))
  13104. (org-set-tags nil 'align))
  13105. ((equal property "SCHEDULED")
  13106. (if (re-search-forward org-scheduled-time-regexp end t)
  13107. (cond
  13108. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13109. ((eq value 'later) (org-timestamp-change 1 'day))
  13110. (t (call-interactively 'org-schedule)))
  13111. (call-interactively 'org-schedule)))
  13112. ((equal property "DEADLINE")
  13113. (if (re-search-forward org-deadline-time-regexp end t)
  13114. (cond
  13115. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13116. ((eq value 'later) (org-timestamp-change 1 'day))
  13117. (t (call-interactively 'org-deadline)))
  13118. (call-interactively 'org-deadline)))
  13119. ((member property org-special-properties)
  13120. (error "The %s property can not yet be set with `org-entry-put'"
  13121. property))
  13122. (t ; a non-special property
  13123. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  13124. (setq range (org-get-property-block beg end 'force))
  13125. (goto-char (car range))
  13126. (if (re-search-forward
  13127. (org-re-property property) (cdr range) t)
  13128. (progn
  13129. (delete-region (match-beginning 0) (match-end 0))
  13130. (goto-char (match-beginning 0)))
  13131. (goto-char (cdr range))
  13132. (insert "\n")
  13133. (backward-char 1)
  13134. (org-indent-line-function))
  13135. (insert ":" property ":")
  13136. (and value (insert " " value))
  13137. (org-indent-line-function)))))
  13138. (run-hook-with-args 'org-property-changed-functions property value)))
  13139. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  13140. "Get all property keys in the current buffer.
  13141. With INCLUDE-SPECIALS, also list the special properties that reflect things
  13142. like tags and TODO state.
  13143. With INCLUDE-DEFAULTS, also include properties that has special meaning
  13144. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
  13145. and others.
  13146. With INCLUDE-COLUMNS, also include property names given in COLUMN
  13147. formats in the current buffer."
  13148. (let (rtn range cfmt s p)
  13149. (save-excursion
  13150. (save-restriction
  13151. (widen)
  13152. (goto-char (point-min))
  13153. (while (re-search-forward org-property-start-re nil t)
  13154. (setq range (org-get-property-block))
  13155. (goto-char (car range))
  13156. (while (re-search-forward
  13157. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  13158. (cdr range) t)
  13159. (add-to-list 'rtn (org-match-string-no-properties 1)))
  13160. (outline-next-heading))))
  13161. (when include-specials
  13162. (setq rtn (append org-special-properties rtn)))
  13163. (when include-defaults
  13164. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  13165. (add-to-list 'rtn org-effort-property))
  13166. (when include-columns
  13167. (save-excursion
  13168. (save-restriction
  13169. (widen)
  13170. (goto-char (point-min))
  13171. (while (re-search-forward
  13172. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  13173. nil t)
  13174. (setq cfmt (match-string 2) s 0)
  13175. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  13176. cfmt s)
  13177. (setq s (match-end 0)
  13178. p (match-string 1 cfmt))
  13179. (unless (or (equal p "ITEM")
  13180. (member p org-special-properties))
  13181. (add-to-list 'rtn (match-string 1 cfmt))))))))
  13182. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  13183. (defun org-property-values (key)
  13184. "Return a list of all values of property KEY in the current buffer."
  13185. (save-excursion
  13186. (save-restriction
  13187. (widen)
  13188. (goto-char (point-min))
  13189. (let ((re (org-re-property key))
  13190. values)
  13191. (while (re-search-forward re nil t)
  13192. (add-to-list 'values (org-trim (match-string 1))))
  13193. (delete "" values)))))
  13194. (defun org-insert-property-drawer ()
  13195. "Insert a property drawer into the current entry."
  13196. (org-back-to-heading t)
  13197. (looking-at org-outline-regexp)
  13198. (let ((indent (if org-adapt-indentation
  13199. (- (match-end 0) (match-beginning 0))
  13200. 0))
  13201. (beg (point))
  13202. (re (concat "^[ \t]*" org-keyword-time-regexp))
  13203. end hiddenp)
  13204. (outline-next-heading)
  13205. (setq end (point))
  13206. (goto-char beg)
  13207. (while (re-search-forward re end t))
  13208. (setq hiddenp (outline-invisible-p))
  13209. (end-of-line 1)
  13210. (and (equal (char-after) ?\n) (forward-char 1))
  13211. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  13212. (if (member (match-string 1) '("CLOCK:" ":END:"))
  13213. ;; just skip this line
  13214. (beginning-of-line 2)
  13215. ;; Drawer start, find the end
  13216. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  13217. (beginning-of-line 1)))
  13218. (org-skip-over-state-notes)
  13219. (skip-chars-backward " \t\n\r")
  13220. (if (eq (char-before) ?*) (forward-char 1))
  13221. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  13222. (beginning-of-line 0)
  13223. (org-indent-to-column indent)
  13224. (beginning-of-line 2)
  13225. (org-indent-to-column indent)
  13226. (beginning-of-line 0)
  13227. (if hiddenp
  13228. (save-excursion
  13229. (org-back-to-heading t)
  13230. (hide-entry))
  13231. (org-flag-drawer t))))
  13232. (defun org-insert-drawer (&optional arg drawer)
  13233. "Insert a drawer at point.
  13234. Optional argument DRAWER, when non-nil, is a string representing
  13235. drawer's name. Otherwise, the user is prompted for a name.
  13236. If a region is active, insert the drawer around that region
  13237. instead.
  13238. Point is left between drawer's boundaries."
  13239. (interactive "P")
  13240. (let* ((logbook (if (stringp org-log-into-drawer) org-log-into-drawer
  13241. "LOGBOOK"))
  13242. ;; SYSTEM-DRAWERS is a list of drawer names that are used
  13243. ;; internally by Org. They are meant to be inserted
  13244. ;; automatically.
  13245. (system-drawers `("CLOCK" ,logbook "PROPERTIES"))
  13246. ;; Remove system drawers from list. Note: For some reason,
  13247. ;; `org-completing-read' ignores the predicate while
  13248. ;; `completing-read' handles it fine.
  13249. (drawer (if arg "PROPERTIES"
  13250. (or drawer
  13251. (completing-read
  13252. "Drawer: " org-drawers
  13253. (lambda (d) (not (member d system-drawers))))))))
  13254. (cond
  13255. ;; With C-u, fall back on `org-insert-property-drawer'
  13256. (arg (org-insert-property-drawer))
  13257. ;; With an active region, insert a drawer at point.
  13258. ((not (org-region-active-p))
  13259. (progn
  13260. (unless (bolp) (insert "\n"))
  13261. (insert (format ":%s:\n\n:END:\n" drawer))
  13262. (forward-line -2)))
  13263. ;; Otherwise, insert the drawer at point
  13264. (t
  13265. (let ((rbeg (region-beginning))
  13266. (rend (copy-marker (region-end))))
  13267. (unwind-protect
  13268. (progn
  13269. (goto-char rbeg)
  13270. (beginning-of-line)
  13271. (when (save-excursion
  13272. (re-search-forward org-outline-regexp-bol rend t))
  13273. (error "Drawers cannot contain headlines"))
  13274. ;; Position point at the beginning of the first
  13275. ;; non-blank line in region. Insert drawer's opening
  13276. ;; there, then indent it.
  13277. (org-skip-whitespace)
  13278. (beginning-of-line)
  13279. (insert ":" drawer ":\n")
  13280. (forward-line -1)
  13281. (indent-for-tab-command)
  13282. ;; Move point to the beginning of the first blank line
  13283. ;; after the last non-blank line in region. Insert
  13284. ;; drawer's closing, then indent it.
  13285. (goto-char rend)
  13286. (skip-chars-backward " \r\t\n")
  13287. (insert "\n:END:")
  13288. (indent-for-tab-command)
  13289. (unless (eolp) (insert "\n")))
  13290. ;; Clear marker, whatever the outcome of insertion is.
  13291. (set-marker rend nil)))))))
  13292. (defvar org-property-set-functions-alist nil
  13293. "Property set function alist.
  13294. Each entry should have the following format:
  13295. (PROPERTY . READ-FUNCTION)
  13296. The read function will be called with the same argument as
  13297. `org-completing-read'.")
  13298. (defun org-set-property-function (property)
  13299. "Get the function that should be used to set PROPERTY.
  13300. This is computed according to `org-property-set-functions-alist'."
  13301. (or (cdr (assoc property org-property-set-functions-alist))
  13302. 'org-completing-read))
  13303. (defun org-read-property-value (property)
  13304. "Read PROPERTY value from user."
  13305. (let* ((completion-ignore-case t)
  13306. (allowed (org-property-get-allowed-values nil property 'table))
  13307. (cur (org-entry-get nil property))
  13308. (prompt (concat property " value"
  13309. (if (and cur (string-match "\\S-" cur))
  13310. (concat " [" cur "]") "") ": "))
  13311. (set-function (org-set-property-function property))
  13312. (val (if allowed
  13313. (funcall set-function prompt allowed nil
  13314. (not (get-text-property 0 'org-unrestricted
  13315. (caar allowed))))
  13316. (let (org-completion-use-ido org-completion-use-iswitchb)
  13317. (funcall set-function prompt
  13318. (mapcar 'list (org-property-values property))
  13319. nil nil "" nil cur)))))
  13320. (if (equal val "")
  13321. cur
  13322. val)))
  13323. (defvar org-last-set-property nil)
  13324. (defun org-read-property-name ()
  13325. "Read a property name."
  13326. (let* ((completion-ignore-case t)
  13327. (keys (org-buffer-property-keys nil t t))
  13328. (default-prop (or (save-excursion
  13329. (save-match-data
  13330. (beginning-of-line)
  13331. (and (looking-at "^\\s-*:\\([^:\n]+\\):")
  13332. (null (string= (match-string 1) "END"))
  13333. (match-string 1))))
  13334. org-last-set-property))
  13335. (property (org-icompleting-read
  13336. (concat "Property"
  13337. (if default-prop (concat " [" default-prop "]") "")
  13338. ": ")
  13339. (mapcar 'list keys)
  13340. nil nil nil nil
  13341. default-prop
  13342. )))
  13343. (if (member property keys)
  13344. property
  13345. (or (cdr (assoc (downcase property)
  13346. (mapcar (lambda (x) (cons (downcase x) x))
  13347. keys)))
  13348. property))))
  13349. (defun org-set-property (property value)
  13350. "In the current entry, set PROPERTY to VALUE.
  13351. When called interactively, this will prompt for a property name, offering
  13352. completion on existing and default properties. And then it will prompt
  13353. for a value, offering completion either on allowed values (via an inherited
  13354. xxx_ALL property) or on existing values in other instances of this property
  13355. in the current file."
  13356. (interactive (list nil nil))
  13357. (let* ((property (or property (org-read-property-name)))
  13358. (value (or value (org-read-property-value property)))
  13359. (fn (assoc property org-properties-postprocess-alist)))
  13360. (setq org-last-set-property property)
  13361. ;; Possibly postprocess the inserted value:
  13362. (when fn (setq value (funcall (cadr fn) value)))
  13363. (unless (equal (org-entry-get nil property) value)
  13364. (org-entry-put nil property value))))
  13365. (defun org-delete-property (property)
  13366. "In the current entry, delete PROPERTY."
  13367. (interactive
  13368. (let* ((completion-ignore-case t)
  13369. (prop (org-icompleting-read "Property: "
  13370. (org-entry-properties nil 'standard))))
  13371. (list prop)))
  13372. (message "Property %s %s" property
  13373. (if (org-entry-delete nil property)
  13374. "deleted"
  13375. "was not present in the entry")))
  13376. (defun org-delete-property-globally (property)
  13377. "Remove PROPERTY globally, from all entries."
  13378. (interactive
  13379. (let* ((completion-ignore-case t)
  13380. (prop (org-icompleting-read
  13381. "Globally remove property: "
  13382. (mapcar 'list (org-buffer-property-keys)))))
  13383. (list prop)))
  13384. (save-excursion
  13385. (save-restriction
  13386. (widen)
  13387. (goto-char (point-min))
  13388. (let ((cnt 0))
  13389. (while (re-search-forward
  13390. (org-re-property property)
  13391. nil t)
  13392. (setq cnt (1+ cnt))
  13393. (replace-match ""))
  13394. (message "Property \"%s\" removed from %d entries" property cnt)))))
  13395. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  13396. (defun org-compute-property-at-point ()
  13397. "Compute the property at point.
  13398. This looks for an enclosing column format, extracts the operator and
  13399. then applies it to the property in the column format's scope."
  13400. (interactive)
  13401. (unless (org-at-property-p)
  13402. (error "Not at a property"))
  13403. (let ((prop (org-match-string-no-properties 2)))
  13404. (org-columns-get-format-and-top-level)
  13405. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  13406. (error "No operator defined for property %s" prop))
  13407. (org-columns-compute prop)))
  13408. (defvar org-property-allowed-value-functions nil
  13409. "Hook for functions supplying allowed values for a specific property.
  13410. The functions must take a single argument, the name of the property, and
  13411. return a flat list of allowed values. If \":ETC\" is one of
  13412. the values, this means that these values are intended as defaults for
  13413. completion, but that other values should be allowed too.
  13414. The functions must return nil if they are not responsible for this
  13415. property.")
  13416. (defun org-property-get-allowed-values (pom property &optional table)
  13417. "Get allowed values for the property PROPERTY.
  13418. When TABLE is non-nil, return an alist that can directly be used for
  13419. completion."
  13420. (let (vals)
  13421. (cond
  13422. ((equal property "TODO")
  13423. (setq vals (org-with-point-at pom
  13424. (append org-todo-keywords-1 '("")))))
  13425. ((equal property "PRIORITY")
  13426. (let ((n org-lowest-priority))
  13427. (while (>= n org-highest-priority)
  13428. (push (char-to-string n) vals)
  13429. (setq n (1- n)))))
  13430. ((member property org-special-properties))
  13431. ((setq vals (run-hook-with-args-until-success
  13432. 'org-property-allowed-value-functions property)))
  13433. (t
  13434. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  13435. (when (and vals (string-match "\\S-" vals))
  13436. (setq vals (car (read-from-string (concat "(" vals ")"))))
  13437. (setq vals (mapcar (lambda (x)
  13438. (cond ((stringp x) x)
  13439. ((numberp x) (number-to-string x))
  13440. ((symbolp x) (symbol-name x))
  13441. (t "???")))
  13442. vals)))))
  13443. (when (member ":ETC" vals)
  13444. (setq vals (remove ":ETC" vals))
  13445. (org-add-props (car vals) '(org-unrestricted t)))
  13446. (if table (mapcar 'list vals) vals)))
  13447. (defun org-property-previous-allowed-value (&optional previous)
  13448. "Switch to the next allowed value for this property."
  13449. (interactive)
  13450. (org-property-next-allowed-value t))
  13451. (defun org-property-next-allowed-value (&optional previous)
  13452. "Switch to the next allowed value for this property."
  13453. (interactive)
  13454. (unless (org-at-property-p)
  13455. (error "Not at a property"))
  13456. (let* ((key (match-string 2))
  13457. (value (match-string 3))
  13458. (allowed (or (org-property-get-allowed-values (point) key)
  13459. (and (member value '("[ ]" "[-]" "[X]"))
  13460. '("[ ]" "[X]"))))
  13461. nval)
  13462. (unless allowed
  13463. (error "Allowed values for this property have not been defined"))
  13464. (if previous (setq allowed (reverse allowed)))
  13465. (if (member value allowed)
  13466. (setq nval (car (cdr (member value allowed)))))
  13467. (setq nval (or nval (car allowed)))
  13468. (if (equal nval value)
  13469. (error "Only one allowed value for this property"))
  13470. (org-at-property-p)
  13471. (replace-match (concat " :" key ": " nval) t t)
  13472. (org-indent-line-function)
  13473. (beginning-of-line 1)
  13474. (skip-chars-forward " \t")
  13475. (run-hook-with-args 'org-property-changed-functions key nval)))
  13476. (defun org-find-olp (path &optional this-buffer)
  13477. "Return a marker pointing to the entry at outline path OLP.
  13478. If anything goes wrong, throw an error.
  13479. You can wrap this call to catch the error like this:
  13480. (condition-case msg
  13481. (org-mobile-locate-entry (match-string 4))
  13482. (error (nth 1 msg)))
  13483. The return value will then be either a string with the error message,
  13484. or a marker if everything is OK.
  13485. If THIS-BUFFER is set, the outline path does not contain a file,
  13486. only headings."
  13487. (let* ((file (if this-buffer buffer-file-name (pop path)))
  13488. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  13489. (level 1)
  13490. (lmin 1)
  13491. (lmax 1)
  13492. limit re end found pos heading cnt flevel)
  13493. (unless buffer (error "File not found :%s" file))
  13494. (with-current-buffer buffer
  13495. (save-excursion
  13496. (save-restriction
  13497. (widen)
  13498. (setq limit (point-max))
  13499. (goto-char (point-min))
  13500. (while (setq heading (pop path))
  13501. (setq re (format org-complex-heading-regexp-format
  13502. (regexp-quote heading)))
  13503. (setq cnt 0 pos (point))
  13504. (while (re-search-forward re end t)
  13505. (setq level (- (match-end 1) (match-beginning 1)))
  13506. (if (and (>= level lmin) (<= level lmax))
  13507. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  13508. (when (= cnt 0) (error "Heading not found on level %d: %s"
  13509. lmax heading))
  13510. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  13511. lmax heading))
  13512. (goto-char found)
  13513. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  13514. (setq end (save-excursion (org-end-of-subtree t t))))
  13515. (when (org-at-heading-p)
  13516. (move-marker (make-marker) (point))))))))
  13517. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  13518. "Find node HEADING in BUFFER.
  13519. Return a marker to the heading if it was found, or nil if not.
  13520. If POS-ONLY is set, return just the position instead of a marker.
  13521. The heading text must match exact, but it may have a TODO keyword,
  13522. a priority cookie and tags in the standard locations."
  13523. (with-current-buffer (or buffer (current-buffer))
  13524. (save-excursion
  13525. (save-restriction
  13526. (widen)
  13527. (goto-char (point-min))
  13528. (let (case-fold-search)
  13529. (if (re-search-forward
  13530. (format org-complex-heading-regexp-format
  13531. (regexp-quote heading)) nil t)
  13532. (if pos-only
  13533. (match-beginning 0)
  13534. (move-marker (make-marker) (match-beginning 0)))))))))
  13535. (defun org-find-exact-heading-in-directory (heading &optional dir)
  13536. "Find Org node headline HEADING in all .org files in directory DIR.
  13537. When the target headline is found, return a marker to this location."
  13538. (let ((files (directory-files (or dir default-directory)
  13539. nil "\\`[^.#].*\\.org\\'"))
  13540. file visiting m buffer)
  13541. (catch 'found
  13542. (while (setq file (pop files))
  13543. (message "trying %s" file)
  13544. (setq visiting (org-find-base-buffer-visiting file))
  13545. (setq buffer (or visiting (find-file-noselect file)))
  13546. (setq m (org-find-exact-headline-in-buffer
  13547. heading buffer))
  13548. (when (and (not m) (not visiting)) (kill-buffer buffer))
  13549. (and m (throw 'found m))))))
  13550. (defun org-find-entry-with-id (ident)
  13551. "Locate the entry that contains the ID property with exact value IDENT.
  13552. IDENT can be a string, a symbol or a number, this function will search for
  13553. the string representation of it.
  13554. Return the position where this entry starts, or nil if there is no such entry."
  13555. (interactive "sID: ")
  13556. (let ((id (cond
  13557. ((stringp ident) ident)
  13558. ((symbol-name ident) (symbol-name ident))
  13559. ((numberp ident) (number-to-string ident))
  13560. (t (error "IDENT %s must be a string, symbol or number" ident))))
  13561. (case-fold-search nil))
  13562. (save-excursion
  13563. (save-restriction
  13564. (widen)
  13565. (goto-char (point-min))
  13566. (when (re-search-forward
  13567. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  13568. nil t)
  13569. (org-back-to-heading t)
  13570. (point))))))
  13571. ;;;; Timestamps
  13572. (defvar org-last-changed-timestamp nil)
  13573. (defvar org-last-inserted-timestamp nil
  13574. "The last time stamp inserted with `org-insert-time-stamp'.")
  13575. (defvar org-time-was-given) ; dynamically scoped parameter
  13576. (defvar org-end-time-was-given) ; dynamically scoped parameter
  13577. (defvar org-ts-what) ; dynamically scoped parameter
  13578. (defun org-time-stamp (arg &optional inactive)
  13579. "Prompt for a date/time and insert a time stamp.
  13580. If the user specifies a time like HH:MM, or if this command is called
  13581. with a prefix argument, the time stamp will contain date and time.
  13582. Otherwise, only the date will be included. All parts of a date not
  13583. specified by the user will be filled in from the current date/time.
  13584. So if you press just return without typing anything, the time stamp
  13585. will represent the current date/time. If there is already a timestamp
  13586. at the cursor, it will be modified."
  13587. (interactive "P")
  13588. (let* ((ts nil)
  13589. (default-time
  13590. ;; Default time is either today, or, when entering a range,
  13591. ;; the range start.
  13592. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  13593. (save-excursion
  13594. (re-search-backward
  13595. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  13596. (- (point) 20) t)))
  13597. (apply 'encode-time (org-parse-time-string (match-string 1)))
  13598. (current-time)))
  13599. (default-input (and ts (org-get-compact-tod ts)))
  13600. (repeater (save-excursion
  13601. (save-match-data
  13602. (beginning-of-line)
  13603. (when (re-search-forward
  13604. "\\([.+-]+[0-9]+[dwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[dwmy]\\)?\\) ?"
  13605. (save-excursion (progn (end-of-line) (point))) t)
  13606. (match-string 0)))))
  13607. org-time-was-given org-end-time-was-given time)
  13608. (cond
  13609. ((and (org-at-timestamp-p t)
  13610. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  13611. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  13612. (insert "--")
  13613. (setq time (let ((this-command this-command))
  13614. (org-read-date arg 'totime nil nil
  13615. default-time default-input inactive)))
  13616. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  13617. ((org-at-timestamp-p t)
  13618. (setq time (let ((this-command this-command))
  13619. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  13620. (when (org-at-timestamp-p t) ; just to get the match data
  13621. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  13622. (replace-match "")
  13623. (setq org-last-changed-timestamp
  13624. (org-insert-time-stamp
  13625. time (or org-time-was-given arg)
  13626. inactive nil nil (list org-end-time-was-given)))
  13627. (when repeater (goto-char (1- (point))) (insert " " repeater)
  13628. (setq org-last-changed-timestamp
  13629. (concat (substring org-last-inserted-timestamp 0 -1)
  13630. " " repeater ">"))))
  13631. (message "Timestamp updated"))
  13632. (t
  13633. (setq time (let ((this-command this-command))
  13634. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  13635. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  13636. nil nil (list org-end-time-was-given))))))
  13637. ;; FIXME: can we use this for something else, like computing time differences?
  13638. (defun org-get-compact-tod (s)
  13639. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  13640. (let* ((t1 (match-string 1 s))
  13641. (h1 (string-to-number (match-string 2 s)))
  13642. (m1 (string-to-number (match-string 3 s)))
  13643. (t2 (and (match-end 4) (match-string 5 s)))
  13644. (h2 (and t2 (string-to-number (match-string 6 s))))
  13645. (m2 (and t2 (string-to-number (match-string 7 s))))
  13646. dh dm)
  13647. (if (not t2)
  13648. t1
  13649. (setq dh (- h2 h1) dm (- m2 m1))
  13650. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  13651. (concat t1 "+" (number-to-string dh)
  13652. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  13653. (defun org-time-stamp-inactive (&optional arg)
  13654. "Insert an inactive time stamp.
  13655. An inactive time stamp is enclosed in square brackets instead of angle
  13656. brackets. It is inactive in the sense that it does not trigger agenda entries,
  13657. does not link to the calendar and cannot be changed with the S-cursor keys.
  13658. So these are more for recording a certain time/date."
  13659. (interactive "P")
  13660. (org-time-stamp arg 'inactive))
  13661. (defvar org-date-ovl (make-overlay 1 1))
  13662. (overlay-put org-date-ovl 'face 'org-warning)
  13663. (org-detach-overlay org-date-ovl)
  13664. (defvar org-ans1) ; dynamically scoped parameter
  13665. (defvar org-ans2) ; dynamically scoped parameter
  13666. (defvar org-plain-time-of-day-regexp) ; defined below
  13667. (defvar org-overriding-default-time nil) ; dynamically scoped
  13668. (defvar org-read-date-overlay nil)
  13669. (defvar org-dcst nil) ; dynamically scoped
  13670. (defvar org-read-date-history nil)
  13671. (defvar org-read-date-final-answer nil)
  13672. (defvar org-read-date-analyze-futurep nil)
  13673. (defvar org-read-date-analyze-forced-year nil)
  13674. (defun org-read-date (&optional with-time to-time from-string prompt
  13675. default-time default-input inactive)
  13676. "Read a date, possibly a time, and make things smooth for the user.
  13677. The prompt will suggest to enter an ISO date, but you can also enter anything
  13678. which will at least partially be understood by `parse-time-string'.
  13679. Unrecognized parts of the date will default to the current day, month, year,
  13680. hour and minute. If this command is called to replace a timestamp at point,
  13681. or to enter the second timestamp of a range, the default time is taken
  13682. from the existing stamp. Furthermore, the command prefers the future,
  13683. so if you are giving a date where the year is not given, and the day-month
  13684. combination is already past in the current year, it will assume you
  13685. mean next year. For details, see the manual. A few examples:
  13686. 3-2-5 --> 2003-02-05
  13687. feb 15 --> currentyear-02-15
  13688. 2/15 --> currentyear-02-15
  13689. sep 12 9 --> 2009-09-12
  13690. 12:45 --> today 12:45
  13691. 22 sept 0:34 --> currentyear-09-22 0:34
  13692. 12 --> currentyear-currentmonth-12
  13693. Fri --> nearest Friday (today or later)
  13694. etc.
  13695. Furthermore you can specify a relative date by giving, as the *first* thing
  13696. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  13697. change in days weeks, months, years.
  13698. With a single plus or minus, the date is relative to today. With a double
  13699. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  13700. +4d --> four days from today
  13701. +4 --> same as above
  13702. +2w --> two weeks from today
  13703. ++5 --> five days from default date
  13704. The function understands only English month and weekday abbreviations,
  13705. but this can be configured with the variables `parse-time-months' and
  13706. `parse-time-weekdays'.
  13707. While prompting, a calendar is popped up - you can also select the
  13708. date with the mouse (button 1). The calendar shows a period of three
  13709. months. To scroll it to other months, use the keys `>' and `<'.
  13710. If you don't like the calendar, turn it off with
  13711. \(setq org-read-date-popup-calendar nil)
  13712. With optional argument TO-TIME, the date will immediately be converted
  13713. to an internal time.
  13714. With an optional argument WITH-TIME, the prompt will suggest to also
  13715. insert a time. Note that when WITH-TIME is not set, you can still
  13716. enter a time, and this function will inform the calling routine about
  13717. this change. The calling routine may then choose to change the format
  13718. used to insert the time stamp into the buffer to include the time.
  13719. With optional argument FROM-STRING, read from this string instead from
  13720. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  13721. the time/date that is used for everything that is not specified by the
  13722. user."
  13723. (require 'parse-time)
  13724. (let* ((org-time-stamp-rounding-minutes
  13725. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  13726. (org-dcst org-display-custom-times)
  13727. (ct (org-current-time))
  13728. (def (or org-overriding-default-time default-time ct))
  13729. (defdecode (decode-time def))
  13730. (dummy (progn
  13731. (when (< (nth 2 defdecode) org-extend-today-until)
  13732. (setcar (nthcdr 2 defdecode) -1)
  13733. (setcar (nthcdr 1 defdecode) 59)
  13734. (setq def (apply 'encode-time defdecode)
  13735. defdecode (decode-time def)))))
  13736. (calendar-frame-setup nil)
  13737. (calendar-setup nil)
  13738. (calendar-move-hook nil)
  13739. (calendar-view-diary-initially-flag nil)
  13740. (calendar-view-holidays-initially-flag nil)
  13741. (timestr (format-time-string
  13742. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  13743. (prompt (concat (if prompt (concat prompt " ") "")
  13744. (format "Date+time [%s]: " timestr)))
  13745. ans (org-ans0 "") org-ans1 org-ans2 final)
  13746. (cond
  13747. (from-string (setq ans from-string))
  13748. (org-read-date-popup-calendar
  13749. (save-excursion
  13750. (save-window-excursion
  13751. (calendar)
  13752. (unwind-protect
  13753. (progn
  13754. (calendar-forward-day (- (time-to-days def)
  13755. (calendar-absolute-from-gregorian
  13756. (calendar-current-date))))
  13757. (org-eval-in-calendar nil t)
  13758. (let* ((old-map (current-local-map))
  13759. (map (copy-keymap calendar-mode-map))
  13760. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  13761. (org-defkey map (kbd "RET") 'org-calendar-select)
  13762. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  13763. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  13764. (org-defkey minibuffer-local-map [(meta shift left)]
  13765. (lambda () (interactive)
  13766. (org-eval-in-calendar '(calendar-backward-month 1))))
  13767. (org-defkey minibuffer-local-map [(meta shift right)]
  13768. (lambda () (interactive)
  13769. (org-eval-in-calendar '(calendar-forward-month 1))))
  13770. (org-defkey minibuffer-local-map [(meta shift up)]
  13771. (lambda () (interactive)
  13772. (org-eval-in-calendar '(calendar-backward-year 1))))
  13773. (org-defkey minibuffer-local-map [(meta shift down)]
  13774. (lambda () (interactive)
  13775. (org-eval-in-calendar '(calendar-forward-year 1))))
  13776. (org-defkey minibuffer-local-map [?\e (shift left)]
  13777. (lambda () (interactive)
  13778. (org-eval-in-calendar '(calendar-backward-month 1))))
  13779. (org-defkey minibuffer-local-map [?\e (shift right)]
  13780. (lambda () (interactive)
  13781. (org-eval-in-calendar '(calendar-forward-month 1))))
  13782. (org-defkey minibuffer-local-map [?\e (shift up)]
  13783. (lambda () (interactive)
  13784. (org-eval-in-calendar '(calendar-backward-year 1))))
  13785. (org-defkey minibuffer-local-map [?\e (shift down)]
  13786. (lambda () (interactive)
  13787. (org-eval-in-calendar '(calendar-forward-year 1))))
  13788. (org-defkey minibuffer-local-map [(shift up)]
  13789. (lambda () (interactive)
  13790. (org-eval-in-calendar '(calendar-backward-week 1))))
  13791. (org-defkey minibuffer-local-map [(shift down)]
  13792. (lambda () (interactive)
  13793. (org-eval-in-calendar '(calendar-forward-week 1))))
  13794. (org-defkey minibuffer-local-map [(shift left)]
  13795. (lambda () (interactive)
  13796. (org-eval-in-calendar '(calendar-backward-day 1))))
  13797. (org-defkey minibuffer-local-map [(shift right)]
  13798. (lambda () (interactive)
  13799. (org-eval-in-calendar '(calendar-forward-day 1))))
  13800. (org-defkey minibuffer-local-map ">"
  13801. (lambda () (interactive)
  13802. (org-eval-in-calendar '(scroll-calendar-left 1))))
  13803. (org-defkey minibuffer-local-map "<"
  13804. (lambda () (interactive)
  13805. (org-eval-in-calendar '(scroll-calendar-right 1))))
  13806. (org-defkey minibuffer-local-map "\C-v"
  13807. (lambda () (interactive)
  13808. (org-eval-in-calendar
  13809. '(calendar-scroll-left-three-months 1))))
  13810. (org-defkey minibuffer-local-map "\M-v"
  13811. (lambda () (interactive)
  13812. (org-eval-in-calendar
  13813. '(calendar-scroll-right-three-months 1))))
  13814. (run-hooks 'org-read-date-minibuffer-setup-hook)
  13815. (unwind-protect
  13816. (progn
  13817. (use-local-map map)
  13818. (setq org-read-date-inactive inactive)
  13819. (add-hook 'post-command-hook 'org-read-date-display)
  13820. (setq org-ans0 (read-string prompt default-input
  13821. 'org-read-date-history nil))
  13822. ;; org-ans0: from prompt
  13823. ;; org-ans1: from mouse click
  13824. ;; org-ans2: from calendar motion
  13825. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  13826. (remove-hook 'post-command-hook 'org-read-date-display)
  13827. (use-local-map old-map)
  13828. (when org-read-date-overlay
  13829. (delete-overlay org-read-date-overlay)
  13830. (setq org-read-date-overlay nil)))))
  13831. (bury-buffer "*Calendar*")))))
  13832. (t ; Naked prompt only
  13833. (unwind-protect
  13834. (setq ans (read-string prompt default-input
  13835. 'org-read-date-history timestr))
  13836. (when org-read-date-overlay
  13837. (delete-overlay org-read-date-overlay)
  13838. (setq org-read-date-overlay nil)))))
  13839. (setq final (org-read-date-analyze ans def defdecode))
  13840. (when org-read-date-analyze-forced-year
  13841. (message "Year was forced into %s"
  13842. (if org-read-date-force-compatible-dates
  13843. "compatible range (1970-2037)"
  13844. "range representable on this machine"))
  13845. (ding))
  13846. ;; One round trip to get rid of 34th of August and stuff like that....
  13847. (setq final (decode-time (apply 'encode-time final)))
  13848. (setq org-read-date-final-answer ans)
  13849. (if to-time
  13850. (apply 'encode-time final)
  13851. (if (and (boundp 'org-time-was-given) org-time-was-given)
  13852. (format "%04d-%02d-%02d %02d:%02d"
  13853. (nth 5 final) (nth 4 final) (nth 3 final)
  13854. (nth 2 final) (nth 1 final))
  13855. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  13856. (defvar def)
  13857. (defvar defdecode)
  13858. (defvar with-time)
  13859. (defvar org-read-date-inactive)
  13860. (defun org-read-date-display ()
  13861. "Display the current date prompt interpretation in the minibuffer."
  13862. (when org-read-date-display-live
  13863. (when org-read-date-overlay
  13864. (delete-overlay org-read-date-overlay))
  13865. (let ((p (point)))
  13866. (end-of-line 1)
  13867. (while (not (equal (buffer-substring
  13868. (max (point-min) (- (point) 4)) (point))
  13869. " "))
  13870. (insert " "))
  13871. (goto-char p))
  13872. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  13873. " " (or org-ans1 org-ans2)))
  13874. (org-end-time-was-given nil)
  13875. (f (org-read-date-analyze ans def defdecode))
  13876. (fmts (if org-dcst
  13877. org-time-stamp-custom-formats
  13878. org-time-stamp-formats))
  13879. (fmt (if (or with-time
  13880. (and (boundp 'org-time-was-given) org-time-was-given))
  13881. (cdr fmts)
  13882. (car fmts)))
  13883. (txt (format-time-string fmt (apply 'encode-time f)))
  13884. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  13885. (txt (concat "=> " txt)))
  13886. (when (and org-end-time-was-given
  13887. (string-match org-plain-time-of-day-regexp txt))
  13888. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  13889. org-end-time-was-given
  13890. (substring txt (match-end 0)))))
  13891. (when org-read-date-analyze-futurep
  13892. (setq txt (concat txt " (=>F)")))
  13893. (setq org-read-date-overlay
  13894. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  13895. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  13896. (defun org-read-date-analyze (ans def defdecode)
  13897. "Analyze the combined answer of the date prompt."
  13898. ;; FIXME: cleanup and comment
  13899. (let ((nowdecode (decode-time (current-time)))
  13900. delta deltan deltaw deltadef year month day
  13901. hour minute second wday pm h2 m2 tl wday1
  13902. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  13903. (setq org-read-date-analyze-futurep nil
  13904. org-read-date-analyze-forced-year nil)
  13905. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  13906. (setq ans "+0"))
  13907. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  13908. (setq ans (replace-match "" t t ans)
  13909. deltan (car delta)
  13910. deltaw (nth 1 delta)
  13911. deltadef (nth 2 delta)))
  13912. ;; Check if there is an iso week date in there
  13913. ;; If yes, store the info and postpone interpreting it until the rest
  13914. ;; of the parsing is done
  13915. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  13916. (setq iso-year (if (match-end 1)
  13917. (org-small-year-to-year
  13918. (string-to-number (match-string 1 ans))))
  13919. iso-weekday (if (match-end 3)
  13920. (string-to-number (match-string 3 ans)))
  13921. iso-week (string-to-number (match-string 2 ans)))
  13922. (setq ans (replace-match "" t t ans)))
  13923. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  13924. (when (string-match
  13925. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  13926. (setq year (if (match-end 2)
  13927. (string-to-number (match-string 2 ans))
  13928. (progn (setq kill-year t)
  13929. (string-to-number (format-time-string "%Y"))))
  13930. month (string-to-number (match-string 3 ans))
  13931. day (string-to-number (match-string 4 ans)))
  13932. (if (< year 100) (setq year (+ 2000 year)))
  13933. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  13934. t nil ans)))
  13935. ;; Help matching dotted european dates
  13936. (when (string-match
  13937. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\. ?\\([1-9][0-9][0-9][0-9]\\)?" ans)
  13938. (setq year (if (match-end 3)
  13939. (string-to-number (match-string 3 ans))
  13940. (progn (setq kill-year t)
  13941. (string-to-number (format-time-string "%Y"))))
  13942. day (string-to-number (match-string 1 ans))
  13943. month (string-to-number (match-string 2 ans))
  13944. ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  13945. t nil ans)))
  13946. ;; Help matching american dates, like 5/30 or 5/30/7
  13947. (when (string-match
  13948. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  13949. (setq year (if (match-end 4)
  13950. (string-to-number (match-string 4 ans))
  13951. (progn (setq kill-year t)
  13952. (string-to-number (format-time-string "%Y"))))
  13953. month (string-to-number (match-string 1 ans))
  13954. day (string-to-number (match-string 2 ans)))
  13955. (if (< year 100) (setq year (+ 2000 year)))
  13956. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  13957. t nil ans)))
  13958. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  13959. ;; If there is a time with am/pm, and *no* time without it, we convert
  13960. ;; so that matching will be successful.
  13961. (loop for i from 1 to 2 do ; twice, for end time as well
  13962. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  13963. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  13964. (setq hour (string-to-number (match-string 1 ans))
  13965. minute (if (match-end 3)
  13966. (string-to-number (match-string 3 ans))
  13967. 0)
  13968. pm (equal ?p
  13969. (string-to-char (downcase (match-string 4 ans)))))
  13970. (if (and (= hour 12) (not pm))
  13971. (setq hour 0)
  13972. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  13973. (setq ans (replace-match (format "%02d:%02d" hour minute)
  13974. t t ans))))
  13975. ;; Check if a time range is given as a duration
  13976. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  13977. (setq hour (string-to-number (match-string 1 ans))
  13978. h2 (+ hour (string-to-number (match-string 3 ans)))
  13979. minute (string-to-number (match-string 2 ans))
  13980. m2 (+ minute (if (match-end 5) (string-to-number
  13981. (match-string 5 ans))0)))
  13982. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  13983. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  13984. t t ans)))
  13985. ;; Check if there is a time range
  13986. (when (boundp 'org-end-time-was-given)
  13987. (setq org-time-was-given nil)
  13988. (when (and (string-match org-plain-time-of-day-regexp ans)
  13989. (match-end 8))
  13990. (setq org-end-time-was-given (match-string 8 ans))
  13991. (setq ans (concat (substring ans 0 (match-beginning 7))
  13992. (substring ans (match-end 7))))))
  13993. (setq tl (parse-time-string ans)
  13994. day (or (nth 3 tl) (nth 3 defdecode))
  13995. month (or (nth 4 tl)
  13996. (if (and org-read-date-prefer-future
  13997. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  13998. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  13999. (nth 4 defdecode)))
  14000. year (or (and (not kill-year) (nth 5 tl))
  14001. (if (and org-read-date-prefer-future
  14002. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  14003. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  14004. (nth 5 defdecode)))
  14005. hour (or (nth 2 tl) (nth 2 defdecode))
  14006. minute (or (nth 1 tl) (nth 1 defdecode))
  14007. second (or (nth 0 tl) 0)
  14008. wday (nth 6 tl))
  14009. (when (and (eq org-read-date-prefer-future 'time)
  14010. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  14011. (equal day (nth 3 nowdecode))
  14012. (equal month (nth 4 nowdecode))
  14013. (equal year (nth 5 nowdecode))
  14014. (nth 2 tl)
  14015. (or (< (nth 2 tl) (nth 2 nowdecode))
  14016. (and (= (nth 2 tl) (nth 2 nowdecode))
  14017. (nth 1 tl)
  14018. (< (nth 1 tl) (nth 1 nowdecode)))))
  14019. (setq day (1+ day)
  14020. futurep t))
  14021. ;; Special date definitions below
  14022. (cond
  14023. (iso-week
  14024. ;; There was an iso week
  14025. (require 'cal-iso)
  14026. (setq futurep nil)
  14027. (setq year (or iso-year year)
  14028. day (or iso-weekday wday 1)
  14029. wday nil ; to make sure that the trigger below does not match
  14030. iso-date (calendar-gregorian-from-absolute
  14031. (calendar-absolute-from-iso
  14032. (list iso-week day year))))
  14033. ; FIXME: Should we also push ISO weeks into the future?
  14034. ; (when (and org-read-date-prefer-future
  14035. ; (not iso-year)
  14036. ; (< (calendar-absolute-from-gregorian iso-date)
  14037. ; (time-to-days (current-time))))
  14038. ; (setq year (1+ year)
  14039. ; iso-date (calendar-gregorian-from-absolute
  14040. ; (calendar-absolute-from-iso
  14041. ; (list iso-week day year)))))
  14042. (setq month (car iso-date)
  14043. year (nth 2 iso-date)
  14044. day (nth 1 iso-date)))
  14045. (deltan
  14046. (setq futurep nil)
  14047. (unless deltadef
  14048. (let ((now (decode-time (current-time))))
  14049. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  14050. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  14051. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  14052. ((equal deltaw "m") (setq month (+ month deltan)))
  14053. ((equal deltaw "y") (setq year (+ year deltan)))))
  14054. ((and wday (not (nth 3 tl)))
  14055. (setq futurep nil)
  14056. ;; Weekday was given, but no day, so pick that day in the week
  14057. ;; on or after the derived date.
  14058. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  14059. (unless (equal wday wday1)
  14060. (setq day (+ day (% (- wday wday1 -7) 7))))))
  14061. (if (and (boundp 'org-time-was-given)
  14062. (nth 2 tl))
  14063. (setq org-time-was-given t))
  14064. (if (< year 100) (setq year (+ 2000 year)))
  14065. ;; Check of the date is representable
  14066. (if org-read-date-force-compatible-dates
  14067. (progn
  14068. (if (< year 1970)
  14069. (setq year 1970 org-read-date-analyze-forced-year t))
  14070. (if (> year 2037)
  14071. (setq year 2037 org-read-date-analyze-forced-year t)))
  14072. (condition-case nil
  14073. (ignore (encode-time second minute hour day month year))
  14074. (error
  14075. (setq year (nth 5 defdecode))
  14076. (setq org-read-date-analyze-forced-year t))))
  14077. (setq org-read-date-analyze-futurep futurep)
  14078. (list second minute hour day month year)))
  14079. (defvar parse-time-weekdays)
  14080. (defun org-read-date-get-relative (s today default)
  14081. "Check string S for special relative date string.
  14082. TODAY and DEFAULT are internal times, for today and for a default.
  14083. Return shift list (N what def-flag)
  14084. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  14085. N is the number of WHATs to shift.
  14086. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  14087. the DEFAULT date rather than TODAY."
  14088. (require 'parse-time)
  14089. (when (and
  14090. (string-match
  14091. (concat
  14092. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  14093. "\\([0-9]+\\)?"
  14094. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  14095. "\\([ \t]\\|$\\)") s)
  14096. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  14097. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  14098. (string-to-char (substring (match-string 1 s) -1))
  14099. ?+))
  14100. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  14101. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  14102. (what (if (match-end 3) (match-string 3 s) "d"))
  14103. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  14104. (date (if rel default today))
  14105. (wday (nth 6 (decode-time date)))
  14106. delta)
  14107. (if wday1
  14108. (progn
  14109. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  14110. (if (= dir ?-) (setq delta (- delta 7)))
  14111. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  14112. (list delta "d" rel))
  14113. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  14114. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  14115. "Turn a user-specified date into the internal representation.
  14116. The internal representation needed by the calendar is (month day year).
  14117. This is a wrapper to handle the brain-dead convention in calendar that
  14118. user function argument order change dependent on argument order."
  14119. (if (boundp 'calendar-date-style)
  14120. (cond
  14121. ((eq calendar-date-style 'american)
  14122. (list arg1 arg2 arg3))
  14123. ((eq calendar-date-style 'european)
  14124. (list arg2 arg1 arg3))
  14125. ((eq calendar-date-style 'iso)
  14126. (list arg2 arg3 arg1)))
  14127. (with-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  14128. (if (org-bound-and-true-p european-calendar-style)
  14129. (list arg2 arg1 arg3)
  14130. (list arg1 arg2 arg3)))))
  14131. (defun org-eval-in-calendar (form &optional keepdate)
  14132. "Eval FORM in the calendar window and return to current window.
  14133. Also, store the cursor date in variable org-ans2."
  14134. (let ((sf (selected-frame))
  14135. (sw (selected-window)))
  14136. (select-window (get-buffer-window "*Calendar*" t))
  14137. (eval form)
  14138. (when (and (not keepdate) (calendar-cursor-to-date))
  14139. (let* ((date (calendar-cursor-to-date))
  14140. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14141. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  14142. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  14143. (select-window sw)
  14144. (org-select-frame-set-input-focus sf)))
  14145. (defun org-calendar-select ()
  14146. "Return to `org-read-date' with the date currently selected.
  14147. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  14148. (interactive)
  14149. (when (calendar-cursor-to-date)
  14150. (let* ((date (calendar-cursor-to-date))
  14151. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14152. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  14153. (if (active-minibuffer-window) (exit-minibuffer))))
  14154. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  14155. "Insert a date stamp for the date given by the internal TIME.
  14156. WITH-HM means use the stamp format that includes the time of the day.
  14157. INACTIVE means use square brackets instead of angular ones, so that the
  14158. stamp will not contribute to the agenda.
  14159. PRE and POST are optional strings to be inserted before and after the
  14160. stamp.
  14161. The command returns the inserted time stamp."
  14162. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  14163. stamp)
  14164. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  14165. (insert-before-markers (or pre ""))
  14166. (when (listp extra)
  14167. (setq extra (car extra))
  14168. (if (and (stringp extra)
  14169. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  14170. (setq extra (format "-%02d:%02d"
  14171. (string-to-number (match-string 1 extra))
  14172. (string-to-number (match-string 2 extra))))
  14173. (setq extra nil)))
  14174. (when extra
  14175. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  14176. (insert-before-markers (setq stamp (format-time-string fmt time)))
  14177. (insert-before-markers (or post ""))
  14178. (setq org-last-inserted-timestamp stamp)))
  14179. (defun org-toggle-time-stamp-overlays ()
  14180. "Toggle the use of custom time stamp formats."
  14181. (interactive)
  14182. (setq org-display-custom-times (not org-display-custom-times))
  14183. (unless org-display-custom-times
  14184. (let ((p (point-min)) (bmp (buffer-modified-p)))
  14185. (while (setq p (next-single-property-change p 'display))
  14186. (if (and (get-text-property p 'display)
  14187. (eq (get-text-property p 'face) 'org-date))
  14188. (remove-text-properties
  14189. p (setq p (next-single-property-change p 'display))
  14190. '(display t))))
  14191. (set-buffer-modified-p bmp)))
  14192. (if (featurep 'xemacs)
  14193. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  14194. (org-restart-font-lock)
  14195. (setq org-table-may-need-update t)
  14196. (if org-display-custom-times
  14197. (message "Time stamps are overlaid with custom format")
  14198. (message "Time stamp overlays removed")))
  14199. (defun org-display-custom-time (beg end)
  14200. "Overlay modified time stamp format over timestamp between BEG and END."
  14201. (let* ((ts (buffer-substring beg end))
  14202. t1 w1 with-hm tf time str w2 (off 0))
  14203. (save-match-data
  14204. (setq t1 (org-parse-time-string ts t))
  14205. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)?\\'" ts)
  14206. (setq off (- (match-end 0) (match-beginning 0)))))
  14207. (setq end (- end off))
  14208. (setq w1 (- end beg)
  14209. with-hm (and (nth 1 t1) (nth 2 t1))
  14210. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  14211. time (org-fix-decoded-time t1)
  14212. str (org-add-props
  14213. (format-time-string
  14214. (substring tf 1 -1) (apply 'encode-time time))
  14215. nil 'mouse-face 'highlight)
  14216. w2 (length str))
  14217. (if (not (= w2 w1))
  14218. (add-text-properties (1+ beg) (+ 2 beg)
  14219. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  14220. (if (featurep 'xemacs)
  14221. (progn
  14222. (put-text-property beg end 'invisible t)
  14223. (put-text-property beg end 'end-glyph (make-glyph str)))
  14224. (put-text-property beg end 'display str))))
  14225. (defun org-translate-time (string)
  14226. "Translate all timestamps in STRING to custom format.
  14227. But do this only if the variable `org-display-custom-times' is set."
  14228. (when org-display-custom-times
  14229. (save-match-data
  14230. (let* ((start 0)
  14231. (re org-ts-regexp-both)
  14232. t1 with-hm inactive tf time str beg end)
  14233. (while (setq start (string-match re string start))
  14234. (setq beg (match-beginning 0)
  14235. end (match-end 0)
  14236. t1 (save-match-data
  14237. (org-parse-time-string (substring string beg end) t))
  14238. with-hm (and (nth 1 t1) (nth 2 t1))
  14239. inactive (equal (substring string beg (1+ beg)) "[")
  14240. tf (funcall (if with-hm 'cdr 'car)
  14241. org-time-stamp-custom-formats)
  14242. time (org-fix-decoded-time t1)
  14243. str (format-time-string
  14244. (concat
  14245. (if inactive "[" "<") (substring tf 1 -1)
  14246. (if inactive "]" ">"))
  14247. (apply 'encode-time time))
  14248. string (replace-match str t t string)
  14249. start (+ start (length str)))))))
  14250. string)
  14251. (defun org-fix-decoded-time (time)
  14252. "Set 0 instead of nil for the first 6 elements of time.
  14253. Don't touch the rest."
  14254. (let ((n 0))
  14255. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  14256. (defun org-days-to-time (timestamp-string)
  14257. "Difference between TIMESTAMP-STRING and now in days."
  14258. (- (time-to-days (org-time-string-to-time timestamp-string))
  14259. (time-to-days (current-time))))
  14260. (defun org-deadline-close (timestamp-string &optional ndays)
  14261. "Is the time in TIMESTAMP-STRING close to the current date?"
  14262. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  14263. (and (< (org-days-to-time timestamp-string) ndays)
  14264. (not (org-entry-is-done-p))))
  14265. (defun org-get-wdays (ts)
  14266. "Get the deadline lead time appropriate for timestring TS."
  14267. (cond
  14268. ((<= org-deadline-warning-days 0)
  14269. ;; 0 or negative, enforce this value no matter what
  14270. (- org-deadline-warning-days))
  14271. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
  14272. ;; lead time is specified.
  14273. (floor (* (string-to-number (match-string 1 ts))
  14274. (cdr (assoc (match-string 2 ts)
  14275. '(("d" . 1) ("w" . 7)
  14276. ("m" . 30.4) ("y" . 365.25)))))))
  14277. ;; go for the default.
  14278. (t org-deadline-warning-days)))
  14279. (defun org-calendar-select-mouse (ev)
  14280. "Return to `org-read-date' with the date currently selected.
  14281. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  14282. (interactive "e")
  14283. (mouse-set-point ev)
  14284. (when (calendar-cursor-to-date)
  14285. (let* ((date (calendar-cursor-to-date))
  14286. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14287. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  14288. (if (active-minibuffer-window) (exit-minibuffer))))
  14289. (defun org-check-deadlines (ndays)
  14290. "Check if there are any deadlines due or past due.
  14291. A deadline is considered due if it happens within `org-deadline-warning-days'
  14292. days from today's date. If the deadline appears in an entry marked DONE,
  14293. it is not shown. The prefix arg NDAYS can be used to test that many
  14294. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  14295. (interactive "P")
  14296. (let* ((org-warn-days
  14297. (cond
  14298. ((equal ndays '(4)) 100000)
  14299. (ndays (prefix-numeric-value ndays))
  14300. (t (abs org-deadline-warning-days))))
  14301. (case-fold-search nil)
  14302. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  14303. (callback
  14304. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  14305. (message "%d deadlines past-due or due within %d days"
  14306. (org-occur regexp nil callback)
  14307. org-warn-days)))
  14308. (defun org-check-before-date (date)
  14309. "Check if there are deadlines or scheduled entries before DATE."
  14310. (interactive (list (org-read-date)))
  14311. (let ((case-fold-search nil)
  14312. (regexp (concat "\\<\\(" org-deadline-string
  14313. "\\|" org-scheduled-string
  14314. "\\) *<\\([^>]+\\)>"))
  14315. (callback
  14316. (lambda () (time-less-p
  14317. (org-time-string-to-time (match-string 2))
  14318. (org-time-string-to-time date)))))
  14319. (message "%d entries before %s"
  14320. (org-occur regexp nil callback) date)))
  14321. (defun org-check-after-date (date)
  14322. "Check if there are deadlines or scheduled entries after DATE."
  14323. (interactive (list (org-read-date)))
  14324. (let ((case-fold-search nil)
  14325. (regexp (concat "\\<\\(" org-deadline-string
  14326. "\\|" org-scheduled-string
  14327. "\\) *<\\([^>]+\\)>"))
  14328. (callback
  14329. (lambda () (not
  14330. (time-less-p
  14331. (org-time-string-to-time (match-string 2))
  14332. (org-time-string-to-time date))))))
  14333. (message "%d entries after %s"
  14334. (org-occur regexp nil callback) date)))
  14335. (defun org-check-dates-range (start-date end-date)
  14336. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  14337. (interactive (list (org-read-date nil nil nil "Range starts")
  14338. (org-read-date nil nil nil "Range end")))
  14339. (let ((case-fold-search nil)
  14340. (regexp (concat "\\<\\(" org-deadline-string
  14341. "\\|" org-scheduled-string
  14342. "\\) *<\\([^>]+\\)>"))
  14343. (callback
  14344. (lambda ()
  14345. (let ((match (match-string 2)))
  14346. (and
  14347. (not (time-less-p
  14348. (org-time-string-to-time match)
  14349. (org-time-string-to-time start-date)))
  14350. (time-less-p
  14351. (org-time-string-to-time match)
  14352. (org-time-string-to-time end-date)))))))
  14353. (message "%d entries between %s and %s"
  14354. (org-occur regexp nil callback) start-date end-date)))
  14355. (defun org-evaluate-time-range (&optional to-buffer)
  14356. "Evaluate a time range by computing the difference between start and end.
  14357. Normally the result is just printed in the echo area, but with prefix arg
  14358. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  14359. If the time range is actually in a table, the result is inserted into the
  14360. next column.
  14361. For time difference computation, a year is assumed to be exactly 365
  14362. days in order to avoid rounding problems."
  14363. (interactive "P")
  14364. (or
  14365. (org-clock-update-time-maybe)
  14366. (save-excursion
  14367. (unless (org-at-date-range-p t)
  14368. (goto-char (point-at-bol))
  14369. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  14370. (if (not (org-at-date-range-p t))
  14371. (error "Not at a time-stamp range, and none found in current line")))
  14372. (let* ((ts1 (match-string 1))
  14373. (ts2 (match-string 2))
  14374. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  14375. (match-end (match-end 0))
  14376. (time1 (org-time-string-to-time ts1))
  14377. (time2 (org-time-string-to-time ts2))
  14378. (t1 (org-float-time time1))
  14379. (t2 (org-float-time time2))
  14380. (diff (abs (- t2 t1)))
  14381. (negative (< (- t2 t1) 0))
  14382. ;; (ys (floor (* 365 24 60 60)))
  14383. (ds (* 24 60 60))
  14384. (hs (* 60 60))
  14385. (fy "%dy %dd %02d:%02d")
  14386. (fy1 "%dy %dd")
  14387. (fd "%dd %02d:%02d")
  14388. (fd1 "%dd")
  14389. (fh "%02d:%02d")
  14390. y d h m align)
  14391. (if havetime
  14392. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  14393. y 0
  14394. d (floor (/ diff ds)) diff (mod diff ds)
  14395. h (floor (/ diff hs)) diff (mod diff hs)
  14396. m (floor (/ diff 60)))
  14397. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  14398. y 0
  14399. d (floor (+ (/ diff ds) 0.5))
  14400. h 0 m 0))
  14401. (if (not to-buffer)
  14402. (message "%s" (org-make-tdiff-string y d h m))
  14403. (if (org-at-table-p)
  14404. (progn
  14405. (goto-char match-end)
  14406. (setq align t)
  14407. (and (looking-at " *|") (goto-char (match-end 0))))
  14408. (goto-char match-end))
  14409. (if (looking-at
  14410. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  14411. (replace-match ""))
  14412. (if negative (insert " -"))
  14413. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  14414. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  14415. (insert " " (format fh h m))))
  14416. (if align (org-table-align))
  14417. (message "Time difference inserted")))))
  14418. (defun org-make-tdiff-string (y d h m)
  14419. (let ((fmt "")
  14420. (l nil))
  14421. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  14422. l (push y l)))
  14423. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  14424. l (push d l)))
  14425. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  14426. l (push h l)))
  14427. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  14428. l (push m l)))
  14429. (apply 'format fmt (nreverse l))))
  14430. (defun org-time-string-to-time (s &optional buffer pos)
  14431. (condition-case errdata
  14432. (apply 'encode-time (org-parse-time-string s))
  14433. (error (error "Bad timestamp `%s'%s\nError was: %s"
  14434. s (if (not (and buffer pos))
  14435. ""
  14436. (format " at %d in buffer `%s'" pos buffer))
  14437. (cdr errdata)))))
  14438. (defun org-time-string-to-seconds (s)
  14439. (org-float-time (org-time-string-to-time s)))
  14440. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  14441. "Convert a time stamp to an absolute day number.
  14442. If there is a specifier for a cyclic time stamp, get the closest date to
  14443. DAYNR.
  14444. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  14445. The variable date is bound by the calendar when this is called."
  14446. (cond
  14447. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  14448. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  14449. daynr
  14450. (+ daynr 1000)))
  14451. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  14452. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  14453. (time-to-days (current-time))) (match-string 0 s)
  14454. prefer show-all))
  14455. (t (time-to-days
  14456. (condition-case errdata
  14457. (apply 'encode-time (org-parse-time-string s))
  14458. (error (error "Bad timestamp `%s'%s\nError was: %s"
  14459. s (if (not (and buffer pos))
  14460. ""
  14461. (format " at %d in buffer `%s'" pos buffer))
  14462. (cdr errdata))))))))
  14463. (defun org-days-to-iso-week (days)
  14464. "Return the iso week number."
  14465. (require 'cal-iso)
  14466. (car (calendar-iso-from-absolute days)))
  14467. (defun org-small-year-to-year (year)
  14468. "Convert 2-digit years into 4-digit years.
  14469. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  14470. The year 2000 cannot be abbreviated. Any year larger than 99
  14471. is returned unchanged."
  14472. (if (< year 38)
  14473. (setq year (+ 2000 year))
  14474. (if (< year 100)
  14475. (setq year (+ 1900 year))))
  14476. year)
  14477. (defun org-time-from-absolute (d)
  14478. "Return the time corresponding to date D.
  14479. D may be an absolute day number, or a calendar-type list (month day year)."
  14480. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  14481. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  14482. (defun org-calendar-holiday ()
  14483. "List of holidays, for Diary display in Org-mode."
  14484. (require 'holidays)
  14485. (let ((hl (funcall
  14486. (if (fboundp 'calendar-check-holidays)
  14487. 'calendar-check-holidays 'check-calendar-holidays) date)))
  14488. (if hl (mapconcat 'identity hl "; "))))
  14489. (defun org-diary-sexp-entry (sexp entry date)
  14490. "Process a SEXP diary ENTRY for DATE."
  14491. (require 'diary-lib)
  14492. (let ((result (if calendar-debug-sexp
  14493. (let ((stack-trace-on-error t))
  14494. (eval (car (read-from-string sexp))))
  14495. (condition-case nil
  14496. (eval (car (read-from-string sexp)))
  14497. (error
  14498. (beep)
  14499. (message "Bad sexp at line %d in %s: %s"
  14500. (org-current-line)
  14501. (buffer-file-name) sexp)
  14502. (sleep-for 2))))))
  14503. (cond ((stringp result) (split-string result "; "))
  14504. ((and (consp result)
  14505. (not (consp (cdr result)))
  14506. (stringp (cdr result))) (cdr result))
  14507. ((and (consp result)
  14508. (stringp (car result))) result)
  14509. (result entry)
  14510. (t nil))))
  14511. (defun org-diary-to-ical-string (frombuf)
  14512. "Get iCalendar entries from diary entries in buffer FROMBUF.
  14513. This uses the icalendar.el library."
  14514. (let* ((tmpdir (if (featurep 'xemacs)
  14515. (temp-directory)
  14516. temporary-file-directory))
  14517. (tmpfile (make-temp-name
  14518. (expand-file-name "orgics" tmpdir)))
  14519. buf rtn b e)
  14520. (with-current-buffer frombuf
  14521. (icalendar-export-region (point-min) (point-max) tmpfile)
  14522. (setq buf (find-buffer-visiting tmpfile))
  14523. (set-buffer buf)
  14524. (goto-char (point-min))
  14525. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  14526. (setq b (match-beginning 0)))
  14527. (goto-char (point-max))
  14528. (if (re-search-backward "^END:VEVENT" nil t)
  14529. (setq e (match-end 0)))
  14530. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  14531. (kill-buffer buf)
  14532. (delete-file tmpfile)
  14533. rtn))
  14534. (defun org-closest-date (start current change prefer show-all)
  14535. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  14536. When PREFER is `past', return a date that is either CURRENT or past.
  14537. When PREFER is `future', return a date that is either CURRENT or future.
  14538. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  14539. ;; Make the proper lists from the dates
  14540. (catch 'exit
  14541. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  14542. dn dw sday cday n1 n2 n0
  14543. d m y y1 y2 date1 date2 nmonths nm ny m2)
  14544. (setq start (org-date-to-gregorian start)
  14545. current (org-date-to-gregorian
  14546. (if show-all
  14547. current
  14548. (time-to-days (current-time))))
  14549. sday (calendar-absolute-from-gregorian start)
  14550. cday (calendar-absolute-from-gregorian current))
  14551. (if (<= cday sday) (throw 'exit sday))
  14552. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  14553. (setq dn (string-to-number (match-string 1 change))
  14554. dw (cdr (assoc (match-string 2 change) a1)))
  14555. (error "Invalid change specifier: %s" change))
  14556. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  14557. (cond
  14558. ((eq dw 'day)
  14559. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  14560. n2 (+ n1 dn)))
  14561. ((eq dw 'year)
  14562. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  14563. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  14564. (setq date1 (list m d y1)
  14565. n1 (calendar-absolute-from-gregorian date1)
  14566. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  14567. n2 (calendar-absolute-from-gregorian date2)))
  14568. ((eq dw 'month)
  14569. ;; approx number of month between the two dates
  14570. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  14571. ;; How often does dn fit in there?
  14572. (setq d (nth 1 start) m (car start) y (nth 2 start)
  14573. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  14574. m (+ m nm)
  14575. ny (floor (/ m 12))
  14576. y (+ y ny)
  14577. m (- m (* ny 12)))
  14578. (while (> m 12) (setq m (- m 12) y (1+ y)))
  14579. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  14580. (setq m2 (+ m dn) y2 y)
  14581. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  14582. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  14583. (while (<= n2 cday)
  14584. (setq n1 n2 m m2 y y2)
  14585. (setq m2 (+ m dn) y2 y)
  14586. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  14587. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  14588. ;; Make sure n1 is the earlier date
  14589. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  14590. (if show-all
  14591. (cond
  14592. ((eq prefer 'past) (if (= cday n2) n2 n1))
  14593. ((eq prefer 'future) (if (= cday n1) n1 n2))
  14594. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  14595. (cond
  14596. ((eq prefer 'past) (if (= cday n2) n2 n1))
  14597. ((eq prefer 'future) (if (= cday n1) n1 n2))
  14598. (t (if (= cday n1) n1 n2)))))))
  14599. (defun org-date-to-gregorian (date)
  14600. "Turn any specification of DATE into a Gregorian date for the calendar."
  14601. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  14602. ((and (listp date) (= (length date) 3)) date)
  14603. ((stringp date)
  14604. (setq date (org-parse-time-string date))
  14605. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  14606. ((listp date)
  14607. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  14608. (defun org-parse-time-string (s &optional nodefault)
  14609. "Parse the standard Org-mode time string.
  14610. This should be a lot faster than the normal `parse-time-string'.
  14611. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  14612. hour and minute fields will be nil if not given."
  14613. (if (string-match org-ts-regexp0 s)
  14614. (list 0
  14615. (if (or (match-beginning 8) (not nodefault))
  14616. (string-to-number (or (match-string 8 s) "0")))
  14617. (if (or (match-beginning 7) (not nodefault))
  14618. (string-to-number (or (match-string 7 s) "0")))
  14619. (string-to-number (match-string 4 s))
  14620. (string-to-number (match-string 3 s))
  14621. (string-to-number (match-string 2 s))
  14622. nil nil nil)
  14623. (error "Not a standard Org-mode time string: %s" s)))
  14624. (defun org-timestamp-up (&optional arg)
  14625. "Increase the date item at the cursor by one.
  14626. If the cursor is on the year, change the year. If it is on the month,
  14627. the day or the time, change that.
  14628. With prefix ARG, change by that many units."
  14629. (interactive "p")
  14630. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  14631. (defun org-timestamp-down (&optional arg)
  14632. "Decrease the date item at the cursor by one.
  14633. If the cursor is on the year, change the year. If it is on the month,
  14634. the day or the time, change that.
  14635. With prefix ARG, change by that many units."
  14636. (interactive "p")
  14637. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  14638. (defun org-timestamp-up-day (&optional arg)
  14639. "Increase the date in the time stamp by one day.
  14640. With prefix ARG, change that many days."
  14641. (interactive "p")
  14642. (if (and (not (org-at-timestamp-p t))
  14643. (org-at-heading-p))
  14644. (org-todo 'up)
  14645. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  14646. (defun org-timestamp-down-day (&optional arg)
  14647. "Decrease the date in the time stamp by one day.
  14648. With prefix ARG, change that many days."
  14649. (interactive "p")
  14650. (if (and (not (org-at-timestamp-p t))
  14651. (org-at-heading-p))
  14652. (org-todo 'down)
  14653. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  14654. (defun org-at-timestamp-p (&optional inactive-ok)
  14655. "Determine if the cursor is in or at a timestamp."
  14656. (interactive)
  14657. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  14658. (pos (point))
  14659. (ans (or (looking-at tsr)
  14660. (save-excursion
  14661. (skip-chars-backward "^[<\n\r\t")
  14662. (if (> (point) (point-min)) (backward-char 1))
  14663. (and (looking-at tsr)
  14664. (> (- (match-end 0) pos) -1))))))
  14665. (and ans
  14666. (boundp 'org-ts-what)
  14667. (setq org-ts-what
  14668. (cond
  14669. ((= pos (match-beginning 0)) 'bracket)
  14670. ;; Point is considered to be "on the bracket" whether
  14671. ;; it's really on it or right after it.
  14672. ((or (= pos (1- (match-end 0)))
  14673. (= pos (match-end 0))) 'bracket)
  14674. ((org-pos-in-match-range pos 2) 'year)
  14675. ((org-pos-in-match-range pos 3) 'month)
  14676. ((org-pos-in-match-range pos 7) 'hour)
  14677. ((org-pos-in-match-range pos 8) 'minute)
  14678. ((or (org-pos-in-match-range pos 4)
  14679. (org-pos-in-match-range pos 5)) 'day)
  14680. ((and (> pos (or (match-end 8) (match-end 5)))
  14681. (< pos (match-end 0)))
  14682. (- pos (or (match-end 8) (match-end 5))))
  14683. (t 'day))))
  14684. ans))
  14685. (defun org-toggle-timestamp-type ()
  14686. "Toggle the type (<active> or [inactive]) of a time stamp."
  14687. (interactive)
  14688. (when (org-at-timestamp-p t)
  14689. (let ((beg (match-beginning 0)) (end (match-end 0))
  14690. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  14691. (save-excursion
  14692. (goto-char beg)
  14693. (while (re-search-forward "[][<>]" end t)
  14694. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  14695. t t)))
  14696. (message "Timestamp is now %sactive"
  14697. (if (equal (char-after beg) ?<) "" "in")))))
  14698. (defun org-timestamp-change (n &optional what updown)
  14699. "Change the date in the time stamp at point.
  14700. The date will be changed by N times WHAT. WHAT can be `day', `month',
  14701. `year', `minute', `second'. If WHAT is not given, the cursor position
  14702. in the timestamp determines what will be changed."
  14703. (let ((origin (point)) origin-cat
  14704. with-hm inactive
  14705. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  14706. org-ts-what
  14707. extra rem
  14708. ts time time0)
  14709. (if (not (org-at-timestamp-p t))
  14710. (error "Not at a timestamp"))
  14711. (if (and (not what) (eq org-ts-what 'bracket))
  14712. (org-toggle-timestamp-type)
  14713. ;; Point isn't on brackets. Remember the part of the time-stamp
  14714. ;; the point was in. Indeed, size of time-stamps may change,
  14715. ;; but point must be kept in the same category nonetheless.
  14716. (setq origin-cat org-ts-what)
  14717. (if (and (not what) (not (eq org-ts-what 'day))
  14718. org-display-custom-times
  14719. (get-text-property (point) 'display)
  14720. (not (get-text-property (1- (point)) 'display)))
  14721. (setq org-ts-what 'day))
  14722. (setq org-ts-what (or what org-ts-what)
  14723. inactive (= (char-after (match-beginning 0)) ?\[)
  14724. ts (match-string 0))
  14725. (replace-match "")
  14726. (if (string-match
  14727. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)*\\)[]>]"
  14728. ts)
  14729. (setq extra (match-string 1 ts)))
  14730. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  14731. (setq with-hm t))
  14732. (setq time0 (org-parse-time-string ts))
  14733. (when (and updown
  14734. (eq org-ts-what 'minute)
  14735. (not current-prefix-arg))
  14736. ;; This looks like s-up and s-down. Change by one rounding step.
  14737. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  14738. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  14739. (setcar (cdr time0) (+ (nth 1 time0)
  14740. (if (> n 0) (- rem) (- dm rem))))))
  14741. (setq time
  14742. (encode-time (or (car time0) 0)
  14743. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  14744. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  14745. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  14746. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  14747. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  14748. (nthcdr 6 time0)))
  14749. (when (and (member org-ts-what '(hour minute))
  14750. extra
  14751. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  14752. (setq extra (org-modify-ts-extra
  14753. extra
  14754. (if (eq org-ts-what 'hour) 2 5)
  14755. n dm)))
  14756. (when (integerp org-ts-what)
  14757. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  14758. (if (eq what 'calendar)
  14759. (let ((cal-date (org-get-date-from-calendar)))
  14760. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  14761. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  14762. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  14763. (setcar time0 (or (car time0) 0))
  14764. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  14765. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  14766. (setq time (apply 'encode-time time0))))
  14767. ;; Insert the new time-stamp, and ensure point stays in the same
  14768. ;; category as before (i.e. not after the last position in that
  14769. ;; category).
  14770. (let ((pos (point)))
  14771. ;; Stay before inserted string. `save-excursion' is of no use.
  14772. (setq org-last-changed-timestamp
  14773. (org-insert-time-stamp time with-hm inactive nil nil extra))
  14774. (goto-char pos))
  14775. (save-match-data
  14776. (looking-at org-ts-regexp3)
  14777. (goto-char (cond
  14778. ;; `day' category ends before `hour' if any, or at
  14779. ;; the end of the day name.
  14780. ((eq origin-cat 'day)
  14781. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  14782. ((eq origin-cat 'hour) (min (match-end 7) origin))
  14783. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  14784. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  14785. ;; `year' and `month' have both fixed size: point
  14786. ;; couldn't have moved into another part.
  14787. (t origin))))
  14788. ;; Update clock if on a CLOCK line.
  14789. (org-clock-update-time-maybe)
  14790. ;; Try to recenter the calendar window, if any.
  14791. (if (and org-calendar-follow-timestamp-change
  14792. (get-buffer-window "*Calendar*" t)
  14793. (memq org-ts-what '(day month year)))
  14794. (org-recenter-calendar (time-to-days time))))))
  14795. (defun org-modify-ts-extra (s pos n dm)
  14796. "Change the different parts of the lead-time and repeat fields in timestamp."
  14797. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  14798. ng h m new rem)
  14799. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  14800. (cond
  14801. ((or (org-pos-in-match-range pos 2)
  14802. (org-pos-in-match-range pos 3))
  14803. (setq m (string-to-number (match-string 3 s))
  14804. h (string-to-number (match-string 2 s)))
  14805. (if (org-pos-in-match-range pos 2)
  14806. (setq h (+ h n))
  14807. (setq n (* dm (org-no-warnings (signum n))))
  14808. (when (not (= 0 (setq rem (% m dm))))
  14809. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  14810. (setq m (+ m n)))
  14811. (if (< m 0) (setq m (+ m 60) h (1- h)))
  14812. (if (> m 59) (setq m (- m 60) h (1+ h)))
  14813. (setq h (min 24 (max 0 h)))
  14814. (setq ng 1 new (format "-%02d:%02d" h m)))
  14815. ((org-pos-in-match-range pos 6)
  14816. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  14817. ((org-pos-in-match-range pos 5)
  14818. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  14819. ((org-pos-in-match-range pos 9)
  14820. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  14821. ((org-pos-in-match-range pos 8)
  14822. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  14823. (when ng
  14824. (setq s (concat
  14825. (substring s 0 (match-beginning ng))
  14826. new
  14827. (substring s (match-end ng))))))
  14828. s))
  14829. (defun org-recenter-calendar (date)
  14830. "If the calendar is visible, recenter it to DATE."
  14831. (let ((cwin (get-buffer-window "*Calendar*" t)))
  14832. (when cwin
  14833. (let ((calendar-move-hook nil))
  14834. (with-selected-window cwin
  14835. (calendar-goto-date (if (listp date) date
  14836. (calendar-gregorian-from-absolute date))))))))
  14837. (defun org-goto-calendar (&optional arg)
  14838. "Go to the Emacs calendar at the current date.
  14839. If there is a time stamp in the current line, go to that date.
  14840. A prefix ARG can be used to force the current date."
  14841. (interactive "P")
  14842. (let ((tsr org-ts-regexp) diff
  14843. (calendar-move-hook nil)
  14844. (calendar-view-holidays-initially-flag nil)
  14845. (calendar-view-diary-initially-flag nil))
  14846. (if (or (org-at-timestamp-p)
  14847. (save-excursion
  14848. (beginning-of-line 1)
  14849. (looking-at (concat ".*" tsr))))
  14850. (let ((d1 (time-to-days (current-time)))
  14851. (d2 (time-to-days
  14852. (org-time-string-to-time (match-string 1)))))
  14853. (setq diff (- d2 d1))))
  14854. (calendar)
  14855. (calendar-goto-today)
  14856. (if (and diff (not arg)) (calendar-forward-day diff))))
  14857. (defun org-get-date-from-calendar ()
  14858. "Return a list (month day year) of date at point in calendar."
  14859. (with-current-buffer "*Calendar*"
  14860. (save-match-data
  14861. (calendar-cursor-to-date))))
  14862. (defun org-date-from-calendar ()
  14863. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  14864. If there is already a time stamp at the cursor position, update it."
  14865. (interactive)
  14866. (if (org-at-timestamp-p t)
  14867. (org-timestamp-change 0 'calendar)
  14868. (let ((cal-date (org-get-date-from-calendar)))
  14869. (org-insert-time-stamp
  14870. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  14871. (defun org-minutes-to-hh:mm-string (m)
  14872. "Compute H:MM from a number of minutes."
  14873. (let ((h (/ m 60)))
  14874. (setq m (- m (* 60 h)))
  14875. (format org-time-clocksum-format h m)))
  14876. (defun org-hh:mm-string-to-minutes (s)
  14877. "Convert a string H:MM to a number of minutes.
  14878. If the string is just a number, interpret it as minutes.
  14879. In fact, the first hh:mm or number in the string will be taken,
  14880. there can be extra stuff in the string.
  14881. If no number is found, the return value is 0."
  14882. (cond
  14883. ((integerp s) s)
  14884. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  14885. (+ (* (string-to-number (match-string 1 s)) 60)
  14886. (string-to-number (match-string 2 s))))
  14887. ((string-match "\\([0-9]+\\)" s)
  14888. (string-to-number (match-string 1 s)))
  14889. (t 0)))
  14890. (defcustom org-effort-durations
  14891. `(("h" . 60)
  14892. ("d" . ,(* 60 8))
  14893. ("w" . ,(* 60 8 5))
  14894. ("m" . ,(* 60 8 5 4))
  14895. ("y" . ,(* 60 8 5 40)))
  14896. "Conversion factor to minutes for an effort modifier.
  14897. Each entry has the form (MODIFIER . MINUTES).
  14898. In an effort string, a number followed by MODIFIER is multiplied
  14899. by the specified number of MINUTES to obtain an effort in
  14900. minutes.
  14901. For example, if the value of this variable is ((\"hours\" . 60)), then an
  14902. effort string \"2hours\" is equivalent to 120 minutes."
  14903. :group 'org-agenda
  14904. :version "24.1"
  14905. :type '(alist :key-type (string :tag "Modifier")
  14906. :value-type (number :tag "Minutes")))
  14907. (defun org-duration-string-to-minutes (s)
  14908. "Convert a duration string S to minutes.
  14909. A bare number is interpreted as minutes, modifiers can be set by
  14910. customizing `org-effort-durations' (which see).
  14911. Entries containing a colon are interpreted as H:MM by
  14912. `org-hh:mm-string-to-minutes'."
  14913. (let ((result 0)
  14914. (re (concat "\\([0-9]+\\) *\\("
  14915. (regexp-opt (mapcar 'car org-effort-durations))
  14916. "\\)")))
  14917. (while (string-match re s)
  14918. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  14919. (string-to-number (match-string 1 s))))
  14920. (setq s (replace-match "" nil t s)))
  14921. (incf result (org-hh:mm-string-to-minutes s))
  14922. result))
  14923. ;;;; Files
  14924. (defun org-save-all-org-buffers ()
  14925. "Save all Org-mode buffers without user confirmation."
  14926. (interactive)
  14927. (message "Saving all Org-mode buffers...")
  14928. (save-some-buffers t (lambda () (eq major-mode 'org-mode)))
  14929. (when (featurep 'org-id) (org-id-locations-save))
  14930. (message "Saving all Org-mode buffers... done"))
  14931. (defun org-revert-all-org-buffers ()
  14932. "Revert all Org-mode buffers.
  14933. Prompt for confirmation when there are unsaved changes.
  14934. Be sure you know what you are doing before letting this function
  14935. overwrite your changes.
  14936. This function is useful in a setup where one tracks org files
  14937. with a version control system, to revert on one machine after pulling
  14938. changes from another. I believe the procedure must be like this:
  14939. 1. M-x org-save-all-org-buffers
  14940. 2. Pull changes from the other machine, resolve conflicts
  14941. 3. M-x org-revert-all-org-buffers"
  14942. (interactive)
  14943. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  14944. (error "Abort"))
  14945. (save-excursion
  14946. (save-window-excursion
  14947. (mapc
  14948. (lambda (b)
  14949. (when (and (with-current-buffer b (eq major-mode 'org-mode))
  14950. (with-current-buffer b buffer-file-name))
  14951. (org-pop-to-buffer-same-window b)
  14952. (revert-buffer t 'no-confirm)))
  14953. (buffer-list))
  14954. (when (and (featurep 'org-id) org-id-track-globally)
  14955. (org-id-locations-load)))))
  14956. ;;;; Agenda files
  14957. ;;;###autoload
  14958. (defun org-switchb (&optional arg)
  14959. "Switch between Org buffers.
  14960. With one prefix argument, restrict available buffers to files.
  14961. With two prefix arguments, restrict available buffers to agenda files.
  14962. Defaults to `iswitchb' for buffer name completion.
  14963. Set `org-completion-use-ido' to make it use ido instead."
  14964. (interactive "P")
  14965. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  14966. ((equal arg '(16)) (org-buffer-list 'agenda))
  14967. (t (org-buffer-list))))
  14968. (org-completion-use-iswitchb org-completion-use-iswitchb)
  14969. (org-completion-use-ido org-completion-use-ido))
  14970. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  14971. (setq org-completion-use-iswitchb t))
  14972. (org-pop-to-buffer-same-window
  14973. (org-icompleting-read "Org buffer: "
  14974. (mapcar 'list (mapcar 'buffer-name blist))
  14975. nil t))))
  14976. ;;; Define some older names previously used for this functionality
  14977. ;;;###autoload
  14978. (defalias 'org-ido-switchb 'org-switchb)
  14979. ;;;###autoload
  14980. (defalias 'org-iswitchb 'org-switchb)
  14981. (defun org-buffer-list (&optional predicate exclude-tmp)
  14982. "Return a list of Org buffers.
  14983. PREDICATE can be `export', `files' or `agenda'.
  14984. export restrict the list to Export buffers.
  14985. files restrict the list to buffers visiting Org files.
  14986. agenda restrict the list to buffers visiting agenda files.
  14987. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  14988. (let* ((bfn nil)
  14989. (agenda-files (and (eq predicate 'agenda)
  14990. (mapcar 'file-truename (org-agenda-files t))))
  14991. (filter
  14992. (cond
  14993. ((eq predicate 'files)
  14994. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  14995. ((eq predicate 'export)
  14996. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  14997. ((eq predicate 'agenda)
  14998. (lambda (b)
  14999. (with-current-buffer b
  15000. (and (eq major-mode 'org-mode)
  15001. (setq bfn (buffer-file-name b))
  15002. (member (file-truename bfn) agenda-files)))))
  15003. (t (lambda (b) (with-current-buffer b
  15004. (or (eq major-mode 'org-mode)
  15005. (string-match "\*Org .*Export"
  15006. (buffer-name b)))))))))
  15007. (delq nil
  15008. (mapcar
  15009. (lambda(b)
  15010. (if (and (funcall filter b)
  15011. (or (not exclude-tmp)
  15012. (not (string-match "tmp" (buffer-name b)))))
  15013. b
  15014. nil))
  15015. (buffer-list)))))
  15016. (defun org-agenda-files (&optional unrestricted archives)
  15017. "Get the list of agenda files.
  15018. Optional UNRESTRICTED means return the full list even if a restriction
  15019. is currently in place.
  15020. When ARCHIVES is t, include all archive files that are really being
  15021. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  15022. `org-agenda-archives-mode' is t."
  15023. (let ((files
  15024. (cond
  15025. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  15026. ((stringp org-agenda-files) (org-read-agenda-file-list))
  15027. ((listp org-agenda-files) org-agenda-files)
  15028. (t (error "Invalid value of `org-agenda-files'")))))
  15029. (setq files (apply 'append
  15030. (mapcar (lambda (f)
  15031. (if (file-directory-p f)
  15032. (directory-files
  15033. f t org-agenda-file-regexp)
  15034. (list f)))
  15035. files)))
  15036. (when org-agenda-skip-unavailable-files
  15037. (setq files (delq nil
  15038. (mapcar (function
  15039. (lambda (file)
  15040. (and (file-readable-p file) file)))
  15041. files))))
  15042. (when (or (eq archives t)
  15043. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  15044. (setq files (org-add-archive-files files)))
  15045. files))
  15046. (defun org-agenda-file-p (&optional file)
  15047. "Return non-nil, if FILE is an agenda file.
  15048. If FILE is omitted, use the file associated with the current
  15049. buffer."
  15050. (member (or file (buffer-file-name))
  15051. (org-agenda-files t)))
  15052. (defun org-edit-agenda-file-list ()
  15053. "Edit the list of agenda files.
  15054. Depending on setup, this either uses customize to edit the variable
  15055. `org-agenda-files', or it visits the file that is holding the list. In the
  15056. latter case, the buffer is set up in a way that saving it automatically kills
  15057. the buffer and restores the previous window configuration."
  15058. (interactive)
  15059. (if (stringp org-agenda-files)
  15060. (let ((cw (current-window-configuration)))
  15061. (find-file org-agenda-files)
  15062. (org-set-local 'org-window-configuration cw)
  15063. (org-add-hook 'after-save-hook
  15064. (lambda ()
  15065. (set-window-configuration
  15066. (prog1 org-window-configuration
  15067. (kill-buffer (current-buffer))))
  15068. (org-install-agenda-files-menu)
  15069. (message "New agenda file list installed"))
  15070. nil 'local)
  15071. (message "%s" (substitute-command-keys
  15072. "Edit list and finish with \\[save-buffer]")))
  15073. (customize-variable 'org-agenda-files)))
  15074. (defun org-store-new-agenda-file-list (list)
  15075. "Set new value for the agenda file list and save it correctly."
  15076. (if (stringp org-agenda-files)
  15077. (let ((fe (org-read-agenda-file-list t)) b u)
  15078. (while (setq b (find-buffer-visiting org-agenda-files))
  15079. (kill-buffer b))
  15080. (with-temp-file org-agenda-files
  15081. (insert
  15082. (mapconcat
  15083. (lambda (f) ;; Keep un-expanded entries.
  15084. (if (setq u (assoc f fe))
  15085. (cdr u)
  15086. f))
  15087. list "\n")
  15088. "\n")))
  15089. (let ((org-mode-hook nil) (org-inhibit-startup t)
  15090. (org-insert-mode-line-in-empty-file nil))
  15091. (setq org-agenda-files list)
  15092. (customize-save-variable 'org-agenda-files org-agenda-files))))
  15093. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  15094. "Read the list of agenda files from a file.
  15095. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  15096. filenames, used by `org-store-new-agenda-file-list' to write back
  15097. un-expanded file names."
  15098. (when (file-directory-p org-agenda-files)
  15099. (error "`org-agenda-files' cannot be a single directory"))
  15100. (when (stringp org-agenda-files)
  15101. (with-temp-buffer
  15102. (insert-file-contents org-agenda-files)
  15103. (mapcar
  15104. (lambda (f)
  15105. (let ((e (expand-file-name (substitute-in-file-name f)
  15106. org-directory)))
  15107. (if pair-with-expansion
  15108. (cons e f)
  15109. e)))
  15110. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  15111. ;;;###autoload
  15112. (defun org-cycle-agenda-files ()
  15113. "Cycle through the files in `org-agenda-files'.
  15114. If the current buffer visits an agenda file, find the next one in the list.
  15115. If the current buffer does not, find the first agenda file."
  15116. (interactive)
  15117. (let* ((fs (org-agenda-files t))
  15118. (files (append fs (list (car fs))))
  15119. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  15120. file)
  15121. (unless files (error "No agenda files"))
  15122. (catch 'exit
  15123. (while (setq file (pop files))
  15124. (if (equal (file-truename file) tcf)
  15125. (when (car files)
  15126. (find-file (car files))
  15127. (throw 'exit t))))
  15128. (find-file (car fs)))
  15129. (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  15130. (defun org-agenda-file-to-front (&optional to-end)
  15131. "Move/add the current file to the top of the agenda file list.
  15132. If the file is not present in the list, it is added to the front. If it is
  15133. present, it is moved there. With optional argument TO-END, add/move to the
  15134. end of the list."
  15135. (interactive "P")
  15136. (let ((org-agenda-skip-unavailable-files nil)
  15137. (file-alist (mapcar (lambda (x)
  15138. (cons (file-truename x) x))
  15139. (org-agenda-files t)))
  15140. (ctf (file-truename buffer-file-name))
  15141. x had)
  15142. (setq x (assoc ctf file-alist) had x)
  15143. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  15144. (if to-end
  15145. (setq file-alist (append (delq x file-alist) (list x)))
  15146. (setq file-alist (cons x (delq x file-alist))))
  15147. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  15148. (org-install-agenda-files-menu)
  15149. (message "File %s to %s of agenda file list"
  15150. (if had "moved" "added") (if to-end "end" "front"))))
  15151. (defun org-remove-file (&optional file)
  15152. "Remove current file from the list of files in variable `org-agenda-files'.
  15153. These are the files which are being checked for agenda entries.
  15154. Optional argument FILE means use this file instead of the current."
  15155. (interactive)
  15156. (let* ((org-agenda-skip-unavailable-files nil)
  15157. (file (or file buffer-file-name))
  15158. (true-file (file-truename file))
  15159. (afile (abbreviate-file-name file))
  15160. (files (delq nil (mapcar
  15161. (lambda (x)
  15162. (if (equal true-file
  15163. (file-truename x))
  15164. nil x))
  15165. (org-agenda-files t)))))
  15166. (if (not (= (length files) (length (org-agenda-files t))))
  15167. (progn
  15168. (org-store-new-agenda-file-list files)
  15169. (org-install-agenda-files-menu)
  15170. (message "Removed file: %s" afile))
  15171. (message "File was not in list: %s (not removed)" afile))))
  15172. (defun org-file-menu-entry (file)
  15173. (vector file (list 'find-file file) t))
  15174. (defun org-check-agenda-file (file)
  15175. "Make sure FILE exists. If not, ask user what to do."
  15176. (when (not (file-exists-p file))
  15177. (message "non-existent agenda file %s. [R]emove from list or [A]bort?"
  15178. (abbreviate-file-name file))
  15179. (let ((r (downcase (read-char-exclusive))))
  15180. (cond
  15181. ((equal r ?r)
  15182. (org-remove-file file)
  15183. (throw 'nextfile t))
  15184. (t (error "Abort"))))))
  15185. (defun org-get-agenda-file-buffer (file)
  15186. "Get a buffer visiting FILE. If the buffer needs to be created, add
  15187. it to the list of buffers which might be released later."
  15188. (let ((buf (org-find-base-buffer-visiting file)))
  15189. (if buf
  15190. buf ; just return it
  15191. ;; Make a new buffer and remember it
  15192. (setq buf (find-file-noselect file))
  15193. (if buf (push buf org-agenda-new-buffers))
  15194. buf)))
  15195. (defun org-release-buffers (blist)
  15196. "Release all buffers in list, asking the user for confirmation when needed.
  15197. When a buffer is unmodified, it is just killed. When modified, it is saved
  15198. \(if the user agrees) and then killed."
  15199. (let (buf file)
  15200. (while (setq buf (pop blist))
  15201. (setq file (buffer-file-name buf))
  15202. (when (and (buffer-modified-p buf)
  15203. file
  15204. (y-or-n-p (format "Save file %s? " file)))
  15205. (with-current-buffer buf (save-buffer)))
  15206. (kill-buffer buf))))
  15207. (defun org-prepare-agenda-buffers (files)
  15208. "Create buffers for all agenda files, protect archived trees and comments."
  15209. (interactive)
  15210. (let ((pa '(:org-archived t))
  15211. (pc '(:org-comment t))
  15212. (pall '(:org-archived t :org-comment t))
  15213. (inhibit-read-only t)
  15214. (rea (concat ":" org-archive-tag ":"))
  15215. bmp file re)
  15216. (save-excursion
  15217. (save-restriction
  15218. (while (setq file (pop files))
  15219. (catch 'nextfile
  15220. (if (bufferp file)
  15221. (set-buffer file)
  15222. (org-check-agenda-file file)
  15223. (set-buffer (org-get-agenda-file-buffer file)))
  15224. (widen)
  15225. (setq bmp (buffer-modified-p))
  15226. (org-refresh-category-properties)
  15227. (setq org-todo-keywords-for-agenda
  15228. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  15229. (setq org-done-keywords-for-agenda
  15230. (append org-done-keywords-for-agenda org-done-keywords))
  15231. (setq org-todo-keyword-alist-for-agenda
  15232. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  15233. (setq org-drawers-for-agenda
  15234. (append org-drawers-for-agenda org-drawers))
  15235. (setq org-tag-alist-for-agenda
  15236. (append org-tag-alist-for-agenda org-tag-alist))
  15237. (save-excursion
  15238. (remove-text-properties (point-min) (point-max) pall)
  15239. (when org-agenda-skip-archived-trees
  15240. (goto-char (point-min))
  15241. (while (re-search-forward rea nil t)
  15242. (if (org-at-heading-p t)
  15243. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  15244. (goto-char (point-min))
  15245. (setq re (format org-heading-keyword-regexp-format
  15246. org-comment-string))
  15247. (while (re-search-forward re nil t)
  15248. (add-text-properties
  15249. (match-beginning 0) (org-end-of-subtree t) pc)))
  15250. (set-buffer-modified-p bmp)))))
  15251. (setq org-todo-keywords-for-agenda
  15252. (org-uniquify org-todo-keywords-for-agenda))
  15253. (setq org-todo-keyword-alist-for-agenda
  15254. (org-uniquify org-todo-keyword-alist-for-agenda)
  15255. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  15256. ;;;; Embedded LaTeX
  15257. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  15258. "Keymap for the minor `org-cdlatex-mode'.")
  15259. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  15260. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  15261. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  15262. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  15263. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  15264. (defvar org-cdlatex-texmathp-advice-is-done nil
  15265. "Flag remembering if we have applied the advice to texmathp already.")
  15266. (define-minor-mode org-cdlatex-mode
  15267. "Toggle the minor `org-cdlatex-mode'.
  15268. This mode supports entering LaTeX environment and math in LaTeX fragments
  15269. in Org-mode.
  15270. \\{org-cdlatex-mode-map}"
  15271. nil " OCDL" nil
  15272. (when org-cdlatex-mode
  15273. (require 'cdlatex)
  15274. (run-hooks 'cdlatex-mode-hook)
  15275. (cdlatex-compute-tables))
  15276. (unless org-cdlatex-texmathp-advice-is-done
  15277. (setq org-cdlatex-texmathp-advice-is-done t)
  15278. (defadvice texmathp (around org-math-always-on activate)
  15279. "Always return t in org-mode buffers.
  15280. This is because we want to insert math symbols without dollars even outside
  15281. the LaTeX math segments. If Orgmode thinks that point is actually inside
  15282. an embedded LaTeX fragment, let texmathp do its job.
  15283. \\[org-cdlatex-mode-map]"
  15284. (interactive)
  15285. (let (p)
  15286. (cond
  15287. ((not (eq major-mode 'org-mode)) ad-do-it)
  15288. ((eq this-command 'cdlatex-math-symbol)
  15289. (setq ad-return-value t
  15290. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  15291. (t
  15292. (let ((p (org-inside-LaTeX-fragment-p)))
  15293. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  15294. (setq ad-return-value t
  15295. texmathp-why '("Org-mode embedded math" . 0))
  15296. (if p ad-do-it)))))))))
  15297. (defun turn-on-org-cdlatex ()
  15298. "Unconditionally turn on `org-cdlatex-mode'."
  15299. (org-cdlatex-mode 1))
  15300. (defun org-inside-LaTeX-fragment-p ()
  15301. "Test if point is inside a LaTeX fragment.
  15302. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  15303. sequence appearing also before point.
  15304. Even though the matchers for math are configurable, this function assumes
  15305. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  15306. delimiters are skipped when they have been removed by customization.
  15307. The return value is nil, or a cons cell with the delimiter and the
  15308. position of this delimiter.
  15309. This function does a reasonably good job, but can locally be fooled by
  15310. for example currency specifications. For example it will assume being in
  15311. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  15312. fragments that are properly closed, but during editing, we have to live
  15313. with the uncertainty caused by missing closing delimiters. This function
  15314. looks only before point, not after."
  15315. (catch 'exit
  15316. (let ((pos (point))
  15317. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  15318. (lim (progn
  15319. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  15320. (point)))
  15321. dd-on str (start 0) m re)
  15322. (goto-char pos)
  15323. (when dodollar
  15324. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  15325. re (nth 1 (assoc "$" org-latex-regexps)))
  15326. (while (string-match re str start)
  15327. (cond
  15328. ((= (match-end 0) (length str))
  15329. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  15330. ((= (match-end 0) (- (length str) 5))
  15331. (throw 'exit nil))
  15332. (t (setq start (match-end 0))))))
  15333. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  15334. (goto-char pos)
  15335. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  15336. (and (match-beginning 2) (throw 'exit nil))
  15337. ;; count $$
  15338. (while (re-search-backward "\\$\\$" lim t)
  15339. (setq dd-on (not dd-on)))
  15340. (goto-char pos)
  15341. (if dd-on (cons "$$" m))))))
  15342. (defun org-inside-latex-macro-p ()
  15343. "Is point inside a LaTeX macro or its arguments?"
  15344. (save-match-data
  15345. (org-in-regexp
  15346. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  15347. (defun org-try-cdlatex-tab ()
  15348. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  15349. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  15350. - inside a LaTeX fragment, or
  15351. - after the first word in a line, where an abbreviation expansion could
  15352. insert a LaTeX environment."
  15353. (when org-cdlatex-mode
  15354. (cond
  15355. ;; Before any word on the line: No expansion possible.
  15356. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  15357. ;; Just after first word on the line: Expand it. Make sure it
  15358. ;; cannot happen on headlines, though.
  15359. ((save-excursion
  15360. (skip-chars-backward "a-zA-Z0-9*")
  15361. (skip-chars-backward " \t")
  15362. (and (bolp) (not (org-at-heading-p))))
  15363. (cdlatex-tab) t)
  15364. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  15365. (defun org-cdlatex-underscore-caret (&optional arg)
  15366. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  15367. Revert to the normal definition outside of these fragments."
  15368. (interactive "P")
  15369. (if (org-inside-LaTeX-fragment-p)
  15370. (call-interactively 'cdlatex-sub-superscript)
  15371. (let (org-cdlatex-mode)
  15372. (call-interactively (key-binding (vector last-input-event))))))
  15373. (defun org-cdlatex-math-modify (&optional arg)
  15374. "Execute `cdlatex-math-modify' in LaTeX fragments.
  15375. Revert to the normal definition outside of these fragments."
  15376. (interactive "P")
  15377. (if (org-inside-LaTeX-fragment-p)
  15378. (call-interactively 'cdlatex-math-modify)
  15379. (let (org-cdlatex-mode)
  15380. (call-interactively (key-binding (vector last-input-event))))))
  15381. (defvar org-latex-fragment-image-overlays nil
  15382. "List of overlays carrying the images of latex fragments.")
  15383. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  15384. (defun org-remove-latex-fragment-image-overlays ()
  15385. "Remove all overlays with LaTeX fragment images in current buffer."
  15386. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  15387. (setq org-latex-fragment-image-overlays nil))
  15388. (defun org-preview-latex-fragment (&optional subtree)
  15389. "Preview the LaTeX fragment at point, or all locally or globally.
  15390. If the cursor is in a LaTeX fragment, create the image and overlay
  15391. it over the source code. If there is no fragment at point, display
  15392. all fragments in the current text, from one headline to the next. With
  15393. prefix SUBTREE, display all fragments in the current subtree. With a
  15394. double prefix arg \\[universal-argument] \\[universal-argument], or when \
  15395. the cursor is before the first headline,
  15396. display all fragments in the buffer.
  15397. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  15398. (interactive "P")
  15399. (unless buffer-file-name
  15400. (error "Can't preview LaTeX fragment in a non-file buffer"))
  15401. (org-remove-latex-fragment-image-overlays)
  15402. (save-excursion
  15403. (save-restriction
  15404. (let (beg end at msg)
  15405. (cond
  15406. ((or (equal subtree '(16))
  15407. (not (save-excursion
  15408. (re-search-backward org-outline-regexp-bol nil t))))
  15409. (setq beg (point-min) end (point-max)
  15410. msg "Creating images for buffer...%s"))
  15411. ((equal subtree '(4))
  15412. (org-back-to-heading)
  15413. (setq beg (point) end (org-end-of-subtree t)
  15414. msg "Creating images for subtree...%s"))
  15415. (t
  15416. (if (setq at (org-inside-LaTeX-fragment-p))
  15417. (goto-char (max (point-min) (- (cdr at) 2)))
  15418. (org-back-to-heading))
  15419. (setq beg (point) end (progn (outline-next-heading) (point))
  15420. msg (if at "Creating image...%s"
  15421. "Creating images for entry...%s"))))
  15422. (message msg "")
  15423. (narrow-to-region beg end)
  15424. (goto-char beg)
  15425. (org-format-latex
  15426. (concat "ltxpng/" (file-name-sans-extension
  15427. (file-name-nondirectory
  15428. buffer-file-name)))
  15429. default-directory 'overlays msg at 'forbuffer 'dvipng)
  15430. (message msg "done. Use `C-c C-c' to remove images.")))))
  15431. (defvar org-latex-regexps
  15432. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  15433. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  15434. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  15435. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  15436. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  15437. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  15438. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  15439. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  15440. "Regular expressions for matching embedded LaTeX.")
  15441. (defvar org-export-have-math nil) ;; dynamic scoping
  15442. (defun org-format-latex (prefix &optional dir overlays msg at
  15443. forbuffer processing-type)
  15444. "Replace LaTeX fragments with links to an image, and produce images.
  15445. Some of the options can be changed using the variable
  15446. `org-format-latex-options'."
  15447. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  15448. (let* ((prefixnodir (file-name-nondirectory prefix))
  15449. (absprefix (expand-file-name prefix dir))
  15450. (todir (file-name-directory absprefix))
  15451. (opt org-format-latex-options)
  15452. (matchers (plist-get opt :matchers))
  15453. (re-list org-latex-regexps)
  15454. (org-format-latex-header-extra
  15455. (plist-get (org-infile-export-plist) :latex-header-extra))
  15456. (cnt 0) txt hash link beg end re e checkdir
  15457. executables-checked string
  15458. m n block-type block linkfile movefile ov)
  15459. ;; Check the different regular expressions
  15460. (while (setq e (pop re-list))
  15461. (setq m (car e) re (nth 1 e) n (nth 2 e) block-type (nth 3 e)
  15462. block (if block-type "\n\n" ""))
  15463. (when (member m matchers)
  15464. (goto-char (point-min))
  15465. (while (re-search-forward re nil t)
  15466. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  15467. (not (get-text-property (match-beginning n)
  15468. 'org-protected))
  15469. (or (not overlays)
  15470. (not (eq (get-char-property (match-beginning n)
  15471. 'org-overlay-type)
  15472. 'org-latex-overlay))))
  15473. (setq org-export-have-math t)
  15474. (cond
  15475. ((eq processing-type 'verbatim)
  15476. ;; Leave the text verbatim, just protect it
  15477. (add-text-properties (match-beginning n) (match-end n)
  15478. '(org-protected t)))
  15479. ((eq processing-type 'mathjax)
  15480. ;; Prepare for MathJax processing
  15481. (setq string (match-string n))
  15482. (if (member m '("$" "$1"))
  15483. (save-excursion
  15484. (delete-region (match-beginning n) (match-end n))
  15485. (goto-char (match-beginning n))
  15486. (insert (org-add-props (concat "\\(" (substring string 1 -1)
  15487. "\\)")
  15488. '(org-protected t))))
  15489. (add-text-properties (match-beginning n) (match-end n)
  15490. '(org-protected t))))
  15491. ((eq processing-type 'dvipng)
  15492. ;; Process to an image
  15493. (setq txt (match-string n)
  15494. beg (match-beginning n) end (match-end n)
  15495. cnt (1+ cnt))
  15496. (let (print-length print-level) ; make sure full list is printed
  15497. (setq hash (sha1 (prin1-to-string
  15498. (list org-format-latex-header
  15499. org-format-latex-header-extra
  15500. org-export-latex-default-packages-alist
  15501. org-export-latex-packages-alist
  15502. org-format-latex-options
  15503. forbuffer txt)))
  15504. linkfile (format "%s_%s.png" prefix hash)
  15505. movefile (format "%s_%s.png" absprefix hash)))
  15506. (setq link (concat block "[[file:" linkfile "]]" block))
  15507. (if msg (message msg cnt))
  15508. (goto-char beg)
  15509. (unless checkdir ; make sure the directory exists
  15510. (setq checkdir t)
  15511. (or (file-directory-p todir) (make-directory todir t)))
  15512. (unless executables-checked
  15513. (org-check-external-command
  15514. "latex" "needed to convert LaTeX fragments to images")
  15515. (org-check-external-command
  15516. "dvipng" "needed to convert LaTeX fragments to images")
  15517. (setq executables-checked t))
  15518. (unless (file-exists-p movefile)
  15519. (org-create-formula-image
  15520. txt movefile opt forbuffer))
  15521. (if overlays
  15522. (progn
  15523. (mapc (lambda (o)
  15524. (if (eq (overlay-get o 'org-overlay-type)
  15525. 'org-latex-overlay)
  15526. (delete-overlay o)))
  15527. (overlays-in beg end))
  15528. (setq ov (make-overlay beg end))
  15529. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  15530. (if (featurep 'xemacs)
  15531. (progn
  15532. (overlay-put ov 'invisible t)
  15533. (overlay-put
  15534. ov 'end-glyph
  15535. (make-glyph (vector 'png :file movefile))))
  15536. (overlay-put
  15537. ov 'display
  15538. (list 'image :type 'png :file movefile :ascent 'center)))
  15539. (push ov org-latex-fragment-image-overlays)
  15540. (goto-char end))
  15541. (delete-region beg end)
  15542. (insert (org-add-props link
  15543. (list 'org-latex-src
  15544. (replace-regexp-in-string
  15545. "\"" "" txt)
  15546. 'org-latex-src-embed-type
  15547. (if block-type 'paragraph 'character))))))
  15548. ((eq processing-type 'mathml)
  15549. ;; Process to MathML
  15550. (unless executables-checked
  15551. (unless (save-match-data (org-format-latex-mathml-available-p))
  15552. (error "LaTeX to MathML converter not configured"))
  15553. (setq executables-checked t))
  15554. (setq txt (match-string n)
  15555. beg (match-beginning n) end (match-end n)
  15556. cnt (1+ cnt))
  15557. (if msg (message msg cnt))
  15558. (goto-char beg)
  15559. (delete-region beg end)
  15560. (insert (org-format-latex-as-mathml
  15561. txt block-type prefix dir)))
  15562. (t
  15563. (error "Unknown conversion type %s for latex fragments"
  15564. processing-type)))))))))
  15565. (defun org-create-math-formula (latex-frag &optional mathml-file)
  15566. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  15567. Use `org-latex-to-mathml-convert-command'. If the conversion is
  15568. sucessful, return the portion between \"<math...> </math>\"
  15569. elements otherwise return nil. When MATHML-FILE is specified,
  15570. write the results in to that file. When invoked as an
  15571. interactive command, prompt for LATEX-FRAG, with initial value
  15572. set to the current active region and echo the results for user
  15573. inspection."
  15574. (interactive (list (let ((frag (when (region-active-p)
  15575. (buffer-substring-no-properties
  15576. (region-beginning) (region-end)))))
  15577. (read-string "LaTeX Fragment: " frag nil frag))))
  15578. (unless latex-frag (error "Invalid latex-frag"))
  15579. (let* ((tmp-in-file (file-relative-name
  15580. (make-temp-name (expand-file-name "ltxmathml-in"))))
  15581. (ignore (write-region latex-frag nil tmp-in-file))
  15582. (tmp-out-file (file-relative-name
  15583. (make-temp-name (expand-file-name "ltxmathml-out"))))
  15584. (cmd (format-spec
  15585. org-latex-to-mathml-convert-command
  15586. `((?j . ,(shell-quote-argument
  15587. (expand-file-name org-latex-to-mathml-jar-file)))
  15588. (?I . ,(shell-quote-argument tmp-in-file))
  15589. (?o . ,(shell-quote-argument tmp-out-file)))))
  15590. mathml shell-command-output)
  15591. (when (org-called-interactively-p 'any)
  15592. (unless (org-format-latex-mathml-available-p)
  15593. (error "LaTeX to MathML converter not configured")))
  15594. (message "Running %s" cmd)
  15595. (setq shell-command-output (shell-command-to-string cmd))
  15596. (setq mathml
  15597. (when (file-readable-p tmp-out-file)
  15598. (with-current-buffer (find-file-noselect tmp-out-file t)
  15599. (goto-char (point-min))
  15600. (when (re-search-forward
  15601. (concat
  15602. (regexp-quote
  15603. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
  15604. "\\(.\\|\n\\)*"
  15605. (regexp-quote "</math>")) nil t)
  15606. (prog1 (match-string 0) (kill-buffer))))))
  15607. (cond
  15608. (mathml
  15609. (setq mathml
  15610. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  15611. (when mathml-file
  15612. (write-region mathml nil mathml-file))
  15613. (when (org-called-interactively-p 'any)
  15614. (message mathml)))
  15615. ((message "LaTeX to MathML conversion failed")
  15616. (message shell-command-output)))
  15617. (delete-file tmp-in-file)
  15618. (when (file-exists-p tmp-out-file)
  15619. (delete-file tmp-out-file))
  15620. mathml))
  15621. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  15622. prefix &optional dir)
  15623. "Use `org-create-math-formula' but check local cache first."
  15624. (let* ((absprefix (expand-file-name prefix dir))
  15625. (print-length nil) (print-level nil)
  15626. (formula-id (concat
  15627. "formula-"
  15628. (sha1
  15629. (prin1-to-string
  15630. (list latex-frag
  15631. org-latex-to-mathml-convert-command)))))
  15632. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  15633. (formula-cache-dir (file-name-directory formula-cache)))
  15634. (unless (file-directory-p formula-cache-dir)
  15635. (make-directory formula-cache-dir t))
  15636. (unless (file-exists-p formula-cache)
  15637. (org-create-math-formula latex-frag formula-cache))
  15638. (if (file-exists-p formula-cache)
  15639. ;; Successful conversion. Return the link to MathML file.
  15640. (org-add-props
  15641. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  15642. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  15643. 'org-latex-src-embed-type (if latex-frag-type
  15644. 'paragraph 'character)))
  15645. ;; Failed conversion. Return the LaTeX fragment verbatim
  15646. (add-text-properties
  15647. 0 (1- (length latex-frag)) '(org-protected t) latex-frag)
  15648. latex-frag)))
  15649. ;; This function borrows from Ganesh Swami's latex2png.el
  15650. (defun org-create-formula-image (string tofile options buffer)
  15651. "This calls dvipng."
  15652. (require 'org-latex)
  15653. (let* ((tmpdir (if (featurep 'xemacs)
  15654. (temp-directory)
  15655. temporary-file-directory))
  15656. (texfilebase (make-temp-name
  15657. (expand-file-name "orgtex" tmpdir)))
  15658. (texfile (concat texfilebase ".tex"))
  15659. (dvifile (concat texfilebase ".dvi"))
  15660. (pngfile (concat texfilebase ".png"))
  15661. (fnh (if (featurep 'xemacs)
  15662. (font-height (get-face-font 'default))
  15663. (face-attribute 'default :height nil)))
  15664. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  15665. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  15666. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  15667. "Black"))
  15668. (bg (or (plist-get options (if buffer :background :html-background))
  15669. "Transparent")))
  15670. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  15671. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  15672. (with-temp-file texfile
  15673. (insert (org-splice-latex-header
  15674. org-format-latex-header
  15675. org-export-latex-default-packages-alist
  15676. org-export-latex-packages-alist t
  15677. org-format-latex-header-extra))
  15678. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")
  15679. (require 'org-latex)
  15680. (org-export-latex-fix-inputenc))
  15681. (let ((dir default-directory))
  15682. (condition-case nil
  15683. (progn
  15684. (cd tmpdir)
  15685. (call-process "latex" nil nil nil texfile))
  15686. (error nil))
  15687. (cd dir))
  15688. (if (not (file-exists-p dvifile))
  15689. (progn (message "Failed to create dvi file from %s" texfile) nil)
  15690. (condition-case nil
  15691. (call-process "dvipng" nil nil nil
  15692. "-fg" fg "-bg" bg
  15693. "-D" dpi
  15694. ;;"-x" scale "-y" scale
  15695. "-T" "tight"
  15696. "-o" pngfile
  15697. dvifile)
  15698. (error nil))
  15699. (if (not (file-exists-p pngfile))
  15700. (if org-format-latex-signal-error
  15701. (error "Failed to create png file from %s" texfile)
  15702. (message "Failed to create png file from %s" texfile)
  15703. nil)
  15704. ;; Use the requested file name and clean up
  15705. (copy-file pngfile tofile 'replace)
  15706. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  15707. (delete-file (concat texfilebase e)))
  15708. pngfile))))
  15709. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  15710. "Fill a LaTeX header template TPL.
  15711. In the template, the following place holders will be recognized:
  15712. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  15713. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  15714. [PACKAGES] \\usepackage statements for PKG
  15715. [NO-PACKAGES] do not include PKG
  15716. [EXTRA] the string EXTRA
  15717. [NO-EXTRA] do not include EXTRA
  15718. For backward compatibility, if both the positive and the negative place
  15719. holder is missing, the positive one (without the \"NO-\") will be
  15720. assumed to be present at the end of the template.
  15721. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  15722. EXTRA is a string.
  15723. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  15724. (let (rpl (end ""))
  15725. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  15726. (setq rpl (if (or (match-end 1) (not def-pkg))
  15727. "" (org-latex-packages-to-string def-pkg snippets-p t))
  15728. tpl (replace-match rpl t t tpl))
  15729. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  15730. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  15731. (setq rpl (if (or (match-end 1) (not pkg))
  15732. "" (org-latex-packages-to-string pkg snippets-p t))
  15733. tpl (replace-match rpl t t tpl))
  15734. (if pkg (setq end
  15735. (concat end "\n"
  15736. (org-latex-packages-to-string pkg snippets-p)))))
  15737. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  15738. (setq rpl (if (or (match-end 1) (not extra))
  15739. "" (concat extra "\n"))
  15740. tpl (replace-match rpl t t tpl))
  15741. (if (and extra (string-match "\\S-" extra))
  15742. (setq end (concat end "\n" extra))))
  15743. (if (string-match "\\S-" end)
  15744. (concat tpl "\n" end)
  15745. tpl)))
  15746. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  15747. "Turn an alist of packages into a string with the \\usepackage macros."
  15748. (setq pkg (mapconcat (lambda(p)
  15749. (cond
  15750. ((stringp p) p)
  15751. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  15752. (format "%% Package %s omitted" (cadr p)))
  15753. ((equal "" (car p))
  15754. (format "\\usepackage{%s}" (cadr p)))
  15755. (t
  15756. (format "\\usepackage[%s]{%s}"
  15757. (car p) (cadr p)))))
  15758. pkg
  15759. "\n"))
  15760. (if newline (concat pkg "\n") pkg))
  15761. (defun org-dvipng-color (attr)
  15762. "Return an rgb color specification for dvipng."
  15763. (apply 'format "rgb %s %s %s"
  15764. (mapcar 'org-normalize-color
  15765. (color-values (face-attribute 'default attr nil)))))
  15766. (defun org-normalize-color (value)
  15767. "Return string to be used as color value for an RGB component."
  15768. (format "%g" (/ value 65535.0)))
  15769. ;; Image display
  15770. (defvar org-inline-image-overlays nil)
  15771. (make-variable-buffer-local 'org-inline-image-overlays)
  15772. (defun org-toggle-inline-images (&optional include-linked)
  15773. "Toggle the display of inline images.
  15774. INCLUDE-LINKED is passed to `org-display-inline-images'."
  15775. (interactive "P")
  15776. (if org-inline-image-overlays
  15777. (progn
  15778. (org-remove-inline-images)
  15779. (message "Inline image display turned off"))
  15780. (org-display-inline-images include-linked)
  15781. (if org-inline-image-overlays
  15782. (message "%d images displayed inline"
  15783. (length org-inline-image-overlays))
  15784. (message "No images to display inline"))))
  15785. (defun org-display-inline-images (&optional include-linked refresh beg end)
  15786. "Display inline images.
  15787. Normally only links without a description part are inlined, because this
  15788. is how it will work for export. When INCLUDE-LINKED is set, also links
  15789. with a description part will be inlined. This can be nice for a quick
  15790. look at those images, but it does not reflect what exported files will look
  15791. like.
  15792. When REFRESH is set, refresh existing images between BEG and END.
  15793. This will create new image displays only if necessary.
  15794. BEG and END default to the buffer boundaries."
  15795. (interactive "P")
  15796. (unless refresh
  15797. (org-remove-inline-images)
  15798. (if (fboundp 'clear-image-cache) (clear-image-cache)))
  15799. (save-excursion
  15800. (save-restriction
  15801. (widen)
  15802. (setq beg (or beg (point-min)) end (or end (point-max)))
  15803. (goto-char (point-min))
  15804. (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
  15805. (substring (org-image-file-name-regexp) 0 -2)
  15806. "\\)\\]" (if include-linked "" "\\]")))
  15807. old file ov img)
  15808. (while (re-search-forward re end t)
  15809. (setq old (get-char-property-and-overlay (match-beginning 1)
  15810. 'org-image-overlay))
  15811. (setq file (expand-file-name
  15812. (concat (or (match-string 3) "") (match-string 4))))
  15813. (when (file-exists-p file)
  15814. (if (and (car-safe old) refresh)
  15815. (image-refresh (overlay-get (cdr old) 'display))
  15816. (setq img (save-match-data (create-image file)))
  15817. (when img
  15818. (setq ov (make-overlay (match-beginning 0) (match-end 0)))
  15819. (overlay-put ov 'display img)
  15820. (overlay-put ov 'face 'default)
  15821. (overlay-put ov 'org-image-overlay t)
  15822. (overlay-put ov 'modification-hooks
  15823. (list 'org-display-inline-modification-hook))
  15824. (push ov org-inline-image-overlays)))))))))
  15825. (defun org-display-inline-modification-hook (ov after beg end &optional len)
  15826. "Remove inline-display overlay if a corresponding region is modified."
  15827. (let ((inhibit-modification-hooks t))
  15828. (when (and ov after)
  15829. (delete ov org-inline-image-overlays)
  15830. (delete-overlay ov))))
  15831. (defun org-remove-inline-images ()
  15832. "Remove inline display of images."
  15833. (interactive)
  15834. (mapc 'delete-overlay org-inline-image-overlays)
  15835. (setq org-inline-image-overlays nil))
  15836. ;;;; Key bindings
  15837. ;; Outline functions from `outline-mode-prefix-map'
  15838. ;; that can be remapped in Org:
  15839. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  15840. (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
  15841. (define-key org-mode-map [remap outline-forward-same-level]
  15842. 'org-forward-same-level)
  15843. (define-key org-mode-map [remap outline-backward-same-level]
  15844. 'org-backward-same-level)
  15845. (define-key org-mode-map [remap show-branches]
  15846. 'org-kill-note-or-show-branches)
  15847. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  15848. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  15849. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  15850. ;; Outline functions from `outline-mode-prefix-map'
  15851. ;; that can not be remapped in Org:
  15852. ;; - the column "key binding" shows whether the Outline function is still
  15853. ;; available in Org mode on the same key that it has been bound to in
  15854. ;; Outline mode:
  15855. ;; - "overridden": key used for a different functionality in Org mode
  15856. ;; - else: key still bound to the same Outline function in Org mode
  15857. ;; | Outline function | key binding | Org replacement |
  15858. ;; |------------------------------------+-------------+-----------------------|
  15859. ;; | `outline-next-visible-heading' | `C-c C-n' | still same function |
  15860. ;; | `outline-previous-visible-heading' | `C-c C-p' | still same function |
  15861. ;; | `show-children' | `C-c C-i' | visibility cycling |
  15862. ;; | `hide-subtree' | overridden | visibility cycling |
  15863. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  15864. ;; | `hide-body' | overridden | no replacement |
  15865. ;; | `show-all' | overridden | no replacement |
  15866. ;; | `hide-entry' | overridden | visibility cycling |
  15867. ;; | `show-entry' | overridden | no replacement |
  15868. ;; | `hide-leaves' | overridden | no replacement |
  15869. ;; | `hide-sublevels' | overridden | no replacement |
  15870. ;; | `hide-other' | overridden | no replacement |
  15871. ;; | `outline-move-subtree-up' | `C-c C-^' | better: org-shiftup |
  15872. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  15873. ;; Make `C-c C-x' a prefix key
  15874. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  15875. ;; TAB key with modifiers
  15876. (org-defkey org-mode-map "\C-i" 'org-cycle)
  15877. (org-defkey org-mode-map [(tab)] 'org-cycle)
  15878. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  15879. (org-defkey org-mode-map [(meta tab)] 'pcomplete)
  15880. (org-defkey org-mode-map "\M-\t" 'pcomplete)
  15881. (org-defkey org-mode-map "\M-\C-i" 'pcomplete)
  15882. ;; The following line is necessary under Suse GNU/Linux
  15883. (unless (featurep 'xemacs)
  15884. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  15885. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  15886. (define-key org-mode-map [backtab] 'org-shifttab)
  15887. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  15888. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  15889. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  15890. ;; Cursor keys with modifiers
  15891. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  15892. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  15893. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  15894. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  15895. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  15896. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  15897. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  15898. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  15899. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  15900. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  15901. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  15902. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  15903. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  15904. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  15905. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  15906. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  15907. ;; Babel keys
  15908. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  15909. (mapc (lambda (pair)
  15910. (define-key org-babel-map (car pair) (cdr pair)))
  15911. org-babel-key-bindings)
  15912. ;;; Extra keys for tty access.
  15913. ;; We only set them when really needed because otherwise the
  15914. ;; menus don't show the simple keys
  15915. (when (or org-use-extra-keys
  15916. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  15917. (not window-system))
  15918. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  15919. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  15920. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  15921. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  15922. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  15923. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  15924. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  15925. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  15926. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  15927. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  15928. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  15929. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  15930. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  15931. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  15932. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  15933. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  15934. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  15935. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  15936. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  15937. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  15938. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  15939. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  15940. (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
  15941. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  15942. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  15943. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  15944. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  15945. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  15946. ;; All the other keys
  15947. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  15948. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  15949. (if (boundp 'narrow-map)
  15950. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  15951. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  15952. (if (boundp 'narrow-map)
  15953. (org-defkey narrow-map "b" 'org-narrow-to-block)
  15954. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  15955. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-same-level)
  15956. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-same-level)
  15957. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  15958. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  15959. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  15960. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  15961. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  15962. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  15963. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  15964. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  15965. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  15966. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  15967. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  15968. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  15969. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  15970. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  15971. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  15972. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  15973. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  15974. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  15975. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  15976. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  15977. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  15978. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  15979. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  15980. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  15981. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  15982. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  15983. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  15984. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  15985. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  15986. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  15987. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  15988. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  15989. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  15990. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  15991. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  15992. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  15993. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  15994. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  15995. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  15996. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  15997. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  15998. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  15999. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  16000. (org-defkey org-mode-map "\C-c^" 'org-sort)
  16001. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  16002. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  16003. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  16004. (org-defkey org-mode-map "\C-m" 'org-return)
  16005. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  16006. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  16007. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  16008. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  16009. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  16010. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  16011. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  16012. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  16013. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  16014. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  16015. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  16016. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  16017. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  16018. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  16019. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  16020. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  16021. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  16022. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  16023. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  16024. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  16025. (org-defkey org-mode-map "\C-c\C-@" 'org-mark-list)
  16026. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  16027. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  16028. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  16029. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  16030. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  16031. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  16032. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  16033. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  16034. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  16035. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  16036. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  16037. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  16038. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  16039. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  16040. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  16041. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  16042. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  16043. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  16044. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  16045. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  16046. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  16047. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  16048. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  16049. (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
  16050. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  16051. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  16052. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  16053. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  16054. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  16055. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  16056. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  16057. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  16058. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  16059. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  16060. (when (featurep 'xemacs)
  16061. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  16062. (defconst org-speed-commands-default
  16063. '(
  16064. ("Outline Navigation")
  16065. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  16066. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  16067. ("f" . (org-speed-move-safe 'org-forward-same-level))
  16068. ("b" . (org-speed-move-safe 'org-backward-same-level))
  16069. ("u" . (org-speed-move-safe 'outline-up-heading))
  16070. ("j" . org-goto)
  16071. ("g" . (org-refile t))
  16072. ("Outline Visibility")
  16073. ("c" . org-cycle)
  16074. ("C" . org-shifttab)
  16075. (" " . org-display-outline-path)
  16076. ("Outline Structure Editing")
  16077. ("U" . org-shiftmetaup)
  16078. ("D" . org-shiftmetadown)
  16079. ("r" . org-metaright)
  16080. ("l" . org-metaleft)
  16081. ("R" . org-shiftmetaright)
  16082. ("L" . org-shiftmetaleft)
  16083. ("i" . (progn (forward-char 1) (call-interactively
  16084. 'org-insert-heading-respect-content)))
  16085. ("^" . org-sort)
  16086. ("w" . org-refile)
  16087. ("a" . org-archive-subtree-default-with-confirmation)
  16088. ("." . org-mark-subtree)
  16089. ("Clock Commands")
  16090. ("I" . org-clock-in)
  16091. ("O" . org-clock-out)
  16092. ("Meta Data Editing")
  16093. ("t" . org-todo)
  16094. ("0" . (org-priority ?\ ))
  16095. ("1" . (org-priority ?A))
  16096. ("2" . (org-priority ?B))
  16097. ("3" . (org-priority ?C))
  16098. (";" . org-set-tags-command)
  16099. ("e" . org-set-effort)
  16100. ("Agenda Views etc")
  16101. ("v" . org-agenda)
  16102. ("/" . org-sparse-tree)
  16103. ("Misc")
  16104. ("o" . org-open-at-point)
  16105. ("?" . org-speed-command-help)
  16106. ("<" . (org-agenda-set-restriction-lock 'subtree))
  16107. (">" . (org-agenda-remove-restriction-lock))
  16108. )
  16109. "The default speed commands.")
  16110. (defun org-print-speed-command (e)
  16111. (if (> (length (car e)) 1)
  16112. (progn
  16113. (princ "\n")
  16114. (princ (car e))
  16115. (princ "\n")
  16116. (princ (make-string (length (car e)) ?-))
  16117. (princ "\n"))
  16118. (princ (car e))
  16119. (princ " ")
  16120. (if (symbolp (cdr e))
  16121. (princ (symbol-name (cdr e)))
  16122. (prin1 (cdr e)))
  16123. (princ "\n")))
  16124. (defun org-speed-command-help ()
  16125. "Show the available speed commands."
  16126. (interactive)
  16127. (if (not org-use-speed-commands)
  16128. (error "Speed commands are not activated, customize `org-use-speed-commands'")
  16129. (with-output-to-temp-buffer "*Help*"
  16130. (princ "User-defined Speed commands\n===========================\n")
  16131. (mapc 'org-print-speed-command org-speed-commands-user)
  16132. (princ "\n")
  16133. (princ "Built-in Speed commands\n=======================\n")
  16134. (mapc 'org-print-speed-command org-speed-commands-default))
  16135. (with-current-buffer "*Help*"
  16136. (setq truncate-lines t))))
  16137. (defun org-speed-move-safe (cmd)
  16138. "Execute CMD, but make sure that the cursor always ends up in a headline.
  16139. If not, return to the original position and throw an error."
  16140. (interactive)
  16141. (let ((pos (point)))
  16142. (call-interactively cmd)
  16143. (unless (and (bolp) (org-at-heading-p))
  16144. (goto-char pos)
  16145. (error "Boundary reached while executing %s" cmd))))
  16146. (defvar org-self-insert-command-undo-counter 0)
  16147. (defvar org-table-auto-blank-field) ; defined in org-table.el
  16148. (defvar org-speed-command nil)
  16149. (defun org-speed-command-default-hook (keys)
  16150. "Hook for activating single-letter speed commands.
  16151. `org-speed-commands-default' specifies a minimal command set.
  16152. Use `org-speed-commands-user' for further customization."
  16153. (when (or (and (bolp) (looking-at org-outline-regexp))
  16154. (and (functionp org-use-speed-commands)
  16155. (funcall org-use-speed-commands)))
  16156. (cdr (assoc keys (append org-speed-commands-user
  16157. org-speed-commands-default)))))
  16158. (defun org-babel-speed-command-hook (keys)
  16159. "Hook for activating single-letter code block commands."
  16160. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  16161. (cdr (assoc keys org-babel-key-bindings))))
  16162. (defcustom org-speed-command-hook
  16163. '(org-speed-command-default-hook org-babel-speed-command-hook)
  16164. "Hook for activating speed commands at strategic locations.
  16165. Hook functions are called in sequence until a valid handler is
  16166. found.
  16167. Each hook takes a single argument, a user-pressed command key
  16168. which is also a `self-insert-command' from the global map.
  16169. Within the hook, examine the cursor position and the command key
  16170. and return nil or a valid handler as appropriate. Handler could
  16171. be one of an interactive command, a function, or a form.
  16172. Set `org-use-speed-commands' to non-nil value to enable this
  16173. hook. The default setting is `org-speed-command-default-hook'."
  16174. :group 'org-structure
  16175. :version "24.1"
  16176. :type 'hook)
  16177. (defun org-self-insert-command (N)
  16178. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  16179. If the cursor is in a table looking at whitespace, the whitespace is
  16180. overwritten, and the table is not marked as requiring realignment."
  16181. (interactive "p")
  16182. (org-check-before-invisible-edit 'insert)
  16183. (cond
  16184. ((and org-use-speed-commands
  16185. (setq org-speed-command
  16186. (run-hook-with-args-until-success
  16187. 'org-speed-command-hook (this-command-keys))))
  16188. (cond
  16189. ((commandp org-speed-command)
  16190. (setq this-command org-speed-command)
  16191. (call-interactively org-speed-command))
  16192. ((functionp org-speed-command)
  16193. (funcall org-speed-command))
  16194. ((and org-speed-command (listp org-speed-command))
  16195. (eval org-speed-command))
  16196. (t (let (org-use-speed-commands)
  16197. (call-interactively 'org-self-insert-command)))))
  16198. ((and
  16199. (org-table-p)
  16200. (progn
  16201. ;; check if we blank the field, and if that triggers align
  16202. (and (featurep 'org-table) org-table-auto-blank-field
  16203. (member last-command
  16204. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  16205. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  16206. ;; got extra space, this field does not determine column width
  16207. (let (org-table-may-need-update) (org-table-blank-field))
  16208. ;; no extra space, this field may determine column width
  16209. (org-table-blank-field)))
  16210. t)
  16211. (eq N 1)
  16212. (looking-at "[^|\n]* |"))
  16213. (let (org-table-may-need-update)
  16214. (goto-char (1- (match-end 0)))
  16215. (delete-char -1)
  16216. (goto-char (match-beginning 0))
  16217. (self-insert-command N)))
  16218. (t
  16219. (setq org-table-may-need-update t)
  16220. (self-insert-command N)
  16221. (org-fix-tags-on-the-fly)
  16222. (if org-self-insert-cluster-for-undo
  16223. (if (not (eq last-command 'org-self-insert-command))
  16224. (setq org-self-insert-command-undo-counter 1)
  16225. (if (>= org-self-insert-command-undo-counter 20)
  16226. (setq org-self-insert-command-undo-counter 1)
  16227. (and (> org-self-insert-command-undo-counter 0)
  16228. buffer-undo-list (listp buffer-undo-list)
  16229. (not (cadr buffer-undo-list)) ; remove nil entry
  16230. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  16231. (setq org-self-insert-command-undo-counter
  16232. (1+ org-self-insert-command-undo-counter))))))))
  16233. (defun org-check-before-invisible-edit (kind)
  16234. "Check is editing if kind KIND would be dangerous with invisible text around.
  16235. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  16236. ;; First, try to get out of here as quickly as possible, to reduce overhead
  16237. (if (and org-catch-invisible-edits
  16238. (or (not (boundp 'visible-mode)) (not visible-mode))
  16239. (or (get-char-property (point) 'invisible)
  16240. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  16241. ;; OK, we need to take a closer look
  16242. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  16243. (invisible-before-point (if (bobp) nil (get-char-property
  16244. (1- (point)) 'invisible)))
  16245. (border-and-ok-direction
  16246. (or
  16247. ;; Check if we are acting predictably before invisible text
  16248. (and invisible-at-point (not invisible-before-point)
  16249. (memq kind '(insert delete-backward)))
  16250. ;; Check if we are acting predictably after invisible text
  16251. ;; This works not well, and I have turned it off. It seems
  16252. ;; better to always show and stop after invisible text.
  16253. ;; (and (not invisible-at-point) invisible-before-point
  16254. ;; (memq kind '(insert delete)))
  16255. )))
  16256. (when (or (memq invisible-at-point '(outline org-hide-block))
  16257. (memq invisible-before-point '(outline org-hide-block)))
  16258. (if (eq org-catch-invisible-edits 'error)
  16259. (error "Editing in invisible areas is prohibited - make visible first"))
  16260. ;; Make the area visible
  16261. (save-excursion
  16262. (if invisible-before-point
  16263. (goto-char (previous-single-char-property-change
  16264. (point) 'invisible)))
  16265. (org-cycle))
  16266. (cond
  16267. ((eq org-catch-invisible-edits 'show)
  16268. ;; That's it, we do the edit after showing
  16269. (message
  16270. "Unfolding invisible region around point before editing")
  16271. (sit-for 1))
  16272. ((and (eq org-catch-invisible-edits 'smart)
  16273. border-and-ok-direction)
  16274. (message "Unfolding invisible region around point before editing"))
  16275. (t
  16276. ;; Don't do the edit, make the user repeat it in full visibility
  16277. (error "Edit in invisible region aborted, repeat to confirm with text visible")))))))
  16278. (defun org-fix-tags-on-the-fly ()
  16279. (when (and (equal (char-after (point-at-bol)) ?*)
  16280. (org-at-heading-p))
  16281. (org-align-tags-here org-tags-column)))
  16282. (defun org-delete-backward-char (N)
  16283. "Like `delete-backward-char', insert whitespace at field end in tables.
  16284. When deleting backwards, in tables this function will insert whitespace in
  16285. front of the next \"|\" separator, to keep the table aligned. The table will
  16286. still be marked for re-alignment if the field did fill the entire column,
  16287. because, in this case the deletion might narrow the column."
  16288. (interactive "p")
  16289. (org-check-before-invisible-edit 'delete-backward)
  16290. (if (and (org-table-p)
  16291. (eq N 1)
  16292. (string-match "|" (buffer-substring (point-at-bol) (point)))
  16293. (looking-at ".*?|"))
  16294. (let ((pos (point))
  16295. (noalign (looking-at "[^|\n\r]* |"))
  16296. (c org-table-may-need-update))
  16297. (backward-delete-char N)
  16298. (if (not overwrite-mode)
  16299. (progn
  16300. (skip-chars-forward "^|")
  16301. (insert " ")
  16302. (goto-char (1- pos))))
  16303. ;; noalign: if there were two spaces at the end, this field
  16304. ;; does not determine the width of the column.
  16305. (if noalign (setq org-table-may-need-update c)))
  16306. (backward-delete-char N)
  16307. (org-fix-tags-on-the-fly)))
  16308. (defun org-delete-char (N)
  16309. "Like `delete-char', but insert whitespace at field end in tables.
  16310. When deleting characters, in tables this function will insert whitespace in
  16311. front of the next \"|\" separator, to keep the table aligned. The table will
  16312. still be marked for re-alignment if the field did fill the entire column,
  16313. because, in this case the deletion might narrow the column."
  16314. (interactive "p")
  16315. (org-check-before-invisible-edit 'delete)
  16316. (if (and (org-table-p)
  16317. (not (bolp))
  16318. (not (= (char-after) ?|))
  16319. (eq N 1))
  16320. (if (looking-at ".*?|")
  16321. (let ((pos (point))
  16322. (noalign (looking-at "[^|\n\r]* |"))
  16323. (c org-table-may-need-update))
  16324. (replace-match (concat
  16325. (substring (match-string 0) 1 -1)
  16326. " |"))
  16327. (goto-char pos)
  16328. ;; noalign: if there were two spaces at the end, this field
  16329. ;; does not determine the width of the column.
  16330. (if noalign (setq org-table-may-need-update c)))
  16331. (delete-char N))
  16332. (delete-char N)
  16333. (org-fix-tags-on-the-fly)))
  16334. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  16335. (put 'org-self-insert-command 'delete-selection t)
  16336. (put 'orgtbl-self-insert-command 'delete-selection t)
  16337. (put 'org-delete-char 'delete-selection 'supersede)
  16338. (put 'org-delete-backward-char 'delete-selection 'supersede)
  16339. (put 'org-yank 'delete-selection 'yank)
  16340. ;; Make `flyspell-mode' delay after some commands
  16341. (put 'org-self-insert-command 'flyspell-delayed t)
  16342. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  16343. (put 'org-delete-char 'flyspell-delayed t)
  16344. (put 'org-delete-backward-char 'flyspell-delayed t)
  16345. ;; Make pabbrev-mode expand after org-mode commands
  16346. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  16347. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  16348. ;; How to do this: Measure non-white length of current string
  16349. ;; If equal to column width, we should realign.
  16350. (defun org-remap (map &rest commands)
  16351. "In MAP, remap the functions given in COMMANDS.
  16352. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  16353. (let (new old)
  16354. (while commands
  16355. (setq old (pop commands) new (pop commands))
  16356. (if (fboundp 'command-remapping)
  16357. (org-defkey map (vector 'remap old) new)
  16358. (substitute-key-definition old new map global-map)))))
  16359. (when (eq org-enable-table-editor 'optimized)
  16360. ;; If the user wants maximum table support, we need to hijack
  16361. ;; some standard editing functions
  16362. (org-remap org-mode-map
  16363. 'self-insert-command 'org-self-insert-command
  16364. 'delete-char 'org-delete-char
  16365. 'delete-backward-char 'org-delete-backward-char)
  16366. (org-defkey org-mode-map "|" 'org-force-self-insert))
  16367. (defvar org-ctrl-c-ctrl-c-hook nil
  16368. "Hook for functions attaching themselves to `C-c C-c'.
  16369. This can be used to add additional functionality to the C-c C-c
  16370. key which executes context-dependent commands. This hook is run
  16371. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  16372. run after the last test.
  16373. Each function will be called with no arguments. The function
  16374. must check if the context is appropriate for it to act. If yes,
  16375. it should do its thing and then return a non-nil value. If the
  16376. context is wrong, just do nothing and return nil.")
  16377. (defvar org-ctrl-c-ctrl-c-final-hook nil
  16378. "Hook for functions attaching themselves to `C-c C-c'.
  16379. This can be used to add additional functionality to the C-c C-c
  16380. key which executes context-dependent commands. This hook is run
  16381. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  16382. before the first test.
  16383. Each function will be called with no arguments. The function
  16384. must check if the context is appropriate for it to act. If yes,
  16385. it should do its thing and then return a non-nil value. If the
  16386. context is wrong, just do nothing and return nil.")
  16387. (defvar org-tab-first-hook nil
  16388. "Hook for functions to attach themselves to TAB.
  16389. See `org-ctrl-c-ctrl-c-hook' for more information.
  16390. This hook runs as the first action when TAB is pressed, even before
  16391. `org-cycle' messes around with the `outline-regexp' to cater for
  16392. inline tasks and plain list item folding.
  16393. If any function in this hook returns t, any other actions that
  16394. would have been caused by TAB (such as table field motion or visibility
  16395. cycling) will not occur.")
  16396. (defvar org-tab-after-check-for-table-hook nil
  16397. "Hook for functions to attach themselves to TAB.
  16398. See `org-ctrl-c-ctrl-c-hook' for more information.
  16399. This hook runs after it has been established that the cursor is not in a
  16400. table, but before checking if the cursor is in a headline or if global cycling
  16401. should be done.
  16402. If any function in this hook returns t, not other actions like visibility
  16403. cycling will be done.")
  16404. (defvar org-tab-after-check-for-cycling-hook nil
  16405. "Hook for functions to attach themselves to TAB.
  16406. See `org-ctrl-c-ctrl-c-hook' for more information.
  16407. This hook runs after it has been established that not table field motion and
  16408. not visibility should be done because of current context. This is probably
  16409. the place where a package like yasnippets can hook in.")
  16410. (defvar org-tab-before-tab-emulation-hook nil
  16411. "Hook for functions to attach themselves to TAB.
  16412. See `org-ctrl-c-ctrl-c-hook' for more information.
  16413. This hook runs after every other options for TAB have been exhausted, but
  16414. before indentation and \t insertion takes place.")
  16415. (defvar org-metaleft-hook nil
  16416. "Hook for functions attaching themselves to `M-left'.
  16417. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16418. (defvar org-metaright-hook nil
  16419. "Hook for functions attaching themselves to `M-right'.
  16420. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16421. (defvar org-metaup-hook nil
  16422. "Hook for functions attaching themselves to `M-up'.
  16423. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16424. (defvar org-metadown-hook nil
  16425. "Hook for functions attaching themselves to `M-down'.
  16426. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16427. (defvar org-shiftmetaleft-hook nil
  16428. "Hook for functions attaching themselves to `M-S-left'.
  16429. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16430. (defvar org-shiftmetaright-hook nil
  16431. "Hook for functions attaching themselves to `M-S-right'.
  16432. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16433. (defvar org-shiftmetaup-hook nil
  16434. "Hook for functions attaching themselves to `M-S-up'.
  16435. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16436. (defvar org-shiftmetadown-hook nil
  16437. "Hook for functions attaching themselves to `M-S-down'.
  16438. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16439. (defvar org-metareturn-hook nil
  16440. "Hook for functions attaching themselves to `M-RET'.
  16441. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16442. (defvar org-shiftup-hook nil
  16443. "Hook for functions attaching themselves to `S-up'.
  16444. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16445. (defvar org-shiftup-final-hook nil
  16446. "Hook for functions attaching themselves to `S-up'.
  16447. This one runs after all other options except shift-select have been excluded.
  16448. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16449. (defvar org-shiftdown-hook nil
  16450. "Hook for functions attaching themselves to `S-down'.
  16451. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16452. (defvar org-shiftdown-final-hook nil
  16453. "Hook for functions attaching themselves to `S-down'.
  16454. This one runs after all other options except shift-select have been excluded.
  16455. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16456. (defvar org-shiftleft-hook nil
  16457. "Hook for functions attaching themselves to `S-left'.
  16458. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16459. (defvar org-shiftleft-final-hook nil
  16460. "Hook for functions attaching themselves to `S-left'.
  16461. This one runs after all other options except shift-select have been excluded.
  16462. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16463. (defvar org-shiftright-hook nil
  16464. "Hook for functions attaching themselves to `S-right'.
  16465. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16466. (defvar org-shiftright-final-hook nil
  16467. "Hook for functions attaching themselves to `S-right'.
  16468. This one runs after all other options except shift-select have been excluded.
  16469. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16470. (defun org-modifier-cursor-error ()
  16471. "Throw an error, a modified cursor command was applied in wrong context."
  16472. (error "This command is active in special context like tables, headlines or items"))
  16473. (defun org-shiftselect-error ()
  16474. "Throw an error because Shift-Cursor command was applied in wrong context."
  16475. (if (and (boundp 'shift-select-mode) shift-select-mode)
  16476. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  16477. (error "This command works only in special context like headlines or timestamps")))
  16478. (defun org-call-for-shift-select (cmd)
  16479. (let ((this-command-keys-shift-translated t))
  16480. (call-interactively cmd)))
  16481. (defun org-shifttab (&optional arg)
  16482. "Global visibility cycling or move to previous table field.
  16483. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  16484. on context.
  16485. See the individual commands for more information."
  16486. (interactive "P")
  16487. (cond
  16488. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  16489. ((integerp arg)
  16490. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  16491. (message "Content view to level: %d" arg)
  16492. (org-content (prefix-numeric-value arg2))
  16493. (setq org-cycle-global-status 'overview)))
  16494. (t (call-interactively 'org-global-cycle))))
  16495. (defun org-shiftmetaleft ()
  16496. "Promote subtree or delete table column.
  16497. Calls `org-promote-subtree', `org-outdent-item',
  16498. or `org-table-delete-column', depending on context.
  16499. See the individual commands for more information."
  16500. (interactive)
  16501. (cond
  16502. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  16503. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  16504. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  16505. ((org-at-item-p) (call-interactively 'org-outdent-item-tree))
  16506. (t (org-modifier-cursor-error))))
  16507. (defun org-shiftmetaright ()
  16508. "Demote subtree or insert table column.
  16509. Calls `org-demote-subtree', `org-indent-item',
  16510. or `org-table-insert-column', depending on context.
  16511. See the individual commands for more information."
  16512. (interactive)
  16513. (cond
  16514. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  16515. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  16516. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  16517. ((org-at-item-p) (call-interactively 'org-indent-item-tree))
  16518. (t (org-modifier-cursor-error))))
  16519. (defun org-shiftmetaup (&optional arg)
  16520. "Move subtree up or kill table row.
  16521. Calls `org-move-subtree-up' or `org-table-kill-row' or
  16522. `org-move-item-up' depending on context. See the individual commands
  16523. for more information."
  16524. (interactive "P")
  16525. (cond
  16526. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  16527. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  16528. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  16529. ((org-at-item-p) (call-interactively 'org-move-item-up))
  16530. (t (org-modifier-cursor-error))))
  16531. (defun org-shiftmetadown (&optional arg)
  16532. "Move subtree down or insert table row.
  16533. Calls `org-move-subtree-down' or `org-table-insert-row' or
  16534. `org-move-item-down', depending on context. See the individual
  16535. commands for more information."
  16536. (interactive "P")
  16537. (cond
  16538. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  16539. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  16540. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  16541. ((org-at-item-p) (call-interactively 'org-move-item-down))
  16542. (t (org-modifier-cursor-error))))
  16543. (defsubst org-hidden-tree-error ()
  16544. (error
  16545. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  16546. (defun org-metaleft (&optional arg)
  16547. "Promote heading or move table column to left.
  16548. Calls `org-do-promote' or `org-table-move-column', depending on context.
  16549. With no specific context, calls the Emacs default `backward-word'.
  16550. See the individual commands for more information."
  16551. (interactive "P")
  16552. (cond
  16553. ((run-hook-with-args-until-success 'org-metaleft-hook))
  16554. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  16555. ((org-with-limited-levels
  16556. (or (org-at-heading-p)
  16557. (and (org-region-active-p)
  16558. (save-excursion
  16559. (goto-char (region-beginning))
  16560. (org-at-heading-p)))))
  16561. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  16562. (call-interactively 'org-do-promote))
  16563. ;; At an inline task.
  16564. ((org-at-heading-p)
  16565. (call-interactively 'org-inlinetask-promote))
  16566. ((or (org-at-item-p)
  16567. (and (org-region-active-p)
  16568. (save-excursion
  16569. (goto-char (region-beginning))
  16570. (org-at-item-p))))
  16571. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  16572. (call-interactively 'org-outdent-item))
  16573. (t (call-interactively 'backward-word))))
  16574. (defun org-metaright (&optional arg)
  16575. "Demote a subtree, a list item or move table column to right.
  16576. In front of a drawer or a block keyword, indent it correctly.
  16577. With no specific context, calls the Emacs default `forward-word'.
  16578. See the individual commands for more information."
  16579. (interactive "P")
  16580. (cond
  16581. ((run-hook-with-args-until-success 'org-metaright-hook))
  16582. ((org-at-table-p) (call-interactively 'org-table-move-column))
  16583. ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
  16584. ((org-at-block-p) (call-interactively 'org-indent-block))
  16585. ((org-with-limited-levels
  16586. (or (org-at-heading-p)
  16587. (and (org-region-active-p)
  16588. (save-excursion
  16589. (goto-char (region-beginning))
  16590. (org-at-heading-p)))))
  16591. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  16592. (call-interactively 'org-do-demote))
  16593. ;; At an inline task.
  16594. ((org-at-heading-p)
  16595. (call-interactively 'org-inlinetask-demote))
  16596. ((or (org-at-item-p)
  16597. (and (org-region-active-p)
  16598. (save-excursion
  16599. (goto-char (region-beginning))
  16600. (org-at-item-p))))
  16601. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  16602. (call-interactively 'org-indent-item))
  16603. (t (call-interactively 'forward-word))))
  16604. (defun org-check-for-hidden (what)
  16605. "Check if there are hidden headlines/items in the current visual line.
  16606. WHAT can be either `headlines' or `items'. If the current line is
  16607. an outline or item heading and it has a folded subtree below it,
  16608. this function returns t, nil otherwise."
  16609. (let ((re (cond
  16610. ((eq what 'headlines) org-outline-regexp-bol)
  16611. ((eq what 'items) (org-item-beginning-re))
  16612. (t (error "This should not happen"))))
  16613. beg end)
  16614. (save-excursion
  16615. (catch 'exit
  16616. (unless (org-region-active-p)
  16617. (setq beg (point-at-bol))
  16618. (beginning-of-line 2)
  16619. (while (and (not (eobp)) ;; this is like `next-line'
  16620. (get-char-property (1- (point)) 'invisible))
  16621. (beginning-of-line 2))
  16622. (setq end (point))
  16623. (goto-char beg)
  16624. (goto-char (point-at-eol))
  16625. (setq end (max end (point)))
  16626. (while (re-search-forward re end t)
  16627. (if (get-char-property (match-beginning 0) 'invisible)
  16628. (throw 'exit t))))
  16629. nil))))
  16630. (defun org-metaup (&optional arg)
  16631. "Move subtree up or move table row up.
  16632. Calls `org-move-subtree-up' or `org-table-move-row' or
  16633. `org-move-item-up', depending on context. See the individual commands
  16634. for more information."
  16635. (interactive "P")
  16636. (cond
  16637. ((run-hook-with-args-until-success 'org-metaup-hook))
  16638. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  16639. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  16640. ((org-at-item-p) (call-interactively 'org-move-item-up))
  16641. (t (transpose-lines 1) (beginning-of-line -1))))
  16642. (defun org-metadown (&optional arg)
  16643. "Move subtree down or move table row down.
  16644. Calls `org-move-subtree-down' or `org-table-move-row' or
  16645. `org-move-item-down', depending on context. See the individual
  16646. commands for more information."
  16647. (interactive "P")
  16648. (cond
  16649. ((run-hook-with-args-until-success 'org-metadown-hook))
  16650. ((org-at-table-p) (call-interactively 'org-table-move-row))
  16651. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  16652. ((org-at-item-p) (call-interactively 'org-move-item-down))
  16653. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  16654. (defun org-shiftup (&optional arg)
  16655. "Increase item in timestamp or increase priority of current headline.
  16656. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  16657. depending on context. See the individual commands for more information."
  16658. (interactive "P")
  16659. (cond
  16660. ((run-hook-with-args-until-success 'org-shiftup-hook))
  16661. ((and org-support-shift-select (org-region-active-p))
  16662. (org-call-for-shift-select 'previous-line))
  16663. ((org-at-timestamp-p t)
  16664. (call-interactively (if org-edit-timestamp-down-means-later
  16665. 'org-timestamp-down 'org-timestamp-up)))
  16666. ((and (not (eq org-support-shift-select 'always))
  16667. org-enable-priority-commands
  16668. (org-at-heading-p))
  16669. (call-interactively 'org-priority-up))
  16670. ((and (not org-support-shift-select) (org-at-item-p))
  16671. (call-interactively 'org-previous-item))
  16672. ((org-clocktable-try-shift 'up arg))
  16673. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  16674. (org-support-shift-select
  16675. (org-call-for-shift-select 'previous-line))
  16676. (t (org-shiftselect-error))))
  16677. (defun org-shiftdown (&optional arg)
  16678. "Decrease item in timestamp or decrease priority of current headline.
  16679. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  16680. depending on context. See the individual commands for more information."
  16681. (interactive "P")
  16682. (cond
  16683. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  16684. ((and org-support-shift-select (org-region-active-p))
  16685. (org-call-for-shift-select 'next-line))
  16686. ((org-at-timestamp-p t)
  16687. (call-interactively (if org-edit-timestamp-down-means-later
  16688. 'org-timestamp-up 'org-timestamp-down)))
  16689. ((and (not (eq org-support-shift-select 'always))
  16690. org-enable-priority-commands
  16691. (org-at-heading-p))
  16692. (call-interactively 'org-priority-down))
  16693. ((and (not org-support-shift-select) (org-at-item-p))
  16694. (call-interactively 'org-next-item))
  16695. ((org-clocktable-try-shift 'down arg))
  16696. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  16697. (org-support-shift-select
  16698. (org-call-for-shift-select 'next-line))
  16699. (t (org-shiftselect-error))))
  16700. (defun org-shiftright (&optional arg)
  16701. "Cycle the thing at point or in the current line, depending on context.
  16702. Depending on context, this does one of the following:
  16703. - switch a timestamp at point one day into the future
  16704. - on a headline, switch to the next TODO keyword.
  16705. - on an item, switch entire list to the next bullet type
  16706. - on a property line, switch to the next allowed value
  16707. - on a clocktable definition line, move time block into the future"
  16708. (interactive "P")
  16709. (cond
  16710. ((run-hook-with-args-until-success 'org-shiftright-hook))
  16711. ((and org-support-shift-select (org-region-active-p))
  16712. (org-call-for-shift-select 'forward-char))
  16713. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  16714. ((and (not (eq org-support-shift-select 'always))
  16715. (org-at-heading-p))
  16716. (let ((org-inhibit-logging
  16717. (not org-treat-S-cursor-todo-selection-as-state-change))
  16718. (org-inhibit-blocking
  16719. (not org-treat-S-cursor-todo-selection-as-state-change)))
  16720. (org-call-with-arg 'org-todo 'right)))
  16721. ((or (and org-support-shift-select
  16722. (not (eq org-support-shift-select 'always))
  16723. (org-at-item-bullet-p))
  16724. (and (not org-support-shift-select) (org-at-item-p)))
  16725. (org-call-with-arg 'org-cycle-list-bullet nil))
  16726. ((and (not (eq org-support-shift-select 'always))
  16727. (org-at-property-p))
  16728. (call-interactively 'org-property-next-allowed-value))
  16729. ((org-clocktable-try-shift 'right arg))
  16730. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  16731. (org-support-shift-select
  16732. (org-call-for-shift-select 'forward-char))
  16733. (t (org-shiftselect-error))))
  16734. (defun org-shiftleft (&optional arg)
  16735. "Cycle the thing at point or in the current line, depending on context.
  16736. Depending on context, this does one of the following:
  16737. - switch a timestamp at point one day into the past
  16738. - on a headline, switch to the previous TODO keyword.
  16739. - on an item, switch entire list to the previous bullet type
  16740. - on a property line, switch to the previous allowed value
  16741. - on a clocktable definition line, move time block into the past"
  16742. (interactive "P")
  16743. (cond
  16744. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  16745. ((and org-support-shift-select (org-region-active-p))
  16746. (org-call-for-shift-select 'backward-char))
  16747. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  16748. ((and (not (eq org-support-shift-select 'always))
  16749. (org-at-heading-p))
  16750. (let ((org-inhibit-logging
  16751. (not org-treat-S-cursor-todo-selection-as-state-change))
  16752. (org-inhibit-blocking
  16753. (not org-treat-S-cursor-todo-selection-as-state-change)))
  16754. (org-call-with-arg 'org-todo 'left)))
  16755. ((or (and org-support-shift-select
  16756. (not (eq org-support-shift-select 'always))
  16757. (org-at-item-bullet-p))
  16758. (and (not org-support-shift-select) (org-at-item-p)))
  16759. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  16760. ((and (not (eq org-support-shift-select 'always))
  16761. (org-at-property-p))
  16762. (call-interactively 'org-property-previous-allowed-value))
  16763. ((org-clocktable-try-shift 'left arg))
  16764. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  16765. (org-support-shift-select
  16766. (org-call-for-shift-select 'backward-char))
  16767. (t (org-shiftselect-error))))
  16768. (defun org-shiftcontrolright ()
  16769. "Switch to next TODO set."
  16770. (interactive)
  16771. (cond
  16772. ((and org-support-shift-select (org-region-active-p))
  16773. (org-call-for-shift-select 'forward-word))
  16774. ((and (not (eq org-support-shift-select 'always))
  16775. (org-at-heading-p))
  16776. (org-call-with-arg 'org-todo 'nextset))
  16777. (org-support-shift-select
  16778. (org-call-for-shift-select 'forward-word))
  16779. (t (org-shiftselect-error))))
  16780. (defun org-shiftcontrolleft ()
  16781. "Switch to previous TODO set."
  16782. (interactive)
  16783. (cond
  16784. ((and org-support-shift-select (org-region-active-p))
  16785. (org-call-for-shift-select 'backward-word))
  16786. ((and (not (eq org-support-shift-select 'always))
  16787. (org-at-heading-p))
  16788. (org-call-with-arg 'org-todo 'previousset))
  16789. (org-support-shift-select
  16790. (org-call-for-shift-select 'backward-word))
  16791. (t (org-shiftselect-error))))
  16792. (defun org-shiftcontrolup ()
  16793. "Change timestamps synchronously up in CLOCK log lines."
  16794. (interactive)
  16795. (cond ((and (not org-support-shift-select)
  16796. (org-at-clock-log-p)
  16797. (org-at-timestamp-p t))
  16798. (org-clock-timestamps-up))
  16799. (t (org-shiftselect-error))))
  16800. (defun org-shiftcontroldown ()
  16801. "Change timestamps synchronously down in CLOCK log lines."
  16802. (interactive)
  16803. (cond ((and (not org-support-shift-select)
  16804. (org-at-clock-log-p)
  16805. (org-at-timestamp-p t))
  16806. (org-clock-timestamps-down))
  16807. (t (org-shiftselect-error))))
  16808. (defun org-ctrl-c-ret ()
  16809. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  16810. (interactive)
  16811. (cond
  16812. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  16813. (t (call-interactively 'org-insert-heading))))
  16814. (defun org-find-visible ()
  16815. (let ((s (point)))
  16816. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  16817. (get-char-property s 'invisible)))
  16818. s))
  16819. (defun org-find-invisible ()
  16820. (let ((s (point)))
  16821. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  16822. (not (get-char-property s 'invisible))))
  16823. s))
  16824. (defun org-copy-visible (beg end)
  16825. "Copy the visible parts of the region."
  16826. (interactive "r")
  16827. (let (snippets s)
  16828. (save-excursion
  16829. (save-restriction
  16830. (narrow-to-region beg end)
  16831. (setq s (goto-char (point-min)))
  16832. (while (not (= (point) (point-max)))
  16833. (goto-char (org-find-invisible))
  16834. (push (buffer-substring s (point)) snippets)
  16835. (setq s (goto-char (org-find-visible))))))
  16836. (kill-new (apply 'concat (nreverse snippets)))))
  16837. (defun org-copy-special ()
  16838. "Copy region in table or copy current subtree.
  16839. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  16840. See the individual commands for more information."
  16841. (interactive)
  16842. (call-interactively
  16843. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  16844. (defun org-cut-special ()
  16845. "Cut region in table or cut current subtree.
  16846. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  16847. See the individual commands for more information."
  16848. (interactive)
  16849. (call-interactively
  16850. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  16851. (defun org-paste-special (arg)
  16852. "Paste rectangular region into table, or past subtree relative to level.
  16853. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  16854. See the individual commands for more information."
  16855. (interactive "P")
  16856. (if (org-at-table-p)
  16857. (org-table-paste-rectangle)
  16858. (org-paste-subtree arg)))
  16859. (defun org-edit-special (&optional arg)
  16860. "Call a special editor for the stuff at point.
  16861. When at a table, call the formula editor with `org-table-edit-formulas'.
  16862. When at the first line of an src example, call `org-edit-src-code'.
  16863. When in an #+include line, visit the include file. Otherwise call
  16864. `ffap' to visit the file at point."
  16865. (interactive)
  16866. ;; possibly prep session before editing source
  16867. (when arg
  16868. (let* ((info (org-babel-get-src-block-info))
  16869. (lang (nth 0 info))
  16870. (params (nth 2 info))
  16871. (session (cdr (assoc :session params))))
  16872. (when (and info session) ;; we are in a source-code block with a session
  16873. (funcall
  16874. (intern (concat "org-babel-prep-session:" lang)) session params))))
  16875. (cond ;; proceed with `org-edit-special'
  16876. ((save-excursion
  16877. (beginning-of-line 1)
  16878. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  16879. (find-file (org-trim (match-string 1))))
  16880. ((org-edit-src-code))
  16881. ((org-edit-fixed-width-region))
  16882. ((org-at-table.el-p)
  16883. (org-edit-src-code))
  16884. ((or (org-at-table-p)
  16885. (save-excursion
  16886. (beginning-of-line 1)
  16887. (looking-at "[ \t]*#\\+TBLFM:")))
  16888. (call-interactively 'org-table-edit-formulas))
  16889. (t (call-interactively 'ffap))))
  16890. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  16891. (defun org-ctrl-c-ctrl-c (&optional arg)
  16892. "Set tags in headline, or update according to changed information at point.
  16893. This command does many different things, depending on context:
  16894. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  16895. this is what we do.
  16896. - If the cursor is on a statistics cookie, update it.
  16897. - If the cursor is in a headline, prompt for tags and insert them
  16898. into the current line, aligned to `org-tags-column'. When called
  16899. with prefix arg, realign all tags in the current buffer.
  16900. - If the cursor is in one of the special #+KEYWORD lines, this
  16901. triggers scanning the buffer for these lines and updating the
  16902. information.
  16903. - If the cursor is inside a table, realign the table. This command
  16904. works even if the automatic table editor has been turned off.
  16905. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  16906. the entire table.
  16907. - If the cursor is at a footnote reference or definition, jump to
  16908. the corresponding definition or references, respectively.
  16909. - If the cursor is a the beginning of a dynamic block, update it.
  16910. - If the current buffer is a capture buffer, close note and file it.
  16911. - If the cursor is on a <<<target>>>, update radio targets and
  16912. corresponding links in this buffer.
  16913. - If the cursor is on a numbered item in a plain list, renumber the
  16914. ordered list.
  16915. - If the cursor is on a checkbox, toggle it.
  16916. - If the cursor is on a code block, evaluate it. The variable
  16917. `org-confirm-babel-evaluate' can be used to control prompting
  16918. before code block evaluation, by default every code block
  16919. evaluation requires confirmation. Code block evaluation can be
  16920. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  16921. (interactive "P")
  16922. (let ((org-enable-table-editor t))
  16923. (cond
  16924. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  16925. org-occur-highlights
  16926. org-latex-fragment-image-overlays)
  16927. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  16928. (org-remove-occur-highlights)
  16929. (org-remove-latex-fragment-image-overlays)
  16930. (message "Temporary highlights/overlays removed from current buffer"))
  16931. ((and (local-variable-p 'org-finish-function (current-buffer))
  16932. (fboundp org-finish-function))
  16933. (funcall org-finish-function))
  16934. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  16935. ((org-in-regexp org-ts-regexp-both)
  16936. (org-timestamp-change 0 'day))
  16937. ((or (looking-at org-property-start-re)
  16938. (org-at-property-p))
  16939. (call-interactively 'org-property-action))
  16940. ((org-at-target-p) (call-interactively 'org-update-radio-target-regexp))
  16941. ((and (org-in-regexp "\\[\\([0-9]*%\\|[0-9]*/[0-9]*\\)\\]")
  16942. (or (org-at-heading-p) (org-at-item-p)))
  16943. (call-interactively 'org-update-statistics-cookies))
  16944. ((org-at-heading-p) (call-interactively 'org-set-tags))
  16945. ((org-at-table.el-p)
  16946. (message "Use C-c ' to edit table.el tables"))
  16947. ((org-at-table-p)
  16948. (org-table-maybe-eval-formula)
  16949. (if arg
  16950. (call-interactively 'org-table-recalculate)
  16951. (org-table-maybe-recalculate-line))
  16952. (call-interactively 'org-table-align)
  16953. (orgtbl-send-table 'maybe))
  16954. ((or (org-footnote-at-reference-p)
  16955. (org-footnote-at-definition-p))
  16956. (call-interactively 'org-footnote-action))
  16957. ((org-at-item-checkbox-p)
  16958. ;; Cursor at a checkbox: repair list and update checkboxes. Send
  16959. ;; list only if at top item.
  16960. (let* ((cbox (match-string 1))
  16961. (struct (org-list-struct))
  16962. (old-struct (copy-tree struct))
  16963. (parents (org-list-parents-alist struct))
  16964. (orderedp (org-entry-get nil "ORDERED"))
  16965. (firstp (= (org-list-get-top-point struct) (point-at-bol)))
  16966. block-item)
  16967. ;; Use a light version of `org-toggle-checkbox' to avoid
  16968. ;; computing list structure twice.
  16969. (let ((new-box (cond
  16970. ((equal arg '(16)) "[-]")
  16971. ((equal arg '(4)) nil)
  16972. ((equal "[X]" cbox) "[ ]")
  16973. (t "[X]"))))
  16974. (if (and firstp arg)
  16975. ;; If at first item of sub-list, remove check-box from
  16976. ;; every item at the same level.
  16977. (mapc
  16978. (lambda (pos) (org-list-set-checkbox pos struct new-box))
  16979. (org-list-get-all-items
  16980. (point-at-bol) struct (org-list-prevs-alist struct)))
  16981. (org-list-set-checkbox (point-at-bol) struct new-box)))
  16982. ;; Replicate `org-list-write-struct', while grabbing a return
  16983. ;; value from `org-list-struct-fix-box'.
  16984. (org-list-struct-fix-ind struct parents 2)
  16985. (org-list-struct-fix-item-end struct)
  16986. (let ((prevs (org-list-prevs-alist struct)))
  16987. (org-list-struct-fix-bul struct prevs)
  16988. (org-list-struct-fix-ind struct parents)
  16989. (setq block-item
  16990. (org-list-struct-fix-box struct parents prevs orderedp)))
  16991. (org-list-struct-apply-struct struct old-struct)
  16992. (org-update-checkbox-count-maybe)
  16993. (when block-item
  16994. (message
  16995. "Checkboxes were removed due to unchecked box at line %d"
  16996. (org-current-line block-item)))
  16997. (when firstp (org-list-send-list 'maybe))))
  16998. ((org-at-item-p)
  16999. ;; Cursor at an item: repair list. Do checkbox related actions
  17000. ;; only if function was called with an argument. Send list only
  17001. ;; if at top item.
  17002. (let* ((struct (org-list-struct))
  17003. (firstp (= (org-list-get-top-point struct) (point-at-bol)))
  17004. old-struct)
  17005. (when arg
  17006. (setq old-struct (copy-tree struct))
  17007. (if firstp
  17008. ;; If at first item of sub-list, add check-box to every
  17009. ;; item at the same level.
  17010. (mapc
  17011. (lambda (pos)
  17012. (unless (org-list-get-checkbox pos struct)
  17013. (org-list-set-checkbox pos struct "[ ]")))
  17014. (org-list-get-all-items
  17015. (point-at-bol) struct (org-list-prevs-alist struct)))
  17016. (org-list-set-checkbox (point-at-bol) struct "[ ]")))
  17017. (org-list-write-struct
  17018. struct (org-list-parents-alist struct) old-struct)
  17019. (when arg (org-update-checkbox-count-maybe))
  17020. (when firstp (org-list-send-list 'maybe))))
  17021. ((save-excursion (beginning-of-line 1) (looking-at org-dblock-start-re))
  17022. ;; Dynamic block
  17023. (beginning-of-line 1)
  17024. (save-excursion (org-update-dblock)))
  17025. ((save-excursion
  17026. (beginning-of-line 1)
  17027. (looking-at "[ \t]*#\\+\\([A-Z]+\\)"))
  17028. (cond
  17029. ((equal (match-string 1) "TBLFM")
  17030. ;; Recalculate the table before this line
  17031. (save-excursion
  17032. (beginning-of-line 1)
  17033. (skip-chars-backward " \r\n\t")
  17034. (if (org-at-table-p)
  17035. (org-call-with-arg 'org-table-recalculate (or arg t)))))
  17036. (t
  17037. (let ((org-inhibit-startup-visibility-stuff t)
  17038. (org-startup-align-all-tables nil))
  17039. (when (boundp 'org-table-coordinate-overlays)
  17040. (mapc 'delete-overlay org-table-coordinate-overlays)
  17041. (setq org-table-coordinate-overlays nil))
  17042. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  17043. (message "Local setup has been refreshed"))))
  17044. ((org-clock-update-time-maybe))
  17045. (t
  17046. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  17047. (error "C-c C-c can do nothing useful at this location"))))))
  17048. (defun org-mode-restart ()
  17049. "Restart Org-mode, to scan again for special lines.
  17050. Also updates the keyword regular expressions."
  17051. (interactive)
  17052. (org-mode)
  17053. (message "Org-mode restarted"))
  17054. (defun org-kill-note-or-show-branches ()
  17055. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  17056. (interactive)
  17057. (if (not org-finish-function)
  17058. (progn
  17059. (hide-subtree)
  17060. (call-interactively 'show-branches))
  17061. (let ((org-note-abort t))
  17062. (funcall org-finish-function))))
  17063. (defun org-return (&optional indent)
  17064. "Goto next table row or insert a newline.
  17065. Calls `org-table-next-row' or `newline', depending on context.
  17066. See the individual commands for more information."
  17067. (interactive)
  17068. (cond
  17069. ((or (bobp) (org-in-src-block-p))
  17070. (if indent (newline-and-indent) (newline)))
  17071. ((org-at-table-p)
  17072. (org-table-justify-field-maybe)
  17073. (call-interactively 'org-table-next-row))
  17074. ;; when `newline-and-indent' is called within a list, make sure
  17075. ;; text moved stays inside the item.
  17076. ((and (org-in-item-p) indent)
  17077. (if (and (org-at-item-p) (>= (point) (match-end 0)))
  17078. (progn
  17079. (save-match-data (newline))
  17080. (org-indent-line-to (length (match-string 0))))
  17081. (let ((ind (org-get-indentation)))
  17082. (newline)
  17083. (if (org-looking-back org-list-end-re)
  17084. (org-indent-line-function)
  17085. (org-indent-line-to ind)))))
  17086. ((and org-return-follows-link
  17087. (let ((tprop (get-text-property (point) 'face)))
  17088. (or (eq tprop 'org-link)
  17089. (and (listp tprop) (memq 'org-link tprop)))))
  17090. (call-interactively 'org-open-at-point))
  17091. ((and (org-at-heading-p)
  17092. (looking-at
  17093. (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
  17094. (org-show-entry)
  17095. (end-of-line 1)
  17096. (newline))
  17097. (t (if indent (newline-and-indent) (newline)))))
  17098. (defun org-return-indent ()
  17099. "Goto next table row or insert a newline and indent.
  17100. Calls `org-table-next-row' or `newline-and-indent', depending on
  17101. context. See the individual commands for more information."
  17102. (interactive)
  17103. (org-return t))
  17104. (defun org-ctrl-c-star ()
  17105. "Compute table, or change heading status of lines.
  17106. Calls `org-table-recalculate' or `org-toggle-heading',
  17107. depending on context."
  17108. (interactive)
  17109. (cond
  17110. ((org-at-table-p)
  17111. (call-interactively 'org-table-recalculate))
  17112. (t
  17113. ;; Convert all lines in region to list items
  17114. (call-interactively 'org-toggle-heading))))
  17115. (defun org-ctrl-c-minus ()
  17116. "Insert separator line in table or modify bullet status of line.
  17117. Also turns a plain line or a region of lines into list items.
  17118. Calls `org-table-insert-hline', `org-toggle-item', or
  17119. `org-cycle-list-bullet', depending on context."
  17120. (interactive)
  17121. (cond
  17122. ((org-at-table-p)
  17123. (call-interactively 'org-table-insert-hline))
  17124. ((org-region-active-p)
  17125. (call-interactively 'org-toggle-item))
  17126. ((org-in-item-p)
  17127. (call-interactively 'org-cycle-list-bullet))
  17128. (t
  17129. (call-interactively 'org-toggle-item))))
  17130. (defun org-toggle-item (arg)
  17131. "Convert headings or normal lines to items, items to normal lines.
  17132. If there is no active region, only the current line is considered.
  17133. If the first non blank line in the region is an headline, convert
  17134. all headlines to items, shifting text accordingly.
  17135. If it is an item, convert all items to normal lines.
  17136. If it is normal text, change region into an item. With a prefix
  17137. argument ARG, change each line in region into an item."
  17138. (interactive "P")
  17139. (let ((shift-text
  17140. (function
  17141. ;; Shift text in current section to IND, from point to END.
  17142. ;; The function leaves point to END line.
  17143. (lambda (ind end)
  17144. (let ((min-i 1000) (end (copy-marker end)))
  17145. ;; First determine the minimum indentation (MIN-I) of
  17146. ;; the text.
  17147. (save-excursion
  17148. (catch 'exit
  17149. (while (< (point) end)
  17150. (let ((i (org-get-indentation)))
  17151. (cond
  17152. ;; Skip blank lines and inline tasks.
  17153. ((looking-at "^[ \t]*$"))
  17154. ((looking-at org-outline-regexp-bol))
  17155. ;; We can't find less than 0 indentation.
  17156. ((zerop i) (throw 'exit (setq min-i 0)))
  17157. ((< i min-i) (setq min-i i))))
  17158. (forward-line))))
  17159. ;; Then indent each line so that a line indented to
  17160. ;; MIN-I becomes indented to IND. Ignore blank lines
  17161. ;; and inline tasks in the process.
  17162. (let ((delta (- ind min-i)))
  17163. (while (< (point) end)
  17164. (unless (or (looking-at "^[ \t]*$")
  17165. (looking-at org-outline-regexp-bol))
  17166. (org-indent-line-to (+ (org-get-indentation) delta)))
  17167. (forward-line)))))))
  17168. (skip-blanks
  17169. (function
  17170. ;; Return beginning of first non-blank line, starting from
  17171. ;; line at POS.
  17172. (lambda (pos)
  17173. (save-excursion
  17174. (goto-char pos)
  17175. (skip-chars-forward " \r\t\n")
  17176. (point-at-bol)))))
  17177. beg end)
  17178. ;; Determine boundaries of changes.
  17179. (if (org-region-active-p)
  17180. (setq beg (funcall skip-blanks (region-beginning))
  17181. end (copy-marker (region-end)))
  17182. (setq beg (funcall skip-blanks (point-at-bol))
  17183. end (copy-marker (point-at-eol))))
  17184. ;; Depending on the starting line, choose an action on the text
  17185. ;; between BEG and END.
  17186. (org-with-limited-levels
  17187. (save-excursion
  17188. (goto-char beg)
  17189. (cond
  17190. ;; Case 1. Start at an item: de-itemize. Note that it only
  17191. ;; happens when a region is active: `org-ctrl-c-minus'
  17192. ;; would call `org-cycle-list-bullet' otherwise.
  17193. ((org-at-item-p)
  17194. (while (< (point) end)
  17195. (when (org-at-item-p)
  17196. (skip-chars-forward " \t")
  17197. (delete-region (point) (match-end 0)))
  17198. (forward-line)))
  17199. ;; Case 2. Start at an heading: convert to items.
  17200. ((org-at-heading-p)
  17201. (let* ((bul (org-list-bullet-string "-"))
  17202. (bul-len (length bul))
  17203. ;; Indentation of the first heading. It should be
  17204. ;; relative to the indentation of its parent, if any.
  17205. (start-ind (save-excursion
  17206. (cond
  17207. ((not org-adapt-indentation) 0)
  17208. ((not (outline-previous-heading)) 0)
  17209. (t (length (match-string 0))))))
  17210. ;; Level of first heading. Further headings will be
  17211. ;; compared to it to determine hierarchy in the list.
  17212. (ref-level (org-reduced-level (org-outline-level))))
  17213. (while (< (point) end)
  17214. (let* ((level (org-reduced-level (org-outline-level)))
  17215. (delta (max 0 (- level ref-level))))
  17216. ;; If current headline is less indented than the first
  17217. ;; one, set it as reference, in order to preserve
  17218. ;; subtrees.
  17219. (when (< level ref-level) (setq ref-level level))
  17220. (replace-match bul t t)
  17221. (org-indent-line-to (+ start-ind (* delta bul-len)))
  17222. ;; Ensure all text down to END (or SECTION-END) belongs
  17223. ;; to the newly created item.
  17224. (let ((section-end (save-excursion
  17225. (or (outline-next-heading) (point)))))
  17226. (forward-line)
  17227. (funcall shift-text
  17228. (+ start-ind (* (1+ delta) bul-len))
  17229. (min end section-end)))))))
  17230. ;; Case 3. Normal line with ARG: turn each non-item line into
  17231. ;; an item.
  17232. (arg
  17233. (while (< (point) end)
  17234. (unless (or (org-at-heading-p) (org-at-item-p))
  17235. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  17236. (replace-match
  17237. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  17238. (forward-line)))
  17239. ;; Case 4. Normal line without ARG: make the first line of
  17240. ;; region an item, and shift indentation of others
  17241. ;; lines to set them as item's body.
  17242. (t (let* ((bul (org-list-bullet-string "-"))
  17243. (bul-len (length bul))
  17244. (ref-ind (org-get-indentation)))
  17245. (skip-chars-forward " \t")
  17246. (insert bul)
  17247. (forward-line)
  17248. (while (< (point) end)
  17249. ;; Ensure that lines less indented than first one
  17250. ;; still get included in item body.
  17251. (funcall shift-text
  17252. (+ ref-ind bul-len)
  17253. (min end (save-excursion (or (outline-next-heading)
  17254. (point)))))
  17255. (forward-line)))))))))
  17256. (defun org-toggle-heading (&optional nstars)
  17257. "Convert headings to normal text, or items or text to headings.
  17258. If there is no active region, only the current line is considered.
  17259. If the first non blank line is an headline, remove the stars from
  17260. all headlines in the region.
  17261. If it is a plain list item, turn all plain list items into headings.
  17262. If it is a normal line, turn each and every normal line (i.e. not
  17263. an heading or an item) in the region into a heading.
  17264. When converting a line into a heading, the number of stars is chosen
  17265. such that the lines become children of the current entry. However,
  17266. when a prefix argument is given, its value determines the number of
  17267. stars to add."
  17268. (interactive "P")
  17269. (let ((skip-blanks
  17270. (function
  17271. ;; Return beginning of first non-blank line, starting from
  17272. ;; line at POS.
  17273. (lambda (pos)
  17274. (save-excursion
  17275. (goto-char pos)
  17276. (skip-chars-forward " \r\t\n")
  17277. (point-at-bol)))))
  17278. beg end)
  17279. ;; Determine boundaries of changes. If region ends at a bol, do
  17280. ;; not consider the last line to be in the region.
  17281. (if (org-region-active-p)
  17282. (setq beg (funcall skip-blanks (region-beginning))
  17283. end (copy-marker (save-excursion
  17284. (goto-char (region-end))
  17285. (if (bolp) (point) (point-at-eol)))))
  17286. (setq beg (funcall skip-blanks (point-at-bol))
  17287. end (copy-marker (point-at-eol))))
  17288. ;; Ensure inline tasks don't count as headings.
  17289. (org-with-limited-levels
  17290. (save-excursion
  17291. (goto-char beg)
  17292. (cond
  17293. ;; Case 1. Started at an heading: de-star headings.
  17294. ((org-at-heading-p)
  17295. (while (< (point) end)
  17296. (when (org-at-heading-p t)
  17297. (looking-at org-outline-regexp) (replace-match ""))
  17298. (forward-line)))
  17299. ;; Case 2. Started at an item: change items into headlines.
  17300. ;; One star will be added by `org-list-to-subtree'.
  17301. ((org-at-item-p)
  17302. (let* ((stars (make-string
  17303. (if nstars
  17304. ;; subtract the star that will be added again by
  17305. ;; `org-list-to-subtree'
  17306. (1- (prefix-numeric-value current-prefix-arg))
  17307. (or (org-current-level) 0))
  17308. ?*))
  17309. (add-stars
  17310. (cond (nstars "") ; stars from prefix only
  17311. ((equal stars "") "") ; before first heading
  17312. (org-odd-levels-only "*") ; inside heading, odd
  17313. (t "")))) ; inside heading, oddeven
  17314. (while (< (point) end)
  17315. (when (org-at-item-p)
  17316. ;; Pay attention to cases when region ends before list.
  17317. (let* ((struct (org-list-struct))
  17318. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  17319. (save-restriction
  17320. (narrow-to-region (point) list-end)
  17321. (insert
  17322. (org-list-to-subtree
  17323. (org-list-parse-list t)
  17324. '(:istart (concat stars add-stars (funcall get-stars depth))
  17325. :icount (concat stars add-stars (funcall get-stars depth))))))))
  17326. (forward-line))))
  17327. ;; Case 3. Started at normal text: make every line an heading,
  17328. ;; skipping headlines and items.
  17329. (t (let* ((stars (make-string
  17330. (if nstars
  17331. (prefix-numeric-value current-prefix-arg)
  17332. (or (org-current-level) 0))
  17333. ?*))
  17334. (add-stars
  17335. (cond (nstars "") ; stars from prefix only
  17336. ((equal stars "") "*") ; before first heading
  17337. (org-odd-levels-only "**") ; inside heading, odd
  17338. (t "*"))) ; inside heading, oddeven
  17339. (rpl (concat stars add-stars " ")))
  17340. (while (< (point) end)
  17341. (when (and (not (org-at-heading-p)) (not (org-at-item-p))
  17342. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  17343. (replace-match (concat rpl (match-string 2))))
  17344. (forward-line)))))))))
  17345. (defun org-meta-return (&optional arg)
  17346. "Insert a new heading or wrap a region in a table.
  17347. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  17348. See the individual commands for more information."
  17349. (interactive "P")
  17350. (cond
  17351. ((run-hook-with-args-until-success 'org-metareturn-hook))
  17352. ((or (org-at-drawer-p) (org-at-property-p))
  17353. (newline-and-indent))
  17354. ((org-at-table-p)
  17355. (call-interactively 'org-table-wrap-region))
  17356. (t (call-interactively 'org-insert-heading))))
  17357. ;;; Menu entries
  17358. ;; Define the Org-mode menus
  17359. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  17360. '("Tbl"
  17361. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  17362. ["Next Field" org-cycle (org-at-table-p)]
  17363. ["Previous Field" org-shifttab (org-at-table-p)]
  17364. ["Next Row" org-return (org-at-table-p)]
  17365. "--"
  17366. ["Blank Field" org-table-blank-field (org-at-table-p)]
  17367. ["Edit Field" org-table-edit-field (org-at-table-p)]
  17368. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  17369. "--"
  17370. ("Column"
  17371. ["Move Column Left" org-metaleft (org-at-table-p)]
  17372. ["Move Column Right" org-metaright (org-at-table-p)]
  17373. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  17374. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  17375. ("Row"
  17376. ["Move Row Up" org-metaup (org-at-table-p)]
  17377. ["Move Row Down" org-metadown (org-at-table-p)]
  17378. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  17379. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  17380. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  17381. "--"
  17382. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  17383. ("Rectangle"
  17384. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  17385. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  17386. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  17387. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  17388. "--"
  17389. ("Calculate"
  17390. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  17391. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  17392. ["Edit Formulas" org-edit-special (org-at-table-p)]
  17393. "--"
  17394. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  17395. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  17396. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  17397. "--"
  17398. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  17399. "--"
  17400. ["Sum Column/Rectangle" org-table-sum
  17401. (or (org-at-table-p) (org-region-active-p))]
  17402. ["Which Column?" org-table-current-column (org-at-table-p)])
  17403. ["Debug Formulas"
  17404. org-table-toggle-formula-debugger
  17405. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  17406. ["Show Col/Row Numbers"
  17407. org-table-toggle-coordinate-overlays
  17408. :style toggle
  17409. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  17410. "--"
  17411. ["Create" org-table-create (and (not (org-at-table-p))
  17412. org-enable-table-editor)]
  17413. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  17414. ["Import from File" org-table-import (not (org-at-table-p))]
  17415. ["Export to File" org-table-export (org-at-table-p)]
  17416. "--"
  17417. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  17418. (easy-menu-define org-org-menu org-mode-map "Org menu"
  17419. '("Org"
  17420. ("Show/Hide"
  17421. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  17422. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  17423. ["Sparse Tree..." org-sparse-tree t]
  17424. ["Reveal Context" org-reveal t]
  17425. ["Show All" show-all t]
  17426. "--"
  17427. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  17428. "--"
  17429. ["New Heading" org-insert-heading t]
  17430. ("Navigate Headings"
  17431. ["Up" outline-up-heading t]
  17432. ["Next" outline-next-visible-heading t]
  17433. ["Previous" outline-previous-visible-heading t]
  17434. ["Next Same Level" outline-forward-same-level t]
  17435. ["Previous Same Level" outline-backward-same-level t]
  17436. "--"
  17437. ["Jump" org-goto t])
  17438. ("Edit Structure"
  17439. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  17440. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  17441. "--"
  17442. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  17443. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  17444. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  17445. "--"
  17446. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  17447. "--"
  17448. ["Copy visible text" org-copy-visible t]
  17449. "--"
  17450. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  17451. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  17452. ["Demote Heading" org-metaright (not (org-at-table-p))]
  17453. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  17454. "--"
  17455. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  17456. "--"
  17457. ["Convert to odd levels" org-convert-to-odd-levels t]
  17458. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  17459. ("Editing"
  17460. ["Emphasis..." org-emphasize t]
  17461. ["Edit Source Example" org-edit-special t]
  17462. "--"
  17463. ["Footnote new/jump" org-footnote-action t]
  17464. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  17465. ("Archive"
  17466. ["Archive (default method)" org-archive-subtree-default t]
  17467. "--"
  17468. ["Move Subtree to Archive file" org-advertized-archive-subtree t]
  17469. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  17470. ["Move subtree to Archive sibling" org-archive-to-archive-sibling t]
  17471. )
  17472. "--"
  17473. ("Hyperlinks"
  17474. ["Store Link (Global)" org-store-link t]
  17475. ["Find existing link to here" org-occur-link-in-agenda-files t]
  17476. ["Insert Link" org-insert-link t]
  17477. ["Follow Link" org-open-at-point t]
  17478. "--"
  17479. ["Next link" org-next-link t]
  17480. ["Previous link" org-previous-link t]
  17481. "--"
  17482. ["Descriptive Links"
  17483. org-toggle-link-display
  17484. :style radio
  17485. :selected org-descriptive-links
  17486. ]
  17487. ["Literal Links"
  17488. org-toggle-link-display
  17489. :style radio
  17490. :selected (not org-descriptive-links)])
  17491. "--"
  17492. ("TODO Lists"
  17493. ["TODO/DONE/-" org-todo t]
  17494. ("Select keyword"
  17495. ["Next keyword" org-shiftright (org-at-heading-p)]
  17496. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  17497. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  17498. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  17499. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  17500. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  17501. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  17502. "--"
  17503. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  17504. :selected org-enforce-todo-dependencies :style toggle :active t]
  17505. "Settings for tree at point"
  17506. ["Do Children sequentially" org-toggle-ordered-property :style radio
  17507. :selected (org-entry-get nil "ORDERED")
  17508. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  17509. ["Do Children parallel" org-toggle-ordered-property :style radio
  17510. :selected (not (org-entry-get nil "ORDERED"))
  17511. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  17512. "--"
  17513. ["Set Priority" org-priority t]
  17514. ["Priority Up" org-shiftup t]
  17515. ["Priority Down" org-shiftdown t]
  17516. "--"
  17517. ["Get news from all feeds" org-feed-update-all t]
  17518. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  17519. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  17520. ("TAGS and Properties"
  17521. ["Set Tags" org-set-tags-command t]
  17522. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  17523. "--"
  17524. ["Set property" org-set-property t]
  17525. ["Column view of properties" org-columns t]
  17526. ["Insert Column View DBlock" org-insert-columns-dblock t])
  17527. ("Dates and Scheduling"
  17528. ["Timestamp" org-time-stamp t]
  17529. ["Timestamp (inactive)" org-time-stamp-inactive t]
  17530. ("Change Date"
  17531. ["1 Day Later" org-shiftright t]
  17532. ["1 Day Earlier" org-shiftleft t]
  17533. ["1 ... Later" org-shiftup t]
  17534. ["1 ... Earlier" org-shiftdown t])
  17535. ["Compute Time Range" org-evaluate-time-range t]
  17536. ["Schedule Item" org-schedule t]
  17537. ["Deadline" org-deadline t]
  17538. "--"
  17539. ["Custom time format" org-toggle-time-stamp-overlays
  17540. :style radio :selected org-display-custom-times]
  17541. "--"
  17542. ["Goto Calendar" org-goto-calendar t]
  17543. ["Date from Calendar" org-date-from-calendar t]
  17544. "--"
  17545. ["Start/Restart Timer" org-timer-start t]
  17546. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  17547. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  17548. ["Insert Timer String" org-timer t]
  17549. ["Insert Timer Item" org-timer-item t])
  17550. ("Logging work"
  17551. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  17552. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  17553. ["Clock out" org-clock-out t]
  17554. ["Clock cancel" org-clock-cancel t]
  17555. "--"
  17556. ["Mark as default task" org-clock-mark-default-task t]
  17557. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  17558. ["Goto running clock" org-clock-goto t]
  17559. "--"
  17560. ["Display times" org-clock-display t]
  17561. ["Create clock table" org-clock-report t]
  17562. "--"
  17563. ["Record DONE time"
  17564. (progn (setq org-log-done (not org-log-done))
  17565. (message "Switching to %s will %s record a timestamp"
  17566. (car org-done-keywords)
  17567. (if org-log-done "automatically" "not")))
  17568. :style toggle :selected org-log-done])
  17569. "--"
  17570. ["Agenda Command..." org-agenda t]
  17571. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  17572. ("File List for Agenda")
  17573. ("Special views current file"
  17574. ["TODO Tree" org-show-todo-tree t]
  17575. ["Check Deadlines" org-check-deadlines t]
  17576. ["Timeline" org-timeline t]
  17577. ["Tags/Property tree" org-match-sparse-tree t])
  17578. "--"
  17579. ["Export/Publish..." org-export t]
  17580. ("LaTeX"
  17581. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  17582. :selected org-cdlatex-mode]
  17583. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  17584. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  17585. ["Modify math symbol" org-cdlatex-math-modify
  17586. (org-inside-LaTeX-fragment-p)]
  17587. ["Insert citation" org-reftex-citation t]
  17588. "--"
  17589. ["Template for BEAMER" org-insert-beamer-options-template t])
  17590. "--"
  17591. ("MobileOrg"
  17592. ["Push Files and Views" org-mobile-push t]
  17593. ["Get Captured and Flagged" org-mobile-pull t]
  17594. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  17595. "--"
  17596. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  17597. "--"
  17598. ("Documentation"
  17599. ["Show Version" org-version t]
  17600. ["Info Documentation" org-info t])
  17601. ("Customize"
  17602. ["Browse Org Group" org-customize t]
  17603. "--"
  17604. ["Expand This Menu" org-create-customize-menu
  17605. (fboundp 'customize-menu-create)])
  17606. ["Send bug report" org-submit-bug-report t]
  17607. "--"
  17608. ("Refresh/Reload"
  17609. ["Refresh setup current buffer" org-mode-restart t]
  17610. ["Reload Org (after update)" org-reload t]
  17611. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  17612. ))
  17613. (defun org-info (&optional node)
  17614. "Read documentation for Org-mode in the info system.
  17615. With optional NODE, go directly to that node."
  17616. (interactive)
  17617. (info (format "(org)%s" (or node ""))))
  17618. ;;;###autoload
  17619. (defun org-submit-bug-report ()
  17620. "Submit a bug report on Org-mode via mail.
  17621. Don't hesitate to report any problems or inaccurate documentation.
  17622. If you don't have setup sending mail from (X)Emacs, please copy the
  17623. output buffer into your mail program, as it gives us important
  17624. information about your Org-mode version and configuration."
  17625. (interactive)
  17626. (require 'reporter)
  17627. (org-load-modules-maybe)
  17628. (org-require-autoloaded-modules)
  17629. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  17630. (reporter-submit-bug-report
  17631. "emacs-orgmode@gnu.org"
  17632. (org-version)
  17633. (let (list)
  17634. (save-window-excursion
  17635. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  17636. (delete-other-windows)
  17637. (erase-buffer)
  17638. (insert "You are about to submit a bug report to the Org-mode mailing list.
  17639. We would like to add your full Org-mode and Outline configuration to the
  17640. bug report. This greatly simplifies the work of the maintainer and
  17641. other experts on the mailing list.
  17642. HOWEVER, some variables you have customized may contain private
  17643. information. The names of customers, colleagues, or friends, might
  17644. appear in the form of file names, tags, todo states, or search strings.
  17645. If you answer yes to the prompt, you might want to check and remove
  17646. such private information before sending the email.")
  17647. (add-text-properties (point-min) (point-max) '(face org-warning))
  17648. (when (yes-or-no-p "Include your Org-mode configuration ")
  17649. (mapatoms
  17650. (lambda (v)
  17651. (and (boundp v)
  17652. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  17653. (or (and (symbol-value v)
  17654. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  17655. (and
  17656. (get v 'custom-type) (get v 'standard-value)
  17657. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  17658. (push v list)))))
  17659. (kill-buffer (get-buffer "*Warn about privacy*"))
  17660. list))
  17661. nil nil
  17662. "Remember to cover the basics, that is, what you expected to happen and
  17663. what in fact did happen. You don't know how to make a good report? See
  17664. http://orgmode.org/manual/Feedback.html#Feedback
  17665. Your bug report will be posted to the Org-mode mailing list.
  17666. ------------------------------------------------------------------------")
  17667. (save-excursion
  17668. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  17669. (replace-match "\\1Bug: \\3 [\\2]")))))
  17670. (defun org-install-agenda-files-menu ()
  17671. (let ((bl (buffer-list)))
  17672. (save-excursion
  17673. (while bl
  17674. (set-buffer (pop bl))
  17675. (if (eq major-mode 'org-mode) (setq bl nil)))
  17676. (when (eq major-mode 'org-mode)
  17677. (easy-menu-change
  17678. '("Org") "File List for Agenda"
  17679. (append
  17680. (list
  17681. ["Edit File List" (org-edit-agenda-file-list) t]
  17682. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  17683. ["Remove Current File from List" org-remove-file t]
  17684. ["Cycle through agenda files" org-cycle-agenda-files t]
  17685. ["Occur in all agenda files" org-occur-in-agenda-files t]
  17686. "--")
  17687. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  17688. ;;;; Documentation
  17689. ;;;###autoload
  17690. (defun org-require-autoloaded-modules ()
  17691. (interactive)
  17692. (mapc 'require
  17693. '(org-agenda org-archive org-ascii org-attach org-clock org-colview
  17694. org-docbook org-exp org-html org-icalendar
  17695. org-id org-latex
  17696. org-publish org-remember org-table
  17697. org-timer org-xoxo)))
  17698. ;;;###autoload
  17699. (defun org-reload (&optional uncompiled)
  17700. "Reload all org lisp files.
  17701. With prefix arg UNCOMPILED, load the uncompiled versions."
  17702. (interactive "P")
  17703. (require 'find-func)
  17704. (let* ((file-re "^\\(org\\|orgtbl\\)\\(\\.el\\|-.*\\.el\\)")
  17705. (dir-org (file-name-directory (org-find-library-name "org")))
  17706. (dir-org-contrib (ignore-errors
  17707. (file-name-directory
  17708. (org-find-library-name "org-contribdir"))))
  17709. (babel-files
  17710. (mapcar (lambda (el) (concat "ob" (when el (format "-%s" el)) ".el"))
  17711. (append (list nil "comint" "eval" "exp" "keys"
  17712. "lob" "ref" "table" "tangle")
  17713. (delq nil
  17714. (mapcar
  17715. (lambda (lang)
  17716. (when (cdr lang) (symbol-name (car lang))))
  17717. org-babel-load-languages)))))
  17718. (files
  17719. (append (directory-files dir-org t file-re)
  17720. babel-files
  17721. (and dir-org-contrib
  17722. (directory-files dir-org-contrib t file-re))))
  17723. (remove-re (concat (if (featurep 'xemacs)
  17724. "org-colview" "org-colview-xemacs")
  17725. "\\'")))
  17726. (setq files (mapcar 'file-name-sans-extension files))
  17727. (setq files (mapcar
  17728. (lambda (x) (if (string-match remove-re x) nil x))
  17729. files))
  17730. (setq files (delq nil files))
  17731. (mapc
  17732. (lambda (f)
  17733. (when (featurep (intern (file-name-nondirectory f)))
  17734. (if (and (not uncompiled)
  17735. (file-exists-p (concat f ".elc")))
  17736. (load (concat f ".elc") nil nil t)
  17737. (load (concat f ".el") nil nil t))))
  17738. files))
  17739. (org-version))
  17740. ;;;###autoload
  17741. (defun org-customize ()
  17742. "Call the customize function with org as argument."
  17743. (interactive)
  17744. (org-load-modules-maybe)
  17745. (org-require-autoloaded-modules)
  17746. (customize-browse 'org))
  17747. (defun org-create-customize-menu ()
  17748. "Create a full customization menu for Org-mode, insert it into the menu."
  17749. (interactive)
  17750. (org-load-modules-maybe)
  17751. (org-require-autoloaded-modules)
  17752. (if (fboundp 'customize-menu-create)
  17753. (progn
  17754. (easy-menu-change
  17755. '("Org") "Customize"
  17756. `(["Browse Org group" org-customize t]
  17757. "--"
  17758. ,(customize-menu-create 'org)
  17759. ["Set" Custom-set t]
  17760. ["Save" Custom-save t]
  17761. ["Reset to Current" Custom-reset-current t]
  17762. ["Reset to Saved" Custom-reset-saved t]
  17763. ["Reset to Standard Settings" Custom-reset-standard t]))
  17764. (message "\"Org\"-menu now contains full customization menu"))
  17765. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  17766. ;;;; Miscellaneous stuff
  17767. ;;; Generally useful functions
  17768. (defun org-get-at-bol (property)
  17769. "Get text property PROPERTY at beginning of line."
  17770. (get-text-property (point-at-bol) property))
  17771. (defun org-find-text-property-in-string (prop s)
  17772. "Return the first non-nil value of property PROP in string S."
  17773. (or (get-text-property 0 prop s)
  17774. (get-text-property (or (next-single-property-change 0 prop s) 0)
  17775. prop s)))
  17776. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  17777. "Display the given MESSAGE as a warning."
  17778. (if (fboundp 'display-warning)
  17779. (display-warning 'org message
  17780. (if (featurep 'xemacs) 'warning :warning))
  17781. (let ((buf (get-buffer-create "*Org warnings*")))
  17782. (with-current-buffer buf
  17783. (goto-char (point-max))
  17784. (insert "Warning (Org): " message)
  17785. (unless (bolp)
  17786. (newline)))
  17787. (display-buffer buf)
  17788. (sit-for 0))))
  17789. (defun org-eval (form)
  17790. "Eval FORM and return result."
  17791. (condition-case error
  17792. (eval form)
  17793. (error (format "%%![Error: %s]" error))))
  17794. (defun org-in-clocktable-p ()
  17795. "Check if the cursor is in a clocktable."
  17796. (let ((pos (point)) start)
  17797. (save-excursion
  17798. (end-of-line 1)
  17799. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  17800. (setq start (match-beginning 0))
  17801. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  17802. (>= (match-end 0) pos)
  17803. start))))
  17804. (defun org-in-commented-line ()
  17805. "Is point in a line starting with `#'?"
  17806. (equal (char-after (point-at-bol)) ?#))
  17807. (defun org-in-indented-comment-line ()
  17808. "Is point in a line starting with `#' after some white space?"
  17809. (save-excursion
  17810. (save-match-data
  17811. (goto-char (point-at-bol))
  17812. (looking-at "[ \t]*#"))))
  17813. (defun org-in-verbatim-emphasis ()
  17814. (save-match-data
  17815. (and (org-in-regexp org-emph-re 2) (member (match-string 3) '("=" "~")))))
  17816. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  17817. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  17818. (if (and marker (marker-buffer marker)
  17819. (buffer-live-p (marker-buffer marker)))
  17820. (progn
  17821. (org-pop-to-buffer-same-window (marker-buffer marker))
  17822. (if (or (> marker (point-max)) (< marker (point-min)))
  17823. (widen))
  17824. (goto-char marker)
  17825. (org-show-context 'org-goto))
  17826. (if bookmark
  17827. (bookmark-jump bookmark)
  17828. (error "Cannot find location"))))
  17829. (defun org-quote-csv-field (s)
  17830. "Quote field for inclusion in CSV material."
  17831. (if (string-match "[\",]" s)
  17832. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  17833. s))
  17834. (defun org-force-self-insert (N)
  17835. "Needed to enforce self-insert under remapping."
  17836. (interactive "p")
  17837. (self-insert-command N))
  17838. (defun org-string-width (s)
  17839. "Compute width of string, ignoring invisible characters.
  17840. This ignores character with invisibility property `org-link', and also
  17841. characters with property `org-cwidth', because these will become invisible
  17842. upon the next fontification round."
  17843. (let (b l)
  17844. (when (or (eq t buffer-invisibility-spec)
  17845. (assq 'org-link buffer-invisibility-spec))
  17846. (while (setq b (text-property-any 0 (length s)
  17847. 'invisible 'org-link s))
  17848. (setq s (concat (substring s 0 b)
  17849. (substring s (or (next-single-property-change
  17850. b 'invisible s) (length s)))))))
  17851. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  17852. (setq s (concat (substring s 0 b)
  17853. (substring s (or (next-single-property-change
  17854. b 'org-cwidth s) (length s))))))
  17855. (setq l (string-width s) b -1)
  17856. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  17857. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  17858. l))
  17859. (defun org-shorten-string (s maxlength)
  17860. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  17861. If the string is shorter or has length MAXLENGTH, just return the
  17862. original string. If it is longer, the functions finds a space in the
  17863. string, breaks this string off at that locations and adds three dots
  17864. as ellipsis. Including the ellipsis, the string will not be longer
  17865. than MAXLENGTH. If finding a good breaking point in the string does
  17866. not work, the string is just chopped off in the middle of a word
  17867. if necessary."
  17868. (if (<= (length s) maxlength)
  17869. s
  17870. (let* ((n (max (- maxlength 4) 1))
  17871. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  17872. (if (string-match re s)
  17873. (concat (match-string 1 s) "...")
  17874. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  17875. (defun org-get-indentation (&optional line)
  17876. "Get the indentation of the current line, interpreting tabs.
  17877. When LINE is given, assume it represents a line and compute its indentation."
  17878. (if line
  17879. (if (string-match "^ *" (org-remove-tabs line))
  17880. (match-end 0))
  17881. (save-excursion
  17882. (beginning-of-line 1)
  17883. (skip-chars-forward " \t")
  17884. (current-column))))
  17885. (defun org-get-string-indentation (s)
  17886. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  17887. (let ((n -1) (i 0) (w tab-width) c)
  17888. (catch 'exit
  17889. (while (< (setq n (1+ n)) (length s))
  17890. (setq c (aref s n))
  17891. (cond ((= c ?\ ) (setq i (1+ i)))
  17892. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  17893. (t (throw 'exit t)))))
  17894. i))
  17895. (defun org-remove-tabs (s &optional width)
  17896. "Replace tabulators in S with spaces.
  17897. Assumes that s is a single line, starting in column 0."
  17898. (setq width (or width tab-width))
  17899. (while (string-match "\t" s)
  17900. (setq s (replace-match
  17901. (make-string
  17902. (- (* width (/ (+ (match-beginning 0) width) width))
  17903. (match-beginning 0)) ?\ )
  17904. t t s)))
  17905. s)
  17906. (defun org-fix-indentation (line ind)
  17907. "Fix indentation in LINE.
  17908. IND is a cons cell with target and minimum indentation.
  17909. If the current indentation in LINE is smaller than the minimum,
  17910. leave it alone. If it is larger than ind, set it to the target."
  17911. (let* ((l (org-remove-tabs line))
  17912. (i (org-get-indentation l))
  17913. (i1 (car ind)) (i2 (cdr ind)))
  17914. (if (>= i i2) (setq l (substring line i2)))
  17915. (if (> i1 0)
  17916. (concat (make-string i1 ?\ ) l)
  17917. l)))
  17918. (defun org-remove-indentation (code &optional n)
  17919. "Remove the maximum common indentation from the lines in CODE.
  17920. N may optionally be the number of spaces to remove."
  17921. (with-temp-buffer
  17922. (insert code)
  17923. (org-do-remove-indentation n)
  17924. (buffer-string)))
  17925. (defun org-do-remove-indentation (&optional n)
  17926. "Remove the maximum common indentation from the buffer."
  17927. (untabify (point-min) (point-max))
  17928. (let ((min 10000) re)
  17929. (if n
  17930. (setq min n)
  17931. (goto-char (point-min))
  17932. (while (re-search-forward "^ *[^ \n]" nil t)
  17933. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  17934. (unless (or (= min 0) (= min 10000))
  17935. (setq re (format "^ \\{%d\\}" min))
  17936. (goto-char (point-min))
  17937. (while (re-search-forward re nil t)
  17938. (replace-match "")
  17939. (end-of-line 1))
  17940. min)))
  17941. (defun org-fill-template (template alist)
  17942. "Find each %key of ALIST in TEMPLATE and replace it."
  17943. (let ((case-fold-search nil)
  17944. entry key value)
  17945. (setq alist (sort (copy-sequence alist)
  17946. (lambda (a b) (< (length (car a)) (length (car b))))))
  17947. (while (setq entry (pop alist))
  17948. (setq template
  17949. (replace-regexp-in-string
  17950. (concat "%" (regexp-quote (car entry)))
  17951. (cdr entry) template t t)))
  17952. template))
  17953. (defun org-base-buffer (buffer)
  17954. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  17955. (if (not buffer)
  17956. buffer
  17957. (or (buffer-base-buffer buffer)
  17958. buffer)))
  17959. (defun org-trim (s)
  17960. "Remove whitespace at beginning and end of string."
  17961. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  17962. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  17963. s)
  17964. (defun org-wrap (string &optional width lines)
  17965. "Wrap string to either a number of lines, or a width in characters.
  17966. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  17967. that costs. If there is a word longer than WIDTH, the text is actually
  17968. wrapped to the length of that word.
  17969. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  17970. many lines, whatever width that takes.
  17971. The return value is a list of lines, without newlines at the end."
  17972. (let* ((words (org-split-string string "[ \t\n]+"))
  17973. (maxword (apply 'max (mapcar 'org-string-width words)))
  17974. w ll)
  17975. (cond (width
  17976. (org-do-wrap words (max maxword width)))
  17977. (lines
  17978. (setq w maxword)
  17979. (setq ll (org-do-wrap words maxword))
  17980. (if (<= (length ll) lines)
  17981. ll
  17982. (setq ll words)
  17983. (while (> (length ll) lines)
  17984. (setq w (1+ w))
  17985. (setq ll (org-do-wrap words w)))
  17986. ll))
  17987. (t (error "Cannot wrap this")))))
  17988. (defun org-do-wrap (words width)
  17989. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  17990. (let (lines line)
  17991. (while words
  17992. (setq line (pop words))
  17993. (while (and words (< (+ (length line) (length (car words))) width))
  17994. (setq line (concat line " " (pop words))))
  17995. (setq lines (push line lines)))
  17996. (nreverse lines)))
  17997. (defun org-split-string (string &optional separators)
  17998. "Splits STRING into substrings at SEPARATORS.
  17999. No empty strings are returned if there are matches at the beginning
  18000. and end of string."
  18001. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  18002. (start 0)
  18003. notfirst
  18004. (list nil))
  18005. (while (and (string-match rexp string
  18006. (if (and notfirst
  18007. (= start (match-beginning 0))
  18008. (< start (length string)))
  18009. (1+ start) start))
  18010. (< (match-beginning 0) (length string)))
  18011. (setq notfirst t)
  18012. (or (eq (match-beginning 0) 0)
  18013. (and (eq (match-beginning 0) (match-end 0))
  18014. (eq (match-beginning 0) start))
  18015. (setq list
  18016. (cons (substring string start (match-beginning 0))
  18017. list)))
  18018. (setq start (match-end 0)))
  18019. (or (eq start (length string))
  18020. (setq list
  18021. (cons (substring string start)
  18022. list)))
  18023. (nreverse list)))
  18024. (defun org-quote-vert (s)
  18025. "Replace \"|\" with \"\\vert\"."
  18026. (while (string-match "|" s)
  18027. (setq s (replace-match "\\vert" t t s)))
  18028. s)
  18029. (defun org-uuidgen-p (s)
  18030. "Is S an ID created by UUIDGEN?"
  18031. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  18032. (defun org-in-src-block-p nil
  18033. "Whether point is in a code source block."
  18034. (let (ov)
  18035. (when (setq ov (overlays-at (point)))
  18036. (memq 'org-block-background
  18037. (overlay-properties
  18038. (car ov))))))
  18039. (defun org-context ()
  18040. "Return a list of contexts of the current cursor position.
  18041. If several contexts apply, all are returned.
  18042. Each context entry is a list with a symbol naming the context, and
  18043. two positions indicating start and end of the context. Possible
  18044. contexts are:
  18045. :headline anywhere in a headline
  18046. :headline-stars on the leading stars in a headline
  18047. :todo-keyword on a TODO keyword (including DONE) in a headline
  18048. :tags on the TAGS in a headline
  18049. :priority on the priority cookie in a headline
  18050. :item on the first line of a plain list item
  18051. :item-bullet on the bullet/number of a plain list item
  18052. :checkbox on the checkbox in a plain list item
  18053. :table in an org-mode table
  18054. :table-special on a special filed in a table
  18055. :table-table in a table.el table
  18056. :clocktable in a clocktable
  18057. :src-block in a source block
  18058. :link on a hyperlink
  18059. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT, QUOTE.
  18060. :target on a <<target>>
  18061. :radio-target on a <<<radio-target>>>
  18062. :latex-fragment on a LaTeX fragment
  18063. :latex-preview on a LaTeX fragment with overlaid preview image
  18064. This function expects the position to be visible because it uses font-lock
  18065. faces as a help to recognize the following contexts: :table-special, :link,
  18066. and :keyword."
  18067. (let* ((f (get-text-property (point) 'face))
  18068. (faces (if (listp f) f (list f)))
  18069. (case-fold-search t)
  18070. (p (point)) clist o)
  18071. ;; First the large context
  18072. (cond
  18073. ((org-at-heading-p t)
  18074. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  18075. (when (progn
  18076. (beginning-of-line 1)
  18077. (looking-at org-todo-line-tags-regexp))
  18078. (push (org-point-in-group p 1 :headline-stars) clist)
  18079. (push (org-point-in-group p 2 :todo-keyword) clist)
  18080. (push (org-point-in-group p 4 :tags) clist))
  18081. (goto-char p)
  18082. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  18083. (if (looking-at "\\[#[A-Z0-9]\\]")
  18084. (push (org-point-in-group p 0 :priority) clist)))
  18085. ((org-at-item-p)
  18086. (push (org-point-in-group p 2 :item-bullet) clist)
  18087. (push (list :item (point-at-bol)
  18088. (save-excursion (org-end-of-item) (point)))
  18089. clist)
  18090. (and (org-at-item-checkbox-p)
  18091. (push (org-point-in-group p 0 :checkbox) clist)))
  18092. ((org-at-table-p)
  18093. (push (list :table (org-table-begin) (org-table-end)) clist)
  18094. (if (memq 'org-formula faces)
  18095. (push (list :table-special
  18096. (previous-single-property-change p 'face)
  18097. (next-single-property-change p 'face)) clist)))
  18098. ((org-at-table-p 'any)
  18099. (push (list :table-table) clist)))
  18100. (goto-char p)
  18101. ;; New the "medium" contexts: clocktables, source blocks
  18102. (cond ((org-in-clocktable-p)
  18103. (push (list :clocktable
  18104. (and (or (looking-at "#\\+BEGIN: clocktable")
  18105. (search-backward "#+BEGIN: clocktable" nil t))
  18106. (match-beginning 0))
  18107. (and (re-search-forward "#\\+END:?" nil t)
  18108. (match-end 0))) clist))
  18109. ((org-in-src-block-p)
  18110. (push (list :src-block
  18111. (and (or (looking-at "#\\+BEGIN_SRC")
  18112. (search-backward "#+BEGIN_SRC" nil t))
  18113. (match-beginning 0))
  18114. (and (search-forward "#+END_SRC" nil t)
  18115. (match-beginning 0))) clist)))
  18116. (goto-char p)
  18117. ;; Now the small context
  18118. (cond
  18119. ((org-at-timestamp-p)
  18120. (push (org-point-in-group p 0 :timestamp) clist))
  18121. ((memq 'org-link faces)
  18122. (push (list :link
  18123. (previous-single-property-change p 'face)
  18124. (next-single-property-change p 'face)) clist))
  18125. ((memq 'org-special-keyword faces)
  18126. (push (list :keyword
  18127. (previous-single-property-change p 'face)
  18128. (next-single-property-change p 'face)) clist))
  18129. ((org-at-target-p)
  18130. (push (org-point-in-group p 0 :target) clist)
  18131. (goto-char (1- (match-beginning 0)))
  18132. (if (looking-at org-radio-target-regexp)
  18133. (push (org-point-in-group p 0 :radio-target) clist))
  18134. (goto-char p))
  18135. ((setq o (car (delq nil
  18136. (mapcar
  18137. (lambda (x)
  18138. (if (memq x org-latex-fragment-image-overlays) x))
  18139. (overlays-at (point))))))
  18140. (push (list :latex-fragment
  18141. (overlay-start o) (overlay-end o)) clist)
  18142. (push (list :latex-preview
  18143. (overlay-start o) (overlay-end o)) clist))
  18144. ((org-inside-LaTeX-fragment-p)
  18145. ;; FIXME: positions wrong.
  18146. (push (list :latex-fragment (point) (point)) clist)))
  18147. (setq clist (nreverse (delq nil clist)))
  18148. clist))
  18149. ;; FIXME: Compare with at-regexp-p Do we need both?
  18150. (defun org-in-regexp (re &optional nlines visually)
  18151. "Check if point is inside a match of regexp.
  18152. Normally only the current line is checked, but you can include NLINES extra
  18153. lines both before and after point into the search.
  18154. If VISUALLY is set, require that the cursor is not after the match but
  18155. really on, so that the block visually is on the match."
  18156. (catch 'exit
  18157. (let ((pos (point))
  18158. (eol (point-at-eol (+ 1 (or nlines 0))))
  18159. (inc (if visually 1 0)))
  18160. (save-excursion
  18161. (beginning-of-line (- 1 (or nlines 0)))
  18162. (while (re-search-forward re eol t)
  18163. (if (and (<= (match-beginning 0) pos)
  18164. (>= (+ inc (match-end 0)) pos))
  18165. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  18166. (defun org-at-regexp-p (regexp)
  18167. "Is point inside a match of REGEXP in the current line?"
  18168. (catch 'exit
  18169. (save-excursion
  18170. (let ((pos (point)) (end (point-at-eol)))
  18171. (beginning-of-line 1)
  18172. (while (re-search-forward regexp end t)
  18173. (if (and (<= (match-beginning 0) pos)
  18174. (>= (match-end 0) pos))
  18175. (throw 'exit t)))
  18176. nil))))
  18177. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  18178. "Non-nil when point is between matches of START-RE and END-RE.
  18179. Also return a non-nil value when point is on one of the matches.
  18180. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  18181. buffer positions. Default values are the positions of headlines
  18182. surrounding the point.
  18183. The functions returns a cons cell whose car (resp. cdr) is the
  18184. position before START-RE (resp. after END-RE)."
  18185. (save-match-data
  18186. (let ((pos (point))
  18187. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  18188. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  18189. beg end)
  18190. (save-excursion
  18191. ;; Point is on a block when on START-RE or if START-RE can be
  18192. ;; found before it...
  18193. (and (or (org-at-regexp-p start-re)
  18194. (re-search-backward start-re limit-up t))
  18195. (setq beg (match-beginning 0))
  18196. ;; ... and END-RE after it...
  18197. (goto-char (match-end 0))
  18198. (re-search-forward end-re limit-down t)
  18199. (> (setq end (match-end 0)) pos)
  18200. ;; ... without another START-RE in-between.
  18201. (goto-char (match-beginning 0))
  18202. (not (re-search-backward start-re (1+ beg) t))
  18203. ;; Return value.
  18204. (cons beg end))))))
  18205. (defun org-in-block-p (names)
  18206. "Non-nil when point belongs to a block whose name belongs to NAMES.
  18207. NAMES is a list of strings containing names of blocks.
  18208. Return first block name matched, or nil. Beware that in case of
  18209. nested blocks, the returned name may not belong to the closest
  18210. block from point."
  18211. (save-match-data
  18212. (catch 'exit
  18213. (let ((case-fold-search t)
  18214. (lim-up (save-excursion (outline-previous-heading)))
  18215. (lim-down (save-excursion (outline-next-heading))))
  18216. (mapc (lambda (name)
  18217. (let ((n (regexp-quote name)))
  18218. (when (org-between-regexps-p
  18219. (concat "^[ \t]*#\\+begin_" n)
  18220. (concat "^[ \t]*#\\+end_" n)
  18221. lim-up lim-down)
  18222. (throw 'exit n))))
  18223. names))
  18224. nil)))
  18225. (defun org-occur-in-agenda-files (regexp &optional nlines)
  18226. "Call `multi-occur' with buffers for all agenda files."
  18227. (interactive "sOrg-files matching: \np")
  18228. (let* ((files (org-agenda-files))
  18229. (tnames (mapcar 'file-truename files))
  18230. (extra org-agenda-text-search-extra-files)
  18231. f)
  18232. (when (eq (car extra) 'agenda-archives)
  18233. (setq extra (cdr extra))
  18234. (setq files (org-add-archive-files files)))
  18235. (while (setq f (pop extra))
  18236. (unless (member (file-truename f) tnames)
  18237. (add-to-list 'files f 'append)
  18238. (add-to-list 'tnames (file-truename f) 'append)))
  18239. (multi-occur
  18240. (mapcar (lambda (x)
  18241. (with-current-buffer
  18242. (or (get-file-buffer x) (find-file-noselect x))
  18243. (widen)
  18244. (current-buffer)))
  18245. files)
  18246. regexp)))
  18247. (if (boundp 'occur-mode-find-occurrence-hook)
  18248. ;; Emacs 23
  18249. (add-hook 'occur-mode-find-occurrence-hook
  18250. (lambda ()
  18251. (when (eq major-mode 'org-mode)
  18252. (org-reveal))))
  18253. ;; Emacs 22
  18254. (defadvice occur-mode-goto-occurrence
  18255. (after org-occur-reveal activate)
  18256. (and (eq major-mode 'org-mode) (org-reveal)))
  18257. (defadvice occur-mode-goto-occurrence-other-window
  18258. (after org-occur-reveal activate)
  18259. (and (eq major-mode 'org-mode) (org-reveal)))
  18260. (defadvice occur-mode-display-occurrence
  18261. (after org-occur-reveal activate)
  18262. (when (eq major-mode 'org-mode)
  18263. (let ((pos (occur-mode-find-occurrence)))
  18264. (with-current-buffer (marker-buffer pos)
  18265. (save-excursion
  18266. (goto-char pos)
  18267. (org-reveal)))))))
  18268. (defun org-occur-link-in-agenda-files ()
  18269. "Create a link and search for it in the agendas.
  18270. The link is not stored in `org-stored-links', it is just created
  18271. for the search purpose."
  18272. (interactive)
  18273. (let ((link (condition-case nil
  18274. (org-store-link nil)
  18275. (error "Unable to create a link to here"))))
  18276. (org-occur-in-agenda-files (regexp-quote link))))
  18277. (defun org-uniquify (list)
  18278. "Remove duplicate elements from LIST."
  18279. (let (res)
  18280. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  18281. res))
  18282. (defun org-delete-all (elts list)
  18283. "Remove all elements in ELTS from LIST."
  18284. (while elts
  18285. (setq list (delete (pop elts) list)))
  18286. list)
  18287. (defun org-count (cl-item cl-seq)
  18288. "Count the number of occurrences of ITEM in SEQ.
  18289. Taken from `count' in cl-seq.el with all keyword arguments removed."
  18290. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  18291. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  18292. (while (< cl-start cl-end)
  18293. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  18294. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  18295. (setq cl-start (1+ cl-start)))
  18296. cl-count))
  18297. (defun org-remove-if (predicate seq)
  18298. "Remove everything from SEQ that fulfills PREDICATE."
  18299. (let (res e)
  18300. (while seq
  18301. (setq e (pop seq))
  18302. (if (not (funcall predicate e)) (push e res)))
  18303. (nreverse res)))
  18304. (defun org-remove-if-not (predicate seq)
  18305. "Remove everything from SEQ that does not fulfill PREDICATE."
  18306. (let (res e)
  18307. (while seq
  18308. (setq e (pop seq))
  18309. (if (funcall predicate e) (push e res)))
  18310. (nreverse res)))
  18311. (defun org-reduce (cl-func cl-seq &rest cl-keys)
  18312. "Reduce two-argument FUNCTION across SEQ.
  18313. Taken from `reduce' in cl-seq.el with all keyword arguments but
  18314. \":initial-value\" removed."
  18315. (let ((cl-accum (cond ((memq :initial-value cl-keys)
  18316. (cadr (memq :initial-value cl-keys)))
  18317. (cl-seq (pop cl-seq))
  18318. (t (funcall cl-func)))))
  18319. (while cl-seq
  18320. (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
  18321. cl-accum))
  18322. (defun org-back-over-empty-lines ()
  18323. "Move backwards over whitespace, to the beginning of the first empty line.
  18324. Returns the number of empty lines passed."
  18325. (let ((pos (point)))
  18326. (if (cdr (assoc 'heading org-blank-before-new-entry))
  18327. (skip-chars-backward " \t\n\r")
  18328. (unless (eobp)
  18329. (forward-line -1)))
  18330. (beginning-of-line 2)
  18331. (goto-char (min (point) pos))
  18332. (count-lines (point) pos)))
  18333. (defun org-skip-whitespace ()
  18334. (skip-chars-forward " \t\n\r"))
  18335. (defun org-point-in-group (point group &optional context)
  18336. "Check if POINT is in match-group GROUP.
  18337. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  18338. match. If the match group does not exist or point is not inside it,
  18339. return nil."
  18340. (and (match-beginning group)
  18341. (>= point (match-beginning group))
  18342. (<= point (match-end group))
  18343. (if context
  18344. (list context (match-beginning group) (match-end group))
  18345. t)))
  18346. (defun org-switch-to-buffer-other-window (&rest args)
  18347. "Switch to buffer in a second window on the current frame.
  18348. In particular, do not allow pop-up frames.
  18349. Returns the newly created buffer."
  18350. (let (pop-up-frames special-display-buffer-names special-display-regexps
  18351. special-display-function)
  18352. (apply 'switch-to-buffer-other-window args)))
  18353. (defun org-combine-plists (&rest plists)
  18354. "Create a single property list from all plists in PLISTS.
  18355. The process starts by copying the first list, and then setting properties
  18356. from the other lists. Settings in the last list are the most significant
  18357. ones and overrule settings in the other lists."
  18358. (let ((rtn (copy-sequence (pop plists)))
  18359. p v ls)
  18360. (while plists
  18361. (setq ls (pop plists))
  18362. (while ls
  18363. (setq p (pop ls) v (pop ls))
  18364. (setq rtn (plist-put rtn p v))))
  18365. rtn))
  18366. (defun org-move-line-down (arg)
  18367. "Move the current line down. With prefix argument, move it past ARG lines."
  18368. (interactive "p")
  18369. (let ((col (current-column))
  18370. beg end pos)
  18371. (beginning-of-line 1) (setq beg (point))
  18372. (beginning-of-line 2) (setq end (point))
  18373. (beginning-of-line (+ 1 arg))
  18374. (setq pos (move-marker (make-marker) (point)))
  18375. (insert (delete-and-extract-region beg end))
  18376. (goto-char pos)
  18377. (org-move-to-column col)))
  18378. (defun org-move-line-up (arg)
  18379. "Move the current line up. With prefix argument, move it past ARG lines."
  18380. (interactive "p")
  18381. (let ((col (current-column))
  18382. beg end pos)
  18383. (beginning-of-line 1) (setq beg (point))
  18384. (beginning-of-line 2) (setq end (point))
  18385. (beginning-of-line (- arg))
  18386. (setq pos (move-marker (make-marker) (point)))
  18387. (insert (delete-and-extract-region beg end))
  18388. (goto-char pos)
  18389. (org-move-to-column col)))
  18390. (defun org-replace-escapes (string table)
  18391. "Replace %-escapes in STRING with values in TABLE.
  18392. TABLE is an association list with keys like \"%a\" and string values.
  18393. The sequences in STRING may contain normal field width and padding information,
  18394. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  18395. so values can contain further %-escapes if they are define later in TABLE."
  18396. (let ((tbl (copy-alist table))
  18397. (case-fold-search nil)
  18398. (pchg 0)
  18399. e re rpl)
  18400. (while (setq e (pop tbl))
  18401. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  18402. (when (and (cdr e) (string-match re (cdr e)))
  18403. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  18404. (safe "SREF"))
  18405. (add-text-properties 0 3 (list 'sref sref) safe)
  18406. (setcdr e (replace-match safe t t (cdr e)))))
  18407. (while (string-match re string)
  18408. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  18409. (cdr e)))
  18410. (setq string (replace-match rpl t t string))))
  18411. (while (setq pchg (next-property-change pchg string))
  18412. (let ((sref (get-text-property pchg 'sref string)))
  18413. (when (and sref (string-match "SREF" string pchg))
  18414. (setq string (replace-match sref t t string)))))
  18415. string))
  18416. (defun org-sublist (list start end)
  18417. "Return a section of LIST, from START to END.
  18418. Counting starts at 1."
  18419. (let (rtn (c start))
  18420. (setq list (nthcdr (1- start) list))
  18421. (while (and list (<= c end))
  18422. (push (pop list) rtn)
  18423. (setq c (1+ c)))
  18424. (nreverse rtn)))
  18425. (defun org-find-base-buffer-visiting (file)
  18426. "Like `find-buffer-visiting' but always return the base buffer and
  18427. not an indirect buffer."
  18428. (let ((buf (or (get-file-buffer file)
  18429. (find-buffer-visiting file))))
  18430. (if buf
  18431. (or (buffer-base-buffer buf) buf)
  18432. nil)))
  18433. (defun org-image-file-name-regexp (&optional extensions)
  18434. "Return regexp matching the file names of images.
  18435. If EXTENSIONS is given, only match these."
  18436. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  18437. (image-file-name-regexp)
  18438. (let ((image-file-name-extensions
  18439. (or extensions
  18440. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  18441. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  18442. (concat "\\."
  18443. (regexp-opt (nconc (mapcar 'upcase
  18444. image-file-name-extensions)
  18445. image-file-name-extensions)
  18446. t)
  18447. "\\'"))))
  18448. (defun org-file-image-p (file &optional extensions)
  18449. "Return non-nil if FILE is an image."
  18450. (save-match-data
  18451. (string-match (org-image-file-name-regexp extensions) file)))
  18452. (defun org-get-cursor-date ()
  18453. "Return the date at cursor in as a time.
  18454. This works in the calendar and in the agenda, anywhere else it just
  18455. returns the current time."
  18456. (let (date day defd)
  18457. (cond
  18458. ((eq major-mode 'calendar-mode)
  18459. (setq date (calendar-cursor-to-date)
  18460. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  18461. ((eq major-mode 'org-agenda-mode)
  18462. (setq day (get-text-property (point) 'day))
  18463. (if day
  18464. (setq date (calendar-gregorian-from-absolute day)
  18465. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  18466. (nth 2 date))))))
  18467. (or defd (current-time))))
  18468. (defvar org-agenda-action-marker (make-marker)
  18469. "Marker pointing to the entry for the next agenda action.")
  18470. (defun org-mark-entry-for-agenda-action ()
  18471. "Mark the current entry as target of an agenda action.
  18472. Agenda actions are actions executed from the agenda with the key `k',
  18473. which make use of the date at the cursor."
  18474. (interactive)
  18475. (move-marker org-agenda-action-marker
  18476. (save-excursion (org-back-to-heading t) (point))
  18477. (current-buffer))
  18478. (message
  18479. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  18480. (defun org-mark-subtree ()
  18481. "Mark the current subtree.
  18482. This puts point at the start of the current subtree, and mark at the end.
  18483. If point is in an inline task, mark that task instead."
  18484. (interactive)
  18485. (let ((inline-task-p
  18486. (and (featurep 'org-inlinetask)
  18487. (org-inlinetask-in-task-p)))
  18488. (beg))
  18489. ;; Get beginning of subtree
  18490. (cond
  18491. (inline-task-p (org-inlinetask-goto-beginning))
  18492. ((org-at-heading-p) (beginning-of-line))
  18493. (t (org-with-limited-levels (outline-previous-visible-heading 1))))
  18494. (setq beg (point))
  18495. ;; Get end of it
  18496. (if inline-task-p
  18497. (org-inlinetask-goto-end)
  18498. (org-end-of-subtree))
  18499. ;; Mark zone
  18500. (push-mark (point) nil t)
  18501. (goto-char beg)))
  18502. ;;; Paragraph filling stuff.
  18503. ;; We want this to be just right, so use the full arsenal.
  18504. (defun org-indent-line-function ()
  18505. "Indent line depending on context."
  18506. (interactive)
  18507. (let* ((pos (point))
  18508. (itemp (org-at-item-p))
  18509. (case-fold-search t)
  18510. (org-drawer-regexp (or org-drawer-regexp "\000"))
  18511. (inline-task-p (and (featurep 'org-inlinetask)
  18512. (org-inlinetask-in-task-p)))
  18513. (inline-re (and inline-task-p
  18514. (org-inlinetask-outline-regexp)))
  18515. column)
  18516. (beginning-of-line 1)
  18517. (cond
  18518. ;; Comments
  18519. ((looking-at "# ") (setq column 0))
  18520. ;; Headings
  18521. ((looking-at org-outline-regexp) (setq column 0))
  18522. ;; Included files
  18523. ((looking-at "#\\+include:") (setq column 0))
  18524. ;; Footnote definition
  18525. ((looking-at org-footnote-definition-re) (setq column 0))
  18526. ;; Literal examples
  18527. ((looking-at "[ \t]*:\\( \\|$\\)")
  18528. (setq column (org-get-indentation))) ; do nothing
  18529. ;; Lists
  18530. ((ignore-errors (goto-char (org-in-item-p)))
  18531. (setq column (if itemp
  18532. (org-get-indentation)
  18533. (org-list-item-body-column (point))))
  18534. (goto-char pos))
  18535. ;; Drawers
  18536. ((and (looking-at "[ \t]*:END:")
  18537. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  18538. (save-excursion
  18539. (goto-char (1- (match-beginning 1)))
  18540. (setq column (current-column))))
  18541. ;; Special blocks
  18542. ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
  18543. (save-excursion
  18544. (re-search-backward
  18545. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  18546. (setq column (org-get-indentation (match-string 0))))
  18547. ((and (not (looking-at "[ \t]*#\\+begin_"))
  18548. (org-between-regexps-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
  18549. (save-excursion
  18550. (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
  18551. (setq column
  18552. (cond ((equal (downcase (match-string 1)) "src")
  18553. ;; src blocks: let `org-edit-src-exit' handle them
  18554. (org-get-indentation))
  18555. ((equal (downcase (match-string 1)) "example")
  18556. (max (org-get-indentation)
  18557. (org-get-indentation (match-string 0))))
  18558. (t
  18559. (org-get-indentation (match-string 0))))))
  18560. ;; This line has nothing special, look at the previous relevant
  18561. ;; line to compute indentation
  18562. (t
  18563. (beginning-of-line 0)
  18564. (while (and (not (bobp))
  18565. (not (looking-at org-drawer-regexp))
  18566. ;; When point started in an inline task, do not move
  18567. ;; above task starting line.
  18568. (not (and inline-task-p (looking-at inline-re)))
  18569. ;; Skip drawers, blocks, empty lines, verbatim,
  18570. ;; comments, tables, footnotes definitions, lists,
  18571. ;; inline tasks.
  18572. (or (and (looking-at "[ \t]*:END:")
  18573. (re-search-backward org-drawer-regexp nil t))
  18574. (and (looking-at "[ \t]*#\\+end_")
  18575. (re-search-backward "[ \t]*#\\+begin_"nil t))
  18576. (looking-at "[ \t]*[\n:#|]")
  18577. (looking-at org-footnote-definition-re)
  18578. (and (ignore-errors (goto-char (org-in-item-p)))
  18579. (goto-char
  18580. (org-list-get-top-point (org-list-struct))))
  18581. (and (not inline-task-p)
  18582. (featurep 'org-inlinetask)
  18583. (org-inlinetask-in-task-p)
  18584. (or (org-inlinetask-goto-beginning) t))))
  18585. (beginning-of-line 0))
  18586. (cond
  18587. ;; There was an heading above.
  18588. ((looking-at "\\*+[ \t]+")
  18589. (if (not org-adapt-indentation)
  18590. (setq column 0)
  18591. (goto-char (match-end 0))
  18592. (setq column (current-column))))
  18593. ;; A drawer had started and is unfinished
  18594. ((looking-at org-drawer-regexp)
  18595. (goto-char (1- (match-beginning 1)))
  18596. (setq column (current-column)))
  18597. ;; Else, nothing noticeable found: get indentation and go on.
  18598. (t (setq column (org-get-indentation))))))
  18599. ;; Now apply indentation and move cursor accordingly
  18600. (goto-char pos)
  18601. (if (<= (current-column) (current-indentation))
  18602. (org-indent-line-to column)
  18603. (save-excursion (org-indent-line-to column)))
  18604. ;; Special polishing for properties, see `org-property-format'
  18605. (setq column (current-column))
  18606. (beginning-of-line 1)
  18607. (if (looking-at
  18608. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  18609. (replace-match (concat (match-string 1)
  18610. (format org-property-format
  18611. (match-string 2) (match-string 3)))
  18612. t t))
  18613. (org-move-to-column column)))
  18614. (defun org-indent-drawer ()
  18615. "Indent the drawer at point."
  18616. (interactive)
  18617. (let ((p (point))
  18618. (e (and (save-excursion (re-search-forward ":END:" nil t))
  18619. (match-end 0)))
  18620. (folded
  18621. (save-excursion
  18622. (end-of-line)
  18623. (when (overlays-at (point))
  18624. (member 'invisible (overlay-properties
  18625. (car (overlays-at (point)))))))))
  18626. (when folded (org-cycle))
  18627. (indent-for-tab-command)
  18628. (while (and (move-beginning-of-line 2) (< (point) e))
  18629. (indent-for-tab-command))
  18630. (goto-char p)
  18631. (when folded (org-cycle)))
  18632. (message "Drawer at point indented"))
  18633. (defun org-indent-block ()
  18634. "Indent the block at point."
  18635. (interactive)
  18636. (let ((p (point))
  18637. (case-fold-search t)
  18638. (e (and (save-excursion (re-search-forward "#\\+end_?\\(?:[a-z]+\\)?" nil t))
  18639. (match-end 0)))
  18640. (folded
  18641. (save-excursion
  18642. (end-of-line)
  18643. (when (overlays-at (point))
  18644. (member 'invisible (overlay-properties
  18645. (car (overlays-at (point)))))))))
  18646. (when folded (org-cycle))
  18647. (indent-for-tab-command)
  18648. (while (and (move-beginning-of-line 2) (< (point) e))
  18649. (indent-for-tab-command))
  18650. (goto-char p)
  18651. (when folded (org-cycle)))
  18652. (message "Block at point indented"))
  18653. (defvar org-adaptive-fill-regexp-backup adaptive-fill-regexp
  18654. "Variable to store copy of `adaptive-fill-regexp'.
  18655. Since `adaptive-fill-regexp' is set to never match, we need to
  18656. store a backup of its value before entering `org-mode' so that
  18657. the functionality can be provided as a fall-back.")
  18658. (defun org-set-autofill-regexps ()
  18659. (interactive)
  18660. ;; In the paragraph separator we include headlines, because filling
  18661. ;; text in a line directly attached to a headline would otherwise
  18662. ;; fill the headline as well.
  18663. (org-set-local 'comment-start-skip "^#+[ \t]*")
  18664. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|#]")
  18665. ;; The paragraph starter includes hand-formatted lists.
  18666. (org-set-local
  18667. 'paragraph-start
  18668. (concat
  18669. "\f" "\\|"
  18670. "[ ]*$" "\\|"
  18671. org-outline-regexp "\\|"
  18672. "[ \t]*#" "\\|"
  18673. (org-item-re) "\\|"
  18674. "[ \t]*[:|]" "\\|"
  18675. "\\$\\$" "\\|"
  18676. "\\\\\\(begin\\|end\\|[][]\\)"))
  18677. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  18678. ;; But only if the user has not turned off tables or fixed-width regions
  18679. (org-set-local
  18680. 'auto-fill-inhibit-regexp
  18681. (concat org-outline-regexp
  18682. "\\|#\\+"
  18683. "\\|[ \t]*" org-keyword-time-regexp
  18684. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  18685. (concat
  18686. "\\|[ \t]*["
  18687. (if org-enable-table-editor "|" "")
  18688. (if org-enable-fixed-width-editor ":" "")
  18689. "]"))))
  18690. ;; We use our own fill-paragraph function, to make sure that tables
  18691. ;; and fixed-width regions are not wrapped. That function will pass
  18692. ;; through to `fill-paragraph' when appropriate.
  18693. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  18694. ;; Prevent auto-fill from inserting unwanted new items.
  18695. (if (boundp 'fill-nobreak-predicate)
  18696. (org-set-local
  18697. 'fill-nobreak-predicate
  18698. (org-uniquify
  18699. (append fill-nobreak-predicate
  18700. '(org-fill-item-nobreak-p org-fill-line-break-nobreak-p)))))
  18701. ;; Adaptive filling: To get full control, first make sure that
  18702. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  18703. (unless (local-variable-p 'adaptive-fill-regexp (current-buffer))
  18704. (org-set-local 'org-adaptive-fill-regexp-backup
  18705. adaptive-fill-regexp))
  18706. (org-set-local 'adaptive-fill-regexp "\000")
  18707. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  18708. (org-set-local 'adaptive-fill-function
  18709. 'org-adaptive-fill-function)
  18710. (org-set-local
  18711. 'align-mode-rules-list
  18712. '((org-in-buffer-settings
  18713. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  18714. (modes . '(org-mode))))))
  18715. (defun org-fill-item-nobreak-p ()
  18716. "Non-nil when a line break at point would insert a new item."
  18717. (and (looking-at (org-item-re)) (org-list-in-valid-context-p)))
  18718. (defun org-fill-line-break-nobreak-p ()
  18719. "Non-nil when a line break at point would create an Org line break."
  18720. (save-excursion
  18721. (skip-chars-backward "[ \t]")
  18722. (skip-chars-backward "\\\\")
  18723. (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
  18724. (defun org-fill-paragraph (&optional justify)
  18725. "Re-align a table, pass through to fill-paragraph if no table."
  18726. (let ((table-p (org-at-table-p))
  18727. (table.el-p (org-at-table.el-p))
  18728. (itemp (org-in-item-p)))
  18729. (cond ((and (equal (char-after (point-at-bol)) ?*)
  18730. (save-excursion (goto-char (point-at-bol))
  18731. (looking-at org-outline-regexp)))
  18732. t) ; skip headlines
  18733. (table.el-p t) ; skip table.el tables
  18734. (table-p (org-table-align) t) ; align Org tables
  18735. (itemp ; align text in items
  18736. (let* ((struct (save-excursion (goto-char itemp)
  18737. (org-list-struct)))
  18738. (parents (org-list-parents-alist struct))
  18739. (children (org-list-get-children itemp struct parents))
  18740. beg end prev next prefix)
  18741. ;; Determine in which part of item point is: before
  18742. ;; first child, after last child, between two
  18743. ;; sub-lists, or simply in item if there's no child.
  18744. (cond
  18745. ((not children)
  18746. (setq prefix (make-string (org-list-item-body-column itemp) ?\ )
  18747. beg itemp
  18748. end (org-list-get-item-end itemp struct)))
  18749. ((< (point) (setq next (car children)))
  18750. (setq prefix (make-string (org-list-item-body-column itemp) ?\ )
  18751. beg itemp
  18752. end next))
  18753. ((> (point) (setq prev (car (last children))))
  18754. (setq beg (org-list-get-item-end prev struct)
  18755. end (org-list-get-item-end itemp struct)
  18756. prefix (save-excursion
  18757. (goto-char beg)
  18758. (skip-chars-forward " \t")
  18759. (make-string (current-column) ?\ ))))
  18760. (t (catch 'exit
  18761. (while (setq next (pop children))
  18762. (if (> (point) next)
  18763. (setq prev next)
  18764. (setq beg (org-list-get-item-end prev struct)
  18765. end next
  18766. prefix (save-excursion
  18767. (goto-char beg)
  18768. (skip-chars-forward " \t")
  18769. (make-string (current-column) ?\ )))
  18770. (throw 'exit nil))))))
  18771. ;; Use `fill-paragraph' with buffer narrowed to item
  18772. ;; without any child, and with our computed PREFIX.
  18773. (flet ((fill-context-prefix (from to &optional flr) prefix))
  18774. (save-restriction
  18775. (narrow-to-region beg end)
  18776. (save-excursion (fill-paragraph justify)))) t))
  18777. ;; Special case where point is not in a list but is on
  18778. ;; a paragraph adjacent to a list: make sure this paragraph
  18779. ;; doesn't get merged with the end of the list by narrowing
  18780. ;; buffer first.
  18781. ((save-excursion (forward-paragraph -1)
  18782. (setq itemp (org-in-item-p)))
  18783. (let ((struct (save-excursion (goto-char itemp)
  18784. (org-list-struct))))
  18785. (save-restriction
  18786. (narrow-to-region (org-list-get-bottom-point struct)
  18787. (save-excursion (forward-paragraph 1)
  18788. (point)))
  18789. (fill-paragraph justify) t)))
  18790. ;; Else simply call `fill-paragraph'.
  18791. (t nil))))
  18792. ;; For reference, this is the default value of adaptive-fill-regexp
  18793. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  18794. (defun org-adaptive-fill-function ()
  18795. "Return a fill prefix for org-mode files."
  18796. (let (itemp)
  18797. (save-excursion
  18798. (cond
  18799. ;; Comment line
  18800. ((looking-at "#[ \t]+")
  18801. (match-string-no-properties 0))
  18802. ;; Plain list item
  18803. ((org-at-item-p)
  18804. (make-string (org-list-item-body-column (point-at-bol)) ?\ ))
  18805. ;; Point is in a list after `backward-paragraph': original
  18806. ;; point wasn't in the list, or filling would have been taken
  18807. ;; care of by `org-auto-fill-function', but the list and the
  18808. ;; real paragraph are not separated by a blank line. Thus, move
  18809. ;; point after the list to go back to real paragraph and
  18810. ;; determine fill-prefix.
  18811. ((setq itemp (org-in-item-p))
  18812. (goto-char itemp)
  18813. (let* ((struct (org-list-struct))
  18814. (bottom (org-list-get-bottom-point struct)))
  18815. (goto-char bottom)
  18816. (make-string (org-get-indentation) ?\ )))
  18817. ;; Other text
  18818. ((looking-at org-adaptive-fill-regexp-backup)
  18819. (match-string-no-properties 0))))))
  18820. (defun org-auto-fill-function ()
  18821. "Auto-fill function."
  18822. (let (itemp prefix)
  18823. ;; When in a list, compute an appropriate fill-prefix and make
  18824. ;; sure it will be used by `do-auto-fill'.
  18825. (if (setq itemp (org-in-item-p))
  18826. (progn
  18827. (setq prefix (make-string (org-list-item-body-column itemp) ?\ ))
  18828. (flet ((fill-context-prefix (from to &optional flr) prefix))
  18829. (do-auto-fill)))
  18830. ;; Else just use `do-auto-fill'.
  18831. (do-auto-fill))))
  18832. ;;; Other stuff.
  18833. (defun org-toggle-fixed-width-section (arg)
  18834. "Toggle the fixed-width export.
  18835. If there is no active region, the QUOTE keyword at the current headline is
  18836. inserted or removed. When present, it causes the text between this headline
  18837. and the next to be exported as fixed-width text, and unmodified.
  18838. If there is an active region, this command adds or removes a colon as the
  18839. first character of this line. If the first character of a line is a colon,
  18840. this line is also exported in fixed-width font."
  18841. (interactive "P")
  18842. (let* ((cc 0)
  18843. (regionp (org-region-active-p))
  18844. (beg (if regionp (region-beginning) (point)))
  18845. (end (if regionp (region-end)))
  18846. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  18847. (case-fold-search nil)
  18848. (re "[ \t]*\\(:\\(?: \\|$\\)\\)")
  18849. off)
  18850. (if regionp
  18851. (save-excursion
  18852. (goto-char beg)
  18853. (setq cc (current-column))
  18854. (beginning-of-line 1)
  18855. (setq off (looking-at re))
  18856. (while (> nlines 0)
  18857. (setq nlines (1- nlines))
  18858. (beginning-of-line 1)
  18859. (cond
  18860. (arg
  18861. (org-move-to-column cc t)
  18862. (insert ": \n")
  18863. (forward-line -1))
  18864. ((and off (looking-at re))
  18865. (replace-match "" t t nil 1))
  18866. ((not off) (org-move-to-column cc t) (insert ": ")))
  18867. (forward-line 1)))
  18868. (save-excursion
  18869. (org-back-to-heading)
  18870. (cond
  18871. ((looking-at (format org-heading-keyword-regexp-format
  18872. org-quote-string))
  18873. (goto-char (match-end 1))
  18874. (looking-at (concat " +" org-quote-string))
  18875. (replace-match "" t t)
  18876. (when (eolp) (insert " ")))
  18877. ((looking-at org-outline-regexp)
  18878. (goto-char (match-end 0))
  18879. (insert org-quote-string " ")))))))
  18880. (defun org-reftex-citation ()
  18881. "Use reftex-citation to insert a citation into the buffer.
  18882. This looks for a line like
  18883. #+BIBLIOGRAPHY: foo plain option:-d
  18884. and derives from it that foo.bib is the bibliography file relevant
  18885. for this document. It then installs the necessary environment for RefTeX
  18886. to work in this buffer and calls `reftex-citation' to insert a citation
  18887. into the buffer.
  18888. Export of such citations to both LaTeX and HTML is handled by the contributed
  18889. package org-exp-bibtex by Taru Karttunen."
  18890. (interactive)
  18891. (let ((reftex-docstruct-symbol 'rds)
  18892. (reftex-cite-format "\\cite{%l}")
  18893. rds bib)
  18894. (save-excursion
  18895. (save-restriction
  18896. (widen)
  18897. (let ((case-fold-search t)
  18898. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  18899. (if (not (save-excursion
  18900. (or (re-search-forward re nil t)
  18901. (re-search-backward re nil t))))
  18902. (error "No bibliography defined in file")
  18903. (setq bib (concat (match-string 1) ".bib")
  18904. rds (list (list 'bib bib)))))))
  18905. (call-interactively 'reftex-citation)))
  18906. ;;;; Functions extending outline functionality
  18907. (defun org-beginning-of-line (&optional arg)
  18908. "Go to the beginning of the current line. If that is invisible, continue
  18909. to a visible line beginning. This makes the function of C-a more intuitive.
  18910. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  18911. first attempt, and only move to after the tags when the cursor is already
  18912. beyond the end of the headline."
  18913. (interactive "P")
  18914. (let ((pos (point))
  18915. (special (if (consp org-special-ctrl-a/e)
  18916. (car org-special-ctrl-a/e)
  18917. org-special-ctrl-a/e))
  18918. refpos)
  18919. (if (org-bound-and-true-p line-move-visual)
  18920. (beginning-of-visual-line 1)
  18921. (beginning-of-line 1))
  18922. (if (and arg (fboundp 'move-beginning-of-line))
  18923. (call-interactively 'move-beginning-of-line)
  18924. (if (bobp)
  18925. nil
  18926. (backward-char 1)
  18927. (if (org-truely-invisible-p)
  18928. (while (and (not (bobp)) (org-truely-invisible-p))
  18929. (backward-char 1)
  18930. (beginning-of-line 1))
  18931. (forward-char 1))))
  18932. (when special
  18933. (cond
  18934. ((and (looking-at org-complex-heading-regexp)
  18935. (= (char-after (match-end 1)) ?\ ))
  18936. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  18937. (point-at-eol)))
  18938. (goto-char
  18939. (if (eq special t)
  18940. (cond ((> pos refpos) refpos)
  18941. ((= pos (point)) refpos)
  18942. (t (point)))
  18943. (cond ((> pos (point)) (point))
  18944. ((not (eq last-command this-command)) (point))
  18945. (t refpos)))))
  18946. ((org-at-item-p)
  18947. ;; Being at an item and not looking at an the item means point
  18948. ;; was previously moved to beginning of a visual line, whiche
  18949. ;; doesn't contain the item. Therefore, do nothing special,
  18950. ;; just stay here.
  18951. (when (looking-at org-list-full-item-re)
  18952. ;; Set special position at first white space character after
  18953. ;; bullet, and check-box, if any.
  18954. (let ((after-bullet
  18955. (let ((box (match-end 3)))
  18956. (if (not box) (match-end 1)
  18957. (let ((after (char-after box)))
  18958. (if (and after (= after ? )) (1+ box) box))))))
  18959. ;; Special case: Move point to special position when
  18960. ;; currently after it or at beginning of line.
  18961. (if (eq special t)
  18962. (when (or (> pos after-bullet) (= (point) pos))
  18963. (goto-char after-bullet))
  18964. ;; Reversed case: Move point to special position when
  18965. ;; point was already at beginning of line and command is
  18966. ;; repeated.
  18967. (when (and (= (point) pos) (eq last-command this-command))
  18968. (goto-char after-bullet))))))))
  18969. (org-no-warnings
  18970. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  18971. (defun org-end-of-line (&optional arg)
  18972. "Go to the end of the line.
  18973. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  18974. first attempt, and only move to after the tags when the cursor is already
  18975. beyond the end of the headline."
  18976. (interactive "P")
  18977. (let ((special (if (consp org-special-ctrl-a/e)
  18978. (cdr org-special-ctrl-a/e)
  18979. org-special-ctrl-a/e)))
  18980. (cond
  18981. ((or (not special) arg
  18982. (not (or (org-at-heading-p) (org-at-item-p) (org-at-drawer-p))))
  18983. (call-interactively
  18984. (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
  18985. ((fboundp 'move-end-of-line) 'move-end-of-line)
  18986. (t 'end-of-line))))
  18987. ((org-at-heading-p)
  18988. (let ((pos (point)))
  18989. (beginning-of-line 1)
  18990. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  18991. (if (eq special t)
  18992. (if (or (< pos (match-beginning 1))
  18993. (= pos (match-end 0)))
  18994. (goto-char (match-beginning 1))
  18995. (goto-char (match-end 0)))
  18996. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  18997. (goto-char (match-end 0))
  18998. (goto-char (match-beginning 1))))
  18999. (call-interactively (if (fboundp 'move-end-of-line)
  19000. 'move-end-of-line
  19001. 'end-of-line)))))
  19002. ((org-at-drawer-p)
  19003. (move-end-of-line 1)
  19004. (when (overlays-at (1- (point))) (backward-char 1)))
  19005. ;; At an item: Move before any hidden text.
  19006. (t (call-interactively
  19007. (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
  19008. ((fboundp 'move-end-of-line) 'move-end-of-line)
  19009. (t 'end-of-line)))))
  19010. (org-no-warnings
  19011. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  19012. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  19013. (define-key org-mode-map "\C-e" 'org-end-of-line)
  19014. (defun org-backward-sentence (&optional arg)
  19015. "Go to beginning of sentence, or beginning of table field.
  19016. This will call `backward-sentence' or `org-table-beginning-of-field',
  19017. depending on context."
  19018. (interactive "P")
  19019. (cond
  19020. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  19021. (t (call-interactively 'backward-sentence))))
  19022. (defun org-forward-sentence (&optional arg)
  19023. "Go to end of sentence, or end of table field.
  19024. This will call `forward-sentence' or `org-table-end-of-field',
  19025. depending on context."
  19026. (interactive "P")
  19027. (cond
  19028. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  19029. (t (call-interactively 'forward-sentence))))
  19030. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  19031. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  19032. (defun org-kill-line (&optional arg)
  19033. "Kill line, to tags or end of line."
  19034. (interactive "P")
  19035. (cond
  19036. ((or (not org-special-ctrl-k)
  19037. (bolp)
  19038. (not (org-at-heading-p)))
  19039. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  19040. org-ctrl-k-protect-subtree)
  19041. (if (or (eq org-ctrl-k-protect-subtree 'error)
  19042. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  19043. (error "C-k aborted - would kill hidden subtree")))
  19044. (call-interactively
  19045. (if visual-line-mode 'kill-visual-line 'kill-line)))
  19046. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  19047. (kill-region (point) (match-beginning 1))
  19048. (org-set-tags nil t))
  19049. (t (kill-region (point) (point-at-eol)))))
  19050. (define-key org-mode-map "\C-k" 'org-kill-line)
  19051. (defun org-yank (&optional arg)
  19052. "Yank. If the kill is a subtree, treat it specially.
  19053. This command will look at the current kill and check if is a single
  19054. subtree, or a series of subtrees[1]. If it passes the test, and if the
  19055. cursor is at the beginning of a line or after the stars of a currently
  19056. empty headline, then the yank is handled specially. How exactly depends
  19057. on the value of the following variables, both set by default.
  19058. org-yank-folded-subtrees
  19059. When set, the subtree(s) will be folded after insertion, but only
  19060. if doing so would now swallow text after the yanked text.
  19061. org-yank-adjusted-subtrees
  19062. When set, the subtree will be promoted or demoted in order to
  19063. fit into the local outline tree structure, which means that the level
  19064. will be adjusted so that it becomes the smaller one of the two
  19065. *visible* surrounding headings.
  19066. Any prefix to this command will cause `yank' to be called directly with
  19067. no special treatment. In particular, a simple \\[universal-argument] prefix \
  19068. will just
  19069. plainly yank the text as it is.
  19070. \[1] The test checks if the first non-white line is a heading
  19071. and if there are no other headings with fewer stars."
  19072. (interactive "P")
  19073. (org-yank-generic 'yank arg))
  19074. (defun org-yank-generic (command arg)
  19075. "Perform some yank-like command.
  19076. This function implements the behavior described in the `org-yank'
  19077. documentation. However, it has been generalized to work for any
  19078. interactive command with similar behavior."
  19079. ;; pretend to be command COMMAND
  19080. (setq this-command command)
  19081. (if arg
  19082. (call-interactively command)
  19083. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  19084. (and (org-kill-is-subtree-p)
  19085. (or (bolp)
  19086. (and (looking-at "[ \t]*$")
  19087. (string-match
  19088. "\\`\\*+\\'"
  19089. (buffer-substring (point-at-bol) (point)))))))
  19090. swallowp)
  19091. (cond
  19092. ((and subtreep org-yank-folded-subtrees)
  19093. (let ((beg (point))
  19094. end)
  19095. (if (and subtreep org-yank-adjusted-subtrees)
  19096. (org-paste-subtree nil nil 'for-yank)
  19097. (call-interactively command))
  19098. (setq end (point))
  19099. (goto-char beg)
  19100. (when (and (bolp) subtreep
  19101. (not (setq swallowp
  19102. (org-yank-folding-would-swallow-text beg end))))
  19103. (org-with-limited-levels
  19104. (or (looking-at org-outline-regexp)
  19105. (re-search-forward org-outline-regexp-bol end t))
  19106. (while (and (< (point) end) (looking-at org-outline-regexp))
  19107. (hide-subtree)
  19108. (org-cycle-show-empty-lines 'folded)
  19109. (condition-case nil
  19110. (outline-forward-same-level 1)
  19111. (error (goto-char end))))))
  19112. (when swallowp
  19113. (message
  19114. "Inserted text not folded because that would swallow text"))
  19115. (goto-char end)
  19116. (skip-chars-forward " \t\n\r")
  19117. (beginning-of-line 1)
  19118. (push-mark beg 'nomsg)))
  19119. ((and subtreep org-yank-adjusted-subtrees)
  19120. (let ((beg (point-at-bol)))
  19121. (org-paste-subtree nil nil 'for-yank)
  19122. (push-mark beg 'nomsg)))
  19123. (t
  19124. (call-interactively command))))))
  19125. (defun org-yank-folding-would-swallow-text (beg end)
  19126. "Would hide-subtree at BEG swallow any text after END?"
  19127. (let (level)
  19128. (org-with-limited-levels
  19129. (save-excursion
  19130. (goto-char beg)
  19131. (when (or (looking-at org-outline-regexp)
  19132. (re-search-forward org-outline-regexp-bol end t))
  19133. (setq level (org-outline-level)))
  19134. (goto-char end)
  19135. (skip-chars-forward " \t\r\n\v\f")
  19136. (if (or (eobp)
  19137. (and (bolp) (looking-at org-outline-regexp)
  19138. (<= (org-outline-level) level)))
  19139. nil ; Nothing would be swallowed
  19140. t))))) ; something would swallow
  19141. (define-key org-mode-map "\C-y" 'org-yank)
  19142. (defun org-truely-invisible-p ()
  19143. "Check if point is at a character currently not visible.
  19144. This version does not only check the character property, but also
  19145. `visible-mode'."
  19146. ;; Early versions of noutline don't have `outline-invisible-p'.
  19147. (if (org-bound-and-true-p visible-mode)
  19148. nil
  19149. (outline-invisible-p)))
  19150. (defun org-invisible-p2 ()
  19151. "Check if point is at a character currently not visible."
  19152. (save-excursion
  19153. (if (and (eolp) (not (bobp))) (backward-char 1))
  19154. ;; Early versions of noutline don't have `outline-invisible-p'.
  19155. (outline-invisible-p)))
  19156. (defun org-back-to-heading (&optional invisible-ok)
  19157. "Call `outline-back-to-heading', but provide a better error message."
  19158. (condition-case nil
  19159. (outline-back-to-heading invisible-ok)
  19160. (error (error "Before first headline at position %d in buffer %s"
  19161. (point) (current-buffer)))))
  19162. (defun org-beginning-of-defun ()
  19163. "Go to the beginning of the subtree, i.e. back to the heading."
  19164. (org-back-to-heading))
  19165. (defun org-end-of-defun ()
  19166. "Go to the end of the subtree."
  19167. (org-end-of-subtree nil t))
  19168. (defun org-before-first-heading-p ()
  19169. "Before first heading?"
  19170. (save-excursion
  19171. (end-of-line)
  19172. (null (re-search-backward org-outline-regexp-bol nil t))))
  19173. (defun org-at-heading-p (&optional ignored)
  19174. (outline-on-heading-p t))
  19175. ;; Compatibility alias with Org versions < 7.8.03
  19176. (defalias 'org-on-heading-p 'org-at-heading-p)
  19177. (defun org-at-drawer-p nil
  19178. "Is cursor at a drawer keyword?"
  19179. (save-excursion
  19180. (move-beginning-of-line 1)
  19181. (looking-at org-drawer-regexp)))
  19182. (defun org-at-block-p nil
  19183. "Is cursor at a block keyword?"
  19184. (save-excursion
  19185. (move-beginning-of-line 1)
  19186. (looking-at org-block-regexp)))
  19187. (defun org-point-at-end-of-empty-headline ()
  19188. "If point is at the end of an empty headline, return t, else nil.
  19189. If the heading only contains a TODO keyword, it is still still considered
  19190. empty."
  19191. (and (looking-at "[ \t]*$")
  19192. (save-excursion
  19193. (beginning-of-line 1)
  19194. (let ((case-fold-search nil))
  19195. (looking-at org-todo-line-regexp)))
  19196. (string= (match-string 3) "")))
  19197. (defun org-at-heading-or-item-p ()
  19198. (or (org-at-heading-p) (org-at-item-p)))
  19199. (defun org-at-target-p ()
  19200. (or (org-in-regexp org-radio-target-regexp)
  19201. (org-in-regexp org-target-regexp)))
  19202. ;; Compatibility alias with Org versions < 7.8.03
  19203. (defalias 'org-on-target-p 'org-at-target-p)
  19204. (defun org-up-heading-all (arg)
  19205. "Move to the heading line of which the present line is a subheading.
  19206. This function considers both visible and invisible heading lines.
  19207. With argument, move up ARG levels."
  19208. (if (fboundp 'outline-up-heading-all)
  19209. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  19210. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  19211. (defun org-up-heading-safe ()
  19212. "Move to the heading line of which the present line is a subheading.
  19213. This version will not throw an error. It will return the level of the
  19214. headline found, or nil if no higher level is found.
  19215. Also, this function will be a lot faster than `outline-up-heading',
  19216. because it relies on stars being the outline starters. This can really
  19217. make a significant difference in outlines with very many siblings."
  19218. (let (start-level re)
  19219. (org-back-to-heading t)
  19220. (setq start-level (funcall outline-level))
  19221. (if (equal start-level 1)
  19222. nil
  19223. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  19224. (if (re-search-backward re nil t)
  19225. (funcall outline-level)))))
  19226. (defun org-first-sibling-p ()
  19227. "Is this heading the first child of its parents?"
  19228. (interactive)
  19229. (let ((re org-outline-regexp-bol)
  19230. level l)
  19231. (unless (org-at-heading-p t)
  19232. (error "Not at a heading"))
  19233. (setq level (funcall outline-level))
  19234. (save-excursion
  19235. (if (not (re-search-backward re nil t))
  19236. t
  19237. (setq l (funcall outline-level))
  19238. (< l level)))))
  19239. (defun org-goto-sibling (&optional previous)
  19240. "Goto the next sibling, even if it is invisible.
  19241. When PREVIOUS is set, go to the previous sibling instead. Returns t
  19242. when a sibling was found. When none is found, return nil and don't
  19243. move point."
  19244. (let ((fun (if previous 're-search-backward 're-search-forward))
  19245. (pos (point))
  19246. (re org-outline-regexp-bol)
  19247. level l)
  19248. (when (condition-case nil (org-back-to-heading t) (error nil))
  19249. (setq level (funcall outline-level))
  19250. (catch 'exit
  19251. (or previous (forward-char 1))
  19252. (while (funcall fun re nil t)
  19253. (setq l (funcall outline-level))
  19254. (when (< l level) (goto-char pos) (throw 'exit nil))
  19255. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  19256. (goto-char pos)
  19257. nil))))
  19258. (defun org-show-siblings ()
  19259. "Show all siblings of the current headline."
  19260. (save-excursion
  19261. (while (org-goto-sibling) (org-flag-heading nil)))
  19262. (save-excursion
  19263. (while (org-goto-sibling 'previous)
  19264. (org-flag-heading nil))))
  19265. (defun org-goto-first-child ()
  19266. "Goto the first child, even if it is invisible.
  19267. Return t when a child was found. Otherwise don't move point and
  19268. return nil."
  19269. (let (level (pos (point)) (re org-outline-regexp-bol))
  19270. (when (condition-case nil (org-back-to-heading t) (error nil))
  19271. (setq level (outline-level))
  19272. (forward-char 1)
  19273. (if (and (re-search-forward re nil t) (> (outline-level) level))
  19274. (progn (goto-char (match-beginning 0)) t)
  19275. (goto-char pos) nil))))
  19276. (defun org-show-hidden-entry ()
  19277. "Show an entry where even the heading is hidden."
  19278. (save-excursion
  19279. (org-show-entry)))
  19280. (defun org-flag-heading (flag &optional entry)
  19281. "Flag the current heading. FLAG non-nil means make invisible.
  19282. When ENTRY is non-nil, show the entire entry."
  19283. (save-excursion
  19284. (org-back-to-heading t)
  19285. ;; Check if we should show the entire entry
  19286. (if entry
  19287. (progn
  19288. (org-show-entry)
  19289. (save-excursion
  19290. (and (outline-next-heading)
  19291. (org-flag-heading nil))))
  19292. (outline-flag-region (max (point-min) (1- (point)))
  19293. (save-excursion (outline-end-of-heading) (point))
  19294. flag))))
  19295. (defun org-get-next-sibling ()
  19296. "Move to next heading of the same level, and return point.
  19297. If there is no such heading, return nil.
  19298. This is like outline-next-sibling, but invisible headings are ok."
  19299. (let ((level (funcall outline-level)))
  19300. (outline-next-heading)
  19301. (while (and (not (eobp)) (> (funcall outline-level) level))
  19302. (outline-next-heading))
  19303. (if (or (eobp) (< (funcall outline-level) level))
  19304. nil
  19305. (point))))
  19306. (defun org-get-last-sibling ()
  19307. "Move to previous heading of the same level, and return point.
  19308. If there is no such heading, return nil."
  19309. (let ((opoint (point))
  19310. (level (funcall outline-level)))
  19311. (outline-previous-heading)
  19312. (when (and (/= (point) opoint) (outline-on-heading-p t))
  19313. (while (and (> (funcall outline-level) level)
  19314. (not (bobp)))
  19315. (outline-previous-heading))
  19316. (if (< (funcall outline-level) level)
  19317. nil
  19318. (point)))))
  19319. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  19320. ;; This contains an exact copy of the original function, but it uses
  19321. ;; `org-back-to-heading', to make it work also in invisible
  19322. ;; trees. And is uses an invisible-OK argument.
  19323. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  19324. ;; Furthermore, when used inside Org, finding the end of a large subtree
  19325. ;; with many children and grandchildren etc, this can be much faster
  19326. ;; than the outline version.
  19327. (org-back-to-heading invisible-OK)
  19328. (let ((first t)
  19329. (level (funcall outline-level)))
  19330. (if (and (eq major-mode 'org-mode) (< level 1000))
  19331. ;; A true heading (not a plain list item), in Org-mode
  19332. ;; This means we can easily find the end by looking
  19333. ;; only for the right number of stars. Using a regexp to do
  19334. ;; this is so much faster than using a Lisp loop.
  19335. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  19336. (forward-char 1)
  19337. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  19338. ;; something else, do it the slow way
  19339. (while (and (not (eobp))
  19340. (or first (> (funcall outline-level) level)))
  19341. (setq first nil)
  19342. (outline-next-heading)))
  19343. (unless to-heading
  19344. (if (memq (preceding-char) '(?\n ?\^M))
  19345. (progn
  19346. ;; Go to end of line before heading
  19347. (forward-char -1)
  19348. (if (memq (preceding-char) '(?\n ?\^M))
  19349. ;; leave blank line before heading
  19350. (forward-char -1))))))
  19351. (point))
  19352. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  19353. "Use Org version in org-mode, for dramatic speed-up."
  19354. (if (eq major-mode 'org-mode)
  19355. (progn
  19356. (org-end-of-subtree nil t)
  19357. (unless (eobp) (backward-char 1)))
  19358. ad-do-it))
  19359. (defun org-end-of-meta-data-and-drawers ()
  19360. "Jump to the first text after meta data and drawers in the current entry.
  19361. This will move over empty lines, lines with planning time stamps,
  19362. clocking lines, and drawers."
  19363. (org-back-to-heading t)
  19364. (let ((end (save-excursion (outline-next-heading) (point)))
  19365. (re (concat "\\(" org-drawer-regexp "\\)"
  19366. "\\|" "[ \t]*" org-keyword-time-regexp)))
  19367. (forward-line 1)
  19368. (while (re-search-forward re end t)
  19369. (if (not (match-end 1))
  19370. ;; empty or planning line
  19371. (forward-line 1)
  19372. ;; a drawer, find the end
  19373. (re-search-forward "^[ \t]*:END:" end 'move)
  19374. (forward-line 1)))
  19375. (and (re-search-forward "[^\n]" nil t) (backward-char 1))
  19376. (point)))
  19377. (defun org-forward-same-level (arg &optional invisible-ok)
  19378. "Move forward to the arg'th subheading at same level as this one.
  19379. Stop at the first and last subheadings of a superior heading.
  19380. Normally this only looks at visible headings, but when INVISIBLE-OK is non-nil
  19381. it wil also look at invisible ones."
  19382. (interactive "p")
  19383. (org-back-to-heading invisible-ok)
  19384. (org-at-heading-p)
  19385. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  19386. (re (format "^\\*\\{1,%d\\} " level))
  19387. l)
  19388. (forward-char 1)
  19389. (while (> arg 0)
  19390. (while (and (re-search-forward re nil 'move)
  19391. (setq l (- (match-end 0) (match-beginning 0) 1))
  19392. (= l level)
  19393. (not invisible-ok)
  19394. (progn (backward-char 1) (outline-invisible-p)))
  19395. (if (< l level) (setq arg 1)))
  19396. (setq arg (1- arg)))
  19397. (beginning-of-line 1)))
  19398. (defun org-backward-same-level (arg &optional invisible-ok)
  19399. "Move backward to the arg'th subheading at same level as this one.
  19400. Stop at the first and last subheadings of a superior heading."
  19401. (interactive "p")
  19402. (org-back-to-heading)
  19403. (org-at-heading-p)
  19404. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  19405. (re (format "^\\*\\{1,%d\\} " level))
  19406. l)
  19407. (while (> arg 0)
  19408. (while (and (re-search-backward re nil 'move)
  19409. (setq l (- (match-end 0) (match-beginning 0) 1))
  19410. (= l level)
  19411. (not invisible-ok)
  19412. (outline-invisible-p))
  19413. (if (< l level) (setq arg 1)))
  19414. (setq arg (1- arg)))))
  19415. (defun org-show-subtree ()
  19416. "Show everything after this heading at deeper levels."
  19417. (outline-flag-region
  19418. (point)
  19419. (save-excursion
  19420. (org-end-of-subtree t t))
  19421. nil))
  19422. (defun org-show-entry ()
  19423. "Show the body directly following this heading.
  19424. Show the heading too, if it is currently invisible."
  19425. (interactive)
  19426. (save-excursion
  19427. (condition-case nil
  19428. (progn
  19429. (org-back-to-heading t)
  19430. (outline-flag-region
  19431. (max (point-min) (1- (point)))
  19432. (save-excursion
  19433. (if (re-search-forward
  19434. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  19435. (match-beginning 1)
  19436. (point-max)))
  19437. nil)
  19438. (org-cycle-hide-drawers 'children))
  19439. (error nil))))
  19440. (defun org-make-options-regexp (kwds &optional extra)
  19441. "Make a regular expression for keyword lines."
  19442. (concat
  19443. "^"
  19444. "#?[ \t]*\\+\\("
  19445. (mapconcat 'regexp-quote kwds "\\|")
  19446. (if extra (concat "\\|" extra))
  19447. "\\):[ \t]*"
  19448. "\\(.*\\)"))
  19449. ;; Make isearch reveal the necessary context
  19450. (defun org-isearch-end ()
  19451. "Reveal context after isearch exits."
  19452. (when isearch-success ; only if search was successful
  19453. (if (featurep 'xemacs)
  19454. ;; Under XEmacs, the hook is run in the correct place,
  19455. ;; we directly show the context.
  19456. (org-show-context 'isearch)
  19457. ;; In Emacs the hook runs *before* restoring the overlays.
  19458. ;; So we have to use a one-time post-command-hook to do this.
  19459. ;; (Emacs 22 has a special variable, see function `org-mode')
  19460. (unless (and (boundp 'isearch-mode-end-hook-quit)
  19461. isearch-mode-end-hook-quit)
  19462. ;; Only when the isearch was not quitted.
  19463. (org-add-hook 'post-command-hook 'org-isearch-post-command
  19464. 'append 'local)))))
  19465. (defun org-isearch-post-command ()
  19466. "Remove self from hook, and show context."
  19467. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  19468. (org-show-context 'isearch))
  19469. ;;;; Integration with and fixes for other packages
  19470. ;;; Imenu support
  19471. (defvar org-imenu-markers nil
  19472. "All markers currently used by Imenu.")
  19473. (make-variable-buffer-local 'org-imenu-markers)
  19474. (defun org-imenu-new-marker (&optional pos)
  19475. "Return a new marker for use by Imenu, and remember the marker."
  19476. (let ((m (make-marker)))
  19477. (move-marker m (or pos (point)))
  19478. (push m org-imenu-markers)
  19479. m))
  19480. (defun org-imenu-get-tree ()
  19481. "Produce the index for Imenu."
  19482. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  19483. (setq org-imenu-markers nil)
  19484. (let* ((n org-imenu-depth)
  19485. (re (concat "^" (org-get-limited-outline-regexp)))
  19486. (subs (make-vector (1+ n) nil))
  19487. (last-level 0)
  19488. m level head)
  19489. (save-excursion
  19490. (save-restriction
  19491. (widen)
  19492. (goto-char (point-max))
  19493. (while (re-search-backward re nil t)
  19494. (setq level (org-reduced-level (funcall outline-level)))
  19495. (when (<= level n)
  19496. (looking-at org-complex-heading-regexp)
  19497. (setq head (org-link-display-format
  19498. (org-match-string-no-properties 4))
  19499. m (org-imenu-new-marker))
  19500. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  19501. (if (>= level last-level)
  19502. (push (cons head m) (aref subs level))
  19503. (push (cons head (aref subs (1+ level))) (aref subs level))
  19504. (loop for i from (1+ level) to n do (aset subs i nil)))
  19505. (setq last-level level)))))
  19506. (aref subs 1)))
  19507. (eval-after-load "imenu"
  19508. '(progn
  19509. (add-hook 'imenu-after-jump-hook
  19510. (lambda ()
  19511. (if (eq major-mode 'org-mode)
  19512. (org-show-context 'org-goto))))))
  19513. (defun org-link-display-format (link)
  19514. "Replace a link with either the description, or the link target
  19515. if no description is present"
  19516. (save-match-data
  19517. (if (string-match org-bracket-link-analytic-regexp link)
  19518. (replace-match (if (match-end 5)
  19519. (match-string 5 link)
  19520. (concat (match-string 1 link)
  19521. (match-string 3 link)))
  19522. nil t link)
  19523. link)))
  19524. (defun org-toggle-link-display ()
  19525. "Toggle the literal or descriptive display of links."
  19526. (interactive)
  19527. (if org-descriptive-links
  19528. (progn (org-remove-from-invisibility-spec '(org-link))
  19529. (org-restart-font-lock)
  19530. (setq org-descriptive-links nil))
  19531. (progn (add-to-invisibility-spec '(org-link))
  19532. (org-restart-font-lock)
  19533. (setq org-descriptive-links t))))
  19534. ;; Speedbar support
  19535. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  19536. "Overlay marking the agenda restriction line in speedbar.")
  19537. (overlay-put org-speedbar-restriction-lock-overlay
  19538. 'face 'org-agenda-restriction-lock)
  19539. (overlay-put org-speedbar-restriction-lock-overlay
  19540. 'help-echo "Agendas are currently limited to this item.")
  19541. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  19542. (defun org-speedbar-set-agenda-restriction ()
  19543. "Restrict future agenda commands to the location at point in speedbar.
  19544. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  19545. (interactive)
  19546. (require 'org-agenda)
  19547. (let (p m tp np dir txt)
  19548. (cond
  19549. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  19550. 'org-imenu t))
  19551. (setq m (get-text-property p 'org-imenu-marker))
  19552. (with-current-buffer (marker-buffer m)
  19553. (goto-char m)
  19554. (org-agenda-set-restriction-lock 'subtree)))
  19555. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  19556. 'speedbar-function 'speedbar-find-file))
  19557. (setq tp (previous-single-property-change
  19558. (1+ p) 'speedbar-function)
  19559. np (next-single-property-change
  19560. tp 'speedbar-function)
  19561. dir (speedbar-line-directory)
  19562. txt (buffer-substring-no-properties (or tp (point-min))
  19563. (or np (point-max))))
  19564. (with-current-buffer (find-file-noselect
  19565. (let ((default-directory dir))
  19566. (expand-file-name txt)))
  19567. (unless (eq major-mode 'org-mode)
  19568. (error "Cannot restrict to non-Org-mode file"))
  19569. (org-agenda-set-restriction-lock 'file)))
  19570. (t (error "Don't know how to restrict Org-mode's agenda")))
  19571. (move-overlay org-speedbar-restriction-lock-overlay
  19572. (point-at-bol) (point-at-eol))
  19573. (setq current-prefix-arg nil)
  19574. (org-agenda-maybe-redo)))
  19575. (eval-after-load "speedbar"
  19576. '(progn
  19577. (speedbar-add-supported-extension ".org")
  19578. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  19579. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  19580. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  19581. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  19582. (add-hook 'speedbar-visiting-tag-hook
  19583. (lambda () (and (eq major-mode 'org-mode) (org-show-context 'org-goto))))))
  19584. ;;; Fixes and Hacks for problems with other packages
  19585. ;; Make flyspell not check words in links, to not mess up our keymap
  19586. (defun org-mode-flyspell-verify ()
  19587. "Don't let flyspell put overlays at active buttons, or on
  19588. {todo,all-time,additional-option-like}-keywords."
  19589. (let ((pos (max (1- (point)) (point-min)))
  19590. (word (thing-at-point 'word)))
  19591. (and (not (get-text-property pos 'keymap))
  19592. (not (get-text-property pos 'org-no-flyspell))
  19593. (not (member word org-todo-keywords-1))
  19594. (not (member word org-all-time-keywords))
  19595. (not (member word org-additional-option-like-keywords)))))
  19596. (defun org-remove-flyspell-overlays-in (beg end)
  19597. "Remove flyspell overlays in region."
  19598. (and (org-bound-and-true-p flyspell-mode)
  19599. (fboundp 'flyspell-delete-region-overlays)
  19600. (flyspell-delete-region-overlays beg end))
  19601. (add-text-properties beg end '(org-no-flyspell t)))
  19602. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  19603. (eval-after-load "bookmark"
  19604. '(if (boundp 'bookmark-after-jump-hook)
  19605. ;; We can use the hook
  19606. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  19607. ;; Hook not available, use advice
  19608. (defadvice bookmark-jump (after org-make-visible activate)
  19609. "Make the position visible."
  19610. (org-bookmark-jump-unhide))))
  19611. ;; Make sure saveplace shows the location if it was hidden
  19612. (eval-after-load "saveplace"
  19613. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  19614. "Make the position visible."
  19615. (org-bookmark-jump-unhide)))
  19616. ;; Make sure ecb shows the location if it was hidden
  19617. (eval-after-load "ecb"
  19618. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  19619. "Make hierarchy visible when jumping into location from ECB tree buffer."
  19620. (if (eq major-mode 'org-mode)
  19621. (org-show-context))))
  19622. (defun org-bookmark-jump-unhide ()
  19623. "Unhide the current position, to show the bookmark location."
  19624. (and (eq major-mode 'org-mode)
  19625. (or (outline-invisible-p)
  19626. (save-excursion (goto-char (max (point-min) (1- (point))))
  19627. (outline-invisible-p)))
  19628. (org-show-context 'bookmark-jump)))
  19629. ;; Make session.el ignore our circular variable
  19630. (eval-after-load "session"
  19631. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  19632. ;;;; Experimental code
  19633. (defun org-closed-in-range ()
  19634. "Sparse tree of items closed in a certain time range.
  19635. Still experimental, may disappear in the future."
  19636. (interactive)
  19637. ;; Get the time interval from the user.
  19638. (let* ((time1 (org-float-time
  19639. (org-read-date nil 'to-time nil "Starting date: ")))
  19640. (time2 (org-float-time
  19641. (org-read-date nil 'to-time nil "End date:")))
  19642. ;; callback function
  19643. (callback (lambda ()
  19644. (let ((time
  19645. (org-float-time
  19646. (apply 'encode-time
  19647. (org-parse-time-string
  19648. (match-string 1))))))
  19649. ;; check if time in interval
  19650. (and (>= time time1) (<= time time2))))))
  19651. ;; make tree, check each match with the callback
  19652. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  19653. ;;;; Finish up
  19654. (provide 'org)
  19655. (run-hooks 'org-load-hook)
  19656. ;;; org.el ends here