org.el 815 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680
  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. ;;
  10. ;; This file is part of GNU Emacs.
  11. ;;
  12. ;; GNU Emacs is free software: you can redistribute it and/or modify
  13. ;; it under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation, either version 3 of the License, or
  15. ;; (at your option) any later version.
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;; GNU General Public License for more details.
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  22. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  23. ;;
  24. ;;; Commentary:
  25. ;;
  26. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  27. ;; project planning with a fast and effective plain-text system.
  28. ;;
  29. ;; Org-mode develops organizational tasks around NOTES files that contain
  30. ;; information about projects as plain text. Org-mode is implemented on
  31. ;; top of outline-mode, which makes it possible to keep the content of
  32. ;; large files well structured. Visibility cycling and structure editing
  33. ;; help to work with the tree. Tables are easily created with a built-in
  34. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  35. ;; and scheduling. It dynamically compiles entries into an agenda that
  36. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  37. ;; Plain text URL-like links connect to websites, emails, Usenet
  38. ;; messages, BBDB entries, and any files related to the projects. For
  39. ;; printing and sharing of notes, an Org-mode file can be exported as a
  40. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  41. ;; iCalendar file. It can also serve as a publishing tool for a set of
  42. ;; linked webpages.
  43. ;;
  44. ;; Installation and Activation
  45. ;; ---------------------------
  46. ;; See the corresponding sections in the manual at
  47. ;;
  48. ;; http://orgmode.org/org.html#Installation
  49. ;;
  50. ;; Documentation
  51. ;; -------------
  52. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  53. ;; distribution also contains a PDF version of it. At the homepage of
  54. ;; Org-mode, you can read the same text online as HTML. There is also an
  55. ;; excellent reference card made by Philip Rooke. This card can be found
  56. ;; in the etc/ directory of Emacs 22.
  57. ;;
  58. ;; A list of recent changes can be found at
  59. ;; http://orgmode.org/Changes.html
  60. ;;
  61. ;;; Code:
  62. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  63. (defvar org-table-formula-constants-local nil
  64. "Local version of `org-table-formula-constants'.")
  65. (make-variable-buffer-local 'org-table-formula-constants-local)
  66. ;;;; Require other packages
  67. (eval-when-compile
  68. (require 'cl)
  69. (require 'gnus-sum))
  70. (require 'calendar)
  71. (require 'find-func)
  72. (require 'format-spec)
  73. ;; 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. (defvaralias 'org-version 'org-release)
  196. ;;;###autoload
  197. (defun org-version (&optional here)
  198. "Show the org-mode version in the echo area.
  199. With prefix arg HERE, insert it at point."
  200. (interactive "P")
  201. (let* ((origin default-directory)
  202. (version (if (boundp 'org-release) org-release "N/A"))
  203. (git-version (if (boundp 'org-git-version) org-git-version "N/A"))
  204. (org-install (ignore-errors (find-library-name "org-install")))
  205. (version_ (format "Org-mode version %s (%s @ %s)"
  206. version
  207. git-version
  208. (if org-install org-install "org-install.el can not be found!"))))
  209. (if (org-called-interactively-p 'interactive)
  210. (if here (insert version_)
  211. (message version_))
  212. version)))
  213. ;;; Compatibility constants
  214. ;;; The custom variables
  215. (defgroup org nil
  216. "Outline-based notes management and organizer."
  217. :tag "Org"
  218. :group 'outlines
  219. :group 'calendar)
  220. (defcustom org-mode-hook nil
  221. "Mode hook for Org-mode, run after the mode was turned on."
  222. :group 'org
  223. :type 'hook)
  224. (defcustom org-load-hook nil
  225. "Hook that is run after org.el has been loaded."
  226. :group 'org
  227. :type 'hook)
  228. (defcustom org-log-buffer-setup-hook nil
  229. "Hook that is run after an Org log buffer is created."
  230. :group 'org
  231. :version "24.1"
  232. :type 'hook)
  233. (defvar org-modules) ; defined below
  234. (defvar org-modules-loaded nil
  235. "Have the modules been loaded already?")
  236. (defun org-load-modules-maybe (&optional force)
  237. "Load all extensions listed in `org-modules'."
  238. (when (or force (not org-modules-loaded))
  239. (mapc (lambda (ext)
  240. (condition-case nil (require ext)
  241. (error (message "Problems while trying to load feature `%s'" ext))))
  242. org-modules)
  243. (setq org-modules-loaded t)))
  244. (defun org-set-modules (var value)
  245. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  246. (set var value)
  247. (when (featurep 'org)
  248. (org-load-modules-maybe 'force)))
  249. (when (org-bound-and-true-p org-modules)
  250. (let ((a (member 'org-infojs org-modules)))
  251. (and a (setcar a 'org-jsinfo))))
  252. (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)
  253. "Modules that should always be loaded together with org.el.
  254. If a description starts with <C>, the file is not part of Emacs
  255. and loading it will require that you have downloaded and properly installed
  256. the org-mode distribution.
  257. You can also use this system to load external packages (i.e. neither Org
  258. core modules, nor modules from the CONTRIB directory). Just add symbols
  259. to the end of the list. If the package is called org-xyz.el, then you need
  260. to add the symbol `xyz', and the package must have a call to
  261. (provide 'org-xyz)"
  262. :group 'org
  263. :set 'org-set-modules
  264. :type
  265. '(set :greedy t
  266. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  267. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  268. (const :tag " crypt: Encryption of subtrees" org-crypt)
  269. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  270. (const :tag " docview: Links to doc-view buffers" org-docview)
  271. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  272. (const :tag " id: Global IDs for identifying entries" org-id)
  273. (const :tag " info: Links to Info nodes" org-info)
  274. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  275. (const :tag " habit: Track your consistency with habits" org-habit)
  276. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  277. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  278. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  279. (const :tag " mew Links to Mew folders/messages" org-mew)
  280. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  281. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  282. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  283. (const :tag " special-blocks: Turn blocks into LaTeX envs and HTML divs" org-special-blocks)
  284. (const :tag " vm: Links to VM folders/messages" org-vm)
  285. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  286. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  287. (const :tag " mouse: Additional mouse support" org-mouse)
  288. (const :tag " TaskJuggler: Export tasks to a TaskJuggler project" org-taskjuggler)
  289. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  290. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  291. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  292. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  293. (const :tag "C collector: Collect properties into tables" org-collector)
  294. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  295. (const :tag "C drill: Flashcards and spaced repetition for Org-mode" org-drill)
  296. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  297. (const :tag "C eshell Support for links to working directories in eshell" org-eshell)
  298. (const :tag "C eval: Include command output as text" org-eval)
  299. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  300. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  301. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  302. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  303. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  304. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  305. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  306. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  307. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  308. (const :tag "C notmuch: Provide org links to notmuch searches or messages" org-notmuch)
  309. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  310. (const :tag "C mac-link-grabber Grab links and URLs from various Mac applications" org-mac-link-grabber)
  311. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  312. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  313. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  314. (const :tag "C registry: A registry for Org-mode links" org-registry)
  315. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  316. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  317. (const :tag "C secretary: Team management with org-mode" org-secretary)
  318. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  319. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  320. (const :tag "C track: Keep up with Org-mode development" org-track)
  321. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  322. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  323. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  324. (defcustom org-support-shift-select nil
  325. "Non-nil means make shift-cursor commands select text when possible.
  326. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
  327. start selecting a region, or enlarge regions started in this way.
  328. In Org-mode, in special contexts, these same keys are used for
  329. other purposes, important enough to compete with shift selection.
  330. Org tries to balance these needs by supporting `shift-select-mode'
  331. outside these special contexts, under control of this variable.
  332. The default of this variable is nil, to avoid confusing behavior. Shifted
  333. cursor keys will then execute Org commands in the following contexts:
  334. - on a headline, changing TODO state (left/right) and priority (up/down)
  335. - on a time stamp, changing the time
  336. - in a plain list item, changing the bullet type
  337. - in a property definition line, switching between allowed values
  338. - in the BEGIN line of a clock table (changing the time block).
  339. Outside these contexts, the commands will throw an error.
  340. When this variable is t and the cursor is not in a special
  341. context, Org-mode will support shift-selection for making and
  342. enlarging regions. To make this more effective, the bullet
  343. cycling will no longer happen anywhere in an item line, but only
  344. if the cursor is exactly on the bullet.
  345. If you set this variable to the symbol `always', then the keys
  346. will not be special in headlines, property lines, and item lines,
  347. to make shift selection work there as well. If this is what you
  348. want, you can use the following alternative commands: `C-c C-t'
  349. and `C-c ,' to change TODO state and priority, `C-u C-u C-c C-t'
  350. can be used to switch TODO sets, `C-c -' to cycle item bullet
  351. types, and properties can be edited by hand or in column view.
  352. However, when the cursor is on a timestamp, shift-cursor commands
  353. will still edit the time stamp - this is just too good to give up.
  354. XEmacs user should have this variable set to nil, because
  355. `shift-select-mode' is in Emacs 23 or later only."
  356. :group 'org
  357. :type '(choice
  358. (const :tag "Never" nil)
  359. (const :tag "When outside special context" t)
  360. (const :tag "Everywhere except timestamps" always)))
  361. (defcustom org-loop-over-headlines-in-active-region nil
  362. "Shall some commands act upon headlines in the active region?
  363. When set to `t', some commands will be performed in all headlines
  364. within the active region.
  365. When set to `start-level', some commands will be performed in all
  366. headlines within the active region, provided that these headlines
  367. are of the same level than the first one.
  368. When set to a string, those commands will be performed on the
  369. matching headlines within the active region. Such string must be
  370. a tags/property/todo match as it is used in the agenda tags view.
  371. The list of commands is: `org-schedule', `org-deadline',
  372. `org-todo', `org-archive-subtree', `org-archive-set-tag' and
  373. `org-archive-to-archive-sibling'. The archiving commands skip
  374. already archived entries."
  375. :type '(choice (const :tag "Don't loop" nil)
  376. (const :tag "All headlines in active region" t)
  377. (const :tag "In active region, headlines at the same level than the first one" 'start-level)
  378. (string :tag "Tags/Property/Todo matcher"))
  379. :version "24.1"
  380. :group 'org-todo
  381. :group 'org-archive)
  382. (defgroup org-startup nil
  383. "Options concerning startup of Org-mode."
  384. :tag "Org Startup"
  385. :group 'org)
  386. (defcustom org-startup-folded t
  387. "Non-nil means entering Org-mode will switch to OVERVIEW.
  388. This can also be configured on a per-file basis by adding one of
  389. the following lines anywhere in the buffer:
  390. #+STARTUP: fold (or `overview', this is equivalent)
  391. #+STARTUP: nofold (or `showall', this is equivalent)
  392. #+STARTUP: content
  393. #+STARTUP: showeverything"
  394. :group 'org-startup
  395. :type '(choice
  396. (const :tag "nofold: show all" nil)
  397. (const :tag "fold: overview" t)
  398. (const :tag "content: all headlines" content)
  399. (const :tag "show everything, even drawers" showeverything)))
  400. (defcustom org-startup-truncated t
  401. "Non-nil means entering Org-mode will set `truncate-lines'.
  402. This is useful since some lines containing links can be very long and
  403. uninteresting. Also tables look terrible when wrapped."
  404. :group 'org-startup
  405. :type 'boolean)
  406. (defcustom org-startup-indented nil
  407. "Non-nil means turn on `org-indent-mode' on startup.
  408. This can also be configured on a per-file basis by adding one of
  409. the following lines anywhere in the buffer:
  410. #+STARTUP: indent
  411. #+STARTUP: noindent"
  412. :group 'org-structure
  413. :type '(choice
  414. (const :tag "Not" nil)
  415. (const :tag "Globally (slow on startup in large files)" t)))
  416. (defcustom org-use-sub-superscripts t
  417. "Non-nil means interpret \"_\" and \"^\" for export.
  418. When this option is turned on, you can use TeX-like syntax for sub- and
  419. superscripts. Several characters after \"_\" or \"^\" will be
  420. considered as a single item - so grouping with {} is normally not
  421. needed. For example, the following things will be parsed as single
  422. sub- or superscripts.
  423. 10^24 or 10^tau several digits will be considered 1 item.
  424. 10^-12 or 10^-tau a leading sign with digits or a word
  425. x^2-y^3 will be read as x^2 - y^3, because items are
  426. terminated by almost any nonword/nondigit char.
  427. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  428. Still, ambiguity is possible - so when in doubt use {} to enclose the
  429. sub/superscript. If you set this variable to the symbol `{}',
  430. the braces are *required* in order to trigger interpretations as
  431. sub/superscript. This can be helpful in documents that need \"_\"
  432. frequently in plain text.
  433. Not all export backends support this, but HTML does.
  434. This option can also be set with the +OPTIONS line, e.g. \"^:nil\"."
  435. :group 'org-startup
  436. :group 'org-export-translation
  437. :version "24.1"
  438. :type '(choice
  439. (const :tag "Always interpret" t)
  440. (const :tag "Only with braces" {})
  441. (const :tag "Never interpret" nil)))
  442. (if (fboundp 'defvaralias)
  443. (defvaralias 'org-export-with-sub-superscripts 'org-use-sub-superscripts))
  444. (defcustom org-startup-with-beamer-mode nil
  445. "Non-nil means turn on `org-beamer-mode' on startup.
  446. This can also be configured on a per-file basis by adding one of
  447. the following lines anywhere in the buffer:
  448. #+STARTUP: beamer"
  449. :group 'org-startup
  450. :version "24.1"
  451. :type 'boolean)
  452. (defcustom org-startup-align-all-tables nil
  453. "Non-nil means align all tables when visiting a file.
  454. This is useful when the column width in tables is forced with <N> cookies
  455. in table fields. Such tables will look correct only after the first re-align.
  456. This can also be configured on a per-file basis by adding one of
  457. the following lines anywhere in the buffer:
  458. #+STARTUP: align
  459. #+STARTUP: noalign"
  460. :group 'org-startup
  461. :type 'boolean)
  462. (defcustom org-startup-with-inline-images nil
  463. "Non-nil means show inline images when loading a new Org file.
  464. This can also be configured on a per-file basis by adding one of
  465. the following lines anywhere in the buffer:
  466. #+STARTUP: inlineimages
  467. #+STARTUP: noinlineimages"
  468. :group 'org-startup
  469. :version "24.1"
  470. :type 'boolean)
  471. (defcustom org-insert-mode-line-in-empty-file nil
  472. "Non-nil means insert the first line setting Org-mode in empty files.
  473. When the function `org-mode' is called interactively in an empty file, this
  474. normally means that the file name does not automatically trigger Org-mode.
  475. To ensure that the file will always be in Org-mode in the future, a
  476. line enforcing Org-mode will be inserted into the buffer, if this option
  477. has been set."
  478. :group 'org-startup
  479. :type 'boolean)
  480. (defcustom org-replace-disputed-keys nil
  481. "Non-nil means use alternative key bindings for some keys.
  482. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  483. These keys are also used by other packages like shift-selection-mode'
  484. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  485. If you want to use Org-mode together with one of these other modes,
  486. or more generally if you would like to move some Org-mode commands to
  487. other keys, set this variable and configure the keys with the variable
  488. `org-disputed-keys'.
  489. This option is only relevant at load-time of Org-mode, and must be set
  490. *before* org.el is loaded. Changing it requires a restart of Emacs to
  491. become effective."
  492. :group 'org-startup
  493. :type 'boolean)
  494. (defcustom org-use-extra-keys nil
  495. "Non-nil means use extra key sequence definitions for certain commands.
  496. This happens automatically if you run XEmacs or if `window-system'
  497. is nil. This variable lets you do the same manually. You must
  498. set it before loading org.
  499. Example: on Carbon Emacs 22 running graphically, with an external
  500. keyboard on a Powerbook, the default way of setting M-left might
  501. not work for either Alt or ESC. Setting this variable will make
  502. it work for ESC."
  503. :group 'org-startup
  504. :type 'boolean)
  505. (if (fboundp 'defvaralias)
  506. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  507. (defcustom org-disputed-keys
  508. '(([(shift up)] . [(meta p)])
  509. ([(shift down)] . [(meta n)])
  510. ([(shift left)] . [(meta -)])
  511. ([(shift right)] . [(meta +)])
  512. ([(control shift right)] . [(meta shift +)])
  513. ([(control shift left)] . [(meta shift -)]))
  514. "Keys for which Org-mode and other modes compete.
  515. This is an alist, cars are the default keys, second element specifies
  516. the alternative to use when `org-replace-disputed-keys' is t.
  517. Keys can be specified in any syntax supported by `define-key'.
  518. The value of this option takes effect only at Org-mode's startup,
  519. therefore you'll have to restart Emacs to apply it after changing."
  520. :group 'org-startup
  521. :type 'alist)
  522. (defun org-key (key)
  523. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  524. Or return the original if not disputed.
  525. Also apply the translations defined in `org-xemacs-key-equivalents'."
  526. (when org-replace-disputed-keys
  527. (let* ((nkey (key-description key))
  528. (x (org-find-if (lambda (x)
  529. (equal (key-description (car x)) nkey))
  530. org-disputed-keys)))
  531. (setq key (if x (cdr x) key))))
  532. (when (featurep 'xemacs)
  533. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  534. key)
  535. (defun org-find-if (predicate seq)
  536. (catch 'exit
  537. (while seq
  538. (if (funcall predicate (car seq))
  539. (throw 'exit (car seq))
  540. (pop seq)))))
  541. (defun org-defkey (keymap key def)
  542. "Define a key, possibly translated, as returned by `org-key'."
  543. (define-key keymap (org-key key) def))
  544. (defcustom org-ellipsis nil
  545. "The ellipsis to use in the Org-mode outline.
  546. When nil, just use the standard three dots. When a string, use that instead,
  547. When a face, use the standard 3 dots, but with the specified face.
  548. The change affects only Org-mode (which will then use its own display table).
  549. Changing this requires executing `M-x org-mode' in a buffer to become
  550. effective."
  551. :group 'org-startup
  552. :type '(choice (const :tag "Default" nil)
  553. (face :tag "Face" :value org-warning)
  554. (string :tag "String" :value "...#")))
  555. (defvar org-display-table nil
  556. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  557. (defgroup org-keywords nil
  558. "Keywords in Org-mode."
  559. :tag "Org Keywords"
  560. :group 'org)
  561. (defcustom org-deadline-string "DEADLINE:"
  562. "String to mark deadline entries.
  563. A deadline is this string, followed by a time stamp. Should be a word,
  564. terminated by a colon. You can insert a schedule keyword and
  565. a timestamp with \\[org-deadline].
  566. Changes become only effective after restarting Emacs."
  567. :group 'org-keywords
  568. :type 'string)
  569. (defcustom org-scheduled-string "SCHEDULED:"
  570. "String to mark scheduled TODO entries.
  571. A schedule is this string, followed by a time stamp. Should be a word,
  572. terminated by a colon. You can insert a schedule keyword and
  573. a timestamp with \\[org-schedule].
  574. Changes become only effective after restarting Emacs."
  575. :group 'org-keywords
  576. :type 'string)
  577. (defcustom org-closed-string "CLOSED:"
  578. "String used as the prefix for timestamps logging closing a TODO entry."
  579. :group 'org-keywords
  580. :type 'string)
  581. (defcustom org-clock-string "CLOCK:"
  582. "String used as prefix for timestamps clocking work hours on an item."
  583. :group 'org-keywords
  584. :type 'string)
  585. (defcustom org-comment-string "COMMENT"
  586. "Entries starting with this keyword will never be exported.
  587. An entry can be toggled between COMMENT and normal with
  588. \\[org-toggle-comment].
  589. Changes become only effective after restarting Emacs."
  590. :group 'org-keywords
  591. :type 'string)
  592. (defcustom org-quote-string "QUOTE"
  593. "Entries starting with this keyword will be exported in fixed-width font.
  594. Quoting applies only to the text in the entry following the headline, and does
  595. not extend beyond the next headline, even if that is lower level.
  596. An entry can be toggled between QUOTE and normal with
  597. \\[org-toggle-fixed-width-section]."
  598. :group 'org-keywords
  599. :type 'string)
  600. (defconst org-repeat-re
  601. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)"
  602. "Regular expression for specifying repeated events.
  603. After a match, group 1 contains the repeat expression.")
  604. (defgroup org-structure nil
  605. "Options concerning the general structure of Org-mode files."
  606. :tag "Org Structure"
  607. :group 'org)
  608. (defgroup org-reveal-location nil
  609. "Options about how to make context of a location visible."
  610. :tag "Org Reveal Location"
  611. :group 'org-structure)
  612. (defconst org-context-choice
  613. '(choice
  614. (const :tag "Always" t)
  615. (const :tag "Never" nil)
  616. (repeat :greedy t :tag "Individual contexts"
  617. (cons
  618. (choice :tag "Context"
  619. (const agenda)
  620. (const org-goto)
  621. (const occur-tree)
  622. (const tags-tree)
  623. (const link-search)
  624. (const mark-goto)
  625. (const bookmark-jump)
  626. (const isearch)
  627. (const default))
  628. (boolean))))
  629. "Contexts for the reveal options.")
  630. (defcustom org-show-hierarchy-above '((default . t))
  631. "Non-nil means show full hierarchy when revealing a location.
  632. Org-mode often shows locations in an org-mode file which might have
  633. been invisible before. When this is set, the hierarchy of headings
  634. above the exposed location is shown.
  635. Turning this off for example for sparse trees makes them very compact.
  636. Instead of t, this can also be an alist specifying this option for different
  637. contexts. Valid contexts are
  638. agenda when exposing an entry from the agenda
  639. org-goto when using the command `org-goto' on key C-c C-j
  640. occur-tree when using the command `org-occur' on key C-c /
  641. tags-tree when constructing a sparse tree based on tags matches
  642. link-search when exposing search matches associated with a link
  643. mark-goto when exposing the jump goal of a mark
  644. bookmark-jump when exposing a bookmark location
  645. isearch when exiting from an incremental search
  646. default default for all contexts not set explicitly"
  647. :group 'org-reveal-location
  648. :type org-context-choice)
  649. (defcustom org-show-following-heading '((default . nil))
  650. "Non-nil means show following heading when revealing a location.
  651. Org-mode often shows locations in an org-mode file which might have
  652. been invisible before. When this is set, the heading following the
  653. match is shown.
  654. Turning this off for example for sparse trees makes them very compact,
  655. but makes it harder to edit the location of the match. In such a case,
  656. use the command \\[org-reveal] to show more context.
  657. Instead of t, this can also be an alist specifying this option for different
  658. contexts. See `org-show-hierarchy-above' for valid contexts."
  659. :group 'org-reveal-location
  660. :type org-context-choice)
  661. (defcustom org-show-siblings '((default . nil) (isearch t))
  662. "Non-nil means show all sibling heading when revealing a location.
  663. Org-mode often shows locations in an org-mode file which might have
  664. been invisible before. When this is set, the sibling of the current entry
  665. heading are all made visible. If `org-show-hierarchy-above' is t,
  666. the same happens on each level of the hierarchy above the current entry.
  667. By default this is on for the isearch context, off for all other contexts.
  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-entry-below '((default . nil))
  676. "Non-nil means show the entry below a headline 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 text below the headline that is
  679. exposed is also shown.
  680. By default this is off for all contexts.
  681. Instead of t, this can also be an alist specifying this option for different
  682. contexts. See `org-show-hierarchy-above' for valid contexts."
  683. :group 'org-reveal-location
  684. :type org-context-choice)
  685. (defcustom org-indirect-buffer-display 'other-window
  686. "How should indirect tree buffers be displayed?
  687. This applies to indirect buffers created with the commands
  688. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  689. Valid values are:
  690. current-window Display in the current window
  691. other-window Just display in another window.
  692. dedicated-frame Create one new frame, and re-use it each time.
  693. new-frame Make a new frame each time. Note that in this case
  694. previously-made indirect buffers are kept, and you need to
  695. kill these buffers yourself."
  696. :group 'org-structure
  697. :group 'org-agenda-windows
  698. :type '(choice
  699. (const :tag "In current window" current-window)
  700. (const :tag "In current frame, other window" other-window)
  701. (const :tag "Each time a new frame" new-frame)
  702. (const :tag "One dedicated frame" dedicated-frame)))
  703. (defcustom org-use-speed-commands nil
  704. "Non-nil means activate single letter commands at beginning of a headline.
  705. This may also be a function to test for appropriate locations where speed
  706. commands should be active."
  707. :group 'org-structure
  708. :type '(choice
  709. (const :tag "Never" nil)
  710. (const :tag "At beginning of headline stars" t)
  711. (function)))
  712. (defcustom org-speed-commands-user nil
  713. "Alist of additional speed commands.
  714. This list will be checked before `org-speed-commands-default'
  715. when the variable `org-use-speed-commands' is non-nil
  716. and when the cursor is at the beginning of a headline.
  717. The car if each entry is a string with a single letter, which must
  718. be assigned to `self-insert-command' in the global map.
  719. The cdr is either a command to be called interactively, a function
  720. to be called, or a form to be evaluated.
  721. An entry that is just a list with a single string will be interpreted
  722. as a descriptive headline that will be added when listing the speed
  723. commands in the Help buffer using the `?' speed command."
  724. :group 'org-structure
  725. :type '(repeat :value ("k" . ignore)
  726. (choice :value ("k" . ignore)
  727. (list :tag "Descriptive Headline" (string :tag "Headline"))
  728. (cons :tag "Letter and Command"
  729. (string :tag "Command letter")
  730. (choice
  731. (function)
  732. (sexp))))))
  733. (defgroup org-cycle nil
  734. "Options concerning visibility cycling in Org-mode."
  735. :tag "Org Cycle"
  736. :group 'org-structure)
  737. (defcustom org-cycle-skip-children-state-if-no-children t
  738. "Non-nil means skip CHILDREN state in entries that don't have any."
  739. :group 'org-cycle
  740. :type 'boolean)
  741. (defcustom org-cycle-max-level nil
  742. "Maximum level which should still be subject to visibility cycling.
  743. Levels higher than this will, for cycling, be treated as text, not a headline.
  744. When `org-odd-levels-only' is set, a value of N in this variable actually
  745. means 2N-1 stars as the limiting headline.
  746. When nil, cycle all levels.
  747. Note that the limiting level of cycling is also influenced by
  748. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  749. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  750. than its value."
  751. :group 'org-cycle
  752. :type '(choice
  753. (const :tag "No limit" nil)
  754. (integer :tag "Maximum level")))
  755. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK" "RESULTS")
  756. "Names of drawers. Drawers are not opened by cycling on the headline above.
  757. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  758. this:
  759. :DRAWERNAME:
  760. .....
  761. :END:
  762. The drawer \"PROPERTIES\" is special for capturing properties through
  763. the property API.
  764. Drawers can be defined on the per-file basis with a line like:
  765. #+DRAWERS: HIDDEN STATE PROPERTIES"
  766. :group 'org-structure
  767. :group 'org-cycle
  768. :type '(repeat (string :tag "Drawer Name")))
  769. (defcustom org-hide-block-startup nil
  770. "Non-nil means entering Org-mode will fold all blocks.
  771. This can also be set in on a per-file basis with
  772. #+STARTUP: hideblocks
  773. #+STARTUP: showblocks"
  774. :group 'org-startup
  775. :group 'org-cycle
  776. :type 'boolean)
  777. (defcustom org-cycle-global-at-bob nil
  778. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  779. This makes it possible to do global cycling without having to use S-TAB or
  780. \\[universal-argument] TAB. For this special case to work, the first line \
  781. of the buffer
  782. must not be a headline - it may be empty or some other text. When used in
  783. this way, `org-cycle-hook' is disables temporarily, to make sure the
  784. cursor stays at the beginning of the buffer.
  785. When this option is nil, don't do anything special at the beginning
  786. of the buffer."
  787. :group 'org-cycle
  788. :type 'boolean)
  789. (defcustom org-cycle-level-after-item/entry-creation t
  790. "Non-nil means cycle entry level or item indentation in new empty entries.
  791. When the cursor is at the end of an empty headline, i.e with only stars
  792. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  793. and then all possible ancestor states, before returning to the original state.
  794. This makes data entry extremely fast: M-RET to create a new headline,
  795. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  796. When the cursor is at the end of an empty plain list item, one TAB will
  797. make it a subitem, two or more tabs will back up to make this an item
  798. higher up in the item hierarchy."
  799. :group 'org-cycle
  800. :type 'boolean)
  801. (defcustom org-cycle-emulate-tab t
  802. "Where should `org-cycle' emulate TAB.
  803. nil Never
  804. white Only in completely white lines
  805. whitestart Only at the beginning of lines, before the first non-white char
  806. t Everywhere except in headlines
  807. exc-hl-bol Everywhere except at the start of a headline
  808. If TAB is used in a place where it does not emulate TAB, the current subtree
  809. visibility is cycled."
  810. :group 'org-cycle
  811. :type '(choice (const :tag "Never" nil)
  812. (const :tag "Only in completely white lines" white)
  813. (const :tag "Before first char in a line" whitestart)
  814. (const :tag "Everywhere except in headlines" t)
  815. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  816. ))
  817. (defcustom org-cycle-separator-lines 2
  818. "Number of empty lines needed to keep an empty line between collapsed trees.
  819. If you leave an empty line between the end of a subtree and the following
  820. headline, this empty line is hidden when the subtree is folded.
  821. Org-mode will leave (exactly) one empty line visible if the number of
  822. empty lines is equal or larger to the number given in this variable.
  823. So the default 2 means at least 2 empty lines after the end of a subtree
  824. are needed to produce free space between a collapsed subtree and the
  825. following headline.
  826. If the number is negative, and the number of empty lines is at least -N,
  827. all empty lines are shown.
  828. Special case: when 0, never leave empty lines in collapsed view."
  829. :group 'org-cycle
  830. :type 'integer)
  831. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  832. (defcustom org-pre-cycle-hook nil
  833. "Hook that is run before visibility cycling is happening.
  834. The function(s) in this hook must accept a single argument which indicates
  835. the new state that will be set right after running this hook. The
  836. argument is a symbol. Before a global state change, it can have the values
  837. `overview', `content', or `all'. Before a local state change, it can have
  838. the values `folded', `children', or `subtree'."
  839. :group 'org-cycle
  840. :type 'hook)
  841. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  842. org-cycle-hide-drawers
  843. org-cycle-show-empty-lines
  844. org-optimize-window-after-visibility-change)
  845. "Hook that is run after `org-cycle' has changed the buffer visibility.
  846. The function(s) in this hook must accept a single argument which indicates
  847. the new state that was set by the most recent `org-cycle' command. The
  848. argument is a symbol. After a global state change, it can have the values
  849. `overview', `content', or `all'. After a local state change, it can have
  850. the values `folded', `children', or `subtree'."
  851. :group 'org-cycle
  852. :type 'hook)
  853. (defgroup org-edit-structure nil
  854. "Options concerning structure editing in Org-mode."
  855. :tag "Org Edit Structure"
  856. :group 'org-structure)
  857. (defcustom org-odd-levels-only nil
  858. "Non-nil means skip even levels and only use odd levels for the outline.
  859. This has the effect that two stars are being added/taken away in
  860. promotion/demotion commands. It also influences how levels are
  861. handled by the exporters.
  862. Changing it requires restart of `font-lock-mode' to become effective
  863. for fontification also in regions already fontified.
  864. You may also set this on a per-file basis by adding one of the following
  865. lines to the buffer:
  866. #+STARTUP: odd
  867. #+STARTUP: oddeven"
  868. :group 'org-edit-structure
  869. :group 'org-appearance
  870. :type 'boolean)
  871. (defcustom org-adapt-indentation t
  872. "Non-nil means adapt indentation to outline node level.
  873. When this variable is set, Org assumes that you write outlines by
  874. indenting text in each node to align with the headline (after the stars).
  875. The following issues are influenced by this variable:
  876. - When this is set and the *entire* text in an entry is indented, the
  877. indentation is increased by one space in a demotion command, and
  878. decreased by one in a promotion command. If any line in the entry
  879. body starts with text at column 0, indentation is not changed at all.
  880. - Property drawers and planning information is inserted indented when
  881. this variable s set. When nil, they will not be indented.
  882. - TAB indents a line relative to context. The lines below a headline
  883. will be indented when this variable is set.
  884. Note that this is all about true indentation, by adding and removing
  885. space characters. See also `org-indent.el' which does level-dependent
  886. indentation in a virtual way, i.e. at display time in Emacs."
  887. :group 'org-edit-structure
  888. :type 'boolean)
  889. (defcustom org-special-ctrl-a/e nil
  890. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  891. When t, `C-a' will bring back the cursor to the beginning of the
  892. headline text, i.e. after the stars and after a possible TODO
  893. keyword. In an item, this will be the position after bullet and
  894. check-box, if any. When the cursor is already at that position,
  895. another `C-a' will bring it to the beginning of the line.
  896. `C-e' will jump to the end of the headline, ignoring the presence
  897. of tags in the headline. A second `C-e' will then jump to the
  898. true end of the line, after any tags. This also means that, when
  899. this variable is non-nil, `C-e' also will never jump beyond the
  900. end of the heading of a folded section, i.e. not after the
  901. ellipses.
  902. When set to the symbol `reversed', the first `C-a' or `C-e' works
  903. normally, going to the true line boundary first. Only a directly
  904. following, identical keypress will bring the cursor to the
  905. special positions.
  906. This may also be a cons cell where the behavior for `C-a' and
  907. `C-e' is set separately."
  908. :group 'org-edit-structure
  909. :type '(choice
  910. (const :tag "off" nil)
  911. (const :tag "on: after stars/bullet and before tags first" t)
  912. (const :tag "reversed: true line boundary first" reversed)
  913. (cons :tag "Set C-a and C-e separately"
  914. (choice :tag "Special C-a"
  915. (const :tag "off" nil)
  916. (const :tag "on: after stars/bullet first" t)
  917. (const :tag "reversed: before stars/bullet first" reversed))
  918. (choice :tag "Special C-e"
  919. (const :tag "off" nil)
  920. (const :tag "on: before tags first" t)
  921. (const :tag "reversed: after tags first" reversed)))))
  922. (if (fboundp 'defvaralias)
  923. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  924. (defcustom org-special-ctrl-k nil
  925. "Non-nil means `C-k' will behave specially in headlines.
  926. When nil, `C-k' will call the default `kill-line' command.
  927. When t, the following will happen while the cursor is in the headline:
  928. - When the cursor is at the beginning of a headline, kill the entire
  929. line and possible the folded subtree below the line.
  930. - When in the middle of the headline text, kill the headline up to the tags.
  931. - When after the headline text, kill the tags."
  932. :group 'org-edit-structure
  933. :type 'boolean)
  934. (defcustom org-ctrl-k-protect-subtree nil
  935. "Non-nil means, do not delete a hidden subtree with C-k.
  936. When set to the symbol `error', simply throw an error when C-k is
  937. used to kill (part-of) a headline that has hidden text behind it.
  938. Any other non-nil value will result in a query to the user, if it is
  939. OK to kill that hidden subtree. When nil, kill without remorse."
  940. :group 'org-edit-structure
  941. :version "24.1"
  942. :type '(choice
  943. (const :tag "Do not protect hidden subtrees" nil)
  944. (const :tag "Protect hidden subtrees with a security query" t)
  945. (const :tag "Never kill a hidden subtree with C-k" error)))
  946. (defcustom org-catch-invisible-edits nil
  947. "Check if in invisible region before inserting or deleting a character.
  948. Valid values are:
  949. nil Do not check, so just do invisible edits.
  950. error Throw an error and do nothing.
  951. show Make point visible, and do the requested edit.
  952. show-and-error Make point visible, then throw an error and abort the edit.
  953. smart Make point visible, and do insertion/deletion if it is
  954. adjacent to visible text and the change feels predictable.
  955. Never delete a previously invisible character or add in the
  956. middle or right after an invisible region. Basically, this
  957. allows insertion and backward-delete right before ellipses.
  958. FIXME: maybe in this case we should not even show?"
  959. :group 'org-edit-structure
  960. :version "24.1"
  961. :type '(choice
  962. (const :tag "Do not check" nil)
  963. (const :tag "Throw error when trying to edit" error)
  964. (const :tag "Unhide, but do not do the edit" show-and-error)
  965. (const :tag "Show invisible part and do the edit" show)
  966. (const :tag "Be smart and do the right thing" smart)))
  967. (defcustom org-yank-folded-subtrees t
  968. "Non-nil means when yanking subtrees, fold them.
  969. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  970. it starts with a heading and all other headings in it are either children
  971. or siblings, then fold all the subtrees. However, do this only if no
  972. text after the yank would be swallowed into a folded tree by this action."
  973. :group 'org-edit-structure
  974. :type 'boolean)
  975. (defcustom org-yank-adjusted-subtrees nil
  976. "Non-nil means when yanking subtrees, adjust the level.
  977. With this setting, `org-paste-subtree' is used to insert the subtree, see
  978. this function for details."
  979. :group 'org-edit-structure
  980. :type 'boolean)
  981. (defcustom org-M-RET-may-split-line '((default . t))
  982. "Non-nil means M-RET will split the line at the cursor position.
  983. When nil, it will go to the end of the line before making a
  984. new line.
  985. You may also set this option in a different way for different
  986. contexts. Valid contexts are:
  987. headline when creating a new headline
  988. item when creating a new item
  989. table in a table field
  990. default the value to be used for all contexts not explicitly
  991. customized"
  992. :group 'org-structure
  993. :group 'org-table
  994. :type '(choice
  995. (const :tag "Always" t)
  996. (const :tag "Never" nil)
  997. (repeat :greedy t :tag "Individual contexts"
  998. (cons
  999. (choice :tag "Context"
  1000. (const headline)
  1001. (const item)
  1002. (const table)
  1003. (const default))
  1004. (boolean)))))
  1005. (defcustom org-insert-heading-respect-content nil
  1006. "Non-nil means insert new headings after the current subtree.
  1007. When nil, the new heading is created directly after the current line.
  1008. The commands \\[org-insert-heading-respect-content] and
  1009. \\[org-insert-todo-heading-respect-content] turn this variable on
  1010. for the duration of the command."
  1011. :group 'org-structure
  1012. :type 'boolean)
  1013. (defcustom org-blank-before-new-entry '((heading . auto)
  1014. (plain-list-item . auto))
  1015. "Should `org-insert-heading' leave a blank line before new heading/item?
  1016. The value is an alist, with `heading' and `plain-list-item' as CAR,
  1017. and a boolean flag as CDR. The cdr may also be the symbol `auto', in
  1018. which case Org will look at the surrounding headings/items and try to
  1019. make an intelligent decision whether to insert a blank line or not.
  1020. For plain lists, if the variable `org-empty-line-terminates-plain-lists' is
  1021. set, the setting here is ignored and no empty line is inserted, to avoid
  1022. breaking the list structure."
  1023. :group 'org-edit-structure
  1024. :type '(list
  1025. (cons (const heading)
  1026. (choice (const :tag "Never" nil)
  1027. (const :tag "Always" t)
  1028. (const :tag "Auto" auto)))
  1029. (cons (const plain-list-item)
  1030. (choice (const :tag "Never" nil)
  1031. (const :tag "Always" t)
  1032. (const :tag "Auto" auto)))))
  1033. (defcustom org-insert-heading-hook nil
  1034. "Hook being run after inserting a new heading."
  1035. :group 'org-edit-structure
  1036. :type 'hook)
  1037. (defcustom org-enable-fixed-width-editor t
  1038. "Non-nil means lines starting with \":\" are treated as fixed-width.
  1039. This currently only means they are never auto-wrapped.
  1040. When nil, such lines will be treated like ordinary lines.
  1041. See also the QUOTE keyword."
  1042. :group 'org-edit-structure
  1043. :type 'boolean)
  1044. (defcustom org-goto-auto-isearch t
  1045. "Non-nil means typing characters in `org-goto' starts incremental search."
  1046. :group 'org-edit-structure
  1047. :type 'boolean)
  1048. (defgroup org-sparse-trees nil
  1049. "Options concerning sparse trees in Org-mode."
  1050. :tag "Org Sparse Trees"
  1051. :group 'org-structure)
  1052. (defcustom org-highlight-sparse-tree-matches t
  1053. "Non-nil means highlight all matches that define a sparse tree.
  1054. The highlights will automatically disappear the next time the buffer is
  1055. changed by an edit command."
  1056. :group 'org-sparse-trees
  1057. :type 'boolean)
  1058. (defcustom org-remove-highlights-with-change t
  1059. "Non-nil means any change to the buffer will remove temporary highlights.
  1060. Such highlights are created by `org-occur' and `org-clock-display'.
  1061. When nil, `C-c C-c needs to be used to get rid of the highlights.
  1062. The highlights created by `org-preview-latex-fragment' always need
  1063. `C-c C-c' to be removed."
  1064. :group 'org-sparse-trees
  1065. :group 'org-time
  1066. :type 'boolean)
  1067. (defcustom org-occur-hook '(org-first-headline-recenter)
  1068. "Hook that is run after `org-occur' has constructed a sparse tree.
  1069. This can be used to recenter the window to show as much of the structure
  1070. as possible."
  1071. :group 'org-sparse-trees
  1072. :type 'hook)
  1073. (defgroup org-imenu-and-speedbar nil
  1074. "Options concerning imenu and speedbar in Org-mode."
  1075. :tag "Org Imenu and Speedbar"
  1076. :group 'org-structure)
  1077. (defcustom org-imenu-depth 2
  1078. "The maximum level for Imenu access to Org-mode headlines.
  1079. This also applied for speedbar access."
  1080. :group 'org-imenu-and-speedbar
  1081. :type 'integer)
  1082. (defgroup org-table nil
  1083. "Options concerning tables in Org-mode."
  1084. :tag "Org Table"
  1085. :group 'org)
  1086. (defcustom org-enable-table-editor 'optimized
  1087. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1088. When nil, such lines will be treated like ordinary lines.
  1089. When equal to the symbol `optimized', the table editor will be optimized to
  1090. do the following:
  1091. - Automatic overwrite mode in front of whitespace in table fields.
  1092. This makes the structure of the table stay in tact as long as the edited
  1093. field does not exceed the column width.
  1094. - Minimize the number of realigns. Normally, the table is aligned each time
  1095. TAB or RET are pressed to move to another field. With optimization this
  1096. happens only if changes to a field might have changed the column width.
  1097. Optimization requires replacing the functions `self-insert-command',
  1098. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1099. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1100. very good at guessing when a re-align will be necessary, but you can always
  1101. force one with \\[org-ctrl-c-ctrl-c].
  1102. If you would like to use the optimized version in Org-mode, but the
  1103. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1104. This variable can be used to turn on and off the table editor during a session,
  1105. but in order to toggle optimization, a restart is required.
  1106. See also the variable `org-table-auto-blank-field'."
  1107. :group 'org-table
  1108. :type '(choice
  1109. (const :tag "off" nil)
  1110. (const :tag "on" t)
  1111. (const :tag "on, optimized" optimized)))
  1112. (defcustom org-self-insert-cluster-for-undo t
  1113. "Non-nil means cluster self-insert commands for undo when possible.
  1114. If this is set, then, like in the Emacs command loop, 20 consecutive
  1115. characters will be undone together.
  1116. This is configurable, because there is some impact on typing performance."
  1117. :group 'org-table
  1118. :type 'boolean)
  1119. (defcustom org-table-tab-recognizes-table.el t
  1120. "Non-nil means TAB will automatically notice a table.el table.
  1121. When it sees such a table, it moves point into it and - if necessary -
  1122. calls `table-recognize-table'."
  1123. :group 'org-table-editing
  1124. :type 'boolean)
  1125. (defgroup org-link nil
  1126. "Options concerning links in Org-mode."
  1127. :tag "Org Link"
  1128. :group 'org)
  1129. (defvar org-link-abbrev-alist-local nil
  1130. "Buffer-local version of `org-link-abbrev-alist', which see.
  1131. The value of this is taken from the #+LINK lines.")
  1132. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1133. (defcustom org-link-abbrev-alist nil
  1134. "Alist of link abbreviations.
  1135. The car of each element is a string, to be replaced at the start of a link.
  1136. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1137. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1138. [[linkkey:tag][description]]
  1139. The 'linkkey' must be a word word, starting with a letter, followed
  1140. by letters, numbers, '-' or '_'.
  1141. If REPLACE is a string, the tag will simply be appended to create the link.
  1142. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  1143. the placeholder \"%h\" will cause a url-encoded version of the tag to
  1144. be inserted at that point (see the function `url-hexify-string').
  1145. REPLACE may also be a function that will be called with the tag as the
  1146. only argument to create the link, which should be returned as a string.
  1147. See the manual for examples."
  1148. :group 'org-link
  1149. :type '(repeat
  1150. (cons
  1151. (string :tag "Protocol")
  1152. (choice
  1153. (string :tag "Format")
  1154. (function)))))
  1155. (defcustom org-descriptive-links t
  1156. "Non-nil means Org will display descriptive links.
  1157. E.g. [[http://orgmode.org][Org website]] will be displayed as
  1158. \"Org Website\", hiding the link itself and just displaying its
  1159. description. When set to `nil', Org will display the full links
  1160. literally.
  1161. You can interactively set the value of this variable by calling
  1162. `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
  1163. :group 'org-link
  1164. :type 'boolean)
  1165. (defcustom org-link-file-path-type 'adaptive
  1166. "How the path name in file links should be stored.
  1167. Valid values are:
  1168. relative Relative to the current directory, i.e. the directory of the file
  1169. into which the link is being inserted.
  1170. absolute Absolute path, if possible with ~ for home directory.
  1171. noabbrev Absolute path, no abbreviation of home directory.
  1172. adaptive Use relative path for files in the current directory and sub-
  1173. directories of it. For other files, use an absolute path."
  1174. :group 'org-link
  1175. :type '(choice
  1176. (const relative)
  1177. (const absolute)
  1178. (const noabbrev)
  1179. (const adaptive)))
  1180. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  1181. "Types of links that should be activated in Org-mode files.
  1182. This is a list of symbols, each leading to the activation of a certain link
  1183. type. In principle, it does not hurt to turn on most link types - there may
  1184. be a small gain when turning off unused link types. The types are:
  1185. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1186. angle Links in angular brackets that may contain whitespace like
  1187. <bbdb:Carsten Dominik>.
  1188. plain Plain links in normal text, no whitespace, like http://google.com.
  1189. radio Text that is matched by a radio target, see manual for details.
  1190. tag Tag settings in a headline (link to tag search).
  1191. date Time stamps (link to calendar).
  1192. footnote Footnote labels.
  1193. Changing this variable requires a restart of Emacs to become effective."
  1194. :group 'org-link
  1195. :type '(set :greedy t
  1196. (const :tag "Double bracket links" bracket)
  1197. (const :tag "Angular bracket links" angle)
  1198. (const :tag "Plain text links" plain)
  1199. (const :tag "Radio target matches" radio)
  1200. (const :tag "Tags" tag)
  1201. (const :tag "Timestamps" date)
  1202. (const :tag "Footnotes" footnote)))
  1203. (defcustom org-make-link-description-function nil
  1204. "Function to use to generate link descriptions from links.
  1205. If nil the link location will be used. This function must take
  1206. two parameters; the first is the link and the second the
  1207. description `org-insert-link' has generated, and should return the
  1208. description to use."
  1209. :group 'org-link
  1210. :type 'function)
  1211. (defgroup org-link-store nil
  1212. "Options concerning storing links in Org-mode."
  1213. :tag "Org Store Link"
  1214. :group 'org-link)
  1215. (defcustom org-email-link-description-format "Email %c: %.30s"
  1216. "Format of the description part of a link to an email or usenet message.
  1217. The following %-escapes will be replaced by corresponding information:
  1218. %F full \"From\" field
  1219. %f name, taken from \"From\" field, address if no name
  1220. %T full \"To\" field
  1221. %t first name in \"To\" field, address if no name
  1222. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1223. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1224. %s subject
  1225. %d date
  1226. %m message-id.
  1227. You may use normal field width specification between the % and the letter.
  1228. This is for example useful to limit the length of the subject.
  1229. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1230. :group 'org-link-store
  1231. :type 'string)
  1232. (defcustom org-from-is-user-regexp
  1233. (let (r1 r2)
  1234. (when (and user-mail-address (not (string= user-mail-address "")))
  1235. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1236. (when (and user-full-name (not (string= user-full-name "")))
  1237. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1238. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1239. "Regexp matched against the \"From:\" header of an email or usenet message.
  1240. It should match if the message is from the user him/herself."
  1241. :group 'org-link-store
  1242. :type 'regexp)
  1243. (defcustom org-link-to-org-use-id 'create-if-interactive-and-no-custom-id
  1244. "Non-nil means storing a link to an Org file will use entry IDs.
  1245. Note that before this variable is even considered, org-id must be loaded,
  1246. so please customize `org-modules' and turn it on.
  1247. The variable can have the following values:
  1248. t Create an ID if needed to make a link to the current entry.
  1249. create-if-interactive
  1250. If `org-store-link' is called directly (interactively, as a user
  1251. command), do create an ID to support the link. But when doing the
  1252. job for remember, only use the ID if it already exists. The
  1253. purpose of this setting is to avoid proliferation of unwanted
  1254. IDs, just because you happen to be in an Org file when you
  1255. call `org-remember' that automatically and preemptively
  1256. creates a link. If you do want to get an ID link in a remember
  1257. template to an entry not having an ID, create it first by
  1258. explicitly creating a link to it, using `C-c C-l' first.
  1259. create-if-interactive-and-no-custom-id
  1260. Like create-if-interactive, but do not create an ID if there is
  1261. a CUSTOM_ID property defined in the entry. This is the default.
  1262. use-existing
  1263. Use existing ID, do not create one.
  1264. nil Never use an ID to make a link, instead link using a text search for
  1265. the headline text."
  1266. :group 'org-link-store
  1267. :type '(choice
  1268. (const :tag "Create ID to make link" t)
  1269. (const :tag "Create if storing link interactively"
  1270. create-if-interactive)
  1271. (const :tag "Create if storing link interactively and no CUSTOM_ID is present"
  1272. create-if-interactive-and-no-custom-id)
  1273. (const :tag "Only use existing" use-existing)
  1274. (const :tag "Do not use ID to create link" nil)))
  1275. (defcustom org-context-in-file-links t
  1276. "Non-nil means file links from `org-store-link' contain context.
  1277. A search string will be added to the file name with :: as separator and
  1278. used to find the context when the link is activated by the command
  1279. `org-open-at-point'. When this option is t, the entire active region
  1280. will be placed in the search string of the file link. If set to a
  1281. positive integer, only the first n lines of context will be stored.
  1282. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1283. negates this setting for the duration of the command."
  1284. :group 'org-link-store
  1285. :type '(choice boolean integer))
  1286. (defcustom org-keep-stored-link-after-insertion nil
  1287. "Non-nil means keep link in list for entire session.
  1288. The command `org-store-link' adds a link pointing to the current
  1289. location to an internal list. These links accumulate during a session.
  1290. The command `org-insert-link' can be used to insert links into any
  1291. Org-mode file (offering completion for all stored links). When this
  1292. option is nil, every link which has been inserted once using \\[org-insert-link]
  1293. will be removed from the list, to make completing the unused links
  1294. more efficient."
  1295. :group 'org-link-store
  1296. :type 'boolean)
  1297. (defgroup org-link-follow nil
  1298. "Options concerning following links in Org-mode."
  1299. :tag "Org Follow Link"
  1300. :group 'org-link)
  1301. (defcustom org-link-translation-function nil
  1302. "Function to translate links with different syntax to Org syntax.
  1303. This can be used to translate links created for example by the Planner
  1304. or emacs-wiki packages to Org syntax.
  1305. The function must accept two parameters, a TYPE containing the link
  1306. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1307. which is everything after the link protocol. It should return a cons
  1308. with possibly modified values of type and path.
  1309. Org contains a function for this, so if you set this variable to
  1310. `org-translate-link-from-planner', you should be able follow many
  1311. links created by planner."
  1312. :group 'org-link-follow
  1313. :type 'function)
  1314. (defcustom org-follow-link-hook nil
  1315. "Hook that is run after a link has been followed."
  1316. :group 'org-link-follow
  1317. :type 'hook)
  1318. (defcustom org-tab-follows-link nil
  1319. "Non-nil means on links TAB will follow the link.
  1320. Needs to be set before org.el is loaded.
  1321. This really should not be used, it does not make sense, and the
  1322. implementation is bad."
  1323. :group 'org-link-follow
  1324. :type 'boolean)
  1325. (defcustom org-return-follows-link nil
  1326. "Non-nil means on links RET will follow the link."
  1327. :group 'org-link-follow
  1328. :type 'boolean)
  1329. (defcustom org-mouse-1-follows-link
  1330. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1331. "Non-nil means mouse-1 on a link will follow the link.
  1332. A longer mouse click will still set point. Does not work on XEmacs.
  1333. Needs to be set before org.el is loaded."
  1334. :group 'org-link-follow
  1335. :type 'boolean)
  1336. (defcustom org-mark-ring-length 4
  1337. "Number of different positions to be recorded in the ring.
  1338. Changing this requires a restart of Emacs to work correctly."
  1339. :group 'org-link-follow
  1340. :type 'integer)
  1341. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1342. "Non-nil means internal links in Org files must exactly match a headline.
  1343. When nil, the link search tries to match a phrase with all words
  1344. in the search text."
  1345. :group 'org-link-follow
  1346. :version "24.1"
  1347. :type '(choice
  1348. (const :tag "Use fuzzy text search" nil)
  1349. (const :tag "Match only exact headline" t)
  1350. (const :tag "Match exact headline or query to create it"
  1351. query-to-create)))
  1352. (defcustom org-link-frame-setup
  1353. '((vm . vm-visit-folder-other-frame)
  1354. (vm-imap . vm-visit-imap-folder-other-frame)
  1355. (gnus . org-gnus-no-new-news)
  1356. (file . find-file-other-window)
  1357. (wl . wl-other-frame))
  1358. "Setup the frame configuration for following links.
  1359. When following a link with Emacs, it may often be useful to display
  1360. this link in another window or frame. This variable can be used to
  1361. set this up for the different types of links.
  1362. For VM, use any of
  1363. `vm-visit-folder'
  1364. `vm-visit-folder-other-window'
  1365. `vm-visit-folder-other-frame'
  1366. For Gnus, use any of
  1367. `gnus'
  1368. `gnus-other-frame'
  1369. `org-gnus-no-new-news'
  1370. For FILE, use any of
  1371. `find-file'
  1372. `find-file-other-window'
  1373. `find-file-other-frame'
  1374. For Wanderlust use any of
  1375. `wl'
  1376. `wl-other-frame'
  1377. For the calendar, use the variable `calendar-setup'.
  1378. For BBDB, it is currently only possible to display the matches in
  1379. another window."
  1380. :group 'org-link-follow
  1381. :type '(list
  1382. (cons (const vm)
  1383. (choice
  1384. (const vm-visit-folder)
  1385. (const vm-visit-folder-other-window)
  1386. (const vm-visit-folder-other-frame)))
  1387. (cons (const gnus)
  1388. (choice
  1389. (const gnus)
  1390. (const gnus-other-frame)
  1391. (const org-gnus-no-new-news)))
  1392. (cons (const file)
  1393. (choice
  1394. (const find-file)
  1395. (const find-file-other-window)
  1396. (const find-file-other-frame)))
  1397. (cons (const wl)
  1398. (choice
  1399. (const wl)
  1400. (const wl-other-frame)))))
  1401. (defcustom org-display-internal-link-with-indirect-buffer nil
  1402. "Non-nil means use indirect buffer to display infile links.
  1403. Activating internal links (from one location in a file to another location
  1404. in the same file) normally just jumps to the location. When the link is
  1405. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1406. is displayed in
  1407. another window. When this option is set, the other window actually displays
  1408. an indirect buffer clone of the current buffer, to avoid any visibility
  1409. changes to the current buffer."
  1410. :group 'org-link-follow
  1411. :type 'boolean)
  1412. (defcustom org-open-non-existing-files nil
  1413. "Non-nil means `org-open-file' will open non-existing files.
  1414. When nil, an error will be generated.
  1415. This variable applies only to external applications because they
  1416. might choke on non-existing files. If the link is to a file that
  1417. will be opened in Emacs, the variable is ignored."
  1418. :group 'org-link-follow
  1419. :type 'boolean)
  1420. (defcustom org-open-directory-means-index-dot-org nil
  1421. "Non-nil means a link to a directory really means to index.org.
  1422. When nil, following a directory link will run dired or open a finder/explorer
  1423. window on that directory."
  1424. :group 'org-link-follow
  1425. :type 'boolean)
  1426. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1427. "Function and arguments to call for following mailto links.
  1428. This is a list with the first element being a Lisp function, and the
  1429. remaining elements being arguments to the function. In string arguments,
  1430. %a will be replaced by the address, and %s will be replaced by the subject
  1431. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1432. :group 'org-link-follow
  1433. :type '(choice
  1434. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1435. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1436. (const :tag "message-mail" (message-mail "%a" "%s"))
  1437. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1438. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1439. "Non-nil means ask for confirmation before executing shell links.
  1440. Shell links can be dangerous: just think about a link
  1441. [[shell:rm -rf ~/*][Google Search]]
  1442. This link would show up in your Org-mode document as \"Google Search\",
  1443. but really it would remove your entire home directory.
  1444. Therefore we advise against setting this variable to nil.
  1445. Just change it to `y-or-n-p' if you want to confirm with a
  1446. single keystroke rather than having to type \"yes\"."
  1447. :group 'org-link-follow
  1448. :type '(choice
  1449. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1450. (const :tag "with y-or-n (faster)" y-or-n-p)
  1451. (const :tag "no confirmation (dangerous)" nil)))
  1452. (put 'org-confirm-shell-link-function
  1453. 'safe-local-variable
  1454. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1455. (defcustom org-confirm-shell-link-not-regexp ""
  1456. "A regexp to skip confirmation for shell links."
  1457. :group 'org-link-follow
  1458. :version "24.1"
  1459. :type 'regexp)
  1460. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1461. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1462. Elisp links can be dangerous: just think about a link
  1463. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1464. This link would show up in your Org-mode document as \"Google Search\",
  1465. but really it would remove your entire home directory.
  1466. Therefore we advise against setting this variable to nil.
  1467. Just change it to `y-or-n-p' if you want to confirm with a
  1468. single keystroke rather than having to type \"yes\"."
  1469. :group 'org-link-follow
  1470. :type '(choice
  1471. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1472. (const :tag "with y-or-n (faster)" y-or-n-p)
  1473. (const :tag "no confirmation (dangerous)" nil)))
  1474. (put 'org-confirm-shell-link-function
  1475. 'safe-local-variable
  1476. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1477. (defcustom org-confirm-elisp-link-not-regexp ""
  1478. "A regexp to skip confirmation for Elisp links."
  1479. :group 'org-link-follow
  1480. :version "24.1"
  1481. :type 'regexp)
  1482. (defconst org-file-apps-defaults-gnu
  1483. '((remote . emacs)
  1484. (system . mailcap)
  1485. (t . mailcap))
  1486. "Default file applications on a UNIX or GNU/Linux system.
  1487. See `org-file-apps'.")
  1488. (defconst org-file-apps-defaults-macosx
  1489. '((remote . emacs)
  1490. (t . "open %s")
  1491. (system . "open %s")
  1492. ("ps.gz" . "gv %s")
  1493. ("eps.gz" . "gv %s")
  1494. ("dvi" . "xdvi %s")
  1495. ("fig" . "xfig %s"))
  1496. "Default file applications on a MacOS X system.
  1497. The system \"open\" is known as a default, but we use X11 applications
  1498. for some files for which the OS does not have a good default.
  1499. See `org-file-apps'.")
  1500. (defconst org-file-apps-defaults-windowsnt
  1501. (list
  1502. '(remote . emacs)
  1503. (cons t
  1504. (list (if (featurep 'xemacs)
  1505. 'mswindows-shell-execute
  1506. 'w32-shell-execute)
  1507. "open" 'file))
  1508. (cons 'system
  1509. (list (if (featurep 'xemacs)
  1510. 'mswindows-shell-execute
  1511. 'w32-shell-execute)
  1512. "open" 'file)))
  1513. "Default file applications on a Windows NT system.
  1514. The system \"open\" is used for most files.
  1515. See `org-file-apps'.")
  1516. (defcustom org-file-apps
  1517. '(
  1518. (auto-mode . emacs)
  1519. ("\\.mm\\'" . default)
  1520. ("\\.x?html?\\'" . default)
  1521. ("\\.pdf\\'" . default)
  1522. )
  1523. "External applications for opening `file:path' items in a document.
  1524. Org-mode uses system defaults for different file types, but
  1525. you can use this variable to set the application for a given file
  1526. extension. The entries in this list are cons cells where the car identifies
  1527. files and the cdr the corresponding command. Possible values for the
  1528. file identifier are
  1529. \"string\" A string as a file identifier can be interpreted in different
  1530. ways, depending on its contents:
  1531. - Alphanumeric characters only:
  1532. Match links with this file extension.
  1533. Example: (\"pdf\" . \"evince %s\")
  1534. to open PDFs with evince.
  1535. - Regular expression: Match links where the
  1536. filename matches the regexp. If you want to
  1537. use groups here, use shy groups.
  1538. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1539. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1540. to open *.html and *.xhtml with firefox.
  1541. - Regular expression which contains (non-shy) groups:
  1542. Match links where the whole link, including \"::\", and
  1543. anything after that, matches the regexp.
  1544. In a custom command string, %1, %2, etc. are replaced with
  1545. the parts of the link that were matched by the groups.
  1546. For backwards compatibility, if a command string is given
  1547. that does not use any of the group matches, this case is
  1548. handled identically to the second one (i.e. match against
  1549. file name only).
  1550. In a custom lisp form, you can access the group matches with
  1551. (match-string n link).
  1552. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1553. to open [[file:document.pdf::5]] with evince at page 5.
  1554. `directory' Matches a directory
  1555. `remote' Matches a remote file, accessible through tramp or efs.
  1556. Remote files most likely should be visited through Emacs
  1557. because external applications cannot handle such paths.
  1558. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1559. so all files Emacs knows how to handle. Using this with
  1560. command `emacs' will open most files in Emacs. Beware that this
  1561. will also open html files inside Emacs, unless you add
  1562. (\"html\" . default) to the list as well.
  1563. t Default for files not matched by any of the other options.
  1564. `system' The system command to open files, like `open' on Windows
  1565. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1566. that will be selected if you call `C-c C-o' with a double
  1567. \\[universal-argument] \\[universal-argument] prefix.
  1568. Possible values for the command are:
  1569. `emacs' The file will be visited by the current Emacs process.
  1570. `default' Use the default application for this file type, which is the
  1571. association for t in the list, most likely in the system-specific
  1572. part.
  1573. This can be used to overrule an unwanted setting in the
  1574. system-specific variable.
  1575. `system' Use the system command for opening files, like \"open\".
  1576. This command is specified by the entry whose car is `system'.
  1577. Most likely, the system-specific version of this variable
  1578. does define this command, but you can overrule/replace it
  1579. here.
  1580. string A command to be executed by a shell; %s will be replaced
  1581. by the path to the file.
  1582. sexp A Lisp form which will be evaluated. The file path will
  1583. be available in the Lisp variable `file'.
  1584. For more examples, see the system specific constants
  1585. `org-file-apps-defaults-macosx'
  1586. `org-file-apps-defaults-windowsnt'
  1587. `org-file-apps-defaults-gnu'."
  1588. :group 'org-link-follow
  1589. :type '(repeat
  1590. (cons (choice :value ""
  1591. (string :tag "Extension")
  1592. (const :tag "System command to open files" system)
  1593. (const :tag "Default for unrecognized files" t)
  1594. (const :tag "Remote file" remote)
  1595. (const :tag "Links to a directory" directory)
  1596. (const :tag "Any files that have Emacs modes"
  1597. auto-mode))
  1598. (choice :value ""
  1599. (const :tag "Visit with Emacs" emacs)
  1600. (const :tag "Use default" default)
  1601. (const :tag "Use the system command" system)
  1602. (string :tag "Command")
  1603. (sexp :tag "Lisp form")))))
  1604. (defgroup org-refile nil
  1605. "Options concerning refiling entries in Org-mode."
  1606. :tag "Org Refile"
  1607. :group 'org)
  1608. (defcustom org-directory "~/org"
  1609. "Directory with org files.
  1610. This is just a default location to look for Org files. There is no need
  1611. at all to put your files into this directory. It is only used in the
  1612. following situations:
  1613. 1. When a remember template specifies a target file that is not an
  1614. absolute path. The path will then be interpreted relative to
  1615. `org-directory'
  1616. 2. When a remember note is filed away in an interactive way (when exiting the
  1617. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1618. with `org-directory' as the default path."
  1619. :group 'org-refile
  1620. :group 'org-remember
  1621. :type 'directory)
  1622. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1623. "Default target for storing notes.
  1624. Used as a fall back file for org-remember.el and org-capture.el, for
  1625. templates that do not specify a target file."
  1626. :group 'org-refile
  1627. :group 'org-remember
  1628. :type '(choice
  1629. (const :tag "Default from remember-data-file" nil)
  1630. file))
  1631. (defcustom org-goto-interface 'outline
  1632. "The default interface to be used for `org-goto'.
  1633. Allowed values are:
  1634. outline The interface shows an outline of the relevant file
  1635. and the correct heading is found by moving through
  1636. the outline or by searching with incremental search.
  1637. outline-path-completion Headlines in the current buffer are offered via
  1638. completion. This is the interface also used by
  1639. the refile command."
  1640. :group 'org-refile
  1641. :type '(choice
  1642. (const :tag "Outline" outline)
  1643. (const :tag "Outline-path-completion" outline-path-completion)))
  1644. (defcustom org-goto-max-level 5
  1645. "Maximum target level when running `org-goto' with refile interface."
  1646. :group 'org-refile
  1647. :type 'integer)
  1648. (defcustom org-reverse-note-order nil
  1649. "Non-nil means store new notes at the beginning of a file or entry.
  1650. When nil, new notes will be filed to the end of a file or entry.
  1651. This can also be a list with cons cells of regular expressions that
  1652. are matched against file names, and values."
  1653. :group 'org-remember
  1654. :group 'org-refile
  1655. :type '(choice
  1656. (const :tag "Reverse always" t)
  1657. (const :tag "Reverse never" nil)
  1658. (repeat :tag "By file name regexp"
  1659. (cons regexp boolean))))
  1660. (defcustom org-log-refile nil
  1661. "Information to record when a task is refiled.
  1662. Possible values are:
  1663. nil Don't add anything
  1664. time Add a time stamp to the task
  1665. note Prompt for a note and add it with template `org-log-note-headings'
  1666. This option can also be set with on a per-file-basis with
  1667. #+STARTUP: nologrefile
  1668. #+STARTUP: logrefile
  1669. #+STARTUP: lognoterefile
  1670. You can have local logging settings for a subtree by setting the LOGGING
  1671. property to one or more of these keywords.
  1672. When bulk-refiling from the agenda, the value `note' is forbidden and
  1673. will temporarily be changed to `time'."
  1674. :group 'org-refile
  1675. :group 'org-progress
  1676. :version "24.1"
  1677. :type '(choice
  1678. (const :tag "No logging" nil)
  1679. (const :tag "Record timestamp" time)
  1680. (const :tag "Record timestamp with note." note)))
  1681. (defcustom org-refile-targets nil
  1682. "Targets for refiling entries with \\[org-refile].
  1683. This is a list of cons cells. Each cell contains:
  1684. - a specification of the files to be considered, either a list of files,
  1685. or a symbol whose function or variable value will be used to retrieve
  1686. a file name or a list of file names. If you use `org-agenda-files' for
  1687. that, all agenda files will be scanned for targets. Nil means consider
  1688. headings in the current buffer.
  1689. - A specification of how to find candidate refile targets. This may be
  1690. any of:
  1691. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1692. This tag has to be present in all target headlines, inheritance will
  1693. not be considered.
  1694. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1695. todo keyword.
  1696. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1697. headlines that are refiling targets.
  1698. - a cons cell (:level . N). Any headline of level N is considered a target.
  1699. Note that, when `org-odd-levels-only' is set, level corresponds to
  1700. order in hierarchy, not to the number of stars.
  1701. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1702. Note that, when `org-odd-levels-only' is set, level corresponds to
  1703. order in hierarchy, not to the number of stars.
  1704. Each element of this list generates a set of possible targets.
  1705. The union of these sets is presented (with completion) to
  1706. the user by `org-refile'.
  1707. You can set the variable `org-refile-target-verify-function' to a function
  1708. to verify each headline found by the simple criteria above.
  1709. When this variable is nil, all top-level headlines in the current buffer
  1710. are used, equivalent to the value `((nil . (:level . 1))'."
  1711. :group 'org-refile
  1712. :type '(repeat
  1713. (cons
  1714. (choice :value org-agenda-files
  1715. (const :tag "All agenda files" org-agenda-files)
  1716. (const :tag "Current buffer" nil)
  1717. (function) (variable) (file))
  1718. (choice :tag "Identify target headline by"
  1719. (cons :tag "Specific tag" (const :value :tag) (string))
  1720. (cons :tag "TODO keyword" (const :value :todo) (string))
  1721. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1722. (cons :tag "Level number" (const :value :level) (integer))
  1723. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1724. (defcustom org-refile-target-verify-function nil
  1725. "Function to verify if the headline at point should be a refile target.
  1726. The function will be called without arguments, with point at the
  1727. beginning of the headline. It should return t and leave point
  1728. where it is if the headline is a valid target for refiling.
  1729. If the target should not be selected, the function must return nil.
  1730. In addition to this, it may move point to a place from where the search
  1731. should be continued. For example, the function may decide that the entire
  1732. subtree of the current entry should be excluded and move point to the end
  1733. of the subtree."
  1734. :group 'org-refile
  1735. :type 'function)
  1736. (defcustom org-refile-use-cache nil
  1737. "Non-nil means cache refile targets to speed up the process.
  1738. The cache for a particular file will be updated automatically when
  1739. the buffer has been killed, or when any of the marker used for flagging
  1740. refile targets no longer points at a live buffer.
  1741. If you have added new entries to a buffer that might themselves be targets,
  1742. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  1743. find that easier, `C-u C-u C-u C-c C-w'."
  1744. :group 'org-refile
  1745. :version "24.1"
  1746. :type 'boolean)
  1747. (defcustom org-refile-use-outline-path nil
  1748. "Non-nil means provide refile targets as paths.
  1749. So a level 3 headline will be available as level1/level2/level3.
  1750. When the value is `file', also include the file name (without directory)
  1751. into the path. In this case, you can also stop the completion after
  1752. the file name, to get entries inserted as top level in the file.
  1753. When `full-file-path', include the full file path."
  1754. :group 'org-refile
  1755. :type '(choice
  1756. (const :tag "Not" nil)
  1757. (const :tag "Yes" t)
  1758. (const :tag "Start with file name" file)
  1759. (const :tag "Start with full file path" full-file-path)))
  1760. (defcustom org-outline-path-complete-in-steps t
  1761. "Non-nil means complete the outline path in hierarchical steps.
  1762. When Org-mode uses the refile interface to select an outline path
  1763. \(see variable `org-refile-use-outline-path'), the completion of
  1764. the path can be done is a single go, or if can be done in steps down
  1765. the headline hierarchy. Going in steps is probably the best if you
  1766. do not use a special completion package like `ido' or `icicles'.
  1767. However, when using these packages, going in one step can be very
  1768. fast, while still showing the whole path to the entry."
  1769. :group 'org-refile
  1770. :type 'boolean)
  1771. (defcustom org-refile-allow-creating-parent-nodes nil
  1772. "Non-nil means allow to create new nodes as refile targets.
  1773. New nodes are then created by adding \"/new node name\" to the completion
  1774. of an existing node. When the value of this variable is `confirm',
  1775. new node creation must be confirmed by the user (recommended)
  1776. When nil, the completion must match an existing entry.
  1777. Note that, if the new heading is not seen by the criteria
  1778. listed in `org-refile-targets', multiple instances of the same
  1779. heading would be created by trying again to file under the new
  1780. heading."
  1781. :group 'org-refile
  1782. :type '(choice
  1783. (const :tag "Never" nil)
  1784. (const :tag "Always" t)
  1785. (const :tag "Prompt for confirmation" confirm)))
  1786. (defcustom org-refile-active-region-within-subtree nil
  1787. "Non-nil means also refile active region within a subtree.
  1788. By default `org-refile' doesn't allow refiling regions if they
  1789. don't contain a set of subtrees, but it might be convenient to
  1790. do so sometimes: in that case, the first line of the region is
  1791. converted to a headline before refiling."
  1792. :group 'org-refile
  1793. :version "24.1"
  1794. :type 'boolean)
  1795. (defgroup org-todo nil
  1796. "Options concerning TODO items in Org-mode."
  1797. :tag "Org TODO"
  1798. :group 'org)
  1799. (defgroup org-progress nil
  1800. "Options concerning Progress logging in Org-mode."
  1801. :tag "Org Progress"
  1802. :group 'org-time)
  1803. (defvar org-todo-interpretation-widgets
  1804. '((:tag "Sequence (cycling hits every state)" sequence)
  1805. (:tag "Type (cycling directly to DONE)" type))
  1806. "The available interpretation symbols for customizing `org-todo-keywords'.
  1807. Interested libraries should add to this list.")
  1808. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1809. "List of TODO entry keyword sequences and their interpretation.
  1810. \\<org-mode-map>This is a list of sequences.
  1811. Each sequence starts with a symbol, either `sequence' or `type',
  1812. indicating if the keywords should be interpreted as a sequence of
  1813. action steps, or as different types of TODO items. The first
  1814. keywords are states requiring action - these states will select a headline
  1815. for inclusion into the global TODO list Org-mode produces. If one of
  1816. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  1817. signify that no further action is necessary. If \"|\" is not found,
  1818. the last keyword is treated as the only DONE state of the sequence.
  1819. The command \\[org-todo] cycles an entry through these states, and one
  1820. additional state where no keyword is present. For details about this
  1821. cycling, see the manual.
  1822. TODO keywords and interpretation can also be set on a per-file basis with
  1823. the special #+SEQ_TODO and #+TYP_TODO lines.
  1824. Each keyword can optionally specify a character for fast state selection
  1825. \(in combination with the variable `org-use-fast-todo-selection')
  1826. and specifiers for state change logging, using the same syntax
  1827. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1828. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1829. indicates to record a time stamp each time this state is selected.
  1830. Each keyword may also specify if a timestamp or a note should be
  1831. recorded when entering or leaving the state, by adding additional
  1832. characters in the parenthesis after the keyword. This looks like this:
  1833. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1834. record only the time of the state change. With X and Y being either
  1835. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1836. Y when leaving the state if and only if the *target* state does not
  1837. define X. You may omit any of the fast-selection key or X or /Y,
  1838. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1839. For backward compatibility, this variable may also be just a list
  1840. of keywords - in this case the interpretation (sequence or type) will be
  1841. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1842. :group 'org-todo
  1843. :group 'org-keywords
  1844. :type '(choice
  1845. (repeat :tag "Old syntax, just keywords"
  1846. (string :tag "Keyword"))
  1847. (repeat :tag "New syntax"
  1848. (cons
  1849. (choice
  1850. :tag "Interpretation"
  1851. ;;Quick and dirty way to see
  1852. ;;`org-todo-interpretations'. This takes the
  1853. ;;place of item arguments
  1854. :convert-widget
  1855. (lambda (widget)
  1856. (widget-put widget
  1857. :args (mapcar
  1858. #'(lambda (x)
  1859. (widget-convert
  1860. (cons 'const x)))
  1861. org-todo-interpretation-widgets))
  1862. widget))
  1863. (repeat
  1864. (string :tag "Keyword"))))))
  1865. (defvar org-todo-keywords-1 nil
  1866. "All TODO and DONE keywords active in a buffer.")
  1867. (make-variable-buffer-local 'org-todo-keywords-1)
  1868. (defvar org-todo-keywords-for-agenda nil)
  1869. (defvar org-done-keywords-for-agenda nil)
  1870. (defvar org-drawers-for-agenda nil)
  1871. (defvar org-todo-keyword-alist-for-agenda nil)
  1872. (defvar org-tag-alist-for-agenda nil)
  1873. (defvar org-agenda-contributing-files nil)
  1874. (defvar org-not-done-keywords nil)
  1875. (make-variable-buffer-local 'org-not-done-keywords)
  1876. (defvar org-done-keywords nil)
  1877. (make-variable-buffer-local 'org-done-keywords)
  1878. (defvar org-todo-heads nil)
  1879. (make-variable-buffer-local 'org-todo-heads)
  1880. (defvar org-todo-sets nil)
  1881. (make-variable-buffer-local 'org-todo-sets)
  1882. (defvar org-todo-log-states nil)
  1883. (make-variable-buffer-local 'org-todo-log-states)
  1884. (defvar org-todo-kwd-alist nil)
  1885. (make-variable-buffer-local 'org-todo-kwd-alist)
  1886. (defvar org-todo-key-alist nil)
  1887. (make-variable-buffer-local 'org-todo-key-alist)
  1888. (defvar org-todo-key-trigger nil)
  1889. (make-variable-buffer-local 'org-todo-key-trigger)
  1890. (defcustom org-todo-interpretation 'sequence
  1891. "Controls how TODO keywords are interpreted.
  1892. This variable is in principle obsolete and is only used for
  1893. backward compatibility, if the interpretation of todo keywords is
  1894. not given already in `org-todo-keywords'. See that variable for
  1895. more information."
  1896. :group 'org-todo
  1897. :group 'org-keywords
  1898. :type '(choice (const sequence)
  1899. (const type)))
  1900. (defcustom org-use-fast-todo-selection t
  1901. "Non-nil means use the fast todo selection scheme with C-c C-t.
  1902. This variable describes if and under what circumstances the cycling
  1903. mechanism for TODO keywords will be replaced by a single-key, direct
  1904. selection scheme.
  1905. When nil, fast selection is never used.
  1906. When the symbol `prefix', it will be used when `org-todo' is called with
  1907. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1908. in an agenda buffer.
  1909. When t, fast selection is used by default. In this case, the prefix
  1910. argument forces cycling instead.
  1911. In all cases, the special interface is only used if access keys have actually
  1912. been assigned by the user, i.e. if keywords in the configuration are followed
  1913. by a letter in parenthesis, like TODO(t)."
  1914. :group 'org-todo
  1915. :type '(choice
  1916. (const :tag "Never" nil)
  1917. (const :tag "By default" t)
  1918. (const :tag "Only with C-u C-c C-t" prefix)))
  1919. (defcustom org-provide-todo-statistics t
  1920. "Non-nil means update todo statistics after insert and toggle.
  1921. ALL-HEADLINES means update todo statistics by including headlines
  1922. with no TODO keyword as well, counting them as not done.
  1923. A list of TODO keywords means the same, but skip keywords that are
  1924. not in this list.
  1925. When this is set, todo statistics is updated in the parent of the
  1926. current entry each time a todo state is changed."
  1927. :group 'org-todo
  1928. :type '(choice
  1929. (const :tag "Yes, only for TODO entries" t)
  1930. (const :tag "Yes, including all entries" 'all-headlines)
  1931. (repeat :tag "Yes, for TODOs in this list"
  1932. (string :tag "TODO keyword"))
  1933. (other :tag "No TODO statistics" nil)))
  1934. (defcustom org-hierarchical-todo-statistics t
  1935. "Non-nil means TODO statistics covers just direct children.
  1936. When nil, all entries in the subtree are considered.
  1937. This has only an effect if `org-provide-todo-statistics' is set.
  1938. To set this to nil for only a single subtree, use a COOKIE_DATA
  1939. property and include the word \"recursive\" into the value."
  1940. :group 'org-todo
  1941. :type 'boolean)
  1942. (defcustom org-after-todo-state-change-hook nil
  1943. "Hook which is run after the state of a TODO item was changed.
  1944. The new state (a string with a TODO keyword, or nil) is available in the
  1945. Lisp variable `org-state'."
  1946. :group 'org-todo
  1947. :type 'hook)
  1948. (defvar org-blocker-hook nil
  1949. "Hook for functions that are allowed to block a state change.
  1950. Each function gets as its single argument a property list, see
  1951. `org-trigger-hook' for more information about this list.
  1952. If any of the functions in this hook returns nil, the state change
  1953. is blocked.")
  1954. (defvar org-trigger-hook nil
  1955. "Hook for functions that are triggered by a state change.
  1956. Each function gets as its single argument a property list with at least
  1957. the following elements:
  1958. (:type type-of-change :position pos-at-entry-start
  1959. :from old-state :to new-state)
  1960. Depending on the type, more properties may be present.
  1961. This mechanism is currently implemented for:
  1962. TODO state changes
  1963. ------------------
  1964. :type todo-state-change
  1965. :from previous state (keyword as a string), or nil, or a symbol
  1966. 'todo' or 'done', to indicate the general type of state.
  1967. :to new state, like in :from")
  1968. (defcustom org-enforce-todo-dependencies nil
  1969. "Non-nil means undone TODO entries will block switching the parent to DONE.
  1970. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1971. be blocked if any prior sibling is not yet done.
  1972. Finally, if the parent is blocked because of ordered siblings of its own,
  1973. the child will also be blocked."
  1974. :set (lambda (var val)
  1975. (set var val)
  1976. (if val
  1977. (add-hook 'org-blocker-hook
  1978. 'org-block-todo-from-children-or-siblings-or-parent)
  1979. (remove-hook 'org-blocker-hook
  1980. 'org-block-todo-from-children-or-siblings-or-parent)))
  1981. :group 'org-todo
  1982. :type 'boolean)
  1983. (defcustom org-enforce-todo-checkbox-dependencies nil
  1984. "Non-nil means unchecked boxes will block switching the parent to DONE.
  1985. When this is nil, checkboxes have no influence on switching TODO states.
  1986. When non-nil, you first need to check off all check boxes before the TODO
  1987. entry can be switched to DONE.
  1988. This variable needs to be set before org.el is loaded, and you need to
  1989. restart Emacs after a change to make the change effective. The only way
  1990. to change is while Emacs is running is through the customize interface."
  1991. :set (lambda (var val)
  1992. (set var val)
  1993. (if val
  1994. (add-hook 'org-blocker-hook
  1995. 'org-block-todo-from-checkboxes)
  1996. (remove-hook 'org-blocker-hook
  1997. 'org-block-todo-from-checkboxes)))
  1998. :group 'org-todo
  1999. :type 'boolean)
  2000. (defcustom org-treat-insert-todo-heading-as-state-change nil
  2001. "Non-nil means inserting a TODO heading is treated as state change.
  2002. So when the command \\[org-insert-todo-heading] is used, state change
  2003. logging will apply if appropriate. When nil, the new TODO item will
  2004. be inserted directly, and no logging will take place."
  2005. :group 'org-todo
  2006. :type 'boolean)
  2007. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  2008. "Non-nil means switching TODO states with S-cursor counts as state change.
  2009. This is the default behavior. However, setting this to nil allows a
  2010. convenient way to select a TODO state and bypass any logging associated
  2011. with that."
  2012. :group 'org-todo
  2013. :type 'boolean)
  2014. (defcustom org-todo-state-tags-triggers nil
  2015. "Tag changes that should be triggered by TODO state changes.
  2016. This is a list. Each entry is
  2017. (state-change (tag . flag) .......)
  2018. State-change can be a string with a state, and empty string to indicate the
  2019. state that has no TODO keyword, or it can be one of the symbols `todo'
  2020. or `done', meaning any not-done or done state, respectively."
  2021. :group 'org-todo
  2022. :group 'org-tags
  2023. :type '(repeat
  2024. (cons (choice :tag "When changing to"
  2025. (const :tag "Not-done state" todo)
  2026. (const :tag "Done state" done)
  2027. (string :tag "State"))
  2028. (repeat
  2029. (cons :tag "Tag action"
  2030. (string :tag "Tag")
  2031. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  2032. (defcustom org-log-done nil
  2033. "Information to record when a task moves to the DONE state.
  2034. Possible values are:
  2035. nil Don't add anything, just change the keyword
  2036. time Add a time stamp to the task
  2037. note Prompt for a note and add it with template `org-log-note-headings'
  2038. This option can also be set with on a per-file-basis with
  2039. #+STARTUP: nologdone
  2040. #+STARTUP: logdone
  2041. #+STARTUP: lognotedone
  2042. You can have local logging settings for a subtree by setting the LOGGING
  2043. property to one or more of these keywords."
  2044. :group 'org-todo
  2045. :group 'org-progress
  2046. :type '(choice
  2047. (const :tag "No logging" nil)
  2048. (const :tag "Record CLOSED timestamp" time)
  2049. (const :tag "Record CLOSED timestamp with note." note)))
  2050. ;; Normalize old uses of org-log-done.
  2051. (cond
  2052. ((eq org-log-done t) (setq org-log-done 'time))
  2053. ((and (listp org-log-done) (memq 'done org-log-done))
  2054. (setq org-log-done 'note)))
  2055. (defcustom org-log-reschedule nil
  2056. "Information to record when the scheduling date of a tasks is modified.
  2057. Possible values are:
  2058. nil Don't add anything, just change the date
  2059. time Add a time stamp to the task
  2060. note Prompt for a note and add it with template `org-log-note-headings'
  2061. This option can also be set with on a per-file-basis with
  2062. #+STARTUP: nologreschedule
  2063. #+STARTUP: logreschedule
  2064. #+STARTUP: lognotereschedule"
  2065. :group 'org-todo
  2066. :group 'org-progress
  2067. :type '(choice
  2068. (const :tag "No logging" nil)
  2069. (const :tag "Record timestamp" time)
  2070. (const :tag "Record timestamp with note." note)))
  2071. (defcustom org-log-redeadline nil
  2072. "Information to record when the deadline date of a tasks is modified.
  2073. Possible values are:
  2074. nil Don't add anything, just change the date
  2075. time Add a time stamp to the task
  2076. note Prompt for a note and add it with template `org-log-note-headings'
  2077. This option can also be set with on a per-file-basis with
  2078. #+STARTUP: nologredeadline
  2079. #+STARTUP: logredeadline
  2080. #+STARTUP: lognoteredeadline
  2081. You can have local logging settings for a subtree by setting the LOGGING
  2082. property to one or more of these keywords."
  2083. :group 'org-todo
  2084. :group 'org-progress
  2085. :type '(choice
  2086. (const :tag "No logging" nil)
  2087. (const :tag "Record timestamp" time)
  2088. (const :tag "Record timestamp with note." note)))
  2089. (defcustom org-log-note-clock-out nil
  2090. "Non-nil means record a note when clocking out of an item.
  2091. This can also be configured on a per-file basis by adding one of
  2092. the following lines anywhere in the buffer:
  2093. #+STARTUP: lognoteclock-out
  2094. #+STARTUP: nolognoteclock-out"
  2095. :group 'org-todo
  2096. :group 'org-progress
  2097. :type 'boolean)
  2098. (defcustom org-log-done-with-time t
  2099. "Non-nil means the CLOSED time stamp will contain date and time.
  2100. When nil, only the date will be recorded."
  2101. :group 'org-progress
  2102. :type 'boolean)
  2103. (defcustom org-log-note-headings
  2104. '((done . "CLOSING NOTE %t")
  2105. (state . "State %-12s from %-12S %t")
  2106. (note . "Note taken on %t")
  2107. (reschedule . "Rescheduled from %S on %t")
  2108. (delschedule . "Not scheduled, was %S on %t")
  2109. (redeadline . "New deadline from %S on %t")
  2110. (deldeadline . "Removed deadline, was %S on %t")
  2111. (refile . "Refiled on %t")
  2112. (clock-out . ""))
  2113. "Headings for notes added to entries.
  2114. The value is an alist, with the car being a symbol indicating the note
  2115. context, and the cdr is the heading to be used. The heading may also be the
  2116. empty string.
  2117. %t in the heading will be replaced by a time stamp.
  2118. %T will be an active time stamp instead the default inactive one
  2119. %d will be replaced by a short-format time stamp.
  2120. %D will be replaced by an active short-format time stamp.
  2121. %s will be replaced by the new TODO state, in double quotes.
  2122. %S will be replaced by the old TODO state, in double quotes.
  2123. %u will be replaced by the user name.
  2124. %U will be replaced by the full user name.
  2125. In fact, it is not a good idea to change the `state' entry, because
  2126. agenda log mode depends on the format of these entries."
  2127. :group 'org-todo
  2128. :group 'org-progress
  2129. :type '(list :greedy t
  2130. (cons (const :tag "Heading when closing an item" done) string)
  2131. (cons (const :tag
  2132. "Heading when changing todo state (todo sequence only)"
  2133. state) string)
  2134. (cons (const :tag "Heading when just taking a note" note) string)
  2135. (cons (const :tag "Heading when clocking out" clock-out) string)
  2136. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2137. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2138. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2139. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2140. (cons (const :tag "Heading when refiling" refile) string)))
  2141. (unless (assq 'note org-log-note-headings)
  2142. (push '(note . "%t") org-log-note-headings))
  2143. (defcustom org-log-into-drawer nil
  2144. "Non-nil means insert state change notes and time stamps into a drawer.
  2145. When nil, state changes notes will be inserted after the headline and
  2146. any scheduling and clock lines, but not inside a drawer.
  2147. The value of this variable should be the name of the drawer to use.
  2148. LOGBOOK is proposed as the default drawer for this purpose, you can
  2149. also set this to a string to define the drawer of your choice.
  2150. A value of t is also allowed, representing \"LOGBOOK\".
  2151. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2152. will be ignored.
  2153. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2154. a subtree."
  2155. :group 'org-todo
  2156. :group 'org-progress
  2157. :type '(choice
  2158. (const :tag "Not into a drawer" nil)
  2159. (const :tag "LOGBOOK" t)
  2160. (string :tag "Other")))
  2161. (if (fboundp 'defvaralias)
  2162. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  2163. (defun org-log-into-drawer ()
  2164. "Return the value of `org-log-into-drawer', but let properties overrule.
  2165. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  2166. used instead of the default value."
  2167. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit)))
  2168. (cond
  2169. ((or (not p) (equal p "nil")) org-log-into-drawer)
  2170. ((equal p "t") "LOGBOOK")
  2171. (t p))))
  2172. (defcustom org-log-state-notes-insert-after-drawers nil
  2173. "Non-nil means insert state change notes after any drawers in entry.
  2174. Only the drawers that *immediately* follow the headline and the
  2175. deadline/scheduled line are skipped.
  2176. When nil, insert notes right after the heading and perhaps the line
  2177. with deadline/scheduling if present.
  2178. This variable will have no effect if `org-log-into-drawer' is
  2179. set."
  2180. :group 'org-todo
  2181. :group 'org-progress
  2182. :type 'boolean)
  2183. (defcustom org-log-states-order-reversed t
  2184. "Non-nil means the latest state note will be directly after heading.
  2185. When nil, the state change notes will be ordered according to time."
  2186. :group 'org-todo
  2187. :group 'org-progress
  2188. :type 'boolean)
  2189. (defcustom org-todo-repeat-to-state nil
  2190. "The TODO state to which a repeater should return the repeating task.
  2191. By default this is the first task in a TODO sequence, or the previous state
  2192. in a TODO_TYP set. But you can specify another task here.
  2193. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2194. :group 'org-todo
  2195. :version "24.1"
  2196. :type '(choice (const :tag "Head of sequence" nil)
  2197. (string :tag "Specific state")))
  2198. (defcustom org-log-repeat 'time
  2199. "Non-nil means record moving through the DONE state when triggering repeat.
  2200. An auto-repeating task is immediately switched back to TODO when
  2201. marked DONE. If you are not logging state changes (by adding \"@\"
  2202. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2203. record a closing note, there will be no record of the task moving
  2204. through DONE. This variable forces taking a note anyway.
  2205. nil Don't force a record
  2206. time Record a time stamp
  2207. note Record a note
  2208. This option can also be set with on a per-file-basis with
  2209. #+STARTUP: logrepeat
  2210. #+STARTUP: lognoterepeat
  2211. #+STARTUP: nologrepeat
  2212. You can have local logging settings for a subtree by setting the LOGGING
  2213. property to one or more of these keywords."
  2214. :group 'org-todo
  2215. :group 'org-progress
  2216. :type '(choice
  2217. (const :tag "Don't force a record" nil)
  2218. (const :tag "Force recording the DONE state" time)
  2219. (const :tag "Force recording a note with the DONE state" note)))
  2220. (defgroup org-priorities nil
  2221. "Priorities in Org-mode."
  2222. :tag "Org Priorities"
  2223. :group 'org-todo)
  2224. (defcustom org-enable-priority-commands t
  2225. "Non-nil means priority commands are active.
  2226. When nil, these commands will be disabled, so that you never accidentally
  2227. set a priority."
  2228. :group 'org-priorities
  2229. :type 'boolean)
  2230. (defcustom org-highest-priority ?A
  2231. "The highest priority of TODO items. A character like ?A, ?B etc.
  2232. Must have a smaller ASCII number than `org-lowest-priority'."
  2233. :group 'org-priorities
  2234. :type 'character)
  2235. (defcustom org-lowest-priority ?C
  2236. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2237. Must have a larger ASCII number than `org-highest-priority'."
  2238. :group 'org-priorities
  2239. :type 'character)
  2240. (defcustom org-default-priority ?B
  2241. "The default priority of TODO items.
  2242. This is the priority an item gets if no explicit priority is given.
  2243. When starting to cycle on an empty priority the first step in the cycle
  2244. depends on `org-priority-start-cycle-with-default'. The resulting first
  2245. step priority must not exceed the range from `org-highest-priority' to
  2246. `org-lowest-priority' which means that `org-default-priority' has to be
  2247. in this range exclusive or inclusive the range boundaries. Else the
  2248. first step refuses to set the default and the second will fall back
  2249. to (depending on the command used) the highest or lowest priority."
  2250. :group 'org-priorities
  2251. :type 'character)
  2252. (defcustom org-priority-start-cycle-with-default t
  2253. "Non-nil means start with default priority when starting to cycle.
  2254. When this is nil, the first step in the cycle will be (depending on the
  2255. command used) one higher or lower than the default priority.
  2256. See also `org-default-priority'."
  2257. :group 'org-priorities
  2258. :type 'boolean)
  2259. (defcustom org-get-priority-function nil
  2260. "Function to extract the priority from a string.
  2261. The string is normally the headline. If this is nil Org computes the
  2262. priority from the priority cookie like [#A] in the headline. It returns
  2263. an integer, increasing by 1000 for each priority level.
  2264. The user can set a different function here, which should take a string
  2265. as an argument and return the numeric priority."
  2266. :group 'org-priorities
  2267. :version "24.1"
  2268. :type 'function)
  2269. (defgroup org-time nil
  2270. "Options concerning time stamps and deadlines in Org-mode."
  2271. :tag "Org Time"
  2272. :group 'org)
  2273. (defcustom org-insert-labeled-timestamps-at-point nil
  2274. "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
  2275. When nil, these labeled time stamps are forces into the second line of an
  2276. entry, just after the headline. When scheduling from the global TODO list,
  2277. the time stamp will always be forced into the second line."
  2278. :group 'org-time
  2279. :type 'boolean)
  2280. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  2281. "Formats for `format-time-string' which are used for time stamps.
  2282. It is not recommended to change this constant.")
  2283. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2284. "Number of minutes to round time stamps to.
  2285. These are two values, the first applies when first creating a time stamp.
  2286. The second applies when changing it with the commands `S-up' and `S-down'.
  2287. When changing the time stamp, this means that it will change in steps
  2288. of N minutes, as given by the second value.
  2289. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2290. numbers should be factors of 60, so for example 5, 10, 15.
  2291. When this is larger than 1, you can still force an exact time stamp by using
  2292. a double prefix argument to a time stamp command like `C-c .' or `C-c !',
  2293. and by using a prefix arg to `S-up/down' to specify the exact number
  2294. of minutes to shift."
  2295. :group 'org-time
  2296. :get #'(lambda (var) ; Make sure both elements are there
  2297. (if (integerp (default-value var))
  2298. (list (default-value var) 5)
  2299. (default-value var)))
  2300. :type '(list
  2301. (integer :tag "when inserting times")
  2302. (integer :tag "when modifying times")))
  2303. ;; Normalize old customizations of this variable.
  2304. (when (integerp org-time-stamp-rounding-minutes)
  2305. (setq org-time-stamp-rounding-minutes
  2306. (list org-time-stamp-rounding-minutes
  2307. org-time-stamp-rounding-minutes)))
  2308. (defcustom org-display-custom-times nil
  2309. "Non-nil means overlay custom formats over all time stamps.
  2310. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2311. To turn this on on a per-file basis, insert anywhere in the file:
  2312. #+STARTUP: customtime"
  2313. :group 'org-time
  2314. :set 'set-default
  2315. :type 'sexp)
  2316. (make-variable-buffer-local 'org-display-custom-times)
  2317. (defcustom org-time-stamp-custom-formats
  2318. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2319. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2320. These are overlaid over the default ISO format if the variable
  2321. `org-display-custom-times' is set. Time like %H:%M should be at the
  2322. end of the second format. The custom formats are also honored by export
  2323. commands, if custom time display is turned on at the time of export."
  2324. :group 'org-time
  2325. :type 'sexp)
  2326. (defun org-time-stamp-format (&optional long inactive)
  2327. "Get the right format for a time string."
  2328. (let ((f (if long (cdr org-time-stamp-formats)
  2329. (car org-time-stamp-formats))))
  2330. (if inactive
  2331. (concat "[" (substring f 1 -1) "]")
  2332. f)))
  2333. (defcustom org-time-clocksum-format "%d:%02d"
  2334. "The format string used when creating CLOCKSUM lines.
  2335. This is also used when org-mode generates a time duration."
  2336. :group 'org-time
  2337. :type 'string)
  2338. (defcustom org-time-clocksum-use-fractional nil
  2339. "If non-nil, \\[org-clock-display] uses fractional times.
  2340. org-mode generates a time duration."
  2341. :group 'org-time
  2342. :type 'boolean)
  2343. (defcustom org-time-clocksum-fractional-format "%.2f"
  2344. "The format string used when creating CLOCKSUM lines, or when
  2345. org-mode generates a time duration."
  2346. :group 'org-time
  2347. :type 'string)
  2348. (defcustom org-deadline-warning-days 14
  2349. "No. of days before expiration during which a deadline becomes active.
  2350. This variable governs the display in sparse trees and in the agenda.
  2351. When 0 or negative, it means use this number (the absolute value of it)
  2352. even if a deadline has a different individual lead time specified.
  2353. Custom commands can set this variable in the options section."
  2354. :group 'org-time
  2355. :group 'org-agenda-daily/weekly
  2356. :type 'integer)
  2357. (defcustom org-read-date-prefer-future t
  2358. "Non-nil means assume future for incomplete date input from user.
  2359. This affects the following situations:
  2360. 1. The user gives a month but not a year.
  2361. For example, if it is April and you enter \"feb 2\", this will be read
  2362. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2363. 2. The user gives a day, but no month.
  2364. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2365. read this as the third of *next* month. However, if you enter \"17\",
  2366. it will be considered as *this* month.
  2367. If you set this variable to the symbol `time', then also the following
  2368. will work:
  2369. 3. If the user gives a time, but no day. If the time is before now,
  2370. to will be interpreted as tomorrow.
  2371. Currently none of this works for ISO week specifications.
  2372. When this option is nil, the current day, month and year will always be
  2373. used as defaults.
  2374. See also `org-agenda-jump-prefer-future'."
  2375. :group 'org-time
  2376. :type '(choice
  2377. (const :tag "Never" nil)
  2378. (const :tag "Check month and day" t)
  2379. (const :tag "Check month, day, and time" time)))
  2380. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2381. "Should the agenda jump command prefer the future for incomplete dates?
  2382. The default is to do the same as configured in `org-read-date-prefer-future'.
  2383. But you can also set a deviating value here.
  2384. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2385. :group 'org-agenda
  2386. :group 'org-time
  2387. :version "24.1"
  2388. :type '(choice
  2389. (const :tag "Use org-read-date-prefer-future"
  2390. org-read-date-prefer-future)
  2391. (const :tag "Never" nil)
  2392. (const :tag "Always" t)))
  2393. (defcustom org-read-date-force-compatible-dates t
  2394. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2395. Depending on the system Emacs is running on, certain dates cannot
  2396. be represented with the type used internally to represent time.
  2397. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2398. correctly. Some systems allow for earlier dates, some for later,
  2399. some for both. One way to find out it to insert any date into an
  2400. Org buffer, putting the cursor on the year and hitting S-up and
  2401. S-down to test the range.
  2402. When this variable is set to t, the date/time prompt will not let
  2403. you specify dates outside the 1970-2037 range, so it is certain that
  2404. these dates will work in whatever version of Emacs you are
  2405. running, and also that you can move a file from one Emacs implementation
  2406. to another. WHenever Org is forcing the year for you, it will display
  2407. a message and beep.
  2408. When this variable is nil, Org will check if the date is
  2409. representable in the specific Emacs implementation you are using.
  2410. If not, it will force a year, usually the current year, and beep
  2411. to remind you. Currently this setting is not recommended because
  2412. the likelihood that you will open your Org files in an Emacs that
  2413. has limited date range is not negligible.
  2414. A workaround for this problem is to use diary sexp dates for time
  2415. stamps outside of this range."
  2416. :group 'org-time
  2417. :version "24.1"
  2418. :type 'boolean)
  2419. (defcustom org-read-date-display-live t
  2420. "Non-nil means display current interpretation of date prompt live.
  2421. This display will be in an overlay, in the minibuffer."
  2422. :group 'org-time
  2423. :type 'boolean)
  2424. (defcustom org-read-date-popup-calendar t
  2425. "Non-nil means pop up a calendar when prompting for a date.
  2426. In the calendar, the date can be selected with mouse-1. However, the
  2427. minibuffer will also be active, and you can simply enter the date as well.
  2428. When nil, only the minibuffer will be available."
  2429. :group 'org-time
  2430. :type 'boolean)
  2431. (if (fboundp 'defvaralias)
  2432. (defvaralias 'org-popup-calendar-for-date-prompt
  2433. 'org-read-date-popup-calendar))
  2434. (defcustom org-read-date-minibuffer-setup-hook nil
  2435. "Hook to be used to set up keys for the date/time interface.
  2436. Add key definitions to `minibuffer-local-map', which will be a temporary
  2437. copy."
  2438. :group 'org-time
  2439. :type 'hook)
  2440. (defcustom org-extend-today-until 0
  2441. "The hour when your day really ends. Must be an integer.
  2442. This has influence for the following applications:
  2443. - When switching the agenda to \"today\". It it is still earlier than
  2444. the time given here, the day recognized as TODAY is actually yesterday.
  2445. - When a date is read from the user and it is still before the time given
  2446. here, the current date and time will be assumed to be yesterday, 23:59.
  2447. Also, timestamps inserted in remember templates follow this rule.
  2448. IMPORTANT: This is a feature whose implementation is and likely will
  2449. remain incomplete. Really, it is only here because past midnight seems to
  2450. be the favorite working time of John Wiegley :-)"
  2451. :group 'org-time
  2452. :type 'integer)
  2453. (defcustom org-use-effective-time nil
  2454. "If non-nil, consider `org-extend-today-until' when creating timestamps.
  2455. For example, if `org-extend-today-until' is 8, and it's 4am, then the
  2456. \"effective time\" of any timestamps between midnight and 8am will be
  2457. 23:59 of the previous day."
  2458. :group 'org-time
  2459. :version "24.1"
  2460. :type 'boolean)
  2461. (defcustom org-edit-timestamp-down-means-later nil
  2462. "Non-nil means S-down will increase the time in a time stamp.
  2463. When nil, S-up will increase."
  2464. :group 'org-time
  2465. :type 'boolean)
  2466. (defcustom org-calendar-follow-timestamp-change t
  2467. "Non-nil means make the calendar window follow timestamp changes.
  2468. When a timestamp is modified and the calendar window is visible, it will be
  2469. moved to the new date."
  2470. :group 'org-time
  2471. :type 'boolean)
  2472. (defgroup org-tags nil
  2473. "Options concerning tags in Org-mode."
  2474. :tag "Org Tags"
  2475. :group 'org)
  2476. (defcustom org-tag-alist nil
  2477. "List of tags allowed in Org-mode files.
  2478. When this list is nil, Org-mode will base TAG input on what is already in the
  2479. buffer.
  2480. The value of this variable is an alist, the car of each entry must be a
  2481. keyword as a string, the cdr may be a character that is used to select
  2482. that tag through the fast-tag-selection interface.
  2483. See the manual for details."
  2484. :group 'org-tags
  2485. :type '(repeat
  2486. (choice
  2487. (cons (string :tag "Tag name")
  2488. (character :tag "Access char"))
  2489. (list :tag "Start radio group"
  2490. (const :startgroup)
  2491. (option (string :tag "Group description")))
  2492. (list :tag "End radio group"
  2493. (const :endgroup)
  2494. (option (string :tag "Group description")))
  2495. (const :tag "New line" (:newline)))))
  2496. (defcustom org-tag-persistent-alist nil
  2497. "List of tags that will always appear in all Org-mode files.
  2498. This is in addition to any in buffer settings or customizations
  2499. of `org-tag-alist'.
  2500. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2501. The value of this variable is an alist, the car of each entry must be a
  2502. keyword as a string, the cdr may be a character that is used to select
  2503. that tag through the fast-tag-selection interface.
  2504. See the manual for details.
  2505. To disable these tags on a per-file basis, insert anywhere in the file:
  2506. #+STARTUP: noptag"
  2507. :group 'org-tags
  2508. :type '(repeat
  2509. (choice
  2510. (cons (string :tag "Tag name")
  2511. (character :tag "Access char"))
  2512. (const :tag "Start radio group" (:startgroup))
  2513. (const :tag "End radio group" (:endgroup))
  2514. (const :tag "New line" (:newline)))))
  2515. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2516. "If non-nil, always offer completion for all tags of all agenda files.
  2517. Instead of customizing this variable directly, you might want to
  2518. set it locally for capture buffers, because there no list of
  2519. tags in that file can be created dynamically (there are none).
  2520. (add-hook 'org-capture-mode-hook
  2521. (lambda ()
  2522. (set (make-local-variable
  2523. 'org-complete-tags-always-offer-all-agenda-tags)
  2524. t)))"
  2525. :group 'org-tags
  2526. :version "24.1"
  2527. :type 'boolean)
  2528. (defvar org-file-tags nil
  2529. "List of tags that can be inherited by all entries in the file.
  2530. The tags will be inherited if the variable `org-use-tag-inheritance'
  2531. says they should be.
  2532. This variable is populated from #+FILETAGS lines.")
  2533. (defcustom org-use-fast-tag-selection 'auto
  2534. "Non-nil means use fast tag selection scheme.
  2535. This is a special interface to select and deselect tags with single keys.
  2536. When nil, fast selection is never used.
  2537. When the symbol `auto', fast selection is used if and only if selection
  2538. characters for tags have been configured, either through the variable
  2539. `org-tag-alist' or through a #+TAGS line in the buffer.
  2540. When t, fast selection is always used and selection keys are assigned
  2541. automatically if necessary."
  2542. :group 'org-tags
  2543. :type '(choice
  2544. (const :tag "Always" t)
  2545. (const :tag "Never" nil)
  2546. (const :tag "When selection characters are configured" 'auto)))
  2547. (defcustom org-fast-tag-selection-single-key nil
  2548. "Non-nil means fast tag selection exits after first change.
  2549. When nil, you have to press RET to exit it.
  2550. During fast tag selection, you can toggle this flag with `C-c'.
  2551. This variable can also have the value `expert'. In this case, the window
  2552. displaying the tags menu is not even shown, until you press C-c again."
  2553. :group 'org-tags
  2554. :type '(choice
  2555. (const :tag "No" nil)
  2556. (const :tag "Yes" t)
  2557. (const :tag "Expert" expert)))
  2558. (defvar org-fast-tag-selection-include-todo nil
  2559. "Non-nil means fast tags selection interface will also offer TODO states.
  2560. This is an undocumented feature, you should not rely on it.")
  2561. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2562. "The column to which tags should be indented in a headline.
  2563. If this number is positive, it specifies the column. If it is negative,
  2564. it means that the tags should be flushright to that column. For example,
  2565. -80 works well for a normal 80 character screen.
  2566. When 0, place tags directly after headline text, with only one space in
  2567. between."
  2568. :group 'org-tags
  2569. :type 'integer)
  2570. (defcustom org-auto-align-tags t
  2571. "Non-nil keeps tags aligned when modifying headlines.
  2572. Some operations (i.e. demoting) change the length of a headline and
  2573. therefore shift the tags around. With this option turned on, after
  2574. each such operation the tags are again aligned to `org-tags-column'."
  2575. :group 'org-tags
  2576. :type 'boolean)
  2577. (defcustom org-use-tag-inheritance t
  2578. "Non-nil means tags in levels apply also for sublevels.
  2579. When nil, only the tags directly given in a specific line apply there.
  2580. This may also be a list of tags that should be inherited, or a regexp that
  2581. matches tags that should be inherited. Additional control is possible
  2582. with the variable `org-tags-exclude-from-inheritance' which gives an
  2583. explicit list of tags to be excluded from inheritance., even if the value of
  2584. `org-use-tag-inheritance' would select it for inheritance.
  2585. If this option is t, a match early-on in a tree can lead to a large
  2586. number of matches in the subtree when constructing the agenda or creating
  2587. a sparse tree. If you only want to see the first match in a tree during
  2588. a search, check out the variable `org-tags-match-list-sublevels'."
  2589. :group 'org-tags
  2590. :type '(choice
  2591. (const :tag "Not" nil)
  2592. (const :tag "Always" t)
  2593. (repeat :tag "Specific tags" (string :tag "Tag"))
  2594. (regexp :tag "Tags matched by regexp")))
  2595. (defcustom org-tags-exclude-from-inheritance nil
  2596. "List of tags that should never be inherited.
  2597. This is a way to exclude a few tags from inheritance. For way to do
  2598. the opposite, to actively allow inheritance for selected tags,
  2599. see the variable `org-use-tag-inheritance'."
  2600. :group 'org-tags
  2601. :type '(repeat (string :tag "Tag")))
  2602. (defun org-tag-inherit-p (tag)
  2603. "Check if TAG is one that should be inherited."
  2604. (cond
  2605. ((member tag org-tags-exclude-from-inheritance) nil)
  2606. ((eq org-use-tag-inheritance t) t)
  2607. ((not org-use-tag-inheritance) nil)
  2608. ((stringp org-use-tag-inheritance)
  2609. (string-match org-use-tag-inheritance tag))
  2610. ((listp org-use-tag-inheritance)
  2611. (member tag org-use-tag-inheritance))
  2612. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2613. (defcustom org-tags-match-list-sublevels t
  2614. "Non-nil means list also sublevels of headlines matching a search.
  2615. This variable applies to tags/property searches, and also to stuck
  2616. projects because this search is based on a tags match as well.
  2617. When set to the symbol `indented', sublevels are indented with
  2618. leading dots.
  2619. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2620. the sublevels of a headline matching a tag search often also match
  2621. the same search. Listing all of them can create very long lists.
  2622. Setting this variable to nil causes subtrees of a match to be skipped.
  2623. This variable is semi-obsolete and probably should always be true. It
  2624. is better to limit inheritance to certain tags using the variables
  2625. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2626. :group 'org-tags
  2627. :type '(choice
  2628. (const :tag "No, don't list them" nil)
  2629. (const :tag "Yes, do list them" t)
  2630. (const :tag "List them, indented with leading dots" indented)))
  2631. (defcustom org-tags-sort-function nil
  2632. "When set, tags are sorted using this function as a comparator."
  2633. :group 'org-tags
  2634. :type '(choice
  2635. (const :tag "No sorting" nil)
  2636. (const :tag "Alphabetical" string<)
  2637. (const :tag "Reverse alphabetical" string>)
  2638. (function :tag "Custom function" nil)))
  2639. (defvar org-tags-history nil
  2640. "History of minibuffer reads for tags.")
  2641. (defvar org-last-tags-completion-table nil
  2642. "The last used completion table for tags.")
  2643. (defvar org-after-tags-change-hook nil
  2644. "Hook that is run after the tags in a line have changed.")
  2645. (defgroup org-properties nil
  2646. "Options concerning properties in Org-mode."
  2647. :tag "Org Properties"
  2648. :group 'org)
  2649. (defcustom org-property-format "%-10s %s"
  2650. "How property key/value pairs should be formatted by `indent-line'.
  2651. When `indent-line' hits a property definition, it will format the line
  2652. according to this format, mainly to make sure that the values are
  2653. lined-up with respect to each other."
  2654. :group 'org-properties
  2655. :type 'string)
  2656. (defcustom org-properties-postprocess-alist nil
  2657. "Alist of properties and functions to adjust inserted values.
  2658. Elements of this alist must be of the form
  2659. ([string] [function])
  2660. where [string] must be a property name and [function] must be a
  2661. lambda expression: this lambda expression must take one argument,
  2662. the value to adjust, and return the new value as a string.
  2663. For example, this element will allow the property \"Remaining\"
  2664. to be updated wrt the relation between the \"Effort\" property
  2665. and the clock summary:
  2666. ((\"Remaining\" (lambda(value)
  2667. (let ((clocksum (org-clock-sum-current-item))
  2668. (effort (org-duration-string-to-minutes
  2669. (org-entry-get (point) \"Effort\"))))
  2670. (org-minutes-to-hh:mm-string (- effort clocksum))))))"
  2671. :group 'org-properties
  2672. :version "24.1"
  2673. :type '(alist :key-type (string :tag "Property")
  2674. :value-type (function :tag "Function")))
  2675. (defcustom org-use-property-inheritance nil
  2676. "Non-nil means properties apply also for sublevels.
  2677. This setting is chiefly used during property searches. Turning it on can
  2678. cause significant overhead when doing a search, which is why it is not
  2679. on by default.
  2680. When nil, only the properties directly given in the current entry count.
  2681. When t, every property is inherited. The value may also be a list of
  2682. properties that should have inheritance, or a regular expression matching
  2683. properties that should be inherited.
  2684. However, note that some special properties use inheritance under special
  2685. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2686. and the properties ending in \"_ALL\" when they are used as descriptor
  2687. for valid values of a property.
  2688. Note for programmers:
  2689. When querying an entry with `org-entry-get', you can control if inheritance
  2690. should be used. By default, `org-entry-get' looks only at the local
  2691. properties. You can request inheritance by setting the inherit argument
  2692. to t (to force inheritance) or to `selective' (to respect the setting
  2693. in this variable)."
  2694. :group 'org-properties
  2695. :type '(choice
  2696. (const :tag "Not" nil)
  2697. (const :tag "Always" t)
  2698. (repeat :tag "Specific properties" (string :tag "Property"))
  2699. (regexp :tag "Properties matched by regexp")))
  2700. (defun org-property-inherit-p (property)
  2701. "Check if PROPERTY is one that should be inherited."
  2702. (cond
  2703. ((eq org-use-property-inheritance t) t)
  2704. ((not org-use-property-inheritance) nil)
  2705. ((stringp org-use-property-inheritance)
  2706. (string-match org-use-property-inheritance property))
  2707. ((listp org-use-property-inheritance)
  2708. (member property org-use-property-inheritance))
  2709. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2710. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2711. "The default column format, if no other format has been defined.
  2712. This variable can be set on the per-file basis by inserting a line
  2713. #+COLUMNS: %25ITEM ....."
  2714. :group 'org-properties
  2715. :type 'string)
  2716. (defcustom org-columns-ellipses ".."
  2717. "The ellipses to be used when a field in column view is truncated.
  2718. When this is the empty string, as many characters as possible are shown,
  2719. but then there will be no visual indication that the field has been truncated.
  2720. When this is a string of length N, the last N characters of a truncated
  2721. field are replaced by this string. If the column is narrower than the
  2722. ellipses string, only part of the ellipses string will be shown."
  2723. :group 'org-properties
  2724. :type 'string)
  2725. (defcustom org-columns-modify-value-for-display-function nil
  2726. "Function that modifies values for display in column view.
  2727. For example, it can be used to cut out a certain part from a time stamp.
  2728. The function must take 2 arguments:
  2729. column-title The title of the column (*not* the property name)
  2730. value The value that should be modified.
  2731. The function should return the value that should be displayed,
  2732. or nil if the normal value should be used."
  2733. :group 'org-properties
  2734. :type 'function)
  2735. (defcustom org-effort-property "Effort"
  2736. "The property that is being used to keep track of effort estimates.
  2737. Effort estimates given in this property need to have the format H:MM."
  2738. :group 'org-properties
  2739. :group 'org-progress
  2740. :type '(string :tag "Property"))
  2741. (defconst org-global-properties-fixed
  2742. '(("VISIBILITY_ALL" . "folded children content all")
  2743. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  2744. "List of property/value pairs that can be inherited by any entry.
  2745. These are fixed values, for the preset properties. The user variable
  2746. that can be used to add to this list is `org-global-properties'.
  2747. The entries in this list are cons cells where the car is a property
  2748. name and cdr is a string with the value. If the value represents
  2749. multiple items like an \"_ALL\" property, separate the items by
  2750. spaces.")
  2751. (defcustom org-global-properties nil
  2752. "List of property/value pairs that can be inherited by any entry.
  2753. This list will be combined with the constant `org-global-properties-fixed'.
  2754. The entries in this list are cons cells where the car is a property
  2755. name and cdr is a string with the value.
  2756. You can set buffer-local values for the same purpose in the variable
  2757. `org-file-properties' this by adding lines like
  2758. #+PROPERTY: NAME VALUE"
  2759. :group 'org-properties
  2760. :type '(repeat
  2761. (cons (string :tag "Property")
  2762. (string :tag "Value"))))
  2763. (defvar org-file-properties nil
  2764. "List of property/value pairs that can be inherited by any entry.
  2765. Valid for the current buffer.
  2766. This variable is populated from #+PROPERTY lines.")
  2767. (make-variable-buffer-local 'org-file-properties)
  2768. (defgroup org-agenda nil
  2769. "Options concerning agenda views in Org-mode."
  2770. :tag "Org Agenda"
  2771. :group 'org)
  2772. (defvar org-category nil
  2773. "Variable used by org files to set a category for agenda display.
  2774. Such files should use a file variable to set it, for example
  2775. # -*- mode: org; org-category: \"ELisp\"
  2776. or contain a special line
  2777. #+CATEGORY: ELisp
  2778. If the file does not specify a category, then file's base name
  2779. is used instead.")
  2780. (make-variable-buffer-local 'org-category)
  2781. (put 'org-category 'safe-local-variable #'(lambda (x) (or (symbolp x) (stringp x))))
  2782. (defcustom org-agenda-files nil
  2783. "The files to be used for agenda display.
  2784. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2785. \\[org-remove-file]. You can also use customize to edit the list.
  2786. If an entry is a directory, all files in that directory that are matched by
  2787. `org-agenda-file-regexp' will be part of the file list.
  2788. If the value of the variable is not a list but a single file name, then
  2789. the list of agenda files is actually stored and maintained in that file, one
  2790. agenda file per line. In this file paths can be given relative to
  2791. `org-directory'. Tilde expansion and environment variable substitution
  2792. are also made."
  2793. :group 'org-agenda
  2794. :type '(choice
  2795. (repeat :tag "List of files and directories" file)
  2796. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2797. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2798. "Regular expression to match files for `org-agenda-files'.
  2799. If any element in the list in that variable contains a directory instead
  2800. of a normal file, all files in that directory that are matched by this
  2801. regular expression will be included."
  2802. :group 'org-agenda
  2803. :type 'regexp)
  2804. (defcustom org-agenda-text-search-extra-files nil
  2805. "List of extra files to be searched by text search commands.
  2806. These files will be search in addition to the agenda files by the
  2807. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2808. Note that these files will only be searched for text search commands,
  2809. not for the other agenda views like todo lists, tag searches or the weekly
  2810. agenda. This variable is intended to list notes and possibly archive files
  2811. that should also be searched by these two commands.
  2812. In fact, if the first element in the list is the symbol `agenda-archives',
  2813. than all archive files of all agenda files will be added to the search
  2814. scope."
  2815. :group 'org-agenda
  2816. :type '(set :greedy t
  2817. (const :tag "Agenda Archives" agenda-archives)
  2818. (repeat :inline t (file))))
  2819. (if (fboundp 'defvaralias)
  2820. (defvaralias 'org-agenda-multi-occur-extra-files
  2821. 'org-agenda-text-search-extra-files))
  2822. (defcustom org-agenda-skip-unavailable-files nil
  2823. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2824. A nil value means to remove them, after a query, from the list."
  2825. :group 'org-agenda
  2826. :type 'boolean)
  2827. (defcustom org-calendar-to-agenda-key [?c]
  2828. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2829. The command `org-calendar-goto-agenda' will be bound to this key. The
  2830. default is the character `c' because then `c' can be used to switch back and
  2831. forth between agenda and calendar."
  2832. :group 'org-agenda
  2833. :type 'sexp)
  2834. (defcustom org-calendar-agenda-action-key [?k]
  2835. "The key to be installed in `calendar-mode-map' for agenda-action.
  2836. The command `org-agenda-action' will be bound to this key. The
  2837. default is the character `k' because we use the same key in the agenda."
  2838. :group 'org-agenda
  2839. :type 'sexp)
  2840. (defcustom org-calendar-insert-diary-entry-key [?i]
  2841. "The key to be installed in `calendar-mode-map' for adding diary entries.
  2842. This option is irrelevant until `org-agenda-diary-file' has been configured
  2843. to point to an Org-mode file. When that is the case, the command
  2844. `org-agenda-diary-entry' will be bound to the key given here, by default
  2845. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  2846. if you want to continue doing this, you need to change this to a different
  2847. key."
  2848. :group 'org-agenda
  2849. :type 'sexp)
  2850. (defcustom org-agenda-diary-file 'diary-file
  2851. "File to which to add new entries with the `i' key in agenda and calendar.
  2852. When this is the symbol `diary-file', the functionality in the Emacs
  2853. calendar will be used to add entries to the `diary-file'. But when this
  2854. points to a file, `org-agenda-diary-entry' will be used instead."
  2855. :group 'org-agenda
  2856. :type '(choice
  2857. (const :tag "The standard Emacs diary file" diary-file)
  2858. (file :tag "Special Org file diary entries")))
  2859. (eval-after-load "calendar"
  2860. '(progn
  2861. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2862. 'org-calendar-goto-agenda)
  2863. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2864. 'org-agenda-action)
  2865. (add-hook 'calendar-mode-hook
  2866. (lambda ()
  2867. (unless (eq org-agenda-diary-file 'diary-file)
  2868. (define-key calendar-mode-map
  2869. org-calendar-insert-diary-entry-key
  2870. 'org-agenda-diary-entry))))))
  2871. (defgroup org-latex nil
  2872. "Options for embedding LaTeX code into Org-mode."
  2873. :tag "Org LaTeX"
  2874. :group 'org)
  2875. (defcustom org-format-latex-options
  2876. '(:foreground default :background default :scale 1.0
  2877. :html-foreground "Black" :html-background "Transparent"
  2878. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2879. "Options for creating images from LaTeX fragments.
  2880. This is a property list with the following properties:
  2881. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2882. `default' means use the foreground of the default face.
  2883. :background the background color, or \"Transparent\".
  2884. `default' means use the background of the default face.
  2885. :scale a scaling factor for the size of the images, to get more pixels
  2886. :html-foreground, :html-background, :html-scale
  2887. the same numbers for HTML export.
  2888. :matchers a list indicating which matchers should be used to
  2889. find LaTeX fragments. Valid members of this list are:
  2890. \"begin\" find environments
  2891. \"$1\" find single characters surrounded by $.$
  2892. \"$\" find math expressions surrounded by $...$
  2893. \"$$\" find math expressions surrounded by $$....$$
  2894. \"\\(\" find math expressions surrounded by \\(...\\)
  2895. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2896. :group 'org-latex
  2897. :type 'plist)
  2898. (defcustom org-format-latex-signal-error t
  2899. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  2900. When nil, just push out a message."
  2901. :group 'org-latex
  2902. :version "24.1"
  2903. :type 'boolean)
  2904. (defcustom org-latex-to-mathml-jar-file nil
  2905. "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
  2906. Use this to specify additional executable file say a jar file.
  2907. When using MathToWeb as the converter, specify the full-path to
  2908. your mathtoweb.jar file."
  2909. :group 'org-latex
  2910. :version "24.1"
  2911. :type '(choice
  2912. (const :tag "None" nil)
  2913. (file :tag "JAR file" :must-match t)))
  2914. (defcustom org-latex-to-mathml-convert-command nil
  2915. "Command to convert LaTeX fragments to MathML.
  2916. Replace format-specifiers in the command as noted below and use
  2917. `shell-command' to convert LaTeX to MathML.
  2918. %j: Executable file in fully expanded form as specified by
  2919. `org-latex-to-mathml-jar-file'.
  2920. %I: Input LaTeX file in fully expanded form
  2921. %o: Output MathML file
  2922. This command is used by `org-create-math-formula'.
  2923. When using MathToWeb as the converter, set this to
  2924. \"java -jar %j -unicode -force -df %o %I\"."
  2925. :group 'org-latex
  2926. :version "24.1"
  2927. :type '(choice
  2928. (const :tag "None" nil)
  2929. (string :tag "\nShell command")))
  2930. (defcustom org-latex-create-formula-image-program 'dvipng
  2931. "Program to convert LaTeX fragments with.
  2932. dvipng Process the LaTeX fragments to dvi file, then convert
  2933. dvi files to png files using dvipng.
  2934. This will also include processing of non-math environments.
  2935. imagemagick Convert the LaTeX fragments to pdf files and use imagemagick
  2936. to convert pdf files to png files"
  2937. :group 'org-latex
  2938. :version "24.1"
  2939. :type '(choice
  2940. (const :tag "dvipng" dvipng)
  2941. (const :tag "imagemagick" imagemagick)))
  2942. (defun org-format-latex-mathml-available-p ()
  2943. "Return t if `org-latex-to-mathml-convert-command' is usable."
  2944. (save-match-data
  2945. (when (and (boundp 'org-latex-to-mathml-convert-command)
  2946. org-latex-to-mathml-convert-command)
  2947. (let ((executable (car (split-string
  2948. org-latex-to-mathml-convert-command))))
  2949. (when (executable-find executable)
  2950. (if (string-match
  2951. "%j" org-latex-to-mathml-convert-command)
  2952. (file-readable-p org-latex-to-mathml-jar-file)
  2953. t))))))
  2954. (defcustom org-format-latex-header "\\documentclass{article}
  2955. \\usepackage[usenames]{color}
  2956. \\usepackage{amsmath}
  2957. \\usepackage[mathscr]{eucal}
  2958. \\pagestyle{empty} % do not remove
  2959. \[PACKAGES]
  2960. \[DEFAULT-PACKAGES]
  2961. % The settings below are copied from fullpage.sty
  2962. \\setlength{\\textwidth}{\\paperwidth}
  2963. \\addtolength{\\textwidth}{-3cm}
  2964. \\setlength{\\oddsidemargin}{1.5cm}
  2965. \\addtolength{\\oddsidemargin}{-2.54cm}
  2966. \\setlength{\\evensidemargin}{\\oddsidemargin}
  2967. \\setlength{\\textheight}{\\paperheight}
  2968. \\addtolength{\\textheight}{-\\headheight}
  2969. \\addtolength{\\textheight}{-\\headsep}
  2970. \\addtolength{\\textheight}{-\\footskip}
  2971. \\addtolength{\\textheight}{-3cm}
  2972. \\setlength{\\topmargin}{1.5cm}
  2973. \\addtolength{\\topmargin}{-2.54cm}"
  2974. "The document header used for processing LaTeX fragments.
  2975. It is imperative that this header make sure that no page number
  2976. appears on the page. The package defined in the variables
  2977. `org-export-latex-default-packages-alist' and `org-export-latex-packages-alist'
  2978. will either replace the placeholder \"[PACKAGES]\" in this header, or they
  2979. will be appended."
  2980. :group 'org-latex
  2981. :type 'string)
  2982. (defvar org-format-latex-header-extra nil)
  2983. (defun org-set-packages-alist (var val)
  2984. "Set the packages alist and make sure it has 3 elements per entry."
  2985. (set var (mapcar (lambda (x)
  2986. (if (and (consp x) (= (length x) 2))
  2987. (list (car x) (nth 1 x) t)
  2988. x))
  2989. val)))
  2990. (defun org-get-packages-alist (var)
  2991. "Get the packages alist and make sure it has 3 elements per entry."
  2992. (mapcar (lambda (x)
  2993. (if (and (consp x) (= (length x) 2))
  2994. (list (car x) (nth 1 x) t)
  2995. x))
  2996. (default-value var)))
  2997. ;; The following variables are defined here because is it also used
  2998. ;; when formatting latex fragments. Originally it was part of the
  2999. ;; LaTeX exporter, which is why the name includes "export".
  3000. (defcustom org-export-latex-default-packages-alist
  3001. '(("AUTO" "inputenc" t)
  3002. ("T1" "fontenc" t)
  3003. ("" "fixltx2e" nil)
  3004. ("" "graphicx" t)
  3005. ("" "longtable" nil)
  3006. ("" "float" nil)
  3007. ("" "wrapfig" nil)
  3008. ("" "soul" t)
  3009. ("" "textcomp" t)
  3010. ("" "marvosym" t)
  3011. ("" "wasysym" t)
  3012. ("" "latexsym" t)
  3013. ("" "amssymb" t)
  3014. ("" "hyperref" nil)
  3015. "\\tolerance=1000"
  3016. )
  3017. "Alist of default packages to be inserted in the header.
  3018. Change this only if one of the packages here causes an incompatibility
  3019. with another package you are using.
  3020. The packages in this list are needed by one part or another of Org-mode
  3021. to function properly.
  3022. - inputenc, fontenc: for basic font and character selection
  3023. - textcomp, marvosymb, wasysym, latexsym, amssym: for various symbols used
  3024. for interpreting the entities in `org-entities'. You can skip some of these
  3025. packages if you don't use any of the symbols in it.
  3026. - graphicx: for including images
  3027. - float, wrapfig: for figure placement
  3028. - longtable: for long tables
  3029. - hyperref: for cross references
  3030. Therefore you should not modify this variable unless you know what you
  3031. are doing. The one reason to change it anyway is that you might be loading
  3032. some other package that conflicts with one of the default packages.
  3033. Each cell is of the format \( \"options\" \"package\" snippet-flag\).
  3034. If SNIPPET-FLAG is t, the package also needs to be included when
  3035. compiling LaTeX snippets into images for inclusion into HTML."
  3036. :group 'org-export-latex
  3037. :set 'org-set-packages-alist
  3038. :get 'org-get-packages-alist
  3039. :version "24.1"
  3040. :type '(repeat
  3041. (choice
  3042. (list :tag "options/package pair"
  3043. (string :tag "options")
  3044. (string :tag "package")
  3045. (boolean :tag "Snippet"))
  3046. (string :tag "A line of LaTeX"))))
  3047. (defcustom org-export-latex-packages-alist nil
  3048. "Alist of packages to be inserted in every LaTeX header.
  3049. These will be inserted after `org-export-latex-default-packages-alist'.
  3050. Each cell is of the format \( \"options\" \"package\" snippet-flag \).
  3051. SNIPPET-FLAG, when t, indicates that this package is also needed when
  3052. turning LaTeX snippets into images for inclusion into HTML.
  3053. Make sure that you only list packages here which:
  3054. - you want in every file
  3055. - do not conflict with the default packages in
  3056. `org-export-latex-default-packages-alist'
  3057. - do not conflict with the setup in `org-format-latex-header'."
  3058. :group 'org-export-latex
  3059. :set 'org-set-packages-alist
  3060. :get 'org-get-packages-alist
  3061. :type '(repeat
  3062. (choice
  3063. (list :tag "options/package pair"
  3064. (string :tag "options")
  3065. (string :tag "package")
  3066. (boolean :tag "Snippet"))
  3067. (string :tag "A line of LaTeX"))))
  3068. (defgroup org-appearance nil
  3069. "Settings for Org-mode appearance."
  3070. :tag "Org Appearance"
  3071. :group 'org)
  3072. (defcustom org-level-color-stars-only nil
  3073. "Non-nil means fontify only the stars in each headline.
  3074. When nil, the entire headline is fontified.
  3075. Changing it requires restart of `font-lock-mode' to become effective
  3076. also in regions already fontified."
  3077. :group 'org-appearance
  3078. :type 'boolean)
  3079. (defcustom org-hide-leading-stars nil
  3080. "Non-nil means hide the first N-1 stars in a headline.
  3081. This works by using the face `org-hide' for these stars. This
  3082. face is white for a light background, and black for a dark
  3083. background. You may have to customize the face `org-hide' to
  3084. make this work.
  3085. Changing it requires restart of `font-lock-mode' to become effective
  3086. also in regions already fontified.
  3087. You may also set this on a per-file basis by adding one of the following
  3088. lines to the buffer:
  3089. #+STARTUP: hidestars
  3090. #+STARTUP: showstars"
  3091. :group 'org-appearance
  3092. :type 'boolean)
  3093. (defcustom org-hidden-keywords nil
  3094. "List of symbols corresponding to keywords to be hidden the org buffer.
  3095. For example, a value '(title) for this list will make the document's title
  3096. appear in the buffer without the initial #+TITLE: keyword."
  3097. :group 'org-appearance
  3098. :version "24.1"
  3099. :type '(set (const :tag "#+AUTHOR" author)
  3100. (const :tag "#+DATE" date)
  3101. (const :tag "#+EMAIL" email)
  3102. (const :tag "#+TITLE" title)))
  3103. (defcustom org-fontify-done-headline nil
  3104. "Non-nil means change the face of a headline if it is marked DONE.
  3105. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3106. When this is non-nil, the headline after the keyword is set to the
  3107. `org-headline-done' as an additional indication."
  3108. :group 'org-appearance
  3109. :type 'boolean)
  3110. (defcustom org-fontify-emphasized-text t
  3111. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3112. Changing this variable requires a restart of Emacs to take effect."
  3113. :group 'org-appearance
  3114. :type 'boolean)
  3115. (defcustom org-fontify-whole-heading-line nil
  3116. "Non-nil means fontify the whole line for headings.
  3117. This is useful when setting a background color for the
  3118. org-level-* faces."
  3119. :group 'org-appearance
  3120. :type 'boolean)
  3121. (defcustom org-highlight-latex-fragments-and-specials nil
  3122. "Non-nil means fontify what is treated specially by the exporters."
  3123. :group 'org-appearance
  3124. :type 'boolean)
  3125. (defcustom org-hide-emphasis-markers nil
  3126. "Non-nil mean font-lock should hide the emphasis marker characters."
  3127. :group 'org-appearance
  3128. :type 'boolean)
  3129. (defcustom org-pretty-entities nil
  3130. "Non-nil means show entities as UTF8 characters.
  3131. When nil, the \\name form remains in the buffer."
  3132. :group 'org-appearance
  3133. :version "24.1"
  3134. :type 'boolean)
  3135. (defcustom org-pretty-entities-include-sub-superscripts t
  3136. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  3137. :group 'org-appearance
  3138. :version "24.1"
  3139. :type 'boolean)
  3140. (defvar org-emph-re nil
  3141. "Regular expression for matching emphasis.
  3142. After a match, the match groups contain these elements:
  3143. 0 The match of the full regular expression, including the characters
  3144. before and after the proper match
  3145. 1 The character before the proper match, or empty at beginning of line
  3146. 2 The proper match, including the leading and trailing markers
  3147. 3 The leading marker like * or /, indicating the type of highlighting
  3148. 4 The text between the emphasis markers, not including the markers
  3149. 5 The character after the match, empty at the end of a line")
  3150. (defvar org-verbatim-re nil
  3151. "Regular expression for matching verbatim text.")
  3152. (defvar org-emphasis-regexp-components) ; defined just below
  3153. (defvar org-emphasis-alist) ; defined just below
  3154. (defun org-set-emph-re (var val)
  3155. "Set variable and compute the emphasis regular expression."
  3156. (set var val)
  3157. (when (and (boundp 'org-emphasis-alist)
  3158. (boundp 'org-emphasis-regexp-components)
  3159. org-emphasis-alist org-emphasis-regexp-components)
  3160. (let* ((e org-emphasis-regexp-components)
  3161. (pre (car e))
  3162. (post (nth 1 e))
  3163. (border (nth 2 e))
  3164. (body (nth 3 e))
  3165. (nl (nth 4 e))
  3166. (body1 (concat body "*?"))
  3167. (markers (mapconcat 'car org-emphasis-alist ""))
  3168. (vmarkers (mapconcat
  3169. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  3170. org-emphasis-alist "")))
  3171. ;; make sure special characters appear at the right position in the class
  3172. (if (string-match "\\^" markers)
  3173. (setq markers (concat (replace-match "" t t markers) "^")))
  3174. (if (string-match "-" markers)
  3175. (setq markers (concat (replace-match "" t t markers) "-")))
  3176. (if (string-match "\\^" vmarkers)
  3177. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3178. (if (string-match "-" vmarkers)
  3179. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3180. (if (> nl 0)
  3181. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3182. (int-to-string nl) "\\}")))
  3183. ;; Make the regexp
  3184. (setq org-emph-re
  3185. (concat "\\([" pre "]\\|^\\)"
  3186. "\\("
  3187. "\\([" markers "]\\)"
  3188. "\\("
  3189. "[^" border "]\\|"
  3190. "[^" border "]"
  3191. body1
  3192. "[^" border "]"
  3193. "\\)"
  3194. "\\3\\)"
  3195. "\\([" post "]\\|$\\)"))
  3196. (setq org-verbatim-re
  3197. (concat "\\([" pre "]\\|^\\)"
  3198. "\\("
  3199. "\\([" vmarkers "]\\)"
  3200. "\\("
  3201. "[^" border "]\\|"
  3202. "[^" border "]"
  3203. body1
  3204. "[^" border "]"
  3205. "\\)"
  3206. "\\3\\)"
  3207. "\\([" post "]\\|$\\)")))))
  3208. (defcustom org-emphasis-regexp-components
  3209. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  3210. "Components used to build the regular expression for emphasis.
  3211. This is a list with five entries. Terminology: In an emphasis string
  3212. like \" *strong word* \", we call the initial space PREMATCH, the final
  3213. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3214. and \"trong wor\" is the body. The different components in this variable
  3215. specify what is allowed/forbidden in each part:
  3216. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3217. post Chars allowed as postmatch. End of line will be allowed too.
  3218. border The chars *forbidden* as border characters.
  3219. body-regexp A regexp like \".\" to match a body character. Don't use
  3220. non-shy groups here, and don't allow newline here.
  3221. newline The maximum number of newlines allowed in an emphasis exp.
  3222. Use customize to modify this, or restart Emacs after changing it."
  3223. :group 'org-appearance
  3224. :set 'org-set-emph-re
  3225. :type '(list
  3226. (sexp :tag "Allowed chars in pre ")
  3227. (sexp :tag "Allowed chars in post ")
  3228. (sexp :tag "Forbidden chars in border ")
  3229. (sexp :tag "Regexp for body ")
  3230. (integer :tag "number of newlines allowed")
  3231. (option (boolean :tag "Please ignore this button"))))
  3232. (defcustom org-emphasis-alist
  3233. `(("*" bold "<b>" "</b>")
  3234. ("/" italic "<i>" "</i>")
  3235. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  3236. ("=" org-code "<code>" "</code>" verbatim)
  3237. ("~" org-verbatim "<code>" "</code>" verbatim)
  3238. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  3239. "<del>" "</del>")
  3240. )
  3241. "Special syntax for emphasized text.
  3242. Text starting and ending with a special character will be emphasized, for
  3243. example *bold*, _underlined_ and /italic/. This variable sets the marker
  3244. characters, the face to be used by font-lock for highlighting in Org-mode
  3245. Emacs buffers, and the HTML tags to be used for this.
  3246. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  3247. For DocBook export, see the variable `org-export-docbook-emphasis-alist'.
  3248. Use customize to modify this, or restart Emacs after changing it."
  3249. :group 'org-appearance
  3250. :set 'org-set-emph-re
  3251. :type '(repeat
  3252. (list
  3253. (string :tag "Marker character")
  3254. (choice
  3255. (face :tag "Font-lock-face")
  3256. (plist :tag "Face property list"))
  3257. (string :tag "HTML start tag")
  3258. (string :tag "HTML end tag")
  3259. (option (const verbatim)))))
  3260. (defvar org-protecting-blocks
  3261. '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
  3262. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3263. This is needed for font-lock setup.")
  3264. ;;; Miscellaneous options
  3265. (defgroup org-completion nil
  3266. "Completion in Org-mode."
  3267. :tag "Org Completion"
  3268. :group 'org)
  3269. (defcustom org-completion-use-ido nil
  3270. "Non-nil means use ido completion wherever possible.
  3271. Note that `ido-mode' must be active for this variable to be relevant.
  3272. If you decide to turn this variable on, you might well want to turn off
  3273. `org-outline-path-complete-in-steps'.
  3274. See also `org-completion-use-iswitchb'."
  3275. :group 'org-completion
  3276. :type 'boolean)
  3277. (defcustom org-completion-use-iswitchb nil
  3278. "Non-nil means use iswitchb completion wherever possible.
  3279. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3280. If you decide to turn this variable on, you might well want to turn off
  3281. `org-outline-path-complete-in-steps'.
  3282. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3283. :group 'org-completion
  3284. :type 'boolean)
  3285. (defcustom org-completion-fallback-command 'hippie-expand
  3286. "The expansion command called by \\[pcomplete] in normal context.
  3287. Normal means, no org-mode-specific context."
  3288. :group 'org-completion
  3289. :type 'function)
  3290. ;;; Functions and variables from their packages
  3291. ;; Declared here to avoid compiler warnings
  3292. ;; XEmacs only
  3293. (defvar outline-mode-menu-heading)
  3294. (defvar outline-mode-menu-show)
  3295. (defvar outline-mode-menu-hide)
  3296. (defvar zmacs-regions) ; XEmacs regions
  3297. ;; Emacs only
  3298. (defvar mark-active)
  3299. ;; Various packages
  3300. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  3301. (declare-function calendar-forward-day "cal-move" (arg))
  3302. (declare-function calendar-goto-date "cal-move" (date))
  3303. (declare-function calendar-goto-today "cal-move" ())
  3304. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3305. (defvar calc-embedded-close-formula)
  3306. (defvar calc-embedded-open-formula)
  3307. (declare-function cdlatex-tab "ext:cdlatex" ())
  3308. (declare-function cdlatex-compute-tables "ext:cdlatex" ())
  3309. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3310. (defvar font-lock-unfontify-region-function)
  3311. (declare-function iswitchb-read-buffer "iswitchb"
  3312. (prompt &optional default require-match start matches-set))
  3313. (defvar iswitchb-temp-buflist)
  3314. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3315. (defvar org-agenda-tags-todo-honor-ignore-options)
  3316. (declare-function org-agenda-skip "org-agenda" ())
  3317. (declare-function
  3318. org-agenda-format-item "org-agenda"
  3319. (extra txt &optional category tags dotime noprefix remove-re habitp))
  3320. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3321. (declare-function org-agenda-change-all-lines "org-agenda"
  3322. (newhead hdmarker &optional fixface just-this))
  3323. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3324. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3325. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3326. (beg end))
  3327. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3328. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3329. "org-agenda" (&optional end))
  3330. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3331. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3332. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3333. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3334. (declare-function org-indent-mode "org-indent" (&optional arg))
  3335. (declare-function parse-time-string "parse-time" (string))
  3336. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3337. (declare-function org-export-latex-fix-inputenc "org-latex" ())
  3338. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3339. (defvar remember-data-file)
  3340. (defvar texmathp-why)
  3341. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3342. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3343. (defvar w3m-current-url)
  3344. (defvar w3m-current-title)
  3345. (defvar org-latex-regexps)
  3346. ;;; Autoload and prepare some org modules
  3347. ;; Some table stuff that needs to be defined here, because it is used
  3348. ;; by the functions setting up org-mode or checking for table context.
  3349. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  3350. "Detect an org-type or table-type table.")
  3351. (defconst org-table-line-regexp "^[ \t]*|"
  3352. "Detect an org-type table line.")
  3353. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  3354. "Detect an org-type table line.")
  3355. (defconst org-table-hline-regexp "^[ \t]*|-"
  3356. "Detect an org-type table hline.")
  3357. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  3358. "Detect a table-type table hline.")
  3359. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  3360. "Detect the first line outside a table when searching from within it.
  3361. This works for both table types.")
  3362. ;; Autoload the functions in org-table.el that are needed by functions here.
  3363. (eval-and-compile
  3364. (org-autoload "org-table"
  3365. '(org-table-align org-table-begin org-table-blank-field
  3366. org-table-convert org-table-convert-region org-table-copy-down
  3367. org-table-copy-region org-table-create
  3368. org-table-create-or-convert-from-region
  3369. org-table-create-with-table.el org-table-current-dline
  3370. org-table-cut-region org-table-delete-column org-table-edit-field
  3371. org-table-edit-formulas org-table-end org-table-eval-formula
  3372. org-table-export org-table-field-info
  3373. org-table-get-stored-formulas org-table-goto-column
  3374. org-table-hline-and-move org-table-import org-table-insert-column
  3375. org-table-insert-hline org-table-insert-row org-table-iterate
  3376. org-table-justify-field-maybe org-table-kill-row
  3377. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  3378. org-table-move-column org-table-move-column-left
  3379. org-table-move-column-right org-table-move-row
  3380. org-table-move-row-down org-table-move-row-up
  3381. org-table-next-field org-table-next-row org-table-paste-rectangle
  3382. org-table-previous-field org-table-recalculate
  3383. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  3384. org-table-toggle-coordinate-overlays
  3385. org-table-toggle-formula-debugger org-table-wrap-region
  3386. orgtbl-mode turn-on-orgtbl org-table-to-lisp
  3387. orgtbl-to-generic orgtbl-to-tsv orgtbl-to-csv orgtbl-to-latex
  3388. orgtbl-to-orgtbl orgtbl-to-html orgtbl-to-texinfo)))
  3389. (defun org-at-table-p (&optional table-type)
  3390. "Return t if the cursor is inside an org-type table.
  3391. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  3392. (if org-enable-table-editor
  3393. (save-excursion
  3394. (beginning-of-line 1)
  3395. (looking-at (if table-type org-table-any-line-regexp
  3396. org-table-line-regexp)))
  3397. nil))
  3398. (defsubst org-table-p () (org-at-table-p))
  3399. (defun org-at-table.el-p ()
  3400. "Return t if and only if we are at a table.el table."
  3401. (and (org-at-table-p 'any)
  3402. (save-excursion
  3403. (goto-char (org-table-begin 'any))
  3404. (looking-at org-table1-hline-regexp))))
  3405. (defun org-table-recognize-table.el ()
  3406. "If there is a table.el table nearby, recognize it and move into it."
  3407. (if org-table-tab-recognizes-table.el
  3408. (if (org-at-table.el-p)
  3409. (progn
  3410. (beginning-of-line 1)
  3411. (if (looking-at org-table-dataline-regexp)
  3412. nil
  3413. (if (looking-at org-table1-hline-regexp)
  3414. (progn
  3415. (beginning-of-line 2)
  3416. (if (looking-at org-table-any-border-regexp)
  3417. (beginning-of-line -1)))))
  3418. (if (re-search-forward "|" (org-table-end t) t)
  3419. (progn
  3420. (require 'table)
  3421. (if (table--at-cell-p (point))
  3422. t
  3423. (message "recognizing table.el table...")
  3424. (table-recognize-table)
  3425. (message "recognizing table.el table...done")))
  3426. (error "This should not happen"))
  3427. t)
  3428. nil)
  3429. nil))
  3430. (defun org-at-table-hline-p ()
  3431. "Return t if the cursor is inside a hline in a table."
  3432. (if org-enable-table-editor
  3433. (save-excursion
  3434. (beginning-of-line 1)
  3435. (looking-at org-table-hline-regexp))
  3436. nil))
  3437. (defvar org-table-clean-did-remove-column nil)
  3438. (defun org-table-map-tables (function &optional quietly)
  3439. "Apply FUNCTION to the start of all tables in the buffer."
  3440. (save-excursion
  3441. (save-restriction
  3442. (widen)
  3443. (goto-char (point-min))
  3444. (while (re-search-forward org-table-any-line-regexp nil t)
  3445. (unless quietly
  3446. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
  3447. (beginning-of-line 1)
  3448. (when (and (looking-at org-table-line-regexp)
  3449. ;; Exclude tables in src/example/verbatim/clocktable blocks
  3450. (not (org-in-block-p '("src" "example"))))
  3451. (save-excursion (funcall function))
  3452. (or (looking-at org-table-line-regexp)
  3453. (forward-char 1)))
  3454. (re-search-forward org-table-any-border-regexp nil 1))))
  3455. (unless quietly (message "Mapping tables: done")))
  3456. ;; Declare and autoload functions from org-exp.el & Co
  3457. (declare-function org-default-export-plist "org-exp")
  3458. (declare-function org-infile-export-plist "org-exp")
  3459. (declare-function org-get-current-options "org-exp")
  3460. (eval-and-compile
  3461. (org-autoload "org-exp"
  3462. '(org-export org-export-visible
  3463. org-insert-export-options-template
  3464. org-table-clean-before-export))
  3465. (org-autoload "org-ascii"
  3466. '(org-export-as-ascii org-export-ascii-preprocess
  3467. org-export-as-ascii-to-buffer org-replace-region-by-ascii
  3468. org-export-region-as-ascii))
  3469. (org-autoload "org-latex"
  3470. '(org-export-as-latex-batch org-export-as-latex-to-buffer
  3471. org-replace-region-by-latex org-export-region-as-latex
  3472. org-export-as-latex org-export-as-pdf
  3473. org-export-as-pdf-and-open))
  3474. (org-autoload "org-html"
  3475. '(org-export-as-html-and-open
  3476. org-export-as-html-batch org-export-as-html-to-buffer
  3477. org-replace-region-by-html org-export-region-as-html
  3478. org-export-as-html))
  3479. (org-autoload "org-docbook"
  3480. '(org-export-as-docbook-batch org-export-as-docbook-to-buffer
  3481. org-replace-region-by-docbook org-export-region-as-docbook
  3482. org-export-as-docbook-pdf org-export-as-docbook-pdf-and-open
  3483. org-export-as-docbook))
  3484. (org-autoload "org-icalendar"
  3485. '(org-export-icalendar-this-file
  3486. org-export-icalendar-all-agenda-files
  3487. org-export-icalendar-combine-agenda-files))
  3488. (org-autoload "org-xoxo" '(org-export-as-xoxo))
  3489. (org-autoload "org-beamer" '(org-beamer-mode org-beamer-sectioning)))
  3490. ;; Declare and autoload functions from org-agenda.el
  3491. (eval-and-compile
  3492. (org-autoload "org-agenda"
  3493. '(org-agenda org-agenda-list org-search-view
  3494. org-todo-list org-tags-view org-agenda-list-stuck-projects
  3495. org-diary org-agenda-to-appt
  3496. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  3497. ;; Autoload org-remember
  3498. (eval-and-compile
  3499. (org-autoload "org-remember"
  3500. '(org-remember-insinuate org-remember-annotation
  3501. org-remember-apply-template org-remember org-remember-handler)))
  3502. (eval-and-compile
  3503. (org-autoload "org-capture"
  3504. '(org-capture org-capture-insert-template-here
  3505. org-capture-import-remember-templates)))
  3506. ;; Autoload org-clock.el
  3507. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  3508. (beg end))
  3509. (declare-function org-clock-update-mode-line "org-clock" ())
  3510. (declare-function org-resolve-clocks "org-clock"
  3511. (&optional also-non-dangling-p prompt last-valid))
  3512. (defvar org-clock-start-time)
  3513. (defvar org-clock-marker (make-marker)
  3514. "Marker recording the last clock-in.")
  3515. (defvar org-clock-hd-marker (make-marker)
  3516. "Marker recording the last clock-in, but the headline position.")
  3517. (defvar org-clock-heading ""
  3518. "The heading of the current clock entry.")
  3519. (defun org-clock-is-active ()
  3520. "Return non-nil if clock is currently running.
  3521. The return value is actually the clock marker."
  3522. (marker-buffer org-clock-marker))
  3523. (eval-and-compile
  3524. (org-autoload
  3525. "org-clock"
  3526. '(org-clock-in org-clock-out org-clock-cancel
  3527. org-clock-goto org-clock-sum org-clock-display
  3528. org-clock-remove-overlays org-clock-report
  3529. org-clocktable-shift org-dblock-write:clocktable
  3530. org-get-clocktable org-resolve-clocks)))
  3531. (defun org-clock-update-time-maybe ()
  3532. "If this is a CLOCK line, update it and return t.
  3533. Otherwise, return nil."
  3534. (interactive)
  3535. (save-excursion
  3536. (beginning-of-line 1)
  3537. (skip-chars-forward " \t")
  3538. (when (looking-at org-clock-string)
  3539. (let ((re (concat "[ \t]*" org-clock-string
  3540. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  3541. "\\([ \t]*=>.*\\)?\\)?"))
  3542. ts te h m s neg)
  3543. (cond
  3544. ((not (looking-at re))
  3545. nil)
  3546. ((not (match-end 2))
  3547. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3548. (> org-clock-marker (point))
  3549. (<= org-clock-marker (point-at-eol)))
  3550. ;; The clock is running here
  3551. (setq org-clock-start-time
  3552. (apply 'encode-time
  3553. (org-parse-time-string (match-string 1))))
  3554. (org-clock-update-mode-line)))
  3555. (t
  3556. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  3557. (end-of-line 1)
  3558. (setq ts (match-string 1)
  3559. te (match-string 3))
  3560. (setq s (- (org-float-time
  3561. (apply 'encode-time (org-parse-time-string te)))
  3562. (org-float-time
  3563. (apply 'encode-time (org-parse-time-string ts))))
  3564. neg (< s 0)
  3565. s (abs s)
  3566. h (floor (/ s 3600))
  3567. s (- s (* 3600 h))
  3568. m (floor (/ s 60))
  3569. s (- s (* 60 s)))
  3570. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  3571. t))))))
  3572. (defun org-check-running-clock ()
  3573. "Check if the current buffer contains the running clock.
  3574. If yes, offer to stop it and to save the buffer with the changes."
  3575. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3576. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3577. (buffer-name))))
  3578. (org-clock-out)
  3579. (when (y-or-n-p "Save changed buffer?")
  3580. (save-buffer))))
  3581. (defun org-clocktable-try-shift (dir n)
  3582. "Check if this line starts a clock table, if yes, shift the time block."
  3583. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  3584. (org-clocktable-shift dir n)))
  3585. ;; Autoload org-timer.el
  3586. (eval-and-compile
  3587. (org-autoload
  3588. "org-timer"
  3589. '(org-timer-start org-timer org-timer-item
  3590. org-timer-change-times-in-region
  3591. org-timer-set-timer
  3592. org-timer-reset-timers
  3593. org-timer-show-remaining-time)))
  3594. ;; Autoload org-feed.el
  3595. (eval-and-compile
  3596. (org-autoload
  3597. "org-feed"
  3598. '(org-feed-update org-feed-update-all org-feed-goto-inbox)))
  3599. ;; Autoload org-indent.el
  3600. ;; Define the variable already here, to make sure we have it.
  3601. (defvar org-indent-mode nil
  3602. "Non-nil if Org-Indent mode is enabled.
  3603. Use the command `org-indent-mode' to change this variable.")
  3604. (eval-and-compile
  3605. (org-autoload
  3606. "org-indent"
  3607. '(org-indent-mode)))
  3608. ;; Autoload org-mobile.el
  3609. (eval-and-compile
  3610. (org-autoload
  3611. "org-mobile"
  3612. '(org-mobile-push org-mobile-pull org-mobile-create-sumo-agenda)))
  3613. ;; Autoload archiving code
  3614. ;; The stuff that is needed for cycling and tags has to be defined here.
  3615. (defgroup org-archive nil
  3616. "Options concerning archiving in Org-mode."
  3617. :tag "Org Archive"
  3618. :group 'org-structure)
  3619. (defcustom org-archive-location "%s_archive::"
  3620. "The location where subtrees should be archived.
  3621. The value of this variable is a string, consisting of two parts,
  3622. separated by a double-colon. The first part is a filename and
  3623. the second part is a headline.
  3624. When the filename is omitted, archiving happens in the same file.
  3625. %s in the filename will be replaced by the current file
  3626. name (without the directory part). Archiving to a different file
  3627. is useful to keep archived entries from contributing to the
  3628. Org-mode Agenda.
  3629. The archived entries will be filed as subtrees of the specified
  3630. headline. When the headline is omitted, the subtrees are simply
  3631. filed away at the end of the file, as top-level entries. Also in
  3632. the heading you can use %s to represent the file name, this can be
  3633. useful when using the same archive for a number of different files.
  3634. Here are a few examples:
  3635. \"%s_archive::\"
  3636. If the current file is Projects.org, archive in file
  3637. Projects.org_archive, as top-level trees. This is the default.
  3638. \"::* Archived Tasks\"
  3639. Archive in the current file, under the top-level headline
  3640. \"* Archived Tasks\".
  3641. \"~/org/archive.org::\"
  3642. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3643. \"~/org/archive.org::* From %s\"
  3644. Archive in file ~/org/archive.org (absolute path), under headlines
  3645. \"From FILENAME\" where file name is the current file name.
  3646. \"~/org/datetree.org::datetree/* Finished Tasks\"
  3647. The \"datetree/\" string is special, signifying to archive
  3648. items to the datetree. Items are placed in either the CLOSED
  3649. date of the item, or the current date if there is no CLOSED date.
  3650. The heading will be a subentry to the current date. There doesn't
  3651. need to be a heading, but there always needs to be a slash after
  3652. datetree. For example, to store archived items directly in the
  3653. datetree, use \"~/org/datetree.org::datetree/\".
  3654. \"basement::** Finished Tasks\"
  3655. Archive in file ./basement (relative path), as level 3 trees
  3656. below the level 2 heading \"** Finished Tasks\".
  3657. You may set this option on a per-file basis by adding to the buffer a
  3658. line like
  3659. #+ARCHIVE: basement::** Finished Tasks
  3660. You may also define it locally for a subtree by setting an ARCHIVE property
  3661. in the entry. If such a property is found in an entry, or anywhere up
  3662. the hierarchy, it will be used."
  3663. :group 'org-archive
  3664. :type 'string)
  3665. (defcustom org-archive-tag "ARCHIVE"
  3666. "The tag that marks a subtree as archived.
  3667. An archived subtree does not open during visibility cycling, and does
  3668. not contribute to the agenda listings.
  3669. After changing this, font-lock must be restarted in the relevant buffers to
  3670. get the proper fontification."
  3671. :group 'org-archive
  3672. :group 'org-keywords
  3673. :type 'string)
  3674. (defcustom org-agenda-skip-archived-trees t
  3675. "Non-nil means the agenda will skip any items located in archived trees.
  3676. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3677. variable is no longer recommended, you should leave it at the value t.
  3678. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3679. :group 'org-archive
  3680. :group 'org-agenda-skip
  3681. :type 'boolean)
  3682. (defcustom org-columns-skip-archived-trees t
  3683. "Non-nil means ignore archived trees when creating column view."
  3684. :group 'org-archive
  3685. :group 'org-properties
  3686. :type 'boolean)
  3687. (defcustom org-cycle-open-archived-trees nil
  3688. "Non-nil means `org-cycle' will open archived trees.
  3689. An archived tree is a tree marked with the tag ARCHIVE.
  3690. When nil, archived trees will stay folded. You can still open them with
  3691. normal outline commands like `show-all', but not with the cycling commands."
  3692. :group 'org-archive
  3693. :group 'org-cycle
  3694. :type 'boolean)
  3695. (defcustom org-sparse-tree-open-archived-trees nil
  3696. "Non-nil means sparse tree construction shows matches in archived trees.
  3697. When nil, matches in these trees are highlighted, but the trees are kept in
  3698. collapsed state."
  3699. :group 'org-archive
  3700. :group 'org-sparse-trees
  3701. :type 'boolean)
  3702. (defun org-cycle-hide-archived-subtrees (state)
  3703. "Re-hide all archived subtrees after a visibility state change."
  3704. (when (and (not org-cycle-open-archived-trees)
  3705. (not (memq state '(overview folded))))
  3706. (save-excursion
  3707. (let* ((globalp (memq state '(contents all)))
  3708. (beg (if globalp (point-min) (point)))
  3709. (end (if globalp (point-max) (org-end-of-subtree t))))
  3710. (org-hide-archived-subtrees beg end)
  3711. (goto-char beg)
  3712. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3713. (message "%s" (substitute-command-keys
  3714. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3715. (defun org-force-cycle-archived ()
  3716. "Cycle subtree even if it is archived."
  3717. (interactive)
  3718. (setq this-command 'org-cycle)
  3719. (let ((org-cycle-open-archived-trees t))
  3720. (call-interactively 'org-cycle)))
  3721. (defun org-hide-archived-subtrees (beg end)
  3722. "Re-hide all archived subtrees after a visibility state change."
  3723. (save-excursion
  3724. (let* ((re (concat ":" org-archive-tag ":")))
  3725. (goto-char beg)
  3726. (while (re-search-forward re end t)
  3727. (when (org-at-heading-p)
  3728. (org-flag-subtree t)
  3729. (org-end-of-subtree t))))))
  3730. (defun org-flag-subtree (flag)
  3731. (save-excursion
  3732. (org-back-to-heading t)
  3733. (outline-end-of-heading)
  3734. (outline-flag-region (point)
  3735. (progn (org-end-of-subtree t) (point))
  3736. flag)))
  3737. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3738. (eval-and-compile
  3739. (org-autoload "org-archive"
  3740. '(org-add-archive-files org-archive-subtree
  3741. org-archive-to-archive-sibling org-toggle-archive-tag
  3742. org-archive-subtree-default
  3743. org-archive-subtree-default-with-confirmation)))
  3744. ;; Autoload Column View Code
  3745. (declare-function org-columns-number-to-string "org-colview")
  3746. (declare-function org-columns-get-format-and-top-level "org-colview")
  3747. (declare-function org-columns-compute "org-colview")
  3748. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  3749. '(org-columns-number-to-string org-columns-get-format-and-top-level
  3750. org-columns-compute org-agenda-columns org-columns-remove-overlays
  3751. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  3752. ;; Autoload ID code
  3753. (declare-function org-id-store-link "org-id")
  3754. (declare-function org-id-locations-load "org-id")
  3755. (declare-function org-id-locations-save "org-id")
  3756. (defvar org-id-track-globally)
  3757. (org-autoload "org-id"
  3758. '(org-id-get-create org-id-new org-id-copy org-id-get
  3759. org-id-get-with-outline-path-completion
  3760. org-id-get-with-outline-drilling org-id-store-link
  3761. org-id-goto org-id-find org-id-store-link))
  3762. ;; Autoload Plotting Code
  3763. (org-autoload "org-plot"
  3764. '(org-plot/gnuplot))
  3765. ;;; Variables for pre-computed regular expressions, all buffer local
  3766. (defvar org-drawer-regexp nil
  3767. "Matches first line of a hidden block.")
  3768. (make-variable-buffer-local 'org-drawer-regexp)
  3769. (defvar org-todo-regexp nil
  3770. "Matches any of the TODO state keywords.")
  3771. (make-variable-buffer-local 'org-todo-regexp)
  3772. (defvar org-not-done-regexp nil
  3773. "Matches any of the TODO state keywords except the last one.")
  3774. (make-variable-buffer-local 'org-not-done-regexp)
  3775. (defvar org-not-done-heading-regexp nil
  3776. "Matches a TODO headline that is not done.")
  3777. (make-variable-buffer-local 'org-not-done-regexp)
  3778. (defvar org-todo-line-regexp nil
  3779. "Matches a headline and puts TODO state into group 2 if present.")
  3780. (make-variable-buffer-local 'org-todo-line-regexp)
  3781. (defvar org-complex-heading-regexp nil
  3782. "Matches a headline and puts everything into groups:
  3783. group 1: the stars
  3784. group 2: The todo keyword, maybe
  3785. group 3: Priority cookie
  3786. group 4: True headline
  3787. group 5: Tags")
  3788. (make-variable-buffer-local 'org-complex-heading-regexp)
  3789. (defvar org-complex-heading-regexp-format nil
  3790. "Printf format to make regexp to match an exact headline.
  3791. This regexp will match the headline of any node which has the
  3792. exact headline text that is put into the format, but may have any
  3793. TODO state, priority and tags.")
  3794. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  3795. (defvar org-todo-line-tags-regexp nil
  3796. "Matches a headline and puts TODO state into group 2 if present.
  3797. Also put tags into group 4 if tags are present.")
  3798. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3799. (defvar org-ds-keyword-length 12
  3800. "Maximum length of the Deadline and SCHEDULED keywords.")
  3801. (make-variable-buffer-local 'org-ds-keyword-length)
  3802. (defvar org-deadline-regexp nil
  3803. "Matches the DEADLINE keyword.")
  3804. (make-variable-buffer-local 'org-deadline-regexp)
  3805. (defvar org-deadline-time-regexp nil
  3806. "Matches the DEADLINE keyword together with a time stamp.")
  3807. (make-variable-buffer-local 'org-deadline-time-regexp)
  3808. (defvar org-deadline-line-regexp nil
  3809. "Matches the DEADLINE keyword and the rest of the line.")
  3810. (make-variable-buffer-local 'org-deadline-line-regexp)
  3811. (defvar org-scheduled-regexp nil
  3812. "Matches the SCHEDULED keyword.")
  3813. (make-variable-buffer-local 'org-scheduled-regexp)
  3814. (defvar org-scheduled-time-regexp nil
  3815. "Matches the SCHEDULED keyword together with a time stamp.")
  3816. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3817. (defvar org-closed-time-regexp nil
  3818. "Matches the CLOSED keyword together with a time stamp.")
  3819. (make-variable-buffer-local 'org-closed-time-regexp)
  3820. (defvar org-keyword-time-regexp nil
  3821. "Matches any of the 4 keywords, together with the time stamp.")
  3822. (make-variable-buffer-local 'org-keyword-time-regexp)
  3823. (defvar org-keyword-time-not-clock-regexp nil
  3824. "Matches any of the 3 keywords, together with the time stamp.")
  3825. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3826. (defvar org-maybe-keyword-time-regexp nil
  3827. "Matches a timestamp, possibly preceded by a keyword.")
  3828. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3829. (defvar org-planning-or-clock-line-re nil
  3830. "Matches a line with planning or clock info.")
  3831. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  3832. (defvar org-all-time-keywords nil
  3833. "List of time keywords.")
  3834. (make-variable-buffer-local 'org-all-time-keywords)
  3835. (defconst org-plain-time-of-day-regexp
  3836. (concat
  3837. "\\(\\<[012]?[0-9]"
  3838. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3839. "\\(--?"
  3840. "\\(\\<[012]?[0-9]"
  3841. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3842. "\\)?")
  3843. "Regular expression to match a plain time or time range.
  3844. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3845. groups carry important information:
  3846. 0 the full match
  3847. 1 the first time, range or not
  3848. 8 the second time, if it is a range.")
  3849. (defconst org-plain-time-extension-regexp
  3850. (concat
  3851. "\\(\\<[012]?[0-9]"
  3852. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3853. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  3854. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  3855. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3856. groups carry important information:
  3857. 0 the full match
  3858. 7 hours of duration
  3859. 9 minutes of duration")
  3860. (defconst org-stamp-time-of-day-regexp
  3861. (concat
  3862. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  3863. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  3864. "\\(--?"
  3865. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  3866. "Regular expression to match a timestamp time or time range.
  3867. After a match, the following groups carry important information:
  3868. 0 the full match
  3869. 1 date plus weekday, for back referencing to make sure both times are on the same day
  3870. 2 the first time, range or not
  3871. 4 the second time, if it is a range.")
  3872. (defconst org-startup-options
  3873. '(("fold" org-startup-folded t)
  3874. ("overview" org-startup-folded t)
  3875. ("nofold" org-startup-folded nil)
  3876. ("showall" org-startup-folded nil)
  3877. ("showeverything" org-startup-folded showeverything)
  3878. ("content" org-startup-folded content)
  3879. ("indent" org-startup-indented t)
  3880. ("noindent" org-startup-indented nil)
  3881. ("hidestars" org-hide-leading-stars t)
  3882. ("showstars" org-hide-leading-stars nil)
  3883. ("odd" org-odd-levels-only t)
  3884. ("oddeven" org-odd-levels-only nil)
  3885. ("align" org-startup-align-all-tables t)
  3886. ("noalign" org-startup-align-all-tables nil)
  3887. ("inlineimages" org-startup-with-inline-images t)
  3888. ("noinlineimages" org-startup-with-inline-images nil)
  3889. ("customtime" org-display-custom-times t)
  3890. ("logdone" org-log-done time)
  3891. ("lognotedone" org-log-done note)
  3892. ("nologdone" org-log-done nil)
  3893. ("lognoteclock-out" org-log-note-clock-out t)
  3894. ("nolognoteclock-out" org-log-note-clock-out nil)
  3895. ("logrepeat" org-log-repeat state)
  3896. ("lognoterepeat" org-log-repeat note)
  3897. ("nologrepeat" org-log-repeat nil)
  3898. ("logreschedule" org-log-reschedule time)
  3899. ("lognotereschedule" org-log-reschedule note)
  3900. ("nologreschedule" org-log-reschedule nil)
  3901. ("logredeadline" org-log-redeadline time)
  3902. ("lognoteredeadline" org-log-redeadline note)
  3903. ("nologredeadline" org-log-redeadline nil)
  3904. ("logrefile" org-log-refile time)
  3905. ("lognoterefile" org-log-refile note)
  3906. ("nologrefile" org-log-refile nil)
  3907. ("fninline" org-footnote-define-inline t)
  3908. ("nofninline" org-footnote-define-inline nil)
  3909. ("fnlocal" org-footnote-section nil)
  3910. ("fnauto" org-footnote-auto-label t)
  3911. ("fnprompt" org-footnote-auto-label nil)
  3912. ("fnconfirm" org-footnote-auto-label confirm)
  3913. ("fnplain" org-footnote-auto-label plain)
  3914. ("fnadjust" org-footnote-auto-adjust t)
  3915. ("nofnadjust" org-footnote-auto-adjust nil)
  3916. ("constcgs" constants-unit-system cgs)
  3917. ("constSI" constants-unit-system SI)
  3918. ("noptag" org-tag-persistent-alist nil)
  3919. ("hideblocks" org-hide-block-startup t)
  3920. ("nohideblocks" org-hide-block-startup nil)
  3921. ("beamer" org-startup-with-beamer-mode t)
  3922. ("entitiespretty" org-pretty-entities t)
  3923. ("entitiesplain" org-pretty-entities nil))
  3924. "Variable associated with STARTUP options for org-mode.
  3925. Each element is a list of three items: The startup options as written
  3926. in the #+STARTUP line, the corresponding variable, and the value to
  3927. set this variable to if the option is found. An optional forth element PUSH
  3928. means to push this value onto the list in the variable.")
  3929. (defun org-update-property-plist (key val props)
  3930. "Update PROPS with KEY and VAL."
  3931. (let* ((appending (string= "+" (substring key (- (length key) 1))))
  3932. (key (if appending (substring key 0 (- (length key) 1)) key))
  3933. (remainder (org-remove-if (lambda (p) (string= (car p) key)) props))
  3934. (previous (cdr (assoc key props))))
  3935. (if appending
  3936. (cons (cons key (if previous (concat previous " " val) val)) remainder)
  3937. (cons (cons key val) remainder))))
  3938. (defconst org-block-regexp
  3939. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  3940. "Regular expression for hiding blocks.")
  3941. (defconst org-heading-keyword-regexp-format
  3942. "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  3943. "Printf format for a regexp matching an headline with some keyword.
  3944. This regexp will match the headline of any node which has the
  3945. exact keyword that is put into the format. The keyword isn't in
  3946. any group by default, but the stars and the body are.")
  3947. (defconst org-heading-keyword-maybe-regexp-format
  3948. "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
  3949. "Printf format for a regexp matching an headline, possibly with some keyword.
  3950. This regexp can match any headline with the specified keyword, or
  3951. without a keyword. The keyword isn't in any group by default,
  3952. but the stars and the body are.")
  3953. (defun org-set-regexps-and-options ()
  3954. "Precompute regular expressions for current buffer."
  3955. (when (derived-mode-p 'org-mode)
  3956. (org-set-local 'org-todo-kwd-alist nil)
  3957. (org-set-local 'org-todo-key-alist nil)
  3958. (org-set-local 'org-todo-key-trigger nil)
  3959. (org-set-local 'org-todo-keywords-1 nil)
  3960. (org-set-local 'org-done-keywords nil)
  3961. (org-set-local 'org-todo-heads nil)
  3962. (org-set-local 'org-todo-sets nil)
  3963. (org-set-local 'org-todo-log-states nil)
  3964. (org-set-local 'org-file-properties nil)
  3965. (org-set-local 'org-file-tags nil)
  3966. (let ((re (org-make-options-regexp
  3967. '("CATEGORY" "TODO" "COLUMNS"
  3968. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  3969. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE" "LATEX_CLASS"
  3970. "OPTIONS")
  3971. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  3972. (splitre "[ \t]+")
  3973. (scripts org-use-sub-superscripts)
  3974. kwds kws0 kwsa key log value cat arch tags const links hw dws
  3975. tail sep kws1 prio props ftags drawers beamer-p
  3976. ext-setup-or-nil setup-contents (start 0))
  3977. (save-excursion
  3978. (save-restriction
  3979. (widen)
  3980. (goto-char (point-min))
  3981. (while (or (and ext-setup-or-nil
  3982. (string-match re ext-setup-or-nil start)
  3983. (setq start (match-end 0)))
  3984. (and (setq ext-setup-or-nil nil start 0)
  3985. (re-search-forward re nil t)))
  3986. (setq key (upcase (match-string 1 ext-setup-or-nil))
  3987. value (org-match-string-no-properties 2 ext-setup-or-nil))
  3988. (if (stringp value) (setq value (org-trim value)))
  3989. (cond
  3990. ((equal key "CATEGORY")
  3991. (setq cat value))
  3992. ((member key '("SEQ_TODO" "TODO"))
  3993. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3994. ((equal key "TYP_TODO")
  3995. (push (cons 'type (org-split-string value splitre)) kwds))
  3996. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  3997. ;; general TODO-like setup
  3998. (push (cons (intern (downcase (match-string 1 key)))
  3999. (org-split-string value splitre)) kwds))
  4000. ((equal key "TAGS")
  4001. (setq tags (append tags (if tags '("\\n") nil)
  4002. (org-split-string value splitre))))
  4003. ((equal key "COLUMNS")
  4004. (org-set-local 'org-columns-default-format value))
  4005. ((equal key "LINK")
  4006. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  4007. (push (cons (match-string 1 value)
  4008. (org-trim (match-string 2 value)))
  4009. links)))
  4010. ((equal key "PRIORITIES")
  4011. (setq prio (org-split-string value " +")))
  4012. ((equal key "PROPERTY")
  4013. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4014. (setq props (org-update-property-plist (match-string 1 value)
  4015. (match-string 2 value)
  4016. props))))
  4017. ((equal key "FILETAGS")
  4018. (when (string-match "\\S-" value)
  4019. (setq ftags
  4020. (append
  4021. ftags
  4022. (apply 'append
  4023. (mapcar (lambda (x) (org-split-string x ":"))
  4024. (org-split-string value)))))))
  4025. ((equal key "DRAWERS")
  4026. (setq drawers (delete-dups (append org-drawers (org-split-string value splitre)))))
  4027. ((equal key "CONSTANTS")
  4028. (setq const (append const (org-split-string value splitre))))
  4029. ((equal key "STARTUP")
  4030. (let ((opts (org-split-string value splitre))
  4031. l var val)
  4032. (while (setq l (pop opts))
  4033. (when (setq l (assoc l org-startup-options))
  4034. (setq var (nth 1 l) val (nth 2 l))
  4035. (if (not (nth 3 l))
  4036. (set (make-local-variable var) val)
  4037. (if (not (listp (symbol-value var)))
  4038. (set (make-local-variable var) nil))
  4039. (set (make-local-variable var) (symbol-value var))
  4040. (add-to-list var val))))))
  4041. ((equal key "ARCHIVE")
  4042. (setq arch value)
  4043. (remove-text-properties 0 (length arch)
  4044. '(face t fontified t) arch))
  4045. ((equal key "LATEX_CLASS")
  4046. (setq beamer-p (equal value "beamer")))
  4047. ((equal key "OPTIONS")
  4048. (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value)
  4049. (setq scripts (read (match-string 2 value)))))
  4050. ((equal key "SETUPFILE")
  4051. (setq setup-contents (org-file-contents
  4052. (expand-file-name
  4053. (org-remove-double-quotes value))
  4054. 'noerror))
  4055. (if (not ext-setup-or-nil)
  4056. (setq ext-setup-or-nil setup-contents start 0)
  4057. (setq ext-setup-or-nil
  4058. (concat (substring ext-setup-or-nil 0 start)
  4059. "\n" setup-contents "\n"
  4060. (substring ext-setup-or-nil start)))))))
  4061. ;; search for property blocks
  4062. (goto-char (point-min))
  4063. (while (re-search-forward org-block-regexp nil t)
  4064. (when (equal "PROPERTY" (upcase (match-string 1)))
  4065. (setq value (replace-regexp-in-string
  4066. "[\n\r]" " " (match-string 4)))
  4067. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4068. (setq props (org-update-property-plist (match-string 1 value)
  4069. (match-string 2 value)
  4070. props)))))))
  4071. (org-set-local 'org-use-sub-superscripts scripts)
  4072. (when cat
  4073. (org-set-local 'org-category (intern cat))
  4074. (push (cons "CATEGORY" cat) props))
  4075. (when prio
  4076. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  4077. (setq prio (mapcar 'string-to-char prio))
  4078. (org-set-local 'org-highest-priority (nth 0 prio))
  4079. (org-set-local 'org-lowest-priority (nth 1 prio))
  4080. (org-set-local 'org-default-priority (nth 2 prio)))
  4081. (and props (org-set-local 'org-file-properties (nreverse props)))
  4082. (and ftags (org-set-local 'org-file-tags
  4083. (mapcar 'org-add-prop-inherited ftags)))
  4084. (and drawers (org-set-local 'org-drawers drawers))
  4085. (and arch (org-set-local 'org-archive-location arch))
  4086. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  4087. ;; Process the TODO keywords
  4088. (unless kwds
  4089. ;; Use the global values as if they had been given locally.
  4090. (setq kwds (default-value 'org-todo-keywords))
  4091. (if (stringp (car kwds))
  4092. (setq kwds (list (cons org-todo-interpretation
  4093. (default-value 'org-todo-keywords)))))
  4094. (setq kwds (reverse kwds)))
  4095. (setq kwds (nreverse kwds))
  4096. (let (inter kws kw)
  4097. (while (setq kws (pop kwds))
  4098. (let ((kws (or
  4099. (run-hook-with-args-until-success
  4100. 'org-todo-setup-filter-hook kws)
  4101. kws)))
  4102. (setq inter (pop kws) sep (member "|" kws)
  4103. kws0 (delete "|" (copy-sequence kws))
  4104. kwsa nil
  4105. kws1 (mapcar
  4106. (lambda (x)
  4107. ;; 1 2
  4108. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  4109. (progn
  4110. (setq kw (match-string 1 x)
  4111. key (and (match-end 2) (match-string 2 x))
  4112. log (org-extract-log-state-settings x))
  4113. (push (cons kw (and key (string-to-char key))) kwsa)
  4114. (and log (push log org-todo-log-states))
  4115. kw)
  4116. (error "Invalid TODO keyword %s" x)))
  4117. kws0)
  4118. kwsa (if kwsa (append '((:startgroup))
  4119. (nreverse kwsa)
  4120. '((:endgroup))))
  4121. hw (car kws1)
  4122. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  4123. tail (list inter hw (car dws) (org-last dws))))
  4124. (add-to-list 'org-todo-heads hw 'append)
  4125. (push kws1 org-todo-sets)
  4126. (setq org-done-keywords (append org-done-keywords dws nil))
  4127. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  4128. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  4129. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  4130. (setq org-todo-sets (nreverse org-todo-sets)
  4131. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4132. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  4133. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  4134. ;; Process the constants
  4135. (when const
  4136. (let (e cst)
  4137. (while (setq e (pop const))
  4138. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  4139. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  4140. (setq org-table-formula-constants-local cst)))
  4141. ;; Process the tags.
  4142. (when tags
  4143. (let (e tgs)
  4144. (while (setq e (pop tags))
  4145. (cond
  4146. ((equal e "{") (push '(:startgroup) tgs))
  4147. ((equal e "}") (push '(:endgroup) tgs))
  4148. ((equal e "\\n") (push '(:newline) tgs))
  4149. ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e)
  4150. (push (cons (match-string 1 e)
  4151. (string-to-char (match-string 2 e)))
  4152. tgs))
  4153. (t (push (list e) tgs))))
  4154. (org-set-local 'org-tag-alist nil)
  4155. (while (setq e (pop tgs))
  4156. (or (and (stringp (car e))
  4157. (assoc (car e) org-tag-alist))
  4158. (push e org-tag-alist)))))
  4159. ;; Compute the regular expressions and other local variables.
  4160. ;; Using `org-outline-regexp-bol' would complicate them much,
  4161. ;; because of the fixed white space at the end of that string.
  4162. (if (not org-done-keywords)
  4163. (setq org-done-keywords (and org-todo-keywords-1
  4164. (list (org-last org-todo-keywords-1)))))
  4165. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  4166. (length org-scheduled-string)
  4167. (length org-clock-string)
  4168. (length org-closed-string)))
  4169. org-drawer-regexp
  4170. (concat "^[ \t]*:\\("
  4171. (mapconcat 'regexp-quote org-drawers "\\|")
  4172. "\\):[ \t]*$")
  4173. org-not-done-keywords
  4174. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  4175. org-todo-regexp
  4176. (concat "\\("
  4177. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4178. "\\)")
  4179. org-not-done-regexp
  4180. (concat "\\("
  4181. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  4182. "\\)")
  4183. org-not-done-heading-regexp
  4184. (format org-heading-keyword-regexp-format org-not-done-regexp)
  4185. org-todo-line-regexp
  4186. (format org-heading-keyword-maybe-regexp-format org-todo-regexp)
  4187. org-complex-heading-regexp
  4188. (concat "^\\(\\*+\\)"
  4189. "\\(?: +" org-todo-regexp "\\)?"
  4190. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4191. "\\(?: +\\(.*?\\)\\)?"
  4192. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
  4193. "[ \t]*$")
  4194. org-complex-heading-regexp-format
  4195. (concat "^\\(\\*+\\)"
  4196. "\\(?: +" org-todo-regexp "\\)?"
  4197. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4198. "\\(?: +"
  4199. ;; Stats cookies can be stuck to body.
  4200. "\\(?:\\[[0-9%%/]+\\] *\\)?"
  4201. "\\(%s\\)"
  4202. "\\(?: *\\[[0-9%%/]+\\]\\)?"
  4203. "\\)"
  4204. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
  4205. "[ \t]*$")
  4206. org-todo-line-tags-regexp
  4207. (concat "^\\(\\*+\\)"
  4208. "\\(?: +" org-todo-regexp "\\)?"
  4209. "\\(?: +\\(.*?\\)\\)?"
  4210. (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
  4211. "[ \t]*$")
  4212. org-deadline-regexp (concat "\\<" org-deadline-string)
  4213. org-deadline-time-regexp
  4214. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  4215. org-deadline-line-regexp
  4216. (concat "\\<\\(" org-deadline-string "\\).*")
  4217. org-scheduled-regexp
  4218. (concat "\\<" org-scheduled-string)
  4219. org-scheduled-time-regexp
  4220. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  4221. org-closed-time-regexp
  4222. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  4223. org-keyword-time-regexp
  4224. (concat "\\<\\(" org-scheduled-string
  4225. "\\|" org-deadline-string
  4226. "\\|" org-closed-string
  4227. "\\|" org-clock-string "\\)"
  4228. " *[[<]\\([^]>]+\\)[]>]")
  4229. org-keyword-time-not-clock-regexp
  4230. (concat "\\<\\(" org-scheduled-string
  4231. "\\|" org-deadline-string
  4232. "\\|" org-closed-string
  4233. "\\)"
  4234. " *[[<]\\([^]>]+\\)[]>]")
  4235. org-maybe-keyword-time-regexp
  4236. (concat "\\(\\<\\(" org-scheduled-string
  4237. "\\|" org-deadline-string
  4238. "\\|" org-closed-string
  4239. "\\|" org-clock-string "\\)\\)?"
  4240. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4241. org-planning-or-clock-line-re
  4242. (concat "^[ \t]*\\("
  4243. org-scheduled-string "\\|"
  4244. org-deadline-string "\\|"
  4245. org-closed-string "\\|"
  4246. org-clock-string "\\)")
  4247. org-all-time-keywords
  4248. (mapcar (lambda (w) (substring w 0 -1))
  4249. (list org-scheduled-string org-deadline-string
  4250. org-clock-string org-closed-string))
  4251. )
  4252. (org-compute-latex-and-specials-regexp)
  4253. (org-set-font-lock-defaults))))
  4254. (defun org-file-contents (file &optional noerror)
  4255. "Return the contents of FILE, as a string."
  4256. (if (or (not file)
  4257. (not (file-readable-p file)))
  4258. (if noerror
  4259. (progn
  4260. (message "Cannot read file \"%s\"" file)
  4261. (ding) (sit-for 2)
  4262. "")
  4263. (error "Cannot read file \"%s\"" file))
  4264. (with-temp-buffer
  4265. (insert-file-contents file)
  4266. (buffer-string))))
  4267. (defun org-extract-log-state-settings (x)
  4268. "Extract the log state setting from a TODO keyword string.
  4269. This will extract info from a string like \"WAIT(w@/!)\"."
  4270. (let (kw key log1 log2)
  4271. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4272. (setq kw (match-string 1 x)
  4273. key (and (match-end 2) (match-string 2 x))
  4274. log1 (and (match-end 3) (match-string 3 x))
  4275. log2 (and (match-end 4) (match-string 4 x)))
  4276. (and (or log1 log2)
  4277. (list kw
  4278. (and log1 (if (equal log1 "!") 'time 'note))
  4279. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4280. (defun org-remove-keyword-keys (list)
  4281. "Remove a pair of parenthesis at the end of each string in LIST."
  4282. (mapcar (lambda (x)
  4283. (if (string-match "(.*)$" x)
  4284. (substring x 0 (match-beginning 0))
  4285. x))
  4286. list))
  4287. (defun org-assign-fast-keys (alist)
  4288. "Assign fast keys to a keyword-key alist.
  4289. Respect keys that are already there."
  4290. (let (new e (alt ?0))
  4291. (while (setq e (pop alist))
  4292. (if (or (memq (car e) '(:newline :endgroup :startgroup))
  4293. (cdr e)) ;; Key already assigned.
  4294. (push e new)
  4295. (let ((clist (string-to-list (downcase (car e))))
  4296. (used (append new alist)))
  4297. (when (= (car clist) ?@)
  4298. (pop clist))
  4299. (while (and clist (rassoc (car clist) used))
  4300. (pop clist))
  4301. (unless clist
  4302. (while (rassoc alt used)
  4303. (incf alt)))
  4304. (push (cons (car e) (or (car clist) alt)) new))))
  4305. (nreverse new)))
  4306. ;;; Some variables used in various places
  4307. (defvar org-window-configuration nil
  4308. "Used in various places to store a window configuration.")
  4309. (defvar org-selected-window nil
  4310. "Used in various places to store a window configuration.")
  4311. (defvar org-finish-function nil
  4312. "Function to be called when `C-c C-c' is used.
  4313. This is for getting out of special buffers like remember.")
  4314. ;; FIXME: Occasionally check by commenting these, to make sure
  4315. ;; no other functions uses these, forgetting to let-bind them.
  4316. (defvar entry)
  4317. (defvar org-last-state)
  4318. (defvar date)
  4319. ;; Defined somewhere in this file, but used before definition.
  4320. (defvar org-entities) ;; defined in org-entities.el
  4321. (defvar org-struct-menu)
  4322. (defvar org-org-menu)
  4323. (defvar org-tbl-menu)
  4324. ;;;; Define the Org-mode
  4325. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4326. (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"))
  4327. ;; We use a before-change function to check if a table might need
  4328. ;; an update.
  4329. (defvar org-table-may-need-update t
  4330. "Indicates that a table might need an update.
  4331. This variable is set by `org-before-change-function'.
  4332. `org-table-align' sets it back to nil.")
  4333. (defun org-before-change-function (beg end)
  4334. "Every change indicates that a table might need an update."
  4335. (setq org-table-may-need-update t))
  4336. (defvar org-mode-map)
  4337. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4338. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4339. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4340. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4341. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4342. (defvar org-table-buffer-is-an nil)
  4343. ;; `org-outline-regexp' ought to be a defconst but is let-binding in
  4344. ;; some places -- e.g. see the macro org-with-limited-levels.
  4345. ;;
  4346. ;; In Org buffers, the value of `outline-regexp' is that of
  4347. ;; `org-outline-regexp'. The only function still directly relying on
  4348. ;; `outline-regexp' is `org-overview' so that `org-cycle' can do its
  4349. ;; job when `orgstruct-mode' is active.
  4350. (defvar org-outline-regexp "\\*+ "
  4351. "Regexp to match Org headlines.")
  4352. (defconst org-outline-regexp-bol "^\\*+ "
  4353. "Regexp to match Org headlines.
  4354. This is similar to `org-outline-regexp' but additionally makes
  4355. sure that we are at the beginning of the line.")
  4356. (defconst org-heading-regexp "^\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  4357. "Matches an headline, putting stars and text into groups.
  4358. Stars are put in group 1 and the trimmed body in group 2.")
  4359. (defvar bidi-paragraph-direction)
  4360. (defvar buffer-face-mode-face)
  4361. ;;;###autoload
  4362. (define-derived-mode org-mode outline-mode "Org"
  4363. "Outline-based notes management and organizer, alias
  4364. \"Carsten's outline-mode for keeping track of everything.\"
  4365. Org-mode develops organizational tasks around a NOTES file which
  4366. contains information about projects as plain text. Org-mode is
  4367. implemented on top of outline-mode, which is ideal to keep the content
  4368. of large files well structured. It supports ToDo items, deadlines and
  4369. time stamps, which magically appear in the diary listing of the Emacs
  4370. calendar. Tables are easily created with a built-in table editor.
  4371. Plain text URL-like links connect to websites, emails (VM), Usenet
  4372. messages (Gnus), BBDB entries, and any files related to the project.
  4373. For printing and sharing of notes, an Org-mode file (or a part of it)
  4374. can be exported as a structured ASCII or HTML file.
  4375. The following commands are available:
  4376. \\{org-mode-map}"
  4377. ;; Get rid of Outline menus, they are not needed
  4378. ;; Need to do this here because define-derived-mode sets up
  4379. ;; the keymap so late. Still, it is a waste to call this each time
  4380. ;; we switch another buffer into org-mode.
  4381. (if (featurep 'xemacs)
  4382. (when (boundp 'outline-mode-menu-heading)
  4383. ;; Assume this is Greg's port, it uses easymenu
  4384. (easy-menu-remove outline-mode-menu-heading)
  4385. (easy-menu-remove outline-mode-menu-show)
  4386. (easy-menu-remove outline-mode-menu-hide))
  4387. (define-key org-mode-map [menu-bar headings] 'undefined)
  4388. (define-key org-mode-map [menu-bar hide] 'undefined)
  4389. (define-key org-mode-map [menu-bar show] 'undefined))
  4390. (org-load-modules-maybe)
  4391. (easy-menu-add org-org-menu)
  4392. (easy-menu-add org-tbl-menu)
  4393. (org-install-agenda-files-menu)
  4394. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4395. (add-to-invisibility-spec '(org-cwidth))
  4396. (add-to-invisibility-spec '(org-hide-block . t))
  4397. (when (featurep 'xemacs)
  4398. (org-set-local 'line-move-ignore-invisible t))
  4399. (org-set-local 'outline-regexp org-outline-regexp)
  4400. (org-set-local 'outline-level 'org-outline-level)
  4401. (setq bidi-paragraph-direction 'left-to-right)
  4402. (when (and org-ellipsis
  4403. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4404. (fboundp 'make-glyph-code))
  4405. (unless org-display-table
  4406. (setq org-display-table (make-display-table)))
  4407. (set-display-table-slot
  4408. org-display-table 4
  4409. (vconcat (mapcar
  4410. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4411. org-ellipsis)))
  4412. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4413. (setq buffer-display-table org-display-table))
  4414. (org-set-regexps-and-options)
  4415. (when (and org-tag-faces (not org-tags-special-faces-re))
  4416. ;; tag faces set outside customize.... force initialization.
  4417. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4418. ;; Calc embedded
  4419. (org-set-local 'calc-embedded-open-mode "# ")
  4420. (modify-syntax-entry ?@ "w")
  4421. (if org-startup-truncated (setq truncate-lines t))
  4422. (org-set-local 'font-lock-unfontify-region-function
  4423. 'org-unfontify-region)
  4424. ;; Activate before-change-function
  4425. (org-set-local 'org-table-may-need-update t)
  4426. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4427. 'local)
  4428. ;; Check for running clock before killing a buffer
  4429. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4430. ;; Paragraphs and auto-filling
  4431. (org-set-autofill-regexps)
  4432. (setq indent-line-function 'org-indent-line-function)
  4433. (org-update-radio-target-regexp)
  4434. ;; Beginning/end of defun
  4435. (org-set-local 'beginning-of-defun-function 'org-beginning-of-defun)
  4436. (org-set-local 'end-of-defun-function 'org-end-of-defun)
  4437. ;; Next error for sparse trees
  4438. (org-set-local 'next-error-function 'org-occur-next-match)
  4439. ;; Make sure dependence stuff works reliably, even for users who set it
  4440. ;; too late :-(
  4441. (if org-enforce-todo-dependencies
  4442. (add-hook 'org-blocker-hook
  4443. 'org-block-todo-from-children-or-siblings-or-parent)
  4444. (remove-hook 'org-blocker-hook
  4445. 'org-block-todo-from-children-or-siblings-or-parent))
  4446. (if org-enforce-todo-checkbox-dependencies
  4447. (add-hook 'org-blocker-hook
  4448. 'org-block-todo-from-checkboxes)
  4449. (remove-hook 'org-blocker-hook
  4450. 'org-block-todo-from-checkboxes))
  4451. ;; Comment characters
  4452. (org-set-local 'comment-start "#")
  4453. (org-set-local 'comment-padding " ")
  4454. ;; Align options lines
  4455. (org-set-local
  4456. 'align-mode-rules-list
  4457. '((org-in-buffer-settings
  4458. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4459. (modes . '(org-mode)))))
  4460. ;; Imenu
  4461. (org-set-local 'imenu-create-index-function
  4462. 'org-imenu-get-tree)
  4463. ;; Make isearch reveal context
  4464. (if (or (featurep 'xemacs)
  4465. (not (boundp 'outline-isearch-open-invisible-function)))
  4466. ;; Emacs 21 and XEmacs make use of the hook
  4467. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4468. ;; Emacs 22 deals with this through a special variable
  4469. (org-set-local 'outline-isearch-open-invisible-function
  4470. (lambda (&rest ignore) (org-show-context 'isearch))))
  4471. ;; Turn on org-beamer-mode?
  4472. (and org-startup-with-beamer-mode (org-beamer-mode 1))
  4473. ;; Setup the pcomplete hooks
  4474. (set (make-local-variable 'pcomplete-command-completion-function)
  4475. 'org-pcomplete-initial)
  4476. (set (make-local-variable 'pcomplete-command-name-function)
  4477. 'org-command-at-point)
  4478. (set (make-local-variable 'pcomplete-default-completion-function)
  4479. 'ignore)
  4480. (set (make-local-variable 'pcomplete-parse-arguments-function)
  4481. 'org-parse-arguments)
  4482. (set (make-local-variable 'pcomplete-termination-string) "")
  4483. (when (>= emacs-major-version 23)
  4484. (set (make-local-variable 'buffer-face-mode-face) 'org-default))
  4485. ;; If empty file that did not turn on org-mode automatically, make it to.
  4486. (if (and org-insert-mode-line-in-empty-file
  4487. (org-called-interactively-p 'any)
  4488. (= (point-min) (point-max)))
  4489. (insert "# -*- mode: org -*-\n\n"))
  4490. (unless org-inhibit-startup
  4491. (when org-startup-align-all-tables
  4492. (let ((bmp (buffer-modified-p)))
  4493. (org-table-map-tables 'org-table-align 'quietly)
  4494. (set-buffer-modified-p bmp)))
  4495. (when org-startup-with-inline-images
  4496. (org-display-inline-images))
  4497. (when org-startup-indented
  4498. (require 'org-indent)
  4499. (org-indent-mode 1))
  4500. (unless org-inhibit-startup-visibility-stuff
  4501. (org-set-startup-visibility))))
  4502. (when (fboundp 'abbrev-table-put)
  4503. (abbrev-table-put org-mode-abbrev-table
  4504. :parents (list text-mode-abbrev-table)))
  4505. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4506. (defun org-current-time ()
  4507. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  4508. (if (> (car org-time-stamp-rounding-minutes) 1)
  4509. (let ((r (car org-time-stamp-rounding-minutes))
  4510. (time (decode-time)))
  4511. (apply 'encode-time
  4512. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4513. (nthcdr 2 time))))
  4514. (current-time)))
  4515. (defun org-today ()
  4516. "Return today date, considering `org-extend-today-until'."
  4517. (time-to-days
  4518. (time-subtract (current-time)
  4519. (list 0 (* 3600 org-extend-today-until) 0))))
  4520. ;;;; Font-Lock stuff, including the activators
  4521. (defvar org-mouse-map (make-sparse-keymap))
  4522. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4523. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4524. (when org-mouse-1-follows-link
  4525. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4526. (when org-tab-follows-link
  4527. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4528. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4529. (require 'font-lock)
  4530. (defconst org-non-link-chars "]\t\n\r<>")
  4531. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  4532. "shell" "elisp" "doi" "message"))
  4533. (defvar org-link-types-re nil
  4534. "Matches a link that has a url-like prefix like \"http:\"")
  4535. (defvar org-link-re-with-space nil
  4536. "Matches a link with spaces, optional angular brackets around it.")
  4537. (defvar org-link-re-with-space2 nil
  4538. "Matches a link with spaces, optional angular brackets around it.")
  4539. (defvar org-link-re-with-space3 nil
  4540. "Matches a link with spaces, only for internal part in bracket links.")
  4541. (defvar org-angle-link-re nil
  4542. "Matches link with angular brackets, spaces are allowed.")
  4543. (defvar org-plain-link-re nil
  4544. "Matches plain link, without spaces.")
  4545. (defvar org-bracket-link-regexp nil
  4546. "Matches a link in double brackets.")
  4547. (defvar org-bracket-link-analytic-regexp nil
  4548. "Regular expression used to analyze links.
  4549. Here is what the match groups contain after a match:
  4550. 1: http:
  4551. 2: http
  4552. 3: path
  4553. 4: [desc]
  4554. 5: desc")
  4555. (defvar org-bracket-link-analytic-regexp++ nil
  4556. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4557. (defvar org-any-link-re nil
  4558. "Regular expression matching any link.")
  4559. (defcustom org-match-sexp-depth 3
  4560. "Number of stacked braces for sub/superscript matching.
  4561. This has to be set before loading org.el to be effective."
  4562. :group 'org-export-translation ; ??????????????????????????/
  4563. :type 'integer)
  4564. (defun org-create-multibrace-regexp (left right n)
  4565. "Create a regular expression which will match a balanced sexp.
  4566. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4567. as single character strings.
  4568. The regexp returned will match the entire expression including the
  4569. delimiters. It will also define a single group which contains the
  4570. match except for the outermost delimiters. The maximum depth of
  4571. stacked delimiters is N. Escaping delimiters is not possible."
  4572. (let* ((nothing (concat "[^" left right "]*?"))
  4573. (or "\\|")
  4574. (re nothing)
  4575. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4576. (while (> n 1)
  4577. (setq n (1- n)
  4578. re (concat re or next)
  4579. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4580. (concat left "\\(" re "\\)" right)))
  4581. (defvar org-match-substring-regexp
  4582. (concat
  4583. "\\([^\\]\\|^\\)\\([_^]\\)\\("
  4584. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4585. "\\|"
  4586. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4587. "\\|"
  4588. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  4589. "The regular expression matching a sub- or superscript.")
  4590. (defvar org-match-substring-with-braces-regexp
  4591. (concat
  4592. "\\([^\\]\\|^\\)\\([_^]\\)\\("
  4593. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4594. "\\)")
  4595. "The regular expression matching a sub- or superscript, forcing braces.")
  4596. (defun org-make-link-regexps ()
  4597. "Update the link regular expressions.
  4598. This should be called after the variable `org-link-types' has changed."
  4599. (setq org-link-types-re
  4600. (concat
  4601. "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
  4602. org-link-re-with-space
  4603. (concat
  4604. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4605. "\\([^" org-non-link-chars " ]"
  4606. "[^" org-non-link-chars "]*"
  4607. "[^" org-non-link-chars " ]\\)>?")
  4608. org-link-re-with-space2
  4609. (concat
  4610. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4611. "\\([^" org-non-link-chars " ]"
  4612. "[^\t\n\r]*"
  4613. "[^" org-non-link-chars " ]\\)>?")
  4614. org-link-re-with-space3
  4615. (concat
  4616. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4617. "\\([^" org-non-link-chars " ]"
  4618. "[^\t\n\r]*\\)")
  4619. org-angle-link-re
  4620. (concat
  4621. "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4622. "\\([^" org-non-link-chars " ]"
  4623. "[^" org-non-link-chars "]*"
  4624. "\\)>")
  4625. org-plain-link-re
  4626. (concat
  4627. "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4628. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  4629. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4630. org-bracket-link-regexp
  4631. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4632. org-bracket-link-analytic-regexp
  4633. (concat
  4634. "\\[\\["
  4635. "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
  4636. "\\([^]]+\\)"
  4637. "\\]"
  4638. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4639. "\\]")
  4640. org-bracket-link-analytic-regexp++
  4641. (concat
  4642. "\\[\\["
  4643. "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  4644. "\\([^]]+\\)"
  4645. "\\]"
  4646. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4647. "\\]")
  4648. org-any-link-re
  4649. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4650. org-angle-link-re "\\)\\|\\("
  4651. org-plain-link-re "\\)")))
  4652. (org-make-link-regexps)
  4653. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
  4654. "Regular expression for fast time stamp matching.")
  4655. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
  4656. "Regular expression for fast time stamp matching.")
  4657. (defconst org-ts-regexp0
  4658. "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4659. "Regular expression matching time strings for analysis.
  4660. This one does not require the space after the date, so it can be used
  4661. on a string that terminates immediately after the date.")
  4662. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4663. "Regular expression matching time strings for analysis.")
  4664. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4665. "Regular expression matching time stamps, with groups.")
  4666. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4667. "Regular expression matching time stamps (also [..]), with groups.")
  4668. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4669. "Regular expression matching a time stamp range.")
  4670. (defconst org-tr-regexp-both
  4671. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4672. "Regular expression matching a time stamp range.")
  4673. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4674. org-ts-regexp "\\)?")
  4675. "Regular expression matching a time stamp or time stamp range.")
  4676. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  4677. org-ts-regexp-both "\\)?")
  4678. "Regular expression matching a time stamp or time stamp range.
  4679. The time stamps may be either active or inactive.")
  4680. (defvar org-emph-face nil)
  4681. (defun org-do-emphasis-faces (limit)
  4682. "Run through the buffer and add overlays to emphasized strings."
  4683. (let (rtn a)
  4684. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4685. (if (not (= (char-after (match-beginning 3))
  4686. (char-after (match-beginning 4))))
  4687. (progn
  4688. (setq rtn t)
  4689. (setq a (assoc (match-string 3) org-emphasis-alist))
  4690. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4691. 'face
  4692. (nth 1 a))
  4693. (and (nth 4 a)
  4694. (org-remove-flyspell-overlays-in
  4695. (match-beginning 0) (match-end 0)))
  4696. (add-text-properties (match-beginning 2) (match-end 2)
  4697. '(font-lock-multiline t org-emphasis t))
  4698. (when org-hide-emphasis-markers
  4699. (add-text-properties (match-end 4) (match-beginning 5)
  4700. '(invisible org-link))
  4701. (add-text-properties (match-beginning 3) (match-end 3)
  4702. '(invisible org-link)))))
  4703. (backward-char 1))
  4704. rtn))
  4705. (defun org-emphasize (&optional char)
  4706. "Insert or change an emphasis, i.e. a font like bold or italic.
  4707. If there is an active region, change that region to a new emphasis.
  4708. If there is no region, just insert the marker characters and position
  4709. the cursor between them.
  4710. CHAR should be either the marker character, or the first character of the
  4711. HTML tag associated with that emphasis. If CHAR is a space, the means
  4712. to remove the emphasis of the selected region.
  4713. If char is not given (for example in an interactive call) it
  4714. will be prompted for."
  4715. (interactive)
  4716. (let ((eal org-emphasis-alist) e det
  4717. (erc org-emphasis-regexp-components)
  4718. (prompt "")
  4719. (string "") beg end move tag c s)
  4720. (if (org-region-active-p)
  4721. (setq beg (region-beginning) end (region-end)
  4722. string (buffer-substring beg end))
  4723. (setq move t))
  4724. (while (setq e (pop eal))
  4725. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4726. c (aref tag 0))
  4727. (push (cons c (string-to-char (car e))) det)
  4728. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4729. (substring tag 1)))))
  4730. (setq det (nreverse det))
  4731. (unless char
  4732. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4733. (setq char (read-char-exclusive)))
  4734. (setq char (or (cdr (assoc char det)) char))
  4735. (if (equal char ?\ )
  4736. (setq s "" move nil)
  4737. (unless (assoc (char-to-string char) org-emphasis-alist)
  4738. (error "No such emphasis marker: \"%c\"" char))
  4739. (setq s (char-to-string char)))
  4740. (while (and (> (length string) 1)
  4741. (equal (substring string 0 1) (substring string -1))
  4742. (assoc (substring string 0 1) org-emphasis-alist))
  4743. (setq string (substring string 1 -1)))
  4744. (setq string (concat s string s))
  4745. (if beg (delete-region beg end))
  4746. (unless (or (bolp)
  4747. (string-match (concat "[" (nth 0 erc) "\n]")
  4748. (char-to-string (char-before (point)))))
  4749. (insert " "))
  4750. (unless (or (eobp)
  4751. (string-match (concat "[" (nth 1 erc) "\n]")
  4752. (char-to-string (char-after (point)))))
  4753. (insert " ") (backward-char 1))
  4754. (insert string)
  4755. (and move (backward-char 1))))
  4756. (defconst org-nonsticky-props
  4757. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  4758. (defsubst org-rear-nonsticky-at (pos)
  4759. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  4760. (defun org-activate-plain-links (limit)
  4761. "Run through the buffer and add overlays to links."
  4762. (catch 'exit
  4763. (let (f)
  4764. (if (re-search-forward org-plain-link-re limit t)
  4765. (progn
  4766. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4767. (setq f (get-text-property (match-beginning 0) 'face))
  4768. (if (or (eq f 'org-tag)
  4769. (and (listp f) (memq 'org-tag f)))
  4770. nil
  4771. (add-text-properties (match-beginning 0) (match-end 0)
  4772. (list 'mouse-face 'highlight
  4773. 'face 'org-link
  4774. 'keymap org-mouse-map))
  4775. (org-rear-nonsticky-at (match-end 0)))
  4776. t)))))
  4777. (defun org-activate-code (limit)
  4778. (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  4779. (progn
  4780. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4781. (remove-text-properties (match-beginning 0) (match-end 0)
  4782. '(display t invisible t intangible t))
  4783. t)))
  4784. (defcustom org-src-fontify-natively nil
  4785. "When non-nil, fontify code in code blocks."
  4786. :type 'boolean
  4787. :version "24.1"
  4788. :group 'org-appearance
  4789. :group 'org-babel)
  4790. (defun org-fontify-meta-lines-and-blocks (limit)
  4791. (condition-case nil
  4792. (org-fontify-meta-lines-and-blocks-1 limit)
  4793. (error (message "org-mode fontification error"))))
  4794. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  4795. "Fontify #+ lines and blocks, in the correct ways."
  4796. (let ((case-fold-search t))
  4797. (if (re-search-forward
  4798. "^\\([ \t]*#\\+\\(\\([a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  4799. limit t)
  4800. (let ((beg (match-beginning 0))
  4801. (block-start (match-end 0))
  4802. (block-end nil)
  4803. (lang (match-string 7))
  4804. (beg1 (line-beginning-position 2))
  4805. (dc1 (downcase (match-string 2)))
  4806. (dc3 (downcase (match-string 3)))
  4807. end end1 quoting block-type ovl)
  4808. (cond
  4809. ((member dc1 '("html:" "ascii:" "latex:" "docbook:"))
  4810. ;; a single line of backend-specific content
  4811. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4812. (remove-text-properties (match-beginning 0) (match-end 0)
  4813. '(display t invisible t intangible t))
  4814. (add-text-properties (match-beginning 1) (match-end 3)
  4815. '(font-lock-fontified t face org-meta-line))
  4816. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  4817. '(font-lock-fontified t face org-block))
  4818. ; for backend-specific code
  4819. t)
  4820. ((and (match-end 4) (equal dc3 "begin"))
  4821. ;; Truly a block
  4822. (setq block-type (downcase (match-string 5))
  4823. quoting (member block-type org-protecting-blocks))
  4824. (when (re-search-forward
  4825. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  4826. nil t) ;; on purpose, we look further than LIMIT
  4827. (setq end (min (point-max) (match-end 0))
  4828. end1 (min (point-max) (1- (match-beginning 0))))
  4829. (setq block-end (match-beginning 0))
  4830. (when quoting
  4831. (remove-text-properties beg end
  4832. '(display t invisible t intangible t)))
  4833. (add-text-properties
  4834. beg end
  4835. '(font-lock-fontified t font-lock-multiline t))
  4836. (add-text-properties beg beg1 '(face org-meta-line))
  4837. (add-text-properties end1 (min (point-max) (1+ end))
  4838. '(face org-meta-line)) ; for end_src
  4839. (cond
  4840. ((and lang (not (string= lang "")) org-src-fontify-natively)
  4841. (org-src-font-lock-fontify-block lang block-start block-end)
  4842. ;; remove old background overlays
  4843. (mapc (lambda (ov)
  4844. (if (eq (overlay-get ov 'face) 'org-block-background)
  4845. (delete-overlay ov)))
  4846. (overlays-at (/ (+ beg1 block-end) 2)))
  4847. ;; add a background overlay
  4848. (setq ovl (make-overlay beg1 block-end))
  4849. (overlay-put ovl 'face 'org-block-background)
  4850. (overlay-put ovl 'evaporate t)) ;; make it go away when empty
  4851. (quoting
  4852. (add-text-properties beg1 (min (point-max) (1+ end1))
  4853. '(face org-block))) ; end of source block
  4854. ((not org-fontify-quote-and-verse-blocks))
  4855. ((string= block-type "quote")
  4856. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-quote)))
  4857. ((string= block-type "verse")
  4858. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-verse))))
  4859. (add-text-properties beg beg1 '(face org-block-begin-line))
  4860. (add-text-properties (min (point-max) (1+ end)) (min (point-max) (1+ end1))
  4861. '(face org-block-end-line))
  4862. t))
  4863. ((member dc1 '("title:" "author:" "email:" "date:"))
  4864. (add-text-properties
  4865. beg (match-end 3)
  4866. (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
  4867. '(font-lock-fontified t invisible t)
  4868. '(font-lock-fontified t face org-document-info-keyword)))
  4869. (add-text-properties
  4870. (match-beginning 6) (match-end 6)
  4871. (if (string-equal dc1 "title:")
  4872. '(font-lock-fontified t face org-document-title)
  4873. '(font-lock-fontified t face org-document-info))))
  4874. ((not (member (char-after beg) '(?\ ?\t)))
  4875. ;; just any other in-buffer setting, but not indented
  4876. (add-text-properties
  4877. beg (match-end 0)
  4878. '(font-lock-fontified t face org-meta-line))
  4879. t)
  4880. ((or (member dc1 '("begin:" "end:" "caption:" "label:"
  4881. "orgtbl:" "tblfm:" "tblname:" "results:"
  4882. "call:" "header:" "headers:" "name:"))
  4883. (and (match-end 4) (equal dc3 "attr")))
  4884. (add-text-properties
  4885. beg (match-end 0)
  4886. '(font-lock-fontified t face org-meta-line))
  4887. t)
  4888. ((member dc3 '(" " ""))
  4889. (add-text-properties
  4890. beg (match-end 0)
  4891. '(font-lock-fontified t face font-lock-comment-face)))
  4892. (t nil))))))
  4893. (defun org-strip-protective-commas (beg end)
  4894. "Strip protective commas between BEG and END in the current buffer."
  4895. (interactive "r")
  4896. (save-excursion
  4897. (save-match-data
  4898. (goto-char beg)
  4899. (let ((front-line (save-excursion
  4900. (re-search-forward
  4901. "[^[:space:]]" end t)
  4902. (goto-char (match-beginning 0))
  4903. (current-column))))
  4904. (while (re-search-forward "^[ \t]*\\(,\\)\\([*]\\|#\\+\\)" end t)
  4905. (goto-char (match-beginning 1))
  4906. (when (= (current-column) front-line)
  4907. (replace-match "" nil nil nil 1)))))))
  4908. (defun org-activate-angle-links (limit)
  4909. "Run through the buffer and add overlays to links."
  4910. (if (re-search-forward org-angle-link-re limit t)
  4911. (progn
  4912. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4913. (add-text-properties (match-beginning 0) (match-end 0)
  4914. (list 'mouse-face 'highlight
  4915. 'keymap org-mouse-map))
  4916. (org-rear-nonsticky-at (match-end 0))
  4917. t)))
  4918. (defun org-activate-footnote-links (limit)
  4919. "Run through the buffer and add overlays to footnotes."
  4920. (let ((fn (org-footnote-next-reference-or-definition limit)))
  4921. (when fn
  4922. (let ((beg (nth 1 fn)) (end (nth 2 fn)))
  4923. (org-remove-flyspell-overlays-in beg end)
  4924. (add-text-properties beg end
  4925. (list 'mouse-face 'highlight
  4926. 'keymap org-mouse-map
  4927. 'help-echo
  4928. (if (= (point-at-bol) beg)
  4929. "Footnote definition"
  4930. "Footnote reference")
  4931. 'font-lock-fontified t
  4932. 'font-lock-multiline t
  4933. 'face 'org-footnote))))))
  4934. (defun org-activate-bracket-links (limit)
  4935. "Run through the buffer and add overlays to bracketed links."
  4936. (if (re-search-forward org-bracket-link-regexp limit t)
  4937. (let* ((help (concat "LINK: "
  4938. (org-match-string-no-properties 1)))
  4939. ;; FIXME: above we should remove the escapes.
  4940. ;; but that requires another match, protecting match data,
  4941. ;; a lot of overhead for font-lock.
  4942. (ip (org-maybe-intangible
  4943. (list 'invisible 'org-link
  4944. 'keymap org-mouse-map 'mouse-face 'highlight
  4945. 'font-lock-multiline t 'help-echo help)))
  4946. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  4947. 'font-lock-multiline t 'help-echo help)))
  4948. ;; We need to remove the invisible property here. Table narrowing
  4949. ;; may have made some of this invisible.
  4950. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4951. (remove-text-properties (match-beginning 0) (match-end 0)
  4952. '(invisible nil))
  4953. (if (match-end 3)
  4954. (progn
  4955. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  4956. (org-rear-nonsticky-at (match-beginning 3))
  4957. (add-text-properties (match-beginning 3) (match-end 3) vp)
  4958. (org-rear-nonsticky-at (match-end 3))
  4959. (add-text-properties (match-end 3) (match-end 0) ip)
  4960. (org-rear-nonsticky-at (match-end 0)))
  4961. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  4962. (org-rear-nonsticky-at (match-beginning 1))
  4963. (add-text-properties (match-beginning 1) (match-end 1) vp)
  4964. (org-rear-nonsticky-at (match-end 1))
  4965. (add-text-properties (match-end 1) (match-end 0) ip)
  4966. (org-rear-nonsticky-at (match-end 0)))
  4967. t)))
  4968. (defun org-activate-dates (limit)
  4969. "Run through the buffer and add overlays to dates."
  4970. (if (re-search-forward org-tsr-regexp-both limit t)
  4971. (progn
  4972. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4973. (add-text-properties (match-beginning 0) (match-end 0)
  4974. (list 'mouse-face 'highlight
  4975. 'keymap org-mouse-map))
  4976. (org-rear-nonsticky-at (match-end 0))
  4977. (when org-display-custom-times
  4978. (if (match-end 3)
  4979. (org-display-custom-time (match-beginning 3) (match-end 3)))
  4980. (org-display-custom-time (match-beginning 1) (match-end 1)))
  4981. t)))
  4982. (defvar org-target-link-regexp nil
  4983. "Regular expression matching radio targets in plain text.")
  4984. (make-variable-buffer-local 'org-target-link-regexp)
  4985. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  4986. "Regular expression matching a link target.")
  4987. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  4988. "Regular expression matching a radio target.")
  4989. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  4990. "Regular expression matching any target.")
  4991. (defun org-activate-target-links (limit)
  4992. "Run through the buffer and add overlays to target matches."
  4993. (when org-target-link-regexp
  4994. (let ((case-fold-search t))
  4995. (if (re-search-forward org-target-link-regexp limit t)
  4996. (progn
  4997. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4998. (add-text-properties (match-beginning 0) (match-end 0)
  4999. (list 'mouse-face 'highlight
  5000. 'keymap org-mouse-map
  5001. 'help-echo "Radio target link"
  5002. 'org-linked-text t))
  5003. (org-rear-nonsticky-at (match-end 0))
  5004. t)))))
  5005. (defun org-update-radio-target-regexp ()
  5006. "Find all radio targets in this file and update the regular expression."
  5007. (interactive)
  5008. (when (memq 'radio org-activate-links)
  5009. (setq org-target-link-regexp
  5010. (org-make-target-link-regexp (org-all-targets 'radio)))
  5011. (org-restart-font-lock)))
  5012. (defun org-hide-wide-columns (limit)
  5013. (let (s e)
  5014. (setq s (text-property-any (point) (or limit (point-max))
  5015. 'org-cwidth t))
  5016. (when s
  5017. (setq e (next-single-property-change s 'org-cwidth))
  5018. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  5019. (goto-char e)
  5020. t)))
  5021. (defvar org-latex-and-specials-regexp nil
  5022. "Regular expression for highlighting export special stuff.")
  5023. (defvar org-match-substring-regexp)
  5024. (defvar org-match-substring-with-braces-regexp)
  5025. ;; This should be with the exporter code, but we also use if for font-locking
  5026. (defconst org-export-html-special-string-regexps
  5027. '(("\\\\-" . "&shy;")
  5028. ("---\\([^-]\\)" . "&mdash;\\1")
  5029. ("--\\([^-]\\)" . "&ndash;\\1")
  5030. ("\\.\\.\\." . "&hellip;"))
  5031. "Regular expressions for special string conversion.")
  5032. (defun org-compute-latex-and-specials-regexp ()
  5033. "Compute regular expression for stuff treated specially by exporters."
  5034. (if (not org-highlight-latex-fragments-and-specials)
  5035. (org-set-local 'org-latex-and-specials-regexp nil)
  5036. (require 'org-exp)
  5037. (let*
  5038. ((matchers (plist-get org-format-latex-options :matchers))
  5039. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  5040. org-latex-regexps)))
  5041. (org-export-allow-BIND nil)
  5042. (options (org-combine-plists (org-default-export-plist)
  5043. (org-infile-export-plist)))
  5044. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  5045. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  5046. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  5047. (org-export-html-expand (plist-get options :expand-quoted-html))
  5048. (org-export-with-special-strings (plist-get options :special-strings))
  5049. (re-sub
  5050. (cond
  5051. ((equal org-export-with-sub-superscripts '{})
  5052. (list org-match-substring-with-braces-regexp))
  5053. (org-export-with-sub-superscripts
  5054. (list org-match-substring-regexp))
  5055. (t nil)))
  5056. (re-latex
  5057. (if org-export-with-LaTeX-fragments
  5058. (mapcar (lambda (x) (nth 1 x)) latexs)))
  5059. (re-macros
  5060. (if org-export-with-TeX-macros
  5061. (list (concat "\\\\"
  5062. (regexp-opt
  5063. (append
  5064. (delq nil
  5065. (mapcar 'car-safe
  5066. (append org-entities-user
  5067. org-entities)))
  5068. (if (boundp 'org-latex-entities)
  5069. (mapcar (lambda (x)
  5070. (or (car-safe x) x))
  5071. org-latex-entities)
  5072. nil))
  5073. 'words))) ; FIXME
  5074. ))
  5075. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  5076. (re-special (if org-export-with-special-strings
  5077. (mapcar (lambda (x) (car x))
  5078. org-export-html-special-string-regexps)))
  5079. (re-rest
  5080. (delq nil
  5081. (list
  5082. (if org-export-html-expand "@<[^>\n]+>")
  5083. ))))
  5084. (org-set-local
  5085. 'org-latex-and-specials-regexp
  5086. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  5087. re-rest) "\\|")))))
  5088. (defun org-do-latex-and-special-faces (limit)
  5089. "Run through the buffer and add overlays to links."
  5090. (when org-latex-and-specials-regexp
  5091. (let (rtn d)
  5092. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  5093. limit t))
  5094. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  5095. 'face))
  5096. '(org-code org-verbatim underline)))
  5097. (progn
  5098. (setq rtn t
  5099. d (cond ((member (char-after (1+ (match-beginning 0)))
  5100. '(?_ ?^)) 1)
  5101. (t 0)))
  5102. (font-lock-prepend-text-property
  5103. (+ d (match-beginning 0)) (match-end 0)
  5104. 'face 'org-latex-and-export-specials)
  5105. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  5106. '(font-lock-multiline t)))))
  5107. rtn)))
  5108. (defun org-restart-font-lock ()
  5109. "Restart `font-lock-mode', to force refontification."
  5110. (when (and (boundp 'font-lock-mode) font-lock-mode)
  5111. (font-lock-mode -1)
  5112. (font-lock-mode 1)))
  5113. (defun org-all-targets (&optional radio)
  5114. "Return a list of all targets in this file.
  5115. With optional argument RADIO, only find radio targets."
  5116. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  5117. rtn)
  5118. (save-excursion
  5119. (goto-char (point-min))
  5120. (while (re-search-forward re nil t)
  5121. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  5122. rtn)))
  5123. (defun org-make-target-link-regexp (targets)
  5124. "Make regular expression matching all strings in TARGETS.
  5125. The regular expression finds the targets also if there is a line break
  5126. between words."
  5127. (and targets
  5128. (concat
  5129. "\\<\\("
  5130. (mapconcat
  5131. (lambda (x)
  5132. (setq x (regexp-quote x))
  5133. (while (string-match " +" x)
  5134. (setq x (replace-match "\\s-+" t t x)))
  5135. x)
  5136. targets
  5137. "\\|")
  5138. "\\)\\>")))
  5139. (defun org-activate-tags (limit)
  5140. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  5141. (progn
  5142. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5143. (add-text-properties (match-beginning 1) (match-end 1)
  5144. (list 'mouse-face 'highlight
  5145. 'keymap org-mouse-map))
  5146. (org-rear-nonsticky-at (match-end 1))
  5147. t)))
  5148. (defun org-outline-level ()
  5149. "Compute the outline level of the heading at point.
  5150. This function assumes that the cursor is at the beginning of a line matched
  5151. by `outline-regexp'. Otherwise it returns garbage.
  5152. If this is called at a normal headline, the level is the number of stars.
  5153. Use `org-reduced-level' to remove the effect of `org-odd-levels'."
  5154. (save-excursion
  5155. (looking-at org-outline-regexp)
  5156. (1- (- (match-end 0) (match-beginning 0)))))
  5157. (defvar org-font-lock-keywords nil)
  5158. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\+?\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  5159. "Regular expression matching a property line.")
  5160. (defvar org-font-lock-hook nil
  5161. "Functions to be called for special font lock stuff.")
  5162. (defvar org-font-lock-set-keywords-hook nil
  5163. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5164. This is called after `org-font-lock-extra-keywords' is defined, but before
  5165. it is installed to be used by font lock. This can be useful if something
  5166. needs to be inserted at a specific position in the font-lock sequence.")
  5167. (defun org-font-lock-hook (limit)
  5168. (run-hook-with-args 'org-font-lock-hook limit))
  5169. (defun org-set-font-lock-defaults ()
  5170. (let* ((em org-fontify-emphasized-text)
  5171. (lk org-activate-links)
  5172. (org-font-lock-extra-keywords
  5173. (list
  5174. ;; Call the hook
  5175. '(org-font-lock-hook)
  5176. ;; Headlines
  5177. `(,(if org-fontify-whole-heading-line
  5178. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5179. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5180. (1 (org-get-level-face 1))
  5181. (2 (org-get-level-face 2))
  5182. (3 (org-get-level-face 3)))
  5183. ;; Table lines
  5184. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5185. (1 'org-table t))
  5186. ;; Table internals
  5187. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5188. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5189. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5190. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5191. ;; Drawers
  5192. (list org-drawer-regexp '(0 'org-special-keyword t))
  5193. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  5194. ;; Properties
  5195. (list org-property-re
  5196. '(1 'org-special-keyword t)
  5197. '(3 'org-property-value t))
  5198. ;; Links
  5199. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5200. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5201. (if (memq 'plain lk) '(org-activate-plain-links))
  5202. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5203. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  5204. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5205. (if (memq 'footnote lk) '(org-activate-footnote-links))
  5206. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5207. '(org-hide-wide-columns (0 nil append))
  5208. ;; TODO keyword
  5209. (list (format org-heading-keyword-regexp-format
  5210. org-todo-regexp)
  5211. '(2 (org-get-todo-face 2) t))
  5212. ;; DONE
  5213. (if org-fontify-done-headline
  5214. (list (format org-heading-keyword-regexp-format
  5215. (concat
  5216. "\\(?:"
  5217. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5218. "\\)"))
  5219. '(2 'org-headline-done t))
  5220. nil)
  5221. ;; Priorities
  5222. '(org-font-lock-add-priority-faces)
  5223. ;; Tags
  5224. '(org-font-lock-add-tag-faces)
  5225. ;; Special keywords
  5226. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5227. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5228. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5229. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5230. ;; Emphasis
  5231. (if em
  5232. (if (featurep 'xemacs)
  5233. '(org-do-emphasis-faces (0 nil append))
  5234. '(org-do-emphasis-faces)))
  5235. ;; Checkboxes
  5236. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5237. 1 'org-checkbox prepend)
  5238. (if (cdr (assq 'checkbox org-list-automatic-rules))
  5239. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5240. (0 (org-get-checkbox-statistics-face) t)))
  5241. ;; Description list items
  5242. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5243. 1 'org-list-dt prepend)
  5244. ;; ARCHIVEd headings
  5245. (list (concat
  5246. org-outline-regexp-bol
  5247. "\\(.*:" org-archive-tag ":.*\\)")
  5248. '(1 'org-archived prepend))
  5249. ;; Specials
  5250. '(org-do-latex-and-special-faces)
  5251. '(org-fontify-entities)
  5252. '(org-raise-scripts)
  5253. ;; Code
  5254. '(org-activate-code (1 'org-code t))
  5255. ;; COMMENT
  5256. (list (format org-heading-keyword-regexp-format
  5257. (concat "\\("
  5258. org-comment-string "\\|" org-quote-string
  5259. "\\)"))
  5260. '(2 'org-special-keyword t))
  5261. '("^#.*" (0 'font-lock-comment-face t))
  5262. ;; Blocks and meta lines
  5263. '(org-fontify-meta-lines-and-blocks)
  5264. )))
  5265. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5266. (run-hooks 'org-font-lock-set-keywords-hook)
  5267. ;; Now set the full font-lock-keywords
  5268. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5269. (org-set-local 'font-lock-defaults
  5270. '(org-font-lock-keywords t nil nil backward-paragraph))
  5271. (kill-local-variable 'font-lock-keywords) nil))
  5272. (defun org-toggle-pretty-entities ()
  5273. "Toggle the composition display of entities as UTF8 characters."
  5274. (interactive)
  5275. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5276. (org-restart-font-lock)
  5277. (if org-pretty-entities
  5278. (message "Entities are displayed as UTF8 characters")
  5279. (save-restriction
  5280. (widen)
  5281. (org-decompose-region (point-min) (point-max))
  5282. (message "Entities are displayed plain"))))
  5283. (defun org-fontify-entities (limit)
  5284. "Find an entity to fontify."
  5285. (let (ee)
  5286. (when org-pretty-entities
  5287. (catch 'match
  5288. (while (re-search-forward
  5289. "\\\\\\(frac[13][24]\\|[a-zA-Z]+\\)\\($\\|[^[:alpha:]\n]\\)"
  5290. limit t)
  5291. (if (and (not (org-in-indented-comment-line))
  5292. (setq ee (org-entity-get (match-string 1)))
  5293. (= (length (nth 6 ee)) 1))
  5294. (progn
  5295. (add-text-properties
  5296. (match-beginning 0) (match-end 1)
  5297. (list 'font-lock-fontified t))
  5298. (compose-region (match-beginning 0) (match-end 1)
  5299. (nth 6 ee) nil)
  5300. (backward-char 1)
  5301. (throw 'match t))))
  5302. nil))))
  5303. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5304. "Fontify string S like in Org-mode."
  5305. (with-temp-buffer
  5306. (insert s)
  5307. (let ((org-odd-levels-only odd-levels))
  5308. (org-mode)
  5309. (font-lock-fontify-buffer)
  5310. (buffer-string))))
  5311. (defvar org-m nil)
  5312. (defvar org-l nil)
  5313. (defvar org-f nil)
  5314. (defun org-get-level-face (n)
  5315. "Get the right face for match N in font-lock matching of headlines."
  5316. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5317. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5318. (if org-cycle-level-faces
  5319. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5320. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5321. (cond
  5322. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5323. ((eq n 2) org-f)
  5324. (t (if org-level-color-stars-only nil org-f))))
  5325. (defun org-get-todo-face (kwd)
  5326. "Get the right face for a TODO keyword KWD.
  5327. If KWD is a number, get the corresponding match group."
  5328. (if (numberp kwd) (setq kwd (match-string kwd)))
  5329. (or (org-face-from-face-or-color
  5330. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5331. (and (member kwd org-done-keywords) 'org-done)
  5332. 'org-todo))
  5333. (defun org-face-from-face-or-color (context inherit face-or-color)
  5334. "Create a face list that inherits INHERIT, but sets the foreground color.
  5335. When FACE-OR-COLOR is not a string, just return it."
  5336. (if (stringp face-or-color)
  5337. (list :inherit inherit
  5338. (cdr (assoc context org-faces-easy-properties))
  5339. face-or-color)
  5340. face-or-color))
  5341. (defun org-font-lock-add-tag-faces (limit)
  5342. "Add the special tag faces."
  5343. (when (and org-tag-faces org-tags-special-faces-re)
  5344. (while (re-search-forward org-tags-special-faces-re limit t)
  5345. (add-text-properties (match-beginning 1) (match-end 1)
  5346. (list 'face (org-get-tag-face 1)
  5347. 'font-lock-fontified t))
  5348. (backward-char 1))))
  5349. (defun org-font-lock-add-priority-faces (limit)
  5350. "Add the special priority faces."
  5351. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  5352. (add-text-properties
  5353. (match-beginning 0) (match-end 0)
  5354. (list 'face (or (org-face-from-face-or-color
  5355. 'priority 'org-special-keyword
  5356. (cdr (assoc (char-after (match-beginning 1))
  5357. org-priority-faces)))
  5358. 'org-special-keyword)
  5359. 'font-lock-fontified t))))
  5360. (defun org-get-tag-face (kwd)
  5361. "Get the right face for a TODO keyword KWD.
  5362. If KWD is a number, get the corresponding match group."
  5363. (if (numberp kwd) (setq kwd (match-string kwd)))
  5364. (or (org-face-from-face-or-color
  5365. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  5366. 'org-tag))
  5367. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5368. "Remove fontification and activation overlays from links."
  5369. (font-lock-default-unfontify-region beg end)
  5370. (let* ((buffer-undo-list t)
  5371. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5372. (inhibit-modification-hooks t)
  5373. deactivate-mark buffer-file-name buffer-file-truename)
  5374. (org-decompose-region beg end)
  5375. (remove-text-properties beg end
  5376. '(mouse-face t keymap t org-linked-text t
  5377. invisible t intangible t
  5378. org-no-flyspell t org-emphasis t))
  5379. (org-remove-font-lock-display-properties beg end)))
  5380. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5381. ((raise 0.3) (height 0.7))
  5382. ((raise -0.5))
  5383. ((raise 0.5)))
  5384. "Display properties for showing superscripts and subscripts.")
  5385. (defun org-remove-font-lock-display-properties (beg end)
  5386. "Remove specific display properties that have been added by font lock.
  5387. The will remove the raise properties that are used to show superscripts
  5388. and subscripts."
  5389. (let (next prop)
  5390. (while (< beg end)
  5391. (setq next (next-single-property-change beg 'display nil end)
  5392. prop (get-text-property beg 'display))
  5393. (if (member prop org-script-display)
  5394. (put-text-property beg next 'display nil))
  5395. (setq beg next))))
  5396. (defun org-raise-scripts (limit)
  5397. "Add raise properties to sub/superscripts."
  5398. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5399. (if (re-search-forward
  5400. (if (eq org-use-sub-superscripts t)
  5401. org-match-substring-regexp
  5402. org-match-substring-with-braces-regexp)
  5403. limit t)
  5404. (let* ((pos (point)) table-p comment-p
  5405. (mpos (match-beginning 3))
  5406. (emph-p (get-text-property mpos 'org-emphasis))
  5407. (link-p (get-text-property mpos 'mouse-face))
  5408. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5409. (goto-char (point-at-bol))
  5410. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5411. comment-p (org-looking-at-p "[ \t]*#"))
  5412. (goto-char pos)
  5413. ;; FIXME: Should we go back one character here, for a_b^c
  5414. ;; (goto-char (1- pos)) ;????????????????????
  5415. (if (or comment-p emph-p link-p keyw-p)
  5416. t
  5417. (put-text-property (match-beginning 3) (match-end 0)
  5418. 'display
  5419. (if (equal (char-after (match-beginning 2)) ?^)
  5420. (nth (if table-p 3 1) org-script-display)
  5421. (nth (if table-p 2 0) org-script-display)))
  5422. (add-text-properties (match-beginning 2) (match-end 2)
  5423. (list 'invisible t
  5424. 'org-dwidth t 'org-dwidth-n 1))
  5425. (if (and (eq (char-after (match-beginning 3)) ?{)
  5426. (eq (char-before (match-end 3)) ?}))
  5427. (progn
  5428. (add-text-properties
  5429. (match-beginning 3) (1+ (match-beginning 3))
  5430. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5431. (add-text-properties
  5432. (1- (match-end 3)) (match-end 3)
  5433. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5434. t)))))
  5435. ;;;; Visibility cycling, including org-goto and indirect buffer
  5436. ;;; Cycling
  5437. (defvar org-cycle-global-status nil)
  5438. (make-variable-buffer-local 'org-cycle-global-status)
  5439. (defvar org-cycle-subtree-status nil)
  5440. (make-variable-buffer-local 'org-cycle-subtree-status)
  5441. ;;;###autoload
  5442. (defvar org-inlinetask-min-level)
  5443. (defun org-cycle (&optional arg)
  5444. "TAB-action and visibility cycling for Org-mode.
  5445. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5446. is outline visibility cycling, but it also invokes other actions
  5447. in special contexts.
  5448. - When this function is called with a prefix argument, rotate the entire
  5449. buffer through 3 states (global cycling)
  5450. 1. OVERVIEW: Show only top-level headlines.
  5451. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5452. 3. SHOW ALL: Show everything.
  5453. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5454. determined by the variable `org-startup-folded', and by any VISIBILITY
  5455. properties in the buffer.
  5456. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5457. including any drawers.
  5458. - When inside a table, re-align the table and move to the next field.
  5459. - When point is at the beginning of a headline, rotate the subtree started
  5460. by this line through 3 different states (local cycling)
  5461. 1. FOLDED: Only the main headline is shown.
  5462. 2. CHILDREN: The main headline and the direct children are shown.
  5463. From this state, you can move to one of the children
  5464. and zoom in further.
  5465. 3. SUBTREE: Show the entire subtree, including body text.
  5466. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5467. - When point is at the beginning of an empty headline and the variable
  5468. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5469. of the headline by demoting and promoting it to likely levels. This
  5470. speeds up creation document structure by pressing TAB once or several
  5471. times right after creating a new headline.
  5472. - When there is a numeric prefix, go up to a heading with level ARG, do
  5473. a `show-subtree' and return to the previous cursor position. If ARG
  5474. is negative, go up that many levels.
  5475. - When point is not at the beginning of a headline, execute the global
  5476. binding for TAB, which is re-indenting the line. See the option
  5477. `org-cycle-emulate-tab' for details.
  5478. - Special case: if point is at the beginning of the buffer and there is
  5479. no headline in line 1, this function will act as if called with prefix arg
  5480. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5481. But only if also the variable `org-cycle-global-at-bob' is t."
  5482. (interactive "P")
  5483. (org-load-modules-maybe)
  5484. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5485. (and org-cycle-level-after-item/entry-creation
  5486. (or (org-cycle-level)
  5487. (org-cycle-item-indentation))))
  5488. (let* ((limit-level
  5489. (or org-cycle-max-level
  5490. (and (boundp 'org-inlinetask-min-level)
  5491. org-inlinetask-min-level
  5492. (1- org-inlinetask-min-level))))
  5493. (nstars (and limit-level
  5494. (if org-odd-levels-only
  5495. (and limit-level (1- (* limit-level 2)))
  5496. limit-level)))
  5497. (org-outline-regexp
  5498. (if (not (derived-mode-p 'org-mode))
  5499. outline-regexp
  5500. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5501. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5502. (not (looking-at org-outline-regexp))))
  5503. (org-cycle-hook
  5504. (if bob-special
  5505. (delq 'org-optimize-window-after-visibility-change
  5506. (copy-sequence org-cycle-hook))
  5507. org-cycle-hook))
  5508. (pos (point)))
  5509. (if (or bob-special (equal arg '(4)))
  5510. ;; special case: use global cycling
  5511. (setq arg t))
  5512. (cond
  5513. ((equal arg '(16))
  5514. (setq last-command 'dummy)
  5515. (org-set-startup-visibility)
  5516. (message "Startup visibility, plus VISIBILITY properties"))
  5517. ((equal arg '(64))
  5518. (show-all)
  5519. (message "Entire buffer visible, including drawers"))
  5520. ;; Table: enter it or move to the next field.
  5521. ((org-at-table-p 'any)
  5522. (if (org-at-table.el-p)
  5523. (message "Use C-c ' to edit table.el tables")
  5524. (if arg (org-table-edit-field t)
  5525. (org-table-justify-field-maybe)
  5526. (call-interactively 'org-table-next-field))))
  5527. ((run-hook-with-args-until-success
  5528. 'org-tab-after-check-for-table-hook))
  5529. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5530. ((eq arg t) (org-cycle-internal-global))
  5531. ;; Drawers: delegate to `org-flag-drawer'.
  5532. ((and org-drawers org-drawer-regexp
  5533. (save-excursion
  5534. (beginning-of-line 1)
  5535. (looking-at org-drawer-regexp)))
  5536. (org-flag-drawer ; toggle block visibility
  5537. (not (get-char-property (match-end 0) 'invisible))))
  5538. ;; Show-subtree, ARG levels up from here.
  5539. ((integerp arg)
  5540. (save-excursion
  5541. (org-back-to-heading)
  5542. (outline-up-heading (if (< arg 0) (- arg)
  5543. (- (funcall outline-level) arg)))
  5544. (org-show-subtree)))
  5545. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5546. ((and (featurep 'org-inlinetask)
  5547. (org-inlinetask-at-task-p)
  5548. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5549. (org-inlinetask-toggle-visibility))
  5550. ((org-try-cdlatex-tab))
  5551. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5552. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5553. (save-excursion (beginning-of-line 1)
  5554. (looking-at org-outline-regexp)))
  5555. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5556. (org-cycle-internal-local))
  5557. ;; From there: TAB emulation and template completion.
  5558. (buffer-read-only (org-back-to-heading))
  5559. ((run-hook-with-args-until-success
  5560. 'org-tab-after-check-for-cycling-hook))
  5561. ((org-try-structure-completion))
  5562. ((run-hook-with-args-until-success
  5563. 'org-tab-before-tab-emulation-hook))
  5564. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5565. (or (not (bolp))
  5566. (not (looking-at org-outline-regexp))))
  5567. (call-interactively (global-key-binding "\t")))
  5568. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5569. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5570. (or (and (eq org-cycle-emulate-tab 'white)
  5571. (= (match-end 0) (point-at-eol)))
  5572. (and (eq org-cycle-emulate-tab 'whitestart)
  5573. (>= (match-end 0) pos))))
  5574. t
  5575. (eq org-cycle-emulate-tab t))
  5576. (call-interactively (global-key-binding "\t")))
  5577. (t (save-excursion
  5578. (org-back-to-heading)
  5579. (org-cycle)))))))
  5580. (defun org-cycle-internal-global ()
  5581. "Do the global cycling action."
  5582. ;; Hack to avoid display of messages for .org attachments in Gnus
  5583. (let ((ga (string-match "\\*fontification" (buffer-name))))
  5584. (cond
  5585. ((and (eq last-command this-command)
  5586. (eq org-cycle-global-status 'overview))
  5587. ;; We just created the overview - now do table of contents
  5588. ;; This can be slow in very large buffers, so indicate action
  5589. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  5590. (unless ga (message "CONTENTS..."))
  5591. (org-content)
  5592. (unless ga (message "CONTENTS...done"))
  5593. (setq org-cycle-global-status 'contents)
  5594. (run-hook-with-args 'org-cycle-hook 'contents))
  5595. ((and (eq last-command this-command)
  5596. (eq org-cycle-global-status 'contents))
  5597. ;; We just showed the table of contents - now show everything
  5598. (run-hook-with-args 'org-pre-cycle-hook 'all)
  5599. (show-all)
  5600. (unless ga (message "SHOW ALL"))
  5601. (setq org-cycle-global-status 'all)
  5602. (run-hook-with-args 'org-cycle-hook 'all))
  5603. (t
  5604. ;; Default action: go to overview
  5605. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  5606. (org-overview)
  5607. (unless ga (message "OVERVIEW"))
  5608. (setq org-cycle-global-status 'overview)
  5609. (run-hook-with-args 'org-cycle-hook 'overview)))))
  5610. (defun org-cycle-internal-local ()
  5611. "Do the local cycling action."
  5612. (let ((goal-column 0) eoh eol eos has-children children-skipped struct)
  5613. ;; First, determine end of headline (EOH), end of subtree or item
  5614. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  5615. (save-excursion
  5616. (if (org-at-item-p)
  5617. (progn
  5618. (beginning-of-line)
  5619. (setq struct (org-list-struct))
  5620. (setq eoh (point-at-eol))
  5621. (setq eos (org-list-get-item-end-before-blank (point) struct))
  5622. (setq has-children (org-list-has-child-p (point) struct)))
  5623. (org-back-to-heading)
  5624. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  5625. (setq eos (save-excursion
  5626. (org-end-of-subtree t)
  5627. (unless (eobp)
  5628. (skip-chars-forward " \t\n"))
  5629. (if (eobp) (point) (1- (point)))))
  5630. (setq has-children
  5631. (or (save-excursion
  5632. (let ((level (funcall outline-level)))
  5633. (outline-next-heading)
  5634. (and (org-at-heading-p t)
  5635. (> (funcall outline-level) level))))
  5636. (save-excursion
  5637. (org-list-search-forward (org-item-beginning-re) eos t)))))
  5638. ;; Determine end invisible part of buffer (EOL)
  5639. (beginning-of-line 2)
  5640. ;; XEmacs doesn't have `next-single-char-property-change'
  5641. (if (featurep 'xemacs)
  5642. (while (and (not (eobp)) ;; this is like `next-line'
  5643. (get-char-property (1- (point)) 'invisible))
  5644. (beginning-of-line 2))
  5645. (while (and (not (eobp)) ;; this is like `next-line'
  5646. (get-char-property (1- (point)) 'invisible))
  5647. (goto-char (next-single-char-property-change (point) 'invisible))
  5648. (and (eolp) (beginning-of-line 2))))
  5649. (setq eol (point)))
  5650. ;; Find out what to do next and set `this-command'
  5651. (cond
  5652. ((= eos eoh)
  5653. ;; Nothing is hidden behind this heading
  5654. (run-hook-with-args 'org-pre-cycle-hook 'empty)
  5655. (message "EMPTY ENTRY")
  5656. (setq org-cycle-subtree-status nil)
  5657. (save-excursion
  5658. (goto-char eos)
  5659. (outline-next-heading)
  5660. (if (outline-invisible-p) (org-flag-heading nil))))
  5661. ((and (or (>= eol eos)
  5662. (not (string-match "\\S-" (buffer-substring eol eos))))
  5663. (or has-children
  5664. (not (setq children-skipped
  5665. org-cycle-skip-children-state-if-no-children))))
  5666. ;; Entire subtree is hidden in one line: children view
  5667. (run-hook-with-args 'org-pre-cycle-hook 'children)
  5668. (if (org-at-item-p)
  5669. (org-list-set-item-visibility (point-at-bol) struct 'children)
  5670. (org-show-entry)
  5671. (org-with-limited-levels (show-children))
  5672. ;; FIXME: This slows down the func way too much.
  5673. ;; How keep drawers hidden in subtree anyway?
  5674. ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
  5675. ;; (org-cycle-hide-drawers 'subtree))
  5676. ;; Fold every list in subtree to top-level items.
  5677. (when (eq org-cycle-include-plain-lists 'integrate)
  5678. (save-excursion
  5679. (org-back-to-heading)
  5680. (while (org-list-search-forward (org-item-beginning-re) eos t)
  5681. (beginning-of-line 1)
  5682. (let* ((struct (org-list-struct))
  5683. (prevs (org-list-prevs-alist struct))
  5684. (end (org-list-get-bottom-point struct)))
  5685. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  5686. (org-list-get-all-items (point) struct prevs))
  5687. (goto-char end))))))
  5688. (message "CHILDREN")
  5689. (save-excursion
  5690. (goto-char eos)
  5691. (outline-next-heading)
  5692. (if (outline-invisible-p) (org-flag-heading nil)))
  5693. (setq org-cycle-subtree-status 'children)
  5694. (run-hook-with-args 'org-cycle-hook 'children))
  5695. ((or children-skipped
  5696. (and (eq last-command this-command)
  5697. (eq org-cycle-subtree-status 'children)))
  5698. ;; We just showed the children, or no children are there,
  5699. ;; now show everything.
  5700. (run-hook-with-args 'org-pre-cycle-hook 'subtree)
  5701. (outline-flag-region eoh eos nil)
  5702. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  5703. (setq org-cycle-subtree-status 'subtree)
  5704. (run-hook-with-args 'org-cycle-hook 'subtree))
  5705. (t
  5706. ;; Default action: hide the subtree.
  5707. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  5708. (outline-flag-region eoh eos t)
  5709. (message "FOLDED")
  5710. (setq org-cycle-subtree-status 'folded)
  5711. (run-hook-with-args 'org-cycle-hook 'folded)))))
  5712. ;;;###autoload
  5713. (defun org-global-cycle (&optional arg)
  5714. "Cycle the global visibility. For details see `org-cycle'.
  5715. With \\[universal-argument] prefix arg, switch to startup visibility.
  5716. With a numeric prefix, show all headlines up to that level."
  5717. (interactive "P")
  5718. (let ((org-cycle-include-plain-lists
  5719. (if (derived-mode-p 'org-mode) org-cycle-include-plain-lists nil)))
  5720. (cond
  5721. ((integerp arg)
  5722. (show-all)
  5723. (hide-sublevels arg)
  5724. (setq org-cycle-global-status 'contents))
  5725. ((equal arg '(4))
  5726. (org-set-startup-visibility)
  5727. (message "Startup visibility, plus VISIBILITY properties."))
  5728. (t
  5729. (org-cycle '(4))))))
  5730. (defun org-set-startup-visibility ()
  5731. "Set the visibility required by startup options and properties."
  5732. (cond
  5733. ((eq org-startup-folded t)
  5734. (org-cycle '(4)))
  5735. ((eq org-startup-folded 'content)
  5736. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  5737. (org-cycle '(4)) (org-cycle '(4)))))
  5738. (unless (eq org-startup-folded 'showeverything)
  5739. (if org-hide-block-startup (org-hide-block-all))
  5740. (org-set-visibility-according-to-property 'no-cleanup)
  5741. (org-cycle-hide-archived-subtrees 'all)
  5742. (org-cycle-hide-drawers 'all)
  5743. (org-cycle-show-empty-lines t)))
  5744. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  5745. "Switch subtree visibilities according to :VISIBILITY: property."
  5746. (interactive)
  5747. (let (org-show-entry-below state)
  5748. (save-excursion
  5749. (goto-char (point-min))
  5750. (while (re-search-forward
  5751. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  5752. nil t)
  5753. (setq state (match-string 1))
  5754. (save-excursion
  5755. (org-back-to-heading t)
  5756. (hide-subtree)
  5757. (org-reveal)
  5758. (cond
  5759. ((equal state '("fold" "folded"))
  5760. (hide-subtree))
  5761. ((equal state "children")
  5762. (org-show-hidden-entry)
  5763. (show-children))
  5764. ((equal state "content")
  5765. (save-excursion
  5766. (save-restriction
  5767. (org-narrow-to-subtree)
  5768. (org-content))))
  5769. ((member state '("all" "showall"))
  5770. (show-subtree)))))
  5771. (unless no-cleanup
  5772. (org-cycle-hide-archived-subtrees 'all)
  5773. (org-cycle-hide-drawers 'all)
  5774. (org-cycle-show-empty-lines 'all)))))
  5775. ;; This function uses outline-regexp instead of the more fundamental
  5776. ;; org-outline-regexp so that org-cycle-global works outside of Org
  5777. ;; buffers, where outline-regexp is needed.
  5778. (defun org-overview ()
  5779. "Switch to overview mode, showing only top-level headlines.
  5780. Really, this shows all headlines with level equal or greater than the level
  5781. of the first headline in the buffer. This is important, because if the
  5782. first headline is not level one, then (hide-sublevels 1) gives confusing
  5783. results."
  5784. (interactive)
  5785. (let ((level (save-excursion
  5786. (goto-char (point-min))
  5787. (if (re-search-forward (concat "^" outline-regexp) nil t)
  5788. (progn
  5789. (goto-char (match-beginning 0))
  5790. (funcall outline-level))))))
  5791. (and level (hide-sublevels level))))
  5792. (defun org-content (&optional arg)
  5793. "Show all headlines in the buffer, like a table of contents.
  5794. With numerical argument N, show content up to level N."
  5795. (interactive "P")
  5796. (save-excursion
  5797. ;; Visit all headings and show their offspring
  5798. (and (integerp arg) (org-overview))
  5799. (goto-char (point-max))
  5800. (catch 'exit
  5801. (while (and (progn (condition-case nil
  5802. (outline-previous-visible-heading 1)
  5803. (error (goto-char (point-min))))
  5804. t)
  5805. (looking-at org-outline-regexp))
  5806. (if (integerp arg)
  5807. (show-children (1- arg))
  5808. (show-branches))
  5809. (if (bobp) (throw 'exit nil))))))
  5810. (defun org-optimize-window-after-visibility-change (state)
  5811. "Adjust the window after a change in outline visibility.
  5812. This function is the default value of the hook `org-cycle-hook'."
  5813. (when (get-buffer-window (current-buffer))
  5814. (cond
  5815. ((eq state 'content) nil)
  5816. ((eq state 'all) nil)
  5817. ((eq state 'folded) nil)
  5818. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  5819. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  5820. (defun org-remove-empty-overlays-at (pos)
  5821. "Remove outline overlays that do not contain non-white stuff."
  5822. (mapc
  5823. (lambda (o)
  5824. (and (eq 'outline (overlay-get o 'invisible))
  5825. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  5826. (overlay-end o))))
  5827. (delete-overlay o)))
  5828. (overlays-at pos)))
  5829. (defun org-clean-visibility-after-subtree-move ()
  5830. "Fix visibility issues after moving a subtree."
  5831. ;; First, find a reasonable region to look at:
  5832. ;; Start two siblings above, end three below
  5833. (let* ((beg (save-excursion
  5834. (and (org-get-last-sibling)
  5835. (org-get-last-sibling))
  5836. (point)))
  5837. (end (save-excursion
  5838. (and (org-get-next-sibling)
  5839. (org-get-next-sibling)
  5840. (org-get-next-sibling))
  5841. (if (org-at-heading-p)
  5842. (point-at-eol)
  5843. (point))))
  5844. (level (looking-at "\\*+"))
  5845. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  5846. (save-excursion
  5847. (save-restriction
  5848. (narrow-to-region beg end)
  5849. (when re
  5850. ;; Properly fold already folded siblings
  5851. (goto-char (point-min))
  5852. (while (re-search-forward re nil t)
  5853. (if (and (not (outline-invisible-p))
  5854. (save-excursion
  5855. (goto-char (point-at-eol)) (outline-invisible-p)))
  5856. (hide-entry))))
  5857. (org-cycle-show-empty-lines 'overview)
  5858. (org-cycle-hide-drawers 'overview)))))
  5859. (defun org-cycle-show-empty-lines (state)
  5860. "Show empty lines above all visible headlines.
  5861. The region to be covered depends on STATE when called through
  5862. `org-cycle-hook'. Lisp program can use t for STATE to get the
  5863. entire buffer covered. Note that an empty line is only shown if there
  5864. are at least `org-cycle-separator-lines' empty lines before the headline."
  5865. (when (not (= org-cycle-separator-lines 0))
  5866. (save-excursion
  5867. (let* ((n (abs org-cycle-separator-lines))
  5868. (re (cond
  5869. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  5870. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  5871. (t (let ((ns (number-to-string (- n 2))))
  5872. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  5873. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  5874. beg end b e)
  5875. (cond
  5876. ((memq state '(overview contents t))
  5877. (setq beg (point-min) end (point-max)))
  5878. ((memq state '(children folded))
  5879. (setq beg (point) end (progn (org-end-of-subtree t t)
  5880. (beginning-of-line 2)
  5881. (point)))))
  5882. (when beg
  5883. (goto-char beg)
  5884. (while (re-search-forward re end t)
  5885. (unless (get-char-property (match-end 1) 'invisible)
  5886. (setq e (match-end 1))
  5887. (if (< org-cycle-separator-lines 0)
  5888. (setq b (save-excursion
  5889. (goto-char (match-beginning 0))
  5890. (org-back-over-empty-lines)
  5891. (if (save-excursion
  5892. (goto-char (max (point-min) (1- (point))))
  5893. (org-at-heading-p))
  5894. (1- (point))
  5895. (point))))
  5896. (setq b (match-beginning 1)))
  5897. (outline-flag-region b e nil)))))))
  5898. ;; Never hide empty lines at the end of the file.
  5899. (save-excursion
  5900. (goto-char (point-max))
  5901. (outline-previous-heading)
  5902. (outline-end-of-heading)
  5903. (if (and (looking-at "[ \t\n]+")
  5904. (= (match-end 0) (point-max)))
  5905. (outline-flag-region (point) (match-end 0) nil))))
  5906. (defun org-show-empty-lines-in-parent ()
  5907. "Move to the parent and re-show empty lines before visible headlines."
  5908. (save-excursion
  5909. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  5910. (org-cycle-show-empty-lines context))))
  5911. (defun org-files-list ()
  5912. "Return `org-agenda-files' list, plus all open org-mode files.
  5913. This is useful for operations that need to scan all of a user's
  5914. open and agenda-wise Org files."
  5915. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  5916. (dolist (buf (buffer-list))
  5917. (with-current-buffer buf
  5918. (if (and (derived-mode-p 'org-mode) (buffer-file-name))
  5919. (let ((file (expand-file-name (buffer-file-name))))
  5920. (unless (member file files)
  5921. (push file files))))))
  5922. files))
  5923. (defsubst org-entry-beginning-position ()
  5924. "Return the beginning position of the current entry."
  5925. (save-excursion (outline-back-to-heading t) (point)))
  5926. (defsubst org-entry-end-position ()
  5927. "Return the end position of the current entry."
  5928. (save-excursion (outline-next-heading) (point)))
  5929. (defun org-cycle-hide-drawers (state)
  5930. "Re-hide all drawers after a visibility state change."
  5931. (when (and (derived-mode-p 'org-mode)
  5932. (not (memq state '(overview folded contents))))
  5933. (save-excursion
  5934. (let* ((globalp (memq state '(contents all)))
  5935. (beg (if globalp (point-min) (point)))
  5936. (end (if globalp (point-max)
  5937. (if (eq state 'children)
  5938. (save-excursion (outline-next-heading) (point))
  5939. (org-end-of-subtree t)))))
  5940. (goto-char beg)
  5941. (while (re-search-forward org-drawer-regexp end t)
  5942. (org-flag-drawer t))))))
  5943. (defun org-flag-drawer (flag)
  5944. (save-excursion
  5945. (beginning-of-line 1)
  5946. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  5947. (let ((b (match-end 0)))
  5948. (if (re-search-forward
  5949. "^[ \t]*:END:"
  5950. (save-excursion (outline-next-heading) (point)) t)
  5951. (outline-flag-region b (point-at-eol) flag)
  5952. (error ":END: line missing at position %s" b))))))
  5953. (defun org-subtree-end-visible-p ()
  5954. "Is the end of the current subtree visible?"
  5955. (pos-visible-in-window-p
  5956. (save-excursion (org-end-of-subtree t) (point))))
  5957. (defun org-first-headline-recenter (&optional N)
  5958. "Move cursor to the first headline and recenter the headline.
  5959. Optional argument N means put the headline into the Nth line of the window."
  5960. (goto-char (point-min))
  5961. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  5962. (beginning-of-line)
  5963. (recenter (prefix-numeric-value N))))
  5964. ;;; Saving and restoring visibility
  5965. (defun org-outline-overlay-data (&optional use-markers)
  5966. "Return a list of the locations of all outline overlays.
  5967. These are overlays with the `invisible' property value `outline'.
  5968. The return value is a list of cons cells, with start and stop
  5969. positions for each overlay.
  5970. If USE-MARKERS is set, return the positions as markers."
  5971. (let (beg end)
  5972. (save-excursion
  5973. (save-restriction
  5974. (widen)
  5975. (delq nil
  5976. (mapcar (lambda (o)
  5977. (when (eq (overlay-get o 'invisible) 'outline)
  5978. (setq beg (overlay-start o)
  5979. end (overlay-end o))
  5980. (and beg end (> end beg)
  5981. (if use-markers
  5982. (cons (move-marker (make-marker) beg)
  5983. (move-marker (make-marker) end))
  5984. (cons beg end)))))
  5985. (overlays-in (point-min) (point-max))))))))
  5986. (defun org-set-outline-overlay-data (data)
  5987. "Create visibility overlays for all positions in DATA.
  5988. DATA should have been made by `org-outline-overlay-data'."
  5989. (let (o)
  5990. (save-excursion
  5991. (save-restriction
  5992. (widen)
  5993. (show-all)
  5994. (mapc (lambda (c)
  5995. (outline-flag-region (car c) (cdr c) t))
  5996. data)))))
  5997. ;;; Folding of blocks
  5998. (defvar org-hide-block-overlays nil
  5999. "Overlays hiding blocks.")
  6000. (make-variable-buffer-local 'org-hide-block-overlays)
  6001. (defun org-block-map (function &optional start end)
  6002. "Call FUNCTION at the head of all source blocks in the current buffer.
  6003. Optional arguments START and END can be used to limit the range."
  6004. (let ((start (or start (point-min)))
  6005. (end (or end (point-max))))
  6006. (save-excursion
  6007. (goto-char start)
  6008. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  6009. (save-excursion
  6010. (save-match-data
  6011. (goto-char (match-beginning 0))
  6012. (funcall function)))))))
  6013. (defun org-hide-block-toggle-all ()
  6014. "Toggle the visibility of all blocks in the current buffer."
  6015. (org-block-map #'org-hide-block-toggle))
  6016. (defun org-hide-block-all ()
  6017. "Fold all blocks in the current buffer."
  6018. (interactive)
  6019. (org-show-block-all)
  6020. (org-block-map #'org-hide-block-toggle-maybe))
  6021. (defun org-show-block-all ()
  6022. "Unfold all blocks in the current buffer."
  6023. (interactive)
  6024. (mapc 'delete-overlay org-hide-block-overlays)
  6025. (setq org-hide-block-overlays nil))
  6026. (defun org-hide-block-toggle-maybe ()
  6027. "Toggle visibility of block at point."
  6028. (interactive)
  6029. (let ((case-fold-search t))
  6030. (if (save-excursion
  6031. (beginning-of-line 1)
  6032. (looking-at org-block-regexp))
  6033. (progn (org-hide-block-toggle)
  6034. t) ;; to signal that we took action
  6035. nil))) ;; to signal that we did not
  6036. (defun org-hide-block-toggle (&optional force)
  6037. "Toggle the visibility of the current block."
  6038. (interactive)
  6039. (save-excursion
  6040. (beginning-of-line)
  6041. (if (re-search-forward org-block-regexp nil t)
  6042. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  6043. (end (match-end 0)) ;; end of entire body
  6044. ov)
  6045. (if (memq t (mapcar (lambda (overlay)
  6046. (eq (overlay-get overlay 'invisible)
  6047. 'org-hide-block))
  6048. (overlays-at start)))
  6049. (if (or (not force) (eq force 'off))
  6050. (mapc (lambda (ov)
  6051. (when (member ov org-hide-block-overlays)
  6052. (setq org-hide-block-overlays
  6053. (delq ov org-hide-block-overlays)))
  6054. (when (eq (overlay-get ov 'invisible)
  6055. 'org-hide-block)
  6056. (delete-overlay ov)))
  6057. (overlays-at start)))
  6058. (setq ov (make-overlay start end))
  6059. (overlay-put ov 'invisible 'org-hide-block)
  6060. ;; make the block accessible to isearch
  6061. (overlay-put
  6062. ov 'isearch-open-invisible
  6063. (lambda (ov)
  6064. (when (member ov org-hide-block-overlays)
  6065. (setq org-hide-block-overlays
  6066. (delq ov org-hide-block-overlays)))
  6067. (when (eq (overlay-get ov 'invisible)
  6068. 'org-hide-block)
  6069. (delete-overlay ov))))
  6070. (push ov org-hide-block-overlays)))
  6071. (error "Not looking at a source block"))))
  6072. ;; org-tab-after-check-for-cycling-hook
  6073. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  6074. ;; Remove overlays when changing major mode
  6075. (add-hook 'org-mode-hook
  6076. (lambda () (org-add-hook 'change-major-mode-hook
  6077. 'org-show-block-all 'append 'local)))
  6078. ;;; Org-goto
  6079. (defvar org-goto-window-configuration nil)
  6080. (defvar org-goto-marker nil)
  6081. (defvar org-goto-map
  6082. (let ((map (make-sparse-keymap)))
  6083. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  6084. (while (setq cmd (pop cmds))
  6085. (substitute-key-definition cmd cmd map global-map)))
  6086. (suppress-keymap map)
  6087. (org-defkey map "\C-m" 'org-goto-ret)
  6088. (org-defkey map [(return)] 'org-goto-ret)
  6089. (org-defkey map [(left)] 'org-goto-left)
  6090. (org-defkey map [(right)] 'org-goto-right)
  6091. (org-defkey map [(control ?g)] 'org-goto-quit)
  6092. (org-defkey map "\C-i" 'org-cycle)
  6093. (org-defkey map [(tab)] 'org-cycle)
  6094. (org-defkey map [(down)] 'outline-next-visible-heading)
  6095. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6096. (if org-goto-auto-isearch
  6097. (if (fboundp 'define-key-after)
  6098. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6099. nil)
  6100. (org-defkey map "q" 'org-goto-quit)
  6101. (org-defkey map "n" 'outline-next-visible-heading)
  6102. (org-defkey map "p" 'outline-previous-visible-heading)
  6103. (org-defkey map "f" 'outline-forward-same-level)
  6104. (org-defkey map "b" 'outline-backward-same-level)
  6105. (org-defkey map "u" 'outline-up-heading))
  6106. (org-defkey map "/" 'org-occur)
  6107. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6108. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6109. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6110. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6111. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6112. map))
  6113. (defconst org-goto-help
  6114. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  6115. RET=jump to location [Q]uit and return to previous location
  6116. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6117. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6118. ;; FIXME: Docstring does not mention both interfaces
  6119. (defun org-goto (&optional alternative-interface)
  6120. "Look up a different location in the current file, keeping current visibility.
  6121. When you want look-up or go to a different location in a
  6122. document, the fastest way is often to fold the entire buffer and
  6123. then dive into the tree. This method has the disadvantage, that
  6124. the previous location will be folded, which may not be what you
  6125. want.
  6126. This command works around this by showing a copy of the current
  6127. buffer in an indirect buffer, in overview mode. You can dive
  6128. into the tree in that copy, use org-occur and incremental search
  6129. to find a location. When pressing RET or `Q', the command
  6130. returns to the original buffer in which the visibility is still
  6131. unchanged. After RET it will also jump to the location selected
  6132. in the indirect buffer and expose the headline hierarchy above.
  6133. With a prefix argument, use the alternative interface: e.g. if
  6134. `org-goto-interface' is 'outline use 'outline-path-completion."
  6135. (interactive "P")
  6136. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6137. (org-refile-use-outline-path t)
  6138. (org-refile-target-verify-function nil)
  6139. (interface
  6140. (if (not alternative-interface)
  6141. org-goto-interface
  6142. (if (eq org-goto-interface 'outline)
  6143. 'outline-path-completion
  6144. 'outline)))
  6145. (org-goto-start-pos (point))
  6146. (selected-point
  6147. (if (eq interface 'outline)
  6148. (car (org-get-location (current-buffer) org-goto-help))
  6149. (let ((pa (org-refile-get-location "Goto" nil nil t)))
  6150. (org-refile-check-position pa)
  6151. (nth 3 pa)))))
  6152. (if selected-point
  6153. (progn
  6154. (org-mark-ring-push org-goto-start-pos)
  6155. (goto-char selected-point)
  6156. (if (or (outline-invisible-p) (org-invisible-p2))
  6157. (org-show-context 'org-goto)))
  6158. (message "Quit"))))
  6159. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6160. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6161. (defvar org-goto-local-auto-isearch-map) ; defined below
  6162. (defun org-get-location (buf help)
  6163. "Let the user select a location in the Org-mode buffer BUF.
  6164. This function uses a recursive edit. It returns the selected position
  6165. or nil."
  6166. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6167. (isearch-hide-immediately nil)
  6168. (isearch-search-fun-function
  6169. (lambda () 'org-goto-local-search-headings))
  6170. (org-goto-selected-point org-goto-exit-command)
  6171. (pop-up-frames nil)
  6172. (special-display-buffer-names nil)
  6173. (special-display-regexps nil)
  6174. (special-display-function nil))
  6175. (save-excursion
  6176. (save-window-excursion
  6177. (delete-other-windows)
  6178. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6179. (org-pop-to-buffer-same-window
  6180. (condition-case nil
  6181. (make-indirect-buffer (current-buffer) "*org-goto*")
  6182. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6183. (with-output-to-temp-buffer "*Help*"
  6184. (princ help))
  6185. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  6186. (setq buffer-read-only nil)
  6187. (let ((org-startup-truncated t)
  6188. (org-startup-folded nil)
  6189. (org-startup-align-all-tables nil))
  6190. (org-mode)
  6191. (org-overview))
  6192. (setq buffer-read-only t)
  6193. (if (and (boundp 'org-goto-start-pos)
  6194. (integer-or-marker-p org-goto-start-pos))
  6195. (let ((org-show-hierarchy-above t)
  6196. (org-show-siblings t)
  6197. (org-show-following-heading t))
  6198. (goto-char org-goto-start-pos)
  6199. (and (outline-invisible-p) (org-show-context)))
  6200. (goto-char (point-min)))
  6201. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6202. (message "Select location and press RET")
  6203. (use-local-map org-goto-map)
  6204. (recursive-edit)
  6205. ))
  6206. (kill-buffer "*org-goto*")
  6207. (cons org-goto-selected-point org-goto-exit-command)))
  6208. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6209. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6210. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6211. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  6212. (defun org-goto-local-search-headings (string bound noerror)
  6213. "Search and make sure that any matches are in headlines."
  6214. (catch 'return
  6215. (while (if isearch-forward
  6216. (search-forward string bound noerror)
  6217. (search-backward string bound noerror))
  6218. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  6219. (and (member :headline context)
  6220. (not (member :tags context))))
  6221. (throw 'return (point))))))
  6222. (defun org-goto-local-auto-isearch ()
  6223. "Start isearch."
  6224. (interactive)
  6225. (goto-char (point-min))
  6226. (let ((keys (this-command-keys)))
  6227. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6228. (isearch-mode t)
  6229. (isearch-process-search-char (string-to-char keys)))))
  6230. (defun org-goto-ret (&optional arg)
  6231. "Finish `org-goto' by going to the new location."
  6232. (interactive "P")
  6233. (setq org-goto-selected-point (point)
  6234. org-goto-exit-command 'return)
  6235. (throw 'exit nil))
  6236. (defun org-goto-left ()
  6237. "Finish `org-goto' by going to the new location."
  6238. (interactive)
  6239. (if (org-at-heading-p)
  6240. (progn
  6241. (beginning-of-line 1)
  6242. (setq org-goto-selected-point (point)
  6243. org-goto-exit-command 'left)
  6244. (throw 'exit nil))
  6245. (error "Not on a heading")))
  6246. (defun org-goto-right ()
  6247. "Finish `org-goto' by going to the new location."
  6248. (interactive)
  6249. (if (org-at-heading-p)
  6250. (progn
  6251. (setq org-goto-selected-point (point)
  6252. org-goto-exit-command 'right)
  6253. (throw 'exit nil))
  6254. (error "Not on a heading")))
  6255. (defun org-goto-quit ()
  6256. "Finish `org-goto' without cursor motion."
  6257. (interactive)
  6258. (setq org-goto-selected-point nil)
  6259. (setq org-goto-exit-command 'quit)
  6260. (throw 'exit nil))
  6261. ;;; Indirect buffer display of subtrees
  6262. (defvar org-indirect-dedicated-frame nil
  6263. "This is the frame being used for indirect tree display.")
  6264. (defvar org-last-indirect-buffer nil)
  6265. (defun org-tree-to-indirect-buffer (&optional arg)
  6266. "Create indirect buffer and narrow it to current subtree.
  6267. With numerical prefix ARG, go up to this level and then take that tree.
  6268. If ARG is negative, go up that many levels.
  6269. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6270. indirect buffer previously made with this command, to avoid proliferation of
  6271. indirect buffers. However, when you call the command with a \
  6272. \\[universal-argument] prefix, or
  6273. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6274. is kept so that you can work with several indirect buffers at the same time.
  6275. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6276. \\[universal-argument] prefix also
  6277. requests that a new frame be made for the new buffer, so that the dedicated
  6278. frame is not changed."
  6279. (interactive "P")
  6280. (let ((cbuf (current-buffer))
  6281. (cwin (selected-window))
  6282. (pos (point))
  6283. beg end level heading ibuf)
  6284. (save-excursion
  6285. (org-back-to-heading t)
  6286. (when (numberp arg)
  6287. (setq level (org-outline-level))
  6288. (if (< arg 0) (setq arg (+ level arg)))
  6289. (while (> (setq level (org-outline-level)) arg)
  6290. (outline-up-heading 1 t)))
  6291. (setq beg (point)
  6292. heading (org-get-heading))
  6293. (org-end-of-subtree t t)
  6294. (if (org-at-heading-p) (backward-char 1))
  6295. (setq end (point)))
  6296. (if (and (buffer-live-p org-last-indirect-buffer)
  6297. (not (eq org-indirect-buffer-display 'new-frame))
  6298. (not arg))
  6299. (kill-buffer org-last-indirect-buffer))
  6300. (setq ibuf (org-get-indirect-buffer cbuf)
  6301. org-last-indirect-buffer ibuf)
  6302. (cond
  6303. ((or (eq org-indirect-buffer-display 'new-frame)
  6304. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6305. (select-frame (make-frame))
  6306. (delete-other-windows)
  6307. (org-pop-to-buffer-same-window ibuf)
  6308. (org-set-frame-title heading))
  6309. ((eq org-indirect-buffer-display 'dedicated-frame)
  6310. (raise-frame
  6311. (select-frame (or (and org-indirect-dedicated-frame
  6312. (frame-live-p org-indirect-dedicated-frame)
  6313. org-indirect-dedicated-frame)
  6314. (setq org-indirect-dedicated-frame (make-frame)))))
  6315. (delete-other-windows)
  6316. (org-pop-to-buffer-same-window ibuf)
  6317. (org-set-frame-title (concat "Indirect: " heading)))
  6318. ((eq org-indirect-buffer-display 'current-window)
  6319. (org-pop-to-buffer-same-window ibuf))
  6320. ((eq org-indirect-buffer-display 'other-window)
  6321. (pop-to-buffer ibuf))
  6322. (t (error "Invalid value")))
  6323. (if (featurep 'xemacs)
  6324. (save-excursion (org-mode) (turn-on-font-lock)))
  6325. (narrow-to-region beg end)
  6326. (show-all)
  6327. (goto-char pos)
  6328. (run-hook-with-args 'org-cycle-hook 'all)
  6329. (and (window-live-p cwin) (select-window cwin))))
  6330. (defun org-get-indirect-buffer (&optional buffer)
  6331. (setq buffer (or buffer (current-buffer)))
  6332. (let ((n 1) (base (buffer-name buffer)) bname)
  6333. (while (buffer-live-p
  6334. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  6335. (setq n (1+ n)))
  6336. (condition-case nil
  6337. (make-indirect-buffer buffer bname 'clone)
  6338. (error (make-indirect-buffer buffer bname)))))
  6339. (defun org-set-frame-title (title)
  6340. "Set the title of the current frame to the string TITLE."
  6341. ;; FIXME: how to name a single frame in XEmacs???
  6342. (unless (featurep 'xemacs)
  6343. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6344. ;;;; Structure editing
  6345. ;;; Inserting headlines
  6346. (defun org-previous-line-empty-p ()
  6347. (save-excursion
  6348. (and (not (bobp))
  6349. (or (beginning-of-line 0) t)
  6350. (save-match-data
  6351. (looking-at "[ \t]*$")))))
  6352. (defun org-insert-heading (&optional force-heading invisible-ok)
  6353. "Insert a new heading or item with same depth at point.
  6354. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  6355. If point is at the beginning of a headline, insert a sibling before the
  6356. current headline. If point is not at the beginning, split the line,
  6357. create the new headline with the text in the current line after point
  6358. \(but see also the variable `org-M-RET-may-split-line').
  6359. When INVISIBLE-OK is set, stop at invisible headlines when going back.
  6360. This is important for non-interactive uses of the command."
  6361. (interactive "P")
  6362. (if (or (= (buffer-size) 0)
  6363. (and (not (save-excursion
  6364. (and (ignore-errors (org-back-to-heading invisible-ok))
  6365. (org-at-heading-p))))
  6366. (or force-heading (not (org-in-item-p)))))
  6367. (progn
  6368. (insert "\n* ")
  6369. (run-hooks 'org-insert-heading-hook))
  6370. (when (or force-heading (not (org-insert-item)))
  6371. (let* ((empty-line-p nil)
  6372. (level nil)
  6373. (on-heading (org-at-heading-p))
  6374. (head (save-excursion
  6375. (condition-case nil
  6376. (progn
  6377. (org-back-to-heading invisible-ok)
  6378. (when (and (not on-heading)
  6379. (featurep 'org-inlinetask)
  6380. (integerp org-inlinetask-min-level)
  6381. (>= (length (match-string 0))
  6382. org-inlinetask-min-level))
  6383. ;; Find a heading level before the inline task
  6384. (while (and (setq level (org-up-heading-safe))
  6385. (>= level org-inlinetask-min-level)))
  6386. (if (org-at-heading-p)
  6387. (org-back-to-heading invisible-ok)
  6388. (error "This should not happen")))
  6389. (setq empty-line-p (org-previous-line-empty-p))
  6390. (match-string 0))
  6391. (error "*"))))
  6392. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6393. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6394. pos hide-previous previous-pos)
  6395. (cond
  6396. ((and (org-at-heading-p) (bolp)
  6397. (or (bobp)
  6398. (save-excursion (backward-char 1) (not (outline-invisible-p)))))
  6399. ;; insert before the current line
  6400. (open-line (if blank 2 1)))
  6401. ((and (bolp)
  6402. (not org-insert-heading-respect-content)
  6403. (or (bobp)
  6404. (save-excursion
  6405. (backward-char 1) (not (outline-invisible-p)))))
  6406. ;; insert right here
  6407. nil)
  6408. (t
  6409. ;; somewhere in the line
  6410. (save-excursion
  6411. (setq previous-pos (point-at-bol))
  6412. (end-of-line)
  6413. (setq hide-previous (outline-invisible-p)))
  6414. (and org-insert-heading-respect-content (org-show-subtree))
  6415. (let ((split
  6416. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  6417. (save-excursion
  6418. (let ((p (point)))
  6419. (goto-char (point-at-bol))
  6420. (and (looking-at org-complex-heading-regexp)
  6421. (match-beginning 4)
  6422. (> p (match-beginning 4)))))))
  6423. tags pos)
  6424. (cond
  6425. (org-insert-heading-respect-content
  6426. (org-end-of-subtree nil t)
  6427. (when (featurep 'org-inlinetask)
  6428. (while (and (not (eobp))
  6429. (looking-at "\\(\\*+\\)[ \t]+")
  6430. (>= (length (match-string 1))
  6431. org-inlinetask-min-level))
  6432. (org-end-of-subtree nil t)))
  6433. (or (bolp) (newline))
  6434. (or (org-previous-line-empty-p)
  6435. (and blank (newline)))
  6436. (open-line 1))
  6437. ((org-at-heading-p)
  6438. (when hide-previous
  6439. (show-children)
  6440. (org-show-entry))
  6441. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
  6442. (setq tags (and (match-end 2) (match-string 2)))
  6443. (and (match-end 1)
  6444. (delete-region (match-beginning 1) (match-end 1)))
  6445. (setq pos (point-at-bol))
  6446. (or split (end-of-line 1))
  6447. (delete-horizontal-space)
  6448. (if (string-match "\\`\\*+\\'"
  6449. (buffer-substring (point-at-bol) (point)))
  6450. (insert " "))
  6451. (newline (if blank 2 1))
  6452. (when tags
  6453. (save-excursion
  6454. (goto-char pos)
  6455. (end-of-line 1)
  6456. (insert " " tags)
  6457. (org-set-tags nil 'align))))
  6458. (t
  6459. (or split (end-of-line 1))
  6460. (newline (if blank 2 1)))))))
  6461. (insert head) (just-one-space)
  6462. (setq pos (point))
  6463. (end-of-line 1)
  6464. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  6465. (when (and org-insert-heading-respect-content hide-previous)
  6466. (save-excursion
  6467. (goto-char previous-pos)
  6468. (hide-subtree)))
  6469. (run-hooks 'org-insert-heading-hook)))))
  6470. (defun org-get-heading (&optional no-tags no-todo)
  6471. "Return the heading of the current entry, without the stars.
  6472. When NO-TAGS is non-nil, don't include tags.
  6473. When NO-TODO is non-nil, don't include TODO keywords."
  6474. (save-excursion
  6475. (org-back-to-heading t)
  6476. (cond
  6477. ((and no-tags no-todo)
  6478. (looking-at org-complex-heading-regexp)
  6479. (match-string 4))
  6480. (no-tags
  6481. (looking-at (concat org-outline-regexp
  6482. "\\(.*?\\)"
  6483. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  6484. (match-string 1))
  6485. (no-todo
  6486. (looking-at org-todo-line-regexp)
  6487. (match-string 3))
  6488. (t (looking-at org-heading-regexp)
  6489. (match-string 2)))))
  6490. (defun org-heading-components ()
  6491. "Return the components of the current heading.
  6492. This is a list with the following elements:
  6493. - the level as an integer
  6494. - the reduced level, different if `org-odd-levels-only' is set.
  6495. - the TODO keyword, or nil
  6496. - the priority character, like ?A, or nil if no priority is given
  6497. - the headline text itself, or the tags string if no headline text
  6498. - the tags string, or nil."
  6499. (save-excursion
  6500. (org-back-to-heading t)
  6501. (if (let (case-fold-search) (looking-at org-complex-heading-regexp))
  6502. (list (length (match-string 1))
  6503. (org-reduced-level (length (match-string 1)))
  6504. (org-match-string-no-properties 2)
  6505. (and (match-end 3) (aref (match-string 3) 2))
  6506. (org-match-string-no-properties 4)
  6507. (org-match-string-no-properties 5)))))
  6508. (defun org-get-entry ()
  6509. "Get the entry text, after heading, entire subtree."
  6510. (save-excursion
  6511. (org-back-to-heading t)
  6512. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  6513. (defun org-insert-heading-after-current ()
  6514. "Insert a new heading with same level as current, after current subtree."
  6515. (interactive)
  6516. (org-back-to-heading)
  6517. (org-insert-heading)
  6518. (org-move-subtree-down)
  6519. (end-of-line 1))
  6520. (defun org-insert-heading-respect-content ()
  6521. (interactive)
  6522. (let ((org-insert-heading-respect-content t))
  6523. (org-insert-heading t)))
  6524. (defun org-insert-todo-heading-respect-content (&optional force-state)
  6525. (interactive "P")
  6526. (let ((org-insert-heading-respect-content t))
  6527. (org-insert-todo-heading force-state t)))
  6528. (defun org-insert-todo-heading (arg &optional force-heading)
  6529. "Insert a new heading with the same level and TODO state as current heading.
  6530. If the heading has no TODO state, or if the state is DONE, use the first
  6531. state (TODO by default). Also with prefix arg, force first state."
  6532. (interactive "P")
  6533. (when (or force-heading (not (org-insert-item 'checkbox)))
  6534. (org-insert-heading force-heading)
  6535. (save-excursion
  6536. (org-back-to-heading)
  6537. (outline-previous-heading)
  6538. (looking-at org-todo-line-regexp))
  6539. (let*
  6540. ((new-mark-x
  6541. (if (or arg
  6542. (not (match-beginning 2))
  6543. (member (match-string 2) org-done-keywords))
  6544. (car org-todo-keywords-1)
  6545. (match-string 2)))
  6546. (new-mark
  6547. (or
  6548. (run-hook-with-args-until-success
  6549. 'org-todo-get-default-hook new-mark-x nil)
  6550. new-mark-x)))
  6551. (beginning-of-line 1)
  6552. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  6553. (if org-treat-insert-todo-heading-as-state-change
  6554. (org-todo new-mark)
  6555. (insert new-mark " "))))
  6556. (when org-provide-todo-statistics
  6557. (org-update-parent-todo-statistics))))
  6558. (defun org-insert-subheading (arg)
  6559. "Insert a new subheading and demote it.
  6560. Works for outline headings and for plain lists alike."
  6561. (interactive "P")
  6562. (org-insert-heading arg)
  6563. (cond
  6564. ((org-at-heading-p) (org-do-demote))
  6565. ((org-at-item-p) (org-indent-item))))
  6566. (defun org-insert-todo-subheading (arg)
  6567. "Insert a new subheading with TODO keyword or checkbox and demote it.
  6568. Works for outline headings and for plain lists alike."
  6569. (interactive "P")
  6570. (org-insert-todo-heading arg)
  6571. (cond
  6572. ((org-at-heading-p) (org-do-demote))
  6573. ((org-at-item-p) (org-indent-item))))
  6574. ;;; Promotion and Demotion
  6575. (defvar org-after-demote-entry-hook nil
  6576. "Hook run after an entry has been demoted.
  6577. The cursor will be at the beginning of the entry.
  6578. When a subtree is being demoted, the hook will be called for each node.")
  6579. (defvar org-after-promote-entry-hook nil
  6580. "Hook run after an entry has been promoted.
  6581. The cursor will be at the beginning of the entry.
  6582. When a subtree is being promoted, the hook will be called for each node.")
  6583. (defun org-promote-subtree ()
  6584. "Promote the entire subtree.
  6585. See also `org-promote'."
  6586. (interactive)
  6587. (save-excursion
  6588. (org-with-limited-levels (org-map-tree 'org-promote)))
  6589. (org-fix-position-after-promote))
  6590. (defun org-demote-subtree ()
  6591. "Demote the entire subtree. See `org-demote'.
  6592. See also `org-promote'."
  6593. (interactive)
  6594. (save-excursion
  6595. (org-with-limited-levels (org-map-tree 'org-demote)))
  6596. (org-fix-position-after-promote))
  6597. (defun org-do-promote ()
  6598. "Promote the current heading higher up the tree.
  6599. If the region is active in `transient-mark-mode', promote all headings
  6600. in the region."
  6601. (interactive)
  6602. (save-excursion
  6603. (if (org-region-active-p)
  6604. (org-map-region 'org-promote (region-beginning) (region-end))
  6605. (org-promote)))
  6606. (org-fix-position-after-promote))
  6607. (defun org-do-demote ()
  6608. "Demote the current heading lower down the tree.
  6609. If the region is active in `transient-mark-mode', demote all headings
  6610. in the region."
  6611. (interactive)
  6612. (save-excursion
  6613. (if (org-region-active-p)
  6614. (org-map-region 'org-demote (region-beginning) (region-end))
  6615. (org-demote)))
  6616. (org-fix-position-after-promote))
  6617. (defun org-fix-position-after-promote ()
  6618. "Make sure that after pro/demotion cursor position is right."
  6619. (let ((pos (point)))
  6620. (when (save-excursion
  6621. (beginning-of-line 1)
  6622. (looking-at org-todo-line-regexp)
  6623. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  6624. (cond ((eobp) (insert " "))
  6625. ((eolp) (insert " "))
  6626. ((equal (char-after) ?\ ) (forward-char 1))))))
  6627. (defun org-current-level ()
  6628. "Return the level of the current entry, or nil if before the first headline.
  6629. The level is the number of stars at the beginning of the headline."
  6630. (save-excursion
  6631. (org-with-limited-levels
  6632. (if (ignore-errors (org-back-to-heading t))
  6633. (funcall outline-level)))))
  6634. (defun org-get-previous-line-level ()
  6635. "Return the outline depth of the last headline before the current line.
  6636. Returns 0 for the first headline in the buffer, and nil if before the
  6637. first headline."
  6638. (let ((current-level (org-current-level))
  6639. (prev-level (when (> (line-number-at-pos) 1)
  6640. (save-excursion
  6641. (beginning-of-line 0)
  6642. (org-current-level)))))
  6643. (cond ((null current-level) nil) ; Before first headline
  6644. ((null prev-level) 0) ; At first headline
  6645. (prev-level))))
  6646. (defun org-reduced-level (l)
  6647. "Compute the effective level of a heading.
  6648. This takes into account the setting of `org-odd-levels-only'."
  6649. (cond
  6650. ((zerop l) 0)
  6651. (org-odd-levels-only (1+ (floor (/ l 2))))
  6652. (t l)))
  6653. (defun org-level-increment ()
  6654. "Return the number of stars that will be added or removed at a
  6655. time to headlines when structure editing, based on the value of
  6656. `org-odd-levels-only'."
  6657. (if org-odd-levels-only 2 1))
  6658. (defun org-get-valid-level (level &optional change)
  6659. "Rectify a level change under the influence of `org-odd-levels-only'
  6660. LEVEL is a current level, CHANGE is by how much the level should be
  6661. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  6662. even level numbers will become the next higher odd number."
  6663. (if org-odd-levels-only
  6664. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  6665. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  6666. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  6667. (max 1 (+ level (or change 0)))))
  6668. (if (boundp 'define-obsolete-function-alias)
  6669. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  6670. (define-obsolete-function-alias 'org-get-legal-level
  6671. 'org-get-valid-level)
  6672. (define-obsolete-function-alias 'org-get-legal-level
  6673. 'org-get-valid-level "23.1")))
  6674. (defun org-promote ()
  6675. "Promote the current heading higher up the tree.
  6676. If the region is active in `transient-mark-mode', promote all headings
  6677. in the region."
  6678. (org-back-to-heading t)
  6679. (let* ((level (save-match-data (funcall outline-level)))
  6680. (after-change-functions (remove 'flyspell-after-change-function
  6681. after-change-functions))
  6682. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  6683. (diff (abs (- level (length up-head) -1))))
  6684. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  6685. (replace-match up-head nil t)
  6686. ;; Fixup tag positioning
  6687. (and org-auto-align-tags (org-set-tags nil t))
  6688. (if org-adapt-indentation (org-fixup-indentation (- diff)))
  6689. (run-hooks 'org-after-promote-entry-hook)))
  6690. (defun org-demote ()
  6691. "Demote the current heading lower down the tree.
  6692. If the region is active in `transient-mark-mode', demote all headings
  6693. in the region."
  6694. (org-back-to-heading t)
  6695. (let* ((level (save-match-data (funcall outline-level)))
  6696. (after-change-functions (remove 'flyspell-after-change-function
  6697. after-change-functions))
  6698. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  6699. (diff (abs (- level (length down-head) -1))))
  6700. (replace-match down-head nil t)
  6701. ;; Fixup tag positioning
  6702. (and org-auto-align-tags (org-set-tags nil t))
  6703. (if org-adapt-indentation (org-fixup-indentation diff))
  6704. (run-hooks 'org-after-demote-entry-hook)))
  6705. (defun org-cycle-level ()
  6706. "Cycle the level of an empty headline through possible states.
  6707. This goes first to child, then to parent, level, then up the hierarchy.
  6708. After top level, it switches back to sibling level."
  6709. (interactive)
  6710. (let ((org-adapt-indentation nil))
  6711. (when (org-point-at-end-of-empty-headline)
  6712. (setq this-command 'org-cycle-level) ; Only needed for caching
  6713. (let ((cur-level (org-current-level))
  6714. (prev-level (org-get-previous-line-level)))
  6715. (cond
  6716. ;; If first headline in file, promote to top-level.
  6717. ((= prev-level 0)
  6718. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6719. do (org-do-promote)))
  6720. ;; If same level as prev, demote one.
  6721. ((= prev-level cur-level)
  6722. (org-do-demote))
  6723. ;; If parent is top-level, promote to top level if not already.
  6724. ((= prev-level 1)
  6725. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6726. do (org-do-promote)))
  6727. ;; If top-level, return to prev-level.
  6728. ((= cur-level 1)
  6729. (loop repeat (/ (- prev-level 1) (org-level-increment))
  6730. do (org-do-demote)))
  6731. ;; If less than prev-level, promote one.
  6732. ((< cur-level prev-level)
  6733. (org-do-promote))
  6734. ;; If deeper than prev-level, promote until higher than
  6735. ;; prev-level.
  6736. ((> cur-level prev-level)
  6737. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  6738. do (org-do-promote))))
  6739. t))))
  6740. (defun org-map-tree (fun)
  6741. "Call FUN for every heading underneath the current one."
  6742. (org-back-to-heading)
  6743. (let ((level (funcall outline-level)))
  6744. (save-excursion
  6745. (funcall fun)
  6746. (while (and (progn
  6747. (outline-next-heading)
  6748. (> (funcall outline-level) level))
  6749. (not (eobp)))
  6750. (funcall fun)))))
  6751. (defun org-map-region (fun beg end)
  6752. "Call FUN for every heading between BEG and END."
  6753. (let ((org-ignore-region t))
  6754. (save-excursion
  6755. (setq end (copy-marker end))
  6756. (goto-char beg)
  6757. (if (and (re-search-forward org-outline-regexp-bol nil t)
  6758. (< (point) end))
  6759. (funcall fun))
  6760. (while (and (progn
  6761. (outline-next-heading)
  6762. (< (point) end))
  6763. (not (eobp)))
  6764. (funcall fun)))))
  6765. (defvar org-property-end-re) ; silence byte-compiler
  6766. (defun org-fixup-indentation (diff)
  6767. "Change the indentation in the current entry by DIFF.
  6768. However, if any line in the current entry has no indentation, or if it
  6769. would end up with no indentation after the change, nothing at all is done."
  6770. (save-excursion
  6771. (let ((end (save-excursion (outline-next-heading)
  6772. (point-marker)))
  6773. (prohibit (if (> diff 0)
  6774. "^\\S-"
  6775. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  6776. col)
  6777. (unless (save-excursion (end-of-line 1)
  6778. (re-search-forward prohibit end t))
  6779. (while (and (< (point) end)
  6780. (re-search-forward "^[ \t]+" end t))
  6781. (goto-char (match-end 0))
  6782. (setq col (current-column))
  6783. (if (< diff 0) (replace-match ""))
  6784. (org-indent-to-column (+ diff col))))
  6785. (move-marker end nil))))
  6786. (defun org-convert-to-odd-levels ()
  6787. "Convert an org-mode file with all levels allowed to one with odd levels.
  6788. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  6789. level 5 etc."
  6790. (interactive)
  6791. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  6792. (let ((outline-level 'org-outline-level)
  6793. (org-odd-levels-only nil) n)
  6794. (save-excursion
  6795. (goto-char (point-min))
  6796. (while (re-search-forward "^\\*\\*+ " nil t)
  6797. (setq n (- (length (match-string 0)) 2))
  6798. (while (>= (setq n (1- n)) 0)
  6799. (org-demote))
  6800. (end-of-line 1))))))
  6801. (defun org-convert-to-oddeven-levels ()
  6802. "Convert an org-mode file with only odd levels to one with odd/even levels.
  6803. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  6804. file contains a section with an even level, conversion would
  6805. destroy the structure of the file. An error is signaled in this
  6806. case."
  6807. (interactive)
  6808. (goto-char (point-min))
  6809. ;; First check if there are no even levels
  6810. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  6811. (org-show-context t)
  6812. (error "Not all levels are odd in this file. Conversion not possible"))
  6813. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  6814. (let ((outline-regexp org-outline-regexp)
  6815. (outline-level 'org-outline-level)
  6816. (org-odd-levels-only nil) n)
  6817. (save-excursion
  6818. (goto-char (point-min))
  6819. (while (re-search-forward "^\\*\\*+ " nil t)
  6820. (setq n (/ (1- (length (match-string 0))) 2))
  6821. (while (>= (setq n (1- n)) 0)
  6822. (org-promote))
  6823. (end-of-line 1))))))
  6824. (defun org-tr-level (n)
  6825. "Make N odd if required."
  6826. (if org-odd-levels-only (1+ (/ n 2)) n))
  6827. ;;; Vertical tree motion, cutting and pasting of subtrees
  6828. (defun org-move-subtree-up (&optional arg)
  6829. "Move the current subtree up past ARG headlines of the same level."
  6830. (interactive "p")
  6831. (org-move-subtree-down (- (prefix-numeric-value arg))))
  6832. (defun org-move-subtree-down (&optional arg)
  6833. "Move the current subtree down past ARG headlines of the same level."
  6834. (interactive "p")
  6835. (setq arg (prefix-numeric-value arg))
  6836. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  6837. 'org-get-last-sibling))
  6838. (ins-point (make-marker))
  6839. (cnt (abs arg))
  6840. (col (current-column))
  6841. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  6842. ;; Select the tree
  6843. (org-back-to-heading)
  6844. (setq beg0 (point))
  6845. (save-excursion
  6846. (setq ne-beg (org-back-over-empty-lines))
  6847. (setq beg (point)))
  6848. (save-match-data
  6849. (save-excursion (outline-end-of-heading)
  6850. (setq folded (outline-invisible-p)))
  6851. (outline-end-of-subtree))
  6852. (outline-next-heading)
  6853. (setq ne-end (org-back-over-empty-lines))
  6854. (setq end (point))
  6855. (goto-char beg0)
  6856. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  6857. ;; include less whitespace
  6858. (save-excursion
  6859. (goto-char beg)
  6860. (forward-line (- ne-beg ne-end))
  6861. (setq beg (point))))
  6862. ;; Find insertion point, with error handling
  6863. (while (> cnt 0)
  6864. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  6865. (progn (goto-char beg0)
  6866. (error "Cannot move past superior level or buffer limit")))
  6867. (setq cnt (1- cnt)))
  6868. (if (> arg 0)
  6869. ;; Moving forward - still need to move over subtree
  6870. (progn (org-end-of-subtree t t)
  6871. (save-excursion
  6872. (org-back-over-empty-lines)
  6873. (or (bolp) (newline)))))
  6874. (setq ne-ins (org-back-over-empty-lines))
  6875. (move-marker ins-point (point))
  6876. (setq txt (buffer-substring beg end))
  6877. (org-save-markers-in-region beg end)
  6878. (delete-region beg end)
  6879. (org-remove-empty-overlays-at beg)
  6880. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  6881. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  6882. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  6883. (let ((bbb (point)))
  6884. (insert-before-markers txt)
  6885. (org-reinstall-markers-in-region bbb)
  6886. (move-marker ins-point bbb))
  6887. (or (bolp) (insert "\n"))
  6888. (setq ins-end (point))
  6889. (goto-char ins-point)
  6890. (org-skip-whitespace)
  6891. (when (and (< arg 0)
  6892. (org-first-sibling-p)
  6893. (> ne-ins ne-beg))
  6894. ;; Move whitespace back to beginning
  6895. (save-excursion
  6896. (goto-char ins-end)
  6897. (let ((kill-whole-line t))
  6898. (kill-line (- ne-ins ne-beg)) (point)))
  6899. (insert (make-string (- ne-ins ne-beg) ?\n)))
  6900. (move-marker ins-point nil)
  6901. (if folded
  6902. (hide-subtree)
  6903. (org-show-entry)
  6904. (show-children)
  6905. (org-cycle-hide-drawers 'children))
  6906. (org-clean-visibility-after-subtree-move)
  6907. ;; move back to the initial column we were at
  6908. (move-to-column col)))
  6909. (defvar org-subtree-clip ""
  6910. "Clipboard for cut and paste of subtrees.
  6911. This is actually only a copy of the kill, because we use the normal kill
  6912. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  6913. (defvar org-subtree-clip-folded nil
  6914. "Was the last copied subtree folded?
  6915. This is used to fold the tree back after pasting.")
  6916. (defun org-cut-subtree (&optional n)
  6917. "Cut the current subtree into the clipboard.
  6918. With prefix arg N, cut this many sequential subtrees.
  6919. This is a short-hand for marking the subtree and then cutting it."
  6920. (interactive "p")
  6921. (org-copy-subtree n 'cut))
  6922. (defun org-copy-subtree (&optional n cut force-store-markers)
  6923. "Cut the current subtree into the clipboard.
  6924. With prefix arg N, cut this many sequential subtrees.
  6925. This is a short-hand for marking the subtree and then copying it.
  6926. If CUT is non-nil, actually cut the subtree.
  6927. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  6928. of some markers in the region, even if CUT is non-nil. This is
  6929. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  6930. (interactive "p")
  6931. (let (beg end folded (beg0 (point)))
  6932. (if (org-called-interactively-p 'any)
  6933. (org-back-to-heading nil) ; take what looks like a subtree
  6934. (org-back-to-heading t)) ; take what is really there
  6935. (org-back-over-empty-lines)
  6936. (setq beg (point))
  6937. (skip-chars-forward " \t\r\n")
  6938. (save-match-data
  6939. (save-excursion (outline-end-of-heading)
  6940. (setq folded (outline-invisible-p)))
  6941. (condition-case nil
  6942. (org-forward-same-level (1- n) t)
  6943. (error nil))
  6944. (org-end-of-subtree t t))
  6945. (org-back-over-empty-lines)
  6946. (setq end (point))
  6947. (goto-char beg0)
  6948. (when (> end beg)
  6949. (setq org-subtree-clip-folded folded)
  6950. (when (or cut force-store-markers)
  6951. (org-save-markers-in-region beg end))
  6952. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  6953. (setq org-subtree-clip (current-kill 0))
  6954. (message "%s: Subtree(s) with %d characters"
  6955. (if cut "Cut" "Copied")
  6956. (length org-subtree-clip)))))
  6957. (defun org-paste-subtree (&optional level tree for-yank)
  6958. "Paste the clipboard as a subtree, with modification of headline level.
  6959. The entire subtree is promoted or demoted in order to match a new headline
  6960. level.
  6961. If the cursor is at the beginning of a headline, the same level as
  6962. that headline is used to paste the tree
  6963. If not, the new level is derived from the *visible* headings
  6964. before and after the insertion point, and taken to be the inferior headline
  6965. level of the two. So if the previous visible heading is level 3 and the
  6966. next is level 4 (or vice versa), level 4 will be used for insertion.
  6967. This makes sure that the subtree remains an independent subtree and does
  6968. not swallow low level entries.
  6969. You can also force a different level, either by using a numeric prefix
  6970. argument, or by inserting the heading marker by hand. For example, if the
  6971. cursor is after \"*****\", then the tree will be shifted to level 5.
  6972. If optional TREE is given, use this text instead of the kill ring.
  6973. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  6974. move back over whitespace before inserting, and move point to the end of
  6975. the inserted text when done."
  6976. (interactive "P")
  6977. (setq tree (or tree (and kill-ring (current-kill 0))))
  6978. (unless (org-kill-is-subtree-p tree)
  6979. (error "%s"
  6980. (substitute-command-keys
  6981. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  6982. (org-with-limited-levels
  6983. (let* ((visp (not (outline-invisible-p)))
  6984. (txt tree)
  6985. (^re_ "\\(\\*+\\)[ \t]*")
  6986. (old-level (if (string-match org-outline-regexp-bol txt)
  6987. (- (match-end 0) (match-beginning 0) 1)
  6988. -1))
  6989. (force-level (cond (level (prefix-numeric-value level))
  6990. ((and (looking-at "[ \t]*$")
  6991. (string-match
  6992. "^\\*+$" (buffer-substring
  6993. (point-at-bol) (point))))
  6994. (- (match-end 1) (match-beginning 1)))
  6995. ((and (bolp)
  6996. (looking-at org-outline-regexp))
  6997. (- (match-end 0) (point) 1))
  6998. (t nil)))
  6999. (previous-level (save-excursion
  7000. (condition-case nil
  7001. (progn
  7002. (outline-previous-visible-heading 1)
  7003. (if (looking-at ^re_)
  7004. (- (match-end 0) (match-beginning 0) 1)
  7005. 1))
  7006. (error 1))))
  7007. (next-level (save-excursion
  7008. (condition-case nil
  7009. (progn
  7010. (or (looking-at org-outline-regexp)
  7011. (outline-next-visible-heading 1))
  7012. (if (looking-at ^re_)
  7013. (- (match-end 0) (match-beginning 0) 1)
  7014. 1))
  7015. (error 1))))
  7016. (new-level (or force-level (max previous-level next-level)))
  7017. (shift (if (or (= old-level -1)
  7018. (= new-level -1)
  7019. (= old-level new-level))
  7020. 0
  7021. (- new-level old-level)))
  7022. (delta (if (> shift 0) -1 1))
  7023. (func (if (> shift 0) 'org-demote 'org-promote))
  7024. (org-odd-levels-only nil)
  7025. beg end newend)
  7026. ;; Remove the forced level indicator
  7027. (if force-level
  7028. (delete-region (point-at-bol) (point)))
  7029. ;; Paste
  7030. (beginning-of-line (if (bolp) 1 2))
  7031. (unless for-yank (org-back-over-empty-lines))
  7032. (setq beg (point))
  7033. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  7034. (insert-before-markers txt)
  7035. (unless (string-match "\n\\'" txt) (insert "\n"))
  7036. (setq newend (point))
  7037. (org-reinstall-markers-in-region beg)
  7038. (setq end (point))
  7039. (goto-char beg)
  7040. (skip-chars-forward " \t\n\r")
  7041. (setq beg (point))
  7042. (if (and (outline-invisible-p) visp)
  7043. (save-excursion (outline-show-heading)))
  7044. ;; Shift if necessary
  7045. (unless (= shift 0)
  7046. (save-restriction
  7047. (narrow-to-region beg end)
  7048. (while (not (= shift 0))
  7049. (org-map-region func (point-min) (point-max))
  7050. (setq shift (+ delta shift)))
  7051. (goto-char (point-min))
  7052. (setq newend (point-max))))
  7053. (when (or (org-called-interactively-p 'interactive) for-yank)
  7054. (message "Clipboard pasted as level %d subtree" new-level))
  7055. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  7056. kill-ring
  7057. (eq org-subtree-clip (current-kill 0))
  7058. org-subtree-clip-folded)
  7059. ;; The tree was folded before it was killed/copied
  7060. (hide-subtree))
  7061. (and for-yank (goto-char newend)))))
  7062. (defun org-kill-is-subtree-p (&optional txt)
  7063. "Check if the current kill is an outline subtree, or a set of trees.
  7064. Returns nil if kill does not start with a headline, or if the first
  7065. headline level is not the largest headline level in the tree.
  7066. So this will actually accept several entries of equal levels as well,
  7067. which is OK for `org-paste-subtree'.
  7068. If optional TXT is given, check this string instead of the current kill."
  7069. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  7070. (re (org-get-limited-outline-regexp))
  7071. (^re (concat "^" re))
  7072. (start-level (and kill
  7073. (string-match
  7074. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7075. kill)
  7076. (- (match-end 2) (match-beginning 2) 1)))
  7077. (start (1+ (or (match-beginning 2) -1))))
  7078. (if (not start-level)
  7079. (progn
  7080. nil) ;; does not even start with a heading
  7081. (catch 'exit
  7082. (while (setq start (string-match ^re kill (1+ start)))
  7083. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7084. (throw 'exit nil)))
  7085. t))))
  7086. (defvar org-markers-to-move nil
  7087. "Markers that should be moved with a cut-and-paste operation.
  7088. Those markers are stored together with their positions relative to
  7089. the start of the region.")
  7090. (defun org-save-markers-in-region (beg end)
  7091. "Check markers in region.
  7092. If these markers are between BEG and END, record their position relative
  7093. to BEG, so that after moving the block of text, we can put the markers back
  7094. into place.
  7095. This function gets called just before an entry or tree gets cut from the
  7096. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7097. called immediately, to move the markers with the entries."
  7098. (setq org-markers-to-move nil)
  7099. (when (featurep 'org-clock)
  7100. (org-clock-save-markers-for-cut-and-paste beg end))
  7101. (when (featurep 'org-agenda)
  7102. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7103. (defun org-check-and-save-marker (marker beg end)
  7104. "Check if MARKER is between BEG and END.
  7105. If yes, remember the marker and the distance to BEG."
  7106. (when (and (marker-buffer marker)
  7107. (equal (marker-buffer marker) (current-buffer)))
  7108. (if (and (>= marker beg) (< marker end))
  7109. (push (cons marker (- marker beg)) org-markers-to-move))))
  7110. (defun org-reinstall-markers-in-region (beg)
  7111. "Move all remembered markers to their position relative to BEG."
  7112. (mapc (lambda (x)
  7113. (move-marker (car x) (+ beg (cdr x))))
  7114. org-markers-to-move)
  7115. (setq org-markers-to-move nil))
  7116. (defun org-narrow-to-subtree ()
  7117. "Narrow buffer to the current subtree."
  7118. (interactive)
  7119. (save-excursion
  7120. (save-match-data
  7121. (org-with-limited-levels
  7122. (narrow-to-region
  7123. (progn (org-back-to-heading t) (point))
  7124. (progn (org-end-of-subtree t t)
  7125. (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7126. (point)))))))
  7127. (defun org-narrow-to-block ()
  7128. "Narrow buffer to the current block."
  7129. (interactive)
  7130. (let* ((case-fold-search t)
  7131. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7132. "^[ \t]*#\\+end_.*")))
  7133. (if blockp
  7134. (narrow-to-region (car blockp) (cdr blockp))
  7135. (error "Not in a block"))))
  7136. (eval-when-compile
  7137. (defvar org-property-drawer-re))
  7138. (defvar org-property-start-re) ;; defined below
  7139. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7140. "Clone the task (subtree) at point N times.
  7141. The clones will be inserted as siblings.
  7142. In interactive use, the user will be prompted for the number of
  7143. clones to be produced, and for a time SHIFT, which may be a
  7144. repeater as used in time stamps, for example `+3d'.
  7145. When a valid repeater is given and the entry contains any time
  7146. stamps, the clones will become a sequence in time, with time
  7147. stamps in the subtree shifted for each clone produced. If SHIFT
  7148. is nil or the empty string, time stamps will be left alone. The
  7149. ID property of the original subtree is removed.
  7150. If the original subtree did contain time stamps with a repeater,
  7151. the following will happen:
  7152. - the repeater will be removed in each clone
  7153. - an additional clone will be produced, with the current, unshifted
  7154. date(s) in the entry.
  7155. - the original entry will be placed *after* all the clones, with
  7156. repeater intact.
  7157. - the start days in the repeater in the original entry will be shifted
  7158. to past the last clone.
  7159. In this way you can spell out a number of instances of a repeating task,
  7160. and still retain the repeater to cover future instances of the task."
  7161. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  7162. (let (beg end template task idprop
  7163. shift-n shift-what doshift nmin nmax (n-no-remove -1)
  7164. (drawer-re org-drawer-regexp))
  7165. (if (not (and (integerp n) (> n 0)))
  7166. (error "Invalid number of replications %s" n))
  7167. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7168. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
  7169. shift)))
  7170. (error "Invalid shift specification %s" shift))
  7171. (when doshift
  7172. (setq shift-n (string-to-number (match-string 1 shift))
  7173. shift-what (cdr (assoc (match-string 2 shift)
  7174. '(("d" . day) ("w" . week)
  7175. ("m" . month) ("y" . year))))))
  7176. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7177. (setq nmin 1 nmax n)
  7178. (org-back-to-heading t)
  7179. (setq beg (point))
  7180. (setq idprop (org-entry-get nil "ID"))
  7181. (org-end-of-subtree t t)
  7182. (or (bolp) (insert "\n"))
  7183. (setq end (point))
  7184. (setq template (buffer-substring beg end))
  7185. (when (and doshift
  7186. (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
  7187. (delete-region beg end)
  7188. (setq end beg)
  7189. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7190. (goto-char end)
  7191. (loop for n from nmin to nmax do
  7192. ;; prepare clone
  7193. (with-temp-buffer
  7194. (insert template)
  7195. (org-mode)
  7196. (goto-char (point-min))
  7197. (org-show-subtree)
  7198. (and idprop (if org-clone-delete-id
  7199. (org-entry-delete nil "ID")
  7200. (org-id-get-create t)))
  7201. (unless (= n 0)
  7202. (while (re-search-forward "^[ \t]*CLOCK:.*$" nil t)
  7203. (kill-whole-line))
  7204. (goto-char (point-min))
  7205. (while (re-search-forward drawer-re nil t)
  7206. (mapc (lambda (d)
  7207. (org-remove-empty-drawer-at d (point))) org-drawers)))
  7208. (goto-char (point-min))
  7209. (when doshift
  7210. (while (re-search-forward org-ts-regexp-both nil t)
  7211. (org-timestamp-change (* n shift-n) shift-what))
  7212. (unless (= n n-no-remove)
  7213. (goto-char (point-min))
  7214. (while (re-search-forward org-ts-regexp nil t)
  7215. (save-excursion
  7216. (goto-char (match-beginning 0))
  7217. (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
  7218. (delete-region (match-beginning 1) (match-end 1)))))))
  7219. (setq task (buffer-string)))
  7220. (insert task))
  7221. (goto-char beg)))
  7222. ;;; Outline Sorting
  7223. (defun org-sort (with-case)
  7224. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7225. Optional argument WITH-CASE means sort case-sensitively."
  7226. (interactive "P")
  7227. (cond
  7228. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7229. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7230. (t
  7231. (org-call-with-arg 'org-sort-entries with-case))))
  7232. (defun org-sort-remove-invisible (s)
  7233. (remove-text-properties 0 (length s) org-rm-props s)
  7234. (while (string-match org-bracket-link-regexp s)
  7235. (setq s (replace-match (if (match-end 2)
  7236. (match-string 3 s)
  7237. (match-string 1 s)) t t s)))
  7238. s)
  7239. (defvar org-priority-regexp) ; defined later in the file
  7240. (defvar org-after-sorting-entries-or-items-hook nil
  7241. "Hook that is run after a bunch of entries or items have been sorted.
  7242. When children are sorted, the cursor is in the parent line when this
  7243. hook gets called. When a region or a plain list is sorted, the cursor
  7244. will be in the first entry of the sorted region/list.")
  7245. (defun org-sort-entries
  7246. (&optional with-case sorting-type getkey-func compare-func property)
  7247. "Sort entries on a certain level of an outline tree.
  7248. If there is an active region, the entries in the region are sorted.
  7249. Else, if the cursor is before the first entry, sort the top-level items.
  7250. Else, the children of the entry at point are sorted.
  7251. Sorting can be alphabetically, numerically, by date/time as given by
  7252. a time stamp, by a property or by priority.
  7253. The command prompts for the sorting type unless it has been given to the
  7254. function through the SORTING-TYPE argument, which needs to be a character,
  7255. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?r ?R ?f ?F). Here is the
  7256. precise meaning of each character:
  7257. n Numerically, by converting the beginning of the entry/item to a number.
  7258. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7259. t By date/time, either the first active time stamp in the entry, or, if
  7260. none exist, by the first inactive one.
  7261. s By the scheduled date/time.
  7262. d By deadline date/time.
  7263. c By creation time, which is assumed to be the first inactive time stamp
  7264. at the beginning of a line.
  7265. p By priority according to the cookie.
  7266. r By the value of a property.
  7267. Capital letters will reverse the sort order.
  7268. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7269. called with point at the beginning of the record. It must return either
  7270. a string or a number that should serve as the sorting key for that record.
  7271. Comparing entries ignores case by default. However, with an optional argument
  7272. WITH-CASE, the sorting considers case as well."
  7273. (interactive "P")
  7274. (let ((case-func (if with-case 'identity 'downcase))
  7275. start beg end stars re re2
  7276. txt what tmp)
  7277. ;; Find beginning and end of region to sort
  7278. (cond
  7279. ((org-region-active-p)
  7280. ;; we will sort the region
  7281. (setq end (region-end)
  7282. what "region")
  7283. (goto-char (region-beginning))
  7284. (if (not (org-at-heading-p)) (outline-next-heading))
  7285. (setq start (point)))
  7286. ((or (org-at-heading-p)
  7287. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  7288. ;; we will sort the children of the current headline
  7289. (org-back-to-heading)
  7290. (setq start (point)
  7291. end (progn (org-end-of-subtree t t)
  7292. (or (bolp) (insert "\n"))
  7293. (org-back-over-empty-lines)
  7294. (point))
  7295. what "children")
  7296. (goto-char start)
  7297. (show-subtree)
  7298. (outline-next-heading))
  7299. (t
  7300. ;; we will sort the top-level entries in this file
  7301. (goto-char (point-min))
  7302. (or (org-at-heading-p) (outline-next-heading))
  7303. (setq start (point))
  7304. (goto-char (point-max))
  7305. (beginning-of-line 1)
  7306. (when (looking-at ".*?\\S-")
  7307. ;; File ends in a non-white line
  7308. (end-of-line 1)
  7309. (insert "\n"))
  7310. (setq end (point-max))
  7311. (setq what "top-level")
  7312. (goto-char start)
  7313. (show-all)))
  7314. (setq beg (point))
  7315. (if (>= beg end) (error "Nothing to sort"))
  7316. (looking-at "\\(\\*+\\)")
  7317. (setq stars (match-string 1)
  7318. re (concat "^" (regexp-quote stars) " +")
  7319. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  7320. txt (buffer-substring beg end))
  7321. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  7322. (if (and (not (equal stars "*")) (string-match re2 txt))
  7323. (error "Region to sort contains a level above the first entry"))
  7324. (unless sorting-type
  7325. (message
  7326. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  7327. [t]ime [s]cheduled [d]eadline [c]reated
  7328. A/N/T/S/D/C/P/O/F means reversed:"
  7329. what)
  7330. (setq sorting-type (read-char-exclusive))
  7331. (and (= (downcase sorting-type) ?f)
  7332. (setq getkey-func
  7333. (org-icompleting-read "Sort using function: "
  7334. obarray 'fboundp t nil nil))
  7335. (setq getkey-func (intern getkey-func)))
  7336. (and (= (downcase sorting-type) ?r)
  7337. (setq property
  7338. (org-icompleting-read "Property: "
  7339. (mapcar 'list (org-buffer-property-keys t))
  7340. nil t))))
  7341. (message "Sorting entries...")
  7342. (save-restriction
  7343. (narrow-to-region start end)
  7344. (let ((dcst (downcase sorting-type))
  7345. (case-fold-search nil)
  7346. (now (current-time)))
  7347. (sort-subr
  7348. (/= dcst sorting-type)
  7349. ;; This function moves to the beginning character of the "record" to
  7350. ;; be sorted.
  7351. (lambda nil
  7352. (if (re-search-forward re nil t)
  7353. (goto-char (match-beginning 0))
  7354. (goto-char (point-max))))
  7355. ;; This function moves to the last character of the "record" being
  7356. ;; sorted.
  7357. (lambda nil
  7358. (save-match-data
  7359. (condition-case nil
  7360. (outline-forward-same-level 1)
  7361. (error
  7362. (goto-char (point-max))))))
  7363. ;; This function returns the value that gets sorted against.
  7364. (lambda nil
  7365. (cond
  7366. ((= dcst ?n)
  7367. (if (looking-at org-complex-heading-regexp)
  7368. (string-to-number (match-string 4))
  7369. nil))
  7370. ((= dcst ?a)
  7371. (if (looking-at org-complex-heading-regexp)
  7372. (funcall case-func (match-string 4))
  7373. nil))
  7374. ((= dcst ?t)
  7375. (let ((end (save-excursion (outline-next-heading) (point))))
  7376. (if (or (re-search-forward org-ts-regexp end t)
  7377. (re-search-forward org-ts-regexp-both end t))
  7378. (org-time-string-to-seconds (match-string 0))
  7379. (org-float-time now))))
  7380. ((= dcst ?c)
  7381. (let ((end (save-excursion (outline-next-heading) (point))))
  7382. (if (re-search-forward
  7383. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  7384. end t)
  7385. (org-time-string-to-seconds (match-string 0))
  7386. (org-float-time now))))
  7387. ((= dcst ?s)
  7388. (let ((end (save-excursion (outline-next-heading) (point))))
  7389. (if (re-search-forward org-scheduled-time-regexp end t)
  7390. (org-time-string-to-seconds (match-string 1))
  7391. (org-float-time now))))
  7392. ((= dcst ?d)
  7393. (let ((end (save-excursion (outline-next-heading) (point))))
  7394. (if (re-search-forward org-deadline-time-regexp end t)
  7395. (org-time-string-to-seconds (match-string 1))
  7396. (org-float-time now))))
  7397. ((= dcst ?p)
  7398. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  7399. (string-to-char (match-string 2))
  7400. org-default-priority))
  7401. ((= dcst ?r)
  7402. (or (org-entry-get nil property) ""))
  7403. ((= dcst ?o)
  7404. (if (looking-at org-complex-heading-regexp)
  7405. (- 9999 (length (member (match-string 2)
  7406. org-todo-keywords-1)))))
  7407. ((= dcst ?f)
  7408. (if getkey-func
  7409. (progn
  7410. (setq tmp (funcall getkey-func))
  7411. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  7412. tmp)
  7413. (error "Invalid key function `%s'" getkey-func)))
  7414. (t (error "Invalid sorting type `%c'" sorting-type))))
  7415. nil
  7416. (cond
  7417. ((= dcst ?a) 'string<)
  7418. ((= dcst ?f) compare-func)
  7419. ((member dcst '(?p ?t ?s ?d ?c)) '<)
  7420. (t nil)))))
  7421. (run-hooks 'org-after-sorting-entries-or-items-hook)
  7422. (message "Sorting entries...done")))
  7423. (defun org-do-sort (table what &optional with-case sorting-type)
  7424. "Sort TABLE of WHAT according to SORTING-TYPE.
  7425. The user will be prompted for the SORTING-TYPE if the call to this
  7426. function does not specify it. WHAT is only for the prompt, to indicate
  7427. what is being sorted. The sorting key will be extracted from
  7428. the car of the elements of the table.
  7429. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  7430. (unless sorting-type
  7431. (message
  7432. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  7433. what)
  7434. (setq sorting-type (read-char-exclusive)))
  7435. (let ((dcst (downcase sorting-type))
  7436. extractfun comparefun)
  7437. ;; Define the appropriate functions
  7438. (cond
  7439. ((= dcst ?n)
  7440. (setq extractfun 'string-to-number
  7441. comparefun (if (= dcst sorting-type) '< '>)))
  7442. ((= dcst ?a)
  7443. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  7444. (lambda(x) (downcase (org-sort-remove-invisible x))))
  7445. comparefun (if (= dcst sorting-type)
  7446. 'string<
  7447. (lambda (a b) (and (not (string< a b))
  7448. (not (string= a b)))))))
  7449. ((= dcst ?t)
  7450. (setq extractfun
  7451. (lambda (x)
  7452. (if (or (string-match org-ts-regexp x)
  7453. (string-match org-ts-regexp-both x))
  7454. (org-float-time
  7455. (org-time-string-to-time (match-string 0 x)))
  7456. 0))
  7457. comparefun (if (= dcst sorting-type) '< '>)))
  7458. (t (error "Invalid sorting type `%c'" sorting-type)))
  7459. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  7460. table)
  7461. (lambda (a b) (funcall comparefun (car a) (car b))))))
  7462. ;;; The orgstruct minor mode
  7463. ;; Define a minor mode which can be used in other modes in order to
  7464. ;; integrate the org-mode structure editing commands.
  7465. ;; This is really a hack, because the org-mode structure commands use
  7466. ;; keys which normally belong to the major mode. Here is how it
  7467. ;; works: The minor mode defines all the keys necessary to operate the
  7468. ;; structure commands, but wraps the commands into a function which
  7469. ;; tests if the cursor is currently at a headline or a plain list
  7470. ;; item. If that is the case, the structure command is used,
  7471. ;; temporarily setting many Org-mode variables like regular
  7472. ;; expressions for filling etc. However, when any of those keys is
  7473. ;; used at a different location, function uses `key-binding' to look
  7474. ;; up if the key has an associated command in another currently active
  7475. ;; keymap (minor modes, major mode, global), and executes that
  7476. ;; command. There might be problems if any of the keys is otherwise
  7477. ;; used as a prefix key.
  7478. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  7479. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  7480. ;; addresses this by checking explicitly for both bindings.
  7481. (defvar orgstruct-mode-map (make-sparse-keymap)
  7482. "Keymap for the minor `orgstruct-mode'.")
  7483. (defvar org-local-vars nil
  7484. "List of local variables, for use by `orgstruct-mode'.")
  7485. ;;;###autoload
  7486. (define-minor-mode orgstruct-mode
  7487. "Toggle the minor mode `orgstruct-mode'.
  7488. This mode is for using Org-mode structure commands in other
  7489. modes. The following keys behave as if Org-mode were active, if
  7490. the cursor is on a headline, or on a plain list item (both as
  7491. defined by Org-mode).
  7492. M-up Move entry/item up
  7493. M-down Move entry/item down
  7494. M-left Promote
  7495. M-right Demote
  7496. M-S-up Move entry/item up
  7497. M-S-down Move entry/item down
  7498. M-S-left Promote subtree
  7499. M-S-right Demote subtree
  7500. M-q Fill paragraph and items like in Org-mode
  7501. C-c ^ Sort entries
  7502. C-c - Cycle list bullet
  7503. TAB Cycle item visibility
  7504. M-RET Insert new heading/item
  7505. S-M-RET Insert new TODO heading / Checkbox item
  7506. C-c C-c Set tags / toggle checkbox"
  7507. nil " OrgStruct" nil
  7508. (org-load-modules-maybe)
  7509. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  7510. ;;;###autoload
  7511. (defun turn-on-orgstruct ()
  7512. "Unconditionally turn on `orgstruct-mode'."
  7513. (orgstruct-mode 1))
  7514. (defvar org-fb-vars nil)
  7515. (make-variable-buffer-local 'org-fb-vars)
  7516. (defun orgstruct++-mode (&optional arg)
  7517. "Toggle `orgstruct-mode', the enhanced version of it.
  7518. In addition to setting orgstruct-mode, this also exports all indentation
  7519. and autofilling variables from org-mode into the buffer. It will also
  7520. recognize item context in multiline items.
  7521. Note that turning off orgstruct-mode will *not* remove the
  7522. indentation/paragraph settings. This can only be done by refreshing the
  7523. major mode, for example with \\[normal-mode]."
  7524. (interactive "P")
  7525. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  7526. (if (< arg 1)
  7527. (progn (orgstruct-mode -1)
  7528. (mapc (lambda(v)
  7529. (org-set-local (car v)
  7530. (if (eq (car-safe (cadr v)) 'quote) (cadadr v) (cadr v))))
  7531. org-fb-vars))
  7532. (orgstruct-mode 1)
  7533. (setq org-fb-vars nil)
  7534. (let (var val)
  7535. (mapc
  7536. (lambda (x)
  7537. (when (string-match
  7538. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  7539. (symbol-name (car x)))
  7540. (setq var (car x) val (nth 1 x))
  7541. (push (list var `(quote ,(eval var))) org-fb-vars)
  7542. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  7543. org-local-vars)
  7544. (org-set-local 'orgstruct-is-++ t))))
  7545. (defvar orgstruct-is-++ nil
  7546. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  7547. (make-variable-buffer-local 'orgstruct-is-++)
  7548. ;;;###autoload
  7549. (defun turn-on-orgstruct++ ()
  7550. "Unconditionally turn on `orgstruct++-mode'."
  7551. (orgstruct++-mode 1))
  7552. (defun orgstruct-error ()
  7553. "Error when there is no default binding for a structure key."
  7554. (interactive)
  7555. (error "This key has no function outside structure elements"))
  7556. (defun orgstruct-setup ()
  7557. "Setup orgstruct keymaps."
  7558. (let ((nfunc 0)
  7559. (bindings
  7560. (list
  7561. '([(meta up)] org-metaup)
  7562. '([(meta down)] org-metadown)
  7563. '([(meta left)] org-metaleft)
  7564. '([(meta right)] org-metaright)
  7565. '([(meta shift up)] org-shiftmetaup)
  7566. '([(meta shift down)] org-shiftmetadown)
  7567. '([(meta shift left)] org-shiftmetaleft)
  7568. '([(meta shift right)] org-shiftmetaright)
  7569. '([?\e (up)] org-metaup)
  7570. '([?\e (down)] org-metadown)
  7571. '([?\e (left)] org-metaleft)
  7572. '([?\e (right)] org-metaright)
  7573. '([?\e (shift up)] org-shiftmetaup)
  7574. '([?\e (shift down)] org-shiftmetadown)
  7575. '([?\e (shift left)] org-shiftmetaleft)
  7576. '([?\e (shift right)] org-shiftmetaright)
  7577. '([(shift up)] org-shiftup)
  7578. '([(shift down)] org-shiftdown)
  7579. '([(shift left)] org-shiftleft)
  7580. '([(shift right)] org-shiftright)
  7581. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  7582. '("\M-q" fill-paragraph)
  7583. '("\C-c^" org-sort)
  7584. '("\C-c-" org-cycle-list-bullet)))
  7585. elt key fun cmd)
  7586. (while (setq elt (pop bindings))
  7587. (setq nfunc (1+ nfunc))
  7588. (setq key (org-key (car elt))
  7589. fun (nth 1 elt)
  7590. cmd (orgstruct-make-binding fun nfunc key))
  7591. (org-defkey orgstruct-mode-map key cmd))
  7592. ;; Special treatment needed for TAB and RET
  7593. (org-defkey orgstruct-mode-map [(tab)]
  7594. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  7595. (org-defkey orgstruct-mode-map "\C-i"
  7596. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  7597. (org-defkey orgstruct-mode-map "\M-\C-m"
  7598. (orgstruct-make-binding 'org-insert-heading 105
  7599. "\M-\C-m" [(meta return)]))
  7600. (org-defkey orgstruct-mode-map [(meta return)]
  7601. (orgstruct-make-binding 'org-insert-heading 106
  7602. [(meta return)] "\M-\C-m"))
  7603. (org-defkey orgstruct-mode-map [(shift meta return)]
  7604. (orgstruct-make-binding 'org-insert-todo-heading 107
  7605. [(meta return)] "\M-\C-m"))
  7606. (org-defkey orgstruct-mode-map "\e\C-m"
  7607. (orgstruct-make-binding 'org-insert-heading 108
  7608. "\e\C-m" [?\e (return)]))
  7609. (org-defkey orgstruct-mode-map [?\e (return)]
  7610. (orgstruct-make-binding 'org-insert-heading 109
  7611. [?\e (return)] "\e\C-m"))
  7612. (org-defkey orgstruct-mode-map [?\e (shift return)]
  7613. (orgstruct-make-binding 'org-insert-todo-heading 110
  7614. [?\e (return)] "\e\C-m"))
  7615. (unless org-local-vars
  7616. (setq org-local-vars (org-get-local-variables)))
  7617. t))
  7618. (defun orgstruct-make-binding (fun n &rest keys)
  7619. "Create a function for binding in the structure minor mode.
  7620. FUN is the command to call inside a table. N is used to create a unique
  7621. command name. KEYS are keys that should be checked in for a command
  7622. to execute outside of tables."
  7623. (eval
  7624. (list 'defun
  7625. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  7626. '(arg)
  7627. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  7628. "Outside of structure, run the binding of `"
  7629. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  7630. "'.")
  7631. '(interactive "p")
  7632. (list 'if
  7633. `(org-context-p 'headline 'item
  7634. (and orgstruct-is-++
  7635. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  7636. 'item-body))
  7637. (list 'org-run-like-in-org-mode (list 'quote fun))
  7638. (list 'let '(orgstruct-mode)
  7639. (list 'call-interactively
  7640. (append '(or)
  7641. (mapcar (lambda (k)
  7642. (list 'key-binding k))
  7643. keys)
  7644. '('orgstruct-error))))))))
  7645. (defun org-context-p (&rest contexts)
  7646. "Check if local context is any of CONTEXTS.
  7647. Possible values in the list of contexts are `table', `headline', and `item'."
  7648. (let ((pos (point)))
  7649. (goto-char (point-at-bol))
  7650. (prog1 (or (and (memq 'table contexts)
  7651. (looking-at "[ \t]*|"))
  7652. (and (memq 'headline contexts)
  7653. (looking-at org-outline-regexp))
  7654. (and (memq 'item contexts)
  7655. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  7656. (and (memq 'item-body contexts)
  7657. (org-in-item-p)))
  7658. (goto-char pos))))
  7659. (defun org-get-local-variables ()
  7660. "Return a list of all local variables in an org-mode buffer."
  7661. (let (varlist)
  7662. (with-current-buffer (get-buffer-create "*Org tmp*")
  7663. (erase-buffer)
  7664. (org-mode)
  7665. (setq varlist (buffer-local-variables)))
  7666. (kill-buffer "*Org tmp*")
  7667. (delq nil
  7668. (mapcar
  7669. (lambda (x)
  7670. (setq x
  7671. (if (symbolp x)
  7672. (list x)
  7673. (list (car x) (list 'quote (cdr x)))))
  7674. (if (string-match
  7675. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  7676. (symbol-name (car x)))
  7677. x nil))
  7678. varlist))))
  7679. (defun org-clone-local-variables (from-buffer &optional regexp)
  7680. "Clone local variables from FROM-BUFFER.
  7681. Optional argument REGEXP selects variables to clone."
  7682. (mapc
  7683. (lambda (pair)
  7684. (and (symbolp (car pair))
  7685. (or (null regexp)
  7686. (string-match regexp (symbol-name (car pair))))
  7687. (set (make-local-variable (car pair))
  7688. (cdr pair))))
  7689. (buffer-local-variables from-buffer)))
  7690. ;;;###autoload
  7691. (defun org-run-like-in-org-mode (cmd)
  7692. "Run a command, pretending that the current buffer is in Org-mode.
  7693. This will temporarily bind local variables that are typically bound in
  7694. Org-mode to the values they have in Org-mode, and then interactively
  7695. call CMD."
  7696. (org-load-modules-maybe)
  7697. (unless org-local-vars
  7698. (setq org-local-vars (org-get-local-variables)))
  7699. (eval (list 'let org-local-vars
  7700. (list 'call-interactively (list 'quote cmd)))))
  7701. ;;;; Archiving
  7702. (defun org-get-category (&optional pos force-refresh)
  7703. "Get the category applying to position POS."
  7704. (save-match-data
  7705. (if force-refresh (org-refresh-category-properties))
  7706. (let ((pos (or pos (point))))
  7707. (or (get-text-property pos 'org-category)
  7708. (progn (org-refresh-category-properties)
  7709. (get-text-property pos 'org-category))))))
  7710. (defun org-refresh-category-properties ()
  7711. "Refresh category text properties in the buffer."
  7712. (let ((inhibit-read-only t)
  7713. (def-cat (cond
  7714. ((null org-category)
  7715. (if buffer-file-name
  7716. (file-name-sans-extension
  7717. (file-name-nondirectory buffer-file-name))
  7718. "???"))
  7719. ((symbolp org-category) (symbol-name org-category))
  7720. (t org-category)))
  7721. beg end cat pos optionp)
  7722. (org-unmodified
  7723. (save-excursion
  7724. (save-restriction
  7725. (widen)
  7726. (goto-char (point-min))
  7727. (put-text-property (point) (point-max) 'org-category def-cat)
  7728. (while (re-search-forward
  7729. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  7730. (setq pos (match-end 0)
  7731. optionp (equal (char-after (match-beginning 0)) ?#)
  7732. cat (org-trim (match-string 2)))
  7733. (if optionp
  7734. (setq beg (point-at-bol) end (point-max))
  7735. (org-back-to-heading t)
  7736. (setq beg (point) end (org-end-of-subtree t t)))
  7737. (put-text-property beg end 'org-category cat)
  7738. (put-text-property beg end 'org-category-position beg)
  7739. (goto-char pos)))))))
  7740. ;;;; Link Stuff
  7741. ;;; Link abbreviations
  7742. (defun org-link-expand-abbrev (link)
  7743. "Apply replacements as defined in `org-link-abbrev-alist'."
  7744. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  7745. (let* ((key (match-string 1 link))
  7746. (as (or (assoc key org-link-abbrev-alist-local)
  7747. (assoc key org-link-abbrev-alist)))
  7748. (tag (and (match-end 2) (match-string 3 link)))
  7749. rpl)
  7750. (if (not as)
  7751. link
  7752. (setq rpl (cdr as))
  7753. (cond
  7754. ((symbolp rpl) (funcall rpl tag))
  7755. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  7756. ((string-match "%h" rpl)
  7757. (replace-match (url-hexify-string (or tag "")) t t rpl))
  7758. (t (concat rpl tag)))))
  7759. link))
  7760. ;;; Storing and inserting links
  7761. (defvar org-insert-link-history nil
  7762. "Minibuffer history for links inserted with `org-insert-link'.")
  7763. (defvar org-stored-links nil
  7764. "Contains the links stored with `org-store-link'.")
  7765. (defvar org-store-link-plist nil
  7766. "Plist with info about the most recently link created with `org-store-link'.")
  7767. (defvar org-link-protocols nil
  7768. "Link protocols added to Org-mode using `org-add-link-type'.")
  7769. (defvar org-store-link-functions nil
  7770. "List of functions that are called to create and store a link.
  7771. Each function will be called in turn until one returns a non-nil
  7772. value. Each function should check if it is responsible for creating
  7773. this link (for example by looking at the major mode).
  7774. If not, it must exit and return nil.
  7775. If yes, it should return a non-nil value after a calling
  7776. `org-store-link-props' with a list of properties and values.
  7777. Special properties are:
  7778. :type The link prefix, like \"http\". This must be given.
  7779. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  7780. This is obligatory as well.
  7781. :description Optional default description for the second pair
  7782. of brackets in an Org-mode link. The user can still change
  7783. this when inserting this link into an Org-mode buffer.
  7784. In addition to these, any additional properties can be specified
  7785. and then used in remember templates.")
  7786. (defun org-add-link-type (type &optional follow export)
  7787. "Add TYPE to the list of `org-link-types'.
  7788. Re-compute all regular expressions depending on `org-link-types'
  7789. FOLLOW and EXPORT are two functions.
  7790. FOLLOW should take the link path as the single argument and do whatever
  7791. is necessary to follow the link, for example find a file or display
  7792. a mail message.
  7793. EXPORT should format the link path for export to one of the export formats.
  7794. It should be a function accepting three arguments:
  7795. path the path of the link, the text after the prefix (like \"http:\")
  7796. desc the description of the link, if any, or a description added by
  7797. org-export-normalize-links if there is none
  7798. format the export format, a symbol like `html' or `latex' or `ascii'..
  7799. The function may use the FORMAT information to return different values
  7800. depending on the format. The return value will be put literally into
  7801. the exported file. If the return value is nil, this means Org should
  7802. do what it normally does with links which do not have EXPORT defined.
  7803. Org-mode has a built-in default for exporting links. If you are happy with
  7804. this default, there is no need to define an export function for the link
  7805. type. For a simple example of an export function, see `org-bbdb.el'."
  7806. (add-to-list 'org-link-types type t)
  7807. (org-make-link-regexps)
  7808. (if (assoc type org-link-protocols)
  7809. (setcdr (assoc type org-link-protocols) (list follow export))
  7810. (push (list type follow export) org-link-protocols)))
  7811. (defvar org-agenda-buffer-name)
  7812. ;;;###autoload
  7813. (defun org-store-link (arg)
  7814. "\\<org-mode-map>Store an org-link to the current location.
  7815. This link is added to `org-stored-links' and can later be inserted
  7816. into an org-buffer with \\[org-insert-link].
  7817. For some link types, a prefix arg is interpreted:
  7818. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  7819. For file links, arg negates `org-context-in-file-links'."
  7820. (interactive "P")
  7821. (org-load-modules-maybe)
  7822. (setq org-store-link-plist nil) ; reset
  7823. (org-with-limited-levels
  7824. (let (link cpltxt desc description search txt custom-id agenda-link)
  7825. (cond
  7826. ((run-hook-with-args-until-success 'org-store-link-functions)
  7827. (setq link (plist-get org-store-link-plist :link)
  7828. desc (or (plist-get org-store-link-plist :description) link)))
  7829. ((org-src-edit-buffer-p)
  7830. (let (label gc)
  7831. (while (or (not label)
  7832. (save-excursion
  7833. (save-restriction
  7834. (widen)
  7835. (goto-char (point-min))
  7836. (re-search-forward
  7837. (regexp-quote (format org-coderef-label-format label))
  7838. nil t))))
  7839. (when label (message "Label exists already") (sit-for 2))
  7840. (setq label (read-string "Code line label: " label)))
  7841. (end-of-line 1)
  7842. (setq link (format org-coderef-label-format label))
  7843. (setq gc (- 79 (length link)))
  7844. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  7845. (insert link)
  7846. (setq link (concat "(" label ")") desc nil)))
  7847. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  7848. ;; We are in the agenda, link to referenced location
  7849. (let ((m (or (get-text-property (point) 'org-hd-marker)
  7850. (get-text-property (point) 'org-marker))))
  7851. (when m
  7852. (org-with-point-at m
  7853. (setq agenda-link
  7854. (if (org-called-interactively-p 'any)
  7855. (call-interactively 'org-store-link)
  7856. (org-store-link nil)))))))
  7857. ((eq major-mode 'calendar-mode)
  7858. (let ((cd (calendar-cursor-to-date)))
  7859. (setq link
  7860. (format-time-string
  7861. (car org-time-stamp-formats)
  7862. (apply 'encode-time
  7863. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  7864. nil nil nil))))
  7865. (org-store-link-props :type "calendar" :date cd)))
  7866. ((eq major-mode 'w3-mode)
  7867. (setq cpltxt (if (and (buffer-name)
  7868. (not (string-match "Untitled" (buffer-name))))
  7869. (buffer-name)
  7870. (url-view-url t))
  7871. link (org-make-link (url-view-url t)))
  7872. (org-store-link-props :type "w3" :url (url-view-url t)))
  7873. ((eq major-mode 'w3m-mode)
  7874. (setq cpltxt (or w3m-current-title w3m-current-url)
  7875. link (org-make-link w3m-current-url))
  7876. (org-store-link-props :type "w3m" :url (url-view-url t)))
  7877. ((setq search (run-hook-with-args-until-success
  7878. 'org-create-file-search-functions))
  7879. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  7880. "::" search))
  7881. (setq cpltxt (or description link)))
  7882. ((eq major-mode 'image-mode)
  7883. (setq cpltxt (concat "file:"
  7884. (abbreviate-file-name buffer-file-name))
  7885. link (org-make-link cpltxt))
  7886. (org-store-link-props :type "image" :file buffer-file-name))
  7887. ((eq major-mode 'dired-mode)
  7888. ;; link to the file in the current line
  7889. (let ((file (dired-get-filename nil t)))
  7890. (setq file (if file
  7891. (abbreviate-file-name
  7892. (expand-file-name (dired-get-filename nil t)))
  7893. ;; otherwise, no file so use current directory.
  7894. default-directory))
  7895. (setq cpltxt (concat "file:" file)
  7896. link (org-make-link cpltxt))))
  7897. ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
  7898. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  7899. (cond
  7900. ((org-in-regexp "<<\\(.*?\\)>>")
  7901. (setq cpltxt
  7902. (concat "file:"
  7903. (abbreviate-file-name
  7904. (buffer-file-name (buffer-base-buffer)))
  7905. "::" (match-string 1))
  7906. link (org-make-link cpltxt)))
  7907. ((and (featurep 'org-id)
  7908. (or (eq org-link-to-org-use-id t)
  7909. (and (eq org-link-to-org-use-id 'create-if-interactive)
  7910. (org-called-interactively-p 'any))
  7911. (and (eq org-link-to-org-use-id
  7912. 'create-if-interactive-and-no-custom-id)
  7913. (org-called-interactively-p 'any)
  7914. (not custom-id))
  7915. (and org-link-to-org-use-id
  7916. (org-entry-get nil "ID"))))
  7917. ;; We can make a link using the ID.
  7918. (setq link (condition-case nil
  7919. (prog1 (org-id-store-link)
  7920. (setq desc (plist-get org-store-link-plist
  7921. :description)))
  7922. (error
  7923. ;; probably before first headline, link to file only
  7924. (concat "file:"
  7925. (abbreviate-file-name
  7926. (buffer-file-name (buffer-base-buffer))))))))
  7927. (t
  7928. ;; Just link to current headline
  7929. (setq cpltxt (concat "file:"
  7930. (abbreviate-file-name
  7931. (buffer-file-name (buffer-base-buffer)))))
  7932. ;; Add a context search string
  7933. (when (org-xor org-context-in-file-links arg)
  7934. (setq txt (cond
  7935. ((org-at-heading-p) nil)
  7936. ((org-region-active-p)
  7937. (buffer-substring (region-beginning) (region-end)))
  7938. (t nil)))
  7939. (when (or (null txt) (string-match "\\S-" txt))
  7940. (setq cpltxt
  7941. (concat cpltxt "::"
  7942. (condition-case nil
  7943. (org-make-org-heading-search-string txt)
  7944. (error "")))
  7945. desc (or (nth 4 (ignore-errors
  7946. (org-heading-components))) "NONE"))))
  7947. (if (string-match "::\\'" cpltxt)
  7948. (setq cpltxt (substring cpltxt 0 -2)))
  7949. (setq link (org-make-link cpltxt)))))
  7950. ((buffer-file-name (buffer-base-buffer))
  7951. ;; Just link to this file here.
  7952. (setq cpltxt (concat "file:"
  7953. (abbreviate-file-name
  7954. (buffer-file-name (buffer-base-buffer)))))
  7955. ;; Add a context string
  7956. (when (org-xor org-context-in-file-links arg)
  7957. (setq txt (if (org-region-active-p)
  7958. (buffer-substring (region-beginning) (region-end))
  7959. (buffer-substring (point-at-bol) (point-at-eol))))
  7960. ;; Only use search option if there is some text.
  7961. (when (string-match "\\S-" txt)
  7962. (setq cpltxt
  7963. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  7964. desc "NONE")))
  7965. (setq link (org-make-link cpltxt)))
  7966. ((org-called-interactively-p 'interactive)
  7967. (error "Cannot link to a buffer which is not visiting a file"))
  7968. (t (setq link nil)))
  7969. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  7970. (setq link (or link cpltxt)
  7971. desc (or desc cpltxt))
  7972. (if (equal desc "NONE") (setq desc nil))
  7973. (if (and (or (org-called-interactively-p 'any) executing-kbd-macro) link)
  7974. (progn
  7975. (setq org-stored-links
  7976. (cons (list link desc) org-stored-links))
  7977. (message "Stored: %s" (or desc link))
  7978. (when custom-id
  7979. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  7980. "::#" custom-id))
  7981. (setq org-stored-links
  7982. (cons (list link desc) org-stored-links))))
  7983. (or agenda-link (and link (org-make-link-string link desc)))))))
  7984. (defun org-store-link-props (&rest plist)
  7985. "Store link properties, extract names and addresses."
  7986. (let (x adr)
  7987. (when (setq x (plist-get plist :from))
  7988. (setq adr (mail-extract-address-components x))
  7989. (setq plist (plist-put plist :fromname (car adr)))
  7990. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  7991. (when (setq x (plist-get plist :to))
  7992. (setq adr (mail-extract-address-components x))
  7993. (setq plist (plist-put plist :toname (car adr)))
  7994. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  7995. (let ((from (plist-get plist :from))
  7996. (to (plist-get plist :to)))
  7997. (when (and from to org-from-is-user-regexp)
  7998. (setq plist
  7999. (plist-put plist :fromto
  8000. (if (string-match org-from-is-user-regexp from)
  8001. (concat "to %t")
  8002. (concat "from %f"))))))
  8003. (setq org-store-link-plist plist))
  8004. (defun org-add-link-props (&rest plist)
  8005. "Add these properties to the link property list."
  8006. (let (key value)
  8007. (while plist
  8008. (setq key (pop plist) value (pop plist))
  8009. (setq org-store-link-plist
  8010. (plist-put org-store-link-plist key value)))))
  8011. (defun org-email-link-description (&optional fmt)
  8012. "Return the description part of an email link.
  8013. This takes information from `org-store-link-plist' and formats it
  8014. according to FMT (default from `org-email-link-description-format')."
  8015. (setq fmt (or fmt org-email-link-description-format))
  8016. (let* ((p org-store-link-plist)
  8017. (to (plist-get p :toaddress))
  8018. (from (plist-get p :fromaddress))
  8019. (table
  8020. (list
  8021. (cons "%c" (plist-get p :fromto))
  8022. (cons "%F" (plist-get p :from))
  8023. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  8024. (cons "%T" (plist-get p :to))
  8025. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  8026. (cons "%s" (plist-get p :subject))
  8027. (cons "%d" (plist-get p :date))
  8028. (cons "%m" (plist-get p :message-id)))))
  8029. (when (string-match "%c" fmt)
  8030. ;; Check if the user wrote this message
  8031. (if (and org-from-is-user-regexp from to
  8032. (save-match-data (string-match org-from-is-user-regexp from)))
  8033. (setq fmt (replace-match "to %t" t t fmt))
  8034. (setq fmt (replace-match "from %f" t t fmt))))
  8035. (org-replace-escapes fmt table)))
  8036. (defun org-make-org-heading-search-string (&optional string heading)
  8037. "Make search string for STRING or current headline."
  8038. (interactive)
  8039. (let ((s (or string (org-get-heading)))
  8040. (lines org-context-in-file-links))
  8041. (unless (and string (not heading))
  8042. ;; We are using a headline, clean up garbage in there.
  8043. (if (string-match org-todo-regexp s)
  8044. (setq s (replace-match "" t t s)))
  8045. (if (string-match (org-re ":[[:alnum:]_@#%:]+:[ \t]*$") s)
  8046. (setq s (replace-match "" t t s)))
  8047. (setq s (org-trim s))
  8048. (if (string-match (concat "^\\(" org-quote-string "\\|"
  8049. org-comment-string "\\)") s)
  8050. (setq s (replace-match "" t t s)))
  8051. (while (string-match org-ts-regexp s)
  8052. (setq s (replace-match "" t t s))))
  8053. (or string (setq s (concat "*" s))) ; Add * for headlines
  8054. (when (and string (integerp lines) (> lines 0))
  8055. (let ((slines (org-split-string s "\n")))
  8056. (when (< lines (length slines))
  8057. (setq s (mapconcat
  8058. 'identity
  8059. (reverse (nthcdr (- (length slines) lines)
  8060. (reverse slines))) "\n")))))
  8061. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  8062. (defun org-make-link (&rest strings)
  8063. "Concatenate STRINGS."
  8064. (apply 'concat strings))
  8065. (defun org-make-link-string (link &optional description)
  8066. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8067. (unless (string-match "\\S-" link)
  8068. (error "Empty link"))
  8069. (when (and description
  8070. (stringp description)
  8071. (not (string-match "\\S-" description)))
  8072. (setq description nil))
  8073. (when (stringp description)
  8074. ;; Remove brackets from the description, they are fatal.
  8075. (while (string-match "\\[" description)
  8076. (setq description (replace-match "{" t t description)))
  8077. (while (string-match "\\]" description)
  8078. (setq description (replace-match "}" t t description))))
  8079. (when (equal link description)
  8080. ;; No description needed, it is identical
  8081. (setq description nil))
  8082. (when (and (not description)
  8083. (not (string-match (org-image-file-name-regexp) link))
  8084. (not (equal link (org-link-escape link))))
  8085. (setq description (org-extract-attributes link)))
  8086. (setq link
  8087. (cond ((string-match (org-image-file-name-regexp) link) link)
  8088. ((string-match org-link-types-re link)
  8089. (concat (match-string 1 link)
  8090. (org-link-escape (substring link (match-end 1)))))
  8091. (t (org-link-escape link))))
  8092. (concat "[[" link "]"
  8093. (if description (concat "[" description "]") "")
  8094. "]"))
  8095. (defconst org-link-escape-chars
  8096. '(?\ ?\[ ?\] ?\; ?\= ?\+)
  8097. "List of characters that should be escaped in link.
  8098. This is the list that is used for internal purposes.")
  8099. (defvar org-url-encoding-use-url-hexify nil)
  8100. (defconst org-link-escape-chars-browser
  8101. '(?\ )
  8102. "List of escapes for characters that are problematic in links.
  8103. This is the list that is used before handing over to the browser.")
  8104. (defun org-link-escape (text &optional table merge)
  8105. "Return percent escaped representation of TEXT.
  8106. TEXT is a string with the text to escape.
  8107. Optional argument TABLE is a list with characters that should be
  8108. escaped. When nil, `org-link-escape-chars' is used.
  8109. If optional argument MERGE is set, merge TABLE into
  8110. `org-link-escape-chars'."
  8111. (if (and org-url-encoding-use-url-hexify (not table))
  8112. (url-hexify-string text)
  8113. (cond
  8114. ((and table merge)
  8115. (mapc (lambda (defchr)
  8116. (unless (member defchr table)
  8117. (setq table (cons defchr table)))) org-link-escape-chars))
  8118. ((null table)
  8119. (setq table org-link-escape-chars)))
  8120. (mapconcat
  8121. (lambda (char)
  8122. (if (or (member char table)
  8123. (< char 32) (= char 37) (> char 126))
  8124. (mapconcat (lambda (sequence-element)
  8125. (format "%%%.2X" sequence-element))
  8126. (or (encode-coding-char char 'utf-8)
  8127. (error "Unable to percent escape character: %s"
  8128. (char-to-string char))) "")
  8129. (char-to-string char))) text "")))
  8130. (defun org-link-unescape (str)
  8131. "Unhex hexified Unicode strings as returned from the JavaScript function
  8132. encodeURIComponent. E.g. `%C3%B6' is the german Umlaut `ö'."
  8133. (unless (and (null str) (string= "" str))
  8134. (let ((pos 0) (case-fold-search t) unhexed)
  8135. (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
  8136. (setq unhexed (org-link-unescape-compound (match-string 0 str)))
  8137. (setq str (replace-match unhexed t t str))
  8138. (setq pos (+ pos (length unhexed))))))
  8139. str)
  8140. (defun org-link-unescape-compound (hex)
  8141. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German Umlaut `ö'.
  8142. Note: this function also decodes single byte encodings like
  8143. `%E1' (\"á\") if not followed by another `%[A-F0-9]{2}' group."
  8144. (save-match-data
  8145. (let* ((bytes (cdr (split-string hex "%")))
  8146. (ret "")
  8147. (eat 0)
  8148. (sum 0))
  8149. (while bytes
  8150. (let* ((val (string-to-number (pop bytes) 16))
  8151. (shift-xor
  8152. (if (= 0 eat)
  8153. (cond
  8154. ((>= val 252) (cons 6 252))
  8155. ((>= val 248) (cons 5 248))
  8156. ((>= val 240) (cons 4 240))
  8157. ((>= val 224) (cons 3 224))
  8158. ((>= val 192) (cons 2 192))
  8159. (t (cons 0 0)))
  8160. (cons 6 128))))
  8161. (if (>= val 192) (setq eat (car shift-xor)))
  8162. (setq val (logxor val (cdr shift-xor)))
  8163. (setq sum (+ (lsh sum (car shift-xor)) val))
  8164. (if (> eat 0) (setq eat (- eat 1)))
  8165. (cond
  8166. ((= 0 eat) ;multi byte
  8167. (setq ret (concat ret (org-char-to-string sum)))
  8168. (setq sum 0))
  8169. ((not bytes) ; single byte(s)
  8170. (setq ret (org-link-unescape-single-byte-sequence hex))))
  8171. )) ;; end (while bytes
  8172. ret )))
  8173. (defun org-link-unescape-single-byte-sequence (hex)
  8174. "Unhexify hex-encoded single byte character sequences."
  8175. (mapconcat (lambda (byte)
  8176. (char-to-string (string-to-number byte 16)))
  8177. (cdr (split-string hex "%")) ""))
  8178. (defun org-xor (a b)
  8179. "Exclusive or."
  8180. (if a (not b) b))
  8181. (defun org-fixup-message-id-for-http (s)
  8182. "Replace special characters in a message id, so it can be used in an http query."
  8183. (when (string-match "%" s)
  8184. (setq s (mapconcat (lambda (c)
  8185. (if (eq c ?%)
  8186. "%25"
  8187. (char-to-string c)))
  8188. s "")))
  8189. (while (string-match "<" s)
  8190. (setq s (replace-match "%3C" t t s)))
  8191. (while (string-match ">" s)
  8192. (setq s (replace-match "%3E" t t s)))
  8193. (while (string-match "@" s)
  8194. (setq s (replace-match "%40" t t s)))
  8195. s)
  8196. (defun org-link-prettify (link)
  8197. "Return a human-readable representation of LINK.
  8198. The car of LINK must be a raw link the cdr of LINK must be either
  8199. a link description or nil."
  8200. (let ((desc (or (cadr link) "<no description>")))
  8201. (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
  8202. "[[" (car link) "]]")))
  8203. ;;;###autoload
  8204. (defun org-insert-link-global ()
  8205. "Insert a link like Org-mode does.
  8206. This command can be called in any mode to insert a link in Org-mode syntax."
  8207. (interactive)
  8208. (org-load-modules-maybe)
  8209. (org-run-like-in-org-mode 'org-insert-link))
  8210. (defun org-insert-link (&optional complete-file link-location default-description)
  8211. "Insert a link. At the prompt, enter the link.
  8212. Completion can be used to insert any of the link protocol prefixes like
  8213. http or ftp in use.
  8214. The history can be used to select a link previously stored with
  8215. `org-store-link'. When the empty string is entered (i.e. if you just
  8216. press RET at the prompt), the link defaults to the most recently
  8217. stored link. As SPC triggers completion in the minibuffer, you need to
  8218. use M-SPC or C-q SPC to force the insertion of a space character.
  8219. You will also be prompted for a description, and if one is given, it will
  8220. be displayed in the buffer instead of the link.
  8221. If there is already a link at point, this command will allow you to edit link
  8222. and description parts.
  8223. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  8224. be selected using completion. The path to the file will be relative to the
  8225. current directory if the file is in the current directory or a subdirectory.
  8226. Otherwise, the link will be the absolute path as completed in the minibuffer
  8227. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  8228. option `org-link-file-path-type'.
  8229. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  8230. the current directory or below.
  8231. With three \\[universal-argument] prefixes, negate the meaning of
  8232. `org-keep-stored-link-after-insertion'.
  8233. If `org-make-link-description-function' is non-nil, this function will be
  8234. called with the link target, and the result will be the default
  8235. link description.
  8236. If the LINK-LOCATION parameter is non-nil, this value will be
  8237. used as the link location instead of reading one interactively.
  8238. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  8239. be used as the default description."
  8240. (interactive "P")
  8241. (let* ((wcf (current-window-configuration))
  8242. (region (if (org-region-active-p)
  8243. (buffer-substring (region-beginning) (region-end))))
  8244. (remove (and region (list (region-beginning) (region-end))))
  8245. (desc region)
  8246. tmphist ; byte-compile incorrectly complains about this
  8247. (link link-location)
  8248. entry file all-prefixes)
  8249. (cond
  8250. (link-location) ; specified by arg, just use it.
  8251. ((org-in-regexp org-bracket-link-regexp 1)
  8252. ;; We do have a link at point, and we are going to edit it.
  8253. (setq remove (list (match-beginning 0) (match-end 0)))
  8254. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  8255. (setq link (read-string "Link: "
  8256. (org-link-unescape
  8257. (org-match-string-no-properties 1)))))
  8258. ((or (org-in-regexp org-angle-link-re)
  8259. (org-in-regexp org-plain-link-re))
  8260. ;; Convert to bracket link
  8261. (setq remove (list (match-beginning 0) (match-end 0))
  8262. link (read-string "Link: "
  8263. (org-remove-angle-brackets (match-string 0)))))
  8264. ((member complete-file '((4) (16)))
  8265. ;; Completing read for file names.
  8266. (setq link (org-file-complete-link complete-file)))
  8267. (t
  8268. ;; Read link, with completion for stored links.
  8269. (with-output-to-temp-buffer "*Org Links*"
  8270. (princ "Insert a link.
  8271. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  8272. (when org-stored-links
  8273. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  8274. (princ (mapconcat 'org-link-prettify
  8275. (reverse org-stored-links) "\n"))))
  8276. (let ((cw (selected-window)))
  8277. (select-window (get-buffer-window "*Org Links*" 'visible))
  8278. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  8279. (unless (pos-visible-in-window-p (point-max))
  8280. (org-fit-window-to-buffer))
  8281. (and (window-live-p cw) (select-window cw)))
  8282. ;; Fake a link history, containing the stored links.
  8283. (setq tmphist (append (mapcar 'car org-stored-links)
  8284. org-insert-link-history))
  8285. (setq all-prefixes (append (mapcar 'car org-link-abbrev-alist-local)
  8286. (mapcar 'car org-link-abbrev-alist)
  8287. org-link-types))
  8288. (unwind-protect
  8289. (progn
  8290. (setq link
  8291. (let ((org-completion-use-ido nil)
  8292. (org-completion-use-iswitchb nil))
  8293. (org-completing-read
  8294. "Link: "
  8295. (append
  8296. (mapcar (lambda (x) (list (concat x ":")))
  8297. all-prefixes)
  8298. (mapcar 'car org-stored-links))
  8299. nil nil nil
  8300. 'tmphist
  8301. (car (car org-stored-links)))))
  8302. (if (not (string-match "\\S-" link))
  8303. (error "No link selected"))
  8304. (if (or (member link all-prefixes)
  8305. (and (equal ":" (substring link -1))
  8306. (member (substring link 0 -1) all-prefixes)
  8307. (setq link (substring link 0 -1))))
  8308. (setq link (org-link-try-special-completion link))))
  8309. (set-window-configuration wcf)
  8310. (kill-buffer "*Org Links*"))
  8311. (setq entry (assoc link org-stored-links))
  8312. (or entry (push link org-insert-link-history))
  8313. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  8314. (not org-keep-stored-link-after-insertion))
  8315. (setq org-stored-links (delq (assoc link org-stored-links)
  8316. org-stored-links)))
  8317. (setq desc (or desc (nth 1 entry)))))
  8318. (if (string-match org-plain-link-re link)
  8319. ;; URL-like link, normalize the use of angular brackets.
  8320. (setq link (org-make-link (org-remove-angle-brackets link))))
  8321. ;; Check if we are linking to the current file with a search option
  8322. ;; If yes, simplify the link by using only the search option.
  8323. (when (and buffer-file-name
  8324. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  8325. (let* ((path (match-string 1 link))
  8326. (case-fold-search nil)
  8327. (search (match-string 2 link)))
  8328. (save-match-data
  8329. (if (equal (file-truename buffer-file-name) (file-truename path))
  8330. ;; We are linking to this same file, with a search option
  8331. (setq link search)))))
  8332. ;; Check if we can/should use a relative path. If yes, simplify the link
  8333. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  8334. (let* ((type (match-string 1 link))
  8335. (path (match-string 2 link))
  8336. (origpath path)
  8337. (case-fold-search nil))
  8338. (cond
  8339. ((or (eq org-link-file-path-type 'absolute)
  8340. (equal complete-file '(16)))
  8341. (setq path (abbreviate-file-name (expand-file-name path))))
  8342. ((eq org-link-file-path-type 'noabbrev)
  8343. (setq path (expand-file-name path)))
  8344. ((eq org-link-file-path-type 'relative)
  8345. (setq path (file-relative-name path)))
  8346. (t
  8347. (save-match-data
  8348. (if (string-match (concat "^" (regexp-quote
  8349. (expand-file-name
  8350. (file-name-as-directory
  8351. default-directory))))
  8352. (expand-file-name path))
  8353. ;; We are linking a file with relative path name.
  8354. (setq path (substring (expand-file-name path)
  8355. (match-end 0)))
  8356. (setq path (abbreviate-file-name (expand-file-name path)))))))
  8357. (setq link (concat type path))
  8358. (if (equal desc origpath)
  8359. (setq desc path))))
  8360. (if org-make-link-description-function
  8361. (setq desc (funcall org-make-link-description-function link desc))
  8362. (if default-description (setq desc default-description)))
  8363. (setq desc (read-string "Description: " desc))
  8364. (unless (string-match "\\S-" desc) (setq desc nil))
  8365. (if remove (apply 'delete-region remove))
  8366. (insert (org-make-link-string link desc))))
  8367. (defun org-link-try-special-completion (type)
  8368. "If there is completion support for link type TYPE, offer it."
  8369. (let ((fun (intern (concat "org-" type "-complete-link"))))
  8370. (if (functionp fun)
  8371. (funcall fun)
  8372. (read-string "Link (no completion support): " (concat type ":")))))
  8373. (defun org-file-complete-link (&optional arg)
  8374. "Create a file link using completion."
  8375. (let (file link)
  8376. (setq file (read-file-name "File: "))
  8377. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  8378. (pwd1 (file-name-as-directory (abbreviate-file-name
  8379. (expand-file-name ".")))))
  8380. (cond
  8381. ((equal arg '(16))
  8382. (setq link (org-make-link
  8383. "file:"
  8384. (abbreviate-file-name (expand-file-name file)))))
  8385. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  8386. (setq link (org-make-link "file:" (match-string 1 file))))
  8387. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  8388. (expand-file-name file))
  8389. (setq link (org-make-link
  8390. "file:" (match-string 1 (expand-file-name file)))))
  8391. (t (setq link (org-make-link "file:" file)))))
  8392. link))
  8393. (defun org-completing-read (&rest args)
  8394. "Completing-read with SPACE being a normal character."
  8395. (let ((enable-recursive-minibuffers t)
  8396. (minibuffer-local-completion-map
  8397. (copy-keymap minibuffer-local-completion-map)))
  8398. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  8399. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  8400. (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
  8401. (apply 'org-icompleting-read args)))
  8402. (defun org-completing-read-no-i (&rest args)
  8403. (let (org-completion-use-ido org-completion-use-iswitchb)
  8404. (apply 'org-completing-read args)))
  8405. (defun org-iswitchb-completing-read (prompt choices &rest args)
  8406. "Use iswitch as a completing-read replacement to choose from choices.
  8407. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  8408. from."
  8409. (let* ((iswitchb-use-virtual-buffers nil)
  8410. (iswitchb-make-buflist-hook
  8411. (lambda ()
  8412. (setq iswitchb-temp-buflist choices))))
  8413. (iswitchb-read-buffer prompt)))
  8414. (defun org-icompleting-read (&rest args)
  8415. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  8416. (org-without-partial-completion
  8417. (if (and org-completion-use-ido
  8418. (fboundp 'ido-completing-read)
  8419. (boundp 'ido-mode) ido-mode
  8420. (listp (second args)))
  8421. (let ((ido-enter-matching-directory nil))
  8422. (apply 'ido-completing-read (concat (car args))
  8423. (if (consp (car (nth 1 args)))
  8424. (mapcar 'car (nth 1 args))
  8425. (nth 1 args))
  8426. (cddr args)))
  8427. (if (and org-completion-use-iswitchb
  8428. (boundp 'iswitchb-mode) iswitchb-mode
  8429. (listp (second args)))
  8430. (apply 'org-iswitchb-completing-read (concat (car args))
  8431. (if (consp (car (nth 1 args)))
  8432. (mapcar 'car (nth 1 args))
  8433. (nth 1 args))
  8434. (cddr args))
  8435. (apply 'completing-read args)))))
  8436. (defun org-extract-attributes (s)
  8437. "Extract the attributes cookie from a string and set as text property."
  8438. (let (a attr (start 0) key value)
  8439. (save-match-data
  8440. (when (string-match "{{\\([^}]+\\)}}$" s)
  8441. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  8442. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  8443. (setq key (match-string 1 a) value (match-string 2 a)
  8444. start (match-end 0)
  8445. attr (plist-put attr (intern key) value))))
  8446. (org-add-props s nil 'org-attr attr))
  8447. s))
  8448. (defun org-extract-attributes-from-string (tag)
  8449. (let (key value attr)
  8450. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  8451. (setq key (match-string 1 tag) value (match-string 2 tag)
  8452. tag (replace-match "" t t tag)
  8453. attr (plist-put attr (intern key) value)))
  8454. (cons tag attr)))
  8455. (defun org-attributes-to-string (plist)
  8456. "Format a property list into an HTML attribute list."
  8457. (let ((s "") key value)
  8458. (while plist
  8459. (setq key (pop plist) value (pop plist))
  8460. (and value
  8461. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  8462. s))
  8463. ;;; Opening/following a link
  8464. (defvar org-link-search-failed nil)
  8465. (defvar org-open-link-functions nil
  8466. "Hook for functions finding a plain text link.
  8467. These functions must take a single argument, the link content.
  8468. They will be called for links that look like [[link text][description]]
  8469. when LINK TEXT does not have a protocol like \"http:\" and does not look
  8470. like a filename (e.g. \"./blue.png\").
  8471. These functions will be called *before* Org attempts to resolve the
  8472. link by doing text searches in the current buffer - so if you want a
  8473. link \"[[target]]\" to still find \"<<target>>\", your function should
  8474. handle this as a special case.
  8475. When the function does handle the link, it must return a non-nil value.
  8476. If it decides that it is not responsible for this link, it must return
  8477. nil to indicate that that Org-mode can continue with other options
  8478. like exact and fuzzy text search.")
  8479. (defun org-next-link ()
  8480. "Move forward to the next link.
  8481. If the link is in hidden text, expose it."
  8482. (interactive)
  8483. (when (and org-link-search-failed (eq this-command last-command))
  8484. (goto-char (point-min))
  8485. (message "Link search wrapped back to beginning of buffer"))
  8486. (setq org-link-search-failed nil)
  8487. (let* ((pos (point))
  8488. (ct (org-context))
  8489. (a (assoc :link ct)))
  8490. (if a (goto-char (nth 2 a)))
  8491. (if (re-search-forward org-any-link-re nil t)
  8492. (progn
  8493. (goto-char (match-beginning 0))
  8494. (if (outline-invisible-p) (org-show-context)))
  8495. (goto-char pos)
  8496. (setq org-link-search-failed t)
  8497. (error "No further link found"))))
  8498. (defun org-previous-link ()
  8499. "Move backward to the previous link.
  8500. If the link is in hidden text, expose it."
  8501. (interactive)
  8502. (when (and org-link-search-failed (eq this-command last-command))
  8503. (goto-char (point-max))
  8504. (message "Link search wrapped back to end of buffer"))
  8505. (setq org-link-search-failed nil)
  8506. (let* ((pos (point))
  8507. (ct (org-context))
  8508. (a (assoc :link ct)))
  8509. (if a (goto-char (nth 1 a)))
  8510. (if (re-search-backward org-any-link-re nil t)
  8511. (progn
  8512. (goto-char (match-beginning 0))
  8513. (if (outline-invisible-p) (org-show-context)))
  8514. (goto-char pos)
  8515. (setq org-link-search-failed t)
  8516. (error "No further link found"))))
  8517. (defun org-translate-link (s)
  8518. "Translate a link string if a translation function has been defined."
  8519. (if (and org-link-translation-function
  8520. (fboundp org-link-translation-function)
  8521. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  8522. (progn
  8523. (setq s (funcall org-link-translation-function
  8524. (match-string 1 s) (match-string 2 s)))
  8525. (concat (car s) ":" (cdr s)))
  8526. s))
  8527. (defun org-translate-link-from-planner (type path)
  8528. "Translate a link from Emacs Planner syntax so that Org can follow it.
  8529. This is still an experimental function, your mileage may vary."
  8530. (cond
  8531. ((member type '("http" "https" "news" "ftp"))
  8532. ;; standard Internet links are the same.
  8533. nil)
  8534. ((and (equal type "irc") (string-match "^//" path))
  8535. ;; Planner has two / at the beginning of an irc link, we have 1.
  8536. ;; We should have zero, actually....
  8537. (setq path (substring path 1)))
  8538. ((and (equal type "lisp") (string-match "^/" path))
  8539. ;; Planner has a slash, we do not.
  8540. (setq type "elisp" path (substring path 1)))
  8541. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  8542. ;; A typical message link. Planner has the id after the final slash,
  8543. ;; we separate it with a hash mark
  8544. (setq path (concat (match-string 1 path) "#"
  8545. (org-remove-angle-brackets (match-string 2 path)))))
  8546. )
  8547. (cons type path))
  8548. (defun org-find-file-at-mouse (ev)
  8549. "Open file link or URL at mouse."
  8550. (interactive "e")
  8551. (mouse-set-point ev)
  8552. (org-open-at-point 'in-emacs))
  8553. (defun org-open-at-mouse (ev)
  8554. "Open file link or URL at mouse.
  8555. See the docstring of `org-open-file' for details."
  8556. (interactive "e")
  8557. (mouse-set-point ev)
  8558. (if (eq major-mode 'org-agenda-mode)
  8559. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  8560. (org-open-at-point))
  8561. (defvar org-window-config-before-follow-link nil
  8562. "The window configuration before following a link.
  8563. This is saved in case the need arises to restore it.")
  8564. (defvar org-open-link-marker (make-marker)
  8565. "Marker pointing to the location where `org-open-at-point; was called.")
  8566. ;;;###autoload
  8567. (defun org-open-at-point-global ()
  8568. "Follow a link like Org-mode does.
  8569. This command can be called in any mode to follow a link that has
  8570. Org-mode syntax."
  8571. (interactive)
  8572. (org-run-like-in-org-mode 'org-open-at-point))
  8573. ;;;###autoload
  8574. (defun org-open-link-from-string (s &optional arg reference-buffer)
  8575. "Open a link in the string S, as if it was in Org-mode."
  8576. (interactive "sLink: \nP")
  8577. (let ((reference-buffer (or reference-buffer (current-buffer))))
  8578. (with-temp-buffer
  8579. (let ((org-inhibit-startup (not reference-buffer)))
  8580. (org-mode)
  8581. (insert s)
  8582. (goto-char (point-min))
  8583. (when reference-buffer
  8584. (setq org-link-abbrev-alist-local
  8585. (with-current-buffer reference-buffer
  8586. org-link-abbrev-alist-local)))
  8587. (org-open-at-point arg reference-buffer)))))
  8588. (defvar org-open-at-point-functions nil
  8589. "Hook that is run when following a link at point.
  8590. Functions in this hook must return t if they identify and follow
  8591. a link at point. If they don't find anything interesting at point,
  8592. they must return nil.")
  8593. (defun org-open-at-point (&optional arg reference-buffer)
  8594. "Open link at or after point.
  8595. If there is no link at point, this function will search forward up to
  8596. the end of the current line.
  8597. Normally, files will be opened by an appropriate application. If the
  8598. optional prefix argument ARG is non-nil, Emacs will visit the file.
  8599. With a double prefix argument, try to open outside of Emacs, in the
  8600. application the system uses for this file type."
  8601. (interactive "P")
  8602. ;; if in a code block, then open the block's results
  8603. (unless (call-interactively #'org-babel-open-src-block-result)
  8604. (org-load-modules-maybe)
  8605. (move-marker org-open-link-marker (point))
  8606. (setq org-window-config-before-follow-link (current-window-configuration))
  8607. (org-remove-occur-highlights nil nil t)
  8608. (cond
  8609. ((and (org-at-heading-p)
  8610. (not (org-in-regexp
  8611. (concat org-plain-link-re "\\|"
  8612. org-bracket-link-regexp "\\|"
  8613. org-angle-link-re "\\|"
  8614. "[ \t]:[^ \t\n]+:[ \t]*$")))
  8615. (not (get-text-property (point) 'org-linked-text)))
  8616. (or (org-offer-links-in-entry arg)
  8617. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  8618. ((run-hook-with-args-until-success 'org-open-at-point-functions))
  8619. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  8620. ((and (or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  8621. (not (org-in-regexp org-bracket-link-regexp)))
  8622. (org-footnote-action))
  8623. (t
  8624. (let (type path link line search (pos (point)))
  8625. (catch 'match
  8626. (save-excursion
  8627. (skip-chars-forward "^]\n\r")
  8628. (when (org-in-regexp org-bracket-link-regexp 1)
  8629. (setq link (org-extract-attributes
  8630. (org-link-unescape (org-match-string-no-properties 1))))
  8631. (while (string-match " *\n *" link)
  8632. (setq link (replace-match " " t t link)))
  8633. (setq link (org-link-expand-abbrev link))
  8634. (cond
  8635. ((or (file-name-absolute-p link)
  8636. (string-match "^\\.\\.?/" link))
  8637. (setq type "file" path link))
  8638. ((string-match org-link-re-with-space3 link)
  8639. (setq type (match-string 1 link) path (match-string 2 link)))
  8640. (t (setq type "thisfile" path link)))
  8641. (throw 'match t)))
  8642. (when (get-text-property (point) 'org-linked-text)
  8643. (setq type "thisfile"
  8644. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  8645. (1+ (point)) (point))
  8646. path (buffer-substring
  8647. (or (previous-single-property-change pos 'org-linked-text)
  8648. (point-min))
  8649. (or (next-single-property-change pos 'org-linked-text)
  8650. (point-max))))
  8651. (throw 'match t))
  8652. (save-excursion
  8653. (when (or (org-in-regexp org-angle-link-re)
  8654. (org-in-regexp org-plain-link-re))
  8655. (setq type (match-string 1)
  8656. path (org-link-unescape (match-string 2)))
  8657. (throw 'match t)))
  8658. (save-excursion
  8659. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
  8660. (setq type "tags"
  8661. path (match-string 1))
  8662. (while (string-match ":" path)
  8663. (setq path (replace-match "+" t t path)))
  8664. (throw 'match t)))
  8665. (when (org-in-regexp "<\\([^><\n]+\\)>")
  8666. (setq type "tree-match"
  8667. path (match-string 1))
  8668. (throw 'match t)))
  8669. (unless path
  8670. (error "No link found"))
  8671. ;; switch back to reference buffer
  8672. ;; needed when if called in a temporary buffer through
  8673. ;; org-open-link-from-string
  8674. (with-current-buffer (or reference-buffer (current-buffer))
  8675. ;; Remove any trailing spaces in path
  8676. (if (string-match " +\\'" path)
  8677. (setq path (replace-match "" t t path)))
  8678. (if (and org-link-translation-function
  8679. (fboundp org-link-translation-function))
  8680. ;; Check if we need to translate the link
  8681. (let ((tmp (funcall org-link-translation-function type path)))
  8682. (setq type (car tmp) path (cdr tmp))))
  8683. (cond
  8684. ((assoc type org-link-protocols)
  8685. (funcall (nth 1 (assoc type org-link-protocols)) path))
  8686. ((equal type "mailto")
  8687. (let ((cmd (car org-link-mailto-program))
  8688. (args (cdr org-link-mailto-program)) args1
  8689. (address path) (subject "") a)
  8690. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  8691. (setq address (match-string 1 path)
  8692. subject (org-link-escape (match-string 2 path))))
  8693. (while args
  8694. (cond
  8695. ((not (stringp (car args))) (push (pop args) args1))
  8696. (t (setq a (pop args))
  8697. (if (string-match "%a" a)
  8698. (setq a (replace-match address t t a)))
  8699. (if (string-match "%s" a)
  8700. (setq a (replace-match subject t t a)))
  8701. (push a args1))))
  8702. (apply cmd (nreverse args1))))
  8703. ((member type '("http" "https" "ftp" "news"))
  8704. (browse-url (concat type ":" (if (org-string-match-p "[[:nonascii:] ]" path)
  8705. (org-link-escape
  8706. path org-link-escape-chars-browser)
  8707. path))))
  8708. ((string= type "doi")
  8709. (browse-url (concat "http://dx.doi.org/" (if (org-string-match-p "[[:nonascii:] ]" path)
  8710. (org-link-escape
  8711. path org-link-escape-chars-browser)
  8712. path))))
  8713. ((member type '("message"))
  8714. (browse-url (concat type ":" path)))
  8715. ((string= type "tags")
  8716. (org-tags-view arg path))
  8717. ((string= type "tree-match")
  8718. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  8719. ((string= type "file")
  8720. (if (string-match "::\\([0-9]+\\)\\'" path)
  8721. (setq line (string-to-number (match-string 1 path))
  8722. path (substring path 0 (match-beginning 0)))
  8723. (if (string-match "::\\(.+\\)\\'" path)
  8724. (setq search (match-string 1 path)
  8725. path (substring path 0 (match-beginning 0)))))
  8726. (if (string-match "[*?{]" (file-name-nondirectory path))
  8727. (dired path)
  8728. (org-open-file path arg line search)))
  8729. ((string= type "shell")
  8730. (let ((cmd path))
  8731. (if (or (and (not (string= org-confirm-shell-link-not-regexp ""))
  8732. (string-match org-confirm-shell-link-not-regexp cmd))
  8733. (not org-confirm-shell-link-function)
  8734. (funcall org-confirm-shell-link-function
  8735. (format "Execute \"%s\" in shell? "
  8736. (org-add-props cmd nil
  8737. 'face 'org-warning))))
  8738. (progn
  8739. (message "Executing %s" cmd)
  8740. (shell-command cmd))
  8741. (error "Abort"))))
  8742. ((string= type "elisp")
  8743. (let ((cmd path))
  8744. (if (or (and (not (string= org-confirm-elisp-link-not-regexp ""))
  8745. (string-match org-confirm-elisp-link-not-regexp cmd))
  8746. (not org-confirm-elisp-link-function)
  8747. (funcall org-confirm-elisp-link-function
  8748. (format "Execute \"%s\" as elisp? "
  8749. (org-add-props cmd nil
  8750. 'face 'org-warning))))
  8751. (message "%s => %s" cmd
  8752. (if (equal (string-to-char cmd) ?\()
  8753. (eval (read cmd))
  8754. (call-interactively (read cmd))))
  8755. (error "Abort"))))
  8756. ((and (string= type "thisfile")
  8757. (run-hook-with-args-until-success
  8758. 'org-open-link-functions path)))
  8759. ((string= type "thisfile")
  8760. (if arg
  8761. (switch-to-buffer-other-window
  8762. (org-get-buffer-for-internal-link (current-buffer)))
  8763. (org-mark-ring-push))
  8764. (let ((cmd `(org-link-search
  8765. ,path
  8766. ,(cond ((equal arg '(4)) ''occur)
  8767. ((equal arg '(16)) ''org-occur)
  8768. (t nil))
  8769. ,pos)))
  8770. (condition-case nil (let ((org-link-search-inhibit-query t))
  8771. (eval cmd))
  8772. (error (progn (widen) (eval cmd))))))
  8773. (t
  8774. (browse-url-at-point)))))))
  8775. (move-marker org-open-link-marker nil)
  8776. (run-hook-with-args 'org-follow-link-hook)))
  8777. (defun org-offer-links-in-entry (&optional nth zero)
  8778. "Offer links in the current entry and follow the selected link.
  8779. If there is only one link, follow it immediately as well.
  8780. If NTH is an integer, immediately pick the NTH link found.
  8781. If ZERO is a string, check also this string for a link, and if
  8782. there is one, offer it as link number zero."
  8783. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  8784. "\\(" org-angle-link-re "\\)\\|"
  8785. "\\(" org-plain-link-re "\\)"))
  8786. (cnt ?0)
  8787. (in-emacs (if (integerp nth) nil nth))
  8788. have-zero end links link c)
  8789. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  8790. (push (match-string 0 zero) links)
  8791. (setq cnt (1- cnt) have-zero t))
  8792. (save-excursion
  8793. (org-back-to-heading t)
  8794. (setq end (save-excursion (outline-next-heading) (point)))
  8795. (while (re-search-forward re end t)
  8796. (push (match-string 0) links))
  8797. (setq links (org-uniquify (reverse links))))
  8798. (cond
  8799. ((null links)
  8800. (message "No links"))
  8801. ((equal (length links) 1)
  8802. (setq link (list (car links))))
  8803. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  8804. (setq link (list (nth (if have-zero nth (1- nth)) links))))
  8805. (t ; we have to select a link
  8806. (save-excursion
  8807. (save-window-excursion
  8808. (delete-other-windows)
  8809. (with-output-to-temp-buffer "*Select Link*"
  8810. (mapc (lambda (l)
  8811. (if (not (string-match org-bracket-link-regexp l))
  8812. (princ (format "[%c] %s\n" (incf cnt)
  8813. (org-remove-angle-brackets l)))
  8814. (if (match-end 3)
  8815. (princ (format "[%c] %s (%s)\n" (incf cnt)
  8816. (match-string 3 l) (match-string 1 l)))
  8817. (princ (format "[%c] %s\n" (incf cnt)
  8818. (match-string 1 l))))))
  8819. links))
  8820. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  8821. (message "Select link to open, RET to open all:")
  8822. (setq c (read-char-exclusive))
  8823. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  8824. (when (equal c ?q) (error "Abort"))
  8825. (if (equal c ?\C-m)
  8826. (setq link links)
  8827. (setq nth (- c ?0))
  8828. (if have-zero (setq nth (1+ nth)))
  8829. (unless (and (integerp nth) (>= (length links) nth))
  8830. (error "Invalid link selection"))
  8831. (setq link (list (nth (1- nth) links))))))
  8832. (if link
  8833. (let ((buf (current-buffer)))
  8834. (dolist (l link)
  8835. (org-open-link-from-string l in-emacs buf))
  8836. t)
  8837. nil)))
  8838. ;; Add special file links that specify the way of opening
  8839. (org-add-link-type "file+sys" 'org-open-file-with-system)
  8840. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  8841. (defun org-open-file-with-system (path)
  8842. "Open file at PATH using the system way of opening it."
  8843. (org-open-file path 'system))
  8844. (defun org-open-file-with-emacs (path)
  8845. "Open file at PATH in Emacs."
  8846. (org-open-file path 'emacs))
  8847. (defun org-remove-file-link-modifiers ()
  8848. "Remove the file link modifiers in `file+sys:' and `file+emacs:' links."
  8849. (goto-char (point-min))
  8850. (while (re-search-forward "\\<file\\+\\(sys\\|emacs\\):" nil t)
  8851. (org-if-unprotected
  8852. (replace-match "file:" t t))))
  8853. (eval-after-load "org-exp"
  8854. '(add-hook 'org-export-preprocess-before-normalizing-links-hook
  8855. 'org-remove-file-link-modifiers))
  8856. ;;;; Time estimates
  8857. (defun org-get-effort (&optional pom)
  8858. "Get the effort estimate for the current entry."
  8859. (org-entry-get pom org-effort-property))
  8860. ;;; File search
  8861. (defvar org-create-file-search-functions nil
  8862. "List of functions to construct the right search string for a file link.
  8863. These functions are called in turn with point at the location to
  8864. which the link should point.
  8865. A function in the hook should first test if it would like to
  8866. handle this file type, for example by checking the `major-mode'
  8867. or the file extension. If it decides not to handle this file, it
  8868. should just return nil to give other functions a chance. If it
  8869. does handle the file, it must return the search string to be used
  8870. when following the link. The search string will be part of the
  8871. file link, given after a double colon, and `org-open-at-point'
  8872. will automatically search for it. If special measures must be
  8873. taken to make the search successful, another function should be
  8874. added to the companion hook `org-execute-file-search-functions',
  8875. which see.
  8876. A function in this hook may also use `setq' to set the variable
  8877. `description' to provide a suggestion for the descriptive text to
  8878. be used for this link when it gets inserted into an Org-mode
  8879. buffer with \\[org-insert-link].")
  8880. (defvar org-execute-file-search-functions nil
  8881. "List of functions to execute a file search triggered by a link.
  8882. Functions added to this hook must accept a single argument, the
  8883. search string that was part of the file link, the part after the
  8884. double colon. The function must first check if it would like to
  8885. handle this search, for example by checking the `major-mode' or
  8886. the file extension. If it decides not to handle this search, it
  8887. should just return nil to give other functions a chance. If it
  8888. does handle the search, it must return a non-nil value to keep
  8889. other functions from trying.
  8890. Each function can access the current prefix argument through the
  8891. variable `current-prefix-argument'. Note that a single prefix is
  8892. used to force opening a link in Emacs, so it may be good to only
  8893. use a numeric or double prefix to guide the search function.
  8894. In case this is needed, a function in this hook can also restore
  8895. the window configuration before `org-open-at-point' was called using:
  8896. (set-window-configuration org-window-config-before-follow-link)")
  8897. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  8898. (defun org-link-search (s &optional type avoid-pos stealth)
  8899. "Search for a link search option.
  8900. If S is surrounded by forward slashes, it is interpreted as a
  8901. regular expression. In org-mode files, this will create an `org-occur'
  8902. sparse tree. In ordinary files, `occur' will be used to list matches.
  8903. If the current buffer is in `dired-mode', grep will be used to search
  8904. in all files. If AVOID-POS is given, ignore matches near that position.
  8905. When optional argument STEALTH is non-nil, do not modify
  8906. visibility around point, thus ignoring
  8907. `org-show-hierarchy-above', `org-show-following-heading' and
  8908. `org-show-siblings' variables."
  8909. (let ((case-fold-search t)
  8910. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  8911. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  8912. (append '(("") (" ") ("\t") ("\n"))
  8913. org-emphasis-alist)
  8914. "\\|") "\\)"))
  8915. (pos (point))
  8916. (pre nil) (post nil)
  8917. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  8918. (cond
  8919. ;; First check if there are any special search functions
  8920. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  8921. ;; Now try the builtin stuff
  8922. ((and (equal (string-to-char s0) ?#)
  8923. (> (length s0) 1)
  8924. (save-excursion
  8925. (goto-char (point-min))
  8926. (and
  8927. (re-search-forward
  8928. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  8929. (setq type 'dedicated
  8930. pos (match-beginning 0))))
  8931. ;; There is an exact target for this
  8932. (goto-char pos)
  8933. (org-back-to-heading t)))
  8934. ((save-excursion
  8935. (goto-char (point-min))
  8936. (and
  8937. (re-search-forward
  8938. (concat "<<" (regexp-quote s0) ">>") nil t)
  8939. (setq type 'dedicated
  8940. pos (match-beginning 0))))
  8941. ;; There is an exact target for this
  8942. (goto-char pos))
  8943. ((save-excursion
  8944. (goto-char (point-min))
  8945. (and
  8946. (re-search-forward
  8947. (format "^[ \t]*#\\+TARGET: %s" (regexp-quote s0)) nil t)
  8948. (setq type 'dedicated pos (match-beginning 0))))
  8949. ;; Found an invisible target.
  8950. (goto-char pos))
  8951. ((save-excursion
  8952. (goto-char (point-min))
  8953. (and
  8954. (re-search-forward
  8955. (format "^[ \t]*#\\+NAME: %s" (regexp-quote s0)) nil t)
  8956. (setq type 'dedicated pos (match-beginning 0))))
  8957. ;; Found an element with a matching #+name affiliated keyword.
  8958. (goto-char pos))
  8959. ((and (string-match "^(\\(.*\\))$" s0)
  8960. (save-excursion
  8961. (goto-char (point-min))
  8962. (and
  8963. (re-search-forward
  8964. (concat "[^[]" (regexp-quote
  8965. (format org-coderef-label-format
  8966. (match-string 1 s0))))
  8967. nil t)
  8968. (setq type 'dedicated
  8969. pos (1+ (match-beginning 0))))))
  8970. ;; There is a coderef target for this
  8971. (goto-char pos))
  8972. ((string-match "^/\\(.*\\)/$" s)
  8973. ;; A regular expression
  8974. (cond
  8975. ((derived-mode-p 'org-mode)
  8976. (org-occur (match-string 1 s)))
  8977. ;;((eq major-mode 'dired-mode)
  8978. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  8979. (t (org-do-occur (match-string 1 s)))))
  8980. ((and (derived-mode-p 'org-mode) org-link-search-must-match-exact-headline)
  8981. (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  8982. (goto-char (point-min))
  8983. (cond
  8984. ((let (case-fold-search)
  8985. (re-search-forward (format org-complex-heading-regexp-format
  8986. (regexp-quote s))
  8987. nil t))
  8988. ;; OK, found a match
  8989. (setq type 'dedicated)
  8990. (goto-char (match-beginning 0)))
  8991. ((and (not org-link-search-inhibit-query)
  8992. (eq org-link-search-must-match-exact-headline 'query-to-create)
  8993. (y-or-n-p "No match - create this as a new heading? "))
  8994. (goto-char (point-max))
  8995. (or (bolp) (newline))
  8996. (insert "* " s "\n")
  8997. (beginning-of-line 0))
  8998. (t
  8999. (goto-char pos)
  9000. (error "No match"))))
  9001. (t
  9002. ;; A normal search string
  9003. (when (equal (string-to-char s) ?*)
  9004. ;; Anchor on headlines, post may include tags.
  9005. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  9006. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
  9007. s (substring s 1)))
  9008. (remove-text-properties
  9009. 0 (length s)
  9010. '(face nil mouse-face nil keymap nil fontified nil) s)
  9011. ;; Make a series of regular expressions to find a match
  9012. (setq words (org-split-string s "[ \n\r\t]+")
  9013. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  9014. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  9015. "\\)" markers)
  9016. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  9017. re2a (concat "[ \t\r\n]" re2a_)
  9018. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  9019. re4 (concat "[^a-zA-Z_]" re4_)
  9020. re1 (concat pre re2 post)
  9021. re3 (concat pre (if pre re4_ re4) post)
  9022. re5 (concat pre ".*" re4)
  9023. re2 (concat pre re2)
  9024. re2a (concat pre (if pre re2a_ re2a))
  9025. re4 (concat pre (if pre re4_ re4))
  9026. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  9027. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  9028. re5 "\\)"
  9029. ))
  9030. (cond
  9031. ((eq type 'org-occur) (org-occur reall))
  9032. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  9033. (t (goto-char (point-min))
  9034. (setq type 'fuzzy)
  9035. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  9036. (org-search-not-self 1 re1 nil t)
  9037. (org-search-not-self 1 re2 nil t)
  9038. (org-search-not-self 1 re2a nil t)
  9039. (org-search-not-self 1 re3 nil t)
  9040. (org-search-not-self 1 re4 nil t)
  9041. (org-search-not-self 1 re5 nil t)
  9042. )
  9043. (goto-char (match-beginning 1))
  9044. (goto-char pos)
  9045. (error "No match"))))))
  9046. (and (derived-mode-p 'org-mode)
  9047. (not stealth)
  9048. (org-show-context 'link-search))
  9049. type))
  9050. (defun org-search-not-self (group &rest args)
  9051. "Execute `re-search-forward', but only accept matches that do not
  9052. enclose the position of `org-open-link-marker'."
  9053. (let ((m org-open-link-marker))
  9054. (catch 'exit
  9055. (while (apply 're-search-forward args)
  9056. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  9057. (goto-char (match-end group))
  9058. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  9059. (> (match-beginning 0) (marker-position m))
  9060. (< (match-end 0) (marker-position m)))
  9061. (save-match-data
  9062. (or (not (org-in-regexp
  9063. org-bracket-link-analytic-regexp 1))
  9064. (not (match-end 4)) ; no description
  9065. (and (<= (match-beginning 4) (point))
  9066. (>= (match-end 4) (point))))))
  9067. (throw 'exit (point))))))))
  9068. (defun org-get-buffer-for-internal-link (buffer)
  9069. "Return a buffer to be used for displaying the link target of internal links."
  9070. (cond
  9071. ((not org-display-internal-link-with-indirect-buffer)
  9072. buffer)
  9073. ((string-match "(Clone)$" (buffer-name buffer))
  9074. (message "Buffer is already a clone, not making another one")
  9075. ;; we also do not modify visibility in this case
  9076. buffer)
  9077. (t ; make a new indirect buffer for displaying the link
  9078. (let* ((bn (buffer-name buffer))
  9079. (ibn (concat bn "(Clone)"))
  9080. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  9081. (with-current-buffer ib (org-overview))
  9082. ib))))
  9083. (defun org-do-occur (regexp &optional cleanup)
  9084. "Call the Emacs command `occur'.
  9085. If CLEANUP is non-nil, remove the printout of the regular expression
  9086. in the *Occur* buffer. This is useful if the regex is long and not useful
  9087. to read."
  9088. (occur regexp)
  9089. (when cleanup
  9090. (let ((cwin (selected-window)) win beg end)
  9091. (when (setq win (get-buffer-window "*Occur*"))
  9092. (select-window win))
  9093. (goto-char (point-min))
  9094. (when (re-search-forward "match[a-z]+" nil t)
  9095. (setq beg (match-end 0))
  9096. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  9097. (setq end (1- (match-beginning 0)))))
  9098. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  9099. (goto-char (point-min))
  9100. (select-window cwin))))
  9101. ;;; The mark ring for links jumps
  9102. (defvar org-mark-ring nil
  9103. "Mark ring for positions before jumps in Org-mode.")
  9104. (defvar org-mark-ring-last-goto nil
  9105. "Last position in the mark ring used to go back.")
  9106. ;; Fill and close the ring
  9107. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  9108. (loop for i from 1 to org-mark-ring-length do
  9109. (push (make-marker) org-mark-ring))
  9110. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  9111. org-mark-ring)
  9112. (defun org-mark-ring-push (&optional pos buffer)
  9113. "Put the current position or POS into the mark ring and rotate it."
  9114. (interactive)
  9115. (setq pos (or pos (point)))
  9116. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  9117. (move-marker (car org-mark-ring)
  9118. (or pos (point))
  9119. (or buffer (current-buffer)))
  9120. (message "%s"
  9121. (substitute-command-keys
  9122. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  9123. (defun org-mark-ring-goto (&optional n)
  9124. "Jump to the previous position in the mark ring.
  9125. With prefix arg N, jump back that many stored positions. When
  9126. called several times in succession, walk through the entire ring.
  9127. Org-mode commands jumping to a different position in the current file,
  9128. or to another Org-mode file, automatically push the old position
  9129. onto the ring."
  9130. (interactive "p")
  9131. (let (p m)
  9132. (if (eq last-command this-command)
  9133. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  9134. (setq p org-mark-ring))
  9135. (setq org-mark-ring-last-goto p)
  9136. (setq m (car p))
  9137. (org-pop-to-buffer-same-window (marker-buffer m))
  9138. (goto-char m)
  9139. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  9140. (defun org-remove-angle-brackets (s)
  9141. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  9142. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  9143. s)
  9144. (defun org-add-angle-brackets (s)
  9145. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  9146. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  9147. s)
  9148. (defun org-remove-double-quotes (s)
  9149. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  9150. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  9151. s)
  9152. ;;; Following specific links
  9153. (defun org-follow-timestamp-link ()
  9154. (cond
  9155. ((org-at-date-range-p t)
  9156. (let ((org-agenda-start-on-weekday)
  9157. (t1 (match-string 1))
  9158. (t2 (match-string 2)))
  9159. (setq t1 (time-to-days (org-time-string-to-time t1))
  9160. t2 (time-to-days (org-time-string-to-time t2)))
  9161. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  9162. ((org-at-timestamp-p t)
  9163. (org-agenda-list nil (time-to-days (org-time-string-to-time
  9164. (substring (match-string 1) 0 10)))
  9165. 1))
  9166. (t (error "This should not happen"))))
  9167. ;;; Following file links
  9168. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  9169. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  9170. (declare-function mailcap-mime-info
  9171. "mailcap" (string &optional request no-decode))
  9172. (defvar org-wait nil)
  9173. (defun org-open-file (path &optional in-emacs line search)
  9174. "Open the file at PATH.
  9175. First, this expands any special file name abbreviations. Then the
  9176. configuration variable `org-file-apps' is checked if it contains an
  9177. entry for this file type, and if yes, the corresponding command is launched.
  9178. If no application is found, Emacs simply visits the file.
  9179. With optional prefix argument IN-EMACS, Emacs will visit the file.
  9180. With a double \\[universal-argument] \\[universal-argument] \
  9181. prefix arg, Org tries to avoid opening in Emacs
  9182. and to use an external application to visit the file.
  9183. Optional LINE specifies a line to go to, optional SEARCH a string
  9184. to search for. If LINE or SEARCH is given, the file will be
  9185. opened in Emacs, unless an entry from org-file-apps that makes
  9186. use of groups in a regexp matches.
  9187. If you want to change the way frames are used when following a
  9188. link, please customize `org-link-frame-setup'.
  9189. If the file does not exist, an error is thrown."
  9190. (let* ((file (if (equal path "")
  9191. buffer-file-name
  9192. (substitute-in-file-name (expand-file-name path))))
  9193. (file-apps (append org-file-apps (org-default-apps)))
  9194. (apps (org-remove-if
  9195. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9196. (apps-dlink (org-remove-if-not
  9197. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9198. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  9199. (dirp (if remp nil (file-directory-p file)))
  9200. (file (if (and dirp org-open-directory-means-index-dot-org)
  9201. (concat (file-name-as-directory file) "index.org")
  9202. file))
  9203. (a-m-a-p (assq 'auto-mode apps))
  9204. (dfile (downcase file))
  9205. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  9206. (link (cond ((and (eq line nil)
  9207. (eq search nil))
  9208. file)
  9209. (line
  9210. (concat file "::" (number-to-string line)))
  9211. (search
  9212. (concat file "::" search))))
  9213. (dlink (downcase link))
  9214. (old-buffer (current-buffer))
  9215. (old-pos (point))
  9216. (old-mode major-mode)
  9217. ext cmd link-match-data)
  9218. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  9219. (setq ext (match-string 1 dfile))
  9220. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  9221. (setq ext (match-string 1 dfile))))
  9222. (cond
  9223. ((member in-emacs '((16) system))
  9224. (setq cmd (cdr (assoc 'system apps))))
  9225. (in-emacs (setq cmd 'emacs))
  9226. (t
  9227. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  9228. (and dirp (cdr (assoc 'directory apps)))
  9229. ; first, try matching against apps-dlink
  9230. ; if we get a match here, store the match data for later
  9231. (let ((match (assoc-default dlink apps-dlink
  9232. 'string-match)))
  9233. (if match
  9234. (progn (setq link-match-data (match-data))
  9235. match)
  9236. (progn (setq in-emacs (or in-emacs line search))
  9237. nil))) ; if we have no match in apps-dlink,
  9238. ; always open the file in emacs if line or search
  9239. ; is given (for backwards compatibility)
  9240. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  9241. 'string-match)
  9242. (cdr (assoc ext apps))
  9243. (cdr (assoc t apps))))))
  9244. (when (eq cmd 'system)
  9245. (setq cmd (cdr (assoc 'system apps))))
  9246. (when (eq cmd 'default)
  9247. (setq cmd (cdr (assoc t apps))))
  9248. (when (eq cmd 'mailcap)
  9249. (require 'mailcap)
  9250. (mailcap-parse-mailcaps)
  9251. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  9252. (command (mailcap-mime-info mime-type)))
  9253. (if (stringp command)
  9254. (setq cmd command)
  9255. (setq cmd 'emacs))))
  9256. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  9257. (not (file-exists-p file))
  9258. (not org-open-non-existing-files))
  9259. (error "No such file: %s" file))
  9260. (cond
  9261. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  9262. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  9263. (while (string-match "['\"]%s['\"]" cmd)
  9264. (setq cmd (replace-match "%s" t t cmd)))
  9265. (while (string-match "%s" cmd)
  9266. (setq cmd (replace-match
  9267. (save-match-data
  9268. (shell-quote-argument
  9269. (convert-standard-filename file)))
  9270. t t cmd)))
  9271. ;; Replace "%1", "%2" etc. in command with group matches from regex
  9272. (save-match-data
  9273. (let ((match-index 1)
  9274. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  9275. (set-match-data link-match-data)
  9276. (while (<= match-index number-of-groups)
  9277. (let ((regex (concat "%" (number-to-string match-index)))
  9278. (replace-with (match-string match-index dlink)))
  9279. (while (string-match regex cmd)
  9280. (setq cmd (replace-match replace-with t t cmd))))
  9281. (setq match-index (+ match-index 1)))))
  9282. (save-window-excursion
  9283. (start-process-shell-command cmd nil cmd)
  9284. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  9285. ))
  9286. ((or (stringp cmd)
  9287. (eq cmd 'emacs))
  9288. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  9289. (widen)
  9290. (if line (org-goto-line line)
  9291. (if search (org-link-search search))))
  9292. ((consp cmd)
  9293. (let ((file (convert-standard-filename file)))
  9294. (save-match-data
  9295. (set-match-data link-match-data)
  9296. (eval cmd))))
  9297. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  9298. (and (derived-mode-p 'org-mode) (eq old-mode 'org-mode)
  9299. (or (not (equal old-buffer (current-buffer)))
  9300. (not (equal old-pos (point))))
  9301. (org-mark-ring-push old-pos old-buffer))))
  9302. (defun org-file-apps-entry-match-against-dlink-p (entry)
  9303. "This function returns non-nil if `entry' uses a regular
  9304. expression which should be matched against the whole link by
  9305. org-open-file.
  9306. It assumes that is the case when the entry uses a regular
  9307. expression which has at least one grouping construct and the
  9308. action is either a lisp form or a command string containing
  9309. '%1', i.e. using at least one subexpression match as a
  9310. parameter."
  9311. (let ((selector (car entry))
  9312. (action (cdr entry)))
  9313. (if (stringp selector)
  9314. (and (> (regexp-opt-depth selector) 0)
  9315. (or (and (stringp action)
  9316. (string-match "%[0-9]" action))
  9317. (consp action)))
  9318. nil)))
  9319. (defun org-default-apps ()
  9320. "Return the default applications for this operating system."
  9321. (cond
  9322. ((eq system-type 'darwin)
  9323. org-file-apps-defaults-macosx)
  9324. ((eq system-type 'windows-nt)
  9325. org-file-apps-defaults-windowsnt)
  9326. (t org-file-apps-defaults-gnu)))
  9327. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  9328. "Convert extensions to regular expressions in the cars of LIST.
  9329. Also, weed out any non-string entries, because the return value is used
  9330. only for regexp matching.
  9331. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  9332. point to the symbol `emacs', indicating that the file should
  9333. be opened in Emacs."
  9334. (append
  9335. (delq nil
  9336. (mapcar (lambda (x)
  9337. (if (not (stringp (car x)))
  9338. nil
  9339. (if (string-match "\\W" (car x))
  9340. x
  9341. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  9342. list))
  9343. (if add-auto-mode
  9344. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  9345. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  9346. (defun org-file-remote-p (file)
  9347. "Test whether FILE specifies a location on a remote system.
  9348. Return non-nil if the location is indeed remote.
  9349. For example, the filename \"/user@host:/foo\" specifies a location
  9350. on the system \"/user@host:\"."
  9351. (cond ((fboundp 'file-remote-p)
  9352. (file-remote-p file))
  9353. ((fboundp 'tramp-handle-file-remote-p)
  9354. (tramp-handle-file-remote-p file))
  9355. ((and (boundp 'ange-ftp-name-format)
  9356. (string-match (car ange-ftp-name-format) file))
  9357. t)
  9358. (t nil)))
  9359. ;;;; Refiling
  9360. (defun org-get-org-file ()
  9361. "Read a filename, with default directory `org-directory'."
  9362. (let ((default (or org-default-notes-file remember-data-file)))
  9363. (read-file-name (format "File name [%s]: " default)
  9364. (file-name-as-directory org-directory)
  9365. default)))
  9366. (defun org-notes-order-reversed-p ()
  9367. "Check if the current file should receive notes in reversed order."
  9368. (cond
  9369. ((not org-reverse-note-order) nil)
  9370. ((eq t org-reverse-note-order) t)
  9371. ((not (listp org-reverse-note-order)) nil)
  9372. (t (catch 'exit
  9373. (let ((all org-reverse-note-order)
  9374. entry)
  9375. (while (setq entry (pop all))
  9376. (if (string-match (car entry) buffer-file-name)
  9377. (throw 'exit (cdr entry))))
  9378. nil)))))
  9379. (defvar org-refile-target-table nil
  9380. "The list of refile targets, created by `org-refile'.")
  9381. (defvar org-agenda-new-buffers nil
  9382. "Buffers created to visit agenda files.")
  9383. (defvar org-refile-cache nil
  9384. "Cache for refile targets.")
  9385. (defvar org-refile-markers nil
  9386. "All the markers used for caching refile locations.")
  9387. (defun org-refile-marker (pos)
  9388. "Get a new refile marker, but only if caching is in use."
  9389. (if (not org-refile-use-cache)
  9390. pos
  9391. (let ((m (make-marker)))
  9392. (move-marker m pos)
  9393. (push m org-refile-markers)
  9394. m)))
  9395. (defun org-refile-cache-clear ()
  9396. "Clear the refile cache and disable all the markers."
  9397. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  9398. (setq org-refile-markers nil)
  9399. (setq org-refile-cache nil)
  9400. (message "Refile cache has been cleared"))
  9401. (defun org-refile-cache-check-set (set)
  9402. "Check if all the markers in the cache still have live buffers."
  9403. (let (marker)
  9404. (catch 'exit
  9405. (while (and set (setq marker (nth 3 (pop set))))
  9406. ;; if org-refile-use-outline-path is 'file, marker may be nil
  9407. (when (and marker (null (marker-buffer marker)))
  9408. (message "not found") (sit-for 3)
  9409. (throw 'exit nil)))
  9410. t)))
  9411. (defun org-refile-cache-put (set &rest identifiers)
  9412. "Push the refile targets SET into the cache, under IDENTIFIERS."
  9413. (let* ((key (sha1 (prin1-to-string identifiers)))
  9414. (entry (assoc key org-refile-cache)))
  9415. (if entry
  9416. (setcdr entry set)
  9417. (push (cons key set) org-refile-cache))))
  9418. (defun org-refile-cache-get (&rest identifiers)
  9419. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  9420. (cond
  9421. ((not org-refile-cache) nil)
  9422. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  9423. (t
  9424. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  9425. org-refile-cache))))
  9426. (and set (org-refile-cache-check-set set) set)))))
  9427. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  9428. "Produce a table with refile targets."
  9429. (let ((case-fold-search nil)
  9430. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  9431. (entries (or org-refile-targets '((nil . (:level . 1)))))
  9432. targets tgs txt re files f desc descre fast-path-p level pos0)
  9433. (message "Getting targets...")
  9434. (with-current-buffer (or default-buffer (current-buffer))
  9435. (while (setq entry (pop entries))
  9436. (setq files (car entry) desc (cdr entry))
  9437. (setq fast-path-p nil)
  9438. (cond
  9439. ((null files) (setq files (list (current-buffer))))
  9440. ((eq files 'org-agenda-files)
  9441. (setq files (org-agenda-files 'unrestricted)))
  9442. ((and (symbolp files) (fboundp files))
  9443. (setq files (funcall files)))
  9444. ((and (symbolp files) (boundp files))
  9445. (setq files (symbol-value files))))
  9446. (if (stringp files) (setq files (list files)))
  9447. (cond
  9448. ((eq (car desc) :tag)
  9449. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  9450. ((eq (car desc) :todo)
  9451. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  9452. ((eq (car desc) :regexp)
  9453. (setq descre (cdr desc)))
  9454. ((eq (car desc) :level)
  9455. (setq descre (concat "^\\*\\{" (number-to-string
  9456. (if org-odd-levels-only
  9457. (1- (* 2 (cdr desc)))
  9458. (cdr desc)))
  9459. "\\}[ \t]")))
  9460. ((eq (car desc) :maxlevel)
  9461. (setq fast-path-p t)
  9462. (setq descre (concat "^\\*\\{1," (number-to-string
  9463. (if org-odd-levels-only
  9464. (1- (* 2 (cdr desc)))
  9465. (cdr desc)))
  9466. "\\}[ \t]")))
  9467. (t (error "Bad refiling target description %s" desc)))
  9468. (while (setq f (pop files))
  9469. (with-current-buffer
  9470. (if (bufferp f) f (org-get-agenda-file-buffer f))
  9471. (or
  9472. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  9473. (progn
  9474. (if (bufferp f) (setq f (buffer-file-name
  9475. (buffer-base-buffer f))))
  9476. (setq f (and f (expand-file-name f)))
  9477. (if (eq org-refile-use-outline-path 'file)
  9478. (push (list (file-name-nondirectory f) f nil nil) tgs))
  9479. (save-excursion
  9480. (save-restriction
  9481. (widen)
  9482. (goto-char (point-min))
  9483. (while (re-search-forward descre nil t)
  9484. (goto-char (setq pos0 (point-at-bol)))
  9485. (catch 'next
  9486. (when org-refile-target-verify-function
  9487. (save-match-data
  9488. (or (funcall org-refile-target-verify-function)
  9489. (throw 'next t))))
  9490. (when (and (looking-at org-complex-heading-regexp)
  9491. (not (member (match-string 4) excluded-entries))
  9492. (match-string 4))
  9493. (setq level (org-reduced-level
  9494. (- (match-end 1) (match-beginning 1)))
  9495. txt (org-link-display-format (match-string 4))
  9496. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  9497. re (format org-complex-heading-regexp-format
  9498. (regexp-quote (match-string 4))))
  9499. (when org-refile-use-outline-path
  9500. (setq txt (mapconcat
  9501. 'org-protect-slash
  9502. (append
  9503. (if (eq org-refile-use-outline-path
  9504. 'file)
  9505. (list (file-name-nondirectory
  9506. (buffer-file-name
  9507. (buffer-base-buffer))))
  9508. (if (eq org-refile-use-outline-path
  9509. 'full-file-path)
  9510. (list (buffer-file-name
  9511. (buffer-base-buffer)))))
  9512. (org-get-outline-path fast-path-p
  9513. level txt)
  9514. (list txt))
  9515. "/")))
  9516. (push (list txt f re (org-refile-marker (point)))
  9517. tgs)))
  9518. (when (= (point) pos0)
  9519. ;; verification function has not moved point
  9520. (goto-char (point-at-eol))))))))
  9521. (when org-refile-use-cache
  9522. (org-refile-cache-put tgs (buffer-file-name) descre))
  9523. (setq targets (append tgs targets))
  9524. ))))
  9525. (message "Getting targets...done")
  9526. (nreverse targets)))
  9527. (defun org-protect-slash (s)
  9528. (while (string-match "/" s)
  9529. (setq s (replace-match "\\" t t s)))
  9530. s)
  9531. (defvar org-olpa (make-vector 20 nil))
  9532. (defun org-get-outline-path (&optional fastp level heading)
  9533. "Return the outline path to the current entry, as a list.
  9534. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  9535. routine which makes outline path derivations for an entire file,
  9536. avoiding backtracing. Refile target collection makes use of that."
  9537. (if fastp
  9538. (progn
  9539. (if (> level 19)
  9540. (error "Outline path failure, more than 19 levels"))
  9541. (loop for i from level upto 19 do
  9542. (aset org-olpa i nil))
  9543. (prog1
  9544. (delq nil (append org-olpa nil))
  9545. (aset org-olpa level heading)))
  9546. (let (rtn case-fold-search)
  9547. (save-excursion
  9548. (save-restriction
  9549. (widen)
  9550. (while (org-up-heading-safe)
  9551. (when (looking-at org-complex-heading-regexp)
  9552. (push (org-match-string-no-properties 4) rtn)))
  9553. rtn)))))
  9554. (defun org-format-outline-path (path &optional width prefix)
  9555. "Format the outline path PATH for display.
  9556. Width is the maximum number of characters that is available.
  9557. Prefix is a prefix to be included in the returned string,
  9558. such as the file name."
  9559. (setq width (or width 79))
  9560. (if prefix (setq width (- width (length prefix))))
  9561. (if (not path)
  9562. (or prefix "")
  9563. (let* ((nsteps (length path))
  9564. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  9565. (maxwidth (if (<= total-width width)
  9566. 10000 ;; everything fits
  9567. ;; we need to shorten the level headings
  9568. (/ (- width nsteps) nsteps)))
  9569. (org-odd-levels-only nil)
  9570. (n 0)
  9571. (total (1+ (length prefix))))
  9572. (setq maxwidth (max maxwidth 10))
  9573. (concat prefix
  9574. (mapconcat
  9575. (lambda (h)
  9576. (setq n (1+ n))
  9577. (if (and (= n nsteps) (< maxwidth 10000))
  9578. (setq maxwidth (- total-width total)))
  9579. (if (< (length h) maxwidth)
  9580. (progn (setq total (+ total (length h) 1)) h)
  9581. (setq h (substring h 0 (- maxwidth 2))
  9582. total (+ total maxwidth 1))
  9583. (if (string-match "[ \t]+\\'" h)
  9584. (setq h (substring h 0 (match-beginning 0))))
  9585. (setq h (concat h "..")))
  9586. (org-add-props h nil 'face
  9587. (nth (% (1- n) org-n-level-faces)
  9588. org-level-faces))
  9589. h)
  9590. path "/")))))
  9591. (defun org-display-outline-path (&optional file current)
  9592. "Display the current outline path in the echo area."
  9593. (interactive "P")
  9594. (let* ((bfn (buffer-file-name (buffer-base-buffer)))
  9595. (case-fold-search nil)
  9596. (path (and (derived-mode-p 'org-mode) (org-get-outline-path))))
  9597. (if current (setq path (append path
  9598. (save-excursion
  9599. (org-back-to-heading t)
  9600. (if (looking-at org-complex-heading-regexp)
  9601. (list (match-string 4)))))))
  9602. (message "%s"
  9603. (org-format-outline-path
  9604. path
  9605. (1- (frame-width))
  9606. (and file bfn (concat (file-name-nondirectory bfn) "/"))))))
  9607. (defvar org-refile-history nil
  9608. "History for refiling operations.")
  9609. (defvar org-after-refile-insert-hook nil
  9610. "Hook run after `org-refile' has inserted its stuff at the new location.
  9611. Note that this is still *before* the stuff will be removed from
  9612. the *old* location.")
  9613. (defvar org-capture-last-stored-marker)
  9614. (defun org-refile (&optional goto default-buffer rfloc)
  9615. "Move the entry or entries at point to another heading.
  9616. The list of target headings is compiled using the information in
  9617. `org-refile-targets', which see.
  9618. At the target location, the entry is filed as a subitem of the target
  9619. heading. Depending on `org-reverse-note-order', the new subitem will
  9620. either be the first or the last subitem.
  9621. If there is an active region, all entries in that region will be moved.
  9622. However, the region must fulfill the requirement that the first heading
  9623. is the first one sets the top-level of the moved text - at most siblings
  9624. below it are allowed.
  9625. With prefix arg GOTO, the command will only visit the target location
  9626. and not actually move anything.
  9627. With a double prefix arg \\[universal-argument] \\[universal-argument], \
  9628. go to the location where the last refiling operation has put the subtree.
  9629. With a prefix argument of `2', refile to the running clock.
  9630. RFLOC can be a refile location obtained in a different way.
  9631. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  9632. If you are using target caching (see `org-refile-use-cache'),
  9633. You have to clear the target cache in order to find new targets.
  9634. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  9635. prefix argument (`C-u C-u C-u C-c C-w')."
  9636. (interactive "P")
  9637. (if (member goto '(0 (64)))
  9638. (org-refile-cache-clear)
  9639. (let* ((cbuf (current-buffer))
  9640. (regionp (org-region-active-p))
  9641. (region-start (and regionp (region-beginning)))
  9642. (region-end (and regionp (region-end)))
  9643. (region-length (and regionp (- region-end region-start)))
  9644. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  9645. pos it nbuf file re level reversed)
  9646. (setq last-command nil)
  9647. (when regionp
  9648. (goto-char region-start)
  9649. (or (bolp) (goto-char (point-at-bol)))
  9650. (setq region-start (point))
  9651. (unless (or (org-kill-is-subtree-p
  9652. (buffer-substring region-start region-end))
  9653. (prog1 org-refile-active-region-within-subtree
  9654. (org-toggle-heading)))
  9655. (error "The region is not a (sequence of) subtree(s)")))
  9656. (if (equal goto '(16))
  9657. (org-refile-goto-last-stored)
  9658. (when (or
  9659. (and (equal goto 2)
  9660. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  9661. (prog1
  9662. (setq it (list (or org-clock-heading "running clock")
  9663. (buffer-file-name
  9664. (marker-buffer org-clock-hd-marker))
  9665. ""
  9666. (marker-position org-clock-hd-marker)))
  9667. (setq goto nil)))
  9668. (setq it (or rfloc
  9669. (let (heading-text)
  9670. (save-excursion
  9671. (unless goto
  9672. (org-back-to-heading t)
  9673. (setq heading-text
  9674. (nth 4 (org-heading-components))))
  9675. (org-refile-get-location
  9676. (cond (goto "Goto")
  9677. (regionp "Refile region to")
  9678. (t (concat "Refile subtree \""
  9679. heading-text "\" to")))
  9680. default-buffer
  9681. (and (not (equal '(4) goto))
  9682. org-refile-allow-creating-parent-nodes)
  9683. goto))))))
  9684. (setq file (nth 1 it)
  9685. re (nth 2 it)
  9686. pos (nth 3 it))
  9687. (if (and (not goto)
  9688. pos
  9689. (equal (buffer-file-name) file)
  9690. (if regionp
  9691. (and (>= pos region-start)
  9692. (<= pos region-end))
  9693. (and (>= pos (point))
  9694. (< pos (save-excursion
  9695. (org-end-of-subtree t t))))))
  9696. (error "Cannot refile to position inside the tree or region"))
  9697. (setq nbuf (or (find-buffer-visiting file)
  9698. (find-file-noselect file)))
  9699. (if goto
  9700. (progn
  9701. (org-pop-to-buffer-same-window nbuf)
  9702. (goto-char pos)
  9703. (org-show-context 'org-goto))
  9704. (if regionp
  9705. (progn
  9706. (org-kill-new (buffer-substring region-start region-end))
  9707. (org-save-markers-in-region region-start region-end))
  9708. (org-copy-subtree 1 nil t))
  9709. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  9710. (find-file-noselect file)))
  9711. (setq reversed (org-notes-order-reversed-p))
  9712. (save-excursion
  9713. (save-restriction
  9714. (widen)
  9715. (if pos
  9716. (progn
  9717. (goto-char pos)
  9718. (looking-at org-outline-regexp)
  9719. (setq level (org-get-valid-level (funcall outline-level) 1))
  9720. (goto-char
  9721. (if reversed
  9722. (or (outline-next-heading) (point-max))
  9723. (or (save-excursion (org-get-next-sibling))
  9724. (org-end-of-subtree t t)
  9725. (point-max)))))
  9726. (setq level 1)
  9727. (if (not reversed)
  9728. (goto-char (point-max))
  9729. (goto-char (point-min))
  9730. (or (outline-next-heading) (goto-char (point-max)))))
  9731. (if (not (bolp)) (newline))
  9732. (org-paste-subtree level)
  9733. (when org-log-refile
  9734. (org-add-log-setup 'refile nil nil 'findpos
  9735. org-log-refile)
  9736. (unless (eq org-log-refile 'note)
  9737. (save-excursion (org-add-log-note))))
  9738. (and org-auto-align-tags (org-set-tags nil t))
  9739. (bookmark-set "org-refile-last-stored")
  9740. ;; If we are refiling for capture, make sure that the
  9741. ;; last-capture pointers point here
  9742. (when (org-bound-and-true-p org-refile-for-capture)
  9743. (bookmark-set "org-capture-last-stored-marker")
  9744. (move-marker org-capture-last-stored-marker (point)))
  9745. (if (fboundp 'deactivate-mark) (deactivate-mark))
  9746. (run-hooks 'org-after-refile-insert-hook))))
  9747. (if regionp
  9748. (delete-region (point) (+ (point) region-length))
  9749. (org-cut-subtree))
  9750. (when (featurep 'org-inlinetask)
  9751. (org-inlinetask-remove-END-maybe))
  9752. (setq org-markers-to-move nil)
  9753. (message "Refiled to \"%s\" in file %s" (car it) file)))))))
  9754. (defun org-refile-goto-last-stored ()
  9755. "Go to the location where the last refile was stored."
  9756. (interactive)
  9757. (bookmark-jump "org-refile-last-stored")
  9758. (message "This is the location of the last refile"))
  9759. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  9760. no-exclude)
  9761. "Prompt the user for a refile location, using PROMPT.
  9762. PROMPT should not be suffixed with a colon and a space, because
  9763. this function appends the default value from
  9764. `org-refile-history' automatically, if that is not empty.
  9765. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  9766. this is used for the GOTO interface."
  9767. (let ((org-refile-targets org-refile-targets)
  9768. (org-refile-use-outline-path org-refile-use-outline-path)
  9769. excluded-entries)
  9770. (when (and (derived-mode-p 'org-mode)
  9771. (not org-refile-use-cache)
  9772. (not no-exclude))
  9773. (org-map-tree
  9774. (lambda()
  9775. (setq excluded-entries
  9776. (append excluded-entries (list (org-get-heading t t)))))))
  9777. (setq org-refile-target-table
  9778. (org-refile-get-targets default-buffer excluded-entries)))
  9779. (unless org-refile-target-table
  9780. (error "No refile targets"))
  9781. (let* ((prompt (concat prompt
  9782. (and (car org-refile-history)
  9783. (concat " (default " (car org-refile-history) ")"))
  9784. ": "))
  9785. (cbuf (current-buffer))
  9786. (partial-completion-mode nil)
  9787. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  9788. (cfunc (if (and org-refile-use-outline-path
  9789. org-outline-path-complete-in-steps)
  9790. 'org-olpath-completing-read
  9791. 'org-icompleting-read))
  9792. (extra (if org-refile-use-outline-path "/" ""))
  9793. (filename (and cfn (expand-file-name cfn)))
  9794. (tbl (mapcar
  9795. (lambda (x)
  9796. (if (and (not (member org-refile-use-outline-path
  9797. '(file full-file-path)))
  9798. (not (equal filename (nth 1 x))))
  9799. (cons (concat (car x) extra " ("
  9800. (file-name-nondirectory (nth 1 x)) ")")
  9801. (cdr x))
  9802. (cons (concat (car x) extra) (cdr x))))
  9803. org-refile-target-table))
  9804. (completion-ignore-case t)
  9805. pa answ parent-target child parent old-hist)
  9806. (setq old-hist org-refile-history)
  9807. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  9808. nil 'org-refile-history (car org-refile-history)))
  9809. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  9810. (org-refile-check-position pa)
  9811. (if pa
  9812. (progn
  9813. (when (or (not org-refile-history)
  9814. (not (eq old-hist org-refile-history))
  9815. (not (equal (car pa) (car org-refile-history))))
  9816. (setq org-refile-history
  9817. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  9818. org-refile-history
  9819. (cdr org-refile-history))))
  9820. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  9821. (pop org-refile-history)))
  9822. pa)
  9823. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  9824. (progn
  9825. (setq parent (match-string 1 answ)
  9826. child (match-string 2 answ))
  9827. (setq parent-target (or (assoc parent tbl)
  9828. (assoc (concat parent "/") tbl)))
  9829. (when (and parent-target
  9830. (or (eq new-nodes t)
  9831. (and (eq new-nodes 'confirm)
  9832. (y-or-n-p (format "Create new node \"%s\"? "
  9833. child)))))
  9834. (org-refile-new-child parent-target child)))
  9835. (error "Invalid target location")))))
  9836. (declare-function org-string-nw-p "org-macs.el" (s))
  9837. (defun org-refile-check-position (refile-pointer)
  9838. "Check if the refile pointer matches the readline to which it points."
  9839. (let* ((file (nth 1 refile-pointer))
  9840. (re (nth 2 refile-pointer))
  9841. (pos (nth 3 refile-pointer))
  9842. buffer)
  9843. (when (org-string-nw-p re)
  9844. (setq buffer (if (markerp pos)
  9845. (marker-buffer pos)
  9846. (or (find-buffer-visiting file)
  9847. (find-file-noselect file))))
  9848. (with-current-buffer buffer
  9849. (save-excursion
  9850. (save-restriction
  9851. (widen)
  9852. (goto-char pos)
  9853. (beginning-of-line 1)
  9854. (unless (org-looking-at-p re)
  9855. (error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling"))))))))
  9856. (defun org-refile-new-child (parent-target child)
  9857. "Use refile target PARENT-TARGET to add new CHILD below it."
  9858. (unless parent-target
  9859. (error "Cannot find parent for new node"))
  9860. (let ((file (nth 1 parent-target))
  9861. (pos (nth 3 parent-target))
  9862. level)
  9863. (with-current-buffer (or (find-buffer-visiting file)
  9864. (find-file-noselect file))
  9865. (save-excursion
  9866. (save-restriction
  9867. (widen)
  9868. (if pos
  9869. (goto-char pos)
  9870. (goto-char (point-max))
  9871. (if (not (bolp)) (newline)))
  9872. (when (looking-at org-outline-regexp)
  9873. (setq level (funcall outline-level))
  9874. (org-end-of-subtree t t))
  9875. (org-back-over-empty-lines)
  9876. (insert "\n" (make-string
  9877. (if pos (org-get-valid-level level 1) 1) ?*)
  9878. " " child "\n")
  9879. (beginning-of-line 0)
  9880. (list (concat (car parent-target) "/" child) file "" (point)))))))
  9881. (defun org-olpath-completing-read (prompt collection &rest args)
  9882. "Read an outline path like a file name."
  9883. (let ((thetable collection)
  9884. (org-completion-use-ido nil) ; does not work with ido.
  9885. (org-completion-use-iswitchb nil)) ; or iswitchb
  9886. (apply
  9887. 'org-icompleting-read prompt
  9888. (lambda (string predicate &optional flag)
  9889. (let (rtn r f (l (length string)))
  9890. (cond
  9891. ((eq flag nil)
  9892. ;; try completion
  9893. (try-completion string thetable))
  9894. ((eq flag t)
  9895. ;; all-completions
  9896. (setq rtn (all-completions string thetable predicate))
  9897. (mapcar
  9898. (lambda (x)
  9899. (setq r (substring x l))
  9900. (if (string-match " ([^)]*)$" x)
  9901. (setq f (match-string 0 x))
  9902. (setq f ""))
  9903. (if (string-match "/" r)
  9904. (concat string (substring r 0 (match-end 0)) f)
  9905. x))
  9906. rtn))
  9907. ((eq flag 'lambda)
  9908. ;; exact match?
  9909. (assoc string thetable)))
  9910. ))
  9911. args)))
  9912. ;;;; Dynamic blocks
  9913. (defun org-find-dblock (name)
  9914. "Find the first dynamic block with name NAME in the buffer.
  9915. If not found, stay at current position and return nil."
  9916. (let (pos)
  9917. (save-excursion
  9918. (goto-char (point-min))
  9919. (setq pos (and (re-search-forward (concat "^[ \t]*#\\+BEGIN:[ \t]+" name "\\>")
  9920. nil t)
  9921. (match-beginning 0))))
  9922. (if pos (goto-char pos))
  9923. pos))
  9924. (defconst org-dblock-start-re
  9925. "^[ \t]*#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  9926. "Matches the start line of a dynamic block, with parameters.")
  9927. (defconst org-dblock-end-re "^[ \t]*#\\+END\\([: \t\r\n]\\|$\\)"
  9928. "Matches the end of a dynamic block.")
  9929. (defun org-create-dblock (plist)
  9930. "Create a dynamic block section, with parameters taken from PLIST.
  9931. PLIST must contain a :name entry which is used as name of the block."
  9932. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  9933. (end-of-line 1)
  9934. (newline))
  9935. (let ((col (current-column))
  9936. (name (plist-get plist :name)))
  9937. (insert "#+BEGIN: " name)
  9938. (while plist
  9939. (if (eq (car plist) :name)
  9940. (setq plist (cddr plist))
  9941. (insert " " (prin1-to-string (pop plist)))))
  9942. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  9943. (beginning-of-line -2)))
  9944. (defun org-prepare-dblock ()
  9945. "Prepare dynamic block for refresh.
  9946. This empties the block, puts the cursor at the insert position and returns
  9947. the property list including an extra property :name with the block name."
  9948. (unless (looking-at org-dblock-start-re)
  9949. (error "Not at a dynamic block"))
  9950. (let* ((begdel (1+ (match-end 0)))
  9951. (name (org-no-properties (match-string 1)))
  9952. (params (append (list :name name)
  9953. (read (concat "(" (match-string 3) ")")))))
  9954. (save-excursion
  9955. (beginning-of-line 1)
  9956. (skip-chars-forward " \t")
  9957. (setq params (plist-put params :indentation-column (current-column))))
  9958. (unless (re-search-forward org-dblock-end-re nil t)
  9959. (error "Dynamic block not terminated"))
  9960. (setq params
  9961. (append params
  9962. (list :content (buffer-substring
  9963. begdel (match-beginning 0)))))
  9964. (delete-region begdel (match-beginning 0))
  9965. (goto-char begdel)
  9966. (open-line 1)
  9967. params))
  9968. (defun org-map-dblocks (&optional command)
  9969. "Apply COMMAND to all dynamic blocks in the current buffer.
  9970. If COMMAND is not given, use `org-update-dblock'."
  9971. (let ((cmd (or command 'org-update-dblock)))
  9972. (save-excursion
  9973. (goto-char (point-min))
  9974. (while (re-search-forward org-dblock-start-re nil t)
  9975. (goto-char (match-beginning 0))
  9976. (save-excursion
  9977. (condition-case nil
  9978. (funcall cmd)
  9979. (error (message "Error during update of dynamic block"))))
  9980. (unless (re-search-forward org-dblock-end-re nil t)
  9981. (error "Dynamic block not terminated"))))))
  9982. (defun org-dblock-update (&optional arg)
  9983. "User command for updating dynamic blocks.
  9984. Update the dynamic block at point. With prefix ARG, update all dynamic
  9985. blocks in the buffer."
  9986. (interactive "P")
  9987. (if arg
  9988. (org-update-all-dblocks)
  9989. (or (looking-at org-dblock-start-re)
  9990. (org-beginning-of-dblock))
  9991. (org-update-dblock)))
  9992. (defun org-update-dblock ()
  9993. "Update the dynamic block at point.
  9994. This means to empty the block, parse for parameters and then call
  9995. the correct writing function."
  9996. (interactive)
  9997. (save-window-excursion
  9998. (let* ((pos (point))
  9999. (line (org-current-line))
  10000. (params (org-prepare-dblock))
  10001. (name (plist-get params :name))
  10002. (indent (plist-get params :indentation-column))
  10003. (cmd (intern (concat "org-dblock-write:" name))))
  10004. (message "Updating dynamic block `%s' at line %d..." name line)
  10005. (funcall cmd params)
  10006. (message "Updating dynamic block `%s' at line %d...done" name line)
  10007. (goto-char pos)
  10008. (when (and indent (> indent 0))
  10009. (setq indent (make-string indent ?\ ))
  10010. (save-excursion
  10011. (org-beginning-of-dblock)
  10012. (forward-line 1)
  10013. (while (not (looking-at org-dblock-end-re))
  10014. (insert indent)
  10015. (beginning-of-line 2))
  10016. (when (looking-at org-dblock-end-re)
  10017. (and (looking-at "[ \t]+")
  10018. (replace-match ""))
  10019. (insert indent)))))))
  10020. (defun org-beginning-of-dblock ()
  10021. "Find the beginning of the dynamic block at point.
  10022. Error if there is no such block at point."
  10023. (let ((pos (point))
  10024. beg)
  10025. (end-of-line 1)
  10026. (if (and (re-search-backward org-dblock-start-re nil t)
  10027. (setq beg (match-beginning 0))
  10028. (re-search-forward org-dblock-end-re nil t)
  10029. (> (match-end 0) pos))
  10030. (goto-char beg)
  10031. (goto-char pos)
  10032. (error "Not in a dynamic block"))))
  10033. ;;;###autoload
  10034. (defun org-update-all-dblocks ()
  10035. "Update all dynamic blocks in the buffer.
  10036. This function can be used in a hook."
  10037. (interactive)
  10038. (when (derived-mode-p 'org-mode)
  10039. (org-map-dblocks 'org-update-dblock)))
  10040. ;;;; Completion
  10041. (defconst org-additional-option-like-keywords
  10042. '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML:"
  10043. "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook:"
  10044. "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:"
  10045. "LATEX_CLASS:" "LATEX_CLASS_OPTIONS:" "ATTR_LaTeX:"
  10046. "BEGIN:" "END:"
  10047. "ORGTBL" "TBLFM:" "TBLNAME:"
  10048. "BEGIN_EXAMPLE" "END_EXAMPLE"
  10049. "BEGIN_QUOTE" "END_QUOTE"
  10050. "BEGIN_VERSE" "END_VERSE"
  10051. "BEGIN_CENTER" "END_CENTER"
  10052. "BEGIN_SRC" "END_SRC"
  10053. "BEGIN_RESULT" "END_RESULT"
  10054. "NAME:" "RESULTS:"
  10055. "HEADER:" "HEADERS:"
  10056. "CATEGORY:" "COLUMNS:" "PROPERTY:"
  10057. "CAPTION:" "LABEL:"
  10058. "SETUPFILE:"
  10059. "INCLUDE:"
  10060. "BIND:"
  10061. "MACRO:"))
  10062. (defcustom org-structure-template-alist
  10063. '(
  10064. ("s" "#+BEGIN_SRC ?\n\n#+END_SRC"
  10065. "<src lang=\"?\">\n\n</src>")
  10066. ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE"
  10067. "<example>\n?\n</example>")
  10068. ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE"
  10069. "<quote>\n?\n</quote>")
  10070. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE"
  10071. "<verse>\n?\n</verse>")
  10072. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER"
  10073. "<center>\n?\n</center>")
  10074. ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX"
  10075. "<literal style=\"latex\">\n?\n</literal>")
  10076. ("L" "#+LaTeX: "
  10077. "<literal style=\"latex\">?</literal>")
  10078. ("h" "#+BEGIN_HTML\n?\n#+END_HTML"
  10079. "<literal style=\"html\">\n?\n</literal>")
  10080. ("H" "#+HTML: "
  10081. "<literal style=\"html\">?</literal>")
  10082. ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII")
  10083. ("A" "#+ASCII: ")
  10084. ("i" "#+INDEX: ?"
  10085. "#+INDEX: ?")
  10086. ("I" "#+INCLUDE %file ?"
  10087. "<include file=%file markup=\"?\">")
  10088. )
  10089. "Structure completion elements.
  10090. This is a list of abbreviation keys and values. The value gets inserted
  10091. if you type `<' followed by the key and then press the completion key,
  10092. usually `M-TAB'. %file will be replaced by a file name after prompting
  10093. for the file using completion. The cursor will be placed at the position
  10094. of the `?` in the template.
  10095. There are two templates for each key, the first uses the original Org syntax,
  10096. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  10097. the default when the /org-mtags.el/ module has been loaded. See also the
  10098. variable `org-mtags-prefer-muse-templates'."
  10099. :group 'org-completion
  10100. :type '(repeat
  10101. (string :tag "Key")
  10102. (string :tag "Template")
  10103. (string :tag "Muse Template")))
  10104. (defun org-try-structure-completion ()
  10105. "Try to complete a structure template before point.
  10106. This looks for strings like \"<e\" on an otherwise empty line and
  10107. expands them."
  10108. (let ((l (buffer-substring (point-at-bol) (point)))
  10109. a)
  10110. (when (and (looking-at "[ \t]*$")
  10111. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  10112. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  10113. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  10114. (match-beginning 1)) a)
  10115. t)))
  10116. (defun org-complete-expand-structure-template (start cell)
  10117. "Expand a structure template."
  10118. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  10119. (rpl (nth (if musep 2 1) cell))
  10120. (ind ""))
  10121. (delete-region start (point))
  10122. (when (string-match "\\`#\\+" rpl)
  10123. (cond
  10124. ((bolp))
  10125. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  10126. (setq ind (buffer-substring (point-at-bol) (point))))
  10127. (t (newline))))
  10128. (setq start (point))
  10129. (if (string-match "%file" rpl)
  10130. (setq rpl (replace-match
  10131. (concat
  10132. "\""
  10133. (save-match-data
  10134. (abbreviate-file-name (read-file-name "Include file: ")))
  10135. "\"")
  10136. t t rpl)))
  10137. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  10138. (concat "\n" ind)))
  10139. (insert rpl)
  10140. (if (re-search-backward "\\?" start t) (delete-char 1))))
  10141. ;;;; TODO, DEADLINE, Comments
  10142. (defun org-toggle-comment ()
  10143. "Change the COMMENT state of an entry."
  10144. (interactive)
  10145. (save-excursion
  10146. (org-back-to-heading)
  10147. (let (case-fold-search)
  10148. (cond
  10149. ((looking-at (format org-heading-keyword-regexp-format
  10150. org-comment-string))
  10151. (goto-char (match-end 1))
  10152. (looking-at (concat " +" org-comment-string))
  10153. (replace-match "" t t)
  10154. (when (eolp) (insert " ")))
  10155. ((looking-at org-outline-regexp)
  10156. (goto-char (match-end 0))
  10157. (insert org-comment-string " "))))))
  10158. (defvar org-last-todo-state-is-todo nil
  10159. "This is non-nil when the last TODO state change led to a TODO state.
  10160. If the last change removed the TODO tag or switched to DONE, then
  10161. this is nil.")
  10162. (defvar org-setting-tags nil) ; dynamically skipped
  10163. (defvar org-todo-setup-filter-hook nil
  10164. "Hook for functions that pre-filter todo specs.
  10165. Each function takes a todo spec and returns either nil or the spec
  10166. transformed into canonical form." )
  10167. (defvar org-todo-get-default-hook nil
  10168. "Hook for functions that get a default item for todo.
  10169. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  10170. nil or a string to be used for the todo mark." )
  10171. (defvar org-agenda-headline-snapshot-before-repeat)
  10172. (defun org-current-effective-time ()
  10173. "Return current time adjusted for `org-extend-today-until' variable"
  10174. (let* ((ct (org-current-time))
  10175. (dct (decode-time ct))
  10176. (ct1
  10177. (if (and org-use-effective-time
  10178. (< (nth 2 dct) org-extend-today-until))
  10179. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct))
  10180. ct)))
  10181. ct1))
  10182. (defun org-todo-yesterday (&optional arg)
  10183. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  10184. (interactive "P")
  10185. (if (eq major-mode 'org-agenda-mode)
  10186. (apply 'org-agenda-todo-yesterday arg)
  10187. (let* ((hour (third (decode-time
  10188. (org-current-time))))
  10189. (org-extend-today-until (1+ hour)))
  10190. (org-todo arg))))
  10191. (defun org-todo (&optional arg)
  10192. "Change the TODO state of an item.
  10193. The state of an item is given by a keyword at the start of the heading,
  10194. like
  10195. *** TODO Write paper
  10196. *** DONE Call mom
  10197. The different keywords are specified in the variable `org-todo-keywords'.
  10198. By default the available states are \"TODO\" and \"DONE\".
  10199. So for this example: when the item starts with TODO, it is changed to DONE.
  10200. When it starts with DONE, the DONE is removed. And when neither TODO nor
  10201. DONE are present, add TODO at the beginning of the heading.
  10202. With \\[universal-argument] prefix arg, use completion to determine the new \
  10203. state.
  10204. With numeric prefix arg, switch to that state.
  10205. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  10206. keywords (nextset).
  10207. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  10208. With a numeric prefix arg of 0, inhibit note taking for the change.
  10209. For calling through lisp, arg is also interpreted in the following way:
  10210. 'none -> empty state
  10211. \"\"(empty string) -> switch to empty state
  10212. 'done -> switch to DONE
  10213. 'nextset -> switch to the next set of keywords
  10214. 'previousset -> switch to the previous set of keywords
  10215. \"WAITING\" -> switch to the specified keyword, but only if it
  10216. really is a member of `org-todo-keywords'."
  10217. (interactive "P")
  10218. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  10219. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  10220. 'region-start-level 'region))
  10221. org-loop-over-headlines-in-active-region)
  10222. (org-map-entries
  10223. `(org-todo ,arg)
  10224. org-loop-over-headlines-in-active-region
  10225. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  10226. (if (equal arg '(16)) (setq arg 'nextset))
  10227. (let ((org-blocker-hook org-blocker-hook)
  10228. (case-fold-search nil))
  10229. (when (equal arg '(64))
  10230. (setq arg nil org-blocker-hook nil))
  10231. (when (and org-blocker-hook
  10232. (or org-inhibit-blocking
  10233. (org-entry-get nil "NOBLOCKING")))
  10234. (setq org-blocker-hook nil))
  10235. (save-excursion
  10236. (catch 'exit
  10237. (org-back-to-heading t)
  10238. (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  10239. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  10240. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  10241. (let* ((match-data (match-data))
  10242. (startpos (point-at-bol))
  10243. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  10244. (org-log-done org-log-done)
  10245. (org-log-repeat org-log-repeat)
  10246. (org-todo-log-states org-todo-log-states)
  10247. (org-inhibit-logging
  10248. (if (equal arg 0)
  10249. (progn (setq arg nil) 'note) org-inhibit-logging))
  10250. (this (match-string 1))
  10251. (hl-pos (match-beginning 0))
  10252. (head (org-get-todo-sequence-head this))
  10253. (ass (assoc head org-todo-kwd-alist))
  10254. (interpret (nth 1 ass))
  10255. (done-word (nth 3 ass))
  10256. (final-done-word (nth 4 ass))
  10257. (org-last-state (or this ""))
  10258. (completion-ignore-case t)
  10259. (member (member this org-todo-keywords-1))
  10260. (tail (cdr member))
  10261. (org-state (cond
  10262. ((and org-todo-key-trigger
  10263. (or (and (equal arg '(4))
  10264. (eq org-use-fast-todo-selection 'prefix))
  10265. (and (not arg) org-use-fast-todo-selection
  10266. (not (eq org-use-fast-todo-selection
  10267. 'prefix)))))
  10268. ;; Use fast selection
  10269. (org-fast-todo-selection))
  10270. ((and (equal arg '(4))
  10271. (or (not org-use-fast-todo-selection)
  10272. (not org-todo-key-trigger)))
  10273. ;; Read a state with completion
  10274. (org-icompleting-read
  10275. "State: " (mapcar (lambda(x) (list x))
  10276. org-todo-keywords-1)
  10277. nil t))
  10278. ((eq arg 'right)
  10279. (if this
  10280. (if tail (car tail) nil)
  10281. (car org-todo-keywords-1)))
  10282. ((eq arg 'left)
  10283. (if (equal member org-todo-keywords-1)
  10284. nil
  10285. (if this
  10286. (nth (- (length org-todo-keywords-1)
  10287. (length tail) 2)
  10288. org-todo-keywords-1)
  10289. (org-last org-todo-keywords-1))))
  10290. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  10291. (setq arg nil))) ; hack to fall back to cycling
  10292. (arg
  10293. ;; user or caller requests a specific state
  10294. (cond
  10295. ((equal arg "") nil)
  10296. ((eq arg 'none) nil)
  10297. ((eq arg 'done) (or done-word (car org-done-keywords)))
  10298. ((eq arg 'nextset)
  10299. (or (car (cdr (member head org-todo-heads)))
  10300. (car org-todo-heads)))
  10301. ((eq arg 'previousset)
  10302. (let ((org-todo-heads (reverse org-todo-heads)))
  10303. (or (car (cdr (member head org-todo-heads)))
  10304. (car org-todo-heads))))
  10305. ((car (member arg org-todo-keywords-1)))
  10306. ((stringp arg)
  10307. (error "State `%s' not valid in this file" arg))
  10308. ((nth (1- (prefix-numeric-value arg))
  10309. org-todo-keywords-1))))
  10310. ((null member) (or head (car org-todo-keywords-1)))
  10311. ((equal this final-done-word) nil) ;; -> make empty
  10312. ((null tail) nil) ;; -> first entry
  10313. ((memq interpret '(type priority))
  10314. (if (eq this-command last-command)
  10315. (car tail)
  10316. (if (> (length tail) 0)
  10317. (or done-word (car org-done-keywords))
  10318. nil)))
  10319. (t
  10320. (car tail))))
  10321. (org-state (or
  10322. (run-hook-with-args-until-success
  10323. 'org-todo-get-default-hook org-state org-last-state)
  10324. org-state))
  10325. (next (if org-state (concat " " org-state " ") " "))
  10326. (change-plist (list :type 'todo-state-change :from this :to org-state
  10327. :position startpos))
  10328. dolog now-done-p)
  10329. (when org-blocker-hook
  10330. (setq org-last-todo-state-is-todo
  10331. (not (member this org-done-keywords)))
  10332. (unless (save-excursion
  10333. (save-match-data
  10334. (org-with-wide-buffer
  10335. (run-hook-with-args-until-failure
  10336. 'org-blocker-hook change-plist))))
  10337. (if (org-called-interactively-p 'interactive)
  10338. (error "TODO state change from %s to %s blocked" this org-state)
  10339. ;; fail silently
  10340. (message "TODO state change from %s to %s blocked" this org-state)
  10341. (throw 'exit nil))))
  10342. (store-match-data match-data)
  10343. (replace-match next t t)
  10344. (unless (pos-visible-in-window-p hl-pos)
  10345. (message "TODO state changed to %s" (org-trim next)))
  10346. (unless head
  10347. (setq head (org-get-todo-sequence-head org-state)
  10348. ass (assoc head org-todo-kwd-alist)
  10349. interpret (nth 1 ass)
  10350. done-word (nth 3 ass)
  10351. final-done-word (nth 4 ass)))
  10352. (when (memq arg '(nextset previousset))
  10353. (message "Keyword-Set %d/%d: %s"
  10354. (- (length org-todo-sets) -1
  10355. (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
  10356. (length org-todo-sets)
  10357. (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
  10358. (setq org-last-todo-state-is-todo
  10359. (not (member org-state org-done-keywords)))
  10360. (setq now-done-p (and (member org-state org-done-keywords)
  10361. (not (member this org-done-keywords))))
  10362. (and logging (org-local-logging logging))
  10363. (when (and (or org-todo-log-states org-log-done)
  10364. (not (eq org-inhibit-logging t))
  10365. (not (memq arg '(nextset previousset))))
  10366. ;; we need to look at recording a time and note
  10367. (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
  10368. (nth 2 (assoc this org-todo-log-states))))
  10369. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  10370. (setq dolog 'time))
  10371. (when (and org-state
  10372. (member org-state org-not-done-keywords)
  10373. (not (member this org-not-done-keywords)))
  10374. ;; This is now a todo state and was not one before
  10375. ;; If there was a CLOSED time stamp, get rid of it.
  10376. (org-add-planning-info nil nil 'closed))
  10377. (when (and now-done-p org-log-done)
  10378. ;; It is now done, and it was not done before
  10379. (org-add-planning-info 'closed (org-current-effective-time))
  10380. (if (and (not dolog) (eq 'note org-log-done))
  10381. (org-add-log-setup 'done org-state this 'findpos 'note)))
  10382. (when (and org-state dolog)
  10383. ;; This is a non-nil state, and we need to log it
  10384. (org-add-log-setup 'state org-state this 'findpos dolog)))
  10385. ;; Fixup tag positioning
  10386. (org-todo-trigger-tag-changes org-state)
  10387. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  10388. (when org-provide-todo-statistics
  10389. (org-update-parent-todo-statistics))
  10390. (run-hooks 'org-after-todo-state-change-hook)
  10391. (if (and arg (not (member org-state org-done-keywords)))
  10392. (setq head (org-get-todo-sequence-head org-state)))
  10393. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  10394. ;; Do we need to trigger a repeat?
  10395. (when now-done-p
  10396. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  10397. ;; This is for the agenda, take a snapshot of the headline.
  10398. (save-match-data
  10399. (setq org-agenda-headline-snapshot-before-repeat
  10400. (org-get-heading))))
  10401. (org-auto-repeat-maybe org-state))
  10402. ;; Fixup cursor location if close to the keyword
  10403. (if (and (outline-on-heading-p)
  10404. (not (bolp))
  10405. (save-excursion (beginning-of-line 1)
  10406. (looking-at org-todo-line-regexp))
  10407. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  10408. (progn
  10409. (goto-char (or (match-end 2) (match-end 1)))
  10410. (and (looking-at " ") (just-one-space))))
  10411. (when org-trigger-hook
  10412. (save-excursion
  10413. (run-hook-with-args 'org-trigger-hook change-plist)))))))))
  10414. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  10415. "Block turning an entry into a TODO, using the hierarchy.
  10416. This checks whether the current task should be blocked from state
  10417. changes. Such blocking occurs when:
  10418. 1. The task has children which are not all in a completed state.
  10419. 2. A task has a parent with the property :ORDERED:, and there
  10420. are siblings prior to the current task with incomplete
  10421. status.
  10422. 3. The parent of the task is blocked because it has siblings that should
  10423. be done first, or is child of a block grandparent TODO entry."
  10424. (if (not org-enforce-todo-dependencies)
  10425. t ; if locally turned off don't block
  10426. (catch 'dont-block
  10427. ;; If this is not a todo state change, or if this entry is already DONE,
  10428. ;; do not block
  10429. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10430. (member (plist-get change-plist :from)
  10431. (cons 'done org-done-keywords))
  10432. (member (plist-get change-plist :to)
  10433. (cons 'todo org-not-done-keywords))
  10434. (not (plist-get change-plist :to)))
  10435. (throw 'dont-block t))
  10436. ;; If this task has children, and any are undone, it's blocked
  10437. (save-excursion
  10438. (org-back-to-heading t)
  10439. (let ((this-level (funcall outline-level)))
  10440. (outline-next-heading)
  10441. (let ((child-level (funcall outline-level)))
  10442. (while (and (not (eobp))
  10443. (> child-level this-level))
  10444. ;; this todo has children, check whether they are all
  10445. ;; completed
  10446. (if (and (not (org-entry-is-done-p))
  10447. (org-entry-is-todo-p))
  10448. (throw 'dont-block nil))
  10449. (outline-next-heading)
  10450. (setq child-level (funcall outline-level))))))
  10451. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  10452. ;; any previous siblings are undone, it's blocked
  10453. (save-excursion
  10454. (org-back-to-heading t)
  10455. (let* ((pos (point))
  10456. (parent-pos (and (org-up-heading-safe) (point))))
  10457. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10458. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10459. (forward-line 1)
  10460. (re-search-forward org-not-done-heading-regexp pos t))
  10461. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  10462. ;; Search further up the hierarchy, to see if an ancestor is blocked
  10463. (while t
  10464. (goto-char parent-pos)
  10465. (if (not (looking-at org-not-done-heading-regexp))
  10466. (throw 'dont-block t)) ; do not block, parent is not a TODO
  10467. (setq pos (point))
  10468. (setq parent-pos (and (org-up-heading-safe) (point)))
  10469. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10470. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10471. (forward-line 1)
  10472. (re-search-forward org-not-done-heading-regexp pos t))
  10473. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  10474. (defcustom org-track-ordered-property-with-tag nil
  10475. "Should the ORDERED property also be shown as a tag?
  10476. The ORDERED property decides if an entry should require subtasks to be
  10477. completed in sequence. Since a property is not very visible, setting
  10478. this option means that toggling the ORDERED property with the command
  10479. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  10480. not relevant for the behavior, but it makes things more visible.
  10481. Note that toggling the tag with tags commands will not change the property
  10482. and therefore not influence behavior!
  10483. This can be t, meaning the tag ORDERED should be used, It can also be a
  10484. string to select a different tag for this task."
  10485. :group 'org-todo
  10486. :type '(choice
  10487. (const :tag "No tracking" nil)
  10488. (const :tag "Track with ORDERED tag" t)
  10489. (string :tag "Use other tag")))
  10490. (defun org-toggle-ordered-property ()
  10491. "Toggle the ORDERED property of the current entry.
  10492. For better visibility, you can track the value of this property with a tag.
  10493. See variable `org-track-ordered-property-with-tag'."
  10494. (interactive)
  10495. (let* ((t1 org-track-ordered-property-with-tag)
  10496. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  10497. (save-excursion
  10498. (org-back-to-heading)
  10499. (if (org-entry-get nil "ORDERED")
  10500. (progn
  10501. (org-delete-property "ORDERED")
  10502. (and tag (org-toggle-tag tag 'off))
  10503. (message "Subtasks can be completed in arbitrary order"))
  10504. (org-entry-put nil "ORDERED" "t")
  10505. (and tag (org-toggle-tag tag 'on))
  10506. (message "Subtasks must be completed in sequence")))))
  10507. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  10508. (defun org-block-todo-from-checkboxes (change-plist)
  10509. "Block turning an entry into a TODO, using checkboxes.
  10510. This checks whether the current task should be blocked from state
  10511. changes because there are unchecked boxes in this entry."
  10512. (if (not org-enforce-todo-checkbox-dependencies)
  10513. t ; if locally turned off don't block
  10514. (catch 'dont-block
  10515. ;; If this is not a todo state change, or if this entry is already DONE,
  10516. ;; do not block
  10517. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10518. (member (plist-get change-plist :from)
  10519. (cons 'done org-done-keywords))
  10520. (member (plist-get change-plist :to)
  10521. (cons 'todo org-not-done-keywords))
  10522. (not (plist-get change-plist :to)))
  10523. (throw 'dont-block t))
  10524. ;; If this task has checkboxes that are not checked, it's blocked
  10525. (save-excursion
  10526. (org-back-to-heading t)
  10527. (let ((beg (point)) end)
  10528. (outline-next-heading)
  10529. (setq end (point))
  10530. (goto-char beg)
  10531. (if (org-list-search-forward
  10532. (concat (org-item-beginning-re)
  10533. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  10534. "\\[[- ]\\]")
  10535. end t)
  10536. (progn
  10537. (if (boundp 'org-blocked-by-checkboxes)
  10538. (setq org-blocked-by-checkboxes t))
  10539. (throw 'dont-block nil)))))
  10540. t))) ; do not block
  10541. (defun org-entry-blocked-p ()
  10542. "Is the current entry blocked?"
  10543. (if (org-entry-get nil "NOBLOCKING")
  10544. nil ;; Never block this entry
  10545. (not
  10546. (run-hook-with-args-until-failure
  10547. 'org-blocker-hook
  10548. (list :type 'todo-state-change
  10549. :position (point)
  10550. :from 'todo
  10551. :to 'done)))))
  10552. (defun org-update-statistics-cookies (all)
  10553. "Update the statistics cookie, either from TODO or from checkboxes.
  10554. This should be called with the cursor in a line with a statistics cookie."
  10555. (interactive "P")
  10556. (if all
  10557. (progn
  10558. (org-update-checkbox-count 'all)
  10559. (org-map-entries 'org-update-parent-todo-statistics))
  10560. (if (not (org-at-heading-p))
  10561. (org-update-checkbox-count)
  10562. (let ((pos (move-marker (make-marker) (point)))
  10563. end l1 l2)
  10564. (ignore-errors (org-back-to-heading t))
  10565. (if (not (org-at-heading-p))
  10566. (org-update-checkbox-count)
  10567. (setq l1 (org-outline-level))
  10568. (setq end (save-excursion
  10569. (outline-next-heading)
  10570. (if (org-at-heading-p) (setq l2 (org-outline-level)))
  10571. (point)))
  10572. (if (and (save-excursion
  10573. (re-search-forward
  10574. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  10575. (not (save-excursion (re-search-forward
  10576. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  10577. (org-update-checkbox-count)
  10578. (if (and l2 (> l2 l1))
  10579. (progn
  10580. (goto-char end)
  10581. (org-update-parent-todo-statistics))
  10582. (goto-char pos)
  10583. (beginning-of-line 1)
  10584. (while (re-search-forward
  10585. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  10586. (point-at-eol) t)
  10587. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  10588. (goto-char pos)
  10589. (move-marker pos nil)))))
  10590. (defvar org-entry-property-inherited-from) ;; defined below
  10591. (defun org-update-parent-todo-statistics ()
  10592. "Update any statistics cookie in the parent of the current headline.
  10593. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  10594. the entire subtree and this will travel up the hierarchy and update
  10595. statistics everywhere."
  10596. (let* ((prop (save-excursion (org-up-heading-safe)
  10597. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  10598. (recursive (or (not org-hierarchical-todo-statistics)
  10599. (and prop (string-match "\\<recursive\\>" prop))))
  10600. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  10601. 0))
  10602. (first t)
  10603. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  10604. level ltoggle l1 new ndel
  10605. (cnt-all 0) (cnt-done 0) is-percent kwd
  10606. checkbox-beg ov ovs ove cookie-present)
  10607. (catch 'exit
  10608. (save-excursion
  10609. (beginning-of-line 1)
  10610. (setq ltoggle (funcall outline-level))
  10611. ;; Three situations are to consider:
  10612. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  10613. ;; to the top-level ancestor on the headline;
  10614. ;; 2. If parent has "recursive" property, repeat up to the
  10615. ;; headline setting that property, taking inheritance into
  10616. ;; account;
  10617. ;; 3. Else, move up to direct parent and proceed only once.
  10618. (while (and (setq level (org-up-heading-safe))
  10619. (or recursive first)
  10620. (>= (point) lim))
  10621. (setq first nil cookie-present nil)
  10622. (unless (and level
  10623. (not (string-match
  10624. "\\<checkbox\\>"
  10625. (downcase (or (org-entry-get nil "COOKIE_DATA")
  10626. "")))))
  10627. (throw 'exit nil))
  10628. (while (re-search-forward box-re (point-at-eol) t)
  10629. (setq cnt-all 0 cnt-done 0 cookie-present t)
  10630. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  10631. (save-match-data
  10632. (unless (outline-next-heading) (throw 'exit nil))
  10633. (while (and (looking-at org-complex-heading-regexp)
  10634. (> (setq l1 (length (match-string 1))) level))
  10635. (setq kwd (and (or recursive (= l1 ltoggle))
  10636. (match-string 2)))
  10637. (if (or (eq org-provide-todo-statistics 'all-headlines)
  10638. (and (listp org-provide-todo-statistics)
  10639. (or (member kwd org-provide-todo-statistics)
  10640. (member kwd org-done-keywords))))
  10641. (setq cnt-all (1+ cnt-all))
  10642. (if (eq org-provide-todo-statistics t)
  10643. (and kwd (setq cnt-all (1+ cnt-all)))))
  10644. (and (member kwd org-done-keywords)
  10645. (setq cnt-done (1+ cnt-done)))
  10646. (outline-next-heading)))
  10647. (setq new
  10648. (if is-percent
  10649. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  10650. (format "[%d/%d]" cnt-done cnt-all))
  10651. ndel (- (match-end 0) checkbox-beg))
  10652. ;; handle overlays when updating cookie from column view
  10653. (when (setq ov (car (overlays-at checkbox-beg)))
  10654. (setq ovs (overlay-start ov) ove (overlay-end ov))
  10655. (delete-overlay ov))
  10656. (goto-char checkbox-beg)
  10657. (insert new)
  10658. (delete-region (point) (+ (point) ndel))
  10659. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  10660. (when ov (move-overlay ov ovs ove)))
  10661. (when cookie-present
  10662. (run-hook-with-args 'org-after-todo-statistics-hook
  10663. cnt-done (- cnt-all cnt-done))))))
  10664. (run-hooks 'org-todo-statistics-hook)))
  10665. (defvar org-after-todo-statistics-hook nil
  10666. "Hook that is called after a TODO statistics cookie has been updated.
  10667. Each function is called with two arguments: the number of not-done entries
  10668. and the number of done entries.
  10669. For example, the following function, when added to this hook, will switch
  10670. an entry to DONE when all children are done, and back to TODO when new
  10671. entries are set to a TODO status. Note that this hook is only called
  10672. when there is a statistics cookie in the headline!
  10673. (defun org-summary-todo (n-done n-not-done)
  10674. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  10675. (let (org-log-done org-log-states) ; turn off logging
  10676. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  10677. ")
  10678. (defvar org-todo-statistics-hook nil
  10679. "Hook that is run whenever Org thinks TODO statistics should be updated.
  10680. This hook runs even if there is no statistics cookie present, in which case
  10681. `org-after-todo-statistics-hook' would not run.")
  10682. (defun org-todo-trigger-tag-changes (state)
  10683. "Apply the changes defined in `org-todo-state-tags-triggers'."
  10684. (let ((l org-todo-state-tags-triggers)
  10685. changes)
  10686. (when (or (not state) (equal state ""))
  10687. (setq changes (append changes (cdr (assoc "" l)))))
  10688. (when (and (stringp state) (> (length state) 0))
  10689. (setq changes (append changes (cdr (assoc state l)))))
  10690. (when (member state org-not-done-keywords)
  10691. (setq changes (append changes (cdr (assoc 'todo l)))))
  10692. (when (member state org-done-keywords)
  10693. (setq changes (append changes (cdr (assoc 'done l)))))
  10694. (dolist (c changes)
  10695. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  10696. (defun org-local-logging (value)
  10697. "Get logging settings from a property VALUE."
  10698. (let* (words w a)
  10699. ;; directly set the variables, they are already local.
  10700. (setq org-log-done nil
  10701. org-log-repeat nil
  10702. org-todo-log-states nil)
  10703. (setq words (org-split-string value))
  10704. (while (setq w (pop words))
  10705. (cond
  10706. ((setq a (assoc w org-startup-options))
  10707. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  10708. (set (nth 1 a) (nth 2 a))))
  10709. ((setq a (org-extract-log-state-settings w))
  10710. (and (member (car a) org-todo-keywords-1)
  10711. (push a org-todo-log-states)))))))
  10712. (defun org-get-todo-sequence-head (kwd)
  10713. "Return the head of the TODO sequence to which KWD belongs.
  10714. If KWD is not set, check if there is a text property remembering the
  10715. right sequence."
  10716. (let (p)
  10717. (cond
  10718. ((not kwd)
  10719. (or (get-text-property (point-at-bol) 'org-todo-head)
  10720. (progn
  10721. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  10722. nil (point-at-eol)))
  10723. (get-text-property p 'org-todo-head))))
  10724. ((not (member kwd org-todo-keywords-1))
  10725. (car org-todo-keywords-1))
  10726. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  10727. (defun org-fast-todo-selection ()
  10728. "Fast TODO keyword selection with single keys.
  10729. Returns the new TODO keyword, or nil if no state change should occur."
  10730. (let* ((fulltable org-todo-key-alist)
  10731. (done-keywords org-done-keywords) ;; needed for the faces.
  10732. (maxlen (apply 'max (mapcar
  10733. (lambda (x)
  10734. (if (stringp (car x)) (string-width (car x)) 0))
  10735. fulltable)))
  10736. (expert nil)
  10737. (fwidth (+ maxlen 3 1 3))
  10738. (ncol (/ (- (window-width) 4) fwidth))
  10739. tg cnt e c tbl
  10740. groups ingroup)
  10741. (save-excursion
  10742. (save-window-excursion
  10743. (if expert
  10744. (set-buffer (get-buffer-create " *Org todo*"))
  10745. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  10746. (erase-buffer)
  10747. (org-set-local 'org-done-keywords done-keywords)
  10748. (setq tbl fulltable cnt 0)
  10749. (while (setq e (pop tbl))
  10750. (cond
  10751. ((equal e '(:startgroup))
  10752. (push '() groups) (setq ingroup t)
  10753. (when (not (= cnt 0))
  10754. (setq cnt 0)
  10755. (insert "\n"))
  10756. (insert "{ "))
  10757. ((equal e '(:endgroup))
  10758. (setq ingroup nil cnt 0)
  10759. (insert "}\n"))
  10760. ((equal e '(:newline))
  10761. (when (not (= cnt 0))
  10762. (setq cnt 0)
  10763. (insert "\n")
  10764. (setq e (car tbl))
  10765. (while (equal (car tbl) '(:newline))
  10766. (insert "\n")
  10767. (setq tbl (cdr tbl)))))
  10768. (t
  10769. (setq tg (car e) c (cdr e))
  10770. (if ingroup (push tg (car groups)))
  10771. (setq tg (org-add-props tg nil 'face
  10772. (org-get-todo-face tg)))
  10773. (if (and (= cnt 0) (not ingroup)) (insert " "))
  10774. (insert "[" c "] " tg (make-string
  10775. (- fwidth 4 (length tg)) ?\ ))
  10776. (when (= (setq cnt (1+ cnt)) ncol)
  10777. (insert "\n")
  10778. (if ingroup (insert " "))
  10779. (setq cnt 0)))))
  10780. (insert "\n")
  10781. (goto-char (point-min))
  10782. (if (not expert) (org-fit-window-to-buffer))
  10783. (message "[a-z..]:Set [SPC]:clear")
  10784. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  10785. (cond
  10786. ((or (= c ?\C-g)
  10787. (and (= c ?q) (not (rassoc c fulltable))))
  10788. (setq quit-flag t))
  10789. ((= c ?\ ) nil)
  10790. ((setq e (rassoc c fulltable) tg (car e))
  10791. tg)
  10792. (t (setq quit-flag t)))))))
  10793. (defun org-entry-is-todo-p ()
  10794. (member (org-get-todo-state) org-not-done-keywords))
  10795. (defun org-entry-is-done-p ()
  10796. (member (org-get-todo-state) org-done-keywords))
  10797. (defun org-get-todo-state ()
  10798. (save-excursion
  10799. (org-back-to-heading t)
  10800. (and (looking-at org-todo-line-regexp)
  10801. (match-end 2)
  10802. (match-string 2))))
  10803. (defun org-at-date-range-p (&optional inactive-ok)
  10804. "Is the cursor inside a date range?"
  10805. (interactive)
  10806. (save-excursion
  10807. (catch 'exit
  10808. (let ((pos (point)))
  10809. (skip-chars-backward "^[<\r\n")
  10810. (skip-chars-backward "<[")
  10811. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  10812. (>= (match-end 0) pos)
  10813. (throw 'exit t))
  10814. (skip-chars-backward "^<[\r\n")
  10815. (skip-chars-backward "<[")
  10816. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  10817. (>= (match-end 0) pos)
  10818. (throw 'exit t)))
  10819. nil)))
  10820. (defun org-get-repeat (&optional tagline)
  10821. "Check if there is a deadline/schedule with repeater in this entry."
  10822. (save-match-data
  10823. (save-excursion
  10824. (org-back-to-heading t)
  10825. (and (re-search-forward (if tagline
  10826. (concat tagline "\\s-*" org-repeat-re)
  10827. org-repeat-re)
  10828. (org-entry-end-position) t)
  10829. (match-string-no-properties 1)))))
  10830. (defvar org-last-changed-timestamp)
  10831. (defvar org-last-inserted-timestamp)
  10832. (defvar org-log-post-message)
  10833. (defvar org-log-note-purpose)
  10834. (defvar org-log-note-how)
  10835. (defvar org-log-note-extra)
  10836. (defun org-auto-repeat-maybe (done-word)
  10837. "Check if the current headline contains a repeated deadline/schedule.
  10838. If yes, set TODO state back to what it was and change the base date
  10839. of repeating deadline/scheduled time stamps to new date.
  10840. This function is run automatically after each state change to a DONE state."
  10841. ;; last-state is dynamically scoped into this function
  10842. (let* ((repeat (org-get-repeat))
  10843. (aa (assoc org-last-state org-todo-kwd-alist))
  10844. (interpret (nth 1 aa))
  10845. (head (nth 2 aa))
  10846. (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
  10847. (msg "Entry repeats: ")
  10848. (org-log-done nil)
  10849. (org-todo-log-states nil)
  10850. re type n what ts time to-state)
  10851. (when repeat
  10852. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  10853. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  10854. org-todo-repeat-to-state))
  10855. (unless (and to-state (member to-state org-todo-keywords-1))
  10856. (setq to-state (if (eq interpret 'type) org-last-state head)))
  10857. (org-todo to-state)
  10858. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  10859. (org-entry-put nil "LAST_REPEAT" (format-time-string
  10860. (org-time-stamp-format t t))))
  10861. (when org-log-repeat
  10862. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  10863. (memq 'org-add-log-note post-command-hook))
  10864. ;; OK, we are already setup for some record
  10865. (if (eq org-log-repeat 'note)
  10866. ;; make sure we take a note, not only a time stamp
  10867. (setq org-log-note-how 'note))
  10868. ;; Set up for taking a record
  10869. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  10870. org-last-state
  10871. 'findpos org-log-repeat)))
  10872. (org-back-to-heading t)
  10873. (org-add-planning-info nil nil 'closed)
  10874. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  10875. org-deadline-time-regexp "\\)\\|\\("
  10876. org-ts-regexp "\\)"))
  10877. (while (re-search-forward
  10878. re (save-excursion (outline-next-heading) (point)) t)
  10879. (setq type (if (match-end 1) org-scheduled-string
  10880. (if (match-end 3) org-deadline-string "Plain:"))
  10881. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  10882. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts)
  10883. (setq n (string-to-number (match-string 2 ts))
  10884. what (match-string 3 ts))
  10885. (if (equal what "w") (setq n (* n 7) what "d"))
  10886. (if (and (equal what "h") (not (string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts)))
  10887. (error "Cannot repeat in Repeat in %d hour(s) because no hour has been set" n))
  10888. ;; Preparation, see if we need to modify the start date for the change
  10889. (when (match-end 1)
  10890. (setq time (save-match-data (org-time-string-to-time ts)))
  10891. (cond
  10892. ((equal (match-string 1 ts) ".")
  10893. ;; Shift starting date to today
  10894. (org-timestamp-change
  10895. (- (org-today) (time-to-days time))
  10896. 'day))
  10897. ((equal (match-string 1 ts) "+")
  10898. (let ((nshiftmax 10) (nshift 0))
  10899. (while (or (= nshift 0)
  10900. (<= (time-to-days time)
  10901. (time-to-days (current-time))))
  10902. (when (= (incf nshift) nshiftmax)
  10903. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  10904. (error "Abort")))
  10905. (org-timestamp-change n (cdr (assoc what whata)))
  10906. (org-at-timestamp-p t)
  10907. (setq ts (match-string 1))
  10908. (setq time (save-match-data (org-time-string-to-time ts)))))
  10909. (org-timestamp-change (- n) (cdr (assoc what whata)))
  10910. ;; rematch, so that we have everything in place for the real shift
  10911. (org-at-timestamp-p t)
  10912. (setq ts (match-string 1))
  10913. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))))
  10914. (org-timestamp-change n (cdr (assoc what whata)))
  10915. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  10916. (setq org-log-post-message msg)
  10917. (message "%s" msg))))
  10918. (defun org-show-todo-tree (arg)
  10919. "Make a compact tree which shows all headlines marked with TODO.
  10920. The tree will show the lines where the regexp matches, and all higher
  10921. headlines above the match.
  10922. With a \\[universal-argument] prefix, prompt for a regexp to match.
  10923. With a numeric prefix N, construct a sparse tree for the Nth element
  10924. of `org-todo-keywords-1'."
  10925. (interactive "P")
  10926. (let ((case-fold-search nil)
  10927. (kwd-re
  10928. (cond ((null arg) org-not-done-regexp)
  10929. ((equal arg '(4))
  10930. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  10931. (mapcar 'list org-todo-keywords-1))))
  10932. (concat "\\("
  10933. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  10934. "\\)\\>")))
  10935. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  10936. (regexp-quote (nth (1- (prefix-numeric-value arg))
  10937. org-todo-keywords-1)))
  10938. (t (error "Invalid prefix argument: %s" arg)))))
  10939. (message "%d TODO entries found"
  10940. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  10941. (defun org-deadline (&optional remove time)
  10942. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  10943. With argument REMOVE, remove any deadline from the item.
  10944. With argument TIME, set the deadline at the corresponding date. TIME
  10945. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  10946. (interactive "P")
  10947. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  10948. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  10949. 'region-start-level 'region))
  10950. org-loop-over-headlines-in-active-region)
  10951. (org-map-entries
  10952. `(org-deadline ',remove ,time)
  10953. org-loop-over-headlines-in-active-region
  10954. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  10955. (let* ((old-date (org-entry-get nil "DEADLINE"))
  10956. (repeater (and old-date
  10957. (string-match
  10958. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  10959. old-date)
  10960. (match-string 1 old-date))))
  10961. (if remove
  10962. (progn
  10963. (when (and old-date org-log-redeadline)
  10964. (org-add-log-setup 'deldeadline nil old-date 'findpos
  10965. org-log-redeadline))
  10966. (org-remove-timestamp-with-keyword org-deadline-string)
  10967. (message "Item no longer has a deadline."))
  10968. (org-add-planning-info 'deadline time 'closed)
  10969. (when (and old-date org-log-redeadline
  10970. (not (equal old-date
  10971. (substring org-last-inserted-timestamp 1 -1))))
  10972. (org-add-log-setup 'redeadline nil old-date 'findpos
  10973. org-log-redeadline))
  10974. (when repeater
  10975. (save-excursion
  10976. (org-back-to-heading t)
  10977. (when (re-search-forward (concat org-deadline-string " "
  10978. org-last-inserted-timestamp)
  10979. (save-excursion
  10980. (outline-next-heading) (point)) t)
  10981. (goto-char (1- (match-end 0)))
  10982. (insert " " repeater)
  10983. (setq org-last-inserted-timestamp
  10984. (concat (substring org-last-inserted-timestamp 0 -1)
  10985. " " repeater
  10986. (substring org-last-inserted-timestamp -1))))))
  10987. (message "Deadline on %s" org-last-inserted-timestamp)))))
  10988. (defun org-schedule (&optional remove time)
  10989. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  10990. With argument REMOVE, remove any scheduling date from the item.
  10991. With argument TIME, scheduled at the corresponding date. TIME can
  10992. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  10993. (interactive "P")
  10994. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  10995. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  10996. 'region-start-level 'region))
  10997. org-loop-over-headlines-in-active-region)
  10998. (org-map-entries
  10999. `(org-schedule ',remove ,time)
  11000. org-loop-over-headlines-in-active-region
  11001. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11002. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  11003. (repeater (and old-date
  11004. (string-match
  11005. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11006. old-date)
  11007. (match-string 1 old-date))))
  11008. (if remove
  11009. (progn
  11010. (when (and old-date org-log-reschedule)
  11011. (org-add-log-setup 'delschedule nil old-date 'findpos
  11012. org-log-reschedule))
  11013. (org-remove-timestamp-with-keyword org-scheduled-string)
  11014. (message "Item is no longer scheduled."))
  11015. (org-add-planning-info 'scheduled time 'closed)
  11016. (when (and old-date org-log-reschedule
  11017. (not (equal old-date
  11018. (substring org-last-inserted-timestamp 1 -1))))
  11019. (org-add-log-setup 'reschedule nil old-date 'findpos
  11020. org-log-reschedule))
  11021. (when repeater
  11022. (save-excursion
  11023. (org-back-to-heading t)
  11024. (when (re-search-forward (concat org-scheduled-string " "
  11025. org-last-inserted-timestamp)
  11026. (save-excursion
  11027. (outline-next-heading) (point)) t)
  11028. (goto-char (1- (match-end 0)))
  11029. (insert " " repeater)
  11030. (setq org-last-inserted-timestamp
  11031. (concat (substring org-last-inserted-timestamp 0 -1)
  11032. " " repeater
  11033. (substring org-last-inserted-timestamp -1))))))
  11034. (message "Scheduled to %s" org-last-inserted-timestamp)))))
  11035. (defun org-get-scheduled-time (pom &optional inherit)
  11036. "Get the scheduled time as a time tuple, of a format suitable
  11037. for calling org-schedule with, or if there is no scheduling,
  11038. returns nil."
  11039. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  11040. (when time
  11041. (apply 'encode-time (org-parse-time-string time)))))
  11042. (defun org-get-deadline-time (pom &optional inherit)
  11043. "Get the deadline as a time tuple, of a format suitable for
  11044. calling org-deadline with, or if there is no scheduling, returns
  11045. nil."
  11046. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  11047. (when time
  11048. (apply 'encode-time (org-parse-time-string time)))))
  11049. (defun org-remove-timestamp-with-keyword (keyword)
  11050. "Remove all time stamps with KEYWORD in the current entry."
  11051. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  11052. beg)
  11053. (save-excursion
  11054. (org-back-to-heading t)
  11055. (setq beg (point))
  11056. (outline-next-heading)
  11057. (while (re-search-backward re beg t)
  11058. (replace-match "")
  11059. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  11060. (equal (char-before) ?\ ))
  11061. (backward-delete-char 1)
  11062. (if (string-match "^[ \t]*$" (buffer-substring
  11063. (point-at-bol) (point-at-eol)))
  11064. (delete-region (point-at-bol)
  11065. (min (point-max) (1+ (point-at-eol))))))))))
  11066. (defun org-add-planning-info (what &optional time &rest remove)
  11067. "Insert new timestamp with keyword in the line directly after the headline.
  11068. WHAT indicates what kind of time stamp to add. TIME indicates the time to use.
  11069. If non is given, the user is prompted for a date.
  11070. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  11071. be removed."
  11072. (interactive)
  11073. (let (org-time-was-given org-end-time-was-given ts
  11074. end default-time default-input)
  11075. (catch 'exit
  11076. (when (and (memq what '(scheduled deadline))
  11077. (or (not time)
  11078. (and (stringp time)
  11079. (string-match "^[-+]+[0-9]" time))))
  11080. ;; Try to get a default date/time from existing timestamp
  11081. (save-excursion
  11082. (org-back-to-heading t)
  11083. (setq end (save-excursion (outline-next-heading) (point)))
  11084. (when (re-search-forward (if (eq what 'scheduled)
  11085. org-scheduled-time-regexp
  11086. org-deadline-time-regexp)
  11087. end t)
  11088. (setq ts (match-string 1)
  11089. default-time
  11090. (apply 'encode-time (org-parse-time-string ts))
  11091. default-input (and ts (org-get-compact-tod ts))))))
  11092. (when what
  11093. (setq time
  11094. (if (stringp time)
  11095. ;; This is a string (relative or absolute), set proper date
  11096. (apply 'encode-time
  11097. (org-read-date-analyze
  11098. time default-time (decode-time default-time)))
  11099. ;; If necessary, get the time from the user
  11100. (or time (org-read-date nil 'to-time nil nil
  11101. default-time default-input)))))
  11102. (when (and org-insert-labeled-timestamps-at-point
  11103. (member what '(scheduled deadline)))
  11104. (insert
  11105. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  11106. (org-insert-time-stamp time org-time-was-given
  11107. nil nil nil (list org-end-time-was-given))
  11108. (setq what nil))
  11109. (save-excursion
  11110. (save-restriction
  11111. (let (col list elt ts buffer-invisibility-spec)
  11112. (org-back-to-heading t)
  11113. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
  11114. (goto-char (match-end 1))
  11115. (setq col (current-column))
  11116. (goto-char (match-end 0))
  11117. (if (eobp) (insert "\n") (forward-char 1))
  11118. (when (and (not what)
  11119. (not (looking-at
  11120. (concat "[ \t]*"
  11121. org-keyword-time-not-clock-regexp))))
  11122. ;; Nothing to add, nothing to remove...... :-)
  11123. (throw 'exit nil))
  11124. (if (and (not (looking-at org-outline-regexp))
  11125. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  11126. "[^\r\n]*"))
  11127. (not (equal (match-string 1) org-clock-string)))
  11128. (narrow-to-region (match-beginning 0) (match-end 0))
  11129. (insert-before-markers "\n")
  11130. (backward-char 1)
  11131. (narrow-to-region (point) (point))
  11132. (and org-adapt-indentation (org-indent-to-column col)))
  11133. ;; Check if we have to remove something.
  11134. (setq list (cons what remove))
  11135. (while list
  11136. (setq elt (pop list))
  11137. (when (or (and (eq elt 'scheduled)
  11138. (re-search-forward org-scheduled-time-regexp nil t))
  11139. (and (eq elt 'deadline)
  11140. (re-search-forward org-deadline-time-regexp nil t))
  11141. (and (eq elt 'closed)
  11142. (re-search-forward org-closed-time-regexp nil t)))
  11143. (replace-match "")
  11144. (if (looking-at "--+<[^>]+>") (replace-match ""))))
  11145. (and (looking-at "[ \t]+") (replace-match ""))
  11146. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  11147. (when what
  11148. (insert
  11149. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  11150. (cond ((eq what 'scheduled) org-scheduled-string)
  11151. ((eq what 'deadline) org-deadline-string)
  11152. ((eq what 'closed) org-closed-string))
  11153. " ")
  11154. (setq ts (org-insert-time-stamp
  11155. time
  11156. (or org-time-was-given
  11157. (and (eq what 'closed) org-log-done-with-time))
  11158. (eq what 'closed)
  11159. nil nil (list org-end-time-was-given)))
  11160. (insert
  11161. (if (not (or (bolp) (eq (char-before) ?\ )
  11162. (memq (char-after) '(32 10))
  11163. (eobp))) " " ""))
  11164. (end-of-line 1))
  11165. (goto-char (point-min))
  11166. (widen)
  11167. (if (and (looking-at "[ \t]*\n")
  11168. (equal (char-before) ?\n))
  11169. (delete-region (1- (point)) (point-at-eol)))
  11170. ts))))))
  11171. (defvar org-log-note-marker (make-marker))
  11172. (defvar org-log-note-purpose nil)
  11173. (defvar org-log-note-state nil)
  11174. (defvar org-log-note-previous-state nil)
  11175. (defvar org-log-note-how nil)
  11176. (defvar org-log-note-extra nil)
  11177. (defvar org-log-note-window-configuration nil)
  11178. (defvar org-log-note-return-to (make-marker))
  11179. (defvar org-log-note-effective-time nil
  11180. "Remembered current time so that dynamically scoped
  11181. `org-extend-today-until' affects tha timestamps in state change
  11182. log")
  11183. (defvar org-log-post-message nil
  11184. "Message to be displayed after a log note has been stored.
  11185. The auto-repeater uses this.")
  11186. (defun org-add-note ()
  11187. "Add a note to the current entry.
  11188. This is done in the same way as adding a state change note."
  11189. (interactive)
  11190. (org-add-log-setup 'note nil nil 'findpos nil))
  11191. (defvar org-property-end-re)
  11192. (defun org-add-log-setup (&optional purpose state prev-state
  11193. findpos how extra)
  11194. "Set up the post command hook to take a note.
  11195. If this is about to TODO state change, the new state is expected in STATE.
  11196. When FINDPOS is non-nil, find the correct position for the note in
  11197. the current entry. If not, assume that it can be inserted at point.
  11198. HOW is an indicator what kind of note should be created.
  11199. EXTRA is additional text that will be inserted into the notes buffer."
  11200. (let* ((org-log-into-drawer (org-log-into-drawer))
  11201. (drawer (cond ((stringp org-log-into-drawer)
  11202. org-log-into-drawer)
  11203. (org-log-into-drawer "LOGBOOK")
  11204. (t nil))))
  11205. (save-restriction
  11206. (save-excursion
  11207. (when findpos
  11208. (org-back-to-heading t)
  11209. (narrow-to-region (point) (save-excursion
  11210. (outline-next-heading) (point)))
  11211. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"
  11212. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  11213. "[^\r\n]*\\)?"))
  11214. (goto-char (match-end 0))
  11215. (cond
  11216. (drawer
  11217. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  11218. nil t)
  11219. (progn
  11220. (goto-char (match-end 0))
  11221. (or org-log-states-order-reversed
  11222. (and (re-search-forward org-property-end-re nil t)
  11223. (goto-char (1- (match-beginning 0))))))
  11224. (insert "\n:" drawer ":\n:END:")
  11225. (beginning-of-line 0)
  11226. (org-indent-line-function)
  11227. (beginning-of-line 2)
  11228. (org-indent-line-function)
  11229. (end-of-line 0)))
  11230. ((and org-log-state-notes-insert-after-drawers
  11231. (save-excursion
  11232. (forward-line) (looking-at org-drawer-regexp)))
  11233. (forward-line)
  11234. (while (looking-at org-drawer-regexp)
  11235. (goto-char (match-end 0))
  11236. (re-search-forward org-property-end-re (point-max) t)
  11237. (forward-line))
  11238. (forward-line -1)))
  11239. (unless org-log-states-order-reversed
  11240. (and (= (char-after) ?\n) (forward-char 1))
  11241. (org-skip-over-state-notes)
  11242. (skip-chars-backward " \t\n\r")))
  11243. (move-marker org-log-note-marker (point))
  11244. (setq org-log-note-purpose purpose
  11245. org-log-note-state state
  11246. org-log-note-previous-state prev-state
  11247. org-log-note-how how
  11248. org-log-note-extra extra
  11249. org-log-note-effective-time (org-current-effective-time))
  11250. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  11251. (defun org-skip-over-state-notes ()
  11252. "Skip past the list of State notes in an entry."
  11253. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  11254. (when (ignore-errors (goto-char (org-in-item-p)))
  11255. (let* ((struct (org-list-struct))
  11256. (prevs (org-list-prevs-alist struct)))
  11257. (while (looking-at "[ \t]*- State")
  11258. (goto-char (or (org-list-get-next-item (point) struct prevs)
  11259. (org-list-get-item-end (point) struct)))))))
  11260. (defun org-add-log-note (&optional purpose)
  11261. "Pop up a window for taking a note, and add this note later at point."
  11262. (remove-hook 'post-command-hook 'org-add-log-note)
  11263. (setq org-log-note-window-configuration (current-window-configuration))
  11264. (delete-other-windows)
  11265. (move-marker org-log-note-return-to (point))
  11266. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  11267. (goto-char org-log-note-marker)
  11268. (org-switch-to-buffer-other-window "*Org Note*")
  11269. (erase-buffer)
  11270. (if (memq org-log-note-how '(time state))
  11271. (let (current-prefix-arg) (org-store-log-note))
  11272. (let ((org-inhibit-startup t)) (org-mode))
  11273. (insert (format "# Insert note for %s.
  11274. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  11275. (cond
  11276. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  11277. ((eq org-log-note-purpose 'done) "closed todo item")
  11278. ((eq org-log-note-purpose 'state)
  11279. (format "state change from \"%s\" to \"%s\""
  11280. (or org-log-note-previous-state "")
  11281. (or org-log-note-state "")))
  11282. ((eq org-log-note-purpose 'reschedule)
  11283. "rescheduling")
  11284. ((eq org-log-note-purpose 'delschedule)
  11285. "no longer scheduled")
  11286. ((eq org-log-note-purpose 'redeadline)
  11287. "changing deadline")
  11288. ((eq org-log-note-purpose 'deldeadline)
  11289. "removing deadline")
  11290. ((eq org-log-note-purpose 'refile)
  11291. "refiling")
  11292. ((eq org-log-note-purpose 'note)
  11293. "this entry")
  11294. (t (error "This should not happen")))))
  11295. (if org-log-note-extra (insert org-log-note-extra))
  11296. (org-set-local 'org-finish-function 'org-store-log-note)
  11297. (run-hooks 'org-log-buffer-setup-hook)))
  11298. (defvar org-note-abort nil) ; dynamically scoped
  11299. (defun org-store-log-note ()
  11300. "Finish taking a log note, and insert it to where it belongs."
  11301. (let ((txt (buffer-string))
  11302. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  11303. lines ind bul)
  11304. (kill-buffer (current-buffer))
  11305. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  11306. (setq txt (replace-match "" t t txt)))
  11307. (if (string-match "\\s-+\\'" txt)
  11308. (setq txt (replace-match "" t t txt)))
  11309. (setq lines (org-split-string txt "\n"))
  11310. (when (and note (string-match "\\S-" note))
  11311. (setq note
  11312. (org-replace-escapes
  11313. note
  11314. (list (cons "%u" (user-login-name))
  11315. (cons "%U" user-full-name)
  11316. (cons "%t" (format-time-string
  11317. (org-time-stamp-format 'long 'inactive)
  11318. org-log-note-effective-time))
  11319. (cons "%T" (format-time-string
  11320. (org-time-stamp-format 'long nil)
  11321. org-log-note-effective-time))
  11322. (cons "%d" (format-time-string
  11323. (org-time-stamp-format nil 'inactive)
  11324. org-log-note-effective-time))
  11325. (cons "%D" (format-time-string
  11326. (org-time-stamp-format nil nil)
  11327. org-log-note-effective-time))
  11328. (cons "%s" (if org-log-note-state
  11329. (concat "\"" org-log-note-state "\"")
  11330. ""))
  11331. (cons "%S" (if org-log-note-previous-state
  11332. (concat "\"" org-log-note-previous-state "\"")
  11333. "\"\"")))))
  11334. (if lines (setq note (concat note " \\\\")))
  11335. (push note lines))
  11336. (when (or current-prefix-arg org-note-abort)
  11337. (when org-log-into-drawer
  11338. (org-remove-empty-drawer-at
  11339. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  11340. org-log-note-marker))
  11341. (setq lines nil))
  11342. (when lines
  11343. (with-current-buffer (marker-buffer org-log-note-marker)
  11344. (save-excursion
  11345. (goto-char org-log-note-marker)
  11346. (move-marker org-log-note-marker nil)
  11347. (end-of-line 1)
  11348. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  11349. (setq ind (save-excursion
  11350. (if (ignore-errors (goto-char (org-in-item-p)))
  11351. (let ((struct (org-list-struct)))
  11352. (org-list-get-ind
  11353. (org-list-get-top-point struct) struct))
  11354. (skip-chars-backward " \r\t\n")
  11355. (cond
  11356. ((and (org-at-heading-p)
  11357. org-adapt-indentation)
  11358. (1+ (org-current-level)))
  11359. ((org-at-heading-p) 0)
  11360. (t (org-get-indentation))))))
  11361. (setq bul (org-list-bullet-string "-"))
  11362. (org-indent-line-to ind)
  11363. (insert bul (pop lines))
  11364. (let ((ind-body (+ (length bul) ind)))
  11365. (while lines
  11366. (insert "\n")
  11367. (org-indent-line-to ind-body)
  11368. (insert (pop lines))))
  11369. (message "Note stored")
  11370. (org-back-to-heading t)
  11371. (org-cycle-hide-drawers 'children)))))
  11372. (set-window-configuration org-log-note-window-configuration)
  11373. (with-current-buffer (marker-buffer org-log-note-return-to)
  11374. (goto-char org-log-note-return-to))
  11375. (move-marker org-log-note-return-to nil)
  11376. (and org-log-post-message (message "%s" org-log-post-message)))
  11377. (defun org-remove-empty-drawer-at (drawer pos)
  11378. "Remove an empty drawer DRAWER at position POS.
  11379. POS may also be a marker."
  11380. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  11381. (save-excursion
  11382. (save-restriction
  11383. (widen)
  11384. (goto-char pos)
  11385. (if (org-in-regexp
  11386. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  11387. (replace-match ""))))))
  11388. (defun org-sparse-tree (&optional arg)
  11389. "Create a sparse tree, prompt for the details.
  11390. This command can create sparse trees. You first need to select the type
  11391. of match used to create the tree:
  11392. t Show all TODO entries.
  11393. T Show entries with a specific TODO keyword.
  11394. m Show entries selected by a tags/property match.
  11395. p Enter a property name and its value (both with completion on existing
  11396. names/values) and show entries with that property.
  11397. r Show entries matching a regular expression (`/' can be used as well)
  11398. d Show deadlines due within `org-deadline-warning-days'.
  11399. b Show deadlines and scheduled items before a date.
  11400. a Show deadlines and scheduled items after a date."
  11401. (interactive "P")
  11402. (let (ans kwd value)
  11403. (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")
  11404. (setq ans (read-char-exclusive))
  11405. (cond
  11406. ((equal ans ?d)
  11407. (call-interactively 'org-check-deadlines))
  11408. ((equal ans ?b)
  11409. (call-interactively 'org-check-before-date))
  11410. ((equal ans ?a)
  11411. (call-interactively 'org-check-after-date))
  11412. ((equal ans ?D)
  11413. (call-interactively 'org-check-dates-range))
  11414. ((equal ans ?t)
  11415. (org-show-todo-tree nil))
  11416. ((equal ans ?T)
  11417. (org-show-todo-tree '(4)))
  11418. ((member ans '(?T ?m))
  11419. (call-interactively 'org-match-sparse-tree))
  11420. ((member ans '(?p ?P))
  11421. (setq kwd (org-icompleting-read "Property: "
  11422. (mapcar 'list (org-buffer-property-keys))))
  11423. (setq value (org-icompleting-read "Value: "
  11424. (mapcar 'list (org-property-values kwd))))
  11425. (unless (string-match "\\`{.*}\\'" value)
  11426. (setq value (concat "\"" value "\"")))
  11427. (org-match-sparse-tree arg (concat kwd "=" value)))
  11428. ((member ans '(?r ?R ?/))
  11429. (call-interactively 'org-occur))
  11430. (t (error "No such sparse tree command \"%c\"" ans)))))
  11431. (defvar org-occur-highlights nil
  11432. "List of overlays used for occur matches.")
  11433. (make-variable-buffer-local 'org-occur-highlights)
  11434. (defvar org-occur-parameters nil
  11435. "Parameters of the active org-occur calls.
  11436. This is a list, each call to org-occur pushes as cons cell,
  11437. containing the regular expression and the callback, onto the list.
  11438. The list can contain several entries if `org-occur' has been called
  11439. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  11440. will only contain one set of parameters. When the highlights are
  11441. removed (for example with `C-c C-c', or with the next edit (depending
  11442. on `org-remove-highlights-with-change'), this variable is emptied
  11443. as well.")
  11444. (make-variable-buffer-local 'org-occur-parameters)
  11445. (defun org-occur (regexp &optional keep-previous callback)
  11446. "Make a compact tree which shows all matches of REGEXP.
  11447. The tree will show the lines where the regexp matches, and all higher
  11448. headlines above the match. It will also show the heading after the match,
  11449. to make sure editing the matching entry is easy.
  11450. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  11451. call to `org-occur' will be kept, to allow stacking of calls to this
  11452. command.
  11453. If CALLBACK is non-nil, it is a function which is called to confirm
  11454. that the match should indeed be shown."
  11455. (interactive "sRegexp: \nP")
  11456. (when (equal regexp "")
  11457. (error "Regexp cannot be empty"))
  11458. (unless keep-previous
  11459. (org-remove-occur-highlights nil nil t))
  11460. (push (cons regexp callback) org-occur-parameters)
  11461. (let ((cnt 0))
  11462. (save-excursion
  11463. (goto-char (point-min))
  11464. (if (or (not keep-previous) ; do not want to keep
  11465. (not org-occur-highlights)) ; no previous matches
  11466. ;; hide everything
  11467. (org-overview))
  11468. (while (re-search-forward regexp nil t)
  11469. (when (or (not callback)
  11470. (save-match-data (funcall callback)))
  11471. (setq cnt (1+ cnt))
  11472. (when org-highlight-sparse-tree-matches
  11473. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  11474. (org-show-context 'occur-tree))))
  11475. (when org-remove-highlights-with-change
  11476. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  11477. nil 'local))
  11478. (unless org-sparse-tree-open-archived-trees
  11479. (org-hide-archived-subtrees (point-min) (point-max)))
  11480. (run-hooks 'org-occur-hook)
  11481. (if (org-called-interactively-p 'interactive)
  11482. (message "%d match(es) for regexp %s" cnt regexp))
  11483. cnt))
  11484. (defun org-occur-next-match (&optional n reset)
  11485. "Function for `next-error-function' to find sparse tree matches.
  11486. N is the number of matches to move, when negative move backwards.
  11487. RESET is entirely ignored - this function always goes back to the
  11488. starting point when no match is found."
  11489. (let* ((limit (if (< n 0) (point-min) (point-max)))
  11490. (search-func (if (< n 0)
  11491. 'previous-single-char-property-change
  11492. 'next-single-char-property-change))
  11493. (n (abs n))
  11494. (pos (point))
  11495. p1)
  11496. (catch 'exit
  11497. (while (setq p1 (funcall search-func (point) 'org-type))
  11498. (when (equal p1 limit)
  11499. (goto-char pos)
  11500. (error "No more matches"))
  11501. (when (equal (get-char-property p1 'org-type) 'org-occur)
  11502. (setq n (1- n))
  11503. (when (= n 0)
  11504. (goto-char p1)
  11505. (throw 'exit (point))))
  11506. (goto-char p1))
  11507. (goto-char p1)
  11508. (error "No more matches"))))
  11509. (defun org-show-context (&optional key)
  11510. "Make sure point and context are visible.
  11511. How much context is shown depends upon the variables
  11512. `org-show-hierarchy-above', `org-show-following-heading',
  11513. `org-show-entry-below' and `org-show-siblings'."
  11514. (let ((heading-p (org-at-heading-p t))
  11515. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  11516. (following-p (org-get-alist-option org-show-following-heading key))
  11517. (entry-p (org-get-alist-option org-show-entry-below key))
  11518. (siblings-p (org-get-alist-option org-show-siblings key)))
  11519. (catch 'exit
  11520. ;; Show heading or entry text
  11521. (if (and heading-p (not entry-p))
  11522. (org-flag-heading nil) ; only show the heading
  11523. (and (or entry-p (outline-invisible-p) (org-invisible-p2))
  11524. (org-show-hidden-entry))) ; show entire entry
  11525. (when following-p
  11526. ;; Show next sibling, or heading below text
  11527. (save-excursion
  11528. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  11529. (org-flag-heading nil))))
  11530. (when siblings-p (org-show-siblings))
  11531. (when hierarchy-p
  11532. ;; show all higher headings, possibly with siblings
  11533. (save-excursion
  11534. (while (and (condition-case nil
  11535. (progn (org-up-heading-all 1) t)
  11536. (error nil))
  11537. (not (bobp)))
  11538. (org-flag-heading nil)
  11539. (when siblings-p (org-show-siblings))))))))
  11540. (defvar org-reveal-start-hook nil
  11541. "Hook run before revealing a location.")
  11542. (defun org-reveal (&optional siblings)
  11543. "Show current entry, hierarchy above it, and the following headline.
  11544. This can be used to show a consistent set of context around locations
  11545. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  11546. not t for the search context.
  11547. With optional argument SIBLINGS, on each level of the hierarchy all
  11548. siblings are shown. This repairs the tree structure to what it would
  11549. look like when opened with hierarchical calls to `org-cycle'.
  11550. With double optional argument \\[universal-argument] \\[universal-argument], \
  11551. go to the parent and show the
  11552. entire tree."
  11553. (interactive "P")
  11554. (run-hooks 'org-reveal-start-hook)
  11555. (let ((org-show-hierarchy-above t)
  11556. (org-show-following-heading t)
  11557. (org-show-siblings (if siblings t org-show-siblings)))
  11558. (org-show-context nil))
  11559. (when (equal siblings '(16))
  11560. (save-excursion
  11561. (when (org-up-heading-safe)
  11562. (org-show-subtree)
  11563. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  11564. (defun org-highlight-new-match (beg end)
  11565. "Highlight from BEG to END and mark the highlight is an occur headline."
  11566. (let ((ov (make-overlay beg end)))
  11567. (overlay-put ov 'face 'secondary-selection)
  11568. (overlay-put ov 'org-type 'org-occur)
  11569. (push ov org-occur-highlights)))
  11570. (defun org-remove-occur-highlights (&optional beg end noremove)
  11571. "Remove the occur highlights from the buffer.
  11572. BEG and END are ignored. If NOREMOVE is nil, remove this function
  11573. from the `before-change-functions' in the current buffer."
  11574. (interactive)
  11575. (unless org-inhibit-highlight-removal
  11576. (mapc 'delete-overlay org-occur-highlights)
  11577. (setq org-occur-highlights nil)
  11578. (setq org-occur-parameters nil)
  11579. (unless noremove
  11580. (remove-hook 'before-change-functions
  11581. 'org-remove-occur-highlights 'local))))
  11582. ;;;; Priorities
  11583. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  11584. "Regular expression matching the priority indicator.")
  11585. (defvar org-remove-priority-next-time nil)
  11586. (defun org-priority-up ()
  11587. "Increase the priority of the current item."
  11588. (interactive)
  11589. (org-priority 'up))
  11590. (defun org-priority-down ()
  11591. "Decrease the priority of the current item."
  11592. (interactive)
  11593. (org-priority 'down))
  11594. (defun org-priority (&optional action)
  11595. "Change the priority of an item by ARG.
  11596. ACTION can be `set', `up', `down', or a character."
  11597. (interactive)
  11598. (unless org-enable-priority-commands
  11599. (error "Priority commands are disabled"))
  11600. (setq action (or action 'set))
  11601. (let (current new news have remove)
  11602. (save-excursion
  11603. (org-back-to-heading t)
  11604. (if (looking-at org-priority-regexp)
  11605. (setq current (string-to-char (match-string 2))
  11606. have t))
  11607. (cond
  11608. ((eq action 'remove)
  11609. (setq remove t new ?\ ))
  11610. ((or (eq action 'set)
  11611. (if (featurep 'xemacs) (characterp action) (integerp action)))
  11612. (if (not (eq action 'set))
  11613. (setq new action)
  11614. (message "Priority %c-%c, SPC to remove: "
  11615. org-highest-priority org-lowest-priority)
  11616. (save-match-data
  11617. (setq new (read-char-exclusive))))
  11618. (if (and (= (upcase org-highest-priority) org-highest-priority)
  11619. (= (upcase org-lowest-priority) org-lowest-priority))
  11620. (setq new (upcase new)))
  11621. (cond ((equal new ?\ ) (setq remove t))
  11622. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  11623. (error "Priority must be between `%c' and `%c'"
  11624. org-highest-priority org-lowest-priority))))
  11625. ((eq action 'up)
  11626. (setq new (if have
  11627. (1- current) ; normal cycling
  11628. ;; last priority was empty
  11629. (if (eq last-command this-command)
  11630. org-lowest-priority ; wrap around empty to lowest
  11631. ;; default
  11632. (if org-priority-start-cycle-with-default
  11633. org-default-priority
  11634. (1- org-default-priority))))))
  11635. ((eq action 'down)
  11636. (setq new (if have
  11637. (1+ current) ; normal cycling
  11638. ;; last priority was empty
  11639. (if (eq last-command this-command)
  11640. org-highest-priority ; wrap around empty to highest
  11641. ;; default
  11642. (if org-priority-start-cycle-with-default
  11643. org-default-priority
  11644. (1+ org-default-priority))))))
  11645. (t (error "Invalid action")))
  11646. (if (or (< (upcase new) org-highest-priority)
  11647. (> (upcase new) org-lowest-priority))
  11648. (if (and (memq action '(up down))
  11649. (not have) (not (eq last-command this-command)))
  11650. ;; `new' is from default priority
  11651. (error
  11652. "The default can not be set, see `org-default-priority' why")
  11653. ;; normal cycling: `new' is beyond highest/lowest priority
  11654. ;; and is wrapped around to the empty priority
  11655. (setq remove t)))
  11656. (setq news (format "%c" new))
  11657. (if have
  11658. (if remove
  11659. (replace-match "" t t nil 1)
  11660. (replace-match news t t nil 2))
  11661. (if remove
  11662. (error "No priority cookie found in line")
  11663. (let ((case-fold-search nil))
  11664. (looking-at org-todo-line-regexp))
  11665. (if (match-end 2)
  11666. (progn
  11667. (goto-char (match-end 2))
  11668. (insert " [#" news "]"))
  11669. (goto-char (match-beginning 3))
  11670. (insert "[#" news "] "))))
  11671. (org-preserve-lc (org-set-tags nil 'align)))
  11672. (if remove
  11673. (message "Priority removed")
  11674. (message "Priority of current item set to %s" news))))
  11675. (defun org-get-priority (s)
  11676. "Find priority cookie and return priority."
  11677. (if (functionp org-get-priority-function)
  11678. (funcall org-get-priority-function)
  11679. (save-match-data
  11680. (if (not (string-match org-priority-regexp s))
  11681. (* 1000 (- org-lowest-priority org-default-priority))
  11682. (* 1000 (- org-lowest-priority
  11683. (string-to-char (match-string 2 s))))))))
  11684. ;;;; Tags
  11685. (defvar org-agenda-archives-mode)
  11686. (defvar org-map-continue-from nil
  11687. "Position from where mapping should continue.
  11688. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  11689. (defvar org-scanner-tags nil
  11690. "The current tag list while the tags scanner is running.")
  11691. (defvar org-trust-scanner-tags nil
  11692. "Should `org-get-tags-at' use the tags for the scanner.
  11693. This is for internal dynamical scoping only.
  11694. When this is non-nil, the function `org-get-tags-at' will return the value
  11695. of `org-scanner-tags' instead of building the list by itself. This
  11696. can lead to large speed-ups when the tags scanner is used in a file with
  11697. many entries, and when the list of tags is retrieved, for example to
  11698. obtain a list of properties. Building the tags list for each entry in such
  11699. a file becomes an N^2 operation - but with this variable set, it scales
  11700. as N.")
  11701. (defun org-scan-tags (action matcher todo-only &optional start-level)
  11702. "Scan headline tags with inheritance and produce output ACTION.
  11703. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  11704. or `agenda' to produce an entry list for an agenda view. It can also be
  11705. a Lisp form or a function that should be called at each matched headline, in
  11706. this case the return value is a list of all return values from these calls.
  11707. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  11708. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  11709. only lines with a not-done TODO keyword are included in the output.
  11710. This should be the same variable that was scoped into
  11711. and set by `org-make-tags-matcher' when it constructed MATCHER.
  11712. START-LEVEL can be a string with asterisks, reducing the scope to
  11713. headlines matching this string."
  11714. (require 'org-agenda)
  11715. (let* ((re (concat "^"
  11716. (if start-level
  11717. ;; Get the correct level to match
  11718. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  11719. org-outline-regexp)
  11720. " *\\(\\<\\("
  11721. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  11722. (org-re
  11723. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  11724. (props (list 'face 'default
  11725. 'done-face 'org-agenda-done
  11726. 'undone-face 'default
  11727. 'mouse-face 'highlight
  11728. 'org-not-done-regexp org-not-done-regexp
  11729. 'org-todo-regexp org-todo-regexp
  11730. 'org-complex-heading-regexp org-complex-heading-regexp
  11731. 'help-echo
  11732. (format "mouse-2 or RET jump to org file %s"
  11733. (abbreviate-file-name
  11734. (or (buffer-file-name (buffer-base-buffer))
  11735. (buffer-name (buffer-base-buffer)))))))
  11736. (case-fold-search nil)
  11737. (org-map-continue-from nil)
  11738. lspos tags tags-list
  11739. (tags-alist (list (cons 0 org-file-tags)))
  11740. (llast 0) rtn rtn1 level category i txt
  11741. todo marker entry priority)
  11742. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  11743. (setq action (list 'lambda nil action)))
  11744. (save-excursion
  11745. (goto-char (point-min))
  11746. (when (eq action 'sparse-tree)
  11747. (org-overview)
  11748. (org-remove-occur-highlights))
  11749. (while (re-search-forward re nil t)
  11750. (setq org-map-continue-from nil)
  11751. (catch :skip
  11752. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  11753. tags (if (match-end 4) (org-match-string-no-properties 4)))
  11754. (goto-char (setq lspos (match-beginning 0)))
  11755. (setq level (org-reduced-level (funcall outline-level))
  11756. category (org-get-category))
  11757. (setq i llast llast level)
  11758. ;; remove tag lists from same and sublevels
  11759. (while (>= i level)
  11760. (when (setq entry (assoc i tags-alist))
  11761. (setq tags-alist (delete entry tags-alist)))
  11762. (setq i (1- i)))
  11763. ;; add the next tags
  11764. (when tags
  11765. (setq tags (org-split-string tags ":")
  11766. tags-alist
  11767. (cons (cons level tags) tags-alist)))
  11768. ;; compile tags for current headline
  11769. (setq tags-list
  11770. (if org-use-tag-inheritance
  11771. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  11772. tags)
  11773. org-scanner-tags tags-list)
  11774. (when org-use-tag-inheritance
  11775. (setcdr (car tags-alist)
  11776. (mapcar (lambda (x)
  11777. (setq x (copy-sequence x))
  11778. (org-add-prop-inherited x))
  11779. (cdar tags-alist))))
  11780. (when (and tags org-use-tag-inheritance
  11781. (or (not (eq t org-use-tag-inheritance))
  11782. org-tags-exclude-from-inheritance))
  11783. ;; selective inheritance, remove uninherited ones
  11784. (setcdr (car tags-alist)
  11785. (org-remove-uninherited-tags (cdar tags-alist))))
  11786. (when (and
  11787. ;; eval matcher only when the todo condition is OK
  11788. (and (or (not todo-only) (member todo org-not-done-keywords))
  11789. (let ((case-fold-search t) (org-trust-scanner-tags t))
  11790. (eval matcher)))
  11791. ;; Call the skipper, but return t if it does not skip,
  11792. ;; so that the `and' form continues evaluating
  11793. (progn
  11794. (unless (eq action 'sparse-tree) (org-agenda-skip))
  11795. t)
  11796. ;; Check if timestamps are deselecting this entry
  11797. (or (not todo-only)
  11798. (and (member todo org-not-done-keywords)
  11799. (or (not org-agenda-tags-todo-honor-ignore-options)
  11800. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  11801. ;; Extra check for the archive tag
  11802. ;; FIXME: Does the skipper already do this????
  11803. (or
  11804. (not (member org-archive-tag tags-list))
  11805. ;; we have an archive tag, should we use this anyway?
  11806. (or (not org-agenda-skip-archived-trees)
  11807. (and (eq action 'agenda) org-agenda-archives-mode))))
  11808. ;; select this headline
  11809. (cond
  11810. ((eq action 'sparse-tree)
  11811. (and org-highlight-sparse-tree-matches
  11812. (org-get-heading) (match-end 0)
  11813. (org-highlight-new-match
  11814. (match-beginning 1) (match-end 1)))
  11815. (org-show-context 'tags-tree))
  11816. ((eq action 'agenda)
  11817. (setq txt (org-agenda-format-item
  11818. ""
  11819. (concat
  11820. (if (eq org-tags-match-list-sublevels 'indented)
  11821. (make-string (1- level) ?.) "")
  11822. (org-get-heading))
  11823. category
  11824. tags-list)
  11825. priority (org-get-priority txt))
  11826. (goto-char lspos)
  11827. (setq marker (org-agenda-new-marker))
  11828. (org-add-props txt props
  11829. 'org-marker marker 'org-hd-marker marker 'org-category category
  11830. 'todo-state todo
  11831. 'priority priority 'type "tagsmatch")
  11832. (push txt rtn))
  11833. ((functionp action)
  11834. (setq org-map-continue-from nil)
  11835. (save-excursion
  11836. (setq rtn1 (funcall action))
  11837. (push rtn1 rtn)))
  11838. (t (error "Invalid action")))
  11839. ;; if we are to skip sublevels, jump to end of subtree
  11840. (unless org-tags-match-list-sublevels
  11841. (org-end-of-subtree t)
  11842. (backward-char 1))))
  11843. ;; Get the correct position from where to continue
  11844. (if org-map-continue-from
  11845. (goto-char org-map-continue-from)
  11846. (and (= (point) lspos) (end-of-line 1)))))
  11847. (when (and (eq action 'sparse-tree)
  11848. (not org-sparse-tree-open-archived-trees))
  11849. (org-hide-archived-subtrees (point-min) (point-max)))
  11850. (nreverse rtn)))
  11851. (defun org-remove-uninherited-tags (tags)
  11852. "Remove all tags that are not inherited from the list TAGS."
  11853. (cond
  11854. ((eq org-use-tag-inheritance t)
  11855. (if org-tags-exclude-from-inheritance
  11856. (org-delete-all org-tags-exclude-from-inheritance tags)
  11857. tags))
  11858. ((not org-use-tag-inheritance) nil)
  11859. ((stringp org-use-tag-inheritance)
  11860. (delq nil (mapcar
  11861. (lambda (x)
  11862. (if (and (string-match org-use-tag-inheritance x)
  11863. (not (member x org-tags-exclude-from-inheritance)))
  11864. x nil))
  11865. tags)))
  11866. ((listp org-use-tag-inheritance)
  11867. (delq nil (mapcar
  11868. (lambda (x)
  11869. (if (member x org-use-tag-inheritance) x nil))
  11870. tags)))))
  11871. (defun org-match-sparse-tree (&optional todo-only match)
  11872. "Create a sparse tree according to tags string MATCH.
  11873. MATCH can contain positive and negative selection of tags, like
  11874. \"+WORK+URGENT-WITHBOSS\".
  11875. If optional argument TODO-ONLY is non-nil, only select lines that are
  11876. also TODO lines."
  11877. (interactive "P")
  11878. (org-prepare-agenda-buffers (list (current-buffer)))
  11879. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  11880. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  11881. (defvar org-cached-props nil)
  11882. (defun org-cached-entry-get (pom property)
  11883. (if (or (eq t org-use-property-inheritance)
  11884. (and (stringp org-use-property-inheritance)
  11885. (string-match org-use-property-inheritance property))
  11886. (and (listp org-use-property-inheritance)
  11887. (member property org-use-property-inheritance)))
  11888. ;; Caching is not possible, check it directly
  11889. (org-entry-get pom property 'inherit)
  11890. ;; Get all properties, so that we can do complicated checks easily
  11891. (cdr (assoc property (or org-cached-props
  11892. (setq org-cached-props
  11893. (org-entry-properties pom)))))))
  11894. (defun org-global-tags-completion-table (&optional files)
  11895. "Return the list of all tags in all agenda buffer/files.
  11896. Optional FILES argument is a list of files to which can be used
  11897. instead of the agenda files."
  11898. (save-excursion
  11899. (org-uniquify
  11900. (delq nil
  11901. (apply 'append
  11902. (mapcar
  11903. (lambda (file)
  11904. (set-buffer (find-file-noselect file))
  11905. (append (org-get-buffer-tags)
  11906. (mapcar (lambda (x) (if (stringp (car-safe x))
  11907. (list (car-safe x)) nil))
  11908. org-tag-alist)))
  11909. (if (and files (car files))
  11910. files
  11911. (org-agenda-files))))))))
  11912. (defun org-make-tags-matcher (match)
  11913. "Create the TAGS/TODO matcher form for the selection string MATCH.
  11914. The variable `todo-only' is scoped dynamically into this function; it will be
  11915. set to t if the matcher restricts matching to TODO entries,
  11916. otherwise will not be touched.
  11917. Returns a cons of the selection string MATCH and the constructed
  11918. lisp form implementing the matcher. The matcher is to be
  11919. evaluated at an Org entry, with point on the headline,
  11920. and returns t if the entry matches the
  11921. selection string MATCH. The returned lisp form references
  11922. two variables with information about the entry, which must be
  11923. bound around the form's evaluation: todo, the TODO keyword at the
  11924. entry (or nil of none); and tags-list, the list of all tags at the
  11925. entry including inherited ones. Additionally, the category
  11926. of the entry (if any) must be specified as the text property
  11927. 'org-category on the headline.
  11928. See also `org-scan-tags'.
  11929. "
  11930. (declare (special todo-only))
  11931. (unless (boundp 'todo-only)
  11932. (error "org-make-tags-matcher expects todo-only to be scoped in"))
  11933. (unless match
  11934. ;; Get a new match request, with completion
  11935. (let ((org-last-tags-completion-table
  11936. (org-global-tags-completion-table)))
  11937. (setq match (org-completing-read-no-i
  11938. "Match: " 'org-tags-completion-function nil nil nil
  11939. 'org-tags-history))))
  11940. ;; Parse the string and create a lisp form
  11941. (let ((match0 match)
  11942. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  11943. minus tag mm
  11944. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  11945. orterms term orlist re-p str-p level-p level-op time-p
  11946. prop-p pn pv po gv rest)
  11947. (if (string-match "/+" match)
  11948. ;; match contains also a todo-matching request
  11949. (progn
  11950. (setq tagsmatch (substring match 0 (match-beginning 0))
  11951. todomatch (substring match (match-end 0)))
  11952. (if (string-match "^!" todomatch)
  11953. (setq todo-only t todomatch (substring todomatch 1)))
  11954. (if (string-match "^\\s-*$" todomatch)
  11955. (setq todomatch nil)))
  11956. ;; only matching tags
  11957. (setq tagsmatch match todomatch nil))
  11958. ;; Make the tags matcher
  11959. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  11960. (setq tagsmatcher t)
  11961. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  11962. (while (setq term (pop orterms))
  11963. (while (and (equal (substring term -1) "\\") orterms)
  11964. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  11965. (while (string-match re term)
  11966. (setq rest (substring term (match-end 0))
  11967. minus (and (match-end 1)
  11968. (equal (match-string 1 term) "-"))
  11969. tag (save-match-data (replace-regexp-in-string
  11970. "\\\\-" "-"
  11971. (match-string 2 term)))
  11972. re-p (equal (string-to-char tag) ?{)
  11973. level-p (match-end 4)
  11974. prop-p (match-end 5)
  11975. mm (cond
  11976. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  11977. (level-p
  11978. (setq level-op (org-op-to-function (match-string 3 term)))
  11979. `(,level-op level ,(string-to-number
  11980. (match-string 4 term))))
  11981. (prop-p
  11982. (setq pn (match-string 5 term)
  11983. po (match-string 6 term)
  11984. pv (match-string 7 term)
  11985. re-p (equal (string-to-char pv) ?{)
  11986. str-p (equal (string-to-char pv) ?\")
  11987. time-p (save-match-data
  11988. (string-match "^\"[[<].*[]>]\"$" pv))
  11989. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  11990. (if time-p (setq pv (org-matcher-time pv)))
  11991. (setq po (org-op-to-function po (if time-p 'time str-p)))
  11992. (cond
  11993. ((equal pn "CATEGORY")
  11994. (setq gv '(get-text-property (point) 'org-category)))
  11995. ((equal pn "TODO")
  11996. (setq gv 'todo))
  11997. (t
  11998. (setq gv `(org-cached-entry-get nil ,pn))))
  11999. (if re-p
  12000. (if (eq po 'org<>)
  12001. `(not (string-match ,pv (or ,gv "")))
  12002. `(string-match ,pv (or ,gv "")))
  12003. (if str-p
  12004. `(,po (or ,gv "") ,pv)
  12005. `(,po (string-to-number (or ,gv ""))
  12006. ,(string-to-number pv) ))))
  12007. (t `(member ,tag tags-list)))
  12008. mm (if minus (list 'not mm) mm)
  12009. term rest)
  12010. (push mm tagsmatcher))
  12011. (push (if (> (length tagsmatcher) 1)
  12012. (cons 'and tagsmatcher)
  12013. (car tagsmatcher))
  12014. orlist)
  12015. (setq tagsmatcher nil))
  12016. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  12017. (setq tagsmatcher
  12018. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  12019. ;; Make the todo matcher
  12020. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  12021. (setq todomatcher t)
  12022. (setq orterms (org-split-string todomatch "|") orlist nil)
  12023. (while (setq term (pop orterms))
  12024. (while (string-match re term)
  12025. (setq minus (and (match-end 1)
  12026. (equal (match-string 1 term) "-"))
  12027. kwd (match-string 2 term)
  12028. re-p (equal (string-to-char kwd) ?{)
  12029. term (substring term (match-end 0))
  12030. mm (if re-p
  12031. `(string-match ,(substring kwd 1 -1) todo)
  12032. (list 'equal 'todo kwd))
  12033. mm (if minus (list 'not mm) mm))
  12034. (push mm todomatcher))
  12035. (push (if (> (length todomatcher) 1)
  12036. (cons 'and todomatcher)
  12037. (car todomatcher))
  12038. orlist)
  12039. (setq todomatcher nil))
  12040. (setq todomatcher (if (> (length orlist) 1)
  12041. (cons 'or orlist) (car orlist))))
  12042. ;; Return the string and lisp forms of the matcher
  12043. (setq matcher (if todomatcher
  12044. (list 'and tagsmatcher todomatcher)
  12045. tagsmatcher))
  12046. (when todo-only
  12047. (setq matcher (list 'and '(member todo org-not-done-keywords)
  12048. matcher)))
  12049. (cons match0 matcher)))
  12050. (defun org-op-to-function (op &optional stringp)
  12051. "Turn an operator into the appropriate function."
  12052. (setq op
  12053. (cond
  12054. ((equal op "<" ) '(< string< org-time<))
  12055. ((equal op ">" ) '(> org-string> org-time>))
  12056. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  12057. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  12058. ((member op '("=" "==")) '(= string= org-time=))
  12059. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  12060. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  12061. (defun org<> (a b) (not (= a b)))
  12062. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  12063. (defun org-string>= (a b) (not (string< a b)))
  12064. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  12065. (defun org-string<> (a b) (not (string= a b)))
  12066. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  12067. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  12068. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  12069. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  12070. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  12071. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  12072. (defun org-2ft (s)
  12073. "Convert S to a floating point time.
  12074. If S is already a number, just return it. If it is a string, parse
  12075. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  12076. (cond
  12077. ((numberp s) s)
  12078. ((stringp s)
  12079. (condition-case nil
  12080. (float-time (apply 'encode-time (org-parse-time-string s)))
  12081. (error 0.)))
  12082. (t 0.)))
  12083. (defun org-time-today ()
  12084. "Time in seconds today at 0:00.
  12085. Returns the float number of seconds since the beginning of the
  12086. epoch to the beginning of today (00:00)."
  12087. (float-time (apply 'encode-time
  12088. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  12089. (defun org-matcher-time (s)
  12090. "Interpret a time comparison value."
  12091. (save-match-data
  12092. (cond
  12093. ((string= s "<now>") (float-time))
  12094. ((string= s "<today>") (org-time-today))
  12095. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  12096. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  12097. ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
  12098. (+ (org-time-today)
  12099. (* (string-to-number (match-string 1 s))
  12100. (cdr (assoc (match-string 2 s)
  12101. '(("d" . 86400.0) ("w" . 604800.0)
  12102. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  12103. (t (org-2ft s)))))
  12104. (defun org-match-any-p (re list)
  12105. "Does re match any element of list?"
  12106. (setq list (mapcar (lambda (x) (string-match re x)) list))
  12107. (delq nil list))
  12108. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  12109. (defvar org-tags-overlay (make-overlay 1 1))
  12110. (org-detach-overlay org-tags-overlay)
  12111. (defun org-get-local-tags-at (&optional pos)
  12112. "Get a list of tags defined in the current headline."
  12113. (org-get-tags-at pos 'local))
  12114. (defun org-get-local-tags ()
  12115. "Get a list of tags defined in the current headline."
  12116. (org-get-tags-at nil 'local))
  12117. (defun org-get-tags-at (&optional pos local)
  12118. "Get a list of all headline tags applicable at POS.
  12119. POS defaults to point. If tags are inherited, the list contains
  12120. the targets in the same sequence as the headlines appear, i.e.
  12121. the tags of the current headline come last.
  12122. When LOCAL is non-nil, only return tags from the current headline,
  12123. ignore inherited ones."
  12124. (interactive)
  12125. (if (and org-trust-scanner-tags
  12126. (or (not pos) (equal pos (point)))
  12127. (not local))
  12128. org-scanner-tags
  12129. (let (tags ltags lastpos parent)
  12130. (save-excursion
  12131. (save-restriction
  12132. (widen)
  12133. (goto-char (or pos (point)))
  12134. (save-match-data
  12135. (catch 'done
  12136. (condition-case nil
  12137. (progn
  12138. (org-back-to-heading t)
  12139. (while (not (equal lastpos (point)))
  12140. (setq lastpos (point))
  12141. (when (looking-at
  12142. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  12143. (setq ltags (org-split-string
  12144. (org-match-string-no-properties 1) ":"))
  12145. (when parent
  12146. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  12147. (setq tags (append
  12148. (if parent
  12149. (org-remove-uninherited-tags ltags)
  12150. ltags)
  12151. tags)))
  12152. (or org-use-tag-inheritance (throw 'done t))
  12153. (if local (throw 'done t))
  12154. (or (org-up-heading-safe) (error nil))
  12155. (setq parent t)))
  12156. (error nil)))))
  12157. (if local
  12158. tags
  12159. (append (org-remove-uninherited-tags org-file-tags) tags))))))
  12160. (defun org-add-prop-inherited (s)
  12161. (add-text-properties 0 (length s) '(inherited t) s)
  12162. s)
  12163. (defun org-toggle-tag (tag &optional onoff)
  12164. "Toggle the tag TAG for the current line.
  12165. If ONOFF is `on' or `off', don't toggle but set to this state."
  12166. (let (res current)
  12167. (save-excursion
  12168. (org-back-to-heading t)
  12169. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  12170. (point-at-eol) t)
  12171. (progn
  12172. (setq current (match-string 1))
  12173. (replace-match ""))
  12174. (setq current ""))
  12175. (setq current (nreverse (org-split-string current ":")))
  12176. (cond
  12177. ((eq onoff 'on)
  12178. (setq res t)
  12179. (or (member tag current) (push tag current)))
  12180. ((eq onoff 'off)
  12181. (or (not (member tag current)) (setq current (delete tag current))))
  12182. (t (if (member tag current)
  12183. (setq current (delete tag current))
  12184. (setq res t)
  12185. (push tag current))))
  12186. (end-of-line 1)
  12187. (if current
  12188. (progn
  12189. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  12190. (org-set-tags nil t))
  12191. (delete-horizontal-space))
  12192. (run-hooks 'org-after-tags-change-hook))
  12193. res))
  12194. (defun org-align-tags-here (to-col)
  12195. ;; Assumes that this is a headline
  12196. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  12197. (beginning-of-line 1)
  12198. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12199. (< pos (match-beginning 2)))
  12200. (progn
  12201. (setq tags-l (- (match-end 2) (match-beginning 2)))
  12202. (goto-char (match-beginning 1))
  12203. (insert " ")
  12204. (delete-region (point) (1+ (match-beginning 2)))
  12205. (setq ncol (max (current-column)
  12206. (1+ col)
  12207. (if (> to-col 0)
  12208. to-col
  12209. (- (abs to-col) tags-l))))
  12210. (setq p (point))
  12211. (insert (make-string (- ncol (current-column)) ?\ ))
  12212. (setq ncol (current-column))
  12213. (when indent-tabs-mode (tabify p (point-at-eol)))
  12214. (org-move-to-column (min ncol col) t))
  12215. (goto-char pos))))
  12216. (defun org-set-tags-command (&optional arg just-align)
  12217. "Call the set-tags command for the current entry."
  12218. (interactive "P")
  12219. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  12220. (org-set-tags arg just-align)
  12221. (save-excursion
  12222. (org-back-to-heading t)
  12223. (org-set-tags arg just-align))))
  12224. (defun org-set-tags-to (data)
  12225. "Set the tags of the current entry to DATA, replacing the current tags.
  12226. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  12227. If DATA is nil or the empty string, any tags will be removed."
  12228. (interactive "sTags: ")
  12229. (setq data
  12230. (cond
  12231. ((eq data nil) "")
  12232. ((equal data "") "")
  12233. ((stringp data)
  12234. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  12235. ":"))
  12236. ((listp data)
  12237. (concat ":" (mapconcat 'identity data ":") ":"))
  12238. (t nil)))
  12239. (when data
  12240. (save-excursion
  12241. (org-back-to-heading t)
  12242. (when (looking-at org-complex-heading-regexp)
  12243. (if (match-end 5)
  12244. (progn
  12245. (goto-char (match-beginning 5))
  12246. (insert data)
  12247. (delete-region (point) (point-at-eol))
  12248. (org-set-tags nil 'align))
  12249. (goto-char (point-at-eol))
  12250. (insert " " data)
  12251. (org-set-tags nil 'align)))
  12252. (beginning-of-line 1)
  12253. (if (looking-at ".*?\\([ \t]+\\)$")
  12254. (delete-region (match-beginning 1) (match-end 1))))))
  12255. (defun org-align-all-tags ()
  12256. "Align the tags i all headings."
  12257. (interactive)
  12258. (save-excursion
  12259. (or (ignore-errors (org-back-to-heading t))
  12260. (outline-next-heading))
  12261. (if (org-at-heading-p)
  12262. (org-set-tags t)
  12263. (message "No headings"))))
  12264. (defvar org-indent-indentation-per-level)
  12265. (defun org-set-tags (&optional arg just-align)
  12266. "Set the tags for the current headline.
  12267. With prefix ARG, realign all tags in headings in the current buffer."
  12268. (interactive "P")
  12269. (let* ((re org-outline-regexp-bol)
  12270. (current (unless arg (org-get-tags-string)))
  12271. (col (current-column))
  12272. (org-setting-tags t)
  12273. table current-tags inherited-tags ; computed below when needed
  12274. tags p0 c0 c1 rpl di tc level)
  12275. (if arg
  12276. (save-excursion
  12277. (goto-char (point-min))
  12278. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  12279. (while (re-search-forward re nil t)
  12280. (org-set-tags nil t)
  12281. (end-of-line 1)))
  12282. (message "All tags realigned to column %d" org-tags-column))
  12283. (if just-align
  12284. (setq tags current)
  12285. ;; Get a new set of tags from the user
  12286. (save-excursion
  12287. (setq table (append org-tag-persistent-alist
  12288. (or org-tag-alist (org-get-buffer-tags))
  12289. (and
  12290. org-complete-tags-always-offer-all-agenda-tags
  12291. (org-global-tags-completion-table
  12292. (org-agenda-files))))
  12293. org-last-tags-completion-table table
  12294. current-tags (org-split-string current ":")
  12295. inherited-tags (nreverse
  12296. (nthcdr (length current-tags)
  12297. (nreverse (org-get-tags-at))))
  12298. tags
  12299. (if (or (eq t org-use-fast-tag-selection)
  12300. (and org-use-fast-tag-selection
  12301. (delq nil (mapcar 'cdr table))))
  12302. (org-fast-tag-selection
  12303. current-tags inherited-tags table
  12304. (if org-fast-tag-selection-include-todo
  12305. org-todo-key-alist))
  12306. (let ((org-add-colon-after-tag-completion (< 1 (length table))))
  12307. (org-trim
  12308. (org-icompleting-read "Tags: "
  12309. 'org-tags-completion-function
  12310. nil nil current 'org-tags-history))))))
  12311. (while (string-match "[-+&]+" tags)
  12312. ;; No boolean logic, just a list
  12313. (setq tags (replace-match ":" t t tags))))
  12314. (setq tags (replace-regexp-in-string "[,]" ":" tags))
  12315. (if org-tags-sort-function
  12316. (setq tags (mapconcat 'identity
  12317. (sort (org-split-string
  12318. tags (org-re "[^[:alnum:]_@#%]+"))
  12319. org-tags-sort-function) ":")))
  12320. (if (string-match "\\`[\t ]*\\'" tags)
  12321. (setq tags "")
  12322. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  12323. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  12324. ;; Insert new tags at the correct column
  12325. (beginning-of-line 1)
  12326. (setq level (or (and (looking-at org-outline-regexp)
  12327. (- (match-end 0) (point) 1))
  12328. 1))
  12329. (cond
  12330. ((and (equal current "") (equal tags "")))
  12331. ((re-search-forward
  12332. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  12333. (point-at-eol) t)
  12334. (if (equal tags "")
  12335. (setq rpl "")
  12336. (goto-char (match-beginning 0))
  12337. (setq c0 (current-column)
  12338. ;; compute offset for the case of org-indent-mode active
  12339. di (if org-indent-mode
  12340. (* (1- org-indent-indentation-per-level) (1- level))
  12341. 0)
  12342. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  12343. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  12344. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
  12345. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  12346. (replace-match rpl t t)
  12347. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  12348. tags)
  12349. (t (error "Tags alignment failed")))
  12350. (org-move-to-column col)
  12351. (unless just-align
  12352. (run-hooks 'org-after-tags-change-hook)))))
  12353. (defun org-change-tag-in-region (beg end tag off)
  12354. "Add or remove TAG for each entry in the region.
  12355. This works in the agenda, and also in an org-mode buffer."
  12356. (interactive
  12357. (list (region-beginning) (region-end)
  12358. (let ((org-last-tags-completion-table
  12359. (if (derived-mode-p 'org-mode)
  12360. (org-get-buffer-tags)
  12361. (org-global-tags-completion-table))))
  12362. (org-icompleting-read
  12363. "Tag: " 'org-tags-completion-function nil nil nil
  12364. 'org-tags-history))
  12365. (progn
  12366. (message "[s]et or [r]emove? ")
  12367. (equal (read-char-exclusive) ?r))))
  12368. (if (fboundp 'deactivate-mark) (deactivate-mark))
  12369. (let ((agendap (equal major-mode 'org-agenda-mode))
  12370. l1 l2 m buf pos newhead (cnt 0))
  12371. (goto-char end)
  12372. (setq l2 (1- (org-current-line)))
  12373. (goto-char beg)
  12374. (setq l1 (org-current-line))
  12375. (loop for l from l1 to l2 do
  12376. (org-goto-line l)
  12377. (setq m (get-text-property (point) 'org-hd-marker))
  12378. (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
  12379. (and agendap m))
  12380. (setq buf (if agendap (marker-buffer m) (current-buffer))
  12381. pos (if agendap m (point)))
  12382. (with-current-buffer buf
  12383. (save-excursion
  12384. (save-restriction
  12385. (goto-char pos)
  12386. (setq cnt (1+ cnt))
  12387. (org-toggle-tag tag (if off 'off 'on))
  12388. (setq newhead (org-get-heading)))))
  12389. (and agendap (org-agenda-change-all-lines newhead m))))
  12390. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  12391. (defun org-tags-completion-function (string predicate &optional flag)
  12392. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  12393. (confirm (lambda (x) (stringp (car x)))))
  12394. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  12395. (setq s1 (match-string 1 string)
  12396. s2 (match-string 2 string))
  12397. (setq s1 "" s2 string))
  12398. (cond
  12399. ((eq flag nil)
  12400. ;; try completion
  12401. (setq rtn (try-completion s2 ctable confirm))
  12402. (if (stringp rtn)
  12403. (setq rtn
  12404. (concat s1 s2 (substring rtn (length s2))
  12405. (if (and org-add-colon-after-tag-completion
  12406. (assoc rtn ctable))
  12407. ":" ""))))
  12408. rtn)
  12409. ((eq flag t)
  12410. ;; all-completions
  12411. (all-completions s2 ctable confirm)
  12412. )
  12413. ((eq flag 'lambda)
  12414. ;; exact match?
  12415. (assoc s2 ctable)))
  12416. ))
  12417. (defun org-fast-tag-insert (kwd tags face &optional end)
  12418. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  12419. (insert (format "%-12s" (concat kwd ":"))
  12420. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  12421. (or end "")))
  12422. (defun org-fast-tag-show-exit (flag)
  12423. (save-excursion
  12424. (org-goto-line 3)
  12425. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  12426. (replace-match ""))
  12427. (when flag
  12428. (end-of-line 1)
  12429. (org-move-to-column (- (window-width) 19) t)
  12430. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  12431. (defun org-set-current-tags-overlay (current prefix)
  12432. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  12433. (if (featurep 'xemacs)
  12434. (org-overlay-display org-tags-overlay (concat prefix s)
  12435. 'secondary-selection)
  12436. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  12437. (org-overlay-display org-tags-overlay (concat prefix s)))))
  12438. (defvar org-last-tag-selection-key nil)
  12439. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  12440. "Fast tag selection with single keys.
  12441. CURRENT is the current list of tags in the headline, INHERITED is the
  12442. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  12443. possibly with grouping information. TODO-TABLE is a similar table with
  12444. TODO keywords, should these have keys assigned to them.
  12445. If the keys are nil, a-z are automatically assigned.
  12446. Returns the new tags string, or nil to not change the current settings."
  12447. (let* ((fulltable (append table todo-table))
  12448. (maxlen (apply 'max (mapcar
  12449. (lambda (x)
  12450. (if (stringp (car x)) (string-width (car x)) 0))
  12451. fulltable)))
  12452. (buf (current-buffer))
  12453. (expert (eq org-fast-tag-selection-single-key 'expert))
  12454. (buffer-tags nil)
  12455. (fwidth (+ maxlen 3 1 3))
  12456. (ncol (/ (- (window-width) 4) fwidth))
  12457. (i-face 'org-done)
  12458. (c-face 'org-todo)
  12459. tg cnt e c char c1 c2 ntable tbl rtn
  12460. ov-start ov-end ov-prefix
  12461. (exit-after-next org-fast-tag-selection-single-key)
  12462. (done-keywords org-done-keywords)
  12463. groups ingroup)
  12464. (save-excursion
  12465. (beginning-of-line 1)
  12466. (if (looking-at
  12467. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12468. (setq ov-start (match-beginning 1)
  12469. ov-end (match-end 1)
  12470. ov-prefix "")
  12471. (setq ov-start (1- (point-at-eol))
  12472. ov-end (1+ ov-start))
  12473. (skip-chars-forward "^\n\r")
  12474. (setq ov-prefix
  12475. (concat
  12476. (buffer-substring (1- (point)) (point))
  12477. (if (> (current-column) org-tags-column)
  12478. " "
  12479. (make-string (- org-tags-column (current-column)) ?\ ))))))
  12480. (move-overlay org-tags-overlay ov-start ov-end)
  12481. (save-window-excursion
  12482. (if expert
  12483. (set-buffer (get-buffer-create " *Org tags*"))
  12484. (delete-other-windows)
  12485. (split-window-vertically)
  12486. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  12487. (erase-buffer)
  12488. (org-set-local 'org-done-keywords done-keywords)
  12489. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  12490. (org-fast-tag-insert "Current" current c-face "\n\n")
  12491. (org-fast-tag-show-exit exit-after-next)
  12492. (org-set-current-tags-overlay current ov-prefix)
  12493. (setq tbl fulltable char ?a cnt 0)
  12494. (while (setq e (pop tbl))
  12495. (cond
  12496. ((equal (car e) :startgroup)
  12497. (push '() groups) (setq ingroup t)
  12498. (when (not (= cnt 0))
  12499. (setq cnt 0)
  12500. (insert "\n"))
  12501. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  12502. ((equal (car e) :endgroup)
  12503. (setq ingroup nil cnt 0)
  12504. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  12505. ((equal e '(:newline))
  12506. (when (not (= cnt 0))
  12507. (setq cnt 0)
  12508. (insert "\n")
  12509. (setq e (car tbl))
  12510. (while (equal (car tbl) '(:newline))
  12511. (insert "\n")
  12512. (setq tbl (cdr tbl)))))
  12513. (t
  12514. (setq tg (copy-sequence (car e)) c2 nil)
  12515. (if (cdr e)
  12516. (setq c (cdr e))
  12517. ;; automatically assign a character.
  12518. (setq c1 (string-to-char
  12519. (downcase (substring
  12520. tg (if (= (string-to-char tg) ?@) 1 0)))))
  12521. (if (or (rassoc c1 ntable) (rassoc c1 table))
  12522. (while (or (rassoc char ntable) (rassoc char table))
  12523. (setq char (1+ char)))
  12524. (setq c2 c1))
  12525. (setq c (or c2 char)))
  12526. (if ingroup (push tg (car groups)))
  12527. (setq tg (org-add-props tg nil 'face
  12528. (cond
  12529. ((not (assoc tg table))
  12530. (org-get-todo-face tg))
  12531. ((member tg current) c-face)
  12532. ((member tg inherited) i-face)
  12533. (t nil))))
  12534. (if (and (= cnt 0) (not ingroup)) (insert " "))
  12535. (insert "[" c "] " tg (make-string
  12536. (- fwidth 4 (length tg)) ?\ ))
  12537. (push (cons tg c) ntable)
  12538. (when (= (setq cnt (1+ cnt)) ncol)
  12539. (insert "\n")
  12540. (if ingroup (insert " "))
  12541. (setq cnt 0)))))
  12542. (setq ntable (nreverse ntable))
  12543. (insert "\n")
  12544. (goto-char (point-min))
  12545. (if (not expert) (org-fit-window-to-buffer))
  12546. (setq rtn
  12547. (catch 'exit
  12548. (while t
  12549. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  12550. (if (not groups) "no " "")
  12551. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  12552. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  12553. (setq org-last-tag-selection-key c)
  12554. (cond
  12555. ((= c ?\r) (throw 'exit t))
  12556. ((= c ?!)
  12557. (setq groups (not groups))
  12558. (goto-char (point-min))
  12559. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  12560. ((= c ?\C-c)
  12561. (if (not expert)
  12562. (org-fast-tag-show-exit
  12563. (setq exit-after-next (not exit-after-next)))
  12564. (setq expert nil)
  12565. (delete-other-windows)
  12566. (set-window-buffer (split-window-vertically) " *Org tags*")
  12567. (org-switch-to-buffer-other-window " *Org tags*")
  12568. (org-fit-window-to-buffer)))
  12569. ((or (= c ?\C-g)
  12570. (and (= c ?q) (not (rassoc c ntable))))
  12571. (org-detach-overlay org-tags-overlay)
  12572. (setq quit-flag t))
  12573. ((= c ?\ )
  12574. (setq current nil)
  12575. (if exit-after-next (setq exit-after-next 'now)))
  12576. ((= c ?\t)
  12577. (condition-case nil
  12578. (setq tg (org-icompleting-read
  12579. "Tag: "
  12580. (or buffer-tags
  12581. (with-current-buffer buf
  12582. (org-get-buffer-tags)))))
  12583. (quit (setq tg "")))
  12584. (when (string-match "\\S-" tg)
  12585. (add-to-list 'buffer-tags (list tg))
  12586. (if (member tg current)
  12587. (setq current (delete tg current))
  12588. (push tg current)))
  12589. (if exit-after-next (setq exit-after-next 'now)))
  12590. ((setq e (rassoc c todo-table) tg (car e))
  12591. (with-current-buffer buf
  12592. (save-excursion (org-todo tg)))
  12593. (if exit-after-next (setq exit-after-next 'now)))
  12594. ((setq e (rassoc c ntable) tg (car e))
  12595. (if (member tg current)
  12596. (setq current (delete tg current))
  12597. (loop for g in groups do
  12598. (if (member tg g)
  12599. (mapc (lambda (x)
  12600. (setq current (delete x current)))
  12601. g)))
  12602. (push tg current))
  12603. (if exit-after-next (setq exit-after-next 'now))))
  12604. ;; Create a sorted list
  12605. (setq current
  12606. (sort current
  12607. (lambda (a b)
  12608. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  12609. (if (eq exit-after-next 'now) (throw 'exit t))
  12610. (goto-char (point-min))
  12611. (beginning-of-line 2)
  12612. (delete-region (point) (point-at-eol))
  12613. (org-fast-tag-insert "Current" current c-face)
  12614. (org-set-current-tags-overlay current ov-prefix)
  12615. (while (re-search-forward
  12616. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  12617. (setq tg (match-string 1))
  12618. (add-text-properties
  12619. (match-beginning 1) (match-end 1)
  12620. (list 'face
  12621. (cond
  12622. ((member tg current) c-face)
  12623. ((member tg inherited) i-face)
  12624. (t (get-text-property (match-beginning 1) 'face))))))
  12625. (goto-char (point-min)))))
  12626. (org-detach-overlay org-tags-overlay)
  12627. (if rtn
  12628. (mapconcat 'identity current ":")
  12629. nil))))
  12630. (defun org-get-tags-string ()
  12631. "Get the TAGS string in the current headline."
  12632. (unless (org-at-heading-p t)
  12633. (error "Not on a heading"))
  12634. (save-excursion
  12635. (beginning-of-line 1)
  12636. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12637. (org-match-string-no-properties 1)
  12638. "")))
  12639. (defun org-get-tags ()
  12640. "Get the list of tags specified in the current headline."
  12641. (org-split-string (org-get-tags-string) ":"))
  12642. (defun org-get-buffer-tags ()
  12643. "Get a table of all tags used in the buffer, for completion."
  12644. (let (tags)
  12645. (save-excursion
  12646. (goto-char (point-min))
  12647. (while (re-search-forward
  12648. (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
  12649. (when (equal (char-after (point-at-bol 0)) ?*)
  12650. (mapc (lambda (x) (add-to-list 'tags x))
  12651. (org-split-string (org-match-string-no-properties 1) ":")))))
  12652. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  12653. (mapcar 'list tags)))
  12654. ;;;; The mapping API
  12655. ;;;###autoload
  12656. (defun org-map-entries (func &optional match scope &rest skip)
  12657. "Call FUNC at each headline selected by MATCH in SCOPE.
  12658. FUNC is a function or a lisp form. The function will be called without
  12659. arguments, with the cursor positioned at the beginning of the headline.
  12660. The return values of all calls to the function will be collected and
  12661. returned as a list.
  12662. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  12663. does not need to preserve point. After evaluation, the cursor will be
  12664. moved to the end of the line (presumably of the headline of the
  12665. processed entry) and search continues from there. Under some
  12666. circumstances, this may not produce the wanted results. For example,
  12667. if you have removed (e.g. archived) the current (sub)tree it could
  12668. mean that the next entry will be skipped entirely. In such cases, you
  12669. can specify the position from where search should continue by making
  12670. FUNC set the variable `org-map-continue-from' to the desired buffer
  12671. position.
  12672. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  12673. Only headlines that are matched by this query will be considered during
  12674. the iteration. When MATCH is nil or t, all headlines will be
  12675. visited by the iteration.
  12676. SCOPE determines the scope of this command. It can be any of:
  12677. nil The current buffer, respecting the restriction if any
  12678. tree The subtree started with the entry at point
  12679. region The entries within the active region, if any
  12680. region-start-level
  12681. The entries within the active region, but only those at
  12682. the same level than the first one.
  12683. file The current buffer, without restriction
  12684. file-with-archives
  12685. The current buffer, and any archives associated with it
  12686. agenda All agenda files
  12687. agenda-with-archives
  12688. All agenda files with any archive files associated with them
  12689. \(file1 file2 ...)
  12690. If this is a list, all files in the list will be scanned
  12691. The remaining args are treated as settings for the skipping facilities of
  12692. the scanner. The following items can be given here:
  12693. archive skip trees with the archive tag.
  12694. comment skip trees with the COMMENT keyword
  12695. function or Emacs Lisp form:
  12696. will be used as value for `org-agenda-skip-function', so whenever
  12697. the function returns t, FUNC will not be called for that
  12698. entry and search will continue from the point where the
  12699. function leaves it.
  12700. If your function needs to retrieve the tags including inherited tags
  12701. at the *current* entry, you can use the value of the variable
  12702. `org-scanner-tags' which will be much faster than getting the value
  12703. with `org-get-tags-at'. If your function gets properties with
  12704. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  12705. to t around the call to `org-entry-properties' to get the same speedup.
  12706. Note that if your function moves around to retrieve tags and properties at
  12707. a *different* entry, you cannot use these techniques."
  12708. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  12709. (not (org-region-active-p)))
  12710. (let* ((org-agenda-archives-mode nil) ; just to make sure
  12711. (org-agenda-skip-archived-trees (memq 'archive skip))
  12712. (org-agenda-skip-comment-trees (memq 'comment skip))
  12713. (org-agenda-skip-function
  12714. (car (org-delete-all '(comment archive) skip)))
  12715. (org-tags-match-list-sublevels t)
  12716. (start-level (eq scope 'region-start-level))
  12717. matcher file res
  12718. org-todo-keywords-for-agenda
  12719. org-done-keywords-for-agenda
  12720. org-todo-keyword-alist-for-agenda
  12721. org-drawers-for-agenda
  12722. org-tag-alist-for-agenda
  12723. todo-only)
  12724. (cond
  12725. ((eq match t) (setq matcher t))
  12726. ((eq match nil) (setq matcher t))
  12727. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  12728. (save-excursion
  12729. (save-restriction
  12730. (cond ((eq scope 'tree)
  12731. (org-back-to-heading t)
  12732. (org-narrow-to-subtree)
  12733. (setq scope nil))
  12734. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  12735. (org-region-active-p))
  12736. ;; If needed, set start-level to a string like "2"
  12737. (when start-level
  12738. (save-excursion
  12739. (goto-char (region-beginning))
  12740. (unless (org-at-heading-p) (outline-next-heading))
  12741. (setq start-level (org-current-level))))
  12742. (narrow-to-region (region-beginning)
  12743. (save-excursion
  12744. (goto-char (region-end))
  12745. (unless (and (bolp) (org-at-heading-p))
  12746. (outline-next-heading))
  12747. (point)))
  12748. (setq scope nil)))
  12749. (if (not scope)
  12750. (progn
  12751. (org-prepare-agenda-buffers
  12752. (list (buffer-file-name (current-buffer))))
  12753. (setq res (org-scan-tags func matcher todo-only start-level)))
  12754. ;; Get the right scope
  12755. (cond
  12756. ((and scope (listp scope) (symbolp (car scope)))
  12757. (setq scope (eval scope)))
  12758. ((eq scope 'agenda)
  12759. (setq scope (org-agenda-files t)))
  12760. ((eq scope 'agenda-with-archives)
  12761. (setq scope (org-agenda-files t))
  12762. (setq scope (org-add-archive-files scope)))
  12763. ((eq scope 'file)
  12764. (setq scope (list (buffer-file-name))))
  12765. ((eq scope 'file-with-archives)
  12766. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  12767. (org-prepare-agenda-buffers scope)
  12768. (while (setq file (pop scope))
  12769. (with-current-buffer (org-find-base-buffer-visiting file)
  12770. (save-excursion
  12771. (save-restriction
  12772. (widen)
  12773. (goto-char (point-min))
  12774. (setq res (append res (org-scan-tags func matcher todo-only))))))))))
  12775. res)))
  12776. ;;;; Properties
  12777. ;;; Setting and retrieving properties
  12778. (defconst org-special-properties
  12779. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  12780. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM")
  12781. "The special properties valid in Org-mode.
  12782. These are properties that are not defined in the property drawer,
  12783. but in some other way.")
  12784. (defconst org-default-properties
  12785. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  12786. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  12787. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  12788. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  12789. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  12790. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  12791. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  12792. "Some properties that are used by Org-mode for various purposes.
  12793. Being in this list makes sure that they are offered for completion.")
  12794. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  12795. "Regular expression matching the first line of a property drawer.")
  12796. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  12797. "Regular expression matching the last line of a property drawer.")
  12798. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  12799. "Regular expression matching the first line of a property drawer.")
  12800. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  12801. "Regular expression matching the first line of a property drawer.")
  12802. (defconst org-property-drawer-re
  12803. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  12804. org-property-end-re "\\)\n?")
  12805. "Matches an entire property drawer.")
  12806. (defconst org-clock-drawer-re
  12807. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  12808. org-property-end-re "\\)\n?")
  12809. "Matches an entire clock drawer.")
  12810. (defsubst org-re-property (property)
  12811. "Return a regexp matching PROPERTY.
  12812. Match group 1 will be set to the value "
  12813. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)"))
  12814. (defun org-property-action ()
  12815. "Do an action on properties."
  12816. (interactive)
  12817. (let (c)
  12818. (org-at-property-p)
  12819. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  12820. (setq c (read-char-exclusive))
  12821. (cond
  12822. ((equal c ?s)
  12823. (call-interactively 'org-set-property))
  12824. ((equal c ?d)
  12825. (call-interactively 'org-delete-property))
  12826. ((equal c ?D)
  12827. (call-interactively 'org-delete-property-globally))
  12828. ((equal c ?c)
  12829. (call-interactively 'org-compute-property-at-point))
  12830. (t (error "No such property action %c" c)))))
  12831. (defun org-set-effort (&optional value)
  12832. "Set the effort property of the current entry.
  12833. With numerical prefix arg, use the nth allowed value, 0 stands for the 10th
  12834. allowed value."
  12835. (interactive "P")
  12836. (if (equal value 0) (setq value 10))
  12837. (let* ((completion-ignore-case t)
  12838. (prop org-effort-property)
  12839. (cur (org-entry-get nil prop))
  12840. (allowed (org-property-get-allowed-values nil prop 'table))
  12841. (existing (mapcar 'list (org-property-values prop)))
  12842. rpl
  12843. (val (cond
  12844. ((stringp value) value)
  12845. ((and allowed (integerp value))
  12846. (or (car (nth (1- value) allowed))
  12847. (car (org-last allowed))))
  12848. (allowed
  12849. (message "Select 1-9,0, [RET%s]: %s"
  12850. (if cur (concat "=" cur) "")
  12851. (mapconcat 'car allowed " "))
  12852. (setq rpl (read-char-exclusive))
  12853. (if (equal rpl ?\r)
  12854. cur
  12855. (setq rpl (- rpl ?0))
  12856. (if (equal rpl 0) (setq rpl 10))
  12857. (if (and (> rpl 0) (<= rpl (length allowed)))
  12858. (car (nth (1- rpl) allowed))
  12859. (org-completing-read "Effort: " allowed nil))))
  12860. (t
  12861. (let (org-completion-use-ido org-completion-use-iswitchb)
  12862. (org-completing-read
  12863. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  12864. (concat "[" cur "]") "")
  12865. ": ")
  12866. existing nil nil "" nil cur))))))
  12867. (unless (equal (org-entry-get nil prop) val)
  12868. (org-entry-put nil prop val))
  12869. (message "%s is now %s" prop val)))
  12870. (defun org-at-property-p ()
  12871. "Is cursor inside a property drawer?"
  12872. (save-excursion
  12873. (beginning-of-line 1)
  12874. (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
  12875. (save-match-data ;; Used by calling procedures
  12876. (let ((p (point))
  12877. (range (unless (org-before-first-heading-p)
  12878. (org-get-property-block))))
  12879. (and range (<= (car range) p) (< p (cdr range))))))))
  12880. (defun org-get-property-block (&optional beg end force)
  12881. "Return the (beg . end) range of the body of the property drawer.
  12882. BEG and END can be beginning and end of subtree, if not given
  12883. they will be found.
  12884. If the drawer does not exist and FORCE is non-nil, create the drawer."
  12885. (catch 'exit
  12886. (save-excursion
  12887. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  12888. (end (or end (progn (outline-next-heading) (point)))))
  12889. (goto-char beg)
  12890. (if (re-search-forward org-property-start-re end t)
  12891. (setq beg (1+ (match-end 0)))
  12892. (if force
  12893. (save-excursion
  12894. (org-insert-property-drawer)
  12895. (setq end (progn (outline-next-heading) (point))))
  12896. (throw 'exit nil))
  12897. (goto-char beg)
  12898. (if (re-search-forward org-property-start-re end t)
  12899. (setq beg (1+ (match-end 0)))))
  12900. (if (re-search-forward org-property-end-re end t)
  12901. (setq end (match-beginning 0))
  12902. (or force (throw 'exit nil))
  12903. (goto-char beg)
  12904. (setq end beg)
  12905. (org-indent-line-function)
  12906. (insert ":END:\n"))
  12907. (cons beg end)))))
  12908. (defun org-entry-properties (&optional pom which specific)
  12909. "Get all properties of the entry at point-or-marker POM.
  12910. This includes the TODO keyword, the tags, time strings for deadline,
  12911. scheduled, and clocking, and any additional properties defined in the
  12912. entry. The return value is an alist, keys may occur multiple times
  12913. if the property key was used several times.
  12914. POM may also be nil, in which case the current entry is used.
  12915. If WHICH is nil or `all', get all properties. If WHICH is
  12916. `special' or `standard', only get that subclass. If WHICH
  12917. is a string only get exactly this property. SPECIFIC can be a string, the
  12918. specific property we are interested in. Specifying it can speed
  12919. things up because then unnecessary parsing is avoided."
  12920. (setq which (or which 'all))
  12921. (org-with-point-at pom
  12922. (let ((clockstr (substring org-clock-string 0 -1))
  12923. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  12924. (case-fold-search nil)
  12925. beg end range props sum-props key key1 value string clocksum)
  12926. (save-excursion
  12927. (when (condition-case nil
  12928. (and (derived-mode-p 'org-mode) (org-back-to-heading t))
  12929. (error nil))
  12930. (setq beg (point))
  12931. (setq sum-props (get-text-property (point) 'org-summaries))
  12932. (setq clocksum (get-text-property (point) :org-clock-minutes))
  12933. (outline-next-heading)
  12934. (setq end (point))
  12935. (when (memq which '(all special))
  12936. ;; Get the special properties, like TODO and tags
  12937. (goto-char beg)
  12938. (when (and (or (not specific) (string= specific "TODO"))
  12939. (looking-at org-todo-line-regexp) (match-end 2))
  12940. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  12941. (when (and (or (not specific) (string= specific "PRIORITY"))
  12942. (looking-at org-priority-regexp))
  12943. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  12944. (when (or (not specific) (string= specific "FILE"))
  12945. (push (cons "FILE" buffer-file-name) props))
  12946. (when (and (or (not specific) (string= specific "TAGS"))
  12947. (setq value (org-get-tags-string))
  12948. (string-match "\\S-" value))
  12949. (push (cons "TAGS" value) props))
  12950. (when (and (or (not specific) (string= specific "ALLTAGS"))
  12951. (setq value (org-get-tags-at)))
  12952. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  12953. ":"))
  12954. props))
  12955. (when (or (not specific) (string= specific "BLOCKED"))
  12956. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  12957. (when (or (not specific)
  12958. (member specific
  12959. '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
  12960. "TIMESTAMP" "TIMESTAMP_IA")))
  12961. (catch 'match
  12962. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  12963. (setq key (if (match-end 1)
  12964. (substring (org-match-string-no-properties 1)
  12965. 0 -1))
  12966. string (if (equal key clockstr)
  12967. (org-no-properties
  12968. (org-trim
  12969. (buffer-substring
  12970. (match-beginning 3) (goto-char
  12971. (point-at-eol)))))
  12972. (substring (org-match-string-no-properties 3)
  12973. 1 -1)))
  12974. ;; Get the correct property name from the key. This is
  12975. ;; necessary if the user has configured time keywords.
  12976. (setq key1 (concat key ":"))
  12977. (cond
  12978. ((not key)
  12979. (setq key
  12980. (if (= (char-after (match-beginning 3)) ?\[)
  12981. "TIMESTAMP_IA" "TIMESTAMP")))
  12982. ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
  12983. ((equal key1 org-deadline-string) (setq key "DEADLINE"))
  12984. ((equal key1 org-closed-string) (setq key "CLOSED"))
  12985. ((equal key1 org-clock-string) (setq key "CLOCK")))
  12986. (if (and specific (equal key specific) (not (equal key "CLOCK")))
  12987. (progn
  12988. (push (cons key string) props)
  12989. ;; no need to search further if match is found
  12990. (throw 'match t))
  12991. (when (or (equal key "CLOCK") (not (assoc key props)))
  12992. (push (cons key string) props))))))
  12993. )
  12994. (when (memq which '(all standard))
  12995. ;; Get the standard properties, like :PROP: ...
  12996. (setq range (org-get-property-block beg end))
  12997. (when range
  12998. (goto-char (car range))
  12999. (while (re-search-forward
  13000. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  13001. (cdr range) t)
  13002. (setq key (org-match-string-no-properties 1)
  13003. value (org-trim (or (org-match-string-no-properties 2) "")))
  13004. (unless (member key excluded)
  13005. (push (cons key (or value "")) props)))))
  13006. (if clocksum
  13007. (push (cons "CLOCKSUM"
  13008. (org-columns-number-to-string (/ (float clocksum) 60.)
  13009. 'add_times))
  13010. props))
  13011. (unless (assoc "CATEGORY" props)
  13012. (push (cons "CATEGORY" (org-get-category)) props))
  13013. (append sum-props (nreverse props)))))))
  13014. (defun org-entry-get (pom property &optional inherit literal-nil)
  13015. "Get value of PROPERTY for entry at point-or-marker POM.
  13016. If INHERIT is non-nil and the entry does not have the property,
  13017. then also check higher levels of the hierarchy.
  13018. If INHERIT is the symbol `selective', use inheritance only if the setting
  13019. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  13020. If the property is present but empty, the return value is the empty string.
  13021. If the property is not present at all, nil is returned.
  13022. If LITERAL-NIL is set, return the string value \"nil\" as a string,
  13023. do not interpret it as the list atom nil. This is used for inheritance
  13024. when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
  13025. (org-with-point-at pom
  13026. (if (and inherit (if (eq inherit 'selective)
  13027. (org-property-inherit-p property)
  13028. t))
  13029. (org-entry-get-with-inheritance property literal-nil)
  13030. (if (member property org-special-properties)
  13031. ;; We need a special property. Use `org-entry-properties' to
  13032. ;; retrieve it, but specify the wanted property
  13033. (cdr (assoc property (org-entry-properties nil 'special property)))
  13034. (let ((range (unless (org-before-first-heading-p)
  13035. (org-get-property-block)))
  13036. (props (list (or (assoc property org-file-properties)
  13037. (assoc property org-global-properties)
  13038. (assoc property org-global-properties-fixed))))
  13039. val)
  13040. (flet ((ap (key)
  13041. (when (re-search-forward
  13042. (org-re-property key) (cdr range) t)
  13043. (setq props
  13044. (org-update-property-plist
  13045. key
  13046. (if (match-end 1)
  13047. (org-match-string-no-properties 1) "")
  13048. props)))))
  13049. (when (and range (goto-char (car range)))
  13050. (ap property)
  13051. (goto-char (car range))
  13052. (while (ap (concat property "+")))
  13053. (setq val (cdr (assoc property props)))
  13054. (when val (if literal-nil val (org-not-nil val))))))))))
  13055. (defun org-property-or-variable-value (var &optional inherit)
  13056. "Check if there is a property fixing the value of VAR.
  13057. If yes, return this value. If not, return the current value of the variable."
  13058. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  13059. (if (and prop (stringp prop) (string-match "\\S-" prop))
  13060. (read prop)
  13061. (symbol-value var))))
  13062. (defun org-entry-delete (pom property)
  13063. "Delete the property PROPERTY from entry at point-or-marker POM."
  13064. (org-with-point-at pom
  13065. (if (member property org-special-properties)
  13066. nil ; cannot delete these properties.
  13067. (let ((range (org-get-property-block)))
  13068. (if (and range
  13069. (goto-char (car range))
  13070. (re-search-forward
  13071. (org-re-property property)
  13072. (cdr range) t))
  13073. (progn
  13074. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  13075. t)
  13076. nil)))))
  13077. ;; Multi-values properties are properties that contain multiple values
  13078. ;; These values are assumed to be single words, separated by whitespace.
  13079. (defun org-entry-add-to-multivalued-property (pom property value)
  13080. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  13081. (let* ((old (org-entry-get pom property))
  13082. (values (and old (org-split-string old "[ \t]"))))
  13083. (setq value (org-entry-protect-space value))
  13084. (unless (member value values)
  13085. (setq values (cons value values))
  13086. (org-entry-put pom property
  13087. (mapconcat 'identity values " ")))))
  13088. (defun org-entry-remove-from-multivalued-property (pom property value)
  13089. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  13090. (let* ((old (org-entry-get pom property))
  13091. (values (and old (org-split-string old "[ \t]"))))
  13092. (setq value (org-entry-protect-space value))
  13093. (when (member value values)
  13094. (setq values (delete value values))
  13095. (org-entry-put pom property
  13096. (mapconcat 'identity values " ")))))
  13097. (defun org-entry-member-in-multivalued-property (pom property value)
  13098. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  13099. (let* ((old (org-entry-get pom property))
  13100. (values (and old (org-split-string old "[ \t]"))))
  13101. (setq value (org-entry-protect-space value))
  13102. (member value values)))
  13103. (defun org-entry-get-multivalued-property (pom property)
  13104. "Return a list of values in a multivalued property."
  13105. (let* ((value (org-entry-get pom property))
  13106. (values (and value (org-split-string value "[ \t]"))))
  13107. (mapcar 'org-entry-restore-space values)))
  13108. (defun org-entry-put-multivalued-property (pom property &rest values)
  13109. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  13110. VALUES should be a list of strings. Spaces will be protected."
  13111. (org-entry-put pom property
  13112. (mapconcat 'org-entry-protect-space values " "))
  13113. (let* ((value (org-entry-get pom property))
  13114. (values (and value (org-split-string value "[ \t]"))))
  13115. (mapcar 'org-entry-restore-space values)))
  13116. (defun org-entry-protect-space (s)
  13117. "Protect spaces and newline in string S."
  13118. (while (string-match " " s)
  13119. (setq s (replace-match "%20" t t s)))
  13120. (while (string-match "\n" s)
  13121. (setq s (replace-match "%0A" t t s)))
  13122. s)
  13123. (defun org-entry-restore-space (s)
  13124. "Restore spaces and newline in string S."
  13125. (while (string-match "%20" s)
  13126. (setq s (replace-match " " t t s)))
  13127. (while (string-match "%0A" s)
  13128. (setq s (replace-match "\n" t t s)))
  13129. s)
  13130. (defvar org-entry-property-inherited-from (make-marker)
  13131. "Marker pointing to the entry from where a property was inherited.
  13132. Each call to `org-entry-get-with-inheritance' will set this marker to the
  13133. location of the entry where the inheritance search matched. If there was
  13134. no match, the marker will point nowhere.
  13135. Note that also `org-entry-get' calls this function, if the INHERIT flag
  13136. is set.")
  13137. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  13138. "Get entry property, and search higher levels if not present.
  13139. The search will stop at the first ancestor which has the property defined.
  13140. If the value found is \"nil\", return nil to show that the property
  13141. should be considered as undefined (this is the meaning of nil here).
  13142. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  13143. (move-marker org-entry-property-inherited-from nil)
  13144. (let (tmp)
  13145. (unless (org-before-first-heading-p)
  13146. (save-excursion
  13147. (save-restriction
  13148. (widen)
  13149. (catch 'ex
  13150. (while t
  13151. (when (setq tmp (org-entry-get nil property nil 'literal-nil))
  13152. (org-back-to-heading t)
  13153. (move-marker org-entry-property-inherited-from (point))
  13154. (throw 'ex tmp))
  13155. (or (org-up-heading-safe) (throw 'ex nil)))))))
  13156. (setq tmp (or tmp
  13157. (cdr (assoc property org-file-properties))
  13158. (cdr (assoc property org-global-properties))
  13159. (cdr (assoc property org-global-properties-fixed))))
  13160. (if literal-nil tmp (org-not-nil tmp))))
  13161. (defvar org-property-changed-functions nil
  13162. "Hook called when the value of a property has changed.
  13163. Each hook function should accept two arguments, the name of the property
  13164. and the new value.")
  13165. (defun org-entry-put (pom property value)
  13166. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  13167. (org-with-point-at pom
  13168. (org-back-to-heading t)
  13169. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  13170. range)
  13171. (cond
  13172. ((equal property "TODO")
  13173. (when (and (stringp value) (string-match "\\S-" value)
  13174. (not (member value org-todo-keywords-1)))
  13175. (error "\"%s\" is not a valid TODO state" value))
  13176. (if (or (not value)
  13177. (not (string-match "\\S-" value)))
  13178. (setq value 'none))
  13179. (org-todo value)
  13180. (org-set-tags nil 'align))
  13181. ((equal property "PRIORITY")
  13182. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  13183. (string-to-char value) ?\ ))
  13184. (org-set-tags nil 'align))
  13185. ((equal property "SCHEDULED")
  13186. (if (re-search-forward org-scheduled-time-regexp end t)
  13187. (cond
  13188. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13189. ((eq value 'later) (org-timestamp-change 1 'day))
  13190. (t (call-interactively 'org-schedule)))
  13191. (call-interactively 'org-schedule)))
  13192. ((equal property "DEADLINE")
  13193. (if (re-search-forward org-deadline-time-regexp end t)
  13194. (cond
  13195. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13196. ((eq value 'later) (org-timestamp-change 1 'day))
  13197. (t (call-interactively 'org-deadline)))
  13198. (call-interactively 'org-deadline)))
  13199. ((member property org-special-properties)
  13200. (error "The %s property can not yet be set with `org-entry-put'"
  13201. property))
  13202. (t ; a non-special property
  13203. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  13204. (setq range (org-get-property-block beg end 'force))
  13205. (goto-char (car range))
  13206. (if (re-search-forward
  13207. (org-re-property property) (cdr range) t)
  13208. (progn
  13209. (delete-region (match-beginning 0) (match-end 0))
  13210. (goto-char (match-beginning 0)))
  13211. (goto-char (cdr range))
  13212. (insert "\n")
  13213. (backward-char 1)
  13214. (org-indent-line-function))
  13215. (insert ":" property ":")
  13216. (and value (insert " " value))
  13217. (org-indent-line-function)))))
  13218. (run-hook-with-args 'org-property-changed-functions property value)))
  13219. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  13220. "Get all property keys in the current buffer.
  13221. With INCLUDE-SPECIALS, also list the special properties that reflect things
  13222. like tags and TODO state.
  13223. With INCLUDE-DEFAULTS, also include properties that has special meaning
  13224. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
  13225. and others.
  13226. With INCLUDE-COLUMNS, also include property names given in COLUMN
  13227. formats in the current buffer."
  13228. (let (rtn range cfmt s p)
  13229. (save-excursion
  13230. (save-restriction
  13231. (widen)
  13232. (goto-char (point-min))
  13233. (while (re-search-forward org-property-start-re nil t)
  13234. (setq range (org-get-property-block))
  13235. (goto-char (car range))
  13236. (while (re-search-forward
  13237. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  13238. (cdr range) t)
  13239. (add-to-list 'rtn (org-match-string-no-properties 1)))
  13240. (outline-next-heading))))
  13241. (when include-specials
  13242. (setq rtn (append org-special-properties rtn)))
  13243. (when include-defaults
  13244. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  13245. (add-to-list 'rtn org-effort-property))
  13246. (when include-columns
  13247. (save-excursion
  13248. (save-restriction
  13249. (widen)
  13250. (goto-char (point-min))
  13251. (while (re-search-forward
  13252. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  13253. nil t)
  13254. (setq cfmt (match-string 2) s 0)
  13255. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  13256. cfmt s)
  13257. (setq s (match-end 0)
  13258. p (match-string 1 cfmt))
  13259. (unless (or (equal p "ITEM")
  13260. (member p org-special-properties))
  13261. (add-to-list 'rtn (match-string 1 cfmt))))))))
  13262. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  13263. (defun org-property-values (key)
  13264. "Return a list of all values of property KEY in the current buffer."
  13265. (save-excursion
  13266. (save-restriction
  13267. (widen)
  13268. (goto-char (point-min))
  13269. (let ((re (org-re-property key))
  13270. values)
  13271. (while (re-search-forward re nil t)
  13272. (add-to-list 'values (org-trim (match-string 1))))
  13273. (delete "" values)))))
  13274. (defun org-insert-property-drawer ()
  13275. "Insert a property drawer into the current entry."
  13276. (org-back-to-heading t)
  13277. (looking-at org-outline-regexp)
  13278. (let ((indent (if org-adapt-indentation
  13279. (- (match-end 0) (match-beginning 0))
  13280. 0))
  13281. (beg (point))
  13282. (re (concat "^[ \t]*" org-keyword-time-regexp))
  13283. end hiddenp)
  13284. (outline-next-heading)
  13285. (setq end (point))
  13286. (goto-char beg)
  13287. (while (re-search-forward re end t))
  13288. (setq hiddenp (outline-invisible-p))
  13289. (end-of-line 1)
  13290. (and (equal (char-after) ?\n) (forward-char 1))
  13291. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  13292. (if (member (match-string 1) '("CLOCK:" ":END:"))
  13293. ;; just skip this line
  13294. (beginning-of-line 2)
  13295. ;; Drawer start, find the end
  13296. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  13297. (beginning-of-line 1)))
  13298. (org-skip-over-state-notes)
  13299. (skip-chars-backward " \t\n\r")
  13300. (if (eq (char-before) ?*) (forward-char 1))
  13301. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  13302. (beginning-of-line 0)
  13303. (org-indent-to-column indent)
  13304. (beginning-of-line 2)
  13305. (org-indent-to-column indent)
  13306. (beginning-of-line 0)
  13307. (if hiddenp
  13308. (save-excursion
  13309. (org-back-to-heading t)
  13310. (hide-entry))
  13311. (org-flag-drawer t))))
  13312. (defun org-insert-drawer (&optional arg drawer)
  13313. "Insert a drawer at point.
  13314. Optional argument DRAWER, when non-nil, is a string representing
  13315. drawer's name. Otherwise, the user is prompted for a name.
  13316. If a region is active, insert the drawer around that region
  13317. instead.
  13318. Point is left between drawer's boundaries."
  13319. (interactive "P")
  13320. (let* ((logbook (if (stringp org-log-into-drawer) org-log-into-drawer
  13321. "LOGBOOK"))
  13322. ;; SYSTEM-DRAWERS is a list of drawer names that are used
  13323. ;; internally by Org. They are meant to be inserted
  13324. ;; automatically.
  13325. (system-drawers `("CLOCK" ,logbook "PROPERTIES"))
  13326. ;; Remove system drawers from list. Note: For some reason,
  13327. ;; `org-completing-read' ignores the predicate while
  13328. ;; `completing-read' handles it fine.
  13329. (drawer (if arg "PROPERTIES"
  13330. (or drawer
  13331. (completing-read
  13332. "Drawer: " org-drawers
  13333. (lambda (d) (not (member d system-drawers))))))))
  13334. (cond
  13335. ;; With C-u, fall back on `org-insert-property-drawer'
  13336. (arg (org-insert-property-drawer))
  13337. ;; With an active region, insert a drawer at point.
  13338. ((not (org-region-active-p))
  13339. (progn
  13340. (unless (bolp) (insert "\n"))
  13341. (insert (format ":%s:\n\n:END:\n" drawer))
  13342. (forward-line -2)))
  13343. ;; Otherwise, insert the drawer at point
  13344. (t
  13345. (let ((rbeg (region-beginning))
  13346. (rend (copy-marker (region-end))))
  13347. (unwind-protect
  13348. (progn
  13349. (goto-char rbeg)
  13350. (beginning-of-line)
  13351. (when (save-excursion
  13352. (re-search-forward org-outline-regexp-bol rend t))
  13353. (error "Drawers cannot contain headlines"))
  13354. ;; Position point at the beginning of the first
  13355. ;; non-blank line in region. Insert drawer's opening
  13356. ;; there, then indent it.
  13357. (org-skip-whitespace)
  13358. (beginning-of-line)
  13359. (insert ":" drawer ":\n")
  13360. (forward-line -1)
  13361. (indent-for-tab-command)
  13362. ;; Move point to the beginning of the first blank line
  13363. ;; after the last non-blank line in region. Insert
  13364. ;; drawer's closing, then indent it.
  13365. (goto-char rend)
  13366. (skip-chars-backward " \r\t\n")
  13367. (insert "\n:END:")
  13368. (indent-for-tab-command)
  13369. (unless (eolp) (insert "\n")))
  13370. ;; Clear marker, whatever the outcome of insertion is.
  13371. (set-marker rend nil)))))))
  13372. (defvar org-property-set-functions-alist nil
  13373. "Property set function alist.
  13374. Each entry should have the following format:
  13375. (PROPERTY . READ-FUNCTION)
  13376. The read function will be called with the same argument as
  13377. `org-completing-read'.")
  13378. (defun org-set-property-function (property)
  13379. "Get the function that should be used to set PROPERTY.
  13380. This is computed according to `org-property-set-functions-alist'."
  13381. (or (cdr (assoc property org-property-set-functions-alist))
  13382. 'org-completing-read))
  13383. (defun org-read-property-value (property)
  13384. "Read PROPERTY value from user."
  13385. (let* ((completion-ignore-case t)
  13386. (allowed (org-property-get-allowed-values nil property 'table))
  13387. (cur (org-entry-get nil property))
  13388. (prompt (concat property " value"
  13389. (if (and cur (string-match "\\S-" cur))
  13390. (concat " [" cur "]") "") ": "))
  13391. (set-function (org-set-property-function property))
  13392. (val (if allowed
  13393. (funcall set-function prompt allowed nil
  13394. (not (get-text-property 0 'org-unrestricted
  13395. (caar allowed))))
  13396. (let (org-completion-use-ido org-completion-use-iswitchb)
  13397. (funcall set-function prompt
  13398. (mapcar 'list (org-property-values property))
  13399. nil nil "" nil cur)))))
  13400. (if (equal val "")
  13401. cur
  13402. val)))
  13403. (defvar org-last-set-property nil)
  13404. (defun org-read-property-name ()
  13405. "Read a property name."
  13406. (let* ((completion-ignore-case t)
  13407. (keys (org-buffer-property-keys nil t t))
  13408. (default-prop (or (save-excursion
  13409. (save-match-data
  13410. (beginning-of-line)
  13411. (and (looking-at "^\\s-*:\\([^:\n]+\\):")
  13412. (null (string= (match-string 1) "END"))
  13413. (match-string 1))))
  13414. org-last-set-property))
  13415. (property (org-icompleting-read
  13416. (concat "Property"
  13417. (if default-prop (concat " [" default-prop "]") "")
  13418. ": ")
  13419. (mapcar 'list keys)
  13420. nil nil nil nil
  13421. default-prop
  13422. )))
  13423. (if (member property keys)
  13424. property
  13425. (or (cdr (assoc (downcase property)
  13426. (mapcar (lambda (x) (cons (downcase x) x))
  13427. keys)))
  13428. property))))
  13429. (defun org-set-property (property value)
  13430. "In the current entry, set PROPERTY to VALUE.
  13431. When called interactively, this will prompt for a property name, offering
  13432. completion on existing and default properties. And then it will prompt
  13433. for a value, offering completion either on allowed values (via an inherited
  13434. xxx_ALL property) or on existing values in other instances of this property
  13435. in the current file."
  13436. (interactive (list nil nil))
  13437. (let* ((property (or property (org-read-property-name)))
  13438. (value (or value (org-read-property-value property)))
  13439. (fn (cdr (assoc property org-properties-postprocess-alist))))
  13440. (setq org-last-set-property property)
  13441. ;; Possibly postprocess the inserted value:
  13442. (when fn (setq value (funcall fn value)))
  13443. (unless (equal (org-entry-get nil property) value)
  13444. (org-entry-put nil property value))))
  13445. (defun org-delete-property (property)
  13446. "In the current entry, delete PROPERTY."
  13447. (interactive
  13448. (let* ((completion-ignore-case t)
  13449. (prop (org-icompleting-read "Property: "
  13450. (org-entry-properties nil 'standard))))
  13451. (list prop)))
  13452. (message "Property %s %s" property
  13453. (if (org-entry-delete nil property)
  13454. "deleted"
  13455. "was not present in the entry")))
  13456. (defun org-delete-property-globally (property)
  13457. "Remove PROPERTY globally, from all entries."
  13458. (interactive
  13459. (let* ((completion-ignore-case t)
  13460. (prop (org-icompleting-read
  13461. "Globally remove property: "
  13462. (mapcar 'list (org-buffer-property-keys)))))
  13463. (list prop)))
  13464. (save-excursion
  13465. (save-restriction
  13466. (widen)
  13467. (goto-char (point-min))
  13468. (let ((cnt 0))
  13469. (while (re-search-forward
  13470. (org-re-property property)
  13471. nil t)
  13472. (setq cnt (1+ cnt))
  13473. (delete-region (match-beginning 0) (1+ (point-at-eol))))
  13474. (message "Property \"%s\" removed from %d entries" property cnt)))))
  13475. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  13476. (defun org-compute-property-at-point ()
  13477. "Compute the property at point.
  13478. This looks for an enclosing column format, extracts the operator and
  13479. then applies it to the property in the column format's scope."
  13480. (interactive)
  13481. (unless (org-at-property-p)
  13482. (error "Not at a property"))
  13483. (let ((prop (org-match-string-no-properties 2)))
  13484. (org-columns-get-format-and-top-level)
  13485. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  13486. (error "No operator defined for property %s" prop))
  13487. (org-columns-compute prop)))
  13488. (defvar org-property-allowed-value-functions nil
  13489. "Hook for functions supplying allowed values for a specific property.
  13490. The functions must take a single argument, the name of the property, and
  13491. return a flat list of allowed values. If \":ETC\" is one of
  13492. the values, this means that these values are intended as defaults for
  13493. completion, but that other values should be allowed too.
  13494. The functions must return nil if they are not responsible for this
  13495. property.")
  13496. (defun org-property-get-allowed-values (pom property &optional table)
  13497. "Get allowed values for the property PROPERTY.
  13498. When TABLE is non-nil, return an alist that can directly be used for
  13499. completion."
  13500. (let (vals)
  13501. (cond
  13502. ((equal property "TODO")
  13503. (setq vals (org-with-point-at pom
  13504. (append org-todo-keywords-1 '("")))))
  13505. ((equal property "PRIORITY")
  13506. (let ((n org-lowest-priority))
  13507. (while (>= n org-highest-priority)
  13508. (push (char-to-string n) vals)
  13509. (setq n (1- n)))))
  13510. ((member property org-special-properties))
  13511. ((setq vals (run-hook-with-args-until-success
  13512. 'org-property-allowed-value-functions property)))
  13513. (t
  13514. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  13515. (when (and vals (string-match "\\S-" vals))
  13516. (setq vals (car (read-from-string (concat "(" vals ")"))))
  13517. (setq vals (mapcar (lambda (x)
  13518. (cond ((stringp x) x)
  13519. ((numberp x) (number-to-string x))
  13520. ((symbolp x) (symbol-name x))
  13521. (t "???")))
  13522. vals)))))
  13523. (when (member ":ETC" vals)
  13524. (setq vals (remove ":ETC" vals))
  13525. (org-add-props (car vals) '(org-unrestricted t)))
  13526. (if table (mapcar 'list vals) vals)))
  13527. (defun org-property-previous-allowed-value (&optional previous)
  13528. "Switch to the next allowed value for this property."
  13529. (interactive)
  13530. (org-property-next-allowed-value t))
  13531. (defun org-property-next-allowed-value (&optional previous)
  13532. "Switch to the next allowed value for this property."
  13533. (interactive)
  13534. (unless (org-at-property-p)
  13535. (error "Not at a property"))
  13536. (let* ((key (match-string 2))
  13537. (value (match-string 3))
  13538. (allowed (or (org-property-get-allowed-values (point) key)
  13539. (and (member value '("[ ]" "[-]" "[X]"))
  13540. '("[ ]" "[X]"))))
  13541. nval)
  13542. (unless allowed
  13543. (error "Allowed values for this property have not been defined"))
  13544. (if previous (setq allowed (reverse allowed)))
  13545. (if (member value allowed)
  13546. (setq nval (car (cdr (member value allowed)))))
  13547. (setq nval (or nval (car allowed)))
  13548. (if (equal nval value)
  13549. (error "Only one allowed value for this property"))
  13550. (org-at-property-p)
  13551. (replace-match (concat " :" key ": " nval) t t)
  13552. (org-indent-line-function)
  13553. (beginning-of-line 1)
  13554. (skip-chars-forward " \t")
  13555. (run-hook-with-args 'org-property-changed-functions key nval)))
  13556. (defun org-find-olp (path &optional this-buffer)
  13557. "Return a marker pointing to the entry at outline path OLP.
  13558. If anything goes wrong, throw an error.
  13559. You can wrap this call to catch the error like this:
  13560. (condition-case msg
  13561. (org-mobile-locate-entry (match-string 4))
  13562. (error (nth 1 msg)))
  13563. The return value will then be either a string with the error message,
  13564. or a marker if everything is OK.
  13565. If THIS-BUFFER is set, the outline path does not contain a file,
  13566. only headings."
  13567. (let* ((file (if this-buffer buffer-file-name (pop path)))
  13568. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  13569. (level 1)
  13570. (lmin 1)
  13571. (lmax 1)
  13572. limit re end found pos heading cnt flevel)
  13573. (unless buffer (error "File not found :%s" file))
  13574. (with-current-buffer buffer
  13575. (save-excursion
  13576. (save-restriction
  13577. (widen)
  13578. (setq limit (point-max))
  13579. (goto-char (point-min))
  13580. (while (setq heading (pop path))
  13581. (setq re (format org-complex-heading-regexp-format
  13582. (regexp-quote heading)))
  13583. (setq cnt 0 pos (point))
  13584. (while (re-search-forward re end t)
  13585. (setq level (- (match-end 1) (match-beginning 1)))
  13586. (if (and (>= level lmin) (<= level lmax))
  13587. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  13588. (when (= cnt 0) (error "Heading not found on level %d: %s"
  13589. lmax heading))
  13590. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  13591. lmax heading))
  13592. (goto-char found)
  13593. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  13594. (setq end (save-excursion (org-end-of-subtree t t))))
  13595. (when (org-at-heading-p)
  13596. (move-marker (make-marker) (point))))))))
  13597. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  13598. "Find node HEADING in BUFFER.
  13599. Return a marker to the heading if it was found, or nil if not.
  13600. If POS-ONLY is set, return just the position instead of a marker.
  13601. The heading text must match exact, but it may have a TODO keyword,
  13602. a priority cookie and tags in the standard locations."
  13603. (with-current-buffer (or buffer (current-buffer))
  13604. (save-excursion
  13605. (save-restriction
  13606. (widen)
  13607. (goto-char (point-min))
  13608. (let (case-fold-search)
  13609. (if (re-search-forward
  13610. (format org-complex-heading-regexp-format
  13611. (regexp-quote heading)) nil t)
  13612. (if pos-only
  13613. (match-beginning 0)
  13614. (move-marker (make-marker) (match-beginning 0)))))))))
  13615. (defun org-find-exact-heading-in-directory (heading &optional dir)
  13616. "Find Org node headline HEADING in all .org files in directory DIR.
  13617. When the target headline is found, return a marker to this location."
  13618. (let ((files (directory-files (or dir default-directory)
  13619. nil "\\`[^.#].*\\.org\\'"))
  13620. file visiting m buffer)
  13621. (catch 'found
  13622. (while (setq file (pop files))
  13623. (message "trying %s" file)
  13624. (setq visiting (org-find-base-buffer-visiting file))
  13625. (setq buffer (or visiting (find-file-noselect file)))
  13626. (setq m (org-find-exact-headline-in-buffer
  13627. heading buffer))
  13628. (when (and (not m) (not visiting)) (kill-buffer buffer))
  13629. (and m (throw 'found m))))))
  13630. (defun org-find-entry-with-id (ident)
  13631. "Locate the entry that contains the ID property with exact value IDENT.
  13632. IDENT can be a string, a symbol or a number, this function will search for
  13633. the string representation of it.
  13634. Return the position where this entry starts, or nil if there is no such entry."
  13635. (interactive "sID: ")
  13636. (let ((id (cond
  13637. ((stringp ident) ident)
  13638. ((symbol-name ident) (symbol-name ident))
  13639. ((numberp ident) (number-to-string ident))
  13640. (t (error "IDENT %s must be a string, symbol or number" ident))))
  13641. (case-fold-search nil))
  13642. (save-excursion
  13643. (save-restriction
  13644. (widen)
  13645. (goto-char (point-min))
  13646. (when (re-search-forward
  13647. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  13648. nil t)
  13649. (org-back-to-heading t)
  13650. (point))))))
  13651. ;;;; Timestamps
  13652. (defvar org-last-changed-timestamp nil)
  13653. (defvar org-last-inserted-timestamp nil
  13654. "The last time stamp inserted with `org-insert-time-stamp'.")
  13655. (defvar org-time-was-given) ; dynamically scoped parameter
  13656. (defvar org-end-time-was-given) ; dynamically scoped parameter
  13657. (defvar org-ts-what) ; dynamically scoped parameter
  13658. (defun org-time-stamp (arg &optional inactive)
  13659. "Prompt for a date/time and insert a time stamp.
  13660. If the user specifies a time like HH:MM, or if this command is called
  13661. with a prefix argument, the time stamp will contain date and time.
  13662. Otherwise, only the date will be included. All parts of a date not
  13663. specified by the user will be filled in from the current date/time.
  13664. So if you press just return without typing anything, the time stamp
  13665. will represent the current date/time. If there is already a timestamp
  13666. at the cursor, it will be modified."
  13667. (interactive "P")
  13668. (let* ((ts nil)
  13669. (default-time
  13670. ;; Default time is either today, or, when entering a range,
  13671. ;; the range start.
  13672. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  13673. (save-excursion
  13674. (re-search-backward
  13675. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  13676. (- (point) 20) t)))
  13677. (apply 'encode-time (org-parse-time-string (match-string 1)))
  13678. (current-time)))
  13679. (default-input (and ts (org-get-compact-tod ts)))
  13680. (repeater (save-excursion
  13681. (save-match-data
  13682. (beginning-of-line)
  13683. (when (re-search-forward
  13684. "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  13685. (save-excursion (progn (end-of-line) (point))) t)
  13686. (match-string 0)))))
  13687. org-time-was-given org-end-time-was-given time)
  13688. (cond
  13689. ((and (org-at-timestamp-p t)
  13690. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  13691. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  13692. (insert "--")
  13693. (setq time (let ((this-command this-command))
  13694. (org-read-date arg 'totime nil nil
  13695. default-time default-input inactive)))
  13696. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  13697. ((org-at-timestamp-p t)
  13698. (setq time (let ((this-command this-command))
  13699. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  13700. (when (org-at-timestamp-p t) ; just to get the match data
  13701. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  13702. (replace-match "")
  13703. (setq org-last-changed-timestamp
  13704. (org-insert-time-stamp
  13705. time (or org-time-was-given arg)
  13706. inactive nil nil (list org-end-time-was-given)))
  13707. (when repeater (goto-char (1- (point))) (insert " " repeater)
  13708. (setq org-last-changed-timestamp
  13709. (concat (substring org-last-inserted-timestamp 0 -1)
  13710. " " repeater ">"))))
  13711. (message "Timestamp updated"))
  13712. (t
  13713. (setq time (let ((this-command this-command))
  13714. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  13715. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  13716. nil nil (list org-end-time-was-given))))))
  13717. ;; FIXME: can we use this for something else, like computing time differences?
  13718. (defun org-get-compact-tod (s)
  13719. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  13720. (let* ((t1 (match-string 1 s))
  13721. (h1 (string-to-number (match-string 2 s)))
  13722. (m1 (string-to-number (match-string 3 s)))
  13723. (t2 (and (match-end 4) (match-string 5 s)))
  13724. (h2 (and t2 (string-to-number (match-string 6 s))))
  13725. (m2 (and t2 (string-to-number (match-string 7 s))))
  13726. dh dm)
  13727. (if (not t2)
  13728. t1
  13729. (setq dh (- h2 h1) dm (- m2 m1))
  13730. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  13731. (concat t1 "+" (number-to-string dh)
  13732. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  13733. (defun org-time-stamp-inactive (&optional arg)
  13734. "Insert an inactive time stamp.
  13735. An inactive time stamp is enclosed in square brackets instead of angle
  13736. brackets. It is inactive in the sense that it does not trigger agenda entries,
  13737. does not link to the calendar and cannot be changed with the S-cursor keys.
  13738. So these are more for recording a certain time/date."
  13739. (interactive "P")
  13740. (org-time-stamp arg 'inactive))
  13741. (defvar org-date-ovl (make-overlay 1 1))
  13742. (overlay-put org-date-ovl 'face 'org-date-selected)
  13743. (org-detach-overlay org-date-ovl)
  13744. (defvar org-ans1) ; dynamically scoped parameter
  13745. (defvar org-ans2) ; dynamically scoped parameter
  13746. (defvar org-plain-time-of-day-regexp) ; defined below
  13747. (defvar org-overriding-default-time nil) ; dynamically scoped
  13748. (defvar org-read-date-overlay nil)
  13749. (defvar org-dcst nil) ; dynamically scoped
  13750. (defvar org-read-date-history nil)
  13751. (defvar org-read-date-final-answer nil)
  13752. (defvar org-read-date-analyze-futurep nil)
  13753. (defvar org-read-date-analyze-forced-year nil)
  13754. (defvar org-read-date-inactive)
  13755. (defun org-read-date (&optional org-with-time to-time from-string prompt
  13756. default-time default-input inactive)
  13757. "Read a date, possibly a time, and make things smooth for the user.
  13758. The prompt will suggest to enter an ISO date, but you can also enter anything
  13759. which will at least partially be understood by `parse-time-string'.
  13760. Unrecognized parts of the date will default to the current day, month, year,
  13761. hour and minute. If this command is called to replace a timestamp at point,
  13762. or to enter the second timestamp of a range, the default time is taken
  13763. from the existing stamp. Furthermore, the command prefers the future,
  13764. so if you are giving a date where the year is not given, and the day-month
  13765. combination is already past in the current year, it will assume you
  13766. mean next year. For details, see the manual. A few examples:
  13767. 3-2-5 --> 2003-02-05
  13768. feb 15 --> currentyear-02-15
  13769. 2/15 --> currentyear-02-15
  13770. sep 12 9 --> 2009-09-12
  13771. 12:45 --> today 12:45
  13772. 22 sept 0:34 --> currentyear-09-22 0:34
  13773. 12 --> currentyear-currentmonth-12
  13774. Fri --> nearest Friday (today or later)
  13775. etc.
  13776. Furthermore you can specify a relative date by giving, as the *first* thing
  13777. in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
  13778. change in days weeks, months, years.
  13779. With a single plus or minus, the date is relative to today. With a double
  13780. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  13781. +4d --> four days from today
  13782. +4 --> same as above
  13783. +2w --> two weeks from today
  13784. ++5 --> five days from default date
  13785. The function understands only English month and weekday abbreviations.
  13786. While prompting, a calendar is popped up - you can also select the
  13787. date with the mouse (button 1). The calendar shows a period of three
  13788. months. To scroll it to other months, use the keys `>' and `<'.
  13789. If you don't like the calendar, turn it off with
  13790. \(setq org-read-date-popup-calendar nil)
  13791. With optional argument TO-TIME, the date will immediately be converted
  13792. to an internal time.
  13793. With an optional argument WITH-TIME, the prompt will suggest to also
  13794. insert a time. Note that when WITH-TIME is not set, you can still
  13795. enter a time, and this function will inform the calling routine about
  13796. this change. The calling routine may then choose to change the format
  13797. used to insert the time stamp into the buffer to include the time.
  13798. With optional argument FROM-STRING, read from this string instead from
  13799. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  13800. the time/date that is used for everything that is not specified by the
  13801. user."
  13802. (require 'parse-time)
  13803. (let* ((org-time-stamp-rounding-minutes
  13804. (if (equal org-with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  13805. (org-dcst org-display-custom-times)
  13806. (ct (org-current-time))
  13807. (org-def (or org-overriding-default-time default-time ct))
  13808. (org-defdecode (decode-time org-def))
  13809. (dummy (progn
  13810. (when (< (nth 2 org-defdecode) org-extend-today-until)
  13811. (setcar (nthcdr 2 org-defdecode) -1)
  13812. (setcar (nthcdr 1 org-defdecode) 59)
  13813. (setq org-def (apply 'encode-time org-defdecode)
  13814. org-defdecode (decode-time org-def)))))
  13815. (calendar-frame-setup nil)
  13816. (calendar-setup nil)
  13817. (calendar-move-hook nil)
  13818. (calendar-view-diary-initially-flag nil)
  13819. (calendar-view-holidays-initially-flag nil)
  13820. (timestr (format-time-string
  13821. (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") org-def))
  13822. (prompt (concat (if prompt (concat prompt " ") "")
  13823. (format "Date+time [%s]: " timestr)))
  13824. ans (org-ans0 "") org-ans1 org-ans2 final)
  13825. (cond
  13826. (from-string (setq ans from-string))
  13827. (org-read-date-popup-calendar
  13828. (save-excursion
  13829. (save-window-excursion
  13830. (calendar)
  13831. (org-eval-in-calendar '(setq cursor-type nil) t)
  13832. (unwind-protect
  13833. (progn
  13834. (calendar-forward-day (- (time-to-days org-def)
  13835. (calendar-absolute-from-gregorian
  13836. (calendar-current-date))))
  13837. (org-eval-in-calendar nil t)
  13838. (let* ((old-map (current-local-map))
  13839. (map (copy-keymap calendar-mode-map))
  13840. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  13841. (org-defkey map (kbd "RET") 'org-calendar-select)
  13842. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  13843. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  13844. (org-defkey minibuffer-local-map [(meta shift left)]
  13845. (lambda () (interactive)
  13846. (org-eval-in-calendar '(calendar-backward-month 1))))
  13847. (org-defkey minibuffer-local-map [(meta shift right)]
  13848. (lambda () (interactive)
  13849. (org-eval-in-calendar '(calendar-forward-month 1))))
  13850. (org-defkey minibuffer-local-map [(meta shift up)]
  13851. (lambda () (interactive)
  13852. (org-eval-in-calendar '(calendar-backward-year 1))))
  13853. (org-defkey minibuffer-local-map [(meta shift down)]
  13854. (lambda () (interactive)
  13855. (org-eval-in-calendar '(calendar-forward-year 1))))
  13856. (org-defkey minibuffer-local-map [?\e (shift left)]
  13857. (lambda () (interactive)
  13858. (org-eval-in-calendar '(calendar-backward-month 1))))
  13859. (org-defkey minibuffer-local-map [?\e (shift right)]
  13860. (lambda () (interactive)
  13861. (org-eval-in-calendar '(calendar-forward-month 1))))
  13862. (org-defkey minibuffer-local-map [?\e (shift up)]
  13863. (lambda () (interactive)
  13864. (org-eval-in-calendar '(calendar-backward-year 1))))
  13865. (org-defkey minibuffer-local-map [?\e (shift down)]
  13866. (lambda () (interactive)
  13867. (org-eval-in-calendar '(calendar-forward-year 1))))
  13868. (org-defkey minibuffer-local-map [(shift up)]
  13869. (lambda () (interactive)
  13870. (org-eval-in-calendar '(calendar-backward-week 1))))
  13871. (org-defkey minibuffer-local-map [(shift down)]
  13872. (lambda () (interactive)
  13873. (org-eval-in-calendar '(calendar-forward-week 1))))
  13874. (org-defkey minibuffer-local-map [(shift left)]
  13875. (lambda () (interactive)
  13876. (org-eval-in-calendar '(calendar-backward-day 1))))
  13877. (org-defkey minibuffer-local-map [(shift right)]
  13878. (lambda () (interactive)
  13879. (org-eval-in-calendar '(calendar-forward-day 1))))
  13880. (org-defkey minibuffer-local-map ">"
  13881. (lambda () (interactive)
  13882. (org-eval-in-calendar '(scroll-calendar-left 1))))
  13883. (org-defkey minibuffer-local-map "<"
  13884. (lambda () (interactive)
  13885. (org-eval-in-calendar '(scroll-calendar-right 1))))
  13886. (org-defkey minibuffer-local-map "\C-v"
  13887. (lambda () (interactive)
  13888. (org-eval-in-calendar
  13889. '(calendar-scroll-left-three-months 1))))
  13890. (org-defkey minibuffer-local-map "\M-v"
  13891. (lambda () (interactive)
  13892. (org-eval-in-calendar
  13893. '(calendar-scroll-right-three-months 1))))
  13894. (run-hooks 'org-read-date-minibuffer-setup-hook)
  13895. (unwind-protect
  13896. (progn
  13897. (use-local-map map)
  13898. (setq org-read-date-inactive inactive)
  13899. (add-hook 'post-command-hook 'org-read-date-display)
  13900. (setq org-ans0 (read-string prompt default-input
  13901. 'org-read-date-history nil))
  13902. ;; org-ans0: from prompt
  13903. ;; org-ans1: from mouse click
  13904. ;; org-ans2: from calendar motion
  13905. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  13906. (remove-hook 'post-command-hook 'org-read-date-display)
  13907. (use-local-map old-map)
  13908. (when org-read-date-overlay
  13909. (delete-overlay org-read-date-overlay)
  13910. (setq org-read-date-overlay nil)))))
  13911. (bury-buffer "*Calendar*")))))
  13912. (t ; Naked prompt only
  13913. (unwind-protect
  13914. (setq ans (read-string prompt default-input
  13915. 'org-read-date-history timestr))
  13916. (when org-read-date-overlay
  13917. (delete-overlay org-read-date-overlay)
  13918. (setq org-read-date-overlay nil)))))
  13919. (setq final (org-read-date-analyze ans org-def org-defdecode))
  13920. (when org-read-date-analyze-forced-year
  13921. (message "Year was forced into %s"
  13922. (if org-read-date-force-compatible-dates
  13923. "compatible range (1970-2037)"
  13924. "range representable on this machine"))
  13925. (ding))
  13926. ;; One round trip to get rid of 34th of August and stuff like that....
  13927. (setq final (decode-time (apply 'encode-time final)))
  13928. (setq org-read-date-final-answer ans)
  13929. (if to-time
  13930. (apply 'encode-time final)
  13931. (if (and (boundp 'org-time-was-given) org-time-was-given)
  13932. (format "%04d-%02d-%02d %02d:%02d"
  13933. (nth 5 final) (nth 4 final) (nth 3 final)
  13934. (nth 2 final) (nth 1 final))
  13935. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  13936. (defvar org-def)
  13937. (defvar org-defdecode)
  13938. (defvar org-with-time)
  13939. (defun org-read-date-display ()
  13940. "Display the current date prompt interpretation in the minibuffer."
  13941. (when org-read-date-display-live
  13942. (when org-read-date-overlay
  13943. (delete-overlay org-read-date-overlay))
  13944. (when (minibufferp (current-buffer))
  13945. (save-excursion
  13946. (end-of-line 1)
  13947. (while (not (equal (buffer-substring
  13948. (max (point-min) (- (point) 4)) (point))
  13949. " "))
  13950. (insert " ")))
  13951. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  13952. " " (or org-ans1 org-ans2)))
  13953. (org-end-time-was-given nil)
  13954. (f (org-read-date-analyze ans org-def org-defdecode))
  13955. (fmts (if org-dcst
  13956. org-time-stamp-custom-formats
  13957. org-time-stamp-formats))
  13958. (fmt (if (or org-with-time
  13959. (and (boundp 'org-time-was-given) org-time-was-given))
  13960. (cdr fmts)
  13961. (car fmts)))
  13962. (txt (format-time-string fmt (apply 'encode-time f)))
  13963. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  13964. (txt (concat "=> " txt)))
  13965. (when (and org-end-time-was-given
  13966. (string-match org-plain-time-of-day-regexp txt))
  13967. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  13968. org-end-time-was-given
  13969. (substring txt (match-end 0)))))
  13970. (when org-read-date-analyze-futurep
  13971. (setq txt (concat txt " (=>F)")))
  13972. (setq org-read-date-overlay
  13973. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  13974. (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
  13975. (defun org-read-date-analyze (ans org-def org-defdecode)
  13976. "Analyze the combined answer of the date prompt."
  13977. ;; FIXME: cleanup and comment
  13978. (let ((nowdecode (decode-time (current-time)))
  13979. delta deltan deltaw deltadef year month day
  13980. hour minute second wday pm h2 m2 tl wday1
  13981. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  13982. (setq org-read-date-analyze-futurep nil
  13983. org-read-date-analyze-forced-year nil)
  13984. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  13985. (setq ans "+0"))
  13986. (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
  13987. (setq ans (replace-match "" t t ans)
  13988. deltan (car delta)
  13989. deltaw (nth 1 delta)
  13990. deltadef (nth 2 delta)))
  13991. ;; Check if there is an iso week date in there
  13992. ;; If yes, store the info and postpone interpreting it until the rest
  13993. ;; of the parsing is done
  13994. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  13995. (setq iso-year (if (match-end 1)
  13996. (org-small-year-to-year
  13997. (string-to-number (match-string 1 ans))))
  13998. iso-weekday (if (match-end 3)
  13999. (string-to-number (match-string 3 ans)))
  14000. iso-week (string-to-number (match-string 2 ans)))
  14001. (setq ans (replace-match "" t t ans)))
  14002. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  14003. (when (string-match
  14004. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  14005. (setq year (if (match-end 2)
  14006. (string-to-number (match-string 2 ans))
  14007. (progn (setq kill-year t)
  14008. (string-to-number (format-time-string "%Y"))))
  14009. month (string-to-number (match-string 3 ans))
  14010. day (string-to-number (match-string 4 ans)))
  14011. (if (< year 100) (setq year (+ 2000 year)))
  14012. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  14013. t nil ans)))
  14014. ;; Help matching dotted european dates
  14015. (when (string-match
  14016. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\. ?\\([1-9][0-9][0-9][0-9]\\)?" ans)
  14017. (setq year (if (match-end 3)
  14018. (string-to-number (match-string 3 ans))
  14019. (progn (setq kill-year t)
  14020. (string-to-number (format-time-string "%Y"))))
  14021. day (string-to-number (match-string 1 ans))
  14022. month (string-to-number (match-string 2 ans))
  14023. ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  14024. t nil ans)))
  14025. ;; Help matching american dates, like 5/30 or 5/30/7
  14026. (when (string-match
  14027. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  14028. (setq year (if (match-end 4)
  14029. (string-to-number (match-string 4 ans))
  14030. (progn (setq kill-year t)
  14031. (string-to-number (format-time-string "%Y"))))
  14032. month (string-to-number (match-string 1 ans))
  14033. day (string-to-number (match-string 2 ans)))
  14034. (if (< year 100) (setq year (+ 2000 year)))
  14035. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  14036. t nil ans)))
  14037. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  14038. ;; If there is a time with am/pm, and *no* time without it, we convert
  14039. ;; so that matching will be successful.
  14040. (loop for i from 1 to 2 do ; twice, for end time as well
  14041. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  14042. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  14043. (setq hour (string-to-number (match-string 1 ans))
  14044. minute (if (match-end 3)
  14045. (string-to-number (match-string 3 ans))
  14046. 0)
  14047. pm (equal ?p
  14048. (string-to-char (downcase (match-string 4 ans)))))
  14049. (if (and (= hour 12) (not pm))
  14050. (setq hour 0)
  14051. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  14052. (setq ans (replace-match (format "%02d:%02d" hour minute)
  14053. t t ans))))
  14054. ;; Check if a time range is given as a duration
  14055. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  14056. (setq hour (string-to-number (match-string 1 ans))
  14057. h2 (+ hour (string-to-number (match-string 3 ans)))
  14058. minute (string-to-number (match-string 2 ans))
  14059. m2 (+ minute (if (match-end 5) (string-to-number
  14060. (match-string 5 ans))0)))
  14061. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  14062. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  14063. t t ans)))
  14064. ;; Check if there is a time range
  14065. (when (boundp 'org-end-time-was-given)
  14066. (setq org-time-was-given nil)
  14067. (when (and (string-match org-plain-time-of-day-regexp ans)
  14068. (match-end 8))
  14069. (setq org-end-time-was-given (match-string 8 ans))
  14070. (setq ans (concat (substring ans 0 (match-beginning 7))
  14071. (substring ans (match-end 7))))))
  14072. (setq tl (parse-time-string ans)
  14073. day (or (nth 3 tl) (nth 3 org-defdecode))
  14074. month (or (nth 4 tl)
  14075. (if (and org-read-date-prefer-future
  14076. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  14077. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  14078. (nth 4 org-defdecode)))
  14079. year (or (and (not kill-year) (nth 5 tl))
  14080. (if (and org-read-date-prefer-future
  14081. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  14082. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  14083. (nth 5 org-defdecode)))
  14084. hour (or (nth 2 tl) (nth 2 org-defdecode))
  14085. minute (or (nth 1 tl) (nth 1 org-defdecode))
  14086. second (or (nth 0 tl) 0)
  14087. wday (nth 6 tl))
  14088. (when (and (eq org-read-date-prefer-future 'time)
  14089. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  14090. (equal day (nth 3 nowdecode))
  14091. (equal month (nth 4 nowdecode))
  14092. (equal year (nth 5 nowdecode))
  14093. (nth 2 tl)
  14094. (or (< (nth 2 tl) (nth 2 nowdecode))
  14095. (and (= (nth 2 tl) (nth 2 nowdecode))
  14096. (nth 1 tl)
  14097. (< (nth 1 tl) (nth 1 nowdecode)))))
  14098. (setq day (1+ day)
  14099. futurep t))
  14100. ;; Special date definitions below
  14101. (cond
  14102. (iso-week
  14103. ;; There was an iso week
  14104. (require 'cal-iso)
  14105. (setq futurep nil)
  14106. (setq year (or iso-year year)
  14107. day (or iso-weekday wday 1)
  14108. wday nil ; to make sure that the trigger below does not match
  14109. iso-date (calendar-gregorian-from-absolute
  14110. (calendar-absolute-from-iso
  14111. (list iso-week day year))))
  14112. ; FIXME: Should we also push ISO weeks into the future?
  14113. ; (when (and org-read-date-prefer-future
  14114. ; (not iso-year)
  14115. ; (< (calendar-absolute-from-gregorian iso-date)
  14116. ; (time-to-days (current-time))))
  14117. ; (setq year (1+ year)
  14118. ; iso-date (calendar-gregorian-from-absolute
  14119. ; (calendar-absolute-from-iso
  14120. ; (list iso-week day year)))))
  14121. (setq month (car iso-date)
  14122. year (nth 2 iso-date)
  14123. day (nth 1 iso-date)))
  14124. (deltan
  14125. (setq futurep nil)
  14126. (unless deltadef
  14127. (let ((now (decode-time (current-time))))
  14128. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  14129. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  14130. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  14131. ((equal deltaw "m") (setq month (+ month deltan)))
  14132. ((equal deltaw "y") (setq year (+ year deltan)))))
  14133. ((and wday (not (nth 3 tl)))
  14134. (setq futurep nil)
  14135. ;; Weekday was given, but no day, so pick that day in the week
  14136. ;; on or after the derived date.
  14137. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  14138. (unless (equal wday wday1)
  14139. (setq day (+ day (% (- wday wday1 -7) 7))))))
  14140. (if (and (boundp 'org-time-was-given)
  14141. (nth 2 tl))
  14142. (setq org-time-was-given t))
  14143. (if (< year 100) (setq year (+ 2000 year)))
  14144. ;; Check of the date is representable
  14145. (if org-read-date-force-compatible-dates
  14146. (progn
  14147. (if (< year 1970)
  14148. (setq year 1970 org-read-date-analyze-forced-year t))
  14149. (if (> year 2037)
  14150. (setq year 2037 org-read-date-analyze-forced-year t)))
  14151. (condition-case nil
  14152. (ignore (encode-time second minute hour day month year))
  14153. (error
  14154. (setq year (nth 5 org-defdecode))
  14155. (setq org-read-date-analyze-forced-year t))))
  14156. (setq org-read-date-analyze-futurep futurep)
  14157. (list second minute hour day month year)))
  14158. (defvar parse-time-weekdays)
  14159. (defun org-read-date-get-relative (s today default)
  14160. "Check string S for special relative date string.
  14161. TODAY and DEFAULT are internal times, for today and for a default.
  14162. Return shift list (N what def-flag)
  14163. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  14164. N is the number of WHATs to shift.
  14165. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  14166. the DEFAULT date rather than TODAY."
  14167. (require 'parse-time)
  14168. (when (and
  14169. (string-match
  14170. (concat
  14171. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  14172. "\\([0-9]+\\)?"
  14173. "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  14174. "\\([ \t]\\|$\\)") s)
  14175. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  14176. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  14177. (string-to-char (substring (match-string 1 s) -1))
  14178. ?+))
  14179. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  14180. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  14181. (what (if (match-end 3) (match-string 3 s) "d"))
  14182. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  14183. (date (if rel default today))
  14184. (wday (nth 6 (decode-time date)))
  14185. delta)
  14186. (if wday1
  14187. (progn
  14188. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  14189. (if (= dir ?-) (setq delta (- delta 7)))
  14190. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  14191. (list delta "d" rel))
  14192. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  14193. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  14194. "Turn a user-specified date into the internal representation.
  14195. The internal representation needed by the calendar is (month day year).
  14196. This is a wrapper to handle the brain-dead convention in calendar that
  14197. user function argument order change dependent on argument order."
  14198. (if (boundp 'calendar-date-style)
  14199. (cond
  14200. ((eq calendar-date-style 'american)
  14201. (list arg1 arg2 arg3))
  14202. ((eq calendar-date-style 'european)
  14203. (list arg2 arg1 arg3))
  14204. ((eq calendar-date-style 'iso)
  14205. (list arg2 arg3 arg1)))
  14206. (with-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  14207. (if (org-bound-and-true-p european-calendar-style)
  14208. (list arg2 arg1 arg3)
  14209. (list arg1 arg2 arg3)))))
  14210. (defun org-eval-in-calendar (form &optional keepdate)
  14211. "Eval FORM in the calendar window and return to current window.
  14212. When KEEPDATE is non-nil, update `org-ans2' from the cursor date,
  14213. otherwise stick to the current value of `org-ans2'."
  14214. (let ((sf (selected-frame))
  14215. (sw (selected-window)))
  14216. (select-window (get-buffer-window "*Calendar*" t))
  14217. (eval form)
  14218. (when (and (not keepdate) (calendar-cursor-to-date))
  14219. (let* ((date (calendar-cursor-to-date))
  14220. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14221. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  14222. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  14223. (select-window sw)
  14224. (org-select-frame-set-input-focus sf)))
  14225. (defun org-calendar-select ()
  14226. "Return to `org-read-date' with the date currently selected.
  14227. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  14228. (interactive)
  14229. (when (calendar-cursor-to-date)
  14230. (let* ((date (calendar-cursor-to-date))
  14231. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14232. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  14233. (if (active-minibuffer-window) (exit-minibuffer))))
  14234. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  14235. "Insert a date stamp for the date given by the internal TIME.
  14236. WITH-HM means use the stamp format that includes the time of the day.
  14237. INACTIVE means use square brackets instead of angular ones, so that the
  14238. stamp will not contribute to the agenda.
  14239. PRE and POST are optional strings to be inserted before and after the
  14240. stamp.
  14241. The command returns the inserted time stamp."
  14242. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  14243. stamp)
  14244. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  14245. (insert-before-markers (or pre ""))
  14246. (when (listp extra)
  14247. (setq extra (car extra))
  14248. (if (and (stringp extra)
  14249. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  14250. (setq extra (format "-%02d:%02d"
  14251. (string-to-number (match-string 1 extra))
  14252. (string-to-number (match-string 2 extra))))
  14253. (setq extra nil)))
  14254. (when extra
  14255. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  14256. (insert-before-markers (setq stamp (format-time-string fmt time)))
  14257. (insert-before-markers (or post ""))
  14258. (setq org-last-inserted-timestamp stamp)))
  14259. (defun org-toggle-time-stamp-overlays ()
  14260. "Toggle the use of custom time stamp formats."
  14261. (interactive)
  14262. (setq org-display-custom-times (not org-display-custom-times))
  14263. (unless org-display-custom-times
  14264. (let ((p (point-min)) (bmp (buffer-modified-p)))
  14265. (while (setq p (next-single-property-change p 'display))
  14266. (if (and (get-text-property p 'display)
  14267. (eq (get-text-property p 'face) 'org-date))
  14268. (remove-text-properties
  14269. p (setq p (next-single-property-change p 'display))
  14270. '(display t))))
  14271. (set-buffer-modified-p bmp)))
  14272. (if (featurep 'xemacs)
  14273. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  14274. (org-restart-font-lock)
  14275. (setq org-table-may-need-update t)
  14276. (if org-display-custom-times
  14277. (message "Time stamps are overlaid with custom format")
  14278. (message "Time stamp overlays removed")))
  14279. (defun org-display-custom-time (beg end)
  14280. "Overlay modified time stamp format over timestamp between BEG and END."
  14281. (let* ((ts (buffer-substring beg end))
  14282. t1 w1 with-hm tf time str w2 (off 0))
  14283. (save-match-data
  14284. (setq t1 (org-parse-time-string ts t))
  14285. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
  14286. (setq off (- (match-end 0) (match-beginning 0)))))
  14287. (setq end (- end off))
  14288. (setq w1 (- end beg)
  14289. with-hm (and (nth 1 t1) (nth 2 t1))
  14290. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  14291. time (org-fix-decoded-time t1)
  14292. str (org-add-props
  14293. (format-time-string
  14294. (substring tf 1 -1) (apply 'encode-time time))
  14295. nil 'mouse-face 'highlight)
  14296. w2 (length str))
  14297. (if (not (= w2 w1))
  14298. (add-text-properties (1+ beg) (+ 2 beg)
  14299. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  14300. (if (featurep 'xemacs)
  14301. (progn
  14302. (put-text-property beg end 'invisible t)
  14303. (put-text-property beg end 'end-glyph (make-glyph str)))
  14304. (put-text-property beg end 'display str))))
  14305. (defun org-translate-time (string)
  14306. "Translate all timestamps in STRING to custom format.
  14307. But do this only if the variable `org-display-custom-times' is set."
  14308. (when org-display-custom-times
  14309. (save-match-data
  14310. (let* ((start 0)
  14311. (re org-ts-regexp-both)
  14312. t1 with-hm inactive tf time str beg end)
  14313. (while (setq start (string-match re string start))
  14314. (setq beg (match-beginning 0)
  14315. end (match-end 0)
  14316. t1 (save-match-data
  14317. (org-parse-time-string (substring string beg end) t))
  14318. with-hm (and (nth 1 t1) (nth 2 t1))
  14319. inactive (equal (substring string beg (1+ beg)) "[")
  14320. tf (funcall (if with-hm 'cdr 'car)
  14321. org-time-stamp-custom-formats)
  14322. time (org-fix-decoded-time t1)
  14323. str (format-time-string
  14324. (concat
  14325. (if inactive "[" "<") (substring tf 1 -1)
  14326. (if inactive "]" ">"))
  14327. (apply 'encode-time time))
  14328. string (replace-match str t t string)
  14329. start (+ start (length str)))))))
  14330. string)
  14331. (defun org-fix-decoded-time (time)
  14332. "Set 0 instead of nil for the first 6 elements of time.
  14333. Don't touch the rest."
  14334. (let ((n 0))
  14335. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  14336. (defun org-days-to-time (timestamp-string)
  14337. "Difference between TIMESTAMP-STRING and now in days."
  14338. (- (time-to-days (org-time-string-to-time timestamp-string))
  14339. (time-to-days (current-time))))
  14340. (defun org-deadline-close (timestamp-string &optional ndays)
  14341. "Is the time in TIMESTAMP-STRING close to the current date?"
  14342. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  14343. (and (< (org-days-to-time timestamp-string) ndays)
  14344. (not (org-entry-is-done-p))))
  14345. (defun org-get-wdays (ts)
  14346. "Get the deadline lead time appropriate for timestring TS."
  14347. (cond
  14348. ((<= org-deadline-warning-days 0)
  14349. ;; 0 or negative, enforce this value no matter what
  14350. (- org-deadline-warning-days))
  14351. ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
  14352. ;; lead time is specified.
  14353. (floor (* (string-to-number (match-string 1 ts))
  14354. (cdr (assoc (match-string 2 ts)
  14355. '(("d" . 1) ("w" . 7)
  14356. ("m" . 30.4) ("y" . 365.25)))))))
  14357. ;; go for the default.
  14358. (t org-deadline-warning-days)))
  14359. (defun org-calendar-select-mouse (ev)
  14360. "Return to `org-read-date' with the date currently selected.
  14361. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  14362. (interactive "e")
  14363. (mouse-set-point ev)
  14364. (when (calendar-cursor-to-date)
  14365. (let* ((date (calendar-cursor-to-date))
  14366. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14367. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  14368. (if (active-minibuffer-window) (exit-minibuffer))))
  14369. (defun org-check-deadlines (ndays)
  14370. "Check if there are any deadlines due or past due.
  14371. A deadline is considered due if it happens within `org-deadline-warning-days'
  14372. days from today's date. If the deadline appears in an entry marked DONE,
  14373. it is not shown. The prefix arg NDAYS can be used to test that many
  14374. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  14375. (interactive "P")
  14376. (let* ((org-warn-days
  14377. (cond
  14378. ((equal ndays '(4)) 100000)
  14379. (ndays (prefix-numeric-value ndays))
  14380. (t (abs org-deadline-warning-days))))
  14381. (case-fold-search nil)
  14382. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  14383. (callback
  14384. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  14385. (message "%d deadlines past-due or due within %d days"
  14386. (org-occur regexp nil callback)
  14387. org-warn-days)))
  14388. (defun org-check-before-date (date)
  14389. "Check if there are deadlines or scheduled entries before DATE."
  14390. (interactive (list (org-read-date)))
  14391. (let ((case-fold-search nil)
  14392. (regexp (concat "\\<\\(" org-deadline-string
  14393. "\\|" org-scheduled-string
  14394. "\\) *<\\([^>]+\\)>"))
  14395. (callback
  14396. (lambda () (time-less-p
  14397. (org-time-string-to-time (match-string 2))
  14398. (org-time-string-to-time date)))))
  14399. (message "%d entries before %s"
  14400. (org-occur regexp nil callback) date)))
  14401. (defun org-check-after-date (date)
  14402. "Check if there are deadlines or scheduled entries after DATE."
  14403. (interactive (list (org-read-date)))
  14404. (let ((case-fold-search nil)
  14405. (regexp (concat "\\<\\(" org-deadline-string
  14406. "\\|" org-scheduled-string
  14407. "\\) *<\\([^>]+\\)>"))
  14408. (callback
  14409. (lambda () (not
  14410. (time-less-p
  14411. (org-time-string-to-time (match-string 2))
  14412. (org-time-string-to-time date))))))
  14413. (message "%d entries after %s"
  14414. (org-occur regexp nil callback) date)))
  14415. (defun org-check-dates-range (start-date end-date)
  14416. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  14417. (interactive (list (org-read-date nil nil nil "Range starts")
  14418. (org-read-date nil nil nil "Range end")))
  14419. (let ((case-fold-search nil)
  14420. (regexp (concat "\\<\\(" org-deadline-string
  14421. "\\|" org-scheduled-string
  14422. "\\) *<\\([^>]+\\)>"))
  14423. (callback
  14424. (lambda ()
  14425. (let ((match (match-string 2)))
  14426. (and
  14427. (not (time-less-p
  14428. (org-time-string-to-time match)
  14429. (org-time-string-to-time start-date)))
  14430. (time-less-p
  14431. (org-time-string-to-time match)
  14432. (org-time-string-to-time end-date)))))))
  14433. (message "%d entries between %s and %s"
  14434. (org-occur regexp nil callback) start-date end-date)))
  14435. (defun org-evaluate-time-range (&optional to-buffer)
  14436. "Evaluate a time range by computing the difference between start and end.
  14437. Normally the result is just printed in the echo area, but with prefix arg
  14438. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  14439. If the time range is actually in a table, the result is inserted into the
  14440. next column.
  14441. For time difference computation, a year is assumed to be exactly 365
  14442. days in order to avoid rounding problems."
  14443. (interactive "P")
  14444. (or
  14445. (org-clock-update-time-maybe)
  14446. (save-excursion
  14447. (unless (org-at-date-range-p t)
  14448. (goto-char (point-at-bol))
  14449. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  14450. (if (not (org-at-date-range-p t))
  14451. (error "Not at a time-stamp range, and none found in current line")))
  14452. (let* ((ts1 (match-string 1))
  14453. (ts2 (match-string 2))
  14454. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  14455. (match-end (match-end 0))
  14456. (time1 (org-time-string-to-time ts1))
  14457. (time2 (org-time-string-to-time ts2))
  14458. (t1 (org-float-time time1))
  14459. (t2 (org-float-time time2))
  14460. (diff (abs (- t2 t1)))
  14461. (negative (< (- t2 t1) 0))
  14462. ;; (ys (floor (* 365 24 60 60)))
  14463. (ds (* 24 60 60))
  14464. (hs (* 60 60))
  14465. (fy "%dy %dd %02d:%02d")
  14466. (fy1 "%dy %dd")
  14467. (fd "%dd %02d:%02d")
  14468. (fd1 "%dd")
  14469. (fh "%02d:%02d")
  14470. y d h m align)
  14471. (if havetime
  14472. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  14473. y 0
  14474. d (floor (/ diff ds)) diff (mod diff ds)
  14475. h (floor (/ diff hs)) diff (mod diff hs)
  14476. m (floor (/ diff 60)))
  14477. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  14478. y 0
  14479. d (floor (+ (/ diff ds) 0.5))
  14480. h 0 m 0))
  14481. (if (not to-buffer)
  14482. (message "%s" (org-make-tdiff-string y d h m))
  14483. (if (org-at-table-p)
  14484. (progn
  14485. (goto-char match-end)
  14486. (setq align t)
  14487. (and (looking-at " *|") (goto-char (match-end 0))))
  14488. (goto-char match-end))
  14489. (if (looking-at
  14490. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  14491. (replace-match ""))
  14492. (if negative (insert " -"))
  14493. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  14494. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  14495. (insert " " (format fh h m))))
  14496. (if align (org-table-align))
  14497. (message "Time difference inserted")))))
  14498. (defun org-make-tdiff-string (y d h m)
  14499. (let ((fmt "")
  14500. (l nil))
  14501. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  14502. l (push y l)))
  14503. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  14504. l (push d l)))
  14505. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  14506. l (push h l)))
  14507. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  14508. l (push m l)))
  14509. (apply 'format fmt (nreverse l))))
  14510. (defun org-time-string-to-time (s &optional buffer pos)
  14511. (condition-case errdata
  14512. (apply 'encode-time (org-parse-time-string s))
  14513. (error (error "Bad timestamp `%s'%s\nError was: %s"
  14514. s (if (not (and buffer pos))
  14515. ""
  14516. (format " at %d in buffer `%s'" pos buffer))
  14517. (cdr errdata)))))
  14518. (defun org-time-string-to-seconds (s)
  14519. (org-float-time (org-time-string-to-time s)))
  14520. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  14521. "Convert a time stamp to an absolute day number.
  14522. If there is a specifier for a cyclic time stamp, get the closest date to
  14523. DAYNR.
  14524. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  14525. The variable date is bound by the calendar when this is called."
  14526. (cond
  14527. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  14528. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  14529. daynr
  14530. (+ daynr 1000)))
  14531. ((and daynr (string-match "\\+[0-9]+[hdwmy]" s))
  14532. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  14533. (time-to-days (current-time))) (match-string 0 s)
  14534. prefer show-all))
  14535. (t (time-to-days
  14536. (condition-case errdata
  14537. (apply 'encode-time (org-parse-time-string s))
  14538. (error (error "Bad timestamp `%s'%s\nError was: %s"
  14539. s (if (not (and buffer pos))
  14540. ""
  14541. (format " at %d in buffer `%s'" pos buffer))
  14542. (cdr errdata))))))))
  14543. (defun org-days-to-iso-week (days)
  14544. "Return the iso week number."
  14545. (require 'cal-iso)
  14546. (car (calendar-iso-from-absolute days)))
  14547. (defun org-small-year-to-year (year)
  14548. "Convert 2-digit years into 4-digit years.
  14549. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  14550. The year 2000 cannot be abbreviated. Any year larger than 99
  14551. is returned unchanged."
  14552. (if (< year 38)
  14553. (setq year (+ 2000 year))
  14554. (if (< year 100)
  14555. (setq year (+ 1900 year))))
  14556. year)
  14557. (defun org-time-from-absolute (d)
  14558. "Return the time corresponding to date D.
  14559. D may be an absolute day number, or a calendar-type list (month day year)."
  14560. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  14561. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  14562. (defun org-calendar-holiday ()
  14563. "List of holidays, for Diary display in Org-mode."
  14564. (require 'holidays)
  14565. (let ((hl (funcall
  14566. (if (fboundp 'calendar-check-holidays)
  14567. 'calendar-check-holidays 'check-calendar-holidays) date)))
  14568. (if hl (mapconcat 'identity hl "; "))))
  14569. (defun org-diary-sexp-entry (sexp entry date)
  14570. "Process a SEXP diary ENTRY for DATE."
  14571. (require 'diary-lib)
  14572. (let ((result (if calendar-debug-sexp
  14573. (let ((stack-trace-on-error t))
  14574. (eval (car (read-from-string sexp))))
  14575. (condition-case nil
  14576. (eval (car (read-from-string sexp)))
  14577. (error
  14578. (beep)
  14579. (message "Bad sexp at line %d in %s: %s"
  14580. (org-current-line)
  14581. (buffer-file-name) sexp)
  14582. (sleep-for 2))))))
  14583. (cond ((stringp result) (split-string result "; "))
  14584. ((and (consp result)
  14585. (not (consp (cdr result)))
  14586. (stringp (cdr result))) (cdr result))
  14587. ((and (consp result)
  14588. (stringp (car result))) result)
  14589. (result entry)
  14590. (t nil))))
  14591. (defun org-diary-to-ical-string (frombuf)
  14592. "Get iCalendar entries from diary entries in buffer FROMBUF.
  14593. This uses the icalendar.el library."
  14594. (let* ((tmpdir (if (featurep 'xemacs)
  14595. (temp-directory)
  14596. temporary-file-directory))
  14597. (tmpfile (make-temp-name
  14598. (expand-file-name "orgics" tmpdir)))
  14599. buf rtn b e)
  14600. (with-current-buffer frombuf
  14601. (icalendar-export-region (point-min) (point-max) tmpfile)
  14602. (setq buf (find-buffer-visiting tmpfile))
  14603. (set-buffer buf)
  14604. (goto-char (point-min))
  14605. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  14606. (setq b (match-beginning 0)))
  14607. (goto-char (point-max))
  14608. (if (re-search-backward "^END:VEVENT" nil t)
  14609. (setq e (match-end 0)))
  14610. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  14611. (kill-buffer buf)
  14612. (delete-file tmpfile)
  14613. rtn))
  14614. (defun org-closest-date (start current change prefer show-all)
  14615. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  14616. When PREFER is `past', return a date that is either CURRENT or past.
  14617. When PREFER is `future', return a date that is either CURRENT or future.
  14618. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  14619. ;; Make the proper lists from the dates
  14620. (catch 'exit
  14621. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  14622. dn dw sday cday n1 n2 n0
  14623. d m y y1 y2 date1 date2 nmonths nm ny m2)
  14624. (setq start (org-date-to-gregorian start)
  14625. current (org-date-to-gregorian
  14626. (if show-all
  14627. current
  14628. (time-to-days (current-time))))
  14629. sday (calendar-absolute-from-gregorian start)
  14630. cday (calendar-absolute-from-gregorian current))
  14631. (if (<= cday sday) (throw 'exit sday))
  14632. (if (string-match "\\(\\+[0-9]+\\)\\([hdwmy]\\)" change)
  14633. (setq dn (string-to-number (match-string 1 change))
  14634. dw (cdr (assoc (match-string 2 change) a1)))
  14635. (error "Invalid change specifier: %s" change))
  14636. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  14637. (cond
  14638. ((eq dw 'day)
  14639. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  14640. n2 (+ n1 dn)))
  14641. ((eq dw 'year)
  14642. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  14643. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  14644. (setq date1 (list m d y1)
  14645. n1 (calendar-absolute-from-gregorian date1)
  14646. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  14647. n2 (calendar-absolute-from-gregorian date2)))
  14648. ((eq dw 'month)
  14649. ;; approx number of month between the two dates
  14650. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  14651. ;; How often does dn fit in there?
  14652. (setq d (nth 1 start) m (car start) y (nth 2 start)
  14653. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  14654. m (+ m nm)
  14655. ny (floor (/ m 12))
  14656. y (+ y ny)
  14657. m (- m (* ny 12)))
  14658. (while (> m 12) (setq m (- m 12) y (1+ y)))
  14659. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  14660. (setq m2 (+ m dn) y2 y)
  14661. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  14662. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  14663. (while (<= n2 cday)
  14664. (setq n1 n2 m m2 y y2)
  14665. (setq m2 (+ m dn) y2 y)
  14666. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  14667. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  14668. ;; Make sure n1 is the earlier date
  14669. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  14670. (if show-all
  14671. (cond
  14672. ((eq prefer 'past) (if (= cday n2) n2 n1))
  14673. ((eq prefer 'future) (if (= cday n1) n1 n2))
  14674. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  14675. (cond
  14676. ((eq prefer 'past) (if (= cday n2) n2 n1))
  14677. ((eq prefer 'future) (if (= cday n1) n1 n2))
  14678. (t (if (= cday n1) n1 n2)))))))
  14679. (defun org-date-to-gregorian (date)
  14680. "Turn any specification of DATE into a Gregorian date for the calendar."
  14681. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  14682. ((and (listp date) (= (length date) 3)) date)
  14683. ((stringp date)
  14684. (setq date (org-parse-time-string date))
  14685. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  14686. ((listp date)
  14687. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  14688. (defun org-parse-time-string (s &optional nodefault)
  14689. "Parse the standard Org-mode time string.
  14690. This should be a lot faster than the normal `parse-time-string'.
  14691. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  14692. hour and minute fields will be nil if not given."
  14693. (if (string-match org-ts-regexp0 s)
  14694. (list 0
  14695. (if (or (match-beginning 8) (not nodefault))
  14696. (string-to-number (or (match-string 8 s) "0")))
  14697. (if (or (match-beginning 7) (not nodefault))
  14698. (string-to-number (or (match-string 7 s) "0")))
  14699. (string-to-number (match-string 4 s))
  14700. (string-to-number (match-string 3 s))
  14701. (string-to-number (match-string 2 s))
  14702. nil nil nil)
  14703. (error "Not a standard Org-mode time string: %s" s)))
  14704. (defun org-timestamp-up (&optional arg)
  14705. "Increase the date item at the cursor by one.
  14706. If the cursor is on the year, change the year. If it is on the month,
  14707. the day or the time, change that.
  14708. With prefix ARG, change by that many units."
  14709. (interactive "p")
  14710. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  14711. (defun org-timestamp-down (&optional arg)
  14712. "Decrease the date item at the cursor by one.
  14713. If the cursor is on the year, change the year. If it is on the month,
  14714. the day or the time, change that.
  14715. With prefix ARG, change by that many units."
  14716. (interactive "p")
  14717. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  14718. (defun org-timestamp-up-day (&optional arg)
  14719. "Increase the date in the time stamp by one day.
  14720. With prefix ARG, change that many days."
  14721. (interactive "p")
  14722. (if (and (not (org-at-timestamp-p t))
  14723. (org-at-heading-p))
  14724. (org-todo 'up)
  14725. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  14726. (defun org-timestamp-down-day (&optional arg)
  14727. "Decrease the date in the time stamp by one day.
  14728. With prefix ARG, change that many days."
  14729. (interactive "p")
  14730. (if (and (not (org-at-timestamp-p t))
  14731. (org-at-heading-p))
  14732. (org-todo 'down)
  14733. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  14734. (defun org-at-timestamp-p (&optional inactive-ok)
  14735. "Determine if the cursor is in or at a timestamp."
  14736. (interactive)
  14737. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  14738. (pos (point))
  14739. (ans (or (looking-at tsr)
  14740. (save-excursion
  14741. (skip-chars-backward "^[<\n\r\t")
  14742. (if (> (point) (point-min)) (backward-char 1))
  14743. (and (looking-at tsr)
  14744. (> (- (match-end 0) pos) -1))))))
  14745. (and ans
  14746. (boundp 'org-ts-what)
  14747. (setq org-ts-what
  14748. (cond
  14749. ((= pos (match-beginning 0)) 'bracket)
  14750. ;; Point is considered to be "on the bracket" whether
  14751. ;; it's really on it or right after it.
  14752. ((or (= pos (1- (match-end 0)))
  14753. (= pos (match-end 0))) 'bracket)
  14754. ((org-pos-in-match-range pos 2) 'year)
  14755. ((org-pos-in-match-range pos 3) 'month)
  14756. ((org-pos-in-match-range pos 7) 'hour)
  14757. ((org-pos-in-match-range pos 8) 'minute)
  14758. ((or (org-pos-in-match-range pos 4)
  14759. (org-pos-in-match-range pos 5)) 'day)
  14760. ((and (> pos (or (match-end 8) (match-end 5)))
  14761. (< pos (match-end 0)))
  14762. (- pos (or (match-end 8) (match-end 5))))
  14763. (t 'day))))
  14764. ans))
  14765. (defun org-toggle-timestamp-type ()
  14766. "Toggle the type (<active> or [inactive]) of a time stamp."
  14767. (interactive)
  14768. (when (org-at-timestamp-p t)
  14769. (let ((beg (match-beginning 0)) (end (match-end 0))
  14770. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  14771. (save-excursion
  14772. (goto-char beg)
  14773. (while (re-search-forward "[][<>]" end t)
  14774. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  14775. t t)))
  14776. (message "Timestamp is now %sactive"
  14777. (if (equal (char-after beg) ?<) "" "in")))))
  14778. (defun org-timestamp-change (n &optional what updown)
  14779. "Change the date in the time stamp at point.
  14780. The date will be changed by N times WHAT. WHAT can be `day', `month',
  14781. `year', `minute', `second'. If WHAT is not given, the cursor position
  14782. in the timestamp determines what will be changed."
  14783. (let ((origin (point)) origin-cat
  14784. with-hm inactive
  14785. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  14786. org-ts-what
  14787. extra rem
  14788. ts time time0)
  14789. (if (not (org-at-timestamp-p t))
  14790. (error "Not at a timestamp"))
  14791. (if (and (not what) (eq org-ts-what 'bracket))
  14792. (org-toggle-timestamp-type)
  14793. ;; Point isn't on brackets. Remember the part of the time-stamp
  14794. ;; the point was in. Indeed, size of time-stamps may change,
  14795. ;; but point must be kept in the same category nonetheless.
  14796. (setq origin-cat org-ts-what)
  14797. (if (and (not what) (not (eq org-ts-what 'day))
  14798. org-display-custom-times
  14799. (get-text-property (point) 'display)
  14800. (not (get-text-property (1- (point)) 'display)))
  14801. (setq org-ts-what 'day))
  14802. (setq org-ts-what (or what org-ts-what)
  14803. inactive (= (char-after (match-beginning 0)) ?\[)
  14804. ts (match-string 0))
  14805. (replace-match "")
  14806. (if (string-match
  14807. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
  14808. ts)
  14809. (setq extra (match-string 1 ts)))
  14810. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  14811. (setq with-hm t))
  14812. (setq time0 (org-parse-time-string ts))
  14813. (when (and updown
  14814. (eq org-ts-what 'minute)
  14815. (not current-prefix-arg))
  14816. ;; This looks like s-up and s-down. Change by one rounding step.
  14817. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  14818. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  14819. (setcar (cdr time0) (+ (nth 1 time0)
  14820. (if (> n 0) (- rem) (- dm rem))))))
  14821. (setq time
  14822. (encode-time (or (car time0) 0)
  14823. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  14824. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  14825. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  14826. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  14827. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  14828. (nthcdr 6 time0)))
  14829. (when (and (member org-ts-what '(hour minute))
  14830. extra
  14831. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  14832. (setq extra (org-modify-ts-extra
  14833. extra
  14834. (if (eq org-ts-what 'hour) 2 5)
  14835. n dm)))
  14836. (when (integerp org-ts-what)
  14837. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  14838. (if (eq what 'calendar)
  14839. (let ((cal-date (org-get-date-from-calendar)))
  14840. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  14841. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  14842. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  14843. (setcar time0 (or (car time0) 0))
  14844. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  14845. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  14846. (setq time (apply 'encode-time time0))))
  14847. ;; Insert the new time-stamp, and ensure point stays in the same
  14848. ;; category as before (i.e. not after the last position in that
  14849. ;; category).
  14850. (let ((pos (point)))
  14851. ;; Stay before inserted string. `save-excursion' is of no use.
  14852. (setq org-last-changed-timestamp
  14853. (org-insert-time-stamp time with-hm inactive nil nil extra))
  14854. (goto-char pos))
  14855. (save-match-data
  14856. (looking-at org-ts-regexp3)
  14857. (goto-char (cond
  14858. ;; `day' category ends before `hour' if any, or at
  14859. ;; the end of the day name.
  14860. ((eq origin-cat 'day)
  14861. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  14862. ((eq origin-cat 'hour) (min (match-end 7) origin))
  14863. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  14864. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  14865. ;; `year' and `month' have both fixed size: point
  14866. ;; couldn't have moved into another part.
  14867. (t origin))))
  14868. ;; Update clock if on a CLOCK line.
  14869. (org-clock-update-time-maybe)
  14870. ;; Try to recenter the calendar window, if any.
  14871. (if (and org-calendar-follow-timestamp-change
  14872. (get-buffer-window "*Calendar*" t)
  14873. (memq org-ts-what '(day month year)))
  14874. (org-recenter-calendar (time-to-days time))))))
  14875. (defun org-modify-ts-extra (s pos n dm)
  14876. "Change the different parts of the lead-time and repeat fields in timestamp."
  14877. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  14878. ng h m new rem)
  14879. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  14880. (cond
  14881. ((or (org-pos-in-match-range pos 2)
  14882. (org-pos-in-match-range pos 3))
  14883. (setq m (string-to-number (match-string 3 s))
  14884. h (string-to-number (match-string 2 s)))
  14885. (if (org-pos-in-match-range pos 2)
  14886. (setq h (+ h n))
  14887. (setq n (* dm (org-no-warnings (signum n))))
  14888. (when (not (= 0 (setq rem (% m dm))))
  14889. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  14890. (setq m (+ m n)))
  14891. (if (< m 0) (setq m (+ m 60) h (1- h)))
  14892. (if (> m 59) (setq m (- m 60) h (1+ h)))
  14893. (setq h (min 24 (max 0 h)))
  14894. (setq ng 1 new (format "-%02d:%02d" h m)))
  14895. ((org-pos-in-match-range pos 6)
  14896. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  14897. ((org-pos-in-match-range pos 5)
  14898. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  14899. ((org-pos-in-match-range pos 9)
  14900. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  14901. ((org-pos-in-match-range pos 8)
  14902. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  14903. (when ng
  14904. (setq s (concat
  14905. (substring s 0 (match-beginning ng))
  14906. new
  14907. (substring s (match-end ng))))))
  14908. s))
  14909. (defun org-recenter-calendar (date)
  14910. "If the calendar is visible, recenter it to DATE."
  14911. (let ((cwin (get-buffer-window "*Calendar*" t)))
  14912. (when cwin
  14913. (let ((calendar-move-hook nil))
  14914. (with-selected-window cwin
  14915. (calendar-goto-date (if (listp date) date
  14916. (calendar-gregorian-from-absolute date))))))))
  14917. (defun org-goto-calendar (&optional arg)
  14918. "Go to the Emacs calendar at the current date.
  14919. If there is a time stamp in the current line, go to that date.
  14920. A prefix ARG can be used to force the current date."
  14921. (interactive "P")
  14922. (let ((tsr org-ts-regexp) diff
  14923. (calendar-move-hook nil)
  14924. (calendar-view-holidays-initially-flag nil)
  14925. (calendar-view-diary-initially-flag nil))
  14926. (if (or (org-at-timestamp-p)
  14927. (save-excursion
  14928. (beginning-of-line 1)
  14929. (looking-at (concat ".*" tsr))))
  14930. (let ((d1 (time-to-days (current-time)))
  14931. (d2 (time-to-days
  14932. (org-time-string-to-time (match-string 1)))))
  14933. (setq diff (- d2 d1))))
  14934. (calendar)
  14935. (calendar-goto-today)
  14936. (if (and diff (not arg)) (calendar-forward-day diff))))
  14937. (defun org-get-date-from-calendar ()
  14938. "Return a list (month day year) of date at point in calendar."
  14939. (with-current-buffer "*Calendar*"
  14940. (save-match-data
  14941. (calendar-cursor-to-date))))
  14942. (defun org-date-from-calendar ()
  14943. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  14944. If there is already a time stamp at the cursor position, update it."
  14945. (interactive)
  14946. (if (org-at-timestamp-p t)
  14947. (org-timestamp-change 0 'calendar)
  14948. (let ((cal-date (org-get-date-from-calendar)))
  14949. (org-insert-time-stamp
  14950. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  14951. (defun org-minutes-to-hh:mm-string (m)
  14952. "Compute H:MM from a number of minutes."
  14953. (let ((h (/ m 60)))
  14954. (setq m (- m (* 60 h)))
  14955. (format org-time-clocksum-format h m)))
  14956. (defun org-hh:mm-string-to-minutes (s)
  14957. "Convert a string H:MM to a number of minutes.
  14958. If the string is just a number, interpret it as minutes.
  14959. In fact, the first hh:mm or number in the string will be taken,
  14960. there can be extra stuff in the string.
  14961. If no number is found, the return value is 0."
  14962. (cond
  14963. ((integerp s) s)
  14964. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  14965. (+ (* (string-to-number (match-string 1 s)) 60)
  14966. (string-to-number (match-string 2 s))))
  14967. ((string-match "\\([0-9]+\\)" s)
  14968. (string-to-number (match-string 1 s)))
  14969. (t 0)))
  14970. (defcustom org-effort-durations
  14971. `(("h" . 60)
  14972. ("d" . ,(* 60 8))
  14973. ("w" . ,(* 60 8 5))
  14974. ("m" . ,(* 60 8 5 4))
  14975. ("y" . ,(* 60 8 5 40)))
  14976. "Conversion factor to minutes for an effort modifier.
  14977. Each entry has the form (MODIFIER . MINUTES).
  14978. In an effort string, a number followed by MODIFIER is multiplied
  14979. by the specified number of MINUTES to obtain an effort in
  14980. minutes.
  14981. For example, if the value of this variable is ((\"hours\" . 60)), then an
  14982. effort string \"2hours\" is equivalent to 120 minutes."
  14983. :group 'org-agenda
  14984. :version "24.1"
  14985. :type '(alist :key-type (string :tag "Modifier")
  14986. :value-type (number :tag "Minutes")))
  14987. (defun org-duration-string-to-minutes (s &optional output-to-string)
  14988. "Convert a duration string S to minutes.
  14989. A bare number is interpreted as minutes, modifiers can be set by
  14990. customizing `org-effort-durations' (which see).
  14991. Entries containing a colon are interpreted as H:MM by
  14992. `org-hh:mm-string-to-minutes'."
  14993. (let ((result 0)
  14994. (re (concat "\\([0-9.]+\\) *\\("
  14995. (regexp-opt (mapcar 'car org-effort-durations))
  14996. "\\)")))
  14997. (while (string-match re s)
  14998. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  14999. (string-to-number (match-string 1 s))))
  15000. (setq s (replace-match "" nil t s)))
  15001. (setq result (floor result))
  15002. (incf result (org-hh:mm-string-to-minutes s))
  15003. (if output-to-string (number-to-string result) result)))
  15004. ;;;; Files
  15005. (defun org-save-all-org-buffers ()
  15006. "Save all Org-mode buffers without user confirmation."
  15007. (interactive)
  15008. (message "Saving all Org-mode buffers...")
  15009. (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
  15010. (when (featurep 'org-id) (org-id-locations-save))
  15011. (message "Saving all Org-mode buffers... done"))
  15012. (defun org-revert-all-org-buffers ()
  15013. "Revert all Org-mode buffers.
  15014. Prompt for confirmation when there are unsaved changes.
  15015. Be sure you know what you are doing before letting this function
  15016. overwrite your changes.
  15017. This function is useful in a setup where one tracks org files
  15018. with a version control system, to revert on one machine after pulling
  15019. changes from another. I believe the procedure must be like this:
  15020. 1. M-x org-save-all-org-buffers
  15021. 2. Pull changes from the other machine, resolve conflicts
  15022. 3. M-x org-revert-all-org-buffers"
  15023. (interactive)
  15024. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  15025. (error "Abort"))
  15026. (save-excursion
  15027. (save-window-excursion
  15028. (mapc
  15029. (lambda (b)
  15030. (when (and (with-current-buffer b (derived-mode-p 'org-mode))
  15031. (with-current-buffer b buffer-file-name))
  15032. (org-pop-to-buffer-same-window b)
  15033. (revert-buffer t 'no-confirm)))
  15034. (buffer-list))
  15035. (when (and (featurep 'org-id) org-id-track-globally)
  15036. (org-id-locations-load)))))
  15037. ;;;; Agenda files
  15038. ;;;###autoload
  15039. (defun org-switchb (&optional arg)
  15040. "Switch between Org buffers.
  15041. With one prefix argument, restrict available buffers to files.
  15042. With two prefix arguments, restrict available buffers to agenda files.
  15043. Defaults to `iswitchb' for buffer name completion.
  15044. Set `org-completion-use-ido' to make it use ido instead."
  15045. (interactive "P")
  15046. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  15047. ((equal arg '(16)) (org-buffer-list 'agenda))
  15048. (t (org-buffer-list))))
  15049. (org-completion-use-iswitchb org-completion-use-iswitchb)
  15050. (org-completion-use-ido org-completion-use-ido))
  15051. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  15052. (setq org-completion-use-iswitchb t))
  15053. (org-pop-to-buffer-same-window
  15054. (org-icompleting-read "Org buffer: "
  15055. (mapcar 'list (mapcar 'buffer-name blist))
  15056. nil t))))
  15057. ;;; Define some older names previously used for this functionality
  15058. ;;;###autoload
  15059. (defalias 'org-ido-switchb 'org-switchb)
  15060. ;;;###autoload
  15061. (defalias 'org-iswitchb 'org-switchb)
  15062. (defun org-buffer-list (&optional predicate exclude-tmp)
  15063. "Return a list of Org buffers.
  15064. PREDICATE can be `export', `files' or `agenda'.
  15065. export restrict the list to Export buffers.
  15066. files restrict the list to buffers visiting Org files.
  15067. agenda restrict the list to buffers visiting agenda files.
  15068. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  15069. (let* ((bfn nil)
  15070. (agenda-files (and (eq predicate 'agenda)
  15071. (mapcar 'file-truename (org-agenda-files t))))
  15072. (filter
  15073. (cond
  15074. ((eq predicate 'files)
  15075. (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
  15076. ((eq predicate 'export)
  15077. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  15078. ((eq predicate 'agenda)
  15079. (lambda (b)
  15080. (with-current-buffer b
  15081. (and (derived-mode-p 'org-mode)
  15082. (setq bfn (buffer-file-name b))
  15083. (member (file-truename bfn) agenda-files)))))
  15084. (t (lambda (b) (with-current-buffer b
  15085. (or (derived-mode-p 'org-mode)
  15086. (string-match "\*Org .*Export"
  15087. (buffer-name b)))))))))
  15088. (delq nil
  15089. (mapcar
  15090. (lambda(b)
  15091. (if (and (funcall filter b)
  15092. (or (not exclude-tmp)
  15093. (not (string-match "tmp" (buffer-name b)))))
  15094. b
  15095. nil))
  15096. (buffer-list)))))
  15097. (defun org-agenda-files (&optional unrestricted archives)
  15098. "Get the list of agenda files.
  15099. Optional UNRESTRICTED means return the full list even if a restriction
  15100. is currently in place.
  15101. When ARCHIVES is t, include all archive files that are really being
  15102. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  15103. `org-agenda-archives-mode' is t."
  15104. (let ((files
  15105. (cond
  15106. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  15107. ((stringp org-agenda-files) (org-read-agenda-file-list))
  15108. ((listp org-agenda-files) org-agenda-files)
  15109. (t (error "Invalid value of `org-agenda-files'")))))
  15110. (setq files (apply 'append
  15111. (mapcar (lambda (f)
  15112. (if (file-directory-p f)
  15113. (directory-files
  15114. f t org-agenda-file-regexp)
  15115. (list f)))
  15116. files)))
  15117. (when org-agenda-skip-unavailable-files
  15118. (setq files (delq nil
  15119. (mapcar (function
  15120. (lambda (file)
  15121. (and (file-readable-p file) file)))
  15122. files))))
  15123. (when (or (eq archives t)
  15124. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  15125. (setq files (org-add-archive-files files)))
  15126. files))
  15127. (defun org-agenda-file-p (&optional file)
  15128. "Return non-nil, if FILE is an agenda file.
  15129. If FILE is omitted, use the file associated with the current
  15130. buffer."
  15131. (member (or file (buffer-file-name))
  15132. (org-agenda-files t)))
  15133. (defun org-edit-agenda-file-list ()
  15134. "Edit the list of agenda files.
  15135. Depending on setup, this either uses customize to edit the variable
  15136. `org-agenda-files', or it visits the file that is holding the list. In the
  15137. latter case, the buffer is set up in a way that saving it automatically kills
  15138. the buffer and restores the previous window configuration."
  15139. (interactive)
  15140. (if (stringp org-agenda-files)
  15141. (let ((cw (current-window-configuration)))
  15142. (find-file org-agenda-files)
  15143. (org-set-local 'org-window-configuration cw)
  15144. (org-add-hook 'after-save-hook
  15145. (lambda ()
  15146. (set-window-configuration
  15147. (prog1 org-window-configuration
  15148. (kill-buffer (current-buffer))))
  15149. (org-install-agenda-files-menu)
  15150. (message "New agenda file list installed"))
  15151. nil 'local)
  15152. (message "%s" (substitute-command-keys
  15153. "Edit list and finish with \\[save-buffer]")))
  15154. (customize-variable 'org-agenda-files)))
  15155. (defun org-store-new-agenda-file-list (list)
  15156. "Set new value for the agenda file list and save it correctly."
  15157. (if (stringp org-agenda-files)
  15158. (let ((fe (org-read-agenda-file-list t)) b u)
  15159. (while (setq b (find-buffer-visiting org-agenda-files))
  15160. (kill-buffer b))
  15161. (with-temp-file org-agenda-files
  15162. (insert
  15163. (mapconcat
  15164. (lambda (f) ;; Keep un-expanded entries.
  15165. (if (setq u (assoc f fe))
  15166. (cdr u)
  15167. f))
  15168. list "\n")
  15169. "\n")))
  15170. (let ((org-mode-hook nil) (org-inhibit-startup t)
  15171. (org-insert-mode-line-in-empty-file nil))
  15172. (setq org-agenda-files list)
  15173. (customize-save-variable 'org-agenda-files org-agenda-files))))
  15174. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  15175. "Read the list of agenda files from a file.
  15176. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  15177. filenames, used by `org-store-new-agenda-file-list' to write back
  15178. un-expanded file names."
  15179. (when (file-directory-p org-agenda-files)
  15180. (error "`org-agenda-files' cannot be a single directory"))
  15181. (when (stringp org-agenda-files)
  15182. (with-temp-buffer
  15183. (insert-file-contents org-agenda-files)
  15184. (mapcar
  15185. (lambda (f)
  15186. (let ((e (expand-file-name (substitute-in-file-name f)
  15187. org-directory)))
  15188. (if pair-with-expansion
  15189. (cons e f)
  15190. e)))
  15191. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  15192. ;;;###autoload
  15193. (defun org-cycle-agenda-files ()
  15194. "Cycle through the files in `org-agenda-files'.
  15195. If the current buffer visits an agenda file, find the next one in the list.
  15196. If the current buffer does not, find the first agenda file."
  15197. (interactive)
  15198. (let* ((fs (org-agenda-files t))
  15199. (files (append fs (list (car fs))))
  15200. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  15201. file)
  15202. (unless files (error "No agenda files"))
  15203. (catch 'exit
  15204. (while (setq file (pop files))
  15205. (if (equal (file-truename file) tcf)
  15206. (when (car files)
  15207. (find-file (car files))
  15208. (throw 'exit t))))
  15209. (find-file (car fs)))
  15210. (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  15211. (defun org-agenda-file-to-front (&optional to-end)
  15212. "Move/add the current file to the top of the agenda file list.
  15213. If the file is not present in the list, it is added to the front. If it is
  15214. present, it is moved there. With optional argument TO-END, add/move to the
  15215. end of the list."
  15216. (interactive "P")
  15217. (let ((org-agenda-skip-unavailable-files nil)
  15218. (file-alist (mapcar (lambda (x)
  15219. (cons (file-truename x) x))
  15220. (org-agenda-files t)))
  15221. (ctf (file-truename buffer-file-name))
  15222. x had)
  15223. (setq x (assoc ctf file-alist) had x)
  15224. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  15225. (if to-end
  15226. (setq file-alist (append (delq x file-alist) (list x)))
  15227. (setq file-alist (cons x (delq x file-alist))))
  15228. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  15229. (org-install-agenda-files-menu)
  15230. (message "File %s to %s of agenda file list"
  15231. (if had "moved" "added") (if to-end "end" "front"))))
  15232. (defun org-remove-file (&optional file)
  15233. "Remove current file from the list of files in variable `org-agenda-files'.
  15234. These are the files which are being checked for agenda entries.
  15235. Optional argument FILE means use this file instead of the current."
  15236. (interactive)
  15237. (let* ((org-agenda-skip-unavailable-files nil)
  15238. (file (or file buffer-file-name))
  15239. (true-file (file-truename file))
  15240. (afile (abbreviate-file-name file))
  15241. (files (delq nil (mapcar
  15242. (lambda (x)
  15243. (if (equal true-file
  15244. (file-truename x))
  15245. nil x))
  15246. (org-agenda-files t)))))
  15247. (if (not (= (length files) (length (org-agenda-files t))))
  15248. (progn
  15249. (org-store-new-agenda-file-list files)
  15250. (org-install-agenda-files-menu)
  15251. (message "Removed file: %s" afile))
  15252. (message "File was not in list: %s (not removed)" afile))))
  15253. (defun org-file-menu-entry (file)
  15254. (vector file (list 'find-file file) t))
  15255. (defun org-check-agenda-file (file)
  15256. "Make sure FILE exists. If not, ask user what to do."
  15257. (when (not (file-exists-p file))
  15258. (message "non-existent agenda file %s. [R]emove from list or [A]bort?"
  15259. (abbreviate-file-name file))
  15260. (let ((r (downcase (read-char-exclusive))))
  15261. (cond
  15262. ((equal r ?r)
  15263. (org-remove-file file)
  15264. (throw 'nextfile t))
  15265. (t (error "Abort"))))))
  15266. (defun org-get-agenda-file-buffer (file)
  15267. "Get a buffer visiting FILE. If the buffer needs to be created, add
  15268. it to the list of buffers which might be released later."
  15269. (let ((buf (org-find-base-buffer-visiting file)))
  15270. (if buf
  15271. buf ; just return it
  15272. ;; Make a new buffer and remember it
  15273. (setq buf (find-file-noselect file))
  15274. (if buf (push buf org-agenda-new-buffers))
  15275. buf)))
  15276. (defun org-release-buffers (blist)
  15277. "Release all buffers in list, asking the user for confirmation when needed.
  15278. When a buffer is unmodified, it is just killed. When modified, it is saved
  15279. \(if the user agrees) and then killed."
  15280. (let (buf file)
  15281. (while (setq buf (pop blist))
  15282. (setq file (buffer-file-name buf))
  15283. (when (and (buffer-modified-p buf)
  15284. file
  15285. (y-or-n-p (format "Save file %s? " file)))
  15286. (with-current-buffer buf (save-buffer)))
  15287. (kill-buffer buf))))
  15288. (defun org-prepare-agenda-buffers (files)
  15289. "Create buffers for all agenda files, protect archived trees and comments."
  15290. (interactive)
  15291. (let ((pa '(:org-archived t))
  15292. (pc '(:org-comment t))
  15293. (pall '(:org-archived t :org-comment t))
  15294. (inhibit-read-only t)
  15295. (rea (concat ":" org-archive-tag ":"))
  15296. bmp file re)
  15297. (save-excursion
  15298. (save-restriction
  15299. (while (setq file (pop files))
  15300. (catch 'nextfile
  15301. (if (bufferp file)
  15302. (set-buffer file)
  15303. (org-check-agenda-file file)
  15304. (set-buffer (org-get-agenda-file-buffer file)))
  15305. (widen)
  15306. (setq bmp (buffer-modified-p))
  15307. (org-refresh-category-properties)
  15308. (setq org-todo-keywords-for-agenda
  15309. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  15310. (setq org-done-keywords-for-agenda
  15311. (append org-done-keywords-for-agenda org-done-keywords))
  15312. (setq org-todo-keyword-alist-for-agenda
  15313. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  15314. (setq org-drawers-for-agenda
  15315. (append org-drawers-for-agenda org-drawers))
  15316. (setq org-tag-alist-for-agenda
  15317. (append org-tag-alist-for-agenda org-tag-alist))
  15318. (save-excursion
  15319. (remove-text-properties (point-min) (point-max) pall)
  15320. (when org-agenda-skip-archived-trees
  15321. (goto-char (point-min))
  15322. (while (re-search-forward rea nil t)
  15323. (if (org-at-heading-p t)
  15324. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  15325. (goto-char (point-min))
  15326. (setq re (format org-heading-keyword-regexp-format
  15327. org-comment-string))
  15328. (while (re-search-forward re nil t)
  15329. (add-text-properties
  15330. (match-beginning 0) (org-end-of-subtree t) pc)))
  15331. (set-buffer-modified-p bmp)))))
  15332. (setq org-todo-keywords-for-agenda
  15333. (org-uniquify org-todo-keywords-for-agenda))
  15334. (setq org-todo-keyword-alist-for-agenda
  15335. (org-uniquify org-todo-keyword-alist-for-agenda)
  15336. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  15337. ;;;; Embedded LaTeX
  15338. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  15339. "Keymap for the minor `org-cdlatex-mode'.")
  15340. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  15341. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  15342. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  15343. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  15344. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  15345. (defvar org-cdlatex-texmathp-advice-is-done nil
  15346. "Flag remembering if we have applied the advice to texmathp already.")
  15347. (define-minor-mode org-cdlatex-mode
  15348. "Toggle the minor `org-cdlatex-mode'.
  15349. This mode supports entering LaTeX environment and math in LaTeX fragments
  15350. in Org-mode.
  15351. \\{org-cdlatex-mode-map}"
  15352. nil " OCDL" nil
  15353. (when org-cdlatex-mode
  15354. (require 'cdlatex)
  15355. (run-hooks 'cdlatex-mode-hook)
  15356. (cdlatex-compute-tables))
  15357. (unless org-cdlatex-texmathp-advice-is-done
  15358. (setq org-cdlatex-texmathp-advice-is-done t)
  15359. (defadvice texmathp (around org-math-always-on activate)
  15360. "Always return t in org-mode buffers.
  15361. This is because we want to insert math symbols without dollars even outside
  15362. the LaTeX math segments. If Orgmode thinks that point is actually inside
  15363. an embedded LaTeX fragment, let texmathp do its job.
  15364. \\[org-cdlatex-mode-map]"
  15365. (interactive)
  15366. (let (p)
  15367. (cond
  15368. ((not (derived-mode-p 'org-mode)) ad-do-it)
  15369. ((eq this-command 'cdlatex-math-symbol)
  15370. (setq ad-return-value t
  15371. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  15372. (t
  15373. (let ((p (org-inside-LaTeX-fragment-p)))
  15374. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  15375. (setq ad-return-value t
  15376. texmathp-why '("Org-mode embedded math" . 0))
  15377. (if p ad-do-it)))))))))
  15378. (defun turn-on-org-cdlatex ()
  15379. "Unconditionally turn on `org-cdlatex-mode'."
  15380. (org-cdlatex-mode 1))
  15381. (defun org-inside-LaTeX-fragment-p ()
  15382. "Test if point is inside a LaTeX fragment.
  15383. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  15384. sequence appearing also before point.
  15385. Even though the matchers for math are configurable, this function assumes
  15386. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  15387. delimiters are skipped when they have been removed by customization.
  15388. The return value is nil, or a cons cell with the delimiter and the
  15389. position of this delimiter.
  15390. This function does a reasonably good job, but can locally be fooled by
  15391. for example currency specifications. For example it will assume being in
  15392. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  15393. fragments that are properly closed, but during editing, we have to live
  15394. with the uncertainty caused by missing closing delimiters. This function
  15395. looks only before point, not after."
  15396. (catch 'exit
  15397. (let ((pos (point))
  15398. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  15399. (lim (progn
  15400. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  15401. (point)))
  15402. dd-on str (start 0) m re)
  15403. (goto-char pos)
  15404. (when dodollar
  15405. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  15406. re (nth 1 (assoc "$" org-latex-regexps)))
  15407. (while (string-match re str start)
  15408. (cond
  15409. ((= (match-end 0) (length str))
  15410. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  15411. ((= (match-end 0) (- (length str) 5))
  15412. (throw 'exit nil))
  15413. (t (setq start (match-end 0))))))
  15414. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  15415. (goto-char pos)
  15416. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  15417. (and (match-beginning 2) (throw 'exit nil))
  15418. ;; count $$
  15419. (while (re-search-backward "\\$\\$" lim t)
  15420. (setq dd-on (not dd-on)))
  15421. (goto-char pos)
  15422. (if dd-on (cons "$$" m))))))
  15423. (defun org-inside-latex-macro-p ()
  15424. "Is point inside a LaTeX macro or its arguments?"
  15425. (save-match-data
  15426. (org-in-regexp
  15427. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  15428. (defun org-try-cdlatex-tab ()
  15429. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  15430. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  15431. - inside a LaTeX fragment, or
  15432. - after the first word in a line, where an abbreviation expansion could
  15433. insert a LaTeX environment."
  15434. (when org-cdlatex-mode
  15435. (cond
  15436. ;; Before any word on the line: No expansion possible.
  15437. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  15438. ;; Just after first word on the line: Expand it. Make sure it
  15439. ;; cannot happen on headlines, though.
  15440. ((save-excursion
  15441. (skip-chars-backward "a-zA-Z0-9*")
  15442. (skip-chars-backward " \t")
  15443. (and (bolp) (not (org-at-heading-p))))
  15444. (cdlatex-tab) t)
  15445. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  15446. (defun org-cdlatex-underscore-caret (&optional arg)
  15447. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  15448. Revert to the normal definition outside of these fragments."
  15449. (interactive "P")
  15450. (if (org-inside-LaTeX-fragment-p)
  15451. (call-interactively 'cdlatex-sub-superscript)
  15452. (let (org-cdlatex-mode)
  15453. (call-interactively (key-binding (vector last-input-event))))))
  15454. (defun org-cdlatex-math-modify (&optional arg)
  15455. "Execute `cdlatex-math-modify' in LaTeX fragments.
  15456. Revert to the normal definition outside of these fragments."
  15457. (interactive "P")
  15458. (if (org-inside-LaTeX-fragment-p)
  15459. (call-interactively 'cdlatex-math-modify)
  15460. (let (org-cdlatex-mode)
  15461. (call-interactively (key-binding (vector last-input-event))))))
  15462. (defvar org-latex-fragment-image-overlays nil
  15463. "List of overlays carrying the images of latex fragments.")
  15464. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  15465. (defun org-remove-latex-fragment-image-overlays ()
  15466. "Remove all overlays with LaTeX fragment images in current buffer."
  15467. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  15468. (setq org-latex-fragment-image-overlays nil))
  15469. (defun org-preview-latex-fragment (&optional subtree)
  15470. "Preview the LaTeX fragment at point, or all locally or globally.
  15471. If the cursor is in a LaTeX fragment, create the image and overlay
  15472. it over the source code. If there is no fragment at point, display
  15473. all fragments in the current text, from one headline to the next. With
  15474. prefix SUBTREE, display all fragments in the current subtree. With a
  15475. double prefix arg \\[universal-argument] \\[universal-argument], or when \
  15476. the cursor is before the first headline,
  15477. display all fragments in the buffer.
  15478. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  15479. (interactive "P")
  15480. (unless buffer-file-name
  15481. (error "Can't preview LaTeX fragment in a non-file buffer"))
  15482. (org-remove-latex-fragment-image-overlays)
  15483. (save-excursion
  15484. (save-restriction
  15485. (let (beg end at msg)
  15486. (cond
  15487. ((or (equal subtree '(16))
  15488. (not (save-excursion
  15489. (re-search-backward org-outline-regexp-bol nil t))))
  15490. (setq beg (point-min) end (point-max)
  15491. msg "Creating images for buffer...%s"))
  15492. ((equal subtree '(4))
  15493. (org-back-to-heading)
  15494. (setq beg (point) end (org-end-of-subtree t)
  15495. msg "Creating images for subtree...%s"))
  15496. (t
  15497. (if (setq at (org-inside-LaTeX-fragment-p))
  15498. (goto-char (max (point-min) (- (cdr at) 2)))
  15499. (org-back-to-heading))
  15500. (setq beg (point) end (progn (outline-next-heading) (point))
  15501. msg (if at "Creating image...%s"
  15502. "Creating images for entry...%s"))))
  15503. (message msg "")
  15504. (narrow-to-region beg end)
  15505. (goto-char beg)
  15506. (org-format-latex
  15507. (concat "ltxpng/" (file-name-sans-extension
  15508. (file-name-nondirectory
  15509. buffer-file-name)))
  15510. default-directory 'overlays msg at 'forbuffer
  15511. org-latex-create-formula-image-program)
  15512. (message msg "done. Use `C-c C-c' to remove images.")))))
  15513. (defvar org-latex-regexps
  15514. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  15515. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  15516. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  15517. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  15518. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  15519. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  15520. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  15521. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  15522. "Regular expressions for matching embedded LaTeX.")
  15523. (defvar org-export-have-math nil) ;; dynamic scoping
  15524. (defun org-format-latex (prefix &optional dir overlays msg at
  15525. forbuffer processing-type)
  15526. "Replace LaTeX fragments with links to an image, and produce images.
  15527. Some of the options can be changed using the variable
  15528. `org-format-latex-options'."
  15529. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  15530. (let* ((prefixnodir (file-name-nondirectory prefix))
  15531. (absprefix (expand-file-name prefix dir))
  15532. (todir (file-name-directory absprefix))
  15533. (opt org-format-latex-options)
  15534. (matchers (plist-get opt :matchers))
  15535. (re-list org-latex-regexps)
  15536. (org-format-latex-header-extra
  15537. (plist-get (org-infile-export-plist) :latex-header-extra))
  15538. (cnt 0) txt hash link beg end re e checkdir
  15539. executables-checked string
  15540. m n block-type block linkfile movefile ov)
  15541. ;; Check the different regular expressions
  15542. (while (setq e (pop re-list))
  15543. (setq m (car e) re (nth 1 e) n (nth 2 e) block-type (nth 3 e)
  15544. block (if block-type "\n\n" ""))
  15545. (when (member m matchers)
  15546. (goto-char (point-min))
  15547. (while (re-search-forward re nil t)
  15548. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  15549. (not (get-text-property (match-beginning n)
  15550. 'org-protected))
  15551. (or (not overlays)
  15552. (not (eq (get-char-property (match-beginning n)
  15553. 'org-overlay-type)
  15554. 'org-latex-overlay))))
  15555. (setq org-export-have-math t)
  15556. (cond
  15557. ((eq processing-type 'verbatim)
  15558. ;; Leave the text verbatim, just protect it
  15559. (add-text-properties (match-beginning n) (match-end n)
  15560. '(org-protected t)))
  15561. ((eq processing-type 'mathjax)
  15562. ;; Prepare for MathJax processing
  15563. (setq string (match-string n))
  15564. (if (member m '("$" "$1"))
  15565. (save-excursion
  15566. (delete-region (match-beginning n) (match-end n))
  15567. (goto-char (match-beginning n))
  15568. (insert (org-add-props (concat "\\(" (substring string 1 -1)
  15569. "\\)")
  15570. '(org-protected t))))
  15571. (add-text-properties (match-beginning n) (match-end n)
  15572. '(org-protected t))))
  15573. ((or (eq processing-type 'dvipng)
  15574. (eq processing-type 'imagemagick))
  15575. ;; Process to an image
  15576. (setq txt (match-string n)
  15577. beg (match-beginning n) end (match-end n)
  15578. cnt (1+ cnt))
  15579. (let (print-length print-level) ; make sure full list is printed
  15580. (setq hash (sha1 (prin1-to-string
  15581. (list org-format-latex-header
  15582. org-format-latex-header-extra
  15583. org-export-latex-default-packages-alist
  15584. org-export-latex-packages-alist
  15585. org-format-latex-options
  15586. forbuffer txt)))
  15587. linkfile (format "%s_%s.png" prefix hash)
  15588. movefile (format "%s_%s.png" absprefix hash)))
  15589. (setq link (concat block "[[file:" linkfile "]]" block))
  15590. (if msg (message msg cnt))
  15591. (goto-char beg)
  15592. (unless checkdir ; make sure the directory exists
  15593. (setq checkdir t)
  15594. (or (file-directory-p todir) (make-directory todir t)))
  15595. (cond
  15596. ((eq processing-type 'dvipng)
  15597. (unless executables-checked
  15598. (org-check-external-command
  15599. "latex" "needed to convert LaTeX fragments to images")
  15600. (org-check-external-command
  15601. "dvipng" "needed to convert LaTeX fragments to images")
  15602. (setq executables-checked t))
  15603. (unless (file-exists-p movefile)
  15604. (org-create-formula-image-with-dvipng
  15605. txt movefile opt forbuffer)))
  15606. ((eq processing-type 'imagemagick)
  15607. (unless executables-checked
  15608. (org-check-external-command
  15609. "convert" "you need to install imagemagick")
  15610. (setq executables-checked t))
  15611. (unless (file-exists-p movefile)
  15612. (org-create-formula-image-with-imagemagick
  15613. txt movefile opt forbuffer))))
  15614. (if overlays
  15615. (progn
  15616. (mapc (lambda (o)
  15617. (if (eq (overlay-get o 'org-overlay-type)
  15618. 'org-latex-overlay)
  15619. (delete-overlay o)))
  15620. (overlays-in beg end))
  15621. (setq ov (make-overlay beg end))
  15622. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  15623. (if (featurep 'xemacs)
  15624. (progn
  15625. (overlay-put ov 'invisible t)
  15626. (overlay-put
  15627. ov 'end-glyph
  15628. (make-glyph (vector 'png :file movefile))))
  15629. (overlay-put
  15630. ov 'display
  15631. (list 'image :type 'png :file movefile :ascent 'center)))
  15632. (push ov org-latex-fragment-image-overlays)
  15633. (goto-char end))
  15634. (delete-region beg end)
  15635. (insert (org-add-props link
  15636. (list 'org-latex-src
  15637. (replace-regexp-in-string
  15638. "\"" "" txt)
  15639. 'org-latex-src-embed-type
  15640. (if block-type 'paragraph 'character))))))
  15641. ((eq processing-type 'mathml)
  15642. ;; Process to MathML
  15643. (unless executables-checked
  15644. (unless (save-match-data (org-format-latex-mathml-available-p))
  15645. (error "LaTeX to MathML converter not configured"))
  15646. (setq executables-checked t))
  15647. (setq txt (match-string n)
  15648. beg (match-beginning n) end (match-end n)
  15649. cnt (1+ cnt))
  15650. (if msg (message msg cnt))
  15651. (goto-char beg)
  15652. (delete-region beg end)
  15653. (insert (org-format-latex-as-mathml
  15654. txt block-type prefix dir)))
  15655. (t
  15656. (error "Unknown conversion type %s for latex fragments"
  15657. processing-type)))))))))
  15658. (defun org-create-math-formula (latex-frag &optional mathml-file)
  15659. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  15660. Use `org-latex-to-mathml-convert-command'. If the conversion is
  15661. sucessful, return the portion between \"<math...> </math>\"
  15662. elements otherwise return nil. When MATHML-FILE is specified,
  15663. write the results in to that file. When invoked as an
  15664. interactive command, prompt for LATEX-FRAG, with initial value
  15665. set to the current active region and echo the results for user
  15666. inspection."
  15667. (interactive (list (let ((frag (when (region-active-p)
  15668. (buffer-substring-no-properties
  15669. (region-beginning) (region-end)))))
  15670. (read-string "LaTeX Fragment: " frag nil frag))))
  15671. (unless latex-frag (error "Invalid latex-frag"))
  15672. (let* ((tmp-in-file (file-relative-name
  15673. (make-temp-name (expand-file-name "ltxmathml-in"))))
  15674. (ignore (write-region latex-frag nil tmp-in-file))
  15675. (tmp-out-file (file-relative-name
  15676. (make-temp-name (expand-file-name "ltxmathml-out"))))
  15677. (cmd (format-spec
  15678. org-latex-to-mathml-convert-command
  15679. `((?j . ,(shell-quote-argument
  15680. (expand-file-name org-latex-to-mathml-jar-file)))
  15681. (?I . ,(shell-quote-argument tmp-in-file))
  15682. (?o . ,(shell-quote-argument tmp-out-file)))))
  15683. mathml shell-command-output)
  15684. (when (org-called-interactively-p 'any)
  15685. (unless (org-format-latex-mathml-available-p)
  15686. (error "LaTeX to MathML converter not configured")))
  15687. (message "Running %s" cmd)
  15688. (setq shell-command-output (shell-command-to-string cmd))
  15689. (setq mathml
  15690. (when (file-readable-p tmp-out-file)
  15691. (with-current-buffer (find-file-noselect tmp-out-file t)
  15692. (goto-char (point-min))
  15693. (when (re-search-forward
  15694. (concat
  15695. (regexp-quote
  15696. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
  15697. "\\(.\\|\n\\)*"
  15698. (regexp-quote "</math>")) nil t)
  15699. (prog1 (match-string 0) (kill-buffer))))))
  15700. (cond
  15701. (mathml
  15702. (setq mathml
  15703. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  15704. (when mathml-file
  15705. (write-region mathml nil mathml-file))
  15706. (when (org-called-interactively-p 'any)
  15707. (message mathml)))
  15708. ((message "LaTeX to MathML conversion failed")
  15709. (message shell-command-output)))
  15710. (delete-file tmp-in-file)
  15711. (when (file-exists-p tmp-out-file)
  15712. (delete-file tmp-out-file))
  15713. mathml))
  15714. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  15715. prefix &optional dir)
  15716. "Use `org-create-math-formula' but check local cache first."
  15717. (let* ((absprefix (expand-file-name prefix dir))
  15718. (print-length nil) (print-level nil)
  15719. (formula-id (concat
  15720. "formula-"
  15721. (sha1
  15722. (prin1-to-string
  15723. (list latex-frag
  15724. org-latex-to-mathml-convert-command)))))
  15725. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  15726. (formula-cache-dir (file-name-directory formula-cache)))
  15727. (unless (file-directory-p formula-cache-dir)
  15728. (make-directory formula-cache-dir t))
  15729. (unless (file-exists-p formula-cache)
  15730. (org-create-math-formula latex-frag formula-cache))
  15731. (if (file-exists-p formula-cache)
  15732. ;; Successful conversion. Return the link to MathML file.
  15733. (org-add-props
  15734. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  15735. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  15736. 'org-latex-src-embed-type (if latex-frag-type
  15737. 'paragraph 'character)))
  15738. ;; Failed conversion. Return the LaTeX fragment verbatim
  15739. (add-text-properties
  15740. 0 (1- (length latex-frag)) '(org-protected t) latex-frag)
  15741. latex-frag)))
  15742. ;; This function borrows from Ganesh Swami's latex2png.el
  15743. (defun org-create-formula-image-with-dvipng (string tofile options buffer)
  15744. "This calls dvipng."
  15745. (require 'org-latex)
  15746. (let* ((tmpdir (if (featurep 'xemacs)
  15747. (temp-directory)
  15748. temporary-file-directory))
  15749. (texfilebase (make-temp-name
  15750. (expand-file-name "orgtex" tmpdir)))
  15751. (texfile (concat texfilebase ".tex"))
  15752. (dvifile (concat texfilebase ".dvi"))
  15753. (pngfile (concat texfilebase ".png"))
  15754. (fnh (if (featurep 'xemacs)
  15755. (font-height (face-font 'default))
  15756. (face-attribute 'default :height nil)))
  15757. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  15758. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  15759. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  15760. "Black"))
  15761. (bg (or (plist-get options (if buffer :background :html-background))
  15762. "Transparent")))
  15763. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  15764. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  15765. (with-temp-file texfile
  15766. (insert (org-splice-latex-header
  15767. org-format-latex-header
  15768. org-export-latex-default-packages-alist
  15769. org-export-latex-packages-alist t
  15770. org-format-latex-header-extra))
  15771. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")
  15772. (require 'org-latex)
  15773. (org-export-latex-fix-inputenc))
  15774. (let ((dir default-directory))
  15775. (condition-case nil
  15776. (progn
  15777. (cd tmpdir)
  15778. (call-process "latex" nil nil nil texfile))
  15779. (error nil))
  15780. (cd dir))
  15781. (if (not (file-exists-p dvifile))
  15782. (progn (message "Failed to create dvi file from %s" texfile) nil)
  15783. (condition-case nil
  15784. (if (featurep 'xemacs)
  15785. (call-process "dvipng" nil nil nil
  15786. "-fg" fg "-bg" bg
  15787. "-T" "tight"
  15788. "-o" pngfile
  15789. dvifile)
  15790. (call-process "dvipng" nil nil nil
  15791. "-fg" fg "-bg" bg
  15792. "-D" dpi
  15793. ;;"-x" scale "-y" scale
  15794. "-T" "tight"
  15795. "-o" pngfile
  15796. dvifile))
  15797. (error nil))
  15798. (if (not (file-exists-p pngfile))
  15799. (if org-format-latex-signal-error
  15800. (error "Failed to create png file from %s" texfile)
  15801. (message "Failed to create png file from %s" texfile)
  15802. nil)
  15803. ;; Use the requested file name and clean up
  15804. (copy-file pngfile tofile 'replace)
  15805. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png" ".out") do
  15806. (delete-file (concat texfilebase e)))
  15807. pngfile))))
  15808. (defvar org-latex-to-pdf-process) ;; Defined in org-latex.el
  15809. (defun org-create-formula-image-with-imagemagick (string tofile options buffer)
  15810. "This calls convert, which is included into imagemagick."
  15811. (require 'org-latex)
  15812. (let* ((tmpdir (if (featurep 'xemacs)
  15813. (temp-directory)
  15814. temporary-file-directory))
  15815. (texfilebase (make-temp-name
  15816. (expand-file-name "orgtex" tmpdir)))
  15817. (texfile (concat texfilebase ".tex"))
  15818. (pdffile (concat texfilebase ".pdf"))
  15819. (pngfile (concat texfilebase ".png"))
  15820. (fnh (if (featurep 'xemacs)
  15821. (font-height (face-font 'default))
  15822. (face-attribute 'default :height nil)))
  15823. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  15824. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  15825. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  15826. "black"))
  15827. (bg (or (plist-get options (if buffer :background :html-background))
  15828. "white")))
  15829. (if (eq fg 'default) (setq fg (org-latex-color :foreground))
  15830. (setq fg (org-latex-color-format fg)))
  15831. (if (eq bg 'default) (setq bg (org-latex-color :background))
  15832. (setq bg (org-latex-color-format
  15833. (if (string= bg "Transparent")(setq bg "white")))))
  15834. (with-temp-file texfile
  15835. (insert (org-splice-latex-header
  15836. org-format-latex-header
  15837. org-export-latex-default-packages-alist
  15838. org-export-latex-packages-alist t
  15839. org-format-latex-header-extra))
  15840. (insert "\n\\begin{document}\n"
  15841. "\\definecolor{fg}{rgb}{" fg "}\n"
  15842. "\\definecolor{bg}{rgb}{" bg "}\n"
  15843. "\n\\pagecolor{bg}\n"
  15844. "\n{\\color{fg}\n"
  15845. string
  15846. "\n}\n"
  15847. "\n\\end{document}\n" )
  15848. (require 'org-latex)
  15849. (org-export-latex-fix-inputenc))
  15850. (let ((dir default-directory) cmd cmds latex-frags-cmds)
  15851. (condition-case nil
  15852. (progn
  15853. (cd tmpdir)
  15854. (setq cmds org-latex-to-pdf-process)
  15855. (while cmds
  15856. (setq latex-frags-cmds (pop cmds))
  15857. (if (listp latex-frags-cmds)
  15858. (setq cmds nil)
  15859. (setq latex-frags-cmds (list (car org-latex-to-pdf-process)))))
  15860. (while latex-frags-cmds
  15861. (setq cmd (pop latex-frags-cmds))
  15862. (while (string-match "%b" cmd)
  15863. (setq cmd (replace-match
  15864. (save-match-data
  15865. (shell-quote-argument texfile))
  15866. t t cmd)))
  15867. (while (string-match "%f" cmd)
  15868. (setq cmd (replace-match
  15869. (save-match-data
  15870. (shell-quote-argument (file-name-nondirectory texfile)))
  15871. t t cmd)))
  15872. (while (string-match "%o" cmd)
  15873. (setq cmd (replace-match
  15874. (save-match-data
  15875. (shell-quote-argument (file-name-directory texfile)))
  15876. t t cmd)))
  15877. (setq cmd (split-string cmd))
  15878. (eval (append (list 'call-process (pop cmd) nil nil nil) cmd))))
  15879. (error nil))
  15880. (cd dir))
  15881. (if (not (file-exists-p pdffile))
  15882. (progn (message "Failed to create pdf file from %s" texfile) nil)
  15883. (condition-case nil
  15884. (if (featurep 'xemacs)
  15885. (call-process "convert" nil nil nil
  15886. "-density" "96"
  15887. "-trim"
  15888. "-antialias"
  15889. pdffile
  15890. "-quality" "100"
  15891. ;; "-sharpen" "0x1.0"
  15892. pngfile)
  15893. (call-process "convert" nil nil nil
  15894. "-density" dpi
  15895. "-trim"
  15896. "-antialias"
  15897. pdffile
  15898. "-quality" "100"
  15899. ; "-sharpen" "0x1.0"
  15900. pngfile))
  15901. (error nil))
  15902. (if (not (file-exists-p pngfile))
  15903. (if org-format-latex-signal-error
  15904. (error "Failed to create png file from %s" texfile)
  15905. (message "Failed to create png file from %s" texfile)
  15906. nil)
  15907. ;; Use the requested file name and clean up
  15908. (copy-file pngfile tofile 'replace)
  15909. (loop for e in '(".pdf" ".tex" ".aux" ".log" ".png") do
  15910. (delete-file (concat texfilebase e)))
  15911. pngfile))))
  15912. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  15913. "Fill a LaTeX header template TPL.
  15914. In the template, the following place holders will be recognized:
  15915. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  15916. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  15917. [PACKAGES] \\usepackage statements for PKG
  15918. [NO-PACKAGES] do not include PKG
  15919. [EXTRA] the string EXTRA
  15920. [NO-EXTRA] do not include EXTRA
  15921. For backward compatibility, if both the positive and the negative place
  15922. holder is missing, the positive one (without the \"NO-\") will be
  15923. assumed to be present at the end of the template.
  15924. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  15925. EXTRA is a string.
  15926. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  15927. (let (rpl (end ""))
  15928. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  15929. (setq rpl (if (or (match-end 1) (not def-pkg))
  15930. "" (org-latex-packages-to-string def-pkg snippets-p t))
  15931. tpl (replace-match rpl t t tpl))
  15932. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  15933. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  15934. (setq rpl (if (or (match-end 1) (not pkg))
  15935. "" (org-latex-packages-to-string pkg snippets-p t))
  15936. tpl (replace-match rpl t t tpl))
  15937. (if pkg (setq end
  15938. (concat end "\n"
  15939. (org-latex-packages-to-string pkg snippets-p)))))
  15940. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  15941. (setq rpl (if (or (match-end 1) (not extra))
  15942. "" (concat extra "\n"))
  15943. tpl (replace-match rpl t t tpl))
  15944. (if (and extra (string-match "\\S-" extra))
  15945. (setq end (concat end "\n" extra))))
  15946. (if (string-match "\\S-" end)
  15947. (concat tpl "\n" end)
  15948. tpl)))
  15949. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  15950. "Turn an alist of packages into a string with the \\usepackage macros."
  15951. (setq pkg (mapconcat (lambda(p)
  15952. (cond
  15953. ((stringp p) p)
  15954. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  15955. (format "%% Package %s omitted" (cadr p)))
  15956. ((equal "" (car p))
  15957. (format "\\usepackage{%s}" (cadr p)))
  15958. (t
  15959. (format "\\usepackage[%s]{%s}"
  15960. (car p) (cadr p)))))
  15961. pkg
  15962. "\n"))
  15963. (if newline (concat pkg "\n") pkg))
  15964. (defun org-dvipng-color (attr)
  15965. "Return a RGB color specification for dvipng."
  15966. (apply 'format "rgb %s %s %s"
  15967. (mapcar 'org-normalize-color
  15968. (if (featurep 'xemacs)
  15969. (color-rgb-components
  15970. (face-property 'default
  15971. (cond ((eq attr :foreground) 'foreground)
  15972. ((eq attr :background) 'background))))
  15973. (color-values (face-attribute 'default attr nil))))))
  15974. (defun org-latex-color (attr)
  15975. "Return a RGB color for the LaTeX color package."
  15976. (apply 'format "%s,%s,%s"
  15977. (mapcar 'org-normalize-color
  15978. (if (featurep 'xemacs)
  15979. (color-rgb-components
  15980. (face-property 'default
  15981. (cond ((eq attr :foreground) 'foreground)
  15982. ((eq attr :background) 'background))))
  15983. (color-values (face-attribute 'default attr nil))))))
  15984. (defun org-latex-color-format (color-name)
  15985. "Convert COLOR-NAME to a RGB color value."
  15986. (apply 'format "%s,%s,%s"
  15987. (mapcar 'org-normalize-color
  15988. (color-values color-name))))
  15989. (defun org-normalize-color (value)
  15990. "Return string to be used as color value for an RGB component."
  15991. (format "%g" (/ value 65535.0)))
  15992. ;; Image display
  15993. (defvar org-inline-image-overlays nil)
  15994. (make-variable-buffer-local 'org-inline-image-overlays)
  15995. (defun org-toggle-inline-images (&optional include-linked)
  15996. "Toggle the display of inline images.
  15997. INCLUDE-LINKED is passed to `org-display-inline-images'."
  15998. (interactive "P")
  15999. (if org-inline-image-overlays
  16000. (progn
  16001. (org-remove-inline-images)
  16002. (message "Inline image display turned off"))
  16003. (org-display-inline-images include-linked)
  16004. (if org-inline-image-overlays
  16005. (message "%d images displayed inline"
  16006. (length org-inline-image-overlays))
  16007. (message "No images to display inline"))))
  16008. (defun org-display-inline-images (&optional include-linked refresh beg end)
  16009. "Display inline images.
  16010. Normally only links without a description part are inlined, because this
  16011. is how it will work for export. When INCLUDE-LINKED is set, also links
  16012. with a description part will be inlined. This can be nice for a quick
  16013. look at those images, but it does not reflect what exported files will look
  16014. like.
  16015. When REFRESH is set, refresh existing images between BEG and END.
  16016. This will create new image displays only if necessary.
  16017. BEG and END default to the buffer boundaries."
  16018. (interactive "P")
  16019. (unless refresh
  16020. (org-remove-inline-images)
  16021. (if (fboundp 'clear-image-cache) (clear-image-cache)))
  16022. (save-excursion
  16023. (save-restriction
  16024. (widen)
  16025. (setq beg (or beg (point-min)) end (or end (point-max)))
  16026. (goto-char beg)
  16027. (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
  16028. (substring (org-image-file-name-regexp) 0 -2)
  16029. "\\)\\]" (if include-linked "" "\\]")))
  16030. old file ov img)
  16031. (while (re-search-forward re end t)
  16032. (setq old (get-char-property-and-overlay (match-beginning 1)
  16033. 'org-image-overlay))
  16034. (setq file (expand-file-name
  16035. (concat (or (match-string 3) "") (match-string 4))))
  16036. (when (file-exists-p file)
  16037. (if (and (car-safe old) refresh)
  16038. (image-refresh (overlay-get (cdr old) 'display))
  16039. (setq img (save-match-data (create-image file)))
  16040. (when img
  16041. (setq ov (make-overlay (match-beginning 0) (match-end 0)))
  16042. (overlay-put ov 'display img)
  16043. (overlay-put ov 'face 'default)
  16044. (overlay-put ov 'org-image-overlay t)
  16045. (overlay-put ov 'modification-hooks
  16046. (list 'org-display-inline-modification-hook))
  16047. (push ov org-inline-image-overlays)))))))))
  16048. (defun org-display-inline-modification-hook (ov after beg end &optional len)
  16049. "Remove inline-display overlay if a corresponding region is modified."
  16050. (let ((inhibit-modification-hooks t))
  16051. (when (and ov after)
  16052. (delete ov org-inline-image-overlays)
  16053. (delete-overlay ov))))
  16054. (defun org-remove-inline-images ()
  16055. "Remove inline display of images."
  16056. (interactive)
  16057. (mapc 'delete-overlay org-inline-image-overlays)
  16058. (setq org-inline-image-overlays nil))
  16059. ;;;; Key bindings
  16060. ;; Outline functions from `outline-mode-prefix-map'
  16061. ;; that can be remapped in Org:
  16062. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  16063. (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
  16064. (define-key org-mode-map [remap outline-forward-same-level]
  16065. 'org-forward-same-level)
  16066. (define-key org-mode-map [remap outline-backward-same-level]
  16067. 'org-backward-same-level)
  16068. (define-key org-mode-map [remap show-branches]
  16069. 'org-kill-note-or-show-branches)
  16070. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  16071. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  16072. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  16073. ;; Outline functions from `outline-mode-prefix-map'
  16074. ;; that can not be remapped in Org:
  16075. ;; - the column "key binding" shows whether the Outline function is still
  16076. ;; available in Org mode on the same key that it has been bound to in
  16077. ;; Outline mode:
  16078. ;; - "overridden": key used for a different functionality in Org mode
  16079. ;; - else: key still bound to the same Outline function in Org mode
  16080. ;; | Outline function | key binding | Org replacement |
  16081. ;; |------------------------------------+-------------+-----------------------|
  16082. ;; | `outline-next-visible-heading' | `C-c C-n' | still same function |
  16083. ;; | `outline-previous-visible-heading' | `C-c C-p' | still same function |
  16084. ;; | `show-children' | `C-c C-i' | visibility cycling |
  16085. ;; | `hide-subtree' | overridden | visibility cycling |
  16086. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  16087. ;; | `hide-body' | overridden | no replacement |
  16088. ;; | `show-all' | overridden | no replacement |
  16089. ;; | `hide-entry' | overridden | visibility cycling |
  16090. ;; | `show-entry' | overridden | no replacement |
  16091. ;; | `hide-leaves' | overridden | no replacement |
  16092. ;; | `hide-sublevels' | overridden | no replacement |
  16093. ;; | `hide-other' | overridden | no replacement |
  16094. ;; | `outline-move-subtree-up' | `C-c C-^' | better: org-shiftup |
  16095. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  16096. ;; Make `C-c C-x' a prefix key
  16097. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  16098. ;; TAB key with modifiers
  16099. (org-defkey org-mode-map "\C-i" 'org-cycle)
  16100. (org-defkey org-mode-map [(tab)] 'org-cycle)
  16101. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  16102. (org-defkey org-mode-map "\M-\t" 'pcomplete)
  16103. ;; The following line is necessary under Suse GNU/Linux
  16104. (unless (featurep 'xemacs)
  16105. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  16106. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  16107. (define-key org-mode-map [backtab] 'org-shifttab)
  16108. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  16109. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  16110. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  16111. ;; Cursor keys with modifiers
  16112. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  16113. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  16114. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  16115. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  16116. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  16117. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  16118. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  16119. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  16120. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  16121. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  16122. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  16123. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  16124. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  16125. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  16126. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  16127. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  16128. ;; Babel keys
  16129. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  16130. (mapc (lambda (pair)
  16131. (define-key org-babel-map (car pair) (cdr pair)))
  16132. org-babel-key-bindings)
  16133. ;;; Extra keys for tty access.
  16134. ;; We only set them when really needed because otherwise the
  16135. ;; menus don't show the simple keys
  16136. (when (or org-use-extra-keys
  16137. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  16138. (not window-system))
  16139. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  16140. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  16141. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  16142. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  16143. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  16144. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  16145. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  16146. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  16147. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  16148. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  16149. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  16150. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  16151. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  16152. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  16153. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  16154. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  16155. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  16156. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  16157. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  16158. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  16159. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  16160. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  16161. (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
  16162. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  16163. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  16164. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  16165. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  16166. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  16167. ;; All the other keys
  16168. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  16169. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  16170. (if (boundp 'narrow-map)
  16171. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  16172. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  16173. (if (boundp 'narrow-map)
  16174. (org-defkey narrow-map "b" 'org-narrow-to-block)
  16175. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  16176. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-same-level)
  16177. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-same-level)
  16178. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  16179. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  16180. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  16181. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  16182. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  16183. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  16184. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  16185. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  16186. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  16187. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  16188. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  16189. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  16190. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  16191. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  16192. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  16193. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  16194. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  16195. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  16196. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  16197. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  16198. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  16199. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  16200. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  16201. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  16202. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  16203. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  16204. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  16205. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  16206. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  16207. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  16208. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  16209. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  16210. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  16211. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  16212. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  16213. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  16214. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  16215. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  16216. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  16217. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  16218. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  16219. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  16220. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  16221. (org-defkey org-mode-map "\C-c^" 'org-sort)
  16222. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  16223. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  16224. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  16225. (org-defkey org-mode-map "\C-m" 'org-return)
  16226. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  16227. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  16228. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  16229. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  16230. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  16231. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  16232. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  16233. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  16234. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  16235. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  16236. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  16237. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  16238. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  16239. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  16240. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  16241. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  16242. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  16243. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  16244. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  16245. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  16246. (org-defkey org-mode-map "\C-c\C-@" 'org-mark-list)
  16247. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  16248. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  16249. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  16250. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  16251. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  16252. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  16253. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  16254. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  16255. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  16256. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  16257. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  16258. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  16259. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  16260. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  16261. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  16262. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  16263. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  16264. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  16265. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  16266. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  16267. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  16268. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  16269. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  16270. (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
  16271. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  16272. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  16273. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  16274. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  16275. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  16276. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  16277. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  16278. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  16279. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  16280. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  16281. (when (featurep 'xemacs)
  16282. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  16283. (defconst org-speed-commands-default
  16284. '(
  16285. ("Outline Navigation")
  16286. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  16287. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  16288. ("f" . (org-speed-move-safe 'org-forward-same-level))
  16289. ("b" . (org-speed-move-safe 'org-backward-same-level))
  16290. ("u" . (org-speed-move-safe 'outline-up-heading))
  16291. ("j" . org-goto)
  16292. ("g" . (org-refile t))
  16293. ("Outline Visibility")
  16294. ("c" . org-cycle)
  16295. ("C" . org-shifttab)
  16296. (" " . org-display-outline-path)
  16297. ("Outline Structure Editing")
  16298. ("U" . org-shiftmetaup)
  16299. ("D" . org-shiftmetadown)
  16300. ("r" . org-metaright)
  16301. ("l" . org-metaleft)
  16302. ("R" . org-shiftmetaright)
  16303. ("L" . org-shiftmetaleft)
  16304. ("i" . (progn (forward-char 1) (call-interactively
  16305. 'org-insert-heading-respect-content)))
  16306. ("^" . org-sort)
  16307. ("w" . org-refile)
  16308. ("a" . org-archive-subtree-default-with-confirmation)
  16309. ("." . org-mark-subtree)
  16310. ("Clock Commands")
  16311. ("I" . org-clock-in)
  16312. ("O" . org-clock-out)
  16313. ("Meta Data Editing")
  16314. ("t" . org-todo)
  16315. ("0" . (org-priority ?\ ))
  16316. ("1" . (org-priority ?A))
  16317. ("2" . (org-priority ?B))
  16318. ("3" . (org-priority ?C))
  16319. (";" . org-set-tags-command)
  16320. ("e" . org-set-effort)
  16321. ("Agenda Views etc")
  16322. ("v" . org-agenda)
  16323. ("/" . org-sparse-tree)
  16324. ("Misc")
  16325. ("o" . org-open-at-point)
  16326. ("?" . org-speed-command-help)
  16327. ("<" . (org-agenda-set-restriction-lock 'subtree))
  16328. (">" . (org-agenda-remove-restriction-lock))
  16329. )
  16330. "The default speed commands.")
  16331. (defun org-print-speed-command (e)
  16332. (if (> (length (car e)) 1)
  16333. (progn
  16334. (princ "\n")
  16335. (princ (car e))
  16336. (princ "\n")
  16337. (princ (make-string (length (car e)) ?-))
  16338. (princ "\n"))
  16339. (princ (car e))
  16340. (princ " ")
  16341. (if (symbolp (cdr e))
  16342. (princ (symbol-name (cdr e)))
  16343. (prin1 (cdr e)))
  16344. (princ "\n")))
  16345. (defun org-speed-command-help ()
  16346. "Show the available speed commands."
  16347. (interactive)
  16348. (if (not org-use-speed-commands)
  16349. (error "Speed commands are not activated, customize `org-use-speed-commands'")
  16350. (with-output-to-temp-buffer "*Help*"
  16351. (princ "User-defined Speed commands\n===========================\n")
  16352. (mapc 'org-print-speed-command org-speed-commands-user)
  16353. (princ "\n")
  16354. (princ "Built-in Speed commands\n=======================\n")
  16355. (mapc 'org-print-speed-command org-speed-commands-default))
  16356. (with-current-buffer "*Help*"
  16357. (setq truncate-lines t))))
  16358. (defun org-speed-move-safe (cmd)
  16359. "Execute CMD, but make sure that the cursor always ends up in a headline.
  16360. If not, return to the original position and throw an error."
  16361. (interactive)
  16362. (let ((pos (point)))
  16363. (call-interactively cmd)
  16364. (unless (and (bolp) (org-at-heading-p))
  16365. (goto-char pos)
  16366. (error "Boundary reached while executing %s" cmd))))
  16367. (defvar org-self-insert-command-undo-counter 0)
  16368. (defvar org-table-auto-blank-field) ; defined in org-table.el
  16369. (defvar org-speed-command nil)
  16370. (defun org-speed-command-default-hook (keys)
  16371. "Hook for activating single-letter speed commands.
  16372. `org-speed-commands-default' specifies a minimal command set.
  16373. Use `org-speed-commands-user' for further customization."
  16374. (when (or (and (bolp) (looking-at org-outline-regexp))
  16375. (and (functionp org-use-speed-commands)
  16376. (funcall org-use-speed-commands)))
  16377. (cdr (assoc keys (append org-speed-commands-user
  16378. org-speed-commands-default)))))
  16379. (defun org-babel-speed-command-hook (keys)
  16380. "Hook for activating single-letter code block commands."
  16381. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  16382. (cdr (assoc keys org-babel-key-bindings))))
  16383. (defcustom org-speed-command-hook
  16384. '(org-speed-command-default-hook org-babel-speed-command-hook)
  16385. "Hook for activating speed commands at strategic locations.
  16386. Hook functions are called in sequence until a valid handler is
  16387. found.
  16388. Each hook takes a single argument, a user-pressed command key
  16389. which is also a `self-insert-command' from the global map.
  16390. Within the hook, examine the cursor position and the command key
  16391. and return nil or a valid handler as appropriate. Handler could
  16392. be one of an interactive command, a function, or a form.
  16393. Set `org-use-speed-commands' to non-nil value to enable this
  16394. hook. The default setting is `org-speed-command-default-hook'."
  16395. :group 'org-structure
  16396. :version "24.1"
  16397. :type 'hook)
  16398. (defun org-self-insert-command (N)
  16399. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  16400. If the cursor is in a table looking at whitespace, the whitespace is
  16401. overwritten, and the table is not marked as requiring realignment."
  16402. (interactive "p")
  16403. (org-check-before-invisible-edit 'insert)
  16404. (cond
  16405. ((and org-use-speed-commands
  16406. (setq org-speed-command
  16407. (run-hook-with-args-until-success
  16408. 'org-speed-command-hook (this-command-keys))))
  16409. (cond
  16410. ((commandp org-speed-command)
  16411. (setq this-command org-speed-command)
  16412. (call-interactively org-speed-command))
  16413. ((functionp org-speed-command)
  16414. (funcall org-speed-command))
  16415. ((and org-speed-command (listp org-speed-command))
  16416. (eval org-speed-command))
  16417. (t (let (org-use-speed-commands)
  16418. (call-interactively 'org-self-insert-command)))))
  16419. ((and
  16420. (org-table-p)
  16421. (progn
  16422. ;; check if we blank the field, and if that triggers align
  16423. (and (featurep 'org-table) org-table-auto-blank-field
  16424. (member last-command
  16425. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  16426. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  16427. ;; got extra space, this field does not determine column width
  16428. (let (org-table-may-need-update) (org-table-blank-field))
  16429. ;; no extra space, this field may determine column width
  16430. (org-table-blank-field)))
  16431. t)
  16432. (eq N 1)
  16433. (looking-at "[^|\n]* |"))
  16434. (let (org-table-may-need-update)
  16435. (goto-char (1- (match-end 0)))
  16436. (backward-delete-char 1)
  16437. (goto-char (match-beginning 0))
  16438. (self-insert-command N)))
  16439. (t
  16440. (setq org-table-may-need-update t)
  16441. (self-insert-command N)
  16442. (org-fix-tags-on-the-fly)
  16443. (if org-self-insert-cluster-for-undo
  16444. (if (not (eq last-command 'org-self-insert-command))
  16445. (setq org-self-insert-command-undo-counter 1)
  16446. (if (>= org-self-insert-command-undo-counter 20)
  16447. (setq org-self-insert-command-undo-counter 1)
  16448. (and (> org-self-insert-command-undo-counter 0)
  16449. buffer-undo-list (listp buffer-undo-list)
  16450. (not (cadr buffer-undo-list)) ; remove nil entry
  16451. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  16452. (setq org-self-insert-command-undo-counter
  16453. (1+ org-self-insert-command-undo-counter))))))))
  16454. (defun org-check-before-invisible-edit (kind)
  16455. "Check is editing if kind KIND would be dangerous with invisible text around.
  16456. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  16457. ;; First, try to get out of here as quickly as possible, to reduce overhead
  16458. (if (and org-catch-invisible-edits
  16459. (or (not (boundp 'visible-mode)) (not visible-mode))
  16460. (or (get-char-property (point) 'invisible)
  16461. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  16462. ;; OK, we need to take a closer look
  16463. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  16464. (invisible-before-point (if (bobp) nil (get-char-property
  16465. (1- (point)) 'invisible)))
  16466. (border-and-ok-direction
  16467. (or
  16468. ;; Check if we are acting predictably before invisible text
  16469. (and invisible-at-point (not invisible-before-point)
  16470. (memq kind '(insert delete-backward)))
  16471. ;; Check if we are acting predictably after invisible text
  16472. ;; This works not well, and I have turned it off. It seems
  16473. ;; better to always show and stop after invisible text.
  16474. ;; (and (not invisible-at-point) invisible-before-point
  16475. ;; (memq kind '(insert delete)))
  16476. )))
  16477. (when (or (memq invisible-at-point '(outline org-hide-block))
  16478. (memq invisible-before-point '(outline org-hide-block)))
  16479. (if (eq org-catch-invisible-edits 'error)
  16480. (error "Editing in invisible areas is prohibited - make visible first"))
  16481. ;; Make the area visible
  16482. (save-excursion
  16483. (if invisible-before-point
  16484. (goto-char (previous-single-char-property-change
  16485. (point) 'invisible)))
  16486. (org-cycle))
  16487. (cond
  16488. ((eq org-catch-invisible-edits 'show)
  16489. ;; That's it, we do the edit after showing
  16490. (message
  16491. "Unfolding invisible region around point before editing")
  16492. (sit-for 1))
  16493. ((and (eq org-catch-invisible-edits 'smart)
  16494. border-and-ok-direction)
  16495. (message "Unfolding invisible region around point before editing"))
  16496. (t
  16497. ;; Don't do the edit, make the user repeat it in full visibility
  16498. (error "Edit in invisible region aborted, repeat to confirm with text visible")))))))
  16499. (defun org-fix-tags-on-the-fly ()
  16500. (when (and (equal (char-after (point-at-bol)) ?*)
  16501. (org-at-heading-p))
  16502. (org-align-tags-here org-tags-column)))
  16503. (defun org-delete-backward-char (N)
  16504. "Like `delete-backward-char', insert whitespace at field end in tables.
  16505. When deleting backwards, in tables this function will insert whitespace in
  16506. front of the next \"|\" separator, to keep the table aligned. The table will
  16507. still be marked for re-alignment if the field did fill the entire column,
  16508. because, in this case the deletion might narrow the column."
  16509. (interactive "p")
  16510. (org-check-before-invisible-edit 'delete-backward)
  16511. (if (and (org-table-p)
  16512. (eq N 1)
  16513. (string-match "|" (buffer-substring (point-at-bol) (point)))
  16514. (looking-at ".*?|"))
  16515. (let ((pos (point))
  16516. (noalign (looking-at "[^|\n\r]* |"))
  16517. (c org-table-may-need-update))
  16518. (backward-delete-char N)
  16519. (if (not overwrite-mode)
  16520. (progn
  16521. (skip-chars-forward "^|")
  16522. (insert " ")
  16523. (goto-char (1- pos))))
  16524. ;; noalign: if there were two spaces at the end, this field
  16525. ;; does not determine the width of the column.
  16526. (if noalign (setq org-table-may-need-update c)))
  16527. (backward-delete-char N)
  16528. (org-fix-tags-on-the-fly)))
  16529. (defun org-delete-char (N)
  16530. "Like `delete-char', but insert whitespace at field end in tables.
  16531. When deleting characters, in tables this function will insert whitespace in
  16532. front of the next \"|\" separator, to keep the table aligned. The table will
  16533. still be marked for re-alignment if the field did fill the entire column,
  16534. because, in this case the deletion might narrow the column."
  16535. (interactive "p")
  16536. (org-check-before-invisible-edit 'delete)
  16537. (if (and (org-table-p)
  16538. (not (bolp))
  16539. (not (= (char-after) ?|))
  16540. (eq N 1))
  16541. (if (looking-at ".*?|")
  16542. (let ((pos (point))
  16543. (noalign (looking-at "[^|\n\r]* |"))
  16544. (c org-table-may-need-update))
  16545. (replace-match (concat
  16546. (substring (match-string 0) 1 -1)
  16547. " |"))
  16548. (goto-char pos)
  16549. ;; noalign: if there were two spaces at the end, this field
  16550. ;; does not determine the width of the column.
  16551. (if noalign (setq org-table-may-need-update c)))
  16552. (delete-char N))
  16553. (delete-char N)
  16554. (org-fix-tags-on-the-fly)))
  16555. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  16556. (put 'org-self-insert-command 'delete-selection t)
  16557. (put 'orgtbl-self-insert-command 'delete-selection t)
  16558. (put 'org-delete-char 'delete-selection 'supersede)
  16559. (put 'org-delete-backward-char 'delete-selection 'supersede)
  16560. (put 'org-yank 'delete-selection 'yank)
  16561. ;; Make `flyspell-mode' delay after some commands
  16562. (put 'org-self-insert-command 'flyspell-delayed t)
  16563. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  16564. (put 'org-delete-char 'flyspell-delayed t)
  16565. (put 'org-delete-backward-char 'flyspell-delayed t)
  16566. ;; Make pabbrev-mode expand after org-mode commands
  16567. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  16568. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  16569. ;; How to do this: Measure non-white length of current string
  16570. ;; If equal to column width, we should realign.
  16571. (defun org-remap (map &rest commands)
  16572. "In MAP, remap the functions given in COMMANDS.
  16573. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  16574. (let (new old)
  16575. (while commands
  16576. (setq old (pop commands) new (pop commands))
  16577. (if (fboundp 'command-remapping)
  16578. (org-defkey map (vector 'remap old) new)
  16579. (substitute-key-definition old new map global-map)))))
  16580. (when (eq org-enable-table-editor 'optimized)
  16581. ;; If the user wants maximum table support, we need to hijack
  16582. ;; some standard editing functions
  16583. (org-remap org-mode-map
  16584. 'self-insert-command 'org-self-insert-command
  16585. 'delete-char 'org-delete-char
  16586. 'delete-backward-char 'org-delete-backward-char)
  16587. (org-defkey org-mode-map "|" 'org-force-self-insert))
  16588. (defvar org-ctrl-c-ctrl-c-hook nil
  16589. "Hook for functions attaching themselves to `C-c C-c'.
  16590. This can be used to add additional functionality to the C-c C-c
  16591. key which executes context-dependent commands. This hook is run
  16592. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  16593. run after the last test.
  16594. Each function will be called with no arguments. The function
  16595. must check if the context is appropriate for it to act. If yes,
  16596. it should do its thing and then return a non-nil value. If the
  16597. context is wrong, just do nothing and return nil.")
  16598. (defvar org-ctrl-c-ctrl-c-final-hook nil
  16599. "Hook for functions attaching themselves to `C-c C-c'.
  16600. This can be used to add additional functionality to the C-c C-c
  16601. key which executes context-dependent commands. This hook is run
  16602. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  16603. before the first test.
  16604. Each function will be called with no arguments. The function
  16605. must check if the context is appropriate for it to act. If yes,
  16606. it should do its thing and then return a non-nil value. If the
  16607. context is wrong, just do nothing and return nil.")
  16608. (defvar org-tab-first-hook nil
  16609. "Hook for functions to attach themselves to TAB.
  16610. See `org-ctrl-c-ctrl-c-hook' for more information.
  16611. This hook runs as the first action when TAB is pressed, even before
  16612. `org-cycle' messes around with the `outline-regexp' to cater for
  16613. inline tasks and plain list item folding.
  16614. If any function in this hook returns t, any other actions that
  16615. would have been caused by TAB (such as table field motion or visibility
  16616. cycling) will not occur.")
  16617. (defvar org-tab-after-check-for-table-hook nil
  16618. "Hook for functions to attach themselves to TAB.
  16619. See `org-ctrl-c-ctrl-c-hook' for more information.
  16620. This hook runs after it has been established that the cursor is not in a
  16621. table, but before checking if the cursor is in a headline or if global cycling
  16622. should be done.
  16623. If any function in this hook returns t, not other actions like visibility
  16624. cycling will be done.")
  16625. (defvar org-tab-after-check-for-cycling-hook nil
  16626. "Hook for functions to attach themselves to TAB.
  16627. See `org-ctrl-c-ctrl-c-hook' for more information.
  16628. This hook runs after it has been established that not table field motion and
  16629. not visibility should be done because of current context. This is probably
  16630. the place where a package like yasnippets can hook in.")
  16631. (defvar org-tab-before-tab-emulation-hook nil
  16632. "Hook for functions to attach themselves to TAB.
  16633. See `org-ctrl-c-ctrl-c-hook' for more information.
  16634. This hook runs after every other options for TAB have been exhausted, but
  16635. before indentation and \t insertion takes place.")
  16636. (defvar org-metaleft-hook nil
  16637. "Hook for functions attaching themselves to `M-left'.
  16638. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16639. (defvar org-metaright-hook nil
  16640. "Hook for functions attaching themselves to `M-right'.
  16641. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16642. (defvar org-metaup-hook nil
  16643. "Hook for functions attaching themselves to `M-up'.
  16644. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16645. (defvar org-metadown-hook nil
  16646. "Hook for functions attaching themselves to `M-down'.
  16647. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16648. (defvar org-shiftmetaleft-hook nil
  16649. "Hook for functions attaching themselves to `M-S-left'.
  16650. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16651. (defvar org-shiftmetaright-hook nil
  16652. "Hook for functions attaching themselves to `M-S-right'.
  16653. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16654. (defvar org-shiftmetaup-hook nil
  16655. "Hook for functions attaching themselves to `M-S-up'.
  16656. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16657. (defvar org-shiftmetadown-hook nil
  16658. "Hook for functions attaching themselves to `M-S-down'.
  16659. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16660. (defvar org-metareturn-hook nil
  16661. "Hook for functions attaching themselves to `M-RET'.
  16662. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16663. (defvar org-shiftup-hook nil
  16664. "Hook for functions attaching themselves to `S-up'.
  16665. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16666. (defvar org-shiftup-final-hook nil
  16667. "Hook for functions attaching themselves to `S-up'.
  16668. This one runs after all other options except shift-select have been excluded.
  16669. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16670. (defvar org-shiftdown-hook nil
  16671. "Hook for functions attaching themselves to `S-down'.
  16672. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16673. (defvar org-shiftdown-final-hook nil
  16674. "Hook for functions attaching themselves to `S-down'.
  16675. This one runs after all other options except shift-select have been excluded.
  16676. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16677. (defvar org-shiftleft-hook nil
  16678. "Hook for functions attaching themselves to `S-left'.
  16679. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16680. (defvar org-shiftleft-final-hook nil
  16681. "Hook for functions attaching themselves to `S-left'.
  16682. This one runs after all other options except shift-select have been excluded.
  16683. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16684. (defvar org-shiftright-hook nil
  16685. "Hook for functions attaching themselves to `S-right'.
  16686. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16687. (defvar org-shiftright-final-hook nil
  16688. "Hook for functions attaching themselves to `S-right'.
  16689. This one runs after all other options except shift-select have been excluded.
  16690. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16691. (defun org-modifier-cursor-error ()
  16692. "Throw an error, a modified cursor command was applied in wrong context."
  16693. (error "This command is active in special context like tables, headlines or items"))
  16694. (defun org-shiftselect-error ()
  16695. "Throw an error because Shift-Cursor command was applied in wrong context."
  16696. (if (and (boundp 'shift-select-mode) shift-select-mode)
  16697. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  16698. (error "This command works only in special context like headlines or timestamps")))
  16699. (defun org-call-for-shift-select (cmd)
  16700. (let ((this-command-keys-shift-translated t))
  16701. (call-interactively cmd)))
  16702. (defun org-shifttab (&optional arg)
  16703. "Global visibility cycling or move to previous table field.
  16704. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  16705. on context.
  16706. See the individual commands for more information."
  16707. (interactive "P")
  16708. (cond
  16709. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  16710. ((integerp arg)
  16711. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  16712. (message "Content view to level: %d" arg)
  16713. (org-content (prefix-numeric-value arg2))
  16714. (setq org-cycle-global-status 'overview)))
  16715. (t (call-interactively 'org-global-cycle))))
  16716. (defun org-shiftmetaleft ()
  16717. "Promote subtree or delete table column.
  16718. Calls `org-promote-subtree', `org-outdent-item-tree', or
  16719. `org-table-delete-column', depending on context. See the
  16720. individual commands for more information."
  16721. (interactive)
  16722. (cond
  16723. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  16724. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  16725. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  16726. ((if (not (org-region-active-p)) (org-at-item-p)
  16727. (save-excursion (goto-char (region-beginning))
  16728. (org-at-item-p)))
  16729. (call-interactively 'org-outdent-item-tree))
  16730. (t (org-modifier-cursor-error))))
  16731. (defun org-shiftmetaright ()
  16732. "Demote subtree or insert table column.
  16733. Calls `org-demote-subtree', `org-indent-item-tree', or
  16734. `org-table-insert-column', depending on context. See the
  16735. individual commands for more information."
  16736. (interactive)
  16737. (cond
  16738. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  16739. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  16740. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  16741. ((if (not (org-region-active-p)) (org-at-item-p)
  16742. (save-excursion (goto-char (region-beginning))
  16743. (org-at-item-p)))
  16744. (call-interactively 'org-indent-item-tree))
  16745. (t (org-modifier-cursor-error))))
  16746. (defun org-shiftmetaup (&optional arg)
  16747. "Move subtree up or kill table row.
  16748. Calls `org-move-subtree-up' or `org-table-kill-row' or
  16749. `org-move-item-up' depending on context. See the individual commands
  16750. for more information."
  16751. (interactive "P")
  16752. (cond
  16753. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  16754. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  16755. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  16756. ((org-at-item-p) (call-interactively 'org-move-item-up))
  16757. (t (org-modifier-cursor-error))))
  16758. (defun org-shiftmetadown (&optional arg)
  16759. "Move subtree down or insert table row.
  16760. Calls `org-move-subtree-down' or `org-table-insert-row' or
  16761. `org-move-item-down', depending on context. See the individual
  16762. commands for more information."
  16763. (interactive "P")
  16764. (cond
  16765. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  16766. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  16767. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  16768. ((org-at-item-p) (call-interactively 'org-move-item-down))
  16769. (t (org-modifier-cursor-error))))
  16770. (defsubst org-hidden-tree-error ()
  16771. (error
  16772. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  16773. (defun org-metaleft (&optional arg)
  16774. "Promote heading or move table column to left.
  16775. Calls `org-do-promote' or `org-table-move-column', depending on context.
  16776. With no specific context, calls the Emacs default `backward-word'.
  16777. See the individual commands for more information."
  16778. (interactive "P")
  16779. (cond
  16780. ((run-hook-with-args-until-success 'org-metaleft-hook))
  16781. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  16782. ((org-with-limited-levels
  16783. (or (org-at-heading-p)
  16784. (and (org-region-active-p)
  16785. (save-excursion
  16786. (goto-char (region-beginning))
  16787. (org-at-heading-p)))))
  16788. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  16789. (call-interactively 'org-do-promote))
  16790. ;; At an inline task.
  16791. ((org-at-heading-p)
  16792. (call-interactively 'org-inlinetask-promote))
  16793. ((or (org-at-item-p)
  16794. (and (org-region-active-p)
  16795. (save-excursion
  16796. (goto-char (region-beginning))
  16797. (org-at-item-p))))
  16798. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  16799. (call-interactively 'org-outdent-item))
  16800. (t (call-interactively 'backward-word))))
  16801. (defun org-metaright (&optional arg)
  16802. "Demote a subtree, a list item or move table column to right.
  16803. In front of a drawer or a block keyword, indent it correctly.
  16804. With no specific context, calls the Emacs default `forward-word'.
  16805. See the individual commands for more information."
  16806. (interactive "P")
  16807. (cond
  16808. ((run-hook-with-args-until-success 'org-metaright-hook))
  16809. ((org-at-table-p) (call-interactively 'org-table-move-column))
  16810. ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
  16811. ((org-at-block-p) (call-interactively 'org-indent-block))
  16812. ((org-with-limited-levels
  16813. (or (org-at-heading-p)
  16814. (and (org-region-active-p)
  16815. (save-excursion
  16816. (goto-char (region-beginning))
  16817. (org-at-heading-p)))))
  16818. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  16819. (call-interactively 'org-do-demote))
  16820. ;; At an inline task.
  16821. ((org-at-heading-p)
  16822. (call-interactively 'org-inlinetask-demote))
  16823. ((or (org-at-item-p)
  16824. (and (org-region-active-p)
  16825. (save-excursion
  16826. (goto-char (region-beginning))
  16827. (org-at-item-p))))
  16828. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  16829. (call-interactively 'org-indent-item))
  16830. (t (call-interactively 'forward-word))))
  16831. (defun org-check-for-hidden (what)
  16832. "Check if there are hidden headlines/items in the current visual line.
  16833. WHAT can be either `headlines' or `items'. If the current line is
  16834. an outline or item heading and it has a folded subtree below it,
  16835. this function returns t, nil otherwise."
  16836. (let ((re (cond
  16837. ((eq what 'headlines) org-outline-regexp-bol)
  16838. ((eq what 'items) (org-item-beginning-re))
  16839. (t (error "This should not happen"))))
  16840. beg end)
  16841. (save-excursion
  16842. (catch 'exit
  16843. (unless (org-region-active-p)
  16844. (setq beg (point-at-bol))
  16845. (beginning-of-line 2)
  16846. (while (and (not (eobp)) ;; this is like `next-line'
  16847. (get-char-property (1- (point)) 'invisible))
  16848. (beginning-of-line 2))
  16849. (setq end (point))
  16850. (goto-char beg)
  16851. (goto-char (point-at-eol))
  16852. (setq end (max end (point)))
  16853. (while (re-search-forward re end t)
  16854. (if (get-char-property (match-beginning 0) 'invisible)
  16855. (throw 'exit t))))
  16856. nil))))
  16857. (defun org-metaup (&optional arg)
  16858. "Move subtree up or move table row up.
  16859. Calls `org-move-subtree-up' or `org-table-move-row' or
  16860. `org-move-item-up', depending on context. See the individual commands
  16861. for more information."
  16862. (interactive "P")
  16863. (cond
  16864. ((run-hook-with-args-until-success 'org-metaup-hook))
  16865. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  16866. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  16867. ((org-at-item-p) (call-interactively 'org-move-item-up))
  16868. (t (transpose-lines 1) (beginning-of-line -1))))
  16869. (defun org-metadown (&optional arg)
  16870. "Move subtree down or move table row down.
  16871. Calls `org-move-subtree-down' or `org-table-move-row' or
  16872. `org-move-item-down', depending on context. See the individual
  16873. commands for more information."
  16874. (interactive "P")
  16875. (cond
  16876. ((run-hook-with-args-until-success 'org-metadown-hook))
  16877. ((org-at-table-p) (call-interactively 'org-table-move-row))
  16878. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  16879. ((org-at-item-p) (call-interactively 'org-move-item-down))
  16880. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  16881. (defun org-shiftup (&optional arg)
  16882. "Increase item in timestamp or increase priority of current headline.
  16883. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  16884. depending on context. See the individual commands for more information."
  16885. (interactive "P")
  16886. (cond
  16887. ((run-hook-with-args-until-success 'org-shiftup-hook))
  16888. ((and org-support-shift-select (org-region-active-p))
  16889. (org-call-for-shift-select 'previous-line))
  16890. ((org-at-timestamp-p t)
  16891. (call-interactively (if org-edit-timestamp-down-means-later
  16892. 'org-timestamp-down 'org-timestamp-up)))
  16893. ((and (not (eq org-support-shift-select 'always))
  16894. org-enable-priority-commands
  16895. (org-at-heading-p))
  16896. (call-interactively 'org-priority-up))
  16897. ((and (not org-support-shift-select) (org-at-item-p))
  16898. (call-interactively 'org-previous-item))
  16899. ((org-clocktable-try-shift 'up arg))
  16900. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  16901. (org-support-shift-select
  16902. (org-call-for-shift-select 'previous-line))
  16903. (t (org-shiftselect-error))))
  16904. (defun org-shiftdown (&optional arg)
  16905. "Decrease item in timestamp or decrease priority of current headline.
  16906. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  16907. depending on context. See the individual commands for more information."
  16908. (interactive "P")
  16909. (cond
  16910. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  16911. ((and org-support-shift-select (org-region-active-p))
  16912. (org-call-for-shift-select 'next-line))
  16913. ((org-at-timestamp-p t)
  16914. (call-interactively (if org-edit-timestamp-down-means-later
  16915. 'org-timestamp-up 'org-timestamp-down)))
  16916. ((and (not (eq org-support-shift-select 'always))
  16917. org-enable-priority-commands
  16918. (org-at-heading-p))
  16919. (call-interactively 'org-priority-down))
  16920. ((and (not org-support-shift-select) (org-at-item-p))
  16921. (call-interactively 'org-next-item))
  16922. ((org-clocktable-try-shift 'down arg))
  16923. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  16924. (org-support-shift-select
  16925. (org-call-for-shift-select 'next-line))
  16926. (t (org-shiftselect-error))))
  16927. (defun org-shiftright (&optional arg)
  16928. "Cycle the thing at point or in the current line, depending on context.
  16929. Depending on context, this does one of the following:
  16930. - switch a timestamp at point one day into the future
  16931. - on a headline, switch to the next TODO keyword.
  16932. - on an item, switch entire list to the next bullet type
  16933. - on a property line, switch to the next allowed value
  16934. - on a clocktable definition line, move time block into the future"
  16935. (interactive "P")
  16936. (cond
  16937. ((run-hook-with-args-until-success 'org-shiftright-hook))
  16938. ((and org-support-shift-select (org-region-active-p))
  16939. (org-call-for-shift-select 'forward-char))
  16940. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  16941. ((and (not (eq org-support-shift-select 'always))
  16942. (org-at-heading-p))
  16943. (let ((org-inhibit-logging
  16944. (not org-treat-S-cursor-todo-selection-as-state-change))
  16945. (org-inhibit-blocking
  16946. (not org-treat-S-cursor-todo-selection-as-state-change)))
  16947. (org-call-with-arg 'org-todo 'right)))
  16948. ((or (and org-support-shift-select
  16949. (not (eq org-support-shift-select 'always))
  16950. (org-at-item-bullet-p))
  16951. (and (not org-support-shift-select) (org-at-item-p)))
  16952. (org-call-with-arg 'org-cycle-list-bullet nil))
  16953. ((and (not (eq org-support-shift-select 'always))
  16954. (org-at-property-p))
  16955. (call-interactively 'org-property-next-allowed-value))
  16956. ((org-clocktable-try-shift 'right arg))
  16957. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  16958. (org-support-shift-select
  16959. (org-call-for-shift-select 'forward-char))
  16960. (t (org-shiftselect-error))))
  16961. (defun org-shiftleft (&optional arg)
  16962. "Cycle the thing at point or in the current line, depending on context.
  16963. Depending on context, this does one of the following:
  16964. - switch a timestamp at point one day into the past
  16965. - on a headline, switch to the previous TODO keyword.
  16966. - on an item, switch entire list to the previous bullet type
  16967. - on a property line, switch to the previous allowed value
  16968. - on a clocktable definition line, move time block into the past"
  16969. (interactive "P")
  16970. (cond
  16971. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  16972. ((and org-support-shift-select (org-region-active-p))
  16973. (org-call-for-shift-select 'backward-char))
  16974. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  16975. ((and (not (eq org-support-shift-select 'always))
  16976. (org-at-heading-p))
  16977. (let ((org-inhibit-logging
  16978. (not org-treat-S-cursor-todo-selection-as-state-change))
  16979. (org-inhibit-blocking
  16980. (not org-treat-S-cursor-todo-selection-as-state-change)))
  16981. (org-call-with-arg 'org-todo 'left)))
  16982. ((or (and org-support-shift-select
  16983. (not (eq org-support-shift-select 'always))
  16984. (org-at-item-bullet-p))
  16985. (and (not org-support-shift-select) (org-at-item-p)))
  16986. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  16987. ((and (not (eq org-support-shift-select 'always))
  16988. (org-at-property-p))
  16989. (call-interactively 'org-property-previous-allowed-value))
  16990. ((org-clocktable-try-shift 'left arg))
  16991. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  16992. (org-support-shift-select
  16993. (org-call-for-shift-select 'backward-char))
  16994. (t (org-shiftselect-error))))
  16995. (defun org-shiftcontrolright ()
  16996. "Switch to next TODO set."
  16997. (interactive)
  16998. (cond
  16999. ((and org-support-shift-select (org-region-active-p))
  17000. (org-call-for-shift-select 'forward-word))
  17001. ((and (not (eq org-support-shift-select 'always))
  17002. (org-at-heading-p))
  17003. (org-call-with-arg 'org-todo 'nextset))
  17004. (org-support-shift-select
  17005. (org-call-for-shift-select 'forward-word))
  17006. (t (org-shiftselect-error))))
  17007. (defun org-shiftcontrolleft ()
  17008. "Switch to previous TODO set."
  17009. (interactive)
  17010. (cond
  17011. ((and org-support-shift-select (org-region-active-p))
  17012. (org-call-for-shift-select 'backward-word))
  17013. ((and (not (eq org-support-shift-select 'always))
  17014. (org-at-heading-p))
  17015. (org-call-with-arg 'org-todo 'previousset))
  17016. (org-support-shift-select
  17017. (org-call-for-shift-select 'backward-word))
  17018. (t (org-shiftselect-error))))
  17019. (defun org-shiftcontrolup ()
  17020. "Change timestamps synchronously up in CLOCK log lines."
  17021. (interactive)
  17022. (cond ((and (not org-support-shift-select)
  17023. (org-at-clock-log-p)
  17024. (org-at-timestamp-p t))
  17025. (org-clock-timestamps-up))
  17026. (t (org-shiftselect-error))))
  17027. (defun org-shiftcontroldown ()
  17028. "Change timestamps synchronously down in CLOCK log lines."
  17029. (interactive)
  17030. (cond ((and (not org-support-shift-select)
  17031. (org-at-clock-log-p)
  17032. (org-at-timestamp-p t))
  17033. (org-clock-timestamps-down))
  17034. (t (org-shiftselect-error))))
  17035. (defun org-ctrl-c-ret ()
  17036. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  17037. (interactive)
  17038. (cond
  17039. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  17040. (t (call-interactively 'org-insert-heading))))
  17041. (defun org-find-visible ()
  17042. (let ((s (point)))
  17043. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  17044. (get-char-property s 'invisible)))
  17045. s))
  17046. (defun org-find-invisible ()
  17047. (let ((s (point)))
  17048. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  17049. (not (get-char-property s 'invisible))))
  17050. s))
  17051. (defun org-copy-visible (beg end)
  17052. "Copy the visible parts of the region."
  17053. (interactive "r")
  17054. (let (snippets s)
  17055. (save-excursion
  17056. (save-restriction
  17057. (narrow-to-region beg end)
  17058. (setq s (goto-char (point-min)))
  17059. (while (not (= (point) (point-max)))
  17060. (goto-char (org-find-invisible))
  17061. (push (buffer-substring s (point)) snippets)
  17062. (setq s (goto-char (org-find-visible))))))
  17063. (kill-new (apply 'concat (nreverse snippets)))))
  17064. (defun org-copy-special ()
  17065. "Copy region in table or copy current subtree.
  17066. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  17067. See the individual commands for more information."
  17068. (interactive)
  17069. (call-interactively
  17070. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  17071. (defun org-cut-special ()
  17072. "Cut region in table or cut current subtree.
  17073. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  17074. See the individual commands for more information."
  17075. (interactive)
  17076. (call-interactively
  17077. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  17078. (defun org-paste-special (arg)
  17079. "Paste rectangular region into table, or past subtree relative to level.
  17080. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  17081. See the individual commands for more information."
  17082. (interactive "P")
  17083. (if (org-at-table-p)
  17084. (org-table-paste-rectangle)
  17085. (org-paste-subtree arg)))
  17086. (defun org-edit-special (&optional arg)
  17087. "Call a special editor for the stuff at point.
  17088. When at a table, call the formula editor with `org-table-edit-formulas'.
  17089. When at the first line of an src example, call `org-edit-src-code'.
  17090. When in an #+include line, visit the include file. Otherwise call
  17091. `ffap' to visit the file at point."
  17092. (interactive)
  17093. ;; possibly prep session before editing source
  17094. (when arg
  17095. (let* ((info (org-babel-get-src-block-info))
  17096. (lang (nth 0 info))
  17097. (params (nth 2 info))
  17098. (session (cdr (assoc :session params))))
  17099. (when (and info session) ;; we are in a source-code block with a session
  17100. (funcall
  17101. (intern (concat "org-babel-prep-session:" lang)) session params))))
  17102. (cond ;; proceed with `org-edit-special'
  17103. ((save-excursion
  17104. (beginning-of-line 1)
  17105. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  17106. (find-file (org-trim (match-string 1))))
  17107. ((org-edit-src-code))
  17108. ((org-edit-fixed-width-region))
  17109. ((org-at-table.el-p)
  17110. (org-edit-src-code))
  17111. ((or (org-at-table-p)
  17112. (save-excursion
  17113. (beginning-of-line 1)
  17114. (let ((case-fold-search )) (looking-at "[ \t]*#\\+tblfm:"))))
  17115. (call-interactively 'org-table-edit-formulas))
  17116. (t (call-interactively 'ffap))))
  17117. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  17118. (defun org-ctrl-c-ctrl-c (&optional arg)
  17119. "Set tags in headline, or update according to changed information at point.
  17120. This command does many different things, depending on context:
  17121. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  17122. this is what we do.
  17123. - If the cursor is on a statistics cookie, update it.
  17124. - If the cursor is in a headline, prompt for tags and insert them
  17125. into the current line, aligned to `org-tags-column'. When called
  17126. with prefix arg, realign all tags in the current buffer.
  17127. - If the cursor is in one of the special #+KEYWORD lines, this
  17128. triggers scanning the buffer for these lines and updating the
  17129. information.
  17130. - If the cursor is inside a table, realign the table. This command
  17131. works even if the automatic table editor has been turned off.
  17132. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  17133. the entire table.
  17134. - If the cursor is at a footnote reference or definition, jump to
  17135. the corresponding definition or references, respectively.
  17136. - If the cursor is a the beginning of a dynamic block, update it.
  17137. - If the current buffer is a capture buffer, close note and file it.
  17138. - If the cursor is on a <<<target>>>, update radio targets and
  17139. corresponding links in this buffer.
  17140. - If the cursor is on a numbered item in a plain list, renumber the
  17141. ordered list.
  17142. - If the cursor is on a checkbox, toggle it.
  17143. - If the cursor is on a code block, evaluate it. The variable
  17144. `org-confirm-babel-evaluate' can be used to control prompting
  17145. before code block evaluation, by default every code block
  17146. evaluation requires confirmation. Code block evaluation can be
  17147. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  17148. (interactive "P")
  17149. (let ((org-enable-table-editor t))
  17150. (cond
  17151. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  17152. org-occur-highlights
  17153. org-latex-fragment-image-overlays)
  17154. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  17155. (org-remove-occur-highlights)
  17156. (org-remove-latex-fragment-image-overlays)
  17157. (message "Temporary highlights/overlays removed from current buffer"))
  17158. ((and (local-variable-p 'org-finish-function (current-buffer))
  17159. (fboundp org-finish-function))
  17160. (funcall org-finish-function))
  17161. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  17162. ((org-in-regexp org-ts-regexp-both)
  17163. (org-timestamp-change 0 'day))
  17164. ((or (looking-at org-property-start-re)
  17165. (org-at-property-p))
  17166. (call-interactively 'org-property-action))
  17167. ((org-at-target-p) (call-interactively 'org-update-radio-target-regexp))
  17168. ((and (org-in-regexp "\\[\\([0-9]*%\\|[0-9]*/[0-9]*\\)\\]")
  17169. (or (org-at-heading-p) (org-at-item-p)))
  17170. (call-interactively 'org-update-statistics-cookies))
  17171. ((org-at-heading-p) (call-interactively 'org-set-tags))
  17172. ((org-at-table.el-p)
  17173. (message "Use C-c ' to edit table.el tables"))
  17174. ((org-at-table-p)
  17175. (org-table-maybe-eval-formula)
  17176. (if arg
  17177. (call-interactively 'org-table-recalculate)
  17178. (org-table-maybe-recalculate-line))
  17179. (call-interactively 'org-table-align)
  17180. (orgtbl-send-table 'maybe))
  17181. ((or (org-footnote-at-reference-p)
  17182. (org-footnote-at-definition-p))
  17183. (call-interactively 'org-footnote-action))
  17184. ((org-at-item-checkbox-p)
  17185. ;; Cursor at a checkbox: repair list and update checkboxes. Send
  17186. ;; list only if at top item.
  17187. (let* ((cbox (match-string 1))
  17188. (struct (org-list-struct))
  17189. (old-struct (copy-tree struct))
  17190. (parents (org-list-parents-alist struct))
  17191. (orderedp (org-entry-get nil "ORDERED"))
  17192. (firstp (= (org-list-get-top-point struct) (point-at-bol)))
  17193. block-item)
  17194. ;; Use a light version of `org-toggle-checkbox' to avoid
  17195. ;; computing list structure twice.
  17196. (let ((new-box (cond
  17197. ((equal arg '(16)) "[-]")
  17198. ((equal arg '(4)) nil)
  17199. ((equal "[X]" cbox) "[ ]")
  17200. (t "[X]"))))
  17201. (if (and firstp arg)
  17202. ;; If at first item of sub-list, remove check-box from
  17203. ;; every item at the same level.
  17204. (mapc
  17205. (lambda (pos) (org-list-set-checkbox pos struct new-box))
  17206. (org-list-get-all-items
  17207. (point-at-bol) struct (org-list-prevs-alist struct)))
  17208. (org-list-set-checkbox (point-at-bol) struct new-box)))
  17209. ;; Replicate `org-list-write-struct', while grabbing a return
  17210. ;; value from `org-list-struct-fix-box'.
  17211. (org-list-struct-fix-ind struct parents 2)
  17212. (org-list-struct-fix-item-end struct)
  17213. (let ((prevs (org-list-prevs-alist struct)))
  17214. (org-list-struct-fix-bul struct prevs)
  17215. (org-list-struct-fix-ind struct parents)
  17216. (setq block-item
  17217. (org-list-struct-fix-box struct parents prevs orderedp)))
  17218. (org-list-struct-apply-struct struct old-struct)
  17219. (org-update-checkbox-count-maybe)
  17220. (when block-item
  17221. (message
  17222. "Checkboxes were removed due to unchecked box at line %d"
  17223. (org-current-line block-item)))
  17224. (when firstp (org-list-send-list 'maybe))))
  17225. ((org-at-item-p)
  17226. ;; Cursor at an item: repair list. Do checkbox related actions
  17227. ;; only if function was called with an argument. Send list only
  17228. ;; if at top item.
  17229. (let* ((struct (org-list-struct))
  17230. (firstp (= (org-list-get-top-point struct) (point-at-bol)))
  17231. old-struct)
  17232. (when arg
  17233. (setq old-struct (copy-tree struct))
  17234. (if firstp
  17235. ;; If at first item of sub-list, add check-box to every
  17236. ;; item at the same level.
  17237. (mapc
  17238. (lambda (pos)
  17239. (unless (org-list-get-checkbox pos struct)
  17240. (org-list-set-checkbox pos struct "[ ]")))
  17241. (org-list-get-all-items
  17242. (point-at-bol) struct (org-list-prevs-alist struct)))
  17243. (org-list-set-checkbox (point-at-bol) struct "[ ]")))
  17244. (org-list-write-struct
  17245. struct (org-list-parents-alist struct) old-struct)
  17246. (when arg (org-update-checkbox-count-maybe))
  17247. (when firstp (org-list-send-list 'maybe))))
  17248. ((save-excursion (beginning-of-line 1) (looking-at org-dblock-start-re))
  17249. ;; Dynamic block
  17250. (beginning-of-line 1)
  17251. (save-excursion (org-update-dblock)))
  17252. ((save-excursion
  17253. (let ((case-fold-search t))
  17254. (beginning-of-line 1)
  17255. (looking-at "[ \t]*#\\+\\([a-z]+\\)")))
  17256. (cond
  17257. ((or (equal (match-string 1) "TBLFM")
  17258. (equal (match-string 1) "tblfm"))
  17259. ;; Recalculate the table before this line
  17260. (save-excursion
  17261. (beginning-of-line 1)
  17262. (skip-chars-backward " \r\n\t")
  17263. (if (org-at-table-p)
  17264. (org-call-with-arg 'org-table-recalculate (or arg t)))))
  17265. (t
  17266. (let ((org-inhibit-startup-visibility-stuff t)
  17267. (org-startup-align-all-tables nil))
  17268. (when (boundp 'org-table-coordinate-overlays)
  17269. (mapc 'delete-overlay org-table-coordinate-overlays)
  17270. (setq org-table-coordinate-overlays nil))
  17271. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  17272. (message "Local setup has been refreshed"))))
  17273. ((org-clock-update-time-maybe))
  17274. (t
  17275. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  17276. (error "C-c C-c can do nothing useful at this location"))))))
  17277. (defun org-mode-restart ()
  17278. "Restart Org-mode, to scan again for special lines.
  17279. Also updates the keyword regular expressions."
  17280. (interactive)
  17281. (org-mode)
  17282. (message "Org-mode restarted"))
  17283. (defun org-kill-note-or-show-branches ()
  17284. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  17285. (interactive)
  17286. (if (not org-finish-function)
  17287. (progn
  17288. (hide-subtree)
  17289. (call-interactively 'show-branches))
  17290. (let ((org-note-abort t))
  17291. (funcall org-finish-function))))
  17292. (defun org-return (&optional indent)
  17293. "Goto next table row or insert a newline.
  17294. Calls `org-table-next-row' or `newline', depending on context.
  17295. See the individual commands for more information."
  17296. (interactive)
  17297. (cond
  17298. ((or (bobp) (org-in-src-block-p))
  17299. (if indent (newline-and-indent) (newline)))
  17300. ((org-at-table-p)
  17301. (org-table-justify-field-maybe)
  17302. (call-interactively 'org-table-next-row))
  17303. ;; when `newline-and-indent' is called within a list, make sure
  17304. ;; text moved stays inside the item.
  17305. ((and (org-in-item-p) indent)
  17306. (if (and (org-at-item-p) (>= (point) (match-end 0)))
  17307. (progn
  17308. (save-match-data (newline))
  17309. (org-indent-line-to (length (match-string 0))))
  17310. (let ((ind (org-get-indentation)))
  17311. (newline)
  17312. (if (org-looking-back org-list-end-re)
  17313. (org-indent-line-function)
  17314. (org-indent-line-to ind)))))
  17315. ((and org-return-follows-link
  17316. (let ((tprop (get-text-property (point) 'face)))
  17317. (or (eq tprop 'org-link)
  17318. (and (listp tprop) (memq 'org-link tprop)))))
  17319. (call-interactively 'org-open-at-point))
  17320. ((and (org-at-heading-p)
  17321. (looking-at
  17322. (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
  17323. (org-show-entry)
  17324. (end-of-line 1)
  17325. (newline))
  17326. (t (if indent (newline-and-indent) (newline)))))
  17327. (defun org-return-indent ()
  17328. "Goto next table row or insert a newline and indent.
  17329. Calls `org-table-next-row' or `newline-and-indent', depending on
  17330. context. See the individual commands for more information."
  17331. (interactive)
  17332. (org-return t))
  17333. (defun org-ctrl-c-star ()
  17334. "Compute table, or change heading status of lines.
  17335. Calls `org-table-recalculate' or `org-toggle-heading',
  17336. depending on context."
  17337. (interactive)
  17338. (cond
  17339. ((org-at-table-p)
  17340. (call-interactively 'org-table-recalculate))
  17341. (t
  17342. ;; Convert all lines in region to list items
  17343. (call-interactively 'org-toggle-heading))))
  17344. (defun org-ctrl-c-minus ()
  17345. "Insert separator line in table or modify bullet status of line.
  17346. Also turns a plain line or a region of lines into list items.
  17347. Calls `org-table-insert-hline', `org-toggle-item', or
  17348. `org-cycle-list-bullet', depending on context."
  17349. (interactive)
  17350. (cond
  17351. ((org-at-table-p)
  17352. (call-interactively 'org-table-insert-hline))
  17353. ((org-region-active-p)
  17354. (call-interactively 'org-toggle-item))
  17355. ((org-in-item-p)
  17356. (call-interactively 'org-cycle-list-bullet))
  17357. (t
  17358. (call-interactively 'org-toggle-item))))
  17359. (defun org-toggle-item (arg)
  17360. "Convert headings or normal lines to items, items to normal lines.
  17361. If there is no active region, only the current line is considered.
  17362. If the first non blank line in the region is an headline, convert
  17363. all headlines to items, shifting text accordingly.
  17364. If it is an item, convert all items to normal lines.
  17365. If it is normal text, change region into an item. With a prefix
  17366. argument ARG, change each line in region into an item."
  17367. (interactive "P")
  17368. (let ((shift-text
  17369. (function
  17370. ;; Shift text in current section to IND, from point to END.
  17371. ;; The function leaves point to END line.
  17372. (lambda (ind end)
  17373. (let ((min-i 1000) (end (copy-marker end)))
  17374. ;; First determine the minimum indentation (MIN-I) of
  17375. ;; the text.
  17376. (save-excursion
  17377. (catch 'exit
  17378. (while (< (point) end)
  17379. (let ((i (org-get-indentation)))
  17380. (cond
  17381. ;; Skip blank lines and inline tasks.
  17382. ((looking-at "^[ \t]*$"))
  17383. ((looking-at org-outline-regexp-bol))
  17384. ;; We can't find less than 0 indentation.
  17385. ((zerop i) (throw 'exit (setq min-i 0)))
  17386. ((< i min-i) (setq min-i i))))
  17387. (forward-line))))
  17388. ;; Then indent each line so that a line indented to
  17389. ;; MIN-I becomes indented to IND. Ignore blank lines
  17390. ;; and inline tasks in the process.
  17391. (let ((delta (- ind min-i)))
  17392. (while (< (point) end)
  17393. (unless (or (looking-at "^[ \t]*$")
  17394. (looking-at org-outline-regexp-bol))
  17395. (org-indent-line-to (+ (org-get-indentation) delta)))
  17396. (forward-line)))))))
  17397. (skip-blanks
  17398. (function
  17399. ;; Return beginning of first non-blank line, starting from
  17400. ;; line at POS.
  17401. (lambda (pos)
  17402. (save-excursion
  17403. (goto-char pos)
  17404. (skip-chars-forward " \r\t\n")
  17405. (point-at-bol)))))
  17406. beg end)
  17407. ;; Determine boundaries of changes.
  17408. (if (org-region-active-p)
  17409. (setq beg (funcall skip-blanks (region-beginning))
  17410. end (copy-marker (region-end)))
  17411. (setq beg (funcall skip-blanks (point-at-bol))
  17412. end (copy-marker (point-at-eol))))
  17413. ;; Depending on the starting line, choose an action on the text
  17414. ;; between BEG and END.
  17415. (org-with-limited-levels
  17416. (save-excursion
  17417. (goto-char beg)
  17418. (cond
  17419. ;; Case 1. Start at an item: de-itemize. Note that it only
  17420. ;; happens when a region is active: `org-ctrl-c-minus'
  17421. ;; would call `org-cycle-list-bullet' otherwise.
  17422. ((org-at-item-p)
  17423. (while (< (point) end)
  17424. (when (org-at-item-p)
  17425. (skip-chars-forward " \t")
  17426. (delete-region (point) (match-end 0)))
  17427. (forward-line)))
  17428. ;; Case 2. Start at an heading: convert to items.
  17429. ((org-at-heading-p)
  17430. (let* ((bul (org-list-bullet-string "-"))
  17431. (bul-len (length bul))
  17432. ;; Indentation of the first heading. It should be
  17433. ;; relative to the indentation of its parent, if any.
  17434. (start-ind (save-excursion
  17435. (cond
  17436. ((not org-adapt-indentation) 0)
  17437. ((not (outline-previous-heading)) 0)
  17438. (t (length (match-string 0))))))
  17439. ;; Level of first heading. Further headings will be
  17440. ;; compared to it to determine hierarchy in the list.
  17441. (ref-level (org-reduced-level (org-outline-level))))
  17442. (while (< (point) end)
  17443. (let* ((level (org-reduced-level (org-outline-level)))
  17444. (delta (max 0 (- level ref-level))))
  17445. ;; If current headline is less indented than the first
  17446. ;; one, set it as reference, in order to preserve
  17447. ;; subtrees.
  17448. (when (< level ref-level) (setq ref-level level))
  17449. (replace-match bul t t)
  17450. (org-indent-line-to (+ start-ind (* delta bul-len)))
  17451. ;; Ensure all text down to END (or SECTION-END) belongs
  17452. ;; to the newly created item.
  17453. (let ((section-end (save-excursion
  17454. (or (outline-next-heading) (point)))))
  17455. (forward-line)
  17456. (funcall shift-text
  17457. (+ start-ind (* (1+ delta) bul-len))
  17458. (min end section-end)))))))
  17459. ;; Case 3. Normal line with ARG: turn each non-item line into
  17460. ;; an item.
  17461. (arg
  17462. (while (< (point) end)
  17463. (unless (or (org-at-heading-p) (org-at-item-p))
  17464. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  17465. (replace-match
  17466. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  17467. (forward-line)))
  17468. ;; Case 4. Normal line without ARG: make the first line of
  17469. ;; region an item, and shift indentation of others
  17470. ;; lines to set them as item's body.
  17471. (t (let* ((bul (org-list-bullet-string "-"))
  17472. (bul-len (length bul))
  17473. (ref-ind (org-get-indentation)))
  17474. (skip-chars-forward " \t")
  17475. (insert bul)
  17476. (forward-line)
  17477. (while (< (point) end)
  17478. ;; Ensure that lines less indented than first one
  17479. ;; still get included in item body.
  17480. (funcall shift-text
  17481. (+ ref-ind bul-len)
  17482. (min end (save-excursion (or (outline-next-heading)
  17483. (point)))))
  17484. (forward-line)))))))))
  17485. (defun org-toggle-heading (&optional nstars)
  17486. "Convert headings to normal text, or items or text to headings.
  17487. If there is no active region, only the current line is considered.
  17488. If the first non blank line is an headline, remove the stars from
  17489. all headlines in the region.
  17490. If it is a plain list item, turn all plain list items into headings.
  17491. If it is a normal line, turn each and every normal line (i.e. not
  17492. an heading or an item) in the region into a heading.
  17493. When converting a line into a heading, the number of stars is chosen
  17494. such that the lines become children of the current entry. However,
  17495. when a prefix argument is given, its value determines the number of
  17496. stars to add."
  17497. (interactive "P")
  17498. (let ((skip-blanks
  17499. (function
  17500. ;; Return beginning of first non-blank line, starting from
  17501. ;; line at POS.
  17502. (lambda (pos)
  17503. (save-excursion
  17504. (goto-char pos)
  17505. (skip-chars-forward " \r\t\n")
  17506. (point-at-bol)))))
  17507. beg end)
  17508. ;; Determine boundaries of changes. If region ends at a bol, do
  17509. ;; not consider the last line to be in the region.
  17510. (if (org-region-active-p)
  17511. (setq beg (funcall skip-blanks (region-beginning))
  17512. end (copy-marker (save-excursion
  17513. (goto-char (region-end))
  17514. (if (bolp) (point) (point-at-eol)))))
  17515. (setq beg (funcall skip-blanks (point-at-bol))
  17516. end (copy-marker (point-at-eol))))
  17517. ;; Ensure inline tasks don't count as headings.
  17518. (org-with-limited-levels
  17519. (save-excursion
  17520. (goto-char beg)
  17521. (cond
  17522. ;; Case 1. Started at an heading: de-star headings.
  17523. ((org-at-heading-p)
  17524. (while (< (point) end)
  17525. (when (org-at-heading-p t)
  17526. (looking-at org-outline-regexp) (replace-match ""))
  17527. (forward-line)))
  17528. ;; Case 2. Started at an item: change items into headlines.
  17529. ;; One star will be added by `org-list-to-subtree'.
  17530. ((org-at-item-p)
  17531. (let* ((stars (make-string
  17532. (if nstars
  17533. ;; subtract the star that will be added again by
  17534. ;; `org-list-to-subtree'
  17535. (1- (prefix-numeric-value current-prefix-arg))
  17536. (or (org-current-level) 0))
  17537. ?*))
  17538. (add-stars
  17539. (cond (nstars "") ; stars from prefix only
  17540. ((equal stars "") "") ; before first heading
  17541. (org-odd-levels-only "*") ; inside heading, odd
  17542. (t "")))) ; inside heading, oddeven
  17543. (while (< (point) end)
  17544. (when (org-at-item-p)
  17545. ;; Pay attention to cases when region ends before list.
  17546. (let* ((struct (org-list-struct))
  17547. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  17548. (save-restriction
  17549. (narrow-to-region (point) list-end)
  17550. (insert
  17551. (org-list-to-subtree
  17552. (org-list-parse-list t)
  17553. '(:istart (concat stars add-stars (funcall get-stars depth))
  17554. :icount (concat stars add-stars (funcall get-stars depth))))))))
  17555. (forward-line))))
  17556. ;; Case 3. Started at normal text: make every line an heading,
  17557. ;; skipping headlines and items.
  17558. (t (let* ((stars (make-string
  17559. (if nstars
  17560. (prefix-numeric-value current-prefix-arg)
  17561. (or (org-current-level) 0))
  17562. ?*))
  17563. (add-stars
  17564. (cond (nstars "") ; stars from prefix only
  17565. ((equal stars "") "*") ; before first heading
  17566. (org-odd-levels-only "**") ; inside heading, odd
  17567. (t "*"))) ; inside heading, oddeven
  17568. (rpl (concat stars add-stars " ")))
  17569. (while (< (point) end)
  17570. (when (and (not (org-at-heading-p)) (not (org-at-item-p))
  17571. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  17572. (replace-match (concat rpl (match-string 2))))
  17573. (forward-line)))))))))
  17574. (defun org-meta-return (&optional arg)
  17575. "Insert a new heading or wrap a region in a table.
  17576. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  17577. See the individual commands for more information."
  17578. (interactive "P")
  17579. (cond
  17580. ((run-hook-with-args-until-success 'org-metareturn-hook))
  17581. ((or (org-at-drawer-p) (org-at-property-p))
  17582. (newline-and-indent))
  17583. ((org-at-table-p)
  17584. (call-interactively 'org-table-wrap-region))
  17585. (t (call-interactively 'org-insert-heading))))
  17586. ;;; Menu entries
  17587. ;; Define the Org-mode menus
  17588. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  17589. '("Tbl"
  17590. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  17591. ["Next Field" org-cycle (org-at-table-p)]
  17592. ["Previous Field" org-shifttab (org-at-table-p)]
  17593. ["Next Row" org-return (org-at-table-p)]
  17594. "--"
  17595. ["Blank Field" org-table-blank-field (org-at-table-p)]
  17596. ["Edit Field" org-table-edit-field (org-at-table-p)]
  17597. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  17598. "--"
  17599. ("Column"
  17600. ["Move Column Left" org-metaleft (org-at-table-p)]
  17601. ["Move Column Right" org-metaright (org-at-table-p)]
  17602. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  17603. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  17604. ("Row"
  17605. ["Move Row Up" org-metaup (org-at-table-p)]
  17606. ["Move Row Down" org-metadown (org-at-table-p)]
  17607. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  17608. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  17609. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  17610. "--"
  17611. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  17612. ("Rectangle"
  17613. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  17614. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  17615. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  17616. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  17617. "--"
  17618. ("Calculate"
  17619. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  17620. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  17621. ["Edit Formulas" org-edit-special (org-at-table-p)]
  17622. "--"
  17623. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  17624. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  17625. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  17626. "--"
  17627. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  17628. "--"
  17629. ["Sum Column/Rectangle" org-table-sum
  17630. (or (org-at-table-p) (org-region-active-p))]
  17631. ["Which Column?" org-table-current-column (org-at-table-p)])
  17632. ["Debug Formulas"
  17633. org-table-toggle-formula-debugger
  17634. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  17635. ["Show Col/Row Numbers"
  17636. org-table-toggle-coordinate-overlays
  17637. :style toggle
  17638. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  17639. "--"
  17640. ["Create" org-table-create (and (not (org-at-table-p))
  17641. org-enable-table-editor)]
  17642. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  17643. ["Import from File" org-table-import (not (org-at-table-p))]
  17644. ["Export to File" org-table-export (org-at-table-p)]
  17645. "--"
  17646. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  17647. (easy-menu-define org-org-menu org-mode-map "Org menu"
  17648. '("Org"
  17649. ("Show/Hide"
  17650. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  17651. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  17652. ["Sparse Tree..." org-sparse-tree t]
  17653. ["Reveal Context" org-reveal t]
  17654. ["Show All" show-all t]
  17655. "--"
  17656. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  17657. "--"
  17658. ["New Heading" org-insert-heading t]
  17659. ("Navigate Headings"
  17660. ["Up" outline-up-heading t]
  17661. ["Next" outline-next-visible-heading t]
  17662. ["Previous" outline-previous-visible-heading t]
  17663. ["Next Same Level" outline-forward-same-level t]
  17664. ["Previous Same Level" outline-backward-same-level t]
  17665. "--"
  17666. ["Jump" org-goto t])
  17667. ("Edit Structure"
  17668. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  17669. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  17670. "--"
  17671. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  17672. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  17673. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  17674. "--"
  17675. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  17676. "--"
  17677. ["Copy visible text" org-copy-visible t]
  17678. "--"
  17679. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  17680. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  17681. ["Demote Heading" org-metaright (not (org-at-table-p))]
  17682. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  17683. "--"
  17684. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  17685. "--"
  17686. ["Convert to odd levels" org-convert-to-odd-levels t]
  17687. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  17688. ("Editing"
  17689. ["Emphasis..." org-emphasize t]
  17690. ["Edit Source Example" org-edit-special t]
  17691. "--"
  17692. ["Footnote new/jump" org-footnote-action t]
  17693. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  17694. ("Archive"
  17695. ["Archive (default method)" org-archive-subtree-default t]
  17696. "--"
  17697. ["Move Subtree to Archive file" org-advertized-archive-subtree t]
  17698. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  17699. ["Move subtree to Archive sibling" org-archive-to-archive-sibling t]
  17700. )
  17701. "--"
  17702. ("Hyperlinks"
  17703. ["Store Link (Global)" org-store-link t]
  17704. ["Find existing link to here" org-occur-link-in-agenda-files t]
  17705. ["Insert Link" org-insert-link t]
  17706. ["Follow Link" org-open-at-point t]
  17707. "--"
  17708. ["Next link" org-next-link t]
  17709. ["Previous link" org-previous-link t]
  17710. "--"
  17711. ["Descriptive Links"
  17712. org-toggle-link-display
  17713. :style radio
  17714. :selected org-descriptive-links
  17715. ]
  17716. ["Literal Links"
  17717. org-toggle-link-display
  17718. :style radio
  17719. :selected (not org-descriptive-links)])
  17720. "--"
  17721. ("TODO Lists"
  17722. ["TODO/DONE/-" org-todo t]
  17723. ("Select keyword"
  17724. ["Next keyword" org-shiftright (org-at-heading-p)]
  17725. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  17726. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  17727. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  17728. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  17729. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  17730. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  17731. "--"
  17732. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  17733. :selected org-enforce-todo-dependencies :style toggle :active t]
  17734. "Settings for tree at point"
  17735. ["Do Children sequentially" org-toggle-ordered-property :style radio
  17736. :selected (org-entry-get nil "ORDERED")
  17737. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  17738. ["Do Children parallel" org-toggle-ordered-property :style radio
  17739. :selected (not (org-entry-get nil "ORDERED"))
  17740. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  17741. "--"
  17742. ["Set Priority" org-priority t]
  17743. ["Priority Up" org-shiftup t]
  17744. ["Priority Down" org-shiftdown t]
  17745. "--"
  17746. ["Get news from all feeds" org-feed-update-all t]
  17747. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  17748. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  17749. ("TAGS and Properties"
  17750. ["Set Tags" org-set-tags-command t]
  17751. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  17752. "--"
  17753. ["Set property" org-set-property t]
  17754. ["Column view of properties" org-columns t]
  17755. ["Insert Column View DBlock" org-insert-columns-dblock t])
  17756. ("Dates and Scheduling"
  17757. ["Timestamp" org-time-stamp t]
  17758. ["Timestamp (inactive)" org-time-stamp-inactive t]
  17759. ("Change Date"
  17760. ["1 Day Later" org-shiftright t]
  17761. ["1 Day Earlier" org-shiftleft t]
  17762. ["1 ... Later" org-shiftup t]
  17763. ["1 ... Earlier" org-shiftdown t])
  17764. ["Compute Time Range" org-evaluate-time-range t]
  17765. ["Schedule Item" org-schedule t]
  17766. ["Deadline" org-deadline t]
  17767. "--"
  17768. ["Custom time format" org-toggle-time-stamp-overlays
  17769. :style radio :selected org-display-custom-times]
  17770. "--"
  17771. ["Goto Calendar" org-goto-calendar t]
  17772. ["Date from Calendar" org-date-from-calendar t]
  17773. "--"
  17774. ["Start/Restart Timer" org-timer-start t]
  17775. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  17776. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  17777. ["Insert Timer String" org-timer t]
  17778. ["Insert Timer Item" org-timer-item t])
  17779. ("Logging work"
  17780. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  17781. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  17782. ["Clock out" org-clock-out t]
  17783. ["Clock cancel" org-clock-cancel t]
  17784. "--"
  17785. ["Mark as default task" org-clock-mark-default-task t]
  17786. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  17787. ["Goto running clock" org-clock-goto t]
  17788. "--"
  17789. ["Display times" org-clock-display t]
  17790. ["Create clock table" org-clock-report t]
  17791. "--"
  17792. ["Record DONE time"
  17793. (progn (setq org-log-done (not org-log-done))
  17794. (message "Switching to %s will %s record a timestamp"
  17795. (car org-done-keywords)
  17796. (if org-log-done "automatically" "not")))
  17797. :style toggle :selected org-log-done])
  17798. "--"
  17799. ["Agenda Command..." org-agenda t]
  17800. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  17801. ("File List for Agenda")
  17802. ("Special views current file"
  17803. ["TODO Tree" org-show-todo-tree t]
  17804. ["Check Deadlines" org-check-deadlines t]
  17805. ["Timeline" org-timeline t]
  17806. ["Tags/Property tree" org-match-sparse-tree t])
  17807. "--"
  17808. ["Export/Publish..." org-export t]
  17809. ("LaTeX"
  17810. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  17811. :selected org-cdlatex-mode]
  17812. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  17813. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  17814. ["Modify math symbol" org-cdlatex-math-modify
  17815. (org-inside-LaTeX-fragment-p)]
  17816. ["Insert citation" org-reftex-citation t]
  17817. "--"
  17818. ["Template for BEAMER" (progn (require 'org-beamer)
  17819. (org-insert-beamer-options-template)) t])
  17820. "--"
  17821. ("MobileOrg"
  17822. ["Push Files and Views" org-mobile-push t]
  17823. ["Get Captured and Flagged" org-mobile-pull t]
  17824. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  17825. "--"
  17826. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  17827. "--"
  17828. ("Documentation"
  17829. ["Show Version" org-version t]
  17830. ["Info Documentation" org-info t])
  17831. ("Customize"
  17832. ["Browse Org Group" org-customize t]
  17833. "--"
  17834. ["Expand This Menu" org-create-customize-menu
  17835. (fboundp 'customize-menu-create)])
  17836. ["Send bug report" org-submit-bug-report t]
  17837. "--"
  17838. ("Refresh/Reload"
  17839. ["Refresh setup current buffer" org-mode-restart t]
  17840. ["Reload Org (after update)" org-reload t]
  17841. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  17842. ))
  17843. (defun org-info (&optional node)
  17844. "Read documentation for Org-mode in the info system.
  17845. With optional NODE, go directly to that node."
  17846. (interactive)
  17847. (info (format "(org)%s" (or node ""))))
  17848. ;;;###autoload
  17849. (defun org-submit-bug-report ()
  17850. "Submit a bug report on Org-mode via mail.
  17851. Don't hesitate to report any problems or inaccurate documentation.
  17852. If you don't have setup sending mail from (X)Emacs, please copy the
  17853. output buffer into your mail program, as it gives us important
  17854. information about your Org-mode version and configuration."
  17855. (interactive)
  17856. (require 'reporter)
  17857. (org-load-modules-maybe)
  17858. (org-require-autoloaded-modules)
  17859. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  17860. (reporter-submit-bug-report
  17861. "emacs-orgmode@gnu.org"
  17862. (org-version)
  17863. (let (list)
  17864. (save-window-excursion
  17865. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  17866. (delete-other-windows)
  17867. (erase-buffer)
  17868. (insert "You are about to submit a bug report to the Org-mode mailing list.
  17869. We would like to add your full Org-mode and Outline configuration to the
  17870. bug report. This greatly simplifies the work of the maintainer and
  17871. other experts on the mailing list.
  17872. HOWEVER, some variables you have customized may contain private
  17873. information. The names of customers, colleagues, or friends, might
  17874. appear in the form of file names, tags, todo states, or search strings.
  17875. If you answer yes to the prompt, you might want to check and remove
  17876. such private information before sending the email.")
  17877. (add-text-properties (point-min) (point-max) '(face org-warning))
  17878. (when (yes-or-no-p "Include your Org-mode configuration ")
  17879. (mapatoms
  17880. (lambda (v)
  17881. (and (boundp v)
  17882. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  17883. (or (and (symbol-value v)
  17884. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  17885. (and
  17886. (get v 'custom-type) (get v 'standard-value)
  17887. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  17888. (push v list)))))
  17889. (kill-buffer (get-buffer "*Warn about privacy*"))
  17890. list))
  17891. nil nil
  17892. "Remember to cover the basics, that is, what you expected to happen and
  17893. what in fact did happen. You don't know how to make a good report? See
  17894. http://orgmode.org/manual/Feedback.html#Feedback
  17895. Your bug report will be posted to the Org-mode mailing list.
  17896. ------------------------------------------------------------------------")
  17897. (save-excursion
  17898. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  17899. (replace-match "\\1Bug: \\3 [\\2]")))))
  17900. (defun org-install-agenda-files-menu ()
  17901. (let ((bl (buffer-list)))
  17902. (save-excursion
  17903. (while bl
  17904. (set-buffer (pop bl))
  17905. (if (derived-mode-p 'org-mode) (setq bl nil)))
  17906. (when (derived-mode-p 'org-mode)
  17907. (easy-menu-change
  17908. '("Org") "File List for Agenda"
  17909. (append
  17910. (list
  17911. ["Edit File List" (org-edit-agenda-file-list) t]
  17912. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  17913. ["Remove Current File from List" org-remove-file t]
  17914. ["Cycle through agenda files" org-cycle-agenda-files t]
  17915. ["Occur in all agenda files" org-occur-in-agenda-files t]
  17916. "--")
  17917. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  17918. ;;;; Documentation
  17919. ;;;###autoload
  17920. (defun org-require-autoloaded-modules ()
  17921. (interactive)
  17922. (mapc 'require
  17923. '(org-agenda org-archive org-ascii org-attach org-clock org-colview
  17924. org-docbook org-exp org-html org-icalendar
  17925. org-id org-latex
  17926. org-publish org-remember org-table
  17927. org-timer org-xoxo)))
  17928. ;;;###autoload
  17929. (defun org-reload (&optional uncompiled)
  17930. "Reload all org lisp files.
  17931. With prefix arg UNCOMPILED, load the uncompiled versions."
  17932. (interactive "P")
  17933. (require 'find-func)
  17934. (let* ((file-re "^\\(org\\|orgtbl\\)\\(\\.el\\|-.*\\.el\\)")
  17935. (dir-org (file-name-directory (org-find-library-name "org")))
  17936. (dir-org-contrib (ignore-errors
  17937. (file-name-directory
  17938. (org-find-library-name "org-contribdir"))))
  17939. (babel-files
  17940. (mapcar (lambda (el) (concat "ob" (when el (format "-%s" el)) ".el"))
  17941. (append (list nil "comint" "eval" "exp" "keys"
  17942. "lob" "ref" "table" "tangle")
  17943. (delq nil
  17944. (mapcar
  17945. (lambda (lang)
  17946. (when (cdr lang) (symbol-name (car lang))))
  17947. org-babel-load-languages)))))
  17948. (files
  17949. (append (directory-files dir-org t file-re)
  17950. babel-files
  17951. (and dir-org-contrib
  17952. (directory-files dir-org-contrib t file-re))))
  17953. (remove-re (concat (if (featurep 'xemacs)
  17954. "org-colview" "org-colview-xemacs")
  17955. "\\'")))
  17956. (setq files (mapcar 'file-name-sans-extension files))
  17957. (setq files (mapcar
  17958. (lambda (x) (if (string-match remove-re x) nil x))
  17959. files))
  17960. (setq files (delq nil files))
  17961. (mapc
  17962. (lambda (f)
  17963. (when (featurep (intern (file-name-nondirectory f)))
  17964. (if (and (not uncompiled)
  17965. (file-exists-p (concat f ".elc")))
  17966. (load (concat f ".elc") nil nil t)
  17967. (load (concat f ".el") nil nil t))))
  17968. files))
  17969. (org-version))
  17970. ;;;###autoload
  17971. (defun org-customize ()
  17972. "Call the customize function with org as argument."
  17973. (interactive)
  17974. (org-load-modules-maybe)
  17975. (org-require-autoloaded-modules)
  17976. (customize-browse 'org))
  17977. (defun org-create-customize-menu ()
  17978. "Create a full customization menu for Org-mode, insert it into the menu."
  17979. (interactive)
  17980. (org-load-modules-maybe)
  17981. (org-require-autoloaded-modules)
  17982. (if (fboundp 'customize-menu-create)
  17983. (progn
  17984. (easy-menu-change
  17985. '("Org") "Customize"
  17986. `(["Browse Org group" org-customize t]
  17987. "--"
  17988. ,(customize-menu-create 'org)
  17989. ["Set" Custom-set t]
  17990. ["Save" Custom-save t]
  17991. ["Reset to Current" Custom-reset-current t]
  17992. ["Reset to Saved" Custom-reset-saved t]
  17993. ["Reset to Standard Settings" Custom-reset-standard t]))
  17994. (message "\"Org\"-menu now contains full customization menu"))
  17995. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  17996. ;;;; Miscellaneous stuff
  17997. ;;; Generally useful functions
  17998. (defun org-get-at-bol (property)
  17999. "Get text property PROPERTY at beginning of line."
  18000. (get-text-property (point-at-bol) property))
  18001. (defun org-find-text-property-in-string (prop s)
  18002. "Return the first non-nil value of property PROP in string S."
  18003. (or (get-text-property 0 prop s)
  18004. (get-text-property (or (next-single-property-change 0 prop s) 0)
  18005. prop s)))
  18006. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  18007. "Display the given MESSAGE as a warning."
  18008. (if (fboundp 'display-warning)
  18009. (display-warning 'org message
  18010. (if (featurep 'xemacs) 'warning :warning))
  18011. (let ((buf (get-buffer-create "*Org warnings*")))
  18012. (with-current-buffer buf
  18013. (goto-char (point-max))
  18014. (insert "Warning (Org): " message)
  18015. (unless (bolp)
  18016. (newline)))
  18017. (display-buffer buf)
  18018. (sit-for 0))))
  18019. (defun org-eval (form)
  18020. "Eval FORM and return result."
  18021. (condition-case error
  18022. (eval form)
  18023. (error (format "%%![Error: %s]" error))))
  18024. (defun org-in-clocktable-p ()
  18025. "Check if the cursor is in a clocktable."
  18026. (let ((pos (point)) start)
  18027. (save-excursion
  18028. (end-of-line 1)
  18029. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  18030. (setq start (match-beginning 0))
  18031. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  18032. (>= (match-end 0) pos)
  18033. start))))
  18034. (defun org-in-commented-line ()
  18035. "Is point in a line starting with `#'?"
  18036. (equal (char-after (point-at-bol)) ?#))
  18037. (defun org-in-indented-comment-line ()
  18038. "Is point in a line starting with `#' after some white space?"
  18039. (save-excursion
  18040. (save-match-data
  18041. (goto-char (point-at-bol))
  18042. (looking-at "[ \t]*#"))))
  18043. (defun org-in-verbatim-emphasis ()
  18044. (save-match-data
  18045. (and (org-in-regexp org-emph-re 2) (member (match-string 3) '("=" "~")))))
  18046. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  18047. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  18048. (if (and marker (marker-buffer marker)
  18049. (buffer-live-p (marker-buffer marker)))
  18050. (progn
  18051. (org-pop-to-buffer-same-window (marker-buffer marker))
  18052. (if (or (> marker (point-max)) (< marker (point-min)))
  18053. (widen))
  18054. (goto-char marker)
  18055. (org-show-context 'org-goto))
  18056. (if bookmark
  18057. (bookmark-jump bookmark)
  18058. (error "Cannot find location"))))
  18059. (defun org-quote-csv-field (s)
  18060. "Quote field for inclusion in CSV material."
  18061. (if (string-match "[\",]" s)
  18062. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  18063. s))
  18064. (defun org-force-self-insert (N)
  18065. "Needed to enforce self-insert under remapping."
  18066. (interactive "p")
  18067. (self-insert-command N))
  18068. (defun org-string-width (s)
  18069. "Compute width of string, ignoring invisible characters.
  18070. This ignores character with invisibility property `org-link', and also
  18071. characters with property `org-cwidth', because these will become invisible
  18072. upon the next fontification round."
  18073. (let (b l)
  18074. (when (or (eq t buffer-invisibility-spec)
  18075. (assq 'org-link buffer-invisibility-spec))
  18076. (while (setq b (text-property-any 0 (length s)
  18077. 'invisible 'org-link s))
  18078. (setq s (concat (substring s 0 b)
  18079. (substring s (or (next-single-property-change
  18080. b 'invisible s) (length s)))))))
  18081. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  18082. (setq s (concat (substring s 0 b)
  18083. (substring s (or (next-single-property-change
  18084. b 'org-cwidth s) (length s))))))
  18085. (setq l (string-width s) b -1)
  18086. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  18087. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  18088. l))
  18089. (defun org-shorten-string (s maxlength)
  18090. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  18091. If the string is shorter or has length MAXLENGTH, just return the
  18092. original string. If it is longer, the functions finds a space in the
  18093. string, breaks this string off at that locations and adds three dots
  18094. as ellipsis. Including the ellipsis, the string will not be longer
  18095. than MAXLENGTH. If finding a good breaking point in the string does
  18096. not work, the string is just chopped off in the middle of a word
  18097. if necessary."
  18098. (if (<= (length s) maxlength)
  18099. s
  18100. (let* ((n (max (- maxlength 4) 1))
  18101. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  18102. (if (string-match re s)
  18103. (concat (match-string 1 s) "...")
  18104. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  18105. (defun org-get-indentation (&optional line)
  18106. "Get the indentation of the current line, interpreting tabs.
  18107. When LINE is given, assume it represents a line and compute its indentation."
  18108. (if line
  18109. (if (string-match "^ *" (org-remove-tabs line))
  18110. (match-end 0))
  18111. (save-excursion
  18112. (beginning-of-line 1)
  18113. (skip-chars-forward " \t")
  18114. (current-column))))
  18115. (defun org-get-string-indentation (s)
  18116. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  18117. (let ((n -1) (i 0) (w tab-width) c)
  18118. (catch 'exit
  18119. (while (< (setq n (1+ n)) (length s))
  18120. (setq c (aref s n))
  18121. (cond ((= c ?\ ) (setq i (1+ i)))
  18122. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  18123. (t (throw 'exit t)))))
  18124. i))
  18125. (defun org-remove-tabs (s &optional width)
  18126. "Replace tabulators in S with spaces.
  18127. Assumes that s is a single line, starting in column 0."
  18128. (setq width (or width tab-width))
  18129. (while (string-match "\t" s)
  18130. (setq s (replace-match
  18131. (make-string
  18132. (- (* width (/ (+ (match-beginning 0) width) width))
  18133. (match-beginning 0)) ?\ )
  18134. t t s)))
  18135. s)
  18136. (defun org-fix-indentation (line ind)
  18137. "Fix indentation in LINE.
  18138. IND is a cons cell with target and minimum indentation.
  18139. If the current indentation in LINE is smaller than the minimum,
  18140. leave it alone. If it is larger than ind, set it to the target."
  18141. (let* ((l (org-remove-tabs line))
  18142. (i (org-get-indentation l))
  18143. (i1 (car ind)) (i2 (cdr ind)))
  18144. (if (>= i i2) (setq l (substring line i2)))
  18145. (if (> i1 0)
  18146. (concat (make-string i1 ?\ ) l)
  18147. l)))
  18148. (defun org-remove-indentation (code &optional n)
  18149. "Remove the maximum common indentation from the lines in CODE.
  18150. N may optionally be the number of spaces to remove."
  18151. (with-temp-buffer
  18152. (insert code)
  18153. (org-do-remove-indentation n)
  18154. (buffer-string)))
  18155. (defun org-do-remove-indentation (&optional n)
  18156. "Remove the maximum common indentation from the buffer."
  18157. (untabify (point-min) (point-max))
  18158. (let ((min 10000) re)
  18159. (if n
  18160. (setq min n)
  18161. (goto-char (point-min))
  18162. (while (re-search-forward "^ *[^ \n]" nil t)
  18163. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  18164. (unless (or (= min 0) (= min 10000))
  18165. (setq re (format "^ \\{%d\\}" min))
  18166. (goto-char (point-min))
  18167. (while (re-search-forward re nil t)
  18168. (replace-match "")
  18169. (end-of-line 1))
  18170. min)))
  18171. (defun org-fill-template (template alist)
  18172. "Find each %key of ALIST in TEMPLATE and replace it."
  18173. (let ((case-fold-search nil)
  18174. entry key value)
  18175. (setq alist (sort (copy-sequence alist)
  18176. (lambda (a b) (< (length (car a)) (length (car b))))))
  18177. (while (setq entry (pop alist))
  18178. (setq template
  18179. (replace-regexp-in-string
  18180. (concat "%" (regexp-quote (car entry)))
  18181. (cdr entry) template t t)))
  18182. template))
  18183. (defun org-base-buffer (buffer)
  18184. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  18185. (if (not buffer)
  18186. buffer
  18187. (or (buffer-base-buffer buffer)
  18188. buffer)))
  18189. (defun org-trim (s)
  18190. "Remove whitespace at beginning and end of string."
  18191. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  18192. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  18193. s)
  18194. (defun org-wrap (string &optional width lines)
  18195. "Wrap string to either a number of lines, or a width in characters.
  18196. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  18197. that costs. If there is a word longer than WIDTH, the text is actually
  18198. wrapped to the length of that word.
  18199. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  18200. many lines, whatever width that takes.
  18201. The return value is a list of lines, without newlines at the end."
  18202. (let* ((words (org-split-string string "[ \t\n]+"))
  18203. (maxword (apply 'max (mapcar 'org-string-width words)))
  18204. w ll)
  18205. (cond (width
  18206. (org-do-wrap words (max maxword width)))
  18207. (lines
  18208. (setq w maxword)
  18209. (setq ll (org-do-wrap words maxword))
  18210. (if (<= (length ll) lines)
  18211. ll
  18212. (setq ll words)
  18213. (while (> (length ll) lines)
  18214. (setq w (1+ w))
  18215. (setq ll (org-do-wrap words w)))
  18216. ll))
  18217. (t (error "Cannot wrap this")))))
  18218. (defun org-do-wrap (words width)
  18219. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  18220. (let (lines line)
  18221. (while words
  18222. (setq line (pop words))
  18223. (while (and words (< (+ (length line) (length (car words))) width))
  18224. (setq line (concat line " " (pop words))))
  18225. (setq lines (push line lines)))
  18226. (nreverse lines)))
  18227. (defun org-split-string (string &optional separators)
  18228. "Splits STRING into substrings at SEPARATORS.
  18229. No empty strings are returned if there are matches at the beginning
  18230. and end of string."
  18231. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  18232. (start 0)
  18233. notfirst
  18234. (list nil))
  18235. (while (and (string-match rexp string
  18236. (if (and notfirst
  18237. (= start (match-beginning 0))
  18238. (< start (length string)))
  18239. (1+ start) start))
  18240. (< (match-beginning 0) (length string)))
  18241. (setq notfirst t)
  18242. (or (eq (match-beginning 0) 0)
  18243. (and (eq (match-beginning 0) (match-end 0))
  18244. (eq (match-beginning 0) start))
  18245. (setq list
  18246. (cons (substring string start (match-beginning 0))
  18247. list)))
  18248. (setq start (match-end 0)))
  18249. (or (eq start (length string))
  18250. (setq list
  18251. (cons (substring string start)
  18252. list)))
  18253. (nreverse list)))
  18254. (defun org-quote-vert (s)
  18255. "Replace \"|\" with \"\\vert\"."
  18256. (while (string-match "|" s)
  18257. (setq s (replace-match "\\vert" t t s)))
  18258. s)
  18259. (defun org-uuidgen-p (s)
  18260. "Is S an ID created by UUIDGEN?"
  18261. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  18262. (defun org-in-src-block-p nil
  18263. "Whether point is in a code source block."
  18264. (let (ov)
  18265. (when (setq ov (overlays-at (point)))
  18266. (memq 'org-block-background
  18267. (overlay-properties
  18268. (car ov))))))
  18269. (defun org-context ()
  18270. "Return a list of contexts of the current cursor position.
  18271. If several contexts apply, all are returned.
  18272. Each context entry is a list with a symbol naming the context, and
  18273. two positions indicating start and end of the context. Possible
  18274. contexts are:
  18275. :headline anywhere in a headline
  18276. :headline-stars on the leading stars in a headline
  18277. :todo-keyword on a TODO keyword (including DONE) in a headline
  18278. :tags on the TAGS in a headline
  18279. :priority on the priority cookie in a headline
  18280. :item on the first line of a plain list item
  18281. :item-bullet on the bullet/number of a plain list item
  18282. :checkbox on the checkbox in a plain list item
  18283. :table in an org-mode table
  18284. :table-special on a special filed in a table
  18285. :table-table in a table.el table
  18286. :clocktable in a clocktable
  18287. :src-block in a source block
  18288. :link on a hyperlink
  18289. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT, QUOTE.
  18290. :target on a <<target>>
  18291. :radio-target on a <<<radio-target>>>
  18292. :latex-fragment on a LaTeX fragment
  18293. :latex-preview on a LaTeX fragment with overlaid preview image
  18294. This function expects the position to be visible because it uses font-lock
  18295. faces as a help to recognize the following contexts: :table-special, :link,
  18296. and :keyword."
  18297. (let* ((f (get-text-property (point) 'face))
  18298. (faces (if (listp f) f (list f)))
  18299. (case-fold-search t)
  18300. (p (point)) clist o)
  18301. ;; First the large context
  18302. (cond
  18303. ((org-at-heading-p t)
  18304. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  18305. (when (progn
  18306. (beginning-of-line 1)
  18307. (looking-at org-todo-line-tags-regexp))
  18308. (push (org-point-in-group p 1 :headline-stars) clist)
  18309. (push (org-point-in-group p 2 :todo-keyword) clist)
  18310. (push (org-point-in-group p 4 :tags) clist))
  18311. (goto-char p)
  18312. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  18313. (if (looking-at "\\[#[A-Z0-9]\\]")
  18314. (push (org-point-in-group p 0 :priority) clist)))
  18315. ((org-at-item-p)
  18316. (push (org-point-in-group p 2 :item-bullet) clist)
  18317. (push (list :item (point-at-bol)
  18318. (save-excursion (org-end-of-item) (point)))
  18319. clist)
  18320. (and (org-at-item-checkbox-p)
  18321. (push (org-point-in-group p 0 :checkbox) clist)))
  18322. ((org-at-table-p)
  18323. (push (list :table (org-table-begin) (org-table-end)) clist)
  18324. (if (memq 'org-formula faces)
  18325. (push (list :table-special
  18326. (previous-single-property-change p 'face)
  18327. (next-single-property-change p 'face)) clist)))
  18328. ((org-at-table-p 'any)
  18329. (push (list :table-table) clist)))
  18330. (goto-char p)
  18331. ;; New the "medium" contexts: clocktables, source blocks
  18332. (cond ((org-in-clocktable-p)
  18333. (push (list :clocktable
  18334. (and (or (looking-at "#\\+BEGIN: clocktable")
  18335. (search-backward "#+BEGIN: clocktable" nil t))
  18336. (match-beginning 0))
  18337. (and (re-search-forward "#\\+END:?" nil t)
  18338. (match-end 0))) clist))
  18339. ((org-in-src-block-p)
  18340. (push (list :src-block
  18341. (and (or (looking-at "#\\+BEGIN_SRC")
  18342. (search-backward "#+BEGIN_SRC" nil t))
  18343. (match-beginning 0))
  18344. (and (search-forward "#+END_SRC" nil t)
  18345. (match-beginning 0))) clist)))
  18346. (goto-char p)
  18347. ;; Now the small context
  18348. (cond
  18349. ((org-at-timestamp-p)
  18350. (push (org-point-in-group p 0 :timestamp) clist))
  18351. ((memq 'org-link faces)
  18352. (push (list :link
  18353. (previous-single-property-change p 'face)
  18354. (next-single-property-change p 'face)) clist))
  18355. ((memq 'org-special-keyword faces)
  18356. (push (list :keyword
  18357. (previous-single-property-change p 'face)
  18358. (next-single-property-change p 'face)) clist))
  18359. ((org-at-target-p)
  18360. (push (org-point-in-group p 0 :target) clist)
  18361. (goto-char (1- (match-beginning 0)))
  18362. (if (looking-at org-radio-target-regexp)
  18363. (push (org-point-in-group p 0 :radio-target) clist))
  18364. (goto-char p))
  18365. ((setq o (car (delq nil
  18366. (mapcar
  18367. (lambda (x)
  18368. (if (memq x org-latex-fragment-image-overlays) x))
  18369. (overlays-at (point))))))
  18370. (push (list :latex-fragment
  18371. (overlay-start o) (overlay-end o)) clist)
  18372. (push (list :latex-preview
  18373. (overlay-start o) (overlay-end o)) clist))
  18374. ((org-inside-LaTeX-fragment-p)
  18375. ;; FIXME: positions wrong.
  18376. (push (list :latex-fragment (point) (point)) clist)))
  18377. (setq clist (nreverse (delq nil clist)))
  18378. clist))
  18379. ;; FIXME: Compare with at-regexp-p Do we need both?
  18380. (defun org-in-regexp (re &optional nlines visually)
  18381. "Check if point is inside a match of regexp.
  18382. Normally only the current line is checked, but you can include NLINES extra
  18383. lines both before and after point into the search.
  18384. If VISUALLY is set, require that the cursor is not after the match but
  18385. really on, so that the block visually is on the match."
  18386. (catch 'exit
  18387. (let ((pos (point))
  18388. (eol (point-at-eol (+ 1 (or nlines 0))))
  18389. (inc (if visually 1 0)))
  18390. (save-excursion
  18391. (beginning-of-line (- 1 (or nlines 0)))
  18392. (while (re-search-forward re eol t)
  18393. (if (and (<= (match-beginning 0) pos)
  18394. (>= (+ inc (match-end 0)) pos))
  18395. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  18396. (defun org-at-regexp-p (regexp)
  18397. "Is point inside a match of REGEXP in the current line?"
  18398. (catch 'exit
  18399. (save-excursion
  18400. (let ((pos (point)) (end (point-at-eol)))
  18401. (beginning-of-line 1)
  18402. (while (re-search-forward regexp end t)
  18403. (if (and (<= (match-beginning 0) pos)
  18404. (>= (match-end 0) pos))
  18405. (throw 'exit t)))
  18406. nil))))
  18407. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  18408. "Non-nil when point is between matches of START-RE and END-RE.
  18409. Also return a non-nil value when point is on one of the matches.
  18410. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  18411. buffer positions. Default values are the positions of headlines
  18412. surrounding the point.
  18413. The functions returns a cons cell whose car (resp. cdr) is the
  18414. position before START-RE (resp. after END-RE)."
  18415. (save-match-data
  18416. (let ((pos (point))
  18417. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  18418. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  18419. beg end)
  18420. (save-excursion
  18421. ;; Point is on a block when on START-RE or if START-RE can be
  18422. ;; found before it...
  18423. (and (or (org-at-regexp-p start-re)
  18424. (re-search-backward start-re limit-up t))
  18425. (setq beg (match-beginning 0))
  18426. ;; ... and END-RE after it...
  18427. (goto-char (match-end 0))
  18428. (re-search-forward end-re limit-down t)
  18429. (> (setq end (match-end 0)) pos)
  18430. ;; ... without another START-RE in-between.
  18431. (goto-char (match-beginning 0))
  18432. (not (re-search-backward start-re (1+ beg) t))
  18433. ;; Return value.
  18434. (cons beg end))))))
  18435. (defun org-in-block-p (names)
  18436. "Non-nil when point belongs to a block whose name belongs to NAMES.
  18437. NAMES is a list of strings containing names of blocks.
  18438. Return first block name matched, or nil. Beware that in case of
  18439. nested blocks, the returned name may not belong to the closest
  18440. block from point."
  18441. (save-match-data
  18442. (catch 'exit
  18443. (let ((case-fold-search t)
  18444. (lim-up (save-excursion (outline-previous-heading)))
  18445. (lim-down (save-excursion (outline-next-heading))))
  18446. (mapc (lambda (name)
  18447. (let ((n (regexp-quote name)))
  18448. (when (org-between-regexps-p
  18449. (concat "^[ \t]*#\\+begin_" n)
  18450. (concat "^[ \t]*#\\+end_" n)
  18451. lim-up lim-down)
  18452. (throw 'exit n))))
  18453. names))
  18454. nil)))
  18455. (defun org-occur-in-agenda-files (regexp &optional nlines)
  18456. "Call `multi-occur' with buffers for all agenda files."
  18457. (interactive "sOrg-files matching: \np")
  18458. (let* ((files (org-agenda-files))
  18459. (tnames (mapcar 'file-truename files))
  18460. (extra org-agenda-text-search-extra-files)
  18461. f)
  18462. (when (eq (car extra) 'agenda-archives)
  18463. (setq extra (cdr extra))
  18464. (setq files (org-add-archive-files files)))
  18465. (while (setq f (pop extra))
  18466. (unless (member (file-truename f) tnames)
  18467. (add-to-list 'files f 'append)
  18468. (add-to-list 'tnames (file-truename f) 'append)))
  18469. (multi-occur
  18470. (mapcar (lambda (x)
  18471. (with-current-buffer
  18472. (or (get-file-buffer x) (find-file-noselect x))
  18473. (widen)
  18474. (current-buffer)))
  18475. files)
  18476. regexp)))
  18477. (if (boundp 'occur-mode-find-occurrence-hook)
  18478. ;; Emacs 23
  18479. (add-hook 'occur-mode-find-occurrence-hook
  18480. (lambda ()
  18481. (when (derived-mode-p 'org-mode)
  18482. (org-reveal))))
  18483. ;; Emacs 22
  18484. (defadvice occur-mode-goto-occurrence
  18485. (after org-occur-reveal activate)
  18486. (and (derived-mode-p 'org-mode) (org-reveal)))
  18487. (defadvice occur-mode-goto-occurrence-other-window
  18488. (after org-occur-reveal activate)
  18489. (and (derived-mode-p 'org-mode) (org-reveal)))
  18490. (defadvice occur-mode-display-occurrence
  18491. (after org-occur-reveal activate)
  18492. (when (derived-mode-p 'org-mode)
  18493. (let ((pos (occur-mode-find-occurrence)))
  18494. (with-current-buffer (marker-buffer pos)
  18495. (save-excursion
  18496. (goto-char pos)
  18497. (org-reveal)))))))
  18498. (defun org-occur-link-in-agenda-files ()
  18499. "Create a link and search for it in the agendas.
  18500. The link is not stored in `org-stored-links', it is just created
  18501. for the search purpose."
  18502. (interactive)
  18503. (let ((link (condition-case nil
  18504. (org-store-link nil)
  18505. (error "Unable to create a link to here"))))
  18506. (org-occur-in-agenda-files (regexp-quote link))))
  18507. (defun org-uniquify (list)
  18508. "Remove duplicate elements from LIST."
  18509. (let (res)
  18510. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  18511. res))
  18512. (defun org-delete-all (elts list)
  18513. "Remove all elements in ELTS from LIST."
  18514. (while elts
  18515. (setq list (delete (pop elts) list)))
  18516. list)
  18517. (defun org-count (cl-item cl-seq)
  18518. "Count the number of occurrences of ITEM in SEQ.
  18519. Taken from `count' in cl-seq.el with all keyword arguments removed."
  18520. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  18521. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  18522. (while (< cl-start cl-end)
  18523. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  18524. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  18525. (setq cl-start (1+ cl-start)))
  18526. cl-count))
  18527. (defun org-remove-if (predicate seq)
  18528. "Remove everything from SEQ that fulfills PREDICATE."
  18529. (let (res e)
  18530. (while seq
  18531. (setq e (pop seq))
  18532. (if (not (funcall predicate e)) (push e res)))
  18533. (nreverse res)))
  18534. (defun org-remove-if-not (predicate seq)
  18535. "Remove everything from SEQ that does not fulfill PREDICATE."
  18536. (let (res e)
  18537. (while seq
  18538. (setq e (pop seq))
  18539. (if (funcall predicate e) (push e res)))
  18540. (nreverse res)))
  18541. (defun org-reduce (cl-func cl-seq &rest cl-keys)
  18542. "Reduce two-argument FUNCTION across SEQ.
  18543. Taken from `reduce' in cl-seq.el with all keyword arguments but
  18544. \":initial-value\" removed."
  18545. (let ((cl-accum (cond ((memq :initial-value cl-keys)
  18546. (cadr (memq :initial-value cl-keys)))
  18547. (cl-seq (pop cl-seq))
  18548. (t (funcall cl-func)))))
  18549. (while cl-seq
  18550. (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
  18551. cl-accum))
  18552. (defun org-back-over-empty-lines ()
  18553. "Move backwards over whitespace, to the beginning of the first empty line.
  18554. Returns the number of empty lines passed."
  18555. (let ((pos (point)))
  18556. (if (cdr (assoc 'heading org-blank-before-new-entry))
  18557. (skip-chars-backward " \t\n\r")
  18558. (unless (eobp)
  18559. (forward-line -1)))
  18560. (beginning-of-line 2)
  18561. (goto-char (min (point) pos))
  18562. (count-lines (point) pos)))
  18563. (defun org-skip-whitespace ()
  18564. (skip-chars-forward " \t\n\r"))
  18565. (defun org-point-in-group (point group &optional context)
  18566. "Check if POINT is in match-group GROUP.
  18567. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  18568. match. If the match group does not exist or point is not inside it,
  18569. return nil."
  18570. (and (match-beginning group)
  18571. (>= point (match-beginning group))
  18572. (<= point (match-end group))
  18573. (if context
  18574. (list context (match-beginning group) (match-end group))
  18575. t)))
  18576. (defun org-switch-to-buffer-other-window (&rest args)
  18577. "Switch to buffer in a second window on the current frame.
  18578. In particular, do not allow pop-up frames.
  18579. Returns the newly created buffer."
  18580. (let (pop-up-frames special-display-buffer-names special-display-regexps
  18581. special-display-function)
  18582. (apply 'switch-to-buffer-other-window args)))
  18583. (defun org-combine-plists (&rest plists)
  18584. "Create a single property list from all plists in PLISTS.
  18585. The process starts by copying the first list, and then setting properties
  18586. from the other lists. Settings in the last list are the most significant
  18587. ones and overrule settings in the other lists."
  18588. (let ((rtn (copy-sequence (pop plists)))
  18589. p v ls)
  18590. (while plists
  18591. (setq ls (pop plists))
  18592. (while ls
  18593. (setq p (pop ls) v (pop ls))
  18594. (setq rtn (plist-put rtn p v))))
  18595. rtn))
  18596. (defun org-move-line-down (arg)
  18597. "Move the current line down. With prefix argument, move it past ARG lines."
  18598. (interactive "p")
  18599. (let ((col (current-column))
  18600. beg end pos)
  18601. (beginning-of-line 1) (setq beg (point))
  18602. (beginning-of-line 2) (setq end (point))
  18603. (beginning-of-line (+ 1 arg))
  18604. (setq pos (move-marker (make-marker) (point)))
  18605. (insert (delete-and-extract-region beg end))
  18606. (goto-char pos)
  18607. (org-move-to-column col)))
  18608. (defun org-move-line-up (arg)
  18609. "Move the current line up. With prefix argument, move it past ARG lines."
  18610. (interactive "p")
  18611. (let ((col (current-column))
  18612. beg end pos)
  18613. (beginning-of-line 1) (setq beg (point))
  18614. (beginning-of-line 2) (setq end (point))
  18615. (beginning-of-line (- arg))
  18616. (setq pos (move-marker (make-marker) (point)))
  18617. (insert (delete-and-extract-region beg end))
  18618. (goto-char pos)
  18619. (org-move-to-column col)))
  18620. (defun org-replace-escapes (string table)
  18621. "Replace %-escapes in STRING with values in TABLE.
  18622. TABLE is an association list with keys like \"%a\" and string values.
  18623. The sequences in STRING may contain normal field width and padding information,
  18624. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  18625. so values can contain further %-escapes if they are define later in TABLE."
  18626. (let ((tbl (copy-alist table))
  18627. (case-fold-search nil)
  18628. (pchg 0)
  18629. e re rpl)
  18630. (while (setq e (pop tbl))
  18631. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  18632. (when (and (cdr e) (string-match re (cdr e)))
  18633. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  18634. (safe "SREF"))
  18635. (add-text-properties 0 3 (list 'sref sref) safe)
  18636. (setcdr e (replace-match safe t t (cdr e)))))
  18637. (while (string-match re string)
  18638. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  18639. (cdr e)))
  18640. (setq string (replace-match rpl t t string))))
  18641. (while (setq pchg (next-property-change pchg string))
  18642. (let ((sref (get-text-property pchg 'sref string)))
  18643. (when (and sref (string-match "SREF" string pchg))
  18644. (setq string (replace-match sref t t string)))))
  18645. string))
  18646. (defun org-sublist (list start end)
  18647. "Return a section of LIST, from START to END.
  18648. Counting starts at 1."
  18649. (let (rtn (c start))
  18650. (setq list (nthcdr (1- start) list))
  18651. (while (and list (<= c end))
  18652. (push (pop list) rtn)
  18653. (setq c (1+ c)))
  18654. (nreverse rtn)))
  18655. (defun org-find-base-buffer-visiting (file)
  18656. "Like `find-buffer-visiting' but always return the base buffer and
  18657. not an indirect buffer."
  18658. (let ((buf (or (get-file-buffer file)
  18659. (find-buffer-visiting file))))
  18660. (if buf
  18661. (or (buffer-base-buffer buf) buf)
  18662. nil)))
  18663. (defun org-image-file-name-regexp (&optional extensions)
  18664. "Return regexp matching the file names of images.
  18665. If EXTENSIONS is given, only match these."
  18666. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  18667. (image-file-name-regexp)
  18668. (let ((image-file-name-extensions
  18669. (or extensions
  18670. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  18671. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  18672. (concat "\\."
  18673. (regexp-opt (nconc (mapcar 'upcase
  18674. image-file-name-extensions)
  18675. image-file-name-extensions)
  18676. t)
  18677. "\\'"))))
  18678. (defun org-file-image-p (file &optional extensions)
  18679. "Return non-nil if FILE is an image."
  18680. (save-match-data
  18681. (string-match (org-image-file-name-regexp extensions) file)))
  18682. (defun org-get-cursor-date ()
  18683. "Return the date at cursor in as a time.
  18684. This works in the calendar and in the agenda, anywhere else it just
  18685. returns the current time."
  18686. (let (date day defd)
  18687. (cond
  18688. ((eq major-mode 'calendar-mode)
  18689. (setq date (calendar-cursor-to-date)
  18690. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  18691. ((eq major-mode 'org-agenda-mode)
  18692. (setq day (get-text-property (point) 'day))
  18693. (if day
  18694. (setq date (calendar-gregorian-from-absolute day)
  18695. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  18696. (nth 2 date))))))
  18697. (or defd (current-time))))
  18698. (defvar org-agenda-action-marker (make-marker)
  18699. "Marker pointing to the entry for the next agenda action.")
  18700. (defun org-mark-entry-for-agenda-action ()
  18701. "Mark the current entry as target of an agenda action.
  18702. Agenda actions are actions executed from the agenda with the key `k',
  18703. which make use of the date at the cursor."
  18704. (interactive)
  18705. (move-marker org-agenda-action-marker
  18706. (save-excursion (org-back-to-heading t) (point))
  18707. (current-buffer))
  18708. (message
  18709. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  18710. (defun org-mark-subtree ()
  18711. "Mark the current subtree.
  18712. This puts point at the start of the current subtree, and mark at the end.
  18713. If point is in an inline task, mark that task instead."
  18714. (interactive)
  18715. (let ((inline-task-p
  18716. (and (featurep 'org-inlinetask)
  18717. (org-inlinetask-in-task-p)))
  18718. (beg))
  18719. ;; Get beginning of subtree
  18720. (cond
  18721. (inline-task-p (org-inlinetask-goto-beginning))
  18722. ((org-at-heading-p) (beginning-of-line))
  18723. (t (org-with-limited-levels (outline-previous-visible-heading 1))))
  18724. (setq beg (point))
  18725. ;; Get end of it
  18726. (if inline-task-p
  18727. (org-inlinetask-goto-end)
  18728. (org-end-of-subtree))
  18729. ;; Mark zone
  18730. (push-mark (point) nil t)
  18731. (goto-char beg)))
  18732. ;;; Paragraph filling stuff.
  18733. ;; We want this to be just right, so use the full arsenal.
  18734. (declare-function orgstruct++-ignore-org-filling "org-macs.el" (&rest body))
  18735. (defun org-indent-line-function ()
  18736. "Indent line depending on context."
  18737. (interactive)
  18738. (if orgstruct-is-++
  18739. (orgstruct++-ignore-org-filling
  18740. (funcall indent-line-function))
  18741. (let* ((pos (point))
  18742. (itemp (org-at-item-p))
  18743. (case-fold-search t)
  18744. (org-drawer-regexp (or org-drawer-regexp "\000"))
  18745. (inline-task-p (and (featurep 'org-inlinetask)
  18746. (org-inlinetask-in-task-p)))
  18747. (inline-re (and inline-task-p
  18748. (org-inlinetask-outline-regexp)))
  18749. column)
  18750. (beginning-of-line 1)
  18751. (cond
  18752. ;; Comments
  18753. ((looking-at "# ") (setq column 0))
  18754. ;; Headings
  18755. ((looking-at org-outline-regexp) (setq column 0))
  18756. ;; Included files
  18757. ((looking-at "#\\+include:") (setq column 0))
  18758. ;; Footnote definition
  18759. ((looking-at org-footnote-definition-re) (setq column 0))
  18760. ;; Literal examples
  18761. ((looking-at "[ \t]*:\\( \\|$\\)")
  18762. (setq column (org-get-indentation))) ; do nothing
  18763. ;; Lists
  18764. ((ignore-errors (goto-char (org-in-item-p)))
  18765. (setq column (if itemp
  18766. (org-get-indentation)
  18767. (org-list-item-body-column (point))))
  18768. (goto-char pos))
  18769. ;; Drawers
  18770. ((and (looking-at "[ \t]*:END:")
  18771. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  18772. (save-excursion
  18773. (goto-char (1- (match-beginning 1)))
  18774. (setq column (current-column))))
  18775. ;; Special blocks
  18776. ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
  18777. (save-excursion
  18778. (re-search-backward
  18779. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  18780. (setq column (org-get-indentation (match-string 0))))
  18781. ((and (not (looking-at "[ \t]*#\\+begin_"))
  18782. (org-between-regexps-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
  18783. (save-excursion
  18784. (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
  18785. (setq column
  18786. (cond ((equal (downcase (match-string 1)) "src")
  18787. ;; src blocks: let `org-edit-src-exit' handle them
  18788. (org-get-indentation))
  18789. ((equal (downcase (match-string 1)) "example")
  18790. (max (org-get-indentation)
  18791. (org-get-indentation (match-string 0))))
  18792. (t
  18793. (org-get-indentation (match-string 0))))))
  18794. ;; This line has nothing special, look at the previous relevant
  18795. ;; line to compute indentation
  18796. (t
  18797. (beginning-of-line 0)
  18798. (while (and (not (bobp))
  18799. (not (looking-at org-drawer-regexp))
  18800. ;; When point started in an inline task, do not move
  18801. ;; above task starting line.
  18802. (not (and inline-task-p (looking-at inline-re)))
  18803. ;; Skip drawers, blocks, empty lines, verbatim,
  18804. ;; comments, tables, footnotes definitions, lists,
  18805. ;; inline tasks.
  18806. (or (and (looking-at "[ \t]*:END:")
  18807. (re-search-backward org-drawer-regexp nil t))
  18808. (and (looking-at "[ \t]*#\\+end_")
  18809. (re-search-backward "[ \t]*#\\+begin_"nil t))
  18810. (looking-at "[ \t]*[\n:#|]")
  18811. (looking-at org-footnote-definition-re)
  18812. (and (ignore-errors (goto-char (org-in-item-p)))
  18813. (goto-char
  18814. (org-list-get-top-point (org-list-struct))))
  18815. (and (not inline-task-p)
  18816. (featurep 'org-inlinetask)
  18817. (org-inlinetask-in-task-p)
  18818. (or (org-inlinetask-goto-beginning) t))))
  18819. (beginning-of-line 0))
  18820. (cond
  18821. ;; There was an heading above.
  18822. ((looking-at "\\*+[ \t]+")
  18823. (if (not org-adapt-indentation)
  18824. (setq column 0)
  18825. (goto-char (match-end 0))
  18826. (setq column (current-column))))
  18827. ;; A drawer had started and is unfinished
  18828. ((looking-at org-drawer-regexp)
  18829. (goto-char (1- (match-beginning 1)))
  18830. (setq column (current-column)))
  18831. ;; Else, nothing noticeable found: get indentation and go on.
  18832. (t (setq column (org-get-indentation))))))
  18833. ;; Now apply indentation and move cursor accordingly
  18834. (goto-char pos)
  18835. (if (<= (current-column) (current-indentation))
  18836. (org-indent-line-to column)
  18837. (save-excursion (org-indent-line-to column)))
  18838. ;; Special polishing for properties, see `org-property-format'
  18839. (setq column (current-column))
  18840. (beginning-of-line 1)
  18841. (if (looking-at
  18842. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  18843. (replace-match (concat (match-string 1)
  18844. (format org-property-format
  18845. (match-string 2) (match-string 3)))
  18846. t t))
  18847. (org-move-to-column column))))
  18848. (defun org-indent-drawer ()
  18849. "Indent the drawer at point."
  18850. (interactive)
  18851. (let ((p (point))
  18852. (e (and (save-excursion (re-search-forward ":END:" nil t))
  18853. (match-end 0)))
  18854. (folded
  18855. (save-excursion
  18856. (end-of-line)
  18857. (when (overlays-at (point))
  18858. (member 'invisible (overlay-properties
  18859. (car (overlays-at (point)))))))))
  18860. (when folded (org-cycle))
  18861. (indent-for-tab-command)
  18862. (while (and (move-beginning-of-line 2) (< (point) e))
  18863. (indent-for-tab-command))
  18864. (goto-char p)
  18865. (when folded (org-cycle)))
  18866. (message "Drawer at point indented"))
  18867. (defun org-indent-block ()
  18868. "Indent the block at point."
  18869. (interactive)
  18870. (let ((p (point))
  18871. (case-fold-search t)
  18872. (e (and (save-excursion (re-search-forward "#\\+end_?\\(?:[a-z]+\\)?" nil t))
  18873. (match-end 0)))
  18874. (folded
  18875. (save-excursion
  18876. (end-of-line)
  18877. (when (overlays-at (point))
  18878. (member 'invisible (overlay-properties
  18879. (car (overlays-at (point)))))))))
  18880. (when folded (org-cycle))
  18881. (indent-for-tab-command)
  18882. (while (and (move-beginning-of-line 2) (< (point) e))
  18883. (indent-for-tab-command))
  18884. (goto-char p)
  18885. (when folded (org-cycle)))
  18886. (message "Block at point indented"))
  18887. (defvar org-adaptive-fill-regexp-backup adaptive-fill-regexp
  18888. "Variable to store copy of `adaptive-fill-regexp'.
  18889. Since `adaptive-fill-regexp' is set to never match, we need to
  18890. store a backup of its value before entering `org-mode' so that
  18891. the functionality can be provided as a fall-back.")
  18892. (defun org-set-autofill-regexps ()
  18893. (interactive)
  18894. ;; In the paragraph separator we include headlines, because filling
  18895. ;; text in a line directly attached to a headline would otherwise
  18896. ;; fill the headline as well.
  18897. (org-set-local 'comment-start-skip "^#+[ \t]*")
  18898. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|#]")
  18899. ;; The paragraph starter includes hand-formatted lists.
  18900. (org-set-local
  18901. 'paragraph-start
  18902. (concat
  18903. "\f" "\\|"
  18904. "[ ]*$" "\\|"
  18905. org-outline-regexp "\\|"
  18906. "[ \t]*#" "\\|"
  18907. (org-item-re) "\\|"
  18908. "[ \t]*[:|]" "\\|"
  18909. "\\$\\$" "\\|"
  18910. "\\\\\\(begin\\|end\\|[][]\\)"))
  18911. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  18912. ;; But only if the user has not turned off tables or fixed-width regions
  18913. (org-set-local
  18914. 'auto-fill-inhibit-regexp
  18915. (concat org-outline-regexp
  18916. "\\|#\\+"
  18917. "\\|[ \t]*" org-keyword-time-regexp
  18918. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  18919. (concat
  18920. "\\|[ \t]*["
  18921. (if org-enable-table-editor "|" "")
  18922. (if org-enable-fixed-width-editor ":" "")
  18923. "]"))))
  18924. ;; We use our own fill-paragraph function, to make sure that tables
  18925. ;; and fixed-width regions are not wrapped. That function will pass
  18926. ;; through to `fill-paragraph' when appropriate.
  18927. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  18928. ;; Prevent auto-fill from inserting unwanted new items.
  18929. (if (boundp 'fill-nobreak-predicate)
  18930. (org-set-local
  18931. 'fill-nobreak-predicate
  18932. (org-uniquify
  18933. (append fill-nobreak-predicate
  18934. '(org-fill-item-nobreak-p org-fill-line-break-nobreak-p)))))
  18935. ;; Adaptive filling: To get full control, first make sure that
  18936. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  18937. (unless (local-variable-p 'adaptive-fill-regexp (current-buffer))
  18938. (org-set-local 'org-adaptive-fill-regexp-backup
  18939. adaptive-fill-regexp))
  18940. (org-set-local 'adaptive-fill-regexp "\000")
  18941. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  18942. (org-set-local 'adaptive-fill-function
  18943. 'org-adaptive-fill-function)
  18944. (org-set-local
  18945. 'align-mode-rules-list
  18946. '((org-in-buffer-settings
  18947. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  18948. (modes . '(org-mode))))))
  18949. (defun org-fill-item-nobreak-p ()
  18950. "Non-nil when a line break at point would insert a new item."
  18951. (and (looking-at (org-item-re)) (org-list-in-valid-context-p)))
  18952. (defun org-fill-line-break-nobreak-p ()
  18953. "Non-nil when a line break at point would create an Org line break."
  18954. (save-excursion
  18955. (skip-chars-backward "[ \t]")
  18956. (skip-chars-backward "\\\\")
  18957. (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
  18958. (defun org-fill-paragraph (&optional justify)
  18959. "Re-align a table, pass through to fill-paragraph if no table."
  18960. (let ((table-p (org-at-table-p))
  18961. (table.el-p (org-at-table.el-p))
  18962. (itemp (org-in-item-p)))
  18963. (cond ((and (equal (char-after (point-at-bol)) ?*)
  18964. (save-excursion (goto-char (point-at-bol))
  18965. (looking-at org-outline-regexp)))
  18966. t) ; skip headlines
  18967. (table.el-p t) ; skip table.el tables
  18968. (table-p (org-table-align) t) ; align Org tables
  18969. (itemp ; align text in items
  18970. (let* ((struct (save-excursion (goto-char itemp)
  18971. (org-list-struct)))
  18972. (parents (org-list-parents-alist struct))
  18973. (children (org-list-get-children itemp struct parents))
  18974. beg end prev next prefix)
  18975. ;; Determine in which part of item point is: before
  18976. ;; first child, after last child, between two
  18977. ;; sub-lists, or simply in item if there's no child.
  18978. (cond
  18979. ((not children)
  18980. (setq prefix (make-string (org-list-item-body-column itemp) ?\ )
  18981. beg itemp
  18982. end (org-list-get-item-end itemp struct)))
  18983. ((< (point) (setq next (car children)))
  18984. (setq prefix (make-string (org-list-item-body-column itemp) ?\ )
  18985. beg itemp
  18986. end next))
  18987. ((> (point) (setq prev (car (last children))))
  18988. (setq beg (org-list-get-item-end prev struct)
  18989. end (org-list-get-item-end itemp struct)
  18990. prefix (save-excursion
  18991. (goto-char beg)
  18992. (skip-chars-forward " \t")
  18993. (make-string (current-column) ?\ ))))
  18994. (t (catch 'exit
  18995. (while (setq next (pop children))
  18996. (if (> (point) next)
  18997. (setq prev next)
  18998. (setq beg (org-list-get-item-end prev struct)
  18999. end next
  19000. prefix (save-excursion
  19001. (goto-char beg)
  19002. (skip-chars-forward " \t")
  19003. (make-string (current-column) ?\ )))
  19004. (throw 'exit nil))))))
  19005. ;; Use `fill-paragraph' with buffer narrowed to item
  19006. ;; without any child, and with our computed PREFIX.
  19007. (flet ((fill-context-prefix (from to &optional flr) prefix))
  19008. (save-restriction
  19009. (narrow-to-region beg end)
  19010. (save-excursion (fill-paragraph justify)))) t))
  19011. ;; Special case where point is not in a list but is on
  19012. ;; a paragraph adjacent to a list: make sure this paragraph
  19013. ;; doesn't get merged with the end of the list by narrowing
  19014. ;; buffer first.
  19015. ((and (derived-mode-p 'org-mode)
  19016. (save-excursion (forward-paragraph -1)
  19017. (setq itemp (org-in-item-p))))
  19018. (let ((struct (save-excursion (goto-char itemp)
  19019. (org-list-struct))))
  19020. (save-restriction
  19021. (narrow-to-region (org-list-get-bottom-point struct)
  19022. (save-excursion (forward-paragraph 1)
  19023. (point)))
  19024. (fill-paragraph justify) t)))
  19025. ;; Don't fill schedule/deadline line before a paragraph
  19026. ;; This only makes sense in real org-mode buffers
  19027. ((and (eq major-mode 'org-mode)
  19028. (save-excursion (forward-paragraph -1)
  19029. (or (looking-at (concat "^[^\n]*" org-scheduled-regexp ".*$"))
  19030. (looking-at (concat "^[^\n]*" org-deadline-regexp ".*$")))))
  19031. (save-restriction
  19032. (narrow-to-region (1+ (match-end 0))
  19033. (save-excursion (forward-paragraph 1) (point)))
  19034. (fill-paragraph justify) t))
  19035. ;; Else falls back on `org-fill-paragraph-fallback-function'
  19036. (orgstruct-is-++
  19037. (orgstruct++-ignore-org-filling
  19038. (fill-paragraph)))
  19039. ;; (org-fill-paragraph-fallback-function
  19040. ;; (funcall org-fill-paragraph-fallback-function justify))
  19041. ;; Else simply call `fill-paragraph'.
  19042. (t nil))))
  19043. ;; For reference, this is the default value of adaptive-fill-regexp
  19044. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  19045. (defun org-adaptive-fill-function ()
  19046. "Return a fill prefix for org-mode files."
  19047. (let (itemp)
  19048. (save-excursion
  19049. (cond
  19050. ;; Comment line
  19051. ((looking-at "#[ \t]+")
  19052. (match-string-no-properties 0))
  19053. ;; Plain list item
  19054. ((org-at-item-p)
  19055. (make-string (org-list-item-body-column (point-at-bol)) ?\ ))
  19056. ;; Point is in a list after `backward-paragraph': original
  19057. ;; point wasn't in the list, or filling would have been taken
  19058. ;; care of by `org-auto-fill-function', but the list and the
  19059. ;; real paragraph are not separated by a blank line. Thus, move
  19060. ;; point after the list to go back to real paragraph and
  19061. ;; determine fill-prefix.
  19062. ((setq itemp (org-in-item-p))
  19063. (goto-char itemp)
  19064. (let* ((struct (org-list-struct))
  19065. (bottom (org-list-get-bottom-point struct)))
  19066. (goto-char bottom)
  19067. (make-string (org-get-indentation) ?\ )))
  19068. ;; Other text
  19069. ((looking-at org-adaptive-fill-regexp-backup)
  19070. (match-string-no-properties 0))))))
  19071. (defun org-auto-fill-function ()
  19072. "Auto-fill function."
  19073. (let (itemp prefix)
  19074. ;; When in a list, compute an appropriate fill-prefix and make
  19075. ;; sure it will be used by `do-auto-fill'.
  19076. (cond ((setq itemp (org-in-item-p))
  19077. (progn
  19078. (setq prefix (make-string (org-list-item-body-column itemp) ?\ ))
  19079. (flet ((fill-context-prefix (from to &optional flr) prefix))
  19080. (do-auto-fill))))
  19081. (orgstruct-is-++
  19082. (orgstruct++-ignore-org-filling
  19083. (do-auto-fill)))
  19084. (t (do-auto-fill)))))
  19085. ;;; Other stuff.
  19086. (defun org-toggle-fixed-width-section (arg)
  19087. "Toggle the fixed-width export.
  19088. If there is no active region, the QUOTE keyword at the current headline is
  19089. inserted or removed. When present, it causes the text between this headline
  19090. and the next to be exported as fixed-width text, and unmodified.
  19091. If there is an active region, this command adds or removes a colon as the
  19092. first character of this line. If the first character of a line is a colon,
  19093. this line is also exported in fixed-width font."
  19094. (interactive "P")
  19095. (let* ((cc 0)
  19096. (regionp (org-region-active-p))
  19097. (beg (if regionp (region-beginning) (point)))
  19098. (end (if regionp (region-end)))
  19099. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  19100. (case-fold-search nil)
  19101. (re "[ \t]*\\(:\\(?: \\|$\\)\\)")
  19102. off)
  19103. (if regionp
  19104. (save-excursion
  19105. (goto-char beg)
  19106. (setq cc (current-column))
  19107. (beginning-of-line 1)
  19108. (setq off (looking-at re))
  19109. (while (> nlines 0)
  19110. (setq nlines (1- nlines))
  19111. (beginning-of-line 1)
  19112. (cond
  19113. (arg
  19114. (org-move-to-column cc t)
  19115. (insert ": \n")
  19116. (forward-line -1))
  19117. ((and off (looking-at re))
  19118. (replace-match "" t t nil 1))
  19119. ((not off) (org-move-to-column cc t) (insert ": ")))
  19120. (forward-line 1)))
  19121. (save-excursion
  19122. (org-back-to-heading)
  19123. (cond
  19124. ((looking-at (format org-heading-keyword-regexp-format
  19125. org-quote-string))
  19126. (goto-char (match-end 1))
  19127. (looking-at (concat " +" org-quote-string))
  19128. (replace-match "" t t)
  19129. (when (eolp) (insert " ")))
  19130. ((looking-at org-outline-regexp)
  19131. (goto-char (match-end 0))
  19132. (insert org-quote-string " ")))))))
  19133. (defun org-reftex-citation ()
  19134. "Use reftex-citation to insert a citation into the buffer.
  19135. This looks for a line like
  19136. #+BIBLIOGRAPHY: foo plain option:-d
  19137. and derives from it that foo.bib is the bibliography file relevant
  19138. for this document. It then installs the necessary environment for RefTeX
  19139. to work in this buffer and calls `reftex-citation' to insert a citation
  19140. into the buffer.
  19141. Export of such citations to both LaTeX and HTML is handled by the contributed
  19142. package org-exp-bibtex by Taru Karttunen."
  19143. (interactive)
  19144. (let ((reftex-docstruct-symbol 'rds)
  19145. (reftex-cite-format "\\cite{%l}")
  19146. rds bib)
  19147. (save-excursion
  19148. (save-restriction
  19149. (widen)
  19150. (let ((case-fold-search t)
  19151. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  19152. (if (not (save-excursion
  19153. (or (re-search-forward re nil t)
  19154. (re-search-backward re nil t))))
  19155. (error "No bibliography defined in file")
  19156. (setq bib (concat (match-string 1) ".bib")
  19157. rds (list (list 'bib bib)))))))
  19158. (call-interactively 'reftex-citation)))
  19159. ;;;; Functions extending outline functionality
  19160. (defun org-beginning-of-line (&optional arg)
  19161. "Go to the beginning of the current line. If that is invisible, continue
  19162. to a visible line beginning. This makes the function of C-a more intuitive.
  19163. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  19164. first attempt, and only move to after the tags when the cursor is already
  19165. beyond the end of the headline."
  19166. (interactive "P")
  19167. (let ((pos (point))
  19168. (special (if (consp org-special-ctrl-a/e)
  19169. (car org-special-ctrl-a/e)
  19170. org-special-ctrl-a/e))
  19171. refpos)
  19172. (if (org-bound-and-true-p line-move-visual)
  19173. (beginning-of-visual-line 1)
  19174. (beginning-of-line 1))
  19175. (if (and arg (fboundp 'move-beginning-of-line))
  19176. (call-interactively 'move-beginning-of-line)
  19177. (if (bobp)
  19178. nil
  19179. (backward-char 1)
  19180. (if (org-truely-invisible-p)
  19181. (while (and (not (bobp)) (org-truely-invisible-p))
  19182. (backward-char 1)
  19183. (beginning-of-line 1))
  19184. (forward-char 1))))
  19185. (when special
  19186. (cond
  19187. ((and (looking-at org-complex-heading-regexp)
  19188. (= (char-after (match-end 1)) ?\ ))
  19189. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  19190. (point-at-eol)))
  19191. (goto-char
  19192. (if (eq special t)
  19193. (cond ((> pos refpos) refpos)
  19194. ((= pos (point)) refpos)
  19195. (t (point)))
  19196. (cond ((> pos (point)) (point))
  19197. ((not (eq last-command this-command)) (point))
  19198. (t refpos)))))
  19199. ((org-at-item-p)
  19200. ;; Being at an item and not looking at an the item means point
  19201. ;; was previously moved to beginning of a visual line, which
  19202. ;; doesn't contain the item. Therefore, do nothing special,
  19203. ;; just stay here.
  19204. (when (looking-at org-list-full-item-re)
  19205. ;; Set special position at first white space character after
  19206. ;; bullet, and check-box, if any.
  19207. (let ((after-bullet
  19208. (let ((box (match-end 3)))
  19209. (if (not box) (match-end 1)
  19210. (let ((after (char-after box)))
  19211. (if (and after (= after ? )) (1+ box) box))))))
  19212. ;; Special case: Move point to special position when
  19213. ;; currently after it or at beginning of line.
  19214. (if (eq special t)
  19215. (when (or (> pos after-bullet) (= (point) pos))
  19216. (goto-char after-bullet))
  19217. ;; Reversed case: Move point to special position when
  19218. ;; point was already at beginning of line and command is
  19219. ;; repeated.
  19220. (when (and (= (point) pos) (eq last-command this-command))
  19221. (goto-char after-bullet))))))))
  19222. (org-no-warnings
  19223. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  19224. (defun org-end-of-line (&optional arg)
  19225. "Go to the end of the line.
  19226. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  19227. first attempt, and only move to after the tags when the cursor is already
  19228. beyond the end of the headline."
  19229. (interactive "P")
  19230. (let ((special (if (consp org-special-ctrl-a/e)
  19231. (cdr org-special-ctrl-a/e)
  19232. org-special-ctrl-a/e)))
  19233. (cond
  19234. ((or (not special) arg
  19235. (not (or (org-at-heading-p) (org-at-item-p) (org-at-drawer-p))))
  19236. (call-interactively
  19237. (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
  19238. ((fboundp 'move-end-of-line) 'move-end-of-line)
  19239. (t 'end-of-line))))
  19240. ((org-at-heading-p)
  19241. (let ((pos (point)))
  19242. (beginning-of-line 1)
  19243. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  19244. (if (eq special t)
  19245. (if (or (< pos (match-beginning 1))
  19246. (= pos (match-end 0)))
  19247. (goto-char (match-beginning 1))
  19248. (goto-char (match-end 0)))
  19249. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  19250. (goto-char (match-end 0))
  19251. (goto-char (match-beginning 1))))
  19252. (call-interactively (if (fboundp 'move-end-of-line)
  19253. 'move-end-of-line
  19254. 'end-of-line)))))
  19255. ((org-at-drawer-p)
  19256. (move-end-of-line 1)
  19257. (when (overlays-at (1- (point))) (backward-char 1)))
  19258. ;; At an item: Move before any hidden text.
  19259. (t (call-interactively
  19260. (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
  19261. ((fboundp 'move-end-of-line) 'move-end-of-line)
  19262. (t 'end-of-line)))))
  19263. (org-no-warnings
  19264. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  19265. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  19266. (define-key org-mode-map "\C-e" 'org-end-of-line)
  19267. (defun org-backward-sentence (&optional arg)
  19268. "Go to beginning of sentence, or beginning of table field.
  19269. This will call `backward-sentence' or `org-table-beginning-of-field',
  19270. depending on context."
  19271. (interactive "P")
  19272. (cond
  19273. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  19274. (t (call-interactively 'backward-sentence))))
  19275. (defun org-forward-sentence (&optional arg)
  19276. "Go to end of sentence, or end of table field.
  19277. This will call `forward-sentence' or `org-table-end-of-field',
  19278. depending on context."
  19279. (interactive "P")
  19280. (cond
  19281. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  19282. (t (call-interactively 'forward-sentence))))
  19283. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  19284. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  19285. (defun org-kill-line (&optional arg)
  19286. "Kill line, to tags or end of line."
  19287. (interactive "P")
  19288. (cond
  19289. ((or (not org-special-ctrl-k)
  19290. (bolp)
  19291. (not (org-at-heading-p)))
  19292. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  19293. org-ctrl-k-protect-subtree)
  19294. (if (or (eq org-ctrl-k-protect-subtree 'error)
  19295. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  19296. (error "C-k aborted - would kill hidden subtree")))
  19297. (call-interactively
  19298. (if (and (boundp 'visual-line-mode) visual-line-mode) 'kill-visual-line 'kill-line)))
  19299. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  19300. (kill-region (point) (match-beginning 1))
  19301. (org-set-tags nil t))
  19302. (t (kill-region (point) (point-at-eol)))))
  19303. (define-key org-mode-map "\C-k" 'org-kill-line)
  19304. (defun org-yank (&optional arg)
  19305. "Yank. If the kill is a subtree, treat it specially.
  19306. This command will look at the current kill and check if is a single
  19307. subtree, or a series of subtrees[1]. If it passes the test, and if the
  19308. cursor is at the beginning of a line or after the stars of a currently
  19309. empty headline, then the yank is handled specially. How exactly depends
  19310. on the value of the following variables, both set by default.
  19311. org-yank-folded-subtrees
  19312. When set, the subtree(s) will be folded after insertion, but only
  19313. if doing so would now swallow text after the yanked text.
  19314. org-yank-adjusted-subtrees
  19315. When set, the subtree will be promoted or demoted in order to
  19316. fit into the local outline tree structure, which means that the level
  19317. will be adjusted so that it becomes the smaller one of the two
  19318. *visible* surrounding headings.
  19319. Any prefix to this command will cause `yank' to be called directly with
  19320. no special treatment. In particular, a simple \\[universal-argument] prefix \
  19321. will just
  19322. plainly yank the text as it is.
  19323. \[1] The test checks if the first non-white line is a heading
  19324. and if there are no other headings with fewer stars."
  19325. (interactive "P")
  19326. (org-yank-generic 'yank arg))
  19327. (defun org-yank-generic (command arg)
  19328. "Perform some yank-like command.
  19329. This function implements the behavior described in the `org-yank'
  19330. documentation. However, it has been generalized to work for any
  19331. interactive command with similar behavior."
  19332. ;; pretend to be command COMMAND
  19333. (setq this-command command)
  19334. (if arg
  19335. (call-interactively command)
  19336. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  19337. (and (org-kill-is-subtree-p)
  19338. (or (bolp)
  19339. (and (looking-at "[ \t]*$")
  19340. (string-match
  19341. "\\`\\*+\\'"
  19342. (buffer-substring (point-at-bol) (point)))))))
  19343. swallowp)
  19344. (cond
  19345. ((and subtreep org-yank-folded-subtrees)
  19346. (let ((beg (point))
  19347. end)
  19348. (if (and subtreep org-yank-adjusted-subtrees)
  19349. (org-paste-subtree nil nil 'for-yank)
  19350. (call-interactively command))
  19351. (setq end (point))
  19352. (goto-char beg)
  19353. (when (and (bolp) subtreep
  19354. (not (setq swallowp
  19355. (org-yank-folding-would-swallow-text beg end))))
  19356. (org-with-limited-levels
  19357. (or (looking-at org-outline-regexp)
  19358. (re-search-forward org-outline-regexp-bol end t))
  19359. (while (and (< (point) end) (looking-at org-outline-regexp))
  19360. (hide-subtree)
  19361. (org-cycle-show-empty-lines 'folded)
  19362. (condition-case nil
  19363. (outline-forward-same-level 1)
  19364. (error (goto-char end))))))
  19365. (when swallowp
  19366. (message
  19367. "Inserted text not folded because that would swallow text"))
  19368. (goto-char end)
  19369. (skip-chars-forward " \t\n\r")
  19370. (beginning-of-line 1)
  19371. (push-mark beg 'nomsg)))
  19372. ((and subtreep org-yank-adjusted-subtrees)
  19373. (let ((beg (point-at-bol)))
  19374. (org-paste-subtree nil nil 'for-yank)
  19375. (push-mark beg 'nomsg)))
  19376. (t
  19377. (call-interactively command))))))
  19378. (defun org-yank-folding-would-swallow-text (beg end)
  19379. "Would hide-subtree at BEG swallow any text after END?"
  19380. (let (level)
  19381. (org-with-limited-levels
  19382. (save-excursion
  19383. (goto-char beg)
  19384. (when (or (looking-at org-outline-regexp)
  19385. (re-search-forward org-outline-regexp-bol end t))
  19386. (setq level (org-outline-level)))
  19387. (goto-char end)
  19388. (skip-chars-forward " \t\r\n\v\f")
  19389. (if (or (eobp)
  19390. (and (bolp) (looking-at org-outline-regexp)
  19391. (<= (org-outline-level) level)))
  19392. nil ; Nothing would be swallowed
  19393. t))))) ; something would swallow
  19394. (define-key org-mode-map "\C-y" 'org-yank)
  19395. (defun org-truely-invisible-p ()
  19396. "Check if point is at a character currently not visible.
  19397. This version does not only check the character property, but also
  19398. `visible-mode'."
  19399. ;; Early versions of noutline don't have `outline-invisible-p'.
  19400. (if (org-bound-and-true-p visible-mode)
  19401. nil
  19402. (outline-invisible-p)))
  19403. (defun org-invisible-p2 ()
  19404. "Check if point is at a character currently not visible."
  19405. (save-excursion
  19406. (if (and (eolp) (not (bobp))) (backward-char 1))
  19407. ;; Early versions of noutline don't have `outline-invisible-p'.
  19408. (outline-invisible-p)))
  19409. (defun org-back-to-heading (&optional invisible-ok)
  19410. "Call `outline-back-to-heading', but provide a better error message."
  19411. (condition-case nil
  19412. (outline-back-to-heading invisible-ok)
  19413. (error (error "Before first headline at position %d in buffer %s"
  19414. (point) (current-buffer)))))
  19415. (defun org-beginning-of-defun ()
  19416. "Go to the beginning of the subtree, i.e. back to the heading."
  19417. (org-back-to-heading))
  19418. (defun org-end-of-defun ()
  19419. "Go to the end of the subtree."
  19420. (org-end-of-subtree nil t))
  19421. (defun org-before-first-heading-p ()
  19422. "Before first heading?"
  19423. (save-excursion
  19424. (end-of-line)
  19425. (null (re-search-backward org-outline-regexp-bol nil t))))
  19426. (defun org-at-heading-p (&optional ignored)
  19427. (outline-on-heading-p t))
  19428. ;; Compatibility alias with Org versions < 7.8.03
  19429. (defalias 'org-on-heading-p 'org-at-heading-p)
  19430. (defun org-at-drawer-p nil
  19431. "Is cursor at a drawer keyword?"
  19432. (save-excursion
  19433. (move-beginning-of-line 1)
  19434. (looking-at org-drawer-regexp)))
  19435. (defun org-at-block-p nil
  19436. "Is cursor at a block keyword?"
  19437. (save-excursion
  19438. (move-beginning-of-line 1)
  19439. (looking-at org-block-regexp)))
  19440. (defun org-point-at-end-of-empty-headline ()
  19441. "If point is at the end of an empty headline, return t, else nil.
  19442. If the heading only contains a TODO keyword, it is still still considered
  19443. empty."
  19444. (and (looking-at "[ \t]*$")
  19445. (when org-todo-line-regexp
  19446. (save-excursion
  19447. (beginning-of-line 1)
  19448. (let ((case-fold-search nil))
  19449. (looking-at org-todo-line-regexp)
  19450. (string= (match-string 3) ""))))))
  19451. (defun org-at-heading-or-item-p ()
  19452. (or (org-at-heading-p) (org-at-item-p)))
  19453. (defun org-at-target-p ()
  19454. (or (org-in-regexp org-radio-target-regexp)
  19455. (org-in-regexp org-target-regexp)))
  19456. ;; Compatibility alias with Org versions < 7.8.03
  19457. (defalias 'org-on-target-p 'org-at-target-p)
  19458. (defun org-up-heading-all (arg)
  19459. "Move to the heading line of which the present line is a subheading.
  19460. This function considers both visible and invisible heading lines.
  19461. With argument, move up ARG levels."
  19462. (if (fboundp 'outline-up-heading-all)
  19463. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  19464. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  19465. (defun org-up-heading-safe ()
  19466. "Move to the heading line of which the present line is a subheading.
  19467. This version will not throw an error. It will return the level of the
  19468. headline found, or nil if no higher level is found.
  19469. Also, this function will be a lot faster than `outline-up-heading',
  19470. because it relies on stars being the outline starters. This can really
  19471. make a significant difference in outlines with very many siblings."
  19472. (let (start-level re)
  19473. (org-back-to-heading t)
  19474. (setq start-level (funcall outline-level))
  19475. (if (equal start-level 1)
  19476. nil
  19477. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  19478. (if (re-search-backward re nil t)
  19479. (funcall outline-level)))))
  19480. (defun org-first-sibling-p ()
  19481. "Is this heading the first child of its parents?"
  19482. (interactive)
  19483. (let ((re org-outline-regexp-bol)
  19484. level l)
  19485. (unless (org-at-heading-p t)
  19486. (error "Not at a heading"))
  19487. (setq level (funcall outline-level))
  19488. (save-excursion
  19489. (if (not (re-search-backward re nil t))
  19490. t
  19491. (setq l (funcall outline-level))
  19492. (< l level)))))
  19493. (defun org-goto-sibling (&optional previous)
  19494. "Goto the next sibling, even if it is invisible.
  19495. When PREVIOUS is set, go to the previous sibling instead. Returns t
  19496. when a sibling was found. When none is found, return nil and don't
  19497. move point."
  19498. (let ((fun (if previous 're-search-backward 're-search-forward))
  19499. (pos (point))
  19500. (re org-outline-regexp-bol)
  19501. level l)
  19502. (when (condition-case nil (org-back-to-heading t) (error nil))
  19503. (setq level (funcall outline-level))
  19504. (catch 'exit
  19505. (or previous (forward-char 1))
  19506. (while (funcall fun re nil t)
  19507. (setq l (funcall outline-level))
  19508. (when (< l level) (goto-char pos) (throw 'exit nil))
  19509. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  19510. (goto-char pos)
  19511. nil))))
  19512. (defun org-show-siblings ()
  19513. "Show all siblings of the current headline."
  19514. (save-excursion
  19515. (while (org-goto-sibling) (org-flag-heading nil)))
  19516. (save-excursion
  19517. (while (org-goto-sibling 'previous)
  19518. (org-flag-heading nil))))
  19519. (defun org-goto-first-child ()
  19520. "Goto the first child, even if it is invisible.
  19521. Return t when a child was found. Otherwise don't move point and
  19522. return nil."
  19523. (let (level (pos (point)) (re org-outline-regexp-bol))
  19524. (when (condition-case nil (org-back-to-heading t) (error nil))
  19525. (setq level (outline-level))
  19526. (forward-char 1)
  19527. (if (and (re-search-forward re nil t) (> (outline-level) level))
  19528. (progn (goto-char (match-beginning 0)) t)
  19529. (goto-char pos) nil))))
  19530. (defun org-show-hidden-entry ()
  19531. "Show an entry where even the heading is hidden."
  19532. (save-excursion
  19533. (org-show-entry)))
  19534. (defun org-flag-heading (flag &optional entry)
  19535. "Flag the current heading. FLAG non-nil means make invisible.
  19536. When ENTRY is non-nil, show the entire entry."
  19537. (save-excursion
  19538. (org-back-to-heading t)
  19539. ;; Check if we should show the entire entry
  19540. (if entry
  19541. (progn
  19542. (org-show-entry)
  19543. (save-excursion
  19544. (and (outline-next-heading)
  19545. (org-flag-heading nil))))
  19546. (outline-flag-region (max (point-min) (1- (point)))
  19547. (save-excursion (outline-end-of-heading) (point))
  19548. flag))))
  19549. (defun org-get-next-sibling ()
  19550. "Move to next heading of the same level, and return point.
  19551. If there is no such heading, return nil.
  19552. This is like outline-next-sibling, but invisible headings are ok."
  19553. (let ((level (funcall outline-level)))
  19554. (outline-next-heading)
  19555. (while (and (not (eobp)) (> (funcall outline-level) level))
  19556. (outline-next-heading))
  19557. (if (or (eobp) (< (funcall outline-level) level))
  19558. nil
  19559. (point))))
  19560. (defun org-get-last-sibling ()
  19561. "Move to previous heading of the same level, and return point.
  19562. If there is no such heading, return nil."
  19563. (let ((opoint (point))
  19564. (level (funcall outline-level)))
  19565. (outline-previous-heading)
  19566. (when (and (/= (point) opoint) (outline-on-heading-p t))
  19567. (while (and (> (funcall outline-level) level)
  19568. (not (bobp)))
  19569. (outline-previous-heading))
  19570. (if (< (funcall outline-level) level)
  19571. nil
  19572. (point)))))
  19573. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  19574. ;; This contains an exact copy of the original function, but it uses
  19575. ;; `org-back-to-heading', to make it work also in invisible
  19576. ;; trees. And is uses an invisible-OK argument.
  19577. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  19578. ;; Furthermore, when used inside Org, finding the end of a large subtree
  19579. ;; with many children and grandchildren etc, this can be much faster
  19580. ;; than the outline version.
  19581. (org-back-to-heading invisible-OK)
  19582. (let ((first t)
  19583. (level (funcall outline-level)))
  19584. (if (and (derived-mode-p 'org-mode) (< level 1000))
  19585. ;; A true heading (not a plain list item), in Org-mode
  19586. ;; This means we can easily find the end by looking
  19587. ;; only for the right number of stars. Using a regexp to do
  19588. ;; this is so much faster than using a Lisp loop.
  19589. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  19590. (forward-char 1)
  19591. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  19592. ;; something else, do it the slow way
  19593. (while (and (not (eobp))
  19594. (or first (> (funcall outline-level) level)))
  19595. (setq first nil)
  19596. (outline-next-heading)))
  19597. (unless to-heading
  19598. (if (memq (preceding-char) '(?\n ?\^M))
  19599. (progn
  19600. ;; Go to end of line before heading
  19601. (forward-char -1)
  19602. (if (memq (preceding-char) '(?\n ?\^M))
  19603. ;; leave blank line before heading
  19604. (forward-char -1))))))
  19605. (point))
  19606. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  19607. "Use Org version in org-mode, for dramatic speed-up."
  19608. (if (derived-mode-p 'org-mode)
  19609. (progn
  19610. (org-end-of-subtree nil t)
  19611. (unless (eobp) (backward-char 1)))
  19612. ad-do-it))
  19613. (defun org-end-of-meta-data-and-drawers ()
  19614. "Jump to the first text after meta data and drawers in the current entry.
  19615. This will move over empty lines, lines with planning time stamps,
  19616. clocking lines, and drawers."
  19617. (org-back-to-heading t)
  19618. (let ((end (save-excursion (outline-next-heading) (point)))
  19619. (re (concat "\\(" org-drawer-regexp "\\)"
  19620. "\\|" "[ \t]*" org-keyword-time-regexp)))
  19621. (forward-line 1)
  19622. (while (re-search-forward re end t)
  19623. (if (not (match-end 1))
  19624. ;; empty or planning line
  19625. (forward-line 1)
  19626. ;; a drawer, find the end
  19627. (re-search-forward "^[ \t]*:END:" end 'move)
  19628. (forward-line 1)))
  19629. (and (re-search-forward "[^\n]" nil t) (backward-char 1))
  19630. (point)))
  19631. (defun org-forward-same-level (arg &optional invisible-ok)
  19632. "Move forward to the arg'th subheading at same level as this one.
  19633. Stop at the first and last subheadings of a superior heading.
  19634. Normally this only looks at visible headings, but when INVISIBLE-OK is non-nil
  19635. it wil also look at invisible ones."
  19636. (interactive "p")
  19637. (org-back-to-heading invisible-ok)
  19638. (org-at-heading-p)
  19639. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  19640. (re (format "^\\*\\{1,%d\\} " level))
  19641. l)
  19642. (forward-char 1)
  19643. (while (> arg 0)
  19644. (while (and (re-search-forward re nil 'move)
  19645. (setq l (- (match-end 0) (match-beginning 0) 1))
  19646. (= l level)
  19647. (not invisible-ok)
  19648. (progn (backward-char 1) (outline-invisible-p)))
  19649. (if (< l level) (setq arg 1)))
  19650. (setq arg (1- arg)))
  19651. (beginning-of-line 1)))
  19652. (defun org-backward-same-level (arg &optional invisible-ok)
  19653. "Move backward to the arg'th subheading at same level as this one.
  19654. Stop at the first and last subheadings of a superior heading."
  19655. (interactive "p")
  19656. (org-back-to-heading)
  19657. (org-at-heading-p)
  19658. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  19659. (re (format "^\\*\\{1,%d\\} " level))
  19660. l)
  19661. (while (> arg 0)
  19662. (while (and (re-search-backward re nil 'move)
  19663. (setq l (- (match-end 0) (match-beginning 0) 1))
  19664. (= l level)
  19665. (not invisible-ok)
  19666. (outline-invisible-p))
  19667. (if (< l level) (setq arg 1)))
  19668. (setq arg (1- arg)))))
  19669. (defun org-show-subtree ()
  19670. "Show everything after this heading at deeper levels."
  19671. (interactive)
  19672. (outline-flag-region
  19673. (point)
  19674. (save-excursion
  19675. (org-end-of-subtree t t))
  19676. nil))
  19677. (defun org-show-entry ()
  19678. "Show the body directly following this heading.
  19679. Show the heading too, if it is currently invisible."
  19680. (interactive)
  19681. (save-excursion
  19682. (condition-case nil
  19683. (progn
  19684. (org-back-to-heading t)
  19685. (outline-flag-region
  19686. (max (point-min) (1- (point)))
  19687. (save-excursion
  19688. (if (re-search-forward
  19689. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  19690. (match-beginning 1)
  19691. (point-max)))
  19692. nil)
  19693. (org-cycle-hide-drawers 'children))
  19694. (error nil))))
  19695. (defun org-make-options-regexp (kwds &optional extra)
  19696. "Make a regular expression for keyword lines."
  19697. (concat
  19698. "^"
  19699. "#?[ \t]*\\+\\("
  19700. (mapconcat 'regexp-quote kwds "\\|")
  19701. (if extra (concat "\\|" extra))
  19702. "\\):[ \t]*"
  19703. "\\(.*\\)"))
  19704. ;; Make isearch reveal the necessary context
  19705. (defun org-isearch-end ()
  19706. "Reveal context after isearch exits."
  19707. (when isearch-success ; only if search was successful
  19708. (if (featurep 'xemacs)
  19709. ;; Under XEmacs, the hook is run in the correct place,
  19710. ;; we directly show the context.
  19711. (org-show-context 'isearch)
  19712. ;; In Emacs the hook runs *before* restoring the overlays.
  19713. ;; So we have to use a one-time post-command-hook to do this.
  19714. ;; (Emacs 22 has a special variable, see function `org-mode')
  19715. (unless (and (boundp 'isearch-mode-end-hook-quit)
  19716. isearch-mode-end-hook-quit)
  19717. ;; Only when the isearch was not quitted.
  19718. (org-add-hook 'post-command-hook 'org-isearch-post-command
  19719. 'append 'local)))))
  19720. (defun org-isearch-post-command ()
  19721. "Remove self from hook, and show context."
  19722. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  19723. (org-show-context 'isearch))
  19724. ;;;; Integration with and fixes for other packages
  19725. ;;; Imenu support
  19726. (defvar org-imenu-markers nil
  19727. "All markers currently used by Imenu.")
  19728. (make-variable-buffer-local 'org-imenu-markers)
  19729. (defun org-imenu-new-marker (&optional pos)
  19730. "Return a new marker for use by Imenu, and remember the marker."
  19731. (let ((m (make-marker)))
  19732. (move-marker m (or pos (point)))
  19733. (push m org-imenu-markers)
  19734. m))
  19735. (defun org-imenu-get-tree ()
  19736. "Produce the index for Imenu."
  19737. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  19738. (setq org-imenu-markers nil)
  19739. (let* ((n org-imenu-depth)
  19740. (re (concat "^" (org-get-limited-outline-regexp)))
  19741. (subs (make-vector (1+ n) nil))
  19742. (last-level 0)
  19743. m level head)
  19744. (save-excursion
  19745. (save-restriction
  19746. (widen)
  19747. (goto-char (point-max))
  19748. (while (re-search-backward re nil t)
  19749. (setq level (org-reduced-level (funcall outline-level)))
  19750. (when (and (<= level n)
  19751. (looking-at org-complex-heading-regexp))
  19752. (setq head (org-link-display-format
  19753. (org-match-string-no-properties 4))
  19754. m (org-imenu-new-marker))
  19755. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  19756. (if (>= level last-level)
  19757. (push (cons head m) (aref subs level))
  19758. (push (cons head (aref subs (1+ level))) (aref subs level))
  19759. (loop for i from (1+ level) to n do (aset subs i nil)))
  19760. (setq last-level level)))))
  19761. (aref subs 1)))
  19762. (eval-after-load "imenu"
  19763. '(progn
  19764. (add-hook 'imenu-after-jump-hook
  19765. (lambda ()
  19766. (if (derived-mode-p 'org-mode)
  19767. (org-show-context 'org-goto))))))
  19768. (defun org-link-display-format (link)
  19769. "Replace a link with either the description, or the link target
  19770. if no description is present"
  19771. (save-match-data
  19772. (if (string-match org-bracket-link-analytic-regexp link)
  19773. (replace-match (if (match-end 5)
  19774. (match-string 5 link)
  19775. (concat (match-string 1 link)
  19776. (match-string 3 link)))
  19777. nil t link)
  19778. link)))
  19779. (defun org-toggle-link-display ()
  19780. "Toggle the literal or descriptive display of links."
  19781. (interactive)
  19782. (if org-descriptive-links
  19783. (progn (org-remove-from-invisibility-spec '(org-link))
  19784. (org-restart-font-lock)
  19785. (setq org-descriptive-links nil))
  19786. (progn (add-to-invisibility-spec '(org-link))
  19787. (org-restart-font-lock)
  19788. (setq org-descriptive-links t))))
  19789. ;; Speedbar support
  19790. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  19791. "Overlay marking the agenda restriction line in speedbar.")
  19792. (overlay-put org-speedbar-restriction-lock-overlay
  19793. 'face 'org-agenda-restriction-lock)
  19794. (overlay-put org-speedbar-restriction-lock-overlay
  19795. 'help-echo "Agendas are currently limited to this item.")
  19796. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  19797. (defun org-speedbar-set-agenda-restriction ()
  19798. "Restrict future agenda commands to the location at point in speedbar.
  19799. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  19800. (interactive)
  19801. (require 'org-agenda)
  19802. (let (p m tp np dir txt)
  19803. (cond
  19804. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  19805. 'org-imenu t))
  19806. (setq m (get-text-property p 'org-imenu-marker))
  19807. (with-current-buffer (marker-buffer m)
  19808. (goto-char m)
  19809. (org-agenda-set-restriction-lock 'subtree)))
  19810. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  19811. 'speedbar-function 'speedbar-find-file))
  19812. (setq tp (previous-single-property-change
  19813. (1+ p) 'speedbar-function)
  19814. np (next-single-property-change
  19815. tp 'speedbar-function)
  19816. dir (speedbar-line-directory)
  19817. txt (buffer-substring-no-properties (or tp (point-min))
  19818. (or np (point-max))))
  19819. (with-current-buffer (find-file-noselect
  19820. (let ((default-directory dir))
  19821. (expand-file-name txt)))
  19822. (unless (derived-mode-p 'org-mode)
  19823. (error "Cannot restrict to non-Org-mode file"))
  19824. (org-agenda-set-restriction-lock 'file)))
  19825. (t (error "Don't know how to restrict Org-mode's agenda")))
  19826. (move-overlay org-speedbar-restriction-lock-overlay
  19827. (point-at-bol) (point-at-eol))
  19828. (setq current-prefix-arg nil)
  19829. (org-agenda-maybe-redo)))
  19830. (eval-after-load "speedbar"
  19831. '(progn
  19832. (speedbar-add-supported-extension ".org")
  19833. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  19834. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  19835. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  19836. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  19837. (add-hook 'speedbar-visiting-tag-hook
  19838. (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
  19839. ;;; Fixes and Hacks for problems with other packages
  19840. ;; Make flyspell not check words in links, to not mess up our keymap
  19841. (defun org-mode-flyspell-verify ()
  19842. "Don't let flyspell put overlays at active buttons, or on
  19843. {todo,all-time,additional-option-like}-keywords."
  19844. (let ((pos (max (1- (point)) (point-min)))
  19845. (word (thing-at-point 'word)))
  19846. (and (not (get-text-property pos 'keymap))
  19847. (not (get-text-property pos 'org-no-flyspell))
  19848. (not (member word org-todo-keywords-1))
  19849. (not (member word org-all-time-keywords))
  19850. (not (member word org-additional-option-like-keywords)))))
  19851. (defun org-remove-flyspell-overlays-in (beg end)
  19852. "Remove flyspell overlays in region."
  19853. (and (org-bound-and-true-p flyspell-mode)
  19854. (fboundp 'flyspell-delete-region-overlays)
  19855. (flyspell-delete-region-overlays beg end))
  19856. (add-text-properties beg end '(org-no-flyspell t)))
  19857. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  19858. (eval-after-load "bookmark"
  19859. '(if (boundp 'bookmark-after-jump-hook)
  19860. ;; We can use the hook
  19861. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  19862. ;; Hook not available, use advice
  19863. (defadvice bookmark-jump (after org-make-visible activate)
  19864. "Make the position visible."
  19865. (org-bookmark-jump-unhide))))
  19866. ;; Make sure saveplace shows the location if it was hidden
  19867. (eval-after-load "saveplace"
  19868. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  19869. "Make the position visible."
  19870. (org-bookmark-jump-unhide)))
  19871. ;; Make sure ecb shows the location if it was hidden
  19872. (eval-after-load "ecb"
  19873. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  19874. "Make hierarchy visible when jumping into location from ECB tree buffer."
  19875. (if (derived-mode-p 'org-mode)
  19876. (org-show-context))))
  19877. (defun org-bookmark-jump-unhide ()
  19878. "Unhide the current position, to show the bookmark location."
  19879. (and (derived-mode-p 'org-mode)
  19880. (or (outline-invisible-p)
  19881. (save-excursion (goto-char (max (point-min) (1- (point))))
  19882. (outline-invisible-p)))
  19883. (org-show-context 'bookmark-jump)))
  19884. ;; Make session.el ignore our circular variable
  19885. (eval-after-load "session"
  19886. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  19887. ;;;; Experimental code
  19888. (defun org-closed-in-range ()
  19889. "Sparse tree of items closed in a certain time range.
  19890. Still experimental, may disappear in the future."
  19891. (interactive)
  19892. ;; Get the time interval from the user.
  19893. (let* ((time1 (org-float-time
  19894. (org-read-date nil 'to-time nil "Starting date: ")))
  19895. (time2 (org-float-time
  19896. (org-read-date nil 'to-time nil "End date:")))
  19897. ;; callback function
  19898. (callback (lambda ()
  19899. (let ((time
  19900. (org-float-time
  19901. (apply 'encode-time
  19902. (org-parse-time-string
  19903. (match-string 1))))))
  19904. ;; check if time in interval
  19905. (and (>= time time1) (<= time time2))))))
  19906. ;; make tree, check each match with the callback
  19907. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  19908. ;;;; Finish up
  19909. (provide 'org)
  19910. (run-hooks 'org-load-hook)
  19911. ;;; org.el ends here