org.el 808 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498
  1. ;;; org.el --- Outline-based notes management and organizer
  2. ;; Carstens outline-mode for keeping track of everything.
  3. ;; Copyright (C) 2004-2012 Free Software Foundation, Inc.
  4. ;;
  5. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  6. ;; Maintainer: Bastien Guerry <bzg at gnu dot org>
  7. ;; Keywords: outlines, hypermedia, calendar, wp
  8. ;; Homepage: http://orgmode.org
  9. ;; Version: 7.8.09
  10. ;;
  11. ;; This file is part of GNU Emacs.
  12. ;;
  13. ;; GNU Emacs is free software: you can redistribute it and/or modify
  14. ;; it under the terms of the GNU General Public License as published by
  15. ;; the Free Software Foundation, either version 3 of the License, or
  16. ;; (at your option) any later version.
  17. ;; GNU Emacs is distributed in the hope that it will be useful,
  18. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. ;; GNU General Public License for more details.
  21. ;; You should have received a copy of the GNU General Public License
  22. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  23. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  24. ;;
  25. ;;; Commentary:
  26. ;;
  27. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  28. ;; project planning with a fast and effective plain-text system.
  29. ;;
  30. ;; Org-mode develops organizational tasks around NOTES files that contain
  31. ;; information about projects as plain text. Org-mode is implemented on
  32. ;; top of outline-mode, which makes it possible to keep the content of
  33. ;; large files well structured. Visibility cycling and structure editing
  34. ;; help to work with the tree. Tables are easily created with a built-in
  35. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  36. ;; and scheduling. It dynamically compiles entries into an agenda that
  37. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  38. ;; Plain text URL-like links connect to websites, emails, Usenet
  39. ;; messages, BBDB entries, and any files related to the projects. For
  40. ;; printing and sharing of notes, an Org-mode file can be exported as a
  41. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  42. ;; iCalendar file. It can also serve as a publishing tool for a set of
  43. ;; linked webpages.
  44. ;;
  45. ;; Installation and Activation
  46. ;; ---------------------------
  47. ;; See the corresponding sections in the manual at
  48. ;;
  49. ;; http://orgmode.org/org.html#Installation
  50. ;;
  51. ;; Documentation
  52. ;; -------------
  53. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  54. ;; distribution also contains a PDF version of it. At the homepage of
  55. ;; Org-mode, you can read the same text online as HTML. There is also an
  56. ;; excellent reference card made by Philip Rooke. This card can be found
  57. ;; in the etc/ directory of Emacs 22.
  58. ;;
  59. ;; A list of recent changes can be found at
  60. ;; http://orgmode.org/Changes.html
  61. ;;
  62. ;;; Code:
  63. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  64. (defvar org-table-formula-constants-local nil
  65. "Local version of `org-table-formula-constants'.")
  66. (make-variable-buffer-local 'org-table-formula-constants-local)
  67. ;;;; Require other packages
  68. (eval-when-compile
  69. (require 'cl)
  70. (require 'gnus-sum))
  71. (require 'calendar)
  72. (require 'format-spec)
  73. ;; Emacs 22 calendar compatibility: Make sure the new variables are available
  74. (when (fboundp 'defvaralias)
  75. (unless (boundp 'calendar-view-holidays-initially-flag)
  76. (defvaralias 'calendar-view-holidays-initially-flag
  77. 'view-calendar-holidays-initially))
  78. (unless (boundp 'calendar-view-diary-initially-flag)
  79. (defvaralias 'calendar-view-diary-initially-flag
  80. 'view-diary-entries-initially))
  81. (unless (boundp 'diary-fancy-buffer)
  82. (defvaralias 'diary-fancy-buffer 'fancy-diary-buffer)))
  83. (require 'outline) (require 'noutline)
  84. ;; Other stuff we need.
  85. (require 'time-date)
  86. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  87. (require 'easymenu)
  88. (require 'overlay)
  89. (require 'org-macs)
  90. (require 'org-entities)
  91. (require 'org-compat)
  92. (require 'org-faces)
  93. (require 'org-list)
  94. (require 'org-pcomplete)
  95. (require 'org-src)
  96. (require 'org-footnote)
  97. (declare-function org-inlinetask-at-task-p "org-inlinetask" ())
  98. (declare-function org-inlinetask-outline-regexp "org-inlinetask" ())
  99. (declare-function org-inlinetask-toggle-visibility "org-inlinetask" ())
  100. (declare-function org-pop-to-buffer-same-window "org-compat" (&optional buffer-or-name norecord label))
  101. (declare-function org-at-clock-log-p "org-clock" ())
  102. (declare-function org-clock-timestamps-up "org-clock" ())
  103. (declare-function org-clock-timestamps-down "org-clock" ())
  104. ;; babel
  105. (require 'ob)
  106. (require 'ob-table)
  107. (require 'ob-lob)
  108. (require 'ob-ref)
  109. (require 'ob-tangle)
  110. (require 'ob-comint)
  111. (require 'ob-keys)
  112. ;; load languages based on value of `org-babel-load-languages'
  113. (defvar org-babel-load-languages)
  114. ;;;###autoload
  115. (defun org-babel-do-load-languages (sym value)
  116. "Load the languages defined in `org-babel-load-languages'."
  117. (set-default sym value)
  118. (mapc (lambda (pair)
  119. (let ((active (cdr pair)) (lang (symbol-name (car pair))))
  120. (if active
  121. (progn
  122. (require (intern (concat "ob-" lang))))
  123. (progn
  124. (funcall 'fmakunbound
  125. (intern (concat "org-babel-execute:" lang)))
  126. (funcall 'fmakunbound
  127. (intern (concat "org-babel-expand-body:" lang)))))))
  128. org-babel-load-languages))
  129. (defcustom org-babel-load-languages '((emacs-lisp . t))
  130. "Languages which can be evaluated in Org-mode buffers.
  131. This list can be used to load support for any of the languages
  132. below, note that each language will depend on a different set of
  133. system executables and/or Emacs modes. When a language is
  134. \"loaded\", then code blocks in that language can be evaluated
  135. with `org-babel-execute-src-block' bound by default to C-c
  136. C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
  137. be set to remove code block evaluation from the C-c C-c
  138. keybinding. By default only Emacs Lisp (which has no
  139. requirements) is loaded."
  140. :group 'org-babel
  141. :set 'org-babel-do-load-languages
  142. :version "24.1"
  143. :type '(alist :tag "Babel Languages"
  144. :key-type
  145. (choice
  146. (const :tag "Awk" awk)
  147. (const :tag "C" C)
  148. (const :tag "R" R)
  149. (const :tag "Asymptote" asymptote)
  150. (const :tag "Calc" calc)
  151. (const :tag "Clojure" clojure)
  152. (const :tag "CSS" css)
  153. (const :tag "Ditaa" ditaa)
  154. (const :tag "Dot" dot)
  155. (const :tag "Emacs Lisp" emacs-lisp)
  156. (const :tag "Fortran" fortran)
  157. (const :tag "Gnuplot" gnuplot)
  158. (const :tag "Haskell" haskell)
  159. (const :tag "IO" io)
  160. (const :tag "Java" java)
  161. (const :tag "Javascript" js)
  162. (const :tag "LaTeX" latex)
  163. (const :tag "Ledger" ledger)
  164. (const :tag "Lilypond" lilypond)
  165. (const :tag "Maxima" maxima)
  166. (const :tag "Matlab" matlab)
  167. (const :tag "Mscgen" mscgen)
  168. (const :tag "Ocaml" ocaml)
  169. (const :tag "Octave" octave)
  170. (const :tag "Org" org)
  171. (const :tag "Perl" perl)
  172. (const :tag "Pico Lisp" picolisp)
  173. (const :tag "PlantUML" plantuml)
  174. (const :tag "Python" python)
  175. (const :tag "Ruby" ruby)
  176. (const :tag "Sass" sass)
  177. (const :tag "Scala" scala)
  178. (const :tag "Scheme" scheme)
  179. (const :tag "Screen" screen)
  180. (const :tag "Shell Script" sh)
  181. (const :tag "Shen" shen)
  182. (const :tag "Sql" sql)
  183. (const :tag "Sqlite" sqlite))
  184. :value-type (boolean :tag "Activate" :value t)))
  185. ;;;; Customization variables
  186. (defcustom org-clone-delete-id nil
  187. "Remove ID property of clones of a subtree.
  188. When non-nil, clones of a subtree don't inherit the ID property.
  189. Otherwise they inherit the ID property with a new unique
  190. identifier."
  191. :type 'boolean
  192. :version "24.1"
  193. :group 'org-id)
  194. ;;; Version
  195. (defconst org-version "7.8.09"
  196. "The version number of the file org.el.")
  197. (defconst org-git-version "N/A"
  198. "The Git version of org-mode. Inserted by installing org-mode
  199. or when a release is made.")
  200. ;;;###autoload
  201. (defun org-version (&optional here)
  202. "Show the org-mode version in the echo area.
  203. With prefix arg HERE, insert it at point."
  204. (interactive "P")
  205. (let* ((origin default-directory)
  206. (version org-version)
  207. (git-version org-git-version)
  208. (dir (concat (file-name-directory (locate-library "org")) "../" )))
  209. (when (and (file-exists-p (expand-file-name ".git" dir))
  210. (executable-find "git"))
  211. (unwind-protect
  212. (progn
  213. (cd dir)
  214. (when (eql 0 (shell-command "git describe --abbrev=4 HEAD"))
  215. (with-current-buffer "*Shell Command Output*"
  216. (goto-char (point-min))
  217. (setq git-version (buffer-substring (point) (point-at-eol))))
  218. (subst-char-in-string ?- ?. git-version t)
  219. (when (string-match "\\S-"
  220. (shell-command-to-string "git status -uno --porcelain"))
  221. (setq git-version (concat git-version ".dirty")))
  222. (setq version (concat version " (" git-version ")"))))
  223. (cd origin)))
  224. (setq version (format "Org-mode version %s (%s)" version git-version))
  225. (if here (insert version))
  226. (message version)))
  227. ;;; Compatibility constants
  228. ;;; The custom variables
  229. (defgroup org nil
  230. "Outline-based notes management and organizer."
  231. :tag "Org"
  232. :group 'outlines
  233. :group 'calendar)
  234. (defcustom org-mode-hook nil
  235. "Mode hook for Org-mode, run after the mode was turned on."
  236. :group 'org
  237. :type 'hook)
  238. (defcustom org-load-hook nil
  239. "Hook that is run after org.el has been loaded."
  240. :group 'org
  241. :type 'hook)
  242. (defcustom org-log-buffer-setup-hook nil
  243. "Hook that is run after an Org log buffer is created."
  244. :group 'org
  245. :version "24.1"
  246. :type 'hook)
  247. (defvar org-modules) ; defined below
  248. (defvar org-modules-loaded nil
  249. "Have the modules been loaded already?")
  250. (defun org-load-modules-maybe (&optional force)
  251. "Load all extensions listed in `org-modules'."
  252. (when (or force (not org-modules-loaded))
  253. (mapc (lambda (ext)
  254. (condition-case nil (require ext)
  255. (error (message "Problems while trying to load feature `%s'" ext))))
  256. org-modules)
  257. (setq org-modules-loaded t)))
  258. (defun org-set-modules (var value)
  259. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  260. (set var value)
  261. (when (featurep 'org)
  262. (org-load-modules-maybe 'force)))
  263. (when (org-bound-and-true-p org-modules)
  264. (let ((a (member 'org-infojs org-modules)))
  265. (and a (setcar a 'org-jsinfo))))
  266. (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)
  267. "Modules that should always be loaded together with org.el.
  268. If a description starts with <C>, the file is not part of Emacs
  269. and loading it will require that you have downloaded and properly installed
  270. the org-mode distribution.
  271. You can also use this system to load external packages (i.e. neither Org
  272. core modules, nor modules from the CONTRIB directory). Just add symbols
  273. to the end of the list. If the package is called org-xyz.el, then you need
  274. to add the symbol `xyz', and the package must have a call to
  275. (provide 'org-xyz)"
  276. :group 'org
  277. :set 'org-set-modules
  278. :type
  279. '(set :greedy t
  280. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  281. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  282. (const :tag " crypt: Encryption of subtrees" org-crypt)
  283. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  284. (const :tag " docview: Links to doc-view buffers" org-docview)
  285. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  286. (const :tag " id: Global IDs for identifying entries" org-id)
  287. (const :tag " info: Links to Info nodes" org-info)
  288. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  289. (const :tag " habit: Track your consistency with habits" org-habit)
  290. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  291. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  292. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  293. (const :tag " mew Links to Mew folders/messages" org-mew)
  294. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  295. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  296. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  297. (const :tag " special-blocks: Turn blocks into LaTeX envs and HTML divs" org-special-blocks)
  298. (const :tag " vm: Links to VM folders/messages" org-vm)
  299. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  300. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  301. (const :tag " mouse: Additional mouse support" org-mouse)
  302. (const :tag " TaskJuggler: Export tasks to a TaskJuggler project" org-taskjuggler)
  303. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  304. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  305. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  306. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  307. (const :tag "C collector: Collect properties into tables" org-collector)
  308. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  309. (const :tag "C drill: Flashcards and spaced repetition for Org-mode" org-drill)
  310. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  311. (const :tag "C eshell Support for links to working directories in eshell" org-eshell)
  312. (const :tag "C eval: Include command output as text" org-eval)
  313. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  314. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  315. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  316. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  317. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  318. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  319. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  320. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  321. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  322. (const :tag "C notmuch: Provide org links to notmuch searches or messages" org-notmuch)
  323. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  324. (const :tag "C mac-link-grabber Grab links and URLs from various Mac applications" org-mac-link-grabber)
  325. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  326. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  327. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  328. (const :tag "C registry: A registry for Org-mode links" org-registry)
  329. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  330. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  331. (const :tag "C secretary: Team management with org-mode" org-secretary)
  332. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  333. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  334. (const :tag "C track: Keep up with Org-mode development" org-track)
  335. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  336. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  337. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  338. (defcustom org-support-shift-select nil
  339. "Non-nil means make shift-cursor commands select text when possible.
  340. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
  341. start selecting a region, or enlarge regions started in this way.
  342. In Org-mode, in special contexts, these same keys are used for
  343. other purposes, important enough to compete with shift selection.
  344. Org tries to balance these needs by supporting `shift-select-mode'
  345. outside these special contexts, under control of this variable.
  346. The default of this variable is nil, to avoid confusing behavior. Shifted
  347. cursor keys will then execute Org commands in the following contexts:
  348. - on a headline, changing TODO state (left/right) and priority (up/down)
  349. - on a time stamp, changing the time
  350. - in a plain list item, changing the bullet type
  351. - in a property definition line, switching between allowed values
  352. - in the BEGIN line of a clock table (changing the time block).
  353. Outside these contexts, the commands will throw an error.
  354. When this variable is t and the cursor is not in a special
  355. context, Org-mode will support shift-selection for making and
  356. enlarging regions. To make this more effective, the bullet
  357. cycling will no longer happen anywhere in an item line, but only
  358. if the cursor is exactly on the bullet.
  359. If you set this variable to the symbol `always', then the keys
  360. will not be special in headlines, property lines, and item lines,
  361. to make shift selection work there as well. If this is what you
  362. want, you can use the following alternative commands: `C-c C-t'
  363. and `C-c ,' to change TODO state and priority, `C-u C-u C-c C-t'
  364. can be used to switch TODO sets, `C-c -' to cycle item bullet
  365. types, and properties can be edited by hand or in column view.
  366. However, when the cursor is on a timestamp, shift-cursor commands
  367. will still edit the time stamp - this is just too good to give up.
  368. XEmacs user should have this variable set to nil, because
  369. `shift-select-mode' is in Emacs 23 or later only."
  370. :group 'org
  371. :type '(choice
  372. (const :tag "Never" nil)
  373. (const :tag "When outside special context" t)
  374. (const :tag "Everywhere except timestamps" always)))
  375. (defcustom org-loop-over-headlines-in-active-region nil
  376. "Shall some commands act upon headlines in the active region?
  377. When set to `t', some commands will be performed in all headlines
  378. within the active region.
  379. When set to `start-level', some commands will be performed in all
  380. headlines within the active region, provided that these headlines
  381. are of the same level than the first one.
  382. When set to a string, those commands will be performed on the
  383. matching headlines within the active region. Such string must be
  384. a tags/property/todo match as it is used in the agenda tags view.
  385. The list of commands is: `org-schedule', `org-deadline',
  386. `org-todo', `org-archive-subtree', `org-archive-set-tag' and
  387. `org-archive-to-archive-sibling'. The archiving commands skip
  388. already archived entries."
  389. :type '(choice (const :tag "Don't loop" nil)
  390. (const :tag "All headlines in active region" t)
  391. (const :tag "In active region, headlines at the same level than the first one" 'start-level)
  392. (string :tag "Tags/Property/Todo matcher"))
  393. :version "24.1"
  394. :group 'org-todo
  395. :group 'org-archive)
  396. (defgroup org-startup nil
  397. "Options concerning startup of Org-mode."
  398. :tag "Org Startup"
  399. :group 'org)
  400. (defcustom org-startup-folded t
  401. "Non-nil means entering Org-mode will switch to OVERVIEW.
  402. This can also be configured on a per-file basis by adding one of
  403. the following lines anywhere in the buffer:
  404. #+STARTUP: fold (or `overview', this is equivalent)
  405. #+STARTUP: nofold (or `showall', this is equivalent)
  406. #+STARTUP: content
  407. #+STARTUP: showeverything"
  408. :group 'org-startup
  409. :type '(choice
  410. (const :tag "nofold: show all" nil)
  411. (const :tag "fold: overview" t)
  412. (const :tag "content: all headlines" content)
  413. (const :tag "show everything, even drawers" showeverything)))
  414. (defcustom org-startup-truncated t
  415. "Non-nil means entering Org-mode will set `truncate-lines'.
  416. This is useful since some lines containing links can be very long and
  417. uninteresting. Also tables look terrible when wrapped."
  418. :group 'org-startup
  419. :type 'boolean)
  420. (defcustom org-startup-indented nil
  421. "Non-nil means turn on `org-indent-mode' on startup.
  422. This can also be configured on a per-file basis by adding one of
  423. the following lines anywhere in the buffer:
  424. #+STARTUP: indent
  425. #+STARTUP: noindent"
  426. :group 'org-structure
  427. :type '(choice
  428. (const :tag "Not" nil)
  429. (const :tag "Globally (slow on startup in large files)" t)))
  430. (defcustom org-use-sub-superscripts t
  431. "Non-nil means interpret \"_\" and \"^\" for export.
  432. When this option is turned on, you can use TeX-like syntax for sub- and
  433. superscripts. Several characters after \"_\" or \"^\" will be
  434. considered as a single item - so grouping with {} is normally not
  435. needed. For example, the following things will be parsed as single
  436. sub- or superscripts.
  437. 10^24 or 10^tau several digits will be considered 1 item.
  438. 10^-12 or 10^-tau a leading sign with digits or a word
  439. x^2-y^3 will be read as x^2 - y^3, because items are
  440. terminated by almost any nonword/nondigit char.
  441. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  442. Still, ambiguity is possible - so when in doubt use {} to enclose the
  443. sub/superscript. If you set this variable to the symbol `{}',
  444. the braces are *required* in order to trigger interpretations as
  445. sub/superscript. This can be helpful in documents that need \"_\"
  446. frequently in plain text.
  447. Not all export backends support this, but HTML does.
  448. This option can also be set with the +OPTIONS line, e.g. \"^:nil\"."
  449. :group 'org-startup
  450. :group 'org-export-translation
  451. :version "24.1"
  452. :type '(choice
  453. (const :tag "Always interpret" t)
  454. (const :tag "Only with braces" {})
  455. (const :tag "Never interpret" nil)))
  456. (if (fboundp 'defvaralias)
  457. (defvaralias 'org-export-with-sub-superscripts 'org-use-sub-superscripts))
  458. (defcustom org-startup-with-beamer-mode nil
  459. "Non-nil means turn on `org-beamer-mode' on startup.
  460. This can also be configured on a per-file basis by adding one of
  461. the following lines anywhere in the buffer:
  462. #+STARTUP: beamer"
  463. :group 'org-startup
  464. :version "24.1"
  465. :type 'boolean)
  466. (defcustom org-startup-align-all-tables nil
  467. "Non-nil means align all tables when visiting a file.
  468. This is useful when the column width in tables is forced with <N> cookies
  469. in table fields. Such tables will look correct only after the first re-align.
  470. This can also be configured on a per-file basis by adding one of
  471. the following lines anywhere in the buffer:
  472. #+STARTUP: align
  473. #+STARTUP: noalign"
  474. :group 'org-startup
  475. :type 'boolean)
  476. (defcustom org-startup-with-inline-images nil
  477. "Non-nil means show inline images when loading a new Org file.
  478. This can also be configured on a per-file basis by adding one of
  479. the following lines anywhere in the buffer:
  480. #+STARTUP: inlineimages
  481. #+STARTUP: noinlineimages"
  482. :group 'org-startup
  483. :version "24.1"
  484. :type 'boolean)
  485. (defcustom org-insert-mode-line-in-empty-file nil
  486. "Non-nil means insert the first line setting Org-mode in empty files.
  487. When the function `org-mode' is called interactively in an empty file, this
  488. normally means that the file name does not automatically trigger Org-mode.
  489. To ensure that the file will always be in Org-mode in the future, a
  490. line enforcing Org-mode will be inserted into the buffer, if this option
  491. has been set."
  492. :group 'org-startup
  493. :type 'boolean)
  494. (defcustom org-replace-disputed-keys nil
  495. "Non-nil means use alternative key bindings for some keys.
  496. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  497. These keys are also used by other packages like shift-selection-mode'
  498. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  499. If you want to use Org-mode together with one of these other modes,
  500. or more generally if you would like to move some Org-mode commands to
  501. other keys, set this variable and configure the keys with the variable
  502. `org-disputed-keys'.
  503. This option is only relevant at load-time of Org-mode, and must be set
  504. *before* org.el is loaded. Changing it requires a restart of Emacs to
  505. become effective."
  506. :group 'org-startup
  507. :type 'boolean)
  508. (defcustom org-use-extra-keys nil
  509. "Non-nil means use extra key sequence definitions for certain commands.
  510. This happens automatically if you run XEmacs or if `window-system'
  511. is nil. This variable lets you do the same manually. You must
  512. set it before loading org.
  513. Example: on Carbon Emacs 22 running graphically, with an external
  514. keyboard on a Powerbook, the default way of setting M-left might
  515. not work for either Alt or ESC. Setting this variable will make
  516. it work for ESC."
  517. :group 'org-startup
  518. :type 'boolean)
  519. (if (fboundp 'defvaralias)
  520. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  521. (defcustom org-disputed-keys
  522. '(([(shift up)] . [(meta p)])
  523. ([(shift down)] . [(meta n)])
  524. ([(shift left)] . [(meta -)])
  525. ([(shift right)] . [(meta +)])
  526. ([(control shift right)] . [(meta shift +)])
  527. ([(control shift left)] . [(meta shift -)]))
  528. "Keys for which Org-mode and other modes compete.
  529. This is an alist, cars are the default keys, second element specifies
  530. the alternative to use when `org-replace-disputed-keys' is t.
  531. Keys can be specified in any syntax supported by `define-key'.
  532. The value of this option takes effect only at Org-mode's startup,
  533. therefore you'll have to restart Emacs to apply it after changing."
  534. :group 'org-startup
  535. :type 'alist)
  536. (defun org-key (key)
  537. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  538. Or return the original if not disputed.
  539. Also apply the translations defined in `org-xemacs-key-equivalents'."
  540. (when org-replace-disputed-keys
  541. (let* ((nkey (key-description key))
  542. (x (org-find-if (lambda (x)
  543. (equal (key-description (car x)) nkey))
  544. org-disputed-keys)))
  545. (setq key (if x (cdr x) key))))
  546. (when (featurep 'xemacs)
  547. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  548. key)
  549. (defun org-find-if (predicate seq)
  550. (catch 'exit
  551. (while seq
  552. (if (funcall predicate (car seq))
  553. (throw 'exit (car seq))
  554. (pop seq)))))
  555. (defun org-defkey (keymap key def)
  556. "Define a key, possibly translated, as returned by `org-key'."
  557. (define-key keymap (org-key key) def))
  558. (defcustom org-ellipsis nil
  559. "The ellipsis to use in the Org-mode outline.
  560. When nil, just use the standard three dots. When a string, use that instead,
  561. When a face, use the standard 3 dots, but with the specified face.
  562. The change affects only Org-mode (which will then use its own display table).
  563. Changing this requires executing `M-x org-mode' in a buffer to become
  564. effective."
  565. :group 'org-startup
  566. :type '(choice (const :tag "Default" nil)
  567. (face :tag "Face" :value org-warning)
  568. (string :tag "String" :value "...#")))
  569. (defvar org-display-table nil
  570. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  571. (defgroup org-keywords nil
  572. "Keywords in Org-mode."
  573. :tag "Org Keywords"
  574. :group 'org)
  575. (defcustom org-deadline-string "DEADLINE:"
  576. "String to mark deadline entries.
  577. A deadline is this string, followed by a time stamp. Should be a word,
  578. terminated by a colon. You can insert a schedule keyword and
  579. a timestamp with \\[org-deadline].
  580. Changes become only effective after restarting Emacs."
  581. :group 'org-keywords
  582. :type 'string)
  583. (defcustom org-scheduled-string "SCHEDULED:"
  584. "String to mark scheduled TODO entries.
  585. A schedule is this string, followed by a time stamp. Should be a word,
  586. terminated by a colon. You can insert a schedule keyword and
  587. a timestamp with \\[org-schedule].
  588. Changes become only effective after restarting Emacs."
  589. :group 'org-keywords
  590. :type 'string)
  591. (defcustom org-closed-string "CLOSED:"
  592. "String used as the prefix for timestamps logging closing a TODO entry."
  593. :group 'org-keywords
  594. :type 'string)
  595. (defcustom org-clock-string "CLOCK:"
  596. "String used as prefix for timestamps clocking work hours on an item."
  597. :group 'org-keywords
  598. :type 'string)
  599. (defcustom org-comment-string "COMMENT"
  600. "Entries starting with this keyword will never be exported.
  601. An entry can be toggled between COMMENT and normal with
  602. \\[org-toggle-comment].
  603. Changes become only effective after restarting Emacs."
  604. :group 'org-keywords
  605. :type 'string)
  606. (defcustom org-quote-string "QUOTE"
  607. "Entries starting with this keyword will be exported in fixed-width font.
  608. Quoting applies only to the text in the entry following the headline, and does
  609. not extend beyond the next headline, even if that is lower level.
  610. An entry can be toggled between QUOTE and normal with
  611. \\[org-toggle-fixed-width-section]."
  612. :group 'org-keywords
  613. :type 'string)
  614. (defconst org-repeat-re
  615. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)"
  616. "Regular expression for specifying repeated events.
  617. After a match, group 1 contains the repeat expression.")
  618. (defgroup org-structure nil
  619. "Options concerning the general structure of Org-mode files."
  620. :tag "Org Structure"
  621. :group 'org)
  622. (defgroup org-reveal-location nil
  623. "Options about how to make context of a location visible."
  624. :tag "Org Reveal Location"
  625. :group 'org-structure)
  626. (defconst org-context-choice
  627. '(choice
  628. (const :tag "Always" t)
  629. (const :tag "Never" nil)
  630. (repeat :greedy t :tag "Individual contexts"
  631. (cons
  632. (choice :tag "Context"
  633. (const agenda)
  634. (const org-goto)
  635. (const occur-tree)
  636. (const tags-tree)
  637. (const link-search)
  638. (const mark-goto)
  639. (const bookmark-jump)
  640. (const isearch)
  641. (const default))
  642. (boolean))))
  643. "Contexts for the reveal options.")
  644. (defcustom org-show-hierarchy-above '((default . t))
  645. "Non-nil means show full hierarchy when revealing a location.
  646. Org-mode often shows locations in an org-mode file which might have
  647. been invisible before. When this is set, the hierarchy of headings
  648. above the exposed location is shown.
  649. Turning this off for example for sparse trees makes them very compact.
  650. Instead of t, this can also be an alist specifying this option for different
  651. contexts. Valid contexts are
  652. agenda when exposing an entry from the agenda
  653. org-goto when using the command `org-goto' on key C-c C-j
  654. occur-tree when using the command `org-occur' on key C-c /
  655. tags-tree when constructing a sparse tree based on tags matches
  656. link-search when exposing search matches associated with a link
  657. mark-goto when exposing the jump goal of a mark
  658. bookmark-jump when exposing a bookmark location
  659. isearch when exiting from an incremental search
  660. default default for all contexts not set explicitly"
  661. :group 'org-reveal-location
  662. :type org-context-choice)
  663. (defcustom org-show-following-heading '((default . nil))
  664. "Non-nil means show following heading when revealing a location.
  665. Org-mode often shows locations in an org-mode file which might have
  666. been invisible before. When this is set, the heading following the
  667. match is shown.
  668. Turning this off for example for sparse trees makes them very compact,
  669. but makes it harder to edit the location of the match. In such a case,
  670. use the command \\[org-reveal] to show more context.
  671. Instead of t, this can also be an alist specifying this option for different
  672. contexts. See `org-show-hierarchy-above' for valid contexts."
  673. :group 'org-reveal-location
  674. :type org-context-choice)
  675. (defcustom org-show-siblings '((default . nil) (isearch t))
  676. "Non-nil means show all sibling heading when revealing a location.
  677. Org-mode often shows locations in an org-mode file which might have
  678. been invisible before. When this is set, the sibling of the current entry
  679. heading are all made visible. If `org-show-hierarchy-above' is t,
  680. the same happens on each level of the hierarchy above the current entry.
  681. By default this is on for the isearch context, off for all other contexts.
  682. Turning this off for example for sparse trees makes them very compact,
  683. but makes it harder to edit the location of the match. In such a case,
  684. use the command \\[org-reveal] to show more context.
  685. Instead of t, this can also be an alist specifying this option for different
  686. contexts. See `org-show-hierarchy-above' for valid contexts."
  687. :group 'org-reveal-location
  688. :type org-context-choice)
  689. (defcustom org-show-entry-below '((default . nil))
  690. "Non-nil means show the entry below a headline when revealing a location.
  691. Org-mode often shows locations in an org-mode file which might have
  692. been invisible before. When this is set, the text below the headline that is
  693. exposed is also shown.
  694. By default this is off for all contexts.
  695. Instead of t, this can also be an alist specifying this option for different
  696. contexts. See `org-show-hierarchy-above' for valid contexts."
  697. :group 'org-reveal-location
  698. :type org-context-choice)
  699. (defcustom org-indirect-buffer-display 'other-window
  700. "How should indirect tree buffers be displayed?
  701. This applies to indirect buffers created with the commands
  702. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  703. Valid values are:
  704. current-window Display in the current window
  705. other-window Just display in another window.
  706. dedicated-frame Create one new frame, and re-use it each time.
  707. new-frame Make a new frame each time. Note that in this case
  708. previously-made indirect buffers are kept, and you need to
  709. kill these buffers yourself."
  710. :group 'org-structure
  711. :group 'org-agenda-windows
  712. :type '(choice
  713. (const :tag "In current window" current-window)
  714. (const :tag "In current frame, other window" other-window)
  715. (const :tag "Each time a new frame" new-frame)
  716. (const :tag "One dedicated frame" dedicated-frame)))
  717. (defcustom org-use-speed-commands nil
  718. "Non-nil means activate single letter commands at beginning of a headline.
  719. This may also be a function to test for appropriate locations where speed
  720. commands should be active."
  721. :group 'org-structure
  722. :type '(choice
  723. (const :tag "Never" nil)
  724. (const :tag "At beginning of headline stars" t)
  725. (function)))
  726. (defcustom org-speed-commands-user nil
  727. "Alist of additional speed commands.
  728. This list will be checked before `org-speed-commands-default'
  729. when the variable `org-use-speed-commands' is non-nil
  730. and when the cursor is at the beginning of a headline.
  731. The car if each entry is a string with a single letter, which must
  732. be assigned to `self-insert-command' in the global map.
  733. The cdr is either a command to be called interactively, a function
  734. to be called, or a form to be evaluated.
  735. An entry that is just a list with a single string will be interpreted
  736. as a descriptive headline that will be added when listing the speed
  737. commands in the Help buffer using the `?' speed command."
  738. :group 'org-structure
  739. :type '(repeat :value ("k" . ignore)
  740. (choice :value ("k" . ignore)
  741. (list :tag "Descriptive Headline" (string :tag "Headline"))
  742. (cons :tag "Letter and Command"
  743. (string :tag "Command letter")
  744. (choice
  745. (function)
  746. (sexp))))))
  747. (defgroup org-cycle nil
  748. "Options concerning visibility cycling in Org-mode."
  749. :tag "Org Cycle"
  750. :group 'org-structure)
  751. (defcustom org-cycle-skip-children-state-if-no-children t
  752. "Non-nil means skip CHILDREN state in entries that don't have any."
  753. :group 'org-cycle
  754. :type 'boolean)
  755. (defcustom org-cycle-max-level nil
  756. "Maximum level which should still be subject to visibility cycling.
  757. Levels higher than this will, for cycling, be treated as text, not a headline.
  758. When `org-odd-levels-only' is set, a value of N in this variable actually
  759. means 2N-1 stars as the limiting headline.
  760. When nil, cycle all levels.
  761. Note that the limiting level of cycling is also influenced by
  762. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  763. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  764. than its value."
  765. :group 'org-cycle
  766. :type '(choice
  767. (const :tag "No limit" nil)
  768. (integer :tag "Maximum level")))
  769. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK" "RESULTS")
  770. "Names of drawers. Drawers are not opened by cycling on the headline above.
  771. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  772. this:
  773. :DRAWERNAME:
  774. .....
  775. :END:
  776. The drawer \"PROPERTIES\" is special for capturing properties through
  777. the property API.
  778. Drawers can be defined on the per-file basis with a line like:
  779. #+DRAWERS: HIDDEN STATE PROPERTIES"
  780. :group 'org-structure
  781. :group 'org-cycle
  782. :type '(repeat (string :tag "Drawer Name")))
  783. (defcustom org-hide-block-startup nil
  784. "Non-nil means entering Org-mode will fold all blocks.
  785. This can also be set in on a per-file basis with
  786. #+STARTUP: hideblocks
  787. #+STARTUP: showblocks"
  788. :group 'org-startup
  789. :group 'org-cycle
  790. :type 'boolean)
  791. (defcustom org-cycle-global-at-bob nil
  792. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  793. This makes it possible to do global cycling without having to use S-TAB or
  794. \\[universal-argument] TAB. For this special case to work, the first line \
  795. of the buffer
  796. must not be a headline - it may be empty or some other text. When used in
  797. this way, `org-cycle-hook' is disables temporarily, to make sure the
  798. cursor stays at the beginning of the buffer.
  799. When this option is nil, don't do anything special at the beginning
  800. of the buffer."
  801. :group 'org-cycle
  802. :type 'boolean)
  803. (defcustom org-cycle-level-after-item/entry-creation t
  804. "Non-nil means cycle entry level or item indentation in new empty entries.
  805. When the cursor is at the end of an empty headline, i.e with only stars
  806. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  807. and then all possible ancestor states, before returning to the original state.
  808. This makes data entry extremely fast: M-RET to create a new headline,
  809. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  810. When the cursor is at the end of an empty plain list item, one TAB will
  811. make it a subitem, two or more tabs will back up to make this an item
  812. higher up in the item hierarchy."
  813. :group 'org-cycle
  814. :type 'boolean)
  815. (defcustom org-cycle-emulate-tab t
  816. "Where should `org-cycle' emulate TAB.
  817. nil Never
  818. white Only in completely white lines
  819. whitestart Only at the beginning of lines, before the first non-white char
  820. t Everywhere except in headlines
  821. exc-hl-bol Everywhere except at the start of a headline
  822. If TAB is used in a place where it does not emulate TAB, the current subtree
  823. visibility is cycled."
  824. :group 'org-cycle
  825. :type '(choice (const :tag "Never" nil)
  826. (const :tag "Only in completely white lines" white)
  827. (const :tag "Before first char in a line" whitestart)
  828. (const :tag "Everywhere except in headlines" t)
  829. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  830. ))
  831. (defcustom org-cycle-separator-lines 2
  832. "Number of empty lines needed to keep an empty line between collapsed trees.
  833. If you leave an empty line between the end of a subtree and the following
  834. headline, this empty line is hidden when the subtree is folded.
  835. Org-mode will leave (exactly) one empty line visible if the number of
  836. empty lines is equal or larger to the number given in this variable.
  837. So the default 2 means at least 2 empty lines after the end of a subtree
  838. are needed to produce free space between a collapsed subtree and the
  839. following headline.
  840. If the number is negative, and the number of empty lines is at least -N,
  841. all empty lines are shown.
  842. Special case: when 0, never leave empty lines in collapsed view."
  843. :group 'org-cycle
  844. :type 'integer)
  845. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  846. (defcustom org-pre-cycle-hook nil
  847. "Hook that is run before visibility cycling is happening.
  848. The function(s) in this hook must accept a single argument which indicates
  849. the new state that will be set right after running this hook. The
  850. argument is a symbol. Before a global state change, it can have the values
  851. `overview', `content', or `all'. Before a local state change, it can have
  852. the values `folded', `children', or `subtree'."
  853. :group 'org-cycle
  854. :type 'hook)
  855. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  856. org-cycle-hide-drawers
  857. org-cycle-show-empty-lines
  858. org-optimize-window-after-visibility-change)
  859. "Hook that is run after `org-cycle' has changed the buffer visibility.
  860. The function(s) in this hook must accept a single argument which indicates
  861. the new state that was set by the most recent `org-cycle' command. The
  862. argument is a symbol. After a global state change, it can have the values
  863. `overview', `content', or `all'. After a local state change, it can have
  864. the values `folded', `children', or `subtree'."
  865. :group 'org-cycle
  866. :type 'hook)
  867. (defgroup org-edit-structure nil
  868. "Options concerning structure editing in Org-mode."
  869. :tag "Org Edit Structure"
  870. :group 'org-structure)
  871. (defcustom org-odd-levels-only nil
  872. "Non-nil means skip even levels and only use odd levels for the outline.
  873. This has the effect that two stars are being added/taken away in
  874. promotion/demotion commands. It also influences how levels are
  875. handled by the exporters.
  876. Changing it requires restart of `font-lock-mode' to become effective
  877. for fontification also in regions already fontified.
  878. You may also set this on a per-file basis by adding one of the following
  879. lines to the buffer:
  880. #+STARTUP: odd
  881. #+STARTUP: oddeven"
  882. :group 'org-edit-structure
  883. :group 'org-appearance
  884. :type 'boolean)
  885. (defcustom org-adapt-indentation t
  886. "Non-nil means adapt indentation to outline node level.
  887. When this variable is set, Org assumes that you write outlines by
  888. indenting text in each node to align with the headline (after the stars).
  889. The following issues are influenced by this variable:
  890. - When this is set and the *entire* text in an entry is indented, the
  891. indentation is increased by one space in a demotion command, and
  892. decreased by one in a promotion command. If any line in the entry
  893. body starts with text at column 0, indentation is not changed at all.
  894. - Property drawers and planning information is inserted indented when
  895. this variable s set. When nil, they will not be indented.
  896. - TAB indents a line relative to context. The lines below a headline
  897. will be indented when this variable is set.
  898. Note that this is all about true indentation, by adding and removing
  899. space characters. See also `org-indent.el' which does level-dependent
  900. indentation in a virtual way, i.e. at display time in Emacs."
  901. :group 'org-edit-structure
  902. :type 'boolean)
  903. (defcustom org-special-ctrl-a/e nil
  904. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  905. When t, `C-a' will bring back the cursor to the beginning of the
  906. headline text, i.e. after the stars and after a possible TODO
  907. keyword. In an item, this will be the position after bullet and
  908. check-box, if any. When the cursor is already at that position,
  909. another `C-a' will bring it to the beginning of the line.
  910. `C-e' will jump to the end of the headline, ignoring the presence
  911. of tags in the headline. A second `C-e' will then jump to the
  912. true end of the line, after any tags. This also means that, when
  913. this variable is non-nil, `C-e' also will never jump beyond the
  914. end of the heading of a folded section, i.e. not after the
  915. ellipses.
  916. When set to the symbol `reversed', the first `C-a' or `C-e' works
  917. normally, going to the true line boundary first. Only a directly
  918. following, identical keypress will bring the cursor to the
  919. special positions.
  920. This may also be a cons cell where the behavior for `C-a' and
  921. `C-e' is set separately."
  922. :group 'org-edit-structure
  923. :type '(choice
  924. (const :tag "off" nil)
  925. (const :tag "on: after stars/bullet and before tags first" t)
  926. (const :tag "reversed: true line boundary first" reversed)
  927. (cons :tag "Set C-a and C-e separately"
  928. (choice :tag "Special C-a"
  929. (const :tag "off" nil)
  930. (const :tag "on: after stars/bullet first" t)
  931. (const :tag "reversed: before stars/bullet first" reversed))
  932. (choice :tag "Special C-e"
  933. (const :tag "off" nil)
  934. (const :tag "on: before tags first" t)
  935. (const :tag "reversed: after tags first" reversed)))))
  936. (if (fboundp 'defvaralias)
  937. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  938. (defcustom org-special-ctrl-k nil
  939. "Non-nil means `C-k' will behave specially in headlines.
  940. When nil, `C-k' will call the default `kill-line' command.
  941. When t, the following will happen while the cursor is in the headline:
  942. - When the cursor is at the beginning of a headline, kill the entire
  943. line and possible the folded subtree below the line.
  944. - When in the middle of the headline text, kill the headline up to the tags.
  945. - When after the headline text, kill the tags."
  946. :group 'org-edit-structure
  947. :type 'boolean)
  948. (defcustom org-ctrl-k-protect-subtree nil
  949. "Non-nil means, do not delete a hidden subtree with C-k.
  950. When set to the symbol `error', simply throw an error when C-k is
  951. used to kill (part-of) a headline that has hidden text behind it.
  952. Any other non-nil value will result in a query to the user, if it is
  953. OK to kill that hidden subtree. When nil, kill without remorse."
  954. :group 'org-edit-structure
  955. :version "24.1"
  956. :type '(choice
  957. (const :tag "Do not protect hidden subtrees" nil)
  958. (const :tag "Protect hidden subtrees with a security query" t)
  959. (const :tag "Never kill a hidden subtree with C-k" error)))
  960. (defcustom org-catch-invisible-edits nil
  961. "Check if in invisible region before inserting or deleting a character.
  962. Valid values are:
  963. nil Do not check, so just do invisible edits.
  964. error Throw an error and do nothing.
  965. show Make point visible, and do the requested edit.
  966. show-and-error Make point visible, then throw an error and abort the edit.
  967. smart Make point visible, and do insertion/deletion if it is
  968. adjacent to visible text and the change feels predictable.
  969. Never delete a previously invisible character or add in the
  970. middle or right after an invisible region. Basically, this
  971. allows insertion and backward-delete right before ellipses.
  972. FIXME: maybe in this case we should not even show?"
  973. :group 'org-edit-structure
  974. :version "24.1"
  975. :type '(choice
  976. (const :tag "Do not check" nil)
  977. (const :tag "Throw error when trying to edit" error)
  978. (const :tag "Unhide, but do not do the edit" show-and-error)
  979. (const :tag "Show invisible part and do the edit" show)
  980. (const :tag "Be smart and do the right thing" smart)))
  981. (defcustom org-yank-folded-subtrees t
  982. "Non-nil means when yanking subtrees, fold them.
  983. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  984. it starts with a heading and all other headings in it are either children
  985. or siblings, then fold all the subtrees. However, do this only if no
  986. text after the yank would be swallowed into a folded tree by this action."
  987. :group 'org-edit-structure
  988. :type 'boolean)
  989. (defcustom org-yank-adjusted-subtrees nil
  990. "Non-nil means when yanking subtrees, adjust the level.
  991. With this setting, `org-paste-subtree' is used to insert the subtree, see
  992. this function for details."
  993. :group 'org-edit-structure
  994. :type 'boolean)
  995. (defcustom org-M-RET-may-split-line '((default . t))
  996. "Non-nil means M-RET will split the line at the cursor position.
  997. When nil, it will go to the end of the line before making a
  998. new line.
  999. You may also set this option in a different way for different
  1000. contexts. Valid contexts are:
  1001. headline when creating a new headline
  1002. item when creating a new item
  1003. table in a table field
  1004. default the value to be used for all contexts not explicitly
  1005. customized"
  1006. :group 'org-structure
  1007. :group 'org-table
  1008. :type '(choice
  1009. (const :tag "Always" t)
  1010. (const :tag "Never" nil)
  1011. (repeat :greedy t :tag "Individual contexts"
  1012. (cons
  1013. (choice :tag "Context"
  1014. (const headline)
  1015. (const item)
  1016. (const table)
  1017. (const default))
  1018. (boolean)))))
  1019. (defcustom org-insert-heading-respect-content nil
  1020. "Non-nil means insert new headings after the current subtree.
  1021. When nil, the new heading is created directly after the current line.
  1022. The commands \\[org-insert-heading-respect-content] and
  1023. \\[org-insert-todo-heading-respect-content] turn this variable on
  1024. for the duration of the command."
  1025. :group 'org-structure
  1026. :type 'boolean)
  1027. (defcustom org-blank-before-new-entry '((heading . auto)
  1028. (plain-list-item . auto))
  1029. "Should `org-insert-heading' leave a blank line before new heading/item?
  1030. The value is an alist, with `heading' and `plain-list-item' as CAR,
  1031. and a boolean flag as CDR. The cdr may also be the symbol `auto', in
  1032. which case Org will look at the surrounding headings/items and try to
  1033. make an intelligent decision whether to insert a blank line or not.
  1034. For plain lists, if the variable `org-empty-line-terminates-plain-lists' is
  1035. set, the setting here is ignored and no empty line is inserted, to avoid
  1036. breaking the list structure."
  1037. :group 'org-edit-structure
  1038. :type '(list
  1039. (cons (const heading)
  1040. (choice (const :tag "Never" nil)
  1041. (const :tag "Always" t)
  1042. (const :tag "Auto" auto)))
  1043. (cons (const plain-list-item)
  1044. (choice (const :tag "Never" nil)
  1045. (const :tag "Always" t)
  1046. (const :tag "Auto" auto)))))
  1047. (defcustom org-insert-heading-hook nil
  1048. "Hook being run after inserting a new heading."
  1049. :group 'org-edit-structure
  1050. :type 'hook)
  1051. (defcustom org-enable-fixed-width-editor t
  1052. "Non-nil means lines starting with \":\" are treated as fixed-width.
  1053. This currently only means they are never auto-wrapped.
  1054. When nil, such lines will be treated like ordinary lines.
  1055. See also the QUOTE keyword."
  1056. :group 'org-edit-structure
  1057. :type 'boolean)
  1058. (defcustom org-goto-auto-isearch t
  1059. "Non-nil means typing characters in `org-goto' starts incremental search."
  1060. :group 'org-edit-structure
  1061. :type 'boolean)
  1062. (defgroup org-sparse-trees nil
  1063. "Options concerning sparse trees in Org-mode."
  1064. :tag "Org Sparse Trees"
  1065. :group 'org-structure)
  1066. (defcustom org-highlight-sparse-tree-matches t
  1067. "Non-nil means highlight all matches that define a sparse tree.
  1068. The highlights will automatically disappear the next time the buffer is
  1069. changed by an edit command."
  1070. :group 'org-sparse-trees
  1071. :type 'boolean)
  1072. (defcustom org-remove-highlights-with-change t
  1073. "Non-nil means any change to the buffer will remove temporary highlights.
  1074. Such highlights are created by `org-occur' and `org-clock-display'.
  1075. When nil, `C-c C-c needs to be used to get rid of the highlights.
  1076. The highlights created by `org-preview-latex-fragment' always need
  1077. `C-c C-c' to be removed."
  1078. :group 'org-sparse-trees
  1079. :group 'org-time
  1080. :type 'boolean)
  1081. (defcustom org-occur-hook '(org-first-headline-recenter)
  1082. "Hook that is run after `org-occur' has constructed a sparse tree.
  1083. This can be used to recenter the window to show as much of the structure
  1084. as possible."
  1085. :group 'org-sparse-trees
  1086. :type 'hook)
  1087. (defgroup org-imenu-and-speedbar nil
  1088. "Options concerning imenu and speedbar in Org-mode."
  1089. :tag "Org Imenu and Speedbar"
  1090. :group 'org-structure)
  1091. (defcustom org-imenu-depth 2
  1092. "The maximum level for Imenu access to Org-mode headlines.
  1093. This also applied for speedbar access."
  1094. :group 'org-imenu-and-speedbar
  1095. :type 'integer)
  1096. (defgroup org-table nil
  1097. "Options concerning tables in Org-mode."
  1098. :tag "Org Table"
  1099. :group 'org)
  1100. (defcustom org-enable-table-editor 'optimized
  1101. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1102. When nil, such lines will be treated like ordinary lines.
  1103. When equal to the symbol `optimized', the table editor will be optimized to
  1104. do the following:
  1105. - Automatic overwrite mode in front of whitespace in table fields.
  1106. This makes the structure of the table stay in tact as long as the edited
  1107. field does not exceed the column width.
  1108. - Minimize the number of realigns. Normally, the table is aligned each time
  1109. TAB or RET are pressed to move to another field. With optimization this
  1110. happens only if changes to a field might have changed the column width.
  1111. Optimization requires replacing the functions `self-insert-command',
  1112. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1113. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1114. very good at guessing when a re-align will be necessary, but you can always
  1115. force one with \\[org-ctrl-c-ctrl-c].
  1116. If you would like to use the optimized version in Org-mode, but the
  1117. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1118. This variable can be used to turn on and off the table editor during a session,
  1119. but in order to toggle optimization, a restart is required.
  1120. See also the variable `org-table-auto-blank-field'."
  1121. :group 'org-table
  1122. :type '(choice
  1123. (const :tag "off" nil)
  1124. (const :tag "on" t)
  1125. (const :tag "on, optimized" optimized)))
  1126. (defcustom org-self-insert-cluster-for-undo t
  1127. "Non-nil means cluster self-insert commands for undo when possible.
  1128. If this is set, then, like in the Emacs command loop, 20 consecutive
  1129. characters will be undone together.
  1130. This is configurable, because there is some impact on typing performance."
  1131. :group 'org-table
  1132. :type 'boolean)
  1133. (defcustom org-table-tab-recognizes-table.el t
  1134. "Non-nil means TAB will automatically notice a table.el table.
  1135. When it sees such a table, it moves point into it and - if necessary -
  1136. calls `table-recognize-table'."
  1137. :group 'org-table-editing
  1138. :type 'boolean)
  1139. (defgroup org-link nil
  1140. "Options concerning links in Org-mode."
  1141. :tag "Org Link"
  1142. :group 'org)
  1143. (defvar org-link-abbrev-alist-local nil
  1144. "Buffer-local version of `org-link-abbrev-alist', which see.
  1145. The value of this is taken from the #+LINK lines.")
  1146. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1147. (defcustom org-link-abbrev-alist nil
  1148. "Alist of link abbreviations.
  1149. The car of each element is a string, to be replaced at the start of a link.
  1150. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1151. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1152. [[linkkey:tag][description]]
  1153. The 'linkkey' must be a word word, starting with a letter, followed
  1154. by letters, numbers, '-' or '_'.
  1155. If REPLACE is a string, the tag will simply be appended to create the link.
  1156. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  1157. the placeholder \"%h\" will cause a url-encoded version of the tag to
  1158. be inserted at that point (see the function `url-hexify-string').
  1159. REPLACE may also be a function that will be called with the tag as the
  1160. only argument to create the link, which should be returned as a string.
  1161. See the manual for examples."
  1162. :group 'org-link
  1163. :type '(repeat
  1164. (cons
  1165. (string :tag "Protocol")
  1166. (choice
  1167. (string :tag "Format")
  1168. (function)))))
  1169. (defcustom org-descriptive-links t
  1170. "Non-nil means Org will display descriptive links.
  1171. E.g. [[http://orgmode.org][Org website]] will be displayed as
  1172. \"Org Website\", hiding the link itself and just displaying its
  1173. description. When set to `nil', Org will display the full links
  1174. literally.
  1175. You can interactively set the value of this variable by calling
  1176. `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
  1177. :group 'org-link
  1178. :type 'boolean)
  1179. (defcustom org-link-file-path-type 'adaptive
  1180. "How the path name in file links should be stored.
  1181. Valid values are:
  1182. relative Relative to the current directory, i.e. the directory of the file
  1183. into which the link is being inserted.
  1184. absolute Absolute path, if possible with ~ for home directory.
  1185. noabbrev Absolute path, no abbreviation of home directory.
  1186. adaptive Use relative path for files in the current directory and sub-
  1187. directories of it. For other files, use an absolute path."
  1188. :group 'org-link
  1189. :type '(choice
  1190. (const relative)
  1191. (const absolute)
  1192. (const noabbrev)
  1193. (const adaptive)))
  1194. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  1195. "Types of links that should be activated in Org-mode files.
  1196. This is a list of symbols, each leading to the activation of a certain link
  1197. type. In principle, it does not hurt to turn on most link types - there may
  1198. be a small gain when turning off unused link types. The types are:
  1199. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1200. angle Links in angular brackets that may contain whitespace like
  1201. <bbdb:Carsten Dominik>.
  1202. plain Plain links in normal text, no whitespace, like http://google.com.
  1203. radio Text that is matched by a radio target, see manual for details.
  1204. tag Tag settings in a headline (link to tag search).
  1205. date Time stamps (link to calendar).
  1206. footnote Footnote labels.
  1207. Changing this variable requires a restart of Emacs to become effective."
  1208. :group 'org-link
  1209. :type '(set :greedy t
  1210. (const :tag "Double bracket links" bracket)
  1211. (const :tag "Angular bracket links" angle)
  1212. (const :tag "Plain text links" plain)
  1213. (const :tag "Radio target matches" radio)
  1214. (const :tag "Tags" tag)
  1215. (const :tag "Timestamps" date)
  1216. (const :tag "Footnotes" footnote)))
  1217. (defcustom org-make-link-description-function nil
  1218. "Function to use to generate link descriptions from links.
  1219. If nil the link location will be used. This function must take
  1220. two parameters; the first is the link and the second the
  1221. description `org-insert-link' has generated, and should return the
  1222. description to use."
  1223. :group 'org-link
  1224. :type 'function)
  1225. (defgroup org-link-store nil
  1226. "Options concerning storing links in Org-mode."
  1227. :tag "Org Store Link"
  1228. :group 'org-link)
  1229. (defcustom org-email-link-description-format "Email %c: %.30s"
  1230. "Format of the description part of a link to an email or usenet message.
  1231. The following %-escapes will be replaced by corresponding information:
  1232. %F full \"From\" field
  1233. %f name, taken from \"From\" field, address if no name
  1234. %T full \"To\" field
  1235. %t first name in \"To\" field, address if no name
  1236. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1237. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1238. %s subject
  1239. %d date
  1240. %m message-id.
  1241. You may use normal field width specification between the % and the letter.
  1242. This is for example useful to limit the length of the subject.
  1243. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1244. :group 'org-link-store
  1245. :type 'string)
  1246. (defcustom org-from-is-user-regexp
  1247. (let (r1 r2)
  1248. (when (and user-mail-address (not (string= user-mail-address "")))
  1249. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1250. (when (and user-full-name (not (string= user-full-name "")))
  1251. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1252. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1253. "Regexp matched against the \"From:\" header of an email or usenet message.
  1254. It should match if the message is from the user him/herself."
  1255. :group 'org-link-store
  1256. :type 'regexp)
  1257. (defcustom org-link-to-org-use-id 'create-if-interactive-and-no-custom-id
  1258. "Non-nil means storing a link to an Org file will use entry IDs.
  1259. Note that before this variable is even considered, org-id must be loaded,
  1260. so please customize `org-modules' and turn it on.
  1261. The variable can have the following values:
  1262. t Create an ID if needed to make a link to the current entry.
  1263. create-if-interactive
  1264. If `org-store-link' is called directly (interactively, as a user
  1265. command), do create an ID to support the link. But when doing the
  1266. job for remember, only use the ID if it already exists. The
  1267. purpose of this setting is to avoid proliferation of unwanted
  1268. IDs, just because you happen to be in an Org file when you
  1269. call `org-remember' that automatically and preemptively
  1270. creates a link. If you do want to get an ID link in a remember
  1271. template to an entry not having an ID, create it first by
  1272. explicitly creating a link to it, using `C-c C-l' first.
  1273. create-if-interactive-and-no-custom-id
  1274. Like create-if-interactive, but do not create an ID if there is
  1275. a CUSTOM_ID property defined in the entry. This is the default.
  1276. use-existing
  1277. Use existing ID, do not create one.
  1278. nil Never use an ID to make a link, instead link using a text search for
  1279. the headline text."
  1280. :group 'org-link-store
  1281. :type '(choice
  1282. (const :tag "Create ID to make link" t)
  1283. (const :tag "Create if storing link interactively"
  1284. create-if-interactive)
  1285. (const :tag "Create if storing link interactively and no CUSTOM_ID is present"
  1286. create-if-interactive-and-no-custom-id)
  1287. (const :tag "Only use existing" use-existing)
  1288. (const :tag "Do not use ID to create link" nil)))
  1289. (defcustom org-context-in-file-links t
  1290. "Non-nil means file links from `org-store-link' contain context.
  1291. A search string will be added to the file name with :: as separator and
  1292. used to find the context when the link is activated by the command
  1293. `org-open-at-point'. When this option is t, the entire active region
  1294. will be placed in the search string of the file link. If set to a
  1295. positive integer, only the first n lines of context will be stored.
  1296. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1297. negates this setting for the duration of the command."
  1298. :group 'org-link-store
  1299. :type '(choice boolean integer))
  1300. (defcustom org-keep-stored-link-after-insertion nil
  1301. "Non-nil means keep link in list for entire session.
  1302. The command `org-store-link' adds a link pointing to the current
  1303. location to an internal list. These links accumulate during a session.
  1304. The command `org-insert-link' can be used to insert links into any
  1305. Org-mode file (offering completion for all stored links). When this
  1306. option is nil, every link which has been inserted once using \\[org-insert-link]
  1307. will be removed from the list, to make completing the unused links
  1308. more efficient."
  1309. :group 'org-link-store
  1310. :type 'boolean)
  1311. (defgroup org-link-follow nil
  1312. "Options concerning following links in Org-mode."
  1313. :tag "Org Follow Link"
  1314. :group 'org-link)
  1315. (defcustom org-link-translation-function nil
  1316. "Function to translate links with different syntax to Org syntax.
  1317. This can be used to translate links created for example by the Planner
  1318. or emacs-wiki packages to Org syntax.
  1319. The function must accept two parameters, a TYPE containing the link
  1320. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1321. which is everything after the link protocol. It should return a cons
  1322. with possibly modified values of type and path.
  1323. Org contains a function for this, so if you set this variable to
  1324. `org-translate-link-from-planner', you should be able follow many
  1325. links created by planner."
  1326. :group 'org-link-follow
  1327. :type 'function)
  1328. (defcustom org-follow-link-hook nil
  1329. "Hook that is run after a link has been followed."
  1330. :group 'org-link-follow
  1331. :type 'hook)
  1332. (defcustom org-tab-follows-link nil
  1333. "Non-nil means on links TAB will follow the link.
  1334. Needs to be set before org.el is loaded.
  1335. This really should not be used, it does not make sense, and the
  1336. implementation is bad."
  1337. :group 'org-link-follow
  1338. :type 'boolean)
  1339. (defcustom org-return-follows-link nil
  1340. "Non-nil means on links RET will follow the link."
  1341. :group 'org-link-follow
  1342. :type 'boolean)
  1343. (defcustom org-mouse-1-follows-link
  1344. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1345. "Non-nil means mouse-1 on a link will follow the link.
  1346. A longer mouse click will still set point. Does not work on XEmacs.
  1347. Needs to be set before org.el is loaded."
  1348. :group 'org-link-follow
  1349. :type 'boolean)
  1350. (defcustom org-mark-ring-length 4
  1351. "Number of different positions to be recorded in the ring.
  1352. Changing this requires a restart of Emacs to work correctly."
  1353. :group 'org-link-follow
  1354. :type 'integer)
  1355. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1356. "Non-nil means internal links in Org files must exactly match a headline.
  1357. When nil, the link search tries to match a phrase with all words
  1358. in the search text."
  1359. :group 'org-link-follow
  1360. :version "24.1"
  1361. :type '(choice
  1362. (const :tag "Use fuzzy text search" nil)
  1363. (const :tag "Match only exact headline" t)
  1364. (const :tag "Match exact headline or query to create it"
  1365. query-to-create)))
  1366. (defcustom org-link-frame-setup
  1367. '((vm . vm-visit-folder-other-frame)
  1368. (vm-imap . vm-visit-imap-folder-other-frame)
  1369. (gnus . org-gnus-no-new-news)
  1370. (file . find-file-other-window)
  1371. (wl . wl-other-frame))
  1372. "Setup the frame configuration for following links.
  1373. When following a link with Emacs, it may often be useful to display
  1374. this link in another window or frame. This variable can be used to
  1375. set this up for the different types of links.
  1376. For VM, use any of
  1377. `vm-visit-folder'
  1378. `vm-visit-folder-other-window'
  1379. `vm-visit-folder-other-frame'
  1380. For Gnus, use any of
  1381. `gnus'
  1382. `gnus-other-frame'
  1383. `org-gnus-no-new-news'
  1384. For FILE, use any of
  1385. `find-file'
  1386. `find-file-other-window'
  1387. `find-file-other-frame'
  1388. For Wanderlust use any of
  1389. `wl'
  1390. `wl-other-frame'
  1391. For the calendar, use the variable `calendar-setup'.
  1392. For BBDB, it is currently only possible to display the matches in
  1393. another window."
  1394. :group 'org-link-follow
  1395. :type '(list
  1396. (cons (const vm)
  1397. (choice
  1398. (const vm-visit-folder)
  1399. (const vm-visit-folder-other-window)
  1400. (const vm-visit-folder-other-frame)))
  1401. (cons (const gnus)
  1402. (choice
  1403. (const gnus)
  1404. (const gnus-other-frame)
  1405. (const org-gnus-no-new-news)))
  1406. (cons (const file)
  1407. (choice
  1408. (const find-file)
  1409. (const find-file-other-window)
  1410. (const find-file-other-frame)))
  1411. (cons (const wl)
  1412. (choice
  1413. (const wl)
  1414. (const wl-other-frame)))))
  1415. (defcustom org-display-internal-link-with-indirect-buffer nil
  1416. "Non-nil means use indirect buffer to display infile links.
  1417. Activating internal links (from one location in a file to another location
  1418. in the same file) normally just jumps to the location. When the link is
  1419. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1420. is displayed in
  1421. another window. When this option is set, the other window actually displays
  1422. an indirect buffer clone of the current buffer, to avoid any visibility
  1423. changes to the current buffer."
  1424. :group 'org-link-follow
  1425. :type 'boolean)
  1426. (defcustom org-open-non-existing-files nil
  1427. "Non-nil means `org-open-file' will open non-existing files.
  1428. When nil, an error will be generated.
  1429. This variable applies only to external applications because they
  1430. might choke on non-existing files. If the link is to a file that
  1431. will be opened in Emacs, the variable is ignored."
  1432. :group 'org-link-follow
  1433. :type 'boolean)
  1434. (defcustom org-open-directory-means-index-dot-org nil
  1435. "Non-nil means a link to a directory really means to index.org.
  1436. When nil, following a directory link will run dired or open a finder/explorer
  1437. window on that directory."
  1438. :group 'org-link-follow
  1439. :type 'boolean)
  1440. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1441. "Function and arguments to call for following mailto links.
  1442. This is a list with the first element being a Lisp function, and the
  1443. remaining elements being arguments to the function. In string arguments,
  1444. %a will be replaced by the address, and %s will be replaced by the subject
  1445. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1446. :group 'org-link-follow
  1447. :type '(choice
  1448. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1449. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1450. (const :tag "message-mail" (message-mail "%a" "%s"))
  1451. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1452. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1453. "Non-nil means ask for confirmation before executing shell links.
  1454. Shell links can be dangerous: just think about a link
  1455. [[shell:rm -rf ~/*][Google Search]]
  1456. This link would show up in your Org-mode document as \"Google Search\",
  1457. but really it would remove your entire home directory.
  1458. Therefore we advise against setting this variable to nil.
  1459. Just change it to `y-or-n-p' if you want to confirm with a
  1460. single keystroke rather than having to type \"yes\"."
  1461. :group 'org-link-follow
  1462. :type '(choice
  1463. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1464. (const :tag "with y-or-n (faster)" y-or-n-p)
  1465. (const :tag "no confirmation (dangerous)" nil)))
  1466. (put 'org-confirm-shell-link-function
  1467. 'safe-local-variable
  1468. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1469. (defcustom org-confirm-shell-link-not-regexp ""
  1470. "A regexp to skip confirmation for shell links."
  1471. :group 'org-link-follow
  1472. :version "24.1"
  1473. :type 'regexp)
  1474. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1475. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1476. Elisp links can be dangerous: just think about a link
  1477. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1478. This link would show up in your Org-mode document as \"Google Search\",
  1479. but really it would remove your entire home directory.
  1480. Therefore we advise against setting this variable to nil.
  1481. Just change it to `y-or-n-p' if you want to confirm with a
  1482. single keystroke rather than having to type \"yes\"."
  1483. :group 'org-link-follow
  1484. :type '(choice
  1485. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1486. (const :tag "with y-or-n (faster)" y-or-n-p)
  1487. (const :tag "no confirmation (dangerous)" nil)))
  1488. (put 'org-confirm-shell-link-function
  1489. 'safe-local-variable
  1490. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1491. (defcustom org-confirm-elisp-link-not-regexp ""
  1492. "A regexp to skip confirmation for Elisp links."
  1493. :group 'org-link-follow
  1494. :version "24.1"
  1495. :type 'regexp)
  1496. (defconst org-file-apps-defaults-gnu
  1497. '((remote . emacs)
  1498. (system . mailcap)
  1499. (t . mailcap))
  1500. "Default file applications on a UNIX or GNU/Linux system.
  1501. See `org-file-apps'.")
  1502. (defconst org-file-apps-defaults-macosx
  1503. '((remote . emacs)
  1504. (t . "open %s")
  1505. (system . "open %s")
  1506. ("ps.gz" . "gv %s")
  1507. ("eps.gz" . "gv %s")
  1508. ("dvi" . "xdvi %s")
  1509. ("fig" . "xfig %s"))
  1510. "Default file applications on a MacOS X system.
  1511. The system \"open\" is known as a default, but we use X11 applications
  1512. for some files for which the OS does not have a good default.
  1513. See `org-file-apps'.")
  1514. (defconst org-file-apps-defaults-windowsnt
  1515. (list
  1516. '(remote . emacs)
  1517. (cons t
  1518. (list (if (featurep 'xemacs)
  1519. 'mswindows-shell-execute
  1520. 'w32-shell-execute)
  1521. "open" 'file))
  1522. (cons 'system
  1523. (list (if (featurep 'xemacs)
  1524. 'mswindows-shell-execute
  1525. 'w32-shell-execute)
  1526. "open" 'file)))
  1527. "Default file applications on a Windows NT system.
  1528. The system \"open\" is used for most files.
  1529. See `org-file-apps'.")
  1530. (defcustom org-file-apps
  1531. '(
  1532. (auto-mode . emacs)
  1533. ("\\.mm\\'" . default)
  1534. ("\\.x?html?\\'" . default)
  1535. ("\\.pdf\\'" . default)
  1536. )
  1537. "External applications for opening `file:path' items in a document.
  1538. Org-mode uses system defaults for different file types, but
  1539. you can use this variable to set the application for a given file
  1540. extension. The entries in this list are cons cells where the car identifies
  1541. files and the cdr the corresponding command. Possible values for the
  1542. file identifier are
  1543. \"string\" A string as a file identifier can be interpreted in different
  1544. ways, depending on its contents:
  1545. - Alphanumeric characters only:
  1546. Match links with this file extension.
  1547. Example: (\"pdf\" . \"evince %s\")
  1548. to open PDFs with evince.
  1549. - Regular expression: Match links where the
  1550. filename matches the regexp. If you want to
  1551. use groups here, use shy groups.
  1552. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1553. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1554. to open *.html and *.xhtml with firefox.
  1555. - Regular expression which contains (non-shy) groups:
  1556. Match links where the whole link, including \"::\", and
  1557. anything after that, matches the regexp.
  1558. In a custom command string, %1, %2, etc. are replaced with
  1559. the parts of the link that were matched by the groups.
  1560. For backwards compatibility, if a command string is given
  1561. that does not use any of the group matches, this case is
  1562. handled identically to the second one (i.e. match against
  1563. file name only).
  1564. In a custom lisp form, you can access the group matches with
  1565. (match-string n link).
  1566. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1567. to open [[file:document.pdf::5]] with evince at page 5.
  1568. `directory' Matches a directory
  1569. `remote' Matches a remote file, accessible through tramp or efs.
  1570. Remote files most likely should be visited through Emacs
  1571. because external applications cannot handle such paths.
  1572. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1573. so all files Emacs knows how to handle. Using this with
  1574. command `emacs' will open most files in Emacs. Beware that this
  1575. will also open html files inside Emacs, unless you add
  1576. (\"html\" . default) to the list as well.
  1577. t Default for files not matched by any of the other options.
  1578. `system' The system command to open files, like `open' on Windows
  1579. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1580. that will be selected if you call `C-c C-o' with a double
  1581. \\[universal-argument] \\[universal-argument] prefix.
  1582. Possible values for the command are:
  1583. `emacs' The file will be visited by the current Emacs process.
  1584. `default' Use the default application for this file type, which is the
  1585. association for t in the list, most likely in the system-specific
  1586. part.
  1587. This can be used to overrule an unwanted setting in the
  1588. system-specific variable.
  1589. `system' Use the system command for opening files, like \"open\".
  1590. This command is specified by the entry whose car is `system'.
  1591. Most likely, the system-specific version of this variable
  1592. does define this command, but you can overrule/replace it
  1593. here.
  1594. string A command to be executed by a shell; %s will be replaced
  1595. by the path to the file.
  1596. sexp A Lisp form which will be evaluated. The file path will
  1597. be available in the Lisp variable `file'.
  1598. For more examples, see the system specific constants
  1599. `org-file-apps-defaults-macosx'
  1600. `org-file-apps-defaults-windowsnt'
  1601. `org-file-apps-defaults-gnu'."
  1602. :group 'org-link-follow
  1603. :type '(repeat
  1604. (cons (choice :value ""
  1605. (string :tag "Extension")
  1606. (const :tag "System command to open files" system)
  1607. (const :tag "Default for unrecognized files" t)
  1608. (const :tag "Remote file" remote)
  1609. (const :tag "Links to a directory" directory)
  1610. (const :tag "Any files that have Emacs modes"
  1611. auto-mode))
  1612. (choice :value ""
  1613. (const :tag "Visit with Emacs" emacs)
  1614. (const :tag "Use default" default)
  1615. (const :tag "Use the system command" system)
  1616. (string :tag "Command")
  1617. (sexp :tag "Lisp form")))))
  1618. (defgroup org-refile nil
  1619. "Options concerning refiling entries in Org-mode."
  1620. :tag "Org Refile"
  1621. :group 'org)
  1622. (defcustom org-directory "~/org"
  1623. "Directory with org files.
  1624. This is just a default location to look for Org files. There is no need
  1625. at all to put your files into this directory. It is only used in the
  1626. following situations:
  1627. 1. When a remember template specifies a target file that is not an
  1628. absolute path. The path will then be interpreted relative to
  1629. `org-directory'
  1630. 2. When a remember note is filed away in an interactive way (when exiting the
  1631. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1632. with `org-directory' as the default path."
  1633. :group 'org-refile
  1634. :group 'org-remember
  1635. :type 'directory)
  1636. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1637. "Default target for storing notes.
  1638. Used as a fall back file for org-remember.el and org-capture.el, for
  1639. templates that do not specify a target file."
  1640. :group 'org-refile
  1641. :group 'org-remember
  1642. :type '(choice
  1643. (const :tag "Default from remember-data-file" nil)
  1644. file))
  1645. (defcustom org-goto-interface 'outline
  1646. "The default interface to be used for `org-goto'.
  1647. Allowed values are:
  1648. outline The interface shows an outline of the relevant file
  1649. and the correct heading is found by moving through
  1650. the outline or by searching with incremental search.
  1651. outline-path-completion Headlines in the current buffer are offered via
  1652. completion. This is the interface also used by
  1653. the refile command."
  1654. :group 'org-refile
  1655. :type '(choice
  1656. (const :tag "Outline" outline)
  1657. (const :tag "Outline-path-completion" outline-path-completion)))
  1658. (defcustom org-goto-max-level 5
  1659. "Maximum target level when running `org-goto' with refile interface."
  1660. :group 'org-refile
  1661. :type 'integer)
  1662. (defcustom org-reverse-note-order nil
  1663. "Non-nil means store new notes at the beginning of a file or entry.
  1664. When nil, new notes will be filed to the end of a file or entry.
  1665. This can also be a list with cons cells of regular expressions that
  1666. are matched against file names, and values."
  1667. :group 'org-remember
  1668. :group 'org-refile
  1669. :type '(choice
  1670. (const :tag "Reverse always" t)
  1671. (const :tag "Reverse never" nil)
  1672. (repeat :tag "By file name regexp"
  1673. (cons regexp boolean))))
  1674. (defcustom org-log-refile nil
  1675. "Information to record when a task is refiled.
  1676. Possible values are:
  1677. nil Don't add anything
  1678. time Add a time stamp to the task
  1679. note Prompt for a note and add it with template `org-log-note-headings'
  1680. This option can also be set with on a per-file-basis with
  1681. #+STARTUP: nologrefile
  1682. #+STARTUP: logrefile
  1683. #+STARTUP: lognoterefile
  1684. You can have local logging settings for a subtree by setting the LOGGING
  1685. property to one or more of these keywords.
  1686. When bulk-refiling from the agenda, the value `note' is forbidden and
  1687. will temporarily be changed to `time'."
  1688. :group 'org-refile
  1689. :group 'org-progress
  1690. :version "24.1"
  1691. :type '(choice
  1692. (const :tag "No logging" nil)
  1693. (const :tag "Record timestamp" time)
  1694. (const :tag "Record timestamp with note." note)))
  1695. (defcustom org-refile-targets nil
  1696. "Targets for refiling entries with \\[org-refile].
  1697. This is a list of cons cells. Each cell contains:
  1698. - a specification of the files to be considered, either a list of files,
  1699. or a symbol whose function or variable value will be used to retrieve
  1700. a file name or a list of file names. If you use `org-agenda-files' for
  1701. that, all agenda files will be scanned for targets. Nil means consider
  1702. headings in the current buffer.
  1703. - A specification of how to find candidate refile targets. This may be
  1704. any of:
  1705. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1706. This tag has to be present in all target headlines, inheritance will
  1707. not be considered.
  1708. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1709. todo keyword.
  1710. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1711. headlines that are refiling targets.
  1712. - a cons cell (:level . N). Any headline of level N is considered a target.
  1713. Note that, when `org-odd-levels-only' is set, level corresponds to
  1714. order in hierarchy, not to the number of stars.
  1715. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1716. Note that, when `org-odd-levels-only' is set, level corresponds to
  1717. order in hierarchy, not to the number of stars.
  1718. Each element of this list generates a set of possible targets.
  1719. The union of these sets is presented (with completion) to
  1720. the user by `org-refile'.
  1721. You can set the variable `org-refile-target-verify-function' to a function
  1722. to verify each headline found by the simple criteria above.
  1723. When this variable is nil, all top-level headlines in the current buffer
  1724. are used, equivalent to the value `((nil . (:level . 1))'."
  1725. :group 'org-refile
  1726. :type '(repeat
  1727. (cons
  1728. (choice :value org-agenda-files
  1729. (const :tag "All agenda files" org-agenda-files)
  1730. (const :tag "Current buffer" nil)
  1731. (function) (variable) (file))
  1732. (choice :tag "Identify target headline by"
  1733. (cons :tag "Specific tag" (const :value :tag) (string))
  1734. (cons :tag "TODO keyword" (const :value :todo) (string))
  1735. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1736. (cons :tag "Level number" (const :value :level) (integer))
  1737. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1738. (defcustom org-refile-target-verify-function nil
  1739. "Function to verify if the headline at point should be a refile target.
  1740. The function will be called without arguments, with point at the
  1741. beginning of the headline. It should return t and leave point
  1742. where it is if the headline is a valid target for refiling.
  1743. If the target should not be selected, the function must return nil.
  1744. In addition to this, it may move point to a place from where the search
  1745. should be continued. For example, the function may decide that the entire
  1746. subtree of the current entry should be excluded and move point to the end
  1747. of the subtree."
  1748. :group 'org-refile
  1749. :type 'function)
  1750. (defcustom org-refile-use-cache nil
  1751. "Non-nil means cache refile targets to speed up the process.
  1752. The cache for a particular file will be updated automatically when
  1753. the buffer has been killed, or when any of the marker used for flagging
  1754. refile targets no longer points at a live buffer.
  1755. If you have added new entries to a buffer that might themselves be targets,
  1756. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  1757. find that easier, `C-u C-u C-u C-c C-w'."
  1758. :group 'org-refile
  1759. :version "24.1"
  1760. :type 'boolean)
  1761. (defcustom org-refile-use-outline-path nil
  1762. "Non-nil means provide refile targets as paths.
  1763. So a level 3 headline will be available as level1/level2/level3.
  1764. When the value is `file', also include the file name (without directory)
  1765. into the path. In this case, you can also stop the completion after
  1766. the file name, to get entries inserted as top level in the file.
  1767. When `full-file-path', include the full file path."
  1768. :group 'org-refile
  1769. :type '(choice
  1770. (const :tag "Not" nil)
  1771. (const :tag "Yes" t)
  1772. (const :tag "Start with file name" file)
  1773. (const :tag "Start with full file path" full-file-path)))
  1774. (defcustom org-outline-path-complete-in-steps t
  1775. "Non-nil means complete the outline path in hierarchical steps.
  1776. When Org-mode uses the refile interface to select an outline path
  1777. \(see variable `org-refile-use-outline-path'), the completion of
  1778. the path can be done is a single go, or if can be done in steps down
  1779. the headline hierarchy. Going in steps is probably the best if you
  1780. do not use a special completion package like `ido' or `icicles'.
  1781. However, when using these packages, going in one step can be very
  1782. fast, while still showing the whole path to the entry."
  1783. :group 'org-refile
  1784. :type 'boolean)
  1785. (defcustom org-refile-allow-creating-parent-nodes nil
  1786. "Non-nil means allow to create new nodes as refile targets.
  1787. New nodes are then created by adding \"/new node name\" to the completion
  1788. of an existing node. When the value of this variable is `confirm',
  1789. new node creation must be confirmed by the user (recommended)
  1790. When nil, the completion must match an existing entry.
  1791. Note that, if the new heading is not seen by the criteria
  1792. listed in `org-refile-targets', multiple instances of the same
  1793. heading would be created by trying again to file under the new
  1794. heading."
  1795. :group 'org-refile
  1796. :type '(choice
  1797. (const :tag "Never" nil)
  1798. (const :tag "Always" t)
  1799. (const :tag "Prompt for confirmation" confirm)))
  1800. (defcustom org-refile-active-region-within-subtree nil
  1801. "Non-nil means also refile active region within a subtree.
  1802. By default `org-refile' doesn't allow refiling regions if they
  1803. don't contain a set of subtrees, but it might be convenient to
  1804. do so sometimes: in that case, the first line of the region is
  1805. converted to a headline before refiling."
  1806. :group 'org-refile
  1807. :version "24.1"
  1808. :type 'boolean)
  1809. (defgroup org-todo nil
  1810. "Options concerning TODO items in Org-mode."
  1811. :tag "Org TODO"
  1812. :group 'org)
  1813. (defgroup org-progress nil
  1814. "Options concerning Progress logging in Org-mode."
  1815. :tag "Org Progress"
  1816. :group 'org-time)
  1817. (defvar org-todo-interpretation-widgets
  1818. '((:tag "Sequence (cycling hits every state)" sequence)
  1819. (:tag "Type (cycling directly to DONE)" type))
  1820. "The available interpretation symbols for customizing `org-todo-keywords'.
  1821. Interested libraries should add to this list.")
  1822. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1823. "List of TODO entry keyword sequences and their interpretation.
  1824. \\<org-mode-map>This is a list of sequences.
  1825. Each sequence starts with a symbol, either `sequence' or `type',
  1826. indicating if the keywords should be interpreted as a sequence of
  1827. action steps, or as different types of TODO items. The first
  1828. keywords are states requiring action - these states will select a headline
  1829. for inclusion into the global TODO list Org-mode produces. If one of
  1830. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  1831. signify that no further action is necessary. If \"|\" is not found,
  1832. the last keyword is treated as the only DONE state of the sequence.
  1833. The command \\[org-todo] cycles an entry through these states, and one
  1834. additional state where no keyword is present. For details about this
  1835. cycling, see the manual.
  1836. TODO keywords and interpretation can also be set on a per-file basis with
  1837. the special #+SEQ_TODO and #+TYP_TODO lines.
  1838. Each keyword can optionally specify a character for fast state selection
  1839. \(in combination with the variable `org-use-fast-todo-selection')
  1840. and specifiers for state change logging, using the same syntax
  1841. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1842. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1843. indicates to record a time stamp each time this state is selected.
  1844. Each keyword may also specify if a timestamp or a note should be
  1845. recorded when entering or leaving the state, by adding additional
  1846. characters in the parenthesis after the keyword. This looks like this:
  1847. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1848. record only the time of the state change. With X and Y being either
  1849. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1850. Y when leaving the state if and only if the *target* state does not
  1851. define X. You may omit any of the fast-selection key or X or /Y,
  1852. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1853. For backward compatibility, this variable may also be just a list
  1854. of keywords - in this case the interpretation (sequence or type) will be
  1855. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1856. :group 'org-todo
  1857. :group 'org-keywords
  1858. :type '(choice
  1859. (repeat :tag "Old syntax, just keywords"
  1860. (string :tag "Keyword"))
  1861. (repeat :tag "New syntax"
  1862. (cons
  1863. (choice
  1864. :tag "Interpretation"
  1865. ;;Quick and dirty way to see
  1866. ;;`org-todo-interpretations'. This takes the
  1867. ;;place of item arguments
  1868. :convert-widget
  1869. (lambda (widget)
  1870. (widget-put widget
  1871. :args (mapcar
  1872. #'(lambda (x)
  1873. (widget-convert
  1874. (cons 'const x)))
  1875. org-todo-interpretation-widgets))
  1876. widget))
  1877. (repeat
  1878. (string :tag "Keyword"))))))
  1879. (defvar org-todo-keywords-1 nil
  1880. "All TODO and DONE keywords active in a buffer.")
  1881. (make-variable-buffer-local 'org-todo-keywords-1)
  1882. (defvar org-todo-keywords-for-agenda nil)
  1883. (defvar org-done-keywords-for-agenda nil)
  1884. (defvar org-drawers-for-agenda nil)
  1885. (defvar org-todo-keyword-alist-for-agenda nil)
  1886. (defvar org-tag-alist-for-agenda nil)
  1887. (defvar org-agenda-contributing-files nil)
  1888. (defvar org-not-done-keywords nil)
  1889. (make-variable-buffer-local 'org-not-done-keywords)
  1890. (defvar org-done-keywords nil)
  1891. (make-variable-buffer-local 'org-done-keywords)
  1892. (defvar org-todo-heads nil)
  1893. (make-variable-buffer-local 'org-todo-heads)
  1894. (defvar org-todo-sets nil)
  1895. (make-variable-buffer-local 'org-todo-sets)
  1896. (defvar org-todo-log-states nil)
  1897. (make-variable-buffer-local 'org-todo-log-states)
  1898. (defvar org-todo-kwd-alist nil)
  1899. (make-variable-buffer-local 'org-todo-kwd-alist)
  1900. (defvar org-todo-key-alist nil)
  1901. (make-variable-buffer-local 'org-todo-key-alist)
  1902. (defvar org-todo-key-trigger nil)
  1903. (make-variable-buffer-local 'org-todo-key-trigger)
  1904. (defcustom org-todo-interpretation 'sequence
  1905. "Controls how TODO keywords are interpreted.
  1906. This variable is in principle obsolete and is only used for
  1907. backward compatibility, if the interpretation of todo keywords is
  1908. not given already in `org-todo-keywords'. See that variable for
  1909. more information."
  1910. :group 'org-todo
  1911. :group 'org-keywords
  1912. :type '(choice (const sequence)
  1913. (const type)))
  1914. (defcustom org-use-fast-todo-selection t
  1915. "Non-nil means use the fast todo selection scheme with C-c C-t.
  1916. This variable describes if and under what circumstances the cycling
  1917. mechanism for TODO keywords will be replaced by a single-key, direct
  1918. selection scheme.
  1919. When nil, fast selection is never used.
  1920. When the symbol `prefix', it will be used when `org-todo' is called with
  1921. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1922. in an agenda buffer.
  1923. When t, fast selection is used by default. In this case, the prefix
  1924. argument forces cycling instead.
  1925. In all cases, the special interface is only used if access keys have actually
  1926. been assigned by the user, i.e. if keywords in the configuration are followed
  1927. by a letter in parenthesis, like TODO(t)."
  1928. :group 'org-todo
  1929. :type '(choice
  1930. (const :tag "Never" nil)
  1931. (const :tag "By default" t)
  1932. (const :tag "Only with C-u C-c C-t" prefix)))
  1933. (defcustom org-provide-todo-statistics t
  1934. "Non-nil means update todo statistics after insert and toggle.
  1935. ALL-HEADLINES means update todo statistics by including headlines
  1936. with no TODO keyword as well, counting them as not done.
  1937. A list of TODO keywords means the same, but skip keywords that are
  1938. not in this list.
  1939. When this is set, todo statistics is updated in the parent of the
  1940. current entry each time a todo state is changed."
  1941. :group 'org-todo
  1942. :type '(choice
  1943. (const :tag "Yes, only for TODO entries" t)
  1944. (const :tag "Yes, including all entries" 'all-headlines)
  1945. (repeat :tag "Yes, for TODOs in this list"
  1946. (string :tag "TODO keyword"))
  1947. (other :tag "No TODO statistics" nil)))
  1948. (defcustom org-hierarchical-todo-statistics t
  1949. "Non-nil means TODO statistics covers just direct children.
  1950. When nil, all entries in the subtree are considered.
  1951. This has only an effect if `org-provide-todo-statistics' is set.
  1952. To set this to nil for only a single subtree, use a COOKIE_DATA
  1953. property and include the word \"recursive\" into the value."
  1954. :group 'org-todo
  1955. :type 'boolean)
  1956. (defcustom org-after-todo-state-change-hook nil
  1957. "Hook which is run after the state of a TODO item was changed.
  1958. The new state (a string with a TODO keyword, or nil) is available in the
  1959. Lisp variable `org-state'."
  1960. :group 'org-todo
  1961. :type 'hook)
  1962. (defvar org-blocker-hook nil
  1963. "Hook for functions that are allowed to block a state change.
  1964. Each function gets as its single argument a property list, see
  1965. `org-trigger-hook' for more information about this list.
  1966. If any of the functions in this hook returns nil, the state change
  1967. is blocked.")
  1968. (defvar org-trigger-hook nil
  1969. "Hook for functions that are triggered by a state change.
  1970. Each function gets as its single argument a property list with at least
  1971. the following elements:
  1972. (:type type-of-change :position pos-at-entry-start
  1973. :from old-state :to new-state)
  1974. Depending on the type, more properties may be present.
  1975. This mechanism is currently implemented for:
  1976. TODO state changes
  1977. ------------------
  1978. :type todo-state-change
  1979. :from previous state (keyword as a string), or nil, or a symbol
  1980. 'todo' or 'done', to indicate the general type of state.
  1981. :to new state, like in :from")
  1982. (defcustom org-enforce-todo-dependencies nil
  1983. "Non-nil means undone TODO entries will block switching the parent to DONE.
  1984. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1985. be blocked if any prior sibling is not yet done.
  1986. Finally, if the parent is blocked because of ordered siblings of its own,
  1987. the child will also be blocked."
  1988. :set (lambda (var val)
  1989. (set var val)
  1990. (if val
  1991. (add-hook 'org-blocker-hook
  1992. 'org-block-todo-from-children-or-siblings-or-parent)
  1993. (remove-hook 'org-blocker-hook
  1994. 'org-block-todo-from-children-or-siblings-or-parent)))
  1995. :group 'org-todo
  1996. :type 'boolean)
  1997. (defcustom org-enforce-todo-checkbox-dependencies nil
  1998. "Non-nil means unchecked boxes will block switching the parent to DONE.
  1999. When this is nil, checkboxes have no influence on switching TODO states.
  2000. When non-nil, you first need to check off all check boxes before the TODO
  2001. entry can be switched to DONE.
  2002. This variable needs to be set before org.el is loaded, and you need to
  2003. restart Emacs after a change to make the change effective. The only way
  2004. to change is while Emacs is running is through the customize interface."
  2005. :set (lambda (var val)
  2006. (set var val)
  2007. (if val
  2008. (add-hook 'org-blocker-hook
  2009. 'org-block-todo-from-checkboxes)
  2010. (remove-hook 'org-blocker-hook
  2011. 'org-block-todo-from-checkboxes)))
  2012. :group 'org-todo
  2013. :type 'boolean)
  2014. (defcustom org-treat-insert-todo-heading-as-state-change nil
  2015. "Non-nil means inserting a TODO heading is treated as state change.
  2016. So when the command \\[org-insert-todo-heading] is used, state change
  2017. logging will apply if appropriate. When nil, the new TODO item will
  2018. be inserted directly, and no logging will take place."
  2019. :group 'org-todo
  2020. :type 'boolean)
  2021. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  2022. "Non-nil means switching TODO states with S-cursor counts as state change.
  2023. This is the default behavior. However, setting this to nil allows a
  2024. convenient way to select a TODO state and bypass any logging associated
  2025. with that."
  2026. :group 'org-todo
  2027. :type 'boolean)
  2028. (defcustom org-todo-state-tags-triggers nil
  2029. "Tag changes that should be triggered by TODO state changes.
  2030. This is a list. Each entry is
  2031. (state-change (tag . flag) .......)
  2032. State-change can be a string with a state, and empty string to indicate the
  2033. state that has no TODO keyword, or it can be one of the symbols `todo'
  2034. or `done', meaning any not-done or done state, respectively."
  2035. :group 'org-todo
  2036. :group 'org-tags
  2037. :type '(repeat
  2038. (cons (choice :tag "When changing to"
  2039. (const :tag "Not-done state" todo)
  2040. (const :tag "Done state" done)
  2041. (string :tag "State"))
  2042. (repeat
  2043. (cons :tag "Tag action"
  2044. (string :tag "Tag")
  2045. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  2046. (defcustom org-log-done nil
  2047. "Information to record when a task moves to the DONE state.
  2048. Possible values are:
  2049. nil Don't add anything, just change the keyword
  2050. time Add a time stamp to the task
  2051. note Prompt for a note and add it with template `org-log-note-headings'
  2052. This option can also be set with on a per-file-basis with
  2053. #+STARTUP: nologdone
  2054. #+STARTUP: logdone
  2055. #+STARTUP: lognotedone
  2056. You can have local logging settings for a subtree by setting the LOGGING
  2057. property to one or more of these keywords."
  2058. :group 'org-todo
  2059. :group 'org-progress
  2060. :type '(choice
  2061. (const :tag "No logging" nil)
  2062. (const :tag "Record CLOSED timestamp" time)
  2063. (const :tag "Record CLOSED timestamp with note." note)))
  2064. ;; Normalize old uses of org-log-done.
  2065. (cond
  2066. ((eq org-log-done t) (setq org-log-done 'time))
  2067. ((and (listp org-log-done) (memq 'done org-log-done))
  2068. (setq org-log-done 'note)))
  2069. (defcustom org-log-reschedule nil
  2070. "Information to record when the scheduling date of a tasks is modified.
  2071. Possible values are:
  2072. nil Don't add anything, just change the date
  2073. time Add a time stamp to the task
  2074. note Prompt for a note and add it with template `org-log-note-headings'
  2075. This option can also be set with on a per-file-basis with
  2076. #+STARTUP: nologreschedule
  2077. #+STARTUP: logreschedule
  2078. #+STARTUP: lognotereschedule"
  2079. :group 'org-todo
  2080. :group 'org-progress
  2081. :type '(choice
  2082. (const :tag "No logging" nil)
  2083. (const :tag "Record timestamp" time)
  2084. (const :tag "Record timestamp with note." note)))
  2085. (defcustom org-log-redeadline nil
  2086. "Information to record when the deadline date of a tasks is modified.
  2087. Possible values are:
  2088. nil Don't add anything, just change the date
  2089. time Add a time stamp to the task
  2090. note Prompt for a note and add it with template `org-log-note-headings'
  2091. This option can also be set with on a per-file-basis with
  2092. #+STARTUP: nologredeadline
  2093. #+STARTUP: logredeadline
  2094. #+STARTUP: lognoteredeadline
  2095. You can have local logging settings for a subtree by setting the LOGGING
  2096. property to one or more of these keywords."
  2097. :group 'org-todo
  2098. :group 'org-progress
  2099. :type '(choice
  2100. (const :tag "No logging" nil)
  2101. (const :tag "Record timestamp" time)
  2102. (const :tag "Record timestamp with note." note)))
  2103. (defcustom org-log-note-clock-out nil
  2104. "Non-nil means record a note when clocking out of an item.
  2105. This can also be configured on a per-file basis by adding one of
  2106. the following lines anywhere in the buffer:
  2107. #+STARTUP: lognoteclock-out
  2108. #+STARTUP: nolognoteclock-out"
  2109. :group 'org-todo
  2110. :group 'org-progress
  2111. :type 'boolean)
  2112. (defcustom org-log-done-with-time t
  2113. "Non-nil means the CLOSED time stamp will contain date and time.
  2114. When nil, only the date will be recorded."
  2115. :group 'org-progress
  2116. :type 'boolean)
  2117. (defcustom org-log-note-headings
  2118. '((done . "CLOSING NOTE %t")
  2119. (state . "State %-12s from %-12S %t")
  2120. (note . "Note taken on %t")
  2121. (reschedule . "Rescheduled from %S on %t")
  2122. (delschedule . "Not scheduled, was %S on %t")
  2123. (redeadline . "New deadline from %S on %t")
  2124. (deldeadline . "Removed deadline, was %S on %t")
  2125. (refile . "Refiled on %t")
  2126. (clock-out . ""))
  2127. "Headings for notes added to entries.
  2128. The value is an alist, with the car being a symbol indicating the note
  2129. context, and the cdr is the heading to be used. The heading may also be the
  2130. empty string.
  2131. %t in the heading will be replaced by a time stamp.
  2132. %T will be an active time stamp instead the default inactive one
  2133. %d will be replaced by a short-format time stamp.
  2134. %D will be replaced by an active short-format time stamp.
  2135. %s will be replaced by the new TODO state, in double quotes.
  2136. %S will be replaced by the old TODO state, in double quotes.
  2137. %u will be replaced by the user name.
  2138. %U will be replaced by the full user name.
  2139. In fact, it is not a good idea to change the `state' entry, because
  2140. agenda log mode depends on the format of these entries."
  2141. :group 'org-todo
  2142. :group 'org-progress
  2143. :type '(list :greedy t
  2144. (cons (const :tag "Heading when closing an item" done) string)
  2145. (cons (const :tag
  2146. "Heading when changing todo state (todo sequence only)"
  2147. state) string)
  2148. (cons (const :tag "Heading when just taking a note" note) string)
  2149. (cons (const :tag "Heading when clocking out" clock-out) string)
  2150. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2151. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2152. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2153. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2154. (cons (const :tag "Heading when refiling" refile) string)))
  2155. (unless (assq 'note org-log-note-headings)
  2156. (push '(note . "%t") org-log-note-headings))
  2157. (defcustom org-log-into-drawer nil
  2158. "Non-nil means insert state change notes and time stamps into a drawer.
  2159. When nil, state changes notes will be inserted after the headline and
  2160. any scheduling and clock lines, but not inside a drawer.
  2161. The value of this variable should be the name of the drawer to use.
  2162. LOGBOOK is proposed as the default drawer for this purpose, you can
  2163. also set this to a string to define the drawer of your choice.
  2164. A value of t is also allowed, representing \"LOGBOOK\".
  2165. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2166. will be ignored.
  2167. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2168. a subtree."
  2169. :group 'org-todo
  2170. :group 'org-progress
  2171. :type '(choice
  2172. (const :tag "Not into a drawer" nil)
  2173. (const :tag "LOGBOOK" t)
  2174. (string :tag "Other")))
  2175. (if (fboundp 'defvaralias)
  2176. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  2177. (defun org-log-into-drawer ()
  2178. "Return the value of `org-log-into-drawer', but let properties overrule.
  2179. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  2180. used instead of the default value."
  2181. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit)))
  2182. (cond
  2183. ((or (not p) (equal p "nil")) org-log-into-drawer)
  2184. ((equal p "t") "LOGBOOK")
  2185. (t p))))
  2186. (defcustom org-log-state-notes-insert-after-drawers nil
  2187. "Non-nil means insert state change notes after any drawers in entry.
  2188. Only the drawers that *immediately* follow the headline and the
  2189. deadline/scheduled line are skipped.
  2190. When nil, insert notes right after the heading and perhaps the line
  2191. with deadline/scheduling if present.
  2192. This variable will have no effect if `org-log-into-drawer' is
  2193. set."
  2194. :group 'org-todo
  2195. :group 'org-progress
  2196. :type 'boolean)
  2197. (defcustom org-log-states-order-reversed t
  2198. "Non-nil means the latest state note will be directly after heading.
  2199. When nil, the state change notes will be ordered according to time."
  2200. :group 'org-todo
  2201. :group 'org-progress
  2202. :type 'boolean)
  2203. (defcustom org-todo-repeat-to-state nil
  2204. "The TODO state to which a repeater should return the repeating task.
  2205. By default this is the first task in a TODO sequence, or the previous state
  2206. in a TODO_TYP set. But you can specify another task here.
  2207. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2208. :group 'org-todo
  2209. :version "24.1"
  2210. :type '(choice (const :tag "Head of sequence" nil)
  2211. (string :tag "Specific state")))
  2212. (defcustom org-log-repeat 'time
  2213. "Non-nil means record moving through the DONE state when triggering repeat.
  2214. An auto-repeating task is immediately switched back to TODO when
  2215. marked DONE. If you are not logging state changes (by adding \"@\"
  2216. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2217. record a closing note, there will be no record of the task moving
  2218. through DONE. This variable forces taking a note anyway.
  2219. nil Don't force a record
  2220. time Record a time stamp
  2221. note Record a note
  2222. This option can also be set with on a per-file-basis with
  2223. #+STARTUP: logrepeat
  2224. #+STARTUP: lognoterepeat
  2225. #+STARTUP: nologrepeat
  2226. You can have local logging settings for a subtree by setting the LOGGING
  2227. property to one or more of these keywords."
  2228. :group 'org-todo
  2229. :group 'org-progress
  2230. :type '(choice
  2231. (const :tag "Don't force a record" nil)
  2232. (const :tag "Force recording the DONE state" time)
  2233. (const :tag "Force recording a note with the DONE state" note)))
  2234. (defgroup org-priorities nil
  2235. "Priorities in Org-mode."
  2236. :tag "Org Priorities"
  2237. :group 'org-todo)
  2238. (defcustom org-enable-priority-commands t
  2239. "Non-nil means priority commands are active.
  2240. When nil, these commands will be disabled, so that you never accidentally
  2241. set a priority."
  2242. :group 'org-priorities
  2243. :type 'boolean)
  2244. (defcustom org-highest-priority ?A
  2245. "The highest priority of TODO items. A character like ?A, ?B etc.
  2246. Must have a smaller ASCII number than `org-lowest-priority'."
  2247. :group 'org-priorities
  2248. :type 'character)
  2249. (defcustom org-lowest-priority ?C
  2250. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2251. Must have a larger ASCII number than `org-highest-priority'."
  2252. :group 'org-priorities
  2253. :type 'character)
  2254. (defcustom org-default-priority ?B
  2255. "The default priority of TODO items.
  2256. This is the priority an item gets if no explicit priority is given.
  2257. When starting to cycle on an empty priority the first step in the cycle
  2258. depends on `org-priority-start-cycle-with-default'. The resulting first
  2259. step priority must not exceed the range from `org-highest-priority' to
  2260. `org-lowest-priority' which means that `org-default-priority' has to be
  2261. in this range exclusive or inclusive the range boundaries. Else the
  2262. first step refuses to set the default and the second will fall back
  2263. to (depending on the command used) the highest or lowest priority."
  2264. :group 'org-priorities
  2265. :type 'character)
  2266. (defcustom org-priority-start-cycle-with-default t
  2267. "Non-nil means start with default priority when starting to cycle.
  2268. When this is nil, the first step in the cycle will be (depending on the
  2269. command used) one higher or lower than the default priority.
  2270. See also `org-default-priority'."
  2271. :group 'org-priorities
  2272. :type 'boolean)
  2273. (defcustom org-get-priority-function nil
  2274. "Function to extract the priority from a string.
  2275. The string is normally the headline. If this is nil Org computes the
  2276. priority from the priority cookie like [#A] in the headline. It returns
  2277. an integer, increasing by 1000 for each priority level.
  2278. The user can set a different function here, which should take a string
  2279. as an argument and return the numeric priority."
  2280. :group 'org-priorities
  2281. :version "24.1"
  2282. :type 'function)
  2283. (defgroup org-time nil
  2284. "Options concerning time stamps and deadlines in Org-mode."
  2285. :tag "Org Time"
  2286. :group 'org)
  2287. (defcustom org-insert-labeled-timestamps-at-point nil
  2288. "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
  2289. When nil, these labeled time stamps are forces into the second line of an
  2290. entry, just after the headline. When scheduling from the global TODO list,
  2291. the time stamp will always be forced into the second line."
  2292. :group 'org-time
  2293. :type 'boolean)
  2294. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  2295. "Formats for `format-time-string' which are used for time stamps.
  2296. It is not recommended to change this constant.")
  2297. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2298. "Number of minutes to round time stamps to.
  2299. These are two values, the first applies when first creating a time stamp.
  2300. The second applies when changing it with the commands `S-up' and `S-down'.
  2301. When changing the time stamp, this means that it will change in steps
  2302. of N minutes, as given by the second value.
  2303. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2304. numbers should be factors of 60, so for example 5, 10, 15.
  2305. When this is larger than 1, you can still force an exact time stamp by using
  2306. a double prefix argument to a time stamp command like `C-c .' or `C-c !',
  2307. and by using a prefix arg to `S-up/down' to specify the exact number
  2308. of minutes to shift."
  2309. :group 'org-time
  2310. :get #'(lambda (var) ; Make sure both elements are there
  2311. (if (integerp (default-value var))
  2312. (list (default-value var) 5)
  2313. (default-value var)))
  2314. :type '(list
  2315. (integer :tag "when inserting times")
  2316. (integer :tag "when modifying times")))
  2317. ;; Normalize old customizations of this variable.
  2318. (when (integerp org-time-stamp-rounding-minutes)
  2319. (setq org-time-stamp-rounding-minutes
  2320. (list org-time-stamp-rounding-minutes
  2321. org-time-stamp-rounding-minutes)))
  2322. (defcustom org-display-custom-times nil
  2323. "Non-nil means overlay custom formats over all time stamps.
  2324. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2325. To turn this on on a per-file basis, insert anywhere in the file:
  2326. #+STARTUP: customtime"
  2327. :group 'org-time
  2328. :set 'set-default
  2329. :type 'sexp)
  2330. (make-variable-buffer-local 'org-display-custom-times)
  2331. (defcustom org-time-stamp-custom-formats
  2332. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2333. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2334. These are overlaid over the default ISO format if the variable
  2335. `org-display-custom-times' is set. Time like %H:%M should be at the
  2336. end of the second format. The custom formats are also honored by export
  2337. commands, if custom time display is turned on at the time of export."
  2338. :group 'org-time
  2339. :type 'sexp)
  2340. (defun org-time-stamp-format (&optional long inactive)
  2341. "Get the right format for a time string."
  2342. (let ((f (if long (cdr org-time-stamp-formats)
  2343. (car org-time-stamp-formats))))
  2344. (if inactive
  2345. (concat "[" (substring f 1 -1) "]")
  2346. f)))
  2347. (defcustom org-time-clocksum-format "%d:%02d"
  2348. "The format string used when creating CLOCKSUM lines.
  2349. This is also used when org-mode generates a time duration."
  2350. :group 'org-time
  2351. :type 'string)
  2352. (defcustom org-time-clocksum-use-fractional nil
  2353. "If non-nil, \\[org-clock-display] uses fractional times.
  2354. org-mode generates a time duration."
  2355. :group 'org-time
  2356. :type 'boolean)
  2357. (defcustom org-time-clocksum-fractional-format "%.2f"
  2358. "The format string used when creating CLOCKSUM lines, or when
  2359. org-mode generates a time duration."
  2360. :group 'org-time
  2361. :type 'string)
  2362. (defcustom org-deadline-warning-days 14
  2363. "No. of days before expiration during which a deadline becomes active.
  2364. This variable governs the display in sparse trees and in the agenda.
  2365. When 0 or negative, it means use this number (the absolute value of it)
  2366. even if a deadline has a different individual lead time specified.
  2367. Custom commands can set this variable in the options section."
  2368. :group 'org-time
  2369. :group 'org-agenda-daily/weekly
  2370. :type 'integer)
  2371. (defcustom org-read-date-prefer-future t
  2372. "Non-nil means assume future for incomplete date input from user.
  2373. This affects the following situations:
  2374. 1. The user gives a month but not a year.
  2375. For example, if it is April and you enter \"feb 2\", this will be read
  2376. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2377. 2. The user gives a day, but no month.
  2378. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2379. read this as the third of *next* month. However, if you enter \"17\",
  2380. it will be considered as *this* month.
  2381. If you set this variable to the symbol `time', then also the following
  2382. will work:
  2383. 3. If the user gives a time, but no day. If the time is before now,
  2384. to will be interpreted as tomorrow.
  2385. Currently none of this works for ISO week specifications.
  2386. When this option is nil, the current day, month and year will always be
  2387. used as defaults.
  2388. See also `org-agenda-jump-prefer-future'."
  2389. :group 'org-time
  2390. :type '(choice
  2391. (const :tag "Never" nil)
  2392. (const :tag "Check month and day" t)
  2393. (const :tag "Check month, day, and time" time)))
  2394. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2395. "Should the agenda jump command prefer the future for incomplete dates?
  2396. The default is to do the same as configured in `org-read-date-prefer-future'.
  2397. But you can also set a deviating value here.
  2398. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2399. :group 'org-agenda
  2400. :group 'org-time
  2401. :version "24.1"
  2402. :type '(choice
  2403. (const :tag "Use org-read-date-prefer-future"
  2404. org-read-date-prefer-future)
  2405. (const :tag "Never" nil)
  2406. (const :tag "Always" t)))
  2407. (defcustom org-read-date-force-compatible-dates t
  2408. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2409. Depending on the system Emacs is running on, certain dates cannot
  2410. be represented with the type used internally to represent time.
  2411. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2412. correctly. Some systems allow for earlier dates, some for later,
  2413. some for both. One way to find out it to insert any date into an
  2414. Org buffer, putting the cursor on the year and hitting S-up and
  2415. S-down to test the range.
  2416. When this variable is set to t, the date/time prompt will not let
  2417. you specify dates outside the 1970-2037 range, so it is certain that
  2418. these dates will work in whatever version of Emacs you are
  2419. running, and also that you can move a file from one Emacs implementation
  2420. to another. WHenever Org is forcing the year for you, it will display
  2421. a message and beep.
  2422. When this variable is nil, Org will check if the date is
  2423. representable in the specific Emacs implementation you are using.
  2424. If not, it will force a year, usually the current year, and beep
  2425. to remind you. Currently this setting is not recommended because
  2426. the likelihood that you will open your Org files in an Emacs that
  2427. has limited date range is not negligible.
  2428. A workaround for this problem is to use diary sexp dates for time
  2429. stamps outside of this range."
  2430. :group 'org-time
  2431. :version "24.1"
  2432. :type 'boolean)
  2433. (defcustom org-read-date-display-live t
  2434. "Non-nil means display current interpretation of date prompt live.
  2435. This display will be in an overlay, in the minibuffer."
  2436. :group 'org-time
  2437. :type 'boolean)
  2438. (defcustom org-read-date-popup-calendar t
  2439. "Non-nil means pop up a calendar when prompting for a date.
  2440. In the calendar, the date can be selected with mouse-1. However, the
  2441. minibuffer will also be active, and you can simply enter the date as well.
  2442. When nil, only the minibuffer will be available."
  2443. :group 'org-time
  2444. :type 'boolean)
  2445. (if (fboundp 'defvaralias)
  2446. (defvaralias 'org-popup-calendar-for-date-prompt
  2447. 'org-read-date-popup-calendar))
  2448. (defcustom org-read-date-minibuffer-setup-hook nil
  2449. "Hook to be used to set up keys for the date/time interface.
  2450. Add key definitions to `minibuffer-local-map', which will be a temporary
  2451. copy."
  2452. :group 'org-time
  2453. :type 'hook)
  2454. (defcustom org-extend-today-until 0
  2455. "The hour when your day really ends. Must be an integer.
  2456. This has influence for the following applications:
  2457. - When switching the agenda to \"today\". It it is still earlier than
  2458. the time given here, the day recognized as TODAY is actually yesterday.
  2459. - When a date is read from the user and it is still before the time given
  2460. here, the current date and time will be assumed to be yesterday, 23:59.
  2461. Also, timestamps inserted in remember templates follow this rule.
  2462. IMPORTANT: This is a feature whose implementation is and likely will
  2463. remain incomplete. Really, it is only here because past midnight seems to
  2464. be the favorite working time of John Wiegley :-)"
  2465. :group 'org-time
  2466. :type 'integer)
  2467. (defcustom org-use-effective-time nil
  2468. "If non-nil, consider `org-extend-today-until' when creating timestamps.
  2469. For example, if `org-extend-today-until' is 8, and it's 4am, then the
  2470. \"effective time\" of any timestamps between midnight and 8am will be
  2471. 23:59 of the previous day."
  2472. :group 'org-time
  2473. :version "24.1"
  2474. :type 'boolean)
  2475. (defcustom org-edit-timestamp-down-means-later nil
  2476. "Non-nil means S-down will increase the time in a time stamp.
  2477. When nil, S-up will increase."
  2478. :group 'org-time
  2479. :type 'boolean)
  2480. (defcustom org-calendar-follow-timestamp-change t
  2481. "Non-nil means make the calendar window follow timestamp changes.
  2482. When a timestamp is modified and the calendar window is visible, it will be
  2483. moved to the new date."
  2484. :group 'org-time
  2485. :type 'boolean)
  2486. (defgroup org-tags nil
  2487. "Options concerning tags in Org-mode."
  2488. :tag "Org Tags"
  2489. :group 'org)
  2490. (defcustom org-tag-alist nil
  2491. "List of tags allowed in Org-mode files.
  2492. When this list is nil, Org-mode will base TAG input on what is already in the
  2493. buffer.
  2494. The value of this variable is an alist, the car of each entry must be a
  2495. keyword as a string, the cdr may be a character that is used to select
  2496. that tag through the fast-tag-selection interface.
  2497. See the manual for details."
  2498. :group 'org-tags
  2499. :type '(repeat
  2500. (choice
  2501. (cons (string :tag "Tag name")
  2502. (character :tag "Access char"))
  2503. (list :tag "Start radio group"
  2504. (const :startgroup)
  2505. (option (string :tag "Group description")))
  2506. (list :tag "End radio group"
  2507. (const :endgroup)
  2508. (option (string :tag "Group description")))
  2509. (const :tag "New line" (:newline)))))
  2510. (defcustom org-tag-persistent-alist nil
  2511. "List of tags that will always appear in all Org-mode files.
  2512. This is in addition to any in buffer settings or customizations
  2513. of `org-tag-alist'.
  2514. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2515. The value of this variable is an alist, the car of each entry must be a
  2516. keyword as a string, the cdr may be a character that is used to select
  2517. that tag through the fast-tag-selection interface.
  2518. See the manual for details.
  2519. To disable these tags on a per-file basis, insert anywhere in the file:
  2520. #+STARTUP: noptag"
  2521. :group 'org-tags
  2522. :type '(repeat
  2523. (choice
  2524. (cons (string :tag "Tag name")
  2525. (character :tag "Access char"))
  2526. (const :tag "Start radio group" (:startgroup))
  2527. (const :tag "End radio group" (:endgroup))
  2528. (const :tag "New line" (:newline)))))
  2529. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2530. "If non-nil, always offer completion for all tags of all agenda files.
  2531. Instead of customizing this variable directly, you might want to
  2532. set it locally for capture buffers, because there no list of
  2533. tags in that file can be created dynamically (there are none).
  2534. (add-hook 'org-capture-mode-hook
  2535. (lambda ()
  2536. (set (make-local-variable
  2537. 'org-complete-tags-always-offer-all-agenda-tags)
  2538. t)))"
  2539. :group 'org-tags
  2540. :version "24.1"
  2541. :type 'boolean)
  2542. (defvar org-file-tags nil
  2543. "List of tags that can be inherited by all entries in the file.
  2544. The tags will be inherited if the variable `org-use-tag-inheritance'
  2545. says they should be.
  2546. This variable is populated from #+FILETAGS lines.")
  2547. (defcustom org-use-fast-tag-selection 'auto
  2548. "Non-nil means use fast tag selection scheme.
  2549. This is a special interface to select and deselect tags with single keys.
  2550. When nil, fast selection is never used.
  2551. When the symbol `auto', fast selection is used if and only if selection
  2552. characters for tags have been configured, either through the variable
  2553. `org-tag-alist' or through a #+TAGS line in the buffer.
  2554. When t, fast selection is always used and selection keys are assigned
  2555. automatically if necessary."
  2556. :group 'org-tags
  2557. :type '(choice
  2558. (const :tag "Always" t)
  2559. (const :tag "Never" nil)
  2560. (const :tag "When selection characters are configured" 'auto)))
  2561. (defcustom org-fast-tag-selection-single-key nil
  2562. "Non-nil means fast tag selection exits after first change.
  2563. When nil, you have to press RET to exit it.
  2564. During fast tag selection, you can toggle this flag with `C-c'.
  2565. This variable can also have the value `expert'. In this case, the window
  2566. displaying the tags menu is not even shown, until you press C-c again."
  2567. :group 'org-tags
  2568. :type '(choice
  2569. (const :tag "No" nil)
  2570. (const :tag "Yes" t)
  2571. (const :tag "Expert" expert)))
  2572. (defvar org-fast-tag-selection-include-todo nil
  2573. "Non-nil means fast tags selection interface will also offer TODO states.
  2574. This is an undocumented feature, you should not rely on it.")
  2575. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2576. "The column to which tags should be indented in a headline.
  2577. If this number is positive, it specifies the column. If it is negative,
  2578. it means that the tags should be flushright to that column. For example,
  2579. -80 works well for a normal 80 character screen.
  2580. When 0, place tags directly after headline text, with only one space in
  2581. between."
  2582. :group 'org-tags
  2583. :type 'integer)
  2584. (defcustom org-auto-align-tags t
  2585. "Non-nil keeps tags aligned when modifying headlines.
  2586. Some operations (i.e. demoting) change the length of a headline and
  2587. therefore shift the tags around. With this option turned on, after
  2588. each such operation the tags are again aligned to `org-tags-column'."
  2589. :group 'org-tags
  2590. :type 'boolean)
  2591. (defcustom org-use-tag-inheritance t
  2592. "Non-nil means tags in levels apply also for sublevels.
  2593. When nil, only the tags directly given in a specific line apply there.
  2594. This may also be a list of tags that should be inherited, or a regexp that
  2595. matches tags that should be inherited. Additional control is possible
  2596. with the variable `org-tags-exclude-from-inheritance' which gives an
  2597. explicit list of tags to be excluded from inheritance., even if the value of
  2598. `org-use-tag-inheritance' would select it for inheritance.
  2599. If this option is t, a match early-on in a tree can lead to a large
  2600. number of matches in the subtree when constructing the agenda or creating
  2601. a sparse tree. If you only want to see the first match in a tree during
  2602. a search, check out the variable `org-tags-match-list-sublevels'."
  2603. :group 'org-tags
  2604. :type '(choice
  2605. (const :tag "Not" nil)
  2606. (const :tag "Always" t)
  2607. (repeat :tag "Specific tags" (string :tag "Tag"))
  2608. (regexp :tag "Tags matched by regexp")))
  2609. (defcustom org-tags-exclude-from-inheritance nil
  2610. "List of tags that should never be inherited.
  2611. This is a way to exclude a few tags from inheritance. For way to do
  2612. the opposite, to actively allow inheritance for selected tags,
  2613. see the variable `org-use-tag-inheritance'."
  2614. :group 'org-tags
  2615. :type '(repeat (string :tag "Tag")))
  2616. (defun org-tag-inherit-p (tag)
  2617. "Check if TAG is one that should be inherited."
  2618. (cond
  2619. ((member tag org-tags-exclude-from-inheritance) nil)
  2620. ((eq org-use-tag-inheritance t) t)
  2621. ((not org-use-tag-inheritance) nil)
  2622. ((stringp org-use-tag-inheritance)
  2623. (string-match org-use-tag-inheritance tag))
  2624. ((listp org-use-tag-inheritance)
  2625. (member tag org-use-tag-inheritance))
  2626. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2627. (defcustom org-tags-match-list-sublevels t
  2628. "Non-nil means list also sublevels of headlines matching a search.
  2629. This variable applies to tags/property searches, and also to stuck
  2630. projects because this search is based on a tags match as well.
  2631. When set to the symbol `indented', sublevels are indented with
  2632. leading dots.
  2633. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2634. the sublevels of a headline matching a tag search often also match
  2635. the same search. Listing all of them can create very long lists.
  2636. Setting this variable to nil causes subtrees of a match to be skipped.
  2637. This variable is semi-obsolete and probably should always be true. It
  2638. is better to limit inheritance to certain tags using the variables
  2639. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2640. :group 'org-tags
  2641. :type '(choice
  2642. (const :tag "No, don't list them" nil)
  2643. (const :tag "Yes, do list them" t)
  2644. (const :tag "List them, indented with leading dots" indented)))
  2645. (defcustom org-tags-sort-function nil
  2646. "When set, tags are sorted using this function as a comparator."
  2647. :group 'org-tags
  2648. :type '(choice
  2649. (const :tag "No sorting" nil)
  2650. (const :tag "Alphabetical" string<)
  2651. (const :tag "Reverse alphabetical" string>)
  2652. (function :tag "Custom function" nil)))
  2653. (defvar org-tags-history nil
  2654. "History of minibuffer reads for tags.")
  2655. (defvar org-last-tags-completion-table nil
  2656. "The last used completion table for tags.")
  2657. (defvar org-after-tags-change-hook nil
  2658. "Hook that is run after the tags in a line have changed.")
  2659. (defgroup org-properties nil
  2660. "Options concerning properties in Org-mode."
  2661. :tag "Org Properties"
  2662. :group 'org)
  2663. (defcustom org-property-format "%-10s %s"
  2664. "How property key/value pairs should be formatted by `indent-line'.
  2665. When `indent-line' hits a property definition, it will format the line
  2666. according to this format, mainly to make sure that the values are
  2667. lined-up with respect to each other."
  2668. :group 'org-properties
  2669. :type 'string)
  2670. (defcustom org-properties-postprocess-alist nil
  2671. "Alist of properties and functions to adjust inserted values.
  2672. Elements of this alist must be of the form
  2673. ([string] [function])
  2674. where [string] must be a property name and [function] must be a
  2675. lambda expression: this lambda expression must take one argument,
  2676. the value to adjust, and return the new value as a string.
  2677. For example, this element will allow the property \"Remaining\"
  2678. to be updated wrt the relation between the \"Effort\" property
  2679. and the clock summary:
  2680. ((\"Remaining\" (lambda(value)
  2681. (let ((clocksum (org-clock-sum-current-item))
  2682. (effort (org-duration-string-to-minutes
  2683. (org-entry-get (point) \"Effort\"))))
  2684. (org-minutes-to-hh:mm-string (- effort clocksum))))))"
  2685. :group 'org-properties
  2686. :version "24.1"
  2687. :type 'alist)
  2688. (defcustom org-use-property-inheritance nil
  2689. "Non-nil means properties apply also for sublevels.
  2690. This setting is chiefly used during property searches. Turning it on can
  2691. cause significant overhead when doing a search, which is why it is not
  2692. on by default.
  2693. When nil, only the properties directly given in the current entry count.
  2694. When t, every property is inherited. The value may also be a list of
  2695. properties that should have inheritance, or a regular expression matching
  2696. properties that should be inherited.
  2697. However, note that some special properties use inheritance under special
  2698. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2699. and the properties ending in \"_ALL\" when they are used as descriptor
  2700. for valid values of a property.
  2701. Note for programmers:
  2702. When querying an entry with `org-entry-get', you can control if inheritance
  2703. should be used. By default, `org-entry-get' looks only at the local
  2704. properties. You can request inheritance by setting the inherit argument
  2705. to t (to force inheritance) or to `selective' (to respect the setting
  2706. in this variable)."
  2707. :group 'org-properties
  2708. :type '(choice
  2709. (const :tag "Not" nil)
  2710. (const :tag "Always" t)
  2711. (repeat :tag "Specific properties" (string :tag "Property"))
  2712. (regexp :tag "Properties matched by regexp")))
  2713. (defun org-property-inherit-p (property)
  2714. "Check if PROPERTY is one that should be inherited."
  2715. (cond
  2716. ((eq org-use-property-inheritance t) t)
  2717. ((not org-use-property-inheritance) nil)
  2718. ((stringp org-use-property-inheritance)
  2719. (string-match org-use-property-inheritance property))
  2720. ((listp org-use-property-inheritance)
  2721. (member property org-use-property-inheritance))
  2722. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2723. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2724. "The default column format, if no other format has been defined.
  2725. This variable can be set on the per-file basis by inserting a line
  2726. #+COLUMNS: %25ITEM ....."
  2727. :group 'org-properties
  2728. :type 'string)
  2729. (defcustom org-columns-ellipses ".."
  2730. "The ellipses to be used when a field in column view is truncated.
  2731. When this is the empty string, as many characters as possible are shown,
  2732. but then there will be no visual indication that the field has been truncated.
  2733. When this is a string of length N, the last N characters of a truncated
  2734. field are replaced by this string. If the column is narrower than the
  2735. ellipses string, only part of the ellipses string will be shown."
  2736. :group 'org-properties
  2737. :type 'string)
  2738. (defcustom org-columns-modify-value-for-display-function nil
  2739. "Function that modifies values for display in column view.
  2740. For example, it can be used to cut out a certain part from a time stamp.
  2741. The function must take 2 arguments:
  2742. column-title The title of the column (*not* the property name)
  2743. value The value that should be modified.
  2744. The function should return the value that should be displayed,
  2745. or nil if the normal value should be used."
  2746. :group 'org-properties
  2747. :type 'function)
  2748. (defcustom org-effort-property "Effort"
  2749. "The property that is being used to keep track of effort estimates.
  2750. Effort estimates given in this property need to have the format H:MM."
  2751. :group 'org-properties
  2752. :group 'org-progress
  2753. :type '(string :tag "Property"))
  2754. (defconst org-global-properties-fixed
  2755. '(("VISIBILITY_ALL" . "folded children content all")
  2756. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  2757. "List of property/value pairs that can be inherited by any entry.
  2758. These are fixed values, for the preset properties. The user variable
  2759. that can be used to add to this list is `org-global-properties'.
  2760. The entries in this list are cons cells where the car is a property
  2761. name and cdr is a string with the value. If the value represents
  2762. multiple items like an \"_ALL\" property, separate the items by
  2763. spaces.")
  2764. (defcustom org-global-properties nil
  2765. "List of property/value pairs that can be inherited by any entry.
  2766. This list will be combined with the constant `org-global-properties-fixed'.
  2767. The entries in this list are cons cells where the car is a property
  2768. name and cdr is a string with the value.
  2769. You can set buffer-local values for the same purpose in the variable
  2770. `org-file-properties' this by adding lines like
  2771. #+PROPERTY: NAME VALUE"
  2772. :group 'org-properties
  2773. :type '(repeat
  2774. (cons (string :tag "Property")
  2775. (string :tag "Value"))))
  2776. (defvar org-file-properties nil
  2777. "List of property/value pairs that can be inherited by any entry.
  2778. Valid for the current buffer.
  2779. This variable is populated from #+PROPERTY lines.")
  2780. (make-variable-buffer-local 'org-file-properties)
  2781. (defgroup org-agenda nil
  2782. "Options concerning agenda views in Org-mode."
  2783. :tag "Org Agenda"
  2784. :group 'org)
  2785. (defvar org-category nil
  2786. "Variable used by org files to set a category for agenda display.
  2787. Such files should use a file variable to set it, for example
  2788. # -*- mode: org; org-category: \"ELisp\"
  2789. or contain a special line
  2790. #+CATEGORY: ELisp
  2791. If the file does not specify a category, then file's base name
  2792. is used instead.")
  2793. (make-variable-buffer-local 'org-category)
  2794. (put 'org-category 'safe-local-variable #'(lambda (x) (or (symbolp x) (stringp x))))
  2795. (defcustom org-agenda-files nil
  2796. "The files to be used for agenda display.
  2797. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2798. \\[org-remove-file]. You can also use customize to edit the list.
  2799. If an entry is a directory, all files in that directory that are matched by
  2800. `org-agenda-file-regexp' will be part of the file list.
  2801. If the value of the variable is not a list but a single file name, then
  2802. the list of agenda files is actually stored and maintained in that file, one
  2803. agenda file per line. In this file paths can be given relative to
  2804. `org-directory'. Tilde expansion and environment variable substitution
  2805. are also made."
  2806. :group 'org-agenda
  2807. :type '(choice
  2808. (repeat :tag "List of files and directories" file)
  2809. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2810. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2811. "Regular expression to match files for `org-agenda-files'.
  2812. If any element in the list in that variable contains a directory instead
  2813. of a normal file, all files in that directory that are matched by this
  2814. regular expression will be included."
  2815. :group 'org-agenda
  2816. :type 'regexp)
  2817. (defcustom org-agenda-text-search-extra-files nil
  2818. "List of extra files to be searched by text search commands.
  2819. These files will be search in addition to the agenda files by the
  2820. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2821. Note that these files will only be searched for text search commands,
  2822. not for the other agenda views like todo lists, tag searches or the weekly
  2823. agenda. This variable is intended to list notes and possibly archive files
  2824. that should also be searched by these two commands.
  2825. In fact, if the first element in the list is the symbol `agenda-archives',
  2826. than all archive files of all agenda files will be added to the search
  2827. scope."
  2828. :group 'org-agenda
  2829. :type '(set :greedy t
  2830. (const :tag "Agenda Archives" agenda-archives)
  2831. (repeat :inline t (file))))
  2832. (if (fboundp 'defvaralias)
  2833. (defvaralias 'org-agenda-multi-occur-extra-files
  2834. 'org-agenda-text-search-extra-files))
  2835. (defcustom org-agenda-skip-unavailable-files nil
  2836. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2837. A nil value means to remove them, after a query, from the list."
  2838. :group 'org-agenda
  2839. :type 'boolean)
  2840. (defcustom org-calendar-to-agenda-key [?c]
  2841. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2842. The command `org-calendar-goto-agenda' will be bound to this key. The
  2843. default is the character `c' because then `c' can be used to switch back and
  2844. forth between agenda and calendar."
  2845. :group 'org-agenda
  2846. :type 'sexp)
  2847. (defcustom org-calendar-agenda-action-key [?k]
  2848. "The key to be installed in `calendar-mode-map' for agenda-action.
  2849. The command `org-agenda-action' will be bound to this key. The
  2850. default is the character `k' because we use the same key in the agenda."
  2851. :group 'org-agenda
  2852. :type 'sexp)
  2853. (defcustom org-calendar-insert-diary-entry-key [?i]
  2854. "The key to be installed in `calendar-mode-map' for adding diary entries.
  2855. This option is irrelevant until `org-agenda-diary-file' has been configured
  2856. to point to an Org-mode file. When that is the case, the command
  2857. `org-agenda-diary-entry' will be bound to the key given here, by default
  2858. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  2859. if you want to continue doing this, you need to change this to a different
  2860. key."
  2861. :group 'org-agenda
  2862. :type 'sexp)
  2863. (defcustom org-agenda-diary-file 'diary-file
  2864. "File to which to add new entries with the `i' key in agenda and calendar.
  2865. When this is the symbol `diary-file', the functionality in the Emacs
  2866. calendar will be used to add entries to the `diary-file'. But when this
  2867. points to a file, `org-agenda-diary-entry' will be used instead."
  2868. :group 'org-agenda
  2869. :type '(choice
  2870. (const :tag "The standard Emacs diary file" diary-file)
  2871. (file :tag "Special Org file diary entries")))
  2872. (eval-after-load "calendar"
  2873. '(progn
  2874. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2875. 'org-calendar-goto-agenda)
  2876. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2877. 'org-agenda-action)
  2878. (add-hook 'calendar-mode-hook
  2879. (lambda ()
  2880. (unless (eq org-agenda-diary-file 'diary-file)
  2881. (define-key calendar-mode-map
  2882. org-calendar-insert-diary-entry-key
  2883. 'org-agenda-diary-entry))))))
  2884. (defgroup org-latex nil
  2885. "Options for embedding LaTeX code into Org-mode."
  2886. :tag "Org LaTeX"
  2887. :group 'org)
  2888. (defcustom org-format-latex-options
  2889. '(:foreground default :background default :scale 1.0
  2890. :html-foreground "Black" :html-background "Transparent"
  2891. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2892. "Options for creating images from LaTeX fragments.
  2893. This is a property list with the following properties:
  2894. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2895. `default' means use the foreground of the default face.
  2896. :background the background color, or \"Transparent\".
  2897. `default' means use the background of the default face.
  2898. :scale a scaling factor for the size of the images, to get more pixels
  2899. :html-foreground, :html-background, :html-scale
  2900. the same numbers for HTML export.
  2901. :matchers a list indicating which matchers should be used to
  2902. find LaTeX fragments. Valid members of this list are:
  2903. \"begin\" find environments
  2904. \"$1\" find single characters surrounded by $.$
  2905. \"$\" find math expressions surrounded by $...$
  2906. \"$$\" find math expressions surrounded by $$....$$
  2907. \"\\(\" find math expressions surrounded by \\(...\\)
  2908. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2909. :group 'org-latex
  2910. :type 'plist)
  2911. (defcustom org-format-latex-signal-error t
  2912. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  2913. When nil, just push out a message."
  2914. :group 'org-latex
  2915. :version "24.1"
  2916. :type 'boolean)
  2917. (defcustom org-latex-to-mathml-jar-file nil
  2918. "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
  2919. Use this to specify additional executable file say a jar file.
  2920. When using MathToWeb as the converter, specify the full-path to
  2921. your mathtoweb.jar file."
  2922. :group 'org-latex
  2923. :version "24.1"
  2924. :type '(choice
  2925. (const :tag "None" nil)
  2926. (file :tag "JAR file" :must-match t)))
  2927. (defcustom org-latex-to-mathml-convert-command nil
  2928. "Command to convert LaTeX fragments to MathML.
  2929. Replace format-specifiers in the command as noted below and use
  2930. `shell-command' to convert LaTeX to MathML.
  2931. %j: Executable file in fully expanded form as specified by
  2932. `org-latex-to-mathml-jar-file'.
  2933. %I: Input LaTeX file in fully expanded form
  2934. %o: Output MathML file
  2935. This command is used by `org-create-math-formula'.
  2936. When using MathToWeb as the converter, set this to
  2937. \"java -jar %j -unicode -force -df %o %I\"."
  2938. :group 'org-latex
  2939. :version "24.1"
  2940. :type '(choice
  2941. (const :tag "None" nil)
  2942. (string :tag "\nShell command")))
  2943. (defun org-format-latex-mathml-available-p ()
  2944. "Return t if `org-latex-to-mathml-convert-command' is usable."
  2945. (save-match-data
  2946. (when (and (boundp 'org-latex-to-mathml-convert-command)
  2947. org-latex-to-mathml-convert-command)
  2948. (let ((executable (car (split-string
  2949. org-latex-to-mathml-convert-command))))
  2950. (when (executable-find executable)
  2951. (if (string-match
  2952. "%j" org-latex-to-mathml-convert-command)
  2953. (file-readable-p org-latex-to-mathml-jar-file)
  2954. t))))))
  2955. (defcustom org-format-latex-header "\\documentclass{article}
  2956. \\usepackage[usenames]{color}
  2957. \\usepackage{amsmath}
  2958. \\usepackage[mathscr]{eucal}
  2959. \\pagestyle{empty} % do not remove
  2960. \[PACKAGES]
  2961. \[DEFAULT-PACKAGES]
  2962. % The settings below are copied from fullpage.sty
  2963. \\setlength{\\textwidth}{\\paperwidth}
  2964. \\addtolength{\\textwidth}{-3cm}
  2965. \\setlength{\\oddsidemargin}{1.5cm}
  2966. \\addtolength{\\oddsidemargin}{-2.54cm}
  2967. \\setlength{\\evensidemargin}{\\oddsidemargin}
  2968. \\setlength{\\textheight}{\\paperheight}
  2969. \\addtolength{\\textheight}{-\\headheight}
  2970. \\addtolength{\\textheight}{-\\headsep}
  2971. \\addtolength{\\textheight}{-\\footskip}
  2972. \\addtolength{\\textheight}{-3cm}
  2973. \\setlength{\\topmargin}{1.5cm}
  2974. \\addtolength{\\topmargin}{-2.54cm}"
  2975. "The document header used for processing LaTeX fragments.
  2976. It is imperative that this header make sure that no page number
  2977. appears on the page. The package defined in the variables
  2978. `org-export-latex-default-packages-alist' and `org-export-latex-packages-alist'
  2979. will either replace the placeholder \"[PACKAGES]\" in this header, or they
  2980. will be appended."
  2981. :group 'org-latex
  2982. :type 'string)
  2983. (defvar org-format-latex-header-extra nil)
  2984. (defun org-set-packages-alist (var val)
  2985. "Set the packages alist and make sure it has 3 elements per entry."
  2986. (set var (mapcar (lambda (x)
  2987. (if (and (consp x) (= (length x) 2))
  2988. (list (car x) (nth 1 x) t)
  2989. x))
  2990. val)))
  2991. (defun org-get-packages-alist (var)
  2992. "Get the packages alist and make sure it has 3 elements per entry."
  2993. (mapcar (lambda (x)
  2994. (if (and (consp x) (= (length x) 2))
  2995. (list (car x) (nth 1 x) t)
  2996. x))
  2997. (default-value var)))
  2998. ;; The following variables are defined here because is it also used
  2999. ;; when formatting latex fragments. Originally it was part of the
  3000. ;; LaTeX exporter, which is why the name includes "export".
  3001. (defcustom org-export-latex-default-packages-alist
  3002. '(("AUTO" "inputenc" t)
  3003. ("T1" "fontenc" t)
  3004. ("" "fixltx2e" nil)
  3005. ("" "graphicx" t)
  3006. ("" "longtable" nil)
  3007. ("" "float" nil)
  3008. ("" "wrapfig" nil)
  3009. ("" "soul" t)
  3010. ("" "textcomp" t)
  3011. ("" "marvosym" t)
  3012. ("" "wasysym" t)
  3013. ("" "latexsym" t)
  3014. ("" "amssymb" t)
  3015. ("" "hyperref" nil)
  3016. "\\tolerance=1000"
  3017. )
  3018. "Alist of default packages to be inserted in the header.
  3019. Change this only if one of the packages here causes an incompatibility
  3020. with another package you are using.
  3021. The packages in this list are needed by one part or another of Org-mode
  3022. to function properly.
  3023. - inputenc, fontenc: for basic font and character selection
  3024. - textcomp, marvosymb, wasysym, latexsym, amssym: for various symbols used
  3025. for interpreting the entities in `org-entities'. You can skip some of these
  3026. packages if you don't use any of the symbols in it.
  3027. - graphicx: for including images
  3028. - float, wrapfig: for figure placement
  3029. - longtable: for long tables
  3030. - hyperref: for cross references
  3031. Therefore you should not modify this variable unless you know what you
  3032. are doing. The one reason to change it anyway is that you might be loading
  3033. some other package that conflicts with one of the default packages.
  3034. Each cell is of the format \( \"options\" \"package\" snippet-flag\).
  3035. If SNIPPET-FLAG is t, the package also needs to be included when
  3036. compiling LaTeX snippets into images for inclusion into HTML."
  3037. :group 'org-export-latex
  3038. :set 'org-set-packages-alist
  3039. :get 'org-get-packages-alist
  3040. :version "24.1"
  3041. :type '(repeat
  3042. (choice
  3043. (list :tag "options/package pair"
  3044. (string :tag "options")
  3045. (string :tag "package")
  3046. (boolean :tag "Snippet"))
  3047. (string :tag "A line of LaTeX"))))
  3048. (defcustom org-export-latex-packages-alist nil
  3049. "Alist of packages to be inserted in every LaTeX header.
  3050. These will be inserted after `org-export-latex-default-packages-alist'.
  3051. Each cell is of the format \( \"options\" \"package\" snippet-flag \).
  3052. SNIPPET-FLAG, when t, indicates that this package is also needed when
  3053. turning LaTeX snippets into images for inclusion into HTML.
  3054. Make sure that you only list packages here which:
  3055. - you want in every file
  3056. - do not conflict with the default packages in
  3057. `org-export-latex-default-packages-alist'
  3058. - do not conflict with the setup in `org-format-latex-header'."
  3059. :group 'org-export-latex
  3060. :set 'org-set-packages-alist
  3061. :get 'org-get-packages-alist
  3062. :type '(repeat
  3063. (choice
  3064. (list :tag "options/package pair"
  3065. (string :tag "options")
  3066. (string :tag "package")
  3067. (boolean :tag "Snippet"))
  3068. (string :tag "A line of LaTeX"))))
  3069. (defgroup org-appearance nil
  3070. "Settings for Org-mode appearance."
  3071. :tag "Org Appearance"
  3072. :group 'org)
  3073. (defcustom org-level-color-stars-only nil
  3074. "Non-nil means fontify only the stars in each headline.
  3075. When nil, the entire headline is fontified.
  3076. Changing it requires restart of `font-lock-mode' to become effective
  3077. also in regions already fontified."
  3078. :group 'org-appearance
  3079. :type 'boolean)
  3080. (defcustom org-hide-leading-stars nil
  3081. "Non-nil means hide the first N-1 stars in a headline.
  3082. This works by using the face `org-hide' for these stars. This
  3083. face is white for a light background, and black for a dark
  3084. background. You may have to customize the face `org-hide' to
  3085. make this work.
  3086. Changing it requires restart of `font-lock-mode' to become effective
  3087. also in regions already fontified.
  3088. You may also set this on a per-file basis by adding one of the following
  3089. lines to the buffer:
  3090. #+STARTUP: hidestars
  3091. #+STARTUP: showstars"
  3092. :group 'org-appearance
  3093. :type 'boolean)
  3094. (defcustom org-hidden-keywords nil
  3095. "List of symbols corresponding to keywords to be hidden the org buffer.
  3096. For example, a value '(title) for this list will make the document's title
  3097. appear in the buffer without the initial #+TITLE: keyword."
  3098. :group 'org-appearance
  3099. :version "24.1"
  3100. :type '(set (const :tag "#+AUTHOR" author)
  3101. (const :tag "#+DATE" date)
  3102. (const :tag "#+EMAIL" email)
  3103. (const :tag "#+TITLE" title)))
  3104. (defcustom org-fontify-done-headline nil
  3105. "Non-nil means change the face of a headline if it is marked DONE.
  3106. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3107. When this is non-nil, the headline after the keyword is set to the
  3108. `org-headline-done' as an additional indication."
  3109. :group 'org-appearance
  3110. :type 'boolean)
  3111. (defcustom org-fontify-emphasized-text t
  3112. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3113. Changing this variable requires a restart of Emacs to take effect."
  3114. :group 'org-appearance
  3115. :type 'boolean)
  3116. (defcustom org-fontify-whole-heading-line nil
  3117. "Non-nil means fontify the whole line for headings.
  3118. This is useful when setting a background color for the
  3119. org-level-* faces."
  3120. :group 'org-appearance
  3121. :type 'boolean)
  3122. (defcustom org-highlight-latex-fragments-and-specials nil
  3123. "Non-nil means fontify what is treated specially by the exporters."
  3124. :group 'org-appearance
  3125. :type 'boolean)
  3126. (defcustom org-hide-emphasis-markers nil
  3127. "Non-nil mean font-lock should hide the emphasis marker characters."
  3128. :group 'org-appearance
  3129. :type 'boolean)
  3130. (defcustom org-pretty-entities nil
  3131. "Non-nil means show entities as UTF8 characters.
  3132. When nil, the \\name form remains in the buffer."
  3133. :group 'org-appearance
  3134. :version "24.1"
  3135. :type 'boolean)
  3136. (defcustom org-pretty-entities-include-sub-superscripts t
  3137. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  3138. :group 'org-appearance
  3139. :version "24.1"
  3140. :type 'boolean)
  3141. (defvar org-emph-re nil
  3142. "Regular expression for matching emphasis.
  3143. After a match, the match groups contain these elements:
  3144. 0 The match of the full regular expression, including the characters
  3145. before and after the proper match
  3146. 1 The character before the proper match, or empty at beginning of line
  3147. 2 The proper match, including the leading and trailing markers
  3148. 3 The leading marker like * or /, indicating the type of highlighting
  3149. 4 The text between the emphasis markers, not including the markers
  3150. 5 The character after the match, empty at the end of a line")
  3151. (defvar org-verbatim-re nil
  3152. "Regular expression for matching verbatim text.")
  3153. (defvar org-emphasis-regexp-components) ; defined just below
  3154. (defvar org-emphasis-alist) ; defined just below
  3155. (defun org-set-emph-re (var val)
  3156. "Set variable and compute the emphasis regular expression."
  3157. (set var val)
  3158. (when (and (boundp 'org-emphasis-alist)
  3159. (boundp 'org-emphasis-regexp-components)
  3160. org-emphasis-alist org-emphasis-regexp-components)
  3161. (let* ((e org-emphasis-regexp-components)
  3162. (pre (car e))
  3163. (post (nth 1 e))
  3164. (border (nth 2 e))
  3165. (body (nth 3 e))
  3166. (nl (nth 4 e))
  3167. (body1 (concat body "*?"))
  3168. (markers (mapconcat 'car org-emphasis-alist ""))
  3169. (vmarkers (mapconcat
  3170. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  3171. org-emphasis-alist "")))
  3172. ;; make sure special characters appear at the right position in the class
  3173. (if (string-match "\\^" markers)
  3174. (setq markers (concat (replace-match "" t t markers) "^")))
  3175. (if (string-match "-" markers)
  3176. (setq markers (concat (replace-match "" t t markers) "-")))
  3177. (if (string-match "\\^" vmarkers)
  3178. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3179. (if (string-match "-" vmarkers)
  3180. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3181. (if (> nl 0)
  3182. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3183. (int-to-string nl) "\\}")))
  3184. ;; Make the regexp
  3185. (setq org-emph-re
  3186. (concat "\\([" pre "]\\|^\\)"
  3187. "\\("
  3188. "\\([" markers "]\\)"
  3189. "\\("
  3190. "[^" border "]\\|"
  3191. "[^" border "]"
  3192. body1
  3193. "[^" border "]"
  3194. "\\)"
  3195. "\\3\\)"
  3196. "\\([" post "]\\|$\\)"))
  3197. (setq org-verbatim-re
  3198. (concat "\\([" pre "]\\|^\\)"
  3199. "\\("
  3200. "\\([" vmarkers "]\\)"
  3201. "\\("
  3202. "[^" border "]\\|"
  3203. "[^" border "]"
  3204. body1
  3205. "[^" border "]"
  3206. "\\)"
  3207. "\\3\\)"
  3208. "\\([" post "]\\|$\\)")))))
  3209. (defcustom org-emphasis-regexp-components
  3210. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  3211. "Components used to build the regular expression for emphasis.
  3212. This is a list with five entries. Terminology: In an emphasis string
  3213. like \" *strong word* \", we call the initial space PREMATCH, the final
  3214. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3215. and \"trong wor\" is the body. The different components in this variable
  3216. specify what is allowed/forbidden in each part:
  3217. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3218. post Chars allowed as postmatch. End of line will be allowed too.
  3219. border The chars *forbidden* as border characters.
  3220. body-regexp A regexp like \".\" to match a body character. Don't use
  3221. non-shy groups here, and don't allow newline here.
  3222. newline The maximum number of newlines allowed in an emphasis exp.
  3223. Use customize to modify this, or restart Emacs after changing it."
  3224. :group 'org-appearance
  3225. :set 'org-set-emph-re
  3226. :type '(list
  3227. (sexp :tag "Allowed chars in pre ")
  3228. (sexp :tag "Allowed chars in post ")
  3229. (sexp :tag "Forbidden chars in border ")
  3230. (sexp :tag "Regexp for body ")
  3231. (integer :tag "number of newlines allowed")
  3232. (option (boolean :tag "Please ignore this button"))))
  3233. (defcustom org-emphasis-alist
  3234. `(("*" bold "<b>" "</b>")
  3235. ("/" italic "<i>" "</i>")
  3236. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  3237. ("=" org-code "<code>" "</code>" verbatim)
  3238. ("~" org-verbatim "<code>" "</code>" verbatim)
  3239. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  3240. "<del>" "</del>")
  3241. )
  3242. "Special syntax for emphasized text.
  3243. Text starting and ending with a special character will be emphasized, for
  3244. example *bold*, _underlined_ and /italic/. This variable sets the marker
  3245. characters, the face to be used by font-lock for highlighting in Org-mode
  3246. Emacs buffers, and the HTML tags to be used for this.
  3247. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  3248. For DocBook export, see the variable `org-export-docbook-emphasis-alist'.
  3249. Use customize to modify this, or restart Emacs after changing it."
  3250. :group 'org-appearance
  3251. :set 'org-set-emph-re
  3252. :type '(repeat
  3253. (list
  3254. (string :tag "Marker character")
  3255. (choice
  3256. (face :tag "Font-lock-face")
  3257. (plist :tag "Face property list"))
  3258. (string :tag "HTML start tag")
  3259. (string :tag "HTML end tag")
  3260. (option (const verbatim)))))
  3261. (defvar org-protecting-blocks
  3262. '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
  3263. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3264. This is needed for font-lock setup.")
  3265. ;;; Miscellaneous options
  3266. (defgroup org-completion nil
  3267. "Completion in Org-mode."
  3268. :tag "Org Completion"
  3269. :group 'org)
  3270. (defcustom org-completion-use-ido nil
  3271. "Non-nil means use ido completion wherever possible.
  3272. Note that `ido-mode' must be active for this variable to be relevant.
  3273. If you decide to turn this variable on, you might well want to turn off
  3274. `org-outline-path-complete-in-steps'.
  3275. See also `org-completion-use-iswitchb'."
  3276. :group 'org-completion
  3277. :type 'boolean)
  3278. (defcustom org-completion-use-iswitchb nil
  3279. "Non-nil means use iswitchb completion wherever possible.
  3280. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3281. If you decide to turn this variable on, you might well want to turn off
  3282. `org-outline-path-complete-in-steps'.
  3283. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3284. :group 'org-completion
  3285. :type 'boolean)
  3286. (defcustom org-completion-fallback-command 'hippie-expand
  3287. "The expansion command called by \\[pcomplete] in normal context.
  3288. Normal means, no org-mode-specific context."
  3289. :group 'org-completion
  3290. :type 'function)
  3291. ;;; Functions and variables from their packages
  3292. ;; Declared here to avoid compiler warnings
  3293. ;; XEmacs only
  3294. (defvar outline-mode-menu-heading)
  3295. (defvar outline-mode-menu-show)
  3296. (defvar outline-mode-menu-hide)
  3297. (defvar zmacs-regions) ; XEmacs regions
  3298. ;; Emacs only
  3299. (defvar mark-active)
  3300. ;; Various packages
  3301. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  3302. (declare-function calendar-forward-day "cal-move" (arg))
  3303. (declare-function calendar-goto-date "cal-move" (date))
  3304. (declare-function calendar-goto-today "cal-move" ())
  3305. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3306. (defvar calc-embedded-close-formula)
  3307. (defvar calc-embedded-open-formula)
  3308. (declare-function cdlatex-tab "ext:cdlatex" ())
  3309. (declare-function cdlatex-compute-tables "ext:cdlatex" ())
  3310. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3311. (defvar font-lock-unfontify-region-function)
  3312. (declare-function iswitchb-read-buffer "iswitchb"
  3313. (prompt &optional default require-match start matches-set))
  3314. (defvar iswitchb-temp-buflist)
  3315. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3316. (defvar org-agenda-tags-todo-honor-ignore-options)
  3317. (declare-function org-agenda-skip "org-agenda" ())
  3318. (declare-function
  3319. org-agenda-format-item "org-agenda"
  3320. (extra txt &optional category tags dotime noprefix remove-re habitp))
  3321. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3322. (declare-function org-agenda-change-all-lines "org-agenda"
  3323. (newhead hdmarker &optional fixface just-this))
  3324. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3325. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3326. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3327. (beg end))
  3328. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3329. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3330. "org-agenda" (&optional end))
  3331. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3332. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3333. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3334. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3335. (declare-function org-indent-mode "org-indent" (&optional arg))
  3336. (declare-function parse-time-string "parse-time" (string))
  3337. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3338. (declare-function org-export-latex-fix-inputenc "org-latex" ())
  3339. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3340. (defvar remember-data-file)
  3341. (defvar texmathp-why)
  3342. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3343. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3344. (defvar w3m-current-url)
  3345. (defvar w3m-current-title)
  3346. (defvar org-latex-regexps)
  3347. ;;; Autoload and prepare some org modules
  3348. ;; Some table stuff that needs to be defined here, because it is used
  3349. ;; by the functions setting up org-mode or checking for table context.
  3350. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  3351. "Detect an org-type or table-type table.")
  3352. (defconst org-table-line-regexp "^[ \t]*|"
  3353. "Detect an org-type table line.")
  3354. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  3355. "Detect an org-type table line.")
  3356. (defconst org-table-hline-regexp "^[ \t]*|-"
  3357. "Detect an org-type table hline.")
  3358. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  3359. "Detect a table-type table hline.")
  3360. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  3361. "Detect the first line outside a table when searching from within it.
  3362. This works for both table types.")
  3363. ;; Autoload the functions in org-table.el that are needed by functions here.
  3364. (eval-and-compile
  3365. (org-autoload "org-table"
  3366. '(org-table-align org-table-begin org-table-blank-field
  3367. org-table-convert org-table-convert-region org-table-copy-down
  3368. org-table-copy-region org-table-create
  3369. org-table-create-or-convert-from-region
  3370. org-table-create-with-table.el org-table-current-dline
  3371. org-table-cut-region org-table-delete-column org-table-edit-field
  3372. org-table-edit-formulas org-table-end org-table-eval-formula
  3373. org-table-export org-table-field-info
  3374. org-table-get-stored-formulas org-table-goto-column
  3375. org-table-hline-and-move org-table-import org-table-insert-column
  3376. org-table-insert-hline org-table-insert-row org-table-iterate
  3377. org-table-justify-field-maybe org-table-kill-row
  3378. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  3379. org-table-move-column org-table-move-column-left
  3380. org-table-move-column-right org-table-move-row
  3381. org-table-move-row-down org-table-move-row-up
  3382. org-table-next-field org-table-next-row org-table-paste-rectangle
  3383. org-table-previous-field org-table-recalculate
  3384. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  3385. org-table-toggle-coordinate-overlays
  3386. org-table-toggle-formula-debugger org-table-wrap-region
  3387. orgtbl-mode turn-on-orgtbl org-table-to-lisp
  3388. orgtbl-to-generic orgtbl-to-tsv orgtbl-to-csv orgtbl-to-latex
  3389. orgtbl-to-orgtbl orgtbl-to-html orgtbl-to-texinfo)))
  3390. (defun org-at-table-p (&optional table-type)
  3391. "Return t if the cursor is inside an org-type table.
  3392. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  3393. (if org-enable-table-editor
  3394. (save-excursion
  3395. (beginning-of-line 1)
  3396. (looking-at (if table-type org-table-any-line-regexp
  3397. org-table-line-regexp)))
  3398. nil))
  3399. (defsubst org-table-p () (org-at-table-p))
  3400. (defun org-at-table.el-p ()
  3401. "Return t if and only if we are at a table.el table."
  3402. (and (org-at-table-p 'any)
  3403. (save-excursion
  3404. (goto-char (org-table-begin 'any))
  3405. (looking-at org-table1-hline-regexp))))
  3406. (defun org-table-recognize-table.el ()
  3407. "If there is a table.el table nearby, recognize it and move into it."
  3408. (if org-table-tab-recognizes-table.el
  3409. (if (org-at-table.el-p)
  3410. (progn
  3411. (beginning-of-line 1)
  3412. (if (looking-at org-table-dataline-regexp)
  3413. nil
  3414. (if (looking-at org-table1-hline-regexp)
  3415. (progn
  3416. (beginning-of-line 2)
  3417. (if (looking-at org-table-any-border-regexp)
  3418. (beginning-of-line -1)))))
  3419. (if (re-search-forward "|" (org-table-end t) t)
  3420. (progn
  3421. (require 'table)
  3422. (if (table--at-cell-p (point))
  3423. t
  3424. (message "recognizing table.el table...")
  3425. (table-recognize-table)
  3426. (message "recognizing table.el table...done")))
  3427. (error "This should not happen"))
  3428. t)
  3429. nil)
  3430. nil))
  3431. (defun org-at-table-hline-p ()
  3432. "Return t if the cursor is inside a hline in a table."
  3433. (if org-enable-table-editor
  3434. (save-excursion
  3435. (beginning-of-line 1)
  3436. (looking-at org-table-hline-regexp))
  3437. nil))
  3438. (defvar org-table-clean-did-remove-column nil)
  3439. (defun org-table-map-tables (function &optional quietly)
  3440. "Apply FUNCTION to the start of all tables in the buffer."
  3441. (save-excursion
  3442. (save-restriction
  3443. (widen)
  3444. (goto-char (point-min))
  3445. (while (re-search-forward org-table-any-line-regexp nil t)
  3446. (unless quietly
  3447. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
  3448. (beginning-of-line 1)
  3449. (when (and (looking-at org-table-line-regexp)
  3450. ;; Exclude tables in src/example/verbatim/clocktable blocks
  3451. (not (org-in-block-p '("src" "example"))))
  3452. (save-excursion (funcall function))
  3453. (or (looking-at org-table-line-regexp)
  3454. (forward-char 1)))
  3455. (re-search-forward org-table-any-border-regexp nil 1))))
  3456. (unless quietly (message "Mapping tables: done")))
  3457. ;; Declare and autoload functions from org-exp.el & Co
  3458. (declare-function org-default-export-plist "org-exp")
  3459. (declare-function org-infile-export-plist "org-exp")
  3460. (declare-function org-get-current-options "org-exp")
  3461. (eval-and-compile
  3462. (org-autoload "org-exp"
  3463. '(org-export org-export-visible
  3464. org-insert-export-options-template
  3465. org-table-clean-before-export))
  3466. (org-autoload "org-ascii"
  3467. '(org-export-as-ascii org-export-ascii-preprocess
  3468. org-export-as-ascii-to-buffer org-replace-region-by-ascii
  3469. org-export-region-as-ascii))
  3470. (org-autoload "org-latex"
  3471. '(org-export-as-latex-batch org-export-as-latex-to-buffer
  3472. org-replace-region-by-latex org-export-region-as-latex
  3473. org-export-as-latex org-export-as-pdf
  3474. org-export-as-pdf-and-open))
  3475. (org-autoload "org-html"
  3476. '(org-export-as-html-and-open
  3477. org-export-as-html-batch org-export-as-html-to-buffer
  3478. org-replace-region-by-html org-export-region-as-html
  3479. org-export-as-html))
  3480. (org-autoload "org-docbook"
  3481. '(org-export-as-docbook-batch org-export-as-docbook-to-buffer
  3482. org-replace-region-by-docbook org-export-region-as-docbook
  3483. org-export-as-docbook-pdf org-export-as-docbook-pdf-and-open
  3484. org-export-as-docbook))
  3485. (org-autoload "org-icalendar"
  3486. '(org-export-icalendar-this-file
  3487. org-export-icalendar-all-agenda-files
  3488. org-export-icalendar-combine-agenda-files))
  3489. (org-autoload "org-xoxo" '(org-export-as-xoxo))
  3490. (org-autoload "org-beamer" '(org-beamer-mode org-beamer-sectioning)))
  3491. ;; Declare and autoload functions from org-agenda.el
  3492. (eval-and-compile
  3493. (org-autoload "org-agenda"
  3494. '(org-agenda org-agenda-list org-search-view
  3495. org-todo-list org-tags-view org-agenda-list-stuck-projects
  3496. org-diary org-agenda-to-appt
  3497. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  3498. ;; Autoload org-remember
  3499. (eval-and-compile
  3500. (org-autoload "org-remember"
  3501. '(org-remember-insinuate org-remember-annotation
  3502. org-remember-apply-template org-remember org-remember-handler)))
  3503. (eval-and-compile
  3504. (org-autoload "org-capture"
  3505. '(org-capture org-capture-insert-template-here
  3506. org-capture-import-remember-templates)))
  3507. ;; Autoload org-clock.el
  3508. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  3509. (beg end))
  3510. (declare-function org-clock-update-mode-line "org-clock" ())
  3511. (declare-function org-resolve-clocks "org-clock"
  3512. (&optional also-non-dangling-p prompt last-valid))
  3513. (defvar org-clock-start-time)
  3514. (defvar org-clock-marker (make-marker)
  3515. "Marker recording the last clock-in.")
  3516. (defvar org-clock-hd-marker (make-marker)
  3517. "Marker recording the last clock-in, but the headline position.")
  3518. (defvar org-clock-heading ""
  3519. "The heading of the current clock entry.")
  3520. (defun org-clock-is-active ()
  3521. "Return non-nil if clock is currently running.
  3522. The return value is actually the clock marker."
  3523. (marker-buffer org-clock-marker))
  3524. (eval-and-compile
  3525. (org-autoload
  3526. "org-clock"
  3527. '(org-clock-in org-clock-out org-clock-cancel
  3528. org-clock-goto org-clock-sum org-clock-display
  3529. org-clock-remove-overlays org-clock-report
  3530. org-clocktable-shift org-dblock-write:clocktable
  3531. org-get-clocktable org-resolve-clocks)))
  3532. (defun org-clock-update-time-maybe ()
  3533. "If this is a CLOCK line, update it and return t.
  3534. Otherwise, return nil."
  3535. (interactive)
  3536. (save-excursion
  3537. (beginning-of-line 1)
  3538. (skip-chars-forward " \t")
  3539. (when (looking-at org-clock-string)
  3540. (let ((re (concat "[ \t]*" org-clock-string
  3541. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  3542. "\\([ \t]*=>.*\\)?\\)?"))
  3543. ts te h m s neg)
  3544. (cond
  3545. ((not (looking-at re))
  3546. nil)
  3547. ((not (match-end 2))
  3548. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3549. (> org-clock-marker (point))
  3550. (<= org-clock-marker (point-at-eol)))
  3551. ;; The clock is running here
  3552. (setq org-clock-start-time
  3553. (apply 'encode-time
  3554. (org-parse-time-string (match-string 1))))
  3555. (org-clock-update-mode-line)))
  3556. (t
  3557. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  3558. (end-of-line 1)
  3559. (setq ts (match-string 1)
  3560. te (match-string 3))
  3561. (setq s (- (org-float-time
  3562. (apply 'encode-time (org-parse-time-string te)))
  3563. (org-float-time
  3564. (apply 'encode-time (org-parse-time-string ts))))
  3565. neg (< s 0)
  3566. s (abs s)
  3567. h (floor (/ s 3600))
  3568. s (- s (* 3600 h))
  3569. m (floor (/ s 60))
  3570. s (- s (* 60 s)))
  3571. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  3572. t))))))
  3573. (defun org-check-running-clock ()
  3574. "Check if the current buffer contains the running clock.
  3575. If yes, offer to stop it and to save the buffer with the changes."
  3576. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3577. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3578. (buffer-name))))
  3579. (org-clock-out)
  3580. (when (y-or-n-p "Save changed buffer?")
  3581. (save-buffer))))
  3582. (defun org-clocktable-try-shift (dir n)
  3583. "Check if this line starts a clock table, if yes, shift the time block."
  3584. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  3585. (org-clocktable-shift dir n)))
  3586. ;; Autoload org-timer.el
  3587. (eval-and-compile
  3588. (org-autoload
  3589. "org-timer"
  3590. '(org-timer-start org-timer org-timer-item
  3591. org-timer-change-times-in-region
  3592. org-timer-set-timer
  3593. org-timer-reset-timers
  3594. org-timer-show-remaining-time)))
  3595. ;; Autoload org-feed.el
  3596. (eval-and-compile
  3597. (org-autoload
  3598. "org-feed"
  3599. '(org-feed-update org-feed-update-all org-feed-goto-inbox)))
  3600. ;; Autoload org-indent.el
  3601. ;; Define the variable already here, to make sure we have it.
  3602. (defvar org-indent-mode nil
  3603. "Non-nil if Org-Indent mode is enabled.
  3604. Use the command `org-indent-mode' to change this variable.")
  3605. (eval-and-compile
  3606. (org-autoload
  3607. "org-indent"
  3608. '(org-indent-mode)))
  3609. ;; Autoload org-mobile.el
  3610. (eval-and-compile
  3611. (org-autoload
  3612. "org-mobile"
  3613. '(org-mobile-push org-mobile-pull org-mobile-create-sumo-agenda)))
  3614. ;; Autoload archiving code
  3615. ;; The stuff that is needed for cycling and tags has to be defined here.
  3616. (defgroup org-archive nil
  3617. "Options concerning archiving in Org-mode."
  3618. :tag "Org Archive"
  3619. :group 'org-structure)
  3620. (defcustom org-archive-location "%s_archive::"
  3621. "The location where subtrees should be archived.
  3622. The value of this variable is a string, consisting of two parts,
  3623. separated by a double-colon. The first part is a filename and
  3624. the second part is a headline.
  3625. When the filename is omitted, archiving happens in the same file.
  3626. %s in the filename will be replaced by the current file
  3627. name (without the directory part). Archiving to a different file
  3628. is useful to keep archived entries from contributing to the
  3629. Org-mode Agenda.
  3630. The archived entries will be filed as subtrees of the specified
  3631. headline. When the headline is omitted, the subtrees are simply
  3632. filed away at the end of the file, as top-level entries. Also in
  3633. the heading you can use %s to represent the file name, this can be
  3634. useful when using the same archive for a number of different files.
  3635. Here are a few examples:
  3636. \"%s_archive::\"
  3637. If the current file is Projects.org, archive in file
  3638. Projects.org_archive, as top-level trees. This is the default.
  3639. \"::* Archived Tasks\"
  3640. Archive in the current file, under the top-level headline
  3641. \"* Archived Tasks\".
  3642. \"~/org/archive.org::\"
  3643. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3644. \"~/org/archive.org::* From %s\"
  3645. Archive in file ~/org/archive.org (absolute path), under headlines
  3646. \"From FILENAME\" where file name is the current file name.
  3647. \"basement::** Finished Tasks\"
  3648. Archive in file ./basement (relative path), as level 3 trees
  3649. below the level 2 heading \"** Finished Tasks\".
  3650. You may set this option on a per-file basis by adding to the buffer a
  3651. line like
  3652. #+ARCHIVE: basement::** Finished Tasks
  3653. You may also define it locally for a subtree by setting an ARCHIVE property
  3654. in the entry. If such a property is found in an entry, or anywhere up
  3655. the hierarchy, it will be used."
  3656. :group 'org-archive
  3657. :type 'string)
  3658. (defcustom org-archive-tag "ARCHIVE"
  3659. "The tag that marks a subtree as archived.
  3660. An archived subtree does not open during visibility cycling, and does
  3661. not contribute to the agenda listings.
  3662. After changing this, font-lock must be restarted in the relevant buffers to
  3663. get the proper fontification."
  3664. :group 'org-archive
  3665. :group 'org-keywords
  3666. :type 'string)
  3667. (defcustom org-agenda-skip-archived-trees t
  3668. "Non-nil means the agenda will skip any items located in archived trees.
  3669. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3670. variable is no longer recommended, you should leave it at the value t.
  3671. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3672. :group 'org-archive
  3673. :group 'org-agenda-skip
  3674. :type 'boolean)
  3675. (defcustom org-columns-skip-archived-trees t
  3676. "Non-nil means ignore archived trees when creating column view."
  3677. :group 'org-archive
  3678. :group 'org-properties
  3679. :type 'boolean)
  3680. (defcustom org-cycle-open-archived-trees nil
  3681. "Non-nil means `org-cycle' will open archived trees.
  3682. An archived tree is a tree marked with the tag ARCHIVE.
  3683. When nil, archived trees will stay folded. You can still open them with
  3684. normal outline commands like `show-all', but not with the cycling commands."
  3685. :group 'org-archive
  3686. :group 'org-cycle
  3687. :type 'boolean)
  3688. (defcustom org-sparse-tree-open-archived-trees nil
  3689. "Non-nil means sparse tree construction shows matches in archived trees.
  3690. When nil, matches in these trees are highlighted, but the trees are kept in
  3691. collapsed state."
  3692. :group 'org-archive
  3693. :group 'org-sparse-trees
  3694. :type 'boolean)
  3695. (defun org-cycle-hide-archived-subtrees (state)
  3696. "Re-hide all archived subtrees after a visibility state change."
  3697. (when (and (not org-cycle-open-archived-trees)
  3698. (not (memq state '(overview folded))))
  3699. (save-excursion
  3700. (let* ((globalp (memq state '(contents all)))
  3701. (beg (if globalp (point-min) (point)))
  3702. (end (if globalp (point-max) (org-end-of-subtree t))))
  3703. (org-hide-archived-subtrees beg end)
  3704. (goto-char beg)
  3705. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3706. (message "%s" (substitute-command-keys
  3707. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3708. (defun org-force-cycle-archived ()
  3709. "Cycle subtree even if it is archived."
  3710. (interactive)
  3711. (setq this-command 'org-cycle)
  3712. (let ((org-cycle-open-archived-trees t))
  3713. (call-interactively 'org-cycle)))
  3714. (defun org-hide-archived-subtrees (beg end)
  3715. "Re-hide all archived subtrees after a visibility state change."
  3716. (save-excursion
  3717. (let* ((re (concat ":" org-archive-tag ":")))
  3718. (goto-char beg)
  3719. (while (re-search-forward re end t)
  3720. (when (org-at-heading-p)
  3721. (org-flag-subtree t)
  3722. (org-end-of-subtree t))))))
  3723. (defun org-flag-subtree (flag)
  3724. (save-excursion
  3725. (org-back-to-heading t)
  3726. (outline-end-of-heading)
  3727. (outline-flag-region (point)
  3728. (progn (org-end-of-subtree t) (point))
  3729. flag)))
  3730. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3731. (eval-and-compile
  3732. (org-autoload "org-archive"
  3733. '(org-add-archive-files org-archive-subtree
  3734. org-archive-to-archive-sibling org-toggle-archive-tag
  3735. org-archive-subtree-default
  3736. org-archive-subtree-default-with-confirmation)))
  3737. ;; Autoload Column View Code
  3738. (declare-function org-columns-number-to-string "org-colview")
  3739. (declare-function org-columns-get-format-and-top-level "org-colview")
  3740. (declare-function org-columns-compute "org-colview")
  3741. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  3742. '(org-columns-number-to-string org-columns-get-format-and-top-level
  3743. org-columns-compute org-agenda-columns org-columns-remove-overlays
  3744. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  3745. ;; Autoload ID code
  3746. (declare-function org-id-store-link "org-id")
  3747. (declare-function org-id-locations-load "org-id")
  3748. (declare-function org-id-locations-save "org-id")
  3749. (defvar org-id-track-globally)
  3750. (org-autoload "org-id"
  3751. '(org-id-get-create org-id-new org-id-copy org-id-get
  3752. org-id-get-with-outline-path-completion
  3753. org-id-get-with-outline-drilling org-id-store-link
  3754. org-id-goto org-id-find org-id-store-link))
  3755. ;; Autoload Plotting Code
  3756. (org-autoload "org-plot"
  3757. '(org-plot/gnuplot))
  3758. ;;; Variables for pre-computed regular expressions, all buffer local
  3759. (defvar org-drawer-regexp nil
  3760. "Matches first line of a hidden block.")
  3761. (make-variable-buffer-local 'org-drawer-regexp)
  3762. (defvar org-todo-regexp nil
  3763. "Matches any of the TODO state keywords.")
  3764. (make-variable-buffer-local 'org-todo-regexp)
  3765. (defvar org-not-done-regexp nil
  3766. "Matches any of the TODO state keywords except the last one.")
  3767. (make-variable-buffer-local 'org-not-done-regexp)
  3768. (defvar org-not-done-heading-regexp nil
  3769. "Matches a TODO headline that is not done.")
  3770. (make-variable-buffer-local 'org-not-done-regexp)
  3771. (defvar org-todo-line-regexp nil
  3772. "Matches a headline and puts TODO state into group 2 if present.")
  3773. (make-variable-buffer-local 'org-todo-line-regexp)
  3774. (defvar org-complex-heading-regexp nil
  3775. "Matches a headline and puts everything into groups:
  3776. group 1: the stars
  3777. group 2: The todo keyword, maybe
  3778. group 3: Priority cookie
  3779. group 4: True headline
  3780. group 5: Tags")
  3781. (make-variable-buffer-local 'org-complex-heading-regexp)
  3782. (defvar org-complex-heading-regexp-format nil
  3783. "Printf format to make regexp to match an exact headline.
  3784. This regexp will match the headline of any node which has the
  3785. exact headline text that is put into the format, but may have any
  3786. TODO state, priority and tags.")
  3787. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  3788. (defvar org-todo-line-tags-regexp nil
  3789. "Matches a headline and puts TODO state into group 2 if present.
  3790. Also put tags into group 4 if tags are present.")
  3791. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3792. (defvar org-ds-keyword-length 12
  3793. "Maximum length of the Deadline and SCHEDULED keywords.")
  3794. (make-variable-buffer-local 'org-ds-keyword-length)
  3795. (defvar org-deadline-regexp nil
  3796. "Matches the DEADLINE keyword.")
  3797. (make-variable-buffer-local 'org-deadline-regexp)
  3798. (defvar org-deadline-time-regexp nil
  3799. "Matches the DEADLINE keyword together with a time stamp.")
  3800. (make-variable-buffer-local 'org-deadline-time-regexp)
  3801. (defvar org-deadline-line-regexp nil
  3802. "Matches the DEADLINE keyword and the rest of the line.")
  3803. (make-variable-buffer-local 'org-deadline-line-regexp)
  3804. (defvar org-scheduled-regexp nil
  3805. "Matches the SCHEDULED keyword.")
  3806. (make-variable-buffer-local 'org-scheduled-regexp)
  3807. (defvar org-scheduled-time-regexp nil
  3808. "Matches the SCHEDULED keyword together with a time stamp.")
  3809. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3810. (defvar org-closed-time-regexp nil
  3811. "Matches the CLOSED keyword together with a time stamp.")
  3812. (make-variable-buffer-local 'org-closed-time-regexp)
  3813. (defvar org-keyword-time-regexp nil
  3814. "Matches any of the 4 keywords, together with the time stamp.")
  3815. (make-variable-buffer-local 'org-keyword-time-regexp)
  3816. (defvar org-keyword-time-not-clock-regexp nil
  3817. "Matches any of the 3 keywords, together with the time stamp.")
  3818. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3819. (defvar org-maybe-keyword-time-regexp nil
  3820. "Matches a timestamp, possibly preceded by a keyword.")
  3821. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3822. (defvar org-planning-or-clock-line-re nil
  3823. "Matches a line with planning or clock info.")
  3824. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  3825. (defvar org-all-time-keywords nil
  3826. "List of time keywords.")
  3827. (make-variable-buffer-local 'org-all-time-keywords)
  3828. (defconst org-plain-time-of-day-regexp
  3829. (concat
  3830. "\\(\\<[012]?[0-9]"
  3831. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3832. "\\(--?"
  3833. "\\(\\<[012]?[0-9]"
  3834. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3835. "\\)?")
  3836. "Regular expression to match a plain time or time range.
  3837. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3838. groups carry important information:
  3839. 0 the full match
  3840. 1 the first time, range or not
  3841. 8 the second time, if it is a range.")
  3842. (defconst org-plain-time-extension-regexp
  3843. (concat
  3844. "\\(\\<[012]?[0-9]"
  3845. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3846. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  3847. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  3848. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3849. groups carry important information:
  3850. 0 the full match
  3851. 7 hours of duration
  3852. 9 minutes of duration")
  3853. (defconst org-stamp-time-of-day-regexp
  3854. (concat
  3855. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  3856. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  3857. "\\(--?"
  3858. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  3859. "Regular expression to match a timestamp time or time range.
  3860. After a match, the following groups carry important information:
  3861. 0 the full match
  3862. 1 date plus weekday, for back referencing to make sure both times are on the same day
  3863. 2 the first time, range or not
  3864. 4 the second time, if it is a range.")
  3865. (defconst org-startup-options
  3866. '(("fold" org-startup-folded t)
  3867. ("overview" org-startup-folded t)
  3868. ("nofold" org-startup-folded nil)
  3869. ("showall" org-startup-folded nil)
  3870. ("showeverything" org-startup-folded showeverything)
  3871. ("content" org-startup-folded content)
  3872. ("indent" org-startup-indented t)
  3873. ("noindent" org-startup-indented nil)
  3874. ("hidestars" org-hide-leading-stars t)
  3875. ("showstars" org-hide-leading-stars nil)
  3876. ("odd" org-odd-levels-only t)
  3877. ("oddeven" org-odd-levels-only nil)
  3878. ("align" org-startup-align-all-tables t)
  3879. ("noalign" org-startup-align-all-tables nil)
  3880. ("inlineimages" org-startup-with-inline-images t)
  3881. ("noinlineimages" org-startup-with-inline-images nil)
  3882. ("customtime" org-display-custom-times t)
  3883. ("logdone" org-log-done time)
  3884. ("lognotedone" org-log-done note)
  3885. ("nologdone" org-log-done nil)
  3886. ("lognoteclock-out" org-log-note-clock-out t)
  3887. ("nolognoteclock-out" org-log-note-clock-out nil)
  3888. ("logrepeat" org-log-repeat state)
  3889. ("lognoterepeat" org-log-repeat note)
  3890. ("nologrepeat" org-log-repeat nil)
  3891. ("logreschedule" org-log-reschedule time)
  3892. ("lognotereschedule" org-log-reschedule note)
  3893. ("nologreschedule" org-log-reschedule nil)
  3894. ("logredeadline" org-log-redeadline time)
  3895. ("lognoteredeadline" org-log-redeadline note)
  3896. ("nologredeadline" org-log-redeadline nil)
  3897. ("logrefile" org-log-refile time)
  3898. ("lognoterefile" org-log-refile note)
  3899. ("nologrefile" org-log-refile nil)
  3900. ("fninline" org-footnote-define-inline t)
  3901. ("nofninline" org-footnote-define-inline nil)
  3902. ("fnlocal" org-footnote-section nil)
  3903. ("fnauto" org-footnote-auto-label t)
  3904. ("fnprompt" org-footnote-auto-label nil)
  3905. ("fnconfirm" org-footnote-auto-label confirm)
  3906. ("fnplain" org-footnote-auto-label plain)
  3907. ("fnadjust" org-footnote-auto-adjust t)
  3908. ("nofnadjust" org-footnote-auto-adjust nil)
  3909. ("constcgs" constants-unit-system cgs)
  3910. ("constSI" constants-unit-system SI)
  3911. ("noptag" org-tag-persistent-alist nil)
  3912. ("hideblocks" org-hide-block-startup t)
  3913. ("nohideblocks" org-hide-block-startup nil)
  3914. ("beamer" org-startup-with-beamer-mode t)
  3915. ("entitiespretty" org-pretty-entities t)
  3916. ("entitiesplain" org-pretty-entities nil))
  3917. "Variable associated with STARTUP options for org-mode.
  3918. Each element is a list of three items: The startup options as written
  3919. in the #+STARTUP line, the corresponding variable, and the value to
  3920. set this variable to if the option is found. An optional forth element PUSH
  3921. means to push this value onto the list in the variable.")
  3922. (defun org-update-property-plist (key val props)
  3923. "Update PROPS with KEY and VAL."
  3924. (let* ((appending (string= "+" (substring key (- (length key) 1))))
  3925. (key (if appending (substring key 0 (- (length key) 1)) key))
  3926. (remainder (org-remove-if (lambda (p) (string= (car p) key)) props))
  3927. (previous (cdr (assoc key props))))
  3928. (if appending
  3929. (cons (cons key (if previous (concat previous " " val) val)) remainder)
  3930. (cons (cons key val) remainder))))
  3931. (defconst org-block-regexp
  3932. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  3933. "Regular expression for hiding blocks.")
  3934. (defconst org-heading-keyword-regexp-format
  3935. "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  3936. "Printf format for a regexp matching an headline with some keyword.
  3937. This regexp will match the headline of any node which has the
  3938. exact keyword that is put into the format. The keyword isn't in
  3939. any group by default, but the stars and the body are.")
  3940. (defconst org-heading-keyword-maybe-regexp-format
  3941. "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
  3942. "Printf format for a regexp matching an headline, possibly with some keyword.
  3943. This regexp can match any headline with the specified keyword, or
  3944. without a keyword. The keyword isn't in any group by default,
  3945. but the stars and the body are.")
  3946. (defun org-set-regexps-and-options ()
  3947. "Precompute regular expressions for current buffer."
  3948. (when (eq major-mode 'org-mode)
  3949. (org-set-local 'org-todo-kwd-alist nil)
  3950. (org-set-local 'org-todo-key-alist nil)
  3951. (org-set-local 'org-todo-key-trigger nil)
  3952. (org-set-local 'org-todo-keywords-1 nil)
  3953. (org-set-local 'org-done-keywords nil)
  3954. (org-set-local 'org-todo-heads nil)
  3955. (org-set-local 'org-todo-sets nil)
  3956. (org-set-local 'org-todo-log-states nil)
  3957. (org-set-local 'org-file-properties nil)
  3958. (org-set-local 'org-file-tags nil)
  3959. (let ((re (org-make-options-regexp
  3960. '("CATEGORY" "TODO" "COLUMNS"
  3961. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  3962. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE" "LATEX_CLASS"
  3963. "OPTIONS")
  3964. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  3965. (splitre "[ \t]+")
  3966. (scripts org-use-sub-superscripts)
  3967. kwds kws0 kwsa key log value cat arch tags const links hw dws
  3968. tail sep kws1 prio props ftags drawers beamer-p
  3969. ext-setup-or-nil setup-contents (start 0))
  3970. (save-excursion
  3971. (save-restriction
  3972. (widen)
  3973. (goto-char (point-min))
  3974. (while (or (and ext-setup-or-nil
  3975. (string-match re ext-setup-or-nil start)
  3976. (setq start (match-end 0)))
  3977. (and (setq ext-setup-or-nil nil start 0)
  3978. (re-search-forward re nil t)))
  3979. (setq key (upcase (match-string 1 ext-setup-or-nil))
  3980. value (org-match-string-no-properties 2 ext-setup-or-nil))
  3981. (if (stringp value) (setq value (org-trim value)))
  3982. (cond
  3983. ((equal key "CATEGORY")
  3984. (setq cat value))
  3985. ((member key '("SEQ_TODO" "TODO"))
  3986. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3987. ((equal key "TYP_TODO")
  3988. (push (cons 'type (org-split-string value splitre)) kwds))
  3989. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  3990. ;; general TODO-like setup
  3991. (push (cons (intern (downcase (match-string 1 key)))
  3992. (org-split-string value splitre)) kwds))
  3993. ((equal key "TAGS")
  3994. (setq tags (append tags (if tags '("\\n") nil)
  3995. (org-split-string value splitre))))
  3996. ((equal key "COLUMNS")
  3997. (org-set-local 'org-columns-default-format value))
  3998. ((equal key "LINK")
  3999. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  4000. (push (cons (match-string 1 value)
  4001. (org-trim (match-string 2 value)))
  4002. links)))
  4003. ((equal key "PRIORITIES")
  4004. (setq prio (org-split-string value " +")))
  4005. ((equal key "PROPERTY")
  4006. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4007. (setq props (org-update-property-plist (match-string 1 value)
  4008. (match-string 2 value)
  4009. props))))
  4010. ((equal key "FILETAGS")
  4011. (when (string-match "\\S-" value)
  4012. (setq ftags
  4013. (append
  4014. ftags
  4015. (apply 'append
  4016. (mapcar (lambda (x) (org-split-string x ":"))
  4017. (org-split-string value)))))))
  4018. ((equal key "DRAWERS")
  4019. (setq drawers (delete-dups (append org-drawers (org-split-string value splitre)))))
  4020. ((equal key "CONSTANTS")
  4021. (setq const (append const (org-split-string value splitre))))
  4022. ((equal key "STARTUP")
  4023. (let ((opts (org-split-string value splitre))
  4024. l var val)
  4025. (while (setq l (pop opts))
  4026. (when (setq l (assoc l org-startup-options))
  4027. (setq var (nth 1 l) val (nth 2 l))
  4028. (if (not (nth 3 l))
  4029. (set (make-local-variable var) val)
  4030. (if (not (listp (symbol-value var)))
  4031. (set (make-local-variable var) nil))
  4032. (set (make-local-variable var) (symbol-value var))
  4033. (add-to-list var val))))))
  4034. ((equal key "ARCHIVE")
  4035. (setq arch value)
  4036. (remove-text-properties 0 (length arch)
  4037. '(face t fontified t) arch))
  4038. ((equal key "LATEX_CLASS")
  4039. (setq beamer-p (equal value "beamer")))
  4040. ((equal key "OPTIONS")
  4041. (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value)
  4042. (setq scripts (read (match-string 2 value)))))
  4043. ((equal key "SETUPFILE")
  4044. (setq setup-contents (org-file-contents
  4045. (expand-file-name
  4046. (org-remove-double-quotes value))
  4047. 'noerror))
  4048. (if (not ext-setup-or-nil)
  4049. (setq ext-setup-or-nil setup-contents start 0)
  4050. (setq ext-setup-or-nil
  4051. (concat (substring ext-setup-or-nil 0 start)
  4052. "\n" setup-contents "\n"
  4053. (substring ext-setup-or-nil start)))))))
  4054. ;; search for property blocks
  4055. (goto-char (point-min))
  4056. (while (re-search-forward org-block-regexp nil t)
  4057. (when (equal "PROPERTY" (upcase (match-string 1)))
  4058. (setq value (replace-regexp-in-string
  4059. "[\n\r]" " " (match-string 4)))
  4060. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4061. (setq props (org-update-property-plist (match-string 1 value)
  4062. (match-string 2 value)
  4063. props)))))))
  4064. (org-set-local 'org-use-sub-superscripts scripts)
  4065. (when cat
  4066. (org-set-local 'org-category (intern cat))
  4067. (push (cons "CATEGORY" cat) props))
  4068. (when prio
  4069. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  4070. (setq prio (mapcar 'string-to-char prio))
  4071. (org-set-local 'org-highest-priority (nth 0 prio))
  4072. (org-set-local 'org-lowest-priority (nth 1 prio))
  4073. (org-set-local 'org-default-priority (nth 2 prio)))
  4074. (and props (org-set-local 'org-file-properties (nreverse props)))
  4075. (and ftags (org-set-local 'org-file-tags
  4076. (mapcar 'org-add-prop-inherited ftags)))
  4077. (and drawers (org-set-local 'org-drawers drawers))
  4078. (and arch (org-set-local 'org-archive-location arch))
  4079. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  4080. ;; Process the TODO keywords
  4081. (unless kwds
  4082. ;; Use the global values as if they had been given locally.
  4083. (setq kwds (default-value 'org-todo-keywords))
  4084. (if (stringp (car kwds))
  4085. (setq kwds (list (cons org-todo-interpretation
  4086. (default-value 'org-todo-keywords)))))
  4087. (setq kwds (reverse kwds)))
  4088. (setq kwds (nreverse kwds))
  4089. (let (inter kws kw)
  4090. (while (setq kws (pop kwds))
  4091. (let ((kws (or
  4092. (run-hook-with-args-until-success
  4093. 'org-todo-setup-filter-hook kws)
  4094. kws)))
  4095. (setq inter (pop kws) sep (member "|" kws)
  4096. kws0 (delete "|" (copy-sequence kws))
  4097. kwsa nil
  4098. kws1 (mapcar
  4099. (lambda (x)
  4100. ;; 1 2
  4101. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  4102. (progn
  4103. (setq kw (match-string 1 x)
  4104. key (and (match-end 2) (match-string 2 x))
  4105. log (org-extract-log-state-settings x))
  4106. (push (cons kw (and key (string-to-char key))) kwsa)
  4107. (and log (push log org-todo-log-states))
  4108. kw)
  4109. (error "Invalid TODO keyword %s" x)))
  4110. kws0)
  4111. kwsa (if kwsa (append '((:startgroup))
  4112. (nreverse kwsa)
  4113. '((:endgroup))))
  4114. hw (car kws1)
  4115. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  4116. tail (list inter hw (car dws) (org-last dws))))
  4117. (add-to-list 'org-todo-heads hw 'append)
  4118. (push kws1 org-todo-sets)
  4119. (setq org-done-keywords (append org-done-keywords dws nil))
  4120. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  4121. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  4122. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  4123. (setq org-todo-sets (nreverse org-todo-sets)
  4124. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4125. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  4126. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  4127. ;; Process the constants
  4128. (when const
  4129. (let (e cst)
  4130. (while (setq e (pop const))
  4131. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  4132. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  4133. (setq org-table-formula-constants-local cst)))
  4134. ;; Process the tags.
  4135. (when tags
  4136. (let (e tgs)
  4137. (while (setq e (pop tags))
  4138. (cond
  4139. ((equal e "{") (push '(:startgroup) tgs))
  4140. ((equal e "}") (push '(:endgroup) tgs))
  4141. ((equal e "\\n") (push '(:newline) tgs))
  4142. ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e)
  4143. (push (cons (match-string 1 e)
  4144. (string-to-char (match-string 2 e)))
  4145. tgs))
  4146. (t (push (list e) tgs))))
  4147. (org-set-local 'org-tag-alist nil)
  4148. (while (setq e (pop tgs))
  4149. (or (and (stringp (car e))
  4150. (assoc (car e) org-tag-alist))
  4151. (push e org-tag-alist)))))
  4152. ;; Compute the regular expressions and other local variables.
  4153. ;; Using `org-outline-regexp-bol' would complicate them much,
  4154. ;; because of the fixed white space at the end of that string.
  4155. (if (not org-done-keywords)
  4156. (setq org-done-keywords (and org-todo-keywords-1
  4157. (list (org-last org-todo-keywords-1)))))
  4158. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  4159. (length org-scheduled-string)
  4160. (length org-clock-string)
  4161. (length org-closed-string)))
  4162. org-drawer-regexp
  4163. (concat "^[ \t]*:\\("
  4164. (mapconcat 'regexp-quote org-drawers "\\|")
  4165. "\\):[ \t]*$")
  4166. org-not-done-keywords
  4167. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  4168. org-todo-regexp
  4169. (concat "\\("
  4170. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4171. "\\)")
  4172. org-not-done-regexp
  4173. (concat "\\("
  4174. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  4175. "\\)")
  4176. org-not-done-heading-regexp
  4177. (format org-heading-keyword-regexp-format org-not-done-regexp)
  4178. org-todo-line-regexp
  4179. (format org-heading-keyword-maybe-regexp-format org-todo-regexp)
  4180. org-complex-heading-regexp
  4181. (concat "^\\(\\*+\\)"
  4182. "\\(?: +" org-todo-regexp "\\)?"
  4183. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4184. "\\(?: +\\(.*?\\)\\)?"
  4185. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
  4186. "[ \t]*$")
  4187. org-complex-heading-regexp-format
  4188. (concat "^\\(\\*+\\)"
  4189. "\\(?: +" org-todo-regexp "\\)?"
  4190. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4191. "\\(?: +"
  4192. ;; Stats cookies can be stuck to body.
  4193. "\\(?:\\[[0-9%%/]+\\] *\\)?"
  4194. "\\(%s\\)"
  4195. "\\(?: *\\[[0-9%%/]+\\]\\)?"
  4196. "\\)"
  4197. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
  4198. "[ \t]*$")
  4199. org-todo-line-tags-regexp
  4200. (concat "^\\(\\*+\\)"
  4201. "\\(?: +" org-todo-regexp "\\)?"
  4202. "\\(?: +\\(.*?\\)\\)?"
  4203. (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
  4204. "[ \t]*$")
  4205. org-deadline-regexp (concat "\\<" org-deadline-string)
  4206. org-deadline-time-regexp
  4207. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  4208. org-deadline-line-regexp
  4209. (concat "\\<\\(" org-deadline-string "\\).*")
  4210. org-scheduled-regexp
  4211. (concat "\\<" org-scheduled-string)
  4212. org-scheduled-time-regexp
  4213. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  4214. org-closed-time-regexp
  4215. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  4216. org-keyword-time-regexp
  4217. (concat "\\<\\(" org-scheduled-string
  4218. "\\|" org-deadline-string
  4219. "\\|" org-closed-string
  4220. "\\|" org-clock-string "\\)"
  4221. " *[[<]\\([^]>]+\\)[]>]")
  4222. org-keyword-time-not-clock-regexp
  4223. (concat "\\<\\(" org-scheduled-string
  4224. "\\|" org-deadline-string
  4225. "\\|" org-closed-string
  4226. "\\)"
  4227. " *[[<]\\([^]>]+\\)[]>]")
  4228. org-maybe-keyword-time-regexp
  4229. (concat "\\(\\<\\(" org-scheduled-string
  4230. "\\|" org-deadline-string
  4231. "\\|" org-closed-string
  4232. "\\|" org-clock-string "\\)\\)?"
  4233. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4234. org-planning-or-clock-line-re
  4235. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  4236. "\\|" org-deadline-string
  4237. "\\|" org-closed-string "\\|" org-clock-string
  4238. "\\)\\>\\)")
  4239. org-all-time-keywords
  4240. (mapcar (lambda (w) (substring w 0 -1))
  4241. (list org-scheduled-string org-deadline-string
  4242. org-clock-string org-closed-string))
  4243. )
  4244. (org-compute-latex-and-specials-regexp)
  4245. (org-set-font-lock-defaults))))
  4246. (defun org-file-contents (file &optional noerror)
  4247. "Return the contents of FILE, as a string."
  4248. (if (or (not file)
  4249. (not (file-readable-p file)))
  4250. (if noerror
  4251. (progn
  4252. (message "Cannot read file \"%s\"" file)
  4253. (ding) (sit-for 2)
  4254. "")
  4255. (error "Cannot read file \"%s\"" file))
  4256. (with-temp-buffer
  4257. (insert-file-contents file)
  4258. (buffer-string))))
  4259. (defun org-extract-log-state-settings (x)
  4260. "Extract the log state setting from a TODO keyword string.
  4261. This will extract info from a string like \"WAIT(w@/!)\"."
  4262. (let (kw key log1 log2)
  4263. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4264. (setq kw (match-string 1 x)
  4265. key (and (match-end 2) (match-string 2 x))
  4266. log1 (and (match-end 3) (match-string 3 x))
  4267. log2 (and (match-end 4) (match-string 4 x)))
  4268. (and (or log1 log2)
  4269. (list kw
  4270. (and log1 (if (equal log1 "!") 'time 'note))
  4271. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4272. (defun org-remove-keyword-keys (list)
  4273. "Remove a pair of parenthesis at the end of each string in LIST."
  4274. (mapcar (lambda (x)
  4275. (if (string-match "(.*)$" x)
  4276. (substring x 0 (match-beginning 0))
  4277. x))
  4278. list))
  4279. (defun org-assign-fast-keys (alist)
  4280. "Assign fast keys to a keyword-key alist.
  4281. Respect keys that are already there."
  4282. (let (new e (alt ?0))
  4283. (while (setq e (pop alist))
  4284. (if (or (memq (car e) '(:newline :endgroup :startgroup))
  4285. (cdr e)) ;; Key already assigned.
  4286. (push e new)
  4287. (let ((clist (string-to-list (downcase (car e))))
  4288. (used (append new alist)))
  4289. (when (= (car clist) ?@)
  4290. (pop clist))
  4291. (while (and clist (rassoc (car clist) used))
  4292. (pop clist))
  4293. (unless clist
  4294. (while (rassoc alt used)
  4295. (incf alt)))
  4296. (push (cons (car e) (or (car clist) alt)) new))))
  4297. (nreverse new)))
  4298. ;;; Some variables used in various places
  4299. (defvar org-window-configuration nil
  4300. "Used in various places to store a window configuration.")
  4301. (defvar org-selected-window nil
  4302. "Used in various places to store a window configuration.")
  4303. (defvar org-finish-function nil
  4304. "Function to be called when `C-c C-c' is used.
  4305. This is for getting out of special buffers like remember.")
  4306. ;; FIXME: Occasionally check by commenting these, to make sure
  4307. ;; no other functions uses these, forgetting to let-bind them.
  4308. (defvar entry)
  4309. (defvar org-last-state)
  4310. (defvar date)
  4311. ;; Defined somewhere in this file, but used before definition.
  4312. (defvar org-entities) ;; defined in org-entities.el
  4313. (defvar org-struct-menu)
  4314. (defvar org-org-menu)
  4315. (defvar org-tbl-menu)
  4316. ;;;; Define the Org-mode
  4317. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4318. (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"))
  4319. ;; We use a before-change function to check if a table might need
  4320. ;; an update.
  4321. (defvar org-table-may-need-update t
  4322. "Indicates that a table might need an update.
  4323. This variable is set by `org-before-change-function'.
  4324. `org-table-align' sets it back to nil.")
  4325. (defun org-before-change-function (beg end)
  4326. "Every change indicates that a table might need an update."
  4327. (setq org-table-may-need-update t))
  4328. (defvar org-mode-map)
  4329. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4330. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4331. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4332. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4333. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4334. (defvar org-table-buffer-is-an nil)
  4335. ;; `org-outline-regexp' ought to be a defconst but is let-binding in
  4336. ;; some places -- e.g. see the macro org-with-limited-levels.
  4337. ;;
  4338. ;; In Org buffers, the value of `outline-regexp' is that of
  4339. ;; `org-outline-regexp'. The only function still directly relying on
  4340. ;; `outline-regexp' is `org-overview' so that `org-cycle' can do its
  4341. ;; job when `orgstruct-mode' is active.
  4342. (defvar org-outline-regexp "\\*+ "
  4343. "Regexp to match Org headlines.")
  4344. (defconst org-outline-regexp-bol "^\\*+ "
  4345. "Regexp to match Org headlines.
  4346. This is similar to `org-outline-regexp' but additionally makes
  4347. sure that we are at the beginning of the line.")
  4348. (defconst org-heading-regexp "^\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  4349. "Matches an headline, putting stars and text into groups.
  4350. Stars are put in group 1 and the trimmed body in group 2.")
  4351. (defvar bidi-paragraph-direction)
  4352. (defvar buffer-face-mode-face)
  4353. ;;;###autoload
  4354. (define-derived-mode org-mode outline-mode "Org"
  4355. "Outline-based notes management and organizer, alias
  4356. \"Carsten's outline-mode for keeping track of everything.\"
  4357. Org-mode develops organizational tasks around a NOTES file which
  4358. contains information about projects as plain text. Org-mode is
  4359. implemented on top of outline-mode, which is ideal to keep the content
  4360. of large files well structured. It supports ToDo items, deadlines and
  4361. time stamps, which magically appear in the diary listing of the Emacs
  4362. calendar. Tables are easily created with a built-in table editor.
  4363. Plain text URL-like links connect to websites, emails (VM), Usenet
  4364. messages (Gnus), BBDB entries, and any files related to the project.
  4365. For printing and sharing of notes, an Org-mode file (or a part of it)
  4366. can be exported as a structured ASCII or HTML file.
  4367. The following commands are available:
  4368. \\{org-mode-map}"
  4369. ;; Get rid of Outline menus, they are not needed
  4370. ;; Need to do this here because define-derived-mode sets up
  4371. ;; the keymap so late. Still, it is a waste to call this each time
  4372. ;; we switch another buffer into org-mode.
  4373. (if (featurep 'xemacs)
  4374. (when (boundp 'outline-mode-menu-heading)
  4375. ;; Assume this is Greg's port, it uses easymenu
  4376. (easy-menu-remove outline-mode-menu-heading)
  4377. (easy-menu-remove outline-mode-menu-show)
  4378. (easy-menu-remove outline-mode-menu-hide))
  4379. (define-key org-mode-map [menu-bar headings] 'undefined)
  4380. (define-key org-mode-map [menu-bar hide] 'undefined)
  4381. (define-key org-mode-map [menu-bar show] 'undefined))
  4382. (org-load-modules-maybe)
  4383. (easy-menu-add org-org-menu)
  4384. (easy-menu-add org-tbl-menu)
  4385. (org-install-agenda-files-menu)
  4386. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4387. (add-to-invisibility-spec '(org-cwidth))
  4388. (add-to-invisibility-spec '(org-hide-block . t))
  4389. (when (featurep 'xemacs)
  4390. (org-set-local 'line-move-ignore-invisible t))
  4391. (org-set-local 'outline-regexp org-outline-regexp)
  4392. (org-set-local 'outline-level 'org-outline-level)
  4393. (setq bidi-paragraph-direction 'left-to-right)
  4394. (when (and org-ellipsis
  4395. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4396. (fboundp 'make-glyph-code))
  4397. (unless org-display-table
  4398. (setq org-display-table (make-display-table)))
  4399. (set-display-table-slot
  4400. org-display-table 4
  4401. (vconcat (mapcar
  4402. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4403. org-ellipsis)))
  4404. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4405. (setq buffer-display-table org-display-table))
  4406. (org-set-regexps-and-options)
  4407. (when (and org-tag-faces (not org-tags-special-faces-re))
  4408. ;; tag faces set outside customize.... force initialization.
  4409. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4410. ;; Calc embedded
  4411. (org-set-local 'calc-embedded-open-mode "# ")
  4412. (modify-syntax-entry ?@ "w")
  4413. (if org-startup-truncated (setq truncate-lines t))
  4414. (org-set-local 'font-lock-unfontify-region-function
  4415. 'org-unfontify-region)
  4416. ;; Activate before-change-function
  4417. (org-set-local 'org-table-may-need-update t)
  4418. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4419. 'local)
  4420. ;; Check for running clock before killing a buffer
  4421. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4422. ;; Paragraphs and auto-filling
  4423. (org-set-autofill-regexps)
  4424. (setq indent-line-function 'org-indent-line-function)
  4425. (org-update-radio-target-regexp)
  4426. ;; Beginning/end of defun
  4427. (org-set-local 'beginning-of-defun-function 'org-beginning-of-defun)
  4428. (org-set-local 'end-of-defun-function 'org-end-of-defun)
  4429. ;; Next error for sparse trees
  4430. (org-set-local 'next-error-function 'org-occur-next-match)
  4431. ;; Make sure dependence stuff works reliably, even for users who set it
  4432. ;; too late :-(
  4433. (if org-enforce-todo-dependencies
  4434. (add-hook 'org-blocker-hook
  4435. 'org-block-todo-from-children-or-siblings-or-parent)
  4436. (remove-hook 'org-blocker-hook
  4437. 'org-block-todo-from-children-or-siblings-or-parent))
  4438. (if org-enforce-todo-checkbox-dependencies
  4439. (add-hook 'org-blocker-hook
  4440. 'org-block-todo-from-checkboxes)
  4441. (remove-hook 'org-blocker-hook
  4442. 'org-block-todo-from-checkboxes))
  4443. ;; Comment characters
  4444. (org-set-local 'comment-start "#")
  4445. (org-set-local 'comment-padding " ")
  4446. ;; Align options lines
  4447. (org-set-local
  4448. 'align-mode-rules-list
  4449. '((org-in-buffer-settings
  4450. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4451. (modes . '(org-mode)))))
  4452. ;; Imenu
  4453. (org-set-local 'imenu-create-index-function
  4454. 'org-imenu-get-tree)
  4455. ;; Make isearch reveal context
  4456. (if (or (featurep 'xemacs)
  4457. (not (boundp 'outline-isearch-open-invisible-function)))
  4458. ;; Emacs 21 and XEmacs make use of the hook
  4459. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4460. ;; Emacs 22 deals with this through a special variable
  4461. (org-set-local 'outline-isearch-open-invisible-function
  4462. (lambda (&rest ignore) (org-show-context 'isearch))))
  4463. ;; Turn on org-beamer-mode?
  4464. (and org-startup-with-beamer-mode (org-beamer-mode 1))
  4465. ;; Setup the pcomplete hooks
  4466. (set (make-local-variable 'pcomplete-command-completion-function)
  4467. 'org-pcomplete-initial)
  4468. (set (make-local-variable 'pcomplete-command-name-function)
  4469. 'org-command-at-point)
  4470. (set (make-local-variable 'pcomplete-default-completion-function)
  4471. 'ignore)
  4472. (set (make-local-variable 'pcomplete-parse-arguments-function)
  4473. 'org-parse-arguments)
  4474. (set (make-local-variable 'pcomplete-termination-string) "")
  4475. (when (>= emacs-major-version 23)
  4476. (set (make-local-variable 'buffer-face-mode-face) 'org-default))
  4477. ;; If empty file that did not turn on org-mode automatically, make it to.
  4478. (if (and org-insert-mode-line-in-empty-file
  4479. (org-called-interactively-p 'any)
  4480. (= (point-min) (point-max)))
  4481. (insert "# -*- mode: org -*-\n\n"))
  4482. (unless org-inhibit-startup
  4483. (when org-startup-align-all-tables
  4484. (let ((bmp (buffer-modified-p)))
  4485. (org-table-map-tables 'org-table-align 'quietly)
  4486. (set-buffer-modified-p bmp)))
  4487. (when org-startup-with-inline-images
  4488. (org-display-inline-images))
  4489. (when org-startup-indented
  4490. (require 'org-indent)
  4491. (org-indent-mode 1))
  4492. (unless org-inhibit-startup-visibility-stuff
  4493. (org-set-startup-visibility))))
  4494. (when (fboundp 'abbrev-table-put)
  4495. (abbrev-table-put org-mode-abbrev-table
  4496. :parents (list text-mode-abbrev-table)))
  4497. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4498. (defun org-current-time ()
  4499. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  4500. (if (> (car org-time-stamp-rounding-minutes) 1)
  4501. (let ((r (car org-time-stamp-rounding-minutes))
  4502. (time (decode-time)))
  4503. (apply 'encode-time
  4504. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4505. (nthcdr 2 time))))
  4506. (current-time)))
  4507. (defun org-today ()
  4508. "Return today date, considering `org-extend-today-until'."
  4509. (time-to-days
  4510. (time-subtract (current-time)
  4511. (list 0 (* 3600 org-extend-today-until) 0))))
  4512. ;;;; Font-Lock stuff, including the activators
  4513. (defvar org-mouse-map (make-sparse-keymap))
  4514. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4515. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4516. (when org-mouse-1-follows-link
  4517. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4518. (when org-tab-follows-link
  4519. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4520. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4521. (require 'font-lock)
  4522. (defconst org-non-link-chars "]\t\n\r<>")
  4523. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  4524. "shell" "elisp" "doi" "message"))
  4525. (defvar org-link-types-re nil
  4526. "Matches a link that has a url-like prefix like \"http:\"")
  4527. (defvar org-link-re-with-space nil
  4528. "Matches a link with spaces, optional angular brackets around it.")
  4529. (defvar org-link-re-with-space2 nil
  4530. "Matches a link with spaces, optional angular brackets around it.")
  4531. (defvar org-link-re-with-space3 nil
  4532. "Matches a link with spaces, only for internal part in bracket links.")
  4533. (defvar org-angle-link-re nil
  4534. "Matches link with angular brackets, spaces are allowed.")
  4535. (defvar org-plain-link-re nil
  4536. "Matches plain link, without spaces.")
  4537. (defvar org-bracket-link-regexp nil
  4538. "Matches a link in double brackets.")
  4539. (defvar org-bracket-link-analytic-regexp nil
  4540. "Regular expression used to analyze links.
  4541. Here is what the match groups contain after a match:
  4542. 1: http:
  4543. 2: http
  4544. 3: path
  4545. 4: [desc]
  4546. 5: desc")
  4547. (defvar org-bracket-link-analytic-regexp++ nil
  4548. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4549. (defvar org-any-link-re nil
  4550. "Regular expression matching any link.")
  4551. (defcustom org-match-sexp-depth 3
  4552. "Number of stacked braces for sub/superscript matching.
  4553. This has to be set before loading org.el to be effective."
  4554. :group 'org-export-translation ; ??????????????????????????/
  4555. :type 'integer)
  4556. (defun org-create-multibrace-regexp (left right n)
  4557. "Create a regular expression which will match a balanced sexp.
  4558. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4559. as single character strings.
  4560. The regexp returned will match the entire expression including the
  4561. delimiters. It will also define a single group which contains the
  4562. match except for the outermost delimiters. The maximum depth of
  4563. stacked delimiters is N. Escaping delimiters is not possible."
  4564. (let* ((nothing (concat "[^" left right "]*?"))
  4565. (or "\\|")
  4566. (re nothing)
  4567. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4568. (while (> n 1)
  4569. (setq n (1- n)
  4570. re (concat re or next)
  4571. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4572. (concat left "\\(" re "\\)" right)))
  4573. (defvar org-match-substring-regexp
  4574. (concat
  4575. "\\([^\\]\\|^\\)\\([_^]\\)\\("
  4576. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4577. "\\|"
  4578. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4579. "\\|"
  4580. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  4581. "The regular expression matching a sub- or superscript.")
  4582. (defvar org-match-substring-with-braces-regexp
  4583. (concat
  4584. "\\([^\\]\\|^\\)\\([_^]\\)\\("
  4585. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4586. "\\)")
  4587. "The regular expression matching a sub- or superscript, forcing braces.")
  4588. (defun org-make-link-regexps ()
  4589. "Update the link regular expressions.
  4590. This should be called after the variable `org-link-types' has changed."
  4591. (setq org-link-types-re
  4592. (concat
  4593. "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
  4594. org-link-re-with-space
  4595. (concat
  4596. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4597. "\\([^" org-non-link-chars " ]"
  4598. "[^" org-non-link-chars "]*"
  4599. "[^" org-non-link-chars " ]\\)>?")
  4600. org-link-re-with-space2
  4601. (concat
  4602. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4603. "\\([^" org-non-link-chars " ]"
  4604. "[^\t\n\r]*"
  4605. "[^" org-non-link-chars " ]\\)>?")
  4606. org-link-re-with-space3
  4607. (concat
  4608. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4609. "\\([^" org-non-link-chars " ]"
  4610. "[^\t\n\r]*\\)")
  4611. org-angle-link-re
  4612. (concat
  4613. "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4614. "\\([^" org-non-link-chars " ]"
  4615. "[^" org-non-link-chars "]*"
  4616. "\\)>")
  4617. org-plain-link-re
  4618. (concat
  4619. "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4620. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  4621. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4622. org-bracket-link-regexp
  4623. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4624. org-bracket-link-analytic-regexp
  4625. (concat
  4626. "\\[\\["
  4627. "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
  4628. "\\([^]]+\\)"
  4629. "\\]"
  4630. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4631. "\\]")
  4632. org-bracket-link-analytic-regexp++
  4633. (concat
  4634. "\\[\\["
  4635. "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  4636. "\\([^]]+\\)"
  4637. "\\]"
  4638. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4639. "\\]")
  4640. org-any-link-re
  4641. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4642. org-angle-link-re "\\)\\|\\("
  4643. org-plain-link-re "\\)")))
  4644. (org-make-link-regexps)
  4645. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
  4646. "Regular expression for fast time stamp matching.")
  4647. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
  4648. "Regular expression for fast time stamp matching.")
  4649. (defconst org-ts-regexp0
  4650. "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4651. "Regular expression matching time strings for analysis.
  4652. This one does not require the space after the date, so it can be used
  4653. on a string that terminates immediately after the date.")
  4654. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4655. "Regular expression matching time strings for analysis.")
  4656. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4657. "Regular expression matching time stamps, with groups.")
  4658. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4659. "Regular expression matching time stamps (also [..]), with groups.")
  4660. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4661. "Regular expression matching a time stamp range.")
  4662. (defconst org-tr-regexp-both
  4663. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4664. "Regular expression matching a time stamp range.")
  4665. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4666. org-ts-regexp "\\)?")
  4667. "Regular expression matching a time stamp or time stamp range.")
  4668. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  4669. org-ts-regexp-both "\\)?")
  4670. "Regular expression matching a time stamp or time stamp range.
  4671. The time stamps may be either active or inactive.")
  4672. (defvar org-emph-face nil)
  4673. (defun org-do-emphasis-faces (limit)
  4674. "Run through the buffer and add overlays to emphasized strings."
  4675. (let (rtn a)
  4676. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4677. (if (not (= (char-after (match-beginning 3))
  4678. (char-after (match-beginning 4))))
  4679. (progn
  4680. (setq rtn t)
  4681. (setq a (assoc (match-string 3) org-emphasis-alist))
  4682. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4683. 'face
  4684. (nth 1 a))
  4685. (and (nth 4 a)
  4686. (org-remove-flyspell-overlays-in
  4687. (match-beginning 0) (match-end 0)))
  4688. (add-text-properties (match-beginning 2) (match-end 2)
  4689. '(font-lock-multiline t org-emphasis t))
  4690. (when org-hide-emphasis-markers
  4691. (add-text-properties (match-end 4) (match-beginning 5)
  4692. '(invisible org-link))
  4693. (add-text-properties (match-beginning 3) (match-end 3)
  4694. '(invisible org-link)))))
  4695. (backward-char 1))
  4696. rtn))
  4697. (defun org-emphasize (&optional char)
  4698. "Insert or change an emphasis, i.e. a font like bold or italic.
  4699. If there is an active region, change that region to a new emphasis.
  4700. If there is no region, just insert the marker characters and position
  4701. the cursor between them.
  4702. CHAR should be either the marker character, or the first character of the
  4703. HTML tag associated with that emphasis. If CHAR is a space, the means
  4704. to remove the emphasis of the selected region.
  4705. If char is not given (for example in an interactive call) it
  4706. will be prompted for."
  4707. (interactive)
  4708. (let ((eal org-emphasis-alist) e det
  4709. (erc org-emphasis-regexp-components)
  4710. (prompt "")
  4711. (string "") beg end move tag c s)
  4712. (if (org-region-active-p)
  4713. (setq beg (region-beginning) end (region-end)
  4714. string (buffer-substring beg end))
  4715. (setq move t))
  4716. (while (setq e (pop eal))
  4717. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4718. c (aref tag 0))
  4719. (push (cons c (string-to-char (car e))) det)
  4720. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4721. (substring tag 1)))))
  4722. (setq det (nreverse det))
  4723. (unless char
  4724. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4725. (setq char (read-char-exclusive)))
  4726. (setq char (or (cdr (assoc char det)) char))
  4727. (if (equal char ?\ )
  4728. (setq s "" move nil)
  4729. (unless (assoc (char-to-string char) org-emphasis-alist)
  4730. (error "No such emphasis marker: \"%c\"" char))
  4731. (setq s (char-to-string char)))
  4732. (while (and (> (length string) 1)
  4733. (equal (substring string 0 1) (substring string -1))
  4734. (assoc (substring string 0 1) org-emphasis-alist))
  4735. (setq string (substring string 1 -1)))
  4736. (setq string (concat s string s))
  4737. (if beg (delete-region beg end))
  4738. (unless (or (bolp)
  4739. (string-match (concat "[" (nth 0 erc) "\n]")
  4740. (char-to-string (char-before (point)))))
  4741. (insert " "))
  4742. (unless (or (eobp)
  4743. (string-match (concat "[" (nth 1 erc) "\n]")
  4744. (char-to-string (char-after (point)))))
  4745. (insert " ") (backward-char 1))
  4746. (insert string)
  4747. (and move (backward-char 1))))
  4748. (defconst org-nonsticky-props
  4749. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  4750. (defsubst org-rear-nonsticky-at (pos)
  4751. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  4752. (defun org-activate-plain-links (limit)
  4753. "Run through the buffer and add overlays to links."
  4754. (catch 'exit
  4755. (let (f)
  4756. (if (re-search-forward org-plain-link-re limit t)
  4757. (progn
  4758. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4759. (setq f (get-text-property (match-beginning 0) 'face))
  4760. (if (or (eq f 'org-tag)
  4761. (and (listp f) (memq 'org-tag f)))
  4762. nil
  4763. (add-text-properties (match-beginning 0) (match-end 0)
  4764. (list 'mouse-face 'highlight
  4765. 'face 'org-link
  4766. 'keymap org-mouse-map))
  4767. (org-rear-nonsticky-at (match-end 0)))
  4768. t)))))
  4769. (defun org-activate-code (limit)
  4770. (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  4771. (progn
  4772. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4773. (remove-text-properties (match-beginning 0) (match-end 0)
  4774. '(display t invisible t intangible t))
  4775. t)))
  4776. (defcustom org-src-fontify-natively nil
  4777. "When non-nil, fontify code in code blocks."
  4778. :type 'boolean
  4779. :version "24.1"
  4780. :group 'org-appearance
  4781. :group 'org-babel)
  4782. (defun org-fontify-meta-lines-and-blocks (limit)
  4783. (condition-case nil
  4784. (org-fontify-meta-lines-and-blocks-1 limit)
  4785. (error (message "org-mode fontification error"))))
  4786. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  4787. "Fontify #+ lines and blocks, in the correct ways."
  4788. (let ((case-fold-search t))
  4789. (if (re-search-forward
  4790. "^\\([ \t]*#\\+\\(\\([a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  4791. limit t)
  4792. (let ((beg (match-beginning 0))
  4793. (block-start (match-end 0))
  4794. (block-end nil)
  4795. (lang (match-string 7))
  4796. (beg1 (line-beginning-position 2))
  4797. (dc1 (downcase (match-string 2)))
  4798. (dc3 (downcase (match-string 3)))
  4799. end end1 quoting block-type ovl)
  4800. (cond
  4801. ((member dc1 '("html:" "ascii:" "latex:" "docbook:"))
  4802. ;; a single line of backend-specific content
  4803. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4804. (remove-text-properties (match-beginning 0) (match-end 0)
  4805. '(display t invisible t intangible t))
  4806. (add-text-properties (match-beginning 1) (match-end 3)
  4807. '(font-lock-fontified t face org-meta-line))
  4808. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  4809. '(font-lock-fontified t face org-block))
  4810. ; for backend-specific code
  4811. t)
  4812. ((and (match-end 4) (equal dc3 "begin"))
  4813. ;; Truly a block
  4814. (setq block-type (downcase (match-string 5))
  4815. quoting (member block-type org-protecting-blocks))
  4816. (when (re-search-forward
  4817. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  4818. nil t) ;; on purpose, we look further than LIMIT
  4819. (setq end (min (point-max) (match-end 0))
  4820. end1 (min (point-max) (1- (match-beginning 0))))
  4821. (setq block-end (match-beginning 0))
  4822. (when quoting
  4823. (remove-text-properties beg end
  4824. '(display t invisible t intangible t)))
  4825. (add-text-properties
  4826. beg end
  4827. '(font-lock-fontified t font-lock-multiline t))
  4828. (add-text-properties beg beg1 '(face org-meta-line))
  4829. (add-text-properties end1 (min (point-max) (1+ end))
  4830. '(face org-meta-line)) ; for end_src
  4831. (cond
  4832. ((and lang (not (string= lang "")) org-src-fontify-natively)
  4833. (org-src-font-lock-fontify-block lang block-start block-end)
  4834. ;; remove old background overlays
  4835. (mapc (lambda (ov)
  4836. (if (eq (overlay-get ov 'face) 'org-block-background)
  4837. (delete-overlay ov)))
  4838. (overlays-at (/ (+ beg1 block-end) 2)))
  4839. ;; add a background overlay
  4840. (setq ovl (make-overlay beg1 block-end))
  4841. (overlay-put ovl 'face 'org-block-background)
  4842. (overlay-put ovl 'evaporate t)) ;; make it go away when empty
  4843. (quoting
  4844. (add-text-properties beg1 (min (point-max) (1+ end1))
  4845. '(face org-block))) ; end of source block
  4846. ((not org-fontify-quote-and-verse-blocks))
  4847. ((string= block-type "quote")
  4848. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-quote)))
  4849. ((string= block-type "verse")
  4850. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-verse))))
  4851. (add-text-properties beg beg1 '(face org-block-begin-line))
  4852. (add-text-properties (min (point-max) (1+ end)) (min (point-max) (1+ end1))
  4853. '(face org-block-end-line))
  4854. t))
  4855. ((member dc1 '("title:" "author:" "email:" "date:"))
  4856. (add-text-properties
  4857. beg (match-end 3)
  4858. (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
  4859. '(font-lock-fontified t invisible t)
  4860. '(font-lock-fontified t face org-document-info-keyword)))
  4861. (add-text-properties
  4862. (match-beginning 6) (match-end 6)
  4863. (if (string-equal dc1 "title:")
  4864. '(font-lock-fontified t face org-document-title)
  4865. '(font-lock-fontified t face org-document-info))))
  4866. ((not (member (char-after beg) '(?\ ?\t)))
  4867. ;; just any other in-buffer setting, but not indented
  4868. (add-text-properties
  4869. beg (match-end 0)
  4870. '(font-lock-fontified t face org-meta-line))
  4871. t)
  4872. ((or (member dc1 '("begin:" "end:" "caption:" "label:"
  4873. "orgtbl:" "tblfm:" "tblname:" "results:"
  4874. "call:" "header:" "headers:" "name:"))
  4875. (and (match-end 4) (equal dc3 "attr")))
  4876. (add-text-properties
  4877. beg (match-end 0)
  4878. '(font-lock-fontified t face org-meta-line))
  4879. t)
  4880. ((member dc3 '(" " ""))
  4881. (add-text-properties
  4882. beg (match-end 0)
  4883. '(font-lock-fontified t face font-lock-comment-face)))
  4884. (t nil))))))
  4885. (defun org-strip-protective-commas (beg end)
  4886. "Strip protective commas between BEG and END in the current buffer."
  4887. (interactive "r")
  4888. (save-excursion
  4889. (save-match-data
  4890. (goto-char beg)
  4891. (let ((front-line (save-excursion
  4892. (re-search-forward
  4893. "[^[:space:]]" end t)
  4894. (goto-char (match-beginning 0))
  4895. (current-column))))
  4896. (while (re-search-forward "^[ \t]*\\(,\\)\\([*]\\|#\\+\\)" end t)
  4897. (goto-char (match-beginning 1))
  4898. (when (= (current-column) front-line)
  4899. (replace-match "" nil nil nil 1)))))))
  4900. (defun org-activate-angle-links (limit)
  4901. "Run through the buffer and add overlays to links."
  4902. (if (re-search-forward org-angle-link-re limit t)
  4903. (progn
  4904. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4905. (add-text-properties (match-beginning 0) (match-end 0)
  4906. (list 'mouse-face 'highlight
  4907. 'keymap org-mouse-map))
  4908. (org-rear-nonsticky-at (match-end 0))
  4909. t)))
  4910. (defun org-activate-footnote-links (limit)
  4911. "Run through the buffer and add overlays to footnotes."
  4912. (let ((fn (org-footnote-next-reference-or-definition limit)))
  4913. (when fn
  4914. (let ((beg (nth 1 fn)) (end (nth 2 fn)))
  4915. (org-remove-flyspell-overlays-in beg end)
  4916. (add-text-properties beg end
  4917. (list 'mouse-face 'highlight
  4918. 'keymap org-mouse-map
  4919. 'help-echo
  4920. (if (= (point-at-bol) beg)
  4921. "Footnote definition"
  4922. "Footnote reference")
  4923. 'font-lock-fontified t
  4924. 'font-lock-multiline t
  4925. 'face 'org-footnote))))))
  4926. (defun org-activate-bracket-links (limit)
  4927. "Run through the buffer and add overlays to bracketed links."
  4928. (if (re-search-forward org-bracket-link-regexp limit t)
  4929. (let* ((help (concat "LINK: "
  4930. (org-match-string-no-properties 1)))
  4931. ;; FIXME: above we should remove the escapes.
  4932. ;; but that requires another match, protecting match data,
  4933. ;; a lot of overhead for font-lock.
  4934. (ip (org-maybe-intangible
  4935. (list 'invisible 'org-link
  4936. 'keymap org-mouse-map 'mouse-face 'highlight
  4937. 'font-lock-multiline t 'help-echo help)))
  4938. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  4939. 'font-lock-multiline t 'help-echo help)))
  4940. ;; We need to remove the invisible property here. Table narrowing
  4941. ;; may have made some of this invisible.
  4942. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4943. (remove-text-properties (match-beginning 0) (match-end 0)
  4944. '(invisible nil))
  4945. (if (match-end 3)
  4946. (progn
  4947. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  4948. (org-rear-nonsticky-at (match-beginning 3))
  4949. (add-text-properties (match-beginning 3) (match-end 3) vp)
  4950. (org-rear-nonsticky-at (match-end 3))
  4951. (add-text-properties (match-end 3) (match-end 0) ip)
  4952. (org-rear-nonsticky-at (match-end 0)))
  4953. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  4954. (org-rear-nonsticky-at (match-beginning 1))
  4955. (add-text-properties (match-beginning 1) (match-end 1) vp)
  4956. (org-rear-nonsticky-at (match-end 1))
  4957. (add-text-properties (match-end 1) (match-end 0) ip)
  4958. (org-rear-nonsticky-at (match-end 0)))
  4959. t)))
  4960. (defun org-activate-dates (limit)
  4961. "Run through the buffer and add overlays to dates."
  4962. (if (re-search-forward org-tsr-regexp-both limit t)
  4963. (progn
  4964. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4965. (add-text-properties (match-beginning 0) (match-end 0)
  4966. (list 'mouse-face 'highlight
  4967. 'keymap org-mouse-map))
  4968. (org-rear-nonsticky-at (match-end 0))
  4969. (when org-display-custom-times
  4970. (if (match-end 3)
  4971. (org-display-custom-time (match-beginning 3) (match-end 3)))
  4972. (org-display-custom-time (match-beginning 1) (match-end 1)))
  4973. t)))
  4974. (defvar org-target-link-regexp nil
  4975. "Regular expression matching radio targets in plain text.")
  4976. (make-variable-buffer-local 'org-target-link-regexp)
  4977. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  4978. "Regular expression matching a link target.")
  4979. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  4980. "Regular expression matching a radio target.")
  4981. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  4982. "Regular expression matching any target.")
  4983. (defun org-activate-target-links (limit)
  4984. "Run through the buffer and add overlays to target matches."
  4985. (when org-target-link-regexp
  4986. (let ((case-fold-search t))
  4987. (if (re-search-forward org-target-link-regexp limit t)
  4988. (progn
  4989. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4990. (add-text-properties (match-beginning 0) (match-end 0)
  4991. (list 'mouse-face 'highlight
  4992. 'keymap org-mouse-map
  4993. 'help-echo "Radio target link"
  4994. 'org-linked-text t))
  4995. (org-rear-nonsticky-at (match-end 0))
  4996. t)))))
  4997. (defun org-update-radio-target-regexp ()
  4998. "Find all radio targets in this file and update the regular expression."
  4999. (interactive)
  5000. (when (memq 'radio org-activate-links)
  5001. (setq org-target-link-regexp
  5002. (org-make-target-link-regexp (org-all-targets 'radio)))
  5003. (org-restart-font-lock)))
  5004. (defun org-hide-wide-columns (limit)
  5005. (let (s e)
  5006. (setq s (text-property-any (point) (or limit (point-max))
  5007. 'org-cwidth t))
  5008. (when s
  5009. (setq e (next-single-property-change s 'org-cwidth))
  5010. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  5011. (goto-char e)
  5012. t)))
  5013. (defvar org-latex-and-specials-regexp nil
  5014. "Regular expression for highlighting export special stuff.")
  5015. (defvar org-match-substring-regexp)
  5016. (defvar org-match-substring-with-braces-regexp)
  5017. ;; This should be with the exporter code, but we also use if for font-locking
  5018. (defconst org-export-html-special-string-regexps
  5019. '(("\\\\-" . "&shy;")
  5020. ("---\\([^-]\\)" . "&mdash;\\1")
  5021. ("--\\([^-]\\)" . "&ndash;\\1")
  5022. ("\\.\\.\\." . "&hellip;"))
  5023. "Regular expressions for special string conversion.")
  5024. (defun org-compute-latex-and-specials-regexp ()
  5025. "Compute regular expression for stuff treated specially by exporters."
  5026. (if (not org-highlight-latex-fragments-and-specials)
  5027. (org-set-local 'org-latex-and-specials-regexp nil)
  5028. (require 'org-exp)
  5029. (let*
  5030. ((matchers (plist-get org-format-latex-options :matchers))
  5031. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  5032. org-latex-regexps)))
  5033. (org-export-allow-BIND nil)
  5034. (options (org-combine-plists (org-default-export-plist)
  5035. (org-infile-export-plist)))
  5036. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  5037. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  5038. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  5039. (org-export-html-expand (plist-get options :expand-quoted-html))
  5040. (org-export-with-special-strings (plist-get options :special-strings))
  5041. (re-sub
  5042. (cond
  5043. ((equal org-export-with-sub-superscripts '{})
  5044. (list org-match-substring-with-braces-regexp))
  5045. (org-export-with-sub-superscripts
  5046. (list org-match-substring-regexp))
  5047. (t nil)))
  5048. (re-latex
  5049. (if org-export-with-LaTeX-fragments
  5050. (mapcar (lambda (x) (nth 1 x)) latexs)))
  5051. (re-macros
  5052. (if org-export-with-TeX-macros
  5053. (list (concat "\\\\"
  5054. (regexp-opt
  5055. (append
  5056. (delq nil
  5057. (mapcar 'car-safe
  5058. (append org-entities-user
  5059. org-entities)))
  5060. (if (boundp 'org-latex-entities)
  5061. (mapcar (lambda (x)
  5062. (or (car-safe x) x))
  5063. org-latex-entities)
  5064. nil))
  5065. 'words))) ; FIXME
  5066. ))
  5067. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  5068. (re-special (if org-export-with-special-strings
  5069. (mapcar (lambda (x) (car x))
  5070. org-export-html-special-string-regexps)))
  5071. (re-rest
  5072. (delq nil
  5073. (list
  5074. (if org-export-html-expand "@<[^>\n]+>")
  5075. ))))
  5076. (org-set-local
  5077. 'org-latex-and-specials-regexp
  5078. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  5079. re-rest) "\\|")))))
  5080. (defun org-do-latex-and-special-faces (limit)
  5081. "Run through the buffer and add overlays to links."
  5082. (when org-latex-and-specials-regexp
  5083. (let (rtn d)
  5084. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  5085. limit t))
  5086. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  5087. 'face))
  5088. '(org-code org-verbatim underline)))
  5089. (progn
  5090. (setq rtn t
  5091. d (cond ((member (char-after (1+ (match-beginning 0)))
  5092. '(?_ ?^)) 1)
  5093. (t 0)))
  5094. (font-lock-prepend-text-property
  5095. (+ d (match-beginning 0)) (match-end 0)
  5096. 'face 'org-latex-and-export-specials)
  5097. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  5098. '(font-lock-multiline t)))))
  5099. rtn)))
  5100. (defun org-restart-font-lock ()
  5101. "Restart `font-lock-mode', to force refontification."
  5102. (when (and (boundp 'font-lock-mode) font-lock-mode)
  5103. (font-lock-mode -1)
  5104. (font-lock-mode 1)))
  5105. (defun org-all-targets (&optional radio)
  5106. "Return a list of all targets in this file.
  5107. With optional argument RADIO, only find radio targets."
  5108. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  5109. rtn)
  5110. (save-excursion
  5111. (goto-char (point-min))
  5112. (while (re-search-forward re nil t)
  5113. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  5114. rtn)))
  5115. (defun org-make-target-link-regexp (targets)
  5116. "Make regular expression matching all strings in TARGETS.
  5117. The regular expression finds the targets also if there is a line break
  5118. between words."
  5119. (and targets
  5120. (concat
  5121. "\\<\\("
  5122. (mapconcat
  5123. (lambda (x)
  5124. (setq x (regexp-quote x))
  5125. (while (string-match " +" x)
  5126. (setq x (replace-match "\\s-+" t t x)))
  5127. x)
  5128. targets
  5129. "\\|")
  5130. "\\)\\>")))
  5131. (defun org-activate-tags (limit)
  5132. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  5133. (progn
  5134. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5135. (add-text-properties (match-beginning 1) (match-end 1)
  5136. (list 'mouse-face 'highlight
  5137. 'keymap org-mouse-map))
  5138. (org-rear-nonsticky-at (match-end 1))
  5139. t)))
  5140. (defun org-outline-level ()
  5141. "Compute the outline level of the heading at point.
  5142. This function assumes that the cursor is at the beginning of a line matched
  5143. by `outline-regexp'. Otherwise it returns garbage.
  5144. If this is called at a normal headline, the level is the number of stars.
  5145. Use `org-reduced-level' to remove the effect of `org-odd-levels'."
  5146. (save-excursion
  5147. (looking-at org-outline-regexp)
  5148. (1- (- (match-end 0) (match-beginning 0)))))
  5149. (defvar org-font-lock-keywords nil)
  5150. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\+?\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  5151. "Regular expression matching a property line.")
  5152. (defvar org-font-lock-hook nil
  5153. "Functions to be called for special font lock stuff.")
  5154. (defvar org-font-lock-set-keywords-hook nil
  5155. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5156. This is called after `org-font-lock-extra-keywords' is defined, but before
  5157. it is installed to be used by font lock. This can be useful if something
  5158. needs to be inserted at a specific position in the font-lock sequence.")
  5159. (defun org-font-lock-hook (limit)
  5160. (run-hook-with-args 'org-font-lock-hook limit))
  5161. (defun org-set-font-lock-defaults ()
  5162. (let* ((em org-fontify-emphasized-text)
  5163. (lk org-activate-links)
  5164. (org-font-lock-extra-keywords
  5165. (list
  5166. ;; Call the hook
  5167. '(org-font-lock-hook)
  5168. ;; Headlines
  5169. `(,(if org-fontify-whole-heading-line
  5170. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5171. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5172. (1 (org-get-level-face 1))
  5173. (2 (org-get-level-face 2))
  5174. (3 (org-get-level-face 3)))
  5175. ;; Table lines
  5176. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5177. (1 'org-table t))
  5178. ;; Table internals
  5179. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5180. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5181. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5182. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5183. ;; Drawers
  5184. (list org-drawer-regexp '(0 'org-special-keyword t))
  5185. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  5186. ;; Properties
  5187. (list org-property-re
  5188. '(1 'org-special-keyword t)
  5189. '(3 'org-property-value t))
  5190. ;; Links
  5191. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5192. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5193. (if (memq 'plain lk) '(org-activate-plain-links))
  5194. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5195. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  5196. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5197. (if (memq 'footnote lk) '(org-activate-footnote-links))
  5198. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5199. '(org-hide-wide-columns (0 nil append))
  5200. ;; TODO keyword
  5201. (list (format org-heading-keyword-regexp-format
  5202. org-todo-regexp)
  5203. '(2 (org-get-todo-face 2) t))
  5204. ;; DONE
  5205. (if org-fontify-done-headline
  5206. (list (format org-heading-keyword-regexp-format
  5207. (concat
  5208. "\\(?:"
  5209. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5210. "\\)"))
  5211. '(2 'org-headline-done t))
  5212. nil)
  5213. ;; Priorities
  5214. '(org-font-lock-add-priority-faces)
  5215. ;; Tags
  5216. '(org-font-lock-add-tag-faces)
  5217. ;; Special keywords
  5218. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5219. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5220. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5221. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5222. ;; Emphasis
  5223. (if em
  5224. (if (featurep 'xemacs)
  5225. '(org-do-emphasis-faces (0 nil append))
  5226. '(org-do-emphasis-faces)))
  5227. ;; Checkboxes
  5228. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5229. 1 'org-checkbox prepend)
  5230. (if (cdr (assq 'checkbox org-list-automatic-rules))
  5231. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5232. (0 (org-get-checkbox-statistics-face) t)))
  5233. ;; Description list items
  5234. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5235. 1 'bold prepend)
  5236. ;; ARCHIVEd headings
  5237. (list (concat
  5238. org-outline-regexp-bol
  5239. "\\(.*:" org-archive-tag ":.*\\)")
  5240. '(1 'org-archived prepend))
  5241. ;; Specials
  5242. '(org-do-latex-and-special-faces)
  5243. '(org-fontify-entities)
  5244. '(org-raise-scripts)
  5245. ;; Code
  5246. '(org-activate-code (1 'org-code t))
  5247. ;; COMMENT
  5248. (list (format org-heading-keyword-regexp-format
  5249. (concat "\\("
  5250. org-comment-string "\\|" org-quote-string
  5251. "\\)"))
  5252. '(2 'org-special-keyword t))
  5253. '("^#.*" (0 'font-lock-comment-face t))
  5254. ;; Blocks and meta lines
  5255. '(org-fontify-meta-lines-and-blocks)
  5256. )))
  5257. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5258. (run-hooks 'org-font-lock-set-keywords-hook)
  5259. ;; Now set the full font-lock-keywords
  5260. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5261. (org-set-local 'font-lock-defaults
  5262. '(org-font-lock-keywords t nil nil backward-paragraph))
  5263. (kill-local-variable 'font-lock-keywords) nil))
  5264. (defun org-toggle-pretty-entities ()
  5265. "Toggle the composition display of entities as UTF8 characters."
  5266. (interactive)
  5267. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5268. (org-restart-font-lock)
  5269. (if org-pretty-entities
  5270. (message "Entities are displayed as UTF8 characters")
  5271. (save-restriction
  5272. (widen)
  5273. (org-decompose-region (point-min) (point-max))
  5274. (message "Entities are displayed plain"))))
  5275. (defun org-fontify-entities (limit)
  5276. "Find an entity to fontify."
  5277. (let (ee)
  5278. (when org-pretty-entities
  5279. (catch 'match
  5280. (while (re-search-forward
  5281. "\\\\\\(frac[13][24]\\|[a-zA-Z]+\\)\\($\\|[^[:alpha:]\n]\\)"
  5282. limit t)
  5283. (if (and (not (org-in-indented-comment-line))
  5284. (setq ee (org-entity-get (match-string 1)))
  5285. (= (length (nth 6 ee)) 1))
  5286. (progn
  5287. (add-text-properties
  5288. (match-beginning 0) (match-end 1)
  5289. (list 'font-lock-fontified t))
  5290. (compose-region (match-beginning 0) (match-end 1)
  5291. (nth 6 ee) nil)
  5292. (backward-char 1)
  5293. (throw 'match t))))
  5294. nil))))
  5295. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5296. "Fontify string S like in Org-mode."
  5297. (with-temp-buffer
  5298. (insert s)
  5299. (let ((org-odd-levels-only odd-levels))
  5300. (org-mode)
  5301. (font-lock-fontify-buffer)
  5302. (buffer-string))))
  5303. (defvar org-m nil)
  5304. (defvar org-l nil)
  5305. (defvar org-f nil)
  5306. (defun org-get-level-face (n)
  5307. "Get the right face for match N in font-lock matching of headlines."
  5308. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5309. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5310. (if org-cycle-level-faces
  5311. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5312. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5313. (cond
  5314. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5315. ((eq n 2) org-f)
  5316. (t (if org-level-color-stars-only nil org-f))))
  5317. (defun org-get-todo-face (kwd)
  5318. "Get the right face for a TODO keyword KWD.
  5319. If KWD is a number, get the corresponding match group."
  5320. (if (numberp kwd) (setq kwd (match-string kwd)))
  5321. (or (org-face-from-face-or-color
  5322. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5323. (and (member kwd org-done-keywords) 'org-done)
  5324. 'org-todo))
  5325. (defun org-face-from-face-or-color (context inherit face-or-color)
  5326. "Create a face list that inherits INHERIT, but sets the foreground color.
  5327. When FACE-OR-COLOR is not a string, just return it."
  5328. (if (stringp face-or-color)
  5329. (list :inherit inherit
  5330. (cdr (assoc context org-faces-easy-properties))
  5331. face-or-color)
  5332. face-or-color))
  5333. (defun org-font-lock-add-tag-faces (limit)
  5334. "Add the special tag faces."
  5335. (when (and org-tag-faces org-tags-special-faces-re)
  5336. (while (re-search-forward org-tags-special-faces-re limit t)
  5337. (add-text-properties (match-beginning 1) (match-end 1)
  5338. (list 'face (org-get-tag-face 1)
  5339. 'font-lock-fontified t))
  5340. (backward-char 1))))
  5341. (defun org-font-lock-add-priority-faces (limit)
  5342. "Add the special priority faces."
  5343. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  5344. (add-text-properties
  5345. (match-beginning 0) (match-end 0)
  5346. (list 'face (or (org-face-from-face-or-color
  5347. 'priority 'org-special-keyword
  5348. (cdr (assoc (char-after (match-beginning 1))
  5349. org-priority-faces)))
  5350. 'org-special-keyword)
  5351. 'font-lock-fontified t))))
  5352. (defun org-get-tag-face (kwd)
  5353. "Get the right face for a TODO keyword KWD.
  5354. If KWD is a number, get the corresponding match group."
  5355. (if (numberp kwd) (setq kwd (match-string kwd)))
  5356. (or (org-face-from-face-or-color
  5357. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  5358. 'org-tag))
  5359. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5360. "Remove fontification and activation overlays from links."
  5361. (font-lock-default-unfontify-region beg end)
  5362. (let* ((buffer-undo-list t)
  5363. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5364. (inhibit-modification-hooks t)
  5365. deactivate-mark buffer-file-name buffer-file-truename)
  5366. (org-decompose-region beg end)
  5367. (remove-text-properties beg end
  5368. '(mouse-face t keymap t org-linked-text t
  5369. invisible t intangible t
  5370. org-no-flyspell t org-emphasis t))
  5371. (org-remove-font-lock-display-properties beg end)))
  5372. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5373. ((raise 0.3) (height 0.7))
  5374. ((raise -0.5))
  5375. ((raise 0.5)))
  5376. "Display properties for showing superscripts and subscripts.")
  5377. (defun org-remove-font-lock-display-properties (beg end)
  5378. "Remove specific display properties that have been added by font lock.
  5379. The will remove the raise properties that are used to show superscripts
  5380. and subscripts."
  5381. (let (next prop)
  5382. (while (< beg end)
  5383. (setq next (next-single-property-change beg 'display nil end)
  5384. prop (get-text-property beg 'display))
  5385. (if (member prop org-script-display)
  5386. (put-text-property beg next 'display nil))
  5387. (setq beg next))))
  5388. (defun org-raise-scripts (limit)
  5389. "Add raise properties to sub/superscripts."
  5390. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5391. (if (re-search-forward
  5392. (if (eq org-use-sub-superscripts t)
  5393. org-match-substring-regexp
  5394. org-match-substring-with-braces-regexp)
  5395. limit t)
  5396. (let* ((pos (point)) table-p comment-p
  5397. (mpos (match-beginning 3))
  5398. (emph-p (get-text-property mpos 'org-emphasis))
  5399. (link-p (get-text-property mpos 'mouse-face))
  5400. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5401. (goto-char (point-at-bol))
  5402. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5403. comment-p (org-looking-at-p "[ \t]*#"))
  5404. (goto-char pos)
  5405. ;; FIXME: Should we go back one character here, for a_b^c
  5406. ;; (goto-char (1- pos)) ;????????????????????
  5407. (if (or comment-p emph-p link-p keyw-p)
  5408. t
  5409. (put-text-property (match-beginning 3) (match-end 0)
  5410. 'display
  5411. (if (equal (char-after (match-beginning 2)) ?^)
  5412. (nth (if table-p 3 1) org-script-display)
  5413. (nth (if table-p 2 0) org-script-display)))
  5414. (add-text-properties (match-beginning 2) (match-end 2)
  5415. (list 'invisible t
  5416. 'org-dwidth t 'org-dwidth-n 1))
  5417. (if (and (eq (char-after (match-beginning 3)) ?{)
  5418. (eq (char-before (match-end 3)) ?}))
  5419. (progn
  5420. (add-text-properties
  5421. (match-beginning 3) (1+ (match-beginning 3))
  5422. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5423. (add-text-properties
  5424. (1- (match-end 3)) (match-end 3)
  5425. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5426. t)))))
  5427. ;;;; Visibility cycling, including org-goto and indirect buffer
  5428. ;;; Cycling
  5429. (defvar org-cycle-global-status nil)
  5430. (make-variable-buffer-local 'org-cycle-global-status)
  5431. (defvar org-cycle-subtree-status nil)
  5432. (make-variable-buffer-local 'org-cycle-subtree-status)
  5433. ;;;###autoload
  5434. (defvar org-inlinetask-min-level)
  5435. (defun org-cycle (&optional arg)
  5436. "TAB-action and visibility cycling for Org-mode.
  5437. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5438. is outline visibility cycling, but it also invokes other actions
  5439. in special contexts.
  5440. - When this function is called with a prefix argument, rotate the entire
  5441. buffer through 3 states (global cycling)
  5442. 1. OVERVIEW: Show only top-level headlines.
  5443. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5444. 3. SHOW ALL: Show everything.
  5445. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5446. determined by the variable `org-startup-folded', and by any VISIBILITY
  5447. properties in the buffer.
  5448. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5449. including any drawers.
  5450. - When inside a table, re-align the table and move to the next field.
  5451. - When point is at the beginning of a headline, rotate the subtree started
  5452. by this line through 3 different states (local cycling)
  5453. 1. FOLDED: Only the main headline is shown.
  5454. 2. CHILDREN: The main headline and the direct children are shown.
  5455. From this state, you can move to one of the children
  5456. and zoom in further.
  5457. 3. SUBTREE: Show the entire subtree, including body text.
  5458. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5459. - When point is at the beginning of an empty headline and the variable
  5460. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5461. of the headline by demoting and promoting it to likely levels. This
  5462. speeds up creation document structure by pressing TAB once or several
  5463. times right after creating a new headline.
  5464. - When there is a numeric prefix, go up to a heading with level ARG, do
  5465. a `show-subtree' and return to the previous cursor position. If ARG
  5466. is negative, go up that many levels.
  5467. - When point is not at the beginning of a headline, execute the global
  5468. binding for TAB, which is re-indenting the line. See the option
  5469. `org-cycle-emulate-tab' for details.
  5470. - Special case: if point is at the beginning of the buffer and there is
  5471. no headline in line 1, this function will act as if called with prefix arg
  5472. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5473. But only if also the variable `org-cycle-global-at-bob' is t."
  5474. (interactive "P")
  5475. (org-load-modules-maybe)
  5476. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5477. (and org-cycle-level-after-item/entry-creation
  5478. (or (org-cycle-level)
  5479. (org-cycle-item-indentation))))
  5480. (let* ((limit-level
  5481. (or org-cycle-max-level
  5482. (and (boundp 'org-inlinetask-min-level)
  5483. org-inlinetask-min-level
  5484. (1- org-inlinetask-min-level))))
  5485. (nstars (and limit-level
  5486. (if org-odd-levels-only
  5487. (and limit-level (1- (* limit-level 2)))
  5488. limit-level)))
  5489. (org-outline-regexp
  5490. (if (not (eq major-mode 'org-mode))
  5491. outline-regexp
  5492. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5493. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5494. (not (looking-at org-outline-regexp))))
  5495. (org-cycle-hook
  5496. (if bob-special
  5497. (delq 'org-optimize-window-after-visibility-change
  5498. (copy-sequence org-cycle-hook))
  5499. org-cycle-hook))
  5500. (pos (point)))
  5501. (if (or bob-special (equal arg '(4)))
  5502. ;; special case: use global cycling
  5503. (setq arg t))
  5504. (cond
  5505. ((equal arg '(16))
  5506. (setq last-command 'dummy)
  5507. (org-set-startup-visibility)
  5508. (message "Startup visibility, plus VISIBILITY properties"))
  5509. ((equal arg '(64))
  5510. (show-all)
  5511. (message "Entire buffer visible, including drawers"))
  5512. ;; Table: enter it or move to the next field.
  5513. ((org-at-table-p 'any)
  5514. (if (org-at-table.el-p)
  5515. (message "Use C-c ' to edit table.el tables")
  5516. (if arg (org-table-edit-field t)
  5517. (org-table-justify-field-maybe)
  5518. (call-interactively 'org-table-next-field))))
  5519. ((run-hook-with-args-until-success
  5520. 'org-tab-after-check-for-table-hook))
  5521. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5522. ((eq arg t) (org-cycle-internal-global))
  5523. ;; Drawers: delegate to `org-flag-drawer'.
  5524. ((and org-drawers org-drawer-regexp
  5525. (save-excursion
  5526. (beginning-of-line 1)
  5527. (looking-at org-drawer-regexp)))
  5528. (org-flag-drawer ; toggle block visibility
  5529. (not (get-char-property (match-end 0) 'invisible))))
  5530. ;; Show-subtree, ARG levels up from here.
  5531. ((integerp arg)
  5532. (save-excursion
  5533. (org-back-to-heading)
  5534. (outline-up-heading (if (< arg 0) (- arg)
  5535. (- (funcall outline-level) arg)))
  5536. (org-show-subtree)))
  5537. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5538. ((and (featurep 'org-inlinetask)
  5539. (org-inlinetask-at-task-p)
  5540. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5541. (org-inlinetask-toggle-visibility))
  5542. ((org-try-cdlatex-tab))
  5543. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5544. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5545. (save-excursion (beginning-of-line 1)
  5546. (looking-at org-outline-regexp)))
  5547. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5548. (org-cycle-internal-local))
  5549. ;; From there: TAB emulation and template completion.
  5550. (buffer-read-only (org-back-to-heading))
  5551. ((run-hook-with-args-until-success
  5552. 'org-tab-after-check-for-cycling-hook))
  5553. ((org-try-structure-completion))
  5554. ((run-hook-with-args-until-success
  5555. 'org-tab-before-tab-emulation-hook))
  5556. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5557. (or (not (bolp))
  5558. (not (looking-at org-outline-regexp))))
  5559. (call-interactively (global-key-binding "\t")))
  5560. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5561. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5562. (or (and (eq org-cycle-emulate-tab 'white)
  5563. (= (match-end 0) (point-at-eol)))
  5564. (and (eq org-cycle-emulate-tab 'whitestart)
  5565. (>= (match-end 0) pos))))
  5566. t
  5567. (eq org-cycle-emulate-tab t))
  5568. (call-interactively (global-key-binding "\t")))
  5569. (t (save-excursion
  5570. (org-back-to-heading)
  5571. (org-cycle)))))))
  5572. (defun org-cycle-internal-global ()
  5573. "Do the global cycling action."
  5574. ;; Hack to avoid display of messages for .org attachments in Gnus
  5575. (let ((ga (string-match "\\*fontification" (buffer-name))))
  5576. (cond
  5577. ((and (eq last-command this-command)
  5578. (eq org-cycle-global-status 'overview))
  5579. ;; We just created the overview - now do table of contents
  5580. ;; This can be slow in very large buffers, so indicate action
  5581. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  5582. (unless ga (message "CONTENTS..."))
  5583. (org-content)
  5584. (unless ga (message "CONTENTS...done"))
  5585. (setq org-cycle-global-status 'contents)
  5586. (run-hook-with-args 'org-cycle-hook 'contents))
  5587. ((and (eq last-command this-command)
  5588. (eq org-cycle-global-status 'contents))
  5589. ;; We just showed the table of contents - now show everything
  5590. (run-hook-with-args 'org-pre-cycle-hook 'all)
  5591. (show-all)
  5592. (unless ga (message "SHOW ALL"))
  5593. (setq org-cycle-global-status 'all)
  5594. (run-hook-with-args 'org-cycle-hook 'all))
  5595. (t
  5596. ;; Default action: go to overview
  5597. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  5598. (org-overview)
  5599. (unless ga (message "OVERVIEW"))
  5600. (setq org-cycle-global-status 'overview)
  5601. (run-hook-with-args 'org-cycle-hook 'overview)))))
  5602. (defun org-cycle-internal-local ()
  5603. "Do the local cycling action."
  5604. (let ((goal-column 0) eoh eol eos has-children children-skipped struct)
  5605. ;; First, determine end of headline (EOH), end of subtree or item
  5606. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  5607. (save-excursion
  5608. (if (org-at-item-p)
  5609. (progn
  5610. (beginning-of-line)
  5611. (setq struct (org-list-struct))
  5612. (setq eoh (point-at-eol))
  5613. (setq eos (org-list-get-item-end-before-blank (point) struct))
  5614. (setq has-children (org-list-has-child-p (point) struct)))
  5615. (org-back-to-heading)
  5616. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  5617. (setq eos (save-excursion
  5618. (org-end-of-subtree t)
  5619. (unless (eobp)
  5620. (skip-chars-forward " \t\n"))
  5621. (if (eobp) (point) (1- (point)))))
  5622. (setq has-children
  5623. (or (save-excursion
  5624. (let ((level (funcall outline-level)))
  5625. (outline-next-heading)
  5626. (and (org-at-heading-p t)
  5627. (> (funcall outline-level) level))))
  5628. (save-excursion
  5629. (org-list-search-forward (org-item-beginning-re) eos t)))))
  5630. ;; Determine end invisible part of buffer (EOL)
  5631. (beginning-of-line 2)
  5632. ;; XEmacs doesn't have `next-single-char-property-change'
  5633. (if (featurep 'xemacs)
  5634. (while (and (not (eobp)) ;; this is like `next-line'
  5635. (get-char-property (1- (point)) 'invisible))
  5636. (beginning-of-line 2))
  5637. (while (and (not (eobp)) ;; this is like `next-line'
  5638. (get-char-property (1- (point)) 'invisible))
  5639. (goto-char (next-single-char-property-change (point) 'invisible))
  5640. (and (eolp) (beginning-of-line 2))))
  5641. (setq eol (point)))
  5642. ;; Find out what to do next and set `this-command'
  5643. (cond
  5644. ((= eos eoh)
  5645. ;; Nothing is hidden behind this heading
  5646. (run-hook-with-args 'org-pre-cycle-hook 'empty)
  5647. (message "EMPTY ENTRY")
  5648. (setq org-cycle-subtree-status nil)
  5649. (save-excursion
  5650. (goto-char eos)
  5651. (outline-next-heading)
  5652. (if (outline-invisible-p) (org-flag-heading nil))))
  5653. ((and (or (>= eol eos)
  5654. (not (string-match "\\S-" (buffer-substring eol eos))))
  5655. (or has-children
  5656. (not (setq children-skipped
  5657. org-cycle-skip-children-state-if-no-children))))
  5658. ;; Entire subtree is hidden in one line: children view
  5659. (run-hook-with-args 'org-pre-cycle-hook 'children)
  5660. (if (org-at-item-p)
  5661. (org-list-set-item-visibility (point-at-bol) struct 'children)
  5662. (org-show-entry)
  5663. (org-with-limited-levels (show-children))
  5664. ;; FIXME: This slows down the func way too much.
  5665. ;; How keep drawers hidden in subtree anyway?
  5666. ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
  5667. ;; (org-cycle-hide-drawers 'subtree))
  5668. ;; Fold every list in subtree to top-level items.
  5669. (when (eq org-cycle-include-plain-lists 'integrate)
  5670. (save-excursion
  5671. (org-back-to-heading)
  5672. (while (org-list-search-forward (org-item-beginning-re) eos t)
  5673. (beginning-of-line 1)
  5674. (let* ((struct (org-list-struct))
  5675. (prevs (org-list-prevs-alist struct))
  5676. (end (org-list-get-bottom-point struct)))
  5677. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  5678. (org-list-get-all-items (point) struct prevs))
  5679. (goto-char end))))))
  5680. (message "CHILDREN")
  5681. (save-excursion
  5682. (goto-char eos)
  5683. (outline-next-heading)
  5684. (if (outline-invisible-p) (org-flag-heading nil)))
  5685. (setq org-cycle-subtree-status 'children)
  5686. (run-hook-with-args 'org-cycle-hook 'children))
  5687. ((or children-skipped
  5688. (and (eq last-command this-command)
  5689. (eq org-cycle-subtree-status 'children)))
  5690. ;; We just showed the children, or no children are there,
  5691. ;; now show everything.
  5692. (run-hook-with-args 'org-pre-cycle-hook 'subtree)
  5693. (outline-flag-region eoh eos nil)
  5694. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  5695. (setq org-cycle-subtree-status 'subtree)
  5696. (run-hook-with-args 'org-cycle-hook 'subtree))
  5697. (t
  5698. ;; Default action: hide the subtree.
  5699. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  5700. (outline-flag-region eoh eos t)
  5701. (message "FOLDED")
  5702. (setq org-cycle-subtree-status 'folded)
  5703. (run-hook-with-args 'org-cycle-hook 'folded)))))
  5704. ;;;###autoload
  5705. (defun org-global-cycle (&optional arg)
  5706. "Cycle the global visibility. For details see `org-cycle'.
  5707. With \\[universal-argument] prefix arg, switch to startup visibility.
  5708. With a numeric prefix, show all headlines up to that level."
  5709. (interactive "P")
  5710. (let ((org-cycle-include-plain-lists
  5711. (if (eq major-mode 'org-mode) org-cycle-include-plain-lists nil)))
  5712. (cond
  5713. ((integerp arg)
  5714. (show-all)
  5715. (hide-sublevels arg)
  5716. (setq org-cycle-global-status 'contents))
  5717. ((equal arg '(4))
  5718. (org-set-startup-visibility)
  5719. (message "Startup visibility, plus VISIBILITY properties."))
  5720. (t
  5721. (org-cycle '(4))))))
  5722. (defun org-set-startup-visibility ()
  5723. "Set the visibility required by startup options and properties."
  5724. (cond
  5725. ((eq org-startup-folded t)
  5726. (org-cycle '(4)))
  5727. ((eq org-startup-folded 'content)
  5728. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  5729. (org-cycle '(4)) (org-cycle '(4)))))
  5730. (unless (eq org-startup-folded 'showeverything)
  5731. (if org-hide-block-startup (org-hide-block-all))
  5732. (org-set-visibility-according-to-property 'no-cleanup)
  5733. (org-cycle-hide-archived-subtrees 'all)
  5734. (org-cycle-hide-drawers 'all)
  5735. (org-cycle-show-empty-lines t)))
  5736. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  5737. "Switch subtree visibilities according to :VISIBILITY: property."
  5738. (interactive)
  5739. (let (org-show-entry-below state)
  5740. (save-excursion
  5741. (goto-char (point-min))
  5742. (while (re-search-forward
  5743. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  5744. nil t)
  5745. (setq state (match-string 1))
  5746. (save-excursion
  5747. (org-back-to-heading t)
  5748. (hide-subtree)
  5749. (org-reveal)
  5750. (cond
  5751. ((equal state '("fold" "folded"))
  5752. (hide-subtree))
  5753. ((equal state "children")
  5754. (org-show-hidden-entry)
  5755. (show-children))
  5756. ((equal state "content")
  5757. (save-excursion
  5758. (save-restriction
  5759. (org-narrow-to-subtree)
  5760. (org-content))))
  5761. ((member state '("all" "showall"))
  5762. (show-subtree)))))
  5763. (unless no-cleanup
  5764. (org-cycle-hide-archived-subtrees 'all)
  5765. (org-cycle-hide-drawers 'all)
  5766. (org-cycle-show-empty-lines 'all)))))
  5767. ;; This function uses outline-regexp instead of the more fundamental
  5768. ;; org-outline-regexp so that org-cycle-global works outside of Org
  5769. ;; buffers, where outline-regexp is needed.
  5770. (defun org-overview ()
  5771. "Switch to overview mode, showing only top-level headlines.
  5772. Really, this shows all headlines with level equal or greater than the level
  5773. of the first headline in the buffer. This is important, because if the
  5774. first headline is not level one, then (hide-sublevels 1) gives confusing
  5775. results."
  5776. (interactive)
  5777. (let ((level (save-excursion
  5778. (goto-char (point-min))
  5779. (if (re-search-forward (concat "^" outline-regexp) nil t)
  5780. (progn
  5781. (goto-char (match-beginning 0))
  5782. (funcall outline-level))))))
  5783. (and level (hide-sublevels level))))
  5784. (defun org-content (&optional arg)
  5785. "Show all headlines in the buffer, like a table of contents.
  5786. With numerical argument N, show content up to level N."
  5787. (interactive "P")
  5788. (save-excursion
  5789. ;; Visit all headings and show their offspring
  5790. (and (integerp arg) (org-overview))
  5791. (goto-char (point-max))
  5792. (catch 'exit
  5793. (while (and (progn (condition-case nil
  5794. (outline-previous-visible-heading 1)
  5795. (error (goto-char (point-min))))
  5796. t)
  5797. (looking-at org-outline-regexp))
  5798. (if (integerp arg)
  5799. (show-children (1- arg))
  5800. (show-branches))
  5801. (if (bobp) (throw 'exit nil))))))
  5802. (defun org-optimize-window-after-visibility-change (state)
  5803. "Adjust the window after a change in outline visibility.
  5804. This function is the default value of the hook `org-cycle-hook'."
  5805. (when (get-buffer-window (current-buffer))
  5806. (cond
  5807. ((eq state 'content) nil)
  5808. ((eq state 'all) nil)
  5809. ((eq state 'folded) nil)
  5810. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  5811. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  5812. (defun org-remove-empty-overlays-at (pos)
  5813. "Remove outline overlays that do not contain non-white stuff."
  5814. (mapc
  5815. (lambda (o)
  5816. (and (eq 'outline (overlay-get o 'invisible))
  5817. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  5818. (overlay-end o))))
  5819. (delete-overlay o)))
  5820. (overlays-at pos)))
  5821. (defun org-clean-visibility-after-subtree-move ()
  5822. "Fix visibility issues after moving a subtree."
  5823. ;; First, find a reasonable region to look at:
  5824. ;; Start two siblings above, end three below
  5825. (let* ((beg (save-excursion
  5826. (and (org-get-last-sibling)
  5827. (org-get-last-sibling))
  5828. (point)))
  5829. (end (save-excursion
  5830. (and (org-get-next-sibling)
  5831. (org-get-next-sibling)
  5832. (org-get-next-sibling))
  5833. (if (org-at-heading-p)
  5834. (point-at-eol)
  5835. (point))))
  5836. (level (looking-at "\\*+"))
  5837. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  5838. (save-excursion
  5839. (save-restriction
  5840. (narrow-to-region beg end)
  5841. (when re
  5842. ;; Properly fold already folded siblings
  5843. (goto-char (point-min))
  5844. (while (re-search-forward re nil t)
  5845. (if (and (not (outline-invisible-p))
  5846. (save-excursion
  5847. (goto-char (point-at-eol)) (outline-invisible-p)))
  5848. (hide-entry))))
  5849. (org-cycle-show-empty-lines 'overview)
  5850. (org-cycle-hide-drawers 'overview)))))
  5851. (defun org-cycle-show-empty-lines (state)
  5852. "Show empty lines above all visible headlines.
  5853. The region to be covered depends on STATE when called through
  5854. `org-cycle-hook'. Lisp program can use t for STATE to get the
  5855. entire buffer covered. Note that an empty line is only shown if there
  5856. are at least `org-cycle-separator-lines' empty lines before the headline."
  5857. (when (not (= org-cycle-separator-lines 0))
  5858. (save-excursion
  5859. (let* ((n (abs org-cycle-separator-lines))
  5860. (re (cond
  5861. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  5862. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  5863. (t (let ((ns (number-to-string (- n 2))))
  5864. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  5865. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  5866. beg end b e)
  5867. (cond
  5868. ((memq state '(overview contents t))
  5869. (setq beg (point-min) end (point-max)))
  5870. ((memq state '(children folded))
  5871. (setq beg (point) end (progn (org-end-of-subtree t t)
  5872. (beginning-of-line 2)
  5873. (point)))))
  5874. (when beg
  5875. (goto-char beg)
  5876. (while (re-search-forward re end t)
  5877. (unless (get-char-property (match-end 1) 'invisible)
  5878. (setq e (match-end 1))
  5879. (if (< org-cycle-separator-lines 0)
  5880. (setq b (save-excursion
  5881. (goto-char (match-beginning 0))
  5882. (org-back-over-empty-lines)
  5883. (if (save-excursion
  5884. (goto-char (max (point-min) (1- (point))))
  5885. (org-at-heading-p))
  5886. (1- (point))
  5887. (point))))
  5888. (setq b (match-beginning 1)))
  5889. (outline-flag-region b e nil)))))))
  5890. ;; Never hide empty lines at the end of the file.
  5891. (save-excursion
  5892. (goto-char (point-max))
  5893. (outline-previous-heading)
  5894. (outline-end-of-heading)
  5895. (if (and (looking-at "[ \t\n]+")
  5896. (= (match-end 0) (point-max)))
  5897. (outline-flag-region (point) (match-end 0) nil))))
  5898. (defun org-show-empty-lines-in-parent ()
  5899. "Move to the parent and re-show empty lines before visible headlines."
  5900. (save-excursion
  5901. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  5902. (org-cycle-show-empty-lines context))))
  5903. (defun org-files-list ()
  5904. "Return `org-agenda-files' list, plus all open org-mode files.
  5905. This is useful for operations that need to scan all of a user's
  5906. open and agenda-wise Org files."
  5907. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  5908. (dolist (buf (buffer-list))
  5909. (with-current-buffer buf
  5910. (if (and (eq major-mode 'org-mode) (buffer-file-name))
  5911. (let ((file (expand-file-name (buffer-file-name))))
  5912. (unless (member file files)
  5913. (push file files))))))
  5914. files))
  5915. (defsubst org-entry-beginning-position ()
  5916. "Return the beginning position of the current entry."
  5917. (save-excursion (outline-back-to-heading t) (point)))
  5918. (defsubst org-entry-end-position ()
  5919. "Return the end position of the current entry."
  5920. (save-excursion (outline-next-heading) (point)))
  5921. (defun org-cycle-hide-drawers (state)
  5922. "Re-hide all drawers after a visibility state change."
  5923. (when (and (eq major-mode 'org-mode)
  5924. (not (memq state '(overview folded contents))))
  5925. (save-excursion
  5926. (let* ((globalp (memq state '(contents all)))
  5927. (beg (if globalp (point-min) (point)))
  5928. (end (if globalp (point-max)
  5929. (if (eq state 'children)
  5930. (save-excursion (outline-next-heading) (point))
  5931. (org-end-of-subtree t)))))
  5932. (goto-char beg)
  5933. (while (re-search-forward org-drawer-regexp end t)
  5934. (org-flag-drawer t))))))
  5935. (defun org-flag-drawer (flag)
  5936. (save-excursion
  5937. (beginning-of-line 1)
  5938. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  5939. (let ((b (match-end 0)))
  5940. (if (re-search-forward
  5941. "^[ \t]*:END:"
  5942. (save-excursion (outline-next-heading) (point)) t)
  5943. (outline-flag-region b (point-at-eol) flag)
  5944. (error ":END: line missing at position %s" b))))))
  5945. (defun org-subtree-end-visible-p ()
  5946. "Is the end of the current subtree visible?"
  5947. (pos-visible-in-window-p
  5948. (save-excursion (org-end-of-subtree t) (point))))
  5949. (defun org-first-headline-recenter (&optional N)
  5950. "Move cursor to the first headline and recenter the headline.
  5951. Optional argument N means put the headline into the Nth line of the window."
  5952. (goto-char (point-min))
  5953. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  5954. (beginning-of-line)
  5955. (recenter (prefix-numeric-value N))))
  5956. ;;; Saving and restoring visibility
  5957. (defun org-outline-overlay-data (&optional use-markers)
  5958. "Return a list of the locations of all outline overlays.
  5959. These are overlays with the `invisible' property value `outline'.
  5960. The return value is a list of cons cells, with start and stop
  5961. positions for each overlay.
  5962. If USE-MARKERS is set, return the positions as markers."
  5963. (let (beg end)
  5964. (save-excursion
  5965. (save-restriction
  5966. (widen)
  5967. (delq nil
  5968. (mapcar (lambda (o)
  5969. (when (eq (overlay-get o 'invisible) 'outline)
  5970. (setq beg (overlay-start o)
  5971. end (overlay-end o))
  5972. (and beg end (> end beg)
  5973. (if use-markers
  5974. (cons (move-marker (make-marker) beg)
  5975. (move-marker (make-marker) end))
  5976. (cons beg end)))))
  5977. (overlays-in (point-min) (point-max))))))))
  5978. (defun org-set-outline-overlay-data (data)
  5979. "Create visibility overlays for all positions in DATA.
  5980. DATA should have been made by `org-outline-overlay-data'."
  5981. (let (o)
  5982. (save-excursion
  5983. (save-restriction
  5984. (widen)
  5985. (show-all)
  5986. (mapc (lambda (c)
  5987. (outline-flag-region (car c) (cdr c) t))
  5988. data)))))
  5989. ;;; Folding of blocks
  5990. (defvar org-hide-block-overlays nil
  5991. "Overlays hiding blocks.")
  5992. (make-variable-buffer-local 'org-hide-block-overlays)
  5993. (defun org-block-map (function &optional start end)
  5994. "Call FUNCTION at the head of all source blocks in the current buffer.
  5995. Optional arguments START and END can be used to limit the range."
  5996. (let ((start (or start (point-min)))
  5997. (end (or end (point-max))))
  5998. (save-excursion
  5999. (goto-char start)
  6000. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  6001. (save-excursion
  6002. (save-match-data
  6003. (goto-char (match-beginning 0))
  6004. (funcall function)))))))
  6005. (defun org-hide-block-toggle-all ()
  6006. "Toggle the visibility of all blocks in the current buffer."
  6007. (org-block-map #'org-hide-block-toggle))
  6008. (defun org-hide-block-all ()
  6009. "Fold all blocks in the current buffer."
  6010. (interactive)
  6011. (org-show-block-all)
  6012. (org-block-map #'org-hide-block-toggle-maybe))
  6013. (defun org-show-block-all ()
  6014. "Unfold all blocks in the current buffer."
  6015. (interactive)
  6016. (mapc 'delete-overlay org-hide-block-overlays)
  6017. (setq org-hide-block-overlays nil))
  6018. (defun org-hide-block-toggle-maybe ()
  6019. "Toggle visibility of block at point."
  6020. (interactive)
  6021. (let ((case-fold-search t))
  6022. (if (save-excursion
  6023. (beginning-of-line 1)
  6024. (looking-at org-block-regexp))
  6025. (progn (org-hide-block-toggle)
  6026. t) ;; to signal that we took action
  6027. nil))) ;; to signal that we did not
  6028. (defun org-hide-block-toggle (&optional force)
  6029. "Toggle the visibility of the current block."
  6030. (interactive)
  6031. (save-excursion
  6032. (beginning-of-line)
  6033. (if (re-search-forward org-block-regexp nil t)
  6034. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  6035. (end (match-end 0)) ;; end of entire body
  6036. ov)
  6037. (if (memq t (mapcar (lambda (overlay)
  6038. (eq (overlay-get overlay 'invisible)
  6039. 'org-hide-block))
  6040. (overlays-at start)))
  6041. (if (or (not force) (eq force 'off))
  6042. (mapc (lambda (ov)
  6043. (when (member ov org-hide-block-overlays)
  6044. (setq org-hide-block-overlays
  6045. (delq ov org-hide-block-overlays)))
  6046. (when (eq (overlay-get ov 'invisible)
  6047. 'org-hide-block)
  6048. (delete-overlay ov)))
  6049. (overlays-at start)))
  6050. (setq ov (make-overlay start end))
  6051. (overlay-put ov 'invisible 'org-hide-block)
  6052. ;; make the block accessible to isearch
  6053. (overlay-put
  6054. ov 'isearch-open-invisible
  6055. (lambda (ov)
  6056. (when (member ov org-hide-block-overlays)
  6057. (setq org-hide-block-overlays
  6058. (delq ov org-hide-block-overlays)))
  6059. (when (eq (overlay-get ov 'invisible)
  6060. 'org-hide-block)
  6061. (delete-overlay ov))))
  6062. (push ov org-hide-block-overlays)))
  6063. (error "Not looking at a source block"))))
  6064. ;; org-tab-after-check-for-cycling-hook
  6065. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  6066. ;; Remove overlays when changing major mode
  6067. (add-hook 'org-mode-hook
  6068. (lambda () (org-add-hook 'change-major-mode-hook
  6069. 'org-show-block-all 'append 'local)))
  6070. ;;; Org-goto
  6071. (defvar org-goto-window-configuration nil)
  6072. (defvar org-goto-marker nil)
  6073. (defvar org-goto-map
  6074. (let ((map (make-sparse-keymap)))
  6075. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  6076. (while (setq cmd (pop cmds))
  6077. (substitute-key-definition cmd cmd map global-map)))
  6078. (suppress-keymap map)
  6079. (org-defkey map "\C-m" 'org-goto-ret)
  6080. (org-defkey map [(return)] 'org-goto-ret)
  6081. (org-defkey map [(left)] 'org-goto-left)
  6082. (org-defkey map [(right)] 'org-goto-right)
  6083. (org-defkey map [(control ?g)] 'org-goto-quit)
  6084. (org-defkey map "\C-i" 'org-cycle)
  6085. (org-defkey map [(tab)] 'org-cycle)
  6086. (org-defkey map [(down)] 'outline-next-visible-heading)
  6087. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6088. (if org-goto-auto-isearch
  6089. (if (fboundp 'define-key-after)
  6090. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6091. nil)
  6092. (org-defkey map "q" 'org-goto-quit)
  6093. (org-defkey map "n" 'outline-next-visible-heading)
  6094. (org-defkey map "p" 'outline-previous-visible-heading)
  6095. (org-defkey map "f" 'outline-forward-same-level)
  6096. (org-defkey map "b" 'outline-backward-same-level)
  6097. (org-defkey map "u" 'outline-up-heading))
  6098. (org-defkey map "/" 'org-occur)
  6099. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6100. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6101. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6102. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6103. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6104. map))
  6105. (defconst org-goto-help
  6106. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  6107. RET=jump to location [Q]uit and return to previous location
  6108. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6109. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6110. ;; FIXME: Docstring does not mention both interfaces
  6111. (defun org-goto (&optional alternative-interface)
  6112. "Look up a different location in the current file, keeping current visibility.
  6113. When you want look-up or go to a different location in a document, the
  6114. fastest way is often to fold the entire buffer and then dive into the tree.
  6115. This method has the disadvantage, that the previous location will be folded,
  6116. which may not be what you want.
  6117. This command works around this by showing a copy of the current buffer
  6118. in an indirect buffer, in overview mode. You can dive into the tree in
  6119. that copy, use org-occur and incremental search to find a location.
  6120. When pressing RET or `Q', the command returns to the original buffer in
  6121. which the visibility is still unchanged. After RET it will also jump to
  6122. the location selected in the indirect buffer and expose the headline
  6123. hierarchy above."
  6124. (interactive "P")
  6125. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6126. (org-refile-use-outline-path t)
  6127. (org-refile-target-verify-function nil)
  6128. (interface
  6129. (if (not alternative-interface)
  6130. org-goto-interface
  6131. (if (eq org-goto-interface 'outline)
  6132. 'outline-path-completion
  6133. 'outline)))
  6134. (org-goto-start-pos (point))
  6135. (selected-point
  6136. (if (eq interface 'outline)
  6137. (car (org-get-location (current-buffer) org-goto-help))
  6138. (let ((pa (org-refile-get-location "Goto" nil nil t)))
  6139. (org-refile-check-position pa)
  6140. (nth 3 pa)))))
  6141. (if selected-point
  6142. (progn
  6143. (org-mark-ring-push org-goto-start-pos)
  6144. (goto-char selected-point)
  6145. (if (or (outline-invisible-p) (org-invisible-p2))
  6146. (org-show-context 'org-goto)))
  6147. (message "Quit"))))
  6148. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6149. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6150. (defvar org-goto-local-auto-isearch-map) ; defined below
  6151. (defun org-get-location (buf help)
  6152. "Let the user select a location in the Org-mode buffer BUF.
  6153. This function uses a recursive edit. It returns the selected position
  6154. or nil."
  6155. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6156. (isearch-hide-immediately nil)
  6157. (isearch-search-fun-function
  6158. (lambda () 'org-goto-local-search-headings))
  6159. (org-goto-selected-point org-goto-exit-command)
  6160. (pop-up-frames nil)
  6161. (special-display-buffer-names nil)
  6162. (special-display-regexps nil)
  6163. (special-display-function nil))
  6164. (save-excursion
  6165. (save-window-excursion
  6166. (delete-other-windows)
  6167. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6168. (org-pop-to-buffer-same-window
  6169. (condition-case nil
  6170. (make-indirect-buffer (current-buffer) "*org-goto*")
  6171. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6172. (with-output-to-temp-buffer "*Help*"
  6173. (princ help))
  6174. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  6175. (setq buffer-read-only nil)
  6176. (let ((org-startup-truncated t)
  6177. (org-startup-folded nil)
  6178. (org-startup-align-all-tables nil))
  6179. (org-mode)
  6180. (org-overview))
  6181. (setq buffer-read-only t)
  6182. (if (and (boundp 'org-goto-start-pos)
  6183. (integer-or-marker-p org-goto-start-pos))
  6184. (let ((org-show-hierarchy-above t)
  6185. (org-show-siblings t)
  6186. (org-show-following-heading t))
  6187. (goto-char org-goto-start-pos)
  6188. (and (outline-invisible-p) (org-show-context)))
  6189. (goto-char (point-min)))
  6190. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6191. (message "Select location and press RET")
  6192. (use-local-map org-goto-map)
  6193. (recursive-edit)
  6194. ))
  6195. (kill-buffer "*org-goto*")
  6196. (cons org-goto-selected-point org-goto-exit-command)))
  6197. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6198. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6199. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6200. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  6201. (defun org-goto-local-search-headings (string bound noerror)
  6202. "Search and make sure that any matches are in headlines."
  6203. (catch 'return
  6204. (while (if isearch-forward
  6205. (search-forward string bound noerror)
  6206. (search-backward string bound noerror))
  6207. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  6208. (and (member :headline context)
  6209. (not (member :tags context))))
  6210. (throw 'return (point))))))
  6211. (defun org-goto-local-auto-isearch ()
  6212. "Start isearch."
  6213. (interactive)
  6214. (goto-char (point-min))
  6215. (let ((keys (this-command-keys)))
  6216. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6217. (isearch-mode t)
  6218. (isearch-process-search-char (string-to-char keys)))))
  6219. (defun org-goto-ret (&optional arg)
  6220. "Finish `org-goto' by going to the new location."
  6221. (interactive "P")
  6222. (setq org-goto-selected-point (point)
  6223. org-goto-exit-command 'return)
  6224. (throw 'exit nil))
  6225. (defun org-goto-left ()
  6226. "Finish `org-goto' by going to the new location."
  6227. (interactive)
  6228. (if (org-at-heading-p)
  6229. (progn
  6230. (beginning-of-line 1)
  6231. (setq org-goto-selected-point (point)
  6232. org-goto-exit-command 'left)
  6233. (throw 'exit nil))
  6234. (error "Not on a heading")))
  6235. (defun org-goto-right ()
  6236. "Finish `org-goto' by going to the new location."
  6237. (interactive)
  6238. (if (org-at-heading-p)
  6239. (progn
  6240. (setq org-goto-selected-point (point)
  6241. org-goto-exit-command 'right)
  6242. (throw 'exit nil))
  6243. (error "Not on a heading")))
  6244. (defun org-goto-quit ()
  6245. "Finish `org-goto' without cursor motion."
  6246. (interactive)
  6247. (setq org-goto-selected-point nil)
  6248. (setq org-goto-exit-command 'quit)
  6249. (throw 'exit nil))
  6250. ;;; Indirect buffer display of subtrees
  6251. (defvar org-indirect-dedicated-frame nil
  6252. "This is the frame being used for indirect tree display.")
  6253. (defvar org-last-indirect-buffer nil)
  6254. (defun org-tree-to-indirect-buffer (&optional arg)
  6255. "Create indirect buffer and narrow it to current subtree.
  6256. With numerical prefix ARG, go up to this level and then take that tree.
  6257. If ARG is negative, go up that many levels.
  6258. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6259. indirect buffer previously made with this command, to avoid proliferation of
  6260. indirect buffers. However, when you call the command with a \
  6261. \\[universal-argument] prefix, or
  6262. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6263. is kept so that you can work with several indirect buffers at the same time.
  6264. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6265. \\[universal-argument] prefix also
  6266. requests that a new frame be made for the new buffer, so that the dedicated
  6267. frame is not changed."
  6268. (interactive "P")
  6269. (let ((cbuf (current-buffer))
  6270. (cwin (selected-window))
  6271. (pos (point))
  6272. beg end level heading ibuf)
  6273. (save-excursion
  6274. (org-back-to-heading t)
  6275. (when (numberp arg)
  6276. (setq level (org-outline-level))
  6277. (if (< arg 0) (setq arg (+ level arg)))
  6278. (while (> (setq level (org-outline-level)) arg)
  6279. (outline-up-heading 1 t)))
  6280. (setq beg (point)
  6281. heading (org-get-heading))
  6282. (org-end-of-subtree t t)
  6283. (if (org-at-heading-p) (backward-char 1))
  6284. (setq end (point)))
  6285. (if (and (buffer-live-p org-last-indirect-buffer)
  6286. (not (eq org-indirect-buffer-display 'new-frame))
  6287. (not arg))
  6288. (kill-buffer org-last-indirect-buffer))
  6289. (setq ibuf (org-get-indirect-buffer cbuf)
  6290. org-last-indirect-buffer ibuf)
  6291. (cond
  6292. ((or (eq org-indirect-buffer-display 'new-frame)
  6293. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6294. (select-frame (make-frame))
  6295. (delete-other-windows)
  6296. (org-pop-to-buffer-same-window ibuf)
  6297. (org-set-frame-title heading))
  6298. ((eq org-indirect-buffer-display 'dedicated-frame)
  6299. (raise-frame
  6300. (select-frame (or (and org-indirect-dedicated-frame
  6301. (frame-live-p org-indirect-dedicated-frame)
  6302. org-indirect-dedicated-frame)
  6303. (setq org-indirect-dedicated-frame (make-frame)))))
  6304. (delete-other-windows)
  6305. (org-pop-to-buffer-same-window ibuf)
  6306. (org-set-frame-title (concat "Indirect: " heading)))
  6307. ((eq org-indirect-buffer-display 'current-window)
  6308. (org-pop-to-buffer-same-window ibuf))
  6309. ((eq org-indirect-buffer-display 'other-window)
  6310. (pop-to-buffer ibuf))
  6311. (t (error "Invalid value")))
  6312. (if (featurep 'xemacs)
  6313. (save-excursion (org-mode) (turn-on-font-lock)))
  6314. (narrow-to-region beg end)
  6315. (show-all)
  6316. (goto-char pos)
  6317. (run-hook-with-args 'org-cycle-hook 'all)
  6318. (and (window-live-p cwin) (select-window cwin))))
  6319. (defun org-get-indirect-buffer (&optional buffer)
  6320. (setq buffer (or buffer (current-buffer)))
  6321. (let ((n 1) (base (buffer-name buffer)) bname)
  6322. (while (buffer-live-p
  6323. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  6324. (setq n (1+ n)))
  6325. (condition-case nil
  6326. (make-indirect-buffer buffer bname 'clone)
  6327. (error (make-indirect-buffer buffer bname)))))
  6328. (defun org-set-frame-title (title)
  6329. "Set the title of the current frame to the string TITLE."
  6330. ;; FIXME: how to name a single frame in XEmacs???
  6331. (unless (featurep 'xemacs)
  6332. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6333. ;;;; Structure editing
  6334. ;;; Inserting headlines
  6335. (defun org-previous-line-empty-p ()
  6336. (save-excursion
  6337. (and (not (bobp))
  6338. (or (beginning-of-line 0) t)
  6339. (save-match-data
  6340. (looking-at "[ \t]*$")))))
  6341. (defun org-insert-heading (&optional force-heading invisible-ok)
  6342. "Insert a new heading or item with same depth at point.
  6343. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  6344. If point is at the beginning of a headline, insert a sibling before the
  6345. current headline. If point is not at the beginning, split the line,
  6346. create the new headline with the text in the current line after point
  6347. \(but see also the variable `org-M-RET-may-split-line').
  6348. When INVISIBLE-OK is set, stop at invisible headlines when going back.
  6349. This is important for non-interactive uses of the command."
  6350. (interactive "P")
  6351. (if (or (= (buffer-size) 0)
  6352. (and (not (save-excursion
  6353. (and (ignore-errors (org-back-to-heading invisible-ok))
  6354. (org-at-heading-p))))
  6355. (or force-heading (not (org-in-item-p)))))
  6356. (progn
  6357. (insert "\n* ")
  6358. (run-hooks 'org-insert-heading-hook))
  6359. (when (or force-heading (not (org-insert-item)))
  6360. (let* ((empty-line-p nil)
  6361. (level nil)
  6362. (on-heading (org-at-heading-p))
  6363. (head (save-excursion
  6364. (condition-case nil
  6365. (progn
  6366. (org-back-to-heading invisible-ok)
  6367. (when (and (not on-heading)
  6368. (featurep 'org-inlinetask)
  6369. (integerp org-inlinetask-min-level)
  6370. (>= (length (match-string 0))
  6371. org-inlinetask-min-level))
  6372. ;; Find a heading level before the inline task
  6373. (while (and (setq level (org-up-heading-safe))
  6374. (>= level org-inlinetask-min-level)))
  6375. (if (org-at-heading-p)
  6376. (org-back-to-heading invisible-ok)
  6377. (error "This should not happen")))
  6378. (setq empty-line-p (org-previous-line-empty-p))
  6379. (match-string 0))
  6380. (error "*"))))
  6381. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6382. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6383. pos hide-previous previous-pos)
  6384. (cond
  6385. ((and (org-at-heading-p) (bolp)
  6386. (or (bobp)
  6387. (save-excursion (backward-char 1) (not (outline-invisible-p)))))
  6388. ;; insert before the current line
  6389. (open-line (if blank 2 1)))
  6390. ((and (bolp)
  6391. (not org-insert-heading-respect-content)
  6392. (or (bobp)
  6393. (save-excursion
  6394. (backward-char 1) (not (outline-invisible-p)))))
  6395. ;; insert right here
  6396. nil)
  6397. (t
  6398. ;; somewhere in the line
  6399. (save-excursion
  6400. (setq previous-pos (point-at-bol))
  6401. (end-of-line)
  6402. (setq hide-previous (outline-invisible-p)))
  6403. (and org-insert-heading-respect-content (org-show-subtree))
  6404. (let ((split
  6405. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  6406. (save-excursion
  6407. (let ((p (point)))
  6408. (goto-char (point-at-bol))
  6409. (and (looking-at org-complex-heading-regexp)
  6410. (match-beginning 4)
  6411. (> p (match-beginning 4)))))))
  6412. tags pos)
  6413. (cond
  6414. (org-insert-heading-respect-content
  6415. (org-end-of-subtree nil t)
  6416. (when (featurep 'org-inlinetask)
  6417. (while (and (not (eobp))
  6418. (looking-at "\\(\\*+\\)[ \t]+")
  6419. (>= (length (match-string 1))
  6420. org-inlinetask-min-level))
  6421. (org-end-of-subtree nil t)))
  6422. (or (bolp) (newline))
  6423. (or (org-previous-line-empty-p)
  6424. (and blank (newline)))
  6425. (open-line 1))
  6426. ((org-at-heading-p)
  6427. (when hide-previous
  6428. (show-children)
  6429. (org-show-entry))
  6430. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
  6431. (setq tags (and (match-end 2) (match-string 2)))
  6432. (and (match-end 1)
  6433. (delete-region (match-beginning 1) (match-end 1)))
  6434. (setq pos (point-at-bol))
  6435. (or split (end-of-line 1))
  6436. (delete-horizontal-space)
  6437. (if (string-match "\\`\\*+\\'"
  6438. (buffer-substring (point-at-bol) (point)))
  6439. (insert " "))
  6440. (newline (if blank 2 1))
  6441. (when tags
  6442. (save-excursion
  6443. (goto-char pos)
  6444. (end-of-line 1)
  6445. (insert " " tags)
  6446. (org-set-tags nil 'align))))
  6447. (t
  6448. (or split (end-of-line 1))
  6449. (newline (if blank 2 1)))))))
  6450. (insert head) (just-one-space)
  6451. (setq pos (point))
  6452. (end-of-line 1)
  6453. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  6454. (when (and org-insert-heading-respect-content hide-previous)
  6455. (save-excursion
  6456. (goto-char previous-pos)
  6457. (hide-subtree)))
  6458. (run-hooks 'org-insert-heading-hook)))))
  6459. (defun org-get-heading (&optional no-tags no-todo)
  6460. "Return the heading of the current entry, without the stars.
  6461. When NO-TAGS is non-nil, don't include tags.
  6462. When NO-TODO is non-nil, don't include TODO keywords."
  6463. (save-excursion
  6464. (org-back-to-heading t)
  6465. (cond
  6466. ((and no-tags no-todo)
  6467. (looking-at org-complex-heading-regexp)
  6468. (match-string 4))
  6469. (no-tags
  6470. (looking-at (concat org-outline-regexp
  6471. "\\(.*?\\)"
  6472. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  6473. (match-string 1))
  6474. (no-todo
  6475. (looking-at org-todo-line-regexp)
  6476. (match-string 3))
  6477. (t (looking-at org-heading-regexp)
  6478. (match-string 2)))))
  6479. (defun org-heading-components ()
  6480. "Return the components of the current heading.
  6481. This is a list with the following elements:
  6482. - the level as an integer
  6483. - the reduced level, different if `org-odd-levels-only' is set.
  6484. - the TODO keyword, or nil
  6485. - the priority character, like ?A, or nil if no priority is given
  6486. - the headline text itself, or the tags string if no headline text
  6487. - the tags string, or nil."
  6488. (save-excursion
  6489. (org-back-to-heading t)
  6490. (if (let (case-fold-search) (looking-at org-complex-heading-regexp))
  6491. (list (length (match-string 1))
  6492. (org-reduced-level (length (match-string 1)))
  6493. (org-match-string-no-properties 2)
  6494. (and (match-end 3) (aref (match-string 3) 2))
  6495. (org-match-string-no-properties 4)
  6496. (org-match-string-no-properties 5)))))
  6497. (defun org-get-entry ()
  6498. "Get the entry text, after heading, entire subtree."
  6499. (save-excursion
  6500. (org-back-to-heading t)
  6501. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  6502. (defun org-insert-heading-after-current ()
  6503. "Insert a new heading with same level as current, after current subtree."
  6504. (interactive)
  6505. (org-back-to-heading)
  6506. (org-insert-heading)
  6507. (org-move-subtree-down)
  6508. (end-of-line 1))
  6509. (defun org-insert-heading-respect-content ()
  6510. (interactive)
  6511. (let ((org-insert-heading-respect-content t))
  6512. (org-insert-heading t)))
  6513. (defun org-insert-todo-heading-respect-content (&optional force-state)
  6514. (interactive "P")
  6515. (let ((org-insert-heading-respect-content t))
  6516. (org-insert-todo-heading force-state t)))
  6517. (defun org-insert-todo-heading (arg &optional force-heading)
  6518. "Insert a new heading with the same level and TODO state as current heading.
  6519. If the heading has no TODO state, or if the state is DONE, use the first
  6520. state (TODO by default). Also with prefix arg, force first state."
  6521. (interactive "P")
  6522. (when (or force-heading (not (org-insert-item 'checkbox)))
  6523. (org-insert-heading force-heading)
  6524. (save-excursion
  6525. (org-back-to-heading)
  6526. (outline-previous-heading)
  6527. (looking-at org-todo-line-regexp))
  6528. (let*
  6529. ((new-mark-x
  6530. (if (or arg
  6531. (not (match-beginning 2))
  6532. (member (match-string 2) org-done-keywords))
  6533. (car org-todo-keywords-1)
  6534. (match-string 2)))
  6535. (new-mark
  6536. (or
  6537. (run-hook-with-args-until-success
  6538. 'org-todo-get-default-hook new-mark-x nil)
  6539. new-mark-x)))
  6540. (beginning-of-line 1)
  6541. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  6542. (if org-treat-insert-todo-heading-as-state-change
  6543. (org-todo new-mark)
  6544. (insert new-mark " "))))
  6545. (when org-provide-todo-statistics
  6546. (org-update-parent-todo-statistics))))
  6547. (defun org-insert-subheading (arg)
  6548. "Insert a new subheading and demote it.
  6549. Works for outline headings and for plain lists alike."
  6550. (interactive "P")
  6551. (org-insert-heading arg)
  6552. (cond
  6553. ((org-at-heading-p) (org-do-demote))
  6554. ((org-at-item-p) (org-indent-item))))
  6555. (defun org-insert-todo-subheading (arg)
  6556. "Insert a new subheading with TODO keyword or checkbox and demote it.
  6557. Works for outline headings and for plain lists alike."
  6558. (interactive "P")
  6559. (org-insert-todo-heading arg)
  6560. (cond
  6561. ((org-at-heading-p) (org-do-demote))
  6562. ((org-at-item-p) (org-indent-item))))
  6563. ;;; Promotion and Demotion
  6564. (defvar org-after-demote-entry-hook nil
  6565. "Hook run after an entry has been demoted.
  6566. The cursor will be at the beginning of the entry.
  6567. When a subtree is being demoted, the hook will be called for each node.")
  6568. (defvar org-after-promote-entry-hook nil
  6569. "Hook run after an entry has been promoted.
  6570. The cursor will be at the beginning of the entry.
  6571. When a subtree is being promoted, the hook will be called for each node.")
  6572. (defun org-promote-subtree ()
  6573. "Promote the entire subtree.
  6574. See also `org-promote'."
  6575. (interactive)
  6576. (save-excursion
  6577. (org-with-limited-levels (org-map-tree 'org-promote)))
  6578. (org-fix-position-after-promote))
  6579. (defun org-demote-subtree ()
  6580. "Demote the entire subtree. See `org-demote'.
  6581. See also `org-promote'."
  6582. (interactive)
  6583. (save-excursion
  6584. (org-with-limited-levels (org-map-tree 'org-demote)))
  6585. (org-fix-position-after-promote))
  6586. (defun org-do-promote ()
  6587. "Promote the current heading higher up the tree.
  6588. If the region is active in `transient-mark-mode', promote all headings
  6589. in the region."
  6590. (interactive)
  6591. (save-excursion
  6592. (if (org-region-active-p)
  6593. (org-map-region 'org-promote (region-beginning) (region-end))
  6594. (org-promote)))
  6595. (org-fix-position-after-promote))
  6596. (defun org-do-demote ()
  6597. "Demote the current heading lower down the tree.
  6598. If the region is active in `transient-mark-mode', demote all headings
  6599. in the region."
  6600. (interactive)
  6601. (save-excursion
  6602. (if (org-region-active-p)
  6603. (org-map-region 'org-demote (region-beginning) (region-end))
  6604. (org-demote)))
  6605. (org-fix-position-after-promote))
  6606. (defun org-fix-position-after-promote ()
  6607. "Make sure that after pro/demotion cursor position is right."
  6608. (let ((pos (point)))
  6609. (when (save-excursion
  6610. (beginning-of-line 1)
  6611. (looking-at org-todo-line-regexp)
  6612. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  6613. (cond ((eobp) (insert " "))
  6614. ((eolp) (insert " "))
  6615. ((equal (char-after) ?\ ) (forward-char 1))))))
  6616. (defun org-current-level ()
  6617. "Return the level of the current entry, or nil if before the first headline.
  6618. The level is the number of stars at the beginning of the headline."
  6619. (save-excursion
  6620. (org-with-limited-levels
  6621. (if (ignore-errors (org-back-to-heading t))
  6622. (funcall outline-level)))))
  6623. (defun org-get-previous-line-level ()
  6624. "Return the outline depth of the last headline before the current line.
  6625. Returns 0 for the first headline in the buffer, and nil if before the
  6626. first headline."
  6627. (let ((current-level (org-current-level))
  6628. (prev-level (when (> (line-number-at-pos) 1)
  6629. (save-excursion
  6630. (beginning-of-line 0)
  6631. (org-current-level)))))
  6632. (cond ((null current-level) nil) ; Before first headline
  6633. ((null prev-level) 0) ; At first headline
  6634. (prev-level))))
  6635. (defun org-reduced-level (l)
  6636. "Compute the effective level of a heading.
  6637. This takes into account the setting of `org-odd-levels-only'."
  6638. (cond
  6639. ((zerop l) 0)
  6640. (org-odd-levels-only (1+ (floor (/ l 2))))
  6641. (t l)))
  6642. (defun org-level-increment ()
  6643. "Return the number of stars that will be added or removed at a
  6644. time to headlines when structure editing, based on the value of
  6645. `org-odd-levels-only'."
  6646. (if org-odd-levels-only 2 1))
  6647. (defun org-get-valid-level (level &optional change)
  6648. "Rectify a level change under the influence of `org-odd-levels-only'
  6649. LEVEL is a current level, CHANGE is by how much the level should be
  6650. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  6651. even level numbers will become the next higher odd number."
  6652. (if org-odd-levels-only
  6653. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  6654. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  6655. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  6656. (max 1 (+ level (or change 0)))))
  6657. (if (boundp 'define-obsolete-function-alias)
  6658. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  6659. (define-obsolete-function-alias 'org-get-legal-level
  6660. 'org-get-valid-level)
  6661. (define-obsolete-function-alias 'org-get-legal-level
  6662. 'org-get-valid-level "23.1")))
  6663. (defun org-promote ()
  6664. "Promote the current heading higher up the tree.
  6665. If the region is active in `transient-mark-mode', promote all headings
  6666. in the region."
  6667. (org-back-to-heading t)
  6668. (let* ((level (save-match-data (funcall outline-level)))
  6669. (after-change-functions (remove 'flyspell-after-change-function
  6670. after-change-functions))
  6671. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  6672. (diff (abs (- level (length up-head) -1))))
  6673. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  6674. (replace-match up-head nil t)
  6675. ;; Fixup tag positioning
  6676. (and org-auto-align-tags (org-set-tags nil t))
  6677. (if org-adapt-indentation (org-fixup-indentation (- diff)))
  6678. (run-hooks 'org-after-promote-entry-hook)))
  6679. (defun org-demote ()
  6680. "Demote the current heading lower down the tree.
  6681. If the region is active in `transient-mark-mode', demote all headings
  6682. in the region."
  6683. (org-back-to-heading t)
  6684. (let* ((level (save-match-data (funcall outline-level)))
  6685. (after-change-functions (remove 'flyspell-after-change-function
  6686. after-change-functions))
  6687. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  6688. (diff (abs (- level (length down-head) -1))))
  6689. (replace-match down-head nil t)
  6690. ;; Fixup tag positioning
  6691. (and org-auto-align-tags (org-set-tags nil t))
  6692. (if org-adapt-indentation (org-fixup-indentation diff))
  6693. (run-hooks 'org-after-demote-entry-hook)))
  6694. (defun org-cycle-level ()
  6695. "Cycle the level of an empty headline through possible states.
  6696. This goes first to child, then to parent, level, then up the hierarchy.
  6697. After top level, it switches back to sibling level."
  6698. (interactive)
  6699. (let ((org-adapt-indentation nil))
  6700. (when (org-point-at-end-of-empty-headline)
  6701. (setq this-command 'org-cycle-level) ; Only needed for caching
  6702. (let ((cur-level (org-current-level))
  6703. (prev-level (org-get-previous-line-level)))
  6704. (cond
  6705. ;; If first headline in file, promote to top-level.
  6706. ((= prev-level 0)
  6707. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6708. do (org-do-promote)))
  6709. ;; If same level as prev, demote one.
  6710. ((= prev-level cur-level)
  6711. (org-do-demote))
  6712. ;; If parent is top-level, promote to top level if not already.
  6713. ((= prev-level 1)
  6714. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6715. do (org-do-promote)))
  6716. ;; If top-level, return to prev-level.
  6717. ((= cur-level 1)
  6718. (loop repeat (/ (- prev-level 1) (org-level-increment))
  6719. do (org-do-demote)))
  6720. ;; If less than prev-level, promote one.
  6721. ((< cur-level prev-level)
  6722. (org-do-promote))
  6723. ;; If deeper than prev-level, promote until higher than
  6724. ;; prev-level.
  6725. ((> cur-level prev-level)
  6726. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  6727. do (org-do-promote))))
  6728. t))))
  6729. (defun org-map-tree (fun)
  6730. "Call FUN for every heading underneath the current one."
  6731. (org-back-to-heading)
  6732. (let ((level (funcall outline-level)))
  6733. (save-excursion
  6734. (funcall fun)
  6735. (while (and (progn
  6736. (outline-next-heading)
  6737. (> (funcall outline-level) level))
  6738. (not (eobp)))
  6739. (funcall fun)))))
  6740. (defun org-map-region (fun beg end)
  6741. "Call FUN for every heading between BEG and END."
  6742. (let ((org-ignore-region t))
  6743. (save-excursion
  6744. (setq end (copy-marker end))
  6745. (goto-char beg)
  6746. (if (and (re-search-forward org-outline-regexp-bol nil t)
  6747. (< (point) end))
  6748. (funcall fun))
  6749. (while (and (progn
  6750. (outline-next-heading)
  6751. (< (point) end))
  6752. (not (eobp)))
  6753. (funcall fun)))))
  6754. (defvar org-property-end-re) ; silence byte-compiler
  6755. (defun org-fixup-indentation (diff)
  6756. "Change the indentation in the current entry by DIFF.
  6757. However, if any line in the current entry has no indentation, or if it
  6758. would end up with no indentation after the change, nothing at all is done."
  6759. (save-excursion
  6760. (let ((end (save-excursion (outline-next-heading)
  6761. (point-marker)))
  6762. (prohibit (if (> diff 0)
  6763. "^\\S-"
  6764. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  6765. col)
  6766. (unless (save-excursion (end-of-line 1)
  6767. (re-search-forward prohibit end t))
  6768. (while (and (< (point) end)
  6769. (re-search-forward "^[ \t]+" end t))
  6770. (goto-char (match-end 0))
  6771. (setq col (current-column))
  6772. (if (< diff 0) (replace-match ""))
  6773. (org-indent-to-column (+ diff col))))
  6774. (move-marker end nil))))
  6775. (defun org-convert-to-odd-levels ()
  6776. "Convert an org-mode file with all levels allowed to one with odd levels.
  6777. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  6778. level 5 etc."
  6779. (interactive)
  6780. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  6781. (let ((outline-level 'org-outline-level)
  6782. (org-odd-levels-only nil) n)
  6783. (save-excursion
  6784. (goto-char (point-min))
  6785. (while (re-search-forward "^\\*\\*+ " nil t)
  6786. (setq n (- (length (match-string 0)) 2))
  6787. (while (>= (setq n (1- n)) 0)
  6788. (org-demote))
  6789. (end-of-line 1))))))
  6790. (defun org-convert-to-oddeven-levels ()
  6791. "Convert an org-mode file with only odd levels to one with odd/even levels.
  6792. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  6793. file contains a section with an even level, conversion would
  6794. destroy the structure of the file. An error is signaled in this
  6795. case."
  6796. (interactive)
  6797. (goto-char (point-min))
  6798. ;; First check if there are no even levels
  6799. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  6800. (org-show-context t)
  6801. (error "Not all levels are odd in this file. Conversion not possible"))
  6802. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  6803. (let ((outline-regexp org-outline-regexp)
  6804. (outline-level 'org-outline-level)
  6805. (org-odd-levels-only nil) n)
  6806. (save-excursion
  6807. (goto-char (point-min))
  6808. (while (re-search-forward "^\\*\\*+ " nil t)
  6809. (setq n (/ (1- (length (match-string 0))) 2))
  6810. (while (>= (setq n (1- n)) 0)
  6811. (org-promote))
  6812. (end-of-line 1))))))
  6813. (defun org-tr-level (n)
  6814. "Make N odd if required."
  6815. (if org-odd-levels-only (1+ (/ n 2)) n))
  6816. ;;; Vertical tree motion, cutting and pasting of subtrees
  6817. (defun org-move-subtree-up (&optional arg)
  6818. "Move the current subtree up past ARG headlines of the same level."
  6819. (interactive "p")
  6820. (org-move-subtree-down (- (prefix-numeric-value arg))))
  6821. (defun org-move-subtree-down (&optional arg)
  6822. "Move the current subtree down past ARG headlines of the same level."
  6823. (interactive "p")
  6824. (setq arg (prefix-numeric-value arg))
  6825. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  6826. 'org-get-last-sibling))
  6827. (ins-point (make-marker))
  6828. (cnt (abs arg))
  6829. (col (current-column))
  6830. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  6831. ;; Select the tree
  6832. (org-back-to-heading)
  6833. (setq beg0 (point))
  6834. (save-excursion
  6835. (setq ne-beg (org-back-over-empty-lines))
  6836. (setq beg (point)))
  6837. (save-match-data
  6838. (save-excursion (outline-end-of-heading)
  6839. (setq folded (outline-invisible-p)))
  6840. (outline-end-of-subtree))
  6841. (outline-next-heading)
  6842. (setq ne-end (org-back-over-empty-lines))
  6843. (setq end (point))
  6844. (goto-char beg0)
  6845. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  6846. ;; include less whitespace
  6847. (save-excursion
  6848. (goto-char beg)
  6849. (forward-line (- ne-beg ne-end))
  6850. (setq beg (point))))
  6851. ;; Find insertion point, with error handling
  6852. (while (> cnt 0)
  6853. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  6854. (progn (goto-char beg0)
  6855. (error "Cannot move past superior level or buffer limit")))
  6856. (setq cnt (1- cnt)))
  6857. (if (> arg 0)
  6858. ;; Moving forward - still need to move over subtree
  6859. (progn (org-end-of-subtree t t)
  6860. (save-excursion
  6861. (org-back-over-empty-lines)
  6862. (or (bolp) (newline)))))
  6863. (setq ne-ins (org-back-over-empty-lines))
  6864. (move-marker ins-point (point))
  6865. (setq txt (buffer-substring beg end))
  6866. (org-save-markers-in-region beg end)
  6867. (delete-region beg end)
  6868. (org-remove-empty-overlays-at beg)
  6869. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  6870. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  6871. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  6872. (let ((bbb (point)))
  6873. (insert-before-markers txt)
  6874. (org-reinstall-markers-in-region bbb)
  6875. (move-marker ins-point bbb))
  6876. (or (bolp) (insert "\n"))
  6877. (setq ins-end (point))
  6878. (goto-char ins-point)
  6879. (org-skip-whitespace)
  6880. (when (and (< arg 0)
  6881. (org-first-sibling-p)
  6882. (> ne-ins ne-beg))
  6883. ;; Move whitespace back to beginning
  6884. (save-excursion
  6885. (goto-char ins-end)
  6886. (let ((kill-whole-line t))
  6887. (kill-line (- ne-ins ne-beg)) (point)))
  6888. (insert (make-string (- ne-ins ne-beg) ?\n)))
  6889. (move-marker ins-point nil)
  6890. (if folded
  6891. (hide-subtree)
  6892. (org-show-entry)
  6893. (show-children)
  6894. (org-cycle-hide-drawers 'children))
  6895. (org-clean-visibility-after-subtree-move)
  6896. ;; move back to the initial column we were at
  6897. (move-to-column col)))
  6898. (defvar org-subtree-clip ""
  6899. "Clipboard for cut and paste of subtrees.
  6900. This is actually only a copy of the kill, because we use the normal kill
  6901. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  6902. (defvar org-subtree-clip-folded nil
  6903. "Was the last copied subtree folded?
  6904. This is used to fold the tree back after pasting.")
  6905. (defun org-cut-subtree (&optional n)
  6906. "Cut the current subtree into the clipboard.
  6907. With prefix arg N, cut this many sequential subtrees.
  6908. This is a short-hand for marking the subtree and then cutting it."
  6909. (interactive "p")
  6910. (org-copy-subtree n 'cut))
  6911. (defun org-copy-subtree (&optional n cut force-store-markers)
  6912. "Cut the current subtree into the clipboard.
  6913. With prefix arg N, cut this many sequential subtrees.
  6914. This is a short-hand for marking the subtree and then copying it.
  6915. If CUT is non-nil, actually cut the subtree.
  6916. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  6917. of some markers in the region, even if CUT is non-nil. This is
  6918. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  6919. (interactive "p")
  6920. (let (beg end folded (beg0 (point)))
  6921. (if (org-called-interactively-p 'any)
  6922. (org-back-to-heading nil) ; take what looks like a subtree
  6923. (org-back-to-heading t)) ; take what is really there
  6924. (org-back-over-empty-lines)
  6925. (setq beg (point))
  6926. (skip-chars-forward " \t\r\n")
  6927. (save-match-data
  6928. (save-excursion (outline-end-of-heading)
  6929. (setq folded (outline-invisible-p)))
  6930. (condition-case nil
  6931. (org-forward-same-level (1- n) t)
  6932. (error nil))
  6933. (org-end-of-subtree t t))
  6934. (org-back-over-empty-lines)
  6935. (setq end (point))
  6936. (goto-char beg0)
  6937. (when (> end beg)
  6938. (setq org-subtree-clip-folded folded)
  6939. (when (or cut force-store-markers)
  6940. (org-save-markers-in-region beg end))
  6941. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  6942. (setq org-subtree-clip (current-kill 0))
  6943. (message "%s: Subtree(s) with %d characters"
  6944. (if cut "Cut" "Copied")
  6945. (length org-subtree-clip)))))
  6946. (defun org-paste-subtree (&optional level tree for-yank)
  6947. "Paste the clipboard as a subtree, with modification of headline level.
  6948. The entire subtree is promoted or demoted in order to match a new headline
  6949. level.
  6950. If the cursor is at the beginning of a headline, the same level as
  6951. that headline is used to paste the tree
  6952. If not, the new level is derived from the *visible* headings
  6953. before and after the insertion point, and taken to be the inferior headline
  6954. level of the two. So if the previous visible heading is level 3 and the
  6955. next is level 4 (or vice versa), level 4 will be used for insertion.
  6956. This makes sure that the subtree remains an independent subtree and does
  6957. not swallow low level entries.
  6958. You can also force a different level, either by using a numeric prefix
  6959. argument, or by inserting the heading marker by hand. For example, if the
  6960. cursor is after \"*****\", then the tree will be shifted to level 5.
  6961. If optional TREE is given, use this text instead of the kill ring.
  6962. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  6963. move back over whitespace before inserting, and move point to the end of
  6964. the inserted text when done."
  6965. (interactive "P")
  6966. (setq tree (or tree (and kill-ring (current-kill 0))))
  6967. (unless (org-kill-is-subtree-p tree)
  6968. (error "%s"
  6969. (substitute-command-keys
  6970. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  6971. (org-with-limited-levels
  6972. (let* ((visp (not (outline-invisible-p)))
  6973. (txt tree)
  6974. (^re_ "\\(\\*+\\)[ \t]*")
  6975. (old-level (if (string-match org-outline-regexp-bol txt)
  6976. (- (match-end 0) (match-beginning 0) 1)
  6977. -1))
  6978. (force-level (cond (level (prefix-numeric-value level))
  6979. ((and (looking-at "[ \t]*$")
  6980. (string-match
  6981. "^\\*+$" (buffer-substring
  6982. (point-at-bol) (point))))
  6983. (- (match-end 1) (match-beginning 1)))
  6984. ((and (bolp)
  6985. (looking-at org-outline-regexp))
  6986. (- (match-end 0) (point) 1))
  6987. (t nil)))
  6988. (previous-level (save-excursion
  6989. (condition-case nil
  6990. (progn
  6991. (outline-previous-visible-heading 1)
  6992. (if (looking-at ^re_)
  6993. (- (match-end 0) (match-beginning 0) 1)
  6994. 1))
  6995. (error 1))))
  6996. (next-level (save-excursion
  6997. (condition-case nil
  6998. (progn
  6999. (or (looking-at org-outline-regexp)
  7000. (outline-next-visible-heading 1))
  7001. (if (looking-at ^re_)
  7002. (- (match-end 0) (match-beginning 0) 1)
  7003. 1))
  7004. (error 1))))
  7005. (new-level (or force-level (max previous-level next-level)))
  7006. (shift (if (or (= old-level -1)
  7007. (= new-level -1)
  7008. (= old-level new-level))
  7009. 0
  7010. (- new-level old-level)))
  7011. (delta (if (> shift 0) -1 1))
  7012. (func (if (> shift 0) 'org-demote 'org-promote))
  7013. (org-odd-levels-only nil)
  7014. beg end newend)
  7015. ;; Remove the forced level indicator
  7016. (if force-level
  7017. (delete-region (point-at-bol) (point)))
  7018. ;; Paste
  7019. (beginning-of-line (if (bolp) 1 2))
  7020. (unless for-yank (org-back-over-empty-lines))
  7021. (setq beg (point))
  7022. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  7023. (insert-before-markers txt)
  7024. (unless (string-match "\n\\'" txt) (insert "\n"))
  7025. (setq newend (point))
  7026. (org-reinstall-markers-in-region beg)
  7027. (setq end (point))
  7028. (goto-char beg)
  7029. (skip-chars-forward " \t\n\r")
  7030. (setq beg (point))
  7031. (if (and (outline-invisible-p) visp)
  7032. (save-excursion (outline-show-heading)))
  7033. ;; Shift if necessary
  7034. (unless (= shift 0)
  7035. (save-restriction
  7036. (narrow-to-region beg end)
  7037. (while (not (= shift 0))
  7038. (org-map-region func (point-min) (point-max))
  7039. (setq shift (+ delta shift)))
  7040. (goto-char (point-min))
  7041. (setq newend (point-max))))
  7042. (when (or (org-called-interactively-p 'interactive) for-yank)
  7043. (message "Clipboard pasted as level %d subtree" new-level))
  7044. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  7045. kill-ring
  7046. (eq org-subtree-clip (current-kill 0))
  7047. org-subtree-clip-folded)
  7048. ;; The tree was folded before it was killed/copied
  7049. (hide-subtree))
  7050. (and for-yank (goto-char newend)))))
  7051. (defun org-kill-is-subtree-p (&optional txt)
  7052. "Check if the current kill is an outline subtree, or a set of trees.
  7053. Returns nil if kill does not start with a headline, or if the first
  7054. headline level is not the largest headline level in the tree.
  7055. So this will actually accept several entries of equal levels as well,
  7056. which is OK for `org-paste-subtree'.
  7057. If optional TXT is given, check this string instead of the current kill."
  7058. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  7059. (re (org-get-limited-outline-regexp))
  7060. (^re (concat "^" re))
  7061. (start-level (and kill
  7062. (string-match
  7063. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7064. kill)
  7065. (- (match-end 2) (match-beginning 2) 1)))
  7066. (start (1+ (or (match-beginning 2) -1))))
  7067. (if (not start-level)
  7068. (progn
  7069. nil) ;; does not even start with a heading
  7070. (catch 'exit
  7071. (while (setq start (string-match ^re kill (1+ start)))
  7072. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7073. (throw 'exit nil)))
  7074. t))))
  7075. (defvar org-markers-to-move nil
  7076. "Markers that should be moved with a cut-and-paste operation.
  7077. Those markers are stored together with their positions relative to
  7078. the start of the region.")
  7079. (defun org-save-markers-in-region (beg end)
  7080. "Check markers in region.
  7081. If these markers are between BEG and END, record their position relative
  7082. to BEG, so that after moving the block of text, we can put the markers back
  7083. into place.
  7084. This function gets called just before an entry or tree gets cut from the
  7085. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7086. called immediately, to move the markers with the entries."
  7087. (setq org-markers-to-move nil)
  7088. (when (featurep 'org-clock)
  7089. (org-clock-save-markers-for-cut-and-paste beg end))
  7090. (when (featurep 'org-agenda)
  7091. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7092. (defun org-check-and-save-marker (marker beg end)
  7093. "Check if MARKER is between BEG and END.
  7094. If yes, remember the marker and the distance to BEG."
  7095. (when (and (marker-buffer marker)
  7096. (equal (marker-buffer marker) (current-buffer)))
  7097. (if (and (>= marker beg) (< marker end))
  7098. (push (cons marker (- marker beg)) org-markers-to-move))))
  7099. (defun org-reinstall-markers-in-region (beg)
  7100. "Move all remembered markers to their position relative to BEG."
  7101. (mapc (lambda (x)
  7102. (move-marker (car x) (+ beg (cdr x))))
  7103. org-markers-to-move)
  7104. (setq org-markers-to-move nil))
  7105. (defun org-narrow-to-subtree ()
  7106. "Narrow buffer to the current subtree."
  7107. (interactive)
  7108. (save-excursion
  7109. (save-match-data
  7110. (org-with-limited-levels
  7111. (narrow-to-region
  7112. (progn (org-back-to-heading t) (point))
  7113. (progn (org-end-of-subtree t t)
  7114. (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7115. (point)))))))
  7116. (defun org-narrow-to-block ()
  7117. "Narrow buffer to the current block."
  7118. (interactive)
  7119. (let* ((case-fold-search t)
  7120. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7121. "^[ \t]*#\\+end_.*")))
  7122. (if blockp
  7123. (narrow-to-region (car blockp) (cdr blockp))
  7124. (error "Not in a block"))))
  7125. (eval-when-compile
  7126. (defvar org-property-drawer-re))
  7127. (defvar org-property-start-re) ;; defined below
  7128. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7129. "Clone the task (subtree) at point N times.
  7130. The clones will be inserted as siblings.
  7131. In interactive use, the user will be prompted for the number of
  7132. clones to be produced, and for a time SHIFT, which may be a
  7133. repeater as used in time stamps, for example `+3d'.
  7134. When a valid repeater is given and the entry contains any time
  7135. stamps, the clones will become a sequence in time, with time
  7136. stamps in the subtree shifted for each clone produced. If SHIFT
  7137. is nil or the empty string, time stamps will be left alone. The
  7138. ID property of the original subtree is removed.
  7139. If the original subtree did contain time stamps with a repeater,
  7140. the following will happen:
  7141. - the repeater will be removed in each clone
  7142. - an additional clone will be produced, with the current, unshifted
  7143. date(s) in the entry.
  7144. - the original entry will be placed *after* all the clones, with
  7145. repeater intact.
  7146. - the start days in the repeater in the original entry will be shifted
  7147. to past the last clone.
  7148. In this way you can spell out a number of instances of a repeating task,
  7149. and still retain the repeater to cover future instances of the task."
  7150. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  7151. (let (beg end template task idprop
  7152. shift-n shift-what doshift nmin nmax (n-no-remove -1)
  7153. (drawer-re org-drawer-regexp))
  7154. (if (not (and (integerp n) (> n 0)))
  7155. (error "Invalid number of replications %s" n))
  7156. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7157. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([dwmy]\\)[ \t]*\\'"
  7158. shift)))
  7159. (error "Invalid shift specification %s" shift))
  7160. (when doshift
  7161. (setq shift-n (string-to-number (match-string 1 shift))
  7162. shift-what (cdr (assoc (match-string 2 shift)
  7163. '(("d" . day) ("w" . week)
  7164. ("m" . month) ("y" . year))))))
  7165. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7166. (setq nmin 1 nmax n)
  7167. (org-back-to-heading t)
  7168. (setq beg (point))
  7169. (setq idprop (org-entry-get nil "ID"))
  7170. (org-end-of-subtree t t)
  7171. (or (bolp) (insert "\n"))
  7172. (setq end (point))
  7173. (setq template (buffer-substring beg end))
  7174. (when (and doshift
  7175. (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[dwmy][^<>\n]*>" template))
  7176. (delete-region beg end)
  7177. (setq end beg)
  7178. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7179. (goto-char end)
  7180. (loop for n from nmin to nmax do
  7181. ;; prepare clone
  7182. (with-temp-buffer
  7183. (insert template)
  7184. (org-mode)
  7185. (goto-char (point-min))
  7186. (org-show-subtree)
  7187. (and idprop (if org-clone-delete-id
  7188. (org-entry-delete nil "ID")
  7189. (org-id-get-create t)))
  7190. (unless (= n 0)
  7191. (while (re-search-forward "^[ \t]*CLOCK:.*$" nil t)
  7192. (kill-whole-line))
  7193. (goto-char (point-min))
  7194. (while (re-search-forward drawer-re nil t)
  7195. (mapc (lambda (d)
  7196. (org-remove-empty-drawer-at d (point))) org-drawers)))
  7197. (goto-char (point-min))
  7198. (when doshift
  7199. (while (re-search-forward org-ts-regexp-both nil t)
  7200. (org-timestamp-change (* n shift-n) shift-what))
  7201. (unless (= n n-no-remove)
  7202. (goto-char (point-min))
  7203. (while (re-search-forward org-ts-regexp nil t)
  7204. (save-excursion
  7205. (goto-char (match-beginning 0))
  7206. (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[dwmy]\\)")
  7207. (delete-region (match-beginning 1) (match-end 1)))))))
  7208. (setq task (buffer-string)))
  7209. (insert task))
  7210. (goto-char beg)))
  7211. ;;; Outline Sorting
  7212. (defun org-sort (with-case)
  7213. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7214. Optional argument WITH-CASE means sort case-sensitively."
  7215. (interactive "P")
  7216. (cond
  7217. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7218. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7219. (t
  7220. (org-call-with-arg 'org-sort-entries with-case))))
  7221. (defun org-sort-remove-invisible (s)
  7222. (remove-text-properties 0 (length s) org-rm-props s)
  7223. (while (string-match org-bracket-link-regexp s)
  7224. (setq s (replace-match (if (match-end 2)
  7225. (match-string 3 s)
  7226. (match-string 1 s)) t t s)))
  7227. s)
  7228. (defvar org-priority-regexp) ; defined later in the file
  7229. (defvar org-after-sorting-entries-or-items-hook nil
  7230. "Hook that is run after a bunch of entries or items have been sorted.
  7231. When children are sorted, the cursor is in the parent line when this
  7232. hook gets called. When a region or a plain list is sorted, the cursor
  7233. will be in the first entry of the sorted region/list.")
  7234. (defun org-sort-entries
  7235. (&optional with-case sorting-type getkey-func compare-func property)
  7236. "Sort entries on a certain level of an outline tree.
  7237. If there is an active region, the entries in the region are sorted.
  7238. Else, if the cursor is before the first entry, sort the top-level items.
  7239. Else, the children of the entry at point are sorted.
  7240. Sorting can be alphabetically, numerically, by date/time as given by
  7241. a time stamp, by a property or by priority.
  7242. The command prompts for the sorting type unless it has been given to the
  7243. function through the SORTING-TYPE argument, which needs to be a character,
  7244. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?r ?R ?f ?F). Here is the
  7245. precise meaning of each character:
  7246. n Numerically, by converting the beginning of the entry/item to a number.
  7247. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7248. t By date/time, either the first active time stamp in the entry, or, if
  7249. none exist, by the first inactive one.
  7250. s By the scheduled date/time.
  7251. d By deadline date/time.
  7252. c By creation time, which is assumed to be the first inactive time stamp
  7253. at the beginning of a line.
  7254. p By priority according to the cookie.
  7255. r By the value of a property.
  7256. Capital letters will reverse the sort order.
  7257. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7258. called with point at the beginning of the record. It must return either
  7259. a string or a number that should serve as the sorting key for that record.
  7260. Comparing entries ignores case by default. However, with an optional argument
  7261. WITH-CASE, the sorting considers case as well."
  7262. (interactive "P")
  7263. (let ((case-func (if with-case 'identity 'downcase))
  7264. start beg end stars re re2
  7265. txt what tmp)
  7266. ;; Find beginning and end of region to sort
  7267. (cond
  7268. ((org-region-active-p)
  7269. ;; we will sort the region
  7270. (setq end (region-end)
  7271. what "region")
  7272. (goto-char (region-beginning))
  7273. (if (not (org-at-heading-p)) (outline-next-heading))
  7274. (setq start (point)))
  7275. ((or (org-at-heading-p)
  7276. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  7277. ;; we will sort the children of the current headline
  7278. (org-back-to-heading)
  7279. (setq start (point)
  7280. end (progn (org-end-of-subtree t t)
  7281. (or (bolp) (insert "\n"))
  7282. (org-back-over-empty-lines)
  7283. (point))
  7284. what "children")
  7285. (goto-char start)
  7286. (show-subtree)
  7287. (outline-next-heading))
  7288. (t
  7289. ;; we will sort the top-level entries in this file
  7290. (goto-char (point-min))
  7291. (or (org-at-heading-p) (outline-next-heading))
  7292. (setq start (point))
  7293. (goto-char (point-max))
  7294. (beginning-of-line 1)
  7295. (when (looking-at ".*?\\S-")
  7296. ;; File ends in a non-white line
  7297. (end-of-line 1)
  7298. (insert "\n"))
  7299. (setq end (point-max))
  7300. (setq what "top-level")
  7301. (goto-char start)
  7302. (show-all)))
  7303. (setq beg (point))
  7304. (if (>= beg end) (error "Nothing to sort"))
  7305. (looking-at "\\(\\*+\\)")
  7306. (setq stars (match-string 1)
  7307. re (concat "^" (regexp-quote stars) " +")
  7308. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  7309. txt (buffer-substring beg end))
  7310. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  7311. (if (and (not (equal stars "*")) (string-match re2 txt))
  7312. (error "Region to sort contains a level above the first entry"))
  7313. (unless sorting-type
  7314. (message
  7315. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  7316. [t]ime [s]cheduled [d]eadline [c]reated
  7317. A/N/T/S/D/C/P/O/F means reversed:"
  7318. what)
  7319. (setq sorting-type (read-char-exclusive))
  7320. (and (= (downcase sorting-type) ?f)
  7321. (setq getkey-func
  7322. (org-icompleting-read "Sort using function: "
  7323. obarray 'fboundp t nil nil))
  7324. (setq getkey-func (intern getkey-func)))
  7325. (and (= (downcase sorting-type) ?r)
  7326. (setq property
  7327. (org-icompleting-read "Property: "
  7328. (mapcar 'list (org-buffer-property-keys t))
  7329. nil t))))
  7330. (message "Sorting entries...")
  7331. (save-restriction
  7332. (narrow-to-region start end)
  7333. (let ((dcst (downcase sorting-type))
  7334. (case-fold-search nil)
  7335. (now (current-time)))
  7336. (sort-subr
  7337. (/= dcst sorting-type)
  7338. ;; This function moves to the beginning character of the "record" to
  7339. ;; be sorted.
  7340. (lambda nil
  7341. (if (re-search-forward re nil t)
  7342. (goto-char (match-beginning 0))
  7343. (goto-char (point-max))))
  7344. ;; This function moves to the last character of the "record" being
  7345. ;; sorted.
  7346. (lambda nil
  7347. (save-match-data
  7348. (condition-case nil
  7349. (outline-forward-same-level 1)
  7350. (error
  7351. (goto-char (point-max))))))
  7352. ;; This function returns the value that gets sorted against.
  7353. (lambda nil
  7354. (cond
  7355. ((= dcst ?n)
  7356. (if (looking-at org-complex-heading-regexp)
  7357. (string-to-number (match-string 4))
  7358. nil))
  7359. ((= dcst ?a)
  7360. (if (looking-at org-complex-heading-regexp)
  7361. (funcall case-func (match-string 4))
  7362. nil))
  7363. ((= dcst ?t)
  7364. (let ((end (save-excursion (outline-next-heading) (point))))
  7365. (if (or (re-search-forward org-ts-regexp end t)
  7366. (re-search-forward org-ts-regexp-both end t))
  7367. (org-time-string-to-seconds (match-string 0))
  7368. (org-float-time now))))
  7369. ((= dcst ?c)
  7370. (let ((end (save-excursion (outline-next-heading) (point))))
  7371. (if (re-search-forward
  7372. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  7373. end t)
  7374. (org-time-string-to-seconds (match-string 0))
  7375. (org-float-time now))))
  7376. ((= dcst ?s)
  7377. (let ((end (save-excursion (outline-next-heading) (point))))
  7378. (if (re-search-forward org-scheduled-time-regexp end t)
  7379. (org-time-string-to-seconds (match-string 1))
  7380. (org-float-time now))))
  7381. ((= dcst ?d)
  7382. (let ((end (save-excursion (outline-next-heading) (point))))
  7383. (if (re-search-forward org-deadline-time-regexp end t)
  7384. (org-time-string-to-seconds (match-string 1))
  7385. (org-float-time now))))
  7386. ((= dcst ?p)
  7387. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  7388. (string-to-char (match-string 2))
  7389. org-default-priority))
  7390. ((= dcst ?r)
  7391. (or (org-entry-get nil property) ""))
  7392. ((= dcst ?o)
  7393. (if (looking-at org-complex-heading-regexp)
  7394. (- 9999 (length (member (match-string 2)
  7395. org-todo-keywords-1)))))
  7396. ((= dcst ?f)
  7397. (if getkey-func
  7398. (progn
  7399. (setq tmp (funcall getkey-func))
  7400. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  7401. tmp)
  7402. (error "Invalid key function `%s'" getkey-func)))
  7403. (t (error "Invalid sorting type `%c'" sorting-type))))
  7404. nil
  7405. (cond
  7406. ((= dcst ?a) 'string<)
  7407. ((= dcst ?f) compare-func)
  7408. ((member dcst '(?p ?t ?s ?d ?c)) '<)
  7409. (t nil)))))
  7410. (run-hooks 'org-after-sorting-entries-or-items-hook)
  7411. (message "Sorting entries...done")))
  7412. (defun org-do-sort (table what &optional with-case sorting-type)
  7413. "Sort TABLE of WHAT according to SORTING-TYPE.
  7414. The user will be prompted for the SORTING-TYPE if the call to this
  7415. function does not specify it. WHAT is only for the prompt, to indicate
  7416. what is being sorted. The sorting key will be extracted from
  7417. the car of the elements of the table.
  7418. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  7419. (unless sorting-type
  7420. (message
  7421. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  7422. what)
  7423. (setq sorting-type (read-char-exclusive)))
  7424. (let ((dcst (downcase sorting-type))
  7425. extractfun comparefun)
  7426. ;; Define the appropriate functions
  7427. (cond
  7428. ((= dcst ?n)
  7429. (setq extractfun 'string-to-number
  7430. comparefun (if (= dcst sorting-type) '< '>)))
  7431. ((= dcst ?a)
  7432. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  7433. (lambda(x) (downcase (org-sort-remove-invisible x))))
  7434. comparefun (if (= dcst sorting-type)
  7435. 'string<
  7436. (lambda (a b) (and (not (string< a b))
  7437. (not (string= a b)))))))
  7438. ((= dcst ?t)
  7439. (setq extractfun
  7440. (lambda (x)
  7441. (if (or (string-match org-ts-regexp x)
  7442. (string-match org-ts-regexp-both x))
  7443. (org-float-time
  7444. (org-time-string-to-time (match-string 0 x)))
  7445. 0))
  7446. comparefun (if (= dcst sorting-type) '< '>)))
  7447. (t (error "Invalid sorting type `%c'" sorting-type)))
  7448. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  7449. table)
  7450. (lambda (a b) (funcall comparefun (car a) (car b))))))
  7451. ;;; The orgstruct minor mode
  7452. ;; Define a minor mode which can be used in other modes in order to
  7453. ;; integrate the org-mode structure editing commands.
  7454. ;; This is really a hack, because the org-mode structure commands use
  7455. ;; keys which normally belong to the major mode. Here is how it
  7456. ;; works: The minor mode defines all the keys necessary to operate the
  7457. ;; structure commands, but wraps the commands into a function which
  7458. ;; tests if the cursor is currently at a headline or a plain list
  7459. ;; item. If that is the case, the structure command is used,
  7460. ;; temporarily setting many Org-mode variables like regular
  7461. ;; expressions for filling etc. However, when any of those keys is
  7462. ;; used at a different location, function uses `key-binding' to look
  7463. ;; up if the key has an associated command in another currently active
  7464. ;; keymap (minor modes, major mode, global), and executes that
  7465. ;; command. There might be problems if any of the keys is otherwise
  7466. ;; used as a prefix key.
  7467. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  7468. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  7469. ;; addresses this by checking explicitly for both bindings.
  7470. (defvar orgstruct-mode-map (make-sparse-keymap)
  7471. "Keymap for the minor `orgstruct-mode'.")
  7472. (defvar org-local-vars nil
  7473. "List of local variables, for use by `orgstruct-mode'.")
  7474. ;;;###autoload
  7475. (define-minor-mode orgstruct-mode
  7476. "Toggle the minor mode `orgstruct-mode'.
  7477. This mode is for using Org-mode structure commands in other
  7478. modes. The following keys behave as if Org-mode were active, if
  7479. the cursor is on a headline, or on a plain list item (both as
  7480. defined by Org-mode).
  7481. M-up Move entry/item up
  7482. M-down Move entry/item down
  7483. M-left Promote
  7484. M-right Demote
  7485. M-S-up Move entry/item up
  7486. M-S-down Move entry/item down
  7487. M-S-left Promote subtree
  7488. M-S-right Demote subtree
  7489. M-q Fill paragraph and items like in Org-mode
  7490. C-c ^ Sort entries
  7491. C-c - Cycle list bullet
  7492. TAB Cycle item visibility
  7493. M-RET Insert new heading/item
  7494. S-M-RET Insert new TODO heading / Checkbox item
  7495. C-c C-c Set tags / toggle checkbox"
  7496. nil " OrgStruct" nil
  7497. (org-load-modules-maybe)
  7498. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  7499. ;;;###autoload
  7500. (defun turn-on-orgstruct ()
  7501. "Unconditionally turn on `orgstruct-mode'."
  7502. (orgstruct-mode 1))
  7503. (defun orgstruct++-mode (&optional arg)
  7504. "Toggle `orgstruct-mode', the enhanced version of it.
  7505. In addition to setting orgstruct-mode, this also exports all indentation
  7506. and autofilling variables from org-mode into the buffer. It will also
  7507. recognize item context in multiline items.
  7508. Note that turning off orgstruct-mode will *not* remove the
  7509. indentation/paragraph settings. This can only be done by refreshing the
  7510. major mode, for example with \\[normal-mode]."
  7511. (interactive "P")
  7512. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  7513. (if (< arg 1)
  7514. (orgstruct-mode -1)
  7515. (orgstruct-mode 1)
  7516. (let (var val)
  7517. (mapc
  7518. (lambda (x)
  7519. (when (string-match
  7520. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  7521. (symbol-name (car x)))
  7522. (setq var (car x) val (nth 1 x))
  7523. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  7524. org-local-vars)
  7525. (org-set-local 'orgstruct-is-++ t))))
  7526. (defvar orgstruct-is-++ nil
  7527. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  7528. (make-variable-buffer-local 'orgstruct-is-++)
  7529. ;;;###autoload
  7530. (defun turn-on-orgstruct++ ()
  7531. "Unconditionally turn on `orgstruct++-mode'."
  7532. (orgstruct++-mode 1))
  7533. (defun orgstruct-error ()
  7534. "Error when there is no default binding for a structure key."
  7535. (interactive)
  7536. (error "This key has no function outside structure elements"))
  7537. (defun orgstruct-setup ()
  7538. "Setup orgstruct keymaps."
  7539. (let ((nfunc 0)
  7540. (bindings
  7541. (list
  7542. '([(meta up)] org-metaup)
  7543. '([(meta down)] org-metadown)
  7544. '([(meta left)] org-metaleft)
  7545. '([(meta right)] org-metaright)
  7546. '([(meta shift up)] org-shiftmetaup)
  7547. '([(meta shift down)] org-shiftmetadown)
  7548. '([(meta shift left)] org-shiftmetaleft)
  7549. '([(meta shift right)] org-shiftmetaright)
  7550. '([?\e (up)] org-metaup)
  7551. '([?\e (down)] org-metadown)
  7552. '([?\e (left)] org-metaleft)
  7553. '([?\e (right)] org-metaright)
  7554. '([?\e (shift up)] org-shiftmetaup)
  7555. '([?\e (shift down)] org-shiftmetadown)
  7556. '([?\e (shift left)] org-shiftmetaleft)
  7557. '([?\e (shift right)] org-shiftmetaright)
  7558. '([(shift up)] org-shiftup)
  7559. '([(shift down)] org-shiftdown)
  7560. '([(shift left)] org-shiftleft)
  7561. '([(shift right)] org-shiftright)
  7562. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  7563. '("\M-q" fill-paragraph)
  7564. '("\C-c^" org-sort)
  7565. '("\C-c-" org-cycle-list-bullet)))
  7566. elt key fun cmd)
  7567. (while (setq elt (pop bindings))
  7568. (setq nfunc (1+ nfunc))
  7569. (setq key (org-key (car elt))
  7570. fun (nth 1 elt)
  7571. cmd (orgstruct-make-binding fun nfunc key))
  7572. (org-defkey orgstruct-mode-map key cmd))
  7573. ;; Special treatment needed for TAB and RET
  7574. (org-defkey orgstruct-mode-map [(tab)]
  7575. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  7576. (org-defkey orgstruct-mode-map "\C-i"
  7577. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  7578. (org-defkey orgstruct-mode-map "\M-\C-m"
  7579. (orgstruct-make-binding 'org-insert-heading 105
  7580. "\M-\C-m" [(meta return)]))
  7581. (org-defkey orgstruct-mode-map [(meta return)]
  7582. (orgstruct-make-binding 'org-insert-heading 106
  7583. [(meta return)] "\M-\C-m"))
  7584. (org-defkey orgstruct-mode-map [(shift meta return)]
  7585. (orgstruct-make-binding 'org-insert-todo-heading 107
  7586. [(meta return)] "\M-\C-m"))
  7587. (org-defkey orgstruct-mode-map "\e\C-m"
  7588. (orgstruct-make-binding 'org-insert-heading 108
  7589. "\e\C-m" [?\e (return)]))
  7590. (org-defkey orgstruct-mode-map [?\e (return)]
  7591. (orgstruct-make-binding 'org-insert-heading 109
  7592. [?\e (return)] "\e\C-m"))
  7593. (org-defkey orgstruct-mode-map [?\e (shift return)]
  7594. (orgstruct-make-binding 'org-insert-todo-heading 110
  7595. [?\e (return)] "\e\C-m"))
  7596. (unless org-local-vars
  7597. (setq org-local-vars (org-get-local-variables)))
  7598. t))
  7599. (defun orgstruct-make-binding (fun n &rest keys)
  7600. "Create a function for binding in the structure minor mode.
  7601. FUN is the command to call inside a table. N is used to create a unique
  7602. command name. KEYS are keys that should be checked in for a command
  7603. to execute outside of tables."
  7604. (eval
  7605. (list 'defun
  7606. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  7607. '(arg)
  7608. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  7609. "Outside of structure, run the binding of `"
  7610. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  7611. "'.")
  7612. '(interactive "p")
  7613. (list 'if
  7614. `(org-context-p 'headline 'item
  7615. (and orgstruct-is-++
  7616. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  7617. 'item-body))
  7618. (list 'org-run-like-in-org-mode (list 'quote fun))
  7619. (list 'let '(orgstruct-mode)
  7620. (list 'call-interactively
  7621. (append '(or)
  7622. (mapcar (lambda (k)
  7623. (list 'key-binding k))
  7624. keys)
  7625. '('orgstruct-error))))))))
  7626. (defun org-context-p (&rest contexts)
  7627. "Check if local context is any of CONTEXTS.
  7628. Possible values in the list of contexts are `table', `headline', and `item'."
  7629. (let ((pos (point)))
  7630. (goto-char (point-at-bol))
  7631. (prog1 (or (and (memq 'table contexts)
  7632. (looking-at "[ \t]*|"))
  7633. (and (memq 'headline contexts)
  7634. (looking-at org-outline-regexp))
  7635. (and (memq 'item contexts)
  7636. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  7637. (and (memq 'item-body contexts)
  7638. (org-in-item-p)))
  7639. (goto-char pos))))
  7640. (defun org-get-local-variables ()
  7641. "Return a list of all local variables in an org-mode buffer."
  7642. (let (varlist)
  7643. (with-current-buffer (get-buffer-create "*Org tmp*")
  7644. (erase-buffer)
  7645. (org-mode)
  7646. (setq varlist (buffer-local-variables)))
  7647. (kill-buffer "*Org tmp*")
  7648. (delq nil
  7649. (mapcar
  7650. (lambda (x)
  7651. (setq x
  7652. (if (symbolp x)
  7653. (list x)
  7654. (list (car x) (list 'quote (cdr x)))))
  7655. (if (string-match
  7656. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  7657. (symbol-name (car x)))
  7658. x nil))
  7659. varlist))))
  7660. (defun org-clone-local-variables (from-buffer &optional regexp)
  7661. "Clone local variables from FROM-BUFFER.
  7662. Optional argument REGEXP selects variables to clone."
  7663. (mapc
  7664. (lambda (pair)
  7665. (and (symbolp (car pair))
  7666. (or (null regexp)
  7667. (string-match regexp (symbol-name (car pair))))
  7668. (set (make-local-variable (car pair))
  7669. (cdr pair))))
  7670. (buffer-local-variables from-buffer)))
  7671. ;;;###autoload
  7672. (defun org-run-like-in-org-mode (cmd)
  7673. "Run a command, pretending that the current buffer is in Org-mode.
  7674. This will temporarily bind local variables that are typically bound in
  7675. Org-mode to the values they have in Org-mode, and then interactively
  7676. call CMD."
  7677. (org-load-modules-maybe)
  7678. (unless org-local-vars
  7679. (setq org-local-vars (org-get-local-variables)))
  7680. (eval (list 'let org-local-vars
  7681. (list 'call-interactively (list 'quote cmd)))))
  7682. ;;;; Archiving
  7683. (defun org-get-category (&optional pos force-refresh)
  7684. "Get the category applying to position POS."
  7685. (save-match-data
  7686. (if force-refresh (org-refresh-category-properties))
  7687. (let ((pos (or pos (point))))
  7688. (or (get-text-property pos 'org-category)
  7689. (progn (org-refresh-category-properties)
  7690. (get-text-property pos 'org-category))))))
  7691. (defun org-refresh-category-properties ()
  7692. "Refresh category text properties in the buffer."
  7693. (let ((def-cat (cond
  7694. ((null org-category)
  7695. (if buffer-file-name
  7696. (file-name-sans-extension
  7697. (file-name-nondirectory buffer-file-name))
  7698. "???"))
  7699. ((symbolp org-category) (symbol-name org-category))
  7700. (t org-category)))
  7701. beg end cat pos optionp)
  7702. (org-unmodified
  7703. (save-excursion
  7704. (save-restriction
  7705. (widen)
  7706. (goto-char (point-min))
  7707. (put-text-property (point) (point-max) 'org-category def-cat)
  7708. (while (re-search-forward
  7709. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  7710. (setq pos (match-end 0)
  7711. optionp (equal (char-after (match-beginning 0)) ?#)
  7712. cat (org-trim (match-string 2)))
  7713. (if optionp
  7714. (setq beg (point-at-bol) end (point-max))
  7715. (org-back-to-heading t)
  7716. (setq beg (point) end (org-end-of-subtree t t)))
  7717. (put-text-property beg end 'org-category cat)
  7718. (put-text-property beg end 'org-category-position beg)
  7719. (goto-char pos)))))))
  7720. ;;;; Link Stuff
  7721. ;;; Link abbreviations
  7722. (defun org-link-expand-abbrev (link)
  7723. "Apply replacements as defined in `org-link-abbrev-alist."
  7724. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  7725. (let* ((key (match-string 1 link))
  7726. (as (or (assoc key org-link-abbrev-alist-local)
  7727. (assoc key org-link-abbrev-alist)))
  7728. (tag (and (match-end 2) (match-string 3 link)))
  7729. rpl)
  7730. (if (not as)
  7731. link
  7732. (setq rpl (cdr as))
  7733. (cond
  7734. ((symbolp rpl) (funcall rpl tag))
  7735. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  7736. ((string-match "%h" rpl)
  7737. (replace-match (url-hexify-string (or tag "")) t t rpl))
  7738. (t (concat rpl tag)))))
  7739. link))
  7740. ;;; Storing and inserting links
  7741. (defvar org-insert-link-history nil
  7742. "Minibuffer history for links inserted with `org-insert-link'.")
  7743. (defvar org-stored-links nil
  7744. "Contains the links stored with `org-store-link'.")
  7745. (defvar org-store-link-plist nil
  7746. "Plist with info about the most recently link created with `org-store-link'.")
  7747. (defvar org-link-protocols nil
  7748. "Link protocols added to Org-mode using `org-add-link-type'.")
  7749. (defvar org-store-link-functions nil
  7750. "List of functions that are called to create and store a link.
  7751. Each function will be called in turn until one returns a non-nil
  7752. value. Each function should check if it is responsible for creating
  7753. this link (for example by looking at the major mode).
  7754. If not, it must exit and return nil.
  7755. If yes, it should return a non-nil value after a calling
  7756. `org-store-link-props' with a list of properties and values.
  7757. Special properties are:
  7758. :type The link prefix, like \"http\". This must be given.
  7759. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  7760. This is obligatory as well.
  7761. :description Optional default description for the second pair
  7762. of brackets in an Org-mode link. The user can still change
  7763. this when inserting this link into an Org-mode buffer.
  7764. In addition to these, any additional properties can be specified
  7765. and then used in remember templates.")
  7766. (defun org-add-link-type (type &optional follow export)
  7767. "Add TYPE to the list of `org-link-types'.
  7768. Re-compute all regular expressions depending on `org-link-types'
  7769. FOLLOW and EXPORT are two functions.
  7770. FOLLOW should take the link path as the single argument and do whatever
  7771. is necessary to follow the link, for example find a file or display
  7772. a mail message.
  7773. EXPORT should format the link path for export to one of the export formats.
  7774. It should be a function accepting three arguments:
  7775. path the path of the link, the text after the prefix (like \"http:\")
  7776. desc the description of the link, if any, or a description added by
  7777. org-export-normalize-links if there is none
  7778. format the export format, a symbol like `html' or `latex' or `ascii'..
  7779. The function may use the FORMAT information to return different values
  7780. depending on the format. The return value will be put literally into
  7781. the exported file. If the return value is nil, this means Org should
  7782. do what it normally does with links which do not have EXPORT defined.
  7783. Org-mode has a built-in default for exporting links. If you are happy with
  7784. this default, there is no need to define an export function for the link
  7785. type. For a simple example of an export function, see `org-bbdb.el'."
  7786. (add-to-list 'org-link-types type t)
  7787. (org-make-link-regexps)
  7788. (if (assoc type org-link-protocols)
  7789. (setcdr (assoc type org-link-protocols) (list follow export))
  7790. (push (list type follow export) org-link-protocols)))
  7791. (defvar org-agenda-buffer-name)
  7792. ;;;###autoload
  7793. (defun org-store-link (arg)
  7794. "\\<org-mode-map>Store an org-link to the current location.
  7795. This link is added to `org-stored-links' and can later be inserted
  7796. into an org-buffer with \\[org-insert-link].
  7797. For some link types, a prefix arg is interpreted:
  7798. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  7799. For file links, arg negates `org-context-in-file-links'."
  7800. (interactive "P")
  7801. (org-load-modules-maybe)
  7802. (setq org-store-link-plist nil) ; reset
  7803. (org-with-limited-levels
  7804. (let (link cpltxt desc description search txt custom-id agenda-link)
  7805. (cond
  7806. ((run-hook-with-args-until-success 'org-store-link-functions)
  7807. (setq link (plist-get org-store-link-plist :link)
  7808. desc (or (plist-get org-store-link-plist :description) link)))
  7809. ((org-src-edit-buffer-p)
  7810. (let (label gc)
  7811. (while (or (not label)
  7812. (save-excursion
  7813. (save-restriction
  7814. (widen)
  7815. (goto-char (point-min))
  7816. (re-search-forward
  7817. (regexp-quote (format org-coderef-label-format label))
  7818. nil t))))
  7819. (when label (message "Label exists already") (sit-for 2))
  7820. (setq label (read-string "Code line label: " label)))
  7821. (end-of-line 1)
  7822. (setq link (format org-coderef-label-format label))
  7823. (setq gc (- 79 (length link)))
  7824. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  7825. (insert link)
  7826. (setq link (concat "(" label ")") desc nil)))
  7827. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  7828. ;; We are in the agenda, link to referenced location
  7829. (let ((m (or (get-text-property (point) 'org-hd-marker)
  7830. (get-text-property (point) 'org-marker))))
  7831. (when m
  7832. (org-with-point-at m
  7833. (setq agenda-link
  7834. (if (org-called-interactively-p 'any)
  7835. (call-interactively 'org-store-link)
  7836. (org-store-link nil)))))))
  7837. ((eq major-mode 'calendar-mode)
  7838. (let ((cd (calendar-cursor-to-date)))
  7839. (setq link
  7840. (format-time-string
  7841. (car org-time-stamp-formats)
  7842. (apply 'encode-time
  7843. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  7844. nil nil nil))))
  7845. (org-store-link-props :type "calendar" :date cd)))
  7846. ((eq major-mode 'w3-mode)
  7847. (setq cpltxt (if (and (buffer-name)
  7848. (not (string-match "Untitled" (buffer-name))))
  7849. (buffer-name)
  7850. (url-view-url t))
  7851. link (org-make-link (url-view-url t)))
  7852. (org-store-link-props :type "w3" :url (url-view-url t)))
  7853. ((eq major-mode 'w3m-mode)
  7854. (setq cpltxt (or w3m-current-title w3m-current-url)
  7855. link (org-make-link w3m-current-url))
  7856. (org-store-link-props :type "w3m" :url (url-view-url t)))
  7857. ((setq search (run-hook-with-args-until-success
  7858. 'org-create-file-search-functions))
  7859. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  7860. "::" search))
  7861. (setq cpltxt (or description link)))
  7862. ((eq major-mode 'image-mode)
  7863. (setq cpltxt (concat "file:"
  7864. (abbreviate-file-name buffer-file-name))
  7865. link (org-make-link cpltxt))
  7866. (org-store-link-props :type "image" :file buffer-file-name))
  7867. ((eq major-mode 'dired-mode)
  7868. ;; link to the file in the current line
  7869. (let ((file (dired-get-filename nil t)))
  7870. (setq file (if file
  7871. (abbreviate-file-name
  7872. (expand-file-name (dired-get-filename nil t)))
  7873. ;; otherwise, no file so use current directory.
  7874. default-directory))
  7875. (setq cpltxt (concat "file:" file)
  7876. link (org-make-link cpltxt))))
  7877. ((and (buffer-file-name (buffer-base-buffer)) (eq major-mode 'org-mode))
  7878. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  7879. (cond
  7880. ((org-in-regexp "<<\\(.*?\\)>>")
  7881. (setq cpltxt
  7882. (concat "file:"
  7883. (abbreviate-file-name
  7884. (buffer-file-name (buffer-base-buffer)))
  7885. "::" (match-string 1))
  7886. link (org-make-link cpltxt)))
  7887. ((and (featurep 'org-id)
  7888. (or (eq org-link-to-org-use-id t)
  7889. (and (eq org-link-to-org-use-id 'create-if-interactive)
  7890. (org-called-interactively-p 'any))
  7891. (and (eq org-link-to-org-use-id
  7892. 'create-if-interactive-and-no-custom-id)
  7893. (org-called-interactively-p 'any)
  7894. (not custom-id))
  7895. (and org-link-to-org-use-id
  7896. (org-entry-get nil "ID"))))
  7897. ;; We can make a link using the ID.
  7898. (setq link (condition-case nil
  7899. (prog1 (org-id-store-link)
  7900. (setq desc (plist-get org-store-link-plist
  7901. :description)))
  7902. (error
  7903. ;; probably before first headline, link to file only
  7904. (concat "file:"
  7905. (abbreviate-file-name
  7906. (buffer-file-name (buffer-base-buffer))))))))
  7907. (t
  7908. ;; Just link to current headline
  7909. (setq cpltxt (concat "file:"
  7910. (abbreviate-file-name
  7911. (buffer-file-name (buffer-base-buffer)))))
  7912. ;; Add a context search string
  7913. (when (org-xor org-context-in-file-links arg)
  7914. (setq txt (cond
  7915. ((org-at-heading-p) nil)
  7916. ((org-region-active-p)
  7917. (buffer-substring (region-beginning) (region-end)))
  7918. (t nil)))
  7919. (when (or (null txt) (string-match "\\S-" txt))
  7920. (setq cpltxt
  7921. (concat cpltxt "::"
  7922. (condition-case nil
  7923. (org-make-org-heading-search-string txt)
  7924. (error "")))
  7925. desc (or (nth 4 (ignore-errors
  7926. (org-heading-components))) "NONE"))))
  7927. (if (string-match "::\\'" cpltxt)
  7928. (setq cpltxt (substring cpltxt 0 -2)))
  7929. (setq link (org-make-link cpltxt)))))
  7930. ((buffer-file-name (buffer-base-buffer))
  7931. ;; Just link to this file here.
  7932. (setq cpltxt (concat "file:"
  7933. (abbreviate-file-name
  7934. (buffer-file-name (buffer-base-buffer)))))
  7935. ;; Add a context string
  7936. (when (org-xor org-context-in-file-links arg)
  7937. (setq txt (if (org-region-active-p)
  7938. (buffer-substring (region-beginning) (region-end))
  7939. (buffer-substring (point-at-bol) (point-at-eol))))
  7940. ;; Only use search option if there is some text.
  7941. (when (string-match "\\S-" txt)
  7942. (setq cpltxt
  7943. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  7944. desc "NONE")))
  7945. (setq link (org-make-link cpltxt)))
  7946. ((org-called-interactively-p 'interactive)
  7947. (error "Cannot link to a buffer which is not visiting a file"))
  7948. (t (setq link nil)))
  7949. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  7950. (setq link (or link cpltxt)
  7951. desc (or desc cpltxt))
  7952. (if (equal desc "NONE") (setq desc nil))
  7953. (if (and (or (org-called-interactively-p 'any) executing-kbd-macro) link)
  7954. (progn
  7955. (setq org-stored-links
  7956. (cons (list link desc) org-stored-links))
  7957. (message "Stored: %s" (or desc link))
  7958. (when custom-id
  7959. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  7960. "::#" custom-id))
  7961. (setq org-stored-links
  7962. (cons (list link desc) org-stored-links))))
  7963. (or agenda-link (and link (org-make-link-string link desc)))))))
  7964. (defun org-store-link-props (&rest plist)
  7965. "Store link properties, extract names and addresses."
  7966. (let (x adr)
  7967. (when (setq x (plist-get plist :from))
  7968. (setq adr (mail-extract-address-components x))
  7969. (setq plist (plist-put plist :fromname (car adr)))
  7970. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  7971. (when (setq x (plist-get plist :to))
  7972. (setq adr (mail-extract-address-components x))
  7973. (setq plist (plist-put plist :toname (car adr)))
  7974. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  7975. (let ((from (plist-get plist :from))
  7976. (to (plist-get plist :to)))
  7977. (when (and from to org-from-is-user-regexp)
  7978. (setq plist
  7979. (plist-put plist :fromto
  7980. (if (string-match org-from-is-user-regexp from)
  7981. (concat "to %t")
  7982. (concat "from %f"))))))
  7983. (setq org-store-link-plist plist))
  7984. (defun org-add-link-props (&rest plist)
  7985. "Add these properties to the link property list."
  7986. (let (key value)
  7987. (while plist
  7988. (setq key (pop plist) value (pop plist))
  7989. (setq org-store-link-plist
  7990. (plist-put org-store-link-plist key value)))))
  7991. (defun org-email-link-description (&optional fmt)
  7992. "Return the description part of an email link.
  7993. This takes information from `org-store-link-plist' and formats it
  7994. according to FMT (default from `org-email-link-description-format')."
  7995. (setq fmt (or fmt org-email-link-description-format))
  7996. (let* ((p org-store-link-plist)
  7997. (to (plist-get p :toaddress))
  7998. (from (plist-get p :fromaddress))
  7999. (table
  8000. (list
  8001. (cons "%c" (plist-get p :fromto))
  8002. (cons "%F" (plist-get p :from))
  8003. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  8004. (cons "%T" (plist-get p :to))
  8005. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  8006. (cons "%s" (plist-get p :subject))
  8007. (cons "%d" (plist-get p :date))
  8008. (cons "%m" (plist-get p :message-id)))))
  8009. (when (string-match "%c" fmt)
  8010. ;; Check if the user wrote this message
  8011. (if (and org-from-is-user-regexp from to
  8012. (save-match-data (string-match org-from-is-user-regexp from)))
  8013. (setq fmt (replace-match "to %t" t t fmt))
  8014. (setq fmt (replace-match "from %f" t t fmt))))
  8015. (org-replace-escapes fmt table)))
  8016. (defun org-make-org-heading-search-string (&optional string heading)
  8017. "Make search string for STRING or current headline."
  8018. (interactive)
  8019. (let ((s (or string (org-get-heading)))
  8020. (lines org-context-in-file-links))
  8021. (unless (and string (not heading))
  8022. ;; We are using a headline, clean up garbage in there.
  8023. (if (string-match org-todo-regexp s)
  8024. (setq s (replace-match "" t t s)))
  8025. (if (string-match (org-re ":[[:alnum:]_@#%:]+:[ \t]*$") s)
  8026. (setq s (replace-match "" t t s)))
  8027. (setq s (org-trim s))
  8028. (if (string-match (concat "^\\(" org-quote-string "\\|"
  8029. org-comment-string "\\)") s)
  8030. (setq s (replace-match "" t t s)))
  8031. (while (string-match org-ts-regexp s)
  8032. (setq s (replace-match "" t t s))))
  8033. (or string (setq s (concat "*" s))) ; Add * for headlines
  8034. (when (and string (integerp lines) (> lines 0))
  8035. (let ((slines (org-split-string s "\n")))
  8036. (when (< lines (length slines))
  8037. (setq s (mapconcat
  8038. 'identity
  8039. (reverse (nthcdr (- (length slines) lines)
  8040. (reverse slines))) "\n")))))
  8041. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  8042. (defun org-make-link (&rest strings)
  8043. "Concatenate STRINGS."
  8044. (apply 'concat strings))
  8045. (defun org-make-link-string (link &optional description)
  8046. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8047. (unless (string-match "\\S-" link)
  8048. (error "Empty link"))
  8049. (when (and description
  8050. (stringp description)
  8051. (not (string-match "\\S-" description)))
  8052. (setq description nil))
  8053. (when (stringp description)
  8054. ;; Remove brackets from the description, they are fatal.
  8055. (while (string-match "\\[" description)
  8056. (setq description (replace-match "{" t t description)))
  8057. (while (string-match "\\]" description)
  8058. (setq description (replace-match "}" t t description))))
  8059. (when (equal link description)
  8060. ;; No description needed, it is identical
  8061. (setq description nil))
  8062. (when (and (not description)
  8063. (not (string-match (org-image-file-name-regexp) link))
  8064. (not (equal link (org-link-escape link))))
  8065. (setq description (org-extract-attributes link)))
  8066. (setq link
  8067. (cond ((string-match (org-image-file-name-regexp) link) link)
  8068. ((string-match org-link-types-re link)
  8069. (concat (match-string 1 link)
  8070. (org-link-escape (substring link (match-end 1)))))
  8071. (t (org-link-escape link))))
  8072. (concat "[[" link "]"
  8073. (if description (concat "[" description "]") "")
  8074. "]"))
  8075. (defconst org-link-escape-chars
  8076. '(?\ ?\[ ?\] ?\; ?\= ?\+)
  8077. "List of characters that should be escaped in link.
  8078. This is the list that is used for internal purposes.")
  8079. (defvar org-url-encoding-use-url-hexify nil)
  8080. (defconst org-link-escape-chars-browser
  8081. '(?\ )
  8082. "List of escapes for characters that are problematic in links.
  8083. This is the list that is used before handing over to the browser.")
  8084. (defun org-link-escape (text &optional table merge)
  8085. "Return percent escaped representation of TEXT.
  8086. TEXT is a string with the text to escape.
  8087. Optional argument TABLE is a list with characters that should be
  8088. escaped. When nil, `org-link-escape-chars' is used.
  8089. If optional argument MERGE is set, merge TABLE into
  8090. `org-link-escape-chars'."
  8091. (if (and org-url-encoding-use-url-hexify (not table))
  8092. (url-hexify-string text)
  8093. (cond
  8094. ((and table merge)
  8095. (mapc (lambda (defchr)
  8096. (unless (member defchr table)
  8097. (setq table (cons defchr table)))) org-link-escape-chars))
  8098. ((null table)
  8099. (setq table org-link-escape-chars)))
  8100. (mapconcat
  8101. (lambda (char)
  8102. (if (or (member char table)
  8103. (< char 32) (= char 37) (> char 126))
  8104. (mapconcat (lambda (sequence-element)
  8105. (format "%%%.2X" sequence-element))
  8106. (or (encode-coding-char char 'utf-8)
  8107. (error "Unable to percent escape character: %s"
  8108. (char-to-string char))) "")
  8109. (char-to-string char))) text "")))
  8110. (defun org-link-unescape (str)
  8111. "Unhex hexified Unicode strings as returned from the JavaScript function
  8112. encodeURIComponent. E.g. `%C3%B6' is the german Umlaut `ö'."
  8113. (unless (and (null str) (string= "" str))
  8114. (let ((pos 0) (case-fold-search t) unhexed)
  8115. (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
  8116. (setq unhexed (org-link-unescape-compound (match-string 0 str)))
  8117. (setq str (replace-match unhexed t t str))
  8118. (setq pos (+ pos (length unhexed))))))
  8119. str)
  8120. (defun org-link-unescape-compound (hex)
  8121. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German Umlaut `ö'.
  8122. Note: this function also decodes single byte encodings like
  8123. `%E1' (\"á\") if not followed by another `%[A-F0-9]{2}' group."
  8124. (save-match-data
  8125. (let* ((bytes (cdr (split-string hex "%")))
  8126. (ret "")
  8127. (eat 0)
  8128. (sum 0))
  8129. (while bytes
  8130. (let* ((val (string-to-number (pop bytes) 16))
  8131. (shift-xor
  8132. (if (= 0 eat)
  8133. (cond
  8134. ((>= val 252) (cons 6 252))
  8135. ((>= val 248) (cons 5 248))
  8136. ((>= val 240) (cons 4 240))
  8137. ((>= val 224) (cons 3 224))
  8138. ((>= val 192) (cons 2 192))
  8139. (t (cons 0 0)))
  8140. (cons 6 128))))
  8141. (if (>= val 192) (setq eat (car shift-xor)))
  8142. (setq val (logxor val (cdr shift-xor)))
  8143. (setq sum (+ (lsh sum (car shift-xor)) val))
  8144. (if (> eat 0) (setq eat (- eat 1)))
  8145. (cond
  8146. ((= 0 eat) ;multi byte
  8147. (setq ret (concat ret (org-char-to-string sum)))
  8148. (setq sum 0))
  8149. ((not bytes) ; single byte(s)
  8150. (setq ret (org-link-unescape-single-byte-sequence hex))))
  8151. )) ;; end (while bytes
  8152. ret )))
  8153. (defun org-link-unescape-single-byte-sequence (hex)
  8154. "Unhexify hex-encoded single byte character sequences."
  8155. (mapconcat (lambda (byte)
  8156. (char-to-string (string-to-number byte 16)))
  8157. (cdr (split-string hex "%")) ""))
  8158. (defun org-xor (a b)
  8159. "Exclusive or."
  8160. (if a (not b) b))
  8161. (defun org-fixup-message-id-for-http (s)
  8162. "Replace special characters in a message id, so it can be used in an http query."
  8163. (when (string-match "%" s)
  8164. (setq s (mapconcat (lambda (c)
  8165. (if (eq c ?%)
  8166. "%25"
  8167. (char-to-string c)))
  8168. s "")))
  8169. (while (string-match "<" s)
  8170. (setq s (replace-match "%3C" t t s)))
  8171. (while (string-match ">" s)
  8172. (setq s (replace-match "%3E" t t s)))
  8173. (while (string-match "@" s)
  8174. (setq s (replace-match "%40" t t s)))
  8175. s)
  8176. ;;;###autoload
  8177. (defun org-insert-link-global ()
  8178. "Insert a link like Org-mode does.
  8179. This command can be called in any mode to insert a link in Org-mode syntax."
  8180. (interactive)
  8181. (org-load-modules-maybe)
  8182. (org-run-like-in-org-mode 'org-insert-link))
  8183. (defun org-insert-link (&optional complete-file link-location default-description)
  8184. "Insert a link. At the prompt, enter the link.
  8185. Completion can be used to insert any of the link protocol prefixes like
  8186. http or ftp in use.
  8187. The history can be used to select a link previously stored with
  8188. `org-store-link'. When the empty string is entered (i.e. if you just
  8189. press RET at the prompt), the link defaults to the most recently
  8190. stored link. As SPC triggers completion in the minibuffer, you need to
  8191. use M-SPC or C-q SPC to force the insertion of a space character.
  8192. You will also be prompted for a description, and if one is given, it will
  8193. be displayed in the buffer instead of the link.
  8194. If there is already a link at point, this command will allow you to edit link
  8195. and description parts.
  8196. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  8197. be selected using completion. The path to the file will be relative to the
  8198. current directory if the file is in the current directory or a subdirectory.
  8199. Otherwise, the link will be the absolute path as completed in the minibuffer
  8200. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  8201. option `org-link-file-path-type'.
  8202. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  8203. the current directory or below.
  8204. With three \\[universal-argument] prefixes, negate the meaning of
  8205. `org-keep-stored-link-after-insertion'.
  8206. If `org-make-link-description-function' is non-nil, this function will be
  8207. called with the link target, and the result will be the default
  8208. link description.
  8209. If the LINK-LOCATION parameter is non-nil, this value will be
  8210. used as the link location instead of reading one interactively.
  8211. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  8212. be used as the default description."
  8213. (interactive "P")
  8214. (let* ((wcf (current-window-configuration))
  8215. (region (if (org-region-active-p)
  8216. (buffer-substring (region-beginning) (region-end))))
  8217. (remove (and region (list (region-beginning) (region-end))))
  8218. (desc region)
  8219. tmphist ; byte-compile incorrectly complains about this
  8220. (link link-location)
  8221. entry file all-prefixes)
  8222. (cond
  8223. (link-location) ; specified by arg, just use it.
  8224. ((org-in-regexp org-bracket-link-regexp 1)
  8225. ;; We do have a link at point, and we are going to edit it.
  8226. (setq remove (list (match-beginning 0) (match-end 0)))
  8227. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  8228. (setq link (read-string "Link: "
  8229. (org-link-unescape
  8230. (org-match-string-no-properties 1)))))
  8231. ((or (org-in-regexp org-angle-link-re)
  8232. (org-in-regexp org-plain-link-re))
  8233. ;; Convert to bracket link
  8234. (setq remove (list (match-beginning 0) (match-end 0))
  8235. link (read-string "Link: "
  8236. (org-remove-angle-brackets (match-string 0)))))
  8237. ((member complete-file '((4) (16)))
  8238. ;; Completing read for file names.
  8239. (setq link (org-file-complete-link complete-file)))
  8240. (t
  8241. ;; Read link, with completion for stored links.
  8242. (with-output-to-temp-buffer "*Org Links*"
  8243. (princ "Insert a link.
  8244. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  8245. (when org-stored-links
  8246. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  8247. (princ (mapconcat
  8248. (lambda (x)
  8249. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  8250. (reverse org-stored-links) "\n"))))
  8251. (let ((cw (selected-window)))
  8252. (select-window (get-buffer-window "*Org Links*" 'visible))
  8253. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  8254. (unless (pos-visible-in-window-p (point-max))
  8255. (org-fit-window-to-buffer))
  8256. (and (window-live-p cw) (select-window cw)))
  8257. ;; Fake a link history, containing the stored links.
  8258. (setq tmphist (append (mapcar 'car org-stored-links)
  8259. org-insert-link-history))
  8260. (setq all-prefixes (append (mapcar 'car org-link-abbrev-alist-local)
  8261. (mapcar 'car org-link-abbrev-alist)
  8262. org-link-types))
  8263. (unwind-protect
  8264. (progn
  8265. (setq link
  8266. (let ((org-completion-use-ido nil)
  8267. (org-completion-use-iswitchb nil))
  8268. (org-completing-read
  8269. "Link: "
  8270. (append
  8271. (mapcar (lambda (x) (list (concat x ":")))
  8272. all-prefixes)
  8273. (mapcar 'car org-stored-links))
  8274. nil nil nil
  8275. 'tmphist
  8276. (car (car org-stored-links)))))
  8277. (if (not (string-match "\\S-" link))
  8278. (error "No link selected"))
  8279. (if (or (member link all-prefixes)
  8280. (and (equal ":" (substring link -1))
  8281. (member (substring link 0 -1) all-prefixes)
  8282. (setq link (substring link 0 -1))))
  8283. (setq link (org-link-try-special-completion link))))
  8284. (set-window-configuration wcf)
  8285. (kill-buffer "*Org Links*"))
  8286. (setq entry (assoc link org-stored-links))
  8287. (or entry (push link org-insert-link-history))
  8288. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  8289. (not org-keep-stored-link-after-insertion))
  8290. (setq org-stored-links (delq (assoc link org-stored-links)
  8291. org-stored-links)))
  8292. (setq desc (or desc (nth 1 entry)))))
  8293. (if (string-match org-plain-link-re link)
  8294. ;; URL-like link, normalize the use of angular brackets.
  8295. (setq link (org-make-link (org-remove-angle-brackets link))))
  8296. ;; Check if we are linking to the current file with a search option
  8297. ;; If yes, simplify the link by using only the search option.
  8298. (when (and buffer-file-name
  8299. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  8300. (let* ((path (match-string 1 link))
  8301. (case-fold-search nil)
  8302. (search (match-string 2 link)))
  8303. (save-match-data
  8304. (if (equal (file-truename buffer-file-name) (file-truename path))
  8305. ;; We are linking to this same file, with a search option
  8306. (setq link search)))))
  8307. ;; Check if we can/should use a relative path. If yes, simplify the link
  8308. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  8309. (let* ((type (match-string 1 link))
  8310. (path (match-string 2 link))
  8311. (origpath path)
  8312. (case-fold-search nil))
  8313. (cond
  8314. ((or (eq org-link-file-path-type 'absolute)
  8315. (equal complete-file '(16)))
  8316. (setq path (abbreviate-file-name (expand-file-name path))))
  8317. ((eq org-link-file-path-type 'noabbrev)
  8318. (setq path (expand-file-name path)))
  8319. ((eq org-link-file-path-type 'relative)
  8320. (setq path (file-relative-name path)))
  8321. (t
  8322. (save-match-data
  8323. (if (string-match (concat "^" (regexp-quote
  8324. (expand-file-name
  8325. (file-name-as-directory
  8326. default-directory))))
  8327. (expand-file-name path))
  8328. ;; We are linking a file with relative path name.
  8329. (setq path (substring (expand-file-name path)
  8330. (match-end 0)))
  8331. (setq path (abbreviate-file-name (expand-file-name path)))))))
  8332. (setq link (concat type path))
  8333. (if (equal desc origpath)
  8334. (setq desc path))))
  8335. (if org-make-link-description-function
  8336. (setq desc (funcall org-make-link-description-function link desc))
  8337. (if default-description (setq desc default-description)))
  8338. (setq desc (read-string "Description: " desc))
  8339. (unless (string-match "\\S-" desc) (setq desc nil))
  8340. (if remove (apply 'delete-region remove))
  8341. (insert (org-make-link-string link desc))))
  8342. (defun org-link-try-special-completion (type)
  8343. "If there is completion support for link type TYPE, offer it."
  8344. (let ((fun (intern (concat "org-" type "-complete-link"))))
  8345. (if (functionp fun)
  8346. (funcall fun)
  8347. (read-string "Link (no completion support): " (concat type ":")))))
  8348. (defun org-file-complete-link (&optional arg)
  8349. "Create a file link using completion."
  8350. (let (file link)
  8351. (setq file (read-file-name "File: "))
  8352. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  8353. (pwd1 (file-name-as-directory (abbreviate-file-name
  8354. (expand-file-name ".")))))
  8355. (cond
  8356. ((equal arg '(16))
  8357. (setq link (org-make-link
  8358. "file:"
  8359. (abbreviate-file-name (expand-file-name file)))))
  8360. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  8361. (setq link (org-make-link "file:" (match-string 1 file))))
  8362. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  8363. (expand-file-name file))
  8364. (setq link (org-make-link
  8365. "file:" (match-string 1 (expand-file-name file)))))
  8366. (t (setq link (org-make-link "file:" file)))))
  8367. link))
  8368. (defun org-completing-read (&rest args)
  8369. "Completing-read with SPACE being a normal character."
  8370. (let ((enable-recursive-minibuffers t)
  8371. (minibuffer-local-completion-map
  8372. (copy-keymap minibuffer-local-completion-map)))
  8373. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  8374. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  8375. (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
  8376. (apply 'org-icompleting-read args)))
  8377. (defun org-completing-read-no-i (&rest args)
  8378. (let (org-completion-use-ido org-completion-use-iswitchb)
  8379. (apply 'org-completing-read args)))
  8380. (defun org-iswitchb-completing-read (prompt choices &rest args)
  8381. "Use iswitch as a completing-read replacement to choose from choices.
  8382. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  8383. from."
  8384. (let* ((iswitchb-use-virtual-buffers nil)
  8385. (iswitchb-make-buflist-hook
  8386. (lambda ()
  8387. (setq iswitchb-temp-buflist choices))))
  8388. (iswitchb-read-buffer prompt)))
  8389. (defun org-icompleting-read (&rest args)
  8390. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  8391. (org-without-partial-completion
  8392. (if (and org-completion-use-ido
  8393. (fboundp 'ido-completing-read)
  8394. (boundp 'ido-mode) ido-mode
  8395. (listp (second args)))
  8396. (let ((ido-enter-matching-directory nil))
  8397. (apply 'ido-completing-read (concat (car args))
  8398. (if (consp (car (nth 1 args)))
  8399. (mapcar 'car (nth 1 args))
  8400. (nth 1 args))
  8401. (cddr args)))
  8402. (if (and org-completion-use-iswitchb
  8403. (boundp 'iswitchb-mode) iswitchb-mode
  8404. (listp (second args)))
  8405. (apply 'org-iswitchb-completing-read (concat (car args))
  8406. (if (consp (car (nth 1 args)))
  8407. (mapcar 'car (nth 1 args))
  8408. (nth 1 args))
  8409. (cddr args))
  8410. (apply 'completing-read args)))))
  8411. (defun org-extract-attributes (s)
  8412. "Extract the attributes cookie from a string and set as text property."
  8413. (let (a attr (start 0) key value)
  8414. (save-match-data
  8415. (when (string-match "{{\\([^}]+\\)}}$" s)
  8416. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  8417. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  8418. (setq key (match-string 1 a) value (match-string 2 a)
  8419. start (match-end 0)
  8420. attr (plist-put attr (intern key) value))))
  8421. (org-add-props s nil 'org-attr attr))
  8422. s))
  8423. (defun org-extract-attributes-from-string (tag)
  8424. (let (key value attr)
  8425. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  8426. (setq key (match-string 1 tag) value (match-string 2 tag)
  8427. tag (replace-match "" t t tag)
  8428. attr (plist-put attr (intern key) value)))
  8429. (cons tag attr)))
  8430. (defun org-attributes-to-string (plist)
  8431. "Format a property list into an HTML attribute list."
  8432. (let ((s "") key value)
  8433. (while plist
  8434. (setq key (pop plist) value (pop plist))
  8435. (and value
  8436. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  8437. s))
  8438. ;;; Opening/following a link
  8439. (defvar org-link-search-failed nil)
  8440. (defvar org-open-link-functions nil
  8441. "Hook for functions finding a plain text link.
  8442. These functions must take a single argument, the link content.
  8443. They will be called for links that look like [[link text][description]]
  8444. when LINK TEXT does not have a protocol like \"http:\" and does not look
  8445. like a filename (e.g. \"./blue.png\").
  8446. These functions will be called *before* Org attempts to resolve the
  8447. link by doing text searches in the current buffer - so if you want a
  8448. link \"[[target]]\" to still find \"<<target>>\", your function should
  8449. handle this as a special case.
  8450. When the function does handle the link, it must return a non-nil value.
  8451. If it decides that it is not responsible for this link, it must return
  8452. nil to indicate that that Org-mode can continue with other options
  8453. like exact and fuzzy text search.")
  8454. (defun org-next-link ()
  8455. "Move forward to the next link.
  8456. If the link is in hidden text, expose it."
  8457. (interactive)
  8458. (when (and org-link-search-failed (eq this-command last-command))
  8459. (goto-char (point-min))
  8460. (message "Link search wrapped back to beginning of buffer"))
  8461. (setq org-link-search-failed nil)
  8462. (let* ((pos (point))
  8463. (ct (org-context))
  8464. (a (assoc :link ct)))
  8465. (if a (goto-char (nth 2 a)))
  8466. (if (re-search-forward org-any-link-re nil t)
  8467. (progn
  8468. (goto-char (match-beginning 0))
  8469. (if (outline-invisible-p) (org-show-context)))
  8470. (goto-char pos)
  8471. (setq org-link-search-failed t)
  8472. (error "No further link found"))))
  8473. (defun org-previous-link ()
  8474. "Move backward to the previous link.
  8475. If the link is in hidden text, expose it."
  8476. (interactive)
  8477. (when (and org-link-search-failed (eq this-command last-command))
  8478. (goto-char (point-max))
  8479. (message "Link search wrapped back to end of buffer"))
  8480. (setq org-link-search-failed nil)
  8481. (let* ((pos (point))
  8482. (ct (org-context))
  8483. (a (assoc :link ct)))
  8484. (if a (goto-char (nth 1 a)))
  8485. (if (re-search-backward org-any-link-re nil t)
  8486. (progn
  8487. (goto-char (match-beginning 0))
  8488. (if (outline-invisible-p) (org-show-context)))
  8489. (goto-char pos)
  8490. (setq org-link-search-failed t)
  8491. (error "No further link found"))))
  8492. (defun org-translate-link (s)
  8493. "Translate a link string if a translation function has been defined."
  8494. (if (and org-link-translation-function
  8495. (fboundp org-link-translation-function)
  8496. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  8497. (progn
  8498. (setq s (funcall org-link-translation-function
  8499. (match-string 1) (match-string 2)))
  8500. (concat (car s) ":" (cdr s)))
  8501. s))
  8502. (defun org-translate-link-from-planner (type path)
  8503. "Translate a link from Emacs Planner syntax so that Org can follow it.
  8504. This is still an experimental function, your mileage may vary."
  8505. (cond
  8506. ((member type '("http" "https" "news" "ftp"))
  8507. ;; standard Internet links are the same.
  8508. nil)
  8509. ((and (equal type "irc") (string-match "^//" path))
  8510. ;; Planner has two / at the beginning of an irc link, we have 1.
  8511. ;; We should have zero, actually....
  8512. (setq path (substring path 1)))
  8513. ((and (equal type "lisp") (string-match "^/" path))
  8514. ;; Planner has a slash, we do not.
  8515. (setq type "elisp" path (substring path 1)))
  8516. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  8517. ;; A typical message link. Planner has the id after the final slash,
  8518. ;; we separate it with a hash mark
  8519. (setq path (concat (match-string 1 path) "#"
  8520. (org-remove-angle-brackets (match-string 2 path)))))
  8521. )
  8522. (cons type path))
  8523. (defun org-find-file-at-mouse (ev)
  8524. "Open file link or URL at mouse."
  8525. (interactive "e")
  8526. (mouse-set-point ev)
  8527. (org-open-at-point 'in-emacs))
  8528. (defun org-open-at-mouse (ev)
  8529. "Open file link or URL at mouse.
  8530. See the docstring of `org-open-file' for details."
  8531. (interactive "e")
  8532. (mouse-set-point ev)
  8533. (if (eq major-mode 'org-agenda-mode)
  8534. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  8535. (org-open-at-point))
  8536. (defvar org-window-config-before-follow-link nil
  8537. "The window configuration before following a link.
  8538. This is saved in case the need arises to restore it.")
  8539. (defvar org-open-link-marker (make-marker)
  8540. "Marker pointing to the location where `org-open-at-point; was called.")
  8541. ;;;###autoload
  8542. (defun org-open-at-point-global ()
  8543. "Follow a link like Org-mode does.
  8544. This command can be called in any mode to follow a link that has
  8545. Org-mode syntax."
  8546. (interactive)
  8547. (org-run-like-in-org-mode 'org-open-at-point))
  8548. ;;;###autoload
  8549. (defun org-open-link-from-string (s &optional arg reference-buffer)
  8550. "Open a link in the string S, as if it was in Org-mode."
  8551. (interactive "sLink: \nP")
  8552. (let ((reference-buffer (or reference-buffer (current-buffer))))
  8553. (with-temp-buffer
  8554. (let ((org-inhibit-startup (not reference-buffer)))
  8555. (org-mode)
  8556. (insert s)
  8557. (goto-char (point-min))
  8558. (when reference-buffer
  8559. (setq org-link-abbrev-alist-local
  8560. (with-current-buffer reference-buffer
  8561. org-link-abbrev-alist-local)))
  8562. (org-open-at-point arg reference-buffer)))))
  8563. (defvar org-open-at-point-functions nil
  8564. "Hook that is run when following a link at point.
  8565. Functions in this hook must return t if they identify and follow
  8566. a link at point. If they don't find anything interesting at point,
  8567. they must return nil.")
  8568. (defun org-open-at-point (&optional arg reference-buffer)
  8569. "Open link at or after point.
  8570. If there is no link at point, this function will search forward up to
  8571. the end of the current line.
  8572. Normally, files will be opened by an appropriate application. If the
  8573. optional prefix argument ARG is non-nil, Emacs will visit the file.
  8574. With a double prefix argument, try to open outside of Emacs, in the
  8575. application the system uses for this file type."
  8576. (interactive "P")
  8577. ;; if in a code block, then open the block's results
  8578. (unless (call-interactively #'org-babel-open-src-block-result)
  8579. (org-load-modules-maybe)
  8580. (move-marker org-open-link-marker (point))
  8581. (setq org-window-config-before-follow-link (current-window-configuration))
  8582. (org-remove-occur-highlights nil nil t)
  8583. (cond
  8584. ((and (org-at-heading-p)
  8585. (not (org-in-regexp
  8586. (concat org-plain-link-re "\\|"
  8587. org-bracket-link-regexp "\\|"
  8588. org-angle-link-re "\\|"
  8589. "[ \t]:[^ \t\n]+:[ \t]*$")))
  8590. (not (get-text-property (point) 'org-linked-text)))
  8591. (or (org-offer-links-in-entry arg)
  8592. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  8593. ((run-hook-with-args-until-success 'org-open-at-point-functions))
  8594. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  8595. ((and (or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  8596. (not (org-in-regexp org-bracket-link-regexp)))
  8597. (org-footnote-action))
  8598. (t
  8599. (let (type path link line search (pos (point)))
  8600. (catch 'match
  8601. (save-excursion
  8602. (skip-chars-forward "^]\n\r")
  8603. (when (org-in-regexp org-bracket-link-regexp 1)
  8604. (setq link (org-extract-attributes
  8605. (org-link-unescape (org-match-string-no-properties 1))))
  8606. (while (string-match " *\n *" link)
  8607. (setq link (replace-match " " t t link)))
  8608. (setq link (org-link-expand-abbrev link))
  8609. (cond
  8610. ((or (file-name-absolute-p link)
  8611. (string-match "^\\.\\.?/" link))
  8612. (setq type "file" path link))
  8613. ((string-match org-link-re-with-space3 link)
  8614. (setq type (match-string 1 link) path (match-string 2 link)))
  8615. (t (setq type "thisfile" path link)))
  8616. (throw 'match t)))
  8617. (when (get-text-property (point) 'org-linked-text)
  8618. (setq type "thisfile"
  8619. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  8620. (1+ (point)) (point))
  8621. path (buffer-substring
  8622. (or (previous-single-property-change pos 'org-linked-text)
  8623. (point-min))
  8624. (or (next-single-property-change pos 'org-linked-text)
  8625. (point-max))))
  8626. (throw 'match t))
  8627. (save-excursion
  8628. (when (or (org-in-regexp org-angle-link-re)
  8629. (org-in-regexp org-plain-link-re))
  8630. (setq type (match-string 1)
  8631. path (org-link-unescape (match-string 2)))
  8632. (throw 'match t)))
  8633. (save-excursion
  8634. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
  8635. (setq type "tags"
  8636. path (match-string 1))
  8637. (while (string-match ":" path)
  8638. (setq path (replace-match "+" t t path)))
  8639. (throw 'match t)))
  8640. (when (org-in-regexp "<\\([^><\n]+\\)>")
  8641. (setq type "tree-match"
  8642. path (match-string 1))
  8643. (throw 'match t)))
  8644. (unless path
  8645. (error "No link found"))
  8646. ;; switch back to reference buffer
  8647. ;; needed when if called in a temporary buffer through
  8648. ;; org-open-link-from-string
  8649. (with-current-buffer (or reference-buffer (current-buffer))
  8650. ;; Remove any trailing spaces in path
  8651. (if (string-match " +\\'" path)
  8652. (setq path (replace-match "" t t path)))
  8653. (if (and org-link-translation-function
  8654. (fboundp org-link-translation-function))
  8655. ;; Check if we need to translate the link
  8656. (let ((tmp (funcall org-link-translation-function type path)))
  8657. (setq type (car tmp) path (cdr tmp))))
  8658. (cond
  8659. ((assoc type org-link-protocols)
  8660. (funcall (nth 1 (assoc type org-link-protocols)) path))
  8661. ((equal type "mailto")
  8662. (let ((cmd (car org-link-mailto-program))
  8663. (args (cdr org-link-mailto-program)) args1
  8664. (address path) (subject "") a)
  8665. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  8666. (setq address (match-string 1 path)
  8667. subject (org-link-escape (match-string 2 path))))
  8668. (while args
  8669. (cond
  8670. ((not (stringp (car args))) (push (pop args) args1))
  8671. (t (setq a (pop args))
  8672. (if (string-match "%a" a)
  8673. (setq a (replace-match address t t a)))
  8674. (if (string-match "%s" a)
  8675. (setq a (replace-match subject t t a)))
  8676. (push a args1))))
  8677. (apply cmd (nreverse args1))))
  8678. ((member type '("http" "https" "ftp" "news"))
  8679. (browse-url (concat type ":" (if (org-string-match-p "[[:nonascii:] ]" path)
  8680. (org-link-escape
  8681. path org-link-escape-chars-browser)
  8682. path))))
  8683. ((string= type "doi")
  8684. (browse-url (concat "http://dx.doi.org/" (if (org-string-match-p "[[:nonascii:] ]" path)
  8685. (org-link-escape
  8686. path org-link-escape-chars-browser)
  8687. path))))
  8688. ((member type '("message"))
  8689. (browse-url (concat type ":" path)))
  8690. ((string= type "tags")
  8691. (org-tags-view arg path))
  8692. ((string= type "tree-match")
  8693. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  8694. ((string= type "file")
  8695. (if (string-match "::\\([0-9]+\\)\\'" path)
  8696. (setq line (string-to-number (match-string 1 path))
  8697. path (substring path 0 (match-beginning 0)))
  8698. (if (string-match "::\\(.+\\)\\'" path)
  8699. (setq search (match-string 1 path)
  8700. path (substring path 0 (match-beginning 0)))))
  8701. (if (string-match "[*?{]" (file-name-nondirectory path))
  8702. (dired path)
  8703. (org-open-file path arg line search)))
  8704. ((string= type "shell")
  8705. (let ((cmd path))
  8706. (if (or (and (not (string= org-confirm-shell-link-not-regexp ""))
  8707. (string-match org-confirm-shell-link-not-regexp cmd))
  8708. (not org-confirm-shell-link-function)
  8709. (funcall org-confirm-shell-link-function
  8710. (format "Execute \"%s\" in shell? "
  8711. (org-add-props cmd nil
  8712. 'face 'org-warning))))
  8713. (progn
  8714. (message "Executing %s" cmd)
  8715. (shell-command cmd))
  8716. (error "Abort"))))
  8717. ((string= type "elisp")
  8718. (let ((cmd path))
  8719. (if (or (and (not (string= org-confirm-elisp-link-not-regexp ""))
  8720. (string-match org-confirm-elisp-link-not-regexp cmd))
  8721. (not org-confirm-elisp-link-function)
  8722. (funcall org-confirm-elisp-link-function
  8723. (format "Execute \"%s\" as elisp? "
  8724. (org-add-props cmd nil
  8725. 'face 'org-warning))))
  8726. (message "%s => %s" cmd
  8727. (if (equal (string-to-char cmd) ?\()
  8728. (eval (read cmd))
  8729. (call-interactively (read cmd))))
  8730. (error "Abort"))))
  8731. ((and (string= type "thisfile")
  8732. (run-hook-with-args-until-success
  8733. 'org-open-link-functions path)))
  8734. ((string= type "thisfile")
  8735. (if arg
  8736. (switch-to-buffer-other-window
  8737. (org-get-buffer-for-internal-link (current-buffer)))
  8738. (org-mark-ring-push))
  8739. (let ((cmd `(org-link-search
  8740. ,path
  8741. ,(cond ((equal arg '(4)) ''occur)
  8742. ((equal arg '(16)) ''org-occur)
  8743. (t nil))
  8744. ,pos)))
  8745. (condition-case nil (let ((org-link-search-inhibit-query t))
  8746. (eval cmd))
  8747. (error (progn (widen) (eval cmd))))))
  8748. (t
  8749. (browse-url-at-point)))))))
  8750. (move-marker org-open-link-marker nil)
  8751. (run-hook-with-args 'org-follow-link-hook)))
  8752. (defun org-offer-links-in-entry (&optional nth zero)
  8753. "Offer links in the current entry and follow the selected link.
  8754. If there is only one link, follow it immediately as well.
  8755. If NTH is an integer, immediately pick the NTH link found.
  8756. If ZERO is a string, check also this string for a link, and if
  8757. there is one, offer it as link number zero."
  8758. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  8759. "\\(" org-angle-link-re "\\)\\|"
  8760. "\\(" org-plain-link-re "\\)"))
  8761. (cnt ?0)
  8762. (in-emacs (if (integerp nth) nil nth))
  8763. have-zero end links link c)
  8764. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  8765. (push (match-string 0 zero) links)
  8766. (setq cnt (1- cnt) have-zero t))
  8767. (save-excursion
  8768. (org-back-to-heading t)
  8769. (setq end (save-excursion (outline-next-heading) (point)))
  8770. (while (re-search-forward re end t)
  8771. (push (match-string 0) links))
  8772. (setq links (org-uniquify (reverse links))))
  8773. (cond
  8774. ((null links)
  8775. (message "No links"))
  8776. ((equal (length links) 1)
  8777. (setq link (list (car links))))
  8778. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  8779. (setq link (list (nth (if have-zero nth (1- nth)) links))))
  8780. (t ; we have to select a link
  8781. (save-excursion
  8782. (save-window-excursion
  8783. (delete-other-windows)
  8784. (with-output-to-temp-buffer "*Select Link*"
  8785. (mapc (lambda (l)
  8786. (if (not (string-match org-bracket-link-regexp l))
  8787. (princ (format "[%c] %s\n" (incf cnt)
  8788. (org-remove-angle-brackets l)))
  8789. (if (match-end 3)
  8790. (princ (format "[%c] %s (%s)\n" (incf cnt)
  8791. (match-string 3 l) (match-string 1 l)))
  8792. (princ (format "[%c] %s\n" (incf cnt)
  8793. (match-string 1 l))))))
  8794. links))
  8795. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  8796. (message "Select link to open, RET to open all:")
  8797. (setq c (read-char-exclusive))
  8798. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  8799. (when (equal c ?q) (error "Abort"))
  8800. (if (equal c ?\C-m)
  8801. (setq link links)
  8802. (setq nth (- c ?0))
  8803. (if have-zero (setq nth (1+ nth)))
  8804. (unless (and (integerp nth) (>= (length links) nth))
  8805. (error "Invalid link selection"))
  8806. (setq link (list (nth (1- nth) links))))))
  8807. (if link
  8808. (let ((buf (current-buffer)))
  8809. (dolist (l link)
  8810. (org-open-link-from-string l in-emacs buf))
  8811. t)
  8812. nil)))
  8813. ;; Add special file links that specify the way of opening
  8814. (org-add-link-type "file+sys" 'org-open-file-with-system)
  8815. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  8816. (defun org-open-file-with-system (path)
  8817. "Open file at PATH using the system way of opening it."
  8818. (org-open-file path 'system))
  8819. (defun org-open-file-with-emacs (path)
  8820. "Open file at PATH in Emacs."
  8821. (org-open-file path 'emacs))
  8822. (defun org-remove-file-link-modifiers ()
  8823. "Remove the file link modifiers in `file+sys:' and `file+emacs:' links."
  8824. (goto-char (point-min))
  8825. (while (re-search-forward "\\<file\\+\\(sys\\|emacs\\):" nil t)
  8826. (org-if-unprotected
  8827. (replace-match "file:" t t))))
  8828. (eval-after-load "org-exp"
  8829. '(add-hook 'org-export-preprocess-before-normalizing-links-hook
  8830. 'org-remove-file-link-modifiers))
  8831. ;;;; Time estimates
  8832. (defun org-get-effort (&optional pom)
  8833. "Get the effort estimate for the current entry."
  8834. (org-entry-get pom org-effort-property))
  8835. ;;; File search
  8836. (defvar org-create-file-search-functions nil
  8837. "List of functions to construct the right search string for a file link.
  8838. These functions are called in turn with point at the location to
  8839. which the link should point.
  8840. A function in the hook should first test if it would like to
  8841. handle this file type, for example by checking the `major-mode'
  8842. or the file extension. If it decides not to handle this file, it
  8843. should just return nil to give other functions a chance. If it
  8844. does handle the file, it must return the search string to be used
  8845. when following the link. The search string will be part of the
  8846. file link, given after a double colon, and `org-open-at-point'
  8847. will automatically search for it. If special measures must be
  8848. taken to make the search successful, another function should be
  8849. added to the companion hook `org-execute-file-search-functions',
  8850. which see.
  8851. A function in this hook may also use `setq' to set the variable
  8852. `description' to provide a suggestion for the descriptive text to
  8853. be used for this link when it gets inserted into an Org-mode
  8854. buffer with \\[org-insert-link].")
  8855. (defvar org-execute-file-search-functions nil
  8856. "List of functions to execute a file search triggered by a link.
  8857. Functions added to this hook must accept a single argument, the
  8858. search string that was part of the file link, the part after the
  8859. double colon. The function must first check if it would like to
  8860. handle this search, for example by checking the `major-mode' or
  8861. the file extension. If it decides not to handle this search, it
  8862. should just return nil to give other functions a chance. If it
  8863. does handle the search, it must return a non-nil value to keep
  8864. other functions from trying.
  8865. Each function can access the current prefix argument through the
  8866. variable `current-prefix-argument'. Note that a single prefix is
  8867. used to force opening a link in Emacs, so it may be good to only
  8868. use a numeric or double prefix to guide the search function.
  8869. In case this is needed, a function in this hook can also restore
  8870. the window configuration before `org-open-at-point' was called using:
  8871. (set-window-configuration org-window-config-before-follow-link)")
  8872. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  8873. (defun org-link-search (s &optional type avoid-pos stealth)
  8874. "Search for a link search option.
  8875. If S is surrounded by forward slashes, it is interpreted as a
  8876. regular expression. In org-mode files, this will create an `org-occur'
  8877. sparse tree. In ordinary files, `occur' will be used to list matches.
  8878. If the current buffer is in `dired-mode', grep will be used to search
  8879. in all files. If AVOID-POS is given, ignore matches near that position.
  8880. When optional argument STEALTH is non-nil, do not modify
  8881. visibility around point, thus ignoring
  8882. `org-show-hierarchy-above', `org-show-following-heading' and
  8883. `org-show-siblings' variables."
  8884. (let ((case-fold-search t)
  8885. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  8886. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  8887. (append '(("") (" ") ("\t") ("\n"))
  8888. org-emphasis-alist)
  8889. "\\|") "\\)"))
  8890. (pos (point))
  8891. (pre nil) (post nil)
  8892. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  8893. (cond
  8894. ;; First check if there are any special search functions
  8895. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  8896. ;; Now try the builtin stuff
  8897. ((and (equal (string-to-char s0) ?#)
  8898. (> (length s0) 1)
  8899. (save-excursion
  8900. (goto-char (point-min))
  8901. (and
  8902. (re-search-forward
  8903. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  8904. (setq type 'dedicated
  8905. pos (match-beginning 0))))
  8906. ;; There is an exact target for this
  8907. (goto-char pos)
  8908. (org-back-to-heading t)))
  8909. ((save-excursion
  8910. (goto-char (point-min))
  8911. (and
  8912. (re-search-forward
  8913. (concat "<<" (regexp-quote s0) ">>") nil t)
  8914. (setq type 'dedicated
  8915. pos (match-beginning 0))))
  8916. ;; There is an exact target for this
  8917. (goto-char pos))
  8918. ((save-excursion
  8919. (goto-char (point-min))
  8920. (and
  8921. (re-search-forward
  8922. (format "^[ \t]*#\\+TARGET: %s" (regexp-quote s0)) nil t)
  8923. (setq type 'dedicated pos (match-beginning 0))))
  8924. ;; Found an invisible target.
  8925. (goto-char pos))
  8926. ((save-excursion
  8927. (goto-char (point-min))
  8928. (and
  8929. (re-search-forward
  8930. (format "^[ \t]*#\\+NAME: %s" (regexp-quote s0)) nil t)
  8931. (setq type 'dedicated pos (match-beginning 0))))
  8932. ;; Found an element with a matching #+name affiliated keyword.
  8933. (goto-char pos))
  8934. ((and (string-match "^(\\(.*\\))$" s0)
  8935. (save-excursion
  8936. (goto-char (point-min))
  8937. (and
  8938. (re-search-forward
  8939. (concat "[^[]" (regexp-quote
  8940. (format org-coderef-label-format
  8941. (match-string 1 s0))))
  8942. nil t)
  8943. (setq type 'dedicated
  8944. pos (1+ (match-beginning 0))))))
  8945. ;; There is a coderef target for this
  8946. (goto-char pos))
  8947. ((string-match "^/\\(.*\\)/$" s)
  8948. ;; A regular expression
  8949. (cond
  8950. ((eq major-mode 'org-mode)
  8951. (org-occur (match-string 1 s)))
  8952. ;;((eq major-mode 'dired-mode)
  8953. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  8954. (t (org-do-occur (match-string 1 s)))))
  8955. ((and (eq major-mode 'org-mode) org-link-search-must-match-exact-headline)
  8956. (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  8957. (goto-char (point-min))
  8958. (cond
  8959. ((let (case-fold-search)
  8960. (re-search-forward (format org-complex-heading-regexp-format
  8961. (regexp-quote s))
  8962. nil t))
  8963. ;; OK, found a match
  8964. (setq type 'dedicated)
  8965. (goto-char (match-beginning 0)))
  8966. ((and (not org-link-search-inhibit-query)
  8967. (eq org-link-search-must-match-exact-headline 'query-to-create)
  8968. (y-or-n-p "No match - create this as a new heading? "))
  8969. (goto-char (point-max))
  8970. (or (bolp) (newline))
  8971. (insert "* " s "\n")
  8972. (beginning-of-line 0))
  8973. (t
  8974. (goto-char pos)
  8975. (error "No match"))))
  8976. (t
  8977. ;; A normal search string
  8978. (when (equal (string-to-char s) ?*)
  8979. ;; Anchor on headlines, post may include tags.
  8980. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  8981. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
  8982. s (substring s 1)))
  8983. (remove-text-properties
  8984. 0 (length s)
  8985. '(face nil mouse-face nil keymap nil fontified nil) s)
  8986. ;; Make a series of regular expressions to find a match
  8987. (setq words (org-split-string s "[ \n\r\t]+")
  8988. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  8989. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  8990. "\\)" markers)
  8991. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  8992. re2a (concat "[ \t\r\n]" re2a_)
  8993. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  8994. re4 (concat "[^a-zA-Z_]" re4_)
  8995. re1 (concat pre re2 post)
  8996. re3 (concat pre (if pre re4_ re4) post)
  8997. re5 (concat pre ".*" re4)
  8998. re2 (concat pre re2)
  8999. re2a (concat pre (if pre re2a_ re2a))
  9000. re4 (concat pre (if pre re4_ re4))
  9001. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  9002. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  9003. re5 "\\)"
  9004. ))
  9005. (cond
  9006. ((eq type 'org-occur) (org-occur reall))
  9007. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  9008. (t (goto-char (point-min))
  9009. (setq type 'fuzzy)
  9010. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  9011. (org-search-not-self 1 re1 nil t)
  9012. (org-search-not-self 1 re2 nil t)
  9013. (org-search-not-self 1 re2a nil t)
  9014. (org-search-not-self 1 re3 nil t)
  9015. (org-search-not-self 1 re4 nil t)
  9016. (org-search-not-self 1 re5 nil t)
  9017. )
  9018. (goto-char (match-beginning 1))
  9019. (goto-char pos)
  9020. (error "No match"))))))
  9021. (and (eq major-mode 'org-mode)
  9022. (not stealth)
  9023. (org-show-context 'link-search))
  9024. type))
  9025. (defun org-search-not-self (group &rest args)
  9026. "Execute `re-search-forward', but only accept matches that do not
  9027. enclose the position of `org-open-link-marker'."
  9028. (let ((m org-open-link-marker))
  9029. (catch 'exit
  9030. (while (apply 're-search-forward args)
  9031. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  9032. (goto-char (match-end group))
  9033. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  9034. (> (match-beginning 0) (marker-position m))
  9035. (< (match-end 0) (marker-position m)))
  9036. (save-match-data
  9037. (or (not (org-in-regexp
  9038. org-bracket-link-analytic-regexp 1))
  9039. (not (match-end 4)) ; no description
  9040. (and (<= (match-beginning 4) (point))
  9041. (>= (match-end 4) (point))))))
  9042. (throw 'exit (point))))))))
  9043. (defun org-get-buffer-for-internal-link (buffer)
  9044. "Return a buffer to be used for displaying the link target of internal links."
  9045. (cond
  9046. ((not org-display-internal-link-with-indirect-buffer)
  9047. buffer)
  9048. ((string-match "(Clone)$" (buffer-name buffer))
  9049. (message "Buffer is already a clone, not making another one")
  9050. ;; we also do not modify visibility in this case
  9051. buffer)
  9052. (t ; make a new indirect buffer for displaying the link
  9053. (let* ((bn (buffer-name buffer))
  9054. (ibn (concat bn "(Clone)"))
  9055. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  9056. (with-current-buffer ib (org-overview))
  9057. ib))))
  9058. (defun org-do-occur (regexp &optional cleanup)
  9059. "Call the Emacs command `occur'.
  9060. If CLEANUP is non-nil, remove the printout of the regular expression
  9061. in the *Occur* buffer. This is useful if the regex is long and not useful
  9062. to read."
  9063. (occur regexp)
  9064. (when cleanup
  9065. (let ((cwin (selected-window)) win beg end)
  9066. (when (setq win (get-buffer-window "*Occur*"))
  9067. (select-window win))
  9068. (goto-char (point-min))
  9069. (when (re-search-forward "match[a-z]+" nil t)
  9070. (setq beg (match-end 0))
  9071. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  9072. (setq end (1- (match-beginning 0)))))
  9073. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  9074. (goto-char (point-min))
  9075. (select-window cwin))))
  9076. ;;; The mark ring for links jumps
  9077. (defvar org-mark-ring nil
  9078. "Mark ring for positions before jumps in Org-mode.")
  9079. (defvar org-mark-ring-last-goto nil
  9080. "Last position in the mark ring used to go back.")
  9081. ;; Fill and close the ring
  9082. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  9083. (loop for i from 1 to org-mark-ring-length do
  9084. (push (make-marker) org-mark-ring))
  9085. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  9086. org-mark-ring)
  9087. (defun org-mark-ring-push (&optional pos buffer)
  9088. "Put the current position or POS into the mark ring and rotate it."
  9089. (interactive)
  9090. (setq pos (or pos (point)))
  9091. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  9092. (move-marker (car org-mark-ring)
  9093. (or pos (point))
  9094. (or buffer (current-buffer)))
  9095. (message "%s"
  9096. (substitute-command-keys
  9097. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  9098. (defun org-mark-ring-goto (&optional n)
  9099. "Jump to the previous position in the mark ring.
  9100. With prefix arg N, jump back that many stored positions. When
  9101. called several times in succession, walk through the entire ring.
  9102. Org-mode commands jumping to a different position in the current file,
  9103. or to another Org-mode file, automatically push the old position
  9104. onto the ring."
  9105. (interactive "p")
  9106. (let (p m)
  9107. (if (eq last-command this-command)
  9108. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  9109. (setq p org-mark-ring))
  9110. (setq org-mark-ring-last-goto p)
  9111. (setq m (car p))
  9112. (org-pop-to-buffer-same-window (marker-buffer m))
  9113. (goto-char m)
  9114. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  9115. (defun org-remove-angle-brackets (s)
  9116. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  9117. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  9118. s)
  9119. (defun org-add-angle-brackets (s)
  9120. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  9121. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  9122. s)
  9123. (defun org-remove-double-quotes (s)
  9124. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  9125. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  9126. s)
  9127. ;;; Following specific links
  9128. (defun org-follow-timestamp-link ()
  9129. (cond
  9130. ((org-at-date-range-p t)
  9131. (let ((org-agenda-start-on-weekday)
  9132. (t1 (match-string 1))
  9133. (t2 (match-string 2)))
  9134. (setq t1 (time-to-days (org-time-string-to-time t1))
  9135. t2 (time-to-days (org-time-string-to-time t2)))
  9136. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  9137. ((org-at-timestamp-p t)
  9138. (org-agenda-list nil (time-to-days (org-time-string-to-time
  9139. (substring (match-string 1) 0 10)))
  9140. 1))
  9141. (t (error "This should not happen"))))
  9142. ;;; Following file links
  9143. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  9144. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  9145. (declare-function mailcap-mime-info
  9146. "mailcap" (string &optional request no-decode))
  9147. (defvar org-wait nil)
  9148. (defun org-open-file (path &optional in-emacs line search)
  9149. "Open the file at PATH.
  9150. First, this expands any special file name abbreviations. Then the
  9151. configuration variable `org-file-apps' is checked if it contains an
  9152. entry for this file type, and if yes, the corresponding command is launched.
  9153. If no application is found, Emacs simply visits the file.
  9154. With optional prefix argument IN-EMACS, Emacs will visit the file.
  9155. With a double \\[universal-argument] \\[universal-argument] \
  9156. prefix arg, Org tries to avoid opening in Emacs
  9157. and to use an external application to visit the file.
  9158. Optional LINE specifies a line to go to, optional SEARCH a string
  9159. to search for. If LINE or SEARCH is given, the file will be
  9160. opened in Emacs, unless an entry from org-file-apps that makes
  9161. use of groups in a regexp matches.
  9162. If you want to change the way frames are used when following a
  9163. link, please customize `org-link-frame-setup'.
  9164. If the file does not exist, an error is thrown."
  9165. (let* ((file (if (equal path "")
  9166. buffer-file-name
  9167. (substitute-in-file-name (expand-file-name path))))
  9168. (file-apps (append org-file-apps (org-default-apps)))
  9169. (apps (org-remove-if
  9170. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9171. (apps-dlink (org-remove-if-not
  9172. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9173. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  9174. (dirp (if remp nil (file-directory-p file)))
  9175. (file (if (and dirp org-open-directory-means-index-dot-org)
  9176. (concat (file-name-as-directory file) "index.org")
  9177. file))
  9178. (a-m-a-p (assq 'auto-mode apps))
  9179. (dfile (downcase file))
  9180. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  9181. (link (cond ((and (eq line nil)
  9182. (eq search nil))
  9183. file)
  9184. (line
  9185. (concat file "::" (number-to-string line)))
  9186. (search
  9187. (concat file "::" search))))
  9188. (dlink (downcase link))
  9189. (old-buffer (current-buffer))
  9190. (old-pos (point))
  9191. (old-mode major-mode)
  9192. ext cmd link-match-data)
  9193. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  9194. (setq ext (match-string 1 dfile))
  9195. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  9196. (setq ext (match-string 1 dfile))))
  9197. (cond
  9198. ((member in-emacs '((16) system))
  9199. (setq cmd (cdr (assoc 'system apps))))
  9200. (in-emacs (setq cmd 'emacs))
  9201. (t
  9202. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  9203. (and dirp (cdr (assoc 'directory apps)))
  9204. ; first, try matching against apps-dlink
  9205. ; if we get a match here, store the match data for later
  9206. (let ((match (assoc-default dlink apps-dlink
  9207. 'string-match)))
  9208. (if match
  9209. (progn (setq link-match-data (match-data))
  9210. match)
  9211. (progn (setq in-emacs (or in-emacs line search))
  9212. nil))) ; if we have no match in apps-dlink,
  9213. ; always open the file in emacs if line or search
  9214. ; is given (for backwards compatibility)
  9215. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  9216. 'string-match)
  9217. (cdr (assoc ext apps))
  9218. (cdr (assoc t apps))))))
  9219. (when (eq cmd 'system)
  9220. (setq cmd (cdr (assoc 'system apps))))
  9221. (when (eq cmd 'default)
  9222. (setq cmd (cdr (assoc t apps))))
  9223. (when (eq cmd 'mailcap)
  9224. (require 'mailcap)
  9225. (mailcap-parse-mailcaps)
  9226. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  9227. (command (mailcap-mime-info mime-type)))
  9228. (if (stringp command)
  9229. (setq cmd command)
  9230. (setq cmd 'emacs))))
  9231. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  9232. (not (file-exists-p file))
  9233. (not org-open-non-existing-files))
  9234. (error "No such file: %s" file))
  9235. (cond
  9236. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  9237. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  9238. (while (string-match "['\"]%s['\"]" cmd)
  9239. (setq cmd (replace-match "%s" t t cmd)))
  9240. (while (string-match "%s" cmd)
  9241. (setq cmd (replace-match
  9242. (save-match-data
  9243. (shell-quote-argument
  9244. (convert-standard-filename file)))
  9245. t t cmd)))
  9246. ;; Replace "%1", "%2" etc. in command with group matches from regex
  9247. (save-match-data
  9248. (let ((match-index 1)
  9249. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  9250. (set-match-data link-match-data)
  9251. (while (<= match-index number-of-groups)
  9252. (let ((regex (concat "%" (number-to-string match-index)))
  9253. (replace-with (match-string match-index dlink)))
  9254. (while (string-match regex cmd)
  9255. (setq cmd (replace-match replace-with t t cmd))))
  9256. (setq match-index (+ match-index 1)))))
  9257. (save-window-excursion
  9258. (start-process-shell-command cmd nil cmd)
  9259. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  9260. ))
  9261. ((or (stringp cmd)
  9262. (eq cmd 'emacs))
  9263. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  9264. (widen)
  9265. (if line (org-goto-line line)
  9266. (if search (org-link-search search))))
  9267. ((consp cmd)
  9268. (let ((file (convert-standard-filename file)))
  9269. (save-match-data
  9270. (set-match-data link-match-data)
  9271. (eval cmd))))
  9272. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  9273. (and (eq major-mode 'org-mode) (eq old-mode 'org-mode)
  9274. (or (not (equal old-buffer (current-buffer)))
  9275. (not (equal old-pos (point))))
  9276. (org-mark-ring-push old-pos old-buffer))))
  9277. (defun org-file-apps-entry-match-against-dlink-p (entry)
  9278. "This function returns non-nil if `entry' uses a regular
  9279. expression which should be matched against the whole link by
  9280. org-open-file.
  9281. It assumes that is the case when the entry uses a regular
  9282. expression which has at least one grouping construct and the
  9283. action is either a lisp form or a command string containing
  9284. '%1', i.e. using at least one subexpression match as a
  9285. parameter."
  9286. (let ((selector (car entry))
  9287. (action (cdr entry)))
  9288. (if (stringp selector)
  9289. (and (> (regexp-opt-depth selector) 0)
  9290. (or (and (stringp action)
  9291. (string-match "%[0-9]" action))
  9292. (consp action)))
  9293. nil)))
  9294. (defun org-default-apps ()
  9295. "Return the default applications for this operating system."
  9296. (cond
  9297. ((eq system-type 'darwin)
  9298. org-file-apps-defaults-macosx)
  9299. ((eq system-type 'windows-nt)
  9300. org-file-apps-defaults-windowsnt)
  9301. (t org-file-apps-defaults-gnu)))
  9302. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  9303. "Convert extensions to regular expressions in the cars of LIST.
  9304. Also, weed out any non-string entries, because the return value is used
  9305. only for regexp matching.
  9306. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  9307. point to the symbol `emacs', indicating that the file should
  9308. be opened in Emacs."
  9309. (append
  9310. (delq nil
  9311. (mapcar (lambda (x)
  9312. (if (not (stringp (car x)))
  9313. nil
  9314. (if (string-match "\\W" (car x))
  9315. x
  9316. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  9317. list))
  9318. (if add-auto-mode
  9319. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  9320. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  9321. (defun org-file-remote-p (file)
  9322. "Test whether FILE specifies a location on a remote system.
  9323. Return non-nil if the location is indeed remote.
  9324. For example, the filename \"/user@host:/foo\" specifies a location
  9325. on the system \"/user@host:\"."
  9326. (cond ((fboundp 'file-remote-p)
  9327. (file-remote-p file))
  9328. ((fboundp 'tramp-handle-file-remote-p)
  9329. (tramp-handle-file-remote-p file))
  9330. ((and (boundp 'ange-ftp-name-format)
  9331. (string-match (car ange-ftp-name-format) file))
  9332. t)
  9333. (t nil)))
  9334. ;;;; Refiling
  9335. (defun org-get-org-file ()
  9336. "Read a filename, with default directory `org-directory'."
  9337. (let ((default (or org-default-notes-file remember-data-file)))
  9338. (read-file-name (format "File name [%s]: " default)
  9339. (file-name-as-directory org-directory)
  9340. default)))
  9341. (defun org-notes-order-reversed-p ()
  9342. "Check if the current file should receive notes in reversed order."
  9343. (cond
  9344. ((not org-reverse-note-order) nil)
  9345. ((eq t org-reverse-note-order) t)
  9346. ((not (listp org-reverse-note-order)) nil)
  9347. (t (catch 'exit
  9348. (let ((all org-reverse-note-order)
  9349. entry)
  9350. (while (setq entry (pop all))
  9351. (if (string-match (car entry) buffer-file-name)
  9352. (throw 'exit (cdr entry))))
  9353. nil)))))
  9354. (defvar org-refile-target-table nil
  9355. "The list of refile targets, created by `org-refile'.")
  9356. (defvar org-agenda-new-buffers nil
  9357. "Buffers created to visit agenda files.")
  9358. (defvar org-refile-cache nil
  9359. "Cache for refile targets.")
  9360. (defvar org-refile-markers nil
  9361. "All the markers used for caching refile locations.")
  9362. (defun org-refile-marker (pos)
  9363. "Get a new refile marker, but only if caching is in use."
  9364. (if (not org-refile-use-cache)
  9365. pos
  9366. (let ((m (make-marker)))
  9367. (move-marker m pos)
  9368. (push m org-refile-markers)
  9369. m)))
  9370. (defun org-refile-cache-clear ()
  9371. "Clear the refile cache and disable all the markers."
  9372. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  9373. (setq org-refile-markers nil)
  9374. (setq org-refile-cache nil)
  9375. (message "Refile cache has been cleared"))
  9376. (defun org-refile-cache-check-set (set)
  9377. "Check if all the markers in the cache still have live buffers."
  9378. (let (marker)
  9379. (catch 'exit
  9380. (while (and set (setq marker (nth 3 (pop set))))
  9381. ;; if org-refile-use-outline-path is 'file, marker may be nil
  9382. (when (and marker (null (marker-buffer marker)))
  9383. (message "not found") (sit-for 3)
  9384. (throw 'exit nil)))
  9385. t)))
  9386. (defun org-refile-cache-put (set &rest identifiers)
  9387. "Push the refile targets SET into the cache, under IDENTIFIERS."
  9388. (let* ((key (sha1 (prin1-to-string identifiers)))
  9389. (entry (assoc key org-refile-cache)))
  9390. (if entry
  9391. (setcdr entry set)
  9392. (push (cons key set) org-refile-cache))))
  9393. (defun org-refile-cache-get (&rest identifiers)
  9394. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  9395. (cond
  9396. ((not org-refile-cache) nil)
  9397. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  9398. (t
  9399. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  9400. org-refile-cache))))
  9401. (and set (org-refile-cache-check-set set) set)))))
  9402. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  9403. "Produce a table with refile targets."
  9404. (let ((case-fold-search nil)
  9405. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  9406. (entries (or org-refile-targets '((nil . (:level . 1)))))
  9407. targets tgs txt re files f desc descre fast-path-p level pos0)
  9408. (message "Getting targets...")
  9409. (with-current-buffer (or default-buffer (current-buffer))
  9410. (while (setq entry (pop entries))
  9411. (setq files (car entry) desc (cdr entry))
  9412. (setq fast-path-p nil)
  9413. (cond
  9414. ((null files) (setq files (list (current-buffer))))
  9415. ((eq files 'org-agenda-files)
  9416. (setq files (org-agenda-files 'unrestricted)))
  9417. ((and (symbolp files) (fboundp files))
  9418. (setq files (funcall files)))
  9419. ((and (symbolp files) (boundp files))
  9420. (setq files (symbol-value files))))
  9421. (if (stringp files) (setq files (list files)))
  9422. (cond
  9423. ((eq (car desc) :tag)
  9424. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  9425. ((eq (car desc) :todo)
  9426. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  9427. ((eq (car desc) :regexp)
  9428. (setq descre (cdr desc)))
  9429. ((eq (car desc) :level)
  9430. (setq descre (concat "^\\*\\{" (number-to-string
  9431. (if org-odd-levels-only
  9432. (1- (* 2 (cdr desc)))
  9433. (cdr desc)))
  9434. "\\}[ \t]")))
  9435. ((eq (car desc) :maxlevel)
  9436. (setq fast-path-p t)
  9437. (setq descre (concat "^\\*\\{1," (number-to-string
  9438. (if org-odd-levels-only
  9439. (1- (* 2 (cdr desc)))
  9440. (cdr desc)))
  9441. "\\}[ \t]")))
  9442. (t (error "Bad refiling target description %s" desc)))
  9443. (while (setq f (pop files))
  9444. (with-current-buffer
  9445. (if (bufferp f) f (org-get-agenda-file-buffer f))
  9446. (or
  9447. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  9448. (progn
  9449. (if (bufferp f) (setq f (buffer-file-name
  9450. (buffer-base-buffer f))))
  9451. (setq f (and f (expand-file-name f)))
  9452. (if (eq org-refile-use-outline-path 'file)
  9453. (push (list (file-name-nondirectory f) f nil nil) tgs))
  9454. (save-excursion
  9455. (save-restriction
  9456. (widen)
  9457. (goto-char (point-min))
  9458. (while (re-search-forward descre nil t)
  9459. (goto-char (setq pos0 (point-at-bol)))
  9460. (catch 'next
  9461. (when org-refile-target-verify-function
  9462. (save-match-data
  9463. (or (funcall org-refile-target-verify-function)
  9464. (throw 'next t))))
  9465. (when (and (looking-at org-complex-heading-regexp)
  9466. (not (member (match-string 4) excluded-entries))
  9467. (match-string 4))
  9468. (setq level (org-reduced-level
  9469. (- (match-end 1) (match-beginning 1)))
  9470. txt (org-link-display-format (match-string 4))
  9471. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  9472. re (format org-complex-heading-regexp-format
  9473. (regexp-quote (match-string 4))))
  9474. (when org-refile-use-outline-path
  9475. (setq txt (mapconcat
  9476. 'org-protect-slash
  9477. (append
  9478. (if (eq org-refile-use-outline-path
  9479. 'file)
  9480. (list (file-name-nondirectory
  9481. (buffer-file-name
  9482. (buffer-base-buffer))))
  9483. (if (eq org-refile-use-outline-path
  9484. 'full-file-path)
  9485. (list (buffer-file-name
  9486. (buffer-base-buffer)))))
  9487. (org-get-outline-path fast-path-p
  9488. level txt)
  9489. (list txt))
  9490. "/")))
  9491. (push (list txt f re (org-refile-marker (point)))
  9492. tgs)))
  9493. (when (= (point) pos0)
  9494. ;; verification function has not moved point
  9495. (goto-char (point-at-eol))))))))
  9496. (when org-refile-use-cache
  9497. (org-refile-cache-put tgs (buffer-file-name) descre))
  9498. (setq targets (append tgs targets))
  9499. ))))
  9500. (message "Getting targets...done")
  9501. (nreverse targets)))
  9502. (defun org-protect-slash (s)
  9503. (while (string-match "/" s)
  9504. (setq s (replace-match "\\" t t s)))
  9505. s)
  9506. (defvar org-olpa (make-vector 20 nil))
  9507. (defun org-get-outline-path (&optional fastp level heading)
  9508. "Return the outline path to the current entry, as a list.
  9509. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  9510. routine which makes outline path derivations for an entire file,
  9511. avoiding backtracing. Refile target collection makes use of that."
  9512. (if fastp
  9513. (progn
  9514. (if (> level 19)
  9515. (error "Outline path failure, more than 19 levels"))
  9516. (loop for i from level upto 19 do
  9517. (aset org-olpa i nil))
  9518. (prog1
  9519. (delq nil (append org-olpa nil))
  9520. (aset org-olpa level heading)))
  9521. (let (rtn case-fold-search)
  9522. (save-excursion
  9523. (save-restriction
  9524. (widen)
  9525. (while (org-up-heading-safe)
  9526. (when (looking-at org-complex-heading-regexp)
  9527. (push (org-match-string-no-properties 4) rtn)))
  9528. rtn)))))
  9529. (defun org-format-outline-path (path &optional width prefix)
  9530. "Format the outline path PATH for display.
  9531. Width is the maximum number of characters that is available.
  9532. Prefix is a prefix to be included in the returned string,
  9533. such as the file name."
  9534. (setq width (or width 79))
  9535. (if prefix (setq width (- width (length prefix))))
  9536. (if (not path)
  9537. (or prefix "")
  9538. (let* ((nsteps (length path))
  9539. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  9540. (maxwidth (if (<= total-width width)
  9541. 10000 ;; everything fits
  9542. ;; we need to shorten the level headings
  9543. (/ (- width nsteps) nsteps)))
  9544. (org-odd-levels-only nil)
  9545. (n 0)
  9546. (total (1+ (length prefix))))
  9547. (setq maxwidth (max maxwidth 10))
  9548. (concat prefix
  9549. (mapconcat
  9550. (lambda (h)
  9551. (setq n (1+ n))
  9552. (if (and (= n nsteps) (< maxwidth 10000))
  9553. (setq maxwidth (- total-width total)))
  9554. (if (< (length h) maxwidth)
  9555. (progn (setq total (+ total (length h) 1)) h)
  9556. (setq h (substring h 0 (- maxwidth 2))
  9557. total (+ total maxwidth 1))
  9558. (if (string-match "[ \t]+\\'" h)
  9559. (setq h (substring h 0 (match-beginning 0))))
  9560. (setq h (concat h "..")))
  9561. (org-add-props h nil 'face
  9562. (nth (% (1- n) org-n-level-faces)
  9563. org-level-faces))
  9564. h)
  9565. path "/")))))
  9566. (defun org-display-outline-path (&optional file current)
  9567. "Display the current outline path in the echo area."
  9568. (interactive "P")
  9569. (let* ((bfn (buffer-file-name (buffer-base-buffer)))
  9570. (case-fold-search nil)
  9571. (path (and (eq major-mode 'org-mode) (org-get-outline-path))))
  9572. (if current (setq path (append path
  9573. (save-excursion
  9574. (org-back-to-heading t)
  9575. (if (looking-at org-complex-heading-regexp)
  9576. (list (match-string 4)))))))
  9577. (message "%s"
  9578. (org-format-outline-path
  9579. path
  9580. (1- (frame-width))
  9581. (and file bfn (concat (file-name-nondirectory bfn) "/"))))))
  9582. (defvar org-refile-history nil
  9583. "History for refiling operations.")
  9584. (defvar org-after-refile-insert-hook nil
  9585. "Hook run after `org-refile' has inserted its stuff at the new location.
  9586. Note that this is still *before* the stuff will be removed from
  9587. the *old* location.")
  9588. (defvar org-capture-last-stored-marker)
  9589. (defun org-refile (&optional goto default-buffer rfloc)
  9590. "Move the entry or entries at point to another heading.
  9591. The list of target headings is compiled using the information in
  9592. `org-refile-targets', which see.
  9593. At the target location, the entry is filed as a subitem of the target
  9594. heading. Depending on `org-reverse-note-order', the new subitem will
  9595. either be the first or the last subitem.
  9596. If there is an active region, all entries in that region will be moved.
  9597. However, the region must fulfill the requirement that the first heading
  9598. is the first one sets the top-level of the moved text - at most siblings
  9599. below it are allowed.
  9600. With prefix arg GOTO, the command will only visit the target location
  9601. and not actually move anything.
  9602. With a double prefix arg \\[universal-argument] \\[universal-argument], \
  9603. go to the location where the last refiling operation has put the subtree.
  9604. With a prefix argument of `2', refile to the running clock.
  9605. RFLOC can be a refile location obtained in a different way.
  9606. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  9607. If you are using target caching (see `org-refile-use-cache'),
  9608. You have to clear the target cache in order to find new targets.
  9609. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  9610. prefix argument (`C-u C-u C-u C-c C-w')."
  9611. (interactive "P")
  9612. (if (member goto '(0 (64)))
  9613. (org-refile-cache-clear)
  9614. (let* ((cbuf (current-buffer))
  9615. (regionp (org-region-active-p))
  9616. (region-start (and regionp (region-beginning)))
  9617. (region-end (and regionp (region-end)))
  9618. (region-length (and regionp (- region-end region-start)))
  9619. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  9620. pos it nbuf file re level reversed)
  9621. (setq last-command nil)
  9622. (when regionp
  9623. (goto-char region-start)
  9624. (or (bolp) (goto-char (point-at-bol)))
  9625. (setq region-start (point))
  9626. (unless (or (org-kill-is-subtree-p
  9627. (buffer-substring region-start region-end))
  9628. (prog1 org-refile-active-region-within-subtree
  9629. (org-toggle-heading)))
  9630. (error "The region is not a (sequence of) subtree(s)")))
  9631. (if (equal goto '(16))
  9632. (org-refile-goto-last-stored)
  9633. (when (or
  9634. (and (equal goto 2)
  9635. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  9636. (prog1
  9637. (setq it (list (or org-clock-heading "running clock")
  9638. (buffer-file-name
  9639. (marker-buffer org-clock-hd-marker))
  9640. ""
  9641. (marker-position org-clock-hd-marker)))
  9642. (setq goto nil)))
  9643. (setq it (or rfloc
  9644. (let (heading-text)
  9645. (save-excursion
  9646. (unless goto
  9647. (org-back-to-heading t)
  9648. (setq heading-text
  9649. (nth 4 (org-heading-components))))
  9650. (org-refile-get-location
  9651. (cond (goto "Goto")
  9652. (regionp "Refile region to")
  9653. (t (concat "Refile subtree \""
  9654. heading-text "\" to")))
  9655. default-buffer
  9656. (and (not (equal '(4) goto))
  9657. org-refile-allow-creating-parent-nodes)
  9658. goto))))))
  9659. (setq file (nth 1 it)
  9660. re (nth 2 it)
  9661. pos (nth 3 it))
  9662. (if (and (not goto)
  9663. pos
  9664. (equal (buffer-file-name) file)
  9665. (if regionp
  9666. (and (>= pos region-start)
  9667. (<= pos region-end))
  9668. (and (>= pos (point))
  9669. (< pos (save-excursion
  9670. (org-end-of-subtree t t))))))
  9671. (error "Cannot refile to position inside the tree or region"))
  9672. (setq nbuf (or (find-buffer-visiting file)
  9673. (find-file-noselect file)))
  9674. (if goto
  9675. (progn
  9676. (org-pop-to-buffer-same-window nbuf)
  9677. (goto-char pos)
  9678. (org-show-context 'org-goto))
  9679. (if regionp
  9680. (progn
  9681. (org-kill-new (buffer-substring region-start region-end))
  9682. (org-save-markers-in-region region-start region-end))
  9683. (org-copy-subtree 1 nil t))
  9684. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  9685. (find-file-noselect file)))
  9686. (setq reversed (org-notes-order-reversed-p))
  9687. (save-excursion
  9688. (save-restriction
  9689. (widen)
  9690. (if pos
  9691. (progn
  9692. (goto-char pos)
  9693. (looking-at org-outline-regexp)
  9694. (setq level (org-get-valid-level (funcall outline-level) 1))
  9695. (goto-char
  9696. (if reversed
  9697. (or (outline-next-heading) (point-max))
  9698. (or (save-excursion (org-get-next-sibling))
  9699. (org-end-of-subtree t t)
  9700. (point-max)))))
  9701. (setq level 1)
  9702. (if (not reversed)
  9703. (goto-char (point-max))
  9704. (goto-char (point-min))
  9705. (or (outline-next-heading) (goto-char (point-max)))))
  9706. (if (not (bolp)) (newline))
  9707. (org-paste-subtree level)
  9708. (when org-log-refile
  9709. (org-add-log-setup 'refile nil nil 'findpos
  9710. org-log-refile)
  9711. (unless (eq org-log-refile 'note)
  9712. (save-excursion (org-add-log-note))))
  9713. (and org-auto-align-tags (org-set-tags nil t))
  9714. (bookmark-set "org-refile-last-stored")
  9715. ;; If we are refiling for capture, make sure that the
  9716. ;; last-capture pointers point here
  9717. (when (org-bound-and-true-p org-refile-for-capture)
  9718. (bookmark-set "org-capture-last-stored-marker")
  9719. (move-marker org-capture-last-stored-marker (point)))
  9720. (if (fboundp 'deactivate-mark) (deactivate-mark))
  9721. (run-hooks 'org-after-refile-insert-hook))))
  9722. (if regionp
  9723. (delete-region (point) (+ (point) region-length))
  9724. (org-cut-subtree))
  9725. (when (featurep 'org-inlinetask)
  9726. (org-inlinetask-remove-END-maybe))
  9727. (setq org-markers-to-move nil)
  9728. (message "Refiled to \"%s\" in file %s" (car it) file)))))))
  9729. (defun org-refile-goto-last-stored ()
  9730. "Go to the location where the last refile was stored."
  9731. (interactive)
  9732. (bookmark-jump "org-refile-last-stored")
  9733. (message "This is the location of the last refile"))
  9734. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  9735. no-exclude)
  9736. "Prompt the user for a refile location, using PROMPT.
  9737. PROMPT should not be suffixed with a colon and a space, because
  9738. this function appends the default value from
  9739. `org-refile-history' automatically, if that is not empty.
  9740. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  9741. this is used for the GOTO interface."
  9742. (let ((org-refile-targets org-refile-targets)
  9743. (org-refile-use-outline-path org-refile-use-outline-path)
  9744. excluded-entries)
  9745. (when (and (eq major-mode 'org-mode)
  9746. (not org-refile-use-cache)
  9747. (not no-exclude))
  9748. (org-map-tree
  9749. (lambda()
  9750. (setq excluded-entries
  9751. (append excluded-entries (list (org-get-heading t t)))))))
  9752. (setq org-refile-target-table
  9753. (org-refile-get-targets default-buffer excluded-entries)))
  9754. (unless org-refile-target-table
  9755. (error "No refile targets"))
  9756. (let* ((prompt (concat prompt
  9757. (and (car org-refile-history)
  9758. (concat " (default " (car org-refile-history) ")"))
  9759. ": "))
  9760. (cbuf (current-buffer))
  9761. (partial-completion-mode nil)
  9762. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  9763. (cfunc (if (and org-refile-use-outline-path
  9764. org-outline-path-complete-in-steps)
  9765. 'org-olpath-completing-read
  9766. 'org-icompleting-read))
  9767. (extra (if org-refile-use-outline-path "/" ""))
  9768. (filename (and cfn (expand-file-name cfn)))
  9769. (tbl (mapcar
  9770. (lambda (x)
  9771. (if (and (not (member org-refile-use-outline-path
  9772. '(file full-file-path)))
  9773. (not (equal filename (nth 1 x))))
  9774. (cons (concat (car x) extra " ("
  9775. (file-name-nondirectory (nth 1 x)) ")")
  9776. (cdr x))
  9777. (cons (concat (car x) extra) (cdr x))))
  9778. org-refile-target-table))
  9779. (completion-ignore-case t)
  9780. pa answ parent-target child parent old-hist)
  9781. (setq old-hist org-refile-history)
  9782. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  9783. nil 'org-refile-history (car org-refile-history)))
  9784. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  9785. (org-refile-check-position pa)
  9786. (if pa
  9787. (progn
  9788. (when (or (not org-refile-history)
  9789. (not (eq old-hist org-refile-history))
  9790. (not (equal (car pa) (car org-refile-history))))
  9791. (setq org-refile-history
  9792. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  9793. org-refile-history
  9794. (cdr org-refile-history))))
  9795. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  9796. (pop org-refile-history)))
  9797. pa)
  9798. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  9799. (progn
  9800. (setq parent (match-string 1 answ)
  9801. child (match-string 2 answ))
  9802. (setq parent-target (or (assoc parent tbl)
  9803. (assoc (concat parent "/") tbl)))
  9804. (when (and parent-target
  9805. (or (eq new-nodes t)
  9806. (and (eq new-nodes 'confirm)
  9807. (y-or-n-p (format "Create new node \"%s\"? "
  9808. child)))))
  9809. (org-refile-new-child parent-target child)))
  9810. (error "Invalid target location")))))
  9811. (declare-function org-string-nw-p "org-macs.el" (s))
  9812. (defun org-refile-check-position (refile-pointer)
  9813. "Check if the refile pointer matches the readline to which it points."
  9814. (let* ((file (nth 1 refile-pointer))
  9815. (re (nth 2 refile-pointer))
  9816. (pos (nth 3 refile-pointer))
  9817. buffer)
  9818. (when (org-string-nw-p re)
  9819. (setq buffer (if (markerp pos)
  9820. (marker-buffer pos)
  9821. (or (find-buffer-visiting file)
  9822. (find-file-noselect file))))
  9823. (with-current-buffer buffer
  9824. (save-excursion
  9825. (save-restriction
  9826. (widen)
  9827. (goto-char pos)
  9828. (beginning-of-line 1)
  9829. (unless (org-looking-at-p re)
  9830. (error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling"))))))))
  9831. (defun org-refile-new-child (parent-target child)
  9832. "Use refile target PARENT-TARGET to add new CHILD below it."
  9833. (unless parent-target
  9834. (error "Cannot find parent for new node"))
  9835. (let ((file (nth 1 parent-target))
  9836. (pos (nth 3 parent-target))
  9837. level)
  9838. (with-current-buffer (or (find-buffer-visiting file)
  9839. (find-file-noselect file))
  9840. (save-excursion
  9841. (save-restriction
  9842. (widen)
  9843. (if pos
  9844. (goto-char pos)
  9845. (goto-char (point-max))
  9846. (if (not (bolp)) (newline)))
  9847. (when (looking-at org-outline-regexp)
  9848. (setq level (funcall outline-level))
  9849. (org-end-of-subtree t t))
  9850. (org-back-over-empty-lines)
  9851. (insert "\n" (make-string
  9852. (if pos (org-get-valid-level level 1) 1) ?*)
  9853. " " child "\n")
  9854. (beginning-of-line 0)
  9855. (list (concat (car parent-target) "/" child) file "" (point)))))))
  9856. (defun org-olpath-completing-read (prompt collection &rest args)
  9857. "Read an outline path like a file name."
  9858. (let ((thetable collection)
  9859. (org-completion-use-ido nil) ; does not work with ido.
  9860. (org-completion-use-iswitchb nil)) ; or iswitchb
  9861. (apply
  9862. 'org-icompleting-read prompt
  9863. (lambda (string predicate &optional flag)
  9864. (let (rtn r f (l (length string)))
  9865. (cond
  9866. ((eq flag nil)
  9867. ;; try completion
  9868. (try-completion string thetable))
  9869. ((eq flag t)
  9870. ;; all-completions
  9871. (setq rtn (all-completions string thetable predicate))
  9872. (mapcar
  9873. (lambda (x)
  9874. (setq r (substring x l))
  9875. (if (string-match " ([^)]*)$" x)
  9876. (setq f (match-string 0 x))
  9877. (setq f ""))
  9878. (if (string-match "/" r)
  9879. (concat string (substring r 0 (match-end 0)) f)
  9880. x))
  9881. rtn))
  9882. ((eq flag 'lambda)
  9883. ;; exact match?
  9884. (assoc string thetable)))
  9885. ))
  9886. args)))
  9887. ;;;; Dynamic blocks
  9888. (defun org-find-dblock (name)
  9889. "Find the first dynamic block with name NAME in the buffer.
  9890. If not found, stay at current position and return nil."
  9891. (let (pos)
  9892. (save-excursion
  9893. (goto-char (point-min))
  9894. (setq pos (and (re-search-forward (concat "^[ \t]*#\\+BEGIN:[ \t]+" name "\\>")
  9895. nil t)
  9896. (match-beginning 0))))
  9897. (if pos (goto-char pos))
  9898. pos))
  9899. (defconst org-dblock-start-re
  9900. "^[ \t]*#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  9901. "Matches the start line of a dynamic block, with parameters.")
  9902. (defconst org-dblock-end-re "^[ \t]*#\\+END\\([: \t\r\n]\\|$\\)"
  9903. "Matches the end of a dynamic block.")
  9904. (defun org-create-dblock (plist)
  9905. "Create a dynamic block section, with parameters taken from PLIST.
  9906. PLIST must contain a :name entry which is used as name of the block."
  9907. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  9908. (end-of-line 1)
  9909. (newline))
  9910. (let ((col (current-column))
  9911. (name (plist-get plist :name)))
  9912. (insert "#+BEGIN: " name)
  9913. (while plist
  9914. (if (eq (car plist) :name)
  9915. (setq plist (cddr plist))
  9916. (insert " " (prin1-to-string (pop plist)))))
  9917. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  9918. (beginning-of-line -2)))
  9919. (defun org-prepare-dblock ()
  9920. "Prepare dynamic block for refresh.
  9921. This empties the block, puts the cursor at the insert position and returns
  9922. the property list including an extra property :name with the block name."
  9923. (unless (looking-at org-dblock-start-re)
  9924. (error "Not at a dynamic block"))
  9925. (let* ((begdel (1+ (match-end 0)))
  9926. (name (org-no-properties (match-string 1)))
  9927. (params (append (list :name name)
  9928. (read (concat "(" (match-string 3) ")")))))
  9929. (save-excursion
  9930. (beginning-of-line 1)
  9931. (skip-chars-forward " \t")
  9932. (setq params (plist-put params :indentation-column (current-column))))
  9933. (unless (re-search-forward org-dblock-end-re nil t)
  9934. (error "Dynamic block not terminated"))
  9935. (setq params
  9936. (append params
  9937. (list :content (buffer-substring
  9938. begdel (match-beginning 0)))))
  9939. (delete-region begdel (match-beginning 0))
  9940. (goto-char begdel)
  9941. (open-line 1)
  9942. params))
  9943. (defun org-map-dblocks (&optional command)
  9944. "Apply COMMAND to all dynamic blocks in the current buffer.
  9945. If COMMAND is not given, use `org-update-dblock'."
  9946. (let ((cmd (or command 'org-update-dblock)))
  9947. (save-excursion
  9948. (goto-char (point-min))
  9949. (while (re-search-forward org-dblock-start-re nil t)
  9950. (goto-char (match-beginning 0))
  9951. (save-excursion
  9952. (condition-case nil
  9953. (funcall cmd)
  9954. (error (message "Error during update of dynamic block"))))
  9955. (unless (re-search-forward org-dblock-end-re nil t)
  9956. (error "Dynamic block not terminated"))))))
  9957. (defun org-dblock-update (&optional arg)
  9958. "User command for updating dynamic blocks.
  9959. Update the dynamic block at point. With prefix ARG, update all dynamic
  9960. blocks in the buffer."
  9961. (interactive "P")
  9962. (if arg
  9963. (org-update-all-dblocks)
  9964. (or (looking-at org-dblock-start-re)
  9965. (org-beginning-of-dblock))
  9966. (org-update-dblock)))
  9967. (defun org-update-dblock ()
  9968. "Update the dynamic block at point.
  9969. This means to empty the block, parse for parameters and then call
  9970. the correct writing function."
  9971. (interactive)
  9972. (save-window-excursion
  9973. (let* ((pos (point))
  9974. (line (org-current-line))
  9975. (params (org-prepare-dblock))
  9976. (name (plist-get params :name))
  9977. (indent (plist-get params :indentation-column))
  9978. (cmd (intern (concat "org-dblock-write:" name))))
  9979. (message "Updating dynamic block `%s' at line %d..." name line)
  9980. (funcall cmd params)
  9981. (message "Updating dynamic block `%s' at line %d...done" name line)
  9982. (goto-char pos)
  9983. (when (and indent (> indent 0))
  9984. (setq indent (make-string indent ?\ ))
  9985. (save-excursion
  9986. (org-beginning-of-dblock)
  9987. (forward-line 1)
  9988. (while (not (looking-at org-dblock-end-re))
  9989. (insert indent)
  9990. (beginning-of-line 2))
  9991. (when (looking-at org-dblock-end-re)
  9992. (and (looking-at "[ \t]+")
  9993. (replace-match ""))
  9994. (insert indent)))))))
  9995. (defun org-beginning-of-dblock ()
  9996. "Find the beginning of the dynamic block at point.
  9997. Error if there is no such block at point."
  9998. (let ((pos (point))
  9999. beg)
  10000. (end-of-line 1)
  10001. (if (and (re-search-backward org-dblock-start-re nil t)
  10002. (setq beg (match-beginning 0))
  10003. (re-search-forward org-dblock-end-re nil t)
  10004. (> (match-end 0) pos))
  10005. (goto-char beg)
  10006. (goto-char pos)
  10007. (error "Not in a dynamic block"))))
  10008. ;;;###autoload
  10009. (defun org-update-all-dblocks ()
  10010. "Update all dynamic blocks in the buffer.
  10011. This function can be used in a hook."
  10012. (interactive)
  10013. (when (eq major-mode 'org-mode)
  10014. (org-map-dblocks 'org-update-dblock)))
  10015. ;;;; Completion
  10016. (defconst org-additional-option-like-keywords
  10017. '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML:"
  10018. "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook:"
  10019. "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:"
  10020. "LATEX_CLASS:" "LATEX_CLASS_OPTIONS:" "ATTR_LaTeX:"
  10021. "BEGIN:" "END:"
  10022. "ORGTBL" "TBLFM:" "TBLNAME:"
  10023. "BEGIN_EXAMPLE" "END_EXAMPLE"
  10024. "BEGIN_QUOTE" "END_QUOTE"
  10025. "BEGIN_VERSE" "END_VERSE"
  10026. "BEGIN_CENTER" "END_CENTER"
  10027. "BEGIN_SRC" "END_SRC"
  10028. "BEGIN_RESULT" "END_RESULT"
  10029. "NAME:" "RESULTS:"
  10030. "HEADER:" "HEADERS:"
  10031. "CATEGORY:" "COLUMNS:" "PROPERTY:"
  10032. "CAPTION:" "LABEL:"
  10033. "SETUPFILE:"
  10034. "INCLUDE:"
  10035. "BIND:"
  10036. "MACRO:"))
  10037. (defcustom org-structure-template-alist
  10038. '(
  10039. ("s" "#+BEGIN_SRC ?\n\n#+END_SRC"
  10040. "<src lang=\"?\">\n\n</src>")
  10041. ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE"
  10042. "<example>\n?\n</example>")
  10043. ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE"
  10044. "<quote>\n?\n</quote>")
  10045. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE"
  10046. "<verse>\n?\n</verse>")
  10047. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER"
  10048. "<center>\n?\n</center>")
  10049. ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX"
  10050. "<literal style=\"latex\">\n?\n</literal>")
  10051. ("L" "#+LaTeX: "
  10052. "<literal style=\"latex\">?</literal>")
  10053. ("h" "#+BEGIN_HTML\n?\n#+END_HTML"
  10054. "<literal style=\"html\">\n?\n</literal>")
  10055. ("H" "#+HTML: "
  10056. "<literal style=\"html\">?</literal>")
  10057. ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII")
  10058. ("A" "#+ASCII: ")
  10059. ("i" "#+INDEX: ?"
  10060. "#+INDEX: ?")
  10061. ("I" "#+INCLUDE %file ?"
  10062. "<include file=%file markup=\"?\">")
  10063. )
  10064. "Structure completion elements.
  10065. This is a list of abbreviation keys and values. The value gets inserted
  10066. if you type `<' followed by the key and then press the completion key,
  10067. usually `M-TAB'. %file will be replaced by a file name after prompting
  10068. for the file using completion. The cursor will be placed at the position
  10069. of the `?` in the template.
  10070. There are two templates for each key, the first uses the original Org syntax,
  10071. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  10072. the default when the /org-mtags.el/ module has been loaded. See also the
  10073. variable `org-mtags-prefer-muse-templates'."
  10074. :group 'org-completion
  10075. :type '(repeat
  10076. (string :tag "Key")
  10077. (string :tag "Template")
  10078. (string :tag "Muse Template")))
  10079. (defun org-try-structure-completion ()
  10080. "Try to complete a structure template before point.
  10081. This looks for strings like \"<e\" on an otherwise empty line and
  10082. expands them."
  10083. (let ((l (buffer-substring (point-at-bol) (point)))
  10084. a)
  10085. (when (and (looking-at "[ \t]*$")
  10086. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  10087. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  10088. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  10089. (match-beginning 1)) a)
  10090. t)))
  10091. (defun org-complete-expand-structure-template (start cell)
  10092. "Expand a structure template."
  10093. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  10094. (rpl (nth (if musep 2 1) cell))
  10095. (ind ""))
  10096. (delete-region start (point))
  10097. (when (string-match "\\`#\\+" rpl)
  10098. (cond
  10099. ((bolp))
  10100. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  10101. (setq ind (buffer-substring (point-at-bol) (point))))
  10102. (t (newline))))
  10103. (setq start (point))
  10104. (if (string-match "%file" rpl)
  10105. (setq rpl (replace-match
  10106. (concat
  10107. "\""
  10108. (save-match-data
  10109. (abbreviate-file-name (read-file-name "Include file: ")))
  10110. "\"")
  10111. t t rpl)))
  10112. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  10113. (concat "\n" ind)))
  10114. (insert rpl)
  10115. (if (re-search-backward "\\?" start t) (delete-char 1))))
  10116. ;;;; TODO, DEADLINE, Comments
  10117. (defun org-toggle-comment ()
  10118. "Change the COMMENT state of an entry."
  10119. (interactive)
  10120. (save-excursion
  10121. (org-back-to-heading)
  10122. (let (case-fold-search)
  10123. (cond
  10124. ((looking-at (format org-heading-keyword-regexp-format
  10125. org-comment-string))
  10126. (goto-char (match-end 1))
  10127. (looking-at (concat " +" org-comment-string))
  10128. (replace-match "" t t)
  10129. (when (eolp) (insert " ")))
  10130. ((looking-at org-outline-regexp)
  10131. (goto-char (match-end 0))
  10132. (insert org-comment-string " "))))))
  10133. (defvar org-last-todo-state-is-todo nil
  10134. "This is non-nil when the last TODO state change led to a TODO state.
  10135. If the last change removed the TODO tag or switched to DONE, then
  10136. this is nil.")
  10137. (defvar org-setting-tags nil) ; dynamically skipped
  10138. (defvar org-todo-setup-filter-hook nil
  10139. "Hook for functions that pre-filter todo specs.
  10140. Each function takes a todo spec and returns either nil or the spec
  10141. transformed into canonical form." )
  10142. (defvar org-todo-get-default-hook nil
  10143. "Hook for functions that get a default item for todo.
  10144. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  10145. nil or a string to be used for the todo mark." )
  10146. (defvar org-agenda-headline-snapshot-before-repeat)
  10147. (defun org-current-effective-time ()
  10148. "Return current time adjusted for `org-extend-today-until' variable"
  10149. (let* ((ct (org-current-time))
  10150. (dct (decode-time ct))
  10151. (ct1
  10152. (if (and org-use-effective-time
  10153. (< (nth 2 dct) org-extend-today-until))
  10154. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct))
  10155. ct)))
  10156. ct1))
  10157. (defun org-todo-yesterday (&optional arg)
  10158. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  10159. (interactive "P")
  10160. (if (eq major-mode 'org-agenda-mode)
  10161. (apply 'org-agenda-todo-yesterday arg)
  10162. (let* ((hour (third (decode-time
  10163. (org-current-time))))
  10164. (org-extend-today-until (1+ hour)))
  10165. (org-todo arg))))
  10166. (defun org-todo (&optional arg)
  10167. "Change the TODO state of an item.
  10168. The state of an item is given by a keyword at the start of the heading,
  10169. like
  10170. *** TODO Write paper
  10171. *** DONE Call mom
  10172. The different keywords are specified in the variable `org-todo-keywords'.
  10173. By default the available states are \"TODO\" and \"DONE\".
  10174. So for this example: when the item starts with TODO, it is changed to DONE.
  10175. When it starts with DONE, the DONE is removed. And when neither TODO nor
  10176. DONE are present, add TODO at the beginning of the heading.
  10177. With \\[universal-argument] prefix arg, use completion to determine the new \
  10178. state.
  10179. With numeric prefix arg, switch to that state.
  10180. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  10181. keywords (nextset).
  10182. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  10183. With a numeric prefix arg of 0, inhibit note taking for the change.
  10184. For calling through lisp, arg is also interpreted in the following way:
  10185. 'none -> empty state
  10186. \"\"(empty string) -> switch to empty state
  10187. 'done -> switch to DONE
  10188. 'nextset -> switch to the next set of keywords
  10189. 'previousset -> switch to the previous set of keywords
  10190. \"WAITING\" -> switch to the specified keyword, but only if it
  10191. really is a member of `org-todo-keywords'."
  10192. (interactive "P")
  10193. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  10194. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  10195. 'region-start-level 'region))
  10196. org-loop-over-headlines-in-active-region)
  10197. (org-map-entries
  10198. `(org-todo ,arg)
  10199. org-loop-over-headlines-in-active-region
  10200. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  10201. (if (equal arg '(16)) (setq arg 'nextset))
  10202. (let ((org-blocker-hook org-blocker-hook)
  10203. (case-fold-search nil))
  10204. (when (equal arg '(64))
  10205. (setq arg nil org-blocker-hook nil))
  10206. (when (and org-blocker-hook
  10207. (or org-inhibit-blocking
  10208. (org-entry-get nil "NOBLOCKING")))
  10209. (setq org-blocker-hook nil))
  10210. (save-excursion
  10211. (catch 'exit
  10212. (org-back-to-heading t)
  10213. (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  10214. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  10215. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  10216. (let* ((match-data (match-data))
  10217. (startpos (point-at-bol))
  10218. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  10219. (org-log-done org-log-done)
  10220. (org-log-repeat org-log-repeat)
  10221. (org-todo-log-states org-todo-log-states)
  10222. (org-inhibit-logging
  10223. (if (equal arg 0)
  10224. (progn (setq arg nil) 'note) org-inhibit-logging))
  10225. (this (match-string 1))
  10226. (hl-pos (match-beginning 0))
  10227. (head (org-get-todo-sequence-head this))
  10228. (ass (assoc head org-todo-kwd-alist))
  10229. (interpret (nth 1 ass))
  10230. (done-word (nth 3 ass))
  10231. (final-done-word (nth 4 ass))
  10232. (org-last-state (or this ""))
  10233. (completion-ignore-case t)
  10234. (member (member this org-todo-keywords-1))
  10235. (tail (cdr member))
  10236. (org-state (cond
  10237. ((and org-todo-key-trigger
  10238. (or (and (equal arg '(4))
  10239. (eq org-use-fast-todo-selection 'prefix))
  10240. (and (not arg) org-use-fast-todo-selection
  10241. (not (eq org-use-fast-todo-selection
  10242. 'prefix)))))
  10243. ;; Use fast selection
  10244. (org-fast-todo-selection))
  10245. ((and (equal arg '(4))
  10246. (or (not org-use-fast-todo-selection)
  10247. (not org-todo-key-trigger)))
  10248. ;; Read a state with completion
  10249. (org-icompleting-read
  10250. "State: " (mapcar (lambda(x) (list x))
  10251. org-todo-keywords-1)
  10252. nil t))
  10253. ((eq arg 'right)
  10254. (if this
  10255. (if tail (car tail) nil)
  10256. (car org-todo-keywords-1)))
  10257. ((eq arg 'left)
  10258. (if (equal member org-todo-keywords-1)
  10259. nil
  10260. (if this
  10261. (nth (- (length org-todo-keywords-1)
  10262. (length tail) 2)
  10263. org-todo-keywords-1)
  10264. (org-last org-todo-keywords-1))))
  10265. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  10266. (setq arg nil))) ; hack to fall back to cycling
  10267. (arg
  10268. ;; user or caller requests a specific state
  10269. (cond
  10270. ((equal arg "") nil)
  10271. ((eq arg 'none) nil)
  10272. ((eq arg 'done) (or done-word (car org-done-keywords)))
  10273. ((eq arg 'nextset)
  10274. (or (car (cdr (member head org-todo-heads)))
  10275. (car org-todo-heads)))
  10276. ((eq arg 'previousset)
  10277. (let ((org-todo-heads (reverse org-todo-heads)))
  10278. (or (car (cdr (member head org-todo-heads)))
  10279. (car org-todo-heads))))
  10280. ((car (member arg org-todo-keywords-1)))
  10281. ((stringp arg)
  10282. (error "State `%s' not valid in this file" arg))
  10283. ((nth (1- (prefix-numeric-value arg))
  10284. org-todo-keywords-1))))
  10285. ((null member) (or head (car org-todo-keywords-1)))
  10286. ((equal this final-done-word) nil) ;; -> make empty
  10287. ((null tail) nil) ;; -> first entry
  10288. ((memq interpret '(type priority))
  10289. (if (eq this-command last-command)
  10290. (car tail)
  10291. (if (> (length tail) 0)
  10292. (or done-word (car org-done-keywords))
  10293. nil)))
  10294. (t
  10295. (car tail))))
  10296. (org-state (or
  10297. (run-hook-with-args-until-success
  10298. 'org-todo-get-default-hook org-state org-last-state)
  10299. org-state))
  10300. (next (if org-state (concat " " org-state " ") " "))
  10301. (change-plist (list :type 'todo-state-change :from this :to org-state
  10302. :position startpos))
  10303. dolog now-done-p)
  10304. (when org-blocker-hook
  10305. (setq org-last-todo-state-is-todo
  10306. (not (member this org-done-keywords)))
  10307. (unless (save-excursion
  10308. (save-match-data
  10309. (org-with-wide-buffer
  10310. (run-hook-with-args-until-failure
  10311. 'org-blocker-hook change-plist))))
  10312. (if (org-called-interactively-p 'interactive)
  10313. (error "TODO state change from %s to %s blocked" this org-state)
  10314. ;; fail silently
  10315. (message "TODO state change from %s to %s blocked" this org-state)
  10316. (throw 'exit nil))))
  10317. (store-match-data match-data)
  10318. (replace-match next t t)
  10319. (unless (pos-visible-in-window-p hl-pos)
  10320. (message "TODO state changed to %s" (org-trim next)))
  10321. (unless head
  10322. (setq head (org-get-todo-sequence-head org-state)
  10323. ass (assoc head org-todo-kwd-alist)
  10324. interpret (nth 1 ass)
  10325. done-word (nth 3 ass)
  10326. final-done-word (nth 4 ass)))
  10327. (when (memq arg '(nextset previousset))
  10328. (message "Keyword-Set %d/%d: %s"
  10329. (- (length org-todo-sets) -1
  10330. (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
  10331. (length org-todo-sets)
  10332. (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
  10333. (setq org-last-todo-state-is-todo
  10334. (not (member org-state org-done-keywords)))
  10335. (setq now-done-p (and (member org-state org-done-keywords)
  10336. (not (member this org-done-keywords))))
  10337. (and logging (org-local-logging logging))
  10338. (when (and (or org-todo-log-states org-log-done)
  10339. (not (eq org-inhibit-logging t))
  10340. (not (memq arg '(nextset previousset))))
  10341. ;; we need to look at recording a time and note
  10342. (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
  10343. (nth 2 (assoc this org-todo-log-states))))
  10344. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  10345. (setq dolog 'time))
  10346. (when (and org-state
  10347. (member org-state org-not-done-keywords)
  10348. (not (member this org-not-done-keywords)))
  10349. ;; This is now a todo state and was not one before
  10350. ;; If there was a CLOSED time stamp, get rid of it.
  10351. (org-add-planning-info nil nil 'closed))
  10352. (when (and now-done-p org-log-done)
  10353. ;; It is now done, and it was not done before
  10354. (org-add-planning-info 'closed (org-current-effective-time))
  10355. (if (and (not dolog) (eq 'note org-log-done))
  10356. (org-add-log-setup 'done org-state this 'findpos 'note)))
  10357. (when (and org-state dolog)
  10358. ;; This is a non-nil state, and we need to log it
  10359. (org-add-log-setup 'state org-state this 'findpos dolog)))
  10360. ;; Fixup tag positioning
  10361. (org-todo-trigger-tag-changes org-state)
  10362. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  10363. (when org-provide-todo-statistics
  10364. (org-update-parent-todo-statistics))
  10365. (run-hooks 'org-after-todo-state-change-hook)
  10366. (if (and arg (not (member org-state org-done-keywords)))
  10367. (setq head (org-get-todo-sequence-head org-state)))
  10368. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  10369. ;; Do we need to trigger a repeat?
  10370. (when now-done-p
  10371. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  10372. ;; This is for the agenda, take a snapshot of the headline.
  10373. (save-match-data
  10374. (setq org-agenda-headline-snapshot-before-repeat
  10375. (org-get-heading))))
  10376. (org-auto-repeat-maybe org-state))
  10377. ;; Fixup cursor location if close to the keyword
  10378. (if (and (outline-on-heading-p)
  10379. (not (bolp))
  10380. (save-excursion (beginning-of-line 1)
  10381. (looking-at org-todo-line-regexp))
  10382. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  10383. (progn
  10384. (goto-char (or (match-end 2) (match-end 1)))
  10385. (and (looking-at " ") (just-one-space))))
  10386. (when org-trigger-hook
  10387. (save-excursion
  10388. (run-hook-with-args 'org-trigger-hook change-plist)))))))))
  10389. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  10390. "Block turning an entry into a TODO, using the hierarchy.
  10391. This checks whether the current task should be blocked from state
  10392. changes. Such blocking occurs when:
  10393. 1. The task has children which are not all in a completed state.
  10394. 2. A task has a parent with the property :ORDERED:, and there
  10395. are siblings prior to the current task with incomplete
  10396. status.
  10397. 3. The parent of the task is blocked because it has siblings that should
  10398. be done first, or is child of a block grandparent TODO entry."
  10399. (if (not org-enforce-todo-dependencies)
  10400. t ; if locally turned off don't block
  10401. (catch 'dont-block
  10402. ;; If this is not a todo state change, or if this entry is already DONE,
  10403. ;; do not block
  10404. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10405. (member (plist-get change-plist :from)
  10406. (cons 'done org-done-keywords))
  10407. (member (plist-get change-plist :to)
  10408. (cons 'todo org-not-done-keywords))
  10409. (not (plist-get change-plist :to)))
  10410. (throw 'dont-block t))
  10411. ;; If this task has children, and any are undone, it's blocked
  10412. (save-excursion
  10413. (org-back-to-heading t)
  10414. (let ((this-level (funcall outline-level)))
  10415. (outline-next-heading)
  10416. (let ((child-level (funcall outline-level)))
  10417. (while (and (not (eobp))
  10418. (> child-level this-level))
  10419. ;; this todo has children, check whether they are all
  10420. ;; completed
  10421. (if (and (not (org-entry-is-done-p))
  10422. (org-entry-is-todo-p))
  10423. (throw 'dont-block nil))
  10424. (outline-next-heading)
  10425. (setq child-level (funcall outline-level))))))
  10426. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  10427. ;; any previous siblings are undone, it's blocked
  10428. (save-excursion
  10429. (org-back-to-heading t)
  10430. (let* ((pos (point))
  10431. (parent-pos (and (org-up-heading-safe) (point))))
  10432. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10433. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10434. (forward-line 1)
  10435. (re-search-forward org-not-done-heading-regexp pos t))
  10436. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  10437. ;; Search further up the hierarchy, to see if an ancestor is blocked
  10438. (while t
  10439. (goto-char parent-pos)
  10440. (if (not (looking-at org-not-done-heading-regexp))
  10441. (throw 'dont-block t)) ; do not block, parent is not a TODO
  10442. (setq pos (point))
  10443. (setq parent-pos (and (org-up-heading-safe) (point)))
  10444. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10445. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10446. (forward-line 1)
  10447. (re-search-forward org-not-done-heading-regexp pos t))
  10448. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  10449. (defcustom org-track-ordered-property-with-tag nil
  10450. "Should the ORDERED property also be shown as a tag?
  10451. The ORDERED property decides if an entry should require subtasks to be
  10452. completed in sequence. Since a property is not very visible, setting
  10453. this option means that toggling the ORDERED property with the command
  10454. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  10455. not relevant for the behavior, but it makes things more visible.
  10456. Note that toggling the tag with tags commands will not change the property
  10457. and therefore not influence behavior!
  10458. This can be t, meaning the tag ORDERED should be used, It can also be a
  10459. string to select a different tag for this task."
  10460. :group 'org-todo
  10461. :type '(choice
  10462. (const :tag "No tracking" nil)
  10463. (const :tag "Track with ORDERED tag" t)
  10464. (string :tag "Use other tag")))
  10465. (defun org-toggle-ordered-property ()
  10466. "Toggle the ORDERED property of the current entry.
  10467. For better visibility, you can track the value of this property with a tag.
  10468. See variable `org-track-ordered-property-with-tag'."
  10469. (interactive)
  10470. (let* ((t1 org-track-ordered-property-with-tag)
  10471. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  10472. (save-excursion
  10473. (org-back-to-heading)
  10474. (if (org-entry-get nil "ORDERED")
  10475. (progn
  10476. (org-delete-property "ORDERED")
  10477. (and tag (org-toggle-tag tag 'off))
  10478. (message "Subtasks can be completed in arbitrary order"))
  10479. (org-entry-put nil "ORDERED" "t")
  10480. (and tag (org-toggle-tag tag 'on))
  10481. (message "Subtasks must be completed in sequence")))))
  10482. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  10483. (defun org-block-todo-from-checkboxes (change-plist)
  10484. "Block turning an entry into a TODO, using checkboxes.
  10485. This checks whether the current task should be blocked from state
  10486. changes because there are unchecked boxes in this entry."
  10487. (if (not org-enforce-todo-checkbox-dependencies)
  10488. t ; if locally turned off don't block
  10489. (catch 'dont-block
  10490. ;; If this is not a todo state change, or if this entry is already DONE,
  10491. ;; do not block
  10492. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10493. (member (plist-get change-plist :from)
  10494. (cons 'done org-done-keywords))
  10495. (member (plist-get change-plist :to)
  10496. (cons 'todo org-not-done-keywords))
  10497. (not (plist-get change-plist :to)))
  10498. (throw 'dont-block t))
  10499. ;; If this task has checkboxes that are not checked, it's blocked
  10500. (save-excursion
  10501. (org-back-to-heading t)
  10502. (let ((beg (point)) end)
  10503. (outline-next-heading)
  10504. (setq end (point))
  10505. (goto-char beg)
  10506. (if (org-list-search-forward
  10507. (concat (org-item-beginning-re)
  10508. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  10509. "\\[[- ]\\]")
  10510. end t)
  10511. (progn
  10512. (if (boundp 'org-blocked-by-checkboxes)
  10513. (setq org-blocked-by-checkboxes t))
  10514. (throw 'dont-block nil)))))
  10515. t))) ; do not block
  10516. (defun org-entry-blocked-p ()
  10517. "Is the current entry blocked?"
  10518. (if (org-entry-get nil "NOBLOCKING")
  10519. nil ;; Never block this entry
  10520. (not
  10521. (run-hook-with-args-until-failure
  10522. 'org-blocker-hook
  10523. (list :type 'todo-state-change
  10524. :position (point)
  10525. :from 'todo
  10526. :to 'done)))))
  10527. (defun org-update-statistics-cookies (all)
  10528. "Update the statistics cookie, either from TODO or from checkboxes.
  10529. This should be called with the cursor in a line with a statistics cookie."
  10530. (interactive "P")
  10531. (if all
  10532. (progn
  10533. (org-update-checkbox-count 'all)
  10534. (org-map-entries 'org-update-parent-todo-statistics))
  10535. (if (not (org-at-heading-p))
  10536. (org-update-checkbox-count)
  10537. (let ((pos (move-marker (make-marker) (point)))
  10538. end l1 l2)
  10539. (ignore-errors (org-back-to-heading t))
  10540. (if (not (org-at-heading-p))
  10541. (org-update-checkbox-count)
  10542. (setq l1 (org-outline-level))
  10543. (setq end (save-excursion
  10544. (outline-next-heading)
  10545. (if (org-at-heading-p) (setq l2 (org-outline-level)))
  10546. (point)))
  10547. (if (and (save-excursion
  10548. (re-search-forward
  10549. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  10550. (not (save-excursion (re-search-forward
  10551. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  10552. (org-update-checkbox-count)
  10553. (if (and l2 (> l2 l1))
  10554. (progn
  10555. (goto-char end)
  10556. (org-update-parent-todo-statistics))
  10557. (goto-char pos)
  10558. (beginning-of-line 1)
  10559. (while (re-search-forward
  10560. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  10561. (point-at-eol) t)
  10562. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  10563. (goto-char pos)
  10564. (move-marker pos nil)))))
  10565. (defvar org-entry-property-inherited-from) ;; defined below
  10566. (defun org-update-parent-todo-statistics ()
  10567. "Update any statistics cookie in the parent of the current headline.
  10568. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  10569. the entire subtree and this will travel up the hierarchy and update
  10570. statistics everywhere."
  10571. (let* ((prop (save-excursion (org-up-heading-safe)
  10572. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  10573. (recursive (or (not org-hierarchical-todo-statistics)
  10574. (and prop (string-match "\\<recursive\\>" prop))))
  10575. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  10576. 0))
  10577. (first t)
  10578. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  10579. level ltoggle l1 new ndel
  10580. (cnt-all 0) (cnt-done 0) is-percent kwd
  10581. checkbox-beg ov ovs ove cookie-present)
  10582. (catch 'exit
  10583. (save-excursion
  10584. (beginning-of-line 1)
  10585. (setq ltoggle (funcall outline-level))
  10586. ;; Three situations are to consider:
  10587. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  10588. ;; to the top-level ancestor on the headline;
  10589. ;; 2. If parent has "recursive" property, repeat up to the
  10590. ;; headline setting that property, taking inheritance into
  10591. ;; account;
  10592. ;; 3. Else, move up to direct parent and proceed only once.
  10593. (while (and (setq level (org-up-heading-safe))
  10594. (or recursive first)
  10595. (>= (point) lim))
  10596. (setq first nil cookie-present nil)
  10597. (unless (and level
  10598. (not (string-match
  10599. "\\<checkbox\\>"
  10600. (downcase (or (org-entry-get nil "COOKIE_DATA")
  10601. "")))))
  10602. (throw 'exit nil))
  10603. (while (re-search-forward box-re (point-at-eol) t)
  10604. (setq cnt-all 0 cnt-done 0 cookie-present t)
  10605. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  10606. (save-match-data
  10607. (unless (outline-next-heading) (throw 'exit nil))
  10608. (while (and (looking-at org-complex-heading-regexp)
  10609. (> (setq l1 (length (match-string 1))) level))
  10610. (setq kwd (and (or recursive (= l1 ltoggle))
  10611. (match-string 2)))
  10612. (if (or (eq org-provide-todo-statistics 'all-headlines)
  10613. (and (listp org-provide-todo-statistics)
  10614. (or (member kwd org-provide-todo-statistics)
  10615. (member kwd org-done-keywords))))
  10616. (setq cnt-all (1+ cnt-all))
  10617. (if (eq org-provide-todo-statistics t)
  10618. (and kwd (setq cnt-all (1+ cnt-all)))))
  10619. (and (member kwd org-done-keywords)
  10620. (setq cnt-done (1+ cnt-done)))
  10621. (outline-next-heading)))
  10622. (setq new
  10623. (if is-percent
  10624. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  10625. (format "[%d/%d]" cnt-done cnt-all))
  10626. ndel (- (match-end 0) checkbox-beg))
  10627. ;; handle overlays when updating cookie from column view
  10628. (when (setq ov (car (overlays-at checkbox-beg)))
  10629. (setq ovs (overlay-start ov) ove (overlay-end ov))
  10630. (delete-overlay ov))
  10631. (goto-char checkbox-beg)
  10632. (insert new)
  10633. (delete-region (point) (+ (point) ndel))
  10634. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  10635. (when ov (move-overlay ov ovs ove)))
  10636. (when cookie-present
  10637. (run-hook-with-args 'org-after-todo-statistics-hook
  10638. cnt-done (- cnt-all cnt-done))))))
  10639. (run-hooks 'org-todo-statistics-hook)))
  10640. (defvar org-after-todo-statistics-hook nil
  10641. "Hook that is called after a TODO statistics cookie has been updated.
  10642. Each function is called with two arguments: the number of not-done entries
  10643. and the number of done entries.
  10644. For example, the following function, when added to this hook, will switch
  10645. an entry to DONE when all children are done, and back to TODO when new
  10646. entries are set to a TODO status. Note that this hook is only called
  10647. when there is a statistics cookie in the headline!
  10648. (defun org-summary-todo (n-done n-not-done)
  10649. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  10650. (let (org-log-done org-log-states) ; turn off logging
  10651. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  10652. ")
  10653. (defvar org-todo-statistics-hook nil
  10654. "Hook that is run whenever Org thinks TODO statistics should be updated.
  10655. This hook runs even if there is no statistics cookie present, in which case
  10656. `org-after-todo-statistics-hook' would not run.")
  10657. (defun org-todo-trigger-tag-changes (state)
  10658. "Apply the changes defined in `org-todo-state-tags-triggers'."
  10659. (let ((l org-todo-state-tags-triggers)
  10660. changes)
  10661. (when (or (not state) (equal state ""))
  10662. (setq changes (append changes (cdr (assoc "" l)))))
  10663. (when (and (stringp state) (> (length state) 0))
  10664. (setq changes (append changes (cdr (assoc state l)))))
  10665. (when (member state org-not-done-keywords)
  10666. (setq changes (append changes (cdr (assoc 'todo l)))))
  10667. (when (member state org-done-keywords)
  10668. (setq changes (append changes (cdr (assoc 'done l)))))
  10669. (dolist (c changes)
  10670. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  10671. (defun org-local-logging (value)
  10672. "Get logging settings from a property VALUE."
  10673. (let* (words w a)
  10674. ;; directly set the variables, they are already local.
  10675. (setq org-log-done nil
  10676. org-log-repeat nil
  10677. org-todo-log-states nil)
  10678. (setq words (org-split-string value))
  10679. (while (setq w (pop words))
  10680. (cond
  10681. ((setq a (assoc w org-startup-options))
  10682. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  10683. (set (nth 1 a) (nth 2 a))))
  10684. ((setq a (org-extract-log-state-settings w))
  10685. (and (member (car a) org-todo-keywords-1)
  10686. (push a org-todo-log-states)))))))
  10687. (defun org-get-todo-sequence-head (kwd)
  10688. "Return the head of the TODO sequence to which KWD belongs.
  10689. If KWD is not set, check if there is a text property remembering the
  10690. right sequence."
  10691. (let (p)
  10692. (cond
  10693. ((not kwd)
  10694. (or (get-text-property (point-at-bol) 'org-todo-head)
  10695. (progn
  10696. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  10697. nil (point-at-eol)))
  10698. (get-text-property p 'org-todo-head))))
  10699. ((not (member kwd org-todo-keywords-1))
  10700. (car org-todo-keywords-1))
  10701. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  10702. (defun org-fast-todo-selection ()
  10703. "Fast TODO keyword selection with single keys.
  10704. Returns the new TODO keyword, or nil if no state change should occur."
  10705. (let* ((fulltable org-todo-key-alist)
  10706. (done-keywords org-done-keywords) ;; needed for the faces.
  10707. (maxlen (apply 'max (mapcar
  10708. (lambda (x)
  10709. (if (stringp (car x)) (string-width (car x)) 0))
  10710. fulltable)))
  10711. (expert nil)
  10712. (fwidth (+ maxlen 3 1 3))
  10713. (ncol (/ (- (window-width) 4) fwidth))
  10714. tg cnt e c tbl
  10715. groups ingroup)
  10716. (save-excursion
  10717. (save-window-excursion
  10718. (if expert
  10719. (set-buffer (get-buffer-create " *Org todo*"))
  10720. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  10721. (erase-buffer)
  10722. (org-set-local 'org-done-keywords done-keywords)
  10723. (setq tbl fulltable cnt 0)
  10724. (while (setq e (pop tbl))
  10725. (cond
  10726. ((equal e '(:startgroup))
  10727. (push '() groups) (setq ingroup t)
  10728. (when (not (= cnt 0))
  10729. (setq cnt 0)
  10730. (insert "\n"))
  10731. (insert "{ "))
  10732. ((equal e '(:endgroup))
  10733. (setq ingroup nil cnt 0)
  10734. (insert "}\n"))
  10735. ((equal e '(:newline))
  10736. (when (not (= cnt 0))
  10737. (setq cnt 0)
  10738. (insert "\n")
  10739. (setq e (car tbl))
  10740. (while (equal (car tbl) '(:newline))
  10741. (insert "\n")
  10742. (setq tbl (cdr tbl)))))
  10743. (t
  10744. (setq tg (car e) c (cdr e))
  10745. (if ingroup (push tg (car groups)))
  10746. (setq tg (org-add-props tg nil 'face
  10747. (org-get-todo-face tg)))
  10748. (if (and (= cnt 0) (not ingroup)) (insert " "))
  10749. (insert "[" c "] " tg (make-string
  10750. (- fwidth 4 (length tg)) ?\ ))
  10751. (when (= (setq cnt (1+ cnt)) ncol)
  10752. (insert "\n")
  10753. (if ingroup (insert " "))
  10754. (setq cnt 0)))))
  10755. (insert "\n")
  10756. (goto-char (point-min))
  10757. (if (not expert) (org-fit-window-to-buffer))
  10758. (message "[a-z..]:Set [SPC]:clear")
  10759. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  10760. (cond
  10761. ((or (= c ?\C-g)
  10762. (and (= c ?q) (not (rassoc c fulltable))))
  10763. (setq quit-flag t))
  10764. ((= c ?\ ) nil)
  10765. ((setq e (rassoc c fulltable) tg (car e))
  10766. tg)
  10767. (t (setq quit-flag t)))))))
  10768. (defun org-entry-is-todo-p ()
  10769. (member (org-get-todo-state) org-not-done-keywords))
  10770. (defun org-entry-is-done-p ()
  10771. (member (org-get-todo-state) org-done-keywords))
  10772. (defun org-get-todo-state ()
  10773. (save-excursion
  10774. (org-back-to-heading t)
  10775. (and (looking-at org-todo-line-regexp)
  10776. (match-end 2)
  10777. (match-string 2))))
  10778. (defun org-at-date-range-p (&optional inactive-ok)
  10779. "Is the cursor inside a date range?"
  10780. (interactive)
  10781. (save-excursion
  10782. (catch 'exit
  10783. (let ((pos (point)))
  10784. (skip-chars-backward "^[<\r\n")
  10785. (skip-chars-backward "<[")
  10786. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  10787. (>= (match-end 0) pos)
  10788. (throw 'exit t))
  10789. (skip-chars-backward "^<[\r\n")
  10790. (skip-chars-backward "<[")
  10791. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  10792. (>= (match-end 0) pos)
  10793. (throw 'exit t)))
  10794. nil)))
  10795. (defun org-get-repeat (&optional tagline)
  10796. "Check if there is a deadline/schedule with repeater in this entry."
  10797. (save-match-data
  10798. (save-excursion
  10799. (org-back-to-heading t)
  10800. (and (re-search-forward (if tagline
  10801. (concat tagline "\\s-*" org-repeat-re)
  10802. org-repeat-re)
  10803. (org-entry-end-position) t)
  10804. (match-string-no-properties 1)))))
  10805. (defvar org-last-changed-timestamp)
  10806. (defvar org-last-inserted-timestamp)
  10807. (defvar org-log-post-message)
  10808. (defvar org-log-note-purpose)
  10809. (defvar org-log-note-how)
  10810. (defvar org-log-note-extra)
  10811. (defun org-auto-repeat-maybe (done-word)
  10812. "Check if the current headline contains a repeated deadline/schedule.
  10813. If yes, set TODO state back to what it was and change the base date
  10814. of repeating deadline/scheduled time stamps to new date.
  10815. This function is run automatically after each state change to a DONE state."
  10816. ;; last-state is dynamically scoped into this function
  10817. (let* ((repeat (org-get-repeat))
  10818. (aa (assoc org-last-state org-todo-kwd-alist))
  10819. (interpret (nth 1 aa))
  10820. (head (nth 2 aa))
  10821. (whata '(("d" . day) ("m" . month) ("y" . year)))
  10822. (msg "Entry repeats: ")
  10823. (org-log-done nil)
  10824. (org-todo-log-states nil)
  10825. re type n what ts time to-state)
  10826. (when repeat
  10827. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  10828. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  10829. org-todo-repeat-to-state))
  10830. (unless (and to-state (member to-state org-todo-keywords-1))
  10831. (setq to-state (if (eq interpret 'type) org-last-state head)))
  10832. (org-todo to-state)
  10833. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  10834. (org-entry-put nil "LAST_REPEAT" (format-time-string
  10835. (org-time-stamp-format t t))))
  10836. (when org-log-repeat
  10837. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  10838. (memq 'org-add-log-note post-command-hook))
  10839. ;; OK, we are already setup for some record
  10840. (if (eq org-log-repeat 'note)
  10841. ;; make sure we take a note, not only a time stamp
  10842. (setq org-log-note-how 'note))
  10843. ;; Set up for taking a record
  10844. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  10845. org-last-state
  10846. 'findpos org-log-repeat)))
  10847. (org-back-to-heading t)
  10848. (org-add-planning-info nil nil 'closed)
  10849. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  10850. org-deadline-time-regexp "\\)\\|\\("
  10851. org-ts-regexp "\\)"))
  10852. (while (re-search-forward
  10853. re (save-excursion (outline-next-heading) (point)) t)
  10854. (setq type (if (match-end 1) org-scheduled-string
  10855. (if (match-end 3) org-deadline-string "Plain:"))
  10856. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  10857. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  10858. (setq n (string-to-number (match-string 2 ts))
  10859. what (match-string 3 ts))
  10860. (if (equal what "w") (setq n (* n 7) what "d"))
  10861. ;; Preparation, see if we need to modify the start date for the change
  10862. (when (match-end 1)
  10863. (setq time (save-match-data (org-time-string-to-time ts)))
  10864. (cond
  10865. ((equal (match-string 1 ts) ".")
  10866. ;; Shift starting date to today
  10867. (org-timestamp-change
  10868. (- (org-today) (time-to-days time))
  10869. 'day))
  10870. ((equal (match-string 1 ts) "+")
  10871. (let ((nshiftmax 10) (nshift 0))
  10872. (while (or (= nshift 0)
  10873. (<= (time-to-days time)
  10874. (time-to-days (current-time))))
  10875. (when (= (incf nshift) nshiftmax)
  10876. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  10877. (error "Abort")))
  10878. (org-timestamp-change n (cdr (assoc what whata)))
  10879. (org-at-timestamp-p t)
  10880. (setq ts (match-string 1))
  10881. (setq time (save-match-data (org-time-string-to-time ts)))))
  10882. (org-timestamp-change (- n) (cdr (assoc what whata)))
  10883. ;; rematch, so that we have everything in place for the real shift
  10884. (org-at-timestamp-p t)
  10885. (setq ts (match-string 1))
  10886. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  10887. (org-timestamp-change n (cdr (assoc what whata)))
  10888. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  10889. (setq org-log-post-message msg)
  10890. (message "%s" msg))))
  10891. (defun org-show-todo-tree (arg)
  10892. "Make a compact tree which shows all headlines marked with TODO.
  10893. The tree will show the lines where the regexp matches, and all higher
  10894. headlines above the match.
  10895. With a \\[universal-argument] prefix, prompt for a regexp to match.
  10896. With a numeric prefix N, construct a sparse tree for the Nth element
  10897. of `org-todo-keywords-1'."
  10898. (interactive "P")
  10899. (let ((case-fold-search nil)
  10900. (kwd-re
  10901. (cond ((null arg) org-not-done-regexp)
  10902. ((equal arg '(4))
  10903. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  10904. (mapcar 'list org-todo-keywords-1))))
  10905. (concat "\\("
  10906. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  10907. "\\)\\>")))
  10908. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  10909. (regexp-quote (nth (1- (prefix-numeric-value arg))
  10910. org-todo-keywords-1)))
  10911. (t (error "Invalid prefix argument: %s" arg)))))
  10912. (message "%d TODO entries found"
  10913. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  10914. (defun org-deadline (&optional remove time)
  10915. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  10916. With argument REMOVE, remove any deadline from the item.
  10917. With argument TIME, set the deadline at the corresponding date. TIME
  10918. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  10919. (interactive "P")
  10920. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  10921. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  10922. 'region-start-level 'region))
  10923. org-loop-over-headlines-in-active-region)
  10924. (org-map-entries
  10925. `(org-deadline ',remove ,time)
  10926. org-loop-over-headlines-in-active-region
  10927. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  10928. (let* ((old-date (org-entry-get nil "DEADLINE"))
  10929. (repeater (and old-date
  10930. (string-match
  10931. "\\([.+-]+[0-9]+[dwmy]\\(?:[/ ][-+]?[0-9]+[dwmy]\\)?\\) ?"
  10932. old-date)
  10933. (match-string 1 old-date))))
  10934. (if remove
  10935. (progn
  10936. (when (and old-date org-log-redeadline)
  10937. (org-add-log-setup 'deldeadline nil old-date 'findpos
  10938. org-log-redeadline))
  10939. (org-remove-timestamp-with-keyword org-deadline-string)
  10940. (message "Item no longer has a deadline."))
  10941. (org-add-planning-info 'deadline time 'closed)
  10942. (when (and old-date org-log-redeadline
  10943. (not (equal old-date
  10944. (substring org-last-inserted-timestamp 1 -1))))
  10945. (org-add-log-setup 'redeadline nil old-date 'findpos
  10946. org-log-redeadline))
  10947. (when repeater
  10948. (save-excursion
  10949. (org-back-to-heading t)
  10950. (when (re-search-forward (concat org-deadline-string " "
  10951. org-last-inserted-timestamp)
  10952. (save-excursion
  10953. (outline-next-heading) (point)) t)
  10954. (goto-char (1- (match-end 0)))
  10955. (insert " " repeater)
  10956. (setq org-last-inserted-timestamp
  10957. (concat (substring org-last-inserted-timestamp 0 -1)
  10958. " " repeater
  10959. (substring org-last-inserted-timestamp -1))))))
  10960. (message "Deadline on %s" org-last-inserted-timestamp)))))
  10961. (defun org-schedule (&optional remove time)
  10962. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  10963. With argument REMOVE, remove any scheduling date from the item.
  10964. With argument TIME, scheduled at the corresponding date. TIME can
  10965. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  10966. (interactive "P")
  10967. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  10968. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  10969. 'region-start-level 'region))
  10970. org-loop-over-headlines-in-active-region)
  10971. (org-map-entries
  10972. `(org-schedule ',remove ,time)
  10973. org-loop-over-headlines-in-active-region
  10974. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  10975. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  10976. (repeater (and old-date
  10977. (string-match
  10978. "\\([.+-]+[0-9]+[dwmy]\\(?:[/ ][-+]?[0-9]+[dwmy]\\)?\\) ?"
  10979. old-date)
  10980. (match-string 1 old-date))))
  10981. (if remove
  10982. (progn
  10983. (when (and old-date org-log-reschedule)
  10984. (org-add-log-setup 'delschedule nil old-date 'findpos
  10985. org-log-reschedule))
  10986. (org-remove-timestamp-with-keyword org-scheduled-string)
  10987. (message "Item is no longer scheduled."))
  10988. (org-add-planning-info 'scheduled time 'closed)
  10989. (when (and old-date org-log-reschedule
  10990. (not (equal old-date
  10991. (substring org-last-inserted-timestamp 1 -1))))
  10992. (org-add-log-setup 'reschedule nil old-date 'findpos
  10993. org-log-reschedule))
  10994. (when repeater
  10995. (save-excursion
  10996. (org-back-to-heading t)
  10997. (when (re-search-forward (concat org-scheduled-string " "
  10998. org-last-inserted-timestamp)
  10999. (save-excursion
  11000. (outline-next-heading) (point)) t)
  11001. (goto-char (1- (match-end 0)))
  11002. (insert " " repeater)
  11003. (setq org-last-inserted-timestamp
  11004. (concat (substring org-last-inserted-timestamp 0 -1)
  11005. " " repeater
  11006. (substring org-last-inserted-timestamp -1))))))
  11007. (message "Scheduled to %s" org-last-inserted-timestamp)))))
  11008. (defun org-get-scheduled-time (pom &optional inherit)
  11009. "Get the scheduled time as a time tuple, of a format suitable
  11010. for calling org-schedule with, or if there is no scheduling,
  11011. returns nil."
  11012. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  11013. (when time
  11014. (apply 'encode-time (org-parse-time-string time)))))
  11015. (defun org-get-deadline-time (pom &optional inherit)
  11016. "Get the deadline as a time tuple, of a format suitable for
  11017. calling org-deadline with, or if there is no scheduling, returns
  11018. nil."
  11019. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  11020. (when time
  11021. (apply 'encode-time (org-parse-time-string time)))))
  11022. (defun org-remove-timestamp-with-keyword (keyword)
  11023. "Remove all time stamps with KEYWORD in the current entry."
  11024. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  11025. beg)
  11026. (save-excursion
  11027. (org-back-to-heading t)
  11028. (setq beg (point))
  11029. (outline-next-heading)
  11030. (while (re-search-backward re beg t)
  11031. (replace-match "")
  11032. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  11033. (equal (char-before) ?\ ))
  11034. (backward-delete-char 1)
  11035. (if (string-match "^[ \t]*$" (buffer-substring
  11036. (point-at-bol) (point-at-eol)))
  11037. (delete-region (point-at-bol)
  11038. (min (point-max) (1+ (point-at-eol))))))))))
  11039. (defun org-add-planning-info (what &optional time &rest remove)
  11040. "Insert new timestamp with keyword in the line directly after the headline.
  11041. WHAT indicates what kind of time stamp to add. TIME indicates the time to use.
  11042. If non is given, the user is prompted for a date.
  11043. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  11044. be removed."
  11045. (interactive)
  11046. (let (org-time-was-given org-end-time-was-given ts
  11047. end default-time default-input)
  11048. (catch 'exit
  11049. (when (and (memq what '(scheduled deadline))
  11050. (or (not time)
  11051. (and (stringp time)
  11052. (string-match "^[-+]+[0-9]" time))))
  11053. ;; Try to get a default date/time from existing timestamp
  11054. (save-excursion
  11055. (org-back-to-heading t)
  11056. (setq end (save-excursion (outline-next-heading) (point)))
  11057. (when (re-search-forward (if (eq what 'scheduled)
  11058. org-scheduled-time-regexp
  11059. org-deadline-time-regexp)
  11060. end t)
  11061. (setq ts (match-string 1)
  11062. default-time
  11063. (apply 'encode-time (org-parse-time-string ts))
  11064. default-input (and ts (org-get-compact-tod ts))))))
  11065. (when what
  11066. (setq time
  11067. (if (stringp time)
  11068. ;; This is a string (relative or absolute), set proper date
  11069. (apply 'encode-time
  11070. (org-read-date-analyze
  11071. time default-time (decode-time default-time)))
  11072. ;; If necessary, get the time from the user
  11073. (or time (org-read-date nil 'to-time nil nil
  11074. default-time default-input)))))
  11075. (when (and org-insert-labeled-timestamps-at-point
  11076. (member what '(scheduled deadline)))
  11077. (insert
  11078. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  11079. (org-insert-time-stamp time org-time-was-given
  11080. nil nil nil (list org-end-time-was-given))
  11081. (setq what nil))
  11082. (save-excursion
  11083. (save-restriction
  11084. (let (col list elt ts buffer-invisibility-spec)
  11085. (org-back-to-heading t)
  11086. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
  11087. (goto-char (match-end 1))
  11088. (setq col (current-column))
  11089. (goto-char (match-end 0))
  11090. (if (eobp) (insert "\n") (forward-char 1))
  11091. (when (and (not what)
  11092. (not (looking-at
  11093. (concat "[ \t]*"
  11094. org-keyword-time-not-clock-regexp))))
  11095. ;; Nothing to add, nothing to remove...... :-)
  11096. (throw 'exit nil))
  11097. (if (and (not (looking-at org-outline-regexp))
  11098. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  11099. "[^\r\n]*"))
  11100. (not (equal (match-string 1) org-clock-string)))
  11101. (narrow-to-region (match-beginning 0) (match-end 0))
  11102. (insert-before-markers "\n")
  11103. (backward-char 1)
  11104. (narrow-to-region (point) (point))
  11105. (and org-adapt-indentation (org-indent-to-column col)))
  11106. ;; Check if we have to remove something.
  11107. (setq list (cons what remove))
  11108. (while list
  11109. (setq elt (pop list))
  11110. (when (or (and (eq elt 'scheduled)
  11111. (re-search-forward org-scheduled-time-regexp nil t))
  11112. (and (eq elt 'deadline)
  11113. (re-search-forward org-deadline-time-regexp nil t))
  11114. (and (eq elt 'closed)
  11115. (re-search-forward org-closed-time-regexp nil t)))
  11116. (replace-match "")
  11117. (if (looking-at "--+<[^>]+>") (replace-match ""))))
  11118. (and (looking-at "[ \t]+") (replace-match ""))
  11119. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  11120. (when what
  11121. (insert
  11122. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  11123. (cond ((eq what 'scheduled) org-scheduled-string)
  11124. ((eq what 'deadline) org-deadline-string)
  11125. ((eq what 'closed) org-closed-string))
  11126. " ")
  11127. (setq ts (org-insert-time-stamp
  11128. time
  11129. (or org-time-was-given
  11130. (and (eq what 'closed) org-log-done-with-time))
  11131. (eq what 'closed)
  11132. nil nil (list org-end-time-was-given)))
  11133. (insert
  11134. (if (not (or (bolp) (eq (char-before) ?\ )
  11135. (memq (char-after) '(32 10))
  11136. (eobp))) " " ""))
  11137. (end-of-line 1))
  11138. (goto-char (point-min))
  11139. (widen)
  11140. (if (and (looking-at "[ \t]*\n")
  11141. (equal (char-before) ?\n))
  11142. (delete-region (1- (point)) (point-at-eol)))
  11143. ts))))))
  11144. (defvar org-log-note-marker (make-marker))
  11145. (defvar org-log-note-purpose nil)
  11146. (defvar org-log-note-state nil)
  11147. (defvar org-log-note-previous-state nil)
  11148. (defvar org-log-note-how nil)
  11149. (defvar org-log-note-extra nil)
  11150. (defvar org-log-note-window-configuration nil)
  11151. (defvar org-log-note-return-to (make-marker))
  11152. (defvar org-log-note-effective-time nil
  11153. "Remembered current time so that dynamically scoped
  11154. `org-extend-today-until' affects tha timestamps in state change
  11155. log")
  11156. (defvar org-log-post-message nil
  11157. "Message to be displayed after a log note has been stored.
  11158. The auto-repeater uses this.")
  11159. (defun org-add-note ()
  11160. "Add a note to the current entry.
  11161. This is done in the same way as adding a state change note."
  11162. (interactive)
  11163. (org-add-log-setup 'note nil nil 'findpos nil))
  11164. (defvar org-property-end-re)
  11165. (defun org-add-log-setup (&optional purpose state prev-state
  11166. findpos how extra)
  11167. "Set up the post command hook to take a note.
  11168. If this is about to TODO state change, the new state is expected in STATE.
  11169. When FINDPOS is non-nil, find the correct position for the note in
  11170. the current entry. If not, assume that it can be inserted at point.
  11171. HOW is an indicator what kind of note should be created.
  11172. EXTRA is additional text that will be inserted into the notes buffer."
  11173. (let* ((org-log-into-drawer (org-log-into-drawer))
  11174. (drawer (cond ((stringp org-log-into-drawer)
  11175. org-log-into-drawer)
  11176. (org-log-into-drawer "LOGBOOK")
  11177. (t nil))))
  11178. (save-restriction
  11179. (save-excursion
  11180. (when findpos
  11181. (org-back-to-heading t)
  11182. (narrow-to-region (point) (save-excursion
  11183. (outline-next-heading) (point)))
  11184. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"
  11185. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  11186. "[^\r\n]*\\)?"))
  11187. (goto-char (match-end 0))
  11188. (cond
  11189. (drawer
  11190. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  11191. nil t)
  11192. (progn
  11193. (goto-char (match-end 0))
  11194. (or org-log-states-order-reversed
  11195. (and (re-search-forward org-property-end-re nil t)
  11196. (goto-char (1- (match-beginning 0))))))
  11197. (insert "\n:" drawer ":\n:END:")
  11198. (beginning-of-line 0)
  11199. (org-indent-line-function)
  11200. (beginning-of-line 2)
  11201. (org-indent-line-function)
  11202. (end-of-line 0)))
  11203. ((and org-log-state-notes-insert-after-drawers
  11204. (save-excursion
  11205. (forward-line) (looking-at org-drawer-regexp)))
  11206. (forward-line)
  11207. (while (looking-at org-drawer-regexp)
  11208. (goto-char (match-end 0))
  11209. (re-search-forward org-property-end-re (point-max) t)
  11210. (forward-line))
  11211. (forward-line -1)))
  11212. (unless org-log-states-order-reversed
  11213. (and (= (char-after) ?\n) (forward-char 1))
  11214. (org-skip-over-state-notes)
  11215. (skip-chars-backward " \t\n\r")))
  11216. (move-marker org-log-note-marker (point))
  11217. (setq org-log-note-purpose purpose
  11218. org-log-note-state state
  11219. org-log-note-previous-state prev-state
  11220. org-log-note-how how
  11221. org-log-note-extra extra
  11222. org-log-note-effective-time (org-current-effective-time))
  11223. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  11224. (defun org-skip-over-state-notes ()
  11225. "Skip past the list of State notes in an entry."
  11226. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  11227. (when (ignore-errors (goto-char (org-in-item-p)))
  11228. (let* ((struct (org-list-struct))
  11229. (prevs (org-list-prevs-alist struct)))
  11230. (while (looking-at "[ \t]*- State")
  11231. (goto-char (or (org-list-get-next-item (point) struct prevs)
  11232. (org-list-get-item-end (point) struct)))))))
  11233. (defun org-add-log-note (&optional purpose)
  11234. "Pop up a window for taking a note, and add this note later at point."
  11235. (remove-hook 'post-command-hook 'org-add-log-note)
  11236. (setq org-log-note-window-configuration (current-window-configuration))
  11237. (delete-other-windows)
  11238. (move-marker org-log-note-return-to (point))
  11239. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  11240. (goto-char org-log-note-marker)
  11241. (org-switch-to-buffer-other-window "*Org Note*")
  11242. (erase-buffer)
  11243. (if (memq org-log-note-how '(time state))
  11244. (let (current-prefix-arg) (org-store-log-note))
  11245. (let ((org-inhibit-startup t)) (org-mode))
  11246. (insert (format "# Insert note for %s.
  11247. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  11248. (cond
  11249. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  11250. ((eq org-log-note-purpose 'done) "closed todo item")
  11251. ((eq org-log-note-purpose 'state)
  11252. (format "state change from \"%s\" to \"%s\""
  11253. (or org-log-note-previous-state "")
  11254. (or org-log-note-state "")))
  11255. ((eq org-log-note-purpose 'reschedule)
  11256. "rescheduling")
  11257. ((eq org-log-note-purpose 'delschedule)
  11258. "no longer scheduled")
  11259. ((eq org-log-note-purpose 'redeadline)
  11260. "changing deadline")
  11261. ((eq org-log-note-purpose 'deldeadline)
  11262. "removing deadline")
  11263. ((eq org-log-note-purpose 'refile)
  11264. "refiling")
  11265. ((eq org-log-note-purpose 'note)
  11266. "this entry")
  11267. (t (error "This should not happen")))))
  11268. (if org-log-note-extra (insert org-log-note-extra))
  11269. (org-set-local 'org-finish-function 'org-store-log-note)
  11270. (run-hooks 'org-log-buffer-setup-hook)))
  11271. (defvar org-note-abort nil) ; dynamically scoped
  11272. (defun org-store-log-note ()
  11273. "Finish taking a log note, and insert it to where it belongs."
  11274. (let ((txt (buffer-string))
  11275. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  11276. lines ind bul)
  11277. (kill-buffer (current-buffer))
  11278. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  11279. (setq txt (replace-match "" t t txt)))
  11280. (if (string-match "\\s-+\\'" txt)
  11281. (setq txt (replace-match "" t t txt)))
  11282. (setq lines (org-split-string txt "\n"))
  11283. (when (and note (string-match "\\S-" note))
  11284. (setq note
  11285. (org-replace-escapes
  11286. note
  11287. (list (cons "%u" (user-login-name))
  11288. (cons "%U" user-full-name)
  11289. (cons "%t" (format-time-string
  11290. (org-time-stamp-format 'long 'inactive)
  11291. org-log-note-effective-time))
  11292. (cons "%T" (format-time-string
  11293. (org-time-stamp-format 'long nil)
  11294. org-log-note-effective-time))
  11295. (cons "%d" (format-time-string
  11296. (org-time-stamp-format nil 'inactive)
  11297. org-log-note-effective-time))
  11298. (cons "%D" (format-time-string
  11299. (org-time-stamp-format nil nil)
  11300. org-log-note-effective-time))
  11301. (cons "%s" (if org-log-note-state
  11302. (concat "\"" org-log-note-state "\"")
  11303. ""))
  11304. (cons "%S" (if org-log-note-previous-state
  11305. (concat "\"" org-log-note-previous-state "\"")
  11306. "\"\"")))))
  11307. (if lines (setq note (concat note " \\\\")))
  11308. (push note lines))
  11309. (when (or current-prefix-arg org-note-abort)
  11310. (when org-log-into-drawer
  11311. (org-remove-empty-drawer-at
  11312. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  11313. org-log-note-marker))
  11314. (setq lines nil))
  11315. (when lines
  11316. (with-current-buffer (marker-buffer org-log-note-marker)
  11317. (save-excursion
  11318. (goto-char org-log-note-marker)
  11319. (move-marker org-log-note-marker nil)
  11320. (end-of-line 1)
  11321. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  11322. (setq ind (save-excursion
  11323. (if (ignore-errors (goto-char (org-in-item-p)))
  11324. (let ((struct (org-list-struct)))
  11325. (org-list-get-ind
  11326. (org-list-get-top-point struct) struct))
  11327. (skip-chars-backward " \r\t\n")
  11328. (cond
  11329. ((and (org-at-heading-p)
  11330. org-adapt-indentation)
  11331. (1+ (org-current-level)))
  11332. ((org-at-heading-p) 0)
  11333. (t (org-get-indentation))))))
  11334. (setq bul (org-list-bullet-string "-"))
  11335. (org-indent-line-to ind)
  11336. (insert bul (pop lines))
  11337. (let ((ind-body (+ (length bul) ind)))
  11338. (while lines
  11339. (insert "\n")
  11340. (org-indent-line-to ind-body)
  11341. (insert (pop lines))))
  11342. (message "Note stored")
  11343. (org-back-to-heading t)
  11344. (org-cycle-hide-drawers 'children)))))
  11345. (set-window-configuration org-log-note-window-configuration)
  11346. (with-current-buffer (marker-buffer org-log-note-return-to)
  11347. (goto-char org-log-note-return-to))
  11348. (move-marker org-log-note-return-to nil)
  11349. (and org-log-post-message (message "%s" org-log-post-message)))
  11350. (defun org-remove-empty-drawer-at (drawer pos)
  11351. "Remove an empty drawer DRAWER at position POS.
  11352. POS may also be a marker."
  11353. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  11354. (save-excursion
  11355. (save-restriction
  11356. (widen)
  11357. (goto-char pos)
  11358. (if (org-in-regexp
  11359. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  11360. (replace-match ""))))))
  11361. (defun org-sparse-tree (&optional arg)
  11362. "Create a sparse tree, prompt for the details.
  11363. This command can create sparse trees. You first need to select the type
  11364. of match used to create the tree:
  11365. t Show all TODO entries.
  11366. T Show entries with a specific TODO keyword.
  11367. m Show entries selected by a tags/property match.
  11368. p Enter a property name and its value (both with completion on existing
  11369. names/values) and show entries with that property.
  11370. r Show entries matching a regular expression (`/' can be used as well)
  11371. d Show deadlines due within `org-deadline-warning-days'.
  11372. b Show deadlines and scheduled items before a date.
  11373. a Show deadlines and scheduled items after a date."
  11374. (interactive "P")
  11375. (let (ans kwd value)
  11376. (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")
  11377. (setq ans (read-char-exclusive))
  11378. (cond
  11379. ((equal ans ?d)
  11380. (call-interactively 'org-check-deadlines))
  11381. ((equal ans ?b)
  11382. (call-interactively 'org-check-before-date))
  11383. ((equal ans ?a)
  11384. (call-interactively 'org-check-after-date))
  11385. ((equal ans ?D)
  11386. (call-interactively 'org-check-dates-range))
  11387. ((equal ans ?t)
  11388. (org-show-todo-tree nil))
  11389. ((equal ans ?T)
  11390. (org-show-todo-tree '(4)))
  11391. ((member ans '(?T ?m))
  11392. (call-interactively 'org-match-sparse-tree))
  11393. ((member ans '(?p ?P))
  11394. (setq kwd (org-icompleting-read "Property: "
  11395. (mapcar 'list (org-buffer-property-keys))))
  11396. (setq value (org-icompleting-read "Value: "
  11397. (mapcar 'list (org-property-values kwd))))
  11398. (unless (string-match "\\`{.*}\\'" value)
  11399. (setq value (concat "\"" value "\"")))
  11400. (org-match-sparse-tree arg (concat kwd "=" value)))
  11401. ((member ans '(?r ?R ?/))
  11402. (call-interactively 'org-occur))
  11403. (t (error "No such sparse tree command \"%c\"" ans)))))
  11404. (defvar org-occur-highlights nil
  11405. "List of overlays used for occur matches.")
  11406. (make-variable-buffer-local 'org-occur-highlights)
  11407. (defvar org-occur-parameters nil
  11408. "Parameters of the active org-occur calls.
  11409. This is a list, each call to org-occur pushes as cons cell,
  11410. containing the regular expression and the callback, onto the list.
  11411. The list can contain several entries if `org-occur' has been called
  11412. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  11413. will only contain one set of parameters. When the highlights are
  11414. removed (for example with `C-c C-c', or with the next edit (depending
  11415. on `org-remove-highlights-with-change'), this variable is emptied
  11416. as well.")
  11417. (make-variable-buffer-local 'org-occur-parameters)
  11418. (defun org-occur (regexp &optional keep-previous callback)
  11419. "Make a compact tree which shows all matches of REGEXP.
  11420. The tree will show the lines where the regexp matches, and all higher
  11421. headlines above the match. It will also show the heading after the match,
  11422. to make sure editing the matching entry is easy.
  11423. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  11424. call to `org-occur' will be kept, to allow stacking of calls to this
  11425. command.
  11426. If CALLBACK is non-nil, it is a function which is called to confirm
  11427. that the match should indeed be shown."
  11428. (interactive "sRegexp: \nP")
  11429. (when (equal regexp "")
  11430. (error "Regexp cannot be empty"))
  11431. (unless keep-previous
  11432. (org-remove-occur-highlights nil nil t))
  11433. (push (cons regexp callback) org-occur-parameters)
  11434. (let ((cnt 0))
  11435. (save-excursion
  11436. (goto-char (point-min))
  11437. (if (or (not keep-previous) ; do not want to keep
  11438. (not org-occur-highlights)) ; no previous matches
  11439. ;; hide everything
  11440. (org-overview))
  11441. (while (re-search-forward regexp nil t)
  11442. (when (or (not callback)
  11443. (save-match-data (funcall callback)))
  11444. (setq cnt (1+ cnt))
  11445. (when org-highlight-sparse-tree-matches
  11446. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  11447. (org-show-context 'occur-tree))))
  11448. (when org-remove-highlights-with-change
  11449. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  11450. nil 'local))
  11451. (unless org-sparse-tree-open-archived-trees
  11452. (org-hide-archived-subtrees (point-min) (point-max)))
  11453. (run-hooks 'org-occur-hook)
  11454. (if (org-called-interactively-p 'interactive)
  11455. (message "%d match(es) for regexp %s" cnt regexp))
  11456. cnt))
  11457. (defun org-occur-next-match (&optional n reset)
  11458. "Function for `next-error-function' to find sparse tree matches.
  11459. N is the number of matches to move, when negative move backwards.
  11460. RESET is entirely ignored - this function always goes back to the
  11461. starting point when no match is found."
  11462. (let* ((limit (if (< n 0) (point-min) (point-max)))
  11463. (search-func (if (< n 0)
  11464. 'previous-single-char-property-change
  11465. 'next-single-char-property-change))
  11466. (n (abs n))
  11467. (pos (point))
  11468. p1)
  11469. (catch 'exit
  11470. (while (setq p1 (funcall search-func (point) 'org-type))
  11471. (when (equal p1 limit)
  11472. (goto-char pos)
  11473. (error "No more matches"))
  11474. (when (equal (get-char-property p1 'org-type) 'org-occur)
  11475. (setq n (1- n))
  11476. (when (= n 0)
  11477. (goto-char p1)
  11478. (throw 'exit (point))))
  11479. (goto-char p1))
  11480. (goto-char p1)
  11481. (error "No more matches"))))
  11482. (defun org-show-context (&optional key)
  11483. "Make sure point and context are visible.
  11484. How much context is shown depends upon the variables
  11485. `org-show-hierarchy-above', `org-show-following-heading',
  11486. `org-show-entry-below' and `org-show-siblings'."
  11487. (let ((heading-p (org-at-heading-p t))
  11488. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  11489. (following-p (org-get-alist-option org-show-following-heading key))
  11490. (entry-p (org-get-alist-option org-show-entry-below key))
  11491. (siblings-p (org-get-alist-option org-show-siblings key)))
  11492. (catch 'exit
  11493. ;; Show heading or entry text
  11494. (if (and heading-p (not entry-p))
  11495. (org-flag-heading nil) ; only show the heading
  11496. (and (or entry-p (outline-invisible-p) (org-invisible-p2))
  11497. (org-show-hidden-entry))) ; show entire entry
  11498. (when following-p
  11499. ;; Show next sibling, or heading below text
  11500. (save-excursion
  11501. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  11502. (org-flag-heading nil))))
  11503. (when siblings-p (org-show-siblings))
  11504. (when hierarchy-p
  11505. ;; show all higher headings, possibly with siblings
  11506. (save-excursion
  11507. (while (and (condition-case nil
  11508. (progn (org-up-heading-all 1) t)
  11509. (error nil))
  11510. (not (bobp)))
  11511. (org-flag-heading nil)
  11512. (when siblings-p (org-show-siblings))))))))
  11513. (defvar org-reveal-start-hook nil
  11514. "Hook run before revealing a location.")
  11515. (defun org-reveal (&optional siblings)
  11516. "Show current entry, hierarchy above it, and the following headline.
  11517. This can be used to show a consistent set of context around locations
  11518. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  11519. not t for the search context.
  11520. With optional argument SIBLINGS, on each level of the hierarchy all
  11521. siblings are shown. This repairs the tree structure to what it would
  11522. look like when opened with hierarchical calls to `org-cycle'.
  11523. With double optional argument \\[universal-argument] \\[universal-argument], \
  11524. go to the parent and show the
  11525. entire tree."
  11526. (interactive "P")
  11527. (run-hooks 'org-reveal-start-hook)
  11528. (let ((org-show-hierarchy-above t)
  11529. (org-show-following-heading t)
  11530. (org-show-siblings (if siblings t org-show-siblings)))
  11531. (org-show-context nil))
  11532. (when (equal siblings '(16))
  11533. (save-excursion
  11534. (when (org-up-heading-safe)
  11535. (org-show-subtree)
  11536. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  11537. (defun org-highlight-new-match (beg end)
  11538. "Highlight from BEG to END and mark the highlight is an occur headline."
  11539. (let ((ov (make-overlay beg end)))
  11540. (overlay-put ov 'face 'secondary-selection)
  11541. (overlay-put ov 'org-type 'org-occur)
  11542. (push ov org-occur-highlights)))
  11543. (defun org-remove-occur-highlights (&optional beg end noremove)
  11544. "Remove the occur highlights from the buffer.
  11545. BEG and END are ignored. If NOREMOVE is nil, remove this function
  11546. from the `before-change-functions' in the current buffer."
  11547. (interactive)
  11548. (unless org-inhibit-highlight-removal
  11549. (mapc 'delete-overlay org-occur-highlights)
  11550. (setq org-occur-highlights nil)
  11551. (setq org-occur-parameters nil)
  11552. (unless noremove
  11553. (remove-hook 'before-change-functions
  11554. 'org-remove-occur-highlights 'local))))
  11555. ;;;; Priorities
  11556. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  11557. "Regular expression matching the priority indicator.")
  11558. (defvar org-remove-priority-next-time nil)
  11559. (defun org-priority-up ()
  11560. "Increase the priority of the current item."
  11561. (interactive)
  11562. (org-priority 'up))
  11563. (defun org-priority-down ()
  11564. "Decrease the priority of the current item."
  11565. (interactive)
  11566. (org-priority 'down))
  11567. (defun org-priority (&optional action)
  11568. "Change the priority of an item by ARG.
  11569. ACTION can be `set', `up', `down', or a character."
  11570. (interactive)
  11571. (unless org-enable-priority-commands
  11572. (error "Priority commands are disabled"))
  11573. (setq action (or action 'set))
  11574. (let (current new news have remove)
  11575. (save-excursion
  11576. (org-back-to-heading t)
  11577. (if (looking-at org-priority-regexp)
  11578. (setq current (string-to-char (match-string 2))
  11579. have t))
  11580. (cond
  11581. ((eq action 'remove)
  11582. (setq remove t new ?\ ))
  11583. ((or (eq action 'set)
  11584. (if (featurep 'xemacs) (characterp action) (integerp action)))
  11585. (if (not (eq action 'set))
  11586. (setq new action)
  11587. (message "Priority %c-%c, SPC to remove: "
  11588. org-highest-priority org-lowest-priority)
  11589. (save-match-data
  11590. (setq new (read-char-exclusive))))
  11591. (if (and (= (upcase org-highest-priority) org-highest-priority)
  11592. (= (upcase org-lowest-priority) org-lowest-priority))
  11593. (setq new (upcase new)))
  11594. (cond ((equal new ?\ ) (setq remove t))
  11595. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  11596. (error "Priority must be between `%c' and `%c'"
  11597. org-highest-priority org-lowest-priority))))
  11598. ((eq action 'up)
  11599. (setq new (if have
  11600. (1- current) ; normal cycling
  11601. ;; last priority was empty
  11602. (if (eq last-command this-command)
  11603. org-lowest-priority ; wrap around empty to lowest
  11604. ;; default
  11605. (if org-priority-start-cycle-with-default
  11606. org-default-priority
  11607. (1- org-default-priority))))))
  11608. ((eq action 'down)
  11609. (setq new (if have
  11610. (1+ current) ; normal cycling
  11611. ;; last priority was empty
  11612. (if (eq last-command this-command)
  11613. org-highest-priority ; wrap around empty to highest
  11614. ;; default
  11615. (if org-priority-start-cycle-with-default
  11616. org-default-priority
  11617. (1+ org-default-priority))))))
  11618. (t (error "Invalid action")))
  11619. (if (or (< (upcase new) org-highest-priority)
  11620. (> (upcase new) org-lowest-priority))
  11621. (if (and (memq action '(up down))
  11622. (not have) (not (eq last-command this-command)))
  11623. ;; `new' is from default priority
  11624. (error
  11625. "The default can not be set, see `org-default-priority' why")
  11626. ;; normal cycling: `new' is beyond highest/lowest priority
  11627. ;; and is wrapped around to the empty priority
  11628. (setq remove t)))
  11629. (setq news (format "%c" new))
  11630. (if have
  11631. (if remove
  11632. (replace-match "" t t nil 1)
  11633. (replace-match news t t nil 2))
  11634. (if remove
  11635. (error "No priority cookie found in line")
  11636. (let ((case-fold-search nil))
  11637. (looking-at org-todo-line-regexp))
  11638. (if (match-end 2)
  11639. (progn
  11640. (goto-char (match-end 2))
  11641. (insert " [#" news "]"))
  11642. (goto-char (match-beginning 3))
  11643. (insert "[#" news "] "))))
  11644. (org-preserve-lc (org-set-tags nil 'align)))
  11645. (if remove
  11646. (message "Priority removed")
  11647. (message "Priority of current item set to %s" news))))
  11648. (defun org-get-priority (s)
  11649. "Find priority cookie and return priority."
  11650. (if (functionp org-get-priority-function)
  11651. (funcall org-get-priority-function)
  11652. (save-match-data
  11653. (if (not (string-match org-priority-regexp s))
  11654. (* 1000 (- org-lowest-priority org-default-priority))
  11655. (* 1000 (- org-lowest-priority
  11656. (string-to-char (match-string 2 s))))))))
  11657. ;;;; Tags
  11658. (defvar org-agenda-archives-mode)
  11659. (defvar org-map-continue-from nil
  11660. "Position from where mapping should continue.
  11661. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  11662. (defvar org-scanner-tags nil
  11663. "The current tag list while the tags scanner is running.")
  11664. (defvar org-trust-scanner-tags nil
  11665. "Should `org-get-tags-at' use the tags for the scanner.
  11666. This is for internal dynamical scoping only.
  11667. When this is non-nil, the function `org-get-tags-at' will return the value
  11668. of `org-scanner-tags' instead of building the list by itself. This
  11669. can lead to large speed-ups when the tags scanner is used in a file with
  11670. many entries, and when the list of tags is retrieved, for example to
  11671. obtain a list of properties. Building the tags list for each entry in such
  11672. a file becomes an N^2 operation - but with this variable set, it scales
  11673. as N.")
  11674. (defun org-scan-tags (action matcher todo-only &optional start-level)
  11675. "Scan headline tags with inheritance and produce output ACTION.
  11676. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  11677. or `agenda' to produce an entry list for an agenda view. It can also be
  11678. a Lisp form or a function that should be called at each matched headline, in
  11679. this case the return value is a list of all return values from these calls.
  11680. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  11681. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  11682. only lines with a not-done TODO keyword are included in the output.
  11683. This should be the same variable that was scoped into
  11684. and set by `org-make-tags-matcher' when it constructed MATCHER.
  11685. START-LEVEL can be a string with asterisks, reducing the scope to
  11686. headlines matching this string."
  11687. (require 'org-agenda)
  11688. (let* ((re (concat "^"
  11689. (if start-level
  11690. ;; Get the correct level to match
  11691. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  11692. org-outline-regexp)
  11693. " *\\(\\<\\("
  11694. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  11695. (org-re
  11696. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  11697. (props (list 'face 'default
  11698. 'done-face 'org-agenda-done
  11699. 'undone-face 'default
  11700. 'mouse-face 'highlight
  11701. 'org-not-done-regexp org-not-done-regexp
  11702. 'org-todo-regexp org-todo-regexp
  11703. 'org-complex-heading-regexp org-complex-heading-regexp
  11704. 'help-echo
  11705. (format "mouse-2 or RET jump to org file %s"
  11706. (abbreviate-file-name
  11707. (or (buffer-file-name (buffer-base-buffer))
  11708. (buffer-name (buffer-base-buffer)))))))
  11709. (case-fold-search nil)
  11710. (org-map-continue-from nil)
  11711. lspos tags tags-list
  11712. (tags-alist (list (cons 0 org-file-tags)))
  11713. (llast 0) rtn rtn1 level category i txt
  11714. todo marker entry priority)
  11715. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  11716. (setq action (list 'lambda nil action)))
  11717. (save-excursion
  11718. (goto-char (point-min))
  11719. (when (eq action 'sparse-tree)
  11720. (org-overview)
  11721. (org-remove-occur-highlights))
  11722. (while (re-search-forward re nil t)
  11723. (setq org-map-continue-from nil)
  11724. (catch :skip
  11725. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  11726. tags (if (match-end 4) (org-match-string-no-properties 4)))
  11727. (goto-char (setq lspos (match-beginning 0)))
  11728. (setq level (org-reduced-level (funcall outline-level))
  11729. category (org-get-category))
  11730. (setq i llast llast level)
  11731. ;; remove tag lists from same and sublevels
  11732. (while (>= i level)
  11733. (when (setq entry (assoc i tags-alist))
  11734. (setq tags-alist (delete entry tags-alist)))
  11735. (setq i (1- i)))
  11736. ;; add the next tags
  11737. (when tags
  11738. (setq tags (org-split-string tags ":")
  11739. tags-alist
  11740. (cons (cons level tags) tags-alist)))
  11741. ;; compile tags for current headline
  11742. (setq tags-list
  11743. (if org-use-tag-inheritance
  11744. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  11745. tags)
  11746. org-scanner-tags tags-list)
  11747. (when org-use-tag-inheritance
  11748. (setcdr (car tags-alist)
  11749. (mapcar (lambda (x)
  11750. (setq x (copy-sequence x))
  11751. (org-add-prop-inherited x))
  11752. (cdar tags-alist))))
  11753. (when (and tags org-use-tag-inheritance
  11754. (or (not (eq t org-use-tag-inheritance))
  11755. org-tags-exclude-from-inheritance))
  11756. ;; selective inheritance, remove uninherited ones
  11757. (setcdr (car tags-alist)
  11758. (org-remove-uninherited-tags (cdar tags-alist))))
  11759. (when (and
  11760. ;; eval matcher only when the todo condition is OK
  11761. (and (or (not todo-only) (member todo org-not-done-keywords))
  11762. (let ((case-fold-search t) (org-trust-scanner-tags t))
  11763. (eval matcher)))
  11764. ;; Call the skipper, but return t if it does not skip,
  11765. ;; so that the `and' form continues evaluating
  11766. (progn
  11767. (unless (eq action 'sparse-tree) (org-agenda-skip))
  11768. t)
  11769. ;; Check if timestamps are deselecting this entry
  11770. (or (not todo-only)
  11771. (and (member todo org-not-done-keywords)
  11772. (or (not org-agenda-tags-todo-honor-ignore-options)
  11773. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  11774. ;; Extra check for the archive tag
  11775. ;; FIXME: Does the skipper already do this????
  11776. (or
  11777. (not (member org-archive-tag tags-list))
  11778. ;; we have an archive tag, should we use this anyway?
  11779. (or (not org-agenda-skip-archived-trees)
  11780. (and (eq action 'agenda) org-agenda-archives-mode))))
  11781. ;; select this headline
  11782. (cond
  11783. ((eq action 'sparse-tree)
  11784. (and org-highlight-sparse-tree-matches
  11785. (org-get-heading) (match-end 0)
  11786. (org-highlight-new-match
  11787. (match-beginning 1) (match-end 1)))
  11788. (org-show-context 'tags-tree))
  11789. ((eq action 'agenda)
  11790. (setq txt (org-agenda-format-item
  11791. ""
  11792. (concat
  11793. (if (eq org-tags-match-list-sublevels 'indented)
  11794. (make-string (1- level) ?.) "")
  11795. (org-get-heading))
  11796. category
  11797. tags-list)
  11798. priority (org-get-priority txt))
  11799. (goto-char lspos)
  11800. (setq marker (org-agenda-new-marker))
  11801. (org-add-props txt props
  11802. 'org-marker marker 'org-hd-marker marker 'org-category category
  11803. 'todo-state todo
  11804. 'priority priority 'type "tagsmatch")
  11805. (push txt rtn))
  11806. ((functionp action)
  11807. (setq org-map-continue-from nil)
  11808. (save-excursion
  11809. (setq rtn1 (funcall action))
  11810. (push rtn1 rtn)))
  11811. (t (error "Invalid action")))
  11812. ;; if we are to skip sublevels, jump to end of subtree
  11813. (unless org-tags-match-list-sublevels
  11814. (org-end-of-subtree t)
  11815. (backward-char 1))))
  11816. ;; Get the correct position from where to continue
  11817. (if org-map-continue-from
  11818. (goto-char org-map-continue-from)
  11819. (and (= (point) lspos) (end-of-line 1)))))
  11820. (when (and (eq action 'sparse-tree)
  11821. (not org-sparse-tree-open-archived-trees))
  11822. (org-hide-archived-subtrees (point-min) (point-max)))
  11823. (nreverse rtn)))
  11824. (defun org-remove-uninherited-tags (tags)
  11825. "Remove all tags that are not inherited from the list TAGS."
  11826. (cond
  11827. ((eq org-use-tag-inheritance t)
  11828. (if org-tags-exclude-from-inheritance
  11829. (org-delete-all org-tags-exclude-from-inheritance tags)
  11830. tags))
  11831. ((not org-use-tag-inheritance) nil)
  11832. ((stringp org-use-tag-inheritance)
  11833. (delq nil (mapcar
  11834. (lambda (x)
  11835. (if (and (string-match org-use-tag-inheritance x)
  11836. (not (member x org-tags-exclude-from-inheritance)))
  11837. x nil))
  11838. tags)))
  11839. ((listp org-use-tag-inheritance)
  11840. (delq nil (mapcar
  11841. (lambda (x)
  11842. (if (member x org-use-tag-inheritance) x nil))
  11843. tags)))))
  11844. (defun org-match-sparse-tree (&optional todo-only match)
  11845. "Create a sparse tree according to tags string MATCH.
  11846. MATCH can contain positive and negative selection of tags, like
  11847. \"+WORK+URGENT-WITHBOSS\".
  11848. If optional argument TODO-ONLY is non-nil, only select lines that are
  11849. also TODO lines."
  11850. (interactive "P")
  11851. (org-prepare-agenda-buffers (list (current-buffer)))
  11852. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  11853. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  11854. (defvar org-cached-props nil)
  11855. (defun org-cached-entry-get (pom property)
  11856. (if (or (eq t org-use-property-inheritance)
  11857. (and (stringp org-use-property-inheritance)
  11858. (string-match org-use-property-inheritance property))
  11859. (and (listp org-use-property-inheritance)
  11860. (member property org-use-property-inheritance)))
  11861. ;; Caching is not possible, check it directly
  11862. (org-entry-get pom property 'inherit)
  11863. ;; Get all properties, so that we can do complicated checks easily
  11864. (cdr (assoc property (or org-cached-props
  11865. (setq org-cached-props
  11866. (org-entry-properties pom)))))))
  11867. (defun org-global-tags-completion-table (&optional files)
  11868. "Return the list of all tags in all agenda buffer/files.
  11869. Optional FILES argument is a list of files to which can be used
  11870. instead of the agenda files."
  11871. (save-excursion
  11872. (org-uniquify
  11873. (delq nil
  11874. (apply 'append
  11875. (mapcar
  11876. (lambda (file)
  11877. (set-buffer (find-file-noselect file))
  11878. (append (org-get-buffer-tags)
  11879. (mapcar (lambda (x) (if (stringp (car-safe x))
  11880. (list (car-safe x)) nil))
  11881. org-tag-alist)))
  11882. (if (and files (car files))
  11883. files
  11884. (org-agenda-files))))))))
  11885. (defun org-make-tags-matcher (match)
  11886. "Create the TAGS/TODO matcher form for the selection string MATCH.
  11887. The variable `todo-only' is scoped dynamically into this function; it will be
  11888. set to t if the matcher restricts matching to TODO entries,
  11889. otherwise will not be touched.
  11890. Returns a cons of the selection string MATCH and the constructed
  11891. lisp form implementing the matcher. The matcher is to be
  11892. evaluated at an Org entry, with point on the headline,
  11893. and returns t if the entry matches the
  11894. selection string MATCH. The returned lisp form references
  11895. two variables with information about the entry, which must be
  11896. bound around the form's evaluation: todo, the TODO keyword at the
  11897. entry (or nil of none); and tags-list, the list of all tags at the
  11898. entry including inherited ones. Additionally, the category
  11899. of the entry (if any) must be specified as the text property
  11900. 'org-category on the headline.
  11901. See also `org-scan-tags'.
  11902. "
  11903. (declare (special todo-only))
  11904. (unless (boundp 'todo-only)
  11905. (error "org-make-tags-matcher expects todo-only to be scoped in"))
  11906. (unless match
  11907. ;; Get a new match request, with completion
  11908. (let ((org-last-tags-completion-table
  11909. (org-global-tags-completion-table)))
  11910. (setq match (org-completing-read-no-i
  11911. "Match: " 'org-tags-completion-function nil nil nil
  11912. 'org-tags-history))))
  11913. ;; Parse the string and create a lisp form
  11914. (let ((match0 match)
  11915. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  11916. minus tag mm
  11917. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  11918. orterms term orlist re-p str-p level-p level-op time-p
  11919. prop-p pn pv po gv rest)
  11920. (if (string-match "/+" match)
  11921. ;; match contains also a todo-matching request
  11922. (progn
  11923. (setq tagsmatch (substring match 0 (match-beginning 0))
  11924. todomatch (substring match (match-end 0)))
  11925. (if (string-match "^!" todomatch)
  11926. (setq todo-only t todomatch (substring todomatch 1)))
  11927. (if (string-match "^\\s-*$" todomatch)
  11928. (setq todomatch nil)))
  11929. ;; only matching tags
  11930. (setq tagsmatch match todomatch nil))
  11931. ;; Make the tags matcher
  11932. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  11933. (setq tagsmatcher t)
  11934. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  11935. (while (setq term (pop orterms))
  11936. (while (and (equal (substring term -1) "\\") orterms)
  11937. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  11938. (while (string-match re term)
  11939. (setq rest (substring term (match-end 0))
  11940. minus (and (match-end 1)
  11941. (equal (match-string 1 term) "-"))
  11942. tag (save-match-data (replace-regexp-in-string
  11943. "\\\\-" "-"
  11944. (match-string 2 term)))
  11945. re-p (equal (string-to-char tag) ?{)
  11946. level-p (match-end 4)
  11947. prop-p (match-end 5)
  11948. mm (cond
  11949. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  11950. (level-p
  11951. (setq level-op (org-op-to-function (match-string 3 term)))
  11952. `(,level-op level ,(string-to-number
  11953. (match-string 4 term))))
  11954. (prop-p
  11955. (setq pn (match-string 5 term)
  11956. po (match-string 6 term)
  11957. pv (match-string 7 term)
  11958. re-p (equal (string-to-char pv) ?{)
  11959. str-p (equal (string-to-char pv) ?\")
  11960. time-p (save-match-data
  11961. (string-match "^\"[[<].*[]>]\"$" pv))
  11962. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  11963. (if time-p (setq pv (org-matcher-time pv)))
  11964. (setq po (org-op-to-function po (if time-p 'time str-p)))
  11965. (cond
  11966. ((equal pn "CATEGORY")
  11967. (setq gv '(get-text-property (point) 'org-category)))
  11968. ((equal pn "TODO")
  11969. (setq gv 'todo))
  11970. (t
  11971. (setq gv `(org-cached-entry-get nil ,pn))))
  11972. (if re-p
  11973. (if (eq po 'org<>)
  11974. `(not (string-match ,pv (or ,gv "")))
  11975. `(string-match ,pv (or ,gv "")))
  11976. (if str-p
  11977. `(,po (or ,gv "") ,pv)
  11978. `(,po (string-to-number (or ,gv ""))
  11979. ,(string-to-number pv) ))))
  11980. (t `(member ,tag tags-list)))
  11981. mm (if minus (list 'not mm) mm)
  11982. term rest)
  11983. (push mm tagsmatcher))
  11984. (push (if (> (length tagsmatcher) 1)
  11985. (cons 'and tagsmatcher)
  11986. (car tagsmatcher))
  11987. orlist)
  11988. (setq tagsmatcher nil))
  11989. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  11990. (setq tagsmatcher
  11991. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  11992. ;; Make the todo matcher
  11993. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  11994. (setq todomatcher t)
  11995. (setq orterms (org-split-string todomatch "|") orlist nil)
  11996. (while (setq term (pop orterms))
  11997. (while (string-match re term)
  11998. (setq minus (and (match-end 1)
  11999. (equal (match-string 1 term) "-"))
  12000. kwd (match-string 2 term)
  12001. re-p (equal (string-to-char kwd) ?{)
  12002. term (substring term (match-end 0))
  12003. mm (if re-p
  12004. `(string-match ,(substring kwd 1 -1) todo)
  12005. (list 'equal 'todo kwd))
  12006. mm (if minus (list 'not mm) mm))
  12007. (push mm todomatcher))
  12008. (push (if (> (length todomatcher) 1)
  12009. (cons 'and todomatcher)
  12010. (car todomatcher))
  12011. orlist)
  12012. (setq todomatcher nil))
  12013. (setq todomatcher (if (> (length orlist) 1)
  12014. (cons 'or orlist) (car orlist))))
  12015. ;; Return the string and lisp forms of the matcher
  12016. (setq matcher (if todomatcher
  12017. (list 'and tagsmatcher todomatcher)
  12018. tagsmatcher))
  12019. (when todo-only
  12020. (setq matcher (list 'and '(member todo org-not-done-keywords)
  12021. matcher)))
  12022. (cons match0 matcher)))
  12023. (defun org-op-to-function (op &optional stringp)
  12024. "Turn an operator into the appropriate function."
  12025. (setq op
  12026. (cond
  12027. ((equal op "<" ) '(< string< org-time<))
  12028. ((equal op ">" ) '(> org-string> org-time>))
  12029. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  12030. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  12031. ((member op '("=" "==")) '(= string= org-time=))
  12032. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  12033. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  12034. (defun org<> (a b) (not (= a b)))
  12035. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  12036. (defun org-string>= (a b) (not (string< a b)))
  12037. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  12038. (defun org-string<> (a b) (not (string= a b)))
  12039. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  12040. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  12041. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  12042. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  12043. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  12044. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  12045. (defun org-2ft (s)
  12046. "Convert S to a floating point time.
  12047. If S is already a number, just return it. If it is a string, parse
  12048. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  12049. (cond
  12050. ((numberp s) s)
  12051. ((stringp s)
  12052. (condition-case nil
  12053. (float-time (apply 'encode-time (org-parse-time-string s)))
  12054. (error 0.)))
  12055. (t 0.)))
  12056. (defun org-time-today ()
  12057. "Time in seconds today at 0:00.
  12058. Returns the float number of seconds since the beginning of the
  12059. epoch to the beginning of today (00:00)."
  12060. (float-time (apply 'encode-time
  12061. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  12062. (defun org-matcher-time (s)
  12063. "Interpret a time comparison value."
  12064. (save-match-data
  12065. (cond
  12066. ((string= s "<now>") (float-time))
  12067. ((string= s "<today>") (org-time-today))
  12068. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  12069. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  12070. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  12071. (+ (org-time-today)
  12072. (* (string-to-number (match-string 1 s))
  12073. (cdr (assoc (match-string 2 s)
  12074. '(("d" . 86400.0) ("w" . 604800.0)
  12075. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  12076. (t (org-2ft s)))))
  12077. (defun org-match-any-p (re list)
  12078. "Does re match any element of list?"
  12079. (setq list (mapcar (lambda (x) (string-match re x)) list))
  12080. (delq nil list))
  12081. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  12082. (defvar org-tags-overlay (make-overlay 1 1))
  12083. (org-detach-overlay org-tags-overlay)
  12084. (defun org-get-local-tags-at (&optional pos)
  12085. "Get a list of tags defined in the current headline."
  12086. (org-get-tags-at pos 'local))
  12087. (defun org-get-local-tags ()
  12088. "Get a list of tags defined in the current headline."
  12089. (org-get-tags-at nil 'local))
  12090. (defun org-get-tags-at (&optional pos local)
  12091. "Get a list of all headline tags applicable at POS.
  12092. POS defaults to point. If tags are inherited, the list contains
  12093. the targets in the same sequence as the headlines appear, i.e.
  12094. the tags of the current headline come last.
  12095. When LOCAL is non-nil, only return tags from the current headline,
  12096. ignore inherited ones."
  12097. (interactive)
  12098. (if (and org-trust-scanner-tags
  12099. (or (not pos) (equal pos (point)))
  12100. (not local))
  12101. org-scanner-tags
  12102. (let (tags ltags lastpos parent)
  12103. (save-excursion
  12104. (save-restriction
  12105. (widen)
  12106. (goto-char (or pos (point)))
  12107. (save-match-data
  12108. (catch 'done
  12109. (condition-case nil
  12110. (progn
  12111. (org-back-to-heading t)
  12112. (while (not (equal lastpos (point)))
  12113. (setq lastpos (point))
  12114. (when (looking-at
  12115. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  12116. (setq ltags (org-split-string
  12117. (org-match-string-no-properties 1) ":"))
  12118. (when parent
  12119. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  12120. (setq tags (append
  12121. (if parent
  12122. (org-remove-uninherited-tags ltags)
  12123. ltags)
  12124. tags)))
  12125. (or org-use-tag-inheritance (throw 'done t))
  12126. (if local (throw 'done t))
  12127. (or (org-up-heading-safe) (error nil))
  12128. (setq parent t)))
  12129. (error nil)))))
  12130. (if local
  12131. tags
  12132. (append (org-remove-uninherited-tags org-file-tags) tags))))))
  12133. (defun org-add-prop-inherited (s)
  12134. (add-text-properties 0 (length s) '(inherited t) s)
  12135. s)
  12136. (defun org-toggle-tag (tag &optional onoff)
  12137. "Toggle the tag TAG for the current line.
  12138. If ONOFF is `on' or `off', don't toggle but set to this state."
  12139. (let (res current)
  12140. (save-excursion
  12141. (org-back-to-heading t)
  12142. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  12143. (point-at-eol) t)
  12144. (progn
  12145. (setq current (match-string 1))
  12146. (replace-match ""))
  12147. (setq current ""))
  12148. (setq current (nreverse (org-split-string current ":")))
  12149. (cond
  12150. ((eq onoff 'on)
  12151. (setq res t)
  12152. (or (member tag current) (push tag current)))
  12153. ((eq onoff 'off)
  12154. (or (not (member tag current)) (setq current (delete tag current))))
  12155. (t (if (member tag current)
  12156. (setq current (delete tag current))
  12157. (setq res t)
  12158. (push tag current))))
  12159. (end-of-line 1)
  12160. (if current
  12161. (progn
  12162. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  12163. (org-set-tags nil t))
  12164. (delete-horizontal-space))
  12165. (run-hooks 'org-after-tags-change-hook))
  12166. res))
  12167. (defun org-align-tags-here (to-col)
  12168. ;; Assumes that this is a headline
  12169. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  12170. (beginning-of-line 1)
  12171. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12172. (< pos (match-beginning 2)))
  12173. (progn
  12174. (setq tags-l (- (match-end 2) (match-beginning 2)))
  12175. (goto-char (match-beginning 1))
  12176. (insert " ")
  12177. (delete-region (point) (1+ (match-beginning 2)))
  12178. (setq ncol (max (current-column)
  12179. (1+ col)
  12180. (if (> to-col 0)
  12181. to-col
  12182. (- (abs to-col) tags-l))))
  12183. (setq p (point))
  12184. (insert (make-string (- ncol (current-column)) ?\ ))
  12185. (setq ncol (current-column))
  12186. (when indent-tabs-mode (tabify p (point-at-eol)))
  12187. (org-move-to-column (min ncol col) t))
  12188. (goto-char pos))))
  12189. (defun org-set-tags-command (&optional arg just-align)
  12190. "Call the set-tags command for the current entry."
  12191. (interactive "P")
  12192. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  12193. (org-set-tags arg just-align)
  12194. (save-excursion
  12195. (org-back-to-heading t)
  12196. (org-set-tags arg just-align))))
  12197. (defun org-set-tags-to (data)
  12198. "Set the tags of the current entry to DATA, replacing the current tags.
  12199. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  12200. If DATA is nil or the empty string, any tags will be removed."
  12201. (interactive "sTags: ")
  12202. (setq data
  12203. (cond
  12204. ((eq data nil) "")
  12205. ((equal data "") "")
  12206. ((stringp data)
  12207. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  12208. ":"))
  12209. ((listp data)
  12210. (concat ":" (mapconcat 'identity data ":") ":"))
  12211. (t nil)))
  12212. (when data
  12213. (save-excursion
  12214. (org-back-to-heading t)
  12215. (when (looking-at org-complex-heading-regexp)
  12216. (if (match-end 5)
  12217. (progn
  12218. (goto-char (match-beginning 5))
  12219. (insert data)
  12220. (delete-region (point) (point-at-eol))
  12221. (org-set-tags nil 'align))
  12222. (goto-char (point-at-eol))
  12223. (insert " " data)
  12224. (org-set-tags nil 'align)))
  12225. (beginning-of-line 1)
  12226. (if (looking-at ".*?\\([ \t]+\\)$")
  12227. (delete-region (match-beginning 1) (match-end 1))))))
  12228. (defun org-align-all-tags ()
  12229. "Align the tags i all headings."
  12230. (interactive)
  12231. (save-excursion
  12232. (or (ignore-errors (org-back-to-heading t))
  12233. (outline-next-heading))
  12234. (if (org-at-heading-p)
  12235. (org-set-tags t)
  12236. (message "No headings"))))
  12237. (defvar org-indent-indentation-per-level)
  12238. (defun org-set-tags (&optional arg just-align)
  12239. "Set the tags for the current headline.
  12240. With prefix ARG, realign all tags in headings in the current buffer."
  12241. (interactive "P")
  12242. (let* ((re org-outline-regexp-bol)
  12243. (current (unless arg (org-get-tags-string)))
  12244. (col (current-column))
  12245. (org-setting-tags t)
  12246. table current-tags inherited-tags ; computed below when needed
  12247. tags p0 c0 c1 rpl di tc level)
  12248. (if arg
  12249. (save-excursion
  12250. (goto-char (point-min))
  12251. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  12252. (while (re-search-forward re nil t)
  12253. (org-set-tags nil t)
  12254. (end-of-line 1)))
  12255. (message "All tags realigned to column %d" org-tags-column))
  12256. (if just-align
  12257. (setq tags current)
  12258. ;; Get a new set of tags from the user
  12259. (save-excursion
  12260. (setq table (append org-tag-persistent-alist
  12261. (or org-tag-alist (org-get-buffer-tags))
  12262. (and
  12263. org-complete-tags-always-offer-all-agenda-tags
  12264. (org-global-tags-completion-table
  12265. (org-agenda-files))))
  12266. org-last-tags-completion-table table
  12267. current-tags (org-split-string current ":")
  12268. inherited-tags (nreverse
  12269. (nthcdr (length current-tags)
  12270. (nreverse (org-get-tags-at))))
  12271. tags
  12272. (if (or (eq t org-use-fast-tag-selection)
  12273. (and org-use-fast-tag-selection
  12274. (delq nil (mapcar 'cdr table))))
  12275. (org-fast-tag-selection
  12276. current-tags inherited-tags table
  12277. (if org-fast-tag-selection-include-todo
  12278. org-todo-key-alist))
  12279. (let ((org-add-colon-after-tag-completion (< 1 (length table))))
  12280. (org-trim
  12281. (org-icompleting-read "Tags: "
  12282. 'org-tags-completion-function
  12283. nil nil current 'org-tags-history))))))
  12284. (while (string-match "[-+&]+" tags)
  12285. ;; No boolean logic, just a list
  12286. (setq tags (replace-match ":" t t tags))))
  12287. (setq tags (replace-regexp-in-string "[,]" ":" tags))
  12288. (if org-tags-sort-function
  12289. (setq tags (mapconcat 'identity
  12290. (sort (org-split-string
  12291. tags (org-re "[^[:alnum:]_@#%]+"))
  12292. org-tags-sort-function) ":")))
  12293. (if (string-match "\\`[\t ]*\\'" tags)
  12294. (setq tags "")
  12295. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  12296. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  12297. ;; Insert new tags at the correct column
  12298. (beginning-of-line 1)
  12299. (setq level (or (and (looking-at org-outline-regexp)
  12300. (- (match-end 0) (point) 1))
  12301. 1))
  12302. (cond
  12303. ((and (equal current "") (equal tags "")))
  12304. ((re-search-forward
  12305. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  12306. (point-at-eol) t)
  12307. (if (equal tags "")
  12308. (setq rpl "")
  12309. (goto-char (match-beginning 0))
  12310. (setq c0 (current-column)
  12311. ;; compute offset for the case of org-indent-mode active
  12312. di (if org-indent-mode
  12313. (* (1- org-indent-indentation-per-level) (1- level))
  12314. 0)
  12315. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  12316. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  12317. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
  12318. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  12319. (replace-match rpl t t)
  12320. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  12321. tags)
  12322. (t (error "Tags alignment failed")))
  12323. (org-move-to-column col)
  12324. (unless just-align
  12325. (run-hooks 'org-after-tags-change-hook)))))
  12326. (defun org-change-tag-in-region (beg end tag off)
  12327. "Add or remove TAG for each entry in the region.
  12328. This works in the agenda, and also in an org-mode buffer."
  12329. (interactive
  12330. (list (region-beginning) (region-end)
  12331. (let ((org-last-tags-completion-table
  12332. (if (eq major-mode 'org-mode)
  12333. (org-get-buffer-tags)
  12334. (org-global-tags-completion-table))))
  12335. (org-icompleting-read
  12336. "Tag: " 'org-tags-completion-function nil nil nil
  12337. 'org-tags-history))
  12338. (progn
  12339. (message "[s]et or [r]emove? ")
  12340. (equal (read-char-exclusive) ?r))))
  12341. (if (fboundp 'deactivate-mark) (deactivate-mark))
  12342. (let ((agendap (equal major-mode 'org-agenda-mode))
  12343. l1 l2 m buf pos newhead (cnt 0))
  12344. (goto-char end)
  12345. (setq l2 (1- (org-current-line)))
  12346. (goto-char beg)
  12347. (setq l1 (org-current-line))
  12348. (loop for l from l1 to l2 do
  12349. (org-goto-line l)
  12350. (setq m (get-text-property (point) 'org-hd-marker))
  12351. (when (or (and (eq major-mode 'org-mode) (org-at-heading-p))
  12352. (and agendap m))
  12353. (setq buf (if agendap (marker-buffer m) (current-buffer))
  12354. pos (if agendap m (point)))
  12355. (with-current-buffer buf
  12356. (save-excursion
  12357. (save-restriction
  12358. (goto-char pos)
  12359. (setq cnt (1+ cnt))
  12360. (org-toggle-tag tag (if off 'off 'on))
  12361. (setq newhead (org-get-heading)))))
  12362. (and agendap (org-agenda-change-all-lines newhead m))))
  12363. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  12364. (defun org-tags-completion-function (string predicate &optional flag)
  12365. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  12366. (confirm (lambda (x) (stringp (car x)))))
  12367. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  12368. (setq s1 (match-string 1 string)
  12369. s2 (match-string 2 string))
  12370. (setq s1 "" s2 string))
  12371. (cond
  12372. ((eq flag nil)
  12373. ;; try completion
  12374. (setq rtn (try-completion s2 ctable confirm))
  12375. (if (stringp rtn)
  12376. (setq rtn
  12377. (concat s1 s2 (substring rtn (length s2))
  12378. (if (and org-add-colon-after-tag-completion
  12379. (assoc rtn ctable))
  12380. ":" ""))))
  12381. rtn)
  12382. ((eq flag t)
  12383. ;; all-completions
  12384. (all-completions s2 ctable confirm)
  12385. )
  12386. ((eq flag 'lambda)
  12387. ;; exact match?
  12388. (assoc s2 ctable)))
  12389. ))
  12390. (defun org-fast-tag-insert (kwd tags face &optional end)
  12391. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  12392. (insert (format "%-12s" (concat kwd ":"))
  12393. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  12394. (or end "")))
  12395. (defun org-fast-tag-show-exit (flag)
  12396. (save-excursion
  12397. (org-goto-line 3)
  12398. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  12399. (replace-match ""))
  12400. (when flag
  12401. (end-of-line 1)
  12402. (org-move-to-column (- (window-width) 19) t)
  12403. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  12404. (defun org-set-current-tags-overlay (current prefix)
  12405. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  12406. (if (featurep 'xemacs)
  12407. (org-overlay-display org-tags-overlay (concat prefix s)
  12408. 'secondary-selection)
  12409. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  12410. (org-overlay-display org-tags-overlay (concat prefix s)))))
  12411. (defvar org-last-tag-selection-key nil)
  12412. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  12413. "Fast tag selection with single keys.
  12414. CURRENT is the current list of tags in the headline, INHERITED is the
  12415. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  12416. possibly with grouping information. TODO-TABLE is a similar table with
  12417. TODO keywords, should these have keys assigned to them.
  12418. If the keys are nil, a-z are automatically assigned.
  12419. Returns the new tags string, or nil to not change the current settings."
  12420. (let* ((fulltable (append table todo-table))
  12421. (maxlen (apply 'max (mapcar
  12422. (lambda (x)
  12423. (if (stringp (car x)) (string-width (car x)) 0))
  12424. fulltable)))
  12425. (buf (current-buffer))
  12426. (expert (eq org-fast-tag-selection-single-key 'expert))
  12427. (buffer-tags nil)
  12428. (fwidth (+ maxlen 3 1 3))
  12429. (ncol (/ (- (window-width) 4) fwidth))
  12430. (i-face 'org-done)
  12431. (c-face 'org-todo)
  12432. tg cnt e c char c1 c2 ntable tbl rtn
  12433. ov-start ov-end ov-prefix
  12434. (exit-after-next org-fast-tag-selection-single-key)
  12435. (done-keywords org-done-keywords)
  12436. groups ingroup)
  12437. (save-excursion
  12438. (beginning-of-line 1)
  12439. (if (looking-at
  12440. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12441. (setq ov-start (match-beginning 1)
  12442. ov-end (match-end 1)
  12443. ov-prefix "")
  12444. (setq ov-start (1- (point-at-eol))
  12445. ov-end (1+ ov-start))
  12446. (skip-chars-forward "^\n\r")
  12447. (setq ov-prefix
  12448. (concat
  12449. (buffer-substring (1- (point)) (point))
  12450. (if (> (current-column) org-tags-column)
  12451. " "
  12452. (make-string (- org-tags-column (current-column)) ?\ ))))))
  12453. (move-overlay org-tags-overlay ov-start ov-end)
  12454. (save-window-excursion
  12455. (if expert
  12456. (set-buffer (get-buffer-create " *Org tags*"))
  12457. (delete-other-windows)
  12458. (split-window-vertically)
  12459. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  12460. (erase-buffer)
  12461. (org-set-local 'org-done-keywords done-keywords)
  12462. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  12463. (org-fast-tag-insert "Current" current c-face "\n\n")
  12464. (org-fast-tag-show-exit exit-after-next)
  12465. (org-set-current-tags-overlay current ov-prefix)
  12466. (setq tbl fulltable char ?a cnt 0)
  12467. (while (setq e (pop tbl))
  12468. (cond
  12469. ((equal (car e) :startgroup)
  12470. (push '() groups) (setq ingroup t)
  12471. (when (not (= cnt 0))
  12472. (setq cnt 0)
  12473. (insert "\n"))
  12474. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  12475. ((equal (car e) :endgroup)
  12476. (setq ingroup nil cnt 0)
  12477. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  12478. ((equal e '(:newline))
  12479. (when (not (= cnt 0))
  12480. (setq cnt 0)
  12481. (insert "\n")
  12482. (setq e (car tbl))
  12483. (while (equal (car tbl) '(:newline))
  12484. (insert "\n")
  12485. (setq tbl (cdr tbl)))))
  12486. (t
  12487. (setq tg (copy-sequence (car e)) c2 nil)
  12488. (if (cdr e)
  12489. (setq c (cdr e))
  12490. ;; automatically assign a character.
  12491. (setq c1 (string-to-char
  12492. (downcase (substring
  12493. tg (if (= (string-to-char tg) ?@) 1 0)))))
  12494. (if (or (rassoc c1 ntable) (rassoc c1 table))
  12495. (while (or (rassoc char ntable) (rassoc char table))
  12496. (setq char (1+ char)))
  12497. (setq c2 c1))
  12498. (setq c (or c2 char)))
  12499. (if ingroup (push tg (car groups)))
  12500. (setq tg (org-add-props tg nil 'face
  12501. (cond
  12502. ((not (assoc tg table))
  12503. (org-get-todo-face tg))
  12504. ((member tg current) c-face)
  12505. ((member tg inherited) i-face)
  12506. (t nil))))
  12507. (if (and (= cnt 0) (not ingroup)) (insert " "))
  12508. (insert "[" c "] " tg (make-string
  12509. (- fwidth 4 (length tg)) ?\ ))
  12510. (push (cons tg c) ntable)
  12511. (when (= (setq cnt (1+ cnt)) ncol)
  12512. (insert "\n")
  12513. (if ingroup (insert " "))
  12514. (setq cnt 0)))))
  12515. (setq ntable (nreverse ntable))
  12516. (insert "\n")
  12517. (goto-char (point-min))
  12518. (if (not expert) (org-fit-window-to-buffer))
  12519. (setq rtn
  12520. (catch 'exit
  12521. (while t
  12522. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  12523. (if (not groups) "no " "")
  12524. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  12525. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  12526. (setq org-last-tag-selection-key c)
  12527. (cond
  12528. ((= c ?\r) (throw 'exit t))
  12529. ((= c ?!)
  12530. (setq groups (not groups))
  12531. (goto-char (point-min))
  12532. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  12533. ((= c ?\C-c)
  12534. (if (not expert)
  12535. (org-fast-tag-show-exit
  12536. (setq exit-after-next (not exit-after-next)))
  12537. (setq expert nil)
  12538. (delete-other-windows)
  12539. (set-window-buffer (split-window-vertically) " *Org tags*")
  12540. (org-switch-to-buffer-other-window " *Org tags*")
  12541. (org-fit-window-to-buffer)))
  12542. ((or (= c ?\C-g)
  12543. (and (= c ?q) (not (rassoc c ntable))))
  12544. (org-detach-overlay org-tags-overlay)
  12545. (setq quit-flag t))
  12546. ((= c ?\ )
  12547. (setq current nil)
  12548. (if exit-after-next (setq exit-after-next 'now)))
  12549. ((= c ?\t)
  12550. (condition-case nil
  12551. (setq tg (org-icompleting-read
  12552. "Tag: "
  12553. (or buffer-tags
  12554. (with-current-buffer buf
  12555. (org-get-buffer-tags)))))
  12556. (quit (setq tg "")))
  12557. (when (string-match "\\S-" tg)
  12558. (add-to-list 'buffer-tags (list tg))
  12559. (if (member tg current)
  12560. (setq current (delete tg current))
  12561. (push tg current)))
  12562. (if exit-after-next (setq exit-after-next 'now)))
  12563. ((setq e (rassoc c todo-table) tg (car e))
  12564. (with-current-buffer buf
  12565. (save-excursion (org-todo tg)))
  12566. (if exit-after-next (setq exit-after-next 'now)))
  12567. ((setq e (rassoc c ntable) tg (car e))
  12568. (if (member tg current)
  12569. (setq current (delete tg current))
  12570. (loop for g in groups do
  12571. (if (member tg g)
  12572. (mapc (lambda (x)
  12573. (setq current (delete x current)))
  12574. g)))
  12575. (push tg current))
  12576. (if exit-after-next (setq exit-after-next 'now))))
  12577. ;; Create a sorted list
  12578. (setq current
  12579. (sort current
  12580. (lambda (a b)
  12581. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  12582. (if (eq exit-after-next 'now) (throw 'exit t))
  12583. (goto-char (point-min))
  12584. (beginning-of-line 2)
  12585. (delete-region (point) (point-at-eol))
  12586. (org-fast-tag-insert "Current" current c-face)
  12587. (org-set-current-tags-overlay current ov-prefix)
  12588. (while (re-search-forward
  12589. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  12590. (setq tg (match-string 1))
  12591. (add-text-properties
  12592. (match-beginning 1) (match-end 1)
  12593. (list 'face
  12594. (cond
  12595. ((member tg current) c-face)
  12596. ((member tg inherited) i-face)
  12597. (t (get-text-property (match-beginning 1) 'face))))))
  12598. (goto-char (point-min)))))
  12599. (org-detach-overlay org-tags-overlay)
  12600. (if rtn
  12601. (mapconcat 'identity current ":")
  12602. nil))))
  12603. (defun org-get-tags-string ()
  12604. "Get the TAGS string in the current headline."
  12605. (unless (org-at-heading-p t)
  12606. (error "Not on a heading"))
  12607. (save-excursion
  12608. (beginning-of-line 1)
  12609. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12610. (org-match-string-no-properties 1)
  12611. "")))
  12612. (defun org-get-tags ()
  12613. "Get the list of tags specified in the current headline."
  12614. (org-split-string (org-get-tags-string) ":"))
  12615. (defun org-get-buffer-tags ()
  12616. "Get a table of all tags used in the buffer, for completion."
  12617. (let (tags)
  12618. (save-excursion
  12619. (goto-char (point-min))
  12620. (while (re-search-forward
  12621. (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
  12622. (when (equal (char-after (point-at-bol 0)) ?*)
  12623. (mapc (lambda (x) (add-to-list 'tags x))
  12624. (org-split-string (org-match-string-no-properties 1) ":")))))
  12625. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  12626. (mapcar 'list tags)))
  12627. ;;;; The mapping API
  12628. ;;;###autoload
  12629. (defun org-map-entries (func &optional match scope &rest skip)
  12630. "Call FUNC at each headline selected by MATCH in SCOPE.
  12631. FUNC is a function or a lisp form. The function will be called without
  12632. arguments, with the cursor positioned at the beginning of the headline.
  12633. The return values of all calls to the function will be collected and
  12634. returned as a list.
  12635. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  12636. does not need to preserve point. After evaluation, the cursor will be
  12637. moved to the end of the line (presumably of the headline of the
  12638. processed entry) and search continues from there. Under some
  12639. circumstances, this may not produce the wanted results. For example,
  12640. if you have removed (e.g. archived) the current (sub)tree it could
  12641. mean that the next entry will be skipped entirely. In such cases, you
  12642. can specify the position from where search should continue by making
  12643. FUNC set the variable `org-map-continue-from' to the desired buffer
  12644. position.
  12645. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  12646. Only headlines that are matched by this query will be considered during
  12647. the iteration. When MATCH is nil or t, all headlines will be
  12648. visited by the iteration.
  12649. SCOPE determines the scope of this command. It can be any of:
  12650. nil The current buffer, respecting the restriction if any
  12651. tree The subtree started with the entry at point
  12652. region The entries within the active region, if any
  12653. region-start-level
  12654. The entries within the active region, but only those at
  12655. the same level than the first one.
  12656. file The current buffer, without restriction
  12657. file-with-archives
  12658. The current buffer, and any archives associated with it
  12659. agenda All agenda files
  12660. agenda-with-archives
  12661. All agenda files with any archive files associated with them
  12662. \(file1 file2 ...)
  12663. If this is a list, all files in the list will be scanned
  12664. The remaining args are treated as settings for the skipping facilities of
  12665. the scanner. The following items can be given here:
  12666. archive skip trees with the archive tag.
  12667. comment skip trees with the COMMENT keyword
  12668. function or Emacs Lisp form:
  12669. will be used as value for `org-agenda-skip-function', so whenever
  12670. the function returns t, FUNC will not be called for that
  12671. entry and search will continue from the point where the
  12672. function leaves it.
  12673. If your function needs to retrieve the tags including inherited tags
  12674. at the *current* entry, you can use the value of the variable
  12675. `org-scanner-tags' which will be much faster than getting the value
  12676. with `org-get-tags-at'. If your function gets properties with
  12677. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  12678. to t around the call to `org-entry-properties' to get the same speedup.
  12679. Note that if your function moves around to retrieve tags and properties at
  12680. a *different* entry, you cannot use these techniques."
  12681. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  12682. (not (org-region-active-p)))
  12683. (let* ((org-agenda-archives-mode nil) ; just to make sure
  12684. (org-agenda-skip-archived-trees (memq 'archive skip))
  12685. (org-agenda-skip-comment-trees (memq 'comment skip))
  12686. (org-agenda-skip-function
  12687. (car (org-delete-all '(comment archive) skip)))
  12688. (org-tags-match-list-sublevels t)
  12689. (start-level (eq scope 'region-start-level))
  12690. matcher file res
  12691. org-todo-keywords-for-agenda
  12692. org-done-keywords-for-agenda
  12693. org-todo-keyword-alist-for-agenda
  12694. org-drawers-for-agenda
  12695. org-tag-alist-for-agenda
  12696. todo-only)
  12697. (cond
  12698. ((eq match t) (setq matcher t))
  12699. ((eq match nil) (setq matcher t))
  12700. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  12701. (save-excursion
  12702. (save-restriction
  12703. (cond ((eq scope 'tree)
  12704. (org-back-to-heading t)
  12705. (org-narrow-to-subtree)
  12706. (setq scope nil))
  12707. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  12708. (org-region-active-p))
  12709. ;; If needed, set start-level to a string like "2"
  12710. (when start-level
  12711. (save-excursion
  12712. (goto-char (region-beginning))
  12713. (unless (org-at-heading-p) (outline-next-heading))
  12714. (setq start-level (org-current-level))))
  12715. (narrow-to-region (region-beginning)
  12716. (save-excursion
  12717. (goto-char (region-end))
  12718. (unless (and (bolp) (org-at-heading-p))
  12719. (outline-next-heading))
  12720. (point)))
  12721. (setq scope nil)))
  12722. (if (not scope)
  12723. (progn
  12724. (org-prepare-agenda-buffers
  12725. (list (buffer-file-name (current-buffer))))
  12726. (setq res (org-scan-tags func matcher todo-only start-level)))
  12727. ;; Get the right scope
  12728. (cond
  12729. ((and scope (listp scope) (symbolp (car scope)))
  12730. (setq scope (eval scope)))
  12731. ((eq scope 'agenda)
  12732. (setq scope (org-agenda-files t)))
  12733. ((eq scope 'agenda-with-archives)
  12734. (setq scope (org-agenda-files t))
  12735. (setq scope (org-add-archive-files scope)))
  12736. ((eq scope 'file)
  12737. (setq scope (list (buffer-file-name))))
  12738. ((eq scope 'file-with-archives)
  12739. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  12740. (org-prepare-agenda-buffers scope)
  12741. (while (setq file (pop scope))
  12742. (with-current-buffer (org-find-base-buffer-visiting file)
  12743. (save-excursion
  12744. (save-restriction
  12745. (widen)
  12746. (goto-char (point-min))
  12747. (setq res (append res (org-scan-tags func matcher todo-only))))))))))
  12748. res)))
  12749. ;;;; Properties
  12750. ;;; Setting and retrieving properties
  12751. (defconst org-special-properties
  12752. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  12753. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM")
  12754. "The special properties valid in Org-mode.
  12755. These are properties that are not defined in the property drawer,
  12756. but in some other way.")
  12757. (defconst org-default-properties
  12758. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  12759. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  12760. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  12761. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  12762. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  12763. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  12764. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  12765. "Some properties that are used by Org-mode for various purposes.
  12766. Being in this list makes sure that they are offered for completion.")
  12767. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  12768. "Regular expression matching the first line of a property drawer.")
  12769. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  12770. "Regular expression matching the last line of a property drawer.")
  12771. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  12772. "Regular expression matching the first line of a property drawer.")
  12773. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  12774. "Regular expression matching the first line of a property drawer.")
  12775. (defconst org-property-drawer-re
  12776. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  12777. org-property-end-re "\\)\n?")
  12778. "Matches an entire property drawer.")
  12779. (defconst org-clock-drawer-re
  12780. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  12781. org-property-end-re "\\)\n?")
  12782. "Matches an entire clock drawer.")
  12783. (defsubst org-re-property (property)
  12784. "Return a regexp matching PROPERTY.
  12785. Match group 1 will be set to the value "
  12786. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)"))
  12787. (defun org-property-action ()
  12788. "Do an action on properties."
  12789. (interactive)
  12790. (let (c)
  12791. (org-at-property-p)
  12792. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  12793. (setq c (read-char-exclusive))
  12794. (cond
  12795. ((equal c ?s)
  12796. (call-interactively 'org-set-property))
  12797. ((equal c ?d)
  12798. (call-interactively 'org-delete-property))
  12799. ((equal c ?D)
  12800. (call-interactively 'org-delete-property-globally))
  12801. ((equal c ?c)
  12802. (call-interactively 'org-compute-property-at-point))
  12803. (t (error "No such property action %c" c)))))
  12804. (defun org-set-effort (&optional value)
  12805. "Set the effort property of the current entry.
  12806. With numerical prefix arg, use the nth allowed value, 0 stands for the 10th
  12807. allowed value."
  12808. (interactive "P")
  12809. (if (equal value 0) (setq value 10))
  12810. (let* ((completion-ignore-case t)
  12811. (prop org-effort-property)
  12812. (cur (org-entry-get nil prop))
  12813. (allowed (org-property-get-allowed-values nil prop 'table))
  12814. (existing (mapcar 'list (org-property-values prop)))
  12815. rpl
  12816. (val (cond
  12817. ((stringp value) value)
  12818. ((and allowed (integerp value))
  12819. (or (car (nth (1- value) allowed))
  12820. (car (org-last allowed))))
  12821. (allowed
  12822. (message "Select 1-9,0, [RET%s]: %s"
  12823. (if cur (concat "=" cur) "")
  12824. (mapconcat 'car allowed " "))
  12825. (setq rpl (read-char-exclusive))
  12826. (if (equal rpl ?\r)
  12827. cur
  12828. (setq rpl (- rpl ?0))
  12829. (if (equal rpl 0) (setq rpl 10))
  12830. (if (and (> rpl 0) (<= rpl (length allowed)))
  12831. (car (nth (1- rpl) allowed))
  12832. (org-completing-read "Effort: " allowed nil))))
  12833. (t
  12834. (let (org-completion-use-ido org-completion-use-iswitchb)
  12835. (org-completing-read
  12836. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  12837. (concat "[" cur "]") "")
  12838. ": ")
  12839. existing nil nil "" nil cur))))))
  12840. (unless (equal (org-entry-get nil prop) val)
  12841. (org-entry-put nil prop val))
  12842. (message "%s is now %s" prop val)))
  12843. (defun org-at-property-p ()
  12844. "Is cursor inside a property drawer?"
  12845. (save-excursion
  12846. (beginning-of-line 1)
  12847. (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
  12848. (save-match-data ;; Used by calling procedures
  12849. (let ((p (point))
  12850. (range (unless (org-before-first-heading-p)
  12851. (org-get-property-block))))
  12852. (and range (<= (car range) p) (< p (cdr range))))))))
  12853. (defun org-get-property-block (&optional beg end force)
  12854. "Return the (beg . end) range of the body of the property drawer.
  12855. BEG and END can be beginning and end of subtree, if not given
  12856. they will be found.
  12857. If the drawer does not exist and FORCE is non-nil, create the drawer."
  12858. (catch 'exit
  12859. (save-excursion
  12860. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  12861. (end (or end (progn (outline-next-heading) (point)))))
  12862. (goto-char beg)
  12863. (if (re-search-forward org-property-start-re end t)
  12864. (setq beg (1+ (match-end 0)))
  12865. (if force
  12866. (save-excursion
  12867. (org-insert-property-drawer)
  12868. (setq end (progn (outline-next-heading) (point))))
  12869. (throw 'exit nil))
  12870. (goto-char beg)
  12871. (if (re-search-forward org-property-start-re end t)
  12872. (setq beg (1+ (match-end 0)))))
  12873. (if (re-search-forward org-property-end-re end t)
  12874. (setq end (match-beginning 0))
  12875. (or force (throw 'exit nil))
  12876. (goto-char beg)
  12877. (setq end beg)
  12878. (org-indent-line-function)
  12879. (insert ":END:\n"))
  12880. (cons beg end)))))
  12881. (defun org-entry-properties (&optional pom which specific)
  12882. "Get all properties of the entry at point-or-marker POM.
  12883. This includes the TODO keyword, the tags, time strings for deadline,
  12884. scheduled, and clocking, and any additional properties defined in the
  12885. entry. The return value is an alist, keys may occur multiple times
  12886. if the property key was used several times.
  12887. POM may also be nil, in which case the current entry is used.
  12888. If WHICH is nil or `all', get all properties. If WHICH is
  12889. `special' or `standard', only get that subclass. If WHICH
  12890. is a string only get exactly this property. SPECIFIC can be a string, the
  12891. specific property we are interested in. Specifying it can speed
  12892. things up because then unnecessary parsing is avoided."
  12893. (setq which (or which 'all))
  12894. (org-with-point-at pom
  12895. (let ((clockstr (substring org-clock-string 0 -1))
  12896. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  12897. (case-fold-search nil)
  12898. beg end range props sum-props key key1 value string clocksum)
  12899. (save-excursion
  12900. (when (condition-case nil
  12901. (and (eq major-mode 'org-mode) (org-back-to-heading t))
  12902. (error nil))
  12903. (setq beg (point))
  12904. (setq sum-props (get-text-property (point) 'org-summaries))
  12905. (setq clocksum (get-text-property (point) :org-clock-minutes))
  12906. (outline-next-heading)
  12907. (setq end (point))
  12908. (when (memq which '(all special))
  12909. ;; Get the special properties, like TODO and tags
  12910. (goto-char beg)
  12911. (when (and (or (not specific) (string= specific "TODO"))
  12912. (looking-at org-todo-line-regexp) (match-end 2))
  12913. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  12914. (when (and (or (not specific) (string= specific "PRIORITY"))
  12915. (looking-at org-priority-regexp))
  12916. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  12917. (when (or (not specific) (string= specific "FILE"))
  12918. (push (cons "FILE" buffer-file-name) props))
  12919. (when (and (or (not specific) (string= specific "TAGS"))
  12920. (setq value (org-get-tags-string))
  12921. (string-match "\\S-" value))
  12922. (push (cons "TAGS" value) props))
  12923. (when (and (or (not specific) (string= specific "ALLTAGS"))
  12924. (setq value (org-get-tags-at)))
  12925. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  12926. ":"))
  12927. props))
  12928. (when (or (not specific) (string= specific "BLOCKED"))
  12929. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  12930. (when (or (not specific)
  12931. (member specific
  12932. '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
  12933. "TIMESTAMP" "TIMESTAMP_IA")))
  12934. (catch 'match
  12935. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  12936. (setq key (if (match-end 1)
  12937. (substring (org-match-string-no-properties 1)
  12938. 0 -1))
  12939. string (if (equal key clockstr)
  12940. (org-no-properties
  12941. (org-trim
  12942. (buffer-substring
  12943. (match-beginning 3) (goto-char
  12944. (point-at-eol)))))
  12945. (substring (org-match-string-no-properties 3)
  12946. 1 -1)))
  12947. ;; Get the correct property name from the key. This is
  12948. ;; necessary if the user has configured time keywords.
  12949. (setq key1 (concat key ":"))
  12950. (cond
  12951. ((not key)
  12952. (setq key
  12953. (if (= (char-after (match-beginning 3)) ?\[)
  12954. "TIMESTAMP_IA" "TIMESTAMP")))
  12955. ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
  12956. ((equal key1 org-deadline-string) (setq key "DEADLINE"))
  12957. ((equal key1 org-closed-string) (setq key "CLOSED"))
  12958. ((equal key1 org-clock-string) (setq key "CLOCK")))
  12959. (if (and specific (equal key specific) (not (equal key "CLOCK")))
  12960. (progn
  12961. (push (cons key string) props)
  12962. ;; no need to search further if match is found
  12963. (throw 'match t))
  12964. (when (or (equal key "CLOCK") (not (assoc key props)))
  12965. (push (cons key string) props))))))
  12966. )
  12967. (when (memq which '(all standard))
  12968. ;; Get the standard properties, like :PROP: ...
  12969. (setq range (org-get-property-block beg end))
  12970. (when range
  12971. (goto-char (car range))
  12972. (while (re-search-forward
  12973. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  12974. (cdr range) t)
  12975. (setq key (org-match-string-no-properties 1)
  12976. value (org-trim (or (org-match-string-no-properties 2) "")))
  12977. (unless (member key excluded)
  12978. (push (cons key (or value "")) props)))))
  12979. (if clocksum
  12980. (push (cons "CLOCKSUM"
  12981. (org-columns-number-to-string (/ (float clocksum) 60.)
  12982. 'add_times))
  12983. props))
  12984. (unless (assoc "CATEGORY" props)
  12985. (push (cons "CATEGORY" (org-get-category)) props))
  12986. (append sum-props (nreverse props)))))))
  12987. (defun org-entry-get (pom property &optional inherit literal-nil)
  12988. "Get value of PROPERTY for entry at point-or-marker POM.
  12989. If INHERIT is non-nil and the entry does not have the property,
  12990. then also check higher levels of the hierarchy.
  12991. If INHERIT is the symbol `selective', use inheritance only if the setting
  12992. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  12993. If the property is present but empty, the return value is the empty string.
  12994. If the property is not present at all, nil is returned.
  12995. If LITERAL-NIL is set, return the string value \"nil\" as a string,
  12996. do not interpret it as the list atom nil. This is used for inheritance
  12997. when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
  12998. (org-with-point-at pom
  12999. (if (and inherit (if (eq inherit 'selective)
  13000. (org-property-inherit-p property)
  13001. t))
  13002. (org-entry-get-with-inheritance property literal-nil)
  13003. (if (member property org-special-properties)
  13004. ;; We need a special property. Use `org-entry-properties' to
  13005. ;; retrieve it, but specify the wanted property
  13006. (cdr (assoc property (org-entry-properties nil 'special property)))
  13007. (let ((range (unless (org-before-first-heading-p)
  13008. (org-get-property-block)))
  13009. (props (list (or (assoc property org-file-properties)
  13010. (assoc property org-global-properties)
  13011. (assoc property org-global-properties-fixed))))
  13012. val)
  13013. (flet ((ap (key)
  13014. (when (re-search-forward
  13015. (org-re-property key) (cdr range) t)
  13016. (setq props
  13017. (org-update-property-plist
  13018. key
  13019. (if (match-end 1)
  13020. (org-match-string-no-properties 1) "")
  13021. props)))))
  13022. (when (and range (goto-char (car range)))
  13023. (ap property)
  13024. (goto-char (car range))
  13025. (while (ap (concat property "+")))
  13026. (setq val (cdr (assoc property props)))
  13027. (when val (if literal-nil val (org-not-nil val))))))))))
  13028. (defun org-property-or-variable-value (var &optional inherit)
  13029. "Check if there is a property fixing the value of VAR.
  13030. If yes, return this value. If not, return the current value of the variable."
  13031. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  13032. (if (and prop (stringp prop) (string-match "\\S-" prop))
  13033. (read prop)
  13034. (symbol-value var))))
  13035. (defun org-entry-delete (pom property)
  13036. "Delete the property PROPERTY from entry at point-or-marker POM."
  13037. (org-with-point-at pom
  13038. (if (member property org-special-properties)
  13039. nil ; cannot delete these properties.
  13040. (let ((range (org-get-property-block)))
  13041. (if (and range
  13042. (goto-char (car range))
  13043. (re-search-forward
  13044. (org-re-property property)
  13045. (cdr range) t))
  13046. (progn
  13047. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  13048. t)
  13049. nil)))))
  13050. ;; Multi-values properties are properties that contain multiple values
  13051. ;; These values are assumed to be single words, separated by whitespace.
  13052. (defun org-entry-add-to-multivalued-property (pom property value)
  13053. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  13054. (let* ((old (org-entry-get pom property))
  13055. (values (and old (org-split-string old "[ \t]"))))
  13056. (setq value (org-entry-protect-space value))
  13057. (unless (member value values)
  13058. (setq values (cons value values))
  13059. (org-entry-put pom property
  13060. (mapconcat 'identity values " ")))))
  13061. (defun org-entry-remove-from-multivalued-property (pom property value)
  13062. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  13063. (let* ((old (org-entry-get pom property))
  13064. (values (and old (org-split-string old "[ \t]"))))
  13065. (setq value (org-entry-protect-space value))
  13066. (when (member value values)
  13067. (setq values (delete value values))
  13068. (org-entry-put pom property
  13069. (mapconcat 'identity values " ")))))
  13070. (defun org-entry-member-in-multivalued-property (pom property value)
  13071. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  13072. (let* ((old (org-entry-get pom property))
  13073. (values (and old (org-split-string old "[ \t]"))))
  13074. (setq value (org-entry-protect-space value))
  13075. (member value values)))
  13076. (defun org-entry-get-multivalued-property (pom property)
  13077. "Return a list of values in a multivalued property."
  13078. (let* ((value (org-entry-get pom property))
  13079. (values (and value (org-split-string value "[ \t]"))))
  13080. (mapcar 'org-entry-restore-space values)))
  13081. (defun org-entry-put-multivalued-property (pom property &rest values)
  13082. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  13083. VALUES should be a list of strings. Spaces will be protected."
  13084. (org-entry-put pom property
  13085. (mapconcat 'org-entry-protect-space values " "))
  13086. (let* ((value (org-entry-get pom property))
  13087. (values (and value (org-split-string value "[ \t]"))))
  13088. (mapcar 'org-entry-restore-space values)))
  13089. (defun org-entry-protect-space (s)
  13090. "Protect spaces and newline in string S."
  13091. (while (string-match " " s)
  13092. (setq s (replace-match "%20" t t s)))
  13093. (while (string-match "\n" s)
  13094. (setq s (replace-match "%0A" t t s)))
  13095. s)
  13096. (defun org-entry-restore-space (s)
  13097. "Restore spaces and newline in string S."
  13098. (while (string-match "%20" s)
  13099. (setq s (replace-match " " t t s)))
  13100. (while (string-match "%0A" s)
  13101. (setq s (replace-match "\n" t t s)))
  13102. s)
  13103. (defvar org-entry-property-inherited-from (make-marker)
  13104. "Marker pointing to the entry from where a property was inherited.
  13105. Each call to `org-entry-get-with-inheritance' will set this marker to the
  13106. location of the entry where the inheritance search matched. If there was
  13107. no match, the marker will point nowhere.
  13108. Note that also `org-entry-get' calls this function, if the INHERIT flag
  13109. is set.")
  13110. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  13111. "Get entry property, and search higher levels if not present.
  13112. The search will stop at the first ancestor which has the property defined.
  13113. If the value found is \"nil\", return nil to show that the property
  13114. should be considered as undefined (this is the meaning of nil here).
  13115. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  13116. (move-marker org-entry-property-inherited-from nil)
  13117. (let (tmp)
  13118. (unless (org-before-first-heading-p)
  13119. (save-excursion
  13120. (save-restriction
  13121. (widen)
  13122. (catch 'ex
  13123. (while t
  13124. (when (setq tmp (org-entry-get nil property nil 'literal-nil))
  13125. (org-back-to-heading t)
  13126. (move-marker org-entry-property-inherited-from (point))
  13127. (throw 'ex tmp))
  13128. (or (org-up-heading-safe) (throw 'ex nil)))))))
  13129. (setq tmp (or tmp
  13130. (cdr (assoc property org-file-properties))
  13131. (cdr (assoc property org-global-properties))
  13132. (cdr (assoc property org-global-properties-fixed))))
  13133. (if literal-nil tmp (org-not-nil tmp))))
  13134. (defvar org-property-changed-functions nil
  13135. "Hook called when the value of a property has changed.
  13136. Each hook function should accept two arguments, the name of the property
  13137. and the new value.")
  13138. (defun org-entry-put (pom property value)
  13139. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  13140. (org-with-point-at pom
  13141. (org-back-to-heading t)
  13142. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  13143. range)
  13144. (cond
  13145. ((equal property "TODO")
  13146. (when (and (stringp value) (string-match "\\S-" value)
  13147. (not (member value org-todo-keywords-1)))
  13148. (error "\"%s\" is not a valid TODO state" value))
  13149. (if (or (not value)
  13150. (not (string-match "\\S-" value)))
  13151. (setq value 'none))
  13152. (org-todo value)
  13153. (org-set-tags nil 'align))
  13154. ((equal property "PRIORITY")
  13155. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  13156. (string-to-char value) ?\ ))
  13157. (org-set-tags nil 'align))
  13158. ((equal property "SCHEDULED")
  13159. (if (re-search-forward org-scheduled-time-regexp end t)
  13160. (cond
  13161. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13162. ((eq value 'later) (org-timestamp-change 1 'day))
  13163. (t (call-interactively 'org-schedule)))
  13164. (call-interactively 'org-schedule)))
  13165. ((equal property "DEADLINE")
  13166. (if (re-search-forward org-deadline-time-regexp end t)
  13167. (cond
  13168. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13169. ((eq value 'later) (org-timestamp-change 1 'day))
  13170. (t (call-interactively 'org-deadline)))
  13171. (call-interactively 'org-deadline)))
  13172. ((member property org-special-properties)
  13173. (error "The %s property can not yet be set with `org-entry-put'"
  13174. property))
  13175. (t ; a non-special property
  13176. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  13177. (setq range (org-get-property-block beg end 'force))
  13178. (goto-char (car range))
  13179. (if (re-search-forward
  13180. (org-re-property property) (cdr range) t)
  13181. (progn
  13182. (delete-region (match-beginning 0) (match-end 0))
  13183. (goto-char (match-beginning 0)))
  13184. (goto-char (cdr range))
  13185. (insert "\n")
  13186. (backward-char 1)
  13187. (org-indent-line-function))
  13188. (insert ":" property ":")
  13189. (and value (insert " " value))
  13190. (org-indent-line-function)))))
  13191. (run-hook-with-args 'org-property-changed-functions property value)))
  13192. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  13193. "Get all property keys in the current buffer.
  13194. With INCLUDE-SPECIALS, also list the special properties that reflect things
  13195. like tags and TODO state.
  13196. With INCLUDE-DEFAULTS, also include properties that has special meaning
  13197. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
  13198. and others.
  13199. With INCLUDE-COLUMNS, also include property names given in COLUMN
  13200. formats in the current buffer."
  13201. (let (rtn range cfmt s p)
  13202. (save-excursion
  13203. (save-restriction
  13204. (widen)
  13205. (goto-char (point-min))
  13206. (while (re-search-forward org-property-start-re nil t)
  13207. (setq range (org-get-property-block))
  13208. (goto-char (car range))
  13209. (while (re-search-forward
  13210. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  13211. (cdr range) t)
  13212. (add-to-list 'rtn (org-match-string-no-properties 1)))
  13213. (outline-next-heading))))
  13214. (when include-specials
  13215. (setq rtn (append org-special-properties rtn)))
  13216. (when include-defaults
  13217. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  13218. (add-to-list 'rtn org-effort-property))
  13219. (when include-columns
  13220. (save-excursion
  13221. (save-restriction
  13222. (widen)
  13223. (goto-char (point-min))
  13224. (while (re-search-forward
  13225. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  13226. nil t)
  13227. (setq cfmt (match-string 2) s 0)
  13228. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  13229. cfmt s)
  13230. (setq s (match-end 0)
  13231. p (match-string 1 cfmt))
  13232. (unless (or (equal p "ITEM")
  13233. (member p org-special-properties))
  13234. (add-to-list 'rtn (match-string 1 cfmt))))))))
  13235. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  13236. (defun org-property-values (key)
  13237. "Return a list of all values of property KEY in the current buffer."
  13238. (save-excursion
  13239. (save-restriction
  13240. (widen)
  13241. (goto-char (point-min))
  13242. (let ((re (org-re-property key))
  13243. values)
  13244. (while (re-search-forward re nil t)
  13245. (add-to-list 'values (org-trim (match-string 1))))
  13246. (delete "" values)))))
  13247. (defun org-insert-property-drawer ()
  13248. "Insert a property drawer into the current entry."
  13249. (org-back-to-heading t)
  13250. (looking-at org-outline-regexp)
  13251. (let ((indent (if org-adapt-indentation
  13252. (- (match-end 0) (match-beginning 0))
  13253. 0))
  13254. (beg (point))
  13255. (re (concat "^[ \t]*" org-keyword-time-regexp))
  13256. end hiddenp)
  13257. (outline-next-heading)
  13258. (setq end (point))
  13259. (goto-char beg)
  13260. (while (re-search-forward re end t))
  13261. (setq hiddenp (outline-invisible-p))
  13262. (end-of-line 1)
  13263. (and (equal (char-after) ?\n) (forward-char 1))
  13264. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  13265. (if (member (match-string 1) '("CLOCK:" ":END:"))
  13266. ;; just skip this line
  13267. (beginning-of-line 2)
  13268. ;; Drawer start, find the end
  13269. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  13270. (beginning-of-line 1)))
  13271. (org-skip-over-state-notes)
  13272. (skip-chars-backward " \t\n\r")
  13273. (if (eq (char-before) ?*) (forward-char 1))
  13274. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  13275. (beginning-of-line 0)
  13276. (org-indent-to-column indent)
  13277. (beginning-of-line 2)
  13278. (org-indent-to-column indent)
  13279. (beginning-of-line 0)
  13280. (if hiddenp
  13281. (save-excursion
  13282. (org-back-to-heading t)
  13283. (hide-entry))
  13284. (org-flag-drawer t))))
  13285. (defun org-insert-drawer (&optional arg drawer)
  13286. "Insert a drawer at point.
  13287. Optional argument DRAWER, when non-nil, is a string representing
  13288. drawer's name. Otherwise, the user is prompted for a name.
  13289. If a region is active, insert the drawer around that region
  13290. instead.
  13291. Point is left between drawer's boundaries."
  13292. (interactive "P")
  13293. (let* ((logbook (if (stringp org-log-into-drawer) org-log-into-drawer
  13294. "LOGBOOK"))
  13295. ;; SYSTEM-DRAWERS is a list of drawer names that are used
  13296. ;; internally by Org. They are meant to be inserted
  13297. ;; automatically.
  13298. (system-drawers `("CLOCK" ,logbook "PROPERTIES"))
  13299. ;; Remove system drawers from list. Note: For some reason,
  13300. ;; `org-completing-read' ignores the predicate while
  13301. ;; `completing-read' handles it fine.
  13302. (drawer (if arg "PROPERTIES"
  13303. (or drawer
  13304. (completing-read
  13305. "Drawer: " org-drawers
  13306. (lambda (d) (not (member d system-drawers))))))))
  13307. (cond
  13308. ;; With C-u, fall back on `org-insert-property-drawer'
  13309. (arg (org-insert-property-drawer))
  13310. ;; With an active region, insert a drawer at point.
  13311. ((not (org-region-active-p))
  13312. (progn
  13313. (unless (bolp) (insert "\n"))
  13314. (insert (format ":%s:\n\n:END:\n" drawer))
  13315. (forward-line -2)))
  13316. ;; Otherwise, insert the drawer at point
  13317. (t
  13318. (let ((rbeg (region-beginning))
  13319. (rend (copy-marker (region-end))))
  13320. (unwind-protect
  13321. (progn
  13322. (goto-char rbeg)
  13323. (beginning-of-line)
  13324. (when (save-excursion
  13325. (re-search-forward org-outline-regexp-bol rend t))
  13326. (error "Drawers cannot contain headlines"))
  13327. ;; Position point at the beginning of the first
  13328. ;; non-blank line in region. Insert drawer's opening
  13329. ;; there, then indent it.
  13330. (org-skip-whitespace)
  13331. (beginning-of-line)
  13332. (insert ":" drawer ":\n")
  13333. (forward-line -1)
  13334. (indent-for-tab-command)
  13335. ;; Move point to the beginning of the first blank line
  13336. ;; after the last non-blank line in region. Insert
  13337. ;; drawer's closing, then indent it.
  13338. (goto-char rend)
  13339. (skip-chars-backward " \r\t\n")
  13340. (insert "\n:END:")
  13341. (indent-for-tab-command)
  13342. (unless (eolp) (insert "\n")))
  13343. ;; Clear marker, whatever the outcome of insertion is.
  13344. (set-marker rend nil)))))))
  13345. (defvar org-property-set-functions-alist nil
  13346. "Property set function alist.
  13347. Each entry should have the following format:
  13348. (PROPERTY . READ-FUNCTION)
  13349. The read function will be called with the same argument as
  13350. `org-completing-read'.")
  13351. (defun org-set-property-function (property)
  13352. "Get the function that should be used to set PROPERTY.
  13353. This is computed according to `org-property-set-functions-alist'."
  13354. (or (cdr (assoc property org-property-set-functions-alist))
  13355. 'org-completing-read))
  13356. (defun org-read-property-value (property)
  13357. "Read PROPERTY value from user."
  13358. (let* ((completion-ignore-case t)
  13359. (allowed (org-property-get-allowed-values nil property 'table))
  13360. (cur (org-entry-get nil property))
  13361. (prompt (concat property " value"
  13362. (if (and cur (string-match "\\S-" cur))
  13363. (concat " [" cur "]") "") ": "))
  13364. (set-function (org-set-property-function property))
  13365. (val (if allowed
  13366. (funcall set-function prompt allowed nil
  13367. (not (get-text-property 0 'org-unrestricted
  13368. (caar allowed))))
  13369. (let (org-completion-use-ido org-completion-use-iswitchb)
  13370. (funcall set-function prompt
  13371. (mapcar 'list (org-property-values property))
  13372. nil nil "" nil cur)))))
  13373. (if (equal val "")
  13374. cur
  13375. val)))
  13376. (defvar org-last-set-property nil)
  13377. (defun org-read-property-name ()
  13378. "Read a property name."
  13379. (let* ((completion-ignore-case t)
  13380. (keys (org-buffer-property-keys nil t t))
  13381. (default-prop (or (save-excursion
  13382. (save-match-data
  13383. (beginning-of-line)
  13384. (and (looking-at "^\\s-*:\\([^:\n]+\\):")
  13385. (null (string= (match-string 1) "END"))
  13386. (match-string 1))))
  13387. org-last-set-property))
  13388. (property (org-icompleting-read
  13389. (concat "Property"
  13390. (if default-prop (concat " [" default-prop "]") "")
  13391. ": ")
  13392. (mapcar 'list keys)
  13393. nil nil nil nil
  13394. default-prop
  13395. )))
  13396. (if (member property keys)
  13397. property
  13398. (or (cdr (assoc (downcase property)
  13399. (mapcar (lambda (x) (cons (downcase x) x))
  13400. keys)))
  13401. property))))
  13402. (defun org-set-property (property value)
  13403. "In the current entry, set PROPERTY to VALUE.
  13404. When called interactively, this will prompt for a property name, offering
  13405. completion on existing and default properties. And then it will prompt
  13406. for a value, offering completion either on allowed values (via an inherited
  13407. xxx_ALL property) or on existing values in other instances of this property
  13408. in the current file."
  13409. (interactive (list nil nil))
  13410. (let* ((property (or property (org-read-property-name)))
  13411. (value (or value (org-read-property-value property)))
  13412. (fn (assoc property org-properties-postprocess-alist)))
  13413. (setq org-last-set-property property)
  13414. ;; Possibly postprocess the inserted value:
  13415. (when fn (setq value (funcall (cadr fn) value)))
  13416. (unless (equal (org-entry-get nil property) value)
  13417. (org-entry-put nil property value))))
  13418. (defun org-delete-property (property)
  13419. "In the current entry, delete PROPERTY."
  13420. (interactive
  13421. (let* ((completion-ignore-case t)
  13422. (prop (org-icompleting-read "Property: "
  13423. (org-entry-properties nil 'standard))))
  13424. (list prop)))
  13425. (message "Property %s %s" property
  13426. (if (org-entry-delete nil property)
  13427. "deleted"
  13428. "was not present in the entry")))
  13429. (defun org-delete-property-globally (property)
  13430. "Remove PROPERTY globally, from all entries."
  13431. (interactive
  13432. (let* ((completion-ignore-case t)
  13433. (prop (org-icompleting-read
  13434. "Globally remove property: "
  13435. (mapcar 'list (org-buffer-property-keys)))))
  13436. (list prop)))
  13437. (save-excursion
  13438. (save-restriction
  13439. (widen)
  13440. (goto-char (point-min))
  13441. (let ((cnt 0))
  13442. (while (re-search-forward
  13443. (org-re-property property)
  13444. nil t)
  13445. (setq cnt (1+ cnt))
  13446. (delete-region (match-beginning 0) (1+ (point-at-eol))))
  13447. (message "Property \"%s\" removed from %d entries" property cnt)))))
  13448. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  13449. (defun org-compute-property-at-point ()
  13450. "Compute the property at point.
  13451. This looks for an enclosing column format, extracts the operator and
  13452. then applies it to the property in the column format's scope."
  13453. (interactive)
  13454. (unless (org-at-property-p)
  13455. (error "Not at a property"))
  13456. (let ((prop (org-match-string-no-properties 2)))
  13457. (org-columns-get-format-and-top-level)
  13458. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  13459. (error "No operator defined for property %s" prop))
  13460. (org-columns-compute prop)))
  13461. (defvar org-property-allowed-value-functions nil
  13462. "Hook for functions supplying allowed values for a specific property.
  13463. The functions must take a single argument, the name of the property, and
  13464. return a flat list of allowed values. If \":ETC\" is one of
  13465. the values, this means that these values are intended as defaults for
  13466. completion, but that other values should be allowed too.
  13467. The functions must return nil if they are not responsible for this
  13468. property.")
  13469. (defun org-property-get-allowed-values (pom property &optional table)
  13470. "Get allowed values for the property PROPERTY.
  13471. When TABLE is non-nil, return an alist that can directly be used for
  13472. completion."
  13473. (let (vals)
  13474. (cond
  13475. ((equal property "TODO")
  13476. (setq vals (org-with-point-at pom
  13477. (append org-todo-keywords-1 '("")))))
  13478. ((equal property "PRIORITY")
  13479. (let ((n org-lowest-priority))
  13480. (while (>= n org-highest-priority)
  13481. (push (char-to-string n) vals)
  13482. (setq n (1- n)))))
  13483. ((member property org-special-properties))
  13484. ((setq vals (run-hook-with-args-until-success
  13485. 'org-property-allowed-value-functions property)))
  13486. (t
  13487. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  13488. (when (and vals (string-match "\\S-" vals))
  13489. (setq vals (car (read-from-string (concat "(" vals ")"))))
  13490. (setq vals (mapcar (lambda (x)
  13491. (cond ((stringp x) x)
  13492. ((numberp x) (number-to-string x))
  13493. ((symbolp x) (symbol-name x))
  13494. (t "???")))
  13495. vals)))))
  13496. (when (member ":ETC" vals)
  13497. (setq vals (remove ":ETC" vals))
  13498. (org-add-props (car vals) '(org-unrestricted t)))
  13499. (if table (mapcar 'list vals) vals)))
  13500. (defun org-property-previous-allowed-value (&optional previous)
  13501. "Switch to the next allowed value for this property."
  13502. (interactive)
  13503. (org-property-next-allowed-value t))
  13504. (defun org-property-next-allowed-value (&optional previous)
  13505. "Switch to the next allowed value for this property."
  13506. (interactive)
  13507. (unless (org-at-property-p)
  13508. (error "Not at a property"))
  13509. (let* ((key (match-string 2))
  13510. (value (match-string 3))
  13511. (allowed (or (org-property-get-allowed-values (point) key)
  13512. (and (member value '("[ ]" "[-]" "[X]"))
  13513. '("[ ]" "[X]"))))
  13514. nval)
  13515. (unless allowed
  13516. (error "Allowed values for this property have not been defined"))
  13517. (if previous (setq allowed (reverse allowed)))
  13518. (if (member value allowed)
  13519. (setq nval (car (cdr (member value allowed)))))
  13520. (setq nval (or nval (car allowed)))
  13521. (if (equal nval value)
  13522. (error "Only one allowed value for this property"))
  13523. (org-at-property-p)
  13524. (replace-match (concat " :" key ": " nval) t t)
  13525. (org-indent-line-function)
  13526. (beginning-of-line 1)
  13527. (skip-chars-forward " \t")
  13528. (run-hook-with-args 'org-property-changed-functions key nval)))
  13529. (defun org-find-olp (path &optional this-buffer)
  13530. "Return a marker pointing to the entry at outline path OLP.
  13531. If anything goes wrong, throw an error.
  13532. You can wrap this call to catch the error like this:
  13533. (condition-case msg
  13534. (org-mobile-locate-entry (match-string 4))
  13535. (error (nth 1 msg)))
  13536. The return value will then be either a string with the error message,
  13537. or a marker if everything is OK.
  13538. If THIS-BUFFER is set, the outline path does not contain a file,
  13539. only headings."
  13540. (let* ((file (if this-buffer buffer-file-name (pop path)))
  13541. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  13542. (level 1)
  13543. (lmin 1)
  13544. (lmax 1)
  13545. limit re end found pos heading cnt flevel)
  13546. (unless buffer (error "File not found :%s" file))
  13547. (with-current-buffer buffer
  13548. (save-excursion
  13549. (save-restriction
  13550. (widen)
  13551. (setq limit (point-max))
  13552. (goto-char (point-min))
  13553. (while (setq heading (pop path))
  13554. (setq re (format org-complex-heading-regexp-format
  13555. (regexp-quote heading)))
  13556. (setq cnt 0 pos (point))
  13557. (while (re-search-forward re end t)
  13558. (setq level (- (match-end 1) (match-beginning 1)))
  13559. (if (and (>= level lmin) (<= level lmax))
  13560. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  13561. (when (= cnt 0) (error "Heading not found on level %d: %s"
  13562. lmax heading))
  13563. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  13564. lmax heading))
  13565. (goto-char found)
  13566. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  13567. (setq end (save-excursion (org-end-of-subtree t t))))
  13568. (when (org-at-heading-p)
  13569. (move-marker (make-marker) (point))))))))
  13570. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  13571. "Find node HEADING in BUFFER.
  13572. Return a marker to the heading if it was found, or nil if not.
  13573. If POS-ONLY is set, return just the position instead of a marker.
  13574. The heading text must match exact, but it may have a TODO keyword,
  13575. a priority cookie and tags in the standard locations."
  13576. (with-current-buffer (or buffer (current-buffer))
  13577. (save-excursion
  13578. (save-restriction
  13579. (widen)
  13580. (goto-char (point-min))
  13581. (let (case-fold-search)
  13582. (if (re-search-forward
  13583. (format org-complex-heading-regexp-format
  13584. (regexp-quote heading)) nil t)
  13585. (if pos-only
  13586. (match-beginning 0)
  13587. (move-marker (make-marker) (match-beginning 0)))))))))
  13588. (defun org-find-exact-heading-in-directory (heading &optional dir)
  13589. "Find Org node headline HEADING in all .org files in directory DIR.
  13590. When the target headline is found, return a marker to this location."
  13591. (let ((files (directory-files (or dir default-directory)
  13592. nil "\\`[^.#].*\\.org\\'"))
  13593. file visiting m buffer)
  13594. (catch 'found
  13595. (while (setq file (pop files))
  13596. (message "trying %s" file)
  13597. (setq visiting (org-find-base-buffer-visiting file))
  13598. (setq buffer (or visiting (find-file-noselect file)))
  13599. (setq m (org-find-exact-headline-in-buffer
  13600. heading buffer))
  13601. (when (and (not m) (not visiting)) (kill-buffer buffer))
  13602. (and m (throw 'found m))))))
  13603. (defun org-find-entry-with-id (ident)
  13604. "Locate the entry that contains the ID property with exact value IDENT.
  13605. IDENT can be a string, a symbol or a number, this function will search for
  13606. the string representation of it.
  13607. Return the position where this entry starts, or nil if there is no such entry."
  13608. (interactive "sID: ")
  13609. (let ((id (cond
  13610. ((stringp ident) ident)
  13611. ((symbol-name ident) (symbol-name ident))
  13612. ((numberp ident) (number-to-string ident))
  13613. (t (error "IDENT %s must be a string, symbol or number" ident))))
  13614. (case-fold-search nil))
  13615. (save-excursion
  13616. (save-restriction
  13617. (widen)
  13618. (goto-char (point-min))
  13619. (when (re-search-forward
  13620. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  13621. nil t)
  13622. (org-back-to-heading t)
  13623. (point))))))
  13624. ;;;; Timestamps
  13625. (defvar org-last-changed-timestamp nil)
  13626. (defvar org-last-inserted-timestamp nil
  13627. "The last time stamp inserted with `org-insert-time-stamp'.")
  13628. (defvar org-time-was-given) ; dynamically scoped parameter
  13629. (defvar org-end-time-was-given) ; dynamically scoped parameter
  13630. (defvar org-ts-what) ; dynamically scoped parameter
  13631. (defun org-time-stamp (arg &optional inactive)
  13632. "Prompt for a date/time and insert a time stamp.
  13633. If the user specifies a time like HH:MM, or if this command is called
  13634. with a prefix argument, the time stamp will contain date and time.
  13635. Otherwise, only the date will be included. All parts of a date not
  13636. specified by the user will be filled in from the current date/time.
  13637. So if you press just return without typing anything, the time stamp
  13638. will represent the current date/time. If there is already a timestamp
  13639. at the cursor, it will be modified."
  13640. (interactive "P")
  13641. (let* ((ts nil)
  13642. (default-time
  13643. ;; Default time is either today, or, when entering a range,
  13644. ;; the range start.
  13645. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  13646. (save-excursion
  13647. (re-search-backward
  13648. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  13649. (- (point) 20) t)))
  13650. (apply 'encode-time (org-parse-time-string (match-string 1)))
  13651. (current-time)))
  13652. (default-input (and ts (org-get-compact-tod ts)))
  13653. (repeater (save-excursion
  13654. (save-match-data
  13655. (beginning-of-line)
  13656. (when (re-search-forward
  13657. "\\([.+-]+[0-9]+[dwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[dwmy]\\)?\\) ?"
  13658. (save-excursion (progn (end-of-line) (point))) t)
  13659. (match-string 0)))))
  13660. org-time-was-given org-end-time-was-given time)
  13661. (cond
  13662. ((and (org-at-timestamp-p t)
  13663. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  13664. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  13665. (insert "--")
  13666. (setq time (let ((this-command this-command))
  13667. (org-read-date arg 'totime nil nil
  13668. default-time default-input inactive)))
  13669. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  13670. ((org-at-timestamp-p t)
  13671. (setq time (let ((this-command this-command))
  13672. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  13673. (when (org-at-timestamp-p t) ; just to get the match data
  13674. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  13675. (replace-match "")
  13676. (setq org-last-changed-timestamp
  13677. (org-insert-time-stamp
  13678. time (or org-time-was-given arg)
  13679. inactive nil nil (list org-end-time-was-given)))
  13680. (when repeater (goto-char (1- (point))) (insert " " repeater)
  13681. (setq org-last-changed-timestamp
  13682. (concat (substring org-last-inserted-timestamp 0 -1)
  13683. " " repeater ">"))))
  13684. (message "Timestamp updated"))
  13685. (t
  13686. (setq time (let ((this-command this-command))
  13687. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  13688. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  13689. nil nil (list org-end-time-was-given))))))
  13690. ;; FIXME: can we use this for something else, like computing time differences?
  13691. (defun org-get-compact-tod (s)
  13692. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  13693. (let* ((t1 (match-string 1 s))
  13694. (h1 (string-to-number (match-string 2 s)))
  13695. (m1 (string-to-number (match-string 3 s)))
  13696. (t2 (and (match-end 4) (match-string 5 s)))
  13697. (h2 (and t2 (string-to-number (match-string 6 s))))
  13698. (m2 (and t2 (string-to-number (match-string 7 s))))
  13699. dh dm)
  13700. (if (not t2)
  13701. t1
  13702. (setq dh (- h2 h1) dm (- m2 m1))
  13703. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  13704. (concat t1 "+" (number-to-string dh)
  13705. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  13706. (defun org-time-stamp-inactive (&optional arg)
  13707. "Insert an inactive time stamp.
  13708. An inactive time stamp is enclosed in square brackets instead of angle
  13709. brackets. It is inactive in the sense that it does not trigger agenda entries,
  13710. does not link to the calendar and cannot be changed with the S-cursor keys.
  13711. So these are more for recording a certain time/date."
  13712. (interactive "P")
  13713. (org-time-stamp arg 'inactive))
  13714. (defvar org-date-ovl (make-overlay 1 1))
  13715. (overlay-put org-date-ovl 'face 'org-date-selected)
  13716. (org-detach-overlay org-date-ovl)
  13717. (defvar org-ans1) ; dynamically scoped parameter
  13718. (defvar org-ans2) ; dynamically scoped parameter
  13719. (defvar org-plain-time-of-day-regexp) ; defined below
  13720. (defvar org-overriding-default-time nil) ; dynamically scoped
  13721. (defvar org-read-date-overlay nil)
  13722. (defvar org-dcst nil) ; dynamically scoped
  13723. (defvar org-read-date-history nil)
  13724. (defvar org-read-date-final-answer nil)
  13725. (defvar org-read-date-analyze-futurep nil)
  13726. (defvar org-read-date-analyze-forced-year nil)
  13727. (defvar org-read-date-inactive)
  13728. (defun org-read-date (&optional org-with-time to-time from-string prompt
  13729. default-time default-input inactive)
  13730. "Read a date, possibly a time, and make things smooth for the user.
  13731. The prompt will suggest to enter an ISO date, but you can also enter anything
  13732. which will at least partially be understood by `parse-time-string'.
  13733. Unrecognized parts of the date will default to the current day, month, year,
  13734. hour and minute. If this command is called to replace a timestamp at point,
  13735. or to enter the second timestamp of a range, the default time is taken
  13736. from the existing stamp. Furthermore, the command prefers the future,
  13737. so if you are giving a date where the year is not given, and the day-month
  13738. combination is already past in the current year, it will assume you
  13739. mean next year. For details, see the manual. A few examples:
  13740. 3-2-5 --> 2003-02-05
  13741. feb 15 --> currentyear-02-15
  13742. 2/15 --> currentyear-02-15
  13743. sep 12 9 --> 2009-09-12
  13744. 12:45 --> today 12:45
  13745. 22 sept 0:34 --> currentyear-09-22 0:34
  13746. 12 --> currentyear-currentmonth-12
  13747. Fri --> nearest Friday (today or later)
  13748. etc.
  13749. Furthermore you can specify a relative date by giving, as the *first* thing
  13750. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  13751. change in days weeks, months, years.
  13752. With a single plus or minus, the date is relative to today. With a double
  13753. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  13754. +4d --> four days from today
  13755. +4 --> same as above
  13756. +2w --> two weeks from today
  13757. ++5 --> five days from default date
  13758. The function understands only English month and weekday abbreviations.
  13759. While prompting, a calendar is popped up - you can also select the
  13760. date with the mouse (button 1). The calendar shows a period of three
  13761. months. To scroll it to other months, use the keys `>' and `<'.
  13762. If you don't like the calendar, turn it off with
  13763. \(setq org-read-date-popup-calendar nil)
  13764. With optional argument TO-TIME, the date will immediately be converted
  13765. to an internal time.
  13766. With an optional argument WITH-TIME, the prompt will suggest to also
  13767. insert a time. Note that when WITH-TIME is not set, you can still
  13768. enter a time, and this function will inform the calling routine about
  13769. this change. The calling routine may then choose to change the format
  13770. used to insert the time stamp into the buffer to include the time.
  13771. With optional argument FROM-STRING, read from this string instead from
  13772. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  13773. the time/date that is used for everything that is not specified by the
  13774. user."
  13775. (require 'parse-time)
  13776. (let* ((org-time-stamp-rounding-minutes
  13777. (if (equal org-with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  13778. (org-dcst org-display-custom-times)
  13779. (ct (org-current-time))
  13780. (org-def (or org-overriding-default-time default-time ct))
  13781. (org-defdecode (decode-time org-def))
  13782. (dummy (progn
  13783. (when (< (nth 2 org-defdecode) org-extend-today-until)
  13784. (setcar (nthcdr 2 org-defdecode) -1)
  13785. (setcar (nthcdr 1 org-defdecode) 59)
  13786. (setq org-def (apply 'encode-time org-defdecode)
  13787. org-defdecode (decode-time org-def)))))
  13788. (calendar-frame-setup nil)
  13789. (calendar-setup nil)
  13790. (calendar-move-hook nil)
  13791. (calendar-view-diary-initially-flag nil)
  13792. (calendar-view-holidays-initially-flag nil)
  13793. (timestr (format-time-string
  13794. (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") org-def))
  13795. (prompt (concat (if prompt (concat prompt " ") "")
  13796. (format "Date+time [%s]: " timestr)))
  13797. ans (org-ans0 "") org-ans1 org-ans2 final)
  13798. (cond
  13799. (from-string (setq ans from-string))
  13800. (org-read-date-popup-calendar
  13801. (save-excursion
  13802. (save-window-excursion
  13803. (calendar)
  13804. (unwind-protect
  13805. (progn
  13806. (calendar-forward-day (- (time-to-days org-def)
  13807. (calendar-absolute-from-gregorian
  13808. (calendar-current-date))))
  13809. (org-eval-in-calendar nil t)
  13810. (let* ((old-map (current-local-map))
  13811. (map (copy-keymap calendar-mode-map))
  13812. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  13813. (org-defkey map (kbd "RET") 'org-calendar-select)
  13814. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  13815. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  13816. (org-defkey minibuffer-local-map [(meta shift left)]
  13817. (lambda () (interactive)
  13818. (org-eval-in-calendar '(calendar-backward-month 1))))
  13819. (org-defkey minibuffer-local-map [(meta shift right)]
  13820. (lambda () (interactive)
  13821. (org-eval-in-calendar '(calendar-forward-month 1))))
  13822. (org-defkey minibuffer-local-map [(meta shift up)]
  13823. (lambda () (interactive)
  13824. (org-eval-in-calendar '(calendar-backward-year 1))))
  13825. (org-defkey minibuffer-local-map [(meta shift down)]
  13826. (lambda () (interactive)
  13827. (org-eval-in-calendar '(calendar-forward-year 1))))
  13828. (org-defkey minibuffer-local-map [?\e (shift left)]
  13829. (lambda () (interactive)
  13830. (org-eval-in-calendar '(calendar-backward-month 1))))
  13831. (org-defkey minibuffer-local-map [?\e (shift right)]
  13832. (lambda () (interactive)
  13833. (org-eval-in-calendar '(calendar-forward-month 1))))
  13834. (org-defkey minibuffer-local-map [?\e (shift up)]
  13835. (lambda () (interactive)
  13836. (org-eval-in-calendar '(calendar-backward-year 1))))
  13837. (org-defkey minibuffer-local-map [?\e (shift down)]
  13838. (lambda () (interactive)
  13839. (org-eval-in-calendar '(calendar-forward-year 1))))
  13840. (org-defkey minibuffer-local-map [(shift up)]
  13841. (lambda () (interactive)
  13842. (org-eval-in-calendar '(calendar-backward-week 1))))
  13843. (org-defkey minibuffer-local-map [(shift down)]
  13844. (lambda () (interactive)
  13845. (org-eval-in-calendar '(calendar-forward-week 1))))
  13846. (org-defkey minibuffer-local-map [(shift left)]
  13847. (lambda () (interactive)
  13848. (org-eval-in-calendar '(calendar-backward-day 1))))
  13849. (org-defkey minibuffer-local-map [(shift right)]
  13850. (lambda () (interactive)
  13851. (org-eval-in-calendar '(calendar-forward-day 1))))
  13852. (org-defkey minibuffer-local-map ">"
  13853. (lambda () (interactive)
  13854. (org-eval-in-calendar '(scroll-calendar-left 1))))
  13855. (org-defkey minibuffer-local-map "<"
  13856. (lambda () (interactive)
  13857. (org-eval-in-calendar '(scroll-calendar-right 1))))
  13858. (org-defkey minibuffer-local-map "\C-v"
  13859. (lambda () (interactive)
  13860. (org-eval-in-calendar
  13861. '(calendar-scroll-left-three-months 1))))
  13862. (org-defkey minibuffer-local-map "\M-v"
  13863. (lambda () (interactive)
  13864. (org-eval-in-calendar
  13865. '(calendar-scroll-right-three-months 1))))
  13866. (run-hooks 'org-read-date-minibuffer-setup-hook)
  13867. (unwind-protect
  13868. (progn
  13869. (use-local-map map)
  13870. (setq org-read-date-inactive inactive)
  13871. (add-hook 'post-command-hook 'org-read-date-display)
  13872. (setq org-ans0 (read-string prompt default-input
  13873. 'org-read-date-history nil))
  13874. ;; org-ans0: from prompt
  13875. ;; org-ans1: from mouse click
  13876. ;; org-ans2: from calendar motion
  13877. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  13878. (remove-hook 'post-command-hook 'org-read-date-display)
  13879. (use-local-map old-map)
  13880. (when org-read-date-overlay
  13881. (delete-overlay org-read-date-overlay)
  13882. (setq org-read-date-overlay nil)))))
  13883. (bury-buffer "*Calendar*")))))
  13884. (t ; Naked prompt only
  13885. (unwind-protect
  13886. (setq ans (read-string prompt default-input
  13887. 'org-read-date-history timestr))
  13888. (when org-read-date-overlay
  13889. (delete-overlay org-read-date-overlay)
  13890. (setq org-read-date-overlay nil)))))
  13891. (setq final (org-read-date-analyze ans org-def org-defdecode))
  13892. (when org-read-date-analyze-forced-year
  13893. (message "Year was forced into %s"
  13894. (if org-read-date-force-compatible-dates
  13895. "compatible range (1970-2037)"
  13896. "range representable on this machine"))
  13897. (ding))
  13898. ;; One round trip to get rid of 34th of August and stuff like that....
  13899. (setq final (decode-time (apply 'encode-time final)))
  13900. (setq org-read-date-final-answer ans)
  13901. (if to-time
  13902. (apply 'encode-time final)
  13903. (if (and (boundp 'org-time-was-given) org-time-was-given)
  13904. (format "%04d-%02d-%02d %02d:%02d"
  13905. (nth 5 final) (nth 4 final) (nth 3 final)
  13906. (nth 2 final) (nth 1 final))
  13907. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  13908. (defvar org-def)
  13909. (defvar org-defdecode)
  13910. (defvar org-with-time)
  13911. (defun org-read-date-display ()
  13912. "Display the current date prompt interpretation in the minibuffer."
  13913. (when org-read-date-display-live
  13914. (when org-read-date-overlay
  13915. (delete-overlay org-read-date-overlay))
  13916. (when (minibufferp (current-buffer))
  13917. (save-excursion
  13918. (end-of-line 1)
  13919. (while (not (equal (buffer-substring
  13920. (max (point-min) (- (point) 4)) (point))
  13921. " "))
  13922. (insert " ")))
  13923. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  13924. " " (or org-ans1 org-ans2)))
  13925. (org-end-time-was-given nil)
  13926. (f (org-read-date-analyze ans org-def org-defdecode))
  13927. (fmts (if org-dcst
  13928. org-time-stamp-custom-formats
  13929. org-time-stamp-formats))
  13930. (fmt (if (or org-with-time
  13931. (and (boundp 'org-time-was-given) org-time-was-given))
  13932. (cdr fmts)
  13933. (car fmts)))
  13934. (txt (format-time-string fmt (apply 'encode-time f)))
  13935. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  13936. (txt (concat "=> " txt)))
  13937. (when (and org-end-time-was-given
  13938. (string-match org-plain-time-of-day-regexp txt))
  13939. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  13940. org-end-time-was-given
  13941. (substring txt (match-end 0)))))
  13942. (when org-read-date-analyze-futurep
  13943. (setq txt (concat txt " (=>F)")))
  13944. (setq org-read-date-overlay
  13945. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  13946. (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
  13947. (defun org-read-date-analyze (ans org-def org-defdecode)
  13948. "Analyze the combined answer of the date prompt."
  13949. ;; FIXME: cleanup and comment
  13950. (let ((nowdecode (decode-time (current-time)))
  13951. delta deltan deltaw deltadef year month day
  13952. hour minute second wday pm h2 m2 tl wday1
  13953. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  13954. (setq org-read-date-analyze-futurep nil
  13955. org-read-date-analyze-forced-year nil)
  13956. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  13957. (setq ans "+0"))
  13958. (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
  13959. (setq ans (replace-match "" t t ans)
  13960. deltan (car delta)
  13961. deltaw (nth 1 delta)
  13962. deltadef (nth 2 delta)))
  13963. ;; Check if there is an iso week date in there
  13964. ;; If yes, store the info and postpone interpreting it until the rest
  13965. ;; of the parsing is done
  13966. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  13967. (setq iso-year (if (match-end 1)
  13968. (org-small-year-to-year
  13969. (string-to-number (match-string 1 ans))))
  13970. iso-weekday (if (match-end 3)
  13971. (string-to-number (match-string 3 ans)))
  13972. iso-week (string-to-number (match-string 2 ans)))
  13973. (setq ans (replace-match "" t t ans)))
  13974. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  13975. (when (string-match
  13976. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  13977. (setq year (if (match-end 2)
  13978. (string-to-number (match-string 2 ans))
  13979. (progn (setq kill-year t)
  13980. (string-to-number (format-time-string "%Y"))))
  13981. month (string-to-number (match-string 3 ans))
  13982. day (string-to-number (match-string 4 ans)))
  13983. (if (< year 100) (setq year (+ 2000 year)))
  13984. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  13985. t nil ans)))
  13986. ;; Help matching dotted european dates
  13987. (when (string-match
  13988. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\. ?\\([1-9][0-9][0-9][0-9]\\)?" ans)
  13989. (setq year (if (match-end 3)
  13990. (string-to-number (match-string 3 ans))
  13991. (progn (setq kill-year t)
  13992. (string-to-number (format-time-string "%Y"))))
  13993. day (string-to-number (match-string 1 ans))
  13994. month (string-to-number (match-string 2 ans))
  13995. ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  13996. t nil ans)))
  13997. ;; Help matching american dates, like 5/30 or 5/30/7
  13998. (when (string-match
  13999. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  14000. (setq year (if (match-end 4)
  14001. (string-to-number (match-string 4 ans))
  14002. (progn (setq kill-year t)
  14003. (string-to-number (format-time-string "%Y"))))
  14004. month (string-to-number (match-string 1 ans))
  14005. day (string-to-number (match-string 2 ans)))
  14006. (if (< year 100) (setq year (+ 2000 year)))
  14007. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  14008. t nil ans)))
  14009. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  14010. ;; If there is a time with am/pm, and *no* time without it, we convert
  14011. ;; so that matching will be successful.
  14012. (loop for i from 1 to 2 do ; twice, for end time as well
  14013. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  14014. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  14015. (setq hour (string-to-number (match-string 1 ans))
  14016. minute (if (match-end 3)
  14017. (string-to-number (match-string 3 ans))
  14018. 0)
  14019. pm (equal ?p
  14020. (string-to-char (downcase (match-string 4 ans)))))
  14021. (if (and (= hour 12) (not pm))
  14022. (setq hour 0)
  14023. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  14024. (setq ans (replace-match (format "%02d:%02d" hour minute)
  14025. t t ans))))
  14026. ;; Check if a time range is given as a duration
  14027. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  14028. (setq hour (string-to-number (match-string 1 ans))
  14029. h2 (+ hour (string-to-number (match-string 3 ans)))
  14030. minute (string-to-number (match-string 2 ans))
  14031. m2 (+ minute (if (match-end 5) (string-to-number
  14032. (match-string 5 ans))0)))
  14033. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  14034. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  14035. t t ans)))
  14036. ;; Check if there is a time range
  14037. (when (boundp 'org-end-time-was-given)
  14038. (setq org-time-was-given nil)
  14039. (when (and (string-match org-plain-time-of-day-regexp ans)
  14040. (match-end 8))
  14041. (setq org-end-time-was-given (match-string 8 ans))
  14042. (setq ans (concat (substring ans 0 (match-beginning 7))
  14043. (substring ans (match-end 7))))))
  14044. (setq tl (parse-time-string ans)
  14045. day (or (nth 3 tl) (nth 3 org-defdecode))
  14046. month (or (nth 4 tl)
  14047. (if (and org-read-date-prefer-future
  14048. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  14049. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  14050. (nth 4 org-defdecode)))
  14051. year (or (and (not kill-year) (nth 5 tl))
  14052. (if (and org-read-date-prefer-future
  14053. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  14054. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  14055. (nth 5 org-defdecode)))
  14056. hour (or (nth 2 tl) (nth 2 org-defdecode))
  14057. minute (or (nth 1 tl) (nth 1 org-defdecode))
  14058. second (or (nth 0 tl) 0)
  14059. wday (nth 6 tl))
  14060. (when (and (eq org-read-date-prefer-future 'time)
  14061. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  14062. (equal day (nth 3 nowdecode))
  14063. (equal month (nth 4 nowdecode))
  14064. (equal year (nth 5 nowdecode))
  14065. (nth 2 tl)
  14066. (or (< (nth 2 tl) (nth 2 nowdecode))
  14067. (and (= (nth 2 tl) (nth 2 nowdecode))
  14068. (nth 1 tl)
  14069. (< (nth 1 tl) (nth 1 nowdecode)))))
  14070. (setq day (1+ day)
  14071. futurep t))
  14072. ;; Special date definitions below
  14073. (cond
  14074. (iso-week
  14075. ;; There was an iso week
  14076. (require 'cal-iso)
  14077. (setq futurep nil)
  14078. (setq year (or iso-year year)
  14079. day (or iso-weekday wday 1)
  14080. wday nil ; to make sure that the trigger below does not match
  14081. iso-date (calendar-gregorian-from-absolute
  14082. (calendar-absolute-from-iso
  14083. (list iso-week day year))))
  14084. ; FIXME: Should we also push ISO weeks into the future?
  14085. ; (when (and org-read-date-prefer-future
  14086. ; (not iso-year)
  14087. ; (< (calendar-absolute-from-gregorian iso-date)
  14088. ; (time-to-days (current-time))))
  14089. ; (setq year (1+ year)
  14090. ; iso-date (calendar-gregorian-from-absolute
  14091. ; (calendar-absolute-from-iso
  14092. ; (list iso-week day year)))))
  14093. (setq month (car iso-date)
  14094. year (nth 2 iso-date)
  14095. day (nth 1 iso-date)))
  14096. (deltan
  14097. (setq futurep nil)
  14098. (unless deltadef
  14099. (let ((now (decode-time (current-time))))
  14100. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  14101. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  14102. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  14103. ((equal deltaw "m") (setq month (+ month deltan)))
  14104. ((equal deltaw "y") (setq year (+ year deltan)))))
  14105. ((and wday (not (nth 3 tl)))
  14106. (setq futurep nil)
  14107. ;; Weekday was given, but no day, so pick that day in the week
  14108. ;; on or after the derived date.
  14109. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  14110. (unless (equal wday wday1)
  14111. (setq day (+ day (% (- wday wday1 -7) 7))))))
  14112. (if (and (boundp 'org-time-was-given)
  14113. (nth 2 tl))
  14114. (setq org-time-was-given t))
  14115. (if (< year 100) (setq year (+ 2000 year)))
  14116. ;; Check of the date is representable
  14117. (if org-read-date-force-compatible-dates
  14118. (progn
  14119. (if (< year 1970)
  14120. (setq year 1970 org-read-date-analyze-forced-year t))
  14121. (if (> year 2037)
  14122. (setq year 2037 org-read-date-analyze-forced-year t)))
  14123. (condition-case nil
  14124. (ignore (encode-time second minute hour day month year))
  14125. (error
  14126. (setq year (nth 5 org-defdecode))
  14127. (setq org-read-date-analyze-forced-year t))))
  14128. (setq org-read-date-analyze-futurep futurep)
  14129. (list second minute hour day month year)))
  14130. (defvar parse-time-weekdays)
  14131. (defun org-read-date-get-relative (s today default)
  14132. "Check string S for special relative date string.
  14133. TODAY and DEFAULT are internal times, for today and for a default.
  14134. Return shift list (N what def-flag)
  14135. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  14136. N is the number of WHATs to shift.
  14137. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  14138. the DEFAULT date rather than TODAY."
  14139. (require 'parse-time)
  14140. (when (and
  14141. (string-match
  14142. (concat
  14143. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  14144. "\\([0-9]+\\)?"
  14145. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  14146. "\\([ \t]\\|$\\)") s)
  14147. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  14148. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  14149. (string-to-char (substring (match-string 1 s) -1))
  14150. ?+))
  14151. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  14152. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  14153. (what (if (match-end 3) (match-string 3 s) "d"))
  14154. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  14155. (date (if rel default today))
  14156. (wday (nth 6 (decode-time date)))
  14157. delta)
  14158. (if wday1
  14159. (progn
  14160. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  14161. (if (= dir ?-) (setq delta (- delta 7)))
  14162. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  14163. (list delta "d" rel))
  14164. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  14165. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  14166. "Turn a user-specified date into the internal representation.
  14167. The internal representation needed by the calendar is (month day year).
  14168. This is a wrapper to handle the brain-dead convention in calendar that
  14169. user function argument order change dependent on argument order."
  14170. (if (boundp 'calendar-date-style)
  14171. (cond
  14172. ((eq calendar-date-style 'american)
  14173. (list arg1 arg2 arg3))
  14174. ((eq calendar-date-style 'european)
  14175. (list arg2 arg1 arg3))
  14176. ((eq calendar-date-style 'iso)
  14177. (list arg2 arg3 arg1)))
  14178. (with-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  14179. (if (org-bound-and-true-p european-calendar-style)
  14180. (list arg2 arg1 arg3)
  14181. (list arg1 arg2 arg3)))))
  14182. (defun org-eval-in-calendar (form &optional keepdate)
  14183. "Eval FORM in the calendar window and return to current window.
  14184. Also, store the cursor date in variable org-ans2."
  14185. (let ((sf (selected-frame))
  14186. (sw (selected-window)))
  14187. (select-window (get-buffer-window "*Calendar*" t))
  14188. (eval form)
  14189. (when (and (not keepdate) (calendar-cursor-to-date))
  14190. (let* ((date (calendar-cursor-to-date))
  14191. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14192. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  14193. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  14194. (select-window sw)
  14195. (org-select-frame-set-input-focus sf)))
  14196. (defun org-calendar-select ()
  14197. "Return to `org-read-date' with the date currently selected.
  14198. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  14199. (interactive)
  14200. (when (calendar-cursor-to-date)
  14201. (let* ((date (calendar-cursor-to-date))
  14202. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14203. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  14204. (if (active-minibuffer-window) (exit-minibuffer))))
  14205. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  14206. "Insert a date stamp for the date given by the internal TIME.
  14207. WITH-HM means use the stamp format that includes the time of the day.
  14208. INACTIVE means use square brackets instead of angular ones, so that the
  14209. stamp will not contribute to the agenda.
  14210. PRE and POST are optional strings to be inserted before and after the
  14211. stamp.
  14212. The command returns the inserted time stamp."
  14213. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  14214. stamp)
  14215. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  14216. (insert-before-markers (or pre ""))
  14217. (when (listp extra)
  14218. (setq extra (car extra))
  14219. (if (and (stringp extra)
  14220. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  14221. (setq extra (format "-%02d:%02d"
  14222. (string-to-number (match-string 1 extra))
  14223. (string-to-number (match-string 2 extra))))
  14224. (setq extra nil)))
  14225. (when extra
  14226. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  14227. (insert-before-markers (setq stamp (format-time-string fmt time)))
  14228. (insert-before-markers (or post ""))
  14229. (setq org-last-inserted-timestamp stamp)))
  14230. (defun org-toggle-time-stamp-overlays ()
  14231. "Toggle the use of custom time stamp formats."
  14232. (interactive)
  14233. (setq org-display-custom-times (not org-display-custom-times))
  14234. (unless org-display-custom-times
  14235. (let ((p (point-min)) (bmp (buffer-modified-p)))
  14236. (while (setq p (next-single-property-change p 'display))
  14237. (if (and (get-text-property p 'display)
  14238. (eq (get-text-property p 'face) 'org-date))
  14239. (remove-text-properties
  14240. p (setq p (next-single-property-change p 'display))
  14241. '(display t))))
  14242. (set-buffer-modified-p bmp)))
  14243. (if (featurep 'xemacs)
  14244. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  14245. (org-restart-font-lock)
  14246. (setq org-table-may-need-update t)
  14247. (if org-display-custom-times
  14248. (message "Time stamps are overlaid with custom format")
  14249. (message "Time stamp overlays removed")))
  14250. (defun org-display-custom-time (beg end)
  14251. "Overlay modified time stamp format over timestamp between BEG and END."
  14252. (let* ((ts (buffer-substring beg end))
  14253. t1 w1 with-hm tf time str w2 (off 0))
  14254. (save-match-data
  14255. (setq t1 (org-parse-time-string ts t))
  14256. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)?\\'" ts)
  14257. (setq off (- (match-end 0) (match-beginning 0)))))
  14258. (setq end (- end off))
  14259. (setq w1 (- end beg)
  14260. with-hm (and (nth 1 t1) (nth 2 t1))
  14261. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  14262. time (org-fix-decoded-time t1)
  14263. str (org-add-props
  14264. (format-time-string
  14265. (substring tf 1 -1) (apply 'encode-time time))
  14266. nil 'mouse-face 'highlight)
  14267. w2 (length str))
  14268. (if (not (= w2 w1))
  14269. (add-text-properties (1+ beg) (+ 2 beg)
  14270. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  14271. (if (featurep 'xemacs)
  14272. (progn
  14273. (put-text-property beg end 'invisible t)
  14274. (put-text-property beg end 'end-glyph (make-glyph str)))
  14275. (put-text-property beg end 'display str))))
  14276. (defun org-translate-time (string)
  14277. "Translate all timestamps in STRING to custom format.
  14278. But do this only if the variable `org-display-custom-times' is set."
  14279. (when org-display-custom-times
  14280. (save-match-data
  14281. (let* ((start 0)
  14282. (re org-ts-regexp-both)
  14283. t1 with-hm inactive tf time str beg end)
  14284. (while (setq start (string-match re string start))
  14285. (setq beg (match-beginning 0)
  14286. end (match-end 0)
  14287. t1 (save-match-data
  14288. (org-parse-time-string (substring string beg end) t))
  14289. with-hm (and (nth 1 t1) (nth 2 t1))
  14290. inactive (equal (substring string beg (1+ beg)) "[")
  14291. tf (funcall (if with-hm 'cdr 'car)
  14292. org-time-stamp-custom-formats)
  14293. time (org-fix-decoded-time t1)
  14294. str (format-time-string
  14295. (concat
  14296. (if inactive "[" "<") (substring tf 1 -1)
  14297. (if inactive "]" ">"))
  14298. (apply 'encode-time time))
  14299. string (replace-match str t t string)
  14300. start (+ start (length str)))))))
  14301. string)
  14302. (defun org-fix-decoded-time (time)
  14303. "Set 0 instead of nil for the first 6 elements of time.
  14304. Don't touch the rest."
  14305. (let ((n 0))
  14306. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  14307. (defun org-days-to-time (timestamp-string)
  14308. "Difference between TIMESTAMP-STRING and now in days."
  14309. (- (time-to-days (org-time-string-to-time timestamp-string))
  14310. (time-to-days (current-time))))
  14311. (defun org-deadline-close (timestamp-string &optional ndays)
  14312. "Is the time in TIMESTAMP-STRING close to the current date?"
  14313. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  14314. (and (< (org-days-to-time timestamp-string) ndays)
  14315. (not (org-entry-is-done-p))))
  14316. (defun org-get-wdays (ts)
  14317. "Get the deadline lead time appropriate for timestring TS."
  14318. (cond
  14319. ((<= org-deadline-warning-days 0)
  14320. ;; 0 or negative, enforce this value no matter what
  14321. (- org-deadline-warning-days))
  14322. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
  14323. ;; lead time is specified.
  14324. (floor (* (string-to-number (match-string 1 ts))
  14325. (cdr (assoc (match-string 2 ts)
  14326. '(("d" . 1) ("w" . 7)
  14327. ("m" . 30.4) ("y" . 365.25)))))))
  14328. ;; go for the default.
  14329. (t org-deadline-warning-days)))
  14330. (defun org-calendar-select-mouse (ev)
  14331. "Return to `org-read-date' with the date currently selected.
  14332. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  14333. (interactive "e")
  14334. (mouse-set-point ev)
  14335. (when (calendar-cursor-to-date)
  14336. (let* ((date (calendar-cursor-to-date))
  14337. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14338. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  14339. (if (active-minibuffer-window) (exit-minibuffer))))
  14340. (defun org-check-deadlines (ndays)
  14341. "Check if there are any deadlines due or past due.
  14342. A deadline is considered due if it happens within `org-deadline-warning-days'
  14343. days from today's date. If the deadline appears in an entry marked DONE,
  14344. it is not shown. The prefix arg NDAYS can be used to test that many
  14345. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  14346. (interactive "P")
  14347. (let* ((org-warn-days
  14348. (cond
  14349. ((equal ndays '(4)) 100000)
  14350. (ndays (prefix-numeric-value ndays))
  14351. (t (abs org-deadline-warning-days))))
  14352. (case-fold-search nil)
  14353. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  14354. (callback
  14355. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  14356. (message "%d deadlines past-due or due within %d days"
  14357. (org-occur regexp nil callback)
  14358. org-warn-days)))
  14359. (defun org-check-before-date (date)
  14360. "Check if there are deadlines or scheduled entries before DATE."
  14361. (interactive (list (org-read-date)))
  14362. (let ((case-fold-search nil)
  14363. (regexp (concat "\\<\\(" org-deadline-string
  14364. "\\|" org-scheduled-string
  14365. "\\) *<\\([^>]+\\)>"))
  14366. (callback
  14367. (lambda () (time-less-p
  14368. (org-time-string-to-time (match-string 2))
  14369. (org-time-string-to-time date)))))
  14370. (message "%d entries before %s"
  14371. (org-occur regexp nil callback) date)))
  14372. (defun org-check-after-date (date)
  14373. "Check if there are deadlines or scheduled entries after DATE."
  14374. (interactive (list (org-read-date)))
  14375. (let ((case-fold-search nil)
  14376. (regexp (concat "\\<\\(" org-deadline-string
  14377. "\\|" org-scheduled-string
  14378. "\\) *<\\([^>]+\\)>"))
  14379. (callback
  14380. (lambda () (not
  14381. (time-less-p
  14382. (org-time-string-to-time (match-string 2))
  14383. (org-time-string-to-time date))))))
  14384. (message "%d entries after %s"
  14385. (org-occur regexp nil callback) date)))
  14386. (defun org-check-dates-range (start-date end-date)
  14387. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  14388. (interactive (list (org-read-date nil nil nil "Range starts")
  14389. (org-read-date nil nil nil "Range end")))
  14390. (let ((case-fold-search nil)
  14391. (regexp (concat "\\<\\(" org-deadline-string
  14392. "\\|" org-scheduled-string
  14393. "\\) *<\\([^>]+\\)>"))
  14394. (callback
  14395. (lambda ()
  14396. (let ((match (match-string 2)))
  14397. (and
  14398. (not (time-less-p
  14399. (org-time-string-to-time match)
  14400. (org-time-string-to-time start-date)))
  14401. (time-less-p
  14402. (org-time-string-to-time match)
  14403. (org-time-string-to-time end-date)))))))
  14404. (message "%d entries between %s and %s"
  14405. (org-occur regexp nil callback) start-date end-date)))
  14406. (defun org-evaluate-time-range (&optional to-buffer)
  14407. "Evaluate a time range by computing the difference between start and end.
  14408. Normally the result is just printed in the echo area, but with prefix arg
  14409. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  14410. If the time range is actually in a table, the result is inserted into the
  14411. next column.
  14412. For time difference computation, a year is assumed to be exactly 365
  14413. days in order to avoid rounding problems."
  14414. (interactive "P")
  14415. (or
  14416. (org-clock-update-time-maybe)
  14417. (save-excursion
  14418. (unless (org-at-date-range-p t)
  14419. (goto-char (point-at-bol))
  14420. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  14421. (if (not (org-at-date-range-p t))
  14422. (error "Not at a time-stamp range, and none found in current line")))
  14423. (let* ((ts1 (match-string 1))
  14424. (ts2 (match-string 2))
  14425. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  14426. (match-end (match-end 0))
  14427. (time1 (org-time-string-to-time ts1))
  14428. (time2 (org-time-string-to-time ts2))
  14429. (t1 (org-float-time time1))
  14430. (t2 (org-float-time time2))
  14431. (diff (abs (- t2 t1)))
  14432. (negative (< (- t2 t1) 0))
  14433. ;; (ys (floor (* 365 24 60 60)))
  14434. (ds (* 24 60 60))
  14435. (hs (* 60 60))
  14436. (fy "%dy %dd %02d:%02d")
  14437. (fy1 "%dy %dd")
  14438. (fd "%dd %02d:%02d")
  14439. (fd1 "%dd")
  14440. (fh "%02d:%02d")
  14441. y d h m align)
  14442. (if havetime
  14443. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  14444. y 0
  14445. d (floor (/ diff ds)) diff (mod diff ds)
  14446. h (floor (/ diff hs)) diff (mod diff hs)
  14447. m (floor (/ diff 60)))
  14448. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  14449. y 0
  14450. d (floor (+ (/ diff ds) 0.5))
  14451. h 0 m 0))
  14452. (if (not to-buffer)
  14453. (message "%s" (org-make-tdiff-string y d h m))
  14454. (if (org-at-table-p)
  14455. (progn
  14456. (goto-char match-end)
  14457. (setq align t)
  14458. (and (looking-at " *|") (goto-char (match-end 0))))
  14459. (goto-char match-end))
  14460. (if (looking-at
  14461. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  14462. (replace-match ""))
  14463. (if negative (insert " -"))
  14464. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  14465. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  14466. (insert " " (format fh h m))))
  14467. (if align (org-table-align))
  14468. (message "Time difference inserted")))))
  14469. (defun org-make-tdiff-string (y d h m)
  14470. (let ((fmt "")
  14471. (l nil))
  14472. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  14473. l (push y l)))
  14474. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  14475. l (push d l)))
  14476. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  14477. l (push h l)))
  14478. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  14479. l (push m l)))
  14480. (apply 'format fmt (nreverse l))))
  14481. (defun org-time-string-to-time (s &optional buffer pos)
  14482. (condition-case errdata
  14483. (apply 'encode-time (org-parse-time-string s))
  14484. (error (error "Bad timestamp `%s'%s\nError was: %s"
  14485. s (if (not (and buffer pos))
  14486. ""
  14487. (format " at %d in buffer `%s'" pos buffer))
  14488. (cdr errdata)))))
  14489. (defun org-time-string-to-seconds (s)
  14490. (org-float-time (org-time-string-to-time s)))
  14491. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  14492. "Convert a time stamp to an absolute day number.
  14493. If there is a specifier for a cyclic time stamp, get the closest date to
  14494. DAYNR.
  14495. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  14496. The variable date is bound by the calendar when this is called."
  14497. (cond
  14498. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  14499. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  14500. daynr
  14501. (+ daynr 1000)))
  14502. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  14503. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  14504. (time-to-days (current-time))) (match-string 0 s)
  14505. prefer show-all))
  14506. (t (time-to-days
  14507. (condition-case errdata
  14508. (apply 'encode-time (org-parse-time-string s))
  14509. (error (error "Bad timestamp `%s'%s\nError was: %s"
  14510. s (if (not (and buffer pos))
  14511. ""
  14512. (format " at %d in buffer `%s'" pos buffer))
  14513. (cdr errdata))))))))
  14514. (defun org-days-to-iso-week (days)
  14515. "Return the iso week number."
  14516. (require 'cal-iso)
  14517. (car (calendar-iso-from-absolute days)))
  14518. (defun org-small-year-to-year (year)
  14519. "Convert 2-digit years into 4-digit years.
  14520. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  14521. The year 2000 cannot be abbreviated. Any year larger than 99
  14522. is returned unchanged."
  14523. (if (< year 38)
  14524. (setq year (+ 2000 year))
  14525. (if (< year 100)
  14526. (setq year (+ 1900 year))))
  14527. year)
  14528. (defun org-time-from-absolute (d)
  14529. "Return the time corresponding to date D.
  14530. D may be an absolute day number, or a calendar-type list (month day year)."
  14531. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  14532. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  14533. (defun org-calendar-holiday ()
  14534. "List of holidays, for Diary display in Org-mode."
  14535. (require 'holidays)
  14536. (let ((hl (funcall
  14537. (if (fboundp 'calendar-check-holidays)
  14538. 'calendar-check-holidays 'check-calendar-holidays) date)))
  14539. (if hl (mapconcat 'identity hl "; "))))
  14540. (defun org-diary-sexp-entry (sexp entry date)
  14541. "Process a SEXP diary ENTRY for DATE."
  14542. (require 'diary-lib)
  14543. (let ((result (if calendar-debug-sexp
  14544. (let ((stack-trace-on-error t))
  14545. (eval (car (read-from-string sexp))))
  14546. (condition-case nil
  14547. (eval (car (read-from-string sexp)))
  14548. (error
  14549. (beep)
  14550. (message "Bad sexp at line %d in %s: %s"
  14551. (org-current-line)
  14552. (buffer-file-name) sexp)
  14553. (sleep-for 2))))))
  14554. (cond ((stringp result) (split-string result "; "))
  14555. ((and (consp result)
  14556. (not (consp (cdr result)))
  14557. (stringp (cdr result))) (cdr result))
  14558. ((and (consp result)
  14559. (stringp (car result))) result)
  14560. (result entry)
  14561. (t nil))))
  14562. (defun org-diary-to-ical-string (frombuf)
  14563. "Get iCalendar entries from diary entries in buffer FROMBUF.
  14564. This uses the icalendar.el library."
  14565. (let* ((tmpdir (if (featurep 'xemacs)
  14566. (temp-directory)
  14567. temporary-file-directory))
  14568. (tmpfile (make-temp-name
  14569. (expand-file-name "orgics" tmpdir)))
  14570. buf rtn b e)
  14571. (with-current-buffer frombuf
  14572. (icalendar-export-region (point-min) (point-max) tmpfile)
  14573. (setq buf (find-buffer-visiting tmpfile))
  14574. (set-buffer buf)
  14575. (goto-char (point-min))
  14576. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  14577. (setq b (match-beginning 0)))
  14578. (goto-char (point-max))
  14579. (if (re-search-backward "^END:VEVENT" nil t)
  14580. (setq e (match-end 0)))
  14581. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  14582. (kill-buffer buf)
  14583. (delete-file tmpfile)
  14584. rtn))
  14585. (defun org-closest-date (start current change prefer show-all)
  14586. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  14587. When PREFER is `past', return a date that is either CURRENT or past.
  14588. When PREFER is `future', return a date that is either CURRENT or future.
  14589. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  14590. ;; Make the proper lists from the dates
  14591. (catch 'exit
  14592. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  14593. dn dw sday cday n1 n2 n0
  14594. d m y y1 y2 date1 date2 nmonths nm ny m2)
  14595. (setq start (org-date-to-gregorian start)
  14596. current (org-date-to-gregorian
  14597. (if show-all
  14598. current
  14599. (time-to-days (current-time))))
  14600. sday (calendar-absolute-from-gregorian start)
  14601. cday (calendar-absolute-from-gregorian current))
  14602. (if (<= cday sday) (throw 'exit sday))
  14603. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  14604. (setq dn (string-to-number (match-string 1 change))
  14605. dw (cdr (assoc (match-string 2 change) a1)))
  14606. (error "Invalid change specifier: %s" change))
  14607. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  14608. (cond
  14609. ((eq dw 'day)
  14610. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  14611. n2 (+ n1 dn)))
  14612. ((eq dw 'year)
  14613. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  14614. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  14615. (setq date1 (list m d y1)
  14616. n1 (calendar-absolute-from-gregorian date1)
  14617. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  14618. n2 (calendar-absolute-from-gregorian date2)))
  14619. ((eq dw 'month)
  14620. ;; approx number of month between the two dates
  14621. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  14622. ;; How often does dn fit in there?
  14623. (setq d (nth 1 start) m (car start) y (nth 2 start)
  14624. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  14625. m (+ m nm)
  14626. ny (floor (/ m 12))
  14627. y (+ y ny)
  14628. m (- m (* ny 12)))
  14629. (while (> m 12) (setq m (- m 12) y (1+ y)))
  14630. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  14631. (setq m2 (+ m dn) y2 y)
  14632. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  14633. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  14634. (while (<= n2 cday)
  14635. (setq n1 n2 m m2 y y2)
  14636. (setq m2 (+ m dn) y2 y)
  14637. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  14638. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  14639. ;; Make sure n1 is the earlier date
  14640. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  14641. (if show-all
  14642. (cond
  14643. ((eq prefer 'past) (if (= cday n2) n2 n1))
  14644. ((eq prefer 'future) (if (= cday n1) n1 n2))
  14645. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  14646. (cond
  14647. ((eq prefer 'past) (if (= cday n2) n2 n1))
  14648. ((eq prefer 'future) (if (= cday n1) n1 n2))
  14649. (t (if (= cday n1) n1 n2)))))))
  14650. (defun org-date-to-gregorian (date)
  14651. "Turn any specification of DATE into a Gregorian date for the calendar."
  14652. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  14653. ((and (listp date) (= (length date) 3)) date)
  14654. ((stringp date)
  14655. (setq date (org-parse-time-string date))
  14656. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  14657. ((listp date)
  14658. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  14659. (defun org-parse-time-string (s &optional nodefault)
  14660. "Parse the standard Org-mode time string.
  14661. This should be a lot faster than the normal `parse-time-string'.
  14662. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  14663. hour and minute fields will be nil if not given."
  14664. (if (string-match org-ts-regexp0 s)
  14665. (list 0
  14666. (if (or (match-beginning 8) (not nodefault))
  14667. (string-to-number (or (match-string 8 s) "0")))
  14668. (if (or (match-beginning 7) (not nodefault))
  14669. (string-to-number (or (match-string 7 s) "0")))
  14670. (string-to-number (match-string 4 s))
  14671. (string-to-number (match-string 3 s))
  14672. (string-to-number (match-string 2 s))
  14673. nil nil nil)
  14674. (error "Not a standard Org-mode time string: %s" s)))
  14675. (defun org-timestamp-up (&optional arg)
  14676. "Increase the date item at the cursor by one.
  14677. If the cursor is on the year, change the year. If it is on the month,
  14678. the day or the time, change that.
  14679. With prefix ARG, change by that many units."
  14680. (interactive "p")
  14681. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  14682. (defun org-timestamp-down (&optional arg)
  14683. "Decrease the date item at the cursor by one.
  14684. If the cursor is on the year, change the year. If it is on the month,
  14685. the day or the time, change that.
  14686. With prefix ARG, change by that many units."
  14687. (interactive "p")
  14688. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  14689. (defun org-timestamp-up-day (&optional arg)
  14690. "Increase the date in the time stamp by one day.
  14691. With prefix ARG, change that many days."
  14692. (interactive "p")
  14693. (if (and (not (org-at-timestamp-p t))
  14694. (org-at-heading-p))
  14695. (org-todo 'up)
  14696. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  14697. (defun org-timestamp-down-day (&optional arg)
  14698. "Decrease the date in the time stamp by one day.
  14699. With prefix ARG, change that many days."
  14700. (interactive "p")
  14701. (if (and (not (org-at-timestamp-p t))
  14702. (org-at-heading-p))
  14703. (org-todo 'down)
  14704. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  14705. (defun org-at-timestamp-p (&optional inactive-ok)
  14706. "Determine if the cursor is in or at a timestamp."
  14707. (interactive)
  14708. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  14709. (pos (point))
  14710. (ans (or (looking-at tsr)
  14711. (save-excursion
  14712. (skip-chars-backward "^[<\n\r\t")
  14713. (if (> (point) (point-min)) (backward-char 1))
  14714. (and (looking-at tsr)
  14715. (> (- (match-end 0) pos) -1))))))
  14716. (and ans
  14717. (boundp 'org-ts-what)
  14718. (setq org-ts-what
  14719. (cond
  14720. ((= pos (match-beginning 0)) 'bracket)
  14721. ;; Point is considered to be "on the bracket" whether
  14722. ;; it's really on it or right after it.
  14723. ((or (= pos (1- (match-end 0)))
  14724. (= pos (match-end 0))) 'bracket)
  14725. ((org-pos-in-match-range pos 2) 'year)
  14726. ((org-pos-in-match-range pos 3) 'month)
  14727. ((org-pos-in-match-range pos 7) 'hour)
  14728. ((org-pos-in-match-range pos 8) 'minute)
  14729. ((or (org-pos-in-match-range pos 4)
  14730. (org-pos-in-match-range pos 5)) 'day)
  14731. ((and (> pos (or (match-end 8) (match-end 5)))
  14732. (< pos (match-end 0)))
  14733. (- pos (or (match-end 8) (match-end 5))))
  14734. (t 'day))))
  14735. ans))
  14736. (defun org-toggle-timestamp-type ()
  14737. "Toggle the type (<active> or [inactive]) of a time stamp."
  14738. (interactive)
  14739. (when (org-at-timestamp-p t)
  14740. (let ((beg (match-beginning 0)) (end (match-end 0))
  14741. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  14742. (save-excursion
  14743. (goto-char beg)
  14744. (while (re-search-forward "[][<>]" end t)
  14745. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  14746. t t)))
  14747. (message "Timestamp is now %sactive"
  14748. (if (equal (char-after beg) ?<) "" "in")))))
  14749. (defun org-timestamp-change (n &optional what updown)
  14750. "Change the date in the time stamp at point.
  14751. The date will be changed by N times WHAT. WHAT can be `day', `month',
  14752. `year', `minute', `second'. If WHAT is not given, the cursor position
  14753. in the timestamp determines what will be changed."
  14754. (let ((origin (point)) origin-cat
  14755. with-hm inactive
  14756. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  14757. org-ts-what
  14758. extra rem
  14759. ts time time0)
  14760. (if (not (org-at-timestamp-p t))
  14761. (error "Not at a timestamp"))
  14762. (if (and (not what) (eq org-ts-what 'bracket))
  14763. (org-toggle-timestamp-type)
  14764. ;; Point isn't on brackets. Remember the part of the time-stamp
  14765. ;; the point was in. Indeed, size of time-stamps may change,
  14766. ;; but point must be kept in the same category nonetheless.
  14767. (setq origin-cat org-ts-what)
  14768. (if (and (not what) (not (eq org-ts-what 'day))
  14769. org-display-custom-times
  14770. (get-text-property (point) 'display)
  14771. (not (get-text-property (1- (point)) 'display)))
  14772. (setq org-ts-what 'day))
  14773. (setq org-ts-what (or what org-ts-what)
  14774. inactive (= (char-after (match-beginning 0)) ?\[)
  14775. ts (match-string 0))
  14776. (replace-match "")
  14777. (if (string-match
  14778. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)*\\)[]>]"
  14779. ts)
  14780. (setq extra (match-string 1 ts)))
  14781. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  14782. (setq with-hm t))
  14783. (setq time0 (org-parse-time-string ts))
  14784. (when (and updown
  14785. (eq org-ts-what 'minute)
  14786. (not current-prefix-arg))
  14787. ;; This looks like s-up and s-down. Change by one rounding step.
  14788. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  14789. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  14790. (setcar (cdr time0) (+ (nth 1 time0)
  14791. (if (> n 0) (- rem) (- dm rem))))))
  14792. (setq time
  14793. (encode-time (or (car time0) 0)
  14794. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  14795. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  14796. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  14797. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  14798. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  14799. (nthcdr 6 time0)))
  14800. (when (and (member org-ts-what '(hour minute))
  14801. extra
  14802. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  14803. (setq extra (org-modify-ts-extra
  14804. extra
  14805. (if (eq org-ts-what 'hour) 2 5)
  14806. n dm)))
  14807. (when (integerp org-ts-what)
  14808. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  14809. (if (eq what 'calendar)
  14810. (let ((cal-date (org-get-date-from-calendar)))
  14811. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  14812. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  14813. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  14814. (setcar time0 (or (car time0) 0))
  14815. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  14816. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  14817. (setq time (apply 'encode-time time0))))
  14818. ;; Insert the new time-stamp, and ensure point stays in the same
  14819. ;; category as before (i.e. not after the last position in that
  14820. ;; category).
  14821. (let ((pos (point)))
  14822. ;; Stay before inserted string. `save-excursion' is of no use.
  14823. (setq org-last-changed-timestamp
  14824. (org-insert-time-stamp time with-hm inactive nil nil extra))
  14825. (goto-char pos))
  14826. (save-match-data
  14827. (looking-at org-ts-regexp3)
  14828. (goto-char (cond
  14829. ;; `day' category ends before `hour' if any, or at
  14830. ;; the end of the day name.
  14831. ((eq origin-cat 'day)
  14832. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  14833. ((eq origin-cat 'hour) (min (match-end 7) origin))
  14834. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  14835. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  14836. ;; `year' and `month' have both fixed size: point
  14837. ;; couldn't have moved into another part.
  14838. (t origin))))
  14839. ;; Update clock if on a CLOCK line.
  14840. (org-clock-update-time-maybe)
  14841. ;; Try to recenter the calendar window, if any.
  14842. (if (and org-calendar-follow-timestamp-change
  14843. (get-buffer-window "*Calendar*" t)
  14844. (memq org-ts-what '(day month year)))
  14845. (org-recenter-calendar (time-to-days time))))))
  14846. (defun org-modify-ts-extra (s pos n dm)
  14847. "Change the different parts of the lead-time and repeat fields in timestamp."
  14848. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  14849. ng h m new rem)
  14850. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  14851. (cond
  14852. ((or (org-pos-in-match-range pos 2)
  14853. (org-pos-in-match-range pos 3))
  14854. (setq m (string-to-number (match-string 3 s))
  14855. h (string-to-number (match-string 2 s)))
  14856. (if (org-pos-in-match-range pos 2)
  14857. (setq h (+ h n))
  14858. (setq n (* dm (org-no-warnings (signum n))))
  14859. (when (not (= 0 (setq rem (% m dm))))
  14860. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  14861. (setq m (+ m n)))
  14862. (if (< m 0) (setq m (+ m 60) h (1- h)))
  14863. (if (> m 59) (setq m (- m 60) h (1+ h)))
  14864. (setq h (min 24 (max 0 h)))
  14865. (setq ng 1 new (format "-%02d:%02d" h m)))
  14866. ((org-pos-in-match-range pos 6)
  14867. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  14868. ((org-pos-in-match-range pos 5)
  14869. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  14870. ((org-pos-in-match-range pos 9)
  14871. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  14872. ((org-pos-in-match-range pos 8)
  14873. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  14874. (when ng
  14875. (setq s (concat
  14876. (substring s 0 (match-beginning ng))
  14877. new
  14878. (substring s (match-end ng))))))
  14879. s))
  14880. (defun org-recenter-calendar (date)
  14881. "If the calendar is visible, recenter it to DATE."
  14882. (let ((cwin (get-buffer-window "*Calendar*" t)))
  14883. (when cwin
  14884. (let ((calendar-move-hook nil))
  14885. (with-selected-window cwin
  14886. (calendar-goto-date (if (listp date) date
  14887. (calendar-gregorian-from-absolute date))))))))
  14888. (defun org-goto-calendar (&optional arg)
  14889. "Go to the Emacs calendar at the current date.
  14890. If there is a time stamp in the current line, go to that date.
  14891. A prefix ARG can be used to force the current date."
  14892. (interactive "P")
  14893. (let ((tsr org-ts-regexp) diff
  14894. (calendar-move-hook nil)
  14895. (calendar-view-holidays-initially-flag nil)
  14896. (calendar-view-diary-initially-flag nil))
  14897. (if (or (org-at-timestamp-p)
  14898. (save-excursion
  14899. (beginning-of-line 1)
  14900. (looking-at (concat ".*" tsr))))
  14901. (let ((d1 (time-to-days (current-time)))
  14902. (d2 (time-to-days
  14903. (org-time-string-to-time (match-string 1)))))
  14904. (setq diff (- d2 d1))))
  14905. (calendar)
  14906. (calendar-goto-today)
  14907. (if (and diff (not arg)) (calendar-forward-day diff))))
  14908. (defun org-get-date-from-calendar ()
  14909. "Return a list (month day year) of date at point in calendar."
  14910. (with-current-buffer "*Calendar*"
  14911. (save-match-data
  14912. (calendar-cursor-to-date))))
  14913. (defun org-date-from-calendar ()
  14914. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  14915. If there is already a time stamp at the cursor position, update it."
  14916. (interactive)
  14917. (if (org-at-timestamp-p t)
  14918. (org-timestamp-change 0 'calendar)
  14919. (let ((cal-date (org-get-date-from-calendar)))
  14920. (org-insert-time-stamp
  14921. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  14922. (defun org-minutes-to-hh:mm-string (m)
  14923. "Compute H:MM from a number of minutes."
  14924. (let ((h (/ m 60)))
  14925. (setq m (- m (* 60 h)))
  14926. (format org-time-clocksum-format h m)))
  14927. (defun org-hh:mm-string-to-minutes (s)
  14928. "Convert a string H:MM to a number of minutes.
  14929. If the string is just a number, interpret it as minutes.
  14930. In fact, the first hh:mm or number in the string will be taken,
  14931. there can be extra stuff in the string.
  14932. If no number is found, the return value is 0."
  14933. (cond
  14934. ((integerp s) s)
  14935. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  14936. (+ (* (string-to-number (match-string 1 s)) 60)
  14937. (string-to-number (match-string 2 s))))
  14938. ((string-match "\\([0-9]+\\)" s)
  14939. (string-to-number (match-string 1 s)))
  14940. (t 0)))
  14941. (defcustom org-effort-durations
  14942. `(("h" . 60)
  14943. ("d" . ,(* 60 8))
  14944. ("w" . ,(* 60 8 5))
  14945. ("m" . ,(* 60 8 5 4))
  14946. ("y" . ,(* 60 8 5 40)))
  14947. "Conversion factor to minutes for an effort modifier.
  14948. Each entry has the form (MODIFIER . MINUTES).
  14949. In an effort string, a number followed by MODIFIER is multiplied
  14950. by the specified number of MINUTES to obtain an effort in
  14951. minutes.
  14952. For example, if the value of this variable is ((\"hours\" . 60)), then an
  14953. effort string \"2hours\" is equivalent to 120 minutes."
  14954. :group 'org-agenda
  14955. :version "24.1"
  14956. :type '(alist :key-type (string :tag "Modifier")
  14957. :value-type (number :tag "Minutes")))
  14958. (defun org-duration-string-to-minutes (s)
  14959. "Convert a duration string S to minutes.
  14960. A bare number is interpreted as minutes, modifiers can be set by
  14961. customizing `org-effort-durations' (which see).
  14962. Entries containing a colon are interpreted as H:MM by
  14963. `org-hh:mm-string-to-minutes'."
  14964. (let ((result 0)
  14965. (re (concat "\\([0-9]+\\) *\\("
  14966. (regexp-opt (mapcar 'car org-effort-durations))
  14967. "\\)")))
  14968. (while (string-match re s)
  14969. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  14970. (string-to-number (match-string 1 s))))
  14971. (setq s (replace-match "" nil t s)))
  14972. (incf result (org-hh:mm-string-to-minutes s))
  14973. result))
  14974. ;;;; Files
  14975. (defun org-save-all-org-buffers ()
  14976. "Save all Org-mode buffers without user confirmation."
  14977. (interactive)
  14978. (message "Saving all Org-mode buffers...")
  14979. (save-some-buffers t (lambda () (eq major-mode 'org-mode)))
  14980. (when (featurep 'org-id) (org-id-locations-save))
  14981. (message "Saving all Org-mode buffers... done"))
  14982. (defun org-revert-all-org-buffers ()
  14983. "Revert all Org-mode buffers.
  14984. Prompt for confirmation when there are unsaved changes.
  14985. Be sure you know what you are doing before letting this function
  14986. overwrite your changes.
  14987. This function is useful in a setup where one tracks org files
  14988. with a version control system, to revert on one machine after pulling
  14989. changes from another. I believe the procedure must be like this:
  14990. 1. M-x org-save-all-org-buffers
  14991. 2. Pull changes from the other machine, resolve conflicts
  14992. 3. M-x org-revert-all-org-buffers"
  14993. (interactive)
  14994. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  14995. (error "Abort"))
  14996. (save-excursion
  14997. (save-window-excursion
  14998. (mapc
  14999. (lambda (b)
  15000. (when (and (with-current-buffer b (eq major-mode 'org-mode))
  15001. (with-current-buffer b buffer-file-name))
  15002. (org-pop-to-buffer-same-window b)
  15003. (revert-buffer t 'no-confirm)))
  15004. (buffer-list))
  15005. (when (and (featurep 'org-id) org-id-track-globally)
  15006. (org-id-locations-load)))))
  15007. ;;;; Agenda files
  15008. ;;;###autoload
  15009. (defun org-switchb (&optional arg)
  15010. "Switch between Org buffers.
  15011. With one prefix argument, restrict available buffers to files.
  15012. With two prefix arguments, restrict available buffers to agenda files.
  15013. Defaults to `iswitchb' for buffer name completion.
  15014. Set `org-completion-use-ido' to make it use ido instead."
  15015. (interactive "P")
  15016. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  15017. ((equal arg '(16)) (org-buffer-list 'agenda))
  15018. (t (org-buffer-list))))
  15019. (org-completion-use-iswitchb org-completion-use-iswitchb)
  15020. (org-completion-use-ido org-completion-use-ido))
  15021. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  15022. (setq org-completion-use-iswitchb t))
  15023. (org-pop-to-buffer-same-window
  15024. (org-icompleting-read "Org buffer: "
  15025. (mapcar 'list (mapcar 'buffer-name blist))
  15026. nil t))))
  15027. ;;; Define some older names previously used for this functionality
  15028. ;;;###autoload
  15029. (defalias 'org-ido-switchb 'org-switchb)
  15030. ;;;###autoload
  15031. (defalias 'org-iswitchb 'org-switchb)
  15032. (defun org-buffer-list (&optional predicate exclude-tmp)
  15033. "Return a list of Org buffers.
  15034. PREDICATE can be `export', `files' or `agenda'.
  15035. export restrict the list to Export buffers.
  15036. files restrict the list to buffers visiting Org files.
  15037. agenda restrict the list to buffers visiting agenda files.
  15038. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  15039. (let* ((bfn nil)
  15040. (agenda-files (and (eq predicate 'agenda)
  15041. (mapcar 'file-truename (org-agenda-files t))))
  15042. (filter
  15043. (cond
  15044. ((eq predicate 'files)
  15045. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  15046. ((eq predicate 'export)
  15047. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  15048. ((eq predicate 'agenda)
  15049. (lambda (b)
  15050. (with-current-buffer b
  15051. (and (eq major-mode 'org-mode)
  15052. (setq bfn (buffer-file-name b))
  15053. (member (file-truename bfn) agenda-files)))))
  15054. (t (lambda (b) (with-current-buffer b
  15055. (or (eq major-mode 'org-mode)
  15056. (string-match "\*Org .*Export"
  15057. (buffer-name b)))))))))
  15058. (delq nil
  15059. (mapcar
  15060. (lambda(b)
  15061. (if (and (funcall filter b)
  15062. (or (not exclude-tmp)
  15063. (not (string-match "tmp" (buffer-name b)))))
  15064. b
  15065. nil))
  15066. (buffer-list)))))
  15067. (defun org-agenda-files (&optional unrestricted archives)
  15068. "Get the list of agenda files.
  15069. Optional UNRESTRICTED means return the full list even if a restriction
  15070. is currently in place.
  15071. When ARCHIVES is t, include all archive files that are really being
  15072. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  15073. `org-agenda-archives-mode' is t."
  15074. (let ((files
  15075. (cond
  15076. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  15077. ((stringp org-agenda-files) (org-read-agenda-file-list))
  15078. ((listp org-agenda-files) org-agenda-files)
  15079. (t (error "Invalid value of `org-agenda-files'")))))
  15080. (setq files (apply 'append
  15081. (mapcar (lambda (f)
  15082. (if (file-directory-p f)
  15083. (directory-files
  15084. f t org-agenda-file-regexp)
  15085. (list f)))
  15086. files)))
  15087. (when org-agenda-skip-unavailable-files
  15088. (setq files (delq nil
  15089. (mapcar (function
  15090. (lambda (file)
  15091. (and (file-readable-p file) file)))
  15092. files))))
  15093. (when (or (eq archives t)
  15094. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  15095. (setq files (org-add-archive-files files)))
  15096. files))
  15097. (defun org-agenda-file-p (&optional file)
  15098. "Return non-nil, if FILE is an agenda file.
  15099. If FILE is omitted, use the file associated with the current
  15100. buffer."
  15101. (member (or file (buffer-file-name))
  15102. (org-agenda-files t)))
  15103. (defun org-edit-agenda-file-list ()
  15104. "Edit the list of agenda files.
  15105. Depending on setup, this either uses customize to edit the variable
  15106. `org-agenda-files', or it visits the file that is holding the list. In the
  15107. latter case, the buffer is set up in a way that saving it automatically kills
  15108. the buffer and restores the previous window configuration."
  15109. (interactive)
  15110. (if (stringp org-agenda-files)
  15111. (let ((cw (current-window-configuration)))
  15112. (find-file org-agenda-files)
  15113. (org-set-local 'org-window-configuration cw)
  15114. (org-add-hook 'after-save-hook
  15115. (lambda ()
  15116. (set-window-configuration
  15117. (prog1 org-window-configuration
  15118. (kill-buffer (current-buffer))))
  15119. (org-install-agenda-files-menu)
  15120. (message "New agenda file list installed"))
  15121. nil 'local)
  15122. (message "%s" (substitute-command-keys
  15123. "Edit list and finish with \\[save-buffer]")))
  15124. (customize-variable 'org-agenda-files)))
  15125. (defun org-store-new-agenda-file-list (list)
  15126. "Set new value for the agenda file list and save it correctly."
  15127. (if (stringp org-agenda-files)
  15128. (let ((fe (org-read-agenda-file-list t)) b u)
  15129. (while (setq b (find-buffer-visiting org-agenda-files))
  15130. (kill-buffer b))
  15131. (with-temp-file org-agenda-files
  15132. (insert
  15133. (mapconcat
  15134. (lambda (f) ;; Keep un-expanded entries.
  15135. (if (setq u (assoc f fe))
  15136. (cdr u)
  15137. f))
  15138. list "\n")
  15139. "\n")))
  15140. (let ((org-mode-hook nil) (org-inhibit-startup t)
  15141. (org-insert-mode-line-in-empty-file nil))
  15142. (setq org-agenda-files list)
  15143. (customize-save-variable 'org-agenda-files org-agenda-files))))
  15144. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  15145. "Read the list of agenda files from a file.
  15146. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  15147. filenames, used by `org-store-new-agenda-file-list' to write back
  15148. un-expanded file names."
  15149. (when (file-directory-p org-agenda-files)
  15150. (error "`org-agenda-files' cannot be a single directory"))
  15151. (when (stringp org-agenda-files)
  15152. (with-temp-buffer
  15153. (insert-file-contents org-agenda-files)
  15154. (mapcar
  15155. (lambda (f)
  15156. (let ((e (expand-file-name (substitute-in-file-name f)
  15157. org-directory)))
  15158. (if pair-with-expansion
  15159. (cons e f)
  15160. e)))
  15161. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  15162. ;;;###autoload
  15163. (defun org-cycle-agenda-files ()
  15164. "Cycle through the files in `org-agenda-files'.
  15165. If the current buffer visits an agenda file, find the next one in the list.
  15166. If the current buffer does not, find the first agenda file."
  15167. (interactive)
  15168. (let* ((fs (org-agenda-files t))
  15169. (files (append fs (list (car fs))))
  15170. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  15171. file)
  15172. (unless files (error "No agenda files"))
  15173. (catch 'exit
  15174. (while (setq file (pop files))
  15175. (if (equal (file-truename file) tcf)
  15176. (when (car files)
  15177. (find-file (car files))
  15178. (throw 'exit t))))
  15179. (find-file (car fs)))
  15180. (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  15181. (defun org-agenda-file-to-front (&optional to-end)
  15182. "Move/add the current file to the top of the agenda file list.
  15183. If the file is not present in the list, it is added to the front. If it is
  15184. present, it is moved there. With optional argument TO-END, add/move to the
  15185. end of the list."
  15186. (interactive "P")
  15187. (let ((org-agenda-skip-unavailable-files nil)
  15188. (file-alist (mapcar (lambda (x)
  15189. (cons (file-truename x) x))
  15190. (org-agenda-files t)))
  15191. (ctf (file-truename buffer-file-name))
  15192. x had)
  15193. (setq x (assoc ctf file-alist) had x)
  15194. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  15195. (if to-end
  15196. (setq file-alist (append (delq x file-alist) (list x)))
  15197. (setq file-alist (cons x (delq x file-alist))))
  15198. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  15199. (org-install-agenda-files-menu)
  15200. (message "File %s to %s of agenda file list"
  15201. (if had "moved" "added") (if to-end "end" "front"))))
  15202. (defun org-remove-file (&optional file)
  15203. "Remove current file from the list of files in variable `org-agenda-files'.
  15204. These are the files which are being checked for agenda entries.
  15205. Optional argument FILE means use this file instead of the current."
  15206. (interactive)
  15207. (let* ((org-agenda-skip-unavailable-files nil)
  15208. (file (or file buffer-file-name))
  15209. (true-file (file-truename file))
  15210. (afile (abbreviate-file-name file))
  15211. (files (delq nil (mapcar
  15212. (lambda (x)
  15213. (if (equal true-file
  15214. (file-truename x))
  15215. nil x))
  15216. (org-agenda-files t)))))
  15217. (if (not (= (length files) (length (org-agenda-files t))))
  15218. (progn
  15219. (org-store-new-agenda-file-list files)
  15220. (org-install-agenda-files-menu)
  15221. (message "Removed file: %s" afile))
  15222. (message "File was not in list: %s (not removed)" afile))))
  15223. (defun org-file-menu-entry (file)
  15224. (vector file (list 'find-file file) t))
  15225. (defun org-check-agenda-file (file)
  15226. "Make sure FILE exists. If not, ask user what to do."
  15227. (when (not (file-exists-p file))
  15228. (message "non-existent agenda file %s. [R]emove from list or [A]bort?"
  15229. (abbreviate-file-name file))
  15230. (let ((r (downcase (read-char-exclusive))))
  15231. (cond
  15232. ((equal r ?r)
  15233. (org-remove-file file)
  15234. (throw 'nextfile t))
  15235. (t (error "Abort"))))))
  15236. (defun org-get-agenda-file-buffer (file)
  15237. "Get a buffer visiting FILE. If the buffer needs to be created, add
  15238. it to the list of buffers which might be released later."
  15239. (let ((buf (org-find-base-buffer-visiting file)))
  15240. (if buf
  15241. buf ; just return it
  15242. ;; Make a new buffer and remember it
  15243. (setq buf (find-file-noselect file))
  15244. (if buf (push buf org-agenda-new-buffers))
  15245. buf)))
  15246. (defun org-release-buffers (blist)
  15247. "Release all buffers in list, asking the user for confirmation when needed.
  15248. When a buffer is unmodified, it is just killed. When modified, it is saved
  15249. \(if the user agrees) and then killed."
  15250. (let (buf file)
  15251. (while (setq buf (pop blist))
  15252. (setq file (buffer-file-name buf))
  15253. (when (and (buffer-modified-p buf)
  15254. file
  15255. (y-or-n-p (format "Save file %s? " file)))
  15256. (with-current-buffer buf (save-buffer)))
  15257. (kill-buffer buf))))
  15258. (defun org-prepare-agenda-buffers (files)
  15259. "Create buffers for all agenda files, protect archived trees and comments."
  15260. (interactive)
  15261. (let ((pa '(:org-archived t))
  15262. (pc '(:org-comment t))
  15263. (pall '(:org-archived t :org-comment t))
  15264. (inhibit-read-only t)
  15265. (rea (concat ":" org-archive-tag ":"))
  15266. bmp file re)
  15267. (save-excursion
  15268. (save-restriction
  15269. (while (setq file (pop files))
  15270. (catch 'nextfile
  15271. (if (bufferp file)
  15272. (set-buffer file)
  15273. (org-check-agenda-file file)
  15274. (set-buffer (org-get-agenda-file-buffer file)))
  15275. (widen)
  15276. (setq bmp (buffer-modified-p))
  15277. (org-refresh-category-properties)
  15278. (setq org-todo-keywords-for-agenda
  15279. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  15280. (setq org-done-keywords-for-agenda
  15281. (append org-done-keywords-for-agenda org-done-keywords))
  15282. (setq org-todo-keyword-alist-for-agenda
  15283. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  15284. (setq org-drawers-for-agenda
  15285. (append org-drawers-for-agenda org-drawers))
  15286. (setq org-tag-alist-for-agenda
  15287. (append org-tag-alist-for-agenda org-tag-alist))
  15288. (save-excursion
  15289. (remove-text-properties (point-min) (point-max) pall)
  15290. (when org-agenda-skip-archived-trees
  15291. (goto-char (point-min))
  15292. (while (re-search-forward rea nil t)
  15293. (if (org-at-heading-p t)
  15294. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  15295. (goto-char (point-min))
  15296. (setq re (format org-heading-keyword-regexp-format
  15297. org-comment-string))
  15298. (while (re-search-forward re nil t)
  15299. (add-text-properties
  15300. (match-beginning 0) (org-end-of-subtree t) pc)))
  15301. (set-buffer-modified-p bmp)))))
  15302. (setq org-todo-keywords-for-agenda
  15303. (org-uniquify org-todo-keywords-for-agenda))
  15304. (setq org-todo-keyword-alist-for-agenda
  15305. (org-uniquify org-todo-keyword-alist-for-agenda)
  15306. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  15307. ;;;; Embedded LaTeX
  15308. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  15309. "Keymap for the minor `org-cdlatex-mode'.")
  15310. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  15311. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  15312. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  15313. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  15314. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  15315. (defvar org-cdlatex-texmathp-advice-is-done nil
  15316. "Flag remembering if we have applied the advice to texmathp already.")
  15317. (define-minor-mode org-cdlatex-mode
  15318. "Toggle the minor `org-cdlatex-mode'.
  15319. This mode supports entering LaTeX environment and math in LaTeX fragments
  15320. in Org-mode.
  15321. \\{org-cdlatex-mode-map}"
  15322. nil " OCDL" nil
  15323. (when org-cdlatex-mode
  15324. (require 'cdlatex)
  15325. (run-hooks 'cdlatex-mode-hook)
  15326. (cdlatex-compute-tables))
  15327. (unless org-cdlatex-texmathp-advice-is-done
  15328. (setq org-cdlatex-texmathp-advice-is-done t)
  15329. (defadvice texmathp (around org-math-always-on activate)
  15330. "Always return t in org-mode buffers.
  15331. This is because we want to insert math symbols without dollars even outside
  15332. the LaTeX math segments. If Orgmode thinks that point is actually inside
  15333. an embedded LaTeX fragment, let texmathp do its job.
  15334. \\[org-cdlatex-mode-map]"
  15335. (interactive)
  15336. (let (p)
  15337. (cond
  15338. ((not (eq major-mode 'org-mode)) ad-do-it)
  15339. ((eq this-command 'cdlatex-math-symbol)
  15340. (setq ad-return-value t
  15341. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  15342. (t
  15343. (let ((p (org-inside-LaTeX-fragment-p)))
  15344. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  15345. (setq ad-return-value t
  15346. texmathp-why '("Org-mode embedded math" . 0))
  15347. (if p ad-do-it)))))))))
  15348. (defun turn-on-org-cdlatex ()
  15349. "Unconditionally turn on `org-cdlatex-mode'."
  15350. (org-cdlatex-mode 1))
  15351. (defun org-inside-LaTeX-fragment-p ()
  15352. "Test if point is inside a LaTeX fragment.
  15353. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  15354. sequence appearing also before point.
  15355. Even though the matchers for math are configurable, this function assumes
  15356. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  15357. delimiters are skipped when they have been removed by customization.
  15358. The return value is nil, or a cons cell with the delimiter and the
  15359. position of this delimiter.
  15360. This function does a reasonably good job, but can locally be fooled by
  15361. for example currency specifications. For example it will assume being in
  15362. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  15363. fragments that are properly closed, but during editing, we have to live
  15364. with the uncertainty caused by missing closing delimiters. This function
  15365. looks only before point, not after."
  15366. (catch 'exit
  15367. (let ((pos (point))
  15368. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  15369. (lim (progn
  15370. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  15371. (point)))
  15372. dd-on str (start 0) m re)
  15373. (goto-char pos)
  15374. (when dodollar
  15375. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  15376. re (nth 1 (assoc "$" org-latex-regexps)))
  15377. (while (string-match re str start)
  15378. (cond
  15379. ((= (match-end 0) (length str))
  15380. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  15381. ((= (match-end 0) (- (length str) 5))
  15382. (throw 'exit nil))
  15383. (t (setq start (match-end 0))))))
  15384. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  15385. (goto-char pos)
  15386. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  15387. (and (match-beginning 2) (throw 'exit nil))
  15388. ;; count $$
  15389. (while (re-search-backward "\\$\\$" lim t)
  15390. (setq dd-on (not dd-on)))
  15391. (goto-char pos)
  15392. (if dd-on (cons "$$" m))))))
  15393. (defun org-inside-latex-macro-p ()
  15394. "Is point inside a LaTeX macro or its arguments?"
  15395. (save-match-data
  15396. (org-in-regexp
  15397. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  15398. (defun org-try-cdlatex-tab ()
  15399. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  15400. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  15401. - inside a LaTeX fragment, or
  15402. - after the first word in a line, where an abbreviation expansion could
  15403. insert a LaTeX environment."
  15404. (when org-cdlatex-mode
  15405. (cond
  15406. ;; Before any word on the line: No expansion possible.
  15407. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  15408. ;; Just after first word on the line: Expand it. Make sure it
  15409. ;; cannot happen on headlines, though.
  15410. ((save-excursion
  15411. (skip-chars-backward "a-zA-Z0-9*")
  15412. (skip-chars-backward " \t")
  15413. (and (bolp) (not (org-at-heading-p))))
  15414. (cdlatex-tab) t)
  15415. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  15416. (defun org-cdlatex-underscore-caret (&optional arg)
  15417. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  15418. Revert to the normal definition outside of these fragments."
  15419. (interactive "P")
  15420. (if (org-inside-LaTeX-fragment-p)
  15421. (call-interactively 'cdlatex-sub-superscript)
  15422. (let (org-cdlatex-mode)
  15423. (call-interactively (key-binding (vector last-input-event))))))
  15424. (defun org-cdlatex-math-modify (&optional arg)
  15425. "Execute `cdlatex-math-modify' in LaTeX fragments.
  15426. Revert to the normal definition outside of these fragments."
  15427. (interactive "P")
  15428. (if (org-inside-LaTeX-fragment-p)
  15429. (call-interactively 'cdlatex-math-modify)
  15430. (let (org-cdlatex-mode)
  15431. (call-interactively (key-binding (vector last-input-event))))))
  15432. (defvar org-latex-fragment-image-overlays nil
  15433. "List of overlays carrying the images of latex fragments.")
  15434. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  15435. (defun org-remove-latex-fragment-image-overlays ()
  15436. "Remove all overlays with LaTeX fragment images in current buffer."
  15437. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  15438. (setq org-latex-fragment-image-overlays nil))
  15439. (defun org-preview-latex-fragment (&optional subtree)
  15440. "Preview the LaTeX fragment at point, or all locally or globally.
  15441. If the cursor is in a LaTeX fragment, create the image and overlay
  15442. it over the source code. If there is no fragment at point, display
  15443. all fragments in the current text, from one headline to the next. With
  15444. prefix SUBTREE, display all fragments in the current subtree. With a
  15445. double prefix arg \\[universal-argument] \\[universal-argument], or when \
  15446. the cursor is before the first headline,
  15447. display all fragments in the buffer.
  15448. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  15449. (interactive "P")
  15450. (unless buffer-file-name
  15451. (error "Can't preview LaTeX fragment in a non-file buffer"))
  15452. (org-remove-latex-fragment-image-overlays)
  15453. (save-excursion
  15454. (save-restriction
  15455. (let (beg end at msg)
  15456. (cond
  15457. ((or (equal subtree '(16))
  15458. (not (save-excursion
  15459. (re-search-backward org-outline-regexp-bol nil t))))
  15460. (setq beg (point-min) end (point-max)
  15461. msg "Creating images for buffer...%s"))
  15462. ((equal subtree '(4))
  15463. (org-back-to-heading)
  15464. (setq beg (point) end (org-end-of-subtree t)
  15465. msg "Creating images for subtree...%s"))
  15466. (t
  15467. (if (setq at (org-inside-LaTeX-fragment-p))
  15468. (goto-char (max (point-min) (- (cdr at) 2)))
  15469. (org-back-to-heading))
  15470. (setq beg (point) end (progn (outline-next-heading) (point))
  15471. msg (if at "Creating image...%s"
  15472. "Creating images for entry...%s"))))
  15473. (message msg "")
  15474. (narrow-to-region beg end)
  15475. (goto-char beg)
  15476. (org-format-latex
  15477. (concat "ltxpng/" (file-name-sans-extension
  15478. (file-name-nondirectory
  15479. buffer-file-name)))
  15480. default-directory 'overlays msg at 'forbuffer 'dvipng)
  15481. (message msg "done. Use `C-c C-c' to remove images.")))))
  15482. (defvar org-latex-regexps
  15483. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  15484. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  15485. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  15486. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  15487. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  15488. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  15489. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  15490. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  15491. "Regular expressions for matching embedded LaTeX.")
  15492. (defvar org-export-have-math nil) ;; dynamic scoping
  15493. (defun org-format-latex (prefix &optional dir overlays msg at
  15494. forbuffer processing-type)
  15495. "Replace LaTeX fragments with links to an image, and produce images.
  15496. Some of the options can be changed using the variable
  15497. `org-format-latex-options'."
  15498. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  15499. (let* ((prefixnodir (file-name-nondirectory prefix))
  15500. (absprefix (expand-file-name prefix dir))
  15501. (todir (file-name-directory absprefix))
  15502. (opt org-format-latex-options)
  15503. (matchers (plist-get opt :matchers))
  15504. (re-list org-latex-regexps)
  15505. (org-format-latex-header-extra
  15506. (plist-get (org-infile-export-plist) :latex-header-extra))
  15507. (cnt 0) txt hash link beg end re e checkdir
  15508. executables-checked string
  15509. m n block-type block linkfile movefile ov)
  15510. ;; Check the different regular expressions
  15511. (while (setq e (pop re-list))
  15512. (setq m (car e) re (nth 1 e) n (nth 2 e) block-type (nth 3 e)
  15513. block (if block-type "\n\n" ""))
  15514. (when (member m matchers)
  15515. (goto-char (point-min))
  15516. (while (re-search-forward re nil t)
  15517. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  15518. (not (get-text-property (match-beginning n)
  15519. 'org-protected))
  15520. (or (not overlays)
  15521. (not (eq (get-char-property (match-beginning n)
  15522. 'org-overlay-type)
  15523. 'org-latex-overlay))))
  15524. (setq org-export-have-math t)
  15525. (cond
  15526. ((eq processing-type 'verbatim)
  15527. ;; Leave the text verbatim, just protect it
  15528. (add-text-properties (match-beginning n) (match-end n)
  15529. '(org-protected t)))
  15530. ((eq processing-type 'mathjax)
  15531. ;; Prepare for MathJax processing
  15532. (setq string (match-string n))
  15533. (if (member m '("$" "$1"))
  15534. (save-excursion
  15535. (delete-region (match-beginning n) (match-end n))
  15536. (goto-char (match-beginning n))
  15537. (insert (org-add-props (concat "\\(" (substring string 1 -1)
  15538. "\\)")
  15539. '(org-protected t))))
  15540. (add-text-properties (match-beginning n) (match-end n)
  15541. '(org-protected t))))
  15542. ((eq processing-type 'dvipng)
  15543. ;; Process to an image
  15544. (setq txt (match-string n)
  15545. beg (match-beginning n) end (match-end n)
  15546. cnt (1+ cnt))
  15547. (let (print-length print-level) ; make sure full list is printed
  15548. (setq hash (sha1 (prin1-to-string
  15549. (list org-format-latex-header
  15550. org-format-latex-header-extra
  15551. org-export-latex-default-packages-alist
  15552. org-export-latex-packages-alist
  15553. org-format-latex-options
  15554. forbuffer txt)))
  15555. linkfile (format "%s_%s.png" prefix hash)
  15556. movefile (format "%s_%s.png" absprefix hash)))
  15557. (setq link (concat block "[[file:" linkfile "]]" block))
  15558. (if msg (message msg cnt))
  15559. (goto-char beg)
  15560. (unless checkdir ; make sure the directory exists
  15561. (setq checkdir t)
  15562. (or (file-directory-p todir) (make-directory todir t)))
  15563. (unless executables-checked
  15564. (org-check-external-command
  15565. "latex" "needed to convert LaTeX fragments to images")
  15566. (org-check-external-command
  15567. "dvipng" "needed to convert LaTeX fragments to images")
  15568. (setq executables-checked t))
  15569. (unless (file-exists-p movefile)
  15570. (org-create-formula-image
  15571. txt movefile opt forbuffer))
  15572. (if overlays
  15573. (progn
  15574. (mapc (lambda (o)
  15575. (if (eq (overlay-get o 'org-overlay-type)
  15576. 'org-latex-overlay)
  15577. (delete-overlay o)))
  15578. (overlays-in beg end))
  15579. (setq ov (make-overlay beg end))
  15580. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  15581. (if (featurep 'xemacs)
  15582. (progn
  15583. (overlay-put ov 'invisible t)
  15584. (overlay-put
  15585. ov 'end-glyph
  15586. (make-glyph (vector 'png :file movefile))))
  15587. (overlay-put
  15588. ov 'display
  15589. (list 'image :type 'png :file movefile :ascent 'center)))
  15590. (push ov org-latex-fragment-image-overlays)
  15591. (goto-char end))
  15592. (delete-region beg end)
  15593. (insert (org-add-props link
  15594. (list 'org-latex-src
  15595. (replace-regexp-in-string
  15596. "\"" "" txt)
  15597. 'org-latex-src-embed-type
  15598. (if block-type 'paragraph 'character))))))
  15599. ((eq processing-type 'mathml)
  15600. ;; Process to MathML
  15601. (unless executables-checked
  15602. (unless (save-match-data (org-format-latex-mathml-available-p))
  15603. (error "LaTeX to MathML converter not configured"))
  15604. (setq executables-checked t))
  15605. (setq txt (match-string n)
  15606. beg (match-beginning n) end (match-end n)
  15607. cnt (1+ cnt))
  15608. (if msg (message msg cnt))
  15609. (goto-char beg)
  15610. (delete-region beg end)
  15611. (insert (org-format-latex-as-mathml
  15612. txt block-type prefix dir)))
  15613. (t
  15614. (error "Unknown conversion type %s for latex fragments"
  15615. processing-type)))))))))
  15616. (defun org-create-math-formula (latex-frag &optional mathml-file)
  15617. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  15618. Use `org-latex-to-mathml-convert-command'. If the conversion is
  15619. sucessful, return the portion between \"<math...> </math>\"
  15620. elements otherwise return nil. When MATHML-FILE is specified,
  15621. write the results in to that file. When invoked as an
  15622. interactive command, prompt for LATEX-FRAG, with initial value
  15623. set to the current active region and echo the results for user
  15624. inspection."
  15625. (interactive (list (let ((frag (when (region-active-p)
  15626. (buffer-substring-no-properties
  15627. (region-beginning) (region-end)))))
  15628. (read-string "LaTeX Fragment: " frag nil frag))))
  15629. (unless latex-frag (error "Invalid latex-frag"))
  15630. (let* ((tmp-in-file (file-relative-name
  15631. (make-temp-name (expand-file-name "ltxmathml-in"))))
  15632. (ignore (write-region latex-frag nil tmp-in-file))
  15633. (tmp-out-file (file-relative-name
  15634. (make-temp-name (expand-file-name "ltxmathml-out"))))
  15635. (cmd (format-spec
  15636. org-latex-to-mathml-convert-command
  15637. `((?j . ,(shell-quote-argument
  15638. (expand-file-name org-latex-to-mathml-jar-file)))
  15639. (?I . ,(shell-quote-argument tmp-in-file))
  15640. (?o . ,(shell-quote-argument tmp-out-file)))))
  15641. mathml shell-command-output)
  15642. (when (org-called-interactively-p 'any)
  15643. (unless (org-format-latex-mathml-available-p)
  15644. (error "LaTeX to MathML converter not configured")))
  15645. (message "Running %s" cmd)
  15646. (setq shell-command-output (shell-command-to-string cmd))
  15647. (setq mathml
  15648. (when (file-readable-p tmp-out-file)
  15649. (with-current-buffer (find-file-noselect tmp-out-file t)
  15650. (goto-char (point-min))
  15651. (when (re-search-forward
  15652. (concat
  15653. (regexp-quote
  15654. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
  15655. "\\(.\\|\n\\)*"
  15656. (regexp-quote "</math>")) nil t)
  15657. (prog1 (match-string 0) (kill-buffer))))))
  15658. (cond
  15659. (mathml
  15660. (setq mathml
  15661. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  15662. (when mathml-file
  15663. (write-region mathml nil mathml-file))
  15664. (when (org-called-interactively-p 'any)
  15665. (message mathml)))
  15666. ((message "LaTeX to MathML conversion failed")
  15667. (message shell-command-output)))
  15668. (delete-file tmp-in-file)
  15669. (when (file-exists-p tmp-out-file)
  15670. (delete-file tmp-out-file))
  15671. mathml))
  15672. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  15673. prefix &optional dir)
  15674. "Use `org-create-math-formula' but check local cache first."
  15675. (let* ((absprefix (expand-file-name prefix dir))
  15676. (print-length nil) (print-level nil)
  15677. (formula-id (concat
  15678. "formula-"
  15679. (sha1
  15680. (prin1-to-string
  15681. (list latex-frag
  15682. org-latex-to-mathml-convert-command)))))
  15683. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  15684. (formula-cache-dir (file-name-directory formula-cache)))
  15685. (unless (file-directory-p formula-cache-dir)
  15686. (make-directory formula-cache-dir t))
  15687. (unless (file-exists-p formula-cache)
  15688. (org-create-math-formula latex-frag formula-cache))
  15689. (if (file-exists-p formula-cache)
  15690. ;; Successful conversion. Return the link to MathML file.
  15691. (org-add-props
  15692. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  15693. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  15694. 'org-latex-src-embed-type (if latex-frag-type
  15695. 'paragraph 'character)))
  15696. ;; Failed conversion. Return the LaTeX fragment verbatim
  15697. (add-text-properties
  15698. 0 (1- (length latex-frag)) '(org-protected t) latex-frag)
  15699. latex-frag)))
  15700. ;; This function borrows from Ganesh Swami's latex2png.el
  15701. (defun org-create-formula-image (string tofile options buffer)
  15702. "This calls dvipng."
  15703. (require 'org-latex)
  15704. (let* ((tmpdir (if (featurep 'xemacs)
  15705. (temp-directory)
  15706. temporary-file-directory))
  15707. (texfilebase (make-temp-name
  15708. (expand-file-name "orgtex" tmpdir)))
  15709. (texfile (concat texfilebase ".tex"))
  15710. (dvifile (concat texfilebase ".dvi"))
  15711. (pngfile (concat texfilebase ".png"))
  15712. (fnh (if (featurep 'xemacs)
  15713. (font-height (face-font 'default))
  15714. (face-attribute 'default :height nil)))
  15715. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  15716. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  15717. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  15718. "Black"))
  15719. (bg (or (plist-get options (if buffer :background :html-background))
  15720. "Transparent")))
  15721. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  15722. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  15723. (with-temp-file texfile
  15724. (insert (org-splice-latex-header
  15725. org-format-latex-header
  15726. org-export-latex-default-packages-alist
  15727. org-export-latex-packages-alist t
  15728. org-format-latex-header-extra))
  15729. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")
  15730. (require 'org-latex)
  15731. (org-export-latex-fix-inputenc))
  15732. (let ((dir default-directory))
  15733. (condition-case nil
  15734. (progn
  15735. (cd tmpdir)
  15736. (call-process "latex" nil nil nil texfile))
  15737. (error nil))
  15738. (cd dir))
  15739. (if (not (file-exists-p dvifile))
  15740. (progn (message "Failed to create dvi file from %s" texfile) nil)
  15741. (condition-case nil
  15742. (if (featurep 'xemacs)
  15743. (call-process "dvipng" nil nil nil
  15744. "-fg" fg "-bg" bg
  15745. "-T" "tight"
  15746. "-o" pngfile
  15747. dvifile)
  15748. (call-process "dvipng" nil nil nil
  15749. "-fg" fg "-bg" bg
  15750. "-D" dpi
  15751. ;;"-x" scale "-y" scale
  15752. "-T" "tight"
  15753. "-o" pngfile
  15754. dvifile))
  15755. (error nil))
  15756. (if (not (file-exists-p pngfile))
  15757. (if org-format-latex-signal-error
  15758. (error "Failed to create png file from %s" texfile)
  15759. (message "Failed to create png file from %s" texfile)
  15760. nil)
  15761. ;; Use the requested file name and clean up
  15762. (copy-file pngfile tofile 'replace)
  15763. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  15764. (delete-file (concat texfilebase e)))
  15765. pngfile))))
  15766. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  15767. "Fill a LaTeX header template TPL.
  15768. In the template, the following place holders will be recognized:
  15769. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  15770. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  15771. [PACKAGES] \\usepackage statements for PKG
  15772. [NO-PACKAGES] do not include PKG
  15773. [EXTRA] the string EXTRA
  15774. [NO-EXTRA] do not include EXTRA
  15775. For backward compatibility, if both the positive and the negative place
  15776. holder is missing, the positive one (without the \"NO-\") will be
  15777. assumed to be present at the end of the template.
  15778. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  15779. EXTRA is a string.
  15780. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  15781. (let (rpl (end ""))
  15782. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  15783. (setq rpl (if (or (match-end 1) (not def-pkg))
  15784. "" (org-latex-packages-to-string def-pkg snippets-p t))
  15785. tpl (replace-match rpl t t tpl))
  15786. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  15787. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  15788. (setq rpl (if (or (match-end 1) (not pkg))
  15789. "" (org-latex-packages-to-string pkg snippets-p t))
  15790. tpl (replace-match rpl t t tpl))
  15791. (if pkg (setq end
  15792. (concat end "\n"
  15793. (org-latex-packages-to-string pkg snippets-p)))))
  15794. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  15795. (setq rpl (if (or (match-end 1) (not extra))
  15796. "" (concat extra "\n"))
  15797. tpl (replace-match rpl t t tpl))
  15798. (if (and extra (string-match "\\S-" extra))
  15799. (setq end (concat end "\n" extra))))
  15800. (if (string-match "\\S-" end)
  15801. (concat tpl "\n" end)
  15802. tpl)))
  15803. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  15804. "Turn an alist of packages into a string with the \\usepackage macros."
  15805. (setq pkg (mapconcat (lambda(p)
  15806. (cond
  15807. ((stringp p) p)
  15808. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  15809. (format "%% Package %s omitted" (cadr p)))
  15810. ((equal "" (car p))
  15811. (format "\\usepackage{%s}" (cadr p)))
  15812. (t
  15813. (format "\\usepackage[%s]{%s}"
  15814. (car p) (cadr p)))))
  15815. pkg
  15816. "\n"))
  15817. (if newline (concat pkg "\n") pkg))
  15818. (defun org-dvipng-color (attr)
  15819. "Return an rgb color specification for dvipng."
  15820. (apply 'format "rgb %s %s %s"
  15821. (mapcar 'org-normalize-color
  15822. (if (featurep 'xemacs)
  15823. (color-rgb-components
  15824. (face-property 'default
  15825. (cond ((eq attr :foreground) 'foreground)
  15826. ((eq attr :background) 'background))))
  15827. (color-values (face-attribute 'default attr nil))))))
  15828. (defun org-normalize-color (value)
  15829. "Return string to be used as color value for an RGB component."
  15830. (format "%g" (/ value 65535.0)))
  15831. ;; Image display
  15832. (defvar org-inline-image-overlays nil)
  15833. (make-variable-buffer-local 'org-inline-image-overlays)
  15834. (defun org-toggle-inline-images (&optional include-linked)
  15835. "Toggle the display of inline images.
  15836. INCLUDE-LINKED is passed to `org-display-inline-images'."
  15837. (interactive "P")
  15838. (if org-inline-image-overlays
  15839. (progn
  15840. (org-remove-inline-images)
  15841. (message "Inline image display turned off"))
  15842. (org-display-inline-images include-linked)
  15843. (if org-inline-image-overlays
  15844. (message "%d images displayed inline"
  15845. (length org-inline-image-overlays))
  15846. (message "No images to display inline"))))
  15847. (defun org-display-inline-images (&optional include-linked refresh beg end)
  15848. "Display inline images.
  15849. Normally only links without a description part are inlined, because this
  15850. is how it will work for export. When INCLUDE-LINKED is set, also links
  15851. with a description part will be inlined. This can be nice for a quick
  15852. look at those images, but it does not reflect what exported files will look
  15853. like.
  15854. When REFRESH is set, refresh existing images between BEG and END.
  15855. This will create new image displays only if necessary.
  15856. BEG and END default to the buffer boundaries."
  15857. (interactive "P")
  15858. (unless refresh
  15859. (org-remove-inline-images)
  15860. (if (fboundp 'clear-image-cache) (clear-image-cache)))
  15861. (save-excursion
  15862. (save-restriction
  15863. (widen)
  15864. (setq beg (or beg (point-min)) end (or end (point-max)))
  15865. (goto-char beg)
  15866. (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
  15867. (substring (org-image-file-name-regexp) 0 -2)
  15868. "\\)\\]" (if include-linked "" "\\]")))
  15869. old file ov img)
  15870. (while (re-search-forward re end t)
  15871. (setq old (get-char-property-and-overlay (match-beginning 1)
  15872. 'org-image-overlay))
  15873. (setq file (expand-file-name
  15874. (concat (or (match-string 3) "") (match-string 4))))
  15875. (when (file-exists-p file)
  15876. (if (and (car-safe old) refresh)
  15877. (image-refresh (overlay-get (cdr old) 'display))
  15878. (setq img (save-match-data (create-image file)))
  15879. (when img
  15880. (setq ov (make-overlay (match-beginning 0) (match-end 0)))
  15881. (overlay-put ov 'display img)
  15882. (overlay-put ov 'face 'default)
  15883. (overlay-put ov 'org-image-overlay t)
  15884. (overlay-put ov 'modification-hooks
  15885. (list 'org-display-inline-modification-hook))
  15886. (push ov org-inline-image-overlays)))))))))
  15887. (defun org-display-inline-modification-hook (ov after beg end &optional len)
  15888. "Remove inline-display overlay if a corresponding region is modified."
  15889. (let ((inhibit-modification-hooks t))
  15890. (when (and ov after)
  15891. (delete ov org-inline-image-overlays)
  15892. (delete-overlay ov))))
  15893. (defun org-remove-inline-images ()
  15894. "Remove inline display of images."
  15895. (interactive)
  15896. (mapc 'delete-overlay org-inline-image-overlays)
  15897. (setq org-inline-image-overlays nil))
  15898. ;;;; Key bindings
  15899. ;; Outline functions from `outline-mode-prefix-map'
  15900. ;; that can be remapped in Org:
  15901. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  15902. (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
  15903. (define-key org-mode-map [remap outline-forward-same-level]
  15904. 'org-forward-same-level)
  15905. (define-key org-mode-map [remap outline-backward-same-level]
  15906. 'org-backward-same-level)
  15907. (define-key org-mode-map [remap show-branches]
  15908. 'org-kill-note-or-show-branches)
  15909. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  15910. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  15911. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  15912. ;; Outline functions from `outline-mode-prefix-map'
  15913. ;; that can not be remapped in Org:
  15914. ;; - the column "key binding" shows whether the Outline function is still
  15915. ;; available in Org mode on the same key that it has been bound to in
  15916. ;; Outline mode:
  15917. ;; - "overridden": key used for a different functionality in Org mode
  15918. ;; - else: key still bound to the same Outline function in Org mode
  15919. ;; | Outline function | key binding | Org replacement |
  15920. ;; |------------------------------------+-------------+-----------------------|
  15921. ;; | `outline-next-visible-heading' | `C-c C-n' | still same function |
  15922. ;; | `outline-previous-visible-heading' | `C-c C-p' | still same function |
  15923. ;; | `show-children' | `C-c C-i' | visibility cycling |
  15924. ;; | `hide-subtree' | overridden | visibility cycling |
  15925. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  15926. ;; | `hide-body' | overridden | no replacement |
  15927. ;; | `show-all' | overridden | no replacement |
  15928. ;; | `hide-entry' | overridden | visibility cycling |
  15929. ;; | `show-entry' | overridden | no replacement |
  15930. ;; | `hide-leaves' | overridden | no replacement |
  15931. ;; | `hide-sublevels' | overridden | no replacement |
  15932. ;; | `hide-other' | overridden | no replacement |
  15933. ;; | `outline-move-subtree-up' | `C-c C-^' | better: org-shiftup |
  15934. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  15935. ;; Make `C-c C-x' a prefix key
  15936. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  15937. ;; TAB key with modifiers
  15938. (org-defkey org-mode-map "\C-i" 'org-cycle)
  15939. (org-defkey org-mode-map [(tab)] 'org-cycle)
  15940. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  15941. (org-defkey org-mode-map "\M-\t" 'pcomplete)
  15942. ;; The following line is necessary under Suse GNU/Linux
  15943. (unless (featurep 'xemacs)
  15944. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  15945. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  15946. (define-key org-mode-map [backtab] 'org-shifttab)
  15947. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  15948. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  15949. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  15950. ;; Cursor keys with modifiers
  15951. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  15952. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  15953. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  15954. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  15955. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  15956. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  15957. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  15958. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  15959. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  15960. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  15961. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  15962. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  15963. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  15964. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  15965. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  15966. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  15967. ;; Babel keys
  15968. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  15969. (mapc (lambda (pair)
  15970. (define-key org-babel-map (car pair) (cdr pair)))
  15971. org-babel-key-bindings)
  15972. ;;; Extra keys for tty access.
  15973. ;; We only set them when really needed because otherwise the
  15974. ;; menus don't show the simple keys
  15975. (when (or org-use-extra-keys
  15976. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  15977. (not window-system))
  15978. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  15979. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  15980. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  15981. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  15982. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  15983. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  15984. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  15985. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  15986. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  15987. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  15988. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  15989. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  15990. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  15991. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  15992. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  15993. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  15994. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  15995. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  15996. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  15997. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  15998. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  15999. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  16000. (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
  16001. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  16002. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  16003. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  16004. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  16005. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  16006. ;; All the other keys
  16007. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  16008. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  16009. (if (boundp 'narrow-map)
  16010. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  16011. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  16012. (if (boundp 'narrow-map)
  16013. (org-defkey narrow-map "b" 'org-narrow-to-block)
  16014. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  16015. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-same-level)
  16016. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-same-level)
  16017. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  16018. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  16019. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  16020. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  16021. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  16022. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  16023. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  16024. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  16025. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  16026. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  16027. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  16028. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  16029. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  16030. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  16031. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  16032. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  16033. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  16034. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  16035. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  16036. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  16037. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  16038. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  16039. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  16040. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  16041. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  16042. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  16043. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  16044. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  16045. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  16046. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  16047. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  16048. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  16049. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  16050. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  16051. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  16052. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  16053. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  16054. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  16055. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  16056. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  16057. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  16058. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  16059. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  16060. (org-defkey org-mode-map "\C-c^" 'org-sort)
  16061. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  16062. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  16063. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  16064. (org-defkey org-mode-map "\C-m" 'org-return)
  16065. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  16066. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  16067. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  16068. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  16069. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  16070. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  16071. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  16072. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  16073. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  16074. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  16075. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  16076. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  16077. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  16078. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  16079. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  16080. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  16081. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  16082. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  16083. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  16084. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  16085. (org-defkey org-mode-map "\C-c\C-@" 'org-mark-list)
  16086. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  16087. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  16088. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  16089. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  16090. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  16091. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  16092. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  16093. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  16094. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  16095. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  16096. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  16097. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  16098. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  16099. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  16100. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  16101. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  16102. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  16103. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  16104. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  16105. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  16106. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  16107. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  16108. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  16109. (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
  16110. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  16111. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  16112. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  16113. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  16114. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  16115. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  16116. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  16117. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  16118. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  16119. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  16120. (when (featurep 'xemacs)
  16121. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  16122. (defconst org-speed-commands-default
  16123. '(
  16124. ("Outline Navigation")
  16125. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  16126. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  16127. ("f" . (org-speed-move-safe 'org-forward-same-level))
  16128. ("b" . (org-speed-move-safe 'org-backward-same-level))
  16129. ("u" . (org-speed-move-safe 'outline-up-heading))
  16130. ("j" . org-goto)
  16131. ("g" . (org-refile t))
  16132. ("Outline Visibility")
  16133. ("c" . org-cycle)
  16134. ("C" . org-shifttab)
  16135. (" " . org-display-outline-path)
  16136. ("Outline Structure Editing")
  16137. ("U" . org-shiftmetaup)
  16138. ("D" . org-shiftmetadown)
  16139. ("r" . org-metaright)
  16140. ("l" . org-metaleft)
  16141. ("R" . org-shiftmetaright)
  16142. ("L" . org-shiftmetaleft)
  16143. ("i" . (progn (forward-char 1) (call-interactively
  16144. 'org-insert-heading-respect-content)))
  16145. ("^" . org-sort)
  16146. ("w" . org-refile)
  16147. ("a" . org-archive-subtree-default-with-confirmation)
  16148. ("." . org-mark-subtree)
  16149. ("Clock Commands")
  16150. ("I" . org-clock-in)
  16151. ("O" . org-clock-out)
  16152. ("Meta Data Editing")
  16153. ("t" . org-todo)
  16154. ("0" . (org-priority ?\ ))
  16155. ("1" . (org-priority ?A))
  16156. ("2" . (org-priority ?B))
  16157. ("3" . (org-priority ?C))
  16158. (";" . org-set-tags-command)
  16159. ("e" . org-set-effort)
  16160. ("Agenda Views etc")
  16161. ("v" . org-agenda)
  16162. ("/" . org-sparse-tree)
  16163. ("Misc")
  16164. ("o" . org-open-at-point)
  16165. ("?" . org-speed-command-help)
  16166. ("<" . (org-agenda-set-restriction-lock 'subtree))
  16167. (">" . (org-agenda-remove-restriction-lock))
  16168. )
  16169. "The default speed commands.")
  16170. (defun org-print-speed-command (e)
  16171. (if (> (length (car e)) 1)
  16172. (progn
  16173. (princ "\n")
  16174. (princ (car e))
  16175. (princ "\n")
  16176. (princ (make-string (length (car e)) ?-))
  16177. (princ "\n"))
  16178. (princ (car e))
  16179. (princ " ")
  16180. (if (symbolp (cdr e))
  16181. (princ (symbol-name (cdr e)))
  16182. (prin1 (cdr e)))
  16183. (princ "\n")))
  16184. (defun org-speed-command-help ()
  16185. "Show the available speed commands."
  16186. (interactive)
  16187. (if (not org-use-speed-commands)
  16188. (error "Speed commands are not activated, customize `org-use-speed-commands'")
  16189. (with-output-to-temp-buffer "*Help*"
  16190. (princ "User-defined Speed commands\n===========================\n")
  16191. (mapc 'org-print-speed-command org-speed-commands-user)
  16192. (princ "\n")
  16193. (princ "Built-in Speed commands\n=======================\n")
  16194. (mapc 'org-print-speed-command org-speed-commands-default))
  16195. (with-current-buffer "*Help*"
  16196. (setq truncate-lines t))))
  16197. (defun org-speed-move-safe (cmd)
  16198. "Execute CMD, but make sure that the cursor always ends up in a headline.
  16199. If not, return to the original position and throw an error."
  16200. (interactive)
  16201. (let ((pos (point)))
  16202. (call-interactively cmd)
  16203. (unless (and (bolp) (org-at-heading-p))
  16204. (goto-char pos)
  16205. (error "Boundary reached while executing %s" cmd))))
  16206. (defvar org-self-insert-command-undo-counter 0)
  16207. (defvar org-table-auto-blank-field) ; defined in org-table.el
  16208. (defvar org-speed-command nil)
  16209. (defun org-speed-command-default-hook (keys)
  16210. "Hook for activating single-letter speed commands.
  16211. `org-speed-commands-default' specifies a minimal command set.
  16212. Use `org-speed-commands-user' for further customization."
  16213. (when (or (and (bolp) (looking-at org-outline-regexp))
  16214. (and (functionp org-use-speed-commands)
  16215. (funcall org-use-speed-commands)))
  16216. (cdr (assoc keys (append org-speed-commands-user
  16217. org-speed-commands-default)))))
  16218. (defun org-babel-speed-command-hook (keys)
  16219. "Hook for activating single-letter code block commands."
  16220. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  16221. (cdr (assoc keys org-babel-key-bindings))))
  16222. (defcustom org-speed-command-hook
  16223. '(org-speed-command-default-hook org-babel-speed-command-hook)
  16224. "Hook for activating speed commands at strategic locations.
  16225. Hook functions are called in sequence until a valid handler is
  16226. found.
  16227. Each hook takes a single argument, a user-pressed command key
  16228. which is also a `self-insert-command' from the global map.
  16229. Within the hook, examine the cursor position and the command key
  16230. and return nil or a valid handler as appropriate. Handler could
  16231. be one of an interactive command, a function, or a form.
  16232. Set `org-use-speed-commands' to non-nil value to enable this
  16233. hook. The default setting is `org-speed-command-default-hook'."
  16234. :group 'org-structure
  16235. :version "24.1"
  16236. :type 'hook)
  16237. (defun org-self-insert-command (N)
  16238. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  16239. If the cursor is in a table looking at whitespace, the whitespace is
  16240. overwritten, and the table is not marked as requiring realignment."
  16241. (interactive "p")
  16242. (org-check-before-invisible-edit 'insert)
  16243. (cond
  16244. ((and org-use-speed-commands
  16245. (setq org-speed-command
  16246. (run-hook-with-args-until-success
  16247. 'org-speed-command-hook (this-command-keys))))
  16248. (cond
  16249. ((commandp org-speed-command)
  16250. (setq this-command org-speed-command)
  16251. (call-interactively org-speed-command))
  16252. ((functionp org-speed-command)
  16253. (funcall org-speed-command))
  16254. ((and org-speed-command (listp org-speed-command))
  16255. (eval org-speed-command))
  16256. (t (let (org-use-speed-commands)
  16257. (call-interactively 'org-self-insert-command)))))
  16258. ((and
  16259. (org-table-p)
  16260. (progn
  16261. ;; check if we blank the field, and if that triggers align
  16262. (and (featurep 'org-table) org-table-auto-blank-field
  16263. (member last-command
  16264. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  16265. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  16266. ;; got extra space, this field does not determine column width
  16267. (let (org-table-may-need-update) (org-table-blank-field))
  16268. ;; no extra space, this field may determine column width
  16269. (org-table-blank-field)))
  16270. t)
  16271. (eq N 1)
  16272. (looking-at "[^|\n]* |"))
  16273. (let (org-table-may-need-update)
  16274. (goto-char (1- (match-end 0)))
  16275. (backward-delete-char 1)
  16276. (goto-char (match-beginning 0))
  16277. (self-insert-command N)))
  16278. (t
  16279. (setq org-table-may-need-update t)
  16280. (self-insert-command N)
  16281. (org-fix-tags-on-the-fly)
  16282. (if org-self-insert-cluster-for-undo
  16283. (if (not (eq last-command 'org-self-insert-command))
  16284. (setq org-self-insert-command-undo-counter 1)
  16285. (if (>= org-self-insert-command-undo-counter 20)
  16286. (setq org-self-insert-command-undo-counter 1)
  16287. (and (> org-self-insert-command-undo-counter 0)
  16288. buffer-undo-list (listp buffer-undo-list)
  16289. (not (cadr buffer-undo-list)) ; remove nil entry
  16290. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  16291. (setq org-self-insert-command-undo-counter
  16292. (1+ org-self-insert-command-undo-counter))))))))
  16293. (defun org-check-before-invisible-edit (kind)
  16294. "Check is editing if kind KIND would be dangerous with invisible text around.
  16295. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  16296. ;; First, try to get out of here as quickly as possible, to reduce overhead
  16297. (if (and org-catch-invisible-edits
  16298. (or (not (boundp 'visible-mode)) (not visible-mode))
  16299. (or (get-char-property (point) 'invisible)
  16300. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  16301. ;; OK, we need to take a closer look
  16302. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  16303. (invisible-before-point (if (bobp) nil (get-char-property
  16304. (1- (point)) 'invisible)))
  16305. (border-and-ok-direction
  16306. (or
  16307. ;; Check if we are acting predictably before invisible text
  16308. (and invisible-at-point (not invisible-before-point)
  16309. (memq kind '(insert delete-backward)))
  16310. ;; Check if we are acting predictably after invisible text
  16311. ;; This works not well, and I have turned it off. It seems
  16312. ;; better to always show and stop after invisible text.
  16313. ;; (and (not invisible-at-point) invisible-before-point
  16314. ;; (memq kind '(insert delete)))
  16315. )))
  16316. (when (or (memq invisible-at-point '(outline org-hide-block))
  16317. (memq invisible-before-point '(outline org-hide-block)))
  16318. (if (eq org-catch-invisible-edits 'error)
  16319. (error "Editing in invisible areas is prohibited - make visible first"))
  16320. ;; Make the area visible
  16321. (save-excursion
  16322. (if invisible-before-point
  16323. (goto-char (previous-single-char-property-change
  16324. (point) 'invisible)))
  16325. (org-cycle))
  16326. (cond
  16327. ((eq org-catch-invisible-edits 'show)
  16328. ;; That's it, we do the edit after showing
  16329. (message
  16330. "Unfolding invisible region around point before editing")
  16331. (sit-for 1))
  16332. ((and (eq org-catch-invisible-edits 'smart)
  16333. border-and-ok-direction)
  16334. (message "Unfolding invisible region around point before editing"))
  16335. (t
  16336. ;; Don't do the edit, make the user repeat it in full visibility
  16337. (error "Edit in invisible region aborted, repeat to confirm with text visible")))))))
  16338. (defun org-fix-tags-on-the-fly ()
  16339. (when (and (equal (char-after (point-at-bol)) ?*)
  16340. (org-at-heading-p))
  16341. (org-align-tags-here org-tags-column)))
  16342. (defun org-delete-backward-char (N)
  16343. "Like `delete-backward-char', insert whitespace at field end in tables.
  16344. When deleting backwards, in tables this function will insert whitespace in
  16345. front of the next \"|\" separator, to keep the table aligned. The table will
  16346. still be marked for re-alignment if the field did fill the entire column,
  16347. because, in this case the deletion might narrow the column."
  16348. (interactive "p")
  16349. (org-check-before-invisible-edit 'delete-backward)
  16350. (if (and (org-table-p)
  16351. (eq N 1)
  16352. (string-match "|" (buffer-substring (point-at-bol) (point)))
  16353. (looking-at ".*?|"))
  16354. (let ((pos (point))
  16355. (noalign (looking-at "[^|\n\r]* |"))
  16356. (c org-table-may-need-update))
  16357. (backward-delete-char N)
  16358. (if (not overwrite-mode)
  16359. (progn
  16360. (skip-chars-forward "^|")
  16361. (insert " ")
  16362. (goto-char (1- pos))))
  16363. ;; noalign: if there were two spaces at the end, this field
  16364. ;; does not determine the width of the column.
  16365. (if noalign (setq org-table-may-need-update c)))
  16366. (backward-delete-char N)
  16367. (org-fix-tags-on-the-fly)))
  16368. (defun org-delete-char (N)
  16369. "Like `delete-char', but insert whitespace at field end in tables.
  16370. When deleting characters, in tables this function will insert whitespace in
  16371. front of the next \"|\" separator, to keep the table aligned. The table will
  16372. still be marked for re-alignment if the field did fill the entire column,
  16373. because, in this case the deletion might narrow the column."
  16374. (interactive "p")
  16375. (org-check-before-invisible-edit 'delete)
  16376. (if (and (org-table-p)
  16377. (not (bolp))
  16378. (not (= (char-after) ?|))
  16379. (eq N 1))
  16380. (if (looking-at ".*?|")
  16381. (let ((pos (point))
  16382. (noalign (looking-at "[^|\n\r]* |"))
  16383. (c org-table-may-need-update))
  16384. (replace-match (concat
  16385. (substring (match-string 0) 1 -1)
  16386. " |"))
  16387. (goto-char pos)
  16388. ;; noalign: if there were two spaces at the end, this field
  16389. ;; does not determine the width of the column.
  16390. (if noalign (setq org-table-may-need-update c)))
  16391. (delete-char N))
  16392. (delete-char N)
  16393. (org-fix-tags-on-the-fly)))
  16394. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  16395. (put 'org-self-insert-command 'delete-selection t)
  16396. (put 'orgtbl-self-insert-command 'delete-selection t)
  16397. (put 'org-delete-char 'delete-selection 'supersede)
  16398. (put 'org-delete-backward-char 'delete-selection 'supersede)
  16399. (put 'org-yank 'delete-selection 'yank)
  16400. ;; Make `flyspell-mode' delay after some commands
  16401. (put 'org-self-insert-command 'flyspell-delayed t)
  16402. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  16403. (put 'org-delete-char 'flyspell-delayed t)
  16404. (put 'org-delete-backward-char 'flyspell-delayed t)
  16405. ;; Make pabbrev-mode expand after org-mode commands
  16406. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  16407. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  16408. ;; How to do this: Measure non-white length of current string
  16409. ;; If equal to column width, we should realign.
  16410. (defun org-remap (map &rest commands)
  16411. "In MAP, remap the functions given in COMMANDS.
  16412. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  16413. (let (new old)
  16414. (while commands
  16415. (setq old (pop commands) new (pop commands))
  16416. (if (fboundp 'command-remapping)
  16417. (org-defkey map (vector 'remap old) new)
  16418. (substitute-key-definition old new map global-map)))))
  16419. (when (eq org-enable-table-editor 'optimized)
  16420. ;; If the user wants maximum table support, we need to hijack
  16421. ;; some standard editing functions
  16422. (org-remap org-mode-map
  16423. 'self-insert-command 'org-self-insert-command
  16424. 'delete-char 'org-delete-char
  16425. 'delete-backward-char 'org-delete-backward-char)
  16426. (org-defkey org-mode-map "|" 'org-force-self-insert))
  16427. (defvar org-ctrl-c-ctrl-c-hook nil
  16428. "Hook for functions attaching themselves to `C-c C-c'.
  16429. This can be used to add additional functionality to the C-c C-c
  16430. key which executes context-dependent commands. This hook is run
  16431. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  16432. run after the last test.
  16433. Each function will be called with no arguments. The function
  16434. must check if the context is appropriate for it to act. If yes,
  16435. it should do its thing and then return a non-nil value. If the
  16436. context is wrong, just do nothing and return nil.")
  16437. (defvar org-ctrl-c-ctrl-c-final-hook nil
  16438. "Hook for functions attaching themselves to `C-c C-c'.
  16439. This can be used to add additional functionality to the C-c C-c
  16440. key which executes context-dependent commands. This hook is run
  16441. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  16442. before the first test.
  16443. Each function will be called with no arguments. The function
  16444. must check if the context is appropriate for it to act. If yes,
  16445. it should do its thing and then return a non-nil value. If the
  16446. context is wrong, just do nothing and return nil.")
  16447. (defvar org-tab-first-hook nil
  16448. "Hook for functions to attach themselves to TAB.
  16449. See `org-ctrl-c-ctrl-c-hook' for more information.
  16450. This hook runs as the first action when TAB is pressed, even before
  16451. `org-cycle' messes around with the `outline-regexp' to cater for
  16452. inline tasks and plain list item folding.
  16453. If any function in this hook returns t, any other actions that
  16454. would have been caused by TAB (such as table field motion or visibility
  16455. cycling) will not occur.")
  16456. (defvar org-tab-after-check-for-table-hook nil
  16457. "Hook for functions to attach themselves to TAB.
  16458. See `org-ctrl-c-ctrl-c-hook' for more information.
  16459. This hook runs after it has been established that the cursor is not in a
  16460. table, but before checking if the cursor is in a headline or if global cycling
  16461. should be done.
  16462. If any function in this hook returns t, not other actions like visibility
  16463. cycling will be done.")
  16464. (defvar org-tab-after-check-for-cycling-hook nil
  16465. "Hook for functions to attach themselves to TAB.
  16466. See `org-ctrl-c-ctrl-c-hook' for more information.
  16467. This hook runs after it has been established that not table field motion and
  16468. not visibility should be done because of current context. This is probably
  16469. the place where a package like yasnippets can hook in.")
  16470. (defvar org-tab-before-tab-emulation-hook nil
  16471. "Hook for functions to attach themselves to TAB.
  16472. See `org-ctrl-c-ctrl-c-hook' for more information.
  16473. This hook runs after every other options for TAB have been exhausted, but
  16474. before indentation and \t insertion takes place.")
  16475. (defvar org-metaleft-hook nil
  16476. "Hook for functions attaching themselves to `M-left'.
  16477. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16478. (defvar org-metaright-hook nil
  16479. "Hook for functions attaching themselves to `M-right'.
  16480. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16481. (defvar org-metaup-hook nil
  16482. "Hook for functions attaching themselves to `M-up'.
  16483. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16484. (defvar org-metadown-hook nil
  16485. "Hook for functions attaching themselves to `M-down'.
  16486. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16487. (defvar org-shiftmetaleft-hook nil
  16488. "Hook for functions attaching themselves to `M-S-left'.
  16489. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16490. (defvar org-shiftmetaright-hook nil
  16491. "Hook for functions attaching themselves to `M-S-right'.
  16492. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16493. (defvar org-shiftmetaup-hook nil
  16494. "Hook for functions attaching themselves to `M-S-up'.
  16495. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16496. (defvar org-shiftmetadown-hook nil
  16497. "Hook for functions attaching themselves to `M-S-down'.
  16498. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16499. (defvar org-metareturn-hook nil
  16500. "Hook for functions attaching themselves to `M-RET'.
  16501. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16502. (defvar org-shiftup-hook nil
  16503. "Hook for functions attaching themselves to `S-up'.
  16504. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16505. (defvar org-shiftup-final-hook nil
  16506. "Hook for functions attaching themselves to `S-up'.
  16507. This one runs after all other options except shift-select have been excluded.
  16508. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16509. (defvar org-shiftdown-hook nil
  16510. "Hook for functions attaching themselves to `S-down'.
  16511. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16512. (defvar org-shiftdown-final-hook nil
  16513. "Hook for functions attaching themselves to `S-down'.
  16514. This one runs after all other options except shift-select have been excluded.
  16515. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16516. (defvar org-shiftleft-hook nil
  16517. "Hook for functions attaching themselves to `S-left'.
  16518. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16519. (defvar org-shiftleft-final-hook nil
  16520. "Hook for functions attaching themselves to `S-left'.
  16521. This one runs after all other options except shift-select have been excluded.
  16522. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16523. (defvar org-shiftright-hook nil
  16524. "Hook for functions attaching themselves to `S-right'.
  16525. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16526. (defvar org-shiftright-final-hook nil
  16527. "Hook for functions attaching themselves to `S-right'.
  16528. This one runs after all other options except shift-select have been excluded.
  16529. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16530. (defun org-modifier-cursor-error ()
  16531. "Throw an error, a modified cursor command was applied in wrong context."
  16532. (error "This command is active in special context like tables, headlines or items"))
  16533. (defun org-shiftselect-error ()
  16534. "Throw an error because Shift-Cursor command was applied in wrong context."
  16535. (if (and (boundp 'shift-select-mode) shift-select-mode)
  16536. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  16537. (error "This command works only in special context like headlines or timestamps")))
  16538. (defun org-call-for-shift-select (cmd)
  16539. (let ((this-command-keys-shift-translated t))
  16540. (call-interactively cmd)))
  16541. (defun org-shifttab (&optional arg)
  16542. "Global visibility cycling or move to previous table field.
  16543. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  16544. on context.
  16545. See the individual commands for more information."
  16546. (interactive "P")
  16547. (cond
  16548. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  16549. ((integerp arg)
  16550. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  16551. (message "Content view to level: %d" arg)
  16552. (org-content (prefix-numeric-value arg2))
  16553. (setq org-cycle-global-status 'overview)))
  16554. (t (call-interactively 'org-global-cycle))))
  16555. (defun org-shiftmetaleft ()
  16556. "Promote subtree or delete table column.
  16557. Calls `org-promote-subtree', `org-outdent-item-tree', or
  16558. `org-table-delete-column', depending on context. See the
  16559. individual commands for more information."
  16560. (interactive)
  16561. (cond
  16562. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  16563. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  16564. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  16565. ((if (not (org-region-active-p)) (org-at-item-p)
  16566. (save-excursion (goto-char (region-beginning))
  16567. (org-at-item-p)))
  16568. (call-interactively 'org-outdent-item-tree))
  16569. (t (org-modifier-cursor-error))))
  16570. (defun org-shiftmetaright ()
  16571. "Demote subtree or insert table column.
  16572. Calls `org-demote-subtree', `org-indent-item-tree', or
  16573. `org-table-insert-column', depending on context. See the
  16574. individual commands for more information."
  16575. (interactive)
  16576. (cond
  16577. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  16578. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  16579. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  16580. ((if (not (org-region-active-p)) (org-at-item-p)
  16581. (save-excursion (goto-char (region-beginning))
  16582. (org-at-item-p)))
  16583. (call-interactively 'org-indent-item-tree))
  16584. (t (org-modifier-cursor-error))))
  16585. (defun org-shiftmetaup (&optional arg)
  16586. "Move subtree up or kill table row.
  16587. Calls `org-move-subtree-up' or `org-table-kill-row' or
  16588. `org-move-item-up' depending on context. See the individual commands
  16589. for more information."
  16590. (interactive "P")
  16591. (cond
  16592. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  16593. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  16594. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  16595. ((org-at-item-p) (call-interactively 'org-move-item-up))
  16596. (t (org-modifier-cursor-error))))
  16597. (defun org-shiftmetadown (&optional arg)
  16598. "Move subtree down or insert table row.
  16599. Calls `org-move-subtree-down' or `org-table-insert-row' or
  16600. `org-move-item-down', depending on context. See the individual
  16601. commands for more information."
  16602. (interactive "P")
  16603. (cond
  16604. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  16605. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  16606. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  16607. ((org-at-item-p) (call-interactively 'org-move-item-down))
  16608. (t (org-modifier-cursor-error))))
  16609. (defsubst org-hidden-tree-error ()
  16610. (error
  16611. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  16612. (defun org-metaleft (&optional arg)
  16613. "Promote heading or move table column to left.
  16614. Calls `org-do-promote' or `org-table-move-column', depending on context.
  16615. With no specific context, calls the Emacs default `backward-word'.
  16616. See the individual commands for more information."
  16617. (interactive "P")
  16618. (cond
  16619. ((run-hook-with-args-until-success 'org-metaleft-hook))
  16620. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  16621. ((org-with-limited-levels
  16622. (or (org-at-heading-p)
  16623. (and (org-region-active-p)
  16624. (save-excursion
  16625. (goto-char (region-beginning))
  16626. (org-at-heading-p)))))
  16627. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  16628. (call-interactively 'org-do-promote))
  16629. ;; At an inline task.
  16630. ((org-at-heading-p)
  16631. (call-interactively 'org-inlinetask-promote))
  16632. ((or (org-at-item-p)
  16633. (and (org-region-active-p)
  16634. (save-excursion
  16635. (goto-char (region-beginning))
  16636. (org-at-item-p))))
  16637. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  16638. (call-interactively 'org-outdent-item))
  16639. (t (call-interactively 'backward-word))))
  16640. (defun org-metaright (&optional arg)
  16641. "Demote a subtree, a list item or move table column to right.
  16642. In front of a drawer or a block keyword, indent it correctly.
  16643. With no specific context, calls the Emacs default `forward-word'.
  16644. See the individual commands for more information."
  16645. (interactive "P")
  16646. (cond
  16647. ((run-hook-with-args-until-success 'org-metaright-hook))
  16648. ((org-at-table-p) (call-interactively 'org-table-move-column))
  16649. ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
  16650. ((org-at-block-p) (call-interactively 'org-indent-block))
  16651. ((org-with-limited-levels
  16652. (or (org-at-heading-p)
  16653. (and (org-region-active-p)
  16654. (save-excursion
  16655. (goto-char (region-beginning))
  16656. (org-at-heading-p)))))
  16657. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  16658. (call-interactively 'org-do-demote))
  16659. ;; At an inline task.
  16660. ((org-at-heading-p)
  16661. (call-interactively 'org-inlinetask-demote))
  16662. ((or (org-at-item-p)
  16663. (and (org-region-active-p)
  16664. (save-excursion
  16665. (goto-char (region-beginning))
  16666. (org-at-item-p))))
  16667. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  16668. (call-interactively 'org-indent-item))
  16669. (t (call-interactively 'forward-word))))
  16670. (defun org-check-for-hidden (what)
  16671. "Check if there are hidden headlines/items in the current visual line.
  16672. WHAT can be either `headlines' or `items'. If the current line is
  16673. an outline or item heading and it has a folded subtree below it,
  16674. this function returns t, nil otherwise."
  16675. (let ((re (cond
  16676. ((eq what 'headlines) org-outline-regexp-bol)
  16677. ((eq what 'items) (org-item-beginning-re))
  16678. (t (error "This should not happen"))))
  16679. beg end)
  16680. (save-excursion
  16681. (catch 'exit
  16682. (unless (org-region-active-p)
  16683. (setq beg (point-at-bol))
  16684. (beginning-of-line 2)
  16685. (while (and (not (eobp)) ;; this is like `next-line'
  16686. (get-char-property (1- (point)) 'invisible))
  16687. (beginning-of-line 2))
  16688. (setq end (point))
  16689. (goto-char beg)
  16690. (goto-char (point-at-eol))
  16691. (setq end (max end (point)))
  16692. (while (re-search-forward re end t)
  16693. (if (get-char-property (match-beginning 0) 'invisible)
  16694. (throw 'exit t))))
  16695. nil))))
  16696. (defun org-metaup (&optional arg)
  16697. "Move subtree up or move table row up.
  16698. Calls `org-move-subtree-up' or `org-table-move-row' or
  16699. `org-move-item-up', depending on context. See the individual commands
  16700. for more information."
  16701. (interactive "P")
  16702. (cond
  16703. ((run-hook-with-args-until-success 'org-metaup-hook))
  16704. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  16705. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  16706. ((org-at-item-p) (call-interactively 'org-move-item-up))
  16707. (t (transpose-lines 1) (beginning-of-line -1))))
  16708. (defun org-metadown (&optional arg)
  16709. "Move subtree down or move table row down.
  16710. Calls `org-move-subtree-down' or `org-table-move-row' or
  16711. `org-move-item-down', depending on context. See the individual
  16712. commands for more information."
  16713. (interactive "P")
  16714. (cond
  16715. ((run-hook-with-args-until-success 'org-metadown-hook))
  16716. ((org-at-table-p) (call-interactively 'org-table-move-row))
  16717. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  16718. ((org-at-item-p) (call-interactively 'org-move-item-down))
  16719. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  16720. (defun org-shiftup (&optional arg)
  16721. "Increase item in timestamp or increase priority of current headline.
  16722. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  16723. depending on context. See the individual commands for more information."
  16724. (interactive "P")
  16725. (cond
  16726. ((run-hook-with-args-until-success 'org-shiftup-hook))
  16727. ((and org-support-shift-select (org-region-active-p))
  16728. (org-call-for-shift-select 'previous-line))
  16729. ((org-at-timestamp-p t)
  16730. (call-interactively (if org-edit-timestamp-down-means-later
  16731. 'org-timestamp-down 'org-timestamp-up)))
  16732. ((and (not (eq org-support-shift-select 'always))
  16733. org-enable-priority-commands
  16734. (org-at-heading-p))
  16735. (call-interactively 'org-priority-up))
  16736. ((and (not org-support-shift-select) (org-at-item-p))
  16737. (call-interactively 'org-previous-item))
  16738. ((org-clocktable-try-shift 'up arg))
  16739. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  16740. (org-support-shift-select
  16741. (org-call-for-shift-select 'previous-line))
  16742. (t (org-shiftselect-error))))
  16743. (defun org-shiftdown (&optional arg)
  16744. "Decrease item in timestamp or decrease priority of current headline.
  16745. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  16746. depending on context. See the individual commands for more information."
  16747. (interactive "P")
  16748. (cond
  16749. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  16750. ((and org-support-shift-select (org-region-active-p))
  16751. (org-call-for-shift-select 'next-line))
  16752. ((org-at-timestamp-p t)
  16753. (call-interactively (if org-edit-timestamp-down-means-later
  16754. 'org-timestamp-up 'org-timestamp-down)))
  16755. ((and (not (eq org-support-shift-select 'always))
  16756. org-enable-priority-commands
  16757. (org-at-heading-p))
  16758. (call-interactively 'org-priority-down))
  16759. ((and (not org-support-shift-select) (org-at-item-p))
  16760. (call-interactively 'org-next-item))
  16761. ((org-clocktable-try-shift 'down arg))
  16762. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  16763. (org-support-shift-select
  16764. (org-call-for-shift-select 'next-line))
  16765. (t (org-shiftselect-error))))
  16766. (defun org-shiftright (&optional arg)
  16767. "Cycle the thing at point or in the current line, depending on context.
  16768. Depending on context, this does one of the following:
  16769. - switch a timestamp at point one day into the future
  16770. - on a headline, switch to the next TODO keyword.
  16771. - on an item, switch entire list to the next bullet type
  16772. - on a property line, switch to the next allowed value
  16773. - on a clocktable definition line, move time block into the future"
  16774. (interactive "P")
  16775. (cond
  16776. ((run-hook-with-args-until-success 'org-shiftright-hook))
  16777. ((and org-support-shift-select (org-region-active-p))
  16778. (org-call-for-shift-select 'forward-char))
  16779. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  16780. ((and (not (eq org-support-shift-select 'always))
  16781. (org-at-heading-p))
  16782. (let ((org-inhibit-logging
  16783. (not org-treat-S-cursor-todo-selection-as-state-change))
  16784. (org-inhibit-blocking
  16785. (not org-treat-S-cursor-todo-selection-as-state-change)))
  16786. (org-call-with-arg 'org-todo 'right)))
  16787. ((or (and org-support-shift-select
  16788. (not (eq org-support-shift-select 'always))
  16789. (org-at-item-bullet-p))
  16790. (and (not org-support-shift-select) (org-at-item-p)))
  16791. (org-call-with-arg 'org-cycle-list-bullet nil))
  16792. ((and (not (eq org-support-shift-select 'always))
  16793. (org-at-property-p))
  16794. (call-interactively 'org-property-next-allowed-value))
  16795. ((org-clocktable-try-shift 'right arg))
  16796. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  16797. (org-support-shift-select
  16798. (org-call-for-shift-select 'forward-char))
  16799. (t (org-shiftselect-error))))
  16800. (defun org-shiftleft (&optional arg)
  16801. "Cycle the thing at point or in the current line, depending on context.
  16802. Depending on context, this does one of the following:
  16803. - switch a timestamp at point one day into the past
  16804. - on a headline, switch to the previous TODO keyword.
  16805. - on an item, switch entire list to the previous bullet type
  16806. - on a property line, switch to the previous allowed value
  16807. - on a clocktable definition line, move time block into the past"
  16808. (interactive "P")
  16809. (cond
  16810. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  16811. ((and org-support-shift-select (org-region-active-p))
  16812. (org-call-for-shift-select 'backward-char))
  16813. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  16814. ((and (not (eq org-support-shift-select 'always))
  16815. (org-at-heading-p))
  16816. (let ((org-inhibit-logging
  16817. (not org-treat-S-cursor-todo-selection-as-state-change))
  16818. (org-inhibit-blocking
  16819. (not org-treat-S-cursor-todo-selection-as-state-change)))
  16820. (org-call-with-arg 'org-todo 'left)))
  16821. ((or (and org-support-shift-select
  16822. (not (eq org-support-shift-select 'always))
  16823. (org-at-item-bullet-p))
  16824. (and (not org-support-shift-select) (org-at-item-p)))
  16825. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  16826. ((and (not (eq org-support-shift-select 'always))
  16827. (org-at-property-p))
  16828. (call-interactively 'org-property-previous-allowed-value))
  16829. ((org-clocktable-try-shift 'left arg))
  16830. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  16831. (org-support-shift-select
  16832. (org-call-for-shift-select 'backward-char))
  16833. (t (org-shiftselect-error))))
  16834. (defun org-shiftcontrolright ()
  16835. "Switch to next TODO set."
  16836. (interactive)
  16837. (cond
  16838. ((and org-support-shift-select (org-region-active-p))
  16839. (org-call-for-shift-select 'forward-word))
  16840. ((and (not (eq org-support-shift-select 'always))
  16841. (org-at-heading-p))
  16842. (org-call-with-arg 'org-todo 'nextset))
  16843. (org-support-shift-select
  16844. (org-call-for-shift-select 'forward-word))
  16845. (t (org-shiftselect-error))))
  16846. (defun org-shiftcontrolleft ()
  16847. "Switch to previous TODO set."
  16848. (interactive)
  16849. (cond
  16850. ((and org-support-shift-select (org-region-active-p))
  16851. (org-call-for-shift-select 'backward-word))
  16852. ((and (not (eq org-support-shift-select 'always))
  16853. (org-at-heading-p))
  16854. (org-call-with-arg 'org-todo 'previousset))
  16855. (org-support-shift-select
  16856. (org-call-for-shift-select 'backward-word))
  16857. (t (org-shiftselect-error))))
  16858. (defun org-shiftcontrolup ()
  16859. "Change timestamps synchronously up in CLOCK log lines."
  16860. (interactive)
  16861. (cond ((and (not org-support-shift-select)
  16862. (org-at-clock-log-p)
  16863. (org-at-timestamp-p t))
  16864. (org-clock-timestamps-up))
  16865. (t (org-shiftselect-error))))
  16866. (defun org-shiftcontroldown ()
  16867. "Change timestamps synchronously down in CLOCK log lines."
  16868. (interactive)
  16869. (cond ((and (not org-support-shift-select)
  16870. (org-at-clock-log-p)
  16871. (org-at-timestamp-p t))
  16872. (org-clock-timestamps-down))
  16873. (t (org-shiftselect-error))))
  16874. (defun org-ctrl-c-ret ()
  16875. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  16876. (interactive)
  16877. (cond
  16878. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  16879. (t (call-interactively 'org-insert-heading))))
  16880. (defun org-find-visible ()
  16881. (let ((s (point)))
  16882. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  16883. (get-char-property s 'invisible)))
  16884. s))
  16885. (defun org-find-invisible ()
  16886. (let ((s (point)))
  16887. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  16888. (not (get-char-property s 'invisible))))
  16889. s))
  16890. (defun org-copy-visible (beg end)
  16891. "Copy the visible parts of the region."
  16892. (interactive "r")
  16893. (let (snippets s)
  16894. (save-excursion
  16895. (save-restriction
  16896. (narrow-to-region beg end)
  16897. (setq s (goto-char (point-min)))
  16898. (while (not (= (point) (point-max)))
  16899. (goto-char (org-find-invisible))
  16900. (push (buffer-substring s (point)) snippets)
  16901. (setq s (goto-char (org-find-visible))))))
  16902. (kill-new (apply 'concat (nreverse snippets)))))
  16903. (defun org-copy-special ()
  16904. "Copy region in table or copy current subtree.
  16905. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  16906. See the individual commands for more information."
  16907. (interactive)
  16908. (call-interactively
  16909. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  16910. (defun org-cut-special ()
  16911. "Cut region in table or cut current subtree.
  16912. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  16913. See the individual commands for more information."
  16914. (interactive)
  16915. (call-interactively
  16916. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  16917. (defun org-paste-special (arg)
  16918. "Paste rectangular region into table, or past subtree relative to level.
  16919. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  16920. See the individual commands for more information."
  16921. (interactive "P")
  16922. (if (org-at-table-p)
  16923. (org-table-paste-rectangle)
  16924. (org-paste-subtree arg)))
  16925. (defun org-edit-special (&optional arg)
  16926. "Call a special editor for the stuff at point.
  16927. When at a table, call the formula editor with `org-table-edit-formulas'.
  16928. When at the first line of an src example, call `org-edit-src-code'.
  16929. When in an #+include line, visit the include file. Otherwise call
  16930. `ffap' to visit the file at point."
  16931. (interactive)
  16932. ;; possibly prep session before editing source
  16933. (when arg
  16934. (let* ((info (org-babel-get-src-block-info))
  16935. (lang (nth 0 info))
  16936. (params (nth 2 info))
  16937. (session (cdr (assoc :session params))))
  16938. (when (and info session) ;; we are in a source-code block with a session
  16939. (funcall
  16940. (intern (concat "org-babel-prep-session:" lang)) session params))))
  16941. (cond ;; proceed with `org-edit-special'
  16942. ((save-excursion
  16943. (beginning-of-line 1)
  16944. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  16945. (find-file (org-trim (match-string 1))))
  16946. ((org-edit-src-code))
  16947. ((org-edit-fixed-width-region))
  16948. ((org-at-table.el-p)
  16949. (org-edit-src-code))
  16950. ((or (org-at-table-p)
  16951. (save-excursion
  16952. (beginning-of-line 1)
  16953. (looking-at "[ \t]*#\\+TBLFM:")))
  16954. (call-interactively 'org-table-edit-formulas))
  16955. (t (call-interactively 'ffap))))
  16956. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  16957. (defun org-ctrl-c-ctrl-c (&optional arg)
  16958. "Set tags in headline, or update according to changed information at point.
  16959. This command does many different things, depending on context:
  16960. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  16961. this is what we do.
  16962. - If the cursor is on a statistics cookie, update it.
  16963. - If the cursor is in a headline, prompt for tags and insert them
  16964. into the current line, aligned to `org-tags-column'. When called
  16965. with prefix arg, realign all tags in the current buffer.
  16966. - If the cursor is in one of the special #+KEYWORD lines, this
  16967. triggers scanning the buffer for these lines and updating the
  16968. information.
  16969. - If the cursor is inside a table, realign the table. This command
  16970. works even if the automatic table editor has been turned off.
  16971. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  16972. the entire table.
  16973. - If the cursor is at a footnote reference or definition, jump to
  16974. the corresponding definition or references, respectively.
  16975. - If the cursor is a the beginning of a dynamic block, update it.
  16976. - If the current buffer is a capture buffer, close note and file it.
  16977. - If the cursor is on a <<<target>>>, update radio targets and
  16978. corresponding links in this buffer.
  16979. - If the cursor is on a numbered item in a plain list, renumber the
  16980. ordered list.
  16981. - If the cursor is on a checkbox, toggle it.
  16982. - If the cursor is on a code block, evaluate it. The variable
  16983. `org-confirm-babel-evaluate' can be used to control prompting
  16984. before code block evaluation, by default every code block
  16985. evaluation requires confirmation. Code block evaluation can be
  16986. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  16987. (interactive "P")
  16988. (let ((org-enable-table-editor t))
  16989. (cond
  16990. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  16991. org-occur-highlights
  16992. org-latex-fragment-image-overlays)
  16993. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  16994. (org-remove-occur-highlights)
  16995. (org-remove-latex-fragment-image-overlays)
  16996. (message "Temporary highlights/overlays removed from current buffer"))
  16997. ((and (local-variable-p 'org-finish-function (current-buffer))
  16998. (fboundp org-finish-function))
  16999. (funcall org-finish-function))
  17000. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  17001. ((org-in-regexp org-ts-regexp-both)
  17002. (org-timestamp-change 0 'day))
  17003. ((or (looking-at org-property-start-re)
  17004. (org-at-property-p))
  17005. (call-interactively 'org-property-action))
  17006. ((org-at-target-p) (call-interactively 'org-update-radio-target-regexp))
  17007. ((and (org-in-regexp "\\[\\([0-9]*%\\|[0-9]*/[0-9]*\\)\\]")
  17008. (or (org-at-heading-p) (org-at-item-p)))
  17009. (call-interactively 'org-update-statistics-cookies))
  17010. ((org-at-heading-p) (call-interactively 'org-set-tags))
  17011. ((org-at-table.el-p)
  17012. (message "Use C-c ' to edit table.el tables"))
  17013. ((org-at-table-p)
  17014. (org-table-maybe-eval-formula)
  17015. (if arg
  17016. (call-interactively 'org-table-recalculate)
  17017. (org-table-maybe-recalculate-line))
  17018. (call-interactively 'org-table-align)
  17019. (orgtbl-send-table 'maybe))
  17020. ((or (org-footnote-at-reference-p)
  17021. (org-footnote-at-definition-p))
  17022. (call-interactively 'org-footnote-action))
  17023. ((org-at-item-checkbox-p)
  17024. ;; Cursor at a checkbox: repair list and update checkboxes. Send
  17025. ;; list only if at top item.
  17026. (let* ((cbox (match-string 1))
  17027. (struct (org-list-struct))
  17028. (old-struct (copy-tree struct))
  17029. (parents (org-list-parents-alist struct))
  17030. (orderedp (org-entry-get nil "ORDERED"))
  17031. (firstp (= (org-list-get-top-point struct) (point-at-bol)))
  17032. block-item)
  17033. ;; Use a light version of `org-toggle-checkbox' to avoid
  17034. ;; computing list structure twice.
  17035. (let ((new-box (cond
  17036. ((equal arg '(16)) "[-]")
  17037. ((equal arg '(4)) nil)
  17038. ((equal "[X]" cbox) "[ ]")
  17039. (t "[X]"))))
  17040. (if (and firstp arg)
  17041. ;; If at first item of sub-list, remove check-box from
  17042. ;; every item at the same level.
  17043. (mapc
  17044. (lambda (pos) (org-list-set-checkbox pos struct new-box))
  17045. (org-list-get-all-items
  17046. (point-at-bol) struct (org-list-prevs-alist struct)))
  17047. (org-list-set-checkbox (point-at-bol) struct new-box)))
  17048. ;; Replicate `org-list-write-struct', while grabbing a return
  17049. ;; value from `org-list-struct-fix-box'.
  17050. (org-list-struct-fix-ind struct parents 2)
  17051. (org-list-struct-fix-item-end struct)
  17052. (let ((prevs (org-list-prevs-alist struct)))
  17053. (org-list-struct-fix-bul struct prevs)
  17054. (org-list-struct-fix-ind struct parents)
  17055. (setq block-item
  17056. (org-list-struct-fix-box struct parents prevs orderedp)))
  17057. (org-list-struct-apply-struct struct old-struct)
  17058. (org-update-checkbox-count-maybe)
  17059. (when block-item
  17060. (message
  17061. "Checkboxes were removed due to unchecked box at line %d"
  17062. (org-current-line block-item)))
  17063. (when firstp (org-list-send-list 'maybe))))
  17064. ((org-at-item-p)
  17065. ;; Cursor at an item: repair list. Do checkbox related actions
  17066. ;; only if function was called with an argument. Send list only
  17067. ;; if at top item.
  17068. (let* ((struct (org-list-struct))
  17069. (firstp (= (org-list-get-top-point struct) (point-at-bol)))
  17070. old-struct)
  17071. (when arg
  17072. (setq old-struct (copy-tree struct))
  17073. (if firstp
  17074. ;; If at first item of sub-list, add check-box to every
  17075. ;; item at the same level.
  17076. (mapc
  17077. (lambda (pos)
  17078. (unless (org-list-get-checkbox pos struct)
  17079. (org-list-set-checkbox pos struct "[ ]")))
  17080. (org-list-get-all-items
  17081. (point-at-bol) struct (org-list-prevs-alist struct)))
  17082. (org-list-set-checkbox (point-at-bol) struct "[ ]")))
  17083. (org-list-write-struct
  17084. struct (org-list-parents-alist struct) old-struct)
  17085. (when arg (org-update-checkbox-count-maybe))
  17086. (when firstp (org-list-send-list 'maybe))))
  17087. ((save-excursion (beginning-of-line 1) (looking-at org-dblock-start-re))
  17088. ;; Dynamic block
  17089. (beginning-of-line 1)
  17090. (save-excursion (org-update-dblock)))
  17091. ((save-excursion
  17092. (beginning-of-line 1)
  17093. (looking-at "[ \t]*#\\+\\([A-Z]+\\)"))
  17094. (cond
  17095. ((equal (match-string 1) "TBLFM")
  17096. ;; Recalculate the table before this line
  17097. (save-excursion
  17098. (beginning-of-line 1)
  17099. (skip-chars-backward " \r\n\t")
  17100. (if (org-at-table-p)
  17101. (org-call-with-arg 'org-table-recalculate (or arg t)))))
  17102. (t
  17103. (let ((org-inhibit-startup-visibility-stuff t)
  17104. (org-startup-align-all-tables nil))
  17105. (when (boundp 'org-table-coordinate-overlays)
  17106. (mapc 'delete-overlay org-table-coordinate-overlays)
  17107. (setq org-table-coordinate-overlays nil))
  17108. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  17109. (message "Local setup has been refreshed"))))
  17110. ((org-clock-update-time-maybe))
  17111. (t
  17112. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  17113. (error "C-c C-c can do nothing useful at this location"))))))
  17114. (defun org-mode-restart ()
  17115. "Restart Org-mode, to scan again for special lines.
  17116. Also updates the keyword regular expressions."
  17117. (interactive)
  17118. (org-mode)
  17119. (message "Org-mode restarted"))
  17120. (defun org-kill-note-or-show-branches ()
  17121. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  17122. (interactive)
  17123. (if (not org-finish-function)
  17124. (progn
  17125. (hide-subtree)
  17126. (call-interactively 'show-branches))
  17127. (let ((org-note-abort t))
  17128. (funcall org-finish-function))))
  17129. (defun org-return (&optional indent)
  17130. "Goto next table row or insert a newline.
  17131. Calls `org-table-next-row' or `newline', depending on context.
  17132. See the individual commands for more information."
  17133. (interactive)
  17134. (cond
  17135. ((or (bobp) (org-in-src-block-p))
  17136. (if indent (newline-and-indent) (newline)))
  17137. ((org-at-table-p)
  17138. (org-table-justify-field-maybe)
  17139. (call-interactively 'org-table-next-row))
  17140. ;; when `newline-and-indent' is called within a list, make sure
  17141. ;; text moved stays inside the item.
  17142. ((and (org-in-item-p) indent)
  17143. (if (and (org-at-item-p) (>= (point) (match-end 0)))
  17144. (progn
  17145. (save-match-data (newline))
  17146. (org-indent-line-to (length (match-string 0))))
  17147. (let ((ind (org-get-indentation)))
  17148. (newline)
  17149. (if (org-looking-back org-list-end-re)
  17150. (org-indent-line-function)
  17151. (org-indent-line-to ind)))))
  17152. ((and org-return-follows-link
  17153. (let ((tprop (get-text-property (point) 'face)))
  17154. (or (eq tprop 'org-link)
  17155. (and (listp tprop) (memq 'org-link tprop)))))
  17156. (call-interactively 'org-open-at-point))
  17157. ((and (org-at-heading-p)
  17158. (looking-at
  17159. (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
  17160. (org-show-entry)
  17161. (end-of-line 1)
  17162. (newline))
  17163. (t (if indent (newline-and-indent) (newline)))))
  17164. (defun org-return-indent ()
  17165. "Goto next table row or insert a newline and indent.
  17166. Calls `org-table-next-row' or `newline-and-indent', depending on
  17167. context. See the individual commands for more information."
  17168. (interactive)
  17169. (org-return t))
  17170. (defun org-ctrl-c-star ()
  17171. "Compute table, or change heading status of lines.
  17172. Calls `org-table-recalculate' or `org-toggle-heading',
  17173. depending on context."
  17174. (interactive)
  17175. (cond
  17176. ((org-at-table-p)
  17177. (call-interactively 'org-table-recalculate))
  17178. (t
  17179. ;; Convert all lines in region to list items
  17180. (call-interactively 'org-toggle-heading))))
  17181. (defun org-ctrl-c-minus ()
  17182. "Insert separator line in table or modify bullet status of line.
  17183. Also turns a plain line or a region of lines into list items.
  17184. Calls `org-table-insert-hline', `org-toggle-item', or
  17185. `org-cycle-list-bullet', depending on context."
  17186. (interactive)
  17187. (cond
  17188. ((org-at-table-p)
  17189. (call-interactively 'org-table-insert-hline))
  17190. ((org-region-active-p)
  17191. (call-interactively 'org-toggle-item))
  17192. ((org-in-item-p)
  17193. (call-interactively 'org-cycle-list-bullet))
  17194. (t
  17195. (call-interactively 'org-toggle-item))))
  17196. (defun org-toggle-item (arg)
  17197. "Convert headings or normal lines to items, items to normal lines.
  17198. If there is no active region, only the current line is considered.
  17199. If the first non blank line in the region is an headline, convert
  17200. all headlines to items, shifting text accordingly.
  17201. If it is an item, convert all items to normal lines.
  17202. If it is normal text, change region into an item. With a prefix
  17203. argument ARG, change each line in region into an item."
  17204. (interactive "P")
  17205. (let ((shift-text
  17206. (function
  17207. ;; Shift text in current section to IND, from point to END.
  17208. ;; The function leaves point to END line.
  17209. (lambda (ind end)
  17210. (let ((min-i 1000) (end (copy-marker end)))
  17211. ;; First determine the minimum indentation (MIN-I) of
  17212. ;; the text.
  17213. (save-excursion
  17214. (catch 'exit
  17215. (while (< (point) end)
  17216. (let ((i (org-get-indentation)))
  17217. (cond
  17218. ;; Skip blank lines and inline tasks.
  17219. ((looking-at "^[ \t]*$"))
  17220. ((looking-at org-outline-regexp-bol))
  17221. ;; We can't find less than 0 indentation.
  17222. ((zerop i) (throw 'exit (setq min-i 0)))
  17223. ((< i min-i) (setq min-i i))))
  17224. (forward-line))))
  17225. ;; Then indent each line so that a line indented to
  17226. ;; MIN-I becomes indented to IND. Ignore blank lines
  17227. ;; and inline tasks in the process.
  17228. (let ((delta (- ind min-i)))
  17229. (while (< (point) end)
  17230. (unless (or (looking-at "^[ \t]*$")
  17231. (looking-at org-outline-regexp-bol))
  17232. (org-indent-line-to (+ (org-get-indentation) delta)))
  17233. (forward-line)))))))
  17234. (skip-blanks
  17235. (function
  17236. ;; Return beginning of first non-blank line, starting from
  17237. ;; line at POS.
  17238. (lambda (pos)
  17239. (save-excursion
  17240. (goto-char pos)
  17241. (skip-chars-forward " \r\t\n")
  17242. (point-at-bol)))))
  17243. beg end)
  17244. ;; Determine boundaries of changes.
  17245. (if (org-region-active-p)
  17246. (setq beg (funcall skip-blanks (region-beginning))
  17247. end (copy-marker (region-end)))
  17248. (setq beg (funcall skip-blanks (point-at-bol))
  17249. end (copy-marker (point-at-eol))))
  17250. ;; Depending on the starting line, choose an action on the text
  17251. ;; between BEG and END.
  17252. (org-with-limited-levels
  17253. (save-excursion
  17254. (goto-char beg)
  17255. (cond
  17256. ;; Case 1. Start at an item: de-itemize. Note that it only
  17257. ;; happens when a region is active: `org-ctrl-c-minus'
  17258. ;; would call `org-cycle-list-bullet' otherwise.
  17259. ((org-at-item-p)
  17260. (while (< (point) end)
  17261. (when (org-at-item-p)
  17262. (skip-chars-forward " \t")
  17263. (delete-region (point) (match-end 0)))
  17264. (forward-line)))
  17265. ;; Case 2. Start at an heading: convert to items.
  17266. ((org-at-heading-p)
  17267. (let* ((bul (org-list-bullet-string "-"))
  17268. (bul-len (length bul))
  17269. ;; Indentation of the first heading. It should be
  17270. ;; relative to the indentation of its parent, if any.
  17271. (start-ind (save-excursion
  17272. (cond
  17273. ((not org-adapt-indentation) 0)
  17274. ((not (outline-previous-heading)) 0)
  17275. (t (length (match-string 0))))))
  17276. ;; Level of first heading. Further headings will be
  17277. ;; compared to it to determine hierarchy in the list.
  17278. (ref-level (org-reduced-level (org-outline-level))))
  17279. (while (< (point) end)
  17280. (let* ((level (org-reduced-level (org-outline-level)))
  17281. (delta (max 0 (- level ref-level))))
  17282. ;; If current headline is less indented than the first
  17283. ;; one, set it as reference, in order to preserve
  17284. ;; subtrees.
  17285. (when (< level ref-level) (setq ref-level level))
  17286. (replace-match bul t t)
  17287. (org-indent-line-to (+ start-ind (* delta bul-len)))
  17288. ;; Ensure all text down to END (or SECTION-END) belongs
  17289. ;; to the newly created item.
  17290. (let ((section-end (save-excursion
  17291. (or (outline-next-heading) (point)))))
  17292. (forward-line)
  17293. (funcall shift-text
  17294. (+ start-ind (* (1+ delta) bul-len))
  17295. (min end section-end)))))))
  17296. ;; Case 3. Normal line with ARG: turn each non-item line into
  17297. ;; an item.
  17298. (arg
  17299. (while (< (point) end)
  17300. (unless (or (org-at-heading-p) (org-at-item-p))
  17301. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  17302. (replace-match
  17303. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  17304. (forward-line)))
  17305. ;; Case 4. Normal line without ARG: make the first line of
  17306. ;; region an item, and shift indentation of others
  17307. ;; lines to set them as item's body.
  17308. (t (let* ((bul (org-list-bullet-string "-"))
  17309. (bul-len (length bul))
  17310. (ref-ind (org-get-indentation)))
  17311. (skip-chars-forward " \t")
  17312. (insert bul)
  17313. (forward-line)
  17314. (while (< (point) end)
  17315. ;; Ensure that lines less indented than first one
  17316. ;; still get included in item body.
  17317. (funcall shift-text
  17318. (+ ref-ind bul-len)
  17319. (min end (save-excursion (or (outline-next-heading)
  17320. (point)))))
  17321. (forward-line)))))))))
  17322. (defun org-toggle-heading (&optional nstars)
  17323. "Convert headings to normal text, or items or text to headings.
  17324. If there is no active region, only the current line is considered.
  17325. If the first non blank line is an headline, remove the stars from
  17326. all headlines in the region.
  17327. If it is a plain list item, turn all plain list items into headings.
  17328. If it is a normal line, turn each and every normal line (i.e. not
  17329. an heading or an item) in the region into a heading.
  17330. When converting a line into a heading, the number of stars is chosen
  17331. such that the lines become children of the current entry. However,
  17332. when a prefix argument is given, its value determines the number of
  17333. stars to add."
  17334. (interactive "P")
  17335. (let ((skip-blanks
  17336. (function
  17337. ;; Return beginning of first non-blank line, starting from
  17338. ;; line at POS.
  17339. (lambda (pos)
  17340. (save-excursion
  17341. (goto-char pos)
  17342. (skip-chars-forward " \r\t\n")
  17343. (point-at-bol)))))
  17344. beg end)
  17345. ;; Determine boundaries of changes. If region ends at a bol, do
  17346. ;; not consider the last line to be in the region.
  17347. (if (org-region-active-p)
  17348. (setq beg (funcall skip-blanks (region-beginning))
  17349. end (copy-marker (save-excursion
  17350. (goto-char (region-end))
  17351. (if (bolp) (point) (point-at-eol)))))
  17352. (setq beg (funcall skip-blanks (point-at-bol))
  17353. end (copy-marker (point-at-eol))))
  17354. ;; Ensure inline tasks don't count as headings.
  17355. (org-with-limited-levels
  17356. (save-excursion
  17357. (goto-char beg)
  17358. (cond
  17359. ;; Case 1. Started at an heading: de-star headings.
  17360. ((org-at-heading-p)
  17361. (while (< (point) end)
  17362. (when (org-at-heading-p t)
  17363. (looking-at org-outline-regexp) (replace-match ""))
  17364. (forward-line)))
  17365. ;; Case 2. Started at an item: change items into headlines.
  17366. ;; One star will be added by `org-list-to-subtree'.
  17367. ((org-at-item-p)
  17368. (let* ((stars (make-string
  17369. (if nstars
  17370. ;; subtract the star that will be added again by
  17371. ;; `org-list-to-subtree'
  17372. (1- (prefix-numeric-value current-prefix-arg))
  17373. (or (org-current-level) 0))
  17374. ?*))
  17375. (add-stars
  17376. (cond (nstars "") ; stars from prefix only
  17377. ((equal stars "") "") ; before first heading
  17378. (org-odd-levels-only "*") ; inside heading, odd
  17379. (t "")))) ; inside heading, oddeven
  17380. (while (< (point) end)
  17381. (when (org-at-item-p)
  17382. ;; Pay attention to cases when region ends before list.
  17383. (let* ((struct (org-list-struct))
  17384. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  17385. (save-restriction
  17386. (narrow-to-region (point) list-end)
  17387. (insert
  17388. (org-list-to-subtree
  17389. (org-list-parse-list t)
  17390. '(:istart (concat stars add-stars (funcall get-stars depth))
  17391. :icount (concat stars add-stars (funcall get-stars depth))))))))
  17392. (forward-line))))
  17393. ;; Case 3. Started at normal text: make every line an heading,
  17394. ;; skipping headlines and items.
  17395. (t (let* ((stars (make-string
  17396. (if nstars
  17397. (prefix-numeric-value current-prefix-arg)
  17398. (or (org-current-level) 0))
  17399. ?*))
  17400. (add-stars
  17401. (cond (nstars "") ; stars from prefix only
  17402. ((equal stars "") "*") ; before first heading
  17403. (org-odd-levels-only "**") ; inside heading, odd
  17404. (t "*"))) ; inside heading, oddeven
  17405. (rpl (concat stars add-stars " ")))
  17406. (while (< (point) end)
  17407. (when (and (not (org-at-heading-p)) (not (org-at-item-p))
  17408. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  17409. (replace-match (concat rpl (match-string 2))))
  17410. (forward-line)))))))))
  17411. (defun org-meta-return (&optional arg)
  17412. "Insert a new heading or wrap a region in a table.
  17413. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  17414. See the individual commands for more information."
  17415. (interactive "P")
  17416. (cond
  17417. ((run-hook-with-args-until-success 'org-metareturn-hook))
  17418. ((or (org-at-drawer-p) (org-at-property-p))
  17419. (newline-and-indent))
  17420. ((org-at-table-p)
  17421. (call-interactively 'org-table-wrap-region))
  17422. (t (call-interactively 'org-insert-heading))))
  17423. ;;; Menu entries
  17424. ;; Define the Org-mode menus
  17425. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  17426. '("Tbl"
  17427. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  17428. ["Next Field" org-cycle (org-at-table-p)]
  17429. ["Previous Field" org-shifttab (org-at-table-p)]
  17430. ["Next Row" org-return (org-at-table-p)]
  17431. "--"
  17432. ["Blank Field" org-table-blank-field (org-at-table-p)]
  17433. ["Edit Field" org-table-edit-field (org-at-table-p)]
  17434. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  17435. "--"
  17436. ("Column"
  17437. ["Move Column Left" org-metaleft (org-at-table-p)]
  17438. ["Move Column Right" org-metaright (org-at-table-p)]
  17439. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  17440. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  17441. ("Row"
  17442. ["Move Row Up" org-metaup (org-at-table-p)]
  17443. ["Move Row Down" org-metadown (org-at-table-p)]
  17444. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  17445. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  17446. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  17447. "--"
  17448. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  17449. ("Rectangle"
  17450. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  17451. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  17452. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  17453. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  17454. "--"
  17455. ("Calculate"
  17456. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  17457. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  17458. ["Edit Formulas" org-edit-special (org-at-table-p)]
  17459. "--"
  17460. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  17461. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  17462. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  17463. "--"
  17464. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  17465. "--"
  17466. ["Sum Column/Rectangle" org-table-sum
  17467. (or (org-at-table-p) (org-region-active-p))]
  17468. ["Which Column?" org-table-current-column (org-at-table-p)])
  17469. ["Debug Formulas"
  17470. org-table-toggle-formula-debugger
  17471. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  17472. ["Show Col/Row Numbers"
  17473. org-table-toggle-coordinate-overlays
  17474. :style toggle
  17475. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  17476. "--"
  17477. ["Create" org-table-create (and (not (org-at-table-p))
  17478. org-enable-table-editor)]
  17479. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  17480. ["Import from File" org-table-import (not (org-at-table-p))]
  17481. ["Export to File" org-table-export (org-at-table-p)]
  17482. "--"
  17483. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  17484. (easy-menu-define org-org-menu org-mode-map "Org menu"
  17485. '("Org"
  17486. ("Show/Hide"
  17487. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  17488. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  17489. ["Sparse Tree..." org-sparse-tree t]
  17490. ["Reveal Context" org-reveal t]
  17491. ["Show All" show-all t]
  17492. "--"
  17493. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  17494. "--"
  17495. ["New Heading" org-insert-heading t]
  17496. ("Navigate Headings"
  17497. ["Up" outline-up-heading t]
  17498. ["Next" outline-next-visible-heading t]
  17499. ["Previous" outline-previous-visible-heading t]
  17500. ["Next Same Level" outline-forward-same-level t]
  17501. ["Previous Same Level" outline-backward-same-level t]
  17502. "--"
  17503. ["Jump" org-goto t])
  17504. ("Edit Structure"
  17505. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  17506. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  17507. "--"
  17508. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  17509. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  17510. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  17511. "--"
  17512. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  17513. "--"
  17514. ["Copy visible text" org-copy-visible t]
  17515. "--"
  17516. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  17517. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  17518. ["Demote Heading" org-metaright (not (org-at-table-p))]
  17519. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  17520. "--"
  17521. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  17522. "--"
  17523. ["Convert to odd levels" org-convert-to-odd-levels t]
  17524. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  17525. ("Editing"
  17526. ["Emphasis..." org-emphasize t]
  17527. ["Edit Source Example" org-edit-special t]
  17528. "--"
  17529. ["Footnote new/jump" org-footnote-action t]
  17530. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  17531. ("Archive"
  17532. ["Archive (default method)" org-archive-subtree-default t]
  17533. "--"
  17534. ["Move Subtree to Archive file" org-advertized-archive-subtree t]
  17535. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  17536. ["Move subtree to Archive sibling" org-archive-to-archive-sibling t]
  17537. )
  17538. "--"
  17539. ("Hyperlinks"
  17540. ["Store Link (Global)" org-store-link t]
  17541. ["Find existing link to here" org-occur-link-in-agenda-files t]
  17542. ["Insert Link" org-insert-link t]
  17543. ["Follow Link" org-open-at-point t]
  17544. "--"
  17545. ["Next link" org-next-link t]
  17546. ["Previous link" org-previous-link t]
  17547. "--"
  17548. ["Descriptive Links"
  17549. org-toggle-link-display
  17550. :style radio
  17551. :selected org-descriptive-links
  17552. ]
  17553. ["Literal Links"
  17554. org-toggle-link-display
  17555. :style radio
  17556. :selected (not org-descriptive-links)])
  17557. "--"
  17558. ("TODO Lists"
  17559. ["TODO/DONE/-" org-todo t]
  17560. ("Select keyword"
  17561. ["Next keyword" org-shiftright (org-at-heading-p)]
  17562. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  17563. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  17564. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  17565. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  17566. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  17567. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  17568. "--"
  17569. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  17570. :selected org-enforce-todo-dependencies :style toggle :active t]
  17571. "Settings for tree at point"
  17572. ["Do Children sequentially" org-toggle-ordered-property :style radio
  17573. :selected (org-entry-get nil "ORDERED")
  17574. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  17575. ["Do Children parallel" org-toggle-ordered-property :style radio
  17576. :selected (not (org-entry-get nil "ORDERED"))
  17577. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  17578. "--"
  17579. ["Set Priority" org-priority t]
  17580. ["Priority Up" org-shiftup t]
  17581. ["Priority Down" org-shiftdown t]
  17582. "--"
  17583. ["Get news from all feeds" org-feed-update-all t]
  17584. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  17585. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  17586. ("TAGS and Properties"
  17587. ["Set Tags" org-set-tags-command t]
  17588. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  17589. "--"
  17590. ["Set property" org-set-property t]
  17591. ["Column view of properties" org-columns t]
  17592. ["Insert Column View DBlock" org-insert-columns-dblock t])
  17593. ("Dates and Scheduling"
  17594. ["Timestamp" org-time-stamp t]
  17595. ["Timestamp (inactive)" org-time-stamp-inactive t]
  17596. ("Change Date"
  17597. ["1 Day Later" org-shiftright t]
  17598. ["1 Day Earlier" org-shiftleft t]
  17599. ["1 ... Later" org-shiftup t]
  17600. ["1 ... Earlier" org-shiftdown t])
  17601. ["Compute Time Range" org-evaluate-time-range t]
  17602. ["Schedule Item" org-schedule t]
  17603. ["Deadline" org-deadline t]
  17604. "--"
  17605. ["Custom time format" org-toggle-time-stamp-overlays
  17606. :style radio :selected org-display-custom-times]
  17607. "--"
  17608. ["Goto Calendar" org-goto-calendar t]
  17609. ["Date from Calendar" org-date-from-calendar t]
  17610. "--"
  17611. ["Start/Restart Timer" org-timer-start t]
  17612. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  17613. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  17614. ["Insert Timer String" org-timer t]
  17615. ["Insert Timer Item" org-timer-item t])
  17616. ("Logging work"
  17617. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  17618. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  17619. ["Clock out" org-clock-out t]
  17620. ["Clock cancel" org-clock-cancel t]
  17621. "--"
  17622. ["Mark as default task" org-clock-mark-default-task t]
  17623. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  17624. ["Goto running clock" org-clock-goto t]
  17625. "--"
  17626. ["Display times" org-clock-display t]
  17627. ["Create clock table" org-clock-report t]
  17628. "--"
  17629. ["Record DONE time"
  17630. (progn (setq org-log-done (not org-log-done))
  17631. (message "Switching to %s will %s record a timestamp"
  17632. (car org-done-keywords)
  17633. (if org-log-done "automatically" "not")))
  17634. :style toggle :selected org-log-done])
  17635. "--"
  17636. ["Agenda Command..." org-agenda t]
  17637. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  17638. ("File List for Agenda")
  17639. ("Special views current file"
  17640. ["TODO Tree" org-show-todo-tree t]
  17641. ["Check Deadlines" org-check-deadlines t]
  17642. ["Timeline" org-timeline t]
  17643. ["Tags/Property tree" org-match-sparse-tree t])
  17644. "--"
  17645. ["Export/Publish..." org-export t]
  17646. ("LaTeX"
  17647. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  17648. :selected org-cdlatex-mode]
  17649. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  17650. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  17651. ["Modify math symbol" org-cdlatex-math-modify
  17652. (org-inside-LaTeX-fragment-p)]
  17653. ["Insert citation" org-reftex-citation t]
  17654. "--"
  17655. ["Template for BEAMER" (progn (require 'org-beamer)
  17656. (org-insert-beamer-options-template)) t])
  17657. "--"
  17658. ("MobileOrg"
  17659. ["Push Files and Views" org-mobile-push t]
  17660. ["Get Captured and Flagged" org-mobile-pull t]
  17661. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  17662. "--"
  17663. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  17664. "--"
  17665. ("Documentation"
  17666. ["Show Version" org-version t]
  17667. ["Info Documentation" org-info t])
  17668. ("Customize"
  17669. ["Browse Org Group" org-customize t]
  17670. "--"
  17671. ["Expand This Menu" org-create-customize-menu
  17672. (fboundp 'customize-menu-create)])
  17673. ["Send bug report" org-submit-bug-report t]
  17674. "--"
  17675. ("Refresh/Reload"
  17676. ["Refresh setup current buffer" org-mode-restart t]
  17677. ["Reload Org (after update)" org-reload t]
  17678. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  17679. ))
  17680. (defun org-info (&optional node)
  17681. "Read documentation for Org-mode in the info system.
  17682. With optional NODE, go directly to that node."
  17683. (interactive)
  17684. (info (format "(org)%s" (or node ""))))
  17685. ;;;###autoload
  17686. (defun org-submit-bug-report ()
  17687. "Submit a bug report on Org-mode via mail.
  17688. Don't hesitate to report any problems or inaccurate documentation.
  17689. If you don't have setup sending mail from (X)Emacs, please copy the
  17690. output buffer into your mail program, as it gives us important
  17691. information about your Org-mode version and configuration."
  17692. (interactive)
  17693. (require 'reporter)
  17694. (org-load-modules-maybe)
  17695. (org-require-autoloaded-modules)
  17696. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  17697. (reporter-submit-bug-report
  17698. "emacs-orgmode@gnu.org"
  17699. (org-version)
  17700. (let (list)
  17701. (save-window-excursion
  17702. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  17703. (delete-other-windows)
  17704. (erase-buffer)
  17705. (insert "You are about to submit a bug report to the Org-mode mailing list.
  17706. We would like to add your full Org-mode and Outline configuration to the
  17707. bug report. This greatly simplifies the work of the maintainer and
  17708. other experts on the mailing list.
  17709. HOWEVER, some variables you have customized may contain private
  17710. information. The names of customers, colleagues, or friends, might
  17711. appear in the form of file names, tags, todo states, or search strings.
  17712. If you answer yes to the prompt, you might want to check and remove
  17713. such private information before sending the email.")
  17714. (add-text-properties (point-min) (point-max) '(face org-warning))
  17715. (when (yes-or-no-p "Include your Org-mode configuration ")
  17716. (mapatoms
  17717. (lambda (v)
  17718. (and (boundp v)
  17719. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  17720. (or (and (symbol-value v)
  17721. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  17722. (and
  17723. (get v 'custom-type) (get v 'standard-value)
  17724. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  17725. (push v list)))))
  17726. (kill-buffer (get-buffer "*Warn about privacy*"))
  17727. list))
  17728. nil nil
  17729. "Remember to cover the basics, that is, what you expected to happen and
  17730. what in fact did happen. You don't know how to make a good report? See
  17731. http://orgmode.org/manual/Feedback.html#Feedback
  17732. Your bug report will be posted to the Org-mode mailing list.
  17733. ------------------------------------------------------------------------")
  17734. (save-excursion
  17735. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  17736. (replace-match "\\1Bug: \\3 [\\2]")))))
  17737. (defun org-install-agenda-files-menu ()
  17738. (let ((bl (buffer-list)))
  17739. (save-excursion
  17740. (while bl
  17741. (set-buffer (pop bl))
  17742. (if (eq major-mode 'org-mode) (setq bl nil)))
  17743. (when (eq major-mode 'org-mode)
  17744. (easy-menu-change
  17745. '("Org") "File List for Agenda"
  17746. (append
  17747. (list
  17748. ["Edit File List" (org-edit-agenda-file-list) t]
  17749. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  17750. ["Remove Current File from List" org-remove-file t]
  17751. ["Cycle through agenda files" org-cycle-agenda-files t]
  17752. ["Occur in all agenda files" org-occur-in-agenda-files t]
  17753. "--")
  17754. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  17755. ;;;; Documentation
  17756. ;;;###autoload
  17757. (defun org-require-autoloaded-modules ()
  17758. (interactive)
  17759. (mapc 'require
  17760. '(org-agenda org-archive org-ascii org-attach org-clock org-colview
  17761. org-docbook org-exp org-html org-icalendar
  17762. org-id org-latex
  17763. org-publish org-remember org-table
  17764. org-timer org-xoxo)))
  17765. ;;;###autoload
  17766. (defun org-reload (&optional uncompiled)
  17767. "Reload all org lisp files.
  17768. With prefix arg UNCOMPILED, load the uncompiled versions."
  17769. (interactive "P")
  17770. (require 'find-func)
  17771. (let* ((file-re "^\\(org\\|orgtbl\\)\\(\\.el\\|-.*\\.el\\)")
  17772. (dir-org (file-name-directory (org-find-library-name "org")))
  17773. (dir-org-contrib (ignore-errors
  17774. (file-name-directory
  17775. (org-find-library-name "org-contribdir"))))
  17776. (babel-files
  17777. (mapcar (lambda (el) (concat "ob" (when el (format "-%s" el)) ".el"))
  17778. (append (list nil "comint" "eval" "exp" "keys"
  17779. "lob" "ref" "table" "tangle")
  17780. (delq nil
  17781. (mapcar
  17782. (lambda (lang)
  17783. (when (cdr lang) (symbol-name (car lang))))
  17784. org-babel-load-languages)))))
  17785. (files
  17786. (append (directory-files dir-org t file-re)
  17787. babel-files
  17788. (and dir-org-contrib
  17789. (directory-files dir-org-contrib t file-re))))
  17790. (remove-re (concat (if (featurep 'xemacs)
  17791. "org-colview" "org-colview-xemacs")
  17792. "\\'")))
  17793. (setq files (mapcar 'file-name-sans-extension files))
  17794. (setq files (mapcar
  17795. (lambda (x) (if (string-match remove-re x) nil x))
  17796. files))
  17797. (setq files (delq nil files))
  17798. (mapc
  17799. (lambda (f)
  17800. (when (featurep (intern (file-name-nondirectory f)))
  17801. (if (and (not uncompiled)
  17802. (file-exists-p (concat f ".elc")))
  17803. (load (concat f ".elc") nil nil t)
  17804. (load (concat f ".el") nil nil t))))
  17805. files))
  17806. (org-version))
  17807. ;;;###autoload
  17808. (defun org-customize ()
  17809. "Call the customize function with org as argument."
  17810. (interactive)
  17811. (org-load-modules-maybe)
  17812. (org-require-autoloaded-modules)
  17813. (customize-browse 'org))
  17814. (defun org-create-customize-menu ()
  17815. "Create a full customization menu for Org-mode, insert it into the menu."
  17816. (interactive)
  17817. (org-load-modules-maybe)
  17818. (org-require-autoloaded-modules)
  17819. (if (fboundp 'customize-menu-create)
  17820. (progn
  17821. (easy-menu-change
  17822. '("Org") "Customize"
  17823. `(["Browse Org group" org-customize t]
  17824. "--"
  17825. ,(customize-menu-create 'org)
  17826. ["Set" Custom-set t]
  17827. ["Save" Custom-save t]
  17828. ["Reset to Current" Custom-reset-current t]
  17829. ["Reset to Saved" Custom-reset-saved t]
  17830. ["Reset to Standard Settings" Custom-reset-standard t]))
  17831. (message "\"Org\"-menu now contains full customization menu"))
  17832. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  17833. ;;;; Miscellaneous stuff
  17834. ;;; Generally useful functions
  17835. (defun org-get-at-bol (property)
  17836. "Get text property PROPERTY at beginning of line."
  17837. (get-text-property (point-at-bol) property))
  17838. (defun org-find-text-property-in-string (prop s)
  17839. "Return the first non-nil value of property PROP in string S."
  17840. (or (get-text-property 0 prop s)
  17841. (get-text-property (or (next-single-property-change 0 prop s) 0)
  17842. prop s)))
  17843. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  17844. "Display the given MESSAGE as a warning."
  17845. (if (fboundp 'display-warning)
  17846. (display-warning 'org message
  17847. (if (featurep 'xemacs) 'warning :warning))
  17848. (let ((buf (get-buffer-create "*Org warnings*")))
  17849. (with-current-buffer buf
  17850. (goto-char (point-max))
  17851. (insert "Warning (Org): " message)
  17852. (unless (bolp)
  17853. (newline)))
  17854. (display-buffer buf)
  17855. (sit-for 0))))
  17856. (defun org-eval (form)
  17857. "Eval FORM and return result."
  17858. (condition-case error
  17859. (eval form)
  17860. (error (format "%%![Error: %s]" error))))
  17861. (defun org-in-clocktable-p ()
  17862. "Check if the cursor is in a clocktable."
  17863. (let ((pos (point)) start)
  17864. (save-excursion
  17865. (end-of-line 1)
  17866. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  17867. (setq start (match-beginning 0))
  17868. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  17869. (>= (match-end 0) pos)
  17870. start))))
  17871. (defun org-in-commented-line ()
  17872. "Is point in a line starting with `#'?"
  17873. (equal (char-after (point-at-bol)) ?#))
  17874. (defun org-in-indented-comment-line ()
  17875. "Is point in a line starting with `#' after some white space?"
  17876. (save-excursion
  17877. (save-match-data
  17878. (goto-char (point-at-bol))
  17879. (looking-at "[ \t]*#"))))
  17880. (defun org-in-verbatim-emphasis ()
  17881. (save-match-data
  17882. (and (org-in-regexp org-emph-re 2) (member (match-string 3) '("=" "~")))))
  17883. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  17884. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  17885. (if (and marker (marker-buffer marker)
  17886. (buffer-live-p (marker-buffer marker)))
  17887. (progn
  17888. (org-pop-to-buffer-same-window (marker-buffer marker))
  17889. (if (or (> marker (point-max)) (< marker (point-min)))
  17890. (widen))
  17891. (goto-char marker)
  17892. (org-show-context 'org-goto))
  17893. (if bookmark
  17894. (bookmark-jump bookmark)
  17895. (error "Cannot find location"))))
  17896. (defun org-quote-csv-field (s)
  17897. "Quote field for inclusion in CSV material."
  17898. (if (string-match "[\",]" s)
  17899. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  17900. s))
  17901. (defun org-force-self-insert (N)
  17902. "Needed to enforce self-insert under remapping."
  17903. (interactive "p")
  17904. (self-insert-command N))
  17905. (defun org-string-width (s)
  17906. "Compute width of string, ignoring invisible characters.
  17907. This ignores character with invisibility property `org-link', and also
  17908. characters with property `org-cwidth', because these will become invisible
  17909. upon the next fontification round."
  17910. (let (b l)
  17911. (when (or (eq t buffer-invisibility-spec)
  17912. (assq 'org-link buffer-invisibility-spec))
  17913. (while (setq b (text-property-any 0 (length s)
  17914. 'invisible 'org-link s))
  17915. (setq s (concat (substring s 0 b)
  17916. (substring s (or (next-single-property-change
  17917. b 'invisible s) (length s)))))))
  17918. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  17919. (setq s (concat (substring s 0 b)
  17920. (substring s (or (next-single-property-change
  17921. b 'org-cwidth s) (length s))))))
  17922. (setq l (string-width s) b -1)
  17923. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  17924. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  17925. l))
  17926. (defun org-shorten-string (s maxlength)
  17927. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  17928. If the string is shorter or has length MAXLENGTH, just return the
  17929. original string. If it is longer, the functions finds a space in the
  17930. string, breaks this string off at that locations and adds three dots
  17931. as ellipsis. Including the ellipsis, the string will not be longer
  17932. than MAXLENGTH. If finding a good breaking point in the string does
  17933. not work, the string is just chopped off in the middle of a word
  17934. if necessary."
  17935. (if (<= (length s) maxlength)
  17936. s
  17937. (let* ((n (max (- maxlength 4) 1))
  17938. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  17939. (if (string-match re s)
  17940. (concat (match-string 1 s) "...")
  17941. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  17942. (defun org-get-indentation (&optional line)
  17943. "Get the indentation of the current line, interpreting tabs.
  17944. When LINE is given, assume it represents a line and compute its indentation."
  17945. (if line
  17946. (if (string-match "^ *" (org-remove-tabs line))
  17947. (match-end 0))
  17948. (save-excursion
  17949. (beginning-of-line 1)
  17950. (skip-chars-forward " \t")
  17951. (current-column))))
  17952. (defun org-get-string-indentation (s)
  17953. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  17954. (let ((n -1) (i 0) (w tab-width) c)
  17955. (catch 'exit
  17956. (while (< (setq n (1+ n)) (length s))
  17957. (setq c (aref s n))
  17958. (cond ((= c ?\ ) (setq i (1+ i)))
  17959. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  17960. (t (throw 'exit t)))))
  17961. i))
  17962. (defun org-remove-tabs (s &optional width)
  17963. "Replace tabulators in S with spaces.
  17964. Assumes that s is a single line, starting in column 0."
  17965. (setq width (or width tab-width))
  17966. (while (string-match "\t" s)
  17967. (setq s (replace-match
  17968. (make-string
  17969. (- (* width (/ (+ (match-beginning 0) width) width))
  17970. (match-beginning 0)) ?\ )
  17971. t t s)))
  17972. s)
  17973. (defun org-fix-indentation (line ind)
  17974. "Fix indentation in LINE.
  17975. IND is a cons cell with target and minimum indentation.
  17976. If the current indentation in LINE is smaller than the minimum,
  17977. leave it alone. If it is larger than ind, set it to the target."
  17978. (let* ((l (org-remove-tabs line))
  17979. (i (org-get-indentation l))
  17980. (i1 (car ind)) (i2 (cdr ind)))
  17981. (if (>= i i2) (setq l (substring line i2)))
  17982. (if (> i1 0)
  17983. (concat (make-string i1 ?\ ) l)
  17984. l)))
  17985. (defun org-remove-indentation (code &optional n)
  17986. "Remove the maximum common indentation from the lines in CODE.
  17987. N may optionally be the number of spaces to remove."
  17988. (with-temp-buffer
  17989. (insert code)
  17990. (org-do-remove-indentation n)
  17991. (buffer-string)))
  17992. (defun org-do-remove-indentation (&optional n)
  17993. "Remove the maximum common indentation from the buffer."
  17994. (untabify (point-min) (point-max))
  17995. (let ((min 10000) re)
  17996. (if n
  17997. (setq min n)
  17998. (goto-char (point-min))
  17999. (while (re-search-forward "^ *[^ \n]" nil t)
  18000. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  18001. (unless (or (= min 0) (= min 10000))
  18002. (setq re (format "^ \\{%d\\}" min))
  18003. (goto-char (point-min))
  18004. (while (re-search-forward re nil t)
  18005. (replace-match "")
  18006. (end-of-line 1))
  18007. min)))
  18008. (defun org-fill-template (template alist)
  18009. "Find each %key of ALIST in TEMPLATE and replace it."
  18010. (let ((case-fold-search nil)
  18011. entry key value)
  18012. (setq alist (sort (copy-sequence alist)
  18013. (lambda (a b) (< (length (car a)) (length (car b))))))
  18014. (while (setq entry (pop alist))
  18015. (setq template
  18016. (replace-regexp-in-string
  18017. (concat "%" (regexp-quote (car entry)))
  18018. (cdr entry) template t t)))
  18019. template))
  18020. (defun org-base-buffer (buffer)
  18021. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  18022. (if (not buffer)
  18023. buffer
  18024. (or (buffer-base-buffer buffer)
  18025. buffer)))
  18026. (defun org-trim (s)
  18027. "Remove whitespace at beginning and end of string."
  18028. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  18029. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  18030. s)
  18031. (defun org-wrap (string &optional width lines)
  18032. "Wrap string to either a number of lines, or a width in characters.
  18033. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  18034. that costs. If there is a word longer than WIDTH, the text is actually
  18035. wrapped to the length of that word.
  18036. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  18037. many lines, whatever width that takes.
  18038. The return value is a list of lines, without newlines at the end."
  18039. (let* ((words (org-split-string string "[ \t\n]+"))
  18040. (maxword (apply 'max (mapcar 'org-string-width words)))
  18041. w ll)
  18042. (cond (width
  18043. (org-do-wrap words (max maxword width)))
  18044. (lines
  18045. (setq w maxword)
  18046. (setq ll (org-do-wrap words maxword))
  18047. (if (<= (length ll) lines)
  18048. ll
  18049. (setq ll words)
  18050. (while (> (length ll) lines)
  18051. (setq w (1+ w))
  18052. (setq ll (org-do-wrap words w)))
  18053. ll))
  18054. (t (error "Cannot wrap this")))))
  18055. (defun org-do-wrap (words width)
  18056. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  18057. (let (lines line)
  18058. (while words
  18059. (setq line (pop words))
  18060. (while (and words (< (+ (length line) (length (car words))) width))
  18061. (setq line (concat line " " (pop words))))
  18062. (setq lines (push line lines)))
  18063. (nreverse lines)))
  18064. (defun org-split-string (string &optional separators)
  18065. "Splits STRING into substrings at SEPARATORS.
  18066. No empty strings are returned if there are matches at the beginning
  18067. and end of string."
  18068. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  18069. (start 0)
  18070. notfirst
  18071. (list nil))
  18072. (while (and (string-match rexp string
  18073. (if (and notfirst
  18074. (= start (match-beginning 0))
  18075. (< start (length string)))
  18076. (1+ start) start))
  18077. (< (match-beginning 0) (length string)))
  18078. (setq notfirst t)
  18079. (or (eq (match-beginning 0) 0)
  18080. (and (eq (match-beginning 0) (match-end 0))
  18081. (eq (match-beginning 0) start))
  18082. (setq list
  18083. (cons (substring string start (match-beginning 0))
  18084. list)))
  18085. (setq start (match-end 0)))
  18086. (or (eq start (length string))
  18087. (setq list
  18088. (cons (substring string start)
  18089. list)))
  18090. (nreverse list)))
  18091. (defun org-quote-vert (s)
  18092. "Replace \"|\" with \"\\vert\"."
  18093. (while (string-match "|" s)
  18094. (setq s (replace-match "\\vert" t t s)))
  18095. s)
  18096. (defun org-uuidgen-p (s)
  18097. "Is S an ID created by UUIDGEN?"
  18098. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  18099. (defun org-in-src-block-p nil
  18100. "Whether point is in a code source block."
  18101. (let (ov)
  18102. (when (setq ov (overlays-at (point)))
  18103. (memq 'org-block-background
  18104. (overlay-properties
  18105. (car ov))))))
  18106. (defun org-context ()
  18107. "Return a list of contexts of the current cursor position.
  18108. If several contexts apply, all are returned.
  18109. Each context entry is a list with a symbol naming the context, and
  18110. two positions indicating start and end of the context. Possible
  18111. contexts are:
  18112. :headline anywhere in a headline
  18113. :headline-stars on the leading stars in a headline
  18114. :todo-keyword on a TODO keyword (including DONE) in a headline
  18115. :tags on the TAGS in a headline
  18116. :priority on the priority cookie in a headline
  18117. :item on the first line of a plain list item
  18118. :item-bullet on the bullet/number of a plain list item
  18119. :checkbox on the checkbox in a plain list item
  18120. :table in an org-mode table
  18121. :table-special on a special filed in a table
  18122. :table-table in a table.el table
  18123. :clocktable in a clocktable
  18124. :src-block in a source block
  18125. :link on a hyperlink
  18126. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT, QUOTE.
  18127. :target on a <<target>>
  18128. :radio-target on a <<<radio-target>>>
  18129. :latex-fragment on a LaTeX fragment
  18130. :latex-preview on a LaTeX fragment with overlaid preview image
  18131. This function expects the position to be visible because it uses font-lock
  18132. faces as a help to recognize the following contexts: :table-special, :link,
  18133. and :keyword."
  18134. (let* ((f (get-text-property (point) 'face))
  18135. (faces (if (listp f) f (list f)))
  18136. (case-fold-search t)
  18137. (p (point)) clist o)
  18138. ;; First the large context
  18139. (cond
  18140. ((org-at-heading-p t)
  18141. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  18142. (when (progn
  18143. (beginning-of-line 1)
  18144. (looking-at org-todo-line-tags-regexp))
  18145. (push (org-point-in-group p 1 :headline-stars) clist)
  18146. (push (org-point-in-group p 2 :todo-keyword) clist)
  18147. (push (org-point-in-group p 4 :tags) clist))
  18148. (goto-char p)
  18149. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  18150. (if (looking-at "\\[#[A-Z0-9]\\]")
  18151. (push (org-point-in-group p 0 :priority) clist)))
  18152. ((org-at-item-p)
  18153. (push (org-point-in-group p 2 :item-bullet) clist)
  18154. (push (list :item (point-at-bol)
  18155. (save-excursion (org-end-of-item) (point)))
  18156. clist)
  18157. (and (org-at-item-checkbox-p)
  18158. (push (org-point-in-group p 0 :checkbox) clist)))
  18159. ((org-at-table-p)
  18160. (push (list :table (org-table-begin) (org-table-end)) clist)
  18161. (if (memq 'org-formula faces)
  18162. (push (list :table-special
  18163. (previous-single-property-change p 'face)
  18164. (next-single-property-change p 'face)) clist)))
  18165. ((org-at-table-p 'any)
  18166. (push (list :table-table) clist)))
  18167. (goto-char p)
  18168. ;; New the "medium" contexts: clocktables, source blocks
  18169. (cond ((org-in-clocktable-p)
  18170. (push (list :clocktable
  18171. (and (or (looking-at "#\\+BEGIN: clocktable")
  18172. (search-backward "#+BEGIN: clocktable" nil t))
  18173. (match-beginning 0))
  18174. (and (re-search-forward "#\\+END:?" nil t)
  18175. (match-end 0))) clist))
  18176. ((org-in-src-block-p)
  18177. (push (list :src-block
  18178. (and (or (looking-at "#\\+BEGIN_SRC")
  18179. (search-backward "#+BEGIN_SRC" nil t))
  18180. (match-beginning 0))
  18181. (and (search-forward "#+END_SRC" nil t)
  18182. (match-beginning 0))) clist)))
  18183. (goto-char p)
  18184. ;; Now the small context
  18185. (cond
  18186. ((org-at-timestamp-p)
  18187. (push (org-point-in-group p 0 :timestamp) clist))
  18188. ((memq 'org-link faces)
  18189. (push (list :link
  18190. (previous-single-property-change p 'face)
  18191. (next-single-property-change p 'face)) clist))
  18192. ((memq 'org-special-keyword faces)
  18193. (push (list :keyword
  18194. (previous-single-property-change p 'face)
  18195. (next-single-property-change p 'face)) clist))
  18196. ((org-at-target-p)
  18197. (push (org-point-in-group p 0 :target) clist)
  18198. (goto-char (1- (match-beginning 0)))
  18199. (if (looking-at org-radio-target-regexp)
  18200. (push (org-point-in-group p 0 :radio-target) clist))
  18201. (goto-char p))
  18202. ((setq o (car (delq nil
  18203. (mapcar
  18204. (lambda (x)
  18205. (if (memq x org-latex-fragment-image-overlays) x))
  18206. (overlays-at (point))))))
  18207. (push (list :latex-fragment
  18208. (overlay-start o) (overlay-end o)) clist)
  18209. (push (list :latex-preview
  18210. (overlay-start o) (overlay-end o)) clist))
  18211. ((org-inside-LaTeX-fragment-p)
  18212. ;; FIXME: positions wrong.
  18213. (push (list :latex-fragment (point) (point)) clist)))
  18214. (setq clist (nreverse (delq nil clist)))
  18215. clist))
  18216. ;; FIXME: Compare with at-regexp-p Do we need both?
  18217. (defun org-in-regexp (re &optional nlines visually)
  18218. "Check if point is inside a match of regexp.
  18219. Normally only the current line is checked, but you can include NLINES extra
  18220. lines both before and after point into the search.
  18221. If VISUALLY is set, require that the cursor is not after the match but
  18222. really on, so that the block visually is on the match."
  18223. (catch 'exit
  18224. (let ((pos (point))
  18225. (eol (point-at-eol (+ 1 (or nlines 0))))
  18226. (inc (if visually 1 0)))
  18227. (save-excursion
  18228. (beginning-of-line (- 1 (or nlines 0)))
  18229. (while (re-search-forward re eol t)
  18230. (if (and (<= (match-beginning 0) pos)
  18231. (>= (+ inc (match-end 0)) pos))
  18232. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  18233. (defun org-at-regexp-p (regexp)
  18234. "Is point inside a match of REGEXP in the current line?"
  18235. (catch 'exit
  18236. (save-excursion
  18237. (let ((pos (point)) (end (point-at-eol)))
  18238. (beginning-of-line 1)
  18239. (while (re-search-forward regexp end t)
  18240. (if (and (<= (match-beginning 0) pos)
  18241. (>= (match-end 0) pos))
  18242. (throw 'exit t)))
  18243. nil))))
  18244. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  18245. "Non-nil when point is between matches of START-RE and END-RE.
  18246. Also return a non-nil value when point is on one of the matches.
  18247. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  18248. buffer positions. Default values are the positions of headlines
  18249. surrounding the point.
  18250. The functions returns a cons cell whose car (resp. cdr) is the
  18251. position before START-RE (resp. after END-RE)."
  18252. (save-match-data
  18253. (let ((pos (point))
  18254. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  18255. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  18256. beg end)
  18257. (save-excursion
  18258. ;; Point is on a block when on START-RE or if START-RE can be
  18259. ;; found before it...
  18260. (and (or (org-at-regexp-p start-re)
  18261. (re-search-backward start-re limit-up t))
  18262. (setq beg (match-beginning 0))
  18263. ;; ... and END-RE after it...
  18264. (goto-char (match-end 0))
  18265. (re-search-forward end-re limit-down t)
  18266. (> (setq end (match-end 0)) pos)
  18267. ;; ... without another START-RE in-between.
  18268. (goto-char (match-beginning 0))
  18269. (not (re-search-backward start-re (1+ beg) t))
  18270. ;; Return value.
  18271. (cons beg end))))))
  18272. (defun org-in-block-p (names)
  18273. "Non-nil when point belongs to a block whose name belongs to NAMES.
  18274. NAMES is a list of strings containing names of blocks.
  18275. Return first block name matched, or nil. Beware that in case of
  18276. nested blocks, the returned name may not belong to the closest
  18277. block from point."
  18278. (save-match-data
  18279. (catch 'exit
  18280. (let ((case-fold-search t)
  18281. (lim-up (save-excursion (outline-previous-heading)))
  18282. (lim-down (save-excursion (outline-next-heading))))
  18283. (mapc (lambda (name)
  18284. (let ((n (regexp-quote name)))
  18285. (when (org-between-regexps-p
  18286. (concat "^[ \t]*#\\+begin_" n)
  18287. (concat "^[ \t]*#\\+end_" n)
  18288. lim-up lim-down)
  18289. (throw 'exit n))))
  18290. names))
  18291. nil)))
  18292. (defun org-occur-in-agenda-files (regexp &optional nlines)
  18293. "Call `multi-occur' with buffers for all agenda files."
  18294. (interactive "sOrg-files matching: \np")
  18295. (let* ((files (org-agenda-files))
  18296. (tnames (mapcar 'file-truename files))
  18297. (extra org-agenda-text-search-extra-files)
  18298. f)
  18299. (when (eq (car extra) 'agenda-archives)
  18300. (setq extra (cdr extra))
  18301. (setq files (org-add-archive-files files)))
  18302. (while (setq f (pop extra))
  18303. (unless (member (file-truename f) tnames)
  18304. (add-to-list 'files f 'append)
  18305. (add-to-list 'tnames (file-truename f) 'append)))
  18306. (multi-occur
  18307. (mapcar (lambda (x)
  18308. (with-current-buffer
  18309. (or (get-file-buffer x) (find-file-noselect x))
  18310. (widen)
  18311. (current-buffer)))
  18312. files)
  18313. regexp)))
  18314. (if (boundp 'occur-mode-find-occurrence-hook)
  18315. ;; Emacs 23
  18316. (add-hook 'occur-mode-find-occurrence-hook
  18317. (lambda ()
  18318. (when (eq major-mode 'org-mode)
  18319. (org-reveal))))
  18320. ;; Emacs 22
  18321. (defadvice occur-mode-goto-occurrence
  18322. (after org-occur-reveal activate)
  18323. (and (eq major-mode 'org-mode) (org-reveal)))
  18324. (defadvice occur-mode-goto-occurrence-other-window
  18325. (after org-occur-reveal activate)
  18326. (and (eq major-mode 'org-mode) (org-reveal)))
  18327. (defadvice occur-mode-display-occurrence
  18328. (after org-occur-reveal activate)
  18329. (when (eq major-mode 'org-mode)
  18330. (let ((pos (occur-mode-find-occurrence)))
  18331. (with-current-buffer (marker-buffer pos)
  18332. (save-excursion
  18333. (goto-char pos)
  18334. (org-reveal)))))))
  18335. (defun org-occur-link-in-agenda-files ()
  18336. "Create a link and search for it in the agendas.
  18337. The link is not stored in `org-stored-links', it is just created
  18338. for the search purpose."
  18339. (interactive)
  18340. (let ((link (condition-case nil
  18341. (org-store-link nil)
  18342. (error "Unable to create a link to here"))))
  18343. (org-occur-in-agenda-files (regexp-quote link))))
  18344. (defun org-uniquify (list)
  18345. "Remove duplicate elements from LIST."
  18346. (let (res)
  18347. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  18348. res))
  18349. (defun org-delete-all (elts list)
  18350. "Remove all elements in ELTS from LIST."
  18351. (while elts
  18352. (setq list (delete (pop elts) list)))
  18353. list)
  18354. (defun org-count (cl-item cl-seq)
  18355. "Count the number of occurrences of ITEM in SEQ.
  18356. Taken from `count' in cl-seq.el with all keyword arguments removed."
  18357. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  18358. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  18359. (while (< cl-start cl-end)
  18360. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  18361. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  18362. (setq cl-start (1+ cl-start)))
  18363. cl-count))
  18364. (defun org-remove-if (predicate seq)
  18365. "Remove everything from SEQ that fulfills PREDICATE."
  18366. (let (res e)
  18367. (while seq
  18368. (setq e (pop seq))
  18369. (if (not (funcall predicate e)) (push e res)))
  18370. (nreverse res)))
  18371. (defun org-remove-if-not (predicate seq)
  18372. "Remove everything from SEQ that does not fulfill PREDICATE."
  18373. (let (res e)
  18374. (while seq
  18375. (setq e (pop seq))
  18376. (if (funcall predicate e) (push e res)))
  18377. (nreverse res)))
  18378. (defun org-reduce (cl-func cl-seq &rest cl-keys)
  18379. "Reduce two-argument FUNCTION across SEQ.
  18380. Taken from `reduce' in cl-seq.el with all keyword arguments but
  18381. \":initial-value\" removed."
  18382. (let ((cl-accum (cond ((memq :initial-value cl-keys)
  18383. (cadr (memq :initial-value cl-keys)))
  18384. (cl-seq (pop cl-seq))
  18385. (t (funcall cl-func)))))
  18386. (while cl-seq
  18387. (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
  18388. cl-accum))
  18389. (defun org-back-over-empty-lines ()
  18390. "Move backwards over whitespace, to the beginning of the first empty line.
  18391. Returns the number of empty lines passed."
  18392. (let ((pos (point)))
  18393. (if (cdr (assoc 'heading org-blank-before-new-entry))
  18394. (skip-chars-backward " \t\n\r")
  18395. (unless (eobp)
  18396. (forward-line -1)))
  18397. (beginning-of-line 2)
  18398. (goto-char (min (point) pos))
  18399. (count-lines (point) pos)))
  18400. (defun org-skip-whitespace ()
  18401. (skip-chars-forward " \t\n\r"))
  18402. (defun org-point-in-group (point group &optional context)
  18403. "Check if POINT is in match-group GROUP.
  18404. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  18405. match. If the match group does not exist or point is not inside it,
  18406. return nil."
  18407. (and (match-beginning group)
  18408. (>= point (match-beginning group))
  18409. (<= point (match-end group))
  18410. (if context
  18411. (list context (match-beginning group) (match-end group))
  18412. t)))
  18413. (defun org-switch-to-buffer-other-window (&rest args)
  18414. "Switch to buffer in a second window on the current frame.
  18415. In particular, do not allow pop-up frames.
  18416. Returns the newly created buffer."
  18417. (let (pop-up-frames special-display-buffer-names special-display-regexps
  18418. special-display-function)
  18419. (apply 'switch-to-buffer-other-window args)))
  18420. (defun org-combine-plists (&rest plists)
  18421. "Create a single property list from all plists in PLISTS.
  18422. The process starts by copying the first list, and then setting properties
  18423. from the other lists. Settings in the last list are the most significant
  18424. ones and overrule settings in the other lists."
  18425. (let ((rtn (copy-sequence (pop plists)))
  18426. p v ls)
  18427. (while plists
  18428. (setq ls (pop plists))
  18429. (while ls
  18430. (setq p (pop ls) v (pop ls))
  18431. (setq rtn (plist-put rtn p v))))
  18432. rtn))
  18433. (defun org-move-line-down (arg)
  18434. "Move the current line down. With prefix argument, move it past ARG lines."
  18435. (interactive "p")
  18436. (let ((col (current-column))
  18437. beg end pos)
  18438. (beginning-of-line 1) (setq beg (point))
  18439. (beginning-of-line 2) (setq end (point))
  18440. (beginning-of-line (+ 1 arg))
  18441. (setq pos (move-marker (make-marker) (point)))
  18442. (insert (delete-and-extract-region beg end))
  18443. (goto-char pos)
  18444. (org-move-to-column col)))
  18445. (defun org-move-line-up (arg)
  18446. "Move the current line up. With prefix argument, move it past ARG lines."
  18447. (interactive "p")
  18448. (let ((col (current-column))
  18449. beg end pos)
  18450. (beginning-of-line 1) (setq beg (point))
  18451. (beginning-of-line 2) (setq end (point))
  18452. (beginning-of-line (- arg))
  18453. (setq pos (move-marker (make-marker) (point)))
  18454. (insert (delete-and-extract-region beg end))
  18455. (goto-char pos)
  18456. (org-move-to-column col)))
  18457. (defun org-replace-escapes (string table)
  18458. "Replace %-escapes in STRING with values in TABLE.
  18459. TABLE is an association list with keys like \"%a\" and string values.
  18460. The sequences in STRING may contain normal field width and padding information,
  18461. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  18462. so values can contain further %-escapes if they are define later in TABLE."
  18463. (let ((tbl (copy-alist table))
  18464. (case-fold-search nil)
  18465. (pchg 0)
  18466. e re rpl)
  18467. (while (setq e (pop tbl))
  18468. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  18469. (when (and (cdr e) (string-match re (cdr e)))
  18470. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  18471. (safe "SREF"))
  18472. (add-text-properties 0 3 (list 'sref sref) safe)
  18473. (setcdr e (replace-match safe t t (cdr e)))))
  18474. (while (string-match re string)
  18475. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  18476. (cdr e)))
  18477. (setq string (replace-match rpl t t string))))
  18478. (while (setq pchg (next-property-change pchg string))
  18479. (let ((sref (get-text-property pchg 'sref string)))
  18480. (when (and sref (string-match "SREF" string pchg))
  18481. (setq string (replace-match sref t t string)))))
  18482. string))
  18483. (defun org-sublist (list start end)
  18484. "Return a section of LIST, from START to END.
  18485. Counting starts at 1."
  18486. (let (rtn (c start))
  18487. (setq list (nthcdr (1- start) list))
  18488. (while (and list (<= c end))
  18489. (push (pop list) rtn)
  18490. (setq c (1+ c)))
  18491. (nreverse rtn)))
  18492. (defun org-find-base-buffer-visiting (file)
  18493. "Like `find-buffer-visiting' but always return the base buffer and
  18494. not an indirect buffer."
  18495. (let ((buf (or (get-file-buffer file)
  18496. (find-buffer-visiting file))))
  18497. (if buf
  18498. (or (buffer-base-buffer buf) buf)
  18499. nil)))
  18500. (defun org-image-file-name-regexp (&optional extensions)
  18501. "Return regexp matching the file names of images.
  18502. If EXTENSIONS is given, only match these."
  18503. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  18504. (image-file-name-regexp)
  18505. (let ((image-file-name-extensions
  18506. (or extensions
  18507. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  18508. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  18509. (concat "\\."
  18510. (regexp-opt (nconc (mapcar 'upcase
  18511. image-file-name-extensions)
  18512. image-file-name-extensions)
  18513. t)
  18514. "\\'"))))
  18515. (defun org-file-image-p (file &optional extensions)
  18516. "Return non-nil if FILE is an image."
  18517. (save-match-data
  18518. (string-match (org-image-file-name-regexp extensions) file)))
  18519. (defun org-get-cursor-date ()
  18520. "Return the date at cursor in as a time.
  18521. This works in the calendar and in the agenda, anywhere else it just
  18522. returns the current time."
  18523. (let (date day defd)
  18524. (cond
  18525. ((eq major-mode 'calendar-mode)
  18526. (setq date (calendar-cursor-to-date)
  18527. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  18528. ((eq major-mode 'org-agenda-mode)
  18529. (setq day (get-text-property (point) 'day))
  18530. (if day
  18531. (setq date (calendar-gregorian-from-absolute day)
  18532. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  18533. (nth 2 date))))))
  18534. (or defd (current-time))))
  18535. (defvar org-agenda-action-marker (make-marker)
  18536. "Marker pointing to the entry for the next agenda action.")
  18537. (defun org-mark-entry-for-agenda-action ()
  18538. "Mark the current entry as target of an agenda action.
  18539. Agenda actions are actions executed from the agenda with the key `k',
  18540. which make use of the date at the cursor."
  18541. (interactive)
  18542. (move-marker org-agenda-action-marker
  18543. (save-excursion (org-back-to-heading t) (point))
  18544. (current-buffer))
  18545. (message
  18546. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  18547. (defun org-mark-subtree ()
  18548. "Mark the current subtree.
  18549. This puts point at the start of the current subtree, and mark at the end.
  18550. If point is in an inline task, mark that task instead."
  18551. (interactive)
  18552. (let ((inline-task-p
  18553. (and (featurep 'org-inlinetask)
  18554. (org-inlinetask-in-task-p)))
  18555. (beg))
  18556. ;; Get beginning of subtree
  18557. (cond
  18558. (inline-task-p (org-inlinetask-goto-beginning))
  18559. ((org-at-heading-p) (beginning-of-line))
  18560. (t (org-with-limited-levels (outline-previous-visible-heading 1))))
  18561. (setq beg (point))
  18562. ;; Get end of it
  18563. (if inline-task-p
  18564. (org-inlinetask-goto-end)
  18565. (org-end-of-subtree))
  18566. ;; Mark zone
  18567. (push-mark (point) nil t)
  18568. (goto-char beg)))
  18569. ;;; Paragraph filling stuff.
  18570. ;; We want this to be just right, so use the full arsenal.
  18571. (defun org-indent-line-function ()
  18572. "Indent line depending on context."
  18573. (interactive)
  18574. (let* ((pos (point))
  18575. (itemp (org-at-item-p))
  18576. (case-fold-search t)
  18577. (org-drawer-regexp (or org-drawer-regexp "\000"))
  18578. (inline-task-p (and (featurep 'org-inlinetask)
  18579. (org-inlinetask-in-task-p)))
  18580. (inline-re (and inline-task-p
  18581. (org-inlinetask-outline-regexp)))
  18582. column)
  18583. (beginning-of-line 1)
  18584. (cond
  18585. ;; Comments
  18586. ((looking-at "# ") (setq column 0))
  18587. ;; Headings
  18588. ((looking-at org-outline-regexp) (setq column 0))
  18589. ;; Included files
  18590. ((looking-at "#\\+include:") (setq column 0))
  18591. ;; Footnote definition
  18592. ((looking-at org-footnote-definition-re) (setq column 0))
  18593. ;; Literal examples
  18594. ((looking-at "[ \t]*:\\( \\|$\\)")
  18595. (setq column (org-get-indentation))) ; do nothing
  18596. ;; Lists
  18597. ((ignore-errors (goto-char (org-in-item-p)))
  18598. (setq column (if itemp
  18599. (org-get-indentation)
  18600. (org-list-item-body-column (point))))
  18601. (goto-char pos))
  18602. ;; Drawers
  18603. ((and (looking-at "[ \t]*:END:")
  18604. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  18605. (save-excursion
  18606. (goto-char (1- (match-beginning 1)))
  18607. (setq column (current-column))))
  18608. ;; Special blocks
  18609. ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
  18610. (save-excursion
  18611. (re-search-backward
  18612. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  18613. (setq column (org-get-indentation (match-string 0))))
  18614. ((and (not (looking-at "[ \t]*#\\+begin_"))
  18615. (org-between-regexps-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
  18616. (save-excursion
  18617. (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
  18618. (setq column
  18619. (cond ((equal (downcase (match-string 1)) "src")
  18620. ;; src blocks: let `org-edit-src-exit' handle them
  18621. (org-get-indentation))
  18622. ((equal (downcase (match-string 1)) "example")
  18623. (max (org-get-indentation)
  18624. (org-get-indentation (match-string 0))))
  18625. (t
  18626. (org-get-indentation (match-string 0))))))
  18627. ;; This line has nothing special, look at the previous relevant
  18628. ;; line to compute indentation
  18629. (t
  18630. (beginning-of-line 0)
  18631. (while (and (not (bobp))
  18632. (not (looking-at org-drawer-regexp))
  18633. ;; When point started in an inline task, do not move
  18634. ;; above task starting line.
  18635. (not (and inline-task-p (looking-at inline-re)))
  18636. ;; Skip drawers, blocks, empty lines, verbatim,
  18637. ;; comments, tables, footnotes definitions, lists,
  18638. ;; inline tasks.
  18639. (or (and (looking-at "[ \t]*:END:")
  18640. (re-search-backward org-drawer-regexp nil t))
  18641. (and (looking-at "[ \t]*#\\+end_")
  18642. (re-search-backward "[ \t]*#\\+begin_"nil t))
  18643. (looking-at "[ \t]*[\n:#|]")
  18644. (looking-at org-footnote-definition-re)
  18645. (and (ignore-errors (goto-char (org-in-item-p)))
  18646. (goto-char
  18647. (org-list-get-top-point (org-list-struct))))
  18648. (and (not inline-task-p)
  18649. (featurep 'org-inlinetask)
  18650. (org-inlinetask-in-task-p)
  18651. (or (org-inlinetask-goto-beginning) t))))
  18652. (beginning-of-line 0))
  18653. (cond
  18654. ;; There was an heading above.
  18655. ((looking-at "\\*+[ \t]+")
  18656. (if (not org-adapt-indentation)
  18657. (setq column 0)
  18658. (goto-char (match-end 0))
  18659. (setq column (current-column))))
  18660. ;; A drawer had started and is unfinished
  18661. ((looking-at org-drawer-regexp)
  18662. (goto-char (1- (match-beginning 1)))
  18663. (setq column (current-column)))
  18664. ;; Else, nothing noticeable found: get indentation and go on.
  18665. (t (setq column (org-get-indentation))))))
  18666. ;; Now apply indentation and move cursor accordingly
  18667. (goto-char pos)
  18668. (if (<= (current-column) (current-indentation))
  18669. (org-indent-line-to column)
  18670. (save-excursion (org-indent-line-to column)))
  18671. ;; Special polishing for properties, see `org-property-format'
  18672. (setq column (current-column))
  18673. (beginning-of-line 1)
  18674. (if (looking-at
  18675. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  18676. (replace-match (concat (match-string 1)
  18677. (format org-property-format
  18678. (match-string 2) (match-string 3)))
  18679. t t))
  18680. (org-move-to-column column)))
  18681. (defun org-indent-drawer ()
  18682. "Indent the drawer at point."
  18683. (interactive)
  18684. (let ((p (point))
  18685. (e (and (save-excursion (re-search-forward ":END:" nil t))
  18686. (match-end 0)))
  18687. (folded
  18688. (save-excursion
  18689. (end-of-line)
  18690. (when (overlays-at (point))
  18691. (member 'invisible (overlay-properties
  18692. (car (overlays-at (point)))))))))
  18693. (when folded (org-cycle))
  18694. (indent-for-tab-command)
  18695. (while (and (move-beginning-of-line 2) (< (point) e))
  18696. (indent-for-tab-command))
  18697. (goto-char p)
  18698. (when folded (org-cycle)))
  18699. (message "Drawer at point indented"))
  18700. (defun org-indent-block ()
  18701. "Indent the block at point."
  18702. (interactive)
  18703. (let ((p (point))
  18704. (case-fold-search t)
  18705. (e (and (save-excursion (re-search-forward "#\\+end_?\\(?:[a-z]+\\)?" nil t))
  18706. (match-end 0)))
  18707. (folded
  18708. (save-excursion
  18709. (end-of-line)
  18710. (when (overlays-at (point))
  18711. (member 'invisible (overlay-properties
  18712. (car (overlays-at (point)))))))))
  18713. (when folded (org-cycle))
  18714. (indent-for-tab-command)
  18715. (while (and (move-beginning-of-line 2) (< (point) e))
  18716. (indent-for-tab-command))
  18717. (goto-char p)
  18718. (when folded (org-cycle)))
  18719. (message "Block at point indented"))
  18720. (defvar org-adaptive-fill-regexp-backup adaptive-fill-regexp
  18721. "Variable to store copy of `adaptive-fill-regexp'.
  18722. Since `adaptive-fill-regexp' is set to never match, we need to
  18723. store a backup of its value before entering `org-mode' so that
  18724. the functionality can be provided as a fall-back.")
  18725. (defun org-set-autofill-regexps ()
  18726. (interactive)
  18727. ;; In the paragraph separator we include headlines, because filling
  18728. ;; text in a line directly attached to a headline would otherwise
  18729. ;; fill the headline as well.
  18730. (org-set-local 'comment-start-skip "^#+[ \t]*")
  18731. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|#]")
  18732. ;; The paragraph starter includes hand-formatted lists.
  18733. (org-set-local
  18734. 'paragraph-start
  18735. (concat
  18736. "\f" "\\|"
  18737. "[ ]*$" "\\|"
  18738. org-outline-regexp "\\|"
  18739. "[ \t]*#" "\\|"
  18740. (org-item-re) "\\|"
  18741. "[ \t]*[:|]" "\\|"
  18742. "\\$\\$" "\\|"
  18743. "\\\\\\(begin\\|end\\|[][]\\)"))
  18744. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  18745. ;; But only if the user has not turned off tables or fixed-width regions
  18746. (org-set-local
  18747. 'auto-fill-inhibit-regexp
  18748. (concat org-outline-regexp
  18749. "\\|#\\+"
  18750. "\\|[ \t]*" org-keyword-time-regexp
  18751. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  18752. (concat
  18753. "\\|[ \t]*["
  18754. (if org-enable-table-editor "|" "")
  18755. (if org-enable-fixed-width-editor ":" "")
  18756. "]"))))
  18757. ;; We use our own fill-paragraph function, to make sure that tables
  18758. ;; and fixed-width regions are not wrapped. That function will pass
  18759. ;; through to `fill-paragraph' when appropriate.
  18760. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  18761. ;; Prevent auto-fill from inserting unwanted new items.
  18762. (if (boundp 'fill-nobreak-predicate)
  18763. (org-set-local
  18764. 'fill-nobreak-predicate
  18765. (org-uniquify
  18766. (append fill-nobreak-predicate
  18767. '(org-fill-item-nobreak-p org-fill-line-break-nobreak-p)))))
  18768. ;; Adaptive filling: To get full control, first make sure that
  18769. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  18770. (unless (local-variable-p 'adaptive-fill-regexp (current-buffer))
  18771. (org-set-local 'org-adaptive-fill-regexp-backup
  18772. adaptive-fill-regexp))
  18773. (org-set-local 'adaptive-fill-regexp "\000")
  18774. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  18775. (org-set-local 'adaptive-fill-function
  18776. 'org-adaptive-fill-function)
  18777. (org-set-local
  18778. 'align-mode-rules-list
  18779. '((org-in-buffer-settings
  18780. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  18781. (modes . '(org-mode))))))
  18782. (defun org-fill-item-nobreak-p ()
  18783. "Non-nil when a line break at point would insert a new item."
  18784. (and (looking-at (org-item-re)) (org-list-in-valid-context-p)))
  18785. (defun org-fill-line-break-nobreak-p ()
  18786. "Non-nil when a line break at point would create an Org line break."
  18787. (save-excursion
  18788. (skip-chars-backward "[ \t]")
  18789. (skip-chars-backward "\\\\")
  18790. (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
  18791. (defun org-fill-paragraph (&optional justify)
  18792. "Re-align a table, pass through to fill-paragraph if no table."
  18793. (let ((table-p (org-at-table-p))
  18794. (table.el-p (org-at-table.el-p))
  18795. (itemp (org-in-item-p)))
  18796. (cond ((and (equal (char-after (point-at-bol)) ?*)
  18797. (save-excursion (goto-char (point-at-bol))
  18798. (looking-at org-outline-regexp)))
  18799. t) ; skip headlines
  18800. (table.el-p t) ; skip table.el tables
  18801. (table-p (org-table-align) t) ; align Org tables
  18802. (itemp ; align text in items
  18803. (let* ((struct (save-excursion (goto-char itemp)
  18804. (org-list-struct)))
  18805. (parents (org-list-parents-alist struct))
  18806. (children (org-list-get-children itemp struct parents))
  18807. beg end prev next prefix)
  18808. ;; Determine in which part of item point is: before
  18809. ;; first child, after last child, between two
  18810. ;; sub-lists, or simply in item if there's no child.
  18811. (cond
  18812. ((not children)
  18813. (setq prefix (make-string (org-list-item-body-column itemp) ?\ )
  18814. beg itemp
  18815. end (org-list-get-item-end itemp struct)))
  18816. ((< (point) (setq next (car children)))
  18817. (setq prefix (make-string (org-list-item-body-column itemp) ?\ )
  18818. beg itemp
  18819. end next))
  18820. ((> (point) (setq prev (car (last children))))
  18821. (setq beg (org-list-get-item-end prev struct)
  18822. end (org-list-get-item-end itemp struct)
  18823. prefix (save-excursion
  18824. (goto-char beg)
  18825. (skip-chars-forward " \t")
  18826. (make-string (current-column) ?\ ))))
  18827. (t (catch 'exit
  18828. (while (setq next (pop children))
  18829. (if (> (point) next)
  18830. (setq prev next)
  18831. (setq beg (org-list-get-item-end prev struct)
  18832. end next
  18833. prefix (save-excursion
  18834. (goto-char beg)
  18835. (skip-chars-forward " \t")
  18836. (make-string (current-column) ?\ )))
  18837. (throw 'exit nil))))))
  18838. ;; Use `fill-paragraph' with buffer narrowed to item
  18839. ;; without any child, and with our computed PREFIX.
  18840. (flet ((fill-context-prefix (from to &optional flr) prefix))
  18841. (save-restriction
  18842. (narrow-to-region beg end)
  18843. (save-excursion (fill-paragraph justify)))) t))
  18844. ;; Special case where point is not in a list but is on
  18845. ;; a paragraph adjacent to a list: make sure this paragraph
  18846. ;; doesn't get merged with the end of the list by narrowing
  18847. ;; buffer first.
  18848. ((save-excursion (forward-paragraph -1)
  18849. (setq itemp (org-in-item-p)))
  18850. (let ((struct (save-excursion (goto-char itemp)
  18851. (org-list-struct))))
  18852. (save-restriction
  18853. (narrow-to-region (org-list-get-bottom-point struct)
  18854. (save-excursion (forward-paragraph 1)
  18855. (point)))
  18856. (fill-paragraph justify) t)))
  18857. ;; Don't fill schedule/deadline line before a paragraph
  18858. ((save-excursion (forward-paragraph -1)
  18859. (or (looking-at (concat "^[^\n]*" org-scheduled-regexp ".*$"))
  18860. (looking-at (concat "^[^\n]*" org-deadline-regexp ".*$"))))
  18861. (save-restriction
  18862. (narrow-to-region (1+ (match-end 0))
  18863. (save-excursion (forward-paragraph 1) (point)))
  18864. (fill-paragraph justify) t))
  18865. ;; Else simply call `fill-paragraph'.
  18866. (t nil))))
  18867. ;; For reference, this is the default value of adaptive-fill-regexp
  18868. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  18869. (defun org-adaptive-fill-function ()
  18870. "Return a fill prefix for org-mode files."
  18871. (let (itemp)
  18872. (save-excursion
  18873. (cond
  18874. ;; Comment line
  18875. ((looking-at "#[ \t]+")
  18876. (match-string-no-properties 0))
  18877. ;; Plain list item
  18878. ((org-at-item-p)
  18879. (make-string (org-list-item-body-column (point-at-bol)) ?\ ))
  18880. ;; Point is in a list after `backward-paragraph': original
  18881. ;; point wasn't in the list, or filling would have been taken
  18882. ;; care of by `org-auto-fill-function', but the list and the
  18883. ;; real paragraph are not separated by a blank line. Thus, move
  18884. ;; point after the list to go back to real paragraph and
  18885. ;; determine fill-prefix.
  18886. ((setq itemp (org-in-item-p))
  18887. (goto-char itemp)
  18888. (let* ((struct (org-list-struct))
  18889. (bottom (org-list-get-bottom-point struct)))
  18890. (goto-char bottom)
  18891. (make-string (org-get-indentation) ?\ )))
  18892. ;; Other text
  18893. ((looking-at org-adaptive-fill-regexp-backup)
  18894. (match-string-no-properties 0))))))
  18895. (defun org-auto-fill-function ()
  18896. "Auto-fill function."
  18897. (let (itemp prefix)
  18898. ;; When in a list, compute an appropriate fill-prefix and make
  18899. ;; sure it will be used by `do-auto-fill'.
  18900. (if (setq itemp (org-in-item-p))
  18901. (progn
  18902. (setq prefix (make-string (org-list-item-body-column itemp) ?\ ))
  18903. (flet ((fill-context-prefix (from to &optional flr) prefix))
  18904. (do-auto-fill)))
  18905. ;; Else just use `do-auto-fill'.
  18906. (do-auto-fill))))
  18907. ;;; Other stuff.
  18908. (defun org-toggle-fixed-width-section (arg)
  18909. "Toggle the fixed-width export.
  18910. If there is no active region, the QUOTE keyword at the current headline is
  18911. inserted or removed. When present, it causes the text between this headline
  18912. and the next to be exported as fixed-width text, and unmodified.
  18913. If there is an active region, this command adds or removes a colon as the
  18914. first character of this line. If the first character of a line is a colon,
  18915. this line is also exported in fixed-width font."
  18916. (interactive "P")
  18917. (let* ((cc 0)
  18918. (regionp (org-region-active-p))
  18919. (beg (if regionp (region-beginning) (point)))
  18920. (end (if regionp (region-end)))
  18921. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  18922. (case-fold-search nil)
  18923. (re "[ \t]*\\(:\\(?: \\|$\\)\\)")
  18924. off)
  18925. (if regionp
  18926. (save-excursion
  18927. (goto-char beg)
  18928. (setq cc (current-column))
  18929. (beginning-of-line 1)
  18930. (setq off (looking-at re))
  18931. (while (> nlines 0)
  18932. (setq nlines (1- nlines))
  18933. (beginning-of-line 1)
  18934. (cond
  18935. (arg
  18936. (org-move-to-column cc t)
  18937. (insert ": \n")
  18938. (forward-line -1))
  18939. ((and off (looking-at re))
  18940. (replace-match "" t t nil 1))
  18941. ((not off) (org-move-to-column cc t) (insert ": ")))
  18942. (forward-line 1)))
  18943. (save-excursion
  18944. (org-back-to-heading)
  18945. (cond
  18946. ((looking-at (format org-heading-keyword-regexp-format
  18947. org-quote-string))
  18948. (goto-char (match-end 1))
  18949. (looking-at (concat " +" org-quote-string))
  18950. (replace-match "" t t)
  18951. (when (eolp) (insert " ")))
  18952. ((looking-at org-outline-regexp)
  18953. (goto-char (match-end 0))
  18954. (insert org-quote-string " ")))))))
  18955. (defun org-reftex-citation ()
  18956. "Use reftex-citation to insert a citation into the buffer.
  18957. This looks for a line like
  18958. #+BIBLIOGRAPHY: foo plain option:-d
  18959. and derives from it that foo.bib is the bibliography file relevant
  18960. for this document. It then installs the necessary environment for RefTeX
  18961. to work in this buffer and calls `reftex-citation' to insert a citation
  18962. into the buffer.
  18963. Export of such citations to both LaTeX and HTML is handled by the contributed
  18964. package org-exp-bibtex by Taru Karttunen."
  18965. (interactive)
  18966. (let ((reftex-docstruct-symbol 'rds)
  18967. (reftex-cite-format "\\cite{%l}")
  18968. rds bib)
  18969. (save-excursion
  18970. (save-restriction
  18971. (widen)
  18972. (let ((case-fold-search t)
  18973. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  18974. (if (not (save-excursion
  18975. (or (re-search-forward re nil t)
  18976. (re-search-backward re nil t))))
  18977. (error "No bibliography defined in file")
  18978. (setq bib (concat (match-string 1) ".bib")
  18979. rds (list (list 'bib bib)))))))
  18980. (call-interactively 'reftex-citation)))
  18981. ;;;; Functions extending outline functionality
  18982. (defun org-beginning-of-line (&optional arg)
  18983. "Go to the beginning of the current line. If that is invisible, continue
  18984. to a visible line beginning. This makes the function of C-a more intuitive.
  18985. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  18986. first attempt, and only move to after the tags when the cursor is already
  18987. beyond the end of the headline."
  18988. (interactive "P")
  18989. (let ((pos (point))
  18990. (special (if (consp org-special-ctrl-a/e)
  18991. (car org-special-ctrl-a/e)
  18992. org-special-ctrl-a/e))
  18993. refpos)
  18994. (if (org-bound-and-true-p line-move-visual)
  18995. (beginning-of-visual-line 1)
  18996. (beginning-of-line 1))
  18997. (if (and arg (fboundp 'move-beginning-of-line))
  18998. (call-interactively 'move-beginning-of-line)
  18999. (if (bobp)
  19000. nil
  19001. (backward-char 1)
  19002. (if (org-truely-invisible-p)
  19003. (while (and (not (bobp)) (org-truely-invisible-p))
  19004. (backward-char 1)
  19005. (beginning-of-line 1))
  19006. (forward-char 1))))
  19007. (when special
  19008. (cond
  19009. ((and (looking-at org-complex-heading-regexp)
  19010. (= (char-after (match-end 1)) ?\ ))
  19011. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  19012. (point-at-eol)))
  19013. (goto-char
  19014. (if (eq special t)
  19015. (cond ((> pos refpos) refpos)
  19016. ((= pos (point)) refpos)
  19017. (t (point)))
  19018. (cond ((> pos (point)) (point))
  19019. ((not (eq last-command this-command)) (point))
  19020. (t refpos)))))
  19021. ((org-at-item-p)
  19022. ;; Being at an item and not looking at an the item means point
  19023. ;; was previously moved to beginning of a visual line, which
  19024. ;; doesn't contain the item. Therefore, do nothing special,
  19025. ;; just stay here.
  19026. (when (looking-at org-list-full-item-re)
  19027. ;; Set special position at first white space character after
  19028. ;; bullet, and check-box, if any.
  19029. (let ((after-bullet
  19030. (let ((box (match-end 3)))
  19031. (if (not box) (match-end 1)
  19032. (let ((after (char-after box)))
  19033. (if (and after (= after ? )) (1+ box) box))))))
  19034. ;; Special case: Move point to special position when
  19035. ;; currently after it or at beginning of line.
  19036. (if (eq special t)
  19037. (when (or (> pos after-bullet) (= (point) pos))
  19038. (goto-char after-bullet))
  19039. ;; Reversed case: Move point to special position when
  19040. ;; point was already at beginning of line and command is
  19041. ;; repeated.
  19042. (when (and (= (point) pos) (eq last-command this-command))
  19043. (goto-char after-bullet))))))))
  19044. (org-no-warnings
  19045. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  19046. (defun org-end-of-line (&optional arg)
  19047. "Go to the end of the line.
  19048. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  19049. first attempt, and only move to after the tags when the cursor is already
  19050. beyond the end of the headline."
  19051. (interactive "P")
  19052. (let ((special (if (consp org-special-ctrl-a/e)
  19053. (cdr org-special-ctrl-a/e)
  19054. org-special-ctrl-a/e)))
  19055. (cond
  19056. ((or (not special) arg
  19057. (not (or (org-at-heading-p) (org-at-item-p) (org-at-drawer-p))))
  19058. (call-interactively
  19059. (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
  19060. ((fboundp 'move-end-of-line) 'move-end-of-line)
  19061. (t 'end-of-line))))
  19062. ((org-at-heading-p)
  19063. (let ((pos (point)))
  19064. (beginning-of-line 1)
  19065. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  19066. (if (eq special t)
  19067. (if (or (< pos (match-beginning 1))
  19068. (= pos (match-end 0)))
  19069. (goto-char (match-beginning 1))
  19070. (goto-char (match-end 0)))
  19071. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  19072. (goto-char (match-end 0))
  19073. (goto-char (match-beginning 1))))
  19074. (call-interactively (if (fboundp 'move-end-of-line)
  19075. 'move-end-of-line
  19076. 'end-of-line)))))
  19077. ((org-at-drawer-p)
  19078. (move-end-of-line 1)
  19079. (when (overlays-at (1- (point))) (backward-char 1)))
  19080. ;; At an item: Move before any hidden text.
  19081. (t (call-interactively
  19082. (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
  19083. ((fboundp 'move-end-of-line) 'move-end-of-line)
  19084. (t 'end-of-line)))))
  19085. (org-no-warnings
  19086. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  19087. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  19088. (define-key org-mode-map "\C-e" 'org-end-of-line)
  19089. (defun org-backward-sentence (&optional arg)
  19090. "Go to beginning of sentence, or beginning of table field.
  19091. This will call `backward-sentence' or `org-table-beginning-of-field',
  19092. depending on context."
  19093. (interactive "P")
  19094. (cond
  19095. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  19096. (t (call-interactively 'backward-sentence))))
  19097. (defun org-forward-sentence (&optional arg)
  19098. "Go to end of sentence, or end of table field.
  19099. This will call `forward-sentence' or `org-table-end-of-field',
  19100. depending on context."
  19101. (interactive "P")
  19102. (cond
  19103. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  19104. (t (call-interactively 'forward-sentence))))
  19105. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  19106. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  19107. (defun org-kill-line (&optional arg)
  19108. "Kill line, to tags or end of line."
  19109. (interactive "P")
  19110. (cond
  19111. ((or (not org-special-ctrl-k)
  19112. (bolp)
  19113. (not (org-at-heading-p)))
  19114. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  19115. org-ctrl-k-protect-subtree)
  19116. (if (or (eq org-ctrl-k-protect-subtree 'error)
  19117. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  19118. (error "C-k aborted - would kill hidden subtree")))
  19119. (call-interactively
  19120. (if (and (boundp 'visual-line-mode) visual-line-mode) 'kill-visual-line 'kill-line)))
  19121. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  19122. (kill-region (point) (match-beginning 1))
  19123. (org-set-tags nil t))
  19124. (t (kill-region (point) (point-at-eol)))))
  19125. (define-key org-mode-map "\C-k" 'org-kill-line)
  19126. (defun org-yank (&optional arg)
  19127. "Yank. If the kill is a subtree, treat it specially.
  19128. This command will look at the current kill and check if is a single
  19129. subtree, or a series of subtrees[1]. If it passes the test, and if the
  19130. cursor is at the beginning of a line or after the stars of a currently
  19131. empty headline, then the yank is handled specially. How exactly depends
  19132. on the value of the following variables, both set by default.
  19133. org-yank-folded-subtrees
  19134. When set, the subtree(s) will be folded after insertion, but only
  19135. if doing so would now swallow text after the yanked text.
  19136. org-yank-adjusted-subtrees
  19137. When set, the subtree will be promoted or demoted in order to
  19138. fit into the local outline tree structure, which means that the level
  19139. will be adjusted so that it becomes the smaller one of the two
  19140. *visible* surrounding headings.
  19141. Any prefix to this command will cause `yank' to be called directly with
  19142. no special treatment. In particular, a simple \\[universal-argument] prefix \
  19143. will just
  19144. plainly yank the text as it is.
  19145. \[1] The test checks if the first non-white line is a heading
  19146. and if there are no other headings with fewer stars."
  19147. (interactive "P")
  19148. (org-yank-generic 'yank arg))
  19149. (defun org-yank-generic (command arg)
  19150. "Perform some yank-like command.
  19151. This function implements the behavior described in the `org-yank'
  19152. documentation. However, it has been generalized to work for any
  19153. interactive command with similar behavior."
  19154. ;; pretend to be command COMMAND
  19155. (setq this-command command)
  19156. (if arg
  19157. (call-interactively command)
  19158. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  19159. (and (org-kill-is-subtree-p)
  19160. (or (bolp)
  19161. (and (looking-at "[ \t]*$")
  19162. (string-match
  19163. "\\`\\*+\\'"
  19164. (buffer-substring (point-at-bol) (point)))))))
  19165. swallowp)
  19166. (cond
  19167. ((and subtreep org-yank-folded-subtrees)
  19168. (let ((beg (point))
  19169. end)
  19170. (if (and subtreep org-yank-adjusted-subtrees)
  19171. (org-paste-subtree nil nil 'for-yank)
  19172. (call-interactively command))
  19173. (setq end (point))
  19174. (goto-char beg)
  19175. (when (and (bolp) subtreep
  19176. (not (setq swallowp
  19177. (org-yank-folding-would-swallow-text beg end))))
  19178. (org-with-limited-levels
  19179. (or (looking-at org-outline-regexp)
  19180. (re-search-forward org-outline-regexp-bol end t))
  19181. (while (and (< (point) end) (looking-at org-outline-regexp))
  19182. (hide-subtree)
  19183. (org-cycle-show-empty-lines 'folded)
  19184. (condition-case nil
  19185. (outline-forward-same-level 1)
  19186. (error (goto-char end))))))
  19187. (when swallowp
  19188. (message
  19189. "Inserted text not folded because that would swallow text"))
  19190. (goto-char end)
  19191. (skip-chars-forward " \t\n\r")
  19192. (beginning-of-line 1)
  19193. (push-mark beg 'nomsg)))
  19194. ((and subtreep org-yank-adjusted-subtrees)
  19195. (let ((beg (point-at-bol)))
  19196. (org-paste-subtree nil nil 'for-yank)
  19197. (push-mark beg 'nomsg)))
  19198. (t
  19199. (call-interactively command))))))
  19200. (defun org-yank-folding-would-swallow-text (beg end)
  19201. "Would hide-subtree at BEG swallow any text after END?"
  19202. (let (level)
  19203. (org-with-limited-levels
  19204. (save-excursion
  19205. (goto-char beg)
  19206. (when (or (looking-at org-outline-regexp)
  19207. (re-search-forward org-outline-regexp-bol end t))
  19208. (setq level (org-outline-level)))
  19209. (goto-char end)
  19210. (skip-chars-forward " \t\r\n\v\f")
  19211. (if (or (eobp)
  19212. (and (bolp) (looking-at org-outline-regexp)
  19213. (<= (org-outline-level) level)))
  19214. nil ; Nothing would be swallowed
  19215. t))))) ; something would swallow
  19216. (define-key org-mode-map "\C-y" 'org-yank)
  19217. (defun org-truely-invisible-p ()
  19218. "Check if point is at a character currently not visible.
  19219. This version does not only check the character property, but also
  19220. `visible-mode'."
  19221. ;; Early versions of noutline don't have `outline-invisible-p'.
  19222. (if (org-bound-and-true-p visible-mode)
  19223. nil
  19224. (outline-invisible-p)))
  19225. (defun org-invisible-p2 ()
  19226. "Check if point is at a character currently not visible."
  19227. (save-excursion
  19228. (if (and (eolp) (not (bobp))) (backward-char 1))
  19229. ;; Early versions of noutline don't have `outline-invisible-p'.
  19230. (outline-invisible-p)))
  19231. (defun org-back-to-heading (&optional invisible-ok)
  19232. "Call `outline-back-to-heading', but provide a better error message."
  19233. (condition-case nil
  19234. (outline-back-to-heading invisible-ok)
  19235. (error (error "Before first headline at position %d in buffer %s"
  19236. (point) (current-buffer)))))
  19237. (defun org-beginning-of-defun ()
  19238. "Go to the beginning of the subtree, i.e. back to the heading."
  19239. (org-back-to-heading))
  19240. (defun org-end-of-defun ()
  19241. "Go to the end of the subtree."
  19242. (org-end-of-subtree nil t))
  19243. (defun org-before-first-heading-p ()
  19244. "Before first heading?"
  19245. (save-excursion
  19246. (end-of-line)
  19247. (null (re-search-backward org-outline-regexp-bol nil t))))
  19248. (defun org-at-heading-p (&optional ignored)
  19249. (outline-on-heading-p t))
  19250. ;; Compatibility alias with Org versions < 7.8.03
  19251. (defalias 'org-on-heading-p 'org-at-heading-p)
  19252. (defun org-at-drawer-p nil
  19253. "Is cursor at a drawer keyword?"
  19254. (save-excursion
  19255. (move-beginning-of-line 1)
  19256. (looking-at org-drawer-regexp)))
  19257. (defun org-at-block-p nil
  19258. "Is cursor at a block keyword?"
  19259. (save-excursion
  19260. (move-beginning-of-line 1)
  19261. (looking-at org-block-regexp)))
  19262. (defun org-point-at-end-of-empty-headline ()
  19263. "If point is at the end of an empty headline, return t, else nil.
  19264. If the heading only contains a TODO keyword, it is still still considered
  19265. empty."
  19266. (and (looking-at "[ \t]*$")
  19267. (when org-todo-line-regexp
  19268. (save-excursion
  19269. (beginning-of-line 1)
  19270. (let ((case-fold-search nil))
  19271. (looking-at org-todo-line-regexp)
  19272. (string= (match-string 3) ""))))))
  19273. (defun org-at-heading-or-item-p ()
  19274. (or (org-at-heading-p) (org-at-item-p)))
  19275. (defun org-at-target-p ()
  19276. (or (org-in-regexp org-radio-target-regexp)
  19277. (org-in-regexp org-target-regexp)))
  19278. ;; Compatibility alias with Org versions < 7.8.03
  19279. (defalias 'org-on-target-p 'org-at-target-p)
  19280. (defun org-up-heading-all (arg)
  19281. "Move to the heading line of which the present line is a subheading.
  19282. This function considers both visible and invisible heading lines.
  19283. With argument, move up ARG levels."
  19284. (if (fboundp 'outline-up-heading-all)
  19285. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  19286. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  19287. (defun org-up-heading-safe ()
  19288. "Move to the heading line of which the present line is a subheading.
  19289. This version will not throw an error. It will return the level of the
  19290. headline found, or nil if no higher level is found.
  19291. Also, this function will be a lot faster than `outline-up-heading',
  19292. because it relies on stars being the outline starters. This can really
  19293. make a significant difference in outlines with very many siblings."
  19294. (let (start-level re)
  19295. (org-back-to-heading t)
  19296. (setq start-level (funcall outline-level))
  19297. (if (equal start-level 1)
  19298. nil
  19299. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  19300. (if (re-search-backward re nil t)
  19301. (funcall outline-level)))))
  19302. (defun org-first-sibling-p ()
  19303. "Is this heading the first child of its parents?"
  19304. (interactive)
  19305. (let ((re org-outline-regexp-bol)
  19306. level l)
  19307. (unless (org-at-heading-p t)
  19308. (error "Not at a heading"))
  19309. (setq level (funcall outline-level))
  19310. (save-excursion
  19311. (if (not (re-search-backward re nil t))
  19312. t
  19313. (setq l (funcall outline-level))
  19314. (< l level)))))
  19315. (defun org-goto-sibling (&optional previous)
  19316. "Goto the next sibling, even if it is invisible.
  19317. When PREVIOUS is set, go to the previous sibling instead. Returns t
  19318. when a sibling was found. When none is found, return nil and don't
  19319. move point."
  19320. (let ((fun (if previous 're-search-backward 're-search-forward))
  19321. (pos (point))
  19322. (re org-outline-regexp-bol)
  19323. level l)
  19324. (when (condition-case nil (org-back-to-heading t) (error nil))
  19325. (setq level (funcall outline-level))
  19326. (catch 'exit
  19327. (or previous (forward-char 1))
  19328. (while (funcall fun re nil t)
  19329. (setq l (funcall outline-level))
  19330. (when (< l level) (goto-char pos) (throw 'exit nil))
  19331. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  19332. (goto-char pos)
  19333. nil))))
  19334. (defun org-show-siblings ()
  19335. "Show all siblings of the current headline."
  19336. (save-excursion
  19337. (while (org-goto-sibling) (org-flag-heading nil)))
  19338. (save-excursion
  19339. (while (org-goto-sibling 'previous)
  19340. (org-flag-heading nil))))
  19341. (defun org-goto-first-child ()
  19342. "Goto the first child, even if it is invisible.
  19343. Return t when a child was found. Otherwise don't move point and
  19344. return nil."
  19345. (let (level (pos (point)) (re org-outline-regexp-bol))
  19346. (when (condition-case nil (org-back-to-heading t) (error nil))
  19347. (setq level (outline-level))
  19348. (forward-char 1)
  19349. (if (and (re-search-forward re nil t) (> (outline-level) level))
  19350. (progn (goto-char (match-beginning 0)) t)
  19351. (goto-char pos) nil))))
  19352. (defun org-show-hidden-entry ()
  19353. "Show an entry where even the heading is hidden."
  19354. (save-excursion
  19355. (org-show-entry)))
  19356. (defun org-flag-heading (flag &optional entry)
  19357. "Flag the current heading. FLAG non-nil means make invisible.
  19358. When ENTRY is non-nil, show the entire entry."
  19359. (save-excursion
  19360. (org-back-to-heading t)
  19361. ;; Check if we should show the entire entry
  19362. (if entry
  19363. (progn
  19364. (org-show-entry)
  19365. (save-excursion
  19366. (and (outline-next-heading)
  19367. (org-flag-heading nil))))
  19368. (outline-flag-region (max (point-min) (1- (point)))
  19369. (save-excursion (outline-end-of-heading) (point))
  19370. flag))))
  19371. (defun org-get-next-sibling ()
  19372. "Move to next heading of the same level, and return point.
  19373. If there is no such heading, return nil.
  19374. This is like outline-next-sibling, but invisible headings are ok."
  19375. (let ((level (funcall outline-level)))
  19376. (outline-next-heading)
  19377. (while (and (not (eobp)) (> (funcall outline-level) level))
  19378. (outline-next-heading))
  19379. (if (or (eobp) (< (funcall outline-level) level))
  19380. nil
  19381. (point))))
  19382. (defun org-get-last-sibling ()
  19383. "Move to previous heading of the same level, and return point.
  19384. If there is no such heading, return nil."
  19385. (let ((opoint (point))
  19386. (level (funcall outline-level)))
  19387. (outline-previous-heading)
  19388. (when (and (/= (point) opoint) (outline-on-heading-p t))
  19389. (while (and (> (funcall outline-level) level)
  19390. (not (bobp)))
  19391. (outline-previous-heading))
  19392. (if (< (funcall outline-level) level)
  19393. nil
  19394. (point)))))
  19395. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  19396. ;; This contains an exact copy of the original function, but it uses
  19397. ;; `org-back-to-heading', to make it work also in invisible
  19398. ;; trees. And is uses an invisible-OK argument.
  19399. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  19400. ;; Furthermore, when used inside Org, finding the end of a large subtree
  19401. ;; with many children and grandchildren etc, this can be much faster
  19402. ;; than the outline version.
  19403. (org-back-to-heading invisible-OK)
  19404. (let ((first t)
  19405. (level (funcall outline-level)))
  19406. (if (and (eq major-mode 'org-mode) (< level 1000))
  19407. ;; A true heading (not a plain list item), in Org-mode
  19408. ;; This means we can easily find the end by looking
  19409. ;; only for the right number of stars. Using a regexp to do
  19410. ;; this is so much faster than using a Lisp loop.
  19411. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  19412. (forward-char 1)
  19413. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  19414. ;; something else, do it the slow way
  19415. (while (and (not (eobp))
  19416. (or first (> (funcall outline-level) level)))
  19417. (setq first nil)
  19418. (outline-next-heading)))
  19419. (unless to-heading
  19420. (if (memq (preceding-char) '(?\n ?\^M))
  19421. (progn
  19422. ;; Go to end of line before heading
  19423. (forward-char -1)
  19424. (if (memq (preceding-char) '(?\n ?\^M))
  19425. ;; leave blank line before heading
  19426. (forward-char -1))))))
  19427. (point))
  19428. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  19429. "Use Org version in org-mode, for dramatic speed-up."
  19430. (if (eq major-mode 'org-mode)
  19431. (progn
  19432. (org-end-of-subtree nil t)
  19433. (unless (eobp) (backward-char 1)))
  19434. ad-do-it))
  19435. (defun org-end-of-meta-data-and-drawers ()
  19436. "Jump to the first text after meta data and drawers in the current entry.
  19437. This will move over empty lines, lines with planning time stamps,
  19438. clocking lines, and drawers."
  19439. (org-back-to-heading t)
  19440. (let ((end (save-excursion (outline-next-heading) (point)))
  19441. (re (concat "\\(" org-drawer-regexp "\\)"
  19442. "\\|" "[ \t]*" org-keyword-time-regexp)))
  19443. (forward-line 1)
  19444. (while (re-search-forward re end t)
  19445. (if (not (match-end 1))
  19446. ;; empty or planning line
  19447. (forward-line 1)
  19448. ;; a drawer, find the end
  19449. (re-search-forward "^[ \t]*:END:" end 'move)
  19450. (forward-line 1)))
  19451. (and (re-search-forward "[^\n]" nil t) (backward-char 1))
  19452. (point)))
  19453. (defun org-forward-same-level (arg &optional invisible-ok)
  19454. "Move forward to the arg'th subheading at same level as this one.
  19455. Stop at the first and last subheadings of a superior heading.
  19456. Normally this only looks at visible headings, but when INVISIBLE-OK is non-nil
  19457. it wil also look at invisible ones."
  19458. (interactive "p")
  19459. (org-back-to-heading invisible-ok)
  19460. (org-at-heading-p)
  19461. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  19462. (re (format "^\\*\\{1,%d\\} " level))
  19463. l)
  19464. (forward-char 1)
  19465. (while (> arg 0)
  19466. (while (and (re-search-forward re nil 'move)
  19467. (setq l (- (match-end 0) (match-beginning 0) 1))
  19468. (= l level)
  19469. (not invisible-ok)
  19470. (progn (backward-char 1) (outline-invisible-p)))
  19471. (if (< l level) (setq arg 1)))
  19472. (setq arg (1- arg)))
  19473. (beginning-of-line 1)))
  19474. (defun org-backward-same-level (arg &optional invisible-ok)
  19475. "Move backward to the arg'th subheading at same level as this one.
  19476. Stop at the first and last subheadings of a superior heading."
  19477. (interactive "p")
  19478. (org-back-to-heading)
  19479. (org-at-heading-p)
  19480. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  19481. (re (format "^\\*\\{1,%d\\} " level))
  19482. l)
  19483. (while (> arg 0)
  19484. (while (and (re-search-backward re nil 'move)
  19485. (setq l (- (match-end 0) (match-beginning 0) 1))
  19486. (= l level)
  19487. (not invisible-ok)
  19488. (outline-invisible-p))
  19489. (if (< l level) (setq arg 1)))
  19490. (setq arg (1- arg)))))
  19491. (defun org-show-subtree ()
  19492. "Show everything after this heading at deeper levels."
  19493. (interactive)
  19494. (outline-flag-region
  19495. (point)
  19496. (save-excursion
  19497. (org-end-of-subtree t t))
  19498. nil))
  19499. (defun org-show-entry ()
  19500. "Show the body directly following this heading.
  19501. Show the heading too, if it is currently invisible."
  19502. (interactive)
  19503. (save-excursion
  19504. (condition-case nil
  19505. (progn
  19506. (org-back-to-heading t)
  19507. (outline-flag-region
  19508. (max (point-min) (1- (point)))
  19509. (save-excursion
  19510. (if (re-search-forward
  19511. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  19512. (match-beginning 1)
  19513. (point-max)))
  19514. nil)
  19515. (org-cycle-hide-drawers 'children))
  19516. (error nil))))
  19517. (defun org-make-options-regexp (kwds &optional extra)
  19518. "Make a regular expression for keyword lines."
  19519. (concat
  19520. "^"
  19521. "#?[ \t]*\\+\\("
  19522. (mapconcat 'regexp-quote kwds "\\|")
  19523. (if extra (concat "\\|" extra))
  19524. "\\):[ \t]*"
  19525. "\\(.*\\)"))
  19526. ;; Make isearch reveal the necessary context
  19527. (defun org-isearch-end ()
  19528. "Reveal context after isearch exits."
  19529. (when isearch-success ; only if search was successful
  19530. (if (featurep 'xemacs)
  19531. ;; Under XEmacs, the hook is run in the correct place,
  19532. ;; we directly show the context.
  19533. (org-show-context 'isearch)
  19534. ;; In Emacs the hook runs *before* restoring the overlays.
  19535. ;; So we have to use a one-time post-command-hook to do this.
  19536. ;; (Emacs 22 has a special variable, see function `org-mode')
  19537. (unless (and (boundp 'isearch-mode-end-hook-quit)
  19538. isearch-mode-end-hook-quit)
  19539. ;; Only when the isearch was not quitted.
  19540. (org-add-hook 'post-command-hook 'org-isearch-post-command
  19541. 'append 'local)))))
  19542. (defun org-isearch-post-command ()
  19543. "Remove self from hook, and show context."
  19544. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  19545. (org-show-context 'isearch))
  19546. ;;;; Integration with and fixes for other packages
  19547. ;;; Imenu support
  19548. (defvar org-imenu-markers nil
  19549. "All markers currently used by Imenu.")
  19550. (make-variable-buffer-local 'org-imenu-markers)
  19551. (defun org-imenu-new-marker (&optional pos)
  19552. "Return a new marker for use by Imenu, and remember the marker."
  19553. (let ((m (make-marker)))
  19554. (move-marker m (or pos (point)))
  19555. (push m org-imenu-markers)
  19556. m))
  19557. (defun org-imenu-get-tree ()
  19558. "Produce the index for Imenu."
  19559. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  19560. (setq org-imenu-markers nil)
  19561. (let* ((n org-imenu-depth)
  19562. (re (concat "^" (org-get-limited-outline-regexp)))
  19563. (subs (make-vector (1+ n) nil))
  19564. (last-level 0)
  19565. m level head)
  19566. (save-excursion
  19567. (save-restriction
  19568. (widen)
  19569. (goto-char (point-max))
  19570. (while (re-search-backward re nil t)
  19571. (setq level (org-reduced-level (funcall outline-level)))
  19572. (when (and (<= level n)
  19573. (looking-at org-complex-heading-regexp))
  19574. (setq head (org-link-display-format
  19575. (org-match-string-no-properties 4))
  19576. m (org-imenu-new-marker))
  19577. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  19578. (if (>= level last-level)
  19579. (push (cons head m) (aref subs level))
  19580. (push (cons head (aref subs (1+ level))) (aref subs level))
  19581. (loop for i from (1+ level) to n do (aset subs i nil)))
  19582. (setq last-level level)))))
  19583. (aref subs 1)))
  19584. (eval-after-load "imenu"
  19585. '(progn
  19586. (add-hook 'imenu-after-jump-hook
  19587. (lambda ()
  19588. (if (eq major-mode 'org-mode)
  19589. (org-show-context 'org-goto))))))
  19590. (defun org-link-display-format (link)
  19591. "Replace a link with either the description, or the link target
  19592. if no description is present"
  19593. (save-match-data
  19594. (if (string-match org-bracket-link-analytic-regexp link)
  19595. (replace-match (if (match-end 5)
  19596. (match-string 5 link)
  19597. (concat (match-string 1 link)
  19598. (match-string 3 link)))
  19599. nil t link)
  19600. link)))
  19601. (defun org-toggle-link-display ()
  19602. "Toggle the literal or descriptive display of links."
  19603. (interactive)
  19604. (if org-descriptive-links
  19605. (progn (org-remove-from-invisibility-spec '(org-link))
  19606. (org-restart-font-lock)
  19607. (setq org-descriptive-links nil))
  19608. (progn (add-to-invisibility-spec '(org-link))
  19609. (org-restart-font-lock)
  19610. (setq org-descriptive-links t))))
  19611. ;; Speedbar support
  19612. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  19613. "Overlay marking the agenda restriction line in speedbar.")
  19614. (overlay-put org-speedbar-restriction-lock-overlay
  19615. 'face 'org-agenda-restriction-lock)
  19616. (overlay-put org-speedbar-restriction-lock-overlay
  19617. 'help-echo "Agendas are currently limited to this item.")
  19618. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  19619. (defun org-speedbar-set-agenda-restriction ()
  19620. "Restrict future agenda commands to the location at point in speedbar.
  19621. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  19622. (interactive)
  19623. (require 'org-agenda)
  19624. (let (p m tp np dir txt)
  19625. (cond
  19626. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  19627. 'org-imenu t))
  19628. (setq m (get-text-property p 'org-imenu-marker))
  19629. (with-current-buffer (marker-buffer m)
  19630. (goto-char m)
  19631. (org-agenda-set-restriction-lock 'subtree)))
  19632. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  19633. 'speedbar-function 'speedbar-find-file))
  19634. (setq tp (previous-single-property-change
  19635. (1+ p) 'speedbar-function)
  19636. np (next-single-property-change
  19637. tp 'speedbar-function)
  19638. dir (speedbar-line-directory)
  19639. txt (buffer-substring-no-properties (or tp (point-min))
  19640. (or np (point-max))))
  19641. (with-current-buffer (find-file-noselect
  19642. (let ((default-directory dir))
  19643. (expand-file-name txt)))
  19644. (unless (eq major-mode 'org-mode)
  19645. (error "Cannot restrict to non-Org-mode file"))
  19646. (org-agenda-set-restriction-lock 'file)))
  19647. (t (error "Don't know how to restrict Org-mode's agenda")))
  19648. (move-overlay org-speedbar-restriction-lock-overlay
  19649. (point-at-bol) (point-at-eol))
  19650. (setq current-prefix-arg nil)
  19651. (org-agenda-maybe-redo)))
  19652. (eval-after-load "speedbar"
  19653. '(progn
  19654. (speedbar-add-supported-extension ".org")
  19655. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  19656. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  19657. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  19658. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  19659. (add-hook 'speedbar-visiting-tag-hook
  19660. (lambda () (and (eq major-mode 'org-mode) (org-show-context 'org-goto))))))
  19661. ;;; Fixes and Hacks for problems with other packages
  19662. ;; Make flyspell not check words in links, to not mess up our keymap
  19663. (defun org-mode-flyspell-verify ()
  19664. "Don't let flyspell put overlays at active buttons, or on
  19665. {todo,all-time,additional-option-like}-keywords."
  19666. (let ((pos (max (1- (point)) (point-min)))
  19667. (word (thing-at-point 'word)))
  19668. (and (not (get-text-property pos 'keymap))
  19669. (not (get-text-property pos 'org-no-flyspell))
  19670. (not (member word org-todo-keywords-1))
  19671. (not (member word org-all-time-keywords))
  19672. (not (member word org-additional-option-like-keywords)))))
  19673. (defun org-remove-flyspell-overlays-in (beg end)
  19674. "Remove flyspell overlays in region."
  19675. (and (org-bound-and-true-p flyspell-mode)
  19676. (fboundp 'flyspell-delete-region-overlays)
  19677. (flyspell-delete-region-overlays beg end))
  19678. (add-text-properties beg end '(org-no-flyspell t)))
  19679. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  19680. (eval-after-load "bookmark"
  19681. '(if (boundp 'bookmark-after-jump-hook)
  19682. ;; We can use the hook
  19683. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  19684. ;; Hook not available, use advice
  19685. (defadvice bookmark-jump (after org-make-visible activate)
  19686. "Make the position visible."
  19687. (org-bookmark-jump-unhide))))
  19688. ;; Make sure saveplace shows the location if it was hidden
  19689. (eval-after-load "saveplace"
  19690. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  19691. "Make the position visible."
  19692. (org-bookmark-jump-unhide)))
  19693. ;; Make sure ecb shows the location if it was hidden
  19694. (eval-after-load "ecb"
  19695. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  19696. "Make hierarchy visible when jumping into location from ECB tree buffer."
  19697. (if (eq major-mode 'org-mode)
  19698. (org-show-context))))
  19699. (defun org-bookmark-jump-unhide ()
  19700. "Unhide the current position, to show the bookmark location."
  19701. (and (eq major-mode 'org-mode)
  19702. (or (outline-invisible-p)
  19703. (save-excursion (goto-char (max (point-min) (1- (point))))
  19704. (outline-invisible-p)))
  19705. (org-show-context 'bookmark-jump)))
  19706. ;; Make session.el ignore our circular variable
  19707. (eval-after-load "session"
  19708. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  19709. ;;;; Experimental code
  19710. (defun org-closed-in-range ()
  19711. "Sparse tree of items closed in a certain time range.
  19712. Still experimental, may disappear in the future."
  19713. (interactive)
  19714. ;; Get the time interval from the user.
  19715. (let* ((time1 (org-float-time
  19716. (org-read-date nil 'to-time nil "Starting date: ")))
  19717. (time2 (org-float-time
  19718. (org-read-date nil 'to-time nil "End date:")))
  19719. ;; callback function
  19720. (callback (lambda ()
  19721. (let ((time
  19722. (org-float-time
  19723. (apply 'encode-time
  19724. (org-parse-time-string
  19725. (match-string 1))))))
  19726. ;; check if time in interval
  19727. (and (>= time time1) (<= time time2))))))
  19728. ;; make tree, check each match with the callback
  19729. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  19730. ;;;; Finish up
  19731. (provide 'org)
  19732. (run-hooks 'org-load-hook)
  19733. ;;; org.el ends here