org.el 880 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239
  1. ;;; org.el --- Outline-based notes management and organizer
  2. ;; Carstens outline-mode for keeping track of everything.
  3. ;; Copyright (C) 2004-2013 Free Software Foundation, Inc.
  4. ;;
  5. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  6. ;; Maintainer: Bastien Guerry <bzg at gnu dot org>
  7. ;; Keywords: outlines, hypermedia, calendar, wp
  8. ;; Homepage: http://orgmode.org
  9. ;;
  10. ;; This file is part of GNU Emacs.
  11. ;;
  12. ;; GNU Emacs is free software: you can redistribute it and/or modify
  13. ;; it under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation, either version 3 of the License, or
  15. ;; (at your option) any later version.
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;; GNU General Public License for more details.
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  22. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  23. ;;
  24. ;;; Commentary:
  25. ;;
  26. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  27. ;; project planning with a fast and effective plain-text system.
  28. ;;
  29. ;; Org-mode develops organizational tasks around NOTES files that contain
  30. ;; information about projects as plain text. Org-mode is implemented on
  31. ;; top of outline-mode, which makes it possible to keep the content of
  32. ;; large files well structured. Visibility cycling and structure editing
  33. ;; help to work with the tree. Tables are easily created with a built-in
  34. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  35. ;; and scheduling. It dynamically compiles entries into an agenda that
  36. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  37. ;; Plain text URL-like links connect to websites, emails, Usenet
  38. ;; messages, BBDB entries, and any files related to the projects. For
  39. ;; printing and sharing of notes, an Org-mode file can be exported as a
  40. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  41. ;; iCalendar file. It can also serve as a publishing tool for a set of
  42. ;; linked webpages.
  43. ;;
  44. ;; Installation and Activation
  45. ;; ---------------------------
  46. ;; See the corresponding sections in the manual at
  47. ;;
  48. ;; http://orgmode.org/org.html#Installation
  49. ;;
  50. ;; Documentation
  51. ;; -------------
  52. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  53. ;; distribution also contains a PDF version of it. At the homepage of
  54. ;; Org-mode, you can read the same text online as HTML. There is also an
  55. ;; excellent reference card made by Philip Rooke. This card can be found
  56. ;; in the etc/ directory of Emacs 22.
  57. ;;
  58. ;; A list of recent changes can be found at
  59. ;; http://orgmode.org/Changes.html
  60. ;;
  61. ;;; Code:
  62. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  63. (defvar org-table-formula-constants-local nil
  64. "Local version of `org-table-formula-constants'.")
  65. (make-variable-buffer-local 'org-table-formula-constants-local)
  66. ;;;; Require other packages
  67. (eval-when-compile
  68. (require 'cl)
  69. (require 'gnus-sum))
  70. (require 'calendar)
  71. (require 'find-func)
  72. (require 'format-spec)
  73. (load "org-loaddefs.el" t t t)
  74. (require 'org-macs)
  75. (require 'org-compat)
  76. ;; `org-outline-regexp' ought to be a defconst but is let-binding in
  77. ;; some places -- e.g. see the macro `org-with-limited-levels'.
  78. ;;
  79. ;; In Org buffers, the value of `outline-regexp' is that of
  80. ;; `org-outline-regexp'. The only function still directly relying on
  81. ;; `outline-regexp' is `org-overview' so that `org-cycle' can do its
  82. ;; job when `orgstruct-mode' is active.
  83. (defvar org-outline-regexp "\\*+ "
  84. "Regexp to match Org headlines.")
  85. (defvar org-outline-regexp-bol "^\\*+ "
  86. "Regexp to match Org headlines.
  87. This is similar to `org-outline-regexp' but additionally makes
  88. sure that we are at the beginning of the line.")
  89. (defvar org-heading-regexp "^\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  90. "Matches a headline, putting stars and text into groups.
  91. Stars are put in group 1 and the trimmed body in group 2.")
  92. ;; Emacs 22 calendar compatibility: Make sure the new variables are available
  93. (when (fboundp 'defvaralias)
  94. (unless (boundp 'calendar-view-holidays-initially-flag)
  95. (defvaralias 'calendar-view-holidays-initially-flag
  96. 'view-calendar-holidays-initially))
  97. (unless (boundp 'calendar-view-diary-initially-flag)
  98. (defvaralias 'calendar-view-diary-initially-flag
  99. 'view-diary-entries-initially))
  100. (unless (boundp 'diary-fancy-buffer)
  101. (defvaralias 'diary-fancy-buffer 'fancy-diary-buffer)))
  102. (declare-function org-inlinetask-at-task-p "org-inlinetask" ())
  103. (declare-function org-inlinetask-outline-regexp "org-inlinetask" ())
  104. (declare-function org-inlinetask-toggle-visibility "org-inlinetask" ())
  105. (declare-function org-pop-to-buffer-same-window "org-compat" (&optional buffer-or-name norecord label))
  106. (declare-function org-clock-get-last-clock-out-time "org-clock" ())
  107. (declare-function org-clock-timestamps-up "org-clock" (&optional n))
  108. (declare-function org-clock-timestamps-down "org-clock" (&optional n))
  109. (declare-function org-clock-sum-current-item "org-clock" (&optional tstart))
  110. (declare-function orgtbl-mode "org-table" (&optional arg))
  111. (declare-function org-clock-out "org-clock" (&optional switch-to-state fail-quietly at-time))
  112. (declare-function org-beamer-mode "ox-beamer" ())
  113. (declare-function org-table-edit-field "org-table" (arg))
  114. (declare-function org-table-justify-field-maybe "org-table" (&optional new))
  115. (declare-function org-id-get-create "org-id" (&optional force))
  116. (declare-function org-id-find-id-file "org-id" (id))
  117. (declare-function org-tags-view "org-agenda" (&optional todo-only match))
  118. (declare-function org-agenda-list "org-agenda" (&optional arg start-day span))
  119. (declare-function org-table-align "org-table" ())
  120. (declare-function org-table-paste-rectangle "org-table" ())
  121. (declare-function org-table-maybe-eval-formula "org-table" ())
  122. (declare-function org-table-maybe-recalculate-line "org-table" ())
  123. (declare-function org-element--parse-objects "org-element"
  124. (beg end acc restriction))
  125. (declare-function org-element-at-point "org-element" (&optional keep-trail))
  126. (declare-function org-element-contents "org-element" (element))
  127. (declare-function org-element-context "org-element" (&optional element))
  128. (declare-function org-element-interpret-data "org-element"
  129. (data &optional parent))
  130. (declare-function org-element-map "org-element"
  131. (data types fun &optional info first-match no-recursion))
  132. (declare-function org-element-nested-p "org-element" (elem-a elem-b))
  133. (declare-function org-element-parse-buffer "org-element"
  134. (&optional granularity visible-only))
  135. (declare-function org-element-property "org-element" (property element))
  136. (declare-function org-element-put-property "org-element"
  137. (element property value))
  138. (declare-function org-element-swap-A-B "org-element" (elem-a elem-b))
  139. (declare-function org-element--parse-objects "org-element"
  140. (beg end acc restriction))
  141. (declare-function org-element-parse-buffer "org-element"
  142. (&optional granularity visible-only))
  143. (declare-function org-element-restriction "org-element" (element))
  144. (declare-function org-element-type "org-element" (element))
  145. ;; load languages based on value of `org-babel-load-languages'
  146. (defvar org-babel-load-languages)
  147. ;;;###autoload
  148. (defun org-babel-do-load-languages (sym value)
  149. "Load the languages defined in `org-babel-load-languages'."
  150. (set-default sym value)
  151. (mapc (lambda (pair)
  152. (let ((active (cdr pair)) (lang (symbol-name (car pair))))
  153. (if active
  154. (progn
  155. (require (intern (concat "ob-" lang))))
  156. (progn
  157. (funcall 'fmakunbound
  158. (intern (concat "org-babel-execute:" lang)))
  159. (funcall 'fmakunbound
  160. (intern (concat "org-babel-expand-body:" lang)))))))
  161. org-babel-load-languages))
  162. (defcustom org-babel-load-languages '((emacs-lisp . t))
  163. "Languages which can be evaluated in Org-mode buffers.
  164. This list can be used to load support for any of the languages
  165. below, note that each language will depend on a different set of
  166. system executables and/or Emacs modes. When a language is
  167. \"loaded\", then code blocks in that language can be evaluated
  168. with `org-babel-execute-src-block' bound by default to C-c
  169. C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
  170. be set to remove code block evaluation from the C-c C-c
  171. keybinding. By default only Emacs Lisp (which has no
  172. requirements) is loaded."
  173. :group 'org-babel
  174. :set 'org-babel-do-load-languages
  175. :version "24.1"
  176. :type '(alist :tag "Babel Languages"
  177. :key-type
  178. (choice
  179. (const :tag "Awk" awk)
  180. (const :tag "C" C)
  181. (const :tag "R" R)
  182. (const :tag "Asymptote" asymptote)
  183. (const :tag "Calc" calc)
  184. (const :tag "Clojure" clojure)
  185. (const :tag "CSS" css)
  186. (const :tag "Ditaa" ditaa)
  187. (const :tag "Dot" dot)
  188. (const :tag "Emacs Lisp" emacs-lisp)
  189. (const :tag "Fortran" fortran)
  190. (const :tag "Gnuplot" gnuplot)
  191. (const :tag "Haskell" haskell)
  192. (const :tag "IO" io)
  193. (const :tag "Java" java)
  194. (const :tag "Javascript" js)
  195. (const :tag "LaTeX" latex)
  196. (const :tag "Ledger" ledger)
  197. (const :tag "Lilypond" lilypond)
  198. (const :tag "Lisp" lisp)
  199. (const :tag "Makefile" makefile)
  200. (const :tag "Maxima" maxima)
  201. (const :tag "Matlab" matlab)
  202. (const :tag "Mscgen" mscgen)
  203. (const :tag "Ocaml" ocaml)
  204. (const :tag "Octave" octave)
  205. (const :tag "Org" org)
  206. (const :tag "Perl" perl)
  207. (const :tag "Pico Lisp" picolisp)
  208. (const :tag "PlantUML" plantuml)
  209. (const :tag "Python" python)
  210. (const :tag "Ruby" ruby)
  211. (const :tag "Sass" sass)
  212. (const :tag "Scala" scala)
  213. (const :tag "Scheme" scheme)
  214. (const :tag "Screen" screen)
  215. (const :tag "Shell Script" sh)
  216. (const :tag "Shen" shen)
  217. (const :tag "Sql" sql)
  218. (const :tag "Sqlite" sqlite))
  219. :value-type (boolean :tag "Activate" :value t)))
  220. ;;;; Customization variables
  221. (defcustom org-clone-delete-id nil
  222. "Remove ID property of clones of a subtree.
  223. When non-nil, clones of a subtree don't inherit the ID property.
  224. Otherwise they inherit the ID property with a new unique
  225. identifier."
  226. :type 'boolean
  227. :version "24.1"
  228. :group 'org-id)
  229. ;;; Version
  230. (org-check-version)
  231. ;;;###autoload
  232. (defun org-version (&optional here full message)
  233. "Show the org-mode version in the echo area.
  234. With prefix argument HERE, insert it at point.
  235. When FULL is non-nil, use a verbose version string.
  236. When MESSAGE is non-nil, display a message with the version."
  237. (interactive "P")
  238. (let* ((org-dir (ignore-errors (org-find-library-dir "org")))
  239. (save-load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  240. (load-suffixes (list ".el"))
  241. (org-install-dir (ignore-errors (org-find-library-dir "org-loaddefs")))
  242. (org-trash (or
  243. (and (fboundp 'org-release) (fboundp 'org-git-version))
  244. (org-load-noerror-mustsuffix (concat org-dir "org-version"))))
  245. (load-suffixes save-load-suffixes)
  246. (org-version (org-release))
  247. (git-version (org-git-version))
  248. (version (format "Org-mode version %s (%s @ %s)"
  249. org-version
  250. git-version
  251. (if org-install-dir
  252. (if (string= org-dir org-install-dir)
  253. org-install-dir
  254. (concat "mixed installation! " org-install-dir " and " org-dir))
  255. "org-loaddefs.el can not be found!")))
  256. (_version (if full version org-version)))
  257. (if (org-called-interactively-p 'interactive)
  258. (if here
  259. (insert version)
  260. (message version))
  261. (if message (message _version))
  262. _version)))
  263. (defconst org-version (org-version))
  264. ;;; Compatibility constants
  265. ;;; The custom variables
  266. (defgroup org nil
  267. "Outline-based notes management and organizer."
  268. :tag "Org"
  269. :group 'outlines
  270. :group 'calendar)
  271. (defcustom org-mode-hook nil
  272. "Mode hook for Org-mode, run after the mode was turned on."
  273. :group 'org
  274. :type 'hook)
  275. (defcustom org-load-hook nil
  276. "Hook that is run after org.el has been loaded."
  277. :group 'org
  278. :type 'hook)
  279. (defcustom org-log-buffer-setup-hook nil
  280. "Hook that is run after an Org log buffer is created."
  281. :group 'org
  282. :version "24.1"
  283. :type 'hook)
  284. (defvar org-modules) ; defined below
  285. (defvar org-modules-loaded nil
  286. "Have the modules been loaded already?")
  287. (defun org-load-modules-maybe (&optional force)
  288. "Load all extensions listed in `org-modules'."
  289. (when (or force (not org-modules-loaded))
  290. (mapc (lambda (ext)
  291. (condition-case nil (require ext)
  292. (error (message "Problems while trying to load feature `%s'" ext))))
  293. org-modules)
  294. (setq org-modules-loaded t)))
  295. (defun org-set-modules (var value)
  296. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  297. (set var value)
  298. (when (featurep 'org)
  299. (org-load-modules-maybe 'force)))
  300. (defcustom org-modules '(org-bbdb org-bibtex org-docview org-gnus org-info org-irc org-mhe org-rmail)
  301. "Modules that should always be loaded together with org.el.
  302. If a description starts with <C>, the file is not part of Emacs
  303. and loading it will require that you have downloaded and properly
  304. installed the Org mode distribution.
  305. You can also use this system to load external packages (i.e. neither Org
  306. core modules, nor modules from the CONTRIB directory). Just add symbols
  307. to the end of the list. If the package is called org-xyz.el, then you need
  308. to add the symbol `xyz', and the package must have a call to:
  309. \(provide 'org-xyz)
  310. For export specific modules, see also `org-export-backends'."
  311. :group 'org
  312. :set 'org-set-modules
  313. :version "24.4"
  314. :package-version '(Org . "8.0")
  315. :type
  316. '(set :greedy t
  317. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  318. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  319. (const :tag " crypt: Encryption of subtrees" org-crypt)
  320. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  321. (const :tag " docview: Links to doc-view buffers" org-docview)
  322. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  323. (const :tag " id: Global IDs for identifying entries" org-id)
  324. (const :tag " info: Links to Info nodes" org-info)
  325. (const :tag " habit: Track your consistency with habits" org-habit)
  326. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  327. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  328. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  329. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  330. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  331. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  332. (const :tag " mouse: Additional mouse support" org-mouse)
  333. (const :tag "C vm: Links to VM folders/messages" org-vm)
  334. (const :tag "C wl: Links to Wanderlust folders/messages" org-wl)
  335. (const :tag "C w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  336. (const :tag "C mew: Links to Mew folders/messages" org-mew)
  337. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  338. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  339. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  340. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  341. (const :tag "C collector: Collect properties into tables" org-collector)
  342. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  343. (const :tag "C drill: Flashcards and spaced repetition for Org-mode" org-drill)
  344. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  345. (const :tag "C eshell Support for links to working directories in eshell" org-eshell)
  346. (const :tag "C eval: Include command output as text" org-eval)
  347. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  348. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  349. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  350. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  351. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  352. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  353. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  354. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  355. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  356. (const :tag "C notmuch: Provide org links to notmuch searches or messages" org-notmuch)
  357. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  358. (const :tag "C mac-link-grabber Grab links and URLs from various Mac applications" org-mac-link-grabber)
  359. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  360. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  361. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  362. (const :tag "C registry: A registry for Org-mode links" org-registry)
  363. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  364. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  365. (const :tag "C secretary: Team management with org-mode" org-secretary)
  366. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  367. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  368. (const :tag "C track: Keep up with Org-mode development" org-track)
  369. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  370. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  371. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  372. (defvar org-export-registered-backends) ; From ox.el
  373. (declare-function org-export-derived-backend-p "ox" (backend &rest backends))
  374. (defcustom org-export-backends '(ascii html icalendar latex)
  375. "List of export back-ends that should be always available.
  376. If a description starts with <C>, the file is not part of Emacs
  377. and loading it will require that you have downloaded and properly
  378. installed the Org mode distribution.
  379. Unlike to `org-modules', libraries in this list will not be
  380. loaded along with Org, but only once the export framework is
  381. needed.
  382. This variable needs to be set before org.el is loaded. If you
  383. need to make a change while Emacs is running, use the customize
  384. interface or run the following code, where VALUE stands for the
  385. new value of the variable, after updating it:
  386. \(progn
  387. \(setq org-export-registered-backends
  388. \(org-remove-if-not
  389. \(lambda (backend)
  390. \(or (memq backend val)
  391. \(catch 'parentp
  392. \(mapc
  393. \(lambda (b)
  394. \(and (org-export-derived-backend-p b (car backend))
  395. \(throw 'parentp t)))
  396. val)
  397. nil)))
  398. org-export-registered-backends))
  399. \(let ((new-list (mapcar 'car org-export-registered-backends)))
  400. \(dolist (backend val)
  401. \(cond
  402. \((not (load (format \"ox-%s\" backend) t t))
  403. \(message \"Problems while trying to load export back-end `%s'\"
  404. backend))
  405. \((not (memq backend new-list)) (push backend new-list))))
  406. \(set-default var new-list)))
  407. Adding a back-end to this list will also pull the back-end it
  408. depends on, if any."
  409. :group 'org
  410. :group 'org-export
  411. :version "24.4"
  412. :package-version '(Org . "8.0")
  413. :initialize 'custom-initialize-set
  414. :set (lambda (var val)
  415. (if (not (featurep 'ox)) (set-default var val)
  416. ;; Any back-end not required anymore (not present in VAL and not
  417. ;; a parent of any back-end in the new value) is removed from the
  418. ;; list of registered back-ends.
  419. (setq org-export-registered-backends
  420. (org-remove-if-not
  421. (lambda (backend)
  422. (or (memq backend val)
  423. (catch 'parentp
  424. (mapc
  425. (lambda (b)
  426. (and (org-export-derived-backend-p b (car backend))
  427. (throw 'parentp t)))
  428. val)
  429. nil)))
  430. org-export-registered-backends))
  431. ;; Now build NEW-LIST of both new back-ends and required
  432. ;; parents.
  433. (let ((new-list (mapcar 'car org-export-registered-backends)))
  434. (dolist (backend val)
  435. (cond
  436. ((not (load (format "ox-%s" backend) t t))
  437. (message "Problems while trying to load export back-end `%s'"
  438. backend))
  439. ((not (memq backend new-list)) (push backend new-list))))
  440. ;; Set VAR to that list with fixed dependencies.
  441. (set-default var new-list))))
  442. :type '(set :greedy t
  443. (const :tag " ascii Export buffer to ASCII format" ascii)
  444. (const :tag " beamer Export buffer to Beamer presentation" beamer)
  445. (const :tag " html Export buffer to HTML format" html)
  446. (const :tag " icalendar Export buffer to iCalendar format" icalendar)
  447. (const :tag " latex Export buffer to LaTeX format" latex)
  448. (const :tag " man Export buffer to MAN format" man)
  449. (const :tag " md Export buffer to Markdown format" md)
  450. (const :tag " odt Export buffer to ODT format" odt)
  451. (const :tag " texinfo Export buffer to Texinfo format" texinfo)
  452. (const :tag "C confluence Export buffer to Confluence Wiki format" confluence)
  453. (const :tag "C freemind Export buffer to Freemind mindmap format" freemind)
  454. (const :tag "C groff Export buffer to Groff format" groff)
  455. (const :tag "C koma-letter Export buffer to KOMA Scrlttrl2 format" koma-letter)
  456. (const :tag "C taskjuggler Export buffer to TaskJuggler format" taskjuggler)))
  457. (eval-after-load 'ox
  458. '(mapc
  459. (lambda (backend)
  460. (condition-case nil (require (intern (format "ox-%s" backend)))
  461. (error (message "Problems while trying to load export back-end `%s'"
  462. backend))))
  463. org-export-backends))
  464. (defcustom org-support-shift-select nil
  465. "Non-nil means make shift-cursor commands select text when possible.
  466. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
  467. start selecting a region, or enlarge regions started in this way.
  468. In Org-mode, in special contexts, these same keys are used for
  469. other purposes, important enough to compete with shift selection.
  470. Org tries to balance these needs by supporting `shift-select-mode'
  471. outside these special contexts, under control of this variable.
  472. The default of this variable is nil, to avoid confusing behavior. Shifted
  473. cursor keys will then execute Org commands in the following contexts:
  474. - on a headline, changing TODO state (left/right) and priority (up/down)
  475. - on a time stamp, changing the time
  476. - in a plain list item, changing the bullet type
  477. - in a property definition line, switching between allowed values
  478. - in the BEGIN line of a clock table (changing the time block).
  479. Outside these contexts, the commands will throw an error.
  480. When this variable is t and the cursor is not in a special
  481. context, Org-mode will support shift-selection for making and
  482. enlarging regions. To make this more effective, the bullet
  483. cycling will no longer happen anywhere in an item line, but only
  484. if the cursor is exactly on the bullet.
  485. If you set this variable to the symbol `always', then the keys
  486. will not be special in headlines, property lines, and item lines,
  487. to make shift selection work there as well. If this is what you
  488. want, you can use the following alternative commands: `C-c C-t'
  489. and `C-c ,' to change TODO state and priority, `C-u C-u C-c C-t'
  490. can be used to switch TODO sets, `C-c -' to cycle item bullet
  491. types, and properties can be edited by hand or in column view.
  492. However, when the cursor is on a timestamp, shift-cursor commands
  493. will still edit the time stamp - this is just too good to give up.
  494. XEmacs user should have this variable set to nil, because
  495. `shift-select-mode' is in Emacs 23 or later only."
  496. :group 'org
  497. :type '(choice
  498. (const :tag "Never" nil)
  499. (const :tag "When outside special context" t)
  500. (const :tag "Everywhere except timestamps" always)))
  501. (defcustom org-loop-over-headlines-in-active-region nil
  502. "Shall some commands act upon headlines in the active region?
  503. When set to `t', some commands will be performed in all headlines
  504. within the active region.
  505. When set to `start-level', some commands will be performed in all
  506. headlines within the active region, provided that these headlines
  507. are of the same level than the first one.
  508. When set to a string, those commands will be performed on the
  509. matching headlines within the active region. Such string must be
  510. a tags/property/todo match as it is used in the agenda tags view.
  511. The list of commands is: `org-schedule', `org-deadline',
  512. `org-todo', `org-archive-subtree', `org-archive-set-tag' and
  513. `org-archive-to-archive-sibling'. The archiving commands skip
  514. already archived entries."
  515. :type '(choice (const :tag "Don't loop" nil)
  516. (const :tag "All headlines in active region" t)
  517. (const :tag "In active region, headlines at the same level than the first one" 'start-level)
  518. (string :tag "Tags/Property/Todo matcher"))
  519. :version "24.1"
  520. :group 'org-todo
  521. :group 'org-archive)
  522. (defgroup org-startup nil
  523. "Options concerning startup of Org-mode."
  524. :tag "Org Startup"
  525. :group 'org)
  526. (defcustom org-startup-folded t
  527. "Non-nil means entering Org-mode will switch to OVERVIEW.
  528. This can also be configured on a per-file basis by adding one of
  529. the following lines anywhere in the buffer:
  530. #+STARTUP: fold (or `overview', this is equivalent)
  531. #+STARTUP: nofold (or `showall', this is equivalent)
  532. #+STARTUP: content
  533. #+STARTUP: showeverything
  534. By default, this option is ignored when Org opens agenda files
  535. for the first time. If you want the agenda to honor the startup
  536. option, set `org-agenda-inhibit-startup' to nil."
  537. :group 'org-startup
  538. :type '(choice
  539. (const :tag "nofold: show all" nil)
  540. (const :tag "fold: overview" t)
  541. (const :tag "content: all headlines" content)
  542. (const :tag "show everything, even drawers" showeverything)))
  543. (defcustom org-startup-truncated t
  544. "Non-nil means entering Org-mode will set `truncate-lines'.
  545. This is useful since some lines containing links can be very long and
  546. uninteresting. Also tables look terrible when wrapped."
  547. :group 'org-startup
  548. :type 'boolean)
  549. (defcustom org-startup-indented nil
  550. "Non-nil means turn on `org-indent-mode' on startup.
  551. This can also be configured on a per-file basis by adding one of
  552. the following lines anywhere in the buffer:
  553. #+STARTUP: indent
  554. #+STARTUP: noindent"
  555. :group 'org-structure
  556. :type '(choice
  557. (const :tag "Not" nil)
  558. (const :tag "Globally (slow on startup in large files)" t)))
  559. (defcustom org-use-sub-superscripts t
  560. "Non-nil means interpret \"_\" and \"^\" for display.
  561. When this option is turned on, you can use TeX-like syntax for sub- and
  562. superscripts. Several characters after \"_\" or \"^\" will be
  563. considered as a single item - so grouping with {} is normally not
  564. needed. For example, the following things will be parsed as single
  565. sub- or superscripts.
  566. 10^24 or 10^tau several digits will be considered 1 item.
  567. 10^-12 or 10^-tau a leading sign with digits or a word
  568. x^2-y^3 will be read as x^2 - y^3, because items are
  569. terminated by almost any nonword/nondigit char.
  570. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  571. Still, ambiguity is possible - so when in doubt use {} to enclose
  572. the sub/superscript. If you set this variable to the symbol
  573. `{}', the braces are *required* in order to trigger
  574. interpretations as sub/superscript. This can be helpful in
  575. documents that need \"_\" frequently in plain text."
  576. :group 'org-startup
  577. :version "24.1"
  578. :type '(choice
  579. (const :tag "Always interpret" t)
  580. (const :tag "Only with braces" {})
  581. (const :tag "Never interpret" nil)))
  582. (defcustom org-startup-with-beamer-mode nil
  583. "Non-nil means turn on `org-beamer-mode' on startup.
  584. This can also be configured on a per-file basis by adding one of
  585. the following lines anywhere in the buffer:
  586. #+STARTUP: beamer"
  587. :group 'org-startup
  588. :version "24.1"
  589. :type 'boolean)
  590. (defcustom org-startup-align-all-tables nil
  591. "Non-nil means align all tables when visiting a file.
  592. This is useful when the column width in tables is forced with <N> cookies
  593. in table fields. Such tables will look correct only after the first re-align.
  594. This can also be configured on a per-file basis by adding one of
  595. the following lines anywhere in the buffer:
  596. #+STARTUP: align
  597. #+STARTUP: noalign"
  598. :group 'org-startup
  599. :type 'boolean)
  600. (defcustom org-startup-with-inline-images nil
  601. "Non-nil means show inline images when loading a new Org file.
  602. This can also be configured on a per-file basis by adding one of
  603. the following lines anywhere in the buffer:
  604. #+STARTUP: inlineimages
  605. #+STARTUP: noinlineimages"
  606. :group 'org-startup
  607. :version "24.1"
  608. :type 'boolean)
  609. (defcustom org-startup-with-latex-preview nil
  610. "Non-nil means preview LaTeX fragments when loading a new Org file.
  611. This can also be configured on a per-file basis by adding one of
  612. the followinglines anywhere in the buffer:
  613. #+STARTUP: latexpreview
  614. #+STARTUP: nolatexpreview"
  615. :group 'org-startup
  616. :version "24.4"
  617. :package-version '(Org . "8.0")
  618. :type 'boolean)
  619. (defcustom org-insert-mode-line-in-empty-file nil
  620. "Non-nil means insert the first line setting Org-mode in empty files.
  621. When the function `org-mode' is called interactively in an empty file, this
  622. normally means that the file name does not automatically trigger Org-mode.
  623. To ensure that the file will always be in Org-mode in the future, a
  624. line enforcing Org-mode will be inserted into the buffer, if this option
  625. has been set."
  626. :group 'org-startup
  627. :type 'boolean)
  628. (defcustom org-replace-disputed-keys nil
  629. "Non-nil means use alternative key bindings for some keys.
  630. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  631. These keys are also used by other packages like shift-selection-mode'
  632. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  633. If you want to use Org-mode together with one of these other modes,
  634. or more generally if you would like to move some Org-mode commands to
  635. other keys, set this variable and configure the keys with the variable
  636. `org-disputed-keys'.
  637. This option is only relevant at load-time of Org-mode, and must be set
  638. *before* org.el is loaded. Changing it requires a restart of Emacs to
  639. become effective."
  640. :group 'org-startup
  641. :type 'boolean)
  642. (defcustom org-use-extra-keys nil
  643. "Non-nil means use extra key sequence definitions for certain commands.
  644. This happens automatically if you run XEmacs or if `window-system'
  645. is nil. This variable lets you do the same manually. You must
  646. set it before loading org.
  647. Example: on Carbon Emacs 22 running graphically, with an external
  648. keyboard on a Powerbook, the default way of setting M-left might
  649. not work for either Alt or ESC. Setting this variable will make
  650. it work for ESC."
  651. :group 'org-startup
  652. :type 'boolean)
  653. (if (fboundp 'defvaralias)
  654. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  655. (defcustom org-disputed-keys
  656. '(([(shift up)] . [(meta p)])
  657. ([(shift down)] . [(meta n)])
  658. ([(shift left)] . [(meta -)])
  659. ([(shift right)] . [(meta +)])
  660. ([(control shift right)] . [(meta shift +)])
  661. ([(control shift left)] . [(meta shift -)]))
  662. "Keys for which Org-mode and other modes compete.
  663. This is an alist, cars are the default keys, second element specifies
  664. the alternative to use when `org-replace-disputed-keys' is t.
  665. Keys can be specified in any syntax supported by `define-key'.
  666. The value of this option takes effect only at Org-mode's startup,
  667. therefore you'll have to restart Emacs to apply it after changing."
  668. :group 'org-startup
  669. :type 'alist)
  670. (defun org-key (key)
  671. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  672. Or return the original if not disputed.
  673. Also apply the translations defined in `org-xemacs-key-equivalents'."
  674. (when org-replace-disputed-keys
  675. (let* ((nkey (key-description key))
  676. (x (org-find-if (lambda (x)
  677. (equal (key-description (car x)) nkey))
  678. org-disputed-keys)))
  679. (setq key (if x (cdr x) key))))
  680. (when (featurep 'xemacs)
  681. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  682. key)
  683. (defun org-find-if (predicate seq)
  684. (catch 'exit
  685. (while seq
  686. (if (funcall predicate (car seq))
  687. (throw 'exit (car seq))
  688. (pop seq)))))
  689. (defun org-defkey (keymap key def)
  690. "Define a key, possibly translated, as returned by `org-key'."
  691. (define-key keymap (org-key key) def))
  692. (defcustom org-ellipsis nil
  693. "The ellipsis to use in the Org-mode outline.
  694. When nil, just use the standard three dots. When a string, use that instead,
  695. When a face, use the standard 3 dots, but with the specified face.
  696. The change affects only Org-mode (which will then use its own display table).
  697. Changing this requires executing `M-x org-mode' in a buffer to become
  698. effective."
  699. :group 'org-startup
  700. :type '(choice (const :tag "Default" nil)
  701. (face :tag "Face" :value org-warning)
  702. (string :tag "String" :value "...#")))
  703. (defvar org-display-table nil
  704. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  705. (defgroup org-keywords nil
  706. "Keywords in Org-mode."
  707. :tag "Org Keywords"
  708. :group 'org)
  709. (defcustom org-deadline-string "DEADLINE:"
  710. "String to mark deadline entries.
  711. A deadline is this string, followed by a time stamp. Should be a word,
  712. terminated by a colon. You can insert a schedule keyword and
  713. a timestamp with \\[org-deadline].
  714. Changes become only effective after restarting Emacs."
  715. :group 'org-keywords
  716. :type 'string)
  717. (defcustom org-scheduled-string "SCHEDULED:"
  718. "String to mark scheduled TODO entries.
  719. A schedule is this string, followed by a time stamp. Should be a word,
  720. terminated by a colon. You can insert a schedule keyword and
  721. a timestamp with \\[org-schedule].
  722. Changes become only effective after restarting Emacs."
  723. :group 'org-keywords
  724. :type 'string)
  725. (defcustom org-closed-string "CLOSED:"
  726. "String used as the prefix for timestamps logging closing a TODO entry."
  727. :group 'org-keywords
  728. :type 'string)
  729. (defcustom org-clock-string "CLOCK:"
  730. "String used as prefix for timestamps clocking work hours on an item."
  731. :group 'org-keywords
  732. :type 'string)
  733. (defconst org-planning-or-clock-line-re (concat "^[ \t]*\\("
  734. org-scheduled-string "\\|"
  735. org-deadline-string "\\|"
  736. org-closed-string "\\|"
  737. org-clock-string "\\)")
  738. "Matches a line with planning or clock info.")
  739. (defcustom org-comment-string "COMMENT"
  740. "Entries starting with this keyword will never be exported.
  741. An entry can be toggled between COMMENT and normal with
  742. \\[org-toggle-comment].
  743. Changes become only effective after restarting Emacs."
  744. :group 'org-keywords
  745. :type 'string)
  746. (defcustom org-quote-string "QUOTE"
  747. "Entries starting with this keyword will be exported in fixed-width font.
  748. Quoting applies only to the text in the entry following the headline, and does
  749. not extend beyond the next headline, even if that is lower level.
  750. An entry can be toggled between QUOTE and normal with
  751. \\[org-toggle-fixed-width-section]."
  752. :group 'org-keywords
  753. :type 'string)
  754. (defconst org-repeat-re
  755. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)"
  756. "Regular expression for specifying repeated events.
  757. After a match, group 1 contains the repeat expression.")
  758. (defgroup org-structure nil
  759. "Options concerning the general structure of Org-mode files."
  760. :tag "Org Structure"
  761. :group 'org)
  762. (defgroup org-reveal-location nil
  763. "Options about how to make context of a location visible."
  764. :tag "Org Reveal Location"
  765. :group 'org-structure)
  766. (defconst org-context-choice
  767. '(choice
  768. (const :tag "Always" t)
  769. (const :tag "Never" nil)
  770. (repeat :greedy t :tag "Individual contexts"
  771. (cons
  772. (choice :tag "Context"
  773. (const agenda)
  774. (const org-goto)
  775. (const occur-tree)
  776. (const tags-tree)
  777. (const link-search)
  778. (const mark-goto)
  779. (const bookmark-jump)
  780. (const isearch)
  781. (const default))
  782. (boolean))))
  783. "Contexts for the reveal options.")
  784. (defcustom org-show-hierarchy-above '((default . t))
  785. "Non-nil means show full hierarchy when revealing a location.
  786. Org-mode often shows locations in an org-mode file which might have
  787. been invisible before. When this is set, the hierarchy of headings
  788. above the exposed location is shown.
  789. Turning this off for example for sparse trees makes them very compact.
  790. Instead of t, this can also be an alist specifying this option for different
  791. contexts. Valid contexts are
  792. agenda when exposing an entry from the agenda
  793. org-goto when using the command `org-goto' on key C-c C-j
  794. occur-tree when using the command `org-occur' on key C-c /
  795. tags-tree when constructing a sparse tree based on tags matches
  796. link-search when exposing search matches associated with a link
  797. mark-goto when exposing the jump goal of a mark
  798. bookmark-jump when exposing a bookmark location
  799. isearch when exiting from an incremental search
  800. default default for all contexts not set explicitly"
  801. :group 'org-reveal-location
  802. :type org-context-choice)
  803. (defcustom org-show-following-heading '((default . nil))
  804. "Non-nil means show following heading when revealing a location.
  805. Org-mode often shows locations in an org-mode file which might have
  806. been invisible before. When this is set, the heading following the
  807. match is shown.
  808. Turning this off for example for sparse trees makes them very compact,
  809. but makes it harder to edit the location of the match. In such a case,
  810. use the command \\[org-reveal] to show more context.
  811. Instead of t, this can also be an alist specifying this option for different
  812. contexts. See `org-show-hierarchy-above' for valid contexts."
  813. :group 'org-reveal-location
  814. :type org-context-choice)
  815. (defcustom org-show-siblings '((default . nil) (isearch t))
  816. "Non-nil means show all sibling heading when revealing a location.
  817. Org-mode often shows locations in an org-mode file which might have
  818. been invisible before. When this is set, the sibling of the current entry
  819. heading are all made visible. If `org-show-hierarchy-above' is t,
  820. the same happens on each level of the hierarchy above the current entry.
  821. By default this is on for the isearch context, off for all other contexts.
  822. Turning this off for example for sparse trees makes them very compact,
  823. but makes it harder to edit the location of the match. In such a case,
  824. use the command \\[org-reveal] to show more context.
  825. Instead of t, this can also be an alist specifying this option for different
  826. contexts. See `org-show-hierarchy-above' for valid contexts."
  827. :group 'org-reveal-location
  828. :type org-context-choice)
  829. (defcustom org-show-entry-below '((default . nil))
  830. "Non-nil means show the entry below a headline when revealing a location.
  831. Org-mode often shows locations in an org-mode file which might have
  832. been invisible before. When this is set, the text below the headline that is
  833. exposed is also shown.
  834. By default this is off for all contexts.
  835. Instead of t, this can also be an alist specifying this option for different
  836. contexts. See `org-show-hierarchy-above' for valid contexts."
  837. :group 'org-reveal-location
  838. :type org-context-choice)
  839. (defcustom org-indirect-buffer-display 'other-window
  840. "How should indirect tree buffers be displayed?
  841. This applies to indirect buffers created with the commands
  842. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  843. Valid values are:
  844. current-window Display in the current window
  845. other-window Just display in another window.
  846. dedicated-frame Create one new frame, and re-use it each time.
  847. new-frame Make a new frame each time. Note that in this case
  848. previously-made indirect buffers are kept, and you need to
  849. kill these buffers yourself."
  850. :group 'org-structure
  851. :group 'org-agenda-windows
  852. :type '(choice
  853. (const :tag "In current window" current-window)
  854. (const :tag "In current frame, other window" other-window)
  855. (const :tag "Each time a new frame" new-frame)
  856. (const :tag "One dedicated frame" dedicated-frame)))
  857. (defcustom org-use-speed-commands nil
  858. "Non-nil means activate single letter commands at beginning of a headline.
  859. This may also be a function to test for appropriate locations where speed
  860. commands should be active."
  861. :group 'org-structure
  862. :type '(choice
  863. (const :tag "Never" nil)
  864. (const :tag "At beginning of headline stars" t)
  865. (function)))
  866. (defcustom org-speed-commands-user nil
  867. "Alist of additional speed commands.
  868. This list will be checked before `org-speed-commands-default'
  869. when the variable `org-use-speed-commands' is non-nil
  870. and when the cursor is at the beginning of a headline.
  871. The car if each entry is a string with a single letter, which must
  872. be assigned to `self-insert-command' in the global map.
  873. The cdr is either a command to be called interactively, a function
  874. to be called, or a form to be evaluated.
  875. An entry that is just a list with a single string will be interpreted
  876. as a descriptive headline that will be added when listing the speed
  877. commands in the Help buffer using the `?' speed command."
  878. :group 'org-structure
  879. :type '(repeat :value ("k" . ignore)
  880. (choice :value ("k" . ignore)
  881. (list :tag "Descriptive Headline" (string :tag "Headline"))
  882. (cons :tag "Letter and Command"
  883. (string :tag "Command letter")
  884. (choice
  885. (function)
  886. (sexp))))))
  887. (defgroup org-cycle nil
  888. "Options concerning visibility cycling in Org-mode."
  889. :tag "Org Cycle"
  890. :group 'org-structure)
  891. (defcustom org-cycle-skip-children-state-if-no-children t
  892. "Non-nil means skip CHILDREN state in entries that don't have any."
  893. :group 'org-cycle
  894. :type 'boolean)
  895. (defcustom org-cycle-max-level nil
  896. "Maximum level which should still be subject to visibility cycling.
  897. Levels higher than this will, for cycling, be treated as text, not a headline.
  898. When `org-odd-levels-only' is set, a value of N in this variable actually
  899. means 2N-1 stars as the limiting headline.
  900. When nil, cycle all levels.
  901. Note that the limiting level of cycling is also influenced by
  902. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  903. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  904. than its value."
  905. :group 'org-cycle
  906. :type '(choice
  907. (const :tag "No limit" nil)
  908. (integer :tag "Maximum level")))
  909. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK" "RESULTS")
  910. "Names of drawers. Drawers are not opened by cycling on the headline above.
  911. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  912. this:
  913. :DRAWERNAME:
  914. .....
  915. :END:
  916. The drawer \"PROPERTIES\" is special for capturing properties through
  917. the property API.
  918. Drawers can be defined on the per-file basis with a line like:
  919. #+DRAWERS: HIDDEN STATE PROPERTIES"
  920. :group 'org-structure
  921. :group 'org-cycle
  922. :type '(repeat (string :tag "Drawer Name")))
  923. (defcustom org-hide-block-startup nil
  924. "Non-nil means entering Org-mode will fold all blocks.
  925. This can also be set in on a per-file basis with
  926. #+STARTUP: hideblocks
  927. #+STARTUP: showblocks"
  928. :group 'org-startup
  929. :group 'org-cycle
  930. :type 'boolean)
  931. (defcustom org-cycle-global-at-bob nil
  932. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  933. This makes it possible to do global cycling without having to use S-TAB or
  934. \\[universal-argument] TAB. For this special case to work, the first line
  935. of the buffer must not be a headline -- it may be empty or some other text.
  936. When used in this way, `org-cycle-hook' is disabled temporarily to make
  937. sure the cursor stays at the beginning of the buffer. When this option is
  938. nil, don't do anything special at the beginning of the buffer."
  939. :group 'org-cycle
  940. :type 'boolean)
  941. (defcustom org-cycle-level-after-item/entry-creation t
  942. "Non-nil means cycle entry level or item indentation in new empty entries.
  943. When the cursor is at the end of an empty headline, i.e., with only stars
  944. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  945. and then all possible ancestor states, before returning to the original state.
  946. This makes data entry extremely fast: M-RET to create a new headline,
  947. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  948. When the cursor is at the end of an empty plain list item, one TAB will
  949. make it a subitem, two or more tabs will back up to make this an item
  950. higher up in the item hierarchy."
  951. :group 'org-cycle
  952. :type 'boolean)
  953. (defcustom org-cycle-emulate-tab t
  954. "Where should `org-cycle' emulate TAB.
  955. nil Never
  956. white Only in completely white lines
  957. whitestart Only at the beginning of lines, before the first non-white char
  958. t Everywhere except in headlines
  959. exc-hl-bol Everywhere except at the start of a headline
  960. If TAB is used in a place where it does not emulate TAB, the current subtree
  961. visibility is cycled."
  962. :group 'org-cycle
  963. :type '(choice (const :tag "Never" nil)
  964. (const :tag "Only in completely white lines" white)
  965. (const :tag "Before first char in a line" whitestart)
  966. (const :tag "Everywhere except in headlines" t)
  967. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  968. ))
  969. (defcustom org-cycle-separator-lines 2
  970. "Number of empty lines needed to keep an empty line between collapsed trees.
  971. If you leave an empty line between the end of a subtree and the following
  972. headline, this empty line is hidden when the subtree is folded.
  973. Org-mode will leave (exactly) one empty line visible if the number of
  974. empty lines is equal or larger to the number given in this variable.
  975. So the default 2 means at least 2 empty lines after the end of a subtree
  976. are needed to produce free space between a collapsed subtree and the
  977. following headline.
  978. If the number is negative, and the number of empty lines is at least -N,
  979. all empty lines are shown.
  980. Special case: when 0, never leave empty lines in collapsed view."
  981. :group 'org-cycle
  982. :type 'integer)
  983. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  984. (defcustom org-pre-cycle-hook nil
  985. "Hook that is run before visibility cycling is happening.
  986. The function(s) in this hook must accept a single argument which indicates
  987. the new state that will be set right after running this hook. The
  988. argument is a symbol. Before a global state change, it can have the values
  989. `overview', `content', or `all'. Before a local state change, it can have
  990. the values `folded', `children', or `subtree'."
  991. :group 'org-cycle
  992. :type 'hook)
  993. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  994. org-cycle-hide-drawers
  995. org-cycle-hide-inline-tasks
  996. org-cycle-show-empty-lines
  997. org-optimize-window-after-visibility-change)
  998. "Hook that is run after `org-cycle' has changed the buffer visibility.
  999. The function(s) in this hook must accept a single argument which indicates
  1000. the new state that was set by the most recent `org-cycle' command. The
  1001. argument is a symbol. After a global state change, it can have the values
  1002. `overview', `contents', or `all'. After a local state change, it can have
  1003. the values `folded', `children', or `subtree'."
  1004. :group 'org-cycle
  1005. :type 'hook)
  1006. (defgroup org-edit-structure nil
  1007. "Options concerning structure editing in Org-mode."
  1008. :tag "Org Edit Structure"
  1009. :group 'org-structure)
  1010. (defcustom org-odd-levels-only nil
  1011. "Non-nil means skip even levels and only use odd levels for the outline.
  1012. This has the effect that two stars are being added/taken away in
  1013. promotion/demotion commands. It also influences how levels are
  1014. handled by the exporters.
  1015. Changing it requires restart of `font-lock-mode' to become effective
  1016. for fontification also in regions already fontified.
  1017. You may also set this on a per-file basis by adding one of the following
  1018. lines to the buffer:
  1019. #+STARTUP: odd
  1020. #+STARTUP: oddeven"
  1021. :group 'org-edit-structure
  1022. :group 'org-appearance
  1023. :type 'boolean)
  1024. (defcustom org-adapt-indentation t
  1025. "Non-nil means adapt indentation to outline node level.
  1026. When this variable is set, Org assumes that you write outlines by
  1027. indenting text in each node to align with the headline (after the stars).
  1028. The following issues are influenced by this variable:
  1029. - When this is set and the *entire* text in an entry is indented, the
  1030. indentation is increased by one space in a demotion command, and
  1031. decreased by one in a promotion command. If any line in the entry
  1032. body starts with text at column 0, indentation is not changed at all.
  1033. - Property drawers and planning information is inserted indented when
  1034. this variable s set. When nil, they will not be indented.
  1035. - TAB indents a line relative to context. The lines below a headline
  1036. will be indented when this variable is set.
  1037. Note that this is all about true indentation, by adding and removing
  1038. space characters. See also `org-indent.el' which does level-dependent
  1039. indentation in a virtual way, i.e. at display time in Emacs."
  1040. :group 'org-edit-structure
  1041. :type 'boolean)
  1042. (defcustom org-special-ctrl-a/e nil
  1043. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  1044. When t, `C-a' will bring back the cursor to the beginning of the
  1045. headline text, i.e. after the stars and after a possible TODO
  1046. keyword. In an item, this will be the position after bullet and
  1047. check-box, if any. When the cursor is already at that position,
  1048. another `C-a' will bring it to the beginning of the line.
  1049. `C-e' will jump to the end of the headline, ignoring the presence
  1050. of tags in the headline. A second `C-e' will then jump to the
  1051. true end of the line, after any tags. This also means that, when
  1052. this variable is non-nil, `C-e' also will never jump beyond the
  1053. end of the heading of a folded section, i.e. not after the
  1054. ellipses.
  1055. When set to the symbol `reversed', the first `C-a' or `C-e' works
  1056. normally, going to the true line boundary first. Only a directly
  1057. following, identical keypress will bring the cursor to the
  1058. special positions.
  1059. This may also be a cons cell where the behavior for `C-a' and
  1060. `C-e' is set separately."
  1061. :group 'org-edit-structure
  1062. :type '(choice
  1063. (const :tag "off" nil)
  1064. (const :tag "on: after stars/bullet and before tags first" t)
  1065. (const :tag "reversed: true line boundary first" reversed)
  1066. (cons :tag "Set C-a and C-e separately"
  1067. (choice :tag "Special C-a"
  1068. (const :tag "off" nil)
  1069. (const :tag "on: after stars/bullet first" t)
  1070. (const :tag "reversed: before stars/bullet first" reversed))
  1071. (choice :tag "Special C-e"
  1072. (const :tag "off" nil)
  1073. (const :tag "on: before tags first" t)
  1074. (const :tag "reversed: after tags first" reversed)))))
  1075. (if (fboundp 'defvaralias)
  1076. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  1077. (defcustom org-special-ctrl-k nil
  1078. "Non-nil means `C-k' will behave specially in headlines.
  1079. When nil, `C-k' will call the default `kill-line' command.
  1080. When t, the following will happen while the cursor is in the headline:
  1081. - When the cursor is at the beginning of a headline, kill the entire
  1082. line and possible the folded subtree below the line.
  1083. - When in the middle of the headline text, kill the headline up to the tags.
  1084. - When after the headline text, kill the tags."
  1085. :group 'org-edit-structure
  1086. :type 'boolean)
  1087. (defcustom org-ctrl-k-protect-subtree nil
  1088. "Non-nil means, do not delete a hidden subtree with C-k.
  1089. When set to the symbol `error', simply throw an error when C-k is
  1090. used to kill (part-of) a headline that has hidden text behind it.
  1091. Any other non-nil value will result in a query to the user, if it is
  1092. OK to kill that hidden subtree. When nil, kill without remorse."
  1093. :group 'org-edit-structure
  1094. :version "24.1"
  1095. :type '(choice
  1096. (const :tag "Do not protect hidden subtrees" nil)
  1097. (const :tag "Protect hidden subtrees with a security query" t)
  1098. (const :tag "Never kill a hidden subtree with C-k" error)))
  1099. (defcustom org-catch-invisible-edits nil
  1100. "Check if in invisible region before inserting or deleting a character.
  1101. Valid values are:
  1102. nil Do not check, so just do invisible edits.
  1103. error Throw an error and do nothing.
  1104. show Make point visible, and do the requested edit.
  1105. show-and-error Make point visible, then throw an error and abort the edit.
  1106. smart Make point visible, and do insertion/deletion if it is
  1107. adjacent to visible text and the change feels predictable.
  1108. Never delete a previously invisible character or add in the
  1109. middle or right after an invisible region. Basically, this
  1110. allows insertion and backward-delete right before ellipses.
  1111. FIXME: maybe in this case we should not even show?"
  1112. :group 'org-edit-structure
  1113. :version "24.1"
  1114. :type '(choice
  1115. (const :tag "Do not check" nil)
  1116. (const :tag "Throw error when trying to edit" error)
  1117. (const :tag "Unhide, but do not do the edit" show-and-error)
  1118. (const :tag "Show invisible part and do the edit" show)
  1119. (const :tag "Be smart and do the right thing" smart)))
  1120. (defcustom org-yank-folded-subtrees t
  1121. "Non-nil means when yanking subtrees, fold them.
  1122. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  1123. it starts with a heading and all other headings in it are either children
  1124. or siblings, then fold all the subtrees. However, do this only if no
  1125. text after the yank would be swallowed into a folded tree by this action."
  1126. :group 'org-edit-structure
  1127. :type 'boolean)
  1128. (defcustom org-yank-adjusted-subtrees nil
  1129. "Non-nil means when yanking subtrees, adjust the level.
  1130. With this setting, `org-paste-subtree' is used to insert the subtree, see
  1131. this function for details."
  1132. :group 'org-edit-structure
  1133. :type 'boolean)
  1134. (defcustom org-M-RET-may-split-line '((default . t))
  1135. "Non-nil means M-RET will split the line at the cursor position.
  1136. When nil, it will go to the end of the line before making a
  1137. new line.
  1138. You may also set this option in a different way for different
  1139. contexts. Valid contexts are:
  1140. headline when creating a new headline
  1141. item when creating a new item
  1142. table in a table field
  1143. default the value to be used for all contexts not explicitly
  1144. customized"
  1145. :group 'org-structure
  1146. :group 'org-table
  1147. :type '(choice
  1148. (const :tag "Always" t)
  1149. (const :tag "Never" nil)
  1150. (repeat :greedy t :tag "Individual contexts"
  1151. (cons
  1152. (choice :tag "Context"
  1153. (const headline)
  1154. (const item)
  1155. (const table)
  1156. (const default))
  1157. (boolean)))))
  1158. (defcustom org-insert-heading-respect-content nil
  1159. "Non-nil means insert new headings after the current subtree.
  1160. When nil, the new heading is created directly after the current line.
  1161. The commands \\[org-insert-heading-respect-content] and
  1162. \\[org-insert-todo-heading-respect-content] turn this variable on
  1163. for the duration of the command."
  1164. :group 'org-structure
  1165. :type 'boolean)
  1166. (defcustom org-blank-before-new-entry '((heading . auto)
  1167. (plain-list-item . auto))
  1168. "Should `org-insert-heading' leave a blank line before new heading/item?
  1169. The value is an alist, with `heading' and `plain-list-item' as CAR,
  1170. and a boolean flag as CDR. The cdr may also be the symbol `auto', in
  1171. which case Org will look at the surrounding headings/items and try to
  1172. make an intelligent decision whether to insert a blank line or not.
  1173. For plain lists, if the variable `org-empty-line-terminates-plain-lists' is
  1174. set, the setting here is ignored and no empty line is inserted, to avoid
  1175. breaking the list structure."
  1176. :group 'org-edit-structure
  1177. :type '(list
  1178. (cons (const heading)
  1179. (choice (const :tag "Never" nil)
  1180. (const :tag "Always" t)
  1181. (const :tag "Auto" auto)))
  1182. (cons (const plain-list-item)
  1183. (choice (const :tag "Never" nil)
  1184. (const :tag "Always" t)
  1185. (const :tag "Auto" auto)))))
  1186. (defcustom org-insert-heading-hook nil
  1187. "Hook being run after inserting a new heading."
  1188. :group 'org-edit-structure
  1189. :type 'hook)
  1190. (defcustom org-enable-fixed-width-editor t
  1191. "Non-nil means lines starting with \":\" are treated as fixed-width.
  1192. This currently only means they are never auto-wrapped.
  1193. When nil, such lines will be treated like ordinary lines.
  1194. See also the QUOTE keyword."
  1195. :group 'org-edit-structure
  1196. :type 'boolean)
  1197. (defcustom org-goto-auto-isearch t
  1198. "Non-nil means typing characters in `org-goto' starts incremental search.
  1199. When nil, you can use these keybindings to navigate the buffer:
  1200. q Quit the org-goto interface
  1201. n Go to the next visible heading
  1202. p Go to the previous visible heading
  1203. f Go one heading forward on same level
  1204. b Go one heading backward on same level
  1205. u Go one heading up"
  1206. :group 'org-edit-structure
  1207. :type 'boolean)
  1208. (defgroup org-sparse-trees nil
  1209. "Options concerning sparse trees in Org-mode."
  1210. :tag "Org Sparse Trees"
  1211. :group 'org-structure)
  1212. (defcustom org-highlight-sparse-tree-matches t
  1213. "Non-nil means highlight all matches that define a sparse tree.
  1214. The highlights will automatically disappear the next time the buffer is
  1215. changed by an edit command."
  1216. :group 'org-sparse-trees
  1217. :type 'boolean)
  1218. (defcustom org-remove-highlights-with-change t
  1219. "Non-nil means any change to the buffer will remove temporary highlights.
  1220. Such highlights are created by `org-occur' and `org-clock-display'.
  1221. When nil, `C-c C-c needs to be used to get rid of the highlights.
  1222. The highlights created by `org-preview-latex-fragment' always need
  1223. `C-c C-c' to be removed."
  1224. :group 'org-sparse-trees
  1225. :group 'org-time
  1226. :type 'boolean)
  1227. (defcustom org-occur-hook '(org-first-headline-recenter)
  1228. "Hook that is run after `org-occur' has constructed a sparse tree.
  1229. This can be used to recenter the window to show as much of the structure
  1230. as possible."
  1231. :group 'org-sparse-trees
  1232. :type 'hook)
  1233. (defgroup org-imenu-and-speedbar nil
  1234. "Options concerning imenu and speedbar in Org-mode."
  1235. :tag "Org Imenu and Speedbar"
  1236. :group 'org-structure)
  1237. (defcustom org-imenu-depth 2
  1238. "The maximum level for Imenu access to Org-mode headlines.
  1239. This also applied for speedbar access."
  1240. :group 'org-imenu-and-speedbar
  1241. :type 'integer)
  1242. (defgroup org-table nil
  1243. "Options concerning tables in Org-mode."
  1244. :tag "Org Table"
  1245. :group 'org)
  1246. (defcustom org-enable-table-editor 'optimized
  1247. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1248. When nil, such lines will be treated like ordinary lines.
  1249. When equal to the symbol `optimized', the table editor will be optimized to
  1250. do the following:
  1251. - Automatic overwrite mode in front of whitespace in table fields.
  1252. This makes the structure of the table stay in tact as long as the edited
  1253. field does not exceed the column width.
  1254. - Minimize the number of realigns. Normally, the table is aligned each time
  1255. TAB or RET are pressed to move to another field. With optimization this
  1256. happens only if changes to a field might have changed the column width.
  1257. Optimization requires replacing the functions `self-insert-command',
  1258. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1259. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1260. very good at guessing when a re-align will be necessary, but you can always
  1261. force one with \\[org-ctrl-c-ctrl-c].
  1262. If you would like to use the optimized version in Org-mode, but the
  1263. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1264. This variable can be used to turn on and off the table editor during a session,
  1265. but in order to toggle optimization, a restart is required.
  1266. See also the variable `org-table-auto-blank-field'."
  1267. :group 'org-table
  1268. :type '(choice
  1269. (const :tag "off" nil)
  1270. (const :tag "on" t)
  1271. (const :tag "on, optimized" optimized)))
  1272. (defcustom org-self-insert-cluster-for-undo (or (featurep 'xemacs)
  1273. (version<= emacs-version "24.1"))
  1274. "Non-nil means cluster self-insert commands for undo when possible.
  1275. If this is set, then, like in the Emacs command loop, 20 consecutive
  1276. characters will be undone together.
  1277. This is configurable, because there is some impact on typing performance."
  1278. :group 'org-table
  1279. :type 'boolean)
  1280. (defcustom org-table-tab-recognizes-table.el t
  1281. "Non-nil means TAB will automatically notice a table.el table.
  1282. When it sees such a table, it moves point into it and - if necessary -
  1283. calls `table-recognize-table'."
  1284. :group 'org-table-editing
  1285. :type 'boolean)
  1286. (defgroup org-link nil
  1287. "Options concerning links in Org-mode."
  1288. :tag "Org Link"
  1289. :group 'org)
  1290. (defvar org-link-abbrev-alist-local nil
  1291. "Buffer-local version of `org-link-abbrev-alist', which see.
  1292. The value of this is taken from the #+LINK lines.")
  1293. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1294. (defcustom org-link-abbrev-alist nil
  1295. "Alist of link abbreviations.
  1296. The car of each element is a string, to be replaced at the start of a link.
  1297. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1298. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1299. [[linkkey:tag][description]]
  1300. The 'linkkey' must be a word word, starting with a letter, followed
  1301. by letters, numbers, '-' or '_'.
  1302. If REPLACE is a string, the tag will simply be appended to create the link.
  1303. If the string contains \"%s\", the tag will be inserted there. If the string
  1304. contains \"%h\", it will cause a url-encoded version of the tag to be inserted
  1305. at that point (see the function `url-hexify-string'). If the string contains
  1306. the specifier \"%(my-function)\", then the custom function `my-function' will
  1307. be invoked: this function takes the tag as its only argument and must return
  1308. a string.
  1309. REPLACE may also be a function that will be called with the tag as the
  1310. only argument to create the link, which should be returned as a string.
  1311. See the manual for examples."
  1312. :group 'org-link
  1313. :type '(repeat
  1314. (cons
  1315. (string :tag "Protocol")
  1316. (choice
  1317. (string :tag "Format")
  1318. (function)))))
  1319. (defcustom org-descriptive-links t
  1320. "Non-nil means Org will display descriptive links.
  1321. E.g. [[http://orgmode.org][Org website]] will be displayed as
  1322. \"Org Website\", hiding the link itself and just displaying its
  1323. description. When set to `nil', Org will display the full links
  1324. literally.
  1325. You can interactively set the value of this variable by calling
  1326. `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
  1327. :group 'org-link
  1328. :type 'boolean)
  1329. (defcustom org-link-file-path-type 'adaptive
  1330. "How the path name in file links should be stored.
  1331. Valid values are:
  1332. relative Relative to the current directory, i.e. the directory of the file
  1333. into which the link is being inserted.
  1334. absolute Absolute path, if possible with ~ for home directory.
  1335. noabbrev Absolute path, no abbreviation of home directory.
  1336. adaptive Use relative path for files in the current directory and sub-
  1337. directories of it. For other files, use an absolute path."
  1338. :group 'org-link
  1339. :type '(choice
  1340. (const relative)
  1341. (const absolute)
  1342. (const noabbrev)
  1343. (const adaptive)))
  1344. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  1345. "Types of links that should be activated in Org-mode files.
  1346. This is a list of symbols, each leading to the activation of a certain link
  1347. type. In principle, it does not hurt to turn on most link types - there may
  1348. be a small gain when turning off unused link types. The types are:
  1349. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1350. angle Links in angular brackets that may contain whitespace like
  1351. <bbdb:Carsten Dominik>.
  1352. plain Plain links in normal text, no whitespace, like http://google.com.
  1353. radio Text that is matched by a radio target, see manual for details.
  1354. tag Tag settings in a headline (link to tag search).
  1355. date Time stamps (link to calendar).
  1356. footnote Footnote labels.
  1357. Changing this variable requires a restart of Emacs to become effective."
  1358. :group 'org-link
  1359. :type '(set :greedy t
  1360. (const :tag "Double bracket links" bracket)
  1361. (const :tag "Angular bracket links" angle)
  1362. (const :tag "Plain text links" plain)
  1363. (const :tag "Radio target matches" radio)
  1364. (const :tag "Tags" tag)
  1365. (const :tag "Timestamps" date)
  1366. (const :tag "Footnotes" footnote)))
  1367. (defcustom org-make-link-description-function nil
  1368. "Function to use for generating link descriptions from links.
  1369. When nil, the link location will be used. This function must take
  1370. two parameters: the first one is the link, the second one is the
  1371. description generated by `org-insert-link'. The function should
  1372. return the description to use."
  1373. :group 'org-link
  1374. :type 'function)
  1375. (defgroup org-link-store nil
  1376. "Options concerning storing links in Org-mode."
  1377. :tag "Org Store Link"
  1378. :group 'org-link)
  1379. (defcustom org-url-hexify-p t
  1380. "When non-nil, hexify URL when creating a link."
  1381. :type 'boolean
  1382. :version "24.3"
  1383. :group 'org-link-store)
  1384. (defcustom org-email-link-description-format "Email %c: %.30s"
  1385. "Format of the description part of a link to an email or usenet message.
  1386. The following %-escapes will be replaced by corresponding information:
  1387. %F full \"From\" field
  1388. %f name, taken from \"From\" field, address if no name
  1389. %T full \"To\" field
  1390. %t first name in \"To\" field, address if no name
  1391. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1392. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1393. %s subject
  1394. %d date
  1395. %m message-id.
  1396. You may use normal field width specification between the % and the letter.
  1397. This is for example useful to limit the length of the subject.
  1398. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1399. :group 'org-link-store
  1400. :type 'string)
  1401. (defcustom org-from-is-user-regexp
  1402. (let (r1 r2)
  1403. (when (and user-mail-address (not (string= user-mail-address "")))
  1404. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1405. (when (and user-full-name (not (string= user-full-name "")))
  1406. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1407. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1408. "Regexp matched against the \"From:\" header of an email or usenet message.
  1409. It should match if the message is from the user him/herself."
  1410. :group 'org-link-store
  1411. :type 'regexp)
  1412. (defcustom org-context-in-file-links t
  1413. "Non-nil means file links from `org-store-link' contain context.
  1414. A search string will be added to the file name with :: as separator and
  1415. used to find the context when the link is activated by the command
  1416. `org-open-at-point'. When this option is t, the entire active region
  1417. will be placed in the search string of the file link. If set to a
  1418. positive integer, only the first n lines of context will be stored.
  1419. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1420. negates this setting for the duration of the command."
  1421. :group 'org-link-store
  1422. :type '(choice boolean integer))
  1423. (defcustom org-keep-stored-link-after-insertion nil
  1424. "Non-nil means keep link in list for entire session.
  1425. The command `org-store-link' adds a link pointing to the current
  1426. location to an internal list. These links accumulate during a session.
  1427. The command `org-insert-link' can be used to insert links into any
  1428. Org-mode file (offering completion for all stored links). When this
  1429. option is nil, every link which has been inserted once using \\[org-insert-link]
  1430. will be removed from the list, to make completing the unused links
  1431. more efficient."
  1432. :group 'org-link-store
  1433. :type 'boolean)
  1434. (defgroup org-link-follow nil
  1435. "Options concerning following links in Org-mode."
  1436. :tag "Org Follow Link"
  1437. :group 'org-link)
  1438. (defcustom org-link-translation-function nil
  1439. "Function to translate links with different syntax to Org syntax.
  1440. This can be used to translate links created for example by the Planner
  1441. or emacs-wiki packages to Org syntax.
  1442. The function must accept two parameters, a TYPE containing the link
  1443. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1444. which is everything after the link protocol. It should return a cons
  1445. with possibly modified values of type and path.
  1446. Org contains a function for this, so if you set this variable to
  1447. `org-translate-link-from-planner', you should be able follow many
  1448. links created by planner."
  1449. :group 'org-link-follow
  1450. :type 'function)
  1451. (defcustom org-follow-link-hook nil
  1452. "Hook that is run after a link has been followed."
  1453. :group 'org-link-follow
  1454. :type 'hook)
  1455. (defcustom org-tab-follows-link nil
  1456. "Non-nil means on links TAB will follow the link.
  1457. Needs to be set before org.el is loaded.
  1458. This really should not be used, it does not make sense, and the
  1459. implementation is bad."
  1460. :group 'org-link-follow
  1461. :type 'boolean)
  1462. (defcustom org-return-follows-link nil
  1463. "Non-nil means on links RET will follow the link.
  1464. In tables, the special behavior of RET has precedence."
  1465. :group 'org-link-follow
  1466. :type 'boolean)
  1467. (defcustom org-mouse-1-follows-link
  1468. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1469. "Non-nil means mouse-1 on a link will follow the link.
  1470. A longer mouse click will still set point. Does not work on XEmacs.
  1471. Needs to be set before org.el is loaded."
  1472. :group 'org-link-follow
  1473. :type 'boolean)
  1474. (defcustom org-mark-ring-length 4
  1475. "Number of different positions to be recorded in the ring.
  1476. Changing this requires a restart of Emacs to work correctly."
  1477. :group 'org-link-follow
  1478. :type 'integer)
  1479. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1480. "Non-nil means internal links in Org files must exactly match a headline.
  1481. When nil, the link search tries to match a phrase with all words
  1482. in the search text."
  1483. :group 'org-link-follow
  1484. :version "24.1"
  1485. :type '(choice
  1486. (const :tag "Use fuzzy text search" nil)
  1487. (const :tag "Match only exact headline" t)
  1488. (const :tag "Match exact headline or query to create it"
  1489. query-to-create)))
  1490. (defcustom org-link-frame-setup
  1491. '((vm . vm-visit-folder-other-frame)
  1492. (vm-imap . vm-visit-imap-folder-other-frame)
  1493. (gnus . org-gnus-no-new-news)
  1494. (file . find-file-other-window)
  1495. (wl . wl-other-frame))
  1496. "Setup the frame configuration for following links.
  1497. When following a link with Emacs, it may often be useful to display
  1498. this link in another window or frame. This variable can be used to
  1499. set this up for the different types of links.
  1500. For VM, use any of
  1501. `vm-visit-folder'
  1502. `vm-visit-folder-other-window'
  1503. `vm-visit-folder-other-frame'
  1504. For Gnus, use any of
  1505. `gnus'
  1506. `gnus-other-frame'
  1507. `org-gnus-no-new-news'
  1508. For FILE, use any of
  1509. `find-file'
  1510. `find-file-other-window'
  1511. `find-file-other-frame'
  1512. For Wanderlust use any of
  1513. `wl'
  1514. `wl-other-frame'
  1515. For the calendar, use the variable `calendar-setup'.
  1516. For BBDB, it is currently only possible to display the matches in
  1517. another window."
  1518. :group 'org-link-follow
  1519. :type '(list
  1520. (cons (const vm)
  1521. (choice
  1522. (const vm-visit-folder)
  1523. (const vm-visit-folder-other-window)
  1524. (const vm-visit-folder-other-frame)))
  1525. (cons (const gnus)
  1526. (choice
  1527. (const gnus)
  1528. (const gnus-other-frame)
  1529. (const org-gnus-no-new-news)))
  1530. (cons (const file)
  1531. (choice
  1532. (const find-file)
  1533. (const find-file-other-window)
  1534. (const find-file-other-frame)))
  1535. (cons (const wl)
  1536. (choice
  1537. (const wl)
  1538. (const wl-other-frame)))))
  1539. (defcustom org-display-internal-link-with-indirect-buffer nil
  1540. "Non-nil means use indirect buffer to display infile links.
  1541. Activating internal links (from one location in a file to another location
  1542. in the same file) normally just jumps to the location. When the link is
  1543. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1544. is displayed in
  1545. another window. When this option is set, the other window actually displays
  1546. an indirect buffer clone of the current buffer, to avoid any visibility
  1547. changes to the current buffer."
  1548. :group 'org-link-follow
  1549. :type 'boolean)
  1550. (defcustom org-open-non-existing-files nil
  1551. "Non-nil means `org-open-file' will open non-existing files.
  1552. When nil, an error will be generated.
  1553. This variable applies only to external applications because they
  1554. might choke on non-existing files. If the link is to a file that
  1555. will be opened in Emacs, the variable is ignored."
  1556. :group 'org-link-follow
  1557. :type 'boolean)
  1558. (defcustom org-open-directory-means-index-dot-org nil
  1559. "Non-nil means a link to a directory really means to index.org.
  1560. When nil, following a directory link will run dired or open a finder/explorer
  1561. window on that directory."
  1562. :group 'org-link-follow
  1563. :type 'boolean)
  1564. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1565. "Function and arguments to call for following mailto links.
  1566. This is a list with the first element being a Lisp function, and the
  1567. remaining elements being arguments to the function. In string arguments,
  1568. %a will be replaced by the address, and %s will be replaced by the subject
  1569. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1570. :group 'org-link-follow
  1571. :type '(choice
  1572. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1573. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1574. (const :tag "message-mail" (message-mail "%a" "%s"))
  1575. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1576. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1577. "Non-nil means ask for confirmation before executing shell links.
  1578. Shell links can be dangerous: just think about a link
  1579. [[shell:rm -rf ~/*][Google Search]]
  1580. This link would show up in your Org-mode document as \"Google Search\",
  1581. but really it would remove your entire home directory.
  1582. Therefore we advise against setting this variable to nil.
  1583. Just change it to `y-or-n-p' if you want to confirm with a
  1584. single keystroke rather than having to type \"yes\"."
  1585. :group 'org-link-follow
  1586. :type '(choice
  1587. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1588. (const :tag "with y-or-n (faster)" y-or-n-p)
  1589. (const :tag "no confirmation (dangerous)" nil)))
  1590. (put 'org-confirm-shell-link-function
  1591. 'safe-local-variable
  1592. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1593. (defcustom org-confirm-shell-link-not-regexp ""
  1594. "A regexp to skip confirmation for shell links."
  1595. :group 'org-link-follow
  1596. :version "24.1"
  1597. :type 'regexp)
  1598. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1599. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1600. Elisp links can be dangerous: just think about a link
  1601. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1602. This link would show up in your Org-mode document as \"Google Search\",
  1603. but really it would remove your entire home directory.
  1604. Therefore we advise against setting this variable to nil.
  1605. Just change it to `y-or-n-p' if you want to confirm with a
  1606. single keystroke rather than having to type \"yes\"."
  1607. :group 'org-link-follow
  1608. :type '(choice
  1609. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1610. (const :tag "with y-or-n (faster)" y-or-n-p)
  1611. (const :tag "no confirmation (dangerous)" nil)))
  1612. (put 'org-confirm-shell-link-function
  1613. 'safe-local-variable
  1614. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1615. (defcustom org-confirm-elisp-link-not-regexp ""
  1616. "A regexp to skip confirmation for Elisp links."
  1617. :group 'org-link-follow
  1618. :version "24.1"
  1619. :type 'regexp)
  1620. (defconst org-file-apps-defaults-gnu
  1621. '((remote . emacs)
  1622. (system . mailcap)
  1623. (t . mailcap))
  1624. "Default file applications on a UNIX or GNU/Linux system.
  1625. See `org-file-apps'.")
  1626. (defconst org-file-apps-defaults-macosx
  1627. '((remote . emacs)
  1628. (t . "open %s")
  1629. (system . "open %s")
  1630. ("ps.gz" . "gv %s")
  1631. ("eps.gz" . "gv %s")
  1632. ("dvi" . "xdvi %s")
  1633. ("fig" . "xfig %s"))
  1634. "Default file applications on a MacOS X system.
  1635. The system \"open\" is known as a default, but we use X11 applications
  1636. for some files for which the OS does not have a good default.
  1637. See `org-file-apps'.")
  1638. (defconst org-file-apps-defaults-windowsnt
  1639. (list
  1640. '(remote . emacs)
  1641. (cons t
  1642. (list (if (featurep 'xemacs)
  1643. 'mswindows-shell-execute
  1644. 'w32-shell-execute)
  1645. "open" 'file))
  1646. (cons 'system
  1647. (list (if (featurep 'xemacs)
  1648. 'mswindows-shell-execute
  1649. 'w32-shell-execute)
  1650. "open" 'file)))
  1651. "Default file applications on a Windows NT system.
  1652. The system \"open\" is used for most files.
  1653. See `org-file-apps'.")
  1654. (defcustom org-file-apps
  1655. '(
  1656. (auto-mode . emacs)
  1657. ("\\.mm\\'" . default)
  1658. ("\\.x?html?\\'" . default)
  1659. ("\\.pdf\\'" . default)
  1660. )
  1661. "External applications for opening `file:path' items in a document.
  1662. Org-mode uses system defaults for different file types, but
  1663. you can use this variable to set the application for a given file
  1664. extension. The entries in this list are cons cells where the car identifies
  1665. files and the cdr the corresponding command. Possible values for the
  1666. file identifier are
  1667. \"string\" A string as a file identifier can be interpreted in different
  1668. ways, depending on its contents:
  1669. - Alphanumeric characters only:
  1670. Match links with this file extension.
  1671. Example: (\"pdf\" . \"evince %s\")
  1672. to open PDFs with evince.
  1673. - Regular expression: Match links where the
  1674. filename matches the regexp. If you want to
  1675. use groups here, use shy groups.
  1676. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1677. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1678. to open *.html and *.xhtml with firefox.
  1679. - Regular expression which contains (non-shy) groups:
  1680. Match links where the whole link, including \"::\", and
  1681. anything after that, matches the regexp.
  1682. In a custom command string, %1, %2, etc. are replaced with
  1683. the parts of the link that were matched by the groups.
  1684. For backwards compatibility, if a command string is given
  1685. that does not use any of the group matches, this case is
  1686. handled identically to the second one (i.e. match against
  1687. file name only).
  1688. In a custom lisp form, you can access the group matches with
  1689. (match-string n link).
  1690. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1691. to open [[file:document.pdf::5]] with evince at page 5.
  1692. `directory' Matches a directory
  1693. `remote' Matches a remote file, accessible through tramp or efs.
  1694. Remote files most likely should be visited through Emacs
  1695. because external applications cannot handle such paths.
  1696. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1697. so all files Emacs knows how to handle. Using this with
  1698. command `emacs' will open most files in Emacs. Beware that this
  1699. will also open html files inside Emacs, unless you add
  1700. (\"html\" . default) to the list as well.
  1701. t Default for files not matched by any of the other options.
  1702. `system' The system command to open files, like `open' on Windows
  1703. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1704. that will be selected if you call `C-c C-o' with a double
  1705. \\[universal-argument] \\[universal-argument] prefix.
  1706. Possible values for the command are:
  1707. `emacs' The file will be visited by the current Emacs process.
  1708. `default' Use the default application for this file type, which is the
  1709. association for t in the list, most likely in the system-specific
  1710. part.
  1711. This can be used to overrule an unwanted setting in the
  1712. system-specific variable.
  1713. `system' Use the system command for opening files, like \"open\".
  1714. This command is specified by the entry whose car is `system'.
  1715. Most likely, the system-specific version of this variable
  1716. does define this command, but you can overrule/replace it
  1717. here.
  1718. string A command to be executed by a shell; %s will be replaced
  1719. by the path to the file.
  1720. sexp A Lisp form which will be evaluated. The file path will
  1721. be available in the Lisp variable `file'.
  1722. For more examples, see the system specific constants
  1723. `org-file-apps-defaults-macosx'
  1724. `org-file-apps-defaults-windowsnt'
  1725. `org-file-apps-defaults-gnu'."
  1726. :group 'org-link-follow
  1727. :type '(repeat
  1728. (cons (choice :value ""
  1729. (string :tag "Extension")
  1730. (const :tag "System command to open files" system)
  1731. (const :tag "Default for unrecognized files" t)
  1732. (const :tag "Remote file" remote)
  1733. (const :tag "Links to a directory" directory)
  1734. (const :tag "Any files that have Emacs modes"
  1735. auto-mode))
  1736. (choice :value ""
  1737. (const :tag "Visit with Emacs" emacs)
  1738. (const :tag "Use default" default)
  1739. (const :tag "Use the system command" system)
  1740. (string :tag "Command")
  1741. (sexp :tag "Lisp form")))))
  1742. (defcustom org-doi-server-url "http://dx.doi.org/"
  1743. "The URL of the DOI server."
  1744. :type 'string
  1745. :version "24.3"
  1746. :group 'org-link-follow)
  1747. (defgroup org-refile nil
  1748. "Options concerning refiling entries in Org-mode."
  1749. :tag "Org Refile"
  1750. :group 'org)
  1751. (defcustom org-directory "~/org"
  1752. "Directory with org files.
  1753. This is just a default location to look for Org files. There is no need
  1754. at all to put your files into this directory. It is only used in the
  1755. following situations:
  1756. 1. When a capture template specifies a target file that is not an
  1757. absolute path. The path will then be interpreted relative to
  1758. `org-directory'
  1759. 2. When a capture note is filed away in an interactive way (when exiting the
  1760. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1761. with `org-directory' as the default path."
  1762. :group 'org-refile
  1763. :group 'org-remember
  1764. :group 'org-capture
  1765. :type 'directory)
  1766. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1767. "Default target for storing notes.
  1768. Used as a fall back file for org-remember.el and org-capture.el, for
  1769. templates that do not specify a target file."
  1770. :group 'org-refile
  1771. :group 'org-remember
  1772. :group 'org-capture
  1773. :type '(choice
  1774. (const :tag "Default from remember-data-file" nil)
  1775. file))
  1776. (defcustom org-goto-interface 'outline
  1777. "The default interface to be used for `org-goto'.
  1778. Allowed values are:
  1779. outline The interface shows an outline of the relevant file
  1780. and the correct heading is found by moving through
  1781. the outline or by searching with incremental search.
  1782. outline-path-completion Headlines in the current buffer are offered via
  1783. completion. This is the interface also used by
  1784. the refile command."
  1785. :group 'org-refile
  1786. :type '(choice
  1787. (const :tag "Outline" outline)
  1788. (const :tag "Outline-path-completion" outline-path-completion)))
  1789. (defcustom org-goto-max-level 5
  1790. "Maximum target level when running `org-goto' with refile interface."
  1791. :group 'org-refile
  1792. :type 'integer)
  1793. (defcustom org-reverse-note-order nil
  1794. "Non-nil means store new notes at the beginning of a file or entry.
  1795. When nil, new notes will be filed to the end of a file or entry.
  1796. This can also be a list with cons cells of regular expressions that
  1797. are matched against file names, and values."
  1798. :group 'org-remember
  1799. :group 'org-capture
  1800. :group 'org-refile
  1801. :type '(choice
  1802. (const :tag "Reverse always" t)
  1803. (const :tag "Reverse never" nil)
  1804. (repeat :tag "By file name regexp"
  1805. (cons regexp boolean))))
  1806. (defcustom org-log-refile nil
  1807. "Information to record when a task is refiled.
  1808. Possible values are:
  1809. nil Don't add anything
  1810. time Add a time stamp to the task
  1811. note Prompt for a note and add it with template `org-log-note-headings'
  1812. This option can also be set with on a per-file-basis with
  1813. #+STARTUP: nologrefile
  1814. #+STARTUP: logrefile
  1815. #+STARTUP: lognoterefile
  1816. You can have local logging settings for a subtree by setting the LOGGING
  1817. property to one or more of these keywords.
  1818. When bulk-refiling from the agenda, the value `note' is forbidden and
  1819. will temporarily be changed to `time'."
  1820. :group 'org-refile
  1821. :group 'org-progress
  1822. :version "24.1"
  1823. :type '(choice
  1824. (const :tag "No logging" nil)
  1825. (const :tag "Record timestamp" time)
  1826. (const :tag "Record timestamp with note." note)))
  1827. (defcustom org-refile-targets nil
  1828. "Targets for refiling entries with \\[org-refile].
  1829. This is a list of cons cells. Each cell contains:
  1830. - a specification of the files to be considered, either a list of files,
  1831. or a symbol whose function or variable value will be used to retrieve
  1832. a file name or a list of file names. If you use `org-agenda-files' for
  1833. that, all agenda files will be scanned for targets. Nil means consider
  1834. headings in the current buffer.
  1835. - A specification of how to find candidate refile targets. This may be
  1836. any of:
  1837. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1838. This tag has to be present in all target headlines, inheritance will
  1839. not be considered.
  1840. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1841. todo keyword.
  1842. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1843. headlines that are refiling targets.
  1844. - a cons cell (:level . N). Any headline of level N is considered a target.
  1845. Note that, when `org-odd-levels-only' is set, level corresponds to
  1846. order in hierarchy, not to the number of stars.
  1847. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1848. Note that, when `org-odd-levels-only' is set, level corresponds to
  1849. order in hierarchy, not to the number of stars.
  1850. Each element of this list generates a set of possible targets.
  1851. The union of these sets is presented (with completion) to
  1852. the user by `org-refile'.
  1853. You can set the variable `org-refile-target-verify-function' to a function
  1854. to verify each headline found by the simple criteria above.
  1855. When this variable is nil, all top-level headlines in the current buffer
  1856. are used, equivalent to the value `((nil . (:level . 1))'."
  1857. :group 'org-refile
  1858. :type '(repeat
  1859. (cons
  1860. (choice :value org-agenda-files
  1861. (const :tag "All agenda files" org-agenda-files)
  1862. (const :tag "Current buffer" nil)
  1863. (function) (variable) (file))
  1864. (choice :tag "Identify target headline by"
  1865. (cons :tag "Specific tag" (const :value :tag) (string))
  1866. (cons :tag "TODO keyword" (const :value :todo) (string))
  1867. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1868. (cons :tag "Level number" (const :value :level) (integer))
  1869. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1870. (defcustom org-refile-target-verify-function nil
  1871. "Function to verify if the headline at point should be a refile target.
  1872. The function will be called without arguments, with point at the
  1873. beginning of the headline. It should return t and leave point
  1874. where it is if the headline is a valid target for refiling.
  1875. If the target should not be selected, the function must return nil.
  1876. In addition to this, it may move point to a place from where the search
  1877. should be continued. For example, the function may decide that the entire
  1878. subtree of the current entry should be excluded and move point to the end
  1879. of the subtree."
  1880. :group 'org-refile
  1881. :type 'function)
  1882. (defcustom org-refile-use-cache nil
  1883. "Non-nil means cache refile targets to speed up the process.
  1884. The cache for a particular file will be updated automatically when
  1885. the buffer has been killed, or when any of the marker used for flagging
  1886. refile targets no longer points at a live buffer.
  1887. If you have added new entries to a buffer that might themselves be targets,
  1888. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  1889. find that easier, `C-u C-u C-u C-c C-w'."
  1890. :group 'org-refile
  1891. :version "24.1"
  1892. :type 'boolean)
  1893. (defcustom org-refile-use-outline-path nil
  1894. "Non-nil means provide refile targets as paths.
  1895. So a level 3 headline will be available as level1/level2/level3.
  1896. When the value is `file', also include the file name (without directory)
  1897. into the path. In this case, you can also stop the completion after
  1898. the file name, to get entries inserted as top level in the file.
  1899. When `full-file-path', include the full file path."
  1900. :group 'org-refile
  1901. :type '(choice
  1902. (const :tag "Not" nil)
  1903. (const :tag "Yes" t)
  1904. (const :tag "Start with file name" file)
  1905. (const :tag "Start with full file path" full-file-path)))
  1906. (defcustom org-outline-path-complete-in-steps t
  1907. "Non-nil means complete the outline path in hierarchical steps.
  1908. When Org-mode uses the refile interface to select an outline path
  1909. \(see variable `org-refile-use-outline-path'), the completion of
  1910. the path can be done is a single go, or if can be done in steps down
  1911. the headline hierarchy. Going in steps is probably the best if you
  1912. do not use a special completion package like `ido' or `icicles'.
  1913. However, when using these packages, going in one step can be very
  1914. fast, while still showing the whole path to the entry."
  1915. :group 'org-refile
  1916. :type 'boolean)
  1917. (defcustom org-refile-allow-creating-parent-nodes nil
  1918. "Non-nil means allow to create new nodes as refile targets.
  1919. New nodes are then created by adding \"/new node name\" to the completion
  1920. of an existing node. When the value of this variable is `confirm',
  1921. new node creation must be confirmed by the user (recommended)
  1922. When nil, the completion must match an existing entry.
  1923. Note that, if the new heading is not seen by the criteria
  1924. listed in `org-refile-targets', multiple instances of the same
  1925. heading would be created by trying again to file under the new
  1926. heading."
  1927. :group 'org-refile
  1928. :type '(choice
  1929. (const :tag "Never" nil)
  1930. (const :tag "Always" t)
  1931. (const :tag "Prompt for confirmation" confirm)))
  1932. (defcustom org-refile-active-region-within-subtree nil
  1933. "Non-nil means also refile active region within a subtree.
  1934. By default `org-refile' doesn't allow refiling regions if they
  1935. don't contain a set of subtrees, but it might be convenient to
  1936. do so sometimes: in that case, the first line of the region is
  1937. converted to a headline before refiling."
  1938. :group 'org-refile
  1939. :version "24.1"
  1940. :type 'boolean)
  1941. (defgroup org-todo nil
  1942. "Options concerning TODO items in Org-mode."
  1943. :tag "Org TODO"
  1944. :group 'org)
  1945. (defgroup org-progress nil
  1946. "Options concerning Progress logging in Org-mode."
  1947. :tag "Org Progress"
  1948. :group 'org-time)
  1949. (defvar org-todo-interpretation-widgets
  1950. '((:tag "Sequence (cycling hits every state)" sequence)
  1951. (:tag "Type (cycling directly to DONE)" type))
  1952. "The available interpretation symbols for customizing `org-todo-keywords'.
  1953. Interested libraries should add to this list.")
  1954. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1955. "List of TODO entry keyword sequences and their interpretation.
  1956. \\<org-mode-map>This is a list of sequences.
  1957. Each sequence starts with a symbol, either `sequence' or `type',
  1958. indicating if the keywords should be interpreted as a sequence of
  1959. action steps, or as different types of TODO items. The first
  1960. keywords are states requiring action - these states will select a headline
  1961. for inclusion into the global TODO list Org-mode produces. If one of
  1962. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  1963. signify that no further action is necessary. If \"|\" is not found,
  1964. the last keyword is treated as the only DONE state of the sequence.
  1965. The command \\[org-todo] cycles an entry through these states, and one
  1966. additional state where no keyword is present. For details about this
  1967. cycling, see the manual.
  1968. TODO keywords and interpretation can also be set on a per-file basis with
  1969. the special #+SEQ_TODO and #+TYP_TODO lines.
  1970. Each keyword can optionally specify a character for fast state selection
  1971. \(in combination with the variable `org-use-fast-todo-selection')
  1972. and specifiers for state change logging, using the same syntax that
  1973. is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says that
  1974. the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1975. indicates to record a time stamp each time this state is selected.
  1976. Each keyword may also specify if a timestamp or a note should be
  1977. recorded when entering or leaving the state, by adding additional
  1978. characters in the parenthesis after the keyword. This looks like this:
  1979. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1980. record only the time of the state change. With X and Y being either
  1981. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1982. Y when leaving the state if and only if the *target* state does not
  1983. define X. You may omit any of the fast-selection key or X or /Y,
  1984. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1985. For backward compatibility, this variable may also be just a list
  1986. of keywords. In this case the interpretation (sequence or type) will be
  1987. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1988. :group 'org-todo
  1989. :group 'org-keywords
  1990. :type '(choice
  1991. (repeat :tag "Old syntax, just keywords"
  1992. (string :tag "Keyword"))
  1993. (repeat :tag "New syntax"
  1994. (cons
  1995. (choice
  1996. :tag "Interpretation"
  1997. ;;Quick and dirty way to see
  1998. ;;`org-todo-interpretations'. This takes the
  1999. ;;place of item arguments
  2000. :convert-widget
  2001. (lambda (widget)
  2002. (widget-put widget
  2003. :args (mapcar
  2004. #'(lambda (x)
  2005. (widget-convert
  2006. (cons 'const x)))
  2007. org-todo-interpretation-widgets))
  2008. widget))
  2009. (repeat
  2010. (string :tag "Keyword"))))))
  2011. (defvar org-todo-keywords-1 nil
  2012. "All TODO and DONE keywords active in a buffer.")
  2013. (make-variable-buffer-local 'org-todo-keywords-1)
  2014. (defvar org-todo-keywords-for-agenda nil)
  2015. (defvar org-done-keywords-for-agenda nil)
  2016. (defvar org-drawers-for-agenda nil)
  2017. (defvar org-todo-keyword-alist-for-agenda nil)
  2018. (defvar org-tag-alist-for-agenda nil)
  2019. (defvar org-agenda-contributing-files nil)
  2020. (defvar org-not-done-keywords nil)
  2021. (make-variable-buffer-local 'org-not-done-keywords)
  2022. (defvar org-done-keywords nil)
  2023. (make-variable-buffer-local 'org-done-keywords)
  2024. (defvar org-todo-heads nil)
  2025. (make-variable-buffer-local 'org-todo-heads)
  2026. (defvar org-todo-sets nil)
  2027. (make-variable-buffer-local 'org-todo-sets)
  2028. (defvar org-todo-log-states nil)
  2029. (make-variable-buffer-local 'org-todo-log-states)
  2030. (defvar org-todo-kwd-alist nil)
  2031. (make-variable-buffer-local 'org-todo-kwd-alist)
  2032. (defvar org-todo-key-alist nil)
  2033. (make-variable-buffer-local 'org-todo-key-alist)
  2034. (defvar org-todo-key-trigger nil)
  2035. (make-variable-buffer-local 'org-todo-key-trigger)
  2036. (defcustom org-todo-interpretation 'sequence
  2037. "Controls how TODO keywords are interpreted.
  2038. This variable is in principle obsolete and is only used for
  2039. backward compatibility, if the interpretation of todo keywords is
  2040. not given already in `org-todo-keywords'. See that variable for
  2041. more information."
  2042. :group 'org-todo
  2043. :group 'org-keywords
  2044. :type '(choice (const sequence)
  2045. (const type)))
  2046. (defcustom org-use-fast-todo-selection t
  2047. "Non-nil means use the fast todo selection scheme with C-c C-t.
  2048. This variable describes if and under what circumstances the cycling
  2049. mechanism for TODO keywords will be replaced by a single-key, direct
  2050. selection scheme.
  2051. When nil, fast selection is never used.
  2052. When the symbol `prefix', it will be used when `org-todo' is called
  2053. with a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and
  2054. `C-u t' in an agenda buffer.
  2055. When t, fast selection is used by default. In this case, the prefix
  2056. argument forces cycling instead.
  2057. In all cases, the special interface is only used if access keys have
  2058. actually been assigned by the user, i.e. if keywords in the configuration
  2059. are followed by a letter in parenthesis, like TODO(t)."
  2060. :group 'org-todo
  2061. :type '(choice
  2062. (const :tag "Never" nil)
  2063. (const :tag "By default" t)
  2064. (const :tag "Only with C-u C-c C-t" prefix)))
  2065. (defcustom org-provide-todo-statistics t
  2066. "Non-nil means update todo statistics after insert and toggle.
  2067. ALL-HEADLINES means update todo statistics by including headlines
  2068. with no TODO keyword as well, counting them as not done.
  2069. A list of TODO keywords means the same, but skip keywords that are
  2070. not in this list.
  2071. When this is set, todo statistics is updated in the parent of the
  2072. current entry each time a todo state is changed."
  2073. :group 'org-todo
  2074. :type '(choice
  2075. (const :tag "Yes, only for TODO entries" t)
  2076. (const :tag "Yes, including all entries" 'all-headlines)
  2077. (repeat :tag "Yes, for TODOs in this list"
  2078. (string :tag "TODO keyword"))
  2079. (other :tag "No TODO statistics" nil)))
  2080. (defcustom org-hierarchical-todo-statistics t
  2081. "Non-nil means TODO statistics covers just direct children.
  2082. When nil, all entries in the subtree are considered.
  2083. This has only an effect if `org-provide-todo-statistics' is set.
  2084. To set this to nil for only a single subtree, use a COOKIE_DATA
  2085. property and include the word \"recursive\" into the value."
  2086. :group 'org-todo
  2087. :type 'boolean)
  2088. (defcustom org-after-todo-state-change-hook nil
  2089. "Hook which is run after the state of a TODO item was changed.
  2090. The new state (a string with a TODO keyword, or nil) is available in the
  2091. Lisp variable `org-state'."
  2092. :group 'org-todo
  2093. :type 'hook)
  2094. (defvar org-blocker-hook nil
  2095. "Hook for functions that are allowed to block a state change.
  2096. Functions in this hook should not modify the buffer.
  2097. Each function gets as its single argument a property list,
  2098. see `org-trigger-hook' for more information about this list.
  2099. If any of the functions in this hook returns nil, the state change
  2100. is blocked.")
  2101. (defvar org-trigger-hook nil
  2102. "Hook for functions that are triggered by a state change.
  2103. Each function gets as its single argument a property list with at
  2104. least the following elements:
  2105. (:type type-of-change :position pos-at-entry-start
  2106. :from old-state :to new-state)
  2107. Depending on the type, more properties may be present.
  2108. This mechanism is currently implemented for:
  2109. TODO state changes
  2110. ------------------
  2111. :type todo-state-change
  2112. :from previous state (keyword as a string), or nil, or a symbol
  2113. 'todo' or 'done', to indicate the general type of state.
  2114. :to new state, like in :from")
  2115. (defcustom org-enforce-todo-dependencies nil
  2116. "Non-nil means undone TODO entries will block switching the parent to DONE.
  2117. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  2118. be blocked if any prior sibling is not yet done.
  2119. Finally, if the parent is blocked because of ordered siblings of its own,
  2120. the child will also be blocked."
  2121. :set (lambda (var val)
  2122. (set var val)
  2123. (if val
  2124. (add-hook 'org-blocker-hook
  2125. 'org-block-todo-from-children-or-siblings-or-parent)
  2126. (remove-hook 'org-blocker-hook
  2127. 'org-block-todo-from-children-or-siblings-or-parent)))
  2128. :group 'org-todo
  2129. :type 'boolean)
  2130. (defcustom org-enforce-todo-checkbox-dependencies nil
  2131. "Non-nil means unchecked boxes will block switching the parent to DONE.
  2132. When this is nil, checkboxes have no influence on switching TODO states.
  2133. When non-nil, you first need to check off all check boxes before the TODO
  2134. entry can be switched to DONE.
  2135. This variable needs to be set before org.el is loaded, and you need to
  2136. restart Emacs after a change to make the change effective. The only way
  2137. to change is while Emacs is running is through the customize interface."
  2138. :set (lambda (var val)
  2139. (set var val)
  2140. (if val
  2141. (add-hook 'org-blocker-hook
  2142. 'org-block-todo-from-checkboxes)
  2143. (remove-hook 'org-blocker-hook
  2144. 'org-block-todo-from-checkboxes)))
  2145. :group 'org-todo
  2146. :type 'boolean)
  2147. (defcustom org-treat-insert-todo-heading-as-state-change nil
  2148. "Non-nil means inserting a TODO heading is treated as state change.
  2149. So when the command \\[org-insert-todo-heading] is used, state change
  2150. logging will apply if appropriate. When nil, the new TODO item will
  2151. be inserted directly, and no logging will take place."
  2152. :group 'org-todo
  2153. :type 'boolean)
  2154. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  2155. "Non-nil means switching TODO states with S-cursor counts as state change.
  2156. This is the default behavior. However, setting this to nil allows a
  2157. convenient way to select a TODO state and bypass any logging associated
  2158. with that."
  2159. :group 'org-todo
  2160. :type 'boolean)
  2161. (defcustom org-todo-state-tags-triggers nil
  2162. "Tag changes that should be triggered by TODO state changes.
  2163. This is a list. Each entry is
  2164. (state-change (tag . flag) .......)
  2165. State-change can be a string with a state, and empty string to indicate the
  2166. state that has no TODO keyword, or it can be one of the symbols `todo'
  2167. or `done', meaning any not-done or done state, respectively."
  2168. :group 'org-todo
  2169. :group 'org-tags
  2170. :type '(repeat
  2171. (cons (choice :tag "When changing to"
  2172. (const :tag "Not-done state" todo)
  2173. (const :tag "Done state" done)
  2174. (string :tag "State"))
  2175. (repeat
  2176. (cons :tag "Tag action"
  2177. (string :tag "Tag")
  2178. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  2179. (defcustom org-log-done nil
  2180. "Information to record when a task moves to the DONE state.
  2181. Possible values are:
  2182. nil Don't add anything, just change the keyword
  2183. time Add a time stamp to the task
  2184. note Prompt for a note and add it with template `org-log-note-headings'
  2185. This option can also be set with on a per-file-basis with
  2186. #+STARTUP: nologdone
  2187. #+STARTUP: logdone
  2188. #+STARTUP: lognotedone
  2189. You can have local logging settings for a subtree by setting the LOGGING
  2190. property to one or more of these keywords."
  2191. :group 'org-todo
  2192. :group 'org-progress
  2193. :type '(choice
  2194. (const :tag "No logging" nil)
  2195. (const :tag "Record CLOSED timestamp" time)
  2196. (const :tag "Record CLOSED timestamp with note." note)))
  2197. ;; Normalize old uses of org-log-done.
  2198. (cond
  2199. ((eq org-log-done t) (setq org-log-done 'time))
  2200. ((and (listp org-log-done) (memq 'done org-log-done))
  2201. (setq org-log-done 'note)))
  2202. (defcustom org-log-reschedule nil
  2203. "Information to record when the scheduling date of a tasks is modified.
  2204. Possible values are:
  2205. nil Don't add anything, just change the date
  2206. time Add a time stamp to the task
  2207. note Prompt for a note and add it with template `org-log-note-headings'
  2208. This option can also be set with on a per-file-basis with
  2209. #+STARTUP: nologreschedule
  2210. #+STARTUP: logreschedule
  2211. #+STARTUP: lognotereschedule"
  2212. :group 'org-todo
  2213. :group 'org-progress
  2214. :type '(choice
  2215. (const :tag "No logging" nil)
  2216. (const :tag "Record timestamp" time)
  2217. (const :tag "Record timestamp with note." note)))
  2218. (defcustom org-log-redeadline nil
  2219. "Information to record when the deadline date of a tasks is modified.
  2220. Possible values are:
  2221. nil Don't add anything, just change the date
  2222. time Add a time stamp to the task
  2223. note Prompt for a note and add it with template `org-log-note-headings'
  2224. This option can also be set with on a per-file-basis with
  2225. #+STARTUP: nologredeadline
  2226. #+STARTUP: logredeadline
  2227. #+STARTUP: lognoteredeadline
  2228. You can have local logging settings for a subtree by setting the LOGGING
  2229. property to one or more of these keywords."
  2230. :group 'org-todo
  2231. :group 'org-progress
  2232. :type '(choice
  2233. (const :tag "No logging" nil)
  2234. (const :tag "Record timestamp" time)
  2235. (const :tag "Record timestamp with note." note)))
  2236. (defcustom org-log-note-clock-out nil
  2237. "Non-nil means record a note when clocking out of an item.
  2238. This can also be configured on a per-file basis by adding one of
  2239. the following lines anywhere in the buffer:
  2240. #+STARTUP: lognoteclock-out
  2241. #+STARTUP: nolognoteclock-out"
  2242. :group 'org-todo
  2243. :group 'org-progress
  2244. :type 'boolean)
  2245. (defcustom org-log-done-with-time t
  2246. "Non-nil means the CLOSED time stamp will contain date and time.
  2247. When nil, only the date will be recorded."
  2248. :group 'org-progress
  2249. :type 'boolean)
  2250. (defcustom org-log-note-headings
  2251. '((done . "CLOSING NOTE %t")
  2252. (state . "State %-12s from %-12S %t")
  2253. (note . "Note taken on %t")
  2254. (reschedule . "Rescheduled from %S on %t")
  2255. (delschedule . "Not scheduled, was %S on %t")
  2256. (redeadline . "New deadline from %S on %t")
  2257. (deldeadline . "Removed deadline, was %S on %t")
  2258. (refile . "Refiled on %t")
  2259. (clock-out . ""))
  2260. "Headings for notes added to entries.
  2261. The value is an alist, with the car being a symbol indicating the note
  2262. context, and the cdr is the heading to be used. The heading may also be the
  2263. empty string.
  2264. %t in the heading will be replaced by a time stamp.
  2265. %T will be an active time stamp instead the default inactive one
  2266. %d will be replaced by a short-format time stamp.
  2267. %D will be replaced by an active short-format time stamp.
  2268. %s will be replaced by the new TODO state, in double quotes.
  2269. %S will be replaced by the old TODO state, in double quotes.
  2270. %u will be replaced by the user name.
  2271. %U will be replaced by the full user name.
  2272. In fact, it is not a good idea to change the `state' entry, because
  2273. agenda log mode depends on the format of these entries."
  2274. :group 'org-todo
  2275. :group 'org-progress
  2276. :type '(list :greedy t
  2277. (cons (const :tag "Heading when closing an item" done) string)
  2278. (cons (const :tag
  2279. "Heading when changing todo state (todo sequence only)"
  2280. state) string)
  2281. (cons (const :tag "Heading when just taking a note" note) string)
  2282. (cons (const :tag "Heading when clocking out" clock-out) string)
  2283. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2284. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2285. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2286. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2287. (cons (const :tag "Heading when refiling" refile) string)))
  2288. (unless (assq 'note org-log-note-headings)
  2289. (push '(note . "%t") org-log-note-headings))
  2290. (defcustom org-log-into-drawer nil
  2291. "Non-nil means insert state change notes and time stamps into a drawer.
  2292. When nil, state changes notes will be inserted after the headline and
  2293. any scheduling and clock lines, but not inside a drawer.
  2294. The value of this variable should be the name of the drawer to use.
  2295. LOGBOOK is proposed as the default drawer for this purpose, you can
  2296. also set this to a string to define the drawer of your choice.
  2297. A value of t is also allowed, representing \"LOGBOOK\".
  2298. A value of t or nil can also be set with on a per-file-basis with
  2299. #+STARTUP: logdrawer
  2300. #+STARTUP: nologdrawer
  2301. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2302. will be ignored.
  2303. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2304. a subtree."
  2305. :group 'org-todo
  2306. :group 'org-progress
  2307. :type '(choice
  2308. (const :tag "Not into a drawer" nil)
  2309. (const :tag "LOGBOOK" t)
  2310. (string :tag "Other")))
  2311. (if (fboundp 'defvaralias)
  2312. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  2313. (defun org-log-into-drawer ()
  2314. "Return the value of `org-log-into-drawer', but let properties overrule.
  2315. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  2316. used instead of the default value."
  2317. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit t)))
  2318. (cond
  2319. ((not p) org-log-into-drawer)
  2320. ((equal p "nil") nil)
  2321. ((equal p "t") "LOGBOOK")
  2322. (t p))))
  2323. (defcustom org-log-state-notes-insert-after-drawers nil
  2324. "Non-nil means insert state change notes after any drawers in entry.
  2325. Only the drawers that *immediately* follow the headline and the
  2326. deadline/scheduled line are skipped.
  2327. When nil, insert notes right after the heading and perhaps the line
  2328. with deadline/scheduling if present.
  2329. This variable will have no effect if `org-log-into-drawer' is
  2330. set."
  2331. :group 'org-todo
  2332. :group 'org-progress
  2333. :type 'boolean)
  2334. (defcustom org-log-states-order-reversed t
  2335. "Non-nil means the latest state note will be directly after heading.
  2336. When nil, the state change notes will be ordered according to time.
  2337. This option can also be set with on a per-file-basis with
  2338. #+STARTUP: logstatesreversed
  2339. #+STARTUP: nologstatesreversed"
  2340. :group 'org-todo
  2341. :group 'org-progress
  2342. :type 'boolean)
  2343. (defcustom org-todo-repeat-to-state nil
  2344. "The TODO state to which a repeater should return the repeating task.
  2345. By default this is the first task in a TODO sequence, or the previous state
  2346. in a TODO_TYP set. But you can specify another task here.
  2347. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2348. :group 'org-todo
  2349. :version "24.1"
  2350. :type '(choice (const :tag "Head of sequence" nil)
  2351. (string :tag "Specific state")))
  2352. (defcustom org-log-repeat 'time
  2353. "Non-nil means record moving through the DONE state when triggering repeat.
  2354. An auto-repeating task is immediately switched back to TODO when
  2355. marked DONE. If you are not logging state changes (by adding \"@\"
  2356. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2357. record a closing note, there will be no record of the task moving
  2358. through DONE. This variable forces taking a note anyway.
  2359. nil Don't force a record
  2360. time Record a time stamp
  2361. note Prompt for a note and add it with template `org-log-note-headings'
  2362. This option can also be set with on a per-file-basis with
  2363. #+STARTUP: nologrepeat
  2364. #+STARTUP: logrepeat
  2365. #+STARTUP: lognoterepeat
  2366. You can have local logging settings for a subtree by setting the LOGGING
  2367. property to one or more of these keywords."
  2368. :group 'org-todo
  2369. :group 'org-progress
  2370. :type '(choice
  2371. (const :tag "Don't force a record" nil)
  2372. (const :tag "Force recording the DONE state" time)
  2373. (const :tag "Force recording a note with the DONE state" note)))
  2374. (defgroup org-priorities nil
  2375. "Priorities in Org-mode."
  2376. :tag "Org Priorities"
  2377. :group 'org-todo)
  2378. (defcustom org-enable-priority-commands t
  2379. "Non-nil means priority commands are active.
  2380. When nil, these commands will be disabled, so that you never accidentally
  2381. set a priority."
  2382. :group 'org-priorities
  2383. :type 'boolean)
  2384. (defcustom org-highest-priority ?A
  2385. "The highest priority of TODO items. A character like ?A, ?B etc.
  2386. Must have a smaller ASCII number than `org-lowest-priority'."
  2387. :group 'org-priorities
  2388. :type 'character)
  2389. (defcustom org-lowest-priority ?C
  2390. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2391. Must have a larger ASCII number than `org-highest-priority'."
  2392. :group 'org-priorities
  2393. :type 'character)
  2394. (defcustom org-default-priority ?B
  2395. "The default priority of TODO items.
  2396. This is the priority an item gets if no explicit priority is given.
  2397. When starting to cycle on an empty priority the first step in the cycle
  2398. depends on `org-priority-start-cycle-with-default'. The resulting first
  2399. step priority must not exceed the range from `org-highest-priority' to
  2400. `org-lowest-priority' which means that `org-default-priority' has to be
  2401. in this range exclusive or inclusive the range boundaries. Else the
  2402. first step refuses to set the default and the second will fall back
  2403. to (depending on the command used) the highest or lowest priority."
  2404. :group 'org-priorities
  2405. :type 'character)
  2406. (defcustom org-priority-start-cycle-with-default t
  2407. "Non-nil means start with default priority when starting to cycle.
  2408. When this is nil, the first step in the cycle will be (depending on the
  2409. command used) one higher or lower than the default priority.
  2410. See also `org-default-priority'."
  2411. :group 'org-priorities
  2412. :type 'boolean)
  2413. (defcustom org-get-priority-function nil
  2414. "Function to extract the priority from a string.
  2415. The string is normally the headline. If this is nil Org computes the
  2416. priority from the priority cookie like [#A] in the headline. It returns
  2417. an integer, increasing by 1000 for each priority level.
  2418. The user can set a different function here, which should take a string
  2419. as an argument and return the numeric priority."
  2420. :group 'org-priorities
  2421. :version "24.1"
  2422. :type 'function)
  2423. (defgroup org-time nil
  2424. "Options concerning time stamps and deadlines in Org-mode."
  2425. :tag "Org Time"
  2426. :group 'org)
  2427. (defcustom org-insert-labeled-timestamps-at-point nil
  2428. "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
  2429. When nil, these labeled time stamps are forces into the second line of an
  2430. entry, just after the headline. When scheduling from the global TODO list,
  2431. the time stamp will always be forced into the second line."
  2432. :group 'org-time
  2433. :type 'boolean)
  2434. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  2435. "Formats for `format-time-string' which are used for time stamps.
  2436. It is not recommended to change this constant.")
  2437. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2438. "Number of minutes to round time stamps to.
  2439. These are two values, the first applies when first creating a time stamp.
  2440. The second applies when changing it with the commands `S-up' and `S-down'.
  2441. When changing the time stamp, this means that it will change in steps
  2442. of N minutes, as given by the second value.
  2443. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2444. numbers should be factors of 60, so for example 5, 10, 15.
  2445. When this is larger than 1, you can still force an exact time stamp by using
  2446. a double prefix argument to a time stamp command like `C-c .' or `C-c !',
  2447. and by using a prefix arg to `S-up/down' to specify the exact number
  2448. of minutes to shift."
  2449. :group 'org-time
  2450. :get #'(lambda (var) ; Make sure both elements are there
  2451. (if (integerp (default-value var))
  2452. (list (default-value var) 5)
  2453. (default-value var)))
  2454. :type '(list
  2455. (integer :tag "when inserting times")
  2456. (integer :tag "when modifying times")))
  2457. ;; Normalize old customizations of this variable.
  2458. (when (integerp org-time-stamp-rounding-minutes)
  2459. (setq org-time-stamp-rounding-minutes
  2460. (list org-time-stamp-rounding-minutes
  2461. org-time-stamp-rounding-minutes)))
  2462. (defcustom org-display-custom-times nil
  2463. "Non-nil means overlay custom formats over all time stamps.
  2464. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2465. To turn this on on a per-file basis, insert anywhere in the file:
  2466. #+STARTUP: customtime"
  2467. :group 'org-time
  2468. :set 'set-default
  2469. :type 'sexp)
  2470. (make-variable-buffer-local 'org-display-custom-times)
  2471. (defcustom org-time-stamp-custom-formats
  2472. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2473. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2474. These are overlaid over the default ISO format if the variable
  2475. `org-display-custom-times' is set. Time like %H:%M should be at the
  2476. end of the second format. The custom formats are also honored by export
  2477. commands, if custom time display is turned on at the time of export."
  2478. :group 'org-time
  2479. :type 'sexp)
  2480. (defun org-time-stamp-format (&optional long inactive)
  2481. "Get the right format for a time string."
  2482. (let ((f (if long (cdr org-time-stamp-formats)
  2483. (car org-time-stamp-formats))))
  2484. (if inactive
  2485. (concat "[" (substring f 1 -1) "]")
  2486. f)))
  2487. (defcustom org-time-clocksum-format
  2488. '(:days "%dd " :hours "%d" :require-hours t :minutes ":%02d" :require-minutes t)
  2489. "The format string used when creating CLOCKSUM lines.
  2490. This is also used when Org mode generates a time duration.
  2491. The value can be a single format string containing two
  2492. %-sequences, which will be filled with the number of hours and
  2493. minutes in that order.
  2494. Alternatively, the value can be a plist associating any of the
  2495. keys :years, :months, :weeks, :days, :hours or :minutes with
  2496. format strings. The time duration is formatted using only the
  2497. time components that are needed and concatenating the results.
  2498. If a time unit in absent, it falls back to the next smallest
  2499. unit.
  2500. The keys :require-years, :require-months, :require-days,
  2501. :require-weeks, :require-hours, :require-minutes are also
  2502. meaningful. A non-nil value for these keys indicates that the
  2503. corresponding time component should always be included, even if
  2504. its value is 0.
  2505. For example,
  2506. \(:days \"%dd\" :hours \"%d\" :require-hours t :minutes \":%02d\"
  2507. :require-minutes t)
  2508. means durations longer than a day will be expressed in days,
  2509. hours and minutes, and durations less than a day will always be
  2510. expressed in hours and minutes (even for durations less than an
  2511. hour).
  2512. The value
  2513. \(:days \"%dd\" :minutes \"%dm\")
  2514. means durations longer than a day will be expressed in days and
  2515. minutes, and durations less than a day will be expressed entirely
  2516. in minutes (even for durations longer than an hour)."
  2517. :group 'org-time
  2518. :group 'org-clock
  2519. :version "24.4"
  2520. :package-version '(Org . "8.0")
  2521. :type '(choice (string :tag "Format string")
  2522. (set :tag "Plist"
  2523. (group :inline t (const :tag "Years" :years)
  2524. (string :tag "Format string"))
  2525. (group :inline t
  2526. (const :tag "Always show years" :require-years)
  2527. (const t))
  2528. (group :inline t (const :tag "Months" :months)
  2529. (string :tag "Format string"))
  2530. (group :inline t
  2531. (const :tag "Always show months" :require-months)
  2532. (const t))
  2533. (group :inline t (const :tag "Weeks" :weeks)
  2534. (string :tag "Format string"))
  2535. (group :inline t
  2536. (const :tag "Always show weeks" :require-weeks)
  2537. (const t))
  2538. (group :inline t (const :tag "Days" :days)
  2539. (string :tag "Format string"))
  2540. (group :inline t
  2541. (const :tag "Always show days" :require-days)
  2542. (const t))
  2543. (group :inline t (const :tag "Hours" :hours)
  2544. (string :tag "Format string"))
  2545. (group :inline t
  2546. (const :tag "Always show hours" :require-hours)
  2547. (const t))
  2548. (group :inline t (const :tag "Minutes" :minutes)
  2549. (string :tag "Format string"))
  2550. (group :inline t
  2551. (const :tag "Always show minutes" :require-minutes)
  2552. (const t)))))
  2553. (defcustom org-time-clocksum-use-fractional nil
  2554. "When non-nil, \\[org-clock-display] uses fractional times.
  2555. See `org-time-clocksum-format' for more on time clock formats."
  2556. :group 'org-time
  2557. :group 'org-clock
  2558. :version "24.3"
  2559. :type 'boolean)
  2560. (defcustom org-time-clocksum-use-effort-durations t
  2561. "When non-nil, \\[org-clock-display] uses effort durations.
  2562. E.g. by default, one day is considered to be a 8 hours effort,
  2563. so a task that has been clocked for 16 hours will be displayed
  2564. as during 2 days in the clock display or in the clocktable.
  2565. See `org-effort-durations' on how to set effort durations
  2566. and `org-time-clocksum-format' for more on time clock formats."
  2567. :group 'org-time
  2568. :group 'org-clock
  2569. :version "24.4"
  2570. :package-version '(Org . "8.0")
  2571. :type 'boolean)
  2572. (defcustom org-time-clocksum-fractional-format "%.2f"
  2573. "The format string used when creating CLOCKSUM lines,
  2574. or when Org mode generates a time duration, if
  2575. `org-time-clocksum-use-fractional' is enabled.
  2576. The value can be a single format string containing one
  2577. %-sequence, which will be filled with the number of hours as
  2578. a float.
  2579. Alternatively, the value can be a plist associating any of the
  2580. keys :years, :months, :weeks, :days, :hours or :minutes with
  2581. a format string. The time duration is formatted using the
  2582. largest time unit which gives a non-zero integer part. If all
  2583. specified formats have zero integer part, the smallest time unit
  2584. is used."
  2585. :group 'org-time
  2586. :type '(choice (string :tag "Format string")
  2587. (set (group :inline t (const :tag "Years" :years)
  2588. (string :tag "Format string"))
  2589. (group :inline t (const :tag "Months" :months)
  2590. (string :tag "Format string"))
  2591. (group :inline t (const :tag "Weeks" :weeks)
  2592. (string :tag "Format string"))
  2593. (group :inline t (const :tag "Days" :days)
  2594. (string :tag "Format string"))
  2595. (group :inline t (const :tag "Hours" :hours)
  2596. (string :tag "Format string"))
  2597. (group :inline t (const :tag "Minutes" :minutes)
  2598. (string :tag "Format string")))))
  2599. (defcustom org-deadline-warning-days 14
  2600. "Number of days before expiration during which a deadline becomes active.
  2601. This variable governs the display in sparse trees and in the agenda.
  2602. When 0 or negative, it means use this number (the absolute value of it)
  2603. even if a deadline has a different individual lead time specified.
  2604. Custom commands can set this variable in the options section."
  2605. :group 'org-time
  2606. :group 'org-agenda-daily/weekly
  2607. :type 'integer)
  2608. (defcustom org-scheduled-delay-days 0
  2609. "Number of days before a scheduled item becomes active.
  2610. This variable governs the display in sparse trees and in the agenda.
  2611. The default value (i.e. 0) means: don't delay scheduled item.
  2612. When negative, it means use this number (the absolute value of it)
  2613. even if a scheduled item has a different individual delay time
  2614. specified.
  2615. Custom commands can set this variable in the options section."
  2616. :group 'org-time
  2617. :group 'org-agenda-daily/weekly
  2618. :version "24.4"
  2619. :package-version '(Org . "8.0")
  2620. :type 'integer)
  2621. (defcustom org-read-date-prefer-future t
  2622. "Non-nil means assume future for incomplete date input from user.
  2623. This affects the following situations:
  2624. 1. The user gives a month but not a year.
  2625. For example, if it is April and you enter \"feb 2\", this will be read
  2626. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2627. 2. The user gives a day, but no month.
  2628. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2629. read this as the third of *next* month. However, if you enter \"17\",
  2630. it will be considered as *this* month.
  2631. If you set this variable to the symbol `time', then also the following
  2632. will work:
  2633. 3. If the user gives a time.
  2634. If the time is before now, it will be interpreted as tomorrow.
  2635. Currently none of this works for ISO week specifications.
  2636. When this option is nil, the current day, month and year will always be
  2637. used as defaults.
  2638. See also `org-agenda-jump-prefer-future'."
  2639. :group 'org-time
  2640. :type '(choice
  2641. (const :tag "Never" nil)
  2642. (const :tag "Check month and day" t)
  2643. (const :tag "Check month, day, and time" time)))
  2644. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2645. "Should the agenda jump command prefer the future for incomplete dates?
  2646. The default is to do the same as configured in `org-read-date-prefer-future'.
  2647. But you can also set a deviating value here.
  2648. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2649. :group 'org-agenda
  2650. :group 'org-time
  2651. :version "24.1"
  2652. :type '(choice
  2653. (const :tag "Use org-read-date-prefer-future"
  2654. org-read-date-prefer-future)
  2655. (const :tag "Never" nil)
  2656. (const :tag "Always" t)))
  2657. (defcustom org-read-date-force-compatible-dates t
  2658. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2659. Depending on the system Emacs is running on, certain dates cannot
  2660. be represented with the type used internally to represent time.
  2661. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2662. correctly. Some systems allow for earlier dates, some for later,
  2663. some for both. One way to find out it to insert any date into an
  2664. Org buffer, putting the cursor on the year and hitting S-up and
  2665. S-down to test the range.
  2666. When this variable is set to t, the date/time prompt will not let
  2667. you specify dates outside the 1970-2037 range, so it is certain that
  2668. these dates will work in whatever version of Emacs you are
  2669. running, and also that you can move a file from one Emacs implementation
  2670. to another. WHenever Org is forcing the year for you, it will display
  2671. a message and beep.
  2672. When this variable is nil, Org will check if the date is
  2673. representable in the specific Emacs implementation you are using.
  2674. If not, it will force a year, usually the current year, and beep
  2675. to remind you. Currently this setting is not recommended because
  2676. the likelihood that you will open your Org files in an Emacs that
  2677. has limited date range is not negligible.
  2678. A workaround for this problem is to use diary sexp dates for time
  2679. stamps outside of this range."
  2680. :group 'org-time
  2681. :version "24.1"
  2682. :type 'boolean)
  2683. (defcustom org-read-date-display-live t
  2684. "Non-nil means display current interpretation of date prompt live.
  2685. This display will be in an overlay, in the minibuffer."
  2686. :group 'org-time
  2687. :type 'boolean)
  2688. (defcustom org-read-date-popup-calendar t
  2689. "Non-nil means pop up a calendar when prompting for a date.
  2690. In the calendar, the date can be selected with mouse-1. However, the
  2691. minibuffer will also be active, and you can simply enter the date as well.
  2692. When nil, only the minibuffer will be available."
  2693. :group 'org-time
  2694. :type 'boolean)
  2695. (if (fboundp 'defvaralias)
  2696. (defvaralias 'org-popup-calendar-for-date-prompt
  2697. 'org-read-date-popup-calendar))
  2698. (make-obsolete-variable
  2699. 'org-read-date-minibuffer-setup-hook
  2700. "Set `org-read-date-minibuffer-local-map' instead." "24.4")
  2701. (defcustom org-read-date-minibuffer-setup-hook nil
  2702. "Hook to be used to set up keys for the date/time interface.
  2703. Add key definitions to `minibuffer-local-map', which will be a
  2704. temporary copy.
  2705. WARNING: This option is obsolete, you should use
  2706. `org-read-date-minibuffer-local-map' to set up keys."
  2707. :group 'org-time
  2708. :type 'hook)
  2709. (defcustom org-extend-today-until 0
  2710. "The hour when your day really ends. Must be an integer.
  2711. This has influence for the following applications:
  2712. - When switching the agenda to \"today\". It it is still earlier than
  2713. the time given here, the day recognized as TODAY is actually yesterday.
  2714. - When a date is read from the user and it is still before the time given
  2715. here, the current date and time will be assumed to be yesterday, 23:59.
  2716. Also, timestamps inserted in capture templates follow this rule.
  2717. IMPORTANT: This is a feature whose implementation is and likely will
  2718. remain incomplete. Really, it is only here because past midnight seems to
  2719. be the favorite working time of John Wiegley :-)"
  2720. :group 'org-time
  2721. :type 'integer)
  2722. (defcustom org-use-effective-time nil
  2723. "If non-nil, consider `org-extend-today-until' when creating timestamps.
  2724. For example, if `org-extend-today-until' is 8, and it's 4am, then the
  2725. \"effective time\" of any timestamps between midnight and 8am will be
  2726. 23:59 of the previous day."
  2727. :group 'org-time
  2728. :version "24.1"
  2729. :type 'boolean)
  2730. (defcustom org-use-last-clock-out-time-as-effective-time nil
  2731. "When non-nil, use the last clock out time for `org-todo'.
  2732. Note that this option has precedence over the combined use of
  2733. `org-use-effective-time' and `org-extend-today-until'."
  2734. :group 'org-time
  2735. :version "24.4"
  2736. :package-version '(Org . "8.0")
  2737. :type 'boolean)
  2738. (defcustom org-edit-timestamp-down-means-later nil
  2739. "Non-nil means S-down will increase the time in a time stamp.
  2740. When nil, S-up will increase."
  2741. :group 'org-time
  2742. :type 'boolean)
  2743. (defcustom org-calendar-follow-timestamp-change t
  2744. "Non-nil means make the calendar window follow timestamp changes.
  2745. When a timestamp is modified and the calendar window is visible, it will be
  2746. moved to the new date."
  2747. :group 'org-time
  2748. :type 'boolean)
  2749. (defgroup org-tags nil
  2750. "Options concerning tags in Org-mode."
  2751. :tag "Org Tags"
  2752. :group 'org)
  2753. (defcustom org-tag-alist nil
  2754. "List of tags allowed in Org-mode files.
  2755. When this list is nil, Org-mode will base TAG input on what is already in the
  2756. buffer.
  2757. The value of this variable is an alist, the car of each entry must be a
  2758. keyword as a string, the cdr may be a character that is used to select
  2759. that tag through the fast-tag-selection interface.
  2760. See the manual for details."
  2761. :group 'org-tags
  2762. :type '(repeat
  2763. (choice
  2764. (cons (string :tag "Tag name")
  2765. (character :tag "Access char"))
  2766. (list :tag "Start radio group"
  2767. (const :startgroup)
  2768. (option (string :tag "Group description")))
  2769. (list :tag "End radio group"
  2770. (const :endgroup)
  2771. (option (string :tag "Group description")))
  2772. (const :tag "New line" (:newline)))))
  2773. (defcustom org-tag-persistent-alist nil
  2774. "List of tags that will always appear in all Org-mode files.
  2775. This is in addition to any in buffer settings or customizations
  2776. of `org-tag-alist'.
  2777. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2778. The value of this variable is an alist, the car of each entry must be a
  2779. keyword as a string, the cdr may be a character that is used to select
  2780. that tag through the fast-tag-selection interface.
  2781. See the manual for details.
  2782. To disable these tags on a per-file basis, insert anywhere in the file:
  2783. #+STARTUP: noptag"
  2784. :group 'org-tags
  2785. :type '(repeat
  2786. (choice
  2787. (cons (string :tag "Tag name")
  2788. (character :tag "Access char"))
  2789. (const :tag "Start radio group" (:startgroup))
  2790. (const :tag "End radio group" (:endgroup))
  2791. (const :tag "New line" (:newline)))))
  2792. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2793. "If non-nil, always offer completion for all tags of all agenda files.
  2794. Instead of customizing this variable directly, you might want to
  2795. set it locally for capture buffers, because there no list of
  2796. tags in that file can be created dynamically (there are none).
  2797. (add-hook 'org-capture-mode-hook
  2798. (lambda ()
  2799. (set (make-local-variable
  2800. 'org-complete-tags-always-offer-all-agenda-tags)
  2801. t)))"
  2802. :group 'org-tags
  2803. :version "24.1"
  2804. :type 'boolean)
  2805. (defvar org-file-tags nil
  2806. "List of tags that can be inherited by all entries in the file.
  2807. The tags will be inherited if the variable `org-use-tag-inheritance'
  2808. says they should be.
  2809. This variable is populated from #+FILETAGS lines.")
  2810. (defcustom org-use-fast-tag-selection 'auto
  2811. "Non-nil means use fast tag selection scheme.
  2812. This is a special interface to select and deselect tags with single keys.
  2813. When nil, fast selection is never used.
  2814. When the symbol `auto', fast selection is used if and only if selection
  2815. characters for tags have been configured, either through the variable
  2816. `org-tag-alist' or through a #+TAGS line in the buffer.
  2817. When t, fast selection is always used and selection keys are assigned
  2818. automatically if necessary."
  2819. :group 'org-tags
  2820. :type '(choice
  2821. (const :tag "Always" t)
  2822. (const :tag "Never" nil)
  2823. (const :tag "When selection characters are configured" 'auto)))
  2824. (defcustom org-fast-tag-selection-single-key nil
  2825. "Non-nil means fast tag selection exits after first change.
  2826. When nil, you have to press RET to exit it.
  2827. During fast tag selection, you can toggle this flag with `C-c'.
  2828. This variable can also have the value `expert'. In this case, the window
  2829. displaying the tags menu is not even shown, until you press C-c again."
  2830. :group 'org-tags
  2831. :type '(choice
  2832. (const :tag "No" nil)
  2833. (const :tag "Yes" t)
  2834. (const :tag "Expert" expert)))
  2835. (defvar org-fast-tag-selection-include-todo nil
  2836. "Non-nil means fast tags selection interface will also offer TODO states.
  2837. This is an undocumented feature, you should not rely on it.")
  2838. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2839. "The column to which tags should be indented in a headline.
  2840. If this number is positive, it specifies the column. If it is negative,
  2841. it means that the tags should be flushright to that column. For example,
  2842. -80 works well for a normal 80 character screen.
  2843. When 0, place tags directly after headline text, with only one space in
  2844. between."
  2845. :group 'org-tags
  2846. :type 'integer)
  2847. (defcustom org-auto-align-tags t
  2848. "Non-nil keeps tags aligned when modifying headlines.
  2849. Some operations (i.e. demoting) change the length of a headline and
  2850. therefore shift the tags around. With this option turned on, after
  2851. each such operation the tags are again aligned to `org-tags-column'."
  2852. :group 'org-tags
  2853. :type 'boolean)
  2854. (defcustom org-use-tag-inheritance t
  2855. "Non-nil means tags in levels apply also for sublevels.
  2856. When nil, only the tags directly given in a specific line apply there.
  2857. This may also be a list of tags that should be inherited, or a regexp that
  2858. matches tags that should be inherited. Additional control is possible
  2859. with the variable `org-tags-exclude-from-inheritance' which gives an
  2860. explicit list of tags to be excluded from inheritance, even if the value of
  2861. `org-use-tag-inheritance' would select it for inheritance.
  2862. If this option is t, a match early-on in a tree can lead to a large
  2863. number of matches in the subtree when constructing the agenda or creating
  2864. a sparse tree. If you only want to see the first match in a tree during
  2865. a search, check out the variable `org-tags-match-list-sublevels'."
  2866. :group 'org-tags
  2867. :type '(choice
  2868. (const :tag "Not" nil)
  2869. (const :tag "Always" t)
  2870. (repeat :tag "Specific tags" (string :tag "Tag"))
  2871. (regexp :tag "Tags matched by regexp")))
  2872. (defcustom org-tags-exclude-from-inheritance nil
  2873. "List of tags that should never be inherited.
  2874. This is a way to exclude a few tags from inheritance. For way to do
  2875. the opposite, to actively allow inheritance for selected tags,
  2876. see the variable `org-use-tag-inheritance'."
  2877. :group 'org-tags
  2878. :type '(repeat (string :tag "Tag")))
  2879. (defun org-tag-inherit-p (tag)
  2880. "Check if TAG is one that should be inherited."
  2881. (cond
  2882. ((member tag org-tags-exclude-from-inheritance) nil)
  2883. ((eq org-use-tag-inheritance t) t)
  2884. ((not org-use-tag-inheritance) nil)
  2885. ((stringp org-use-tag-inheritance)
  2886. (string-match org-use-tag-inheritance tag))
  2887. ((listp org-use-tag-inheritance)
  2888. (member tag org-use-tag-inheritance))
  2889. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2890. (defcustom org-tags-match-list-sublevels t
  2891. "Non-nil means list also sublevels of headlines matching a search.
  2892. This variable applies to tags/property searches, and also to stuck
  2893. projects because this search is based on a tags match as well.
  2894. When set to the symbol `indented', sublevels are indented with
  2895. leading dots.
  2896. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2897. the sublevels of a headline matching a tag search often also match
  2898. the same search. Listing all of them can create very long lists.
  2899. Setting this variable to nil causes subtrees of a match to be skipped.
  2900. This variable is semi-obsolete and probably should always be true. It
  2901. is better to limit inheritance to certain tags using the variables
  2902. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2903. :group 'org-tags
  2904. :type '(choice
  2905. (const :tag "No, don't list them" nil)
  2906. (const :tag "Yes, do list them" t)
  2907. (const :tag "List them, indented with leading dots" indented)))
  2908. (defcustom org-tags-sort-function nil
  2909. "When set, tags are sorted using this function as a comparator."
  2910. :group 'org-tags
  2911. :type '(choice
  2912. (const :tag "No sorting" nil)
  2913. (const :tag "Alphabetical" string<)
  2914. (const :tag "Reverse alphabetical" string>)
  2915. (function :tag "Custom function" nil)))
  2916. (defvar org-tags-history nil
  2917. "History of minibuffer reads for tags.")
  2918. (defvar org-last-tags-completion-table nil
  2919. "The last used completion table for tags.")
  2920. (defvar org-after-tags-change-hook nil
  2921. "Hook that is run after the tags in a line have changed.")
  2922. (defgroup org-properties nil
  2923. "Options concerning properties in Org-mode."
  2924. :tag "Org Properties"
  2925. :group 'org)
  2926. (defcustom org-property-format "%-10s %s"
  2927. "How property key/value pairs should be formatted by `indent-line'.
  2928. When `indent-line' hits a property definition, it will format the line
  2929. according to this format, mainly to make sure that the values are
  2930. lined-up with respect to each other."
  2931. :group 'org-properties
  2932. :type 'string)
  2933. (defcustom org-properties-postprocess-alist nil
  2934. "Alist of properties and functions to adjust inserted values.
  2935. Elements of this alist must be of the form
  2936. ([string] [function])
  2937. where [string] must be a property name and [function] must be a
  2938. lambda expression: this lambda expression must take one argument,
  2939. the value to adjust, and return the new value as a string.
  2940. For example, this element will allow the property \"Remaining\"
  2941. to be updated wrt the relation between the \"Effort\" property
  2942. and the clock summary:
  2943. ((\"Remaining\" (lambda(value)
  2944. (let ((clocksum (org-clock-sum-current-item))
  2945. (effort (org-duration-string-to-minutes
  2946. (org-entry-get (point) \"Effort\"))))
  2947. (org-minutes-to-clocksum-string (- effort clocksum))))))"
  2948. :group 'org-properties
  2949. :version "24.1"
  2950. :type '(alist :key-type (string :tag "Property")
  2951. :value-type (function :tag "Function")))
  2952. (defcustom org-use-property-inheritance nil
  2953. "Non-nil means properties apply also for sublevels.
  2954. This setting is chiefly used during property searches. Turning it on can
  2955. cause significant overhead when doing a search, which is why it is not
  2956. on by default.
  2957. When nil, only the properties directly given in the current entry count.
  2958. When t, every property is inherited. The value may also be a list of
  2959. properties that should have inheritance, or a regular expression matching
  2960. properties that should be inherited.
  2961. However, note that some special properties use inheritance under special
  2962. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2963. and the properties ending in \"_ALL\" when they are used as descriptor
  2964. for valid values of a property.
  2965. Note for programmers:
  2966. When querying an entry with `org-entry-get', you can control if inheritance
  2967. should be used. By default, `org-entry-get' looks only at the local
  2968. properties. You can request inheritance by setting the inherit argument
  2969. to t (to force inheritance) or to `selective' (to respect the setting
  2970. in this variable)."
  2971. :group 'org-properties
  2972. :type '(choice
  2973. (const :tag "Not" nil)
  2974. (const :tag "Always" t)
  2975. (repeat :tag "Specific properties" (string :tag "Property"))
  2976. (regexp :tag "Properties matched by regexp")))
  2977. (defun org-property-inherit-p (property)
  2978. "Check if PROPERTY is one that should be inherited."
  2979. (cond
  2980. ((eq org-use-property-inheritance t) t)
  2981. ((not org-use-property-inheritance) nil)
  2982. ((stringp org-use-property-inheritance)
  2983. (string-match org-use-property-inheritance property))
  2984. ((listp org-use-property-inheritance)
  2985. (member property org-use-property-inheritance))
  2986. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2987. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2988. "The default column format, if no other format has been defined.
  2989. This variable can be set on the per-file basis by inserting a line
  2990. #+COLUMNS: %25ITEM ....."
  2991. :group 'org-properties
  2992. :type 'string)
  2993. (defcustom org-columns-ellipses ".."
  2994. "The ellipses to be used when a field in column view is truncated.
  2995. When this is the empty string, as many characters as possible are shown,
  2996. but then there will be no visual indication that the field has been truncated.
  2997. When this is a string of length N, the last N characters of a truncated
  2998. field are replaced by this string. If the column is narrower than the
  2999. ellipses string, only part of the ellipses string will be shown."
  3000. :group 'org-properties
  3001. :type 'string)
  3002. (defcustom org-columns-modify-value-for-display-function nil
  3003. "Function that modifies values for display in column view.
  3004. For example, it can be used to cut out a certain part from a time stamp.
  3005. The function must take 2 arguments:
  3006. column-title The title of the column (*not* the property name)
  3007. value The value that should be modified.
  3008. The function should return the value that should be displayed,
  3009. or nil if the normal value should be used."
  3010. :group 'org-properties
  3011. :type 'function)
  3012. (defcustom org-effort-property "Effort"
  3013. "The property that is being used to keep track of effort estimates.
  3014. Effort estimates given in this property need to have the format H:MM."
  3015. :group 'org-properties
  3016. :group 'org-progress
  3017. :type '(string :tag "Property"))
  3018. (defconst org-global-properties-fixed
  3019. '(("VISIBILITY_ALL" . "folded children content all")
  3020. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  3021. "List of property/value pairs that can be inherited by any entry.
  3022. These are fixed values, for the preset properties. The user variable
  3023. that can be used to add to this list is `org-global-properties'.
  3024. The entries in this list are cons cells where the car is a property
  3025. name and cdr is a string with the value. If the value represents
  3026. multiple items like an \"_ALL\" property, separate the items by
  3027. spaces.")
  3028. (defcustom org-global-properties nil
  3029. "List of property/value pairs that can be inherited by any entry.
  3030. This list will be combined with the constant `org-global-properties-fixed'.
  3031. The entries in this list are cons cells where the car is a property
  3032. name and cdr is a string with the value.
  3033. You can set buffer-local values for the same purpose in the variable
  3034. `org-file-properties' this by adding lines like
  3035. #+PROPERTY: NAME VALUE"
  3036. :group 'org-properties
  3037. :type '(repeat
  3038. (cons (string :tag "Property")
  3039. (string :tag "Value"))))
  3040. (defvar org-file-properties nil
  3041. "List of property/value pairs that can be inherited by any entry.
  3042. Valid for the current buffer.
  3043. This variable is populated from #+PROPERTY lines.")
  3044. (make-variable-buffer-local 'org-file-properties)
  3045. (defgroup org-agenda nil
  3046. "Options concerning agenda views in Org-mode."
  3047. :tag "Org Agenda"
  3048. :group 'org)
  3049. (defvar org-category nil
  3050. "Variable used by org files to set a category for agenda display.
  3051. Such files should use a file variable to set it, for example
  3052. # -*- mode: org; org-category: \"ELisp\"
  3053. or contain a special line
  3054. #+CATEGORY: ELisp
  3055. If the file does not specify a category, then file's base name
  3056. is used instead.")
  3057. (make-variable-buffer-local 'org-category)
  3058. (put 'org-category 'safe-local-variable #'(lambda (x) (or (symbolp x) (stringp x))))
  3059. (defcustom org-agenda-files nil
  3060. "The files to be used for agenda display.
  3061. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  3062. \\[org-remove-file]. You can also use customize to edit the list.
  3063. If an entry is a directory, all files in that directory that are matched by
  3064. `org-agenda-file-regexp' will be part of the file list.
  3065. If the value of the variable is not a list but a single file name, then
  3066. the list of agenda files is actually stored and maintained in that file, one
  3067. agenda file per line. In this file paths can be given relative to
  3068. `org-directory'. Tilde expansion and environment variable substitution
  3069. are also made."
  3070. :group 'org-agenda
  3071. :type '(choice
  3072. (repeat :tag "List of files and directories" file)
  3073. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  3074. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  3075. "Regular expression to match files for `org-agenda-files'.
  3076. If any element in the list in that variable contains a directory instead
  3077. of a normal file, all files in that directory that are matched by this
  3078. regular expression will be included."
  3079. :group 'org-agenda
  3080. :type 'regexp)
  3081. (defcustom org-agenda-text-search-extra-files nil
  3082. "List of extra files to be searched by text search commands.
  3083. These files will be search in addition to the agenda files by the
  3084. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  3085. Note that these files will only be searched for text search commands,
  3086. not for the other agenda views like todo lists, tag searches or the weekly
  3087. agenda. This variable is intended to list notes and possibly archive files
  3088. that should also be searched by these two commands.
  3089. In fact, if the first element in the list is the symbol `agenda-archives',
  3090. than all archive files of all agenda files will be added to the search
  3091. scope."
  3092. :group 'org-agenda
  3093. :type '(set :greedy t
  3094. (const :tag "Agenda Archives" agenda-archives)
  3095. (repeat :inline t (file))))
  3096. (if (fboundp 'defvaralias)
  3097. (defvaralias 'org-agenda-multi-occur-extra-files
  3098. 'org-agenda-text-search-extra-files))
  3099. (defcustom org-agenda-skip-unavailable-files nil
  3100. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  3101. A nil value means to remove them, after a query, from the list."
  3102. :group 'org-agenda
  3103. :type 'boolean)
  3104. (defcustom org-calendar-to-agenda-key [?c]
  3105. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  3106. The command `org-calendar-goto-agenda' will be bound to this key. The
  3107. default is the character `c' because then `c' can be used to switch back and
  3108. forth between agenda and calendar."
  3109. :group 'org-agenda
  3110. :type 'sexp)
  3111. (defcustom org-calendar-insert-diary-entry-key [?i]
  3112. "The key to be installed in `calendar-mode-map' for adding diary entries.
  3113. This option is irrelevant until `org-agenda-diary-file' has been configured
  3114. to point to an Org-mode file. When that is the case, the command
  3115. `org-agenda-diary-entry' will be bound to the key given here, by default
  3116. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  3117. if you want to continue doing this, you need to change this to a different
  3118. key."
  3119. :group 'org-agenda
  3120. :type 'sexp)
  3121. (defcustom org-agenda-diary-file 'diary-file
  3122. "File to which to add new entries with the `i' key in agenda and calendar.
  3123. When this is the symbol `diary-file', the functionality in the Emacs
  3124. calendar will be used to add entries to the `diary-file'. But when this
  3125. points to a file, `org-agenda-diary-entry' will be used instead."
  3126. :group 'org-agenda
  3127. :type '(choice
  3128. (const :tag "The standard Emacs diary file" diary-file)
  3129. (file :tag "Special Org file diary entries")))
  3130. (eval-after-load "calendar"
  3131. '(progn
  3132. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  3133. 'org-calendar-goto-agenda)
  3134. (add-hook 'calendar-mode-hook
  3135. (lambda ()
  3136. (unless (eq org-agenda-diary-file 'diary-file)
  3137. (define-key calendar-mode-map
  3138. org-calendar-insert-diary-entry-key
  3139. 'org-agenda-diary-entry))))))
  3140. (defgroup org-latex nil
  3141. "Options for embedding LaTeX code into Org-mode."
  3142. :tag "Org LaTeX"
  3143. :group 'org)
  3144. (defcustom org-format-latex-options
  3145. '(:foreground default :background default :scale 1.0
  3146. :html-foreground "Black" :html-background "Transparent"
  3147. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  3148. "Options for creating images from LaTeX fragments.
  3149. This is a property list with the following properties:
  3150. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  3151. `default' means use the foreground of the default face.
  3152. `auto' means use the foreground from the text face.
  3153. :background the background color, or \"Transparent\".
  3154. `default' means use the background of the default face.
  3155. `auto' means use the background from the text face.
  3156. :scale a scaling factor for the size of the images, to get more pixels
  3157. :html-foreground, :html-background, :html-scale
  3158. the same numbers for HTML export.
  3159. :matchers a list indicating which matchers should be used to
  3160. find LaTeX fragments. Valid members of this list are:
  3161. \"begin\" find environments
  3162. \"$1\" find single characters surrounded by $.$
  3163. \"$\" find math expressions surrounded by $...$
  3164. \"$$\" find math expressions surrounded by $$....$$
  3165. \"\\(\" find math expressions surrounded by \\(...\\)
  3166. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  3167. :group 'org-latex
  3168. :type 'plist)
  3169. (defcustom org-format-latex-signal-error t
  3170. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  3171. When nil, just push out a message."
  3172. :group 'org-latex
  3173. :version "24.1"
  3174. :type 'boolean)
  3175. (defcustom org-latex-to-mathml-jar-file nil
  3176. "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
  3177. Use this to specify additional executable file say a jar file.
  3178. When using MathToWeb as the converter, specify the full-path to
  3179. your mathtoweb.jar file."
  3180. :group 'org-latex
  3181. :version "24.1"
  3182. :type '(choice
  3183. (const :tag "None" nil)
  3184. (file :tag "JAR file" :must-match t)))
  3185. (defcustom org-latex-to-mathml-convert-command nil
  3186. "Command to convert LaTeX fragments to MathML.
  3187. Replace format-specifiers in the command as noted below and use
  3188. `shell-command' to convert LaTeX to MathML.
  3189. %j: Executable file in fully expanded form as specified by
  3190. `org-latex-to-mathml-jar-file'.
  3191. %I: Input LaTeX file in fully expanded form
  3192. %o: Output MathML file
  3193. This command is used by `org-create-math-formula'.
  3194. When using MathToWeb as the converter, set this to
  3195. \"java -jar %j -unicode -force -df %o %I\"."
  3196. :group 'org-latex
  3197. :version "24.1"
  3198. :type '(choice
  3199. (const :tag "None" nil)
  3200. (string :tag "\nShell command")))
  3201. (defcustom org-latex-create-formula-image-program 'dvipng
  3202. "Program to convert LaTeX fragments with.
  3203. dvipng Process the LaTeX fragments to dvi file, then convert
  3204. dvi files to png files using dvipng.
  3205. This will also include processing of non-math environments.
  3206. imagemagick Convert the LaTeX fragments to pdf files and use imagemagick
  3207. to convert pdf files to png files"
  3208. :group 'org-latex
  3209. :version "24.1"
  3210. :type '(choice
  3211. (const :tag "dvipng" dvipng)
  3212. (const :tag "imagemagick" imagemagick)))
  3213. (defcustom org-latex-preview-ltxpng-directory "ltxpng/"
  3214. "Path to store latex preview images.
  3215. A relative path here creates many directories relative to the
  3216. processed org files paths. An absolute path puts all preview
  3217. images at the same place."
  3218. :group 'org-latex
  3219. :version "24.3"
  3220. :type 'string)
  3221. (defun org-format-latex-mathml-available-p ()
  3222. "Return t if `org-latex-to-mathml-convert-command' is usable."
  3223. (save-match-data
  3224. (when (and (boundp 'org-latex-to-mathml-convert-command)
  3225. org-latex-to-mathml-convert-command)
  3226. (let ((executable (car (split-string
  3227. org-latex-to-mathml-convert-command))))
  3228. (when (executable-find executable)
  3229. (if (string-match
  3230. "%j" org-latex-to-mathml-convert-command)
  3231. (file-readable-p org-latex-to-mathml-jar-file)
  3232. t))))))
  3233. (defcustom org-format-latex-header "\\documentclass{article}
  3234. \\usepackage[usenames]{color}
  3235. \\usepackage{amsmath}
  3236. \\usepackage[mathscr]{eucal}
  3237. \\pagestyle{empty} % do not remove
  3238. \[PACKAGES]
  3239. \[DEFAULT-PACKAGES]
  3240. % The settings below are copied from fullpage.sty
  3241. \\setlength{\\textwidth}{\\paperwidth}
  3242. \\addtolength{\\textwidth}{-3cm}
  3243. \\setlength{\\oddsidemargin}{1.5cm}
  3244. \\addtolength{\\oddsidemargin}{-2.54cm}
  3245. \\setlength{\\evensidemargin}{\\oddsidemargin}
  3246. \\setlength{\\textheight}{\\paperheight}
  3247. \\addtolength{\\textheight}{-\\headheight}
  3248. \\addtolength{\\textheight}{-\\headsep}
  3249. \\addtolength{\\textheight}{-\\footskip}
  3250. \\addtolength{\\textheight}{-3cm}
  3251. \\setlength{\\topmargin}{1.5cm}
  3252. \\addtolength{\\topmargin}{-2.54cm}"
  3253. "The document header used for processing LaTeX fragments.
  3254. It is imperative that this header make sure that no page number
  3255. appears on the page. The package defined in the variables
  3256. `org-latex-default-packages-alist' and `org-latex-packages-alist'
  3257. will either replace the placeholder \"[PACKAGES]\" in this
  3258. header, or they will be appended."
  3259. :group 'org-latex
  3260. :type 'string)
  3261. (defun org-set-packages-alist (var val)
  3262. "Set the packages alist and make sure it has 3 elements per entry."
  3263. (set var (mapcar (lambda (x)
  3264. (if (and (consp x) (= (length x) 2))
  3265. (list (car x) (nth 1 x) t)
  3266. x))
  3267. val)))
  3268. (defun org-get-packages-alist (var)
  3269. "Get the packages alist and make sure it has 3 elements per entry."
  3270. (mapcar (lambda (x)
  3271. (if (and (consp x) (= (length x) 2))
  3272. (list (car x) (nth 1 x) t)
  3273. x))
  3274. (default-value var)))
  3275. (defcustom org-latex-default-packages-alist
  3276. '(("AUTO" "inputenc" t)
  3277. ("T1" "fontenc" t)
  3278. ("" "fixltx2e" nil)
  3279. ("" "graphicx" t)
  3280. ("" "longtable" nil)
  3281. ("" "float" nil)
  3282. ("" "wrapfig" nil)
  3283. ("" "soul" t)
  3284. ("" "textcomp" t)
  3285. ("" "marvosym" t)
  3286. ("" "wasysym" t)
  3287. ("" "latexsym" t)
  3288. ("" "amssymb" t)
  3289. ("" "hyperref" nil)
  3290. "\\tolerance=1000")
  3291. "Alist of default packages to be inserted in the header.
  3292. Change this only if one of the packages here causes an
  3293. incompatibility with another package you are using.
  3294. The packages in this list are needed by one part or another of
  3295. Org mode to function properly:
  3296. - inputenc, fontenc: for basic font and character selection
  3297. - textcomp, marvosymb, wasysym, latexsym, amssym: for various
  3298. symbols used for interpreting the entities in `org-entities'.
  3299. You can skip some of these packages if you don't use any of the
  3300. symbols in it.
  3301. - graphicx: for including images
  3302. - float, wrapfig: for figure placement
  3303. - longtable: for long tables
  3304. - hyperref: for cross references
  3305. Therefore you should not modify this variable unless you know
  3306. what you are doing. The one reason to change it anyway is that
  3307. you might be loading some other package that conflicts with one
  3308. of the default packages. Each cell is of the format
  3309. \( \"options\" \"package\" snippet-flag). If SNIPPET-FLAG is t,
  3310. the package also needs to be included when compiling LaTeX
  3311. snippets into images for inclusion into non-LaTeX output."
  3312. :group 'org-latex
  3313. :group 'org-export-latex
  3314. :set 'org-set-packages-alist
  3315. :get 'org-get-packages-alist
  3316. :version "24.1"
  3317. :type '(repeat
  3318. (choice
  3319. (list :tag "options/package pair"
  3320. (string :tag "options")
  3321. (string :tag "package")
  3322. (boolean :tag "Snippet"))
  3323. (string :tag "A line of LaTeX"))))
  3324. (defcustom org-latex-packages-alist nil
  3325. "Alist of packages to be inserted in every LaTeX header.
  3326. These will be inserted after `org-latex-default-packages-alist'.
  3327. Each cell is of the format:
  3328. \(\"options\" \"package\" snippet-flag)
  3329. SNIPPET-FLAG, when t, indicates that this package is also needed
  3330. when turning LaTeX snippets into images for inclusion into
  3331. non-LaTeX output.
  3332. Make sure that you only list packages here which:
  3333. - you want in every file
  3334. - do not conflict with the setup in `org-format-latex-header'.
  3335. - do not conflict with the default packages in
  3336. `org-latex-default-packages-alist'."
  3337. :group 'org-latex
  3338. :group 'org-export-latex
  3339. :set 'org-set-packages-alist
  3340. :get 'org-get-packages-alist
  3341. :type '(repeat
  3342. (choice
  3343. (list :tag "options/package pair"
  3344. (string :tag "options")
  3345. (string :tag "package")
  3346. (boolean :tag "Snippet"))
  3347. (string :tag "A line of LaTeX"))))
  3348. (defgroup org-appearance nil
  3349. "Settings for Org-mode appearance."
  3350. :tag "Org Appearance"
  3351. :group 'org)
  3352. (defcustom org-level-color-stars-only nil
  3353. "Non-nil means fontify only the stars in each headline.
  3354. When nil, the entire headline is fontified.
  3355. Changing it requires restart of `font-lock-mode' to become effective
  3356. also in regions already fontified."
  3357. :group 'org-appearance
  3358. :type 'boolean)
  3359. (defcustom org-hide-leading-stars nil
  3360. "Non-nil means hide the first N-1 stars in a headline.
  3361. This works by using the face `org-hide' for these stars. This
  3362. face is white for a light background, and black for a dark
  3363. background. You may have to customize the face `org-hide' to
  3364. make this work.
  3365. Changing it requires restart of `font-lock-mode' to become effective
  3366. also in regions already fontified.
  3367. You may also set this on a per-file basis by adding one of the following
  3368. lines to the buffer:
  3369. #+STARTUP: hidestars
  3370. #+STARTUP: showstars"
  3371. :group 'org-appearance
  3372. :type 'boolean)
  3373. (defcustom org-hidden-keywords nil
  3374. "List of symbols corresponding to keywords to be hidden the org buffer.
  3375. For example, a value '(title) for this list will make the document's title
  3376. appear in the buffer without the initial #+TITLE: keyword."
  3377. :group 'org-appearance
  3378. :version "24.1"
  3379. :type '(set (const :tag "#+AUTHOR" author)
  3380. (const :tag "#+DATE" date)
  3381. (const :tag "#+EMAIL" email)
  3382. (const :tag "#+TITLE" title)))
  3383. (defcustom org-custom-properties nil
  3384. "List of properties (as strings) with a special meaning.
  3385. The default use of these custom properties is to let the user
  3386. hide them with `org-toggle-custom-properties-visibility'."
  3387. :group 'org-properties
  3388. :group 'org-appearance
  3389. :version "24.3"
  3390. :type '(repeat (string :tag "Property Name")))
  3391. (defcustom org-fontify-done-headline nil
  3392. "Non-nil means change the face of a headline if it is marked DONE.
  3393. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3394. When this is non-nil, the headline after the keyword is set to the
  3395. `org-headline-done' as an additional indication."
  3396. :group 'org-appearance
  3397. :type 'boolean)
  3398. (defcustom org-fontify-emphasized-text t
  3399. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3400. Changing this variable requires a restart of Emacs to take effect."
  3401. :group 'org-appearance
  3402. :type 'boolean)
  3403. (defcustom org-fontify-whole-heading-line nil
  3404. "Non-nil means fontify the whole line for headings.
  3405. This is useful when setting a background color for the
  3406. org-level-* faces."
  3407. :group 'org-appearance
  3408. :type 'boolean)
  3409. (defcustom org-highlight-latex-and-related nil
  3410. "Non-nil means highlight LaTeX related syntax in the buffer.
  3411. When non nil, the value should be a list containing any of the
  3412. following symbols:
  3413. `latex' Highlight LaTeX snippets and environments.
  3414. `script' Highlight subscript and superscript.
  3415. `entities' Highlight entities."
  3416. :group 'org-appearance
  3417. :version "24.4"
  3418. :package-version '(Org . "8.0")
  3419. :type '(choice
  3420. (const :tag "No highlighting" nil)
  3421. (set :greedy t :tag "Highlight"
  3422. (const :tag "LaTeX snippets and environments" latex)
  3423. (const :tag "Subscript and superscript" script)
  3424. (const :tag "Entities" entities))))
  3425. (defcustom org-hide-emphasis-markers nil
  3426. "Non-nil mean font-lock should hide the emphasis marker characters."
  3427. :group 'org-appearance
  3428. :type 'boolean)
  3429. (defcustom org-pretty-entities nil
  3430. "Non-nil means show entities as UTF8 characters.
  3431. When nil, the \\name form remains in the buffer."
  3432. :group 'org-appearance
  3433. :version "24.1"
  3434. :type 'boolean)
  3435. (defcustom org-pretty-entities-include-sub-superscripts t
  3436. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  3437. :group 'org-appearance
  3438. :version "24.1"
  3439. :type 'boolean)
  3440. (defvar org-emph-re nil
  3441. "Regular expression for matching emphasis.
  3442. After a match, the match groups contain these elements:
  3443. 0 The match of the full regular expression, including the characters
  3444. before and after the proper match
  3445. 1 The character before the proper match, or empty at beginning of line
  3446. 2 The proper match, including the leading and trailing markers
  3447. 3 The leading marker like * or /, indicating the type of highlighting
  3448. 4 The text between the emphasis markers, not including the markers
  3449. 5 The character after the match, empty at the end of a line")
  3450. (defvar org-verbatim-re nil
  3451. "Regular expression for matching verbatim text.")
  3452. (defvar org-emphasis-regexp-components) ; defined just below
  3453. (defvar org-emphasis-alist) ; defined just below
  3454. (defun org-set-emph-re (var val)
  3455. "Set variable and compute the emphasis regular expression."
  3456. (set var val)
  3457. (when (and (boundp 'org-emphasis-alist)
  3458. (boundp 'org-emphasis-regexp-components)
  3459. org-emphasis-alist org-emphasis-regexp-components)
  3460. (let* ((e org-emphasis-regexp-components)
  3461. (pre (car e))
  3462. (post (nth 1 e))
  3463. (border (nth 2 e))
  3464. (body (nth 3 e))
  3465. (nl (nth 4 e))
  3466. (body1 (concat body "*?"))
  3467. (markers (mapconcat 'car org-emphasis-alist ""))
  3468. (vmarkers (mapconcat
  3469. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  3470. org-emphasis-alist "")))
  3471. ;; make sure special characters appear at the right position in the class
  3472. (if (string-match "\\^" markers)
  3473. (setq markers (concat (replace-match "" t t markers) "^")))
  3474. (if (string-match "-" markers)
  3475. (setq markers (concat (replace-match "" t t markers) "-")))
  3476. (if (string-match "\\^" vmarkers)
  3477. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3478. (if (string-match "-" vmarkers)
  3479. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3480. (if (> nl 0)
  3481. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3482. (int-to-string nl) "\\}")))
  3483. ;; Make the regexp
  3484. (setq org-emph-re
  3485. (concat "\\([" pre "]\\|^\\)"
  3486. "\\("
  3487. "\\([" markers "]\\)"
  3488. "\\("
  3489. "[^" border "]\\|"
  3490. "[^" border "]"
  3491. body1
  3492. "[^" border "]"
  3493. "\\)"
  3494. "\\3\\)"
  3495. "\\([" post "]\\|$\\)"))
  3496. (setq org-verbatim-re
  3497. (concat "\\([" pre "]\\|^\\)"
  3498. "\\("
  3499. "\\([" vmarkers "]\\)"
  3500. "\\("
  3501. "[^" border "]\\|"
  3502. "[^" border "]"
  3503. body1
  3504. "[^" border "]"
  3505. "\\)"
  3506. "\\3\\)"
  3507. "\\([" post "]\\|$\\)")))))
  3508. (defcustom org-emphasis-regexp-components
  3509. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  3510. "Components used to build the regular expression for emphasis.
  3511. This is a list with five entries. Terminology: In an emphasis string
  3512. like \" *strong word* \", we call the initial space PREMATCH, the final
  3513. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3514. and \"trong wor\" is the body. The different components in this variable
  3515. specify what is allowed/forbidden in each part:
  3516. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3517. post Chars allowed as postmatch. End of line will be allowed too.
  3518. border The chars *forbidden* as border characters.
  3519. body-regexp A regexp like \".\" to match a body character. Don't use
  3520. non-shy groups here, and don't allow newline here.
  3521. newline The maximum number of newlines allowed in an emphasis exp.
  3522. Use customize to modify this, or restart Emacs after changing it."
  3523. :group 'org-appearance
  3524. :set 'org-set-emph-re
  3525. :type '(list
  3526. (sexp :tag "Allowed chars in pre ")
  3527. (sexp :tag "Allowed chars in post ")
  3528. (sexp :tag "Forbidden chars in border ")
  3529. (sexp :tag "Regexp for body ")
  3530. (integer :tag "number of newlines allowed")
  3531. (option (boolean :tag "Please ignore this button"))))
  3532. (defcustom org-emphasis-alist
  3533. `(("*" bold "<b>" "</b>")
  3534. ("/" italic "<i>" "</i>")
  3535. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  3536. ("=" org-code "<code>" "</code>" verbatim)
  3537. ("~" org-verbatim "<code>" "</code>" verbatim)
  3538. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  3539. "<del>" "</del>")
  3540. )
  3541. "Special syntax for emphasized text.
  3542. Text starting and ending with a special character will be emphasized, for
  3543. example *bold*, _underlined_ and /italic/. This variable sets the marker
  3544. characters, the face to be used by font-lock for highlighting in Org-mode
  3545. Emacs buffers, and the HTML tags to be used for this.
  3546. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  3547. Use customize to modify this, or restart Emacs after changing it."
  3548. :group 'org-appearance
  3549. :set 'org-set-emph-re
  3550. :type '(repeat
  3551. (list
  3552. (string :tag "Marker character")
  3553. (choice
  3554. (face :tag "Font-lock-face")
  3555. (plist :tag "Face property list"))
  3556. (string :tag "HTML start tag")
  3557. (string :tag "HTML end tag")
  3558. (option (const verbatim)))))
  3559. (defvar org-protecting-blocks
  3560. '("src" "example" "latex" "ascii" "html" "ditaa" "dot" "r" "R")
  3561. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3562. This is needed for font-lock setup.")
  3563. ;;; Miscellaneous options
  3564. (defgroup org-completion nil
  3565. "Completion in Org-mode."
  3566. :tag "Org Completion"
  3567. :group 'org)
  3568. (defcustom org-completion-use-ido nil
  3569. "Non-nil means use ido completion wherever possible.
  3570. Note that `ido-mode' must be active for this variable to be relevant.
  3571. If you decide to turn this variable on, you might well want to turn off
  3572. `org-outline-path-complete-in-steps'.
  3573. See also `org-completion-use-iswitchb'."
  3574. :group 'org-completion
  3575. :type 'boolean)
  3576. (defcustom org-completion-use-iswitchb nil
  3577. "Non-nil means use iswitchb completion wherever possible.
  3578. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3579. If you decide to turn this variable on, you might well want to turn off
  3580. `org-outline-path-complete-in-steps'.
  3581. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3582. :group 'org-completion
  3583. :type 'boolean)
  3584. (defcustom org-completion-fallback-command 'hippie-expand
  3585. "The expansion command called by \\[pcomplete] in normal context.
  3586. Normal means, no org-mode-specific context."
  3587. :group 'org-completion
  3588. :type 'function)
  3589. ;;; Functions and variables from their packages
  3590. ;; Declared here to avoid compiler warnings
  3591. ;; XEmacs only
  3592. (defvar outline-mode-menu-heading)
  3593. (defvar outline-mode-menu-show)
  3594. (defvar outline-mode-menu-hide)
  3595. (defvar zmacs-regions) ; XEmacs regions
  3596. ;; Emacs only
  3597. (defvar mark-active)
  3598. ;; Various packages
  3599. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  3600. (declare-function calendar-forward-day "cal-move" (arg))
  3601. (declare-function calendar-goto-date "cal-move" (date))
  3602. (declare-function calendar-goto-today "cal-move" ())
  3603. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3604. (defvar calc-embedded-close-formula)
  3605. (defvar calc-embedded-open-formula)
  3606. (declare-function cdlatex-tab "ext:cdlatex" ())
  3607. (declare-function cdlatex-compute-tables "ext:cdlatex" ())
  3608. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3609. (defvar font-lock-unfontify-region-function)
  3610. (declare-function iswitchb-read-buffer "iswitchb"
  3611. (prompt &optional default require-match start matches-set))
  3612. (defvar iswitchb-temp-buflist)
  3613. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3614. (defvar org-agenda-tags-todo-honor-ignore-options)
  3615. (declare-function org-agenda-skip "org-agenda" ())
  3616. (declare-function
  3617. org-agenda-format-item "org-agenda"
  3618. (extra txt &optional level category tags dotime noprefix remove-re habitp))
  3619. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3620. (declare-function org-agenda-change-all-lines "org-agenda"
  3621. (newhead hdmarker &optional fixface just-this))
  3622. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3623. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3624. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3625. (beg end))
  3626. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3627. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3628. "org-agenda" (&optional end))
  3629. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3630. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3631. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3632. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3633. (declare-function org-indent-mode "org-indent" (&optional arg))
  3634. (declare-function parse-time-string "parse-time" (string))
  3635. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3636. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3637. (defvar remember-data-file)
  3638. (defvar texmathp-why)
  3639. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3640. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3641. (defvar org-latex-regexps)
  3642. ;;; Autoload and prepare some org modules
  3643. ;; Some table stuff that needs to be defined here, because it is used
  3644. ;; by the functions setting up org-mode or checking for table context.
  3645. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  3646. "Detect an org-type or table-type table.")
  3647. (defconst org-table-line-regexp "^[ \t]*|"
  3648. "Detect an org-type table line.")
  3649. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  3650. "Detect an org-type table line.")
  3651. (defconst org-table-hline-regexp "^[ \t]*|-"
  3652. "Detect an org-type table hline.")
  3653. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  3654. "Detect a table-type table hline.")
  3655. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  3656. "Detect the first line outside a table when searching from within it.
  3657. This works for both table types.")
  3658. ;; Autoload the functions in org-table.el that are needed by functions here.
  3659. (eval-and-compile
  3660. (org-autoload "org-table"
  3661. '(org-table-begin org-table-blank-field org-table-end)))
  3662. ;;;###autoload
  3663. (defun turn-on-orgtbl ()
  3664. "Unconditionally turn on `orgtbl-mode'."
  3665. (require 'org-table)
  3666. (orgtbl-mode 1))
  3667. (defun org-at-table-p (&optional table-type)
  3668. "Return t if the cursor is inside an org-type table.
  3669. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  3670. (if org-enable-table-editor
  3671. (save-excursion
  3672. (beginning-of-line 1)
  3673. (looking-at (if table-type org-table-any-line-regexp
  3674. org-table-line-regexp)))
  3675. nil))
  3676. (defsubst org-table-p () (org-at-table-p))
  3677. (defun org-at-table.el-p ()
  3678. "Return t if and only if we are at a table.el table."
  3679. (and (org-at-table-p 'any)
  3680. (save-excursion
  3681. (goto-char (org-table-begin 'any))
  3682. (looking-at org-table1-hline-regexp))))
  3683. (defun org-table-recognize-table.el ()
  3684. "If there is a table.el table nearby, recognize it and move into it."
  3685. (if org-table-tab-recognizes-table.el
  3686. (if (org-at-table.el-p)
  3687. (progn
  3688. (beginning-of-line 1)
  3689. (if (looking-at org-table-dataline-regexp)
  3690. nil
  3691. (if (looking-at org-table1-hline-regexp)
  3692. (progn
  3693. (beginning-of-line 2)
  3694. (if (looking-at org-table-any-border-regexp)
  3695. (beginning-of-line -1)))))
  3696. (if (re-search-forward "|" (org-table-end t) t)
  3697. (progn
  3698. (require 'table)
  3699. (if (table--at-cell-p (point))
  3700. t
  3701. (message "recognizing table.el table...")
  3702. (table-recognize-table)
  3703. (message "recognizing table.el table...done")))
  3704. (error "This should not happen"))
  3705. t)
  3706. nil)
  3707. nil))
  3708. (defun org-at-table-hline-p ()
  3709. "Return t if the cursor is inside a hline in a table."
  3710. (if org-enable-table-editor
  3711. (save-excursion
  3712. (beginning-of-line 1)
  3713. (looking-at org-table-hline-regexp))
  3714. nil))
  3715. (defvar org-table-clean-did-remove-column nil)
  3716. (defun org-table-map-tables (function &optional quietly)
  3717. "Apply FUNCTION to the start of all tables in the buffer."
  3718. (save-excursion
  3719. (save-restriction
  3720. (widen)
  3721. (goto-char (point-min))
  3722. (while (re-search-forward org-table-any-line-regexp nil t)
  3723. (unless quietly
  3724. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
  3725. (beginning-of-line 1)
  3726. (when (and (looking-at org-table-line-regexp)
  3727. ;; Exclude tables in src/example/verbatim/clocktable blocks
  3728. (not (org-in-block-p '("src" "example" "verbatim" "clocktable"))))
  3729. (save-excursion (funcall function))
  3730. (or (looking-at org-table-line-regexp)
  3731. (forward-char 1)))
  3732. (re-search-forward org-table-any-border-regexp nil 1))))
  3733. (unless quietly (message "Mapping tables: done")))
  3734. ;; Declare and autoload functions from ox.el and al.
  3735. (declare-function org-export-get-environment "ox"
  3736. (&optional backend subtreep ext-plist))
  3737. (declare-function org-latex-guess-inputenc "ox-latex" (header))
  3738. ;; Declare and autoload functions from org-agenda.el
  3739. (eval-and-compile
  3740. (org-autoload "org-agenda"
  3741. '(org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  3742. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock" (beg end))
  3743. (declare-function org-clock-update-mode-line "org-clock" ())
  3744. (declare-function org-resolve-clocks "org-clock"
  3745. (&optional also-non-dangling-p prompt last-valid))
  3746. (defvar org-clock-start-time)
  3747. (defvar org-clock-marker (make-marker)
  3748. "Marker recording the last clock-in.")
  3749. (defvar org-clock-hd-marker (make-marker)
  3750. "Marker recording the last clock-in, but the headline position.")
  3751. (defvar org-clock-heading ""
  3752. "The heading of the current clock entry.")
  3753. (defun org-clock-is-active ()
  3754. "Return non-nil if clock is currently running.
  3755. The return value is actually the clock marker."
  3756. (marker-buffer org-clock-marker))
  3757. (eval-and-compile
  3758. (org-autoload "org-clock" '(org-clock-remove-overlays
  3759. org-clock-update-time-maybe
  3760. org-clocktable-shift)))
  3761. (defun org-check-running-clock ()
  3762. "Check if the current buffer contains the running clock.
  3763. If yes, offer to stop it and to save the buffer with the changes."
  3764. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3765. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3766. (buffer-name))))
  3767. (org-clock-out)
  3768. (when (y-or-n-p "Save changed buffer?")
  3769. (save-buffer))))
  3770. (defun org-clocktable-try-shift (dir n)
  3771. "Check if this line starts a clock table, if yes, shift the time block."
  3772. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  3773. (org-clocktable-shift dir n)))
  3774. ;;;###autoload
  3775. (defun org-clock-persistence-insinuate ()
  3776. "Set up hooks for clock persistence."
  3777. (require 'org-clock)
  3778. (add-hook 'org-mode-hook 'org-clock-load)
  3779. (add-hook 'kill-emacs-hook 'org-clock-save))
  3780. ;; Define the variable already here, to make sure we have it.
  3781. (defvar org-indent-mode nil
  3782. "Non-nil if Org-Indent mode is enabled.
  3783. Use the command `org-indent-mode' to change this variable.")
  3784. ;; Autoload archiving code
  3785. ;; The stuff that is needed for cycling and tags has to be defined here.
  3786. (defgroup org-archive nil
  3787. "Options concerning archiving in Org-mode."
  3788. :tag "Org Archive"
  3789. :group 'org-structure)
  3790. (defcustom org-archive-location "%s_archive::"
  3791. "The location where subtrees should be archived.
  3792. The value of this variable is a string, consisting of two parts,
  3793. separated by a double-colon. The first part is a filename and
  3794. the second part is a headline.
  3795. When the filename is omitted, archiving happens in the same file.
  3796. %s in the filename will be replaced by the current file
  3797. name (without the directory part). Archiving to a different file
  3798. is useful to keep archived entries from contributing to the
  3799. Org-mode Agenda.
  3800. The archived entries will be filed as subtrees of the specified
  3801. headline. When the headline is omitted, the subtrees are simply
  3802. filed away at the end of the file, as top-level entries. Also in
  3803. the heading you can use %s to represent the file name, this can be
  3804. useful when using the same archive for a number of different files.
  3805. Here are a few examples:
  3806. \"%s_archive::\"
  3807. If the current file is Projects.org, archive in file
  3808. Projects.org_archive, as top-level trees. This is the default.
  3809. \"::* Archived Tasks\"
  3810. Archive in the current file, under the top-level headline
  3811. \"* Archived Tasks\".
  3812. \"~/org/archive.org::\"
  3813. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3814. \"~/org/archive.org::* From %s\"
  3815. Archive in file ~/org/archive.org (absolute path), under headlines
  3816. \"From FILENAME\" where file name is the current file name.
  3817. \"~/org/datetree.org::datetree/* Finished Tasks\"
  3818. The \"datetree/\" string is special, signifying to archive
  3819. items to the datetree. Items are placed in either the CLOSED
  3820. date of the item, or the current date if there is no CLOSED date.
  3821. The heading will be a subentry to the current date. There doesn't
  3822. need to be a heading, but there always needs to be a slash after
  3823. datetree. For example, to store archived items directly in the
  3824. datetree, use \"~/org/datetree.org::datetree/\".
  3825. \"basement::** Finished Tasks\"
  3826. Archive in file ./basement (relative path), as level 3 trees
  3827. below the level 2 heading \"** Finished Tasks\".
  3828. You may set this option on a per-file basis by adding to the buffer a
  3829. line like
  3830. #+ARCHIVE: basement::** Finished Tasks
  3831. You may also define it locally for a subtree by setting an ARCHIVE property
  3832. in the entry. If such a property is found in an entry, or anywhere up
  3833. the hierarchy, it will be used."
  3834. :group 'org-archive
  3835. :type 'string)
  3836. (defcustom org-archive-tag "ARCHIVE"
  3837. "The tag that marks a subtree as archived.
  3838. An archived subtree does not open during visibility cycling, and does
  3839. not contribute to the agenda listings.
  3840. After changing this, font-lock must be restarted in the relevant buffers to
  3841. get the proper fontification."
  3842. :group 'org-archive
  3843. :group 'org-keywords
  3844. :type 'string)
  3845. (defcustom org-agenda-skip-archived-trees t
  3846. "Non-nil means the agenda will skip any items located in archived trees.
  3847. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3848. variable is no longer recommended, you should leave it at the value t.
  3849. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3850. :group 'org-archive
  3851. :group 'org-agenda-skip
  3852. :type 'boolean)
  3853. (defcustom org-columns-skip-archived-trees t
  3854. "Non-nil means ignore archived trees when creating column view."
  3855. :group 'org-archive
  3856. :group 'org-properties
  3857. :type 'boolean)
  3858. (defcustom org-cycle-open-archived-trees nil
  3859. "Non-nil means `org-cycle' will open archived trees.
  3860. An archived tree is a tree marked with the tag ARCHIVE.
  3861. When nil, archived trees will stay folded. You can still open them with
  3862. normal outline commands like `show-all', but not with the cycling commands."
  3863. :group 'org-archive
  3864. :group 'org-cycle
  3865. :type 'boolean)
  3866. (defcustom org-sparse-tree-open-archived-trees nil
  3867. "Non-nil means sparse tree construction shows matches in archived trees.
  3868. When nil, matches in these trees are highlighted, but the trees are kept in
  3869. collapsed state."
  3870. :group 'org-archive
  3871. :group 'org-sparse-trees
  3872. :type 'boolean)
  3873. (defcustom org-sparse-tree-default-date-type 'scheduled-or-deadline
  3874. "The default date type when building a sparse tree.
  3875. When this is nil, a date is a scheduled or a deadline timestamp.
  3876. Otherwise, these types are allowed:
  3877. all: all timestamps
  3878. active: only active timestamps (<...>)
  3879. inactive: only inactive timestamps (<...)
  3880. scheduled: only scheduled timestamps
  3881. deadline: only deadline timestamps"
  3882. :type '(choice (const :tag "Scheduled or deadline" 'scheduled-or-deadline)
  3883. (const :tag "All timestamps" all)
  3884. (const :tag "Only active timestamps" active)
  3885. (const :tag "Only inactive timestamps" inactive)
  3886. (const :tag "Only scheduled timestamps" scheduled)
  3887. (const :tag "Only deadline timestamps" deadline))
  3888. :version "24.3"
  3889. :group 'org-sparse-trees)
  3890. (defun org-cycle-hide-archived-subtrees (state)
  3891. "Re-hide all archived subtrees after a visibility state change."
  3892. (when (and (not org-cycle-open-archived-trees)
  3893. (not (memq state '(overview folded))))
  3894. (save-excursion
  3895. (let* ((globalp (memq state '(contents all)))
  3896. (beg (if globalp (point-min) (point)))
  3897. (end (if globalp (point-max) (org-end-of-subtree t))))
  3898. (org-hide-archived-subtrees beg end)
  3899. (goto-char beg)
  3900. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3901. (message "%s" (substitute-command-keys
  3902. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3903. (defun org-force-cycle-archived ()
  3904. "Cycle subtree even if it is archived."
  3905. (interactive)
  3906. (setq this-command 'org-cycle)
  3907. (let ((org-cycle-open-archived-trees t))
  3908. (call-interactively 'org-cycle)))
  3909. (defun org-hide-archived-subtrees (beg end)
  3910. "Re-hide all archived subtrees after a visibility state change."
  3911. (save-excursion
  3912. (let* ((re (concat ":" org-archive-tag ":")))
  3913. (goto-char beg)
  3914. (while (re-search-forward re end t)
  3915. (when (org-at-heading-p)
  3916. (org-flag-subtree t)
  3917. (org-end-of-subtree t))))))
  3918. (declare-function outline-end-of-heading "outline" ())
  3919. (declare-function outline-flag-region "outline" (from to flag))
  3920. (defun org-flag-subtree (flag)
  3921. (save-excursion
  3922. (org-back-to-heading t)
  3923. (outline-end-of-heading)
  3924. (outline-flag-region (point)
  3925. (progn (org-end-of-subtree t) (point))
  3926. flag)))
  3927. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3928. (eval-and-compile
  3929. (org-autoload "org-archive"
  3930. '(org-add-archive-files)))
  3931. ;; Autoload Column View Code
  3932. (declare-function org-columns-number-to-string "org-colview" (n fmt &optional printf))
  3933. (declare-function org-columns-get-format-and-top-level "org-colview" ())
  3934. (declare-function org-columns-compute "org-colview" (property))
  3935. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  3936. '(org-columns-number-to-string
  3937. org-columns-get-format-and-top-level
  3938. org-columns-compute
  3939. org-columns-remove-overlays))
  3940. ;; Autoload ID code
  3941. (declare-function org-id-store-link "org-id")
  3942. (declare-function org-id-locations-load "org-id")
  3943. (declare-function org-id-locations-save "org-id")
  3944. (defvar org-id-track-globally)
  3945. (org-autoload "org-id"
  3946. '(org-id-new
  3947. org-id-copy
  3948. org-id-get-with-outline-path-completion
  3949. org-id-get-with-outline-drilling))
  3950. ;;; Variables for pre-computed regular expressions, all buffer local
  3951. (defvar org-drawer-regexp "^[ \t]*:PROPERTIES:[ \t]*$"
  3952. "Matches first line of a hidden block.")
  3953. (make-variable-buffer-local 'org-drawer-regexp)
  3954. (defvar org-todo-regexp nil
  3955. "Matches any of the TODO state keywords.")
  3956. (make-variable-buffer-local 'org-todo-regexp)
  3957. (defvar org-not-done-regexp nil
  3958. "Matches any of the TODO state keywords except the last one.")
  3959. (make-variable-buffer-local 'org-not-done-regexp)
  3960. (defvar org-not-done-heading-regexp nil
  3961. "Matches a TODO headline that is not done.")
  3962. (make-variable-buffer-local 'org-not-done-regexp)
  3963. (defvar org-todo-line-regexp nil
  3964. "Matches a headline and puts TODO state into group 2 if present.")
  3965. (make-variable-buffer-local 'org-todo-line-regexp)
  3966. (defvar org-complex-heading-regexp nil
  3967. "Matches a headline and puts everything into groups:
  3968. group 1: the stars
  3969. group 2: The todo keyword, maybe
  3970. group 3: Priority cookie
  3971. group 4: True headline
  3972. group 5: Tags")
  3973. (make-variable-buffer-local 'org-complex-heading-regexp)
  3974. (defvar org-complex-heading-regexp-format nil
  3975. "Printf format to make regexp to match an exact headline.
  3976. This regexp will match the headline of any node which has the
  3977. exact headline text that is put into the format, but may have any
  3978. TODO state, priority and tags.")
  3979. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  3980. (defvar org-todo-line-tags-regexp nil
  3981. "Matches a headline and puts TODO state into group 2 if present.
  3982. Also put tags into group 4 if tags are present.")
  3983. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3984. (defvar org-ds-keyword-length 12
  3985. "Maximum length of the DEADLINE and SCHEDULED keywords.")
  3986. (make-variable-buffer-local 'org-ds-keyword-length)
  3987. (defvar org-deadline-regexp nil
  3988. "Matches the DEADLINE keyword.")
  3989. (make-variable-buffer-local 'org-deadline-regexp)
  3990. (defvar org-deadline-time-regexp nil
  3991. "Matches the DEADLINE keyword together with a time stamp.")
  3992. (make-variable-buffer-local 'org-deadline-time-regexp)
  3993. (defvar org-deadline-line-regexp nil
  3994. "Matches the DEADLINE keyword and the rest of the line.")
  3995. (make-variable-buffer-local 'org-deadline-line-regexp)
  3996. (defvar org-scheduled-regexp nil
  3997. "Matches the SCHEDULED keyword.")
  3998. (make-variable-buffer-local 'org-scheduled-regexp)
  3999. (defvar org-scheduled-time-regexp nil
  4000. "Matches the SCHEDULED keyword together with a time stamp.")
  4001. (make-variable-buffer-local 'org-scheduled-time-regexp)
  4002. (defvar org-closed-time-regexp nil
  4003. "Matches the CLOSED keyword together with a time stamp.")
  4004. (make-variable-buffer-local 'org-closed-time-regexp)
  4005. (defvar org-keyword-time-regexp nil
  4006. "Matches any of the 4 keywords, together with the time stamp.")
  4007. (make-variable-buffer-local 'org-keyword-time-regexp)
  4008. (defvar org-keyword-time-not-clock-regexp nil
  4009. "Matches any of the 3 keywords, together with the time stamp.")
  4010. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  4011. (defvar org-maybe-keyword-time-regexp nil
  4012. "Matches a timestamp, possibly preceded by a keyword.")
  4013. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  4014. (defvar org-all-time-keywords nil
  4015. "List of time keywords.")
  4016. (make-variable-buffer-local 'org-all-time-keywords)
  4017. (defconst org-plain-time-of-day-regexp
  4018. (concat
  4019. "\\(\\<[012]?[0-9]"
  4020. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4021. "\\(--?"
  4022. "\\(\\<[012]?[0-9]"
  4023. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4024. "\\)?")
  4025. "Regular expression to match a plain time or time range.
  4026. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4027. groups carry important information:
  4028. 0 the full match
  4029. 1 the first time, range or not
  4030. 8 the second time, if it is a range.")
  4031. (defconst org-plain-time-extension-regexp
  4032. (concat
  4033. "\\(\\<[012]?[0-9]"
  4034. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4035. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  4036. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  4037. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4038. groups carry important information:
  4039. 0 the full match
  4040. 7 hours of duration
  4041. 9 minutes of duration")
  4042. (defconst org-stamp-time-of-day-regexp
  4043. (concat
  4044. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  4045. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  4046. "\\(--?"
  4047. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  4048. "Regular expression to match a timestamp time or time range.
  4049. After a match, the following groups carry important information:
  4050. 0 the full match
  4051. 1 date plus weekday, for back referencing to make sure both times are on the same day
  4052. 2 the first time, range or not
  4053. 4 the second time, if it is a range.")
  4054. (defconst org-startup-options
  4055. '(("fold" org-startup-folded t)
  4056. ("overview" org-startup-folded t)
  4057. ("nofold" org-startup-folded nil)
  4058. ("showall" org-startup-folded nil)
  4059. ("showeverything" org-startup-folded showeverything)
  4060. ("content" org-startup-folded content)
  4061. ("indent" org-startup-indented t)
  4062. ("noindent" org-startup-indented nil)
  4063. ("hidestars" org-hide-leading-stars t)
  4064. ("showstars" org-hide-leading-stars nil)
  4065. ("odd" org-odd-levels-only t)
  4066. ("oddeven" org-odd-levels-only nil)
  4067. ("align" org-startup-align-all-tables t)
  4068. ("noalign" org-startup-align-all-tables nil)
  4069. ("inlineimages" org-startup-with-inline-images t)
  4070. ("noinlineimages" org-startup-with-inline-images nil)
  4071. ("latexpreview" org-startup-with-latex-preview t)
  4072. ("nolatexpreview" org-startup-with-latex-preview nil)
  4073. ("customtime" org-display-custom-times t)
  4074. ("logdone" org-log-done time)
  4075. ("lognotedone" org-log-done note)
  4076. ("nologdone" org-log-done nil)
  4077. ("lognoteclock-out" org-log-note-clock-out t)
  4078. ("nolognoteclock-out" org-log-note-clock-out nil)
  4079. ("logrepeat" org-log-repeat state)
  4080. ("lognoterepeat" org-log-repeat note)
  4081. ("logdrawer" org-log-into-drawer t)
  4082. ("nologdrawer" org-log-into-drawer nil)
  4083. ("logstatesreversed" org-log-states-order-reversed t)
  4084. ("nologstatesreversed" org-log-states-order-reversed nil)
  4085. ("nologrepeat" org-log-repeat nil)
  4086. ("logreschedule" org-log-reschedule time)
  4087. ("lognotereschedule" org-log-reschedule note)
  4088. ("nologreschedule" org-log-reschedule nil)
  4089. ("logredeadline" org-log-redeadline time)
  4090. ("lognoteredeadline" org-log-redeadline note)
  4091. ("nologredeadline" org-log-redeadline nil)
  4092. ("logrefile" org-log-refile time)
  4093. ("lognoterefile" org-log-refile note)
  4094. ("nologrefile" org-log-refile nil)
  4095. ("fninline" org-footnote-define-inline t)
  4096. ("nofninline" org-footnote-define-inline nil)
  4097. ("fnlocal" org-footnote-section nil)
  4098. ("fnauto" org-footnote-auto-label t)
  4099. ("fnprompt" org-footnote-auto-label nil)
  4100. ("fnconfirm" org-footnote-auto-label confirm)
  4101. ("fnplain" org-footnote-auto-label plain)
  4102. ("fnadjust" org-footnote-auto-adjust t)
  4103. ("nofnadjust" org-footnote-auto-adjust nil)
  4104. ("constcgs" constants-unit-system cgs)
  4105. ("constSI" constants-unit-system SI)
  4106. ("noptag" org-tag-persistent-alist nil)
  4107. ("hideblocks" org-hide-block-startup t)
  4108. ("nohideblocks" org-hide-block-startup nil)
  4109. ("beamer" org-startup-with-beamer-mode t)
  4110. ("entitiespretty" org-pretty-entities t)
  4111. ("entitiesplain" org-pretty-entities nil))
  4112. "Variable associated with STARTUP options for org-mode.
  4113. Each element is a list of three items: the startup options (as written
  4114. in the #+STARTUP line), the corresponding variable, and the value to set
  4115. this variable to if the option is found. An optional forth element PUSH
  4116. means to push this value onto the list in the variable.")
  4117. (defun org-update-property-plist (key val props)
  4118. "Update PROPS with KEY and VAL."
  4119. (let* ((appending (string= "+" (substring key (- (length key) 1))))
  4120. (key (if appending (substring key 0 (- (length key) 1)) key))
  4121. (remainder (org-remove-if (lambda (p) (string= (car p) key)) props))
  4122. (previous (cdr (assoc key props))))
  4123. (if appending
  4124. (cons (cons key (if previous (concat previous " " val) val)) remainder)
  4125. (cons (cons key val) remainder))))
  4126. (defconst org-block-regexp
  4127. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  4128. "Regular expression for hiding blocks.")
  4129. (defconst org-heading-keyword-regexp-format
  4130. "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  4131. "Printf format for a regexp matching a headline with some keyword.
  4132. This regexp will match the headline of any node which has the
  4133. exact keyword that is put into the format. The keyword isn't in
  4134. any group by default, but the stars and the body are.")
  4135. (defconst org-heading-keyword-maybe-regexp-format
  4136. "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
  4137. "Printf format for a regexp matching a headline, possibly with some keyword.
  4138. This regexp can match any headline with the specified keyword, or
  4139. without a keyword. The keyword isn't in any group by default,
  4140. but the stars and the body are.")
  4141. (defun org-set-regexps-and-options ()
  4142. "Precompute regular expressions for current buffer."
  4143. (when (derived-mode-p 'org-mode)
  4144. (org-set-local 'org-todo-kwd-alist nil)
  4145. (org-set-local 'org-todo-key-alist nil)
  4146. (org-set-local 'org-todo-key-trigger nil)
  4147. (org-set-local 'org-todo-keywords-1 nil)
  4148. (org-set-local 'org-done-keywords nil)
  4149. (org-set-local 'org-todo-heads nil)
  4150. (org-set-local 'org-todo-sets nil)
  4151. (org-set-local 'org-todo-log-states nil)
  4152. (org-set-local 'org-file-properties nil)
  4153. (org-set-local 'org-file-tags nil)
  4154. (let ((re (org-make-options-regexp
  4155. '("CATEGORY" "TODO" "COLUMNS" "STARTUP" "ARCHIVE" "FILETAGS"
  4156. "TAGS" "LINK" "PRIORITIES" "CONSTANTS" "PROPERTY" "DRAWERS"
  4157. "SETUPFILE" "OPTIONS")
  4158. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  4159. (splitre "[ \t]+")
  4160. (scripts org-use-sub-superscripts)
  4161. kwds kws0 kwsa key log value cat arch tags const links hw dws
  4162. tail sep kws1 prio props ftags drawers ext-setup-or-nil setup-contents
  4163. (start 0))
  4164. (save-excursion
  4165. (save-restriction
  4166. (widen)
  4167. (goto-char (point-min))
  4168. (while (or (and ext-setup-or-nil
  4169. (string-match re ext-setup-or-nil start)
  4170. (setq start (match-end 0)))
  4171. (and (setq ext-setup-or-nil nil start 0)
  4172. (re-search-forward re nil t)))
  4173. (setq key (upcase (match-string 1 ext-setup-or-nil))
  4174. value (org-match-string-no-properties 2 ext-setup-or-nil))
  4175. (if (stringp value) (setq value (org-trim value)))
  4176. (cond
  4177. ((equal key "CATEGORY")
  4178. (setq cat value))
  4179. ((member key '("SEQ_TODO" "TODO"))
  4180. (push (cons 'sequence (org-split-string value splitre)) kwds))
  4181. ((equal key "TYP_TODO")
  4182. (push (cons 'type (org-split-string value splitre)) kwds))
  4183. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  4184. ;; general TODO-like setup
  4185. (push (cons (intern (downcase (match-string 1 key)))
  4186. (org-split-string value splitre)) kwds))
  4187. ((equal key "TAGS")
  4188. (setq tags (append tags (if tags '("\\n") nil)
  4189. (org-split-string value splitre))))
  4190. ((equal key "COLUMNS")
  4191. (org-set-local 'org-columns-default-format value))
  4192. ((equal key "LINK")
  4193. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  4194. (push (cons (match-string 1 value)
  4195. (org-trim (match-string 2 value)))
  4196. links)))
  4197. ((equal key "PRIORITIES")
  4198. (setq prio (org-split-string value " +")))
  4199. ((equal key "PROPERTY")
  4200. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4201. (setq props (org-update-property-plist (match-string 1 value)
  4202. (match-string 2 value)
  4203. props))))
  4204. ((equal key "FILETAGS")
  4205. (when (string-match "\\S-" value)
  4206. (setq ftags
  4207. (append
  4208. ftags
  4209. (apply 'append
  4210. (mapcar (lambda (x) (org-split-string x ":"))
  4211. (org-split-string value)))))))
  4212. ((equal key "DRAWERS")
  4213. (setq drawers (delete-dups (append org-drawers (org-split-string value splitre)))))
  4214. ((equal key "CONSTANTS")
  4215. (setq const (append const (org-split-string value splitre))))
  4216. ((equal key "STARTUP")
  4217. (let ((opts (org-split-string value splitre))
  4218. l var val)
  4219. (while (setq l (pop opts))
  4220. (when (setq l (assoc l org-startup-options))
  4221. (setq var (nth 1 l) val (nth 2 l))
  4222. (if (not (nth 3 l))
  4223. (set (make-local-variable var) val)
  4224. (if (not (listp (symbol-value var)))
  4225. (set (make-local-variable var) nil))
  4226. (set (make-local-variable var) (symbol-value var))
  4227. (add-to-list var val))))))
  4228. ((equal key "ARCHIVE")
  4229. (setq arch value)
  4230. (remove-text-properties 0 (length arch)
  4231. '(face t fontified t) arch))
  4232. ((equal key "OPTIONS")
  4233. (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value)
  4234. (setq scripts (read (match-string 2 value)))))
  4235. ((equal key "SETUPFILE")
  4236. (setq setup-contents (org-file-contents
  4237. (expand-file-name
  4238. (org-remove-double-quotes value))
  4239. 'noerror))
  4240. (if (not ext-setup-or-nil)
  4241. (setq ext-setup-or-nil setup-contents start 0)
  4242. (setq ext-setup-or-nil
  4243. (concat (substring ext-setup-or-nil 0 start)
  4244. "\n" setup-contents "\n"
  4245. (substring ext-setup-or-nil start)))))))
  4246. ;; search for property blocks
  4247. (goto-char (point-min))
  4248. (while (re-search-forward org-block-regexp nil t)
  4249. (when (equal "PROPERTY" (upcase (match-string 1)))
  4250. (setq value (replace-regexp-in-string
  4251. "[\n\r]" " " (match-string 4)))
  4252. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4253. (setq props (org-update-property-plist (match-string 1 value)
  4254. (match-string 2 value)
  4255. props)))))))
  4256. (org-set-local 'org-use-sub-superscripts scripts)
  4257. (when cat
  4258. (org-set-local 'org-category (intern cat))
  4259. (push (cons "CATEGORY" cat) props))
  4260. (when prio
  4261. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  4262. (setq prio (mapcar 'string-to-char prio))
  4263. (org-set-local 'org-highest-priority (nth 0 prio))
  4264. (org-set-local 'org-lowest-priority (nth 1 prio))
  4265. (org-set-local 'org-default-priority (nth 2 prio)))
  4266. (and props (org-set-local 'org-file-properties (nreverse props)))
  4267. (and ftags (org-set-local 'org-file-tags
  4268. (mapcar 'org-add-prop-inherited ftags)))
  4269. (and drawers (org-set-local 'org-drawers drawers))
  4270. (and arch (org-set-local 'org-archive-location arch))
  4271. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  4272. ;; Process the TODO keywords
  4273. (unless kwds
  4274. ;; Use the global values as if they had been given locally.
  4275. (setq kwds (default-value 'org-todo-keywords))
  4276. (if (stringp (car kwds))
  4277. (setq kwds (list (cons org-todo-interpretation
  4278. (default-value 'org-todo-keywords)))))
  4279. (setq kwds (reverse kwds)))
  4280. (setq kwds (nreverse kwds))
  4281. (let (inter kws kw)
  4282. (while (setq kws (pop kwds))
  4283. (let ((kws (or
  4284. (run-hook-with-args-until-success
  4285. 'org-todo-setup-filter-hook kws)
  4286. kws)))
  4287. (setq inter (pop kws) sep (member "|" kws)
  4288. kws0 (delete "|" (copy-sequence kws))
  4289. kwsa nil
  4290. kws1 (mapcar
  4291. (lambda (x)
  4292. ;; 1 2
  4293. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  4294. (progn
  4295. (setq kw (match-string 1 x)
  4296. key (and (match-end 2) (match-string 2 x))
  4297. log (org-extract-log-state-settings x))
  4298. (push (cons kw (and key (string-to-char key))) kwsa)
  4299. (and log (push log org-todo-log-states))
  4300. kw)
  4301. (error "Invalid TODO keyword %s" x)))
  4302. kws0)
  4303. kwsa (if kwsa (append '((:startgroup))
  4304. (nreverse kwsa)
  4305. '((:endgroup))))
  4306. hw (car kws1)
  4307. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  4308. tail (list inter hw (car dws) (org-last dws))))
  4309. (add-to-list 'org-todo-heads hw 'append)
  4310. (push kws1 org-todo-sets)
  4311. (setq org-done-keywords (append org-done-keywords dws nil))
  4312. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  4313. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  4314. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  4315. (setq org-todo-sets (nreverse org-todo-sets)
  4316. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4317. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  4318. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  4319. ;; Process the constants
  4320. (when const
  4321. (let (e cst)
  4322. (while (setq e (pop const))
  4323. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  4324. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  4325. (setq org-table-formula-constants-local cst)))
  4326. ;; Process the tags.
  4327. (when tags
  4328. (let (e tgs)
  4329. (while (setq e (pop tags))
  4330. (cond
  4331. ((equal e "{") (push '(:startgroup) tgs))
  4332. ((equal e "}") (push '(:endgroup) tgs))
  4333. ((equal e "\\n") (push '(:newline) tgs))
  4334. ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e)
  4335. (push (cons (match-string 1 e)
  4336. (string-to-char (match-string 2 e)))
  4337. tgs))
  4338. (t (push (list e) tgs))))
  4339. (org-set-local 'org-tag-alist nil)
  4340. (while (setq e (pop tgs))
  4341. (or (and (stringp (car e))
  4342. (assoc (car e) org-tag-alist))
  4343. (push e org-tag-alist)))))
  4344. ;; Compute the regular expressions and other local variables.
  4345. ;; Using `org-outline-regexp-bol' would complicate them much,
  4346. ;; because of the fixed white space at the end of that string.
  4347. (if (not org-done-keywords)
  4348. (setq org-done-keywords (and org-todo-keywords-1
  4349. (list (org-last org-todo-keywords-1)))))
  4350. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  4351. (length org-scheduled-string)
  4352. (length org-clock-string)
  4353. (length org-closed-string)))
  4354. org-drawer-regexp
  4355. (concat "^[ \t]*:\\("
  4356. (mapconcat 'regexp-quote org-drawers "\\|")
  4357. "\\):[ \t]*$")
  4358. org-not-done-keywords
  4359. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  4360. org-todo-regexp
  4361. (concat "\\("
  4362. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4363. "\\)")
  4364. org-not-done-regexp
  4365. (concat "\\("
  4366. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  4367. "\\)")
  4368. org-not-done-heading-regexp
  4369. (format org-heading-keyword-regexp-format org-not-done-regexp)
  4370. org-todo-line-regexp
  4371. (format org-heading-keyword-maybe-regexp-format org-todo-regexp)
  4372. org-complex-heading-regexp
  4373. (concat "^\\(\\*+\\)"
  4374. "\\(?: +" org-todo-regexp "\\)?"
  4375. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4376. "\\(?: +\\(.*?\\)\\)??"
  4377. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
  4378. "[ \t]*$")
  4379. org-complex-heading-regexp-format
  4380. (concat "^\\(\\*+\\)"
  4381. "\\(?: +" org-todo-regexp "\\)?"
  4382. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4383. "\\(?: +"
  4384. ;; Stats cookies can be stuck to body.
  4385. "\\(?:\\[[0-9%%/]+\\] *\\)?"
  4386. "\\(%s\\)"
  4387. "\\(?: *\\[[0-9%%/]+\\]\\)?"
  4388. "\\)"
  4389. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
  4390. "[ \t]*$")
  4391. org-todo-line-tags-regexp
  4392. (concat "^\\(\\*+\\)"
  4393. "\\(?: +" org-todo-regexp "\\)?"
  4394. "\\(?: +\\(.*?\\)\\)??"
  4395. (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
  4396. "[ \t]*$")
  4397. org-deadline-regexp (concat "\\<" org-deadline-string)
  4398. org-deadline-time-regexp
  4399. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  4400. org-deadline-line-regexp
  4401. (concat "\\<\\(" org-deadline-string "\\).*")
  4402. org-scheduled-regexp
  4403. (concat "\\<" org-scheduled-string)
  4404. org-scheduled-time-regexp
  4405. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  4406. org-closed-time-regexp
  4407. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  4408. org-keyword-time-regexp
  4409. (concat "\\<\\(" org-scheduled-string
  4410. "\\|" org-deadline-string
  4411. "\\|" org-closed-string
  4412. "\\|" org-clock-string "\\)"
  4413. " *[[<]\\([^]>]+\\)[]>]")
  4414. org-keyword-time-not-clock-regexp
  4415. (concat "\\<\\(" org-scheduled-string
  4416. "\\|" org-deadline-string
  4417. "\\|" org-closed-string
  4418. "\\)"
  4419. " *[[<]\\([^]>]+\\)[]>]")
  4420. org-maybe-keyword-time-regexp
  4421. (concat "\\(\\<\\(" org-scheduled-string
  4422. "\\|" org-deadline-string
  4423. "\\|" org-closed-string
  4424. "\\|" org-clock-string "\\)\\)?"
  4425. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4426. org-all-time-keywords
  4427. (mapcar (lambda (w) (substring w 0 -1))
  4428. (list org-scheduled-string org-deadline-string
  4429. org-clock-string org-closed-string)))
  4430. (org-compute-latex-and-related-regexp)
  4431. (org-set-font-lock-defaults))))
  4432. (defun org-file-contents (file &optional noerror)
  4433. "Return the contents of FILE, as a string."
  4434. (if (or (not file)
  4435. (not (file-readable-p file)))
  4436. (if noerror
  4437. (progn
  4438. (message "Cannot read file \"%s\"" file)
  4439. (ding) (sit-for 2)
  4440. "")
  4441. (error "Cannot read file \"%s\"" file))
  4442. (with-temp-buffer
  4443. (insert-file-contents file)
  4444. (buffer-string))))
  4445. (defun org-extract-log-state-settings (x)
  4446. "Extract the log state setting from a TODO keyword string.
  4447. This will extract info from a string like \"WAIT(w@/!)\"."
  4448. (let (kw key log1 log2)
  4449. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4450. (setq kw (match-string 1 x)
  4451. key (and (match-end 2) (match-string 2 x))
  4452. log1 (and (match-end 3) (match-string 3 x))
  4453. log2 (and (match-end 4) (match-string 4 x)))
  4454. (and (or log1 log2)
  4455. (list kw
  4456. (and log1 (if (equal log1 "!") 'time 'note))
  4457. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4458. (defun org-remove-keyword-keys (list)
  4459. "Remove a pair of parenthesis at the end of each string in LIST."
  4460. (mapcar (lambda (x)
  4461. (if (string-match "(.*)$" x)
  4462. (substring x 0 (match-beginning 0))
  4463. x))
  4464. list))
  4465. (defun org-assign-fast-keys (alist)
  4466. "Assign fast keys to a keyword-key alist.
  4467. Respect keys that are already there."
  4468. (let (new e (alt ?0))
  4469. (while (setq e (pop alist))
  4470. (if (or (memq (car e) '(:newline :endgroup :startgroup))
  4471. (cdr e)) ;; Key already assigned.
  4472. (push e new)
  4473. (let ((clist (string-to-list (downcase (car e))))
  4474. (used (append new alist)))
  4475. (when (= (car clist) ?@)
  4476. (pop clist))
  4477. (while (and clist (rassoc (car clist) used))
  4478. (pop clist))
  4479. (unless clist
  4480. (while (rassoc alt used)
  4481. (incf alt)))
  4482. (push (cons (car e) (or (car clist) alt)) new))))
  4483. (nreverse new)))
  4484. ;;; Some variables used in various places
  4485. (defvar org-window-configuration nil
  4486. "Used in various places to store a window configuration.")
  4487. (defvar org-selected-window nil
  4488. "Used in various places to store a window configuration.")
  4489. (defvar org-finish-function nil
  4490. "Function to be called when `C-c C-c' is used.
  4491. This is for getting out of special buffers like capture.")
  4492. ;; FIXME: Occasionally check by commenting these, to make sure
  4493. ;; no other functions uses these, forgetting to let-bind them.
  4494. (org-no-warnings (defvar entry)) ;; unprefixed, from calendar.el
  4495. (defvar org-last-state)
  4496. (org-no-warnings (defvar date)) ;; unprefixed, from calendar.el
  4497. ;; Defined somewhere in this file, but used before definition.
  4498. (defvar org-entities) ;; defined in org-entities.el
  4499. (defvar org-struct-menu)
  4500. (defvar org-org-menu)
  4501. (defvar org-tbl-menu)
  4502. ;;;; Define the Org-mode
  4503. ;; We use a before-change function to check if a table might need
  4504. ;; an update.
  4505. (defvar org-table-may-need-update t
  4506. "Indicates that a table might need an update.
  4507. This variable is set by `org-before-change-function'.
  4508. `org-table-align' sets it back to nil.")
  4509. (defun org-before-change-function (beg end)
  4510. "Every change indicates that a table might need an update."
  4511. (setq org-table-may-need-update t))
  4512. (defvar org-mode-map)
  4513. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4514. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4515. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4516. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4517. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4518. (defvar org-table-buffer-is-an nil)
  4519. (defvar bidi-paragraph-direction)
  4520. (defvar buffer-face-mode-face)
  4521. (require 'outline)
  4522. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4523. (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"))
  4524. (require 'noutline "noutline" 'noerror) ;; stock XEmacs does not have it
  4525. ;; Other stuff we need.
  4526. (require 'time-date)
  4527. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  4528. (require 'easymenu)
  4529. (require 'overlay)
  4530. ;; (require 'org-macs) moved higher up in the file before it is first used
  4531. (require 'org-entities)
  4532. ;; (require 'org-compat) moved higher up in the file before it is first used
  4533. (require 'org-faces)
  4534. (require 'org-list)
  4535. (require 'org-pcomplete)
  4536. (require 'org-src)
  4537. (require 'org-footnote)
  4538. (require 'org-macro)
  4539. ;; babel
  4540. (require 'ob)
  4541. ;;;###autoload
  4542. (define-derived-mode org-mode outline-mode "Org"
  4543. "Outline-based notes management and organizer, alias
  4544. \"Carsten's outline-mode for keeping track of everything.\"
  4545. Org-mode develops organizational tasks around a NOTES file which
  4546. contains information about projects as plain text. Org-mode is
  4547. implemented on top of outline-mode, which is ideal to keep the content
  4548. of large files well structured. It supports ToDo items, deadlines and
  4549. time stamps, which magically appear in the diary listing of the Emacs
  4550. calendar. Tables are easily created with a built-in table editor.
  4551. Plain text URL-like links connect to websites, emails (VM), Usenet
  4552. messages (Gnus), BBDB entries, and any files related to the project.
  4553. For printing and sharing of notes, an Org-mode file (or a part of it)
  4554. can be exported as a structured ASCII or HTML file.
  4555. The following commands are available:
  4556. \\{org-mode-map}"
  4557. ;; Get rid of Outline menus, they are not needed
  4558. ;; Need to do this here because define-derived-mode sets up
  4559. ;; the keymap so late. Still, it is a waste to call this each time
  4560. ;; we switch another buffer into org-mode.
  4561. (if (featurep 'xemacs)
  4562. (when (boundp 'outline-mode-menu-heading)
  4563. ;; Assume this is Greg's port, it uses easymenu
  4564. (easy-menu-remove outline-mode-menu-heading)
  4565. (easy-menu-remove outline-mode-menu-show)
  4566. (easy-menu-remove outline-mode-menu-hide))
  4567. (define-key org-mode-map [menu-bar headings] 'undefined)
  4568. (define-key org-mode-map [menu-bar hide] 'undefined)
  4569. (define-key org-mode-map [menu-bar show] 'undefined))
  4570. (org-load-modules-maybe)
  4571. (easy-menu-add org-org-menu)
  4572. (easy-menu-add org-tbl-menu)
  4573. (org-install-agenda-files-menu)
  4574. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4575. (add-to-invisibility-spec '(org-cwidth))
  4576. (add-to-invisibility-spec '(org-hide-block . t))
  4577. (when (featurep 'xemacs)
  4578. (org-set-local 'line-move-ignore-invisible t))
  4579. (org-set-local 'outline-regexp org-outline-regexp)
  4580. (org-set-local 'outline-level 'org-outline-level)
  4581. (setq bidi-paragraph-direction 'left-to-right)
  4582. ;; FIXME Circumvent a bug in outline.el (Emacs <24.4)
  4583. (set (make-local-variable 'paragraph-start) " \\|[ \t]*$\\|\\*+ ")
  4584. (when (and org-ellipsis
  4585. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4586. (fboundp 'make-glyph-code))
  4587. (unless org-display-table
  4588. (setq org-display-table (make-display-table)))
  4589. (set-display-table-slot
  4590. org-display-table 4
  4591. (vconcat (mapcar
  4592. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4593. org-ellipsis)))
  4594. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4595. (setq buffer-display-table org-display-table))
  4596. (org-set-regexps-and-options)
  4597. (when (and org-tag-faces (not org-tags-special-faces-re))
  4598. ;; tag faces set outside customize.... force initialization.
  4599. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4600. ;; Calc embedded
  4601. (org-set-local 'calc-embedded-open-mode "# ")
  4602. ;; Modify a few syntax entries
  4603. (modify-syntax-entry ?@ "w")
  4604. (modify-syntax-entry ?\" "\"")
  4605. (if org-startup-truncated (setq truncate-lines t))
  4606. (when org-startup-indented (require 'org-indent) (org-indent-mode 1))
  4607. (org-set-local 'font-lock-unfontify-region-function
  4608. 'org-unfontify-region)
  4609. ;; Activate before-change-function
  4610. (org-set-local 'org-table-may-need-update t)
  4611. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4612. 'local)
  4613. ;; Check for running clock before killing a buffer
  4614. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4615. ;; Initialize macros templates.
  4616. (org-macro-initialize-templates)
  4617. ;; Initialize radio targets.
  4618. (org-update-radio-target-regexp)
  4619. ;; Indentation.
  4620. (org-set-local 'indent-line-function 'org-indent-line)
  4621. (org-set-local 'indent-region-function 'org-indent-region)
  4622. ;; Filling and auto-filling.
  4623. (org-setup-filling)
  4624. ;; Comments.
  4625. (org-setup-comments-handling)
  4626. ;; Beginning/end of defun
  4627. (org-set-local 'beginning-of-defun-function 'org-backward-element)
  4628. (org-set-local 'end-of-defun-function 'org-forward-element)
  4629. ;; Next error for sparse trees
  4630. (org-set-local 'next-error-function 'org-occur-next-match)
  4631. ;; Make sure dependence stuff works reliably, even for users who set it
  4632. ;; too late :-(
  4633. (if org-enforce-todo-dependencies
  4634. (add-hook 'org-blocker-hook
  4635. 'org-block-todo-from-children-or-siblings-or-parent)
  4636. (remove-hook 'org-blocker-hook
  4637. 'org-block-todo-from-children-or-siblings-or-parent))
  4638. (if org-enforce-todo-checkbox-dependencies
  4639. (add-hook 'org-blocker-hook
  4640. 'org-block-todo-from-checkboxes)
  4641. (remove-hook 'org-blocker-hook
  4642. 'org-block-todo-from-checkboxes))
  4643. ;; Align options lines
  4644. (org-set-local
  4645. 'align-mode-rules-list
  4646. '((org-in-buffer-settings
  4647. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4648. (modes . '(org-mode)))))
  4649. ;; Imenu
  4650. (org-set-local 'imenu-create-index-function
  4651. 'org-imenu-get-tree)
  4652. ;; Make isearch reveal context
  4653. (if (or (featurep 'xemacs)
  4654. (not (boundp 'outline-isearch-open-invisible-function)))
  4655. ;; Emacs 21 and XEmacs make use of the hook
  4656. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4657. ;; Emacs 22 deals with this through a special variable
  4658. (org-set-local 'outline-isearch-open-invisible-function
  4659. (lambda (&rest ignore) (org-show-context 'isearch)))
  4660. (org-add-hook 'isearch-mode-end-hook 'org-fix-ellipsis-at-bol 'append 'local))
  4661. ;; Setup the pcomplete hooks
  4662. (set (make-local-variable 'pcomplete-command-completion-function)
  4663. 'org-pcomplete-initial)
  4664. (set (make-local-variable 'pcomplete-command-name-function)
  4665. 'org-command-at-point)
  4666. (set (make-local-variable 'pcomplete-default-completion-function)
  4667. 'ignore)
  4668. (set (make-local-variable 'pcomplete-parse-arguments-function)
  4669. 'org-parse-arguments)
  4670. (set (make-local-variable 'pcomplete-termination-string) "")
  4671. (when (>= emacs-major-version 23)
  4672. (set (make-local-variable 'buffer-face-mode-face) 'org-default))
  4673. ;; If empty file that did not turn on org-mode automatically, make it to.
  4674. (if (and org-insert-mode-line-in-empty-file
  4675. (org-called-interactively-p 'any)
  4676. (= (point-min) (point-max)))
  4677. (insert "# -*- mode: org -*-\n\n"))
  4678. (unless org-inhibit-startup
  4679. (org-unmodified
  4680. (and org-startup-with-beamer-mode (org-beamer-mode))
  4681. (when org-startup-align-all-tables
  4682. (org-table-map-tables 'org-table-align 'quietly))
  4683. (when org-startup-with-inline-images
  4684. (org-display-inline-images))
  4685. (when org-startup-with-latex-preview
  4686. (org-preview-latex-fragment))
  4687. (unless org-inhibit-startup-visibility-stuff
  4688. (org-set-startup-visibility))))
  4689. ;; Try to set org-hide correctly
  4690. (set-face-foreground 'org-hide (org-find-invisible-foreground)))
  4691. (defvar org-mode-transpose-word-syntax-table
  4692. (let ((st (make-syntax-table)))
  4693. (mapc (lambda(c) (modify-syntax-entry
  4694. (string-to-char (car c)) "w p" st))
  4695. org-emphasis-alist)
  4696. st))
  4697. (when (fboundp 'abbrev-table-put)
  4698. (abbrev-table-put org-mode-abbrev-table
  4699. :parents (list text-mode-abbrev-table)))
  4700. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4701. (defsubst org-fix-ellipsis-at-bol ()
  4702. (save-excursion (goto-char (window-start)) (recenter 0)))
  4703. (defun org-find-invisible-foreground ()
  4704. (let ((candidates (remove
  4705. "unspecified-bg"
  4706. (nconc
  4707. (list (face-background 'default)
  4708. (face-background 'org-default))
  4709. (mapcar
  4710. (lambda (alist)
  4711. (when (boundp alist)
  4712. (cdr (assoc 'background-color (symbol-value alist)))))
  4713. '(default-frame-alist initial-frame-alist window-system-default-frame-alist))
  4714. (list (face-foreground 'org-hide))))))
  4715. (car (remove nil candidates))))
  4716. (defun org-current-time (&optional rounding-minutes past)
  4717. "Current time, possibly rounded to ROUNDING-MINUTES.
  4718. When ROUNDING-MINUTES is not an integer, fall back on the car of
  4719. `org-time-stamp-rounding-minutes'. When PAST is non-nil, ensure
  4720. the rounding returns a past time."
  4721. (let ((r (or (and (integerp rounding-minutes) rounding-minutes)
  4722. (car org-time-stamp-rounding-minutes)))
  4723. (time (decode-time)) res)
  4724. (if (< r 1)
  4725. (current-time)
  4726. (setq res
  4727. (apply 'encode-time
  4728. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4729. (nthcdr 2 time))))
  4730. (if (and past (< (org-float-time (time-subtract (current-time) res)) 0))
  4731. (seconds-to-time (- (org-float-time res) (* r 60)))
  4732. res))))
  4733. (defun org-today ()
  4734. "Return today date, considering `org-extend-today-until'."
  4735. (time-to-days
  4736. (time-subtract (current-time)
  4737. (list 0 (* 3600 org-extend-today-until) 0))))
  4738. ;;;; Font-Lock stuff, including the activators
  4739. (defvar org-mouse-map (make-sparse-keymap))
  4740. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4741. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4742. (when org-mouse-1-follows-link
  4743. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4744. (when org-tab-follows-link
  4745. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4746. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4747. (require 'font-lock)
  4748. (defconst org-non-link-chars "]\t\n\r<>")
  4749. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  4750. "shell" "elisp" "doi" "message"))
  4751. (defvar org-link-types-re nil
  4752. "Matches a link that has a url-like prefix like \"http:\"")
  4753. (defvar org-link-re-with-space nil
  4754. "Matches a link with spaces, optional angular brackets around it.")
  4755. (defvar org-link-re-with-space2 nil
  4756. "Matches a link with spaces, optional angular brackets around it.")
  4757. (defvar org-link-re-with-space3 nil
  4758. "Matches a link with spaces, only for internal part in bracket links.")
  4759. (defvar org-angle-link-re nil
  4760. "Matches link with angular brackets, spaces are allowed.")
  4761. (defvar org-plain-link-re nil
  4762. "Matches plain link, without spaces.")
  4763. (defvar org-bracket-link-regexp nil
  4764. "Matches a link in double brackets.")
  4765. (defvar org-bracket-link-analytic-regexp nil
  4766. "Regular expression used to analyze links.
  4767. Here is what the match groups contain after a match:
  4768. 1: http:
  4769. 2: http
  4770. 3: path
  4771. 4: [desc]
  4772. 5: desc")
  4773. (defvar org-bracket-link-analytic-regexp++ nil
  4774. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4775. (defvar org-any-link-re nil
  4776. "Regular expression matching any link.")
  4777. (defconst org-match-sexp-depth 3
  4778. "Number of stacked braces for sub/superscript matching.")
  4779. (defun org-create-multibrace-regexp (left right n)
  4780. "Create a regular expression which will match a balanced sexp.
  4781. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4782. as single character strings.
  4783. The regexp returned will match the entire expression including the
  4784. delimiters. It will also define a single group which contains the
  4785. match except for the outermost delimiters. The maximum depth of
  4786. stacked delimiters is N. Escaping delimiters is not possible."
  4787. (let* ((nothing (concat "[^" left right "]*?"))
  4788. (or "\\|")
  4789. (re nothing)
  4790. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4791. (while (> n 1)
  4792. (setq n (1- n)
  4793. re (concat re or next)
  4794. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4795. (concat left "\\(" re "\\)" right)))
  4796. (defvar org-match-substring-regexp
  4797. (concat
  4798. "\\(\\S-\\)\\([_^]\\)\\("
  4799. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4800. "\\|"
  4801. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4802. "\\|"
  4803. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  4804. "The regular expression matching a sub- or superscript.")
  4805. (defvar org-match-substring-with-braces-regexp
  4806. (concat
  4807. "\\(\\S-\\)\\([_^]\\)\\("
  4808. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4809. "\\)")
  4810. "The regular expression matching a sub- or superscript, forcing braces.")
  4811. (defun org-make-link-regexps ()
  4812. "Update the link regular expressions.
  4813. This should be called after the variable `org-link-types' has changed."
  4814. (setq org-link-types-re
  4815. (concat
  4816. "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
  4817. org-link-re-with-space
  4818. (concat
  4819. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4820. "\\([^" org-non-link-chars " ]"
  4821. "[^" org-non-link-chars "]*"
  4822. "[^" org-non-link-chars " ]\\)>?")
  4823. org-link-re-with-space2
  4824. (concat
  4825. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4826. "\\([^" org-non-link-chars " ]"
  4827. "[^\t\n\r]*"
  4828. "[^" org-non-link-chars " ]\\)>?")
  4829. org-link-re-with-space3
  4830. (concat
  4831. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4832. "\\([^" org-non-link-chars " ]"
  4833. "[^\t\n\r]*\\)")
  4834. org-angle-link-re
  4835. (concat
  4836. "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4837. "\\([^" org-non-link-chars " ]"
  4838. "[^" org-non-link-chars "]*"
  4839. "\\)>")
  4840. org-plain-link-re
  4841. (concat
  4842. "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4843. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  4844. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4845. org-bracket-link-regexp
  4846. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4847. org-bracket-link-analytic-regexp
  4848. (concat
  4849. "\\[\\["
  4850. "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
  4851. "\\([^]]+\\)"
  4852. "\\]"
  4853. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4854. "\\]")
  4855. org-bracket-link-analytic-regexp++
  4856. (concat
  4857. "\\[\\["
  4858. "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  4859. "\\([^]]+\\)"
  4860. "\\]"
  4861. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4862. "\\]")
  4863. org-any-link-re
  4864. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4865. org-angle-link-re "\\)\\|\\("
  4866. org-plain-link-re "\\)")))
  4867. (org-make-link-regexps)
  4868. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
  4869. "Regular expression for fast time stamp matching.")
  4870. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
  4871. "Regular expression for fast time stamp matching.")
  4872. (defconst org-ts-regexp0
  4873. "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4874. "Regular expression matching time strings for analysis.
  4875. This one does not require the space after the date, so it can be used
  4876. on a string that terminates immediately after the date.")
  4877. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4878. "Regular expression matching time strings for analysis.")
  4879. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4880. "Regular expression matching time stamps, with groups.")
  4881. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4882. "Regular expression matching time stamps (also [..]), with groups.")
  4883. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4884. "Regular expression matching a time stamp range.")
  4885. (defconst org-tr-regexp-both
  4886. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4887. "Regular expression matching a time stamp range.")
  4888. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4889. org-ts-regexp "\\)?")
  4890. "Regular expression matching a time stamp or time stamp range.")
  4891. (defconst org-tsr-regexp-both
  4892. (concat org-ts-regexp-both "\\(--?-?"
  4893. org-ts-regexp-both "\\)?")
  4894. "Regular expression matching a time stamp or time stamp range.
  4895. The time stamps may be either active or inactive.")
  4896. (defvar org-emph-face nil)
  4897. (defun org-do-emphasis-faces (limit)
  4898. "Run through the buffer and add overlays to emphasized strings."
  4899. (let (rtn a)
  4900. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4901. (if (not (= (char-after (match-beginning 3))
  4902. (char-after (match-beginning 4))))
  4903. (progn
  4904. (setq rtn t)
  4905. (setq a (assoc (match-string 3) org-emphasis-alist))
  4906. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4907. 'face
  4908. (nth 1 a))
  4909. (and (nth 4 a)
  4910. (org-remove-flyspell-overlays-in
  4911. (match-beginning 0) (match-end 0)))
  4912. (add-text-properties (match-beginning 2) (match-end 2)
  4913. '(font-lock-multiline t org-emphasis t))
  4914. (when org-hide-emphasis-markers
  4915. (add-text-properties (match-end 4) (match-beginning 5)
  4916. '(invisible org-link))
  4917. (add-text-properties (match-beginning 3) (match-end 3)
  4918. '(invisible org-link)))))
  4919. (backward-char 1))
  4920. rtn))
  4921. (defun org-emphasize (&optional char)
  4922. "Insert or change an emphasis, i.e. a font like bold or italic.
  4923. If there is an active region, change that region to a new emphasis.
  4924. If there is no region, just insert the marker characters and position
  4925. the cursor between them.
  4926. CHAR should be either the marker character, or the first character of the
  4927. HTML tag associated with that emphasis. If CHAR is a space, the means
  4928. to remove the emphasis of the selected region.
  4929. If char is not given (for example in an interactive call) it
  4930. will be prompted for."
  4931. (interactive)
  4932. (let ((eal org-emphasis-alist) e det
  4933. (erc org-emphasis-regexp-components)
  4934. (prompt "")
  4935. (string "") beg end move tag c s)
  4936. (if (org-region-active-p)
  4937. (setq beg (region-beginning) end (region-end)
  4938. string (buffer-substring beg end))
  4939. (setq move t))
  4940. (while (setq e (pop eal))
  4941. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4942. c (aref tag 0))
  4943. (push (cons c (string-to-char (car e))) det)
  4944. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4945. (substring tag 1)))))
  4946. (setq det (nreverse det))
  4947. (unless char
  4948. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4949. (setq char (read-char-exclusive)))
  4950. (setq char (or (cdr (assoc char det)) char))
  4951. (if (equal char ?\ )
  4952. (setq s "" move nil)
  4953. (unless (assoc (char-to-string char) org-emphasis-alist)
  4954. (error "No such emphasis marker: \"%c\"" char))
  4955. (setq s (char-to-string char)))
  4956. (while (and (> (length string) 1)
  4957. (equal (substring string 0 1) (substring string -1))
  4958. (assoc (substring string 0 1) org-emphasis-alist))
  4959. (setq string (substring string 1 -1)))
  4960. (setq string (concat s string s))
  4961. (if beg (delete-region beg end))
  4962. (unless (or (bolp)
  4963. (string-match (concat "[" (nth 0 erc) "\n]")
  4964. (char-to-string (char-before (point)))))
  4965. (insert " "))
  4966. (unless (or (eobp)
  4967. (string-match (concat "[" (nth 1 erc) "\n]")
  4968. (char-to-string (char-after (point)))))
  4969. (insert " ") (backward-char 1))
  4970. (insert string)
  4971. (and move (backward-char 1))))
  4972. (defconst org-nonsticky-props
  4973. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text htmlize-link))
  4974. (defsubst org-rear-nonsticky-at (pos)
  4975. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  4976. (defun org-activate-plain-links (limit)
  4977. "Run through the buffer and add overlays to links."
  4978. (let (f hl)
  4979. (when (and (re-search-forward (concat org-plain-link-re) limit t)
  4980. (not (org-in-src-block-p)))
  4981. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4982. (setq f (get-text-property (match-beginning 0) 'face))
  4983. (setq hl (org-match-string-no-properties 0))
  4984. (if (or (eq f 'org-tag)
  4985. (and (listp f) (memq 'org-tag f)))
  4986. nil
  4987. (add-text-properties (match-beginning 0) (match-end 0)
  4988. (list 'mouse-face 'highlight
  4989. 'face 'org-link
  4990. 'htmlize-link `(:uri ,hl)
  4991. 'keymap org-mouse-map))
  4992. (org-rear-nonsticky-at (match-end 0)))
  4993. t)))
  4994. (defun org-activate-code (limit)
  4995. (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  4996. (progn
  4997. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4998. (remove-text-properties (match-beginning 0) (match-end 0)
  4999. '(display t invisible t intangible t))
  5000. t)))
  5001. (defcustom org-src-fontify-natively nil
  5002. "When non-nil, fontify code in code blocks."
  5003. :type 'boolean
  5004. :version "24.1"
  5005. :group 'org-appearance
  5006. :group 'org-babel)
  5007. (defcustom org-allow-promoting-top-level-subtree nil
  5008. "When non-nil, allow promoting a top level subtree.
  5009. The leading star of the top level headline will be replaced
  5010. by a #."
  5011. :type 'boolean
  5012. :version "24.1"
  5013. :group 'org-appearance)
  5014. (defun org-fontify-meta-lines-and-blocks (limit)
  5015. (condition-case nil
  5016. (org-fontify-meta-lines-and-blocks-1 limit)
  5017. (error (message "org-mode fontification error"))))
  5018. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  5019. "Fontify #+ lines and blocks, in the correct ways."
  5020. (let ((case-fold-search t))
  5021. (if (re-search-forward
  5022. "^\\([ \t]*#\\(\\(\\+[a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  5023. limit t)
  5024. (let ((beg (match-beginning 0))
  5025. (block-start (match-end 0))
  5026. (block-end nil)
  5027. (lang (match-string 7))
  5028. (beg1 (line-beginning-position 2))
  5029. (dc1 (downcase (match-string 2)))
  5030. (dc3 (downcase (match-string 3)))
  5031. end end1 quoting block-type ovl)
  5032. (cond
  5033. ((member dc1 '("+html:" "+ascii:" "+latex:"))
  5034. ;; a single line of backend-specific content
  5035. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5036. (remove-text-properties (match-beginning 0) (match-end 0)
  5037. '(display t invisible t intangible t))
  5038. (add-text-properties (match-beginning 1) (match-end 3)
  5039. '(font-lock-fontified t face org-meta-line))
  5040. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  5041. '(font-lock-fontified t face org-block))
  5042. ; for backend-specific code
  5043. t)
  5044. ((and (match-end 4) (equal dc3 "+begin"))
  5045. ;; Truly a block
  5046. (setq block-type (downcase (match-string 5))
  5047. quoting (member block-type org-protecting-blocks))
  5048. (when (re-search-forward
  5049. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  5050. nil t) ;; on purpose, we look further than LIMIT
  5051. (setq end (min (point-max) (match-end 0))
  5052. end1 (min (point-max) (1- (match-beginning 0))))
  5053. (setq block-end (match-beginning 0))
  5054. (when quoting
  5055. (remove-text-properties beg end
  5056. '(display t invisible t intangible t)))
  5057. (add-text-properties
  5058. beg end
  5059. '(font-lock-fontified t font-lock-multiline t))
  5060. (add-text-properties beg beg1 '(face org-meta-line))
  5061. (add-text-properties end1 (min (point-max) (1+ end))
  5062. '(face org-meta-line)) ; for end_src
  5063. (cond
  5064. ((and lang (not (string= lang "")) org-src-fontify-natively)
  5065. (org-src-font-lock-fontify-block lang block-start block-end)
  5066. ;; remove old background overlays
  5067. (mapc (lambda (ov)
  5068. (if (eq (overlay-get ov 'face) 'org-block-background)
  5069. (delete-overlay ov)))
  5070. (overlays-at (/ (+ beg1 block-end) 2)))
  5071. ;; add a background overlay
  5072. (setq ovl (make-overlay beg1 block-end))
  5073. (overlay-put ovl 'face 'org-block-background)
  5074. (overlay-put ovl 'evaporate t)) ;; make it go away when empty
  5075. (quoting
  5076. (add-text-properties beg1 (min (point-max) (1+ end1))
  5077. '(face org-block))) ; end of source block
  5078. ((not org-fontify-quote-and-verse-blocks))
  5079. ((string= block-type "quote")
  5080. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-quote)))
  5081. ((string= block-type "verse")
  5082. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-verse))))
  5083. (add-text-properties beg beg1 '(face org-block-begin-line))
  5084. (add-text-properties (min (point-max) (1+ end)) (min (point-max) (1+ end1))
  5085. '(face org-block-end-line))
  5086. t))
  5087. ((member dc1 '("+title:" "+author:" "+email:" "+date:"))
  5088. (add-text-properties
  5089. beg (match-end 3)
  5090. (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
  5091. '(font-lock-fontified t invisible t)
  5092. '(font-lock-fontified t face org-document-info-keyword)))
  5093. (add-text-properties
  5094. (match-beginning 6) (min (point-max) (1+ (match-end 6)))
  5095. (if (string-equal dc1 "+title:")
  5096. '(font-lock-fontified t face org-document-title)
  5097. '(font-lock-fontified t face org-document-info))))
  5098. ((or (equal dc1 "+results")
  5099. (member dc1 '("+begin:" "+end:" "+caption:" "+label:"
  5100. "+orgtbl:" "+tblfm:" "+tblname:" "+results:"
  5101. "+call:" "+header:" "+headers:" "+name:"))
  5102. (and (match-end 4) (equal dc3 "+attr")))
  5103. (add-text-properties
  5104. beg (match-end 0)
  5105. '(font-lock-fontified t face org-meta-line))
  5106. t)
  5107. ((member dc3 '(" " ""))
  5108. (add-text-properties
  5109. beg (match-end 0)
  5110. '(font-lock-fontified t face font-lock-comment-face)))
  5111. ((not (member (char-after beg) '(?\ ?\t)))
  5112. ;; just any other in-buffer setting, but not indented
  5113. (add-text-properties
  5114. beg (match-end 0)
  5115. '(font-lock-fontified t face org-meta-line))
  5116. t)
  5117. (t nil))))))
  5118. (defun org-activate-angle-links (limit)
  5119. "Run through the buffer and add overlays to links."
  5120. (if (and (re-search-forward org-angle-link-re limit t)
  5121. (not (org-in-src-block-p)))
  5122. (progn
  5123. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5124. (add-text-properties (match-beginning 0) (match-end 0)
  5125. (list 'mouse-face 'highlight
  5126. 'keymap org-mouse-map))
  5127. (org-rear-nonsticky-at (match-end 0))
  5128. t)))
  5129. (defun org-activate-footnote-links (limit)
  5130. "Run through the buffer and add overlays to footnotes."
  5131. (let ((fn (org-footnote-next-reference-or-definition limit)))
  5132. (when fn
  5133. (let ((beg (nth 1 fn)) (end (nth 2 fn)))
  5134. (org-remove-flyspell-overlays-in beg end)
  5135. (add-text-properties beg end
  5136. (list 'mouse-face 'highlight
  5137. 'keymap org-mouse-map
  5138. 'help-echo
  5139. (if (= (point-at-bol) beg)
  5140. "Footnote definition"
  5141. "Footnote reference")
  5142. 'font-lock-fontified t
  5143. 'font-lock-multiline t
  5144. 'face 'org-footnote))))))
  5145. (defun org-activate-bracket-links (limit)
  5146. "Run through the buffer and add overlays to bracketed links."
  5147. (if (and (re-search-forward org-bracket-link-regexp limit t)
  5148. (not (org-in-src-block-p)))
  5149. (let* ((hl (org-match-string-no-properties 1))
  5150. (help (concat "LINK: " hl))
  5151. ;; FIXME: Above we should remove the escapes. But that
  5152. ;; requires another match, protecting match data, a lot
  5153. ;; of overhead for font-lock.
  5154. (ip (org-maybe-intangible
  5155. (list 'invisible 'org-link
  5156. 'keymap org-mouse-map 'mouse-face 'highlight
  5157. 'font-lock-multiline t 'help-echo help
  5158. 'htmlize-link `(:uri ,hl))))
  5159. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  5160. 'font-lock-multiline t 'help-echo help
  5161. 'htmlize-link `(:uri ,hl))))
  5162. ;; We need to remove the invisible property here. Table narrowing
  5163. ;; may have made some of this invisible.
  5164. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5165. (remove-text-properties (match-beginning 0) (match-end 0)
  5166. '(invisible nil))
  5167. (if (match-end 3)
  5168. (progn
  5169. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  5170. (org-rear-nonsticky-at (match-beginning 3))
  5171. (add-text-properties (match-beginning 3) (match-end 3) vp)
  5172. (org-rear-nonsticky-at (match-end 3))
  5173. (add-text-properties (match-end 3) (match-end 0) ip)
  5174. (org-rear-nonsticky-at (match-end 0)))
  5175. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  5176. (org-rear-nonsticky-at (match-beginning 1))
  5177. (add-text-properties (match-beginning 1) (match-end 1) vp)
  5178. (org-rear-nonsticky-at (match-end 1))
  5179. (add-text-properties (match-end 1) (match-end 0) ip)
  5180. (org-rear-nonsticky-at (match-end 0)))
  5181. t)))
  5182. (defun org-activate-dates (limit)
  5183. "Run through the buffer and add overlays to dates."
  5184. (if (and (re-search-forward org-tsr-regexp-both limit t)
  5185. (not (equal (char-before (match-beginning 0)) 91)))
  5186. (progn
  5187. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5188. (add-text-properties (match-beginning 0) (match-end 0)
  5189. (list 'mouse-face 'highlight
  5190. 'keymap org-mouse-map))
  5191. (org-rear-nonsticky-at (match-end 0))
  5192. (when org-display-custom-times
  5193. (if (match-end 3)
  5194. (org-display-custom-time (match-beginning 3) (match-end 3)))
  5195. (org-display-custom-time (match-beginning 1) (match-end 1)))
  5196. t)))
  5197. (defvar org-target-link-regexp nil
  5198. "Regular expression matching radio targets in plain text.")
  5199. (make-variable-buffer-local 'org-target-link-regexp)
  5200. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  5201. "Regular expression matching a link target.")
  5202. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  5203. "Regular expression matching a radio target.")
  5204. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  5205. "Regular expression matching any target.")
  5206. (defun org-activate-target-links (limit)
  5207. "Run through the buffer and add overlays to target matches."
  5208. (when org-target-link-regexp
  5209. (let ((case-fold-search t))
  5210. (if (re-search-forward org-target-link-regexp limit t)
  5211. (progn
  5212. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5213. (add-text-properties (match-beginning 0) (match-end 0)
  5214. (list 'mouse-face 'highlight
  5215. 'keymap org-mouse-map
  5216. 'help-echo "Radio target link"
  5217. 'org-linked-text t))
  5218. (org-rear-nonsticky-at (match-end 0))
  5219. t)))))
  5220. (defun org-update-radio-target-regexp ()
  5221. "Find all radio targets in this file and update the regular expression."
  5222. (interactive)
  5223. (when (memq 'radio org-activate-links)
  5224. (setq org-target-link-regexp
  5225. (org-make-target-link-regexp (org-all-targets 'radio)))
  5226. (org-restart-font-lock)))
  5227. (defun org-hide-wide-columns (limit)
  5228. (let (s e)
  5229. (setq s (text-property-any (point) (or limit (point-max))
  5230. 'org-cwidth t))
  5231. (when s
  5232. (setq e (next-single-property-change s 'org-cwidth))
  5233. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  5234. (goto-char e)
  5235. t)))
  5236. (defvar org-latex-and-related-regexp nil
  5237. "Regular expression for highlighting LaTeX, entities and sub/superscript.")
  5238. (defvar org-match-substring-regexp)
  5239. (defvar org-match-substring-with-braces-regexp)
  5240. (defun org-compute-latex-and-related-regexp ()
  5241. "Compute regular expression for LaTeX, entities and sub/superscript.
  5242. Result depends on variable `org-highlight-latex-and-related'."
  5243. (org-set-local
  5244. 'org-latex-and-related-regexp
  5245. (let* ((re-sub
  5246. (cond ((not (memq 'script org-highlight-latex-and-related)) nil)
  5247. ((eq org-use-sub-superscripts '{})
  5248. (list org-match-substring-with-braces-regexp))
  5249. (org-use-sub-superscripts (list org-match-substring-regexp))))
  5250. (re-latex
  5251. (when (memq 'latex org-highlight-latex-and-related)
  5252. (let ((matchers (plist-get org-format-latex-options :matchers)))
  5253. (delq nil
  5254. (mapcar (lambda (x)
  5255. (and (member (car x) matchers) (nth 1 x)))
  5256. org-latex-regexps)))))
  5257. (re-entities
  5258. (when (memq 'entities org-highlight-latex-and-related)
  5259. (list "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))))
  5260. (mapconcat 'identity (append re-latex re-entities re-sub) "\\|"))))
  5261. (defun org-do-latex-and-related (limit)
  5262. "Highlight LaTeX snippets and environments, entities and sub/superscript.
  5263. LIMIT bounds the search for syntax to highlight. Stop at first
  5264. highlighted object, if any. Return t if some highlighting was
  5265. done, nil otherwise."
  5266. (when (org-string-nw-p org-latex-and-related-regexp)
  5267. (catch 'found
  5268. (while (re-search-forward org-latex-and-related-regexp limit t)
  5269. (unless (memq (car-safe (get-text-property (1+ (match-beginning 0))
  5270. 'face))
  5271. '(org-code org-verbatim underline))
  5272. (let ((offset (if (memq (char-after (1+ (match-beginning 0)))
  5273. '(?_ ?^))
  5274. 1
  5275. 0)))
  5276. (font-lock-prepend-text-property
  5277. (+ offset (match-beginning 0)) (match-end 0)
  5278. 'face 'org-latex-and-related)
  5279. (add-text-properties (+ offset (match-beginning 0)) (match-end 0)
  5280. '(font-lock-multiline t)))
  5281. (throw 'found t)))
  5282. nil)))
  5283. (defun org-restart-font-lock ()
  5284. "Restart `font-lock-mode', to force refontification."
  5285. (when (and (boundp 'font-lock-mode) font-lock-mode)
  5286. (font-lock-mode -1)
  5287. (font-lock-mode 1)))
  5288. (defun org-all-targets (&optional radio)
  5289. "Return a list of all targets in this file.
  5290. When optional argument RADIO is non-nil, only find radio
  5291. targets."
  5292. (let ((re (if radio org-radio-target-regexp org-target-regexp)) rtn)
  5293. (save-excursion
  5294. (goto-char (point-min))
  5295. (while (re-search-forward re nil t)
  5296. ;; Make sure point is really within the object.
  5297. (backward-char)
  5298. (let ((obj (org-element-context)))
  5299. (when (memq (org-element-type obj) '(radio-target target))
  5300. (add-to-list 'rtn (downcase (org-element-property :value obj))))))
  5301. rtn)))
  5302. (defun org-make-target-link-regexp (targets)
  5303. "Make regular expression matching all strings in TARGETS.
  5304. The regular expression finds the targets also if there is a line break
  5305. between words."
  5306. (and targets
  5307. (concat
  5308. "\\<\\("
  5309. (mapconcat
  5310. (lambda (x)
  5311. (setq x (regexp-quote x))
  5312. (while (string-match " +" x)
  5313. (setq x (replace-match "\\s-+" t t x)))
  5314. x)
  5315. targets
  5316. "\\|")
  5317. "\\)\\>")))
  5318. (defun org-activate-tags (limit)
  5319. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  5320. (progn
  5321. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5322. (add-text-properties (match-beginning 1) (match-end 1)
  5323. (list 'mouse-face 'highlight
  5324. 'keymap org-mouse-map))
  5325. (org-rear-nonsticky-at (match-end 1))
  5326. t)))
  5327. (defun org-outline-level ()
  5328. "Compute the outline level of the heading at point.
  5329. If this is called at a normal headline, the level is the number of stars.
  5330. Use `org-reduced-level' to remove the effect of `org-odd-levels'."
  5331. (save-excursion
  5332. (if (not (condition-case nil
  5333. (org-back-to-heading t)
  5334. (error nil)))
  5335. 0
  5336. (looking-at org-outline-regexp)
  5337. (1- (- (match-end 0) (match-beginning 0))))))
  5338. (defvar org-font-lock-keywords nil)
  5339. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\+?\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  5340. "Regular expression matching a property line.")
  5341. (defvar org-font-lock-hook nil
  5342. "Functions to be called for special font lock stuff.")
  5343. (defvar org-font-lock-set-keywords-hook nil
  5344. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5345. This is called after `org-font-lock-extra-keywords' is defined, but before
  5346. it is installed to be used by font lock. This can be useful if something
  5347. needs to be inserted at a specific position in the font-lock sequence.")
  5348. (defun org-font-lock-hook (limit)
  5349. "Run `org-font-lock-hook' within LIMIT."
  5350. (run-hook-with-args 'org-font-lock-hook limit))
  5351. (defun org-set-font-lock-defaults ()
  5352. "Set font lock defaults for the current buffer."
  5353. (let* ((em org-fontify-emphasized-text)
  5354. (lk org-activate-links)
  5355. (org-font-lock-extra-keywords
  5356. (list
  5357. ;; Call the hook
  5358. '(org-font-lock-hook)
  5359. ;; Headlines
  5360. `(,(if org-fontify-whole-heading-line
  5361. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5362. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5363. (1 (org-get-level-face 1))
  5364. (2 (org-get-level-face 2))
  5365. (3 (org-get-level-face 3)))
  5366. ;; Table lines
  5367. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5368. (1 'org-table t))
  5369. ;; Table internals
  5370. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5371. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5372. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5373. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5374. ;; Drawers
  5375. (list org-drawer-regexp '(0 'org-special-keyword t))
  5376. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  5377. ;; Properties
  5378. (list org-property-re
  5379. '(1 'org-special-keyword t)
  5380. '(3 'org-property-value t))
  5381. ;; Links
  5382. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5383. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5384. (if (memq 'plain lk) '(org-activate-plain-links))
  5385. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5386. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  5387. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5388. (if (memq 'footnote lk) '(org-activate-footnote-links))
  5389. ;; Targets.
  5390. (list org-any-target-regexp '(0 'org-target t))
  5391. ;; Diary sexps.
  5392. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5393. '(org-hide-wide-columns (0 nil append))
  5394. ;; TODO keyword
  5395. (list (format org-heading-keyword-regexp-format
  5396. org-todo-regexp)
  5397. '(2 (org-get-todo-face 2) t))
  5398. ;; DONE
  5399. (if org-fontify-done-headline
  5400. (list (format org-heading-keyword-regexp-format
  5401. (concat
  5402. "\\(?:"
  5403. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5404. "\\)"))
  5405. '(2 'org-headline-done t))
  5406. nil)
  5407. ;; Priorities
  5408. '(org-font-lock-add-priority-faces)
  5409. ;; Tags
  5410. '(org-font-lock-add-tag-faces)
  5411. ;; Special keywords
  5412. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5413. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5414. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5415. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5416. ;; Emphasis
  5417. (if em
  5418. (if (featurep 'xemacs)
  5419. '(org-do-emphasis-faces (0 nil append))
  5420. '(org-do-emphasis-faces)))
  5421. ;; Checkboxes
  5422. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5423. 1 'org-checkbox prepend)
  5424. (if (cdr (assq 'checkbox org-list-automatic-rules))
  5425. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5426. (0 (org-get-checkbox-statistics-face) t)))
  5427. ;; Description list items
  5428. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5429. 1 'org-list-dt prepend)
  5430. ;; ARCHIVEd headings
  5431. (list (concat
  5432. org-outline-regexp-bol
  5433. "\\(.*:" org-archive-tag ":.*\\)")
  5434. '(1 'org-archived prepend))
  5435. ;; Specials
  5436. '(org-do-latex-and-related)
  5437. '(org-fontify-entities)
  5438. '(org-raise-scripts)
  5439. ;; Code
  5440. '(org-activate-code (1 'org-code t))
  5441. ;; COMMENT
  5442. (list (format org-heading-keyword-regexp-format
  5443. (concat "\\("
  5444. org-comment-string "\\|" org-quote-string
  5445. "\\)"))
  5446. '(2 'org-special-keyword t))
  5447. ;; Blocks and meta lines
  5448. '(org-fontify-meta-lines-and-blocks)
  5449. )))
  5450. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5451. (run-hooks 'org-font-lock-set-keywords-hook)
  5452. ;; Now set the full font-lock-keywords
  5453. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5454. (org-set-local 'font-lock-defaults
  5455. '(org-font-lock-keywords t nil nil backward-paragraph))
  5456. (kill-local-variable 'font-lock-keywords) nil))
  5457. (defun org-toggle-pretty-entities ()
  5458. "Toggle the composition display of entities as UTF8 characters."
  5459. (interactive)
  5460. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5461. (org-restart-font-lock)
  5462. (if org-pretty-entities
  5463. (message "Entities are displayed as UTF8 characters")
  5464. (save-restriction
  5465. (widen)
  5466. (org-decompose-region (point-min) (point-max))
  5467. (message "Entities are displayed plain"))))
  5468. (defvar org-custom-properties-overlays nil
  5469. "List of overlays used for custom properties.")
  5470. (make-variable-buffer-local 'org-custom-properties-overlays)
  5471. (defun org-toggle-custom-properties-visibility ()
  5472. "Display or hide properties in `org-custom-properties'."
  5473. (interactive)
  5474. (if org-custom-properties-overlays
  5475. (progn (mapc 'delete-overlay org-custom-properties-overlays)
  5476. (setq org-custom-properties-overlays nil))
  5477. (unless (not org-custom-properties)
  5478. (save-excursion
  5479. (save-restriction
  5480. (widen)
  5481. (goto-char (point-min))
  5482. (while (re-search-forward org-property-re nil t)
  5483. (mapc (lambda(p)
  5484. (when (equal p (substring (match-string 1) 1 -1))
  5485. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5486. (overlay-put o 'invisible t)
  5487. (overlay-put o 'org-custom-property t)
  5488. (push o org-custom-properties-overlays))))
  5489. org-custom-properties)))))))
  5490. (defun org-fontify-entities (limit)
  5491. "Find an entity to fontify."
  5492. (let (ee)
  5493. (when org-pretty-entities
  5494. (catch 'match
  5495. (while (re-search-forward
  5496. "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
  5497. limit t)
  5498. (if (and (not (org-in-indented-comment-line))
  5499. (setq ee (org-entity-get (match-string 1)))
  5500. (= (length (nth 6 ee)) 1))
  5501. (let*
  5502. ((end (if (equal (match-string 2) "{}")
  5503. (match-end 2)
  5504. (match-end 1))))
  5505. (add-text-properties
  5506. (match-beginning 0) end
  5507. (list 'font-lock-fontified t))
  5508. (compose-region (match-beginning 0) end
  5509. (nth 6 ee) nil)
  5510. (backward-char 1)
  5511. (throw 'match t))))
  5512. nil))))
  5513. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5514. "Fontify string S like in Org-mode."
  5515. (with-temp-buffer
  5516. (insert s)
  5517. (let ((org-odd-levels-only odd-levels))
  5518. (org-mode)
  5519. (font-lock-fontify-buffer)
  5520. (buffer-string))))
  5521. (defvar org-m nil)
  5522. (defvar org-l nil)
  5523. (defvar org-f nil)
  5524. (defun org-get-level-face (n)
  5525. "Get the right face for match N in font-lock matching of headlines."
  5526. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5527. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5528. (if org-cycle-level-faces
  5529. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5530. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5531. (cond
  5532. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5533. ((eq n 2) org-f)
  5534. (t (if org-level-color-stars-only nil org-f))))
  5535. (defun org-get-todo-face (kwd)
  5536. "Get the right face for a TODO keyword KWD.
  5537. If KWD is a number, get the corresponding match group."
  5538. (if (numberp kwd) (setq kwd (match-string kwd)))
  5539. (or (org-face-from-face-or-color
  5540. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5541. (and (member kwd org-done-keywords) 'org-done)
  5542. 'org-todo))
  5543. (defun org-face-from-face-or-color (context inherit face-or-color)
  5544. "Create a face list that inherits INHERIT, but sets the foreground color.
  5545. When FACE-OR-COLOR is not a string, just return it."
  5546. (if (stringp face-or-color)
  5547. (list :inherit inherit
  5548. (cdr (assoc context org-faces-easy-properties))
  5549. face-or-color)
  5550. face-or-color))
  5551. (defun org-font-lock-add-tag-faces (limit)
  5552. "Add the special tag faces."
  5553. (when (and org-tag-faces org-tags-special-faces-re)
  5554. (while (re-search-forward org-tags-special-faces-re limit t)
  5555. (add-text-properties (match-beginning 1) (match-end 1)
  5556. (list 'face (org-get-tag-face 1)
  5557. 'font-lock-fontified t))
  5558. (backward-char 1))))
  5559. (defun org-font-lock-add-priority-faces (limit)
  5560. "Add the special priority faces."
  5561. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  5562. (when (save-match-data (org-at-heading-p))
  5563. (add-text-properties
  5564. (match-beginning 0) (match-end 0)
  5565. (list 'face (or (org-face-from-face-or-color
  5566. 'priority 'org-priority
  5567. (cdr (assoc (char-after (match-beginning 1))
  5568. org-priority-faces)))
  5569. 'org-priority)
  5570. 'font-lock-fontified t)))))
  5571. (defun org-get-tag-face (kwd)
  5572. "Get the right face for a TODO keyword KWD.
  5573. If KWD is a number, get the corresponding match group."
  5574. (if (numberp kwd) (setq kwd (match-string kwd)))
  5575. (or (org-face-from-face-or-color
  5576. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  5577. 'org-tag))
  5578. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5579. "Remove fontification and activation overlays from links."
  5580. (font-lock-default-unfontify-region beg end)
  5581. (let* ((buffer-undo-list t)
  5582. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5583. (inhibit-modification-hooks t)
  5584. deactivate-mark buffer-file-name buffer-file-truename)
  5585. (org-decompose-region beg end)
  5586. (remove-text-properties beg end
  5587. '(mouse-face t keymap t org-linked-text t
  5588. invisible t intangible t
  5589. org-no-flyspell t org-emphasis t))
  5590. (org-remove-font-lock-display-properties beg end)))
  5591. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5592. ((raise 0.3) (height 0.7))
  5593. ((raise -0.5))
  5594. ((raise 0.5)))
  5595. "Display properties for showing superscripts and subscripts.")
  5596. (defun org-remove-font-lock-display-properties (beg end)
  5597. "Remove specific display properties that have been added by font lock.
  5598. The will remove the raise properties that are used to show superscripts
  5599. and subscripts."
  5600. (let (next prop)
  5601. (while (< beg end)
  5602. (setq next (next-single-property-change beg 'display nil end)
  5603. prop (get-text-property beg 'display))
  5604. (if (member prop org-script-display)
  5605. (put-text-property beg next 'display nil))
  5606. (setq beg next))))
  5607. (defun org-raise-scripts (limit)
  5608. "Add raise properties to sub/superscripts."
  5609. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5610. (if (re-search-forward
  5611. (if (eq org-use-sub-superscripts t)
  5612. org-match-substring-regexp
  5613. org-match-substring-with-braces-regexp)
  5614. limit t)
  5615. (let* ((pos (point)) table-p comment-p
  5616. (mpos (match-beginning 3))
  5617. (emph-p (get-text-property mpos 'org-emphasis))
  5618. (link-p (get-text-property mpos 'mouse-face))
  5619. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5620. (goto-char (point-at-bol))
  5621. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5622. comment-p (org-looking-at-p "[ \t]*#"))
  5623. (goto-char pos)
  5624. ;; FIXME: Should we go back one character here, for a_b^c
  5625. ;; (goto-char (1- pos)) ;????????????????????
  5626. (if (or comment-p emph-p link-p keyw-p)
  5627. t
  5628. (put-text-property (match-beginning 3) (match-end 0)
  5629. 'display
  5630. (if (equal (char-after (match-beginning 2)) ?^)
  5631. (nth (if table-p 3 1) org-script-display)
  5632. (nth (if table-p 2 0) org-script-display)))
  5633. (add-text-properties (match-beginning 2) (match-end 2)
  5634. (list 'invisible t
  5635. 'org-dwidth t 'org-dwidth-n 1))
  5636. (if (and (eq (char-after (match-beginning 3)) ?{)
  5637. (eq (char-before (match-end 3)) ?}))
  5638. (progn
  5639. (add-text-properties
  5640. (match-beginning 3) (1+ (match-beginning 3))
  5641. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5642. (add-text-properties
  5643. (1- (match-end 3)) (match-end 3)
  5644. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5645. t)))))
  5646. ;;;; Visibility cycling, including org-goto and indirect buffer
  5647. ;;; Cycling
  5648. (defvar org-cycle-global-status nil)
  5649. (make-variable-buffer-local 'org-cycle-global-status)
  5650. (put 'org-cycle-global-status 'org-state t)
  5651. (defvar org-cycle-subtree-status nil)
  5652. (make-variable-buffer-local 'org-cycle-subtree-status)
  5653. (put 'org-cycle-subtree-status 'org-state t)
  5654. (defvar org-inlinetask-min-level)
  5655. ;;;###autoload
  5656. (defun org-cycle (&optional arg)
  5657. "TAB-action and visibility cycling for Org-mode.
  5658. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5659. is outline visibility cycling, but it also invokes other actions
  5660. in special contexts.
  5661. - When this function is called with a prefix argument, rotate the entire
  5662. buffer through 3 states (global cycling)
  5663. 1. OVERVIEW: Show only top-level headlines.
  5664. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5665. 3. SHOW ALL: Show everything.
  5666. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5667. determined by the variable `org-startup-folded', and by any VISIBILITY
  5668. properties in the buffer.
  5669. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5670. including any drawers.
  5671. - When inside a table, re-align the table and move to the next field.
  5672. - When point is at the beginning of a headline, rotate the subtree started
  5673. by this line through 3 different states (local cycling)
  5674. 1. FOLDED: Only the main headline is shown.
  5675. 2. CHILDREN: The main headline and the direct children are shown.
  5676. From this state, you can move to one of the children
  5677. and zoom in further.
  5678. 3. SUBTREE: Show the entire subtree, including body text.
  5679. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5680. - When point is at the beginning of an empty headline and the variable
  5681. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5682. of the headline by demoting and promoting it to likely levels. This
  5683. speeds up creation document structure by pressing TAB once or several
  5684. times right after creating a new headline.
  5685. - When there is a numeric prefix, go up to a heading with level ARG, do
  5686. a `show-subtree' and return to the previous cursor position. If ARG
  5687. is negative, go up that many levels.
  5688. - When point is not at the beginning of a headline, execute the global
  5689. binding for TAB, which is re-indenting the line. See the option
  5690. `org-cycle-emulate-tab' for details.
  5691. - Special case: if point is at the beginning of the buffer and there is
  5692. no headline in line 1, this function will act as if called with prefix arg
  5693. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5694. But only if also the variable `org-cycle-global-at-bob' is t."
  5695. (interactive "P")
  5696. (org-load-modules-maybe)
  5697. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5698. (and org-cycle-level-after-item/entry-creation
  5699. (or (org-cycle-level)
  5700. (org-cycle-item-indentation))))
  5701. (let* (message-log-max ; Don't populate the *Messages* buffer
  5702. (limit-level
  5703. (or org-cycle-max-level
  5704. (and (boundp 'org-inlinetask-min-level)
  5705. org-inlinetask-min-level
  5706. (1- org-inlinetask-min-level))))
  5707. (nstars (and limit-level
  5708. (if org-odd-levels-only
  5709. (and limit-level (1- (* limit-level 2)))
  5710. limit-level)))
  5711. (org-outline-regexp
  5712. (if (not (derived-mode-p 'org-mode))
  5713. outline-regexp
  5714. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5715. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5716. (not (looking-at org-outline-regexp))))
  5717. (org-cycle-hook
  5718. (if bob-special
  5719. (delq 'org-optimize-window-after-visibility-change
  5720. (copy-sequence org-cycle-hook))
  5721. org-cycle-hook))
  5722. (pos (point)))
  5723. (if (or bob-special (equal arg '(4)))
  5724. ;; special case: use global cycling
  5725. (setq arg t))
  5726. (cond
  5727. ((equal arg '(16))
  5728. (setq last-command 'dummy)
  5729. (org-set-startup-visibility)
  5730. (message "Startup visibility, plus VISIBILITY properties"))
  5731. ((equal arg '(64))
  5732. (show-all)
  5733. (message "Entire buffer visible, including drawers"))
  5734. ;; Table: enter it or move to the next field.
  5735. ((org-at-table-p 'any)
  5736. (if (org-at-table.el-p)
  5737. (message "Use C-c ' to edit table.el tables")
  5738. (if arg (org-table-edit-field t)
  5739. (org-table-justify-field-maybe)
  5740. (call-interactively 'org-table-next-field))))
  5741. ((run-hook-with-args-until-success
  5742. 'org-tab-after-check-for-table-hook))
  5743. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5744. ((eq arg t) (org-cycle-internal-global))
  5745. ;; Drawers: delegate to `org-flag-drawer'.
  5746. ((and org-drawers org-drawer-regexp
  5747. (save-excursion
  5748. (beginning-of-line 1)
  5749. (looking-at org-drawer-regexp)))
  5750. (org-flag-drawer ; toggle block visibility
  5751. (not (get-char-property (match-end 0) 'invisible))))
  5752. ;; Show-subtree, ARG levels up from here.
  5753. ((integerp arg)
  5754. (save-excursion
  5755. (org-back-to-heading)
  5756. (outline-up-heading (if (< arg 0) (- arg)
  5757. (- (funcall outline-level) arg)))
  5758. (org-show-subtree)))
  5759. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5760. ((and (featurep 'org-inlinetask)
  5761. (org-inlinetask-at-task-p)
  5762. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5763. (org-inlinetask-toggle-visibility))
  5764. ((org-try-cdlatex-tab))
  5765. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5766. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5767. (save-excursion (beginning-of-line 1)
  5768. (looking-at org-outline-regexp)))
  5769. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5770. (org-cycle-internal-local))
  5771. ;; From there: TAB emulation and template completion.
  5772. (buffer-read-only (org-back-to-heading))
  5773. ((run-hook-with-args-until-success
  5774. 'org-tab-after-check-for-cycling-hook))
  5775. ((org-try-structure-completion))
  5776. ((run-hook-with-args-until-success
  5777. 'org-tab-before-tab-emulation-hook))
  5778. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5779. (or (not (bolp))
  5780. (not (looking-at org-outline-regexp))))
  5781. (call-interactively (global-key-binding "\t")))
  5782. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5783. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5784. (or (and (eq org-cycle-emulate-tab 'white)
  5785. (= (match-end 0) (point-at-eol)))
  5786. (and (eq org-cycle-emulate-tab 'whitestart)
  5787. (>= (match-end 0) pos))))
  5788. t
  5789. (eq org-cycle-emulate-tab t))
  5790. (call-interactively (global-key-binding "\t")))
  5791. (t (save-excursion
  5792. (org-back-to-heading)
  5793. (org-cycle)))))))
  5794. (defun org-cycle-internal-global ()
  5795. "Do the global cycling action."
  5796. ;; Hack to avoid display of messages for .org attachments in Gnus
  5797. (let (message-log-max ; Don't populate the *Messages* buffer
  5798. (ga (string-match "\\*fontification" (buffer-name))))
  5799. (cond
  5800. ((and (eq last-command this-command)
  5801. (eq org-cycle-global-status 'overview))
  5802. ;; We just created the overview - now do table of contents
  5803. ;; This can be slow in very large buffers, so indicate action
  5804. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  5805. (unless ga (message "CONTENTS..."))
  5806. (org-content)
  5807. (unless ga (message "CONTENTS...done"))
  5808. (setq org-cycle-global-status 'contents)
  5809. (run-hook-with-args 'org-cycle-hook 'contents))
  5810. ((and (eq last-command this-command)
  5811. (eq org-cycle-global-status 'contents))
  5812. ;; We just showed the table of contents - now show everything
  5813. (run-hook-with-args 'org-pre-cycle-hook 'all)
  5814. (show-all)
  5815. (unless ga (message "SHOW ALL"))
  5816. (setq org-cycle-global-status 'all)
  5817. (run-hook-with-args 'org-cycle-hook 'all))
  5818. (t
  5819. ;; Default action: go to overview
  5820. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  5821. (org-overview)
  5822. (unless ga (message "OVERVIEW"))
  5823. (setq org-cycle-global-status 'overview)
  5824. (run-hook-with-args 'org-cycle-hook 'overview)))))
  5825. (defun org-cycle-internal-local ()
  5826. "Do the local cycling action."
  5827. (let (message-log-max ; Don't populate the *Messages* buffer
  5828. (goal-column 0) eoh eol eos has-children children-skipped struct)
  5829. ;; First, determine end of headline (EOH), end of subtree or item
  5830. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  5831. (save-excursion
  5832. (if (org-at-item-p)
  5833. (progn
  5834. (beginning-of-line)
  5835. (setq struct (org-list-struct))
  5836. (setq eoh (point-at-eol))
  5837. (setq eos (org-list-get-item-end-before-blank (point) struct))
  5838. (setq has-children (org-list-has-child-p (point) struct)))
  5839. (org-back-to-heading)
  5840. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  5841. (setq eos (save-excursion (1- (org-end-of-subtree t t))))
  5842. (setq has-children
  5843. (or (save-excursion
  5844. (let ((level (funcall outline-level)))
  5845. (outline-next-heading)
  5846. (and (org-at-heading-p t)
  5847. (> (funcall outline-level) level))))
  5848. (save-excursion
  5849. (org-list-search-forward (org-item-beginning-re) eos t)))))
  5850. ;; Determine end invisible part of buffer (EOL)
  5851. (beginning-of-line 2)
  5852. ;; XEmacs doesn't have `next-single-char-property-change'
  5853. (if (featurep 'xemacs)
  5854. (while (and (not (eobp)) ;; this is like `next-line'
  5855. (get-char-property (1- (point)) 'invisible))
  5856. (beginning-of-line 2))
  5857. (while (and (not (eobp)) ;; this is like `next-line'
  5858. (get-char-property (1- (point)) 'invisible))
  5859. (goto-char (next-single-char-property-change (point) 'invisible))
  5860. (and (eolp) (beginning-of-line 2))))
  5861. (setq eol (point)))
  5862. ;; Find out what to do next and set `this-command'
  5863. (cond
  5864. ((= eos eoh)
  5865. ;; Nothing is hidden behind this heading
  5866. (unless (org-before-first-heading-p)
  5867. (run-hook-with-args 'org-pre-cycle-hook 'empty))
  5868. (message "EMPTY ENTRY")
  5869. (setq org-cycle-subtree-status nil)
  5870. (save-excursion
  5871. (goto-char eos)
  5872. (outline-next-heading)
  5873. (if (outline-invisible-p) (org-flag-heading nil))))
  5874. ((and (or (>= eol eos)
  5875. (not (string-match "\\S-" (buffer-substring eol eos))))
  5876. (or has-children
  5877. (not (setq children-skipped
  5878. org-cycle-skip-children-state-if-no-children))))
  5879. ;; Entire subtree is hidden in one line: children view
  5880. (unless (org-before-first-heading-p)
  5881. (run-hook-with-args 'org-pre-cycle-hook 'children))
  5882. (if (org-at-item-p)
  5883. (org-list-set-item-visibility (point-at-bol) struct 'children)
  5884. (org-show-entry)
  5885. (org-with-limited-levels (show-children))
  5886. ;; FIXME: This slows down the func way too much.
  5887. ;; How keep drawers hidden in subtree anyway?
  5888. ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
  5889. ;; (org-cycle-hide-drawers 'subtree))
  5890. ;; Fold every list in subtree to top-level items.
  5891. (when (eq org-cycle-include-plain-lists 'integrate)
  5892. (save-excursion
  5893. (org-back-to-heading)
  5894. (while (org-list-search-forward (org-item-beginning-re) eos t)
  5895. (beginning-of-line 1)
  5896. (let* ((struct (org-list-struct))
  5897. (prevs (org-list-prevs-alist struct))
  5898. (end (org-list-get-bottom-point struct)))
  5899. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  5900. (org-list-get-all-items (point) struct prevs))
  5901. (goto-char end))))))
  5902. (message "CHILDREN")
  5903. (save-excursion
  5904. (goto-char eos)
  5905. (outline-next-heading)
  5906. (if (outline-invisible-p) (org-flag-heading nil)))
  5907. (setq org-cycle-subtree-status 'children)
  5908. (unless (org-before-first-heading-p)
  5909. (run-hook-with-args 'org-cycle-hook 'children)))
  5910. ((or children-skipped
  5911. (and (eq last-command this-command)
  5912. (eq org-cycle-subtree-status 'children)))
  5913. ;; We just showed the children, or no children are there,
  5914. ;; now show everything.
  5915. (unless (org-before-first-heading-p)
  5916. (run-hook-with-args 'org-pre-cycle-hook 'subtree))
  5917. (outline-flag-region eoh eos nil)
  5918. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  5919. (setq org-cycle-subtree-status 'subtree)
  5920. (unless (org-before-first-heading-p)
  5921. (run-hook-with-args 'org-cycle-hook 'subtree)))
  5922. (t
  5923. ;; Default action: hide the subtree.
  5924. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  5925. (outline-flag-region eoh eos t)
  5926. (message "FOLDED")
  5927. (setq org-cycle-subtree-status 'folded)
  5928. (unless (org-before-first-heading-p)
  5929. (run-hook-with-args 'org-cycle-hook 'folded))))))
  5930. ;;;###autoload
  5931. (defun org-global-cycle (&optional arg)
  5932. "Cycle the global visibility. For details see `org-cycle'.
  5933. With \\[universal-argument] prefix arg, switch to startup visibility.
  5934. With a numeric prefix, show all headlines up to that level."
  5935. (interactive "P")
  5936. (let ((org-cycle-include-plain-lists
  5937. (if (derived-mode-p 'org-mode) org-cycle-include-plain-lists nil)))
  5938. (cond
  5939. ((integerp arg)
  5940. (show-all)
  5941. (hide-sublevels arg)
  5942. (setq org-cycle-global-status 'contents))
  5943. ((equal arg '(4))
  5944. (org-set-startup-visibility)
  5945. (message "Startup visibility, plus VISIBILITY properties."))
  5946. (t
  5947. (org-cycle '(4))))))
  5948. (defun org-set-startup-visibility ()
  5949. "Set the visibility required by startup options and properties."
  5950. (cond
  5951. ((eq org-startup-folded t)
  5952. (org-cycle '(4)))
  5953. ((eq org-startup-folded 'content)
  5954. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  5955. (org-cycle '(4)) (org-cycle '(4)))))
  5956. (unless (eq org-startup-folded 'showeverything)
  5957. (if org-hide-block-startup (org-hide-block-all))
  5958. (org-set-visibility-according-to-property 'no-cleanup)
  5959. (org-cycle-hide-archived-subtrees 'all)
  5960. (org-cycle-hide-drawers 'all)
  5961. (org-cycle-show-empty-lines t)))
  5962. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  5963. "Switch subtree visibilities according to :VISIBILITY: property."
  5964. (interactive)
  5965. (let (org-show-entry-below state)
  5966. (save-excursion
  5967. (goto-char (point-min))
  5968. (while (re-search-forward
  5969. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  5970. nil t)
  5971. (setq state (match-string 1))
  5972. (save-excursion
  5973. (org-back-to-heading t)
  5974. (hide-subtree)
  5975. (org-reveal)
  5976. (cond
  5977. ((equal state '("fold" "folded"))
  5978. (hide-subtree))
  5979. ((equal state "children")
  5980. (org-show-hidden-entry)
  5981. (show-children))
  5982. ((equal state "content")
  5983. (save-excursion
  5984. (save-restriction
  5985. (org-narrow-to-subtree)
  5986. (org-content))))
  5987. ((member state '("all" "showall"))
  5988. (show-subtree)))))
  5989. (unless no-cleanup
  5990. (org-cycle-hide-archived-subtrees 'all)
  5991. (org-cycle-hide-drawers 'all)
  5992. (org-cycle-show-empty-lines 'all)))))
  5993. ;; This function uses outline-regexp instead of the more fundamental
  5994. ;; org-outline-regexp so that org-cycle-global works outside of Org
  5995. ;; buffers, where outline-regexp is needed.
  5996. (defun org-overview ()
  5997. "Switch to overview mode, showing only top-level headlines.
  5998. Really, this shows all headlines with level equal or greater than the level
  5999. of the first headline in the buffer. This is important, because if the
  6000. first headline is not level one, then (hide-sublevels 1) gives confusing
  6001. results."
  6002. (interactive)
  6003. (let ((l (org-current-line))
  6004. (level (save-excursion
  6005. (goto-char (point-min))
  6006. (if (re-search-forward (concat "^" outline-regexp) nil t)
  6007. (progn
  6008. (goto-char (match-beginning 0))
  6009. (funcall outline-level))))))
  6010. (and level (hide-sublevels level))
  6011. (recenter '(4))
  6012. (org-goto-line l)))
  6013. (defun org-content (&optional arg)
  6014. "Show all headlines in the buffer, like a table of contents.
  6015. With numerical argument N, show content up to level N."
  6016. (interactive "P")
  6017. (save-excursion
  6018. ;; Visit all headings and show their offspring
  6019. (and (integerp arg) (org-overview))
  6020. (goto-char (point-max))
  6021. (catch 'exit
  6022. (while (and (progn (condition-case nil
  6023. (outline-previous-visible-heading 1)
  6024. (error (goto-char (point-min))))
  6025. t)
  6026. (looking-at org-outline-regexp))
  6027. (if (integerp arg)
  6028. (show-children (1- arg))
  6029. (show-branches))
  6030. (if (bobp) (throw 'exit nil))))))
  6031. (defun org-optimize-window-after-visibility-change (state)
  6032. "Adjust the window after a change in outline visibility.
  6033. This function is the default value of the hook `org-cycle-hook'."
  6034. (when (get-buffer-window (current-buffer))
  6035. (cond
  6036. ((eq state 'content) nil)
  6037. ((eq state 'all) nil)
  6038. ((eq state 'folded) nil)
  6039. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  6040. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  6041. (defun org-remove-empty-overlays-at (pos)
  6042. "Remove outline overlays that do not contain non-white stuff."
  6043. (mapc
  6044. (lambda (o)
  6045. (and (eq 'outline (overlay-get o 'invisible))
  6046. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  6047. (overlay-end o))))
  6048. (delete-overlay o)))
  6049. (overlays-at pos)))
  6050. (defun org-clean-visibility-after-subtree-move ()
  6051. "Fix visibility issues after moving a subtree."
  6052. ;; First, find a reasonable region to look at:
  6053. ;; Start two siblings above, end three below
  6054. (let* ((beg (save-excursion
  6055. (and (org-get-last-sibling)
  6056. (org-get-last-sibling))
  6057. (point)))
  6058. (end (save-excursion
  6059. (and (org-get-next-sibling)
  6060. (org-get-next-sibling)
  6061. (org-get-next-sibling))
  6062. (if (org-at-heading-p)
  6063. (point-at-eol)
  6064. (point))))
  6065. (level (looking-at "\\*+"))
  6066. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  6067. (save-excursion
  6068. (save-restriction
  6069. (narrow-to-region beg end)
  6070. (when re
  6071. ;; Properly fold already folded siblings
  6072. (goto-char (point-min))
  6073. (while (re-search-forward re nil t)
  6074. (if (and (not (outline-invisible-p))
  6075. (save-excursion
  6076. (goto-char (point-at-eol)) (outline-invisible-p)))
  6077. (hide-entry))))
  6078. (org-cycle-show-empty-lines 'overview)
  6079. (org-cycle-hide-drawers 'overview)))))
  6080. (defun org-cycle-show-empty-lines (state)
  6081. "Show empty lines above all visible headlines.
  6082. The region to be covered depends on STATE when called through
  6083. `org-cycle-hook'. Lisp program can use t for STATE to get the
  6084. entire buffer covered. Note that an empty line is only shown if there
  6085. are at least `org-cycle-separator-lines' empty lines before the headline."
  6086. (when (not (= org-cycle-separator-lines 0))
  6087. (save-excursion
  6088. (let* ((n (abs org-cycle-separator-lines))
  6089. (re (cond
  6090. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  6091. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  6092. (t (let ((ns (number-to-string (- n 2))))
  6093. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  6094. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  6095. beg end b e)
  6096. (cond
  6097. ((memq state '(overview contents t))
  6098. (setq beg (point-min) end (point-max)))
  6099. ((memq state '(children folded))
  6100. (setq beg (point) end (progn (org-end-of-subtree t t)
  6101. (beginning-of-line 2)
  6102. (point)))))
  6103. (when beg
  6104. (goto-char beg)
  6105. (while (re-search-forward re end t)
  6106. (unless (get-char-property (match-end 1) 'invisible)
  6107. (setq e (match-end 1))
  6108. (if (< org-cycle-separator-lines 0)
  6109. (setq b (save-excursion
  6110. (goto-char (match-beginning 0))
  6111. (org-back-over-empty-lines)
  6112. (if (save-excursion
  6113. (goto-char (max (point-min) (1- (point))))
  6114. (org-at-heading-p))
  6115. (1- (point))
  6116. (point))))
  6117. (setq b (match-beginning 1)))
  6118. (outline-flag-region b e nil)))))))
  6119. ;; Never hide empty lines at the end of the file.
  6120. (save-excursion
  6121. (goto-char (point-max))
  6122. (outline-previous-heading)
  6123. (outline-end-of-heading)
  6124. (if (and (looking-at "[ \t\n]+")
  6125. (= (match-end 0) (point-max)))
  6126. (outline-flag-region (point) (match-end 0) nil))))
  6127. (defun org-show-empty-lines-in-parent ()
  6128. "Move to the parent and re-show empty lines before visible headlines."
  6129. (save-excursion
  6130. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  6131. (org-cycle-show-empty-lines context))))
  6132. (defun org-files-list ()
  6133. "Return `org-agenda-files' list, plus all open org-mode files.
  6134. This is useful for operations that need to scan all of a user's
  6135. open and agenda-wise Org files."
  6136. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  6137. (dolist (buf (buffer-list))
  6138. (with-current-buffer buf
  6139. (if (and (derived-mode-p 'org-mode) (buffer-file-name))
  6140. (let ((file (expand-file-name (buffer-file-name))))
  6141. (unless (member file files)
  6142. (push file files))))))
  6143. files))
  6144. (defsubst org-entry-beginning-position ()
  6145. "Return the beginning position of the current entry."
  6146. (save-excursion (outline-back-to-heading t) (point)))
  6147. (defsubst org-entry-end-position ()
  6148. "Return the end position of the current entry."
  6149. (save-excursion (outline-next-heading) (point)))
  6150. (defun org-cycle-hide-drawers (state)
  6151. "Re-hide all drawers after a visibility state change."
  6152. (when (and (derived-mode-p 'org-mode)
  6153. (not (memq state '(overview folded contents))))
  6154. (save-excursion
  6155. (let* ((globalp (memq state '(contents all)))
  6156. (beg (if globalp (point-min) (point)))
  6157. (end (if globalp (point-max)
  6158. (if (eq state 'children)
  6159. (save-excursion (outline-next-heading) (point))
  6160. (org-end-of-subtree t)))))
  6161. (goto-char beg)
  6162. (while (re-search-forward org-drawer-regexp end t)
  6163. (org-flag-drawer t))))))
  6164. (defun org-cycle-hide-inline-tasks (state)
  6165. "Re-hide inline task when switching to 'contents visibility state."
  6166. (when (and (eq state 'contents)
  6167. (boundp 'org-inlinetask-min-level)
  6168. org-inlinetask-min-level)
  6169. (hide-sublevels (1- org-inlinetask-min-level))))
  6170. (defun org-flag-drawer (flag)
  6171. "When FLAG is non-nil, hide the drawer we are within.
  6172. Otherwise make it visible."
  6173. (save-excursion
  6174. (beginning-of-line 1)
  6175. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  6176. (let ((b (match-end 0)))
  6177. (if (re-search-forward
  6178. "^[ \t]*:END:"
  6179. (save-excursion (outline-next-heading) (point)) t)
  6180. (outline-flag-region b (point-at-eol) flag)
  6181. (error ":END: line missing at position %s" b))))))
  6182. (defun org-subtree-end-visible-p ()
  6183. "Is the end of the current subtree visible?"
  6184. (pos-visible-in-window-p
  6185. (save-excursion (org-end-of-subtree t) (point))))
  6186. (defun org-first-headline-recenter (&optional N)
  6187. "Move cursor to the first headline and recenter the headline.
  6188. Optional argument N means put the headline into the Nth line of the window."
  6189. (goto-char (point-min))
  6190. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  6191. (beginning-of-line)
  6192. (recenter (prefix-numeric-value N))))
  6193. ;;; Saving and restoring visibility
  6194. (defun org-outline-overlay-data (&optional use-markers)
  6195. "Return a list of the locations of all outline overlays.
  6196. These are overlays with the `invisible' property value `outline'.
  6197. The return value is a list of cons cells, with start and stop
  6198. positions for each overlay.
  6199. If USE-MARKERS is set, return the positions as markers."
  6200. (let (beg end)
  6201. (save-excursion
  6202. (save-restriction
  6203. (widen)
  6204. (delq nil
  6205. (mapcar (lambda (o)
  6206. (when (eq (overlay-get o 'invisible) 'outline)
  6207. (setq beg (overlay-start o)
  6208. end (overlay-end o))
  6209. (and beg end (> end beg)
  6210. (if use-markers
  6211. (cons (move-marker (make-marker) beg)
  6212. (move-marker (make-marker) end))
  6213. (cons beg end)))))
  6214. (overlays-in (point-min) (point-max))))))))
  6215. (defun org-set-outline-overlay-data (data)
  6216. "Create visibility overlays for all positions in DATA.
  6217. DATA should have been made by `org-outline-overlay-data'."
  6218. (let (o)
  6219. (save-excursion
  6220. (save-restriction
  6221. (widen)
  6222. (show-all)
  6223. (mapc (lambda (c)
  6224. (outline-flag-region (car c) (cdr c) t))
  6225. data)))))
  6226. ;;; Folding of blocks
  6227. (defvar org-hide-block-overlays nil
  6228. "Overlays hiding blocks.")
  6229. (make-variable-buffer-local 'org-hide-block-overlays)
  6230. (defun org-block-map (function &optional start end)
  6231. "Call FUNCTION at the head of all source blocks in the current buffer.
  6232. Optional arguments START and END can be used to limit the range."
  6233. (let ((start (or start (point-min)))
  6234. (end (or end (point-max))))
  6235. (save-excursion
  6236. (goto-char start)
  6237. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  6238. (save-excursion
  6239. (save-match-data
  6240. (goto-char (match-beginning 0))
  6241. (funcall function)))))))
  6242. (defun org-hide-block-toggle-all ()
  6243. "Toggle the visibility of all blocks in the current buffer."
  6244. (org-block-map #'org-hide-block-toggle))
  6245. (defun org-hide-block-all ()
  6246. "Fold all blocks in the current buffer."
  6247. (interactive)
  6248. (org-show-block-all)
  6249. (org-block-map #'org-hide-block-toggle-maybe))
  6250. (defun org-show-block-all ()
  6251. "Unfold all blocks in the current buffer."
  6252. (interactive)
  6253. (mapc 'delete-overlay org-hide-block-overlays)
  6254. (setq org-hide-block-overlays nil))
  6255. (defun org-hide-block-toggle-maybe ()
  6256. "Toggle visibility of block at point."
  6257. (interactive)
  6258. (let ((case-fold-search t))
  6259. (if (save-excursion
  6260. (beginning-of-line 1)
  6261. (looking-at org-block-regexp))
  6262. (progn (org-hide-block-toggle)
  6263. t) ;; to signal that we took action
  6264. nil))) ;; to signal that we did not
  6265. (defun org-hide-block-toggle (&optional force)
  6266. "Toggle the visibility of the current block."
  6267. (interactive)
  6268. (save-excursion
  6269. (beginning-of-line)
  6270. (if (re-search-forward org-block-regexp nil t)
  6271. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  6272. (end (match-end 0)) ;; end of entire body
  6273. ov)
  6274. (if (memq t (mapcar (lambda (overlay)
  6275. (eq (overlay-get overlay 'invisible)
  6276. 'org-hide-block))
  6277. (overlays-at start)))
  6278. (if (or (not force) (eq force 'off))
  6279. (mapc (lambda (ov)
  6280. (when (member ov org-hide-block-overlays)
  6281. (setq org-hide-block-overlays
  6282. (delq ov org-hide-block-overlays)))
  6283. (when (eq (overlay-get ov 'invisible)
  6284. 'org-hide-block)
  6285. (delete-overlay ov)))
  6286. (overlays-at start)))
  6287. (setq ov (make-overlay start end))
  6288. (overlay-put ov 'invisible 'org-hide-block)
  6289. ;; make the block accessible to isearch
  6290. (overlay-put
  6291. ov 'isearch-open-invisible
  6292. (lambda (ov)
  6293. (when (member ov org-hide-block-overlays)
  6294. (setq org-hide-block-overlays
  6295. (delq ov org-hide-block-overlays)))
  6296. (when (eq (overlay-get ov 'invisible)
  6297. 'org-hide-block)
  6298. (delete-overlay ov))))
  6299. (push ov org-hide-block-overlays)))
  6300. (error "Not looking at a source block"))))
  6301. ;; org-tab-after-check-for-cycling-hook
  6302. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  6303. ;; Remove overlays when changing major mode
  6304. (add-hook 'org-mode-hook
  6305. (lambda () (org-add-hook 'change-major-mode-hook
  6306. 'org-show-block-all 'append 'local)))
  6307. ;;; Org-goto
  6308. (defvar org-goto-window-configuration nil)
  6309. (defvar org-goto-marker nil)
  6310. (defvar org-goto-map)
  6311. (defun org-goto-map ()
  6312. "Set the keymap `org-goto'."
  6313. (setq org-goto-map
  6314. (let ((map (make-sparse-keymap)))
  6315. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command
  6316. mouse-drag-region universal-argument org-occur))
  6317. cmd)
  6318. (while (setq cmd (pop cmds))
  6319. (substitute-key-definition cmd cmd map global-map)))
  6320. (suppress-keymap map)
  6321. (org-defkey map "\C-m" 'org-goto-ret)
  6322. (org-defkey map [(return)] 'org-goto-ret)
  6323. (org-defkey map [(left)] 'org-goto-left)
  6324. (org-defkey map [(right)] 'org-goto-right)
  6325. (org-defkey map [(control ?g)] 'org-goto-quit)
  6326. (org-defkey map "\C-i" 'org-cycle)
  6327. (org-defkey map [(tab)] 'org-cycle)
  6328. (org-defkey map [(down)] 'outline-next-visible-heading)
  6329. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6330. (if org-goto-auto-isearch
  6331. (if (fboundp 'define-key-after)
  6332. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6333. nil)
  6334. (org-defkey map "q" 'org-goto-quit)
  6335. (org-defkey map "n" 'outline-next-visible-heading)
  6336. (org-defkey map "p" 'outline-previous-visible-heading)
  6337. (org-defkey map "f" 'outline-forward-same-level)
  6338. (org-defkey map "b" 'outline-backward-same-level)
  6339. (org-defkey map "u" 'outline-up-heading))
  6340. (org-defkey map "/" 'org-occur)
  6341. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6342. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6343. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6344. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6345. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6346. map)))
  6347. (defconst org-goto-help
  6348. "Browse buffer copy, to find location or copy text.%s
  6349. RET=jump to location C-g=quit and return to previous location
  6350. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6351. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6352. ;; FIXME: Docstring does not mention both interfaces
  6353. (defun org-goto (&optional alternative-interface)
  6354. "Look up a different location in the current file, keeping current visibility.
  6355. When you want look-up or go to a different location in a
  6356. document, the fastest way is often to fold the entire buffer and
  6357. then dive into the tree. This method has the disadvantage, that
  6358. the previous location will be folded, which may not be what you
  6359. want.
  6360. This command works around this by showing a copy of the current
  6361. buffer in an indirect buffer, in overview mode. You can dive
  6362. into the tree in that copy, use org-occur and incremental search
  6363. to find a location. When pressing RET or `Q', the command
  6364. returns to the original buffer in which the visibility is still
  6365. unchanged. After RET it will also jump to the location selected
  6366. in the indirect buffer and expose the headline hierarchy above.
  6367. With a prefix argument, use the alternative interface: e.g. if
  6368. `org-goto-interface' is 'outline use 'outline-path-completion."
  6369. (interactive "P")
  6370. (org-goto-map)
  6371. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6372. (org-refile-use-outline-path t)
  6373. (org-refile-target-verify-function nil)
  6374. (interface
  6375. (if (not alternative-interface)
  6376. org-goto-interface
  6377. (if (eq org-goto-interface 'outline)
  6378. 'outline-path-completion
  6379. 'outline)))
  6380. (org-goto-start-pos (point))
  6381. (selected-point
  6382. (if (eq interface 'outline)
  6383. (car (org-get-location (current-buffer) org-goto-help))
  6384. (let ((pa (org-refile-get-location "Goto" nil nil t)))
  6385. (org-refile-check-position pa)
  6386. (nth 3 pa)))))
  6387. (if selected-point
  6388. (progn
  6389. (org-mark-ring-push org-goto-start-pos)
  6390. (goto-char selected-point)
  6391. (if (or (outline-invisible-p) (org-invisible-p2))
  6392. (org-show-context 'org-goto)))
  6393. (message "Quit"))))
  6394. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6395. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6396. (defvar org-goto-local-auto-isearch-map) ; defined below
  6397. (defun org-get-location (buf help)
  6398. "Let the user select a location in the Org-mode buffer BUF.
  6399. This function uses a recursive edit. It returns the selected position
  6400. or nil."
  6401. (org-no-popups
  6402. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6403. (isearch-hide-immediately nil)
  6404. (isearch-search-fun-function
  6405. (lambda () 'org-goto-local-search-headings))
  6406. (org-goto-selected-point org-goto-exit-command))
  6407. (save-excursion
  6408. (save-window-excursion
  6409. (delete-other-windows)
  6410. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6411. (org-pop-to-buffer-same-window
  6412. (condition-case nil
  6413. (make-indirect-buffer (current-buffer) "*org-goto*")
  6414. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6415. (with-output-to-temp-buffer "*Org Help*"
  6416. (princ (format help (if org-goto-auto-isearch
  6417. " Just type for auto-isearch."
  6418. " n/p/f/b/u to navigate, q to quit."))))
  6419. (org-fit-window-to-buffer (get-buffer-window "*Org Help*"))
  6420. (setq buffer-read-only nil)
  6421. (let ((org-startup-truncated t)
  6422. (org-startup-folded nil)
  6423. (org-startup-align-all-tables nil))
  6424. (org-mode)
  6425. (org-overview))
  6426. (setq buffer-read-only t)
  6427. (if (and (boundp 'org-goto-start-pos)
  6428. (integer-or-marker-p org-goto-start-pos))
  6429. (let ((org-show-hierarchy-above t)
  6430. (org-show-siblings t)
  6431. (org-show-following-heading t))
  6432. (goto-char org-goto-start-pos)
  6433. (and (outline-invisible-p) (org-show-context)))
  6434. (goto-char (point-min)))
  6435. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6436. (message "Select location and press RET")
  6437. (use-local-map org-goto-map)
  6438. (recursive-edit)))
  6439. (kill-buffer "*org-goto*")
  6440. (cons org-goto-selected-point org-goto-exit-command))))
  6441. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6442. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6443. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6444. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  6445. (defun org-goto-local-search-headings (string bound noerror)
  6446. "Search and make sure that any matches are in headlines."
  6447. (catch 'return
  6448. (while (if isearch-forward
  6449. (search-forward string bound noerror)
  6450. (search-backward string bound noerror))
  6451. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  6452. (and (member :headline context)
  6453. (not (member :tags context))))
  6454. (throw 'return (point))))))
  6455. (defun org-goto-local-auto-isearch ()
  6456. "Start isearch."
  6457. (interactive)
  6458. (goto-char (point-min))
  6459. (let ((keys (this-command-keys)))
  6460. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6461. (isearch-mode t)
  6462. (isearch-process-search-char (string-to-char keys)))))
  6463. (defun org-goto-ret (&optional arg)
  6464. "Finish `org-goto' by going to the new location."
  6465. (interactive "P")
  6466. (setq org-goto-selected-point (point)
  6467. org-goto-exit-command 'return)
  6468. (throw 'exit nil))
  6469. (defun org-goto-left ()
  6470. "Finish `org-goto' by going to the new location."
  6471. (interactive)
  6472. (if (org-at-heading-p)
  6473. (progn
  6474. (beginning-of-line 1)
  6475. (setq org-goto-selected-point (point)
  6476. org-goto-exit-command 'left)
  6477. (throw 'exit nil))
  6478. (error "Not on a heading")))
  6479. (defun org-goto-right ()
  6480. "Finish `org-goto' by going to the new location."
  6481. (interactive)
  6482. (if (org-at-heading-p)
  6483. (progn
  6484. (setq org-goto-selected-point (point)
  6485. org-goto-exit-command 'right)
  6486. (throw 'exit nil))
  6487. (error "Not on a heading")))
  6488. (defun org-goto-quit ()
  6489. "Finish `org-goto' without cursor motion."
  6490. (interactive)
  6491. (setq org-goto-selected-point nil)
  6492. (setq org-goto-exit-command 'quit)
  6493. (throw 'exit nil))
  6494. ;;; Indirect buffer display of subtrees
  6495. (defvar org-indirect-dedicated-frame nil
  6496. "This is the frame being used for indirect tree display.")
  6497. (defvar org-last-indirect-buffer nil)
  6498. (defun org-tree-to-indirect-buffer (&optional arg)
  6499. "Create indirect buffer and narrow it to current subtree.
  6500. With a numerical prefix ARG, go up to this level and then take that tree.
  6501. If ARG is negative, go up that many levels.
  6502. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6503. indirect buffer previously made with this command, to avoid proliferation of
  6504. indirect buffers. However, when you call the command with a \
  6505. \\[universal-argument] prefix, or
  6506. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6507. is kept so that you can work with several indirect buffers at the same time.
  6508. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6509. \\[universal-argument] prefix also
  6510. requests that a new frame be made for the new buffer, so that the dedicated
  6511. frame is not changed."
  6512. (interactive "P")
  6513. (let ((cbuf (current-buffer))
  6514. (cwin (selected-window))
  6515. (pos (point))
  6516. beg end level heading ibuf)
  6517. (save-excursion
  6518. (org-back-to-heading t)
  6519. (when (numberp arg)
  6520. (setq level (org-outline-level))
  6521. (if (< arg 0) (setq arg (+ level arg)))
  6522. (while (> (setq level (org-outline-level)) arg)
  6523. (org-up-heading-safe)))
  6524. (setq beg (point)
  6525. heading (org-get-heading))
  6526. (org-end-of-subtree t t)
  6527. (if (org-at-heading-p) (backward-char 1))
  6528. (setq end (point)))
  6529. (if (and (buffer-live-p org-last-indirect-buffer)
  6530. (not (eq org-indirect-buffer-display 'new-frame))
  6531. (not arg))
  6532. (kill-buffer org-last-indirect-buffer))
  6533. (setq ibuf (org-get-indirect-buffer cbuf)
  6534. org-last-indirect-buffer ibuf)
  6535. (cond
  6536. ((or (eq org-indirect-buffer-display 'new-frame)
  6537. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6538. (select-frame (make-frame))
  6539. (delete-other-windows)
  6540. (org-pop-to-buffer-same-window ibuf)
  6541. (org-set-frame-title heading))
  6542. ((eq org-indirect-buffer-display 'dedicated-frame)
  6543. (raise-frame
  6544. (select-frame (or (and org-indirect-dedicated-frame
  6545. (frame-live-p org-indirect-dedicated-frame)
  6546. org-indirect-dedicated-frame)
  6547. (setq org-indirect-dedicated-frame (make-frame)))))
  6548. (delete-other-windows)
  6549. (org-pop-to-buffer-same-window ibuf)
  6550. (org-set-frame-title (concat "Indirect: " heading)))
  6551. ((eq org-indirect-buffer-display 'current-window)
  6552. (org-pop-to-buffer-same-window ibuf))
  6553. ((eq org-indirect-buffer-display 'other-window)
  6554. (pop-to-buffer ibuf))
  6555. (t (error "Invalid value")))
  6556. (if (featurep 'xemacs)
  6557. (save-excursion (org-mode) (turn-on-font-lock)))
  6558. (narrow-to-region beg end)
  6559. (show-all)
  6560. (goto-char pos)
  6561. (run-hook-with-args 'org-cycle-hook 'all)
  6562. (and (window-live-p cwin) (select-window cwin))))
  6563. (defun org-get-indirect-buffer (&optional buffer)
  6564. (setq buffer (or buffer (current-buffer)))
  6565. (let ((n 1) (base (buffer-name buffer)) bname)
  6566. (while (buffer-live-p
  6567. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  6568. (setq n (1+ n)))
  6569. (condition-case nil
  6570. (make-indirect-buffer buffer bname 'clone)
  6571. (error (make-indirect-buffer buffer bname)))))
  6572. (defun org-set-frame-title (title)
  6573. "Set the title of the current frame to the string TITLE."
  6574. ;; FIXME: how to name a single frame in XEmacs???
  6575. (unless (featurep 'xemacs)
  6576. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6577. ;;;; Structure editing
  6578. ;;; Inserting headlines
  6579. (defun org-previous-line-empty-p (&optional next)
  6580. "Is the previous line a blank line?
  6581. When NEXT is non-nil, check the next line instead."
  6582. (save-excursion
  6583. (and (not (bobp))
  6584. (or (beginning-of-line (if next 2 0)) t)
  6585. (save-match-data
  6586. (looking-at "[ \t]*$")))))
  6587. (defun org-insert-heading (&optional arg invisible-ok)
  6588. "Insert a new heading or item with same depth at point.
  6589. If point is in a plain list and ARG is nil, create a new list item.
  6590. With one universal prefix argument, insert a heading even in lists.
  6591. With two universal prefix arguments, insert the heading at the end
  6592. of the parent subtree.
  6593. If point is at the beginning of a headline, insert a sibling before
  6594. the current headline. If point is not at the beginning, split the line
  6595. and create a new headline with the text in the current line after point
  6596. \(see `org-M-RET-may-split-line' on how to modify this behavior).
  6597. When INVISIBLE-OK is set, stop at invisible headlines when going back.
  6598. This is important for non-interactive uses of the command."
  6599. (interactive "P")
  6600. (if (or (= (buffer-size) 0)
  6601. (and (not (save-excursion
  6602. (and (ignore-errors (org-back-to-heading invisible-ok))
  6603. (org-at-heading-p))))
  6604. (or arg (not (org-in-item-p)))))
  6605. (progn
  6606. (insert
  6607. (if (org-previous-line-empty-p) "" "\n")
  6608. (if (org-in-src-block-p) ",* " "* "))
  6609. (run-hooks 'org-insert-heading-hook))
  6610. (when (or arg
  6611. (not (org-insert-item
  6612. (save-excursion
  6613. (beginning-of-line)
  6614. (looking-at org-list-full-item-re)
  6615. (match-string 3)))))
  6616. (let* ((empty-line-p nil)
  6617. (eops (equal arg '(16))) ; insert at end of parent subtree
  6618. (org-insert-heading-respect-content
  6619. (or (not (null arg)) org-insert-heading-respect-content))
  6620. (level nil)
  6621. (on-heading (org-at-heading-p))
  6622. (head (save-excursion
  6623. (condition-case nil
  6624. (progn
  6625. (org-back-to-heading invisible-ok)
  6626. (when (and (not on-heading)
  6627. (featurep 'org-inlinetask)
  6628. (integerp org-inlinetask-min-level)
  6629. (>= (length (match-string 0))
  6630. org-inlinetask-min-level))
  6631. ;; Find a heading level before the inline task
  6632. (while (and (setq level (org-up-heading-safe))
  6633. (>= level org-inlinetask-min-level)))
  6634. (if (org-at-heading-p)
  6635. (org-back-to-heading invisible-ok)
  6636. (error "This should not happen")))
  6637. (unless (and (save-excursion
  6638. (save-match-data
  6639. (org-backward-heading-same-level 1 invisible-ok))
  6640. (= (point) (match-beginning 0)))
  6641. (not (org-previous-line-empty-p t)))
  6642. (setq empty-line-p (org-previous-line-empty-p)))
  6643. (match-string 0))
  6644. (error "* "))))
  6645. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6646. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6647. pos hide-previous previous-pos)
  6648. (cond
  6649. ;; At the beginning of a heading, open a new line for insertiong
  6650. ((and (bolp) (org-at-heading-p)
  6651. (not eops)
  6652. (or (bobp)
  6653. (save-excursion (backward-char 1) (not (outline-invisible-p)))))
  6654. (open-line (if blank 2 1)))
  6655. (t
  6656. (save-excursion
  6657. (setq previous-pos (point-at-bol))
  6658. (end-of-line)
  6659. (setq hide-previous (outline-invisible-p)))
  6660. (and org-insert-heading-respect-content
  6661. (save-excursion
  6662. (while (outline-invisible-p)
  6663. (org-show-subtree)
  6664. (org-up-heading-safe))))
  6665. (let ((split
  6666. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  6667. (save-excursion
  6668. (let ((p (point)))
  6669. (goto-char (point-at-bol))
  6670. (and (looking-at org-complex-heading-regexp)
  6671. (match-beginning 4)
  6672. (> p (match-beginning 4)))))))
  6673. tags pos)
  6674. (cond
  6675. ;; Insert a new line, possibly at end of parent subtree
  6676. (org-insert-heading-respect-content
  6677. (if (not eops)
  6678. (progn
  6679. (org-end-of-subtree nil t)
  6680. (and (looking-at "^\\*") (backward-char 1))
  6681. (while (and (not (bobp))
  6682. ;; Don't delete spaces in empty headlines
  6683. (not (looking-back org-outline-regexp))
  6684. (member (char-before) '(?\ ?\t ?\n)))
  6685. (backward-delete-char 1)))
  6686. (let ((p (point)))
  6687. (org-up-heading-safe)
  6688. (if (= p (point))
  6689. (goto-char (point-max))
  6690. (org-end-of-subtree nil t))))
  6691. (when (featurep 'org-inlinetask)
  6692. (while (and (not (eobp))
  6693. (looking-at "\\(\\*+\\)[ \t]+")
  6694. (>= (length (match-string 1))
  6695. org-inlinetask-min-level))
  6696. (org-end-of-subtree nil t)))
  6697. (or (bolp) (newline))
  6698. (or (org-previous-line-empty-p)
  6699. (and blank (newline)))
  6700. (if (or empty-line-p eops) (open-line 1)))
  6701. ;; Insert a headling containing text after point
  6702. ((org-at-heading-p)
  6703. (when hide-previous
  6704. (show-children)
  6705. (org-show-entry))
  6706. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
  6707. (setq tags (and (match-end 2) (match-string 2)))
  6708. (and (match-end 1)
  6709. (delete-region (match-beginning 1) (match-end 1)))
  6710. (setq pos (point-at-bol))
  6711. (or split (end-of-line 1))
  6712. (delete-horizontal-space)
  6713. (if (string-match "\\`\\*+\\'"
  6714. (buffer-substring (point-at-bol) (point)))
  6715. (insert " "))
  6716. (newline (if blank 2 1))
  6717. (when tags
  6718. (save-excursion
  6719. (goto-char pos)
  6720. (end-of-line 1)
  6721. (insert " " tags)
  6722. (org-set-tags nil 'align))))
  6723. (t
  6724. (or split (end-of-line 1))
  6725. (newline (if blank 2 1)))))))
  6726. (insert head) (just-one-space)
  6727. (setq pos (point))
  6728. (end-of-line 1)
  6729. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  6730. (when (and org-insert-heading-respect-content hide-previous)
  6731. (save-excursion
  6732. (goto-char previous-pos)
  6733. (hide-subtree)))
  6734. (run-hooks 'org-insert-heading-hook)))))
  6735. (defun org-get-heading (&optional no-tags no-todo)
  6736. "Return the heading of the current entry, without the stars.
  6737. When NO-TAGS is non-nil, don't include tags.
  6738. When NO-TODO is non-nil, don't include TODO keywords."
  6739. (save-excursion
  6740. (org-back-to-heading t)
  6741. (cond
  6742. ((and no-tags no-todo)
  6743. (looking-at org-complex-heading-regexp)
  6744. (match-string 4))
  6745. (no-tags
  6746. (looking-at (concat org-outline-regexp
  6747. "\\(.*?\\)"
  6748. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  6749. (match-string 1))
  6750. (no-todo
  6751. (looking-at org-todo-line-regexp)
  6752. (match-string 3))
  6753. (t (looking-at org-heading-regexp)
  6754. (match-string 2)))))
  6755. (defvar orgstruct-mode) ; defined below
  6756. (defun org-heading-components ()
  6757. "Return the components of the current heading.
  6758. This is a list with the following elements:
  6759. - the level as an integer
  6760. - the reduced level, different if `org-odd-levels-only' is set.
  6761. - the TODO keyword, or nil
  6762. - the priority character, like ?A, or nil if no priority is given
  6763. - the headline text itself, or the tags string if no headline text
  6764. - the tags string, or nil."
  6765. (save-excursion
  6766. (org-back-to-heading t)
  6767. (if (let (case-fold-search)
  6768. (looking-at
  6769. (if orgstruct-mode
  6770. org-heading-regexp
  6771. org-complex-heading-regexp)))
  6772. (if orgstruct-mode
  6773. (list (length (match-string 1))
  6774. (org-reduced-level (length (match-string 1)))
  6775. nil
  6776. nil
  6777. (match-string 2)
  6778. nil)
  6779. (list (length (match-string 1))
  6780. (org-reduced-level (length (match-string 1)))
  6781. (org-match-string-no-properties 2)
  6782. (and (match-end 3) (aref (match-string 3) 2))
  6783. (org-match-string-no-properties 4)
  6784. (org-match-string-no-properties 5))))))
  6785. (defun org-get-entry ()
  6786. "Get the entry text, after heading, entire subtree."
  6787. (save-excursion
  6788. (org-back-to-heading t)
  6789. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  6790. (defun org-insert-heading-after-current ()
  6791. "Insert a new heading with same level as current, after current subtree."
  6792. (interactive)
  6793. (org-back-to-heading)
  6794. (org-insert-heading)
  6795. (org-move-subtree-down)
  6796. (end-of-line 1))
  6797. (defun org-insert-heading-respect-content (&optional arg invisible-ok)
  6798. "Insert heading with `org-insert-heading-respect-content' set to t."
  6799. (interactive "P")
  6800. (let ((org-insert-heading-respect-content t))
  6801. (org-insert-heading arg invisible-ok)))
  6802. (defun org-insert-todo-heading-respect-content (&optional force-state)
  6803. "Insert TODO heading with `org-insert-heading-respect-content' set to t."
  6804. (interactive "P")
  6805. (let ((org-insert-heading-respect-content t))
  6806. (org-insert-todo-heading force-state t)))
  6807. (defun org-insert-todo-heading (arg &optional force-heading)
  6808. "Insert a new heading with the same level and TODO state as current heading.
  6809. If the heading has no TODO state, or if the state is DONE, use the first
  6810. state (TODO by default). Also one prefix arg, force first state. With two
  6811. prefix args, force inserting at the end of the parent subtree."
  6812. (interactive "P")
  6813. (when (or force-heading (not (org-insert-item 'checkbox)))
  6814. (org-insert-heading (or (and (equal arg '(16)) '(16))
  6815. force-heading))
  6816. (save-excursion
  6817. (org-back-to-heading)
  6818. (outline-previous-heading)
  6819. (looking-at org-todo-line-regexp))
  6820. (let*
  6821. ((new-mark-x
  6822. (if (or arg
  6823. (not (match-beginning 2))
  6824. (member (match-string 2) org-done-keywords))
  6825. (car org-todo-keywords-1)
  6826. (match-string 2)))
  6827. (new-mark
  6828. (or
  6829. (run-hook-with-args-until-success
  6830. 'org-todo-get-default-hook new-mark-x nil)
  6831. new-mark-x)))
  6832. (beginning-of-line 1)
  6833. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  6834. (if org-treat-insert-todo-heading-as-state-change
  6835. (org-todo new-mark)
  6836. (insert new-mark " "))))
  6837. (when org-provide-todo-statistics
  6838. (org-update-parent-todo-statistics))))
  6839. (defun org-insert-subheading (arg)
  6840. "Insert a new subheading and demote it.
  6841. Works for outline headings and for plain lists alike."
  6842. (interactive "P")
  6843. (org-insert-heading arg)
  6844. (cond
  6845. ((org-at-heading-p) (org-do-demote))
  6846. ((org-at-item-p) (org-indent-item))))
  6847. (defun org-insert-todo-subheading (arg)
  6848. "Insert a new subheading with TODO keyword or checkbox and demote it.
  6849. Works for outline headings and for plain lists alike."
  6850. (interactive "P")
  6851. (org-insert-todo-heading arg)
  6852. (cond
  6853. ((org-at-heading-p) (org-do-demote))
  6854. ((org-at-item-p) (org-indent-item))))
  6855. ;;; Promotion and Demotion
  6856. (defvar org-after-demote-entry-hook nil
  6857. "Hook run after an entry has been demoted.
  6858. The cursor will be at the beginning of the entry.
  6859. When a subtree is being demoted, the hook will be called for each node.")
  6860. (defvar org-after-promote-entry-hook nil
  6861. "Hook run after an entry has been promoted.
  6862. The cursor will be at the beginning of the entry.
  6863. When a subtree is being promoted, the hook will be called for each node.")
  6864. (defun org-promote-subtree ()
  6865. "Promote the entire subtree.
  6866. See also `org-promote'."
  6867. (interactive)
  6868. (save-excursion
  6869. (org-with-limited-levels (org-map-tree 'org-promote)))
  6870. (org-fix-position-after-promote))
  6871. (defun org-demote-subtree ()
  6872. "Demote the entire subtree. See `org-demote'.
  6873. See also `org-promote'."
  6874. (interactive)
  6875. (save-excursion
  6876. (org-with-limited-levels (org-map-tree 'org-demote)))
  6877. (org-fix-position-after-promote))
  6878. (defun org-do-promote ()
  6879. "Promote the current heading higher up the tree.
  6880. If the region is active in `transient-mark-mode', promote all headings
  6881. in the region."
  6882. (interactive)
  6883. (save-excursion
  6884. (if (org-region-active-p)
  6885. (org-map-region 'org-promote (region-beginning) (region-end))
  6886. (org-promote)))
  6887. (org-fix-position-after-promote))
  6888. (defun org-do-demote ()
  6889. "Demote the current heading lower down the tree.
  6890. If the region is active in `transient-mark-mode', demote all headings
  6891. in the region."
  6892. (interactive)
  6893. (save-excursion
  6894. (if (org-region-active-p)
  6895. (org-map-region 'org-demote (region-beginning) (region-end))
  6896. (org-demote)))
  6897. (org-fix-position-after-promote))
  6898. (defun org-fix-position-after-promote ()
  6899. "Make sure that after pro/demotion cursor position is right."
  6900. (let ((pos (point)))
  6901. (when (save-excursion
  6902. (beginning-of-line 1)
  6903. (looking-at org-todo-line-regexp)
  6904. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  6905. (cond ((eobp) (insert " "))
  6906. ((eolp) (insert " "))
  6907. ((equal (char-after) ?\ ) (forward-char 1))))))
  6908. (defun org-current-level ()
  6909. "Return the level of the current entry, or nil if before the first headline.
  6910. The level is the number of stars at the beginning of the headline."
  6911. (save-excursion
  6912. (org-with-limited-levels
  6913. (if (ignore-errors (org-back-to-heading t))
  6914. (funcall outline-level)))))
  6915. (defun org-get-previous-line-level ()
  6916. "Return the outline depth of the last headline before the current line.
  6917. Returns 0 for the first headline in the buffer, and nil if before the
  6918. first headline."
  6919. (let ((current-level (org-current-level))
  6920. (prev-level (when (> (line-number-at-pos) 1)
  6921. (save-excursion
  6922. (beginning-of-line 0)
  6923. (org-current-level)))))
  6924. (cond ((null current-level) nil) ; Before first headline
  6925. ((null prev-level) 0) ; At first headline
  6926. (prev-level))))
  6927. (defun org-reduced-level (l)
  6928. "Compute the effective level of a heading.
  6929. This takes into account the setting of `org-odd-levels-only'."
  6930. (cond
  6931. ((zerop l) 0)
  6932. (org-odd-levels-only (1+ (floor (/ l 2))))
  6933. (t l)))
  6934. (defun org-level-increment ()
  6935. "Return the number of stars that will be added or removed at a
  6936. time to headlines when structure editing, based on the value of
  6937. `org-odd-levels-only'."
  6938. (if org-odd-levels-only 2 1))
  6939. (defun org-get-valid-level (level &optional change)
  6940. "Rectify a level change under the influence of `org-odd-levels-only'
  6941. LEVEL is a current level, CHANGE is by how much the level should be
  6942. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  6943. even level numbers will become the next higher odd number."
  6944. (if org-odd-levels-only
  6945. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  6946. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  6947. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  6948. (max 1 (+ level (or change 0)))))
  6949. (if (boundp 'define-obsolete-function-alias)
  6950. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  6951. (define-obsolete-function-alias 'org-get-legal-level
  6952. 'org-get-valid-level)
  6953. (define-obsolete-function-alias 'org-get-legal-level
  6954. 'org-get-valid-level "23.1")))
  6955. (defvar org-called-with-limited-levels nil) ;; Dynamically bound in
  6956. ;; ̀org-with-limited-levels'
  6957. (defun org-promote ()
  6958. "Promote the current heading higher up the tree.
  6959. If the region is active in `transient-mark-mode', promote all headings
  6960. in the region."
  6961. (org-back-to-heading t)
  6962. (let* ((level (save-match-data (funcall outline-level)))
  6963. (after-change-functions (remove 'flyspell-after-change-function
  6964. after-change-functions))
  6965. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  6966. (diff (abs (- level (length up-head) -1))))
  6967. (cond ((and (= level 1) org-called-with-limited-levels
  6968. org-allow-promoting-top-level-subtree)
  6969. (replace-match "# " nil t))
  6970. ((= level 1)
  6971. (error "Cannot promote to level 0. UNDO to recover if necessary"))
  6972. (t (replace-match up-head nil t)))
  6973. ;; Fixup tag positioning
  6974. (unless (= level 1)
  6975. (and org-auto-align-tags (org-set-tags nil t))
  6976. (if org-adapt-indentation (org-fixup-indentation (- diff))))
  6977. (run-hooks 'org-after-promote-entry-hook)))
  6978. (defun org-demote ()
  6979. "Demote the current heading lower down the tree.
  6980. If the region is active in `transient-mark-mode', demote all headings
  6981. in the region."
  6982. (org-back-to-heading t)
  6983. (let* ((level (save-match-data (funcall outline-level)))
  6984. (after-change-functions (remove 'flyspell-after-change-function
  6985. after-change-functions))
  6986. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  6987. (diff (abs (- level (length down-head) -1))))
  6988. (replace-match down-head nil t)
  6989. ;; Fixup tag positioning
  6990. (and org-auto-align-tags (org-set-tags nil t))
  6991. (if org-adapt-indentation (org-fixup-indentation diff))
  6992. (run-hooks 'org-after-demote-entry-hook)))
  6993. (defun org-cycle-level ()
  6994. "Cycle the level of an empty headline through possible states.
  6995. This goes first to child, then to parent, level, then up the hierarchy.
  6996. After top level, it switches back to sibling level."
  6997. (interactive)
  6998. (let ((org-adapt-indentation nil))
  6999. (when (org-point-at-end-of-empty-headline)
  7000. (setq this-command 'org-cycle-level) ; Only needed for caching
  7001. (let ((cur-level (org-current-level))
  7002. (prev-level (org-get-previous-line-level)))
  7003. (cond
  7004. ;; If first headline in file, promote to top-level.
  7005. ((= prev-level 0)
  7006. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7007. do (org-do-promote)))
  7008. ;; If same level as prev, demote one.
  7009. ((= prev-level cur-level)
  7010. (org-do-demote))
  7011. ;; If parent is top-level, promote to top level if not already.
  7012. ((= prev-level 1)
  7013. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7014. do (org-do-promote)))
  7015. ;; If top-level, return to prev-level.
  7016. ((= cur-level 1)
  7017. (loop repeat (/ (- prev-level 1) (org-level-increment))
  7018. do (org-do-demote)))
  7019. ;; If less than prev-level, promote one.
  7020. ((< cur-level prev-level)
  7021. (org-do-promote))
  7022. ;; If deeper than prev-level, promote until higher than
  7023. ;; prev-level.
  7024. ((> cur-level prev-level)
  7025. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  7026. do (org-do-promote))))
  7027. t))))
  7028. (defun org-map-tree (fun)
  7029. "Call FUN for every heading underneath the current one."
  7030. (org-back-to-heading)
  7031. (let ((level (funcall outline-level)))
  7032. (save-excursion
  7033. (funcall fun)
  7034. (while (and (progn
  7035. (outline-next-heading)
  7036. (> (funcall outline-level) level))
  7037. (not (eobp)))
  7038. (funcall fun)))))
  7039. (defun org-map-region (fun beg end)
  7040. "Call FUN for every heading between BEG and END."
  7041. (let ((org-ignore-region t))
  7042. (save-excursion
  7043. (setq end (copy-marker end))
  7044. (goto-char beg)
  7045. (if (and (re-search-forward org-outline-regexp-bol nil t)
  7046. (< (point) end))
  7047. (funcall fun))
  7048. (while (and (progn
  7049. (outline-next-heading)
  7050. (< (point) end))
  7051. (not (eobp)))
  7052. (funcall fun)))))
  7053. (defvar org-property-end-re) ; silence byte-compiler
  7054. (defun org-fixup-indentation (diff)
  7055. "Change the indentation in the current entry by DIFF.
  7056. However, if any line in the current entry has no indentation, or if it
  7057. would end up with no indentation after the change, nothing at all is done."
  7058. (save-excursion
  7059. (let ((end (save-excursion (outline-next-heading)
  7060. (point-marker)))
  7061. (prohibit (if (> diff 0)
  7062. "^\\S-"
  7063. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  7064. col)
  7065. (unless (save-excursion (end-of-line 1)
  7066. (re-search-forward prohibit end t))
  7067. (while (and (< (point) end)
  7068. (re-search-forward "^[ \t]+" end t))
  7069. (goto-char (match-end 0))
  7070. (setq col (current-column))
  7071. (if (< diff 0) (replace-match ""))
  7072. (org-indent-to-column (+ diff col))))
  7073. (move-marker end nil))))
  7074. (defun org-convert-to-odd-levels ()
  7075. "Convert an org-mode file with all levels allowed to one with odd levels.
  7076. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  7077. level 5 etc."
  7078. (interactive)
  7079. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  7080. (let ((outline-level 'org-outline-level)
  7081. (org-odd-levels-only nil) n)
  7082. (save-excursion
  7083. (goto-char (point-min))
  7084. (while (re-search-forward "^\\*\\*+ " nil t)
  7085. (setq n (- (length (match-string 0)) 2))
  7086. (while (>= (setq n (1- n)) 0)
  7087. (org-demote))
  7088. (end-of-line 1))))))
  7089. (defun org-convert-to-oddeven-levels ()
  7090. "Convert an org-mode file with only odd levels to one with odd/even levels.
  7091. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  7092. file contains a section with an even level, conversion would
  7093. destroy the structure of the file. An error is signaled in this
  7094. case."
  7095. (interactive)
  7096. (goto-char (point-min))
  7097. ;; First check if there are no even levels
  7098. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  7099. (org-show-context t)
  7100. (error "Not all levels are odd in this file. Conversion not possible"))
  7101. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  7102. (let ((outline-regexp org-outline-regexp)
  7103. (outline-level 'org-outline-level)
  7104. (org-odd-levels-only nil) n)
  7105. (save-excursion
  7106. (goto-char (point-min))
  7107. (while (re-search-forward "^\\*\\*+ " nil t)
  7108. (setq n (/ (1- (length (match-string 0))) 2))
  7109. (while (>= (setq n (1- n)) 0)
  7110. (org-promote))
  7111. (end-of-line 1))))))
  7112. (defun org-tr-level (n)
  7113. "Make N odd if required."
  7114. (if org-odd-levels-only (1+ (/ n 2)) n))
  7115. ;;; Vertical tree motion, cutting and pasting of subtrees
  7116. (defun org-move-subtree-up (&optional arg)
  7117. "Move the current subtree up past ARG headlines of the same level."
  7118. (interactive "p")
  7119. (org-move-subtree-down (- (prefix-numeric-value arg))))
  7120. (defun org-move-subtree-down (&optional arg)
  7121. "Move the current subtree down past ARG headlines of the same level."
  7122. (interactive "p")
  7123. (setq arg (prefix-numeric-value arg))
  7124. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  7125. 'org-get-last-sibling))
  7126. (ins-point (make-marker))
  7127. (cnt (abs arg))
  7128. (col (current-column))
  7129. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  7130. ;; Select the tree
  7131. (org-back-to-heading)
  7132. (setq beg0 (point))
  7133. (save-excursion
  7134. (setq ne-beg (org-back-over-empty-lines))
  7135. (setq beg (point)))
  7136. (save-match-data
  7137. (save-excursion (outline-end-of-heading)
  7138. (setq folded (outline-invisible-p)))
  7139. (outline-end-of-subtree))
  7140. (outline-next-heading)
  7141. (setq ne-end (org-back-over-empty-lines))
  7142. (setq end (point))
  7143. (goto-char beg0)
  7144. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  7145. ;; include less whitespace
  7146. (save-excursion
  7147. (goto-char beg)
  7148. (forward-line (- ne-beg ne-end))
  7149. (setq beg (point))))
  7150. ;; Find insertion point, with error handling
  7151. (while (> cnt 0)
  7152. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  7153. (progn (goto-char beg0)
  7154. (user-error "Cannot move past superior level or buffer limit")))
  7155. (setq cnt (1- cnt)))
  7156. (if (> arg 0)
  7157. ;; Moving forward - still need to move over subtree
  7158. (progn (org-end-of-subtree t t)
  7159. (save-excursion
  7160. (org-back-over-empty-lines)
  7161. (or (bolp) (newline)))))
  7162. (setq ne-ins (org-back-over-empty-lines))
  7163. (move-marker ins-point (point))
  7164. (setq txt (buffer-substring beg end))
  7165. (org-save-markers-in-region beg end)
  7166. (delete-region beg end)
  7167. (org-remove-empty-overlays-at beg)
  7168. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  7169. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  7170. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  7171. (let ((bbb (point)))
  7172. (insert-before-markers txt)
  7173. (org-reinstall-markers-in-region bbb)
  7174. (move-marker ins-point bbb))
  7175. (or (bolp) (insert "\n"))
  7176. (setq ins-end (point))
  7177. (goto-char ins-point)
  7178. (org-skip-whitespace)
  7179. (when (and (< arg 0)
  7180. (org-first-sibling-p)
  7181. (> ne-ins ne-beg))
  7182. ;; Move whitespace back to beginning
  7183. (save-excursion
  7184. (goto-char ins-end)
  7185. (let ((kill-whole-line t))
  7186. (kill-line (- ne-ins ne-beg)) (point)))
  7187. (insert (make-string (- ne-ins ne-beg) ?\n)))
  7188. (move-marker ins-point nil)
  7189. (if folded
  7190. (hide-subtree)
  7191. (org-show-entry)
  7192. (show-children)
  7193. (org-cycle-hide-drawers 'children))
  7194. (org-clean-visibility-after-subtree-move)
  7195. ;; move back to the initial column we were at
  7196. (move-to-column col)))
  7197. (defvar org-subtree-clip ""
  7198. "Clipboard for cut and paste of subtrees.
  7199. This is actually only a copy of the kill, because we use the normal kill
  7200. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  7201. (defvar org-subtree-clip-folded nil
  7202. "Was the last copied subtree folded?
  7203. This is used to fold the tree back after pasting.")
  7204. (defun org-cut-subtree (&optional n)
  7205. "Cut the current subtree into the clipboard.
  7206. With prefix arg N, cut this many sequential subtrees.
  7207. This is a short-hand for marking the subtree and then cutting it."
  7208. (interactive "p")
  7209. (org-copy-subtree n 'cut))
  7210. (defun org-copy-subtree (&optional n cut force-store-markers nosubtrees)
  7211. "Cut the current subtree into the clipboard.
  7212. With prefix arg N, cut this many sequential subtrees.
  7213. This is a short-hand for marking the subtree and then copying it.
  7214. If CUT is non-nil, actually cut the subtree.
  7215. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  7216. of some markers in the region, even if CUT is non-nil. This is
  7217. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  7218. (interactive "p")
  7219. (let (beg end folded (beg0 (point)))
  7220. (if (org-called-interactively-p 'any)
  7221. (org-back-to-heading nil) ; take what looks like a subtree
  7222. (org-back-to-heading t)) ; take what is really there
  7223. (setq beg (point))
  7224. (skip-chars-forward " \t\r\n")
  7225. (save-match-data
  7226. (if nosubtrees
  7227. (outline-next-heading)
  7228. (save-excursion (outline-end-of-heading)
  7229. (setq folded (outline-invisible-p)))
  7230. (condition-case nil
  7231. (org-forward-heading-same-level (1- n) t)
  7232. (error nil))
  7233. (org-end-of-subtree t t)))
  7234. (setq end (point))
  7235. (goto-char beg0)
  7236. (when (> end beg)
  7237. (setq org-subtree-clip-folded folded)
  7238. (when (or cut force-store-markers)
  7239. (org-save-markers-in-region beg end))
  7240. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  7241. (setq org-subtree-clip (current-kill 0))
  7242. (message "%s: Subtree(s) with %d characters"
  7243. (if cut "Cut" "Copied")
  7244. (length org-subtree-clip)))))
  7245. (defun org-paste-subtree (&optional level tree for-yank)
  7246. "Paste the clipboard as a subtree, with modification of headline level.
  7247. The entire subtree is promoted or demoted in order to match a new headline
  7248. level.
  7249. If the cursor is at the beginning of a headline, the same level as
  7250. that headline is used to paste the tree.
  7251. If not, the new level is derived from the *visible* headings
  7252. before and after the insertion point, and taken to be the inferior headline
  7253. level of the two. So if the previous visible heading is level 3 and the
  7254. next is level 4 (or vice versa), level 4 will be used for insertion.
  7255. This makes sure that the subtree remains an independent subtree and does
  7256. not swallow low level entries.
  7257. You can also force a different level, either by using a numeric prefix
  7258. argument, or by inserting the heading marker by hand. For example, if the
  7259. cursor is after \"*****\", then the tree will be shifted to level 5.
  7260. If optional TREE is given, use this text instead of the kill ring.
  7261. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  7262. move back over whitespace before inserting, and move point to the end of
  7263. the inserted text when done."
  7264. (interactive "P")
  7265. (setq tree (or tree (and kill-ring (current-kill 0))))
  7266. (unless (org-kill-is-subtree-p tree)
  7267. (error "%s"
  7268. (substitute-command-keys
  7269. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  7270. (org-with-limited-levels
  7271. (let* ((visp (not (outline-invisible-p)))
  7272. (txt tree)
  7273. (^re_ "\\(\\*+\\)[ \t]*")
  7274. (old-level (if (string-match org-outline-regexp-bol txt)
  7275. (- (match-end 0) (match-beginning 0) 1)
  7276. -1))
  7277. (force-level (cond (level (prefix-numeric-value level))
  7278. ((and (looking-at "[ \t]*$")
  7279. (string-match
  7280. "^\\*+$" (buffer-substring
  7281. (point-at-bol) (point))))
  7282. (- (match-end 1) (match-beginning 1)))
  7283. ((and (bolp)
  7284. (looking-at org-outline-regexp))
  7285. (- (match-end 0) (point) 1))))
  7286. (previous-level (save-excursion
  7287. (condition-case nil
  7288. (progn
  7289. (outline-previous-visible-heading 1)
  7290. (if (looking-at ^re_)
  7291. (- (match-end 0) (match-beginning 0) 1)
  7292. 1))
  7293. (error 1))))
  7294. (next-level (save-excursion
  7295. (condition-case nil
  7296. (progn
  7297. (or (looking-at org-outline-regexp)
  7298. (outline-next-visible-heading 1))
  7299. (if (looking-at ^re_)
  7300. (- (match-end 0) (match-beginning 0) 1)
  7301. 1))
  7302. (error 1))))
  7303. (new-level (or force-level (max previous-level next-level)))
  7304. (shift (if (or (= old-level -1)
  7305. (= new-level -1)
  7306. (= old-level new-level))
  7307. 0
  7308. (- new-level old-level)))
  7309. (delta (if (> shift 0) -1 1))
  7310. (func (if (> shift 0) 'org-demote 'org-promote))
  7311. (org-odd-levels-only nil)
  7312. beg end newend)
  7313. ;; Remove the forced level indicator
  7314. (if force-level
  7315. (delete-region (point-at-bol) (point)))
  7316. ;; Paste
  7317. (beginning-of-line (if (bolp) 1 2))
  7318. (setq beg (point))
  7319. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  7320. (insert-before-markers txt)
  7321. (unless (string-match "\n\\'" txt) (insert "\n"))
  7322. (setq newend (point))
  7323. (org-reinstall-markers-in-region beg)
  7324. (setq end (point))
  7325. (goto-char beg)
  7326. (skip-chars-forward " \t\n\r")
  7327. (setq beg (point))
  7328. (if (and (outline-invisible-p) visp)
  7329. (save-excursion (outline-show-heading)))
  7330. ;; Shift if necessary
  7331. (unless (= shift 0)
  7332. (save-restriction
  7333. (narrow-to-region beg end)
  7334. (while (not (= shift 0))
  7335. (org-map-region func (point-min) (point-max))
  7336. (setq shift (+ delta shift)))
  7337. (goto-char (point-min))
  7338. (setq newend (point-max))))
  7339. (when (or (org-called-interactively-p 'interactive) for-yank)
  7340. (message "Clipboard pasted as level %d subtree" new-level))
  7341. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  7342. kill-ring
  7343. (eq org-subtree-clip (current-kill 0))
  7344. org-subtree-clip-folded)
  7345. ;; The tree was folded before it was killed/copied
  7346. (hide-subtree))
  7347. (and for-yank (goto-char newend)))))
  7348. (defun org-kill-is-subtree-p (&optional txt)
  7349. "Check if the current kill is an outline subtree, or a set of trees.
  7350. Returns nil if kill does not start with a headline, or if the first
  7351. headline level is not the largest headline level in the tree.
  7352. So this will actually accept several entries of equal levels as well,
  7353. which is OK for `org-paste-subtree'.
  7354. If optional TXT is given, check this string instead of the current kill."
  7355. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  7356. (re (org-get-limited-outline-regexp))
  7357. (^re (concat "^" re))
  7358. (start-level (and kill
  7359. (string-match
  7360. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7361. kill)
  7362. (- (match-end 2) (match-beginning 2) 1)))
  7363. (start (1+ (or (match-beginning 2) -1))))
  7364. (if (not start-level)
  7365. (progn
  7366. nil) ;; does not even start with a heading
  7367. (catch 'exit
  7368. (while (setq start (string-match ^re kill (1+ start)))
  7369. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7370. (throw 'exit nil)))
  7371. t))))
  7372. (defvar org-markers-to-move nil
  7373. "Markers that should be moved with a cut-and-paste operation.
  7374. Those markers are stored together with their positions relative to
  7375. the start of the region.")
  7376. (defun org-save-markers-in-region (beg end)
  7377. "Check markers in region.
  7378. If these markers are between BEG and END, record their position relative
  7379. to BEG, so that after moving the block of text, we can put the markers back
  7380. into place.
  7381. This function gets called just before an entry or tree gets cut from the
  7382. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7383. called immediately, to move the markers with the entries."
  7384. (setq org-markers-to-move nil)
  7385. (when (featurep 'org-clock)
  7386. (org-clock-save-markers-for-cut-and-paste beg end))
  7387. (when (featurep 'org-agenda)
  7388. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7389. (defun org-check-and-save-marker (marker beg end)
  7390. "Check if MARKER is between BEG and END.
  7391. If yes, remember the marker and the distance to BEG."
  7392. (when (and (marker-buffer marker)
  7393. (equal (marker-buffer marker) (current-buffer)))
  7394. (if (and (>= marker beg) (< marker end))
  7395. (push (cons marker (- marker beg)) org-markers-to-move))))
  7396. (defun org-reinstall-markers-in-region (beg)
  7397. "Move all remembered markers to their position relative to BEG."
  7398. (mapc (lambda (x)
  7399. (move-marker (car x) (+ beg (cdr x))))
  7400. org-markers-to-move)
  7401. (setq org-markers-to-move nil))
  7402. (defun org-narrow-to-subtree ()
  7403. "Narrow buffer to the current subtree."
  7404. (interactive)
  7405. (save-excursion
  7406. (save-match-data
  7407. (org-with-limited-levels
  7408. (narrow-to-region
  7409. (progn (org-back-to-heading t) (point))
  7410. (progn (org-end-of-subtree t t)
  7411. (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7412. (point)))))))
  7413. (defun org-narrow-to-block ()
  7414. "Narrow buffer to the current block."
  7415. (interactive)
  7416. (let* ((case-fold-search t)
  7417. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7418. "^[ \t]*#\\+end_.*")))
  7419. (if blockp
  7420. (narrow-to-region (car blockp) (cdr blockp))
  7421. (error "Not in a block"))))
  7422. (eval-when-compile
  7423. (defvar org-property-drawer-re))
  7424. (defvar org-property-start-re) ;; defined below
  7425. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7426. "Clone the task (subtree) at point N times.
  7427. The clones will be inserted as siblings.
  7428. In interactive use, the user will be prompted for the number of
  7429. clones to be produced, and for a time SHIFT, which may be a
  7430. repeater as used in time stamps, for example `+3d'.
  7431. When a valid repeater is given and the entry contains any time
  7432. stamps, the clones will become a sequence in time, with time
  7433. stamps in the subtree shifted for each clone produced. If SHIFT
  7434. is nil or the empty string, time stamps will be left alone. The
  7435. ID property of the original subtree is removed.
  7436. If the original subtree did contain time stamps with a repeater,
  7437. the following will happen:
  7438. - the repeater will be removed in each clone
  7439. - an additional clone will be produced, with the current, unshifted
  7440. date(s) in the entry.
  7441. - the original entry will be placed *after* all the clones, with
  7442. repeater intact.
  7443. - the start days in the repeater in the original entry will be shifted
  7444. to past the last clone.
  7445. In this way you can spell out a number of instances of a repeating task,
  7446. and still retain the repeater to cover future instances of the task."
  7447. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  7448. (let (beg end template task idprop
  7449. shift-n shift-what doshift nmin nmax (n-no-remove -1)
  7450. (drawer-re org-drawer-regexp))
  7451. (if (not (and (integerp n) (> n 0)))
  7452. (error "Invalid number of replications %s" n))
  7453. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7454. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
  7455. shift)))
  7456. (error "Invalid shift specification %s" shift))
  7457. (when doshift
  7458. (setq shift-n (string-to-number (match-string 1 shift))
  7459. shift-what (cdr (assoc (match-string 2 shift)
  7460. '(("d" . day) ("w" . week)
  7461. ("m" . month) ("y" . year))))))
  7462. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7463. (setq nmin 1 nmax n)
  7464. (org-back-to-heading t)
  7465. (setq beg (point))
  7466. (setq idprop (org-entry-get nil "ID"))
  7467. (org-end-of-subtree t t)
  7468. (or (bolp) (insert "\n"))
  7469. (setq end (point))
  7470. (setq template (buffer-substring beg end))
  7471. (when (and doshift
  7472. (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
  7473. (delete-region beg end)
  7474. (setq end beg)
  7475. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7476. (goto-char end)
  7477. (loop for n from nmin to nmax do
  7478. ;; prepare clone
  7479. (with-temp-buffer
  7480. (insert template)
  7481. (org-mode)
  7482. (goto-char (point-min))
  7483. (org-show-subtree)
  7484. (and idprop (if org-clone-delete-id
  7485. (org-entry-delete nil "ID")
  7486. (org-id-get-create t)))
  7487. (unless (= n 0)
  7488. (while (re-search-forward "^[ \t]*CLOCK:.*$" nil t)
  7489. (kill-whole-line))
  7490. (goto-char (point-min))
  7491. (while (re-search-forward drawer-re nil t)
  7492. (mapc (lambda (d)
  7493. (org-remove-empty-drawer-at d (point))) org-drawers)))
  7494. (goto-char (point-min))
  7495. (when doshift
  7496. (while (re-search-forward org-ts-regexp-both nil t)
  7497. (org-timestamp-change (* n shift-n) shift-what))
  7498. (unless (= n n-no-remove)
  7499. (goto-char (point-min))
  7500. (while (re-search-forward org-ts-regexp nil t)
  7501. (save-excursion
  7502. (goto-char (match-beginning 0))
  7503. (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
  7504. (delete-region (match-beginning 1) (match-end 1)))))))
  7505. (setq task (buffer-string)))
  7506. (insert task))
  7507. (goto-char beg)))
  7508. ;;; Outline Sorting
  7509. (defun org-sort (with-case)
  7510. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7511. Optional argument WITH-CASE means sort case-sensitively."
  7512. (interactive "P")
  7513. (cond
  7514. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7515. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7516. (t
  7517. (org-call-with-arg 'org-sort-entries with-case))))
  7518. (defun org-sort-remove-invisible (s)
  7519. "Remove invisible links from string S."
  7520. (remove-text-properties 0 (length s) org-rm-props s)
  7521. (while (string-match org-bracket-link-regexp s)
  7522. (setq s (replace-match (if (match-end 2)
  7523. (match-string 3 s)
  7524. (match-string 1 s)) t t s)))
  7525. (let ((st (format " %s " s)))
  7526. (while (string-match org-emph-re st)
  7527. (setq st (replace-match (format " %s " (match-string 4 st)) t t st)))
  7528. (setq s (substring st 1 -1)))
  7529. s)
  7530. (defvar org-priority-regexp) ; defined later in the file
  7531. (defvar org-after-sorting-entries-or-items-hook nil
  7532. "Hook that is run after a bunch of entries or items have been sorted.
  7533. When children are sorted, the cursor is in the parent line when this
  7534. hook gets called. When a region or a plain list is sorted, the cursor
  7535. will be in the first entry of the sorted region/list.")
  7536. (defun org-sort-entries
  7537. (&optional with-case sorting-type getkey-func compare-func property)
  7538. "Sort entries on a certain level of an outline tree.
  7539. If there is an active region, the entries in the region are sorted.
  7540. Else, if the cursor is before the first entry, sort the top-level items.
  7541. Else, the children of the entry at point are sorted.
  7542. Sorting can be alphabetically, numerically, by date/time as given by
  7543. a time stamp, by a property, by priority order, or by a custom function.
  7544. The command prompts for the sorting type unless it has been given to the
  7545. function through the SORTING-TYPE argument, which needs to be a character,
  7546. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?o ?O ?r ?R ?f ?F). Here is the
  7547. precise meaning of each character:
  7548. n Numerically, by converting the beginning of the entry/item to a number.
  7549. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7550. o By order of TODO keywords.
  7551. t By date/time, either the first active time stamp in the entry, or, if
  7552. none exist, by the first inactive one.
  7553. s By the scheduled date/time.
  7554. d By deadline date/time.
  7555. c By creation time, which is assumed to be the first inactive time stamp
  7556. at the beginning of a line.
  7557. p By priority according to the cookie.
  7558. r By the value of a property.
  7559. Capital letters will reverse the sort order.
  7560. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7561. called with point at the beginning of the record. It must return either
  7562. a string or a number that should serve as the sorting key for that record.
  7563. Comparing entries ignores case by default. However, with an optional argument
  7564. WITH-CASE, the sorting considers case as well.
  7565. Sorting is done against the visible part of the headlines, it ignores hidden
  7566. links."
  7567. (interactive "P")
  7568. (let ((case-func (if with-case 'identity 'downcase))
  7569. (cmstr
  7570. ;; The clock marker is lost when using `sort-subr', let's
  7571. ;; store the clocking string.
  7572. (when (equal (marker-buffer org-clock-marker) (current-buffer))
  7573. (save-excursion
  7574. (goto-char org-clock-marker)
  7575. (looking-back "^.*") (match-string-no-properties 0))))
  7576. start beg end stars re re2
  7577. txt what tmp)
  7578. ;; Find beginning and end of region to sort
  7579. (cond
  7580. ((org-region-active-p)
  7581. ;; we will sort the region
  7582. (setq end (region-end)
  7583. what "region")
  7584. (goto-char (region-beginning))
  7585. (if (not (org-at-heading-p)) (outline-next-heading))
  7586. (setq start (point)))
  7587. ((or (org-at-heading-p)
  7588. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  7589. ;; we will sort the children of the current headline
  7590. (org-back-to-heading)
  7591. (setq start (point)
  7592. end (progn (org-end-of-subtree t t)
  7593. (or (bolp) (insert "\n"))
  7594. (org-back-over-empty-lines)
  7595. (point))
  7596. what "children")
  7597. (goto-char start)
  7598. (show-subtree)
  7599. (outline-next-heading))
  7600. (t
  7601. ;; we will sort the top-level entries in this file
  7602. (goto-char (point-min))
  7603. (or (org-at-heading-p) (outline-next-heading))
  7604. (setq start (point))
  7605. (goto-char (point-max))
  7606. (beginning-of-line 1)
  7607. (when (looking-at ".*?\\S-")
  7608. ;; File ends in a non-white line
  7609. (end-of-line 1)
  7610. (insert "\n"))
  7611. (setq end (point-max))
  7612. (setq what "top-level")
  7613. (goto-char start)
  7614. (show-all)))
  7615. (setq beg (point))
  7616. (if (>= beg end) (error "Nothing to sort"))
  7617. (looking-at "\\(\\*+\\)")
  7618. (setq stars (match-string 1)
  7619. re (concat "^" (regexp-quote stars) " +")
  7620. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  7621. txt (buffer-substring beg end))
  7622. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  7623. (if (and (not (equal stars "*")) (string-match re2 txt))
  7624. (error "Region to sort contains a level above the first entry"))
  7625. (unless sorting-type
  7626. (message
  7627. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  7628. [t]ime [s]cheduled [d]eadline [c]reated
  7629. A/N/P/R/O/F/T/S/D/C means reversed:"
  7630. what)
  7631. (setq sorting-type (read-char-exclusive))
  7632. (unless getkey-func
  7633. (and (= (downcase sorting-type) ?f)
  7634. (setq getkey-func
  7635. (org-icompleting-read "Sort using function: "
  7636. obarray 'fboundp t nil nil))
  7637. (setq getkey-func (intern getkey-func))))
  7638. (and (= (downcase sorting-type) ?r)
  7639. (not property)
  7640. (setq property
  7641. (org-icompleting-read "Property: "
  7642. (mapcar 'list (org-buffer-property-keys t))
  7643. nil t))))
  7644. (message "Sorting entries...")
  7645. (save-restriction
  7646. (narrow-to-region start end)
  7647. (let ((dcst (downcase sorting-type))
  7648. (case-fold-search nil)
  7649. (now (current-time)))
  7650. (sort-subr
  7651. (/= dcst sorting-type)
  7652. ;; This function moves to the beginning character of the "record" to
  7653. ;; be sorted.
  7654. (lambda nil
  7655. (if (re-search-forward re nil t)
  7656. (goto-char (match-beginning 0))
  7657. (goto-char (point-max))))
  7658. ;; This function moves to the last character of the "record" being
  7659. ;; sorted.
  7660. (lambda nil
  7661. (save-match-data
  7662. (condition-case nil
  7663. (outline-forward-same-level 1)
  7664. (error
  7665. (goto-char (point-max))))))
  7666. ;; This function returns the value that gets sorted against.
  7667. (lambda nil
  7668. (cond
  7669. ((= dcst ?n)
  7670. (if (looking-at org-complex-heading-regexp)
  7671. (string-to-number (org-sort-remove-invisible (match-string 4)))
  7672. nil))
  7673. ((= dcst ?a)
  7674. (if (looking-at org-complex-heading-regexp)
  7675. (funcall case-func (org-sort-remove-invisible (match-string 4)))
  7676. nil))
  7677. ((= dcst ?t)
  7678. (let ((end (save-excursion (outline-next-heading) (point))))
  7679. (if (or (re-search-forward org-ts-regexp end t)
  7680. (re-search-forward org-ts-regexp-both end t))
  7681. (org-time-string-to-seconds (match-string 0))
  7682. (org-float-time now))))
  7683. ((= dcst ?c)
  7684. (let ((end (save-excursion (outline-next-heading) (point))))
  7685. (if (re-search-forward
  7686. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  7687. end t)
  7688. (org-time-string-to-seconds (match-string 0))
  7689. (org-float-time now))))
  7690. ((= dcst ?s)
  7691. (let ((end (save-excursion (outline-next-heading) (point))))
  7692. (if (re-search-forward org-scheduled-time-regexp end t)
  7693. (org-time-string-to-seconds (match-string 1))
  7694. (org-float-time now))))
  7695. ((= dcst ?d)
  7696. (let ((end (save-excursion (outline-next-heading) (point))))
  7697. (if (re-search-forward org-deadline-time-regexp end t)
  7698. (org-time-string-to-seconds (match-string 1))
  7699. (org-float-time now))))
  7700. ((= dcst ?p)
  7701. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  7702. (string-to-char (match-string 2))
  7703. org-default-priority))
  7704. ((= dcst ?r)
  7705. (or (org-entry-get nil property) ""))
  7706. ((= dcst ?o)
  7707. (if (looking-at org-complex-heading-regexp)
  7708. (- 9999 (length (member (match-string 2)
  7709. org-todo-keywords-1)))))
  7710. ((= dcst ?f)
  7711. (if getkey-func
  7712. (progn
  7713. (setq tmp (funcall getkey-func))
  7714. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  7715. tmp)
  7716. (error "Invalid key function `%s'" getkey-func)))
  7717. (t (error "Invalid sorting type `%c'" sorting-type))))
  7718. nil
  7719. (cond
  7720. ((= dcst ?a) 'string<)
  7721. ((= dcst ?f) compare-func)
  7722. ((member dcst '(?p ?t ?s ?d ?c)) '<)))))
  7723. (run-hooks 'org-after-sorting-entries-or-items-hook)
  7724. ;; Reset the clock marker if needed
  7725. (when cmstr
  7726. (save-excursion
  7727. (goto-char start)
  7728. (search-forward cmstr nil t)
  7729. (move-marker org-clock-marker (point))))
  7730. (message "Sorting entries...done")))
  7731. (defun org-do-sort (table what &optional with-case sorting-type)
  7732. "Sort TABLE of WHAT according to SORTING-TYPE.
  7733. The user will be prompted for the SORTING-TYPE if the call to this
  7734. function does not specify it. WHAT is only for the prompt, to indicate
  7735. what is being sorted. The sorting key will be extracted from
  7736. the car of the elements of the table.
  7737. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  7738. (unless sorting-type
  7739. (message
  7740. "Sort %s: [a]lphabetic, [n]umeric, [t]ime. A/N/T means reversed:"
  7741. what)
  7742. (setq sorting-type (read-char-exclusive)))
  7743. (let ((dcst (downcase sorting-type))
  7744. extractfun comparefun)
  7745. ;; Define the appropriate functions
  7746. (cond
  7747. ((= dcst ?n)
  7748. (setq extractfun 'string-to-number
  7749. comparefun (if (= dcst sorting-type) '< '>)))
  7750. ((= dcst ?a)
  7751. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  7752. (lambda(x) (downcase (org-sort-remove-invisible x))))
  7753. comparefun (if (= dcst sorting-type)
  7754. 'string<
  7755. (lambda (a b) (and (not (string< a b))
  7756. (not (string= a b)))))))
  7757. ((= dcst ?t)
  7758. (setq extractfun
  7759. (lambda (x)
  7760. (if (or (string-match org-ts-regexp x)
  7761. (string-match org-ts-regexp-both x))
  7762. (org-float-time
  7763. (org-time-string-to-time (match-string 0 x)))
  7764. 0))
  7765. comparefun (if (= dcst sorting-type) '< '>)))
  7766. (t (error "Invalid sorting type `%c'" sorting-type)))
  7767. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  7768. table)
  7769. (lambda (a b) (funcall comparefun (car a) (car b))))))
  7770. ;;; The orgstruct minor mode
  7771. ;; Define a minor mode which can be used in other modes in order to
  7772. ;; integrate the org-mode structure editing commands.
  7773. ;; This is really a hack, because the org-mode structure commands use
  7774. ;; keys which normally belong to the major mode. Here is how it
  7775. ;; works: The minor mode defines all the keys necessary to operate the
  7776. ;; structure commands, but wraps the commands into a function which
  7777. ;; tests if the cursor is currently at a headline or a plain list
  7778. ;; item. If that is the case, the structure command is used,
  7779. ;; temporarily setting many Org-mode variables like regular
  7780. ;; expressions for filling etc. However, when any of those keys is
  7781. ;; used at a different location, function uses `key-binding' to look
  7782. ;; up if the key has an associated command in another currently active
  7783. ;; keymap (minor modes, major mode, global), and executes that
  7784. ;; command. There might be problems if any of the keys is otherwise
  7785. ;; used as a prefix key.
  7786. (defcustom orgstruct-heading-prefix-regexp ""
  7787. "Regexp that matches the custom prefix of Org headlines in
  7788. orgstruct(++)-mode."
  7789. :group 'org
  7790. :version "24.4"
  7791. :package-version '(Org . "8.0")
  7792. :type 'string)
  7793. ;;;###autoload(put 'orgstruct-heading-prefix-regexp 'safe-local-variable 'stringp)
  7794. (defcustom orgstruct-setup-hook nil
  7795. "Hook run after orgstruct-mode-map is filled."
  7796. :group 'org
  7797. :version "24.4"
  7798. :package-version '(Org . "8.0")
  7799. :type 'hook)
  7800. (defvar orgstruct-initialized nil)
  7801. (defvar org-local-vars nil
  7802. "List of local variables, for use by `orgstruct-mode'.")
  7803. ;;;###autoload
  7804. (define-minor-mode orgstruct-mode
  7805. "Toggle the minor mode `orgstruct-mode'.
  7806. This mode is for using Org-mode structure commands in other
  7807. modes. The following keys behave as if Org-mode were active, if
  7808. the cursor is on a headline, or on a plain list item (both as
  7809. defined by Org-mode)."
  7810. nil " OrgStruct" (make-sparse-keymap)
  7811. (when orgstruct-mode
  7812. (org-load-modules-maybe)
  7813. (unless orgstruct-initialized
  7814. (orgstruct-setup)
  7815. (setq orgstruct-initialized t))))
  7816. ;;;###autoload
  7817. (defun turn-on-orgstruct ()
  7818. "Unconditionally turn on `orgstruct-mode'."
  7819. (orgstruct-mode 1))
  7820. (defvar org-fb-vars nil)
  7821. (make-variable-buffer-local 'org-fb-vars)
  7822. (defun orgstruct++-mode (&optional arg)
  7823. "Toggle `orgstruct-mode', the enhanced version of it.
  7824. In addition to setting orgstruct-mode, this also exports all
  7825. indentation and autofilling variables from org-mode into the
  7826. buffer. It will also recognize item context in multiline items."
  7827. (interactive "P")
  7828. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  7829. (if (< arg 1)
  7830. (progn (orgstruct-mode -1)
  7831. (mapc (lambda(v)
  7832. (org-set-local (car v)
  7833. (if (eq (car-safe (cadr v)) 'quote) (cadadr v) (cadr v))))
  7834. org-fb-vars))
  7835. (orgstruct-mode 1)
  7836. (setq org-fb-vars nil)
  7837. (let (var val)
  7838. (mapc
  7839. (lambda (x)
  7840. (when (string-match
  7841. "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|fill-prefix\\|indent-\\)"
  7842. (symbol-name (car x)))
  7843. (setq var (car x) val (nth 1 x))
  7844. (push (list var `(quote ,(eval var))) org-fb-vars)
  7845. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  7846. org-local-vars)
  7847. (org-set-local 'orgstruct-is-++ t))))
  7848. (defvar orgstruct-is-++ nil
  7849. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  7850. (make-variable-buffer-local 'orgstruct-is-++)
  7851. ;;;###autoload
  7852. (defun turn-on-orgstruct++ ()
  7853. "Unconditionally turn on `orgstruct++-mode'."
  7854. (orgstruct++-mode 1))
  7855. (defun orgstruct-error ()
  7856. "Error when there is no default binding for a structure key."
  7857. (interactive)
  7858. (error "This key has no function outside structure elements"))
  7859. (defun orgstruct-setup ()
  7860. "Setup orgstruct keymap."
  7861. (dolist (f
  7862. '("org-meta"
  7863. "org-shift"
  7864. "org-shiftmeta"
  7865. org-shifttab
  7866. org-backward-element
  7867. org-backward-heading-same-level
  7868. org-ctrl-c-ret
  7869. org-cycle
  7870. org-forward-heading-same-level
  7871. org-insert-heading
  7872. org-insert-heading-respect-content
  7873. org-kill-note-or-show-branches
  7874. org-mark-subtree
  7875. org-narrow-to-subtree
  7876. org-promote-subtree
  7877. org-reveal
  7878. org-show-subtree
  7879. org-sort
  7880. org-up-element
  7881. outline-demote
  7882. outline-next-visible-heading
  7883. outline-previous-visible-heading
  7884. outline-promote
  7885. outline-up-heading
  7886. show-children))
  7887. (dolist (f (if (stringp f)
  7888. (let ((flist))
  7889. (dolist (postfix
  7890. '("-return" "tab" "left" "right" "up" "down")
  7891. flist)
  7892. (let ((f (intern (concat f postfix))))
  7893. (when (fboundp f)
  7894. (push f flist)))))
  7895. (list f)))
  7896. (dolist (binding (nconc (where-is-internal f org-mode-map)
  7897. (where-is-internal f outline-mode-map)))
  7898. ;; TODO use local-function-key-map
  7899. (dolist (rep '(("<tab>" . "TAB")
  7900. ("<return>" . "RET")
  7901. ("<escape>" . "ESC")
  7902. ("<delete>" . "DEL")))
  7903. (setq binding (read-kbd-macro (replace-regexp-in-string
  7904. (regexp-quote (car rep))
  7905. (cdr rep)
  7906. (key-description binding)))))
  7907. (let ((key (lookup-key orgstruct-mode-map binding)))
  7908. (when (or (not key) (numberp key))
  7909. (condition-case nil
  7910. (org-defkey orgstruct-mode-map
  7911. binding
  7912. (orgstruct-make-binding f binding))
  7913. (error nil)))))))
  7914. (run-hooks 'orgstruct-setup-hook))
  7915. (defun orgstruct-make-binding (fun key)
  7916. "Create a function for binding in the structure minor mode.
  7917. FUN is the command to call inside a table. KEY is the key that
  7918. should be checked in for a command to execute outside of tables."
  7919. (let ((name (concat "orgstruct-hijacker-" (symbol-name fun))))
  7920. (let ((nname name)
  7921. (i 0))
  7922. (while (fboundp (intern nname))
  7923. (setq nname (format "%s-%d" name (setq i (1+ i)))))
  7924. (setq name (intern nname)))
  7925. (eval
  7926. (let ((bindings '((org-heading-regexp
  7927. (concat "^"
  7928. orgstruct-heading-prefix-regexp
  7929. "\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ ]*$"))
  7930. (org-outline-regexp
  7931. (concat orgstruct-heading-prefix-regexp "\\*+ "))
  7932. (org-outline-regexp-bol
  7933. (concat "^" org-outline-regexp))
  7934. (outline-regexp org-outline-regexp)
  7935. (outline-heading-end-regexp "\n")
  7936. (outline-level 'outline-level)
  7937. (outline-heading-alist))))
  7938. `(defun ,name (arg)
  7939. ,(concat "In Structure, run `" (symbol-name fun) "'.\n"
  7940. "Outside of structure, run the binding of `"
  7941. (key-description key) "'.")
  7942. (interactive "p")
  7943. (unless
  7944. (let* ,bindings
  7945. (when (org-context-p 'headline 'item
  7946. ,(when (memq fun '(org-insert-heading))
  7947. '(when orgstruct-is-++
  7948. 'item-body)))
  7949. (org-run-like-in-org-mode
  7950. (lambda ()
  7951. (interactive)
  7952. (let* ,bindings
  7953. (call-interactively ',fun))))
  7954. t))
  7955. (let* ((orgstruct-mode)
  7956. (binding (key-binding ,key)))
  7957. (if (keymapp binding)
  7958. (set-temporary-overlay-map binding)
  7959. (call-interactively
  7960. (or binding 'orgstruct-error))))))))
  7961. name))
  7962. (defun org-contextualize-keys (alist contexts)
  7963. "Return valid elements in ALIST depending on CONTEXTS.
  7964. `org-agenda-custom-commands' or `org-capture-templates' are the
  7965. values used for ALIST, and `org-agenda-custom-commands-contexts'
  7966. or `org-capture-templates-contexts' are the associated contexts
  7967. definitions."
  7968. (let ((contexts
  7969. ;; normalize contexts
  7970. (mapcar
  7971. (lambda(c) (cond ((listp (cadr c))
  7972. (list (car c) (car c) (cadr c)))
  7973. ((string= "" (cadr c))
  7974. (list (car c) (car c) (caddr c)))
  7975. (t c))) contexts))
  7976. (a alist) c r s)
  7977. ;; loop over all commands or templates
  7978. (while (setq c (pop a))
  7979. (let (vrules repl)
  7980. (cond
  7981. ((not (assoc (car c) contexts))
  7982. (push c r))
  7983. ((and (assoc (car c) contexts)
  7984. (setq vrules (org-contextualize-validate-key
  7985. (car c) contexts)))
  7986. (mapc (lambda (vr)
  7987. (when (not (equal (car vr) (cadr vr)))
  7988. (setq repl vr))) vrules)
  7989. (if (not repl) (push c r)
  7990. (push (cadr repl) s)
  7991. (push
  7992. (cons (car c)
  7993. (cdr (or (assoc (cadr repl) alist)
  7994. (error "Undefined key `%s' as contextual replacement for `%s'"
  7995. (cadr repl) (car c)))))
  7996. r))))))
  7997. ;; Return limited ALIST, possibly with keys modified, and deduplicated
  7998. (delq
  7999. nil
  8000. (delete-dups
  8001. (mapcar (lambda (x)
  8002. (let ((tpl (car x)))
  8003. (when (not (delq
  8004. nil
  8005. (mapcar (lambda(y)
  8006. (equal y tpl)) s))) x)))
  8007. (reverse r))))))
  8008. (defun org-contextualize-validate-key (key contexts)
  8009. "Check CONTEXTS for agenda or capture KEY."
  8010. (let (r rr res)
  8011. (while (setq r (pop contexts))
  8012. (mapc
  8013. (lambda (rr)
  8014. (when
  8015. (and (equal key (car r))
  8016. (if (functionp rr) (funcall rr)
  8017. (or (and (eq (car rr) 'in-file)
  8018. (buffer-file-name)
  8019. (string-match (cdr rr) (buffer-file-name)))
  8020. (and (eq (car rr) 'in-mode)
  8021. (string-match (cdr rr) (symbol-name major-mode)))
  8022. (and (eq (car rr) 'in-buffer)
  8023. (string-match (cdr rr) (buffer-name)))
  8024. (when (and (eq (car rr) 'not-in-file)
  8025. (buffer-file-name))
  8026. (not (string-match (cdr rr) (buffer-file-name))))
  8027. (when (eq (car rr) 'not-in-mode)
  8028. (not (string-match (cdr rr) (symbol-name major-mode))))
  8029. (when (eq (car rr) 'not-in-buffer)
  8030. (not (string-match (cdr rr) (buffer-name)))))))
  8031. (push r res)))
  8032. (car (last r))))
  8033. (delete-dups (delq nil res))))
  8034. (defun org-context-p (&rest contexts)
  8035. "Check if local context is any of CONTEXTS.
  8036. Possible values in the list of contexts are `table', `headline', and `item'."
  8037. (let ((pos (point)))
  8038. (goto-char (point-at-bol))
  8039. (prog1 (or (and (memq 'table contexts)
  8040. (looking-at "[ \t]*|"))
  8041. (and (memq 'headline contexts)
  8042. (looking-at org-outline-regexp))
  8043. (and (memq 'item contexts)
  8044. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  8045. (and (memq 'item-body contexts)
  8046. (org-in-item-p)))
  8047. (goto-char pos))))
  8048. (defun org-get-local-variables ()
  8049. "Return a list of all local variables in an Org mode buffer."
  8050. (let (varlist)
  8051. (with-current-buffer (get-buffer-create "*Org tmp*")
  8052. (erase-buffer)
  8053. (org-mode)
  8054. (setq varlist (buffer-local-variables)))
  8055. (kill-buffer "*Org tmp*")
  8056. (delq nil
  8057. (mapcar
  8058. (lambda (x)
  8059. (setq x
  8060. (if (symbolp x)
  8061. (list x)
  8062. (list (car x) (cdr x))))
  8063. (if (and (not (get (car x) 'org-state))
  8064. (string-match
  8065. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
  8066. (symbol-name (car x))))
  8067. x nil))
  8068. varlist))))
  8069. (defun org-clone-local-variables (from-buffer &optional regexp)
  8070. "Clone local variables from FROM-BUFFER.
  8071. Optional argument REGEXP selects variables to clone."
  8072. (mapc
  8073. (lambda (pair)
  8074. (and (symbolp (car pair))
  8075. (or (null regexp)
  8076. (string-match regexp (symbol-name (car pair))))
  8077. (set (make-local-variable (car pair))
  8078. (cdr pair))))
  8079. (buffer-local-variables from-buffer)))
  8080. ;;;###autoload
  8081. (defun org-run-like-in-org-mode (cmd)
  8082. "Run a command, pretending that the current buffer is in Org-mode.
  8083. This will temporarily bind local variables that are typically bound in
  8084. Org-mode to the values they have in Org-mode, and then interactively
  8085. call CMD."
  8086. (org-load-modules-maybe)
  8087. (unless org-local-vars
  8088. (setq org-local-vars (org-get-local-variables)))
  8089. (let (binds)
  8090. (dolist (var org-local-vars)
  8091. (when (or (not (boundp (car var)))
  8092. (eq (symbol-value (car var))
  8093. (default-value (car var))))
  8094. (push (list (car var) `(quote ,(cadr var))) binds)))
  8095. (eval `(let ,binds
  8096. (call-interactively (quote ,cmd))))))
  8097. ;;;; Archiving
  8098. (defun org-get-category (&optional pos force-refresh)
  8099. "Get the category applying to position POS."
  8100. (save-match-data
  8101. (if force-refresh (org-refresh-category-properties))
  8102. (let ((pos (or pos (point))))
  8103. (or (get-text-property pos 'org-category)
  8104. (progn (org-refresh-category-properties)
  8105. (get-text-property pos 'org-category))))))
  8106. (defun org-refresh-category-properties ()
  8107. "Refresh category text properties in the buffer."
  8108. (let ((case-fold-search t)
  8109. (inhibit-read-only t)
  8110. (def-cat (cond
  8111. ((null org-category)
  8112. (if buffer-file-name
  8113. (file-name-sans-extension
  8114. (file-name-nondirectory buffer-file-name))
  8115. "???"))
  8116. ((symbolp org-category) (symbol-name org-category))
  8117. (t org-category)))
  8118. beg end cat pos optionp)
  8119. (org-with-silent-modifications
  8120. (save-excursion
  8121. (save-restriction
  8122. (widen)
  8123. (goto-char (point-min))
  8124. (put-text-property (point) (point-max) 'org-category def-cat)
  8125. (while (re-search-forward
  8126. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  8127. (setq pos (match-end 0)
  8128. optionp (equal (char-after (match-beginning 0)) ?#)
  8129. cat (org-trim (match-string 2)))
  8130. (if optionp
  8131. (setq beg (point-at-bol) end (point-max))
  8132. (org-back-to-heading t)
  8133. (setq beg (point) end (org-end-of-subtree t t)))
  8134. (put-text-property beg end 'org-category cat)
  8135. (put-text-property beg end 'org-category-position beg)
  8136. (goto-char pos)))))))
  8137. (defun org-refresh-properties (dprop tprop)
  8138. "Refresh buffer text properties.
  8139. DPROP is the drawer property and TPROP is the corresponding text
  8140. property to set."
  8141. (let ((case-fold-search t)
  8142. (inhibit-read-only t) p)
  8143. (org-with-silent-modifications
  8144. (save-excursion
  8145. (save-restriction
  8146. (widen)
  8147. (goto-char (point-min))
  8148. (while (re-search-forward (concat "^[ \t]*:" dprop ": +\\(.*\\)[ \t]*$") nil t)
  8149. (setq p (org-match-string-no-properties 1))
  8150. (save-excursion
  8151. (org-back-to-heading t)
  8152. (put-text-property
  8153. (point-at-bol) (point-at-eol) tprop p))))))))
  8154. ;;;; Link Stuff
  8155. ;;; Link abbreviations
  8156. (defun org-link-expand-abbrev (link)
  8157. "Apply replacements as defined in `org-link-abbrev-alist'."
  8158. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  8159. (let* ((key (match-string 1 link))
  8160. (as (or (assoc key org-link-abbrev-alist-local)
  8161. (assoc key org-link-abbrev-alist)))
  8162. (tag (and (match-end 2) (match-string 3 link)))
  8163. rpl)
  8164. (if (not as)
  8165. link
  8166. (setq rpl (cdr as))
  8167. (cond
  8168. ((symbolp rpl) (funcall rpl tag))
  8169. ((string-match "%(\\([^)]+\\))" rpl)
  8170. (replace-match (funcall (intern-soft (match-string 1 rpl)) tag) t t rpl))
  8171. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  8172. ((string-match "%h" rpl)
  8173. (replace-match (url-hexify-string (or tag "")) t t rpl))
  8174. (t (concat rpl tag)))))
  8175. link))
  8176. ;;; Storing and inserting links
  8177. (defvar org-insert-link-history nil
  8178. "Minibuffer history for links inserted with `org-insert-link'.")
  8179. (defvar org-stored-links nil
  8180. "Contains the links stored with `org-store-link'.")
  8181. (defvar org-store-link-plist nil
  8182. "Plist with info about the most recently link created with `org-store-link'.")
  8183. (defvar org-link-protocols nil
  8184. "Link protocols added to Org-mode using `org-add-link-type'.")
  8185. (defvar org-store-link-functions nil
  8186. "List of functions that are called to create and store a link.
  8187. Each function will be called in turn until one returns a non-nil
  8188. value. Each function should check if it is responsible for creating
  8189. this link (for example by looking at the major mode).
  8190. If not, it must exit and return nil.
  8191. If yes, it should return a non-nil value after a calling
  8192. `org-store-link-props' with a list of properties and values.
  8193. Special properties are:
  8194. :type The link prefix, like \"http\". This must be given.
  8195. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  8196. This is obligatory as well.
  8197. :description Optional default description for the second pair
  8198. of brackets in an Org-mode link. The user can still change
  8199. this when inserting this link into an Org-mode buffer.
  8200. In addition to these, any additional properties can be specified
  8201. and then used in capture templates.")
  8202. (defun org-add-link-type (type &optional follow export)
  8203. "Add TYPE to the list of `org-link-types'.
  8204. Re-compute all regular expressions depending on `org-link-types'
  8205. FOLLOW and EXPORT are two functions.
  8206. FOLLOW should take the link path as the single argument and do whatever
  8207. is necessary to follow the link, for example find a file or display
  8208. a mail message.
  8209. EXPORT should format the link path for export to one of the export formats.
  8210. It should be a function accepting three arguments:
  8211. path the path of the link, the text after the prefix (like \"http:\")
  8212. desc the description of the link, if any, or a description added by
  8213. org-export-normalize-links if there is none
  8214. format the export format, a symbol like `html' or `latex' or `ascii'..
  8215. The function may use the FORMAT information to return different values
  8216. depending on the format. The return value will be put literally into
  8217. the exported file. If the return value is nil, this means Org should
  8218. do what it normally does with links which do not have EXPORT defined.
  8219. Org-mode has a built-in default for exporting links. If you are happy with
  8220. this default, there is no need to define an export function for the link
  8221. type. For a simple example of an export function, see `org-bbdb.el'."
  8222. (add-to-list 'org-link-types type t)
  8223. (org-make-link-regexps)
  8224. (if (assoc type org-link-protocols)
  8225. (setcdr (assoc type org-link-protocols) (list follow export))
  8226. (push (list type follow export) org-link-protocols)))
  8227. (defvar org-agenda-buffer-name) ; Defined in org-agenda.el
  8228. (defvar org-id-link-to-org-use-id) ; Defined in org-id.el
  8229. ;;;###autoload
  8230. (defun org-store-link (arg)
  8231. "\\<org-mode-map>Store an org-link to the current location.
  8232. This link is added to `org-stored-links' and can later be inserted
  8233. into an org-buffer with \\[org-insert-link].
  8234. For some link types, a prefix arg is interpreted.
  8235. For links to Usenet articles, arg negates `org-gnus-prefer-web-links'.
  8236. For file links, arg negates `org-context-in-file-links'.
  8237. A double prefix arg force skipping storing functions that are not
  8238. part of Org's core."
  8239. (interactive "P")
  8240. (org-load-modules-maybe)
  8241. (setq org-store-link-plist nil) ; reset
  8242. (org-with-limited-levels
  8243. (let (link cpltxt desc description search txt custom-id agenda-link sfuns sfunsn)
  8244. (cond
  8245. ((and (not (equal arg '(16)))
  8246. (setq sfuns
  8247. (delq
  8248. nil (mapcar (lambda (f) (let (fs) (if (funcall f) (push f fs))))
  8249. org-store-link-functions))
  8250. sfunsn (mapcar (lambda (fu) (symbol-name (car fu))) sfuns))
  8251. (or (and (cdr sfuns)
  8252. (funcall (intern
  8253. (completing-read "Which function for creating the link? "
  8254. sfunsn t (car sfunsn)))))
  8255. (funcall (caar sfuns)))
  8256. (setq link (plist-get org-store-link-plist :link)
  8257. desc (or (plist-get org-store-link-plist :description) link))))
  8258. ((org-src-edit-buffer-p)
  8259. (let (label gc)
  8260. (while (or (not label)
  8261. (save-excursion
  8262. (save-restriction
  8263. (widen)
  8264. (goto-char (point-min))
  8265. (re-search-forward
  8266. (regexp-quote (format org-coderef-label-format label))
  8267. nil t))))
  8268. (when label (message "Label exists already") (sit-for 2))
  8269. (setq label (read-string "Code line label: " label)))
  8270. (end-of-line 1)
  8271. (setq link (format org-coderef-label-format label))
  8272. (setq gc (- 79 (length link)))
  8273. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  8274. (insert link)
  8275. (setq link (concat "(" label ")") desc nil)))
  8276. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  8277. ;; We are in the agenda, link to referenced location
  8278. (let ((m (or (get-text-property (point) 'org-hd-marker)
  8279. (get-text-property (point) 'org-marker))))
  8280. (when m
  8281. (org-with-point-at m
  8282. (setq agenda-link
  8283. (if (org-called-interactively-p 'any)
  8284. (call-interactively 'org-store-link)
  8285. (org-store-link nil)))))))
  8286. ((eq major-mode 'calendar-mode)
  8287. (let ((cd (calendar-cursor-to-date)))
  8288. (setq link
  8289. (format-time-string
  8290. (car org-time-stamp-formats)
  8291. (apply 'encode-time
  8292. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  8293. nil nil nil))))
  8294. (org-store-link-props :type "calendar" :date cd)))
  8295. ((eq major-mode 'help-mode)
  8296. (setq link (concat "help:" (save-excursion
  8297. (goto-char (point-min))
  8298. (looking-at "^[^ ]+")
  8299. (match-string 0))))
  8300. (org-store-link-props :type "help"))
  8301. ((eq major-mode 'w3-mode)
  8302. (setq cpltxt (if (and (buffer-name)
  8303. (not (string-match "Untitled" (buffer-name))))
  8304. (buffer-name)
  8305. (url-view-url t))
  8306. link (url-view-url t))
  8307. (org-store-link-props :type "w3" :url (url-view-url t)))
  8308. ((setq search (run-hook-with-args-until-success
  8309. 'org-create-file-search-functions))
  8310. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  8311. "::" search))
  8312. (setq cpltxt (or description link)))
  8313. ((eq major-mode 'image-mode)
  8314. (setq cpltxt (concat "file:"
  8315. (abbreviate-file-name buffer-file-name))
  8316. link cpltxt)
  8317. (org-store-link-props :type "image" :file buffer-file-name))
  8318. ((eq major-mode 'dired-mode)
  8319. ;; link to the file in the current line
  8320. (let ((file (dired-get-filename nil t)))
  8321. (setq file (if file
  8322. (abbreviate-file-name
  8323. (expand-file-name (dired-get-filename nil t)))
  8324. ;; otherwise, no file so use current directory.
  8325. default-directory))
  8326. (setq cpltxt (concat "file:" file)
  8327. link cpltxt)))
  8328. ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
  8329. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  8330. (cond
  8331. ((org-in-regexp "<<\\(.*?\\)>>")
  8332. (setq cpltxt
  8333. (concat "file:"
  8334. (abbreviate-file-name
  8335. (buffer-file-name (buffer-base-buffer)))
  8336. "::" (match-string 1))
  8337. link cpltxt))
  8338. ((and (featurep 'org-id)
  8339. (or (eq org-id-link-to-org-use-id t)
  8340. (and (org-called-interactively-p 'any)
  8341. (or (eq org-id-link-to-org-use-id 'create-if-interactive)
  8342. (and (eq org-id-link-to-org-use-id
  8343. 'create-if-interactive-and-no-custom-id)
  8344. (not custom-id))))
  8345. (and org-id-link-to-org-use-id (org-entry-get nil "ID"))))
  8346. ;; We can make a link using the ID.
  8347. (setq link (condition-case nil
  8348. (prog1 (org-id-store-link)
  8349. (setq desc (plist-get org-store-link-plist :description)))
  8350. (error
  8351. ;; probably before first headline, link to file only
  8352. (concat "file:"
  8353. (abbreviate-file-name
  8354. (buffer-file-name (buffer-base-buffer))))))))
  8355. (t
  8356. ;; Just link to current headline
  8357. (setq cpltxt (concat "file:"
  8358. (abbreviate-file-name
  8359. (buffer-file-name (buffer-base-buffer)))))
  8360. ;; Add a context search string
  8361. (when (org-xor org-context-in-file-links arg)
  8362. (let* ((ee (org-element-at-point))
  8363. (et (org-element-type ee))
  8364. (ev (plist-get (cadr ee) :value)))
  8365. (setq txt (cond
  8366. ((org-at-heading-p) nil)
  8367. ((eq et 'keyword) ev)
  8368. ((org-region-active-p)
  8369. (buffer-substring (region-beginning) (region-end)))))
  8370. (when (or (null txt) (string-match "\\S-" txt))
  8371. (setq cpltxt
  8372. (concat cpltxt "::"
  8373. (condition-case nil
  8374. (org-make-org-heading-search-string txt)
  8375. (error "")))
  8376. desc (or (and (eq et 'keyword) ev)
  8377. (nth 4 (ignore-errors (org-heading-components)))
  8378. "NONE")))))
  8379. (if (string-match "::\\'" cpltxt)
  8380. (setq cpltxt (substring cpltxt 0 -2)))
  8381. (setq link cpltxt))))
  8382. ((buffer-file-name (buffer-base-buffer))
  8383. ;; Just link to this file here.
  8384. (setq cpltxt (concat "file:"
  8385. (abbreviate-file-name
  8386. (buffer-file-name (buffer-base-buffer)))))
  8387. ;; Add a context string
  8388. (when (org-xor org-context-in-file-links arg)
  8389. (setq txt (if (org-region-active-p)
  8390. (buffer-substring (region-beginning) (region-end))
  8391. (buffer-substring (point-at-bol) (point-at-eol))))
  8392. ;; Only use search option if there is some text.
  8393. (when (string-match "\\S-" txt)
  8394. (setq cpltxt
  8395. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  8396. desc "NONE")))
  8397. (setq link cpltxt))
  8398. ((org-called-interactively-p 'interactive)
  8399. (user-error "No method for storing a link from this buffer"))
  8400. (t (setq link nil)))
  8401. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  8402. (setq link (or link cpltxt)
  8403. desc (or desc cpltxt))
  8404. (cond ((equal desc "NONE") (setq desc nil))
  8405. ((string-match org-bracket-link-regexp desc)
  8406. (setq desc (replace-regexp-in-string
  8407. org-bracket-link-regexp
  8408. (concat "\\3" (if (equal (length (match-string 0 desc))
  8409. (length desc)) "*" "")) desc))))
  8410. (if (and (or (org-called-interactively-p 'any) executing-kbd-macro) link)
  8411. (progn
  8412. (setq org-stored-links
  8413. (cons (list link desc) org-stored-links))
  8414. (message "Stored: %s" (or desc link))
  8415. (when custom-id
  8416. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  8417. "::#" custom-id))
  8418. (setq org-stored-links
  8419. (cons (list link desc) org-stored-links))))
  8420. (or agenda-link (and link (org-make-link-string link desc)))))))
  8421. (defun org-store-link-props (&rest plist)
  8422. "Store link properties, extract names and addresses."
  8423. (let (x adr)
  8424. (when (setq x (plist-get plist :from))
  8425. (setq adr (mail-extract-address-components x))
  8426. (setq plist (plist-put plist :fromname (car adr)))
  8427. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  8428. (when (setq x (plist-get plist :to))
  8429. (setq adr (mail-extract-address-components x))
  8430. (setq plist (plist-put plist :toname (car adr)))
  8431. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  8432. (let ((from (plist-get plist :from))
  8433. (to (plist-get plist :to)))
  8434. (when (and from to org-from-is-user-regexp)
  8435. (setq plist
  8436. (plist-put plist :fromto
  8437. (if (string-match org-from-is-user-regexp from)
  8438. (concat "to %t")
  8439. (concat "from %f"))))))
  8440. (setq org-store-link-plist plist))
  8441. (defun org-add-link-props (&rest plist)
  8442. "Add these properties to the link property list."
  8443. (let (key value)
  8444. (while plist
  8445. (setq key (pop plist) value (pop plist))
  8446. (setq org-store-link-plist
  8447. (plist-put org-store-link-plist key value)))))
  8448. (defun org-email-link-description (&optional fmt)
  8449. "Return the description part of an email link.
  8450. This takes information from `org-store-link-plist' and formats it
  8451. according to FMT (default from `org-email-link-description-format')."
  8452. (setq fmt (or fmt org-email-link-description-format))
  8453. (let* ((p org-store-link-plist)
  8454. (to (plist-get p :toaddress))
  8455. (from (plist-get p :fromaddress))
  8456. (table
  8457. (list
  8458. (cons "%c" (plist-get p :fromto))
  8459. (cons "%F" (plist-get p :from))
  8460. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  8461. (cons "%T" (plist-get p :to))
  8462. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  8463. (cons "%s" (plist-get p :subject))
  8464. (cons "%d" (plist-get p :date))
  8465. (cons "%m" (plist-get p :message-id)))))
  8466. (when (string-match "%c" fmt)
  8467. ;; Check if the user wrote this message
  8468. (if (and org-from-is-user-regexp from to
  8469. (save-match-data (string-match org-from-is-user-regexp from)))
  8470. (setq fmt (replace-match "to %t" t t fmt))
  8471. (setq fmt (replace-match "from %f" t t fmt))))
  8472. (org-replace-escapes fmt table)))
  8473. (defun org-make-org-heading-search-string (&optional string)
  8474. "Make search string for the current headline or STRING."
  8475. (let ((s (or string
  8476. (and (derived-mode-p 'org-mode)
  8477. (save-excursion
  8478. (org-back-to-heading t)
  8479. (org-element-property :raw-value (org-element-at-point))))))
  8480. (lines org-context-in-file-links))
  8481. (or string (setq s (concat "*" s))) ; Add * for headlines
  8482. (setq s (replace-regexp-in-string "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" "" s))
  8483. (when (and string (integerp lines) (> lines 0))
  8484. (let ((slines (org-split-string s "\n")))
  8485. (when (< lines (length slines))
  8486. (setq s (mapconcat
  8487. 'identity
  8488. (reverse (nthcdr (- (length slines) lines)
  8489. (reverse slines))) "\n")))))
  8490. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  8491. (defun org-make-link-string (link &optional description)
  8492. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8493. (unless (string-match "\\S-" link)
  8494. (error "Empty link"))
  8495. (when (and description
  8496. (stringp description)
  8497. (not (string-match "\\S-" description)))
  8498. (setq description nil))
  8499. (when (stringp description)
  8500. ;; Remove brackets from the description, they are fatal.
  8501. (while (string-match "\\[" description)
  8502. (setq description (replace-match "{" t t description)))
  8503. (while (string-match "\\]" description)
  8504. (setq description (replace-match "}" t t description))))
  8505. (when (equal link description)
  8506. ;; No description needed, it is identical
  8507. (setq description nil))
  8508. (when (and (not description)
  8509. (not (string-match (org-image-file-name-regexp) link))
  8510. (not (equal link (org-link-escape link))))
  8511. (setq description (org-extract-attributes link)))
  8512. (setq link
  8513. (cond ((string-match (org-image-file-name-regexp) link) link)
  8514. ((string-match org-link-types-re link)
  8515. (concat (match-string 1 link)
  8516. (org-link-escape (substring link (match-end 1)))))
  8517. (t (org-link-escape link))))
  8518. (concat "[[" link "]"
  8519. (if description (concat "[" description "]") "")
  8520. "]"))
  8521. (defconst org-link-escape-chars
  8522. '(?\ ?\[ ?\] ?\; ?\= ?\+)
  8523. "List of characters that should be escaped in link.
  8524. This is the list that is used for internal purposes.")
  8525. (defconst org-link-escape-chars-browser
  8526. '(?\ )
  8527. "List of escapes for characters that are problematic in links.
  8528. This is the list that is used before handing over to the browser.")
  8529. (defun org-link-escape (text &optional table merge)
  8530. "Return percent escaped representation of TEXT.
  8531. TEXT is a string with the text to escape.
  8532. Optional argument TABLE is a list with characters that should be
  8533. escaped. When nil, `org-link-escape-chars' is used.
  8534. If optional argument MERGE is set, merge TABLE into
  8535. `org-link-escape-chars'."
  8536. (cond
  8537. ((and table merge)
  8538. (mapc (lambda (defchr)
  8539. (unless (member defchr table)
  8540. (setq table (cons defchr table)))) org-link-escape-chars))
  8541. ((null table)
  8542. (setq table org-link-escape-chars)))
  8543. (mapconcat
  8544. (lambda (char)
  8545. (if (or (member char table)
  8546. (and (or (< char 32) (= char 37) (> char 126))
  8547. org-url-hexify-p))
  8548. (mapconcat (lambda (sequence-element)
  8549. (format "%%%.2X" sequence-element))
  8550. (or (encode-coding-char char 'utf-8)
  8551. (error "Unable to percent escape character: %s"
  8552. (char-to-string char))) "")
  8553. (char-to-string char))) text ""))
  8554. (defun org-link-unescape (str)
  8555. "Unhex hexified Unicode strings as returned from the JavaScript function
  8556. encodeURIComponent. E.g. `%C3%B6' is the german o-Umlaut."
  8557. (unless (and (null str) (string= "" str))
  8558. (let ((pos 0) (case-fold-search t) unhexed)
  8559. (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
  8560. (setq unhexed (org-link-unescape-compound (match-string 0 str)))
  8561. (setq str (replace-match unhexed t t str))
  8562. (setq pos (+ pos (length unhexed))))))
  8563. str)
  8564. (defun org-link-unescape-compound (hex)
  8565. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German o-Umlaut.
  8566. Note: this function also decodes single byte encodings like
  8567. `%E1' (a-acute) if not followed by another `%[A-F0-9]{2}' group."
  8568. (save-match-data
  8569. (let* ((bytes (cdr (split-string hex "%")))
  8570. (ret "")
  8571. (eat 0)
  8572. (sum 0))
  8573. (while bytes
  8574. (let* ((val (string-to-number (pop bytes) 16))
  8575. (shift-xor
  8576. (if (= 0 eat)
  8577. (cond
  8578. ((>= val 252) (cons 6 252))
  8579. ((>= val 248) (cons 5 248))
  8580. ((>= val 240) (cons 4 240))
  8581. ((>= val 224) (cons 3 224))
  8582. ((>= val 192) (cons 2 192))
  8583. (t (cons 0 0)))
  8584. (cons 6 128))))
  8585. (if (>= val 192) (setq eat (car shift-xor)))
  8586. (setq val (logxor val (cdr shift-xor)))
  8587. (setq sum (+ (lsh sum (car shift-xor)) val))
  8588. (if (> eat 0) (setq eat (- eat 1)))
  8589. (cond
  8590. ((= 0 eat) ;multi byte
  8591. (setq ret (concat ret (org-char-to-string sum)))
  8592. (setq sum 0))
  8593. ((not bytes) ; single byte(s)
  8594. (setq ret (org-link-unescape-single-byte-sequence hex))))
  8595. )) ;; end (while bytes
  8596. ret )))
  8597. (defun org-link-unescape-single-byte-sequence (hex)
  8598. "Unhexify hex-encoded single byte character sequences."
  8599. (mapconcat (lambda (byte)
  8600. (char-to-string (string-to-number byte 16)))
  8601. (cdr (split-string hex "%")) ""))
  8602. (defun org-xor (a b)
  8603. "Exclusive or."
  8604. (if a (not b) b))
  8605. (defun org-fixup-message-id-for-http (s)
  8606. "Replace special characters in a message id, so it can be used in an http query."
  8607. (when (string-match "%" s)
  8608. (setq s (mapconcat (lambda (c)
  8609. (if (eq c ?%)
  8610. "%25"
  8611. (char-to-string c)))
  8612. s "")))
  8613. (while (string-match "<" s)
  8614. (setq s (replace-match "%3C" t t s)))
  8615. (while (string-match ">" s)
  8616. (setq s (replace-match "%3E" t t s)))
  8617. (while (string-match "@" s)
  8618. (setq s (replace-match "%40" t t s)))
  8619. s)
  8620. (defun org-link-prettify (link)
  8621. "Return a human-readable representation of LINK.
  8622. The car of LINK must be a raw link the cdr of LINK must be either
  8623. a link description or nil."
  8624. (let ((desc (or (cadr link) "<no description>")))
  8625. (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
  8626. "<" (car link) ">")))
  8627. ;;;###autoload
  8628. (defun org-insert-link-global ()
  8629. "Insert a link like Org-mode does.
  8630. This command can be called in any mode to insert a link in Org-mode syntax."
  8631. (interactive)
  8632. (org-load-modules-maybe)
  8633. (org-run-like-in-org-mode 'org-insert-link))
  8634. (defun org-insert-all-links (&optional keep)
  8635. "Insert all links in `org-stored-links'."
  8636. (interactive "P")
  8637. (let ((links (copy-sequence org-stored-links)) l)
  8638. (while (setq l (if keep (pop links) (pop org-stored-links)))
  8639. (insert "- ")
  8640. (org-insert-link nil (car l) (cadr l))
  8641. (insert "\n"))))
  8642. (defun org-link-fontify-links-to-this-file ()
  8643. "Fontify links to the current file in `org-stored-links'."
  8644. (let ((f (buffer-file-name)) a b)
  8645. (setq a (mapcar (lambda(l)
  8646. (let ((ll (car l)))
  8647. (when (and (string-match "^file:\\(.+\\)::" ll)
  8648. (equal f (expand-file-name (match-string 1 ll))))
  8649. ll)))
  8650. org-stored-links))
  8651. (when (featurep 'org-id)
  8652. (setq b (mapcar (lambda(l)
  8653. (let ((ll (car l)))
  8654. (when (and (string-match "^id:\\(.+\\)$" ll)
  8655. (equal f (expand-file-name
  8656. (or (org-id-find-id-file
  8657. (match-string 1 ll)) ""))))
  8658. ll)))
  8659. org-stored-links)))
  8660. (mapcar (lambda(l)
  8661. (put-text-property 0 (length l) 'face 'font-lock-comment-face l))
  8662. (delq nil (append a b)))))
  8663. (defvar org-link-links-in-this-file nil)
  8664. (defun org-insert-link (&optional complete-file link-location default-description)
  8665. "Insert a link. At the prompt, enter the link.
  8666. Completion can be used to insert any of the link protocol prefixes like
  8667. http or ftp in use.
  8668. The history can be used to select a link previously stored with
  8669. `org-store-link'. When the empty string is entered (i.e. if you just
  8670. press RET at the prompt), the link defaults to the most recently
  8671. stored link. As SPC triggers completion in the minibuffer, you need to
  8672. use M-SPC or C-q SPC to force the insertion of a space character.
  8673. You will also be prompted for a description, and if one is given, it will
  8674. be displayed in the buffer instead of the link.
  8675. If there is already a link at point, this command will allow you to edit link
  8676. and description parts.
  8677. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  8678. be selected using completion. The path to the file will be relative to the
  8679. current directory if the file is in the current directory or a subdirectory.
  8680. Otherwise, the link will be the absolute path as completed in the minibuffer
  8681. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  8682. option `org-link-file-path-type'.
  8683. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  8684. the current directory or below.
  8685. With three \\[universal-argument] prefixes, negate the meaning of
  8686. `org-keep-stored-link-after-insertion'.
  8687. If `org-make-link-description-function' is non-nil, this function will be
  8688. called with the link target, and the result will be the default
  8689. link description.
  8690. If the LINK-LOCATION parameter is non-nil, this value will be
  8691. used as the link location instead of reading one interactively.
  8692. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  8693. be used as the default description."
  8694. (interactive "P")
  8695. (let* ((wcf (current-window-configuration))
  8696. (origbuf (current-buffer))
  8697. (region (if (org-region-active-p)
  8698. (buffer-substring (region-beginning) (region-end))))
  8699. (remove (and region (list (region-beginning) (region-end))))
  8700. (desc region)
  8701. tmphist ; byte-compile incorrectly complains about this
  8702. (link link-location)
  8703. (abbrevs org-link-abbrev-alist-local)
  8704. entry file all-prefixes auto-desc)
  8705. (cond
  8706. (link-location) ; specified by arg, just use it.
  8707. ((org-in-regexp org-bracket-link-regexp 1)
  8708. ;; We do have a link at point, and we are going to edit it.
  8709. (setq remove (list (match-beginning 0) (match-end 0)))
  8710. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  8711. (setq link (read-string "Link: "
  8712. (org-link-unescape
  8713. (org-match-string-no-properties 1)))))
  8714. ((or (org-in-regexp org-angle-link-re)
  8715. (org-in-regexp org-plain-link-re))
  8716. ;; Convert to bracket link
  8717. (setq remove (list (match-beginning 0) (match-end 0))
  8718. link (read-string "Link: "
  8719. (org-remove-angle-brackets (match-string 0)))))
  8720. ((member complete-file '((4) (16)))
  8721. ;; Completing read for file names.
  8722. (setq link (org-file-complete-link complete-file)))
  8723. (t
  8724. ;; Read link, with completion for stored links.
  8725. (org-link-fontify-links-to-this-file)
  8726. (org-switch-to-buffer-other-window "*Org Links*")
  8727. (with-current-buffer "*Org Links*"
  8728. (erase-buffer)
  8729. (insert "Insert a link.
  8730. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  8731. (when org-stored-links
  8732. (insert "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  8733. (insert (mapconcat 'org-link-prettify
  8734. (reverse org-stored-links) "\n")))
  8735. (goto-char (point-min)))
  8736. (let ((cw (selected-window)))
  8737. (select-window (get-buffer-window "*Org Links*" 'visible))
  8738. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  8739. (unless (pos-visible-in-window-p (point-max))
  8740. (org-fit-window-to-buffer))
  8741. (and (window-live-p cw) (select-window cw)))
  8742. ;; Fake a link history, containing the stored links.
  8743. (setq tmphist (append (mapcar 'car org-stored-links)
  8744. org-insert-link-history))
  8745. (setq all-prefixes (append (mapcar 'car abbrevs)
  8746. (mapcar 'car org-link-abbrev-alist)
  8747. org-link-types))
  8748. (unwind-protect
  8749. (progn
  8750. (setq link
  8751. (org-completing-read
  8752. "Link: "
  8753. (append
  8754. (mapcar (lambda (x) (concat x ":"))
  8755. all-prefixes)
  8756. (mapcar 'car org-stored-links))
  8757. nil nil nil
  8758. 'tmphist
  8759. (caar org-stored-links)))
  8760. (if (not (string-match "\\S-" link))
  8761. (error "No link selected"))
  8762. (mapc (lambda(l)
  8763. (when (equal link (cadr l)) (setq link (car l) auto-desc t)))
  8764. org-stored-links)
  8765. (if (or (member link all-prefixes)
  8766. (and (equal ":" (substring link -1))
  8767. (member (substring link 0 -1) all-prefixes)
  8768. (setq link (substring link 0 -1))))
  8769. (setq link (with-current-buffer origbuf
  8770. (org-link-try-special-completion link)))))
  8771. (set-window-configuration wcf)
  8772. (kill-buffer "*Org Links*"))
  8773. (setq entry (assoc link org-stored-links))
  8774. (or entry (push link org-insert-link-history))
  8775. (setq desc (or desc (nth 1 entry)))))
  8776. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  8777. (not org-keep-stored-link-after-insertion))
  8778. (setq org-stored-links (delq (assoc link org-stored-links)
  8779. org-stored-links)))
  8780. (if (string-match org-plain-link-re link)
  8781. ;; URL-like link, normalize the use of angular brackets.
  8782. (setq link (org-remove-angle-brackets link)))
  8783. ;; Check if we are linking to the current file with a search
  8784. ;; option If yes, simplify the link by using only the search
  8785. ;; option.
  8786. (when (and buffer-file-name
  8787. (string-match "^file:\\(.+?\\)::\\(.+\\)" link))
  8788. (let* ((path (match-string 1 link))
  8789. (case-fold-search nil)
  8790. (search (match-string 2 link)))
  8791. (save-match-data
  8792. (if (equal (file-truename buffer-file-name) (file-truename path))
  8793. ;; We are linking to this same file, with a search option
  8794. (setq link search)))))
  8795. ;; Check if we can/should use a relative path. If yes, simplify the link
  8796. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  8797. (let* ((type (match-string 1 link))
  8798. (path (match-string 2 link))
  8799. (origpath path)
  8800. (case-fold-search nil))
  8801. (cond
  8802. ((or (eq org-link-file-path-type 'absolute)
  8803. (equal complete-file '(16)))
  8804. (setq path (abbreviate-file-name (expand-file-name path))))
  8805. ((eq org-link-file-path-type 'noabbrev)
  8806. (setq path (expand-file-name path)))
  8807. ((eq org-link-file-path-type 'relative)
  8808. (setq path (file-relative-name path)))
  8809. (t
  8810. (save-match-data
  8811. (if (string-match (concat "^" (regexp-quote
  8812. (expand-file-name
  8813. (file-name-as-directory
  8814. default-directory))))
  8815. (expand-file-name path))
  8816. ;; We are linking a file with relative path name.
  8817. (setq path (substring (expand-file-name path)
  8818. (match-end 0)))
  8819. (setq path (abbreviate-file-name (expand-file-name path)))))))
  8820. (setq link (concat type path))
  8821. (if (equal desc origpath)
  8822. (setq desc path))))
  8823. (if org-make-link-description-function
  8824. (setq desc
  8825. (or (condition-case nil
  8826. (funcall org-make-link-description-function link desc)
  8827. (error (progn (message "Can't get link description from `%s'"
  8828. (symbol-name org-make-link-description-function))
  8829. (sit-for 2) nil)))
  8830. (read-string "Description: " default-description)))
  8831. (if default-description (setq desc default-description)
  8832. (setq desc (or (and auto-desc desc)
  8833. (read-string "Description: " desc)))))
  8834. (unless (string-match "\\S-" desc) (setq desc nil))
  8835. (if remove (apply 'delete-region remove))
  8836. (insert (org-make-link-string link desc))))
  8837. (defun org-link-try-special-completion (type)
  8838. "If there is completion support for link type TYPE, offer it."
  8839. (let ((fun (intern (concat "org-" type "-complete-link"))))
  8840. (if (functionp fun)
  8841. (funcall fun)
  8842. (read-string "Link (no completion support): " (concat type ":")))))
  8843. (defun org-file-complete-link (&optional arg)
  8844. "Create a file link using completion."
  8845. (let (file link)
  8846. (setq file (org-iread-file-name "File: "))
  8847. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  8848. (pwd1 (file-name-as-directory (abbreviate-file-name
  8849. (expand-file-name ".")))))
  8850. (cond
  8851. ((equal arg '(16))
  8852. (setq link (concat
  8853. "file:"
  8854. (abbreviate-file-name (expand-file-name file)))))
  8855. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  8856. (setq link (concat "file:" (match-string 1 file))))
  8857. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  8858. (expand-file-name file))
  8859. (setq link (concat
  8860. "file:" (match-string 1 (expand-file-name file)))))
  8861. (t (setq link (concat "file:" file)))))
  8862. link))
  8863. (defun org-iread-file-name (&rest args)
  8864. "Read-file-name using `ido-mode' speedup if available.
  8865. ARGS are arguments that may be passed to `ido-read-file-name' or `read-file-name'.
  8866. See `read-file-name' for a description of parameters."
  8867. (org-without-partial-completion
  8868. (if (and org-completion-use-ido
  8869. (fboundp 'ido-read-file-name)
  8870. (boundp 'ido-mode) ido-mode
  8871. (listp (second args)))
  8872. (let ((ido-enter-matching-directory nil))
  8873. (apply 'ido-read-file-name args))
  8874. (apply 'read-file-name args))))
  8875. (defun org-completing-read (&rest args)
  8876. "Completing-read with SPACE being a normal character."
  8877. (let ((enable-recursive-minibuffers t)
  8878. (minibuffer-local-completion-map
  8879. (copy-keymap minibuffer-local-completion-map)))
  8880. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  8881. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  8882. (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
  8883. (apply 'org-icompleting-read args)))
  8884. (defun org-completing-read-no-i (&rest args)
  8885. (let (org-completion-use-ido org-completion-use-iswitchb)
  8886. (apply 'org-completing-read args)))
  8887. (defun org-iswitchb-completing-read (prompt choices &rest args)
  8888. "Use iswitch as a completing-read replacement to choose from choices.
  8889. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  8890. from."
  8891. (let* ((iswitchb-use-virtual-buffers nil)
  8892. (iswitchb-make-buflist-hook
  8893. (lambda ()
  8894. (setq iswitchb-temp-buflist choices))))
  8895. (iswitchb-read-buffer prompt)))
  8896. (defun org-icompleting-read (&rest args)
  8897. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  8898. (org-without-partial-completion
  8899. (if (and org-completion-use-ido
  8900. (fboundp 'ido-completing-read)
  8901. (boundp 'ido-mode) ido-mode
  8902. (listp (second args)))
  8903. (let ((ido-enter-matching-directory nil))
  8904. (apply 'ido-completing-read (concat (car args))
  8905. (if (consp (car (nth 1 args)))
  8906. (mapcar 'car (nth 1 args))
  8907. (nth 1 args))
  8908. (cddr args)))
  8909. (if (and org-completion-use-iswitchb
  8910. (boundp 'iswitchb-mode) iswitchb-mode
  8911. (listp (second args)))
  8912. (apply 'org-iswitchb-completing-read (concat (car args))
  8913. (if (consp (car (nth 1 args)))
  8914. (mapcar 'car (nth 1 args))
  8915. (nth 1 args))
  8916. (cddr args))
  8917. (apply 'completing-read args)))))
  8918. (defun org-extract-attributes (s)
  8919. "Extract the attributes cookie from a string and set as text property."
  8920. (let (a attr (start 0) key value)
  8921. (save-match-data
  8922. (when (string-match "{{\\([^}]+\\)}}$" s)
  8923. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  8924. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  8925. (setq key (match-string 1 a) value (match-string 2 a)
  8926. start (match-end 0)
  8927. attr (plist-put attr (intern key) value))))
  8928. (org-add-props s nil 'org-attr attr))
  8929. s))
  8930. (defun org-extract-attributes-from-string (tag)
  8931. (let (key value attr)
  8932. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  8933. (setq key (match-string 1 tag) value (match-string 2 tag)
  8934. tag (replace-match "" t t tag)
  8935. attr (plist-put attr (intern key) value)))
  8936. (cons tag attr)))
  8937. (defun org-attributes-to-string (plist)
  8938. "Format a property list into an HTML attribute list."
  8939. (let ((s "") key value)
  8940. (while plist
  8941. (setq key (pop plist) value (pop plist))
  8942. (and value
  8943. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  8944. s))
  8945. ;;; Opening/following a link
  8946. (defvar org-link-search-failed nil)
  8947. (defvar org-open-link-functions nil
  8948. "Hook for functions finding a plain text link.
  8949. These functions must take a single argument, the link content.
  8950. They will be called for links that look like [[link text][description]]
  8951. when LINK TEXT does not have a protocol like \"http:\" and does not look
  8952. like a filename (e.g. \"./blue.png\").
  8953. These functions will be called *before* Org attempts to resolve the
  8954. link by doing text searches in the current buffer - so if you want a
  8955. link \"[[target]]\" to still find \"<<target>>\", your function should
  8956. handle this as a special case.
  8957. When the function does handle the link, it must return a non-nil value.
  8958. If it decides that it is not responsible for this link, it must return
  8959. nil to indicate that that Org-mode can continue with other options
  8960. like exact and fuzzy text search.")
  8961. (defun org-next-link (&optional search-backward)
  8962. "Move forward to the next link.
  8963. If the link is in hidden text, expose it."
  8964. (interactive "P")
  8965. (when (and org-link-search-failed (eq this-command last-command))
  8966. (goto-char (point-min))
  8967. (message "Link search wrapped back to beginning of buffer"))
  8968. (setq org-link-search-failed nil)
  8969. (let* ((pos (point))
  8970. (ct (org-context))
  8971. (a (assoc :link ct))
  8972. (srch-fun (if search-backward 're-search-backward 're-search-forward)))
  8973. (cond (a (goto-char (nth (if search-backward 1 2) a)))
  8974. ((looking-at org-any-link-re)
  8975. ;; Don't stay stuck at link without an org-link face
  8976. (forward-char (if search-backward -1 1))))
  8977. (if (funcall srch-fun org-any-link-re nil t)
  8978. (progn
  8979. (goto-char (match-beginning 0))
  8980. (if (outline-invisible-p) (org-show-context)))
  8981. (goto-char pos)
  8982. (setq org-link-search-failed t)
  8983. (message "No further link found"))))
  8984. (defun org-previous-link ()
  8985. "Move backward to the previous link.
  8986. If the link is in hidden text, expose it."
  8987. (interactive)
  8988. (funcall 'org-next-link t))
  8989. (defun org-translate-link (s)
  8990. "Translate a link string if a translation function has been defined."
  8991. (if (and org-link-translation-function
  8992. (fboundp org-link-translation-function)
  8993. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  8994. (progn
  8995. (setq s (funcall org-link-translation-function
  8996. (match-string 1 s) (match-string 2 s)))
  8997. (concat (car s) ":" (cdr s)))
  8998. s))
  8999. (defun org-translate-link-from-planner (type path)
  9000. "Translate a link from Emacs Planner syntax so that Org can follow it.
  9001. This is still an experimental function, your mileage may vary."
  9002. (cond
  9003. ((member type '("http" "https" "news" "ftp"))
  9004. ;; standard Internet links are the same.
  9005. nil)
  9006. ((and (equal type "irc") (string-match "^//" path))
  9007. ;; Planner has two / at the beginning of an irc link, we have 1.
  9008. ;; We should have zero, actually....
  9009. (setq path (substring path 1)))
  9010. ((and (equal type "lisp") (string-match "^/" path))
  9011. ;; Planner has a slash, we do not.
  9012. (setq type "elisp" path (substring path 1)))
  9013. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  9014. ;; A typical message link. Planner has the id after the final slash,
  9015. ;; we separate it with a hash mark
  9016. (setq path (concat (match-string 1 path) "#"
  9017. (org-remove-angle-brackets (match-string 2 path)))))
  9018. )
  9019. (cons type path))
  9020. (defun org-find-file-at-mouse (ev)
  9021. "Open file link or URL at mouse."
  9022. (interactive "e")
  9023. (mouse-set-point ev)
  9024. (org-open-at-point 'in-emacs))
  9025. (defun org-open-at-mouse (ev)
  9026. "Open file link or URL at mouse.
  9027. See the docstring of `org-open-file' for details."
  9028. (interactive "e")
  9029. (mouse-set-point ev)
  9030. (if (eq major-mode 'org-agenda-mode)
  9031. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  9032. (org-open-at-point))
  9033. (defvar org-window-config-before-follow-link nil
  9034. "The window configuration before following a link.
  9035. This is saved in case the need arises to restore it.")
  9036. (defvar org-open-link-marker (make-marker)
  9037. "Marker pointing to the location where `org-open-at-point; was called.")
  9038. ;;;###autoload
  9039. (defun org-open-at-point-global ()
  9040. "Follow a link like Org-mode does.
  9041. This command can be called in any mode to follow a link that has
  9042. Org-mode syntax."
  9043. (interactive)
  9044. (org-run-like-in-org-mode 'org-open-at-point))
  9045. ;;;###autoload
  9046. (defun org-open-link-from-string (s &optional arg reference-buffer)
  9047. "Open a link in the string S, as if it was in Org-mode."
  9048. (interactive "sLink: \nP")
  9049. (let ((reference-buffer (or reference-buffer (current-buffer))))
  9050. (with-temp-buffer
  9051. (let ((org-inhibit-startup (not reference-buffer)))
  9052. (org-mode)
  9053. (insert s)
  9054. (goto-char (point-min))
  9055. (when reference-buffer
  9056. (setq org-link-abbrev-alist-local
  9057. (with-current-buffer reference-buffer
  9058. org-link-abbrev-alist-local)))
  9059. (org-open-at-point arg reference-buffer)))))
  9060. (defvar org-open-at-point-functions nil
  9061. "Hook that is run when following a link at point.
  9062. Functions in this hook must return t if they identify and follow
  9063. a link at point. If they don't find anything interesting at point,
  9064. they must return nil.")
  9065. (defvar clean-buffer-list-kill-buffer-names) ; Defined in midnight.el
  9066. (defun org-open-at-point (&optional arg reference-buffer)
  9067. "Open link at or after point.
  9068. If there is no link at point, this function will search forward up to
  9069. the end of the current line.
  9070. Normally, files will be opened by an appropriate application. If the
  9071. optional prefix argument ARG is non-nil, Emacs will visit the file.
  9072. With a double prefix argument, try to open outside of Emacs, in the
  9073. application the system uses for this file type."
  9074. (interactive "P")
  9075. ;; if in a code block, then open the block's results
  9076. (unless (call-interactively #'org-babel-open-src-block-result)
  9077. (org-load-modules-maybe)
  9078. (move-marker org-open-link-marker (point))
  9079. (setq org-window-config-before-follow-link (current-window-configuration))
  9080. (org-remove-occur-highlights nil nil t)
  9081. (cond
  9082. ((and (org-at-heading-p)
  9083. (not (org-at-timestamp-p t))
  9084. (not (org-in-regexp
  9085. (concat org-plain-link-re "\\|"
  9086. org-bracket-link-regexp "\\|"
  9087. org-angle-link-re "\\|"
  9088. "[ \t]:[^ \t\n]+:[ \t]*$")))
  9089. (not (get-text-property (point) 'org-linked-text)))
  9090. (or (let* ((lkall (org-offer-links-in-entry (current-buffer) (point) arg))
  9091. (lk0 (car lkall))
  9092. (lk (if (stringp lk0) (list lk0) lk0))
  9093. (lkend (cdr lkall)))
  9094. (mapcar (lambda(l)
  9095. (search-forward l nil lkend)
  9096. (goto-char (match-beginning 0))
  9097. (org-open-at-point))
  9098. lk))
  9099. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  9100. ((run-hook-with-args-until-success 'org-open-at-point-functions))
  9101. ((and (org-at-timestamp-p t)
  9102. (not (org-in-regexp org-bracket-link-regexp)))
  9103. (org-follow-timestamp-link))
  9104. ((and (or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  9105. (not (org-in-regexp org-any-link-re)))
  9106. (org-footnote-action))
  9107. (t
  9108. (let (type path link line search (pos (point)))
  9109. (catch 'match
  9110. (save-excursion
  9111. (skip-chars-forward "^]\n\r")
  9112. (when (org-in-regexp org-bracket-link-regexp 1)
  9113. (setq link (org-extract-attributes
  9114. (org-link-unescape (org-match-string-no-properties 1))))
  9115. (while (string-match " *\n *" link)
  9116. (setq link (replace-match " " t t link)))
  9117. (setq link (org-link-expand-abbrev link))
  9118. (cond
  9119. ((or (file-name-absolute-p link)
  9120. (string-match "^\\.\\.?/" link))
  9121. (setq type "file" path link))
  9122. ((string-match org-link-re-with-space3 link)
  9123. (setq type (match-string 1 link) path (match-string 2 link)))
  9124. ((string-match "^help:+\\(.+\\)" link)
  9125. (setq type "help" path (match-string 1 link)))
  9126. (t (setq type "thisfile" path link)))
  9127. (throw 'match t)))
  9128. (when (get-text-property (point) 'org-linked-text)
  9129. (setq type "thisfile"
  9130. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  9131. (1+ (point)) (point))
  9132. path (buffer-substring
  9133. (or (previous-single-property-change pos 'org-linked-text)
  9134. (point-min))
  9135. (or (next-single-property-change pos 'org-linked-text)
  9136. (point-max))))
  9137. (throw 'match t))
  9138. (save-excursion
  9139. (when (or (org-in-regexp org-angle-link-re)
  9140. (let ((match (org-in-regexp org-plain-link-re)))
  9141. ;; Check a plain link is not within a bracket link
  9142. (and match
  9143. (save-excursion
  9144. (progn
  9145. (goto-char (car match))
  9146. (not (org-in-regexp org-bracket-link-regexp))))))
  9147. (let ((line_ending (save-excursion (end-of-line) (point))))
  9148. ;; We are in a line before a plain or bracket link
  9149. (or (re-search-forward org-plain-link-re line_ending t)
  9150. (re-search-forward org-bracket-link-regexp line_ending t))))
  9151. (setq type (match-string 1)
  9152. path (org-link-unescape (match-string 2)))
  9153. (throw 'match t)))
  9154. (save-excursion
  9155. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
  9156. (setq type "tags"
  9157. path (match-string 1))
  9158. (while (string-match ":" path)
  9159. (setq path (replace-match "+" t t path)))
  9160. (throw 'match t)))
  9161. (when (org-in-regexp "<\\([^><\n]+\\)>")
  9162. (setq type "tree-match"
  9163. path (match-string 1))
  9164. (throw 'match t)))
  9165. (unless path
  9166. (user-error "No link found"))
  9167. ;; switch back to reference buffer
  9168. ;; needed when if called in a temporary buffer through
  9169. ;; org-open-link-from-string
  9170. (with-current-buffer (or reference-buffer (current-buffer))
  9171. ;; Remove any trailing spaces in path
  9172. (if (string-match " +\\'" path)
  9173. (setq path (replace-match "" t t path)))
  9174. (if (and org-link-translation-function
  9175. (fboundp org-link-translation-function))
  9176. ;; Check if we need to translate the link
  9177. (let ((tmp (funcall org-link-translation-function type path)))
  9178. (setq type (car tmp) path (cdr tmp))))
  9179. (cond
  9180. ((assoc type org-link-protocols)
  9181. (funcall (nth 1 (assoc type org-link-protocols)) path))
  9182. ((equal type "help")
  9183. (let ((f-or-v (intern path)))
  9184. (cond ((fboundp f-or-v)
  9185. (describe-function f-or-v))
  9186. ((boundp f-or-v)
  9187. (describe-variable f-or-v))
  9188. (t (error "Not a known function or variable")))))
  9189. ((equal type "mailto")
  9190. (let ((cmd (car org-link-mailto-program))
  9191. (args (cdr org-link-mailto-program)) args1
  9192. (address path) (subject "") a)
  9193. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  9194. (setq address (match-string 1 path)
  9195. subject (org-link-escape (match-string 2 path))))
  9196. (while args
  9197. (cond
  9198. ((not (stringp (car args))) (push (pop args) args1))
  9199. (t (setq a (pop args))
  9200. (if (string-match "%a" a)
  9201. (setq a (replace-match address t t a)))
  9202. (if (string-match "%s" a)
  9203. (setq a (replace-match subject t t a)))
  9204. (push a args1))))
  9205. (apply cmd (nreverse args1))))
  9206. ((member type '("http" "https" "ftp" "news"))
  9207. (browse-url (concat type ":" (if (org-string-match-p "[[:nonascii:] ]" path)
  9208. (org-link-escape
  9209. path org-link-escape-chars-browser)
  9210. path))))
  9211. ((string= type "doi")
  9212. (browse-url (concat org-doi-server-url (if (org-string-match-p "[[:nonascii:] ]" path)
  9213. (org-link-escape
  9214. path org-link-escape-chars-browser)
  9215. path))))
  9216. ((member type '("message"))
  9217. (browse-url (concat type ":" path)))
  9218. ((string= type "tags")
  9219. (org-tags-view arg path))
  9220. ((string= type "tree-match")
  9221. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  9222. ((string= type "file")
  9223. (if (string-match "::\\([0-9]+\\)\\'" path)
  9224. (setq line (string-to-number (match-string 1 path))
  9225. path (substring path 0 (match-beginning 0)))
  9226. (if (string-match "::\\(.+\\)\\'" path)
  9227. (setq search (match-string 1 path)
  9228. path (substring path 0 (match-beginning 0)))))
  9229. (if (string-match "[*?{]" (file-name-nondirectory path))
  9230. (dired path)
  9231. (org-open-file path arg line search)))
  9232. ((string= type "shell")
  9233. (let ((buf (generate-new-buffer "*Org Shell Output"))
  9234. (cmd path))
  9235. (if (or (and (not (string= org-confirm-shell-link-not-regexp ""))
  9236. (string-match org-confirm-shell-link-not-regexp cmd))
  9237. (not org-confirm-shell-link-function)
  9238. (funcall org-confirm-shell-link-function
  9239. (format "Execute \"%s\" in shell? "
  9240. (org-add-props cmd nil
  9241. 'face 'org-warning))))
  9242. (progn
  9243. (message "Executing %s" cmd)
  9244. (shell-command cmd buf)
  9245. (if (featurep 'midnight)
  9246. (setq clean-buffer-list-kill-buffer-names
  9247. (cons buf clean-buffer-list-kill-buffer-names))))
  9248. (error "Abort"))))
  9249. ((string= type "elisp")
  9250. (let ((cmd path))
  9251. (if (or (and (not (string= org-confirm-elisp-link-not-regexp ""))
  9252. (string-match org-confirm-elisp-link-not-regexp cmd))
  9253. (not org-confirm-elisp-link-function)
  9254. (funcall org-confirm-elisp-link-function
  9255. (format "Execute \"%s\" as elisp? "
  9256. (org-add-props cmd nil
  9257. 'face 'org-warning))))
  9258. (message "%s => %s" cmd
  9259. (if (equal (string-to-char cmd) ?\()
  9260. (eval (read cmd))
  9261. (call-interactively (read cmd))))
  9262. (error "Abort"))))
  9263. ((and (string= type "thisfile")
  9264. (run-hook-with-args-until-success
  9265. 'org-open-link-functions path)))
  9266. ((string= type "thisfile")
  9267. (if arg
  9268. (switch-to-buffer-other-window
  9269. (org-get-buffer-for-internal-link (current-buffer)))
  9270. (org-mark-ring-push))
  9271. (let ((cmd `(org-link-search
  9272. ,path
  9273. ,(cond ((equal arg '(4)) ''occur)
  9274. ((equal arg '(16)) ''org-occur))
  9275. ,pos)))
  9276. (condition-case nil (let ((org-link-search-inhibit-query t))
  9277. (eval cmd))
  9278. (error (progn (widen) (eval cmd))))))
  9279. (t (browse-url-at-point)))))))
  9280. (move-marker org-open-link-marker nil)
  9281. (run-hook-with-args 'org-follow-link-hook)))
  9282. (defun org-offer-links-in-entry (buffer marker &optional nth zero)
  9283. "Offer links in the current entry and return the selected link.
  9284. If there is only one link, return it.
  9285. If NTH is an integer, return the NTH link found.
  9286. If ZERO is a string, check also this string for a link, and if
  9287. there is one, return it."
  9288. (with-current-buffer buffer
  9289. (save-excursion
  9290. (save-restriction
  9291. (widen)
  9292. (goto-char marker)
  9293. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  9294. "\\(" org-angle-link-re "\\)\\|"
  9295. "\\(" org-plain-link-re "\\)"))
  9296. (cnt ?0)
  9297. (in-emacs (if (integerp nth) nil nth))
  9298. have-zero end links link c)
  9299. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  9300. (push (match-string 0 zero) links)
  9301. (setq cnt (1- cnt) have-zero t))
  9302. (save-excursion
  9303. (org-back-to-heading t)
  9304. (setq end (save-excursion (outline-next-heading) (point)))
  9305. (while (re-search-forward re end t)
  9306. (push (match-string 0) links))
  9307. (setq links (org-uniquify (reverse links))))
  9308. (cond
  9309. ((null links)
  9310. (message "No links"))
  9311. ((equal (length links) 1)
  9312. (setq link (car links)))
  9313. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  9314. (setq link (nth (if have-zero nth (1- nth)) links)))
  9315. (t ; we have to select a link
  9316. (save-excursion
  9317. (save-window-excursion
  9318. (delete-other-windows)
  9319. (with-output-to-temp-buffer "*Select Link*"
  9320. (mapc (lambda (l)
  9321. (if (not (string-match org-bracket-link-regexp l))
  9322. (princ (format "[%c] %s\n" (incf cnt)
  9323. (org-remove-angle-brackets l)))
  9324. (if (match-end 3)
  9325. (princ (format "[%c] %s (%s)\n" (incf cnt)
  9326. (match-string 3 l) (match-string 1 l)))
  9327. (princ (format "[%c] %s\n" (incf cnt)
  9328. (match-string 1 l))))))
  9329. links))
  9330. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  9331. (message "Select link to open, RET to open all:")
  9332. (setq c (read-char-exclusive))
  9333. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  9334. (when (equal c ?q) (error "Abort"))
  9335. (if (equal c ?\C-m)
  9336. (setq link links)
  9337. (setq nth (- c ?0))
  9338. (if have-zero (setq nth (1+ nth)))
  9339. (unless (and (integerp nth) (>= (length links) nth))
  9340. (error "Invalid link selection"))
  9341. (setq link (nth (1- nth) links)))))
  9342. (cons link end))))))
  9343. ;; Add special file links that specify the way of opening
  9344. (org-add-link-type "file+sys" 'org-open-file-with-system)
  9345. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  9346. (defun org-open-file-with-system (path)
  9347. "Open file at PATH using the system way of opening it."
  9348. (org-open-file path 'system))
  9349. (defun org-open-file-with-emacs (path)
  9350. "Open file at PATH in Emacs."
  9351. (org-open-file path 'emacs))
  9352. ;;; File search
  9353. (defvar org-create-file-search-functions nil
  9354. "List of functions to construct the right search string for a file link.
  9355. These functions are called in turn with point at the location to
  9356. which the link should point.
  9357. A function in the hook should first test if it would like to
  9358. handle this file type, for example by checking the `major-mode'
  9359. or the file extension. If it decides not to handle this file, it
  9360. should just return nil to give other functions a chance. If it
  9361. does handle the file, it must return the search string to be used
  9362. when following the link. The search string will be part of the
  9363. file link, given after a double colon, and `org-open-at-point'
  9364. will automatically search for it. If special measures must be
  9365. taken to make the search successful, another function should be
  9366. added to the companion hook `org-execute-file-search-functions',
  9367. which see.
  9368. A function in this hook may also use `setq' to set the variable
  9369. `description' to provide a suggestion for the descriptive text to
  9370. be used for this link when it gets inserted into an Org-mode
  9371. buffer with \\[org-insert-link].")
  9372. (defvar org-execute-file-search-functions nil
  9373. "List of functions to execute a file search triggered by a link.
  9374. Functions added to this hook must accept a single argument, the
  9375. search string that was part of the file link, the part after the
  9376. double colon. The function must first check if it would like to
  9377. handle this search, for example by checking the `major-mode' or
  9378. the file extension. If it decides not to handle this search, it
  9379. should just return nil to give other functions a chance. If it
  9380. does handle the search, it must return a non-nil value to keep
  9381. other functions from trying.
  9382. Each function can access the current prefix argument through the
  9383. variable `current-prefix-argument'. Note that a single prefix is
  9384. used to force opening a link in Emacs, so it may be good to only
  9385. use a numeric or double prefix to guide the search function.
  9386. In case this is needed, a function in this hook can also restore
  9387. the window configuration before `org-open-at-point' was called using:
  9388. (set-window-configuration org-window-config-before-follow-link)")
  9389. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  9390. (defun org-link-search (s &optional type avoid-pos stealth)
  9391. "Search for a link search option.
  9392. If S is surrounded by forward slashes, it is interpreted as a
  9393. regular expression. In org-mode files, this will create an `org-occur'
  9394. sparse tree. In ordinary files, `occur' will be used to list matches.
  9395. If the current buffer is in `dired-mode', grep will be used to search
  9396. in all files. If AVOID-POS is given, ignore matches near that position.
  9397. When optional argument STEALTH is non-nil, do not modify
  9398. visibility around point, thus ignoring
  9399. `org-show-hierarchy-above', `org-show-following-heading' and
  9400. `org-show-siblings' variables."
  9401. (let ((case-fold-search t)
  9402. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  9403. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  9404. (append '(("") (" ") ("\t") ("\n"))
  9405. org-emphasis-alist)
  9406. "\\|") "\\)"))
  9407. (pos (point))
  9408. (pre nil) (post nil)
  9409. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  9410. (cond
  9411. ;; First check if there are any special search functions
  9412. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  9413. ;; Now try the builtin stuff
  9414. ((and (equal (string-to-char s0) ?#)
  9415. (> (length s0) 1)
  9416. (save-excursion
  9417. (goto-char (point-min))
  9418. (and
  9419. (re-search-forward
  9420. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  9421. (setq type 'dedicated
  9422. pos (match-beginning 0))))
  9423. ;; There is an exact target for this
  9424. (goto-char pos)
  9425. (org-back-to-heading t)))
  9426. ((save-excursion
  9427. (goto-char (point-min))
  9428. (and
  9429. (re-search-forward
  9430. (concat "<<" (regexp-quote s0) ">>") nil t)
  9431. (setq type 'dedicated
  9432. pos (match-beginning 0))))
  9433. ;; There is an exact target for this
  9434. (goto-char pos))
  9435. ((save-excursion
  9436. (goto-char (point-min))
  9437. (and
  9438. (re-search-forward
  9439. (format "^[ \t]*#\\+TARGET: %s" (regexp-quote s0)) nil t)
  9440. (setq type 'dedicated pos (match-beginning 0))))
  9441. ;; Found an invisible target.
  9442. (goto-char pos))
  9443. ((save-excursion
  9444. (goto-char (point-min))
  9445. (and
  9446. (re-search-forward
  9447. (format "^[ \t]*#\\+NAME: %s" (regexp-quote s0)) nil t)
  9448. (setq type 'dedicated pos (match-beginning 0))))
  9449. ;; Found an element with a matching #+name affiliated keyword.
  9450. (goto-char pos))
  9451. ((and (string-match "^(\\(.*\\))$" s0)
  9452. (save-excursion
  9453. (goto-char (point-min))
  9454. (and
  9455. (re-search-forward
  9456. (concat "[^[]" (regexp-quote
  9457. (format org-coderef-label-format
  9458. (match-string 1 s0))))
  9459. nil t)
  9460. (setq type 'dedicated
  9461. pos (1+ (match-beginning 0))))))
  9462. ;; There is a coderef target for this
  9463. (goto-char pos))
  9464. ((string-match "^/\\(.*\\)/$" s)
  9465. ;; A regular expression
  9466. (cond
  9467. ((derived-mode-p 'org-mode)
  9468. (org-occur (match-string 1 s)))
  9469. (t (org-do-occur (match-string 1 s)))))
  9470. ((and (derived-mode-p 'org-mode) org-link-search-must-match-exact-headline)
  9471. (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  9472. (goto-char (point-min))
  9473. (cond
  9474. ((let (case-fold-search)
  9475. (re-search-forward (format org-complex-heading-regexp-format
  9476. (regexp-quote s))
  9477. nil t))
  9478. ;; OK, found a match
  9479. (setq type 'dedicated)
  9480. (goto-char (match-beginning 0)))
  9481. ((and (not org-link-search-inhibit-query)
  9482. (eq org-link-search-must-match-exact-headline 'query-to-create)
  9483. (y-or-n-p "No match - create this as a new heading? "))
  9484. (goto-char (point-max))
  9485. (or (bolp) (newline))
  9486. (insert "* " s "\n")
  9487. (beginning-of-line 0))
  9488. (t
  9489. (goto-char pos)
  9490. (error "No match"))))
  9491. (t
  9492. ;; A normal search string
  9493. (when (equal (string-to-char s) ?*)
  9494. ;; Anchor on headlines, post may include tags.
  9495. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  9496. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
  9497. s (substring s 1)))
  9498. (remove-text-properties
  9499. 0 (length s)
  9500. '(face nil mouse-face nil keymap nil fontified nil) s)
  9501. ;; Make a series of regular expressions to find a match
  9502. (setq words (org-split-string s "[ \n\r\t]+")
  9503. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  9504. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  9505. "\\)" markers)
  9506. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  9507. re2a (concat "[ \t\r\n]" re2a_)
  9508. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  9509. re4 (concat "[^a-zA-Z_]" re4_)
  9510. re1 (concat pre re2 post)
  9511. re3 (concat pre (if pre re4_ re4) post)
  9512. re5 (concat pre ".*" re4)
  9513. re2 (concat pre re2)
  9514. re2a (concat pre (if pre re2a_ re2a))
  9515. re4 (concat pre (if pre re4_ re4))
  9516. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  9517. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  9518. re5 "\\)"
  9519. ))
  9520. (cond
  9521. ((eq type 'org-occur) (org-occur reall))
  9522. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  9523. (t (goto-char (point-min))
  9524. (setq type 'fuzzy)
  9525. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  9526. (org-search-not-self 1 re1 nil t)
  9527. (org-search-not-self 1 re2 nil t)
  9528. (org-search-not-self 1 re2a nil t)
  9529. (org-search-not-self 1 re3 nil t)
  9530. (org-search-not-self 1 re4 nil t)
  9531. (org-search-not-self 1 re5 nil t)
  9532. )
  9533. (goto-char (match-beginning 1))
  9534. (goto-char pos)
  9535. (error "No match"))))))
  9536. (and (derived-mode-p 'org-mode)
  9537. (not stealth)
  9538. (org-show-context 'link-search))
  9539. type))
  9540. (defun org-search-not-self (group &rest args)
  9541. "Execute `re-search-forward', but only accept matches that do not
  9542. enclose the position of `org-open-link-marker'."
  9543. (let ((m org-open-link-marker))
  9544. (catch 'exit
  9545. (while (apply 're-search-forward args)
  9546. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  9547. (goto-char (match-end group))
  9548. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  9549. (> (match-beginning 0) (marker-position m))
  9550. (< (match-end 0) (marker-position m)))
  9551. (save-match-data
  9552. (or (not (org-in-regexp
  9553. org-bracket-link-analytic-regexp 1))
  9554. (not (match-end 4)) ; no description
  9555. (and (<= (match-beginning 4) (point))
  9556. (>= (match-end 4) (point))))))
  9557. (throw 'exit (point))))))))
  9558. (defun org-get-buffer-for-internal-link (buffer)
  9559. "Return a buffer to be used for displaying the link target of internal links."
  9560. (cond
  9561. ((not org-display-internal-link-with-indirect-buffer)
  9562. buffer)
  9563. ((string-match "(Clone)$" (buffer-name buffer))
  9564. (message "Buffer is already a clone, not making another one")
  9565. ;; we also do not modify visibility in this case
  9566. buffer)
  9567. (t ; make a new indirect buffer for displaying the link
  9568. (let* ((bn (buffer-name buffer))
  9569. (ibn (concat bn "(Clone)"))
  9570. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  9571. (with-current-buffer ib (org-overview))
  9572. ib))))
  9573. (defun org-do-occur (regexp &optional cleanup)
  9574. "Call the Emacs command `occur'.
  9575. If CLEANUP is non-nil, remove the printout of the regular expression
  9576. in the *Occur* buffer. This is useful if the regex is long and not useful
  9577. to read."
  9578. (occur regexp)
  9579. (when cleanup
  9580. (let ((cwin (selected-window)) win beg end)
  9581. (when (setq win (get-buffer-window "*Occur*"))
  9582. (select-window win))
  9583. (goto-char (point-min))
  9584. (when (re-search-forward "match[a-z]+" nil t)
  9585. (setq beg (match-end 0))
  9586. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  9587. (setq end (1- (match-beginning 0)))))
  9588. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  9589. (goto-char (point-min))
  9590. (select-window cwin))))
  9591. ;;; The mark ring for links jumps
  9592. (defvar org-mark-ring nil
  9593. "Mark ring for positions before jumps in Org-mode.")
  9594. (defvar org-mark-ring-last-goto nil
  9595. "Last position in the mark ring used to go back.")
  9596. ;; Fill and close the ring
  9597. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  9598. (loop for i from 1 to org-mark-ring-length do
  9599. (push (make-marker) org-mark-ring))
  9600. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  9601. org-mark-ring)
  9602. (defun org-mark-ring-push (&optional pos buffer)
  9603. "Put the current position or POS into the mark ring and rotate it."
  9604. (interactive)
  9605. (setq pos (or pos (point)))
  9606. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  9607. (move-marker (car org-mark-ring)
  9608. (or pos (point))
  9609. (or buffer (current-buffer)))
  9610. (message "%s"
  9611. (substitute-command-keys
  9612. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  9613. (defun org-mark-ring-goto (&optional n)
  9614. "Jump to the previous position in the mark ring.
  9615. With prefix arg N, jump back that many stored positions. When
  9616. called several times in succession, walk through the entire ring.
  9617. Org-mode commands jumping to a different position in the current file,
  9618. or to another Org-mode file, automatically push the old position
  9619. onto the ring."
  9620. (interactive "p")
  9621. (let (p m)
  9622. (if (eq last-command this-command)
  9623. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  9624. (setq p org-mark-ring))
  9625. (setq org-mark-ring-last-goto p)
  9626. (setq m (car p))
  9627. (org-pop-to-buffer-same-window (marker-buffer m))
  9628. (goto-char m)
  9629. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  9630. (defun org-remove-angle-brackets (s)
  9631. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  9632. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  9633. s)
  9634. (defun org-add-angle-brackets (s)
  9635. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  9636. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  9637. s)
  9638. (defun org-remove-double-quotes (s)
  9639. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  9640. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  9641. s)
  9642. ;;; Following specific links
  9643. (defun org-follow-timestamp-link ()
  9644. "Open an agenda view for the time-stamp date/range at point."
  9645. (cond
  9646. ((org-at-date-range-p t)
  9647. (let ((org-agenda-start-on-weekday)
  9648. (t1 (match-string 1))
  9649. (t2 (match-string 2)) tt1 tt2)
  9650. (setq tt1 (time-to-days (org-time-string-to-time t1))
  9651. tt2 (time-to-days (org-time-string-to-time t2)))
  9652. (let ((org-agenda-buffer-tmp-name
  9653. (format "*Org Agenda(a:%s)"
  9654. (concat (substring t1 0 10) "--" (substring t2 0 10)))))
  9655. (org-agenda-list nil tt1 (1+ (- tt2 tt1))))))
  9656. ((org-at-timestamp-p t)
  9657. (let ((org-agenda-buffer-tmp-name
  9658. (format "*Org Agenda(a:%s)" (substring (match-string 1) 0 10))))
  9659. (org-agenda-list nil (time-to-days (org-time-string-to-time
  9660. (substring (match-string 1) 0 10)))
  9661. 1)))
  9662. (t (error "This should not happen"))))
  9663. ;;; Following file links
  9664. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  9665. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  9666. (declare-function mailcap-mime-info
  9667. "mailcap" (string &optional request no-decode))
  9668. (defvar org-wait nil)
  9669. (defun org-open-file (path &optional in-emacs line search)
  9670. "Open the file at PATH.
  9671. First, this expands any special file name abbreviations. Then the
  9672. configuration variable `org-file-apps' is checked if it contains an
  9673. entry for this file type, and if yes, the corresponding command is launched.
  9674. If no application is found, Emacs simply visits the file.
  9675. With optional prefix argument IN-EMACS, Emacs will visit the file.
  9676. With a double \\[universal-argument] \\[universal-argument] \
  9677. prefix arg, Org tries to avoid opening in Emacs
  9678. and to use an external application to visit the file.
  9679. Optional LINE specifies a line to go to, optional SEARCH a string
  9680. to search for. If LINE or SEARCH is given, the file will be
  9681. opened in Emacs, unless an entry from org-file-apps that makes
  9682. use of groups in a regexp matches.
  9683. If you want to change the way frames are used when following a
  9684. link, please customize `org-link-frame-setup'.
  9685. If the file does not exist, an error is thrown."
  9686. (let* ((file (if (equal path "")
  9687. buffer-file-name
  9688. (substitute-in-file-name (expand-file-name path))))
  9689. (file-apps (append org-file-apps (org-default-apps)))
  9690. (apps (org-remove-if
  9691. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9692. (apps-dlink (org-remove-if-not
  9693. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9694. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  9695. (dirp (if remp nil (file-directory-p file)))
  9696. (file (if (and dirp org-open-directory-means-index-dot-org)
  9697. (concat (file-name-as-directory file) "index.org")
  9698. file))
  9699. (a-m-a-p (assq 'auto-mode apps))
  9700. (dfile (downcase file))
  9701. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  9702. (link (cond ((and (eq line nil)
  9703. (eq search nil))
  9704. file)
  9705. (line
  9706. (concat file "::" (number-to-string line)))
  9707. (search
  9708. (concat file "::" search))))
  9709. (dlink (downcase link))
  9710. (old-buffer (current-buffer))
  9711. (old-pos (point))
  9712. (old-mode major-mode)
  9713. ext cmd link-match-data)
  9714. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  9715. (setq ext (match-string 1 dfile))
  9716. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  9717. (setq ext (match-string 1 dfile))))
  9718. (cond
  9719. ((member in-emacs '((16) system))
  9720. (setq cmd (cdr (assoc 'system apps))))
  9721. (in-emacs (setq cmd 'emacs))
  9722. (t
  9723. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  9724. (and dirp (cdr (assoc 'directory apps)))
  9725. ; first, try matching against apps-dlink
  9726. ; if we get a match here, store the match data for later
  9727. (let ((match (assoc-default dlink apps-dlink
  9728. 'string-match)))
  9729. (if match
  9730. (progn (setq link-match-data (match-data))
  9731. match)
  9732. (progn (setq in-emacs (or in-emacs line search))
  9733. nil))) ; if we have no match in apps-dlink,
  9734. ; always open the file in emacs if line or search
  9735. ; is given (for backwards compatibility)
  9736. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  9737. 'string-match)
  9738. (cdr (assoc ext apps))
  9739. (cdr (assoc t apps))))))
  9740. (when (eq cmd 'system)
  9741. (setq cmd (cdr (assoc 'system apps))))
  9742. (when (eq cmd 'default)
  9743. (setq cmd (cdr (assoc t apps))))
  9744. (when (eq cmd 'mailcap)
  9745. (require 'mailcap)
  9746. (mailcap-parse-mailcaps)
  9747. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  9748. (command (mailcap-mime-info mime-type)))
  9749. (if (stringp command)
  9750. (setq cmd command)
  9751. (setq cmd 'emacs))))
  9752. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  9753. (not (file-exists-p file))
  9754. (not org-open-non-existing-files))
  9755. (error "No such file: %s" file))
  9756. (cond
  9757. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  9758. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  9759. (while (string-match "['\"]%s['\"]" cmd)
  9760. (setq cmd (replace-match "%s" t t cmd)))
  9761. (while (string-match "%s" cmd)
  9762. (setq cmd (replace-match
  9763. (save-match-data
  9764. (shell-quote-argument
  9765. (convert-standard-filename file)))
  9766. t t cmd)))
  9767. ;; Replace "%1", "%2" etc. in command with group matches from regex
  9768. (save-match-data
  9769. (let ((match-index 1)
  9770. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  9771. (set-match-data link-match-data)
  9772. (while (<= match-index number-of-groups)
  9773. (let ((regex (concat "%" (number-to-string match-index)))
  9774. (replace-with (match-string match-index dlink)))
  9775. (while (string-match regex cmd)
  9776. (setq cmd (replace-match replace-with t t cmd))))
  9777. (setq match-index (+ match-index 1)))))
  9778. (save-window-excursion
  9779. (message "Running %s...done" cmd)
  9780. (start-process-shell-command cmd nil cmd)
  9781. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))))
  9782. ((or (stringp cmd)
  9783. (eq cmd 'emacs))
  9784. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  9785. (widen)
  9786. (if line (org-goto-line line)
  9787. (if search (org-link-search search))))
  9788. ((consp cmd)
  9789. (let ((file (convert-standard-filename file)))
  9790. (save-match-data
  9791. (set-match-data link-match-data)
  9792. (eval cmd))))
  9793. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  9794. (and (derived-mode-p 'org-mode) (eq old-mode 'org-mode)
  9795. (or (not (equal old-buffer (current-buffer)))
  9796. (not (equal old-pos (point))))
  9797. (org-mark-ring-push old-pos old-buffer))))
  9798. (defun org-file-apps-entry-match-against-dlink-p (entry)
  9799. "This function returns non-nil if `entry' uses a regular
  9800. expression which should be matched against the whole link by
  9801. org-open-file.
  9802. It assumes that is the case when the entry uses a regular
  9803. expression which has at least one grouping construct and the
  9804. action is either a lisp form or a command string containing
  9805. '%1', i.e. using at least one subexpression match as a
  9806. parameter."
  9807. (let ((selector (car entry))
  9808. (action (cdr entry)))
  9809. (if (stringp selector)
  9810. (and (> (regexp-opt-depth selector) 0)
  9811. (or (and (stringp action)
  9812. (string-match "%[0-9]" action))
  9813. (consp action)))
  9814. nil)))
  9815. (defun org-default-apps ()
  9816. "Return the default applications for this operating system."
  9817. (cond
  9818. ((eq system-type 'darwin)
  9819. org-file-apps-defaults-macosx)
  9820. ((eq system-type 'windows-nt)
  9821. org-file-apps-defaults-windowsnt)
  9822. (t org-file-apps-defaults-gnu)))
  9823. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  9824. "Convert extensions to regular expressions in the cars of LIST.
  9825. Also, weed out any non-string entries, because the return value is used
  9826. only for regexp matching.
  9827. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  9828. point to the symbol `emacs', indicating that the file should
  9829. be opened in Emacs."
  9830. (append
  9831. (delq nil
  9832. (mapcar (lambda (x)
  9833. (if (not (stringp (car x)))
  9834. nil
  9835. (if (string-match "\\W" (car x))
  9836. x
  9837. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  9838. list))
  9839. (if add-auto-mode
  9840. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  9841. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  9842. (defun org-file-remote-p (file)
  9843. "Test whether FILE specifies a location on a remote system.
  9844. Return non-nil if the location is indeed remote.
  9845. For example, the filename \"/user@host:/foo\" specifies a location
  9846. on the system \"/user@host:\"."
  9847. (cond ((fboundp 'file-remote-p)
  9848. (file-remote-p file))
  9849. ((fboundp 'tramp-handle-file-remote-p)
  9850. (tramp-handle-file-remote-p file))
  9851. ((and (boundp 'ange-ftp-name-format)
  9852. (string-match (car ange-ftp-name-format) file))
  9853. t)))
  9854. ;;;; Refiling
  9855. (defun org-get-org-file ()
  9856. "Read a filename, with default directory `org-directory'."
  9857. (let ((default (or org-default-notes-file remember-data-file)))
  9858. (read-file-name (format "File name [%s]: " default)
  9859. (file-name-as-directory org-directory)
  9860. default)))
  9861. (defun org-notes-order-reversed-p ()
  9862. "Check if the current file should receive notes in reversed order."
  9863. (cond
  9864. ((not org-reverse-note-order) nil)
  9865. ((eq t org-reverse-note-order) t)
  9866. ((not (listp org-reverse-note-order)) nil)
  9867. (t (catch 'exit
  9868. (let ((all org-reverse-note-order)
  9869. entry)
  9870. (while (setq entry (pop all))
  9871. (if (string-match (car entry) buffer-file-name)
  9872. (throw 'exit (cdr entry))))
  9873. nil)))))
  9874. (defvar org-refile-target-table nil
  9875. "The list of refile targets, created by `org-refile'.")
  9876. (defvar org-agenda-new-buffers nil
  9877. "Buffers created to visit agenda files.")
  9878. (defvar org-refile-cache nil
  9879. "Cache for refile targets.")
  9880. (defvar org-refile-markers nil
  9881. "All the markers used for caching refile locations.")
  9882. (defun org-refile-marker (pos)
  9883. "Get a new refile marker, but only if caching is in use."
  9884. (if (not org-refile-use-cache)
  9885. pos
  9886. (let ((m (make-marker)))
  9887. (move-marker m pos)
  9888. (push m org-refile-markers)
  9889. m)))
  9890. (defun org-refile-cache-clear ()
  9891. "Clear the refile cache and disable all the markers."
  9892. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  9893. (setq org-refile-markers nil)
  9894. (setq org-refile-cache nil)
  9895. (message "Refile cache has been cleared"))
  9896. (defun org-refile-cache-check-set (set)
  9897. "Check if all the markers in the cache still have live buffers."
  9898. (let (marker)
  9899. (catch 'exit
  9900. (while (and set (setq marker (nth 3 (pop set))))
  9901. ;; if org-refile-use-outline-path is 'file, marker may be nil
  9902. (when (and marker (null (marker-buffer marker)))
  9903. (message "not found") (sit-for 3)
  9904. (throw 'exit nil)))
  9905. t)))
  9906. (defun org-refile-cache-put (set &rest identifiers)
  9907. "Push the refile targets SET into the cache, under IDENTIFIERS."
  9908. (let* ((key (sha1 (prin1-to-string identifiers)))
  9909. (entry (assoc key org-refile-cache)))
  9910. (if entry
  9911. (setcdr entry set)
  9912. (push (cons key set) org-refile-cache))))
  9913. (defun org-refile-cache-get (&rest identifiers)
  9914. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  9915. (cond
  9916. ((not org-refile-cache) nil)
  9917. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  9918. (t
  9919. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  9920. org-refile-cache))))
  9921. (and set (org-refile-cache-check-set set) set)))))
  9922. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  9923. "Produce a table with refile targets."
  9924. (let ((case-fold-search nil)
  9925. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  9926. (entries (or org-refile-targets '((nil . (:level . 1)))))
  9927. targets tgs txt re files f desc descre fast-path-p level pos0)
  9928. (message "Getting targets...")
  9929. (with-current-buffer (or default-buffer (current-buffer))
  9930. (while (setq entry (pop entries))
  9931. (setq files (car entry) desc (cdr entry))
  9932. (setq fast-path-p nil)
  9933. (cond
  9934. ((null files) (setq files (list (current-buffer))))
  9935. ((eq files 'org-agenda-files)
  9936. (setq files (org-agenda-files 'unrestricted)))
  9937. ((and (symbolp files) (fboundp files))
  9938. (setq files (funcall files)))
  9939. ((and (symbolp files) (boundp files))
  9940. (setq files (symbol-value files))))
  9941. (if (stringp files) (setq files (list files)))
  9942. (cond
  9943. ((eq (car desc) :tag)
  9944. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  9945. ((eq (car desc) :todo)
  9946. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  9947. ((eq (car desc) :regexp)
  9948. (setq descre (cdr desc)))
  9949. ((eq (car desc) :level)
  9950. (setq descre (concat "^\\*\\{" (number-to-string
  9951. (if org-odd-levels-only
  9952. (1- (* 2 (cdr desc)))
  9953. (cdr desc)))
  9954. "\\}[ \t]")))
  9955. ((eq (car desc) :maxlevel)
  9956. (setq fast-path-p t)
  9957. (setq descre (concat "^\\*\\{1," (number-to-string
  9958. (if org-odd-levels-only
  9959. (1- (* 2 (cdr desc)))
  9960. (cdr desc)))
  9961. "\\}[ \t]")))
  9962. (t (error "Bad refiling target description %s" desc)))
  9963. (while (setq f (pop files))
  9964. (with-current-buffer
  9965. (if (bufferp f) f (org-get-agenda-file-buffer f))
  9966. (or
  9967. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  9968. (progn
  9969. (if (bufferp f) (setq f (buffer-file-name
  9970. (buffer-base-buffer f))))
  9971. (setq f (and f (expand-file-name f)))
  9972. (if (eq org-refile-use-outline-path 'file)
  9973. (push (list (file-name-nondirectory f) f nil nil) tgs))
  9974. (save-excursion
  9975. (save-restriction
  9976. (widen)
  9977. (goto-char (point-min))
  9978. (while (re-search-forward descre nil t)
  9979. (goto-char (setq pos0 (point-at-bol)))
  9980. (catch 'next
  9981. (when org-refile-target-verify-function
  9982. (save-match-data
  9983. (or (funcall org-refile-target-verify-function)
  9984. (throw 'next t))))
  9985. (when (and (looking-at org-complex-heading-regexp)
  9986. (not (member (match-string 4) excluded-entries))
  9987. (match-string 4))
  9988. (setq level (org-reduced-level
  9989. (- (match-end 1) (match-beginning 1)))
  9990. txt (org-link-display-format (match-string 4))
  9991. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  9992. re (format org-complex-heading-regexp-format
  9993. (regexp-quote (match-string 4))))
  9994. (when org-refile-use-outline-path
  9995. (setq txt (mapconcat
  9996. 'org-protect-slash
  9997. (append
  9998. (if (eq org-refile-use-outline-path
  9999. 'file)
  10000. (list (file-name-nondirectory
  10001. (buffer-file-name
  10002. (buffer-base-buffer))))
  10003. (if (eq org-refile-use-outline-path
  10004. 'full-file-path)
  10005. (list (buffer-file-name
  10006. (buffer-base-buffer)))))
  10007. (org-get-outline-path fast-path-p
  10008. level txt)
  10009. (list txt))
  10010. "/")))
  10011. (push (list txt f re (org-refile-marker (point)))
  10012. tgs)))
  10013. (when (= (point) pos0)
  10014. ;; verification function has not moved point
  10015. (goto-char (point-at-eol))))))))
  10016. (when org-refile-use-cache
  10017. (org-refile-cache-put tgs (buffer-file-name) descre))
  10018. (setq targets (append tgs targets))
  10019. ))))
  10020. (message "Getting targets...done")
  10021. (nreverse targets)))
  10022. (defun org-protect-slash (s)
  10023. (while (string-match "/" s)
  10024. (setq s (replace-match "\\" t t s)))
  10025. s)
  10026. (defvar org-olpa (make-vector 20 nil))
  10027. (defun org-get-outline-path (&optional fastp level heading)
  10028. "Return the outline path to the current entry, as a list.
  10029. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  10030. routine which makes outline path derivations for an entire file,
  10031. avoiding backtracing. Refile target collection makes use of that."
  10032. (if fastp
  10033. (progn
  10034. (if (> level 19)
  10035. (error "Outline path failure, more than 19 levels"))
  10036. (loop for i from level upto 19 do
  10037. (aset org-olpa i nil))
  10038. (prog1
  10039. (delq nil (append org-olpa nil))
  10040. (aset org-olpa level heading)))
  10041. (let (rtn case-fold-search)
  10042. (save-excursion
  10043. (save-restriction
  10044. (widen)
  10045. (while (org-up-heading-safe)
  10046. (when (looking-at org-complex-heading-regexp)
  10047. (push (org-match-string-no-properties 4) rtn)))
  10048. rtn)))))
  10049. (defun org-format-outline-path (path &optional width prefix separator)
  10050. "Format the outline path PATH for display.
  10051. WIDTH is the maximum number of characters that is available.
  10052. PREFIX is a prefix to be included in the returned string,
  10053. such as the file name.
  10054. SEPARATOR is inserted between the different parts of the path,
  10055. the default is \"/\"."
  10056. (setq width (or width 79))
  10057. (if prefix (setq width (- width (length prefix))))
  10058. (if (not path)
  10059. (or prefix "")
  10060. (let* ((nsteps (length path))
  10061. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  10062. (maxwidth (if (<= total-width width)
  10063. 10000 ;; everything fits
  10064. ;; we need to shorten the level headings
  10065. (/ (- width nsteps) nsteps)))
  10066. (org-odd-levels-only nil)
  10067. (n 0)
  10068. (total (1+ (length prefix))))
  10069. (setq maxwidth (max maxwidth 10))
  10070. (concat prefix
  10071. (if prefix (or separator "/"))
  10072. (mapconcat
  10073. (lambda (h)
  10074. (setq n (1+ n))
  10075. (if (and (= n nsteps) (< maxwidth 10000))
  10076. (setq maxwidth (- total-width total)))
  10077. (if (< (length h) maxwidth)
  10078. (progn (setq total (+ total (length h) 1)) h)
  10079. (setq h (substring h 0 (- maxwidth 2))
  10080. total (+ total maxwidth 1))
  10081. (if (string-match "[ \t]+\\'" h)
  10082. (setq h (substring h 0 (match-beginning 0))))
  10083. (setq h (concat h "..")))
  10084. (org-add-props h nil 'face
  10085. (nth (% (1- n) org-n-level-faces)
  10086. org-level-faces))
  10087. h)
  10088. path (or separator "/"))))))
  10089. (defun org-display-outline-path (&optional file current separator just-return-string)
  10090. "Display the current outline path in the echo area.
  10091. If FILE is non-nil, prepend the output with the file name.
  10092. If CURRENT is non-nil, append the current heading to the output.
  10093. SEPARATOR is passed through to `org-format-outline-path'. It separates
  10094. the different parts of the path and defaults to \"/\".
  10095. If JUST-RETURN-STRING is non-nil, return a string, don't display a message."
  10096. (interactive "P")
  10097. (let* (case-fold-search
  10098. message-log-max ; Don't populate the *Messages* buffer
  10099. (bfn (buffer-file-name (buffer-base-buffer)))
  10100. (path (and (derived-mode-p 'org-mode) (org-get-outline-path)))
  10101. res)
  10102. (if current (setq path (append path
  10103. (save-excursion
  10104. (org-back-to-heading t)
  10105. (if (looking-at org-complex-heading-regexp)
  10106. (list (match-string 4)))))))
  10107. (setq res
  10108. (org-format-outline-path
  10109. path
  10110. (1- (frame-width))
  10111. (and file bfn (concat (file-name-nondirectory bfn) separator))
  10112. separator))
  10113. (if just-return-string
  10114. (org-no-properties res)
  10115. (message "%s" res))))
  10116. (defvar org-refile-history nil
  10117. "History for refiling operations.")
  10118. (defvar org-after-refile-insert-hook nil
  10119. "Hook run after `org-refile' has inserted its stuff at the new location.
  10120. Note that this is still *before* the stuff will be removed from
  10121. the *old* location.")
  10122. (defvar org-capture-last-stored-marker)
  10123. (defvar org-refile-keep nil
  10124. "Non-nil means `org-refile' will copy instead of refile.")
  10125. (defun org-copy ()
  10126. "Like `org-refile', but copy."
  10127. (interactive)
  10128. (let ((org-refile-keep t))
  10129. (funcall 'org-refile nil nil nil "Copy")))
  10130. (defun org-refile (&optional goto default-buffer rfloc msg)
  10131. "Move the entry or entries at point to another heading.
  10132. The list of target headings is compiled using the information in
  10133. `org-refile-targets', which see.
  10134. At the target location, the entry is filed as a subitem of the target
  10135. heading. Depending on `org-reverse-note-order', the new subitem will
  10136. either be the first or the last subitem.
  10137. If there is an active region, all entries in that region will be moved.
  10138. However, the region must fulfill the requirement that the first heading
  10139. is the first one sets the top-level of the moved text - at most siblings
  10140. below it are allowed.
  10141. With prefix arg GOTO, the command will only visit the target location
  10142. and not actually move anything.
  10143. With a double prefix arg \\[universal-argument] \\[universal-argument], \
  10144. go to the location where the last refiling operation has put the subtree.
  10145. With a prefix argument of `2', refile to the running clock.
  10146. RFLOC can be a refile location obtained in a different way.
  10147. MSG is a string to replace \"Refile\" in the default prompt with
  10148. another verb. E.g. `org-copy' sets this parameter to \"Copy\".
  10149. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  10150. If you are using target caching (see `org-refile-use-cache'),
  10151. you have to clear the target cache in order to find new targets.
  10152. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  10153. prefix argument (`C-u C-u C-u C-c C-w')."
  10154. (interactive "P")
  10155. (if (member goto '(0 (64)))
  10156. (org-refile-cache-clear)
  10157. (let* ((actionmsg (or msg "Refile"))
  10158. (cbuf (current-buffer))
  10159. (regionp (org-region-active-p))
  10160. (region-start (and regionp (region-beginning)))
  10161. (region-end (and regionp (region-end)))
  10162. (region-length (and regionp (- region-end region-start)))
  10163. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  10164. pos it nbuf file re level reversed)
  10165. (setq last-command nil)
  10166. (when regionp
  10167. (goto-char region-start)
  10168. (or (bolp) (goto-char (point-at-bol)))
  10169. (setq region-start (point))
  10170. (unless (or (org-kill-is-subtree-p
  10171. (buffer-substring region-start region-end))
  10172. (prog1 org-refile-active-region-within-subtree
  10173. (org-toggle-heading)))
  10174. (error "The region is not a (sequence of) subtree(s)")))
  10175. (if (equal goto '(16))
  10176. (org-refile-goto-last-stored)
  10177. (when (or
  10178. (and (equal goto 2)
  10179. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  10180. (prog1
  10181. (setq it (list (or org-clock-heading "running clock")
  10182. (buffer-file-name
  10183. (marker-buffer org-clock-hd-marker))
  10184. ""
  10185. (marker-position org-clock-hd-marker)))
  10186. (setq goto nil)))
  10187. (setq it (or rfloc
  10188. (let (heading-text)
  10189. (save-excursion
  10190. (unless goto
  10191. (org-back-to-heading t)
  10192. (setq heading-text
  10193. (nth 4 (org-heading-components))))
  10194. (org-refile-get-location
  10195. (cond (goto "Goto")
  10196. (regionp (concat actionmsg " region to"))
  10197. (t (concat actionmsg " subtree \""
  10198. heading-text "\" to")))
  10199. default-buffer
  10200. (and (not (equal '(4) goto))
  10201. org-refile-allow-creating-parent-nodes)
  10202. goto))))))
  10203. (setq file (nth 1 it)
  10204. re (nth 2 it)
  10205. pos (nth 3 it))
  10206. (if (and (not goto)
  10207. pos
  10208. (equal (buffer-file-name) file)
  10209. (if regionp
  10210. (and (>= pos region-start)
  10211. (<= pos region-end))
  10212. (and (>= pos (point))
  10213. (< pos (save-excursion
  10214. (org-end-of-subtree t t))))))
  10215. (error "Cannot refile to position inside the tree or region"))
  10216. (setq nbuf (or (find-buffer-visiting file)
  10217. (find-file-noselect file)))
  10218. (if goto
  10219. (progn
  10220. (org-pop-to-buffer-same-window nbuf)
  10221. (goto-char pos)
  10222. (org-show-context 'org-goto))
  10223. (if regionp
  10224. (progn
  10225. (org-kill-new (buffer-substring region-start region-end))
  10226. (org-save-markers-in-region region-start region-end))
  10227. (org-copy-subtree 1 nil t))
  10228. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  10229. (find-file-noselect file)))
  10230. (setq reversed (org-notes-order-reversed-p))
  10231. (save-excursion
  10232. (save-restriction
  10233. (widen)
  10234. (if pos
  10235. (progn
  10236. (goto-char pos)
  10237. (looking-at org-outline-regexp)
  10238. (setq level (org-get-valid-level (funcall outline-level) 1))
  10239. (goto-char
  10240. (if reversed
  10241. (or (outline-next-heading) (point-max))
  10242. (or (save-excursion (org-get-next-sibling))
  10243. (org-end-of-subtree t t)
  10244. (point-max)))))
  10245. (setq level 1)
  10246. (if (not reversed)
  10247. (goto-char (point-max))
  10248. (goto-char (point-min))
  10249. (or (outline-next-heading) (goto-char (point-max)))))
  10250. (if (not (bolp)) (newline))
  10251. (org-paste-subtree level)
  10252. (when org-log-refile
  10253. (org-add-log-setup 'refile nil nil 'findpos
  10254. org-log-refile)
  10255. (unless (eq org-log-refile 'note)
  10256. (save-excursion (org-add-log-note))))
  10257. (and org-auto-align-tags
  10258. (let ((org-loop-over-headlines-in-active-region nil))
  10259. (org-set-tags nil t)))
  10260. (with-demoted-errors
  10261. (bookmark-set "org-refile-last-stored"))
  10262. ;; If we are refiling for capture, make sure that the
  10263. ;; last-capture pointers point here
  10264. (when (org-bound-and-true-p org-refile-for-capture)
  10265. (with-demoted-errors
  10266. (bookmark-set "org-capture-last-stored-marker"))
  10267. (move-marker org-capture-last-stored-marker (point)))
  10268. (if (fboundp 'deactivate-mark) (deactivate-mark))
  10269. (run-hooks 'org-after-refile-insert-hook))))
  10270. (unless org-refile-keep
  10271. (if regionp
  10272. (delete-region (point) (+ (point) region-length))
  10273. (org-cut-subtree)))
  10274. (when (featurep 'org-inlinetask)
  10275. (org-inlinetask-remove-END-maybe))
  10276. (setq org-markers-to-move nil)
  10277. (message (concat actionmsg " to \"%s\" in file %s: done") (car it) file)))))))
  10278. (defun org-refile-goto-last-stored ()
  10279. "Go to the location where the last refile was stored."
  10280. (interactive)
  10281. (bookmark-jump "org-refile-last-stored")
  10282. (message "This is the location of the last refile"))
  10283. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  10284. no-exclude)
  10285. "Prompt the user for a refile location, using PROMPT.
  10286. PROMPT should not be suffixed with a colon and a space, because
  10287. this function appends the default value from
  10288. `org-refile-history' automatically, if that is not empty.
  10289. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  10290. this is used for the GOTO interface."
  10291. (let ((org-refile-targets org-refile-targets)
  10292. (org-refile-use-outline-path org-refile-use-outline-path)
  10293. excluded-entries)
  10294. (when (and (derived-mode-p 'org-mode)
  10295. (not org-refile-use-cache)
  10296. (not no-exclude))
  10297. (org-map-tree
  10298. (lambda()
  10299. (setq excluded-entries
  10300. (append excluded-entries (list (org-get-heading t t)))))))
  10301. (setq org-refile-target-table
  10302. (org-refile-get-targets default-buffer excluded-entries)))
  10303. (unless org-refile-target-table
  10304. (error "No refile targets"))
  10305. (let* ((cbuf (current-buffer))
  10306. (partial-completion-mode nil)
  10307. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  10308. (cfunc (if (and org-refile-use-outline-path
  10309. org-outline-path-complete-in-steps)
  10310. 'org-olpath-completing-read
  10311. 'org-icompleting-read))
  10312. (extra (if org-refile-use-outline-path "/" ""))
  10313. (cbnex (concat (buffer-name) extra))
  10314. (filename (and cfn (expand-file-name cfn)))
  10315. (tbl (mapcar
  10316. (lambda (x)
  10317. (if (and (not (member org-refile-use-outline-path
  10318. '(file full-file-path)))
  10319. (not (equal filename (nth 1 x))))
  10320. (cons (concat (car x) extra " ("
  10321. (file-name-nondirectory (nth 1 x)) ")")
  10322. (cdr x))
  10323. (cons (concat (car x) extra) (cdr x))))
  10324. org-refile-target-table))
  10325. (completion-ignore-case t)
  10326. cdef
  10327. (prompt (concat prompt
  10328. (or (and (car org-refile-history)
  10329. (concat " (default " (car org-refile-history) ")"))
  10330. (and (assoc cbnex tbl) (setq cdef cbnex)
  10331. (concat " (default " cbnex ")"))) ": "))
  10332. pa answ parent-target child parent old-hist)
  10333. (setq old-hist org-refile-history)
  10334. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  10335. nil 'org-refile-history (or cdef (car org-refile-history))))
  10336. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  10337. (org-refile-check-position pa)
  10338. (if pa
  10339. (progn
  10340. (when (or (not org-refile-history)
  10341. (not (eq old-hist org-refile-history))
  10342. (not (equal (car pa) (car org-refile-history))))
  10343. (setq org-refile-history
  10344. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  10345. org-refile-history
  10346. (cdr org-refile-history))))
  10347. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  10348. (pop org-refile-history)))
  10349. pa)
  10350. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  10351. (progn
  10352. (setq parent (match-string 1 answ)
  10353. child (match-string 2 answ))
  10354. (setq parent-target (or (assoc parent tbl)
  10355. (assoc (concat parent "/") tbl)))
  10356. (when (and parent-target
  10357. (or (eq new-nodes t)
  10358. (and (eq new-nodes 'confirm)
  10359. (y-or-n-p (format "Create new node \"%s\"? "
  10360. child)))))
  10361. (org-refile-new-child parent-target child)))
  10362. (error "Invalid target location")))))
  10363. (declare-function org-string-nw-p "org-macs" (s))
  10364. (defun org-refile-check-position (refile-pointer)
  10365. "Check if the refile pointer matches the headline to which it points."
  10366. (let* ((file (nth 1 refile-pointer))
  10367. (re (nth 2 refile-pointer))
  10368. (pos (nth 3 refile-pointer))
  10369. buffer)
  10370. (if (and (not (markerp pos)) (not file))
  10371. (error "Please save the buffer to a file before refiling")
  10372. (when (org-string-nw-p re)
  10373. (setq buffer (if (markerp pos)
  10374. (marker-buffer pos)
  10375. (or (find-buffer-visiting file)
  10376. (find-file-noselect file))))
  10377. (with-current-buffer buffer
  10378. (save-excursion
  10379. (save-restriction
  10380. (widen)
  10381. (goto-char pos)
  10382. (beginning-of-line 1)
  10383. (unless (org-looking-at-p re)
  10384. (error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling")))))))))
  10385. (defun org-refile-new-child (parent-target child)
  10386. "Use refile target PARENT-TARGET to add new CHILD below it."
  10387. (unless parent-target
  10388. (error "Cannot find parent for new node"))
  10389. (let ((file (nth 1 parent-target))
  10390. (pos (nth 3 parent-target))
  10391. level)
  10392. (with-current-buffer (or (find-buffer-visiting file)
  10393. (find-file-noselect file))
  10394. (save-excursion
  10395. (save-restriction
  10396. (widen)
  10397. (if pos
  10398. (goto-char pos)
  10399. (goto-char (point-max))
  10400. (if (not (bolp)) (newline)))
  10401. (when (looking-at org-outline-regexp)
  10402. (setq level (funcall outline-level))
  10403. (org-end-of-subtree t t))
  10404. (org-back-over-empty-lines)
  10405. (insert "\n" (make-string
  10406. (if pos (org-get-valid-level level 1) 1) ?*)
  10407. " " child "\n")
  10408. (beginning-of-line 0)
  10409. (list (concat (car parent-target) "/" child) file "" (point)))))))
  10410. (defun org-olpath-completing-read (prompt collection &rest args)
  10411. "Read an outline path like a file name."
  10412. (let ((thetable collection)
  10413. (org-completion-use-ido nil) ; does not work with ido.
  10414. (org-completion-use-iswitchb nil)) ; or iswitchb
  10415. (apply
  10416. 'org-icompleting-read prompt
  10417. (lambda (string predicate &optional flag)
  10418. (let (rtn r f (l (length string)))
  10419. (cond
  10420. ((eq flag nil)
  10421. ;; try completion
  10422. (try-completion string thetable))
  10423. ((eq flag t)
  10424. ;; all-completions
  10425. (setq rtn (all-completions string thetable predicate))
  10426. (mapcar
  10427. (lambda (x)
  10428. (setq r (substring x l))
  10429. (if (string-match " ([^)]*)$" x)
  10430. (setq f (match-string 0 x))
  10431. (setq f ""))
  10432. (if (string-match "/" r)
  10433. (concat string (substring r 0 (match-end 0)) f)
  10434. x))
  10435. rtn))
  10436. ((eq flag 'lambda)
  10437. ;; exact match?
  10438. (assoc string thetable)))))
  10439. args)))
  10440. ;;;; Dynamic blocks
  10441. (defun org-find-dblock (name)
  10442. "Find the first dynamic block with name NAME in the buffer.
  10443. If not found, stay at current position and return nil."
  10444. (let ((case-fold-search t) pos)
  10445. (save-excursion
  10446. (goto-char (point-min))
  10447. (setq pos (and (re-search-forward
  10448. (concat "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+" name "\\>") nil t)
  10449. (match-beginning 0))))
  10450. (if pos (goto-char pos))
  10451. pos))
  10452. (defconst org-dblock-start-re
  10453. "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  10454. "Matches the start line of a dynamic block, with parameters.")
  10455. (defconst org-dblock-end-re "^[ \t]*#\\+\\(?:END\\|end\\)\\([: \t\r\n]\\|$\\)"
  10456. "Matches the end of a dynamic block.")
  10457. (defun org-create-dblock (plist)
  10458. "Create a dynamic block section, with parameters taken from PLIST.
  10459. PLIST must contain a :name entry which is used as name of the block."
  10460. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  10461. (end-of-line 1)
  10462. (newline))
  10463. (let ((col (current-column))
  10464. (name (plist-get plist :name)))
  10465. (insert "#+BEGIN: " name)
  10466. (while plist
  10467. (if (eq (car plist) :name)
  10468. (setq plist (cddr plist))
  10469. (insert " " (prin1-to-string (pop plist)))))
  10470. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  10471. (beginning-of-line -2)))
  10472. (defun org-prepare-dblock ()
  10473. "Prepare dynamic block for refresh.
  10474. This empties the block, puts the cursor at the insert position and returns
  10475. the property list including an extra property :name with the block name."
  10476. (unless (looking-at org-dblock-start-re)
  10477. (error "Not at a dynamic block"))
  10478. (let* ((begdel (1+ (match-end 0)))
  10479. (name (org-no-properties (match-string 1)))
  10480. (params (append (list :name name)
  10481. (read (concat "(" (match-string 3) ")")))))
  10482. (save-excursion
  10483. (beginning-of-line 1)
  10484. (skip-chars-forward " \t")
  10485. (setq params (plist-put params :indentation-column (current-column))))
  10486. (unless (re-search-forward org-dblock-end-re nil t)
  10487. (error "Dynamic block not terminated"))
  10488. (setq params
  10489. (append params
  10490. (list :content (buffer-substring
  10491. begdel (match-beginning 0)))))
  10492. (delete-region begdel (match-beginning 0))
  10493. (goto-char begdel)
  10494. (open-line 1)
  10495. params))
  10496. (defun org-map-dblocks (&optional command)
  10497. "Apply COMMAND to all dynamic blocks in the current buffer.
  10498. If COMMAND is not given, use `org-update-dblock'."
  10499. (let ((cmd (or command 'org-update-dblock)))
  10500. (save-excursion
  10501. (goto-char (point-min))
  10502. (while (re-search-forward org-dblock-start-re nil t)
  10503. (goto-char (match-beginning 0))
  10504. (save-excursion
  10505. (condition-case nil
  10506. (funcall cmd)
  10507. (error (message "Error during update of dynamic block"))))
  10508. (unless (re-search-forward org-dblock-end-re nil t)
  10509. (error "Dynamic block not terminated"))))))
  10510. (defun org-dblock-update (&optional arg)
  10511. "User command for updating dynamic blocks.
  10512. Update the dynamic block at point. With prefix ARG, update all dynamic
  10513. blocks in the buffer."
  10514. (interactive "P")
  10515. (if arg
  10516. (org-update-all-dblocks)
  10517. (or (looking-at org-dblock-start-re)
  10518. (org-beginning-of-dblock))
  10519. (org-update-dblock)))
  10520. (defun org-update-dblock ()
  10521. "Update the dynamic block at point.
  10522. This means to empty the block, parse for parameters and then call
  10523. the correct writing function."
  10524. (interactive)
  10525. (save-window-excursion
  10526. (let* ((pos (point))
  10527. (line (org-current-line))
  10528. (params (org-prepare-dblock))
  10529. (name (plist-get params :name))
  10530. (indent (plist-get params :indentation-column))
  10531. (cmd (intern (concat "org-dblock-write:" name))))
  10532. (message "Updating dynamic block `%s' at line %d..." name line)
  10533. (funcall cmd params)
  10534. (message "Updating dynamic block `%s' at line %d...done" name line)
  10535. (goto-char pos)
  10536. (when (and indent (> indent 0))
  10537. (setq indent (make-string indent ?\ ))
  10538. (save-excursion
  10539. (org-beginning-of-dblock)
  10540. (forward-line 1)
  10541. (while (not (looking-at org-dblock-end-re))
  10542. (insert indent)
  10543. (beginning-of-line 2))
  10544. (when (looking-at org-dblock-end-re)
  10545. (and (looking-at "[ \t]+")
  10546. (replace-match ""))
  10547. (insert indent)))))))
  10548. (defun org-beginning-of-dblock ()
  10549. "Find the beginning of the dynamic block at point.
  10550. Error if there is no such block at point."
  10551. (let ((pos (point))
  10552. beg)
  10553. (end-of-line 1)
  10554. (if (and (re-search-backward org-dblock-start-re nil t)
  10555. (setq beg (match-beginning 0))
  10556. (re-search-forward org-dblock-end-re nil t)
  10557. (> (match-end 0) pos))
  10558. (goto-char beg)
  10559. (goto-char pos)
  10560. (error "Not in a dynamic block"))))
  10561. (defun org-update-all-dblocks ()
  10562. "Update all dynamic blocks in the buffer.
  10563. This function can be used in a hook."
  10564. (interactive)
  10565. (when (derived-mode-p 'org-mode)
  10566. (org-map-dblocks 'org-update-dblock)))
  10567. ;;;; Completion
  10568. (defun org-get-export-keywords ()
  10569. "Return a list of all currently understood export keywords.
  10570. Export keywords include options, block names, attributes and
  10571. keywords relative to each registered export back-end."
  10572. (delq nil
  10573. (let (keywords)
  10574. (mapc
  10575. (lambda (back-end)
  10576. (let ((props (cdr back-end)))
  10577. ;; Back-end name (for keywords, like #+LATEX:)
  10578. (push (upcase (symbol-name (car back-end))) keywords)
  10579. ;; Back-end options.
  10580. (mapc (lambda (option) (push (cadr option) keywords))
  10581. (plist-get (cdr back-end) :options-alist))))
  10582. (org-bound-and-true-p org-export-registered-backends))
  10583. keywords)))
  10584. (defconst org-options-keywords
  10585. '("ARCHIVE:" "AUTHOR:" "BIND:" "CATEGORY:" "COLUMNS:" "CREATOR:" "DATE:"
  10586. "DESCRIPTION:" "DRAWERS:" "EMAIL:" "EXCLUDE_TAGS:" "FILETAGS:" "INCLUDE:"
  10587. "INDEX:" "KEYWORDS:" "LANGUAGE:" "MACRO:" "OPTIONS:" "PROPERTY:"
  10588. "PRIORITIES:" "SELECT_TAGS:" "SEQ_TODO:" "SETUPFILE:" "STARTUP:" "TAGS:"
  10589. "TITLE:" "TODO:" "TYP_TODO:" "SELECT_TAGS:" "EXCLUDE_TAGS:" "INFOJS_OPT:"))
  10590. (defcustom org-structure-template-alist
  10591. '(("s" "#+BEGIN_SRC ?\n\n#+END_SRC"
  10592. "<src lang=\"?\">\n\n</src>")
  10593. ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE"
  10594. "<example>\n?\n</example>")
  10595. ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE"
  10596. "<quote>\n?\n</quote>")
  10597. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE"
  10598. "<verse>\n?\n</verse>")
  10599. ("V" "#+BEGIN_VERBATIM\n?\n#+END_VERBATIM"
  10600. "<verbatim>\n?\n</verbatim>")
  10601. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER"
  10602. "<center>\n?\n</center>")
  10603. ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX"
  10604. "<literal style=\"latex\">\n?\n</literal>")
  10605. ("L" "#+LaTeX: "
  10606. "<literal style=\"latex\">?</literal>")
  10607. ("h" "#+BEGIN_HTML\n?\n#+END_HTML"
  10608. "<literal style=\"html\">\n?\n</literal>")
  10609. ("H" "#+HTML: "
  10610. "<literal style=\"html\">?</literal>")
  10611. ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII")
  10612. ("A" "#+ASCII: ")
  10613. ("i" "#+INDEX: ?"
  10614. "#+INDEX: ?")
  10615. ("I" "#+INCLUDE: %file ?"
  10616. "<include file=%file markup=\"?\">"))
  10617. "Structure completion elements.
  10618. This is a list of abbreviation keys and values. The value gets inserted
  10619. if you type `<' followed by the key and then press the completion key,
  10620. usually `M-TAB'. %file will be replaced by a file name after prompting
  10621. for the file using completion. The cursor will be placed at the position
  10622. of the `?` in the template.
  10623. There are two templates for each key, the first uses the original Org syntax,
  10624. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  10625. the default when the /org-mtags.el/ module has been loaded. See also the
  10626. variable `org-mtags-prefer-muse-templates'."
  10627. :group 'org-completion
  10628. :type '(repeat
  10629. (string :tag "Key")
  10630. (string :tag "Template")
  10631. (string :tag "Muse Template")))
  10632. (defun org-try-structure-completion ()
  10633. "Try to complete a structure template before point.
  10634. This looks for strings like \"<e\" on an otherwise empty line and
  10635. expands them."
  10636. (let ((l (buffer-substring (point-at-bol) (point)))
  10637. a)
  10638. (when (and (looking-at "[ \t]*$")
  10639. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  10640. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  10641. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  10642. (match-beginning 1)) a)
  10643. t)))
  10644. (defun org-complete-expand-structure-template (start cell)
  10645. "Expand a structure template."
  10646. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  10647. (rpl (nth (if musep 2 1) cell))
  10648. (ind ""))
  10649. (delete-region start (point))
  10650. (when (string-match "\\`#\\+" rpl)
  10651. (cond
  10652. ((bolp))
  10653. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  10654. (setq ind (buffer-substring (point-at-bol) (point))))
  10655. (t (newline))))
  10656. (setq start (point))
  10657. (if (string-match "%file" rpl)
  10658. (setq rpl (replace-match
  10659. (concat
  10660. "\""
  10661. (save-match-data
  10662. (abbreviate-file-name (read-file-name "Include file: ")))
  10663. "\"")
  10664. t t rpl)))
  10665. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  10666. (concat "\n" ind)))
  10667. (insert rpl)
  10668. (if (re-search-backward "\\?" start t) (delete-char 1))))
  10669. ;;;; TODO, DEADLINE, Comments
  10670. (defun org-toggle-comment ()
  10671. "Change the COMMENT state of an entry."
  10672. (interactive)
  10673. (save-excursion
  10674. (org-back-to-heading)
  10675. (let (case-fold-search)
  10676. (cond
  10677. ((looking-at (format org-heading-keyword-regexp-format
  10678. org-comment-string))
  10679. (goto-char (match-end 1))
  10680. (looking-at (concat " +" org-comment-string))
  10681. (replace-match "" t t)
  10682. (when (eolp) (insert " ")))
  10683. ((looking-at org-outline-regexp)
  10684. (goto-char (match-end 0))
  10685. (insert org-comment-string " "))))))
  10686. (defvar org-last-todo-state-is-todo nil
  10687. "This is non-nil when the last TODO state change led to a TODO state.
  10688. If the last change removed the TODO tag or switched to DONE, then
  10689. this is nil.")
  10690. (defvar org-setting-tags nil) ; dynamically skipped
  10691. (defvar org-todo-setup-filter-hook nil
  10692. "Hook for functions that pre-filter todo specs.
  10693. Each function takes a todo spec and returns either nil or the spec
  10694. transformed into canonical form." )
  10695. (defvar org-todo-get-default-hook nil
  10696. "Hook for functions that get a default item for todo.
  10697. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  10698. nil or a string to be used for the todo mark." )
  10699. (defvar org-agenda-headline-snapshot-before-repeat)
  10700. (defun org-current-effective-time ()
  10701. "Return current time adjusted for `org-extend-today-until' variable."
  10702. (let* ((ct (org-current-time))
  10703. (dct (decode-time ct))
  10704. (ct1
  10705. (cond
  10706. (org-use-last-clock-out-time-as-effective-time
  10707. (or (org-clock-get-last-clock-out-time) ct))
  10708. ((and org-use-effective-time (< (nth 2 dct) org-extend-today-until))
  10709. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct)))
  10710. (t ct))))
  10711. ct1))
  10712. (defun org-todo-yesterday (&optional arg)
  10713. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  10714. (interactive "P")
  10715. (if (eq major-mode 'org-agenda-mode)
  10716. (apply 'org-agenda-todo-yesterday arg)
  10717. (let* ((hour (third (decode-time
  10718. (org-current-time))))
  10719. (org-extend-today-until (1+ hour)))
  10720. (org-todo arg))))
  10721. (defvar org-block-entry-blocking ""
  10722. "First entry preventing the TODO state change.")
  10723. (defun org-todo (&optional arg)
  10724. "Change the TODO state of an item.
  10725. The state of an item is given by a keyword at the start of the heading,
  10726. like
  10727. *** TODO Write paper
  10728. *** DONE Call mom
  10729. The different keywords are specified in the variable `org-todo-keywords'.
  10730. By default the available states are \"TODO\" and \"DONE\".
  10731. So for this example: when the item starts with TODO, it is changed to DONE.
  10732. When it starts with DONE, the DONE is removed. And when neither TODO nor
  10733. DONE are present, add TODO at the beginning of the heading.
  10734. With \\[universal-argument] prefix arg, use completion to determine the new \
  10735. state.
  10736. With numeric prefix arg, switch to that state.
  10737. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  10738. keywords (nextset).
  10739. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  10740. With a numeric prefix arg of 0, inhibit note taking for the change.
  10741. For calling through lisp, arg is also interpreted in the following way:
  10742. 'none -> empty state
  10743. \"\"(empty string) -> switch to empty state
  10744. 'done -> switch to DONE
  10745. 'nextset -> switch to the next set of keywords
  10746. 'previousset -> switch to the previous set of keywords
  10747. \"WAITING\" -> switch to the specified keyword, but only if it
  10748. really is a member of `org-todo-keywords'."
  10749. (interactive "P")
  10750. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  10751. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  10752. 'region-start-level 'region))
  10753. org-loop-over-headlines-in-active-region)
  10754. (org-map-entries
  10755. `(org-todo ,arg)
  10756. org-loop-over-headlines-in-active-region
  10757. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  10758. (if (equal arg '(16)) (setq arg 'nextset))
  10759. (let ((org-blocker-hook org-blocker-hook)
  10760. commentp
  10761. case-fold-search)
  10762. (when (equal arg '(64))
  10763. (setq arg nil org-blocker-hook nil))
  10764. (when (and org-blocker-hook
  10765. (or org-inhibit-blocking
  10766. (org-entry-get nil "NOBLOCKING")))
  10767. (setq org-blocker-hook nil))
  10768. (save-excursion
  10769. (catch 'exit
  10770. (org-back-to-heading t)
  10771. (when (looking-at (concat "^\\*+ " org-comment-string))
  10772. (org-toggle-comment)
  10773. (setq commentp t))
  10774. (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  10775. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  10776. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  10777. (let* ((match-data (match-data))
  10778. (startpos (point-at-bol))
  10779. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  10780. (org-log-done org-log-done)
  10781. (org-log-repeat org-log-repeat)
  10782. (org-todo-log-states org-todo-log-states)
  10783. (org-inhibit-logging
  10784. (if (equal arg 0)
  10785. (progn (setq arg nil) 'note) org-inhibit-logging))
  10786. (this (match-string 1))
  10787. (hl-pos (match-beginning 0))
  10788. (head (org-get-todo-sequence-head this))
  10789. (ass (assoc head org-todo-kwd-alist))
  10790. (interpret (nth 1 ass))
  10791. (done-word (nth 3 ass))
  10792. (final-done-word (nth 4 ass))
  10793. (org-last-state (or this ""))
  10794. (completion-ignore-case t)
  10795. (member (member this org-todo-keywords-1))
  10796. (tail (cdr member))
  10797. (org-state (cond
  10798. ((and org-todo-key-trigger
  10799. (or (and (equal arg '(4))
  10800. (eq org-use-fast-todo-selection 'prefix))
  10801. (and (not arg) org-use-fast-todo-selection
  10802. (not (eq org-use-fast-todo-selection
  10803. 'prefix)))))
  10804. ;; Use fast selection
  10805. (org-fast-todo-selection))
  10806. ((and (equal arg '(4))
  10807. (or (not org-use-fast-todo-selection)
  10808. (not org-todo-key-trigger)))
  10809. ;; Read a state with completion
  10810. (org-icompleting-read
  10811. "State: " (mapcar (lambda(x) (list x))
  10812. org-todo-keywords-1)
  10813. nil t))
  10814. ((eq arg 'right)
  10815. (if this
  10816. (if tail (car tail) nil)
  10817. (car org-todo-keywords-1)))
  10818. ((eq arg 'left)
  10819. (if (equal member org-todo-keywords-1)
  10820. nil
  10821. (if this
  10822. (nth (- (length org-todo-keywords-1)
  10823. (length tail) 2)
  10824. org-todo-keywords-1)
  10825. (org-last org-todo-keywords-1))))
  10826. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  10827. (setq arg nil))) ; hack to fall back to cycling
  10828. (arg
  10829. ;; user or caller requests a specific state
  10830. (cond
  10831. ((equal arg "") nil)
  10832. ((eq arg 'none) nil)
  10833. ((eq arg 'done) (or done-word (car org-done-keywords)))
  10834. ((eq arg 'nextset)
  10835. (or (car (cdr (member head org-todo-heads)))
  10836. (car org-todo-heads)))
  10837. ((eq arg 'previousset)
  10838. (let ((org-todo-heads (reverse org-todo-heads)))
  10839. (or (car (cdr (member head org-todo-heads)))
  10840. (car org-todo-heads))))
  10841. ((car (member arg org-todo-keywords-1)))
  10842. ((stringp arg)
  10843. (error "State `%s' not valid in this file" arg))
  10844. ((nth (1- (prefix-numeric-value arg))
  10845. org-todo-keywords-1))))
  10846. ((null member) (or head (car org-todo-keywords-1)))
  10847. ((equal this final-done-word) nil) ;; -> make empty
  10848. ((null tail) nil) ;; -> first entry
  10849. ((memq interpret '(type priority))
  10850. (if (eq this-command last-command)
  10851. (car tail)
  10852. (if (> (length tail) 0)
  10853. (or done-word (car org-done-keywords))
  10854. nil)))
  10855. (t
  10856. (car tail))))
  10857. (org-state (or
  10858. (run-hook-with-args-until-success
  10859. 'org-todo-get-default-hook org-state org-last-state)
  10860. org-state))
  10861. (next (if org-state (concat " " org-state " ") " "))
  10862. (change-plist (list :type 'todo-state-change :from this :to org-state
  10863. :position startpos))
  10864. dolog now-done-p)
  10865. (when org-blocker-hook
  10866. (setq org-last-todo-state-is-todo
  10867. (not (member this org-done-keywords)))
  10868. (unless (save-excursion
  10869. (save-match-data
  10870. (org-with-wide-buffer
  10871. (run-hook-with-args-until-failure
  10872. 'org-blocker-hook change-plist))))
  10873. (if (org-called-interactively-p 'interactive)
  10874. (user-error "TODO state change from %s to %s blocked (by \"%s\")"
  10875. this org-state org-block-entry-blocking)
  10876. ;; fail silently
  10877. (message "TODO state change from %s to %s blocked (by \"%s\")"
  10878. this org-state org-block-entry-blocking)
  10879. (throw 'exit nil))))
  10880. (store-match-data match-data)
  10881. (replace-match next t t)
  10882. (unless (pos-visible-in-window-p hl-pos)
  10883. (message "TODO state changed to %s" (org-trim next)))
  10884. (unless head
  10885. (setq head (org-get-todo-sequence-head org-state)
  10886. ass (assoc head org-todo-kwd-alist)
  10887. interpret (nth 1 ass)
  10888. done-word (nth 3 ass)
  10889. final-done-word (nth 4 ass)))
  10890. (when (memq arg '(nextset previousset))
  10891. (message "Keyword-Set %d/%d: %s"
  10892. (- (length org-todo-sets) -1
  10893. (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
  10894. (length org-todo-sets)
  10895. (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
  10896. (setq org-last-todo-state-is-todo
  10897. (not (member org-state org-done-keywords)))
  10898. (setq now-done-p (and (member org-state org-done-keywords)
  10899. (not (member this org-done-keywords))))
  10900. (and logging (org-local-logging logging))
  10901. (when (and (or org-todo-log-states org-log-done)
  10902. (not (eq org-inhibit-logging t))
  10903. (not (memq arg '(nextset previousset))))
  10904. ;; we need to look at recording a time and note
  10905. (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
  10906. (nth 2 (assoc this org-todo-log-states))))
  10907. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  10908. (setq dolog 'time))
  10909. (when (and org-state
  10910. (member org-state org-not-done-keywords)
  10911. (not (member this org-not-done-keywords)))
  10912. ;; This is now a todo state and was not one before
  10913. ;; If there was a CLOSED time stamp, get rid of it.
  10914. (org-add-planning-info nil nil 'closed))
  10915. (when (and now-done-p org-log-done)
  10916. ;; It is now done, and it was not done before
  10917. (org-add-planning-info 'closed (org-current-effective-time))
  10918. (if (and (not dolog) (eq 'note org-log-done))
  10919. (org-add-log-setup 'done org-state this 'findpos 'note)))
  10920. (when (and org-state dolog)
  10921. ;; This is a non-nil state, and we need to log it
  10922. (org-add-log-setup 'state org-state this 'findpos dolog)))
  10923. ;; Fixup tag positioning
  10924. (org-todo-trigger-tag-changes org-state)
  10925. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  10926. (when org-provide-todo-statistics
  10927. (org-update-parent-todo-statistics))
  10928. (run-hooks 'org-after-todo-state-change-hook)
  10929. (if (and arg (not (member org-state org-done-keywords)))
  10930. (setq head (org-get-todo-sequence-head org-state)))
  10931. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  10932. ;; Do we need to trigger a repeat?
  10933. (when now-done-p
  10934. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  10935. ;; This is for the agenda, take a snapshot of the headline.
  10936. (save-match-data
  10937. (setq org-agenda-headline-snapshot-before-repeat
  10938. (org-get-heading))))
  10939. (org-auto-repeat-maybe org-state))
  10940. ;; Fixup cursor location if close to the keyword
  10941. (if (and (outline-on-heading-p)
  10942. (not (bolp))
  10943. (save-excursion (beginning-of-line 1)
  10944. (looking-at org-todo-line-regexp))
  10945. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  10946. (progn
  10947. (goto-char (or (match-end 2) (match-end 1)))
  10948. (and (looking-at " ") (just-one-space))))
  10949. (when org-trigger-hook
  10950. (save-excursion
  10951. (run-hook-with-args 'org-trigger-hook change-plist)))
  10952. (when commentp (org-toggle-comment))))))))
  10953. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  10954. "Block turning an entry into a TODO, using the hierarchy.
  10955. This checks whether the current task should be blocked from state
  10956. changes. Such blocking occurs when:
  10957. 1. The task has children which are not all in a completed state.
  10958. 2. A task has a parent with the property :ORDERED:, and there
  10959. are siblings prior to the current task with incomplete
  10960. status.
  10961. 3. The parent of the task is blocked because it has siblings that should
  10962. be done first, or is child of a block grandparent TODO entry."
  10963. (if (not org-enforce-todo-dependencies)
  10964. t ; if locally turned off don't block
  10965. (catch 'dont-block
  10966. ;; If this is not a todo state change, or if this entry is already DONE,
  10967. ;; do not block
  10968. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10969. (member (plist-get change-plist :from)
  10970. (cons 'done org-done-keywords))
  10971. (member (plist-get change-plist :to)
  10972. (cons 'todo org-not-done-keywords))
  10973. (not (plist-get change-plist :to)))
  10974. (throw 'dont-block t))
  10975. ;; If this task has children, and any are undone, it's blocked
  10976. (save-excursion
  10977. (org-back-to-heading t)
  10978. (let ((this-level (funcall outline-level)))
  10979. (outline-next-heading)
  10980. (let ((child-level (funcall outline-level)))
  10981. (while (and (not (eobp))
  10982. (> child-level this-level))
  10983. ;; this todo has children, check whether they are all
  10984. ;; completed
  10985. (if (and (not (org-entry-is-done-p))
  10986. (org-entry-is-todo-p))
  10987. (progn (setq org-block-entry-blocking (org-get-heading))
  10988. (throw 'dont-block nil)))
  10989. (outline-next-heading)
  10990. (setq child-level (funcall outline-level))))))
  10991. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  10992. ;; any previous siblings are undone, it's blocked
  10993. (save-excursion
  10994. (org-back-to-heading t)
  10995. (let* ((pos (point))
  10996. (parent-pos (and (org-up-heading-safe) (point))))
  10997. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10998. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10999. (forward-line 1)
  11000. (re-search-forward org-not-done-heading-regexp pos t))
  11001. (setq org-block-entry-blocking (match-string 0))
  11002. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  11003. ;; Search further up the hierarchy, to see if an ancestor is blocked
  11004. (while t
  11005. (goto-char parent-pos)
  11006. (if (not (looking-at org-not-done-heading-regexp))
  11007. (throw 'dont-block t)) ; do not block, parent is not a TODO
  11008. (setq pos (point))
  11009. (setq parent-pos (and (org-up-heading-safe) (point)))
  11010. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11011. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11012. (forward-line 1)
  11013. (re-search-forward org-not-done-heading-regexp pos t)
  11014. (setq org-block-entry-blocking (org-get-heading)))
  11015. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  11016. (defcustom org-track-ordered-property-with-tag nil
  11017. "Should the ORDERED property also be shown as a tag?
  11018. The ORDERED property decides if an entry should require subtasks to be
  11019. completed in sequence. Since a property is not very visible, setting
  11020. this option means that toggling the ORDERED property with the command
  11021. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  11022. not relevant for the behavior, but it makes things more visible.
  11023. Note that toggling the tag with tags commands will not change the property
  11024. and therefore not influence behavior!
  11025. This can be t, meaning the tag ORDERED should be used, It can also be a
  11026. string to select a different tag for this task."
  11027. :group 'org-todo
  11028. :type '(choice
  11029. (const :tag "No tracking" nil)
  11030. (const :tag "Track with ORDERED tag" t)
  11031. (string :tag "Use other tag")))
  11032. (defun org-toggle-ordered-property ()
  11033. "Toggle the ORDERED property of the current entry.
  11034. For better visibility, you can track the value of this property with a tag.
  11035. See variable `org-track-ordered-property-with-tag'."
  11036. (interactive)
  11037. (let* ((t1 org-track-ordered-property-with-tag)
  11038. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  11039. (save-excursion
  11040. (org-back-to-heading)
  11041. (if (org-entry-get nil "ORDERED")
  11042. (progn
  11043. (org-delete-property "ORDERED" "PROPERTIES")
  11044. (and tag (org-toggle-tag tag 'off))
  11045. (message "Subtasks can be completed in arbitrary order"))
  11046. (org-entry-put nil "ORDERED" "t")
  11047. (and tag (org-toggle-tag tag 'on))
  11048. (message "Subtasks must be completed in sequence")))))
  11049. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  11050. (defun org-block-todo-from-checkboxes (change-plist)
  11051. "Block turning an entry into a TODO, using checkboxes.
  11052. This checks whether the current task should be blocked from state
  11053. changes because there are unchecked boxes in this entry."
  11054. (if (not org-enforce-todo-checkbox-dependencies)
  11055. t ; if locally turned off don't block
  11056. (catch 'dont-block
  11057. ;; If this is not a todo state change, or if this entry is already DONE,
  11058. ;; do not block
  11059. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11060. (member (plist-get change-plist :from)
  11061. (cons 'done org-done-keywords))
  11062. (member (plist-get change-plist :to)
  11063. (cons 'todo org-not-done-keywords))
  11064. (not (plist-get change-plist :to)))
  11065. (throw 'dont-block t))
  11066. ;; If this task has checkboxes that are not checked, it's blocked
  11067. (save-excursion
  11068. (org-back-to-heading t)
  11069. (let ((beg (point)) end)
  11070. (outline-next-heading)
  11071. (setq end (point))
  11072. (goto-char beg)
  11073. (if (org-list-search-forward
  11074. (concat (org-item-beginning-re)
  11075. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  11076. "\\[[- ]\\]")
  11077. end t)
  11078. (progn
  11079. (if (boundp 'org-blocked-by-checkboxes)
  11080. (setq org-blocked-by-checkboxes t))
  11081. (throw 'dont-block nil)))))
  11082. t))) ; do not block
  11083. (defun org-entry-blocked-p ()
  11084. "Is the current entry blocked?"
  11085. (org-with-silent-modifications
  11086. (if (org-entry-get nil "NOBLOCKING")
  11087. nil ;; Never block this entry
  11088. (not (run-hook-with-args-until-failure
  11089. 'org-blocker-hook
  11090. (list :type 'todo-state-change
  11091. :position (point)
  11092. :from 'todo
  11093. :to 'done))))))
  11094. (defun org-update-statistics-cookies (all)
  11095. "Update the statistics cookie, either from TODO or from checkboxes.
  11096. This should be called with the cursor in a line with a statistics cookie."
  11097. (interactive "P")
  11098. (if all
  11099. (progn
  11100. (org-update-checkbox-count 'all)
  11101. (org-map-entries 'org-update-parent-todo-statistics))
  11102. (if (not (org-at-heading-p))
  11103. (org-update-checkbox-count)
  11104. (let ((pos (point-marker))
  11105. end l1 l2)
  11106. (ignore-errors (org-back-to-heading t))
  11107. (if (not (org-at-heading-p))
  11108. (org-update-checkbox-count)
  11109. (setq l1 (org-outline-level))
  11110. (setq end (save-excursion
  11111. (outline-next-heading)
  11112. (if (org-at-heading-p) (setq l2 (org-outline-level)))
  11113. (point)))
  11114. (if (and (save-excursion
  11115. (re-search-forward
  11116. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  11117. (not (save-excursion (re-search-forward
  11118. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  11119. (org-update-checkbox-count)
  11120. (if (and l2 (> l2 l1))
  11121. (progn
  11122. (goto-char end)
  11123. (org-update-parent-todo-statistics))
  11124. (goto-char pos)
  11125. (beginning-of-line 1)
  11126. (while (re-search-forward
  11127. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  11128. (point-at-eol) t)
  11129. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  11130. (goto-char pos)
  11131. (move-marker pos nil)))))
  11132. (defvar org-entry-property-inherited-from) ;; defined below
  11133. (defun org-update-parent-todo-statistics ()
  11134. "Update any statistics cookie in the parent of the current headline.
  11135. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  11136. the entire subtree and this will travel up the hierarchy and update
  11137. statistics everywhere."
  11138. (let* ((prop (save-excursion (org-up-heading-safe)
  11139. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  11140. (recursive (or (not org-hierarchical-todo-statistics)
  11141. (and prop (string-match "\\<recursive\\>" prop))))
  11142. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  11143. 0))
  11144. (first t)
  11145. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  11146. level ltoggle l1 new ndel
  11147. (cnt-all 0) (cnt-done 0) is-percent kwd
  11148. checkbox-beg ov ovs ove cookie-present)
  11149. (catch 'exit
  11150. (save-excursion
  11151. (beginning-of-line 1)
  11152. (setq ltoggle (funcall outline-level))
  11153. ;; Three situations are to consider:
  11154. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  11155. ;; to the top-level ancestor on the headline;
  11156. ;; 2. If parent has "recursive" property, repeat up to the
  11157. ;; headline setting that property, taking inheritance into
  11158. ;; account;
  11159. ;; 3. Else, move up to direct parent and proceed only once.
  11160. (while (and (setq level (org-up-heading-safe))
  11161. (or recursive first)
  11162. (>= (point) lim))
  11163. (setq first nil cookie-present nil)
  11164. (unless (and level
  11165. (not (string-match
  11166. "\\<checkbox\\>"
  11167. (downcase (or (org-entry-get nil "COOKIE_DATA")
  11168. "")))))
  11169. (throw 'exit nil))
  11170. (while (re-search-forward box-re (point-at-eol) t)
  11171. (setq cnt-all 0 cnt-done 0 cookie-present t)
  11172. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  11173. (save-match-data
  11174. (unless (outline-next-heading) (throw 'exit nil))
  11175. (while (and (looking-at org-complex-heading-regexp)
  11176. (> (setq l1 (length (match-string 1))) level))
  11177. (setq kwd (and (or recursive (= l1 ltoggle))
  11178. (match-string 2)))
  11179. (if (or (eq org-provide-todo-statistics 'all-headlines)
  11180. (and (listp org-provide-todo-statistics)
  11181. (or (member kwd org-provide-todo-statistics)
  11182. (member kwd org-done-keywords))))
  11183. (setq cnt-all (1+ cnt-all))
  11184. (if (eq org-provide-todo-statistics t)
  11185. (and kwd (setq cnt-all (1+ cnt-all)))))
  11186. (and (member kwd org-done-keywords)
  11187. (setq cnt-done (1+ cnt-done)))
  11188. (outline-next-heading)))
  11189. (setq new
  11190. (if is-percent
  11191. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  11192. (format "[%d/%d]" cnt-done cnt-all))
  11193. ndel (- (match-end 0) checkbox-beg))
  11194. ;; handle overlays when updating cookie from column view
  11195. (when (setq ov (car (overlays-at checkbox-beg)))
  11196. (setq ovs (overlay-start ov) ove (overlay-end ov))
  11197. (delete-overlay ov))
  11198. (goto-char checkbox-beg)
  11199. (insert new)
  11200. (delete-region (point) (+ (point) ndel))
  11201. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  11202. (when ov (move-overlay ov ovs ove)))
  11203. (when cookie-present
  11204. (run-hook-with-args 'org-after-todo-statistics-hook
  11205. cnt-done (- cnt-all cnt-done))))))
  11206. (run-hooks 'org-todo-statistics-hook)))
  11207. (defvar org-after-todo-statistics-hook nil
  11208. "Hook that is called after a TODO statistics cookie has been updated.
  11209. Each function is called with two arguments: the number of not-done entries
  11210. and the number of done entries.
  11211. For example, the following function, when added to this hook, will switch
  11212. an entry to DONE when all children are done, and back to TODO when new
  11213. entries are set to a TODO status. Note that this hook is only called
  11214. when there is a statistics cookie in the headline!
  11215. (defun org-summary-todo (n-done n-not-done)
  11216. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  11217. (let (org-log-done org-log-states) ; turn off logging
  11218. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  11219. ")
  11220. (defvar org-todo-statistics-hook nil
  11221. "Hook that is run whenever Org thinks TODO statistics should be updated.
  11222. This hook runs even if there is no statistics cookie present, in which case
  11223. `org-after-todo-statistics-hook' would not run.")
  11224. (defun org-todo-trigger-tag-changes (state)
  11225. "Apply the changes defined in `org-todo-state-tags-triggers'."
  11226. (let ((l org-todo-state-tags-triggers)
  11227. changes)
  11228. (when (or (not state) (equal state ""))
  11229. (setq changes (append changes (cdr (assoc "" l)))))
  11230. (when (and (stringp state) (> (length state) 0))
  11231. (setq changes (append changes (cdr (assoc state l)))))
  11232. (when (member state org-not-done-keywords)
  11233. (setq changes (append changes (cdr (assoc 'todo l)))))
  11234. (when (member state org-done-keywords)
  11235. (setq changes (append changes (cdr (assoc 'done l)))))
  11236. (dolist (c changes)
  11237. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  11238. (defun org-local-logging (value)
  11239. "Get logging settings from a property VALUE."
  11240. (let* (words w a)
  11241. ;; directly set the variables, they are already local.
  11242. (setq org-log-done nil
  11243. org-log-repeat nil
  11244. org-todo-log-states nil)
  11245. (setq words (org-split-string value))
  11246. (while (setq w (pop words))
  11247. (cond
  11248. ((setq a (assoc w org-startup-options))
  11249. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  11250. (set (nth 1 a) (nth 2 a))))
  11251. ((setq a (org-extract-log-state-settings w))
  11252. (and (member (car a) org-todo-keywords-1)
  11253. (push a org-todo-log-states)))))))
  11254. (defun org-get-todo-sequence-head (kwd)
  11255. "Return the head of the TODO sequence to which KWD belongs.
  11256. If KWD is not set, check if there is a text property remembering the
  11257. right sequence."
  11258. (let (p)
  11259. (cond
  11260. ((not kwd)
  11261. (or (get-text-property (point-at-bol) 'org-todo-head)
  11262. (progn
  11263. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  11264. nil (point-at-eol)))
  11265. (get-text-property p 'org-todo-head))))
  11266. ((not (member kwd org-todo-keywords-1))
  11267. (car org-todo-keywords-1))
  11268. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  11269. (defun org-fast-todo-selection ()
  11270. "Fast TODO keyword selection with single keys.
  11271. Returns the new TODO keyword, or nil if no state change should occur."
  11272. (let* ((fulltable org-todo-key-alist)
  11273. (done-keywords org-done-keywords) ;; needed for the faces.
  11274. (maxlen (apply 'max (mapcar
  11275. (lambda (x)
  11276. (if (stringp (car x)) (string-width (car x)) 0))
  11277. fulltable)))
  11278. (expert nil)
  11279. (fwidth (+ maxlen 3 1 3))
  11280. (ncol (/ (- (window-width) 4) fwidth))
  11281. tg cnt e c tbl
  11282. groups ingroup)
  11283. (save-excursion
  11284. (save-window-excursion
  11285. (if expert
  11286. (set-buffer (get-buffer-create " *Org todo*"))
  11287. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  11288. (erase-buffer)
  11289. (org-set-local 'org-done-keywords done-keywords)
  11290. (setq tbl fulltable cnt 0)
  11291. (while (setq e (pop tbl))
  11292. (cond
  11293. ((equal e '(:startgroup))
  11294. (push '() groups) (setq ingroup t)
  11295. (when (not (= cnt 0))
  11296. (setq cnt 0)
  11297. (insert "\n"))
  11298. (insert "{ "))
  11299. ((equal e '(:endgroup))
  11300. (setq ingroup nil cnt 0)
  11301. (insert "}\n"))
  11302. ((equal e '(:newline))
  11303. (when (not (= cnt 0))
  11304. (setq cnt 0)
  11305. (insert "\n")
  11306. (setq e (car tbl))
  11307. (while (equal (car tbl) '(:newline))
  11308. (insert "\n")
  11309. (setq tbl (cdr tbl)))))
  11310. (t
  11311. (setq tg (car e) c (cdr e))
  11312. (if ingroup (push tg (car groups)))
  11313. (setq tg (org-add-props tg nil 'face
  11314. (org-get-todo-face tg)))
  11315. (if (and (= cnt 0) (not ingroup)) (insert " "))
  11316. (insert "[" c "] " tg (make-string
  11317. (- fwidth 4 (length tg)) ?\ ))
  11318. (when (= (setq cnt (1+ cnt)) ncol)
  11319. (insert "\n")
  11320. (if ingroup (insert " "))
  11321. (setq cnt 0)))))
  11322. (insert "\n")
  11323. (goto-char (point-min))
  11324. (if (not expert) (org-fit-window-to-buffer))
  11325. (message "[a-z..]:Set [SPC]:clear")
  11326. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11327. (cond
  11328. ((or (= c ?\C-g)
  11329. (and (= c ?q) (not (rassoc c fulltable))))
  11330. (setq quit-flag t))
  11331. ((= c ?\ ) nil)
  11332. ((setq e (rassoc c fulltable) tg (car e))
  11333. tg)
  11334. (t (setq quit-flag t)))))))
  11335. (defun org-entry-is-todo-p ()
  11336. (member (org-get-todo-state) org-not-done-keywords))
  11337. (defun org-entry-is-done-p ()
  11338. (member (org-get-todo-state) org-done-keywords))
  11339. (defun org-get-todo-state ()
  11340. (save-excursion
  11341. (org-back-to-heading t)
  11342. (and (looking-at org-todo-line-regexp)
  11343. (match-end 2)
  11344. (match-string 2))))
  11345. (defun org-at-date-range-p (&optional inactive-ok)
  11346. "Is the cursor inside a date range?"
  11347. (interactive)
  11348. (save-excursion
  11349. (catch 'exit
  11350. (let ((pos (point)))
  11351. (skip-chars-backward "^[<\r\n")
  11352. (skip-chars-backward "<[")
  11353. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11354. (>= (match-end 0) pos)
  11355. (throw 'exit t))
  11356. (skip-chars-backward "^<[\r\n")
  11357. (skip-chars-backward "<[")
  11358. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11359. (>= (match-end 0) pos)
  11360. (throw 'exit t)))
  11361. nil)))
  11362. (defun org-get-repeat (&optional tagline)
  11363. "Check if there is a deadline/schedule with repeater in this entry."
  11364. (save-match-data
  11365. (save-excursion
  11366. (org-back-to-heading t)
  11367. (and (re-search-forward (if tagline
  11368. (concat tagline "\\s-*" org-repeat-re)
  11369. org-repeat-re)
  11370. (org-entry-end-position) t)
  11371. (match-string-no-properties 1)))))
  11372. (defvar org-last-changed-timestamp)
  11373. (defvar org-last-inserted-timestamp)
  11374. (defvar org-log-post-message)
  11375. (defvar org-log-note-purpose)
  11376. (defvar org-log-note-how)
  11377. (defvar org-log-note-extra)
  11378. (defun org-auto-repeat-maybe (done-word)
  11379. "Check if the current headline contains a repeated deadline/schedule.
  11380. If yes, set TODO state back to what it was and change the base date
  11381. of repeating deadline/scheduled time stamps to new date.
  11382. This function is run automatically after each state change to a DONE state."
  11383. ;; last-state is dynamically scoped into this function
  11384. (let* ((repeat (org-get-repeat))
  11385. (aa (assoc org-last-state org-todo-kwd-alist))
  11386. (interpret (nth 1 aa))
  11387. (head (nth 2 aa))
  11388. (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
  11389. (msg "Entry repeats: ")
  11390. (org-log-done nil)
  11391. (org-todo-log-states nil)
  11392. re type n what ts time to-state)
  11393. (when repeat
  11394. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  11395. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  11396. org-todo-repeat-to-state))
  11397. (unless (and to-state (member to-state org-todo-keywords-1))
  11398. (setq to-state (if (eq interpret 'type) org-last-state head)))
  11399. (org-todo to-state)
  11400. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  11401. (org-entry-put nil "LAST_REPEAT" (format-time-string
  11402. (org-time-stamp-format t t))))
  11403. (when org-log-repeat
  11404. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  11405. (memq 'org-add-log-note post-command-hook))
  11406. ;; OK, we are already setup for some record
  11407. (if (eq org-log-repeat 'note)
  11408. ;; make sure we take a note, not only a time stamp
  11409. (setq org-log-note-how 'note))
  11410. ;; Set up for taking a record
  11411. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  11412. org-last-state
  11413. 'findpos org-log-repeat)))
  11414. (org-back-to-heading t)
  11415. (org-add-planning-info nil nil 'closed)
  11416. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11417. org-deadline-time-regexp "\\)\\|\\("
  11418. org-ts-regexp "\\)"))
  11419. (while (re-search-forward
  11420. re (save-excursion (outline-next-heading) (point)) t)
  11421. (setq type (if (match-end 1) org-scheduled-string
  11422. (if (match-end 3) org-deadline-string "Plain:"))
  11423. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  11424. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts)
  11425. (setq n (string-to-number (match-string 2 ts))
  11426. what (match-string 3 ts))
  11427. (if (equal what "w") (setq n (* n 7) what "d"))
  11428. (if (and (equal what "h") (not (string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts)))
  11429. (error "Cannot repeat in Repeat in %d hour(s) because no hour has been set" n))
  11430. ;; Preparation, see if we need to modify the start date for the change
  11431. (when (match-end 1)
  11432. (setq time (save-match-data (org-time-string-to-time ts)))
  11433. (cond
  11434. ((equal (match-string 1 ts) ".")
  11435. ;; Shift starting date to today
  11436. (org-timestamp-change
  11437. (- (org-today) (time-to-days time))
  11438. 'day))
  11439. ((equal (match-string 1 ts) "+")
  11440. (let ((nshiftmax 10) (nshift 0))
  11441. (while (or (= nshift 0)
  11442. (<= (time-to-days time)
  11443. (time-to-days (current-time))))
  11444. (when (= (incf nshift) nshiftmax)
  11445. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  11446. (error "Abort")))
  11447. (org-timestamp-change n (cdr (assoc what whata)))
  11448. (org-at-timestamp-p t)
  11449. (setq ts (match-string 1))
  11450. (setq time (save-match-data (org-time-string-to-time ts)))))
  11451. (org-timestamp-change (- n) (cdr (assoc what whata)))
  11452. ;; rematch, so that we have everything in place for the real shift
  11453. (org-at-timestamp-p t)
  11454. (setq ts (match-string 1))
  11455. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))))
  11456. (save-excursion (org-timestamp-change n (cdr (assoc what whata)) nil t))
  11457. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  11458. (setq org-log-post-message msg)
  11459. (message "%s" msg))))
  11460. (defun org-show-todo-tree (arg)
  11461. "Make a compact tree which shows all headlines marked with TODO.
  11462. The tree will show the lines where the regexp matches, and all higher
  11463. headlines above the match.
  11464. With a \\[universal-argument] prefix, prompt for a regexp to match.
  11465. With a numeric prefix N, construct a sparse tree for the Nth element
  11466. of `org-todo-keywords-1'."
  11467. (interactive "P")
  11468. (let ((case-fold-search nil)
  11469. (kwd-re
  11470. (cond ((null arg) org-not-done-regexp)
  11471. ((equal arg '(4))
  11472. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  11473. (mapcar 'list org-todo-keywords-1))))
  11474. (concat "\\("
  11475. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  11476. "\\)\\>")))
  11477. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  11478. (regexp-quote (nth (1- (prefix-numeric-value arg))
  11479. org-todo-keywords-1)))
  11480. (t (error "Invalid prefix argument: %s" arg)))))
  11481. (message "%d TODO entries found"
  11482. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  11483. (defun org-deadline (arg &optional time)
  11484. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  11485. With one universal prefix argument, remove any deadline from the item.
  11486. With two universal prefix arguments, prompt for a warning delay.
  11487. With argument TIME, set the deadline at the corresponding date. TIME
  11488. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11489. (interactive "P")
  11490. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11491. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11492. 'region-start-level 'region))
  11493. org-loop-over-headlines-in-active-region)
  11494. (org-map-entries
  11495. `(org-deadline ',arg ,time)
  11496. org-loop-over-headlines-in-active-region
  11497. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11498. (let* ((old-date (org-entry-get nil "DEADLINE"))
  11499. (old-date-time (if old-date (org-time-string-to-time old-date)))
  11500. (repeater (and old-date
  11501. (string-match
  11502. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11503. old-date)
  11504. (match-string 1 old-date))))
  11505. (cond
  11506. ((equal arg '(4))
  11507. (when (and old-date org-log-redeadline)
  11508. (org-add-log-setup 'deldeadline nil old-date 'findpos
  11509. org-log-redeadline))
  11510. (org-remove-timestamp-with-keyword org-deadline-string)
  11511. (message "Item no longer has a deadline."))
  11512. ((equal arg '(16))
  11513. (save-excursion
  11514. (if (re-search-forward
  11515. org-deadline-time-regexp
  11516. (save-excursion (outline-next-heading) (point)) t)
  11517. (let* ((rpl0 (match-string 1))
  11518. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  11519. (replace-match
  11520. (concat org-deadline-string
  11521. " <" rpl
  11522. (format " -%dd"
  11523. (abs
  11524. (- (time-to-days
  11525. (save-match-data
  11526. (org-read-date nil t nil "Warn starting from" old-date-time)))
  11527. (time-to-days old-date-time))))
  11528. ">") t t))
  11529. (user-error "No deadline information to update"))))
  11530. (t
  11531. (org-add-planning-info 'deadline time 'closed)
  11532. (when (and old-date org-log-redeadline
  11533. (not (equal old-date
  11534. (substring org-last-inserted-timestamp 1 -1))))
  11535. (org-add-log-setup 'redeadline nil old-date 'findpos
  11536. org-log-redeadline))
  11537. (when repeater
  11538. (save-excursion
  11539. (org-back-to-heading t)
  11540. (when (re-search-forward (concat org-deadline-string " "
  11541. org-last-inserted-timestamp)
  11542. (save-excursion
  11543. (outline-next-heading) (point)) t)
  11544. (goto-char (1- (match-end 0)))
  11545. (insert " " repeater)
  11546. (setq org-last-inserted-timestamp
  11547. (concat (substring org-last-inserted-timestamp 0 -1)
  11548. " " repeater
  11549. (substring org-last-inserted-timestamp -1))))))
  11550. (message "Deadline on %s" org-last-inserted-timestamp))))))
  11551. (defun org-schedule (arg &optional time)
  11552. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  11553. With one universal prefix argument, remove any scheduling date from the item.
  11554. With two universal prefix arguments, prompt for a delay cookie.
  11555. With argument TIME, scheduled at the corresponding date. TIME can
  11556. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11557. (interactive "P")
  11558. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11559. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11560. 'region-start-level 'region))
  11561. org-loop-over-headlines-in-active-region)
  11562. (org-map-entries
  11563. `(org-schedule ',arg ,time)
  11564. org-loop-over-headlines-in-active-region
  11565. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11566. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  11567. (old-date-time (if old-date (org-time-string-to-time old-date)))
  11568. (repeater (and old-date
  11569. (string-match
  11570. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11571. old-date)
  11572. (match-string 1 old-date))))
  11573. (cond
  11574. ((equal arg '(4))
  11575. (progn
  11576. (when (and old-date org-log-reschedule)
  11577. (org-add-log-setup 'delschedule nil old-date 'findpos
  11578. org-log-reschedule))
  11579. (org-remove-timestamp-with-keyword org-scheduled-string)
  11580. (message "Item is no longer scheduled.")))
  11581. ((equal arg '(16))
  11582. (save-excursion
  11583. (if (re-search-forward
  11584. org-scheduled-time-regexp
  11585. (save-excursion (outline-next-heading) (point)) t)
  11586. (let* ((rpl0 (match-string 1))
  11587. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  11588. (replace-match
  11589. (concat org-scheduled-string
  11590. " <" rpl
  11591. (format " -%dd"
  11592. (abs
  11593. (- (time-to-days
  11594. (save-match-data
  11595. (org-read-date nil t nil "Delay until" old-date-time)))
  11596. (time-to-days old-date-time))))
  11597. ">") t t))
  11598. (user-error "No scheduled information to update"))))
  11599. (t
  11600. (org-add-planning-info 'scheduled time 'closed)
  11601. (when (and old-date org-log-reschedule
  11602. (not (equal old-date
  11603. (substring org-last-inserted-timestamp 1 -1))))
  11604. (org-add-log-setup 'reschedule nil old-date 'findpos
  11605. org-log-reschedule))
  11606. (when repeater
  11607. (save-excursion
  11608. (org-back-to-heading t)
  11609. (when (re-search-forward (concat org-scheduled-string " "
  11610. org-last-inserted-timestamp)
  11611. (save-excursion
  11612. (outline-next-heading) (point)) t)
  11613. (goto-char (1- (match-end 0)))
  11614. (insert " " repeater)
  11615. (setq org-last-inserted-timestamp
  11616. (concat (substring org-last-inserted-timestamp 0 -1)
  11617. " " repeater
  11618. (substring org-last-inserted-timestamp -1))))))
  11619. (message "Scheduled to %s" org-last-inserted-timestamp))))))
  11620. (defun org-get-scheduled-time (pom &optional inherit)
  11621. "Get the scheduled time as a time tuple, of a format suitable
  11622. for calling org-schedule with, or if there is no scheduling,
  11623. returns nil."
  11624. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  11625. (when time
  11626. (apply 'encode-time (org-parse-time-string time)))))
  11627. (defun org-get-deadline-time (pom &optional inherit)
  11628. "Get the deadline as a time tuple, of a format suitable for
  11629. calling org-deadline with, or if there is no scheduling, returns
  11630. nil."
  11631. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  11632. (when time
  11633. (apply 'encode-time (org-parse-time-string time)))))
  11634. (defun org-remove-timestamp-with-keyword (keyword)
  11635. "Remove all time stamps with KEYWORD in the current entry."
  11636. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  11637. beg)
  11638. (save-excursion
  11639. (org-back-to-heading t)
  11640. (setq beg (point))
  11641. (outline-next-heading)
  11642. (while (re-search-backward re beg t)
  11643. (replace-match "")
  11644. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  11645. (equal (char-before) ?\ ))
  11646. (backward-delete-char 1)
  11647. (if (string-match "^[ \t]*$" (buffer-substring
  11648. (point-at-bol) (point-at-eol)))
  11649. (delete-region (point-at-bol)
  11650. (min (point-max) (1+ (point-at-eol))))))))))
  11651. (defun org-add-planning-info (what &optional time &rest remove)
  11652. "Insert new timestamp with keyword in the line directly after the headline.
  11653. WHAT indicates what kind of time stamp to add. TIME indicates the time to use.
  11654. If non is given, the user is prompted for a date.
  11655. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  11656. be removed."
  11657. (interactive)
  11658. (let (org-time-was-given org-end-time-was-given ts
  11659. end default-time default-input)
  11660. (catch 'exit
  11661. (when (and (memq what '(scheduled deadline))
  11662. (or (not time)
  11663. (and (stringp time)
  11664. (string-match "^[-+]+[0-9]" time))))
  11665. ;; Try to get a default date/time from existing timestamp
  11666. (save-excursion
  11667. (org-back-to-heading t)
  11668. (setq end (save-excursion (outline-next-heading) (point)))
  11669. (when (re-search-forward (if (eq what 'scheduled)
  11670. org-scheduled-time-regexp
  11671. org-deadline-time-regexp)
  11672. end t)
  11673. (setq ts (match-string 1)
  11674. default-time
  11675. (apply 'encode-time (org-parse-time-string ts))
  11676. default-input (and ts (org-get-compact-tod ts))))))
  11677. (when what
  11678. (setq time
  11679. (if (stringp time)
  11680. ;; This is a string (relative or absolute), set proper date
  11681. (apply 'encode-time
  11682. (org-read-date-analyze
  11683. time default-time (decode-time default-time)))
  11684. ;; If necessary, get the time from the user
  11685. (or time (org-read-date nil 'to-time nil nil
  11686. default-time default-input)))))
  11687. (when (and org-insert-labeled-timestamps-at-point
  11688. (member what '(scheduled deadline)))
  11689. (insert
  11690. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  11691. (org-insert-time-stamp time org-time-was-given
  11692. nil nil nil (list org-end-time-was-given))
  11693. (setq what nil))
  11694. (save-excursion
  11695. (save-restriction
  11696. (let (col list elt ts buffer-invisibility-spec)
  11697. (org-back-to-heading t)
  11698. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
  11699. (goto-char (match-end 1))
  11700. (setq col (current-column))
  11701. (goto-char (match-end 0))
  11702. (if (eobp) (insert "\n") (forward-char 1))
  11703. (when (and (not what)
  11704. (not (looking-at
  11705. (concat "[ \t]*"
  11706. org-keyword-time-not-clock-regexp))))
  11707. ;; Nothing to add, nothing to remove...... :-)
  11708. (throw 'exit nil))
  11709. (if (and (not (looking-at org-outline-regexp))
  11710. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  11711. "[^\r\n]*"))
  11712. (not (equal (match-string 1) org-clock-string)))
  11713. (narrow-to-region (match-beginning 0) (match-end 0))
  11714. (insert-before-markers "\n")
  11715. (backward-char 1)
  11716. (narrow-to-region (point) (point))
  11717. (and org-adapt-indentation (org-indent-to-column col)))
  11718. ;; Check if we have to remove something.
  11719. (setq list (cons what remove))
  11720. (while list
  11721. (setq elt (pop list))
  11722. (when (or (and (eq elt 'scheduled)
  11723. (re-search-forward org-scheduled-time-regexp nil t))
  11724. (and (eq elt 'deadline)
  11725. (re-search-forward org-deadline-time-regexp nil t))
  11726. (and (eq elt 'closed)
  11727. (re-search-forward org-closed-time-regexp nil t)))
  11728. (replace-match "")
  11729. (if (looking-at "--+<[^>]+>") (replace-match ""))))
  11730. (and (looking-at "[ \t]+") (replace-match ""))
  11731. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  11732. (when what
  11733. (insert
  11734. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  11735. (cond ((eq what 'scheduled) org-scheduled-string)
  11736. ((eq what 'deadline) org-deadline-string)
  11737. ((eq what 'closed) org-closed-string))
  11738. " ")
  11739. (setq ts (org-insert-time-stamp
  11740. time
  11741. (or org-time-was-given
  11742. (and (eq what 'closed) org-log-done-with-time))
  11743. (eq what 'closed)
  11744. nil nil (list org-end-time-was-given)))
  11745. (insert
  11746. (if (not (or (bolp) (eq (char-before) ?\ )
  11747. (memq (char-after) '(32 10))
  11748. (eobp))) " " ""))
  11749. (end-of-line 1))
  11750. (goto-char (point-min))
  11751. (widen)
  11752. (if (and (looking-at "[ \t]*\n")
  11753. (equal (char-before) ?\n))
  11754. (delete-region (1- (point)) (point-at-eol)))
  11755. ts))))))
  11756. (defvar org-log-note-marker (make-marker))
  11757. (defvar org-log-note-purpose nil)
  11758. (defvar org-log-note-state nil)
  11759. (defvar org-log-note-previous-state nil)
  11760. (defvar org-log-note-how nil)
  11761. (defvar org-log-note-extra nil)
  11762. (defvar org-log-note-window-configuration nil)
  11763. (defvar org-log-note-return-to (make-marker))
  11764. (defvar org-log-note-effective-time nil
  11765. "Remembered current time so that dynamically scoped
  11766. `org-extend-today-until' affects tha timestamps in state change
  11767. log")
  11768. (defvar org-log-post-message nil
  11769. "Message to be displayed after a log note has been stored.
  11770. The auto-repeater uses this.")
  11771. (defun org-add-note ()
  11772. "Add a note to the current entry.
  11773. This is done in the same way as adding a state change note."
  11774. (interactive)
  11775. (org-add-log-setup 'note nil nil 'findpos nil))
  11776. (defvar org-property-end-re)
  11777. (defun org-add-log-setup (&optional purpose state prev-state
  11778. findpos how extra)
  11779. "Set up the post command hook to take a note.
  11780. If this is about to TODO state change, the new state is expected in STATE.
  11781. When FINDPOS is non-nil, find the correct position for the note in
  11782. the current entry. If not, assume that it can be inserted at point.
  11783. HOW is an indicator what kind of note should be created.
  11784. EXTRA is additional text that will be inserted into the notes buffer."
  11785. (let* ((org-log-into-drawer (org-log-into-drawer))
  11786. (drawer (cond ((stringp org-log-into-drawer)
  11787. org-log-into-drawer)
  11788. (org-log-into-drawer "LOGBOOK"))))
  11789. (save-restriction
  11790. (save-excursion
  11791. (when findpos
  11792. (org-back-to-heading t)
  11793. (narrow-to-region (point) (save-excursion
  11794. (outline-next-heading) (point)))
  11795. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"
  11796. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  11797. "[^\r\n]*\\)?"))
  11798. (goto-char (match-end 0))
  11799. (cond
  11800. (drawer
  11801. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  11802. nil t)
  11803. (progn
  11804. (goto-char (match-end 0))
  11805. (or org-log-states-order-reversed
  11806. (and (re-search-forward org-property-end-re nil t)
  11807. (goto-char (1- (match-beginning 0))))))
  11808. (insert "\n:" drawer ":\n:END:")
  11809. (beginning-of-line 0)
  11810. (org-indent-line)
  11811. (beginning-of-line 2)
  11812. (org-indent-line)
  11813. (end-of-line 0)))
  11814. ((and org-log-state-notes-insert-after-drawers
  11815. (save-excursion
  11816. (forward-line) (looking-at org-drawer-regexp)))
  11817. (forward-line)
  11818. (while (looking-at org-drawer-regexp)
  11819. (goto-char (match-end 0))
  11820. (re-search-forward org-property-end-re (point-max) t)
  11821. (forward-line))
  11822. (forward-line -1)))
  11823. (unless org-log-states-order-reversed
  11824. (and (= (char-after) ?\n) (forward-char 1))
  11825. (org-skip-over-state-notes)
  11826. (skip-chars-backward " \t\n\r")))
  11827. (move-marker org-log-note-marker (point))
  11828. (setq org-log-note-purpose purpose
  11829. org-log-note-state state
  11830. org-log-note-previous-state prev-state
  11831. org-log-note-how how
  11832. org-log-note-extra extra
  11833. org-log-note-effective-time (org-current-effective-time))
  11834. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  11835. (defun org-skip-over-state-notes ()
  11836. "Skip past the list of State notes in an entry."
  11837. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  11838. (when (ignore-errors (goto-char (org-in-item-p)))
  11839. (let* ((struct (org-list-struct))
  11840. (prevs (org-list-prevs-alist struct)))
  11841. (while (looking-at "[ \t]*- State")
  11842. (goto-char (or (org-list-get-next-item (point) struct prevs)
  11843. (org-list-get-item-end (point) struct)))))))
  11844. (defun org-add-log-note (&optional purpose)
  11845. "Pop up a window for taking a note, and add this note later at point."
  11846. (remove-hook 'post-command-hook 'org-add-log-note)
  11847. (setq org-log-note-window-configuration (current-window-configuration))
  11848. (delete-other-windows)
  11849. (move-marker org-log-note-return-to (point))
  11850. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  11851. (goto-char org-log-note-marker)
  11852. (org-switch-to-buffer-other-window "*Org Note*")
  11853. (erase-buffer)
  11854. (if (memq org-log-note-how '(time state))
  11855. (let (current-prefix-arg) (org-store-log-note))
  11856. (let ((org-inhibit-startup t)) (org-mode))
  11857. (insert (format "# Insert note for %s.
  11858. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  11859. (cond
  11860. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  11861. ((eq org-log-note-purpose 'done) "closed todo item")
  11862. ((eq org-log-note-purpose 'state)
  11863. (format "state change from \"%s\" to \"%s\""
  11864. (or org-log-note-previous-state "")
  11865. (or org-log-note-state "")))
  11866. ((eq org-log-note-purpose 'reschedule)
  11867. "rescheduling")
  11868. ((eq org-log-note-purpose 'delschedule)
  11869. "no longer scheduled")
  11870. ((eq org-log-note-purpose 'redeadline)
  11871. "changing deadline")
  11872. ((eq org-log-note-purpose 'deldeadline)
  11873. "removing deadline")
  11874. ((eq org-log-note-purpose 'refile)
  11875. "refiling")
  11876. ((eq org-log-note-purpose 'note)
  11877. "this entry")
  11878. (t (error "This should not happen")))))
  11879. (if org-log-note-extra (insert org-log-note-extra))
  11880. (org-set-local 'org-finish-function 'org-store-log-note)
  11881. (run-hooks 'org-log-buffer-setup-hook)))
  11882. (defvar org-note-abort nil) ; dynamically scoped
  11883. (defun org-store-log-note ()
  11884. "Finish taking a log note, and insert it to where it belongs."
  11885. (let ((txt (buffer-string)))
  11886. (kill-buffer (current-buffer))
  11887. (let ((note (cdr (assq org-log-note-purpose org-log-note-headings)))
  11888. lines ind bul)
  11889. (while (string-match "\\`# .*\n[ \t\n]*" txt)
  11890. (setq txt (replace-match "" t t txt)))
  11891. (if (string-match "\\s-+\\'" txt)
  11892. (setq txt (replace-match "" t t txt)))
  11893. (setq lines (org-split-string txt "\n"))
  11894. (when (and note (string-match "\\S-" note))
  11895. (setq note
  11896. (org-replace-escapes
  11897. note
  11898. (list (cons "%u" (user-login-name))
  11899. (cons "%U" user-full-name)
  11900. (cons "%t" (format-time-string
  11901. (org-time-stamp-format 'long 'inactive)
  11902. org-log-note-effective-time))
  11903. (cons "%T" (format-time-string
  11904. (org-time-stamp-format 'long nil)
  11905. org-log-note-effective-time))
  11906. (cons "%d" (format-time-string
  11907. (org-time-stamp-format nil 'inactive)
  11908. org-log-note-effective-time))
  11909. (cons "%D" (format-time-string
  11910. (org-time-stamp-format nil nil)
  11911. org-log-note-effective-time))
  11912. (cons "%s" (if org-log-note-state
  11913. (concat "\"" org-log-note-state "\"")
  11914. ""))
  11915. (cons "%S" (if org-log-note-previous-state
  11916. (concat "\"" org-log-note-previous-state "\"")
  11917. "\"\"")))))
  11918. (if lines (setq note (concat note " \\\\")))
  11919. (push note lines))
  11920. (when (or current-prefix-arg org-note-abort)
  11921. (when org-log-into-drawer
  11922. (org-remove-empty-drawer-at
  11923. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  11924. org-log-note-marker))
  11925. (setq lines nil))
  11926. (when lines
  11927. (with-current-buffer (marker-buffer org-log-note-marker)
  11928. (save-excursion
  11929. (goto-char org-log-note-marker)
  11930. (move-marker org-log-note-marker nil)
  11931. (end-of-line 1)
  11932. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  11933. (setq ind (save-excursion
  11934. (if (ignore-errors (goto-char (org-in-item-p)))
  11935. (let ((struct (org-list-struct)))
  11936. (org-list-get-ind
  11937. (org-list-get-top-point struct) struct))
  11938. (skip-chars-backward " \r\t\n")
  11939. (cond
  11940. ((and (org-at-heading-p)
  11941. org-adapt-indentation)
  11942. (1+ (org-current-level)))
  11943. ((org-at-heading-p) 0)
  11944. (t (org-get-indentation))))))
  11945. (setq bul (org-list-bullet-string "-"))
  11946. (org-indent-line-to ind)
  11947. (insert bul (pop lines))
  11948. (let ((ind-body (+ (length bul) ind)))
  11949. (while lines
  11950. (insert "\n")
  11951. (org-indent-line-to ind-body)
  11952. (insert (pop lines))))
  11953. (message "Note stored")
  11954. (org-back-to-heading t)
  11955. (org-cycle-hide-drawers 'children))))))
  11956. (set-window-configuration org-log-note-window-configuration)
  11957. (with-current-buffer (marker-buffer org-log-note-return-to)
  11958. (goto-char org-log-note-return-to))
  11959. (move-marker org-log-note-return-to nil)
  11960. (and org-log-post-message (message "%s" org-log-post-message)))
  11961. (defun org-remove-empty-drawer-at (drawer pos)
  11962. "Remove an empty drawer DRAWER at position POS.
  11963. POS may also be a marker."
  11964. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  11965. (save-excursion
  11966. (save-restriction
  11967. (widen)
  11968. (goto-char pos)
  11969. (if (org-in-regexp
  11970. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  11971. (replace-match ""))))))
  11972. (defvar org-ts-type nil)
  11973. (defun org-sparse-tree (&optional arg type)
  11974. "Create a sparse tree, prompt for the details.
  11975. This command can create sparse trees. You first need to select the type
  11976. of match used to create the tree:
  11977. t Show all TODO entries.
  11978. T Show entries with a specific TODO keyword.
  11979. m Show entries selected by a tags/property match.
  11980. p Enter a property name and its value (both with completion on existing
  11981. names/values) and show entries with that property.
  11982. r Show entries matching a regular expression (`/' can be used as well).
  11983. b Show deadlines and scheduled items before a date.
  11984. a Show deadlines and scheduled items after a date.
  11985. d Show deadlines due within `org-deadline-warning-days'.
  11986. D Show deadlines and scheduled items between a date range."
  11987. (interactive "P")
  11988. (let (ans kwd value ts-type)
  11989. (setq type (or type org-sparse-tree-default-date-type))
  11990. (setq org-ts-type type)
  11991. (message "Sparse tree: [/]regexp [t]odo [T]odo-kwd [m]atch [p]roperty\n [d]eadlines [b]efore-date [a]fter-date [D]ates range\n [c]ycle through date types: %s"
  11992. (cond ((eq type 'all) "all timestamps")
  11993. ((eq type 'scheduled) "only scheduled")
  11994. ((eq type 'deadline) "only deadline")
  11995. ((eq type 'active) "only active timestamps")
  11996. ((eq type 'inactive) "only inactive timestamps")
  11997. ((eq type 'scheduled-or-deadline) "scheduled/deadline")
  11998. (t "scheduled/deadline")))
  11999. (setq ans (read-char-exclusive))
  12000. (cond
  12001. ((equal ans ?c)
  12002. (org-sparse-tree arg (cadr (member type '(scheduled-or-deadline all scheduled deadline active inactive)))))
  12003. ((equal ans ?d)
  12004. (call-interactively 'org-check-deadlines))
  12005. ((equal ans ?b)
  12006. (call-interactively 'org-check-before-date))
  12007. ((equal ans ?a)
  12008. (call-interactively 'org-check-after-date))
  12009. ((equal ans ?D)
  12010. (call-interactively 'org-check-dates-range))
  12011. ((equal ans ?t)
  12012. (call-interactively 'org-show-todo-tree))
  12013. ((equal ans ?T)
  12014. (org-show-todo-tree '(4)))
  12015. ((member ans '(?T ?m))
  12016. (call-interactively 'org-match-sparse-tree))
  12017. ((member ans '(?p ?P))
  12018. (setq kwd (org-icompleting-read "Property: "
  12019. (mapcar 'list (org-buffer-property-keys))))
  12020. (setq value (org-icompleting-read "Value: "
  12021. (mapcar 'list (org-property-values kwd))))
  12022. (unless (string-match "\\`{.*}\\'" value)
  12023. (setq value (concat "\"" value "\"")))
  12024. (org-match-sparse-tree arg (concat kwd "=" value)))
  12025. ((member ans '(?r ?R ?/))
  12026. (call-interactively 'org-occur))
  12027. (t (error "No such sparse tree command \"%c\"" ans)))))
  12028. (defvar org-occur-highlights nil
  12029. "List of overlays used for occur matches.")
  12030. (make-variable-buffer-local 'org-occur-highlights)
  12031. (defvar org-occur-parameters nil
  12032. "Parameters of the active org-occur calls.
  12033. This is a list, each call to org-occur pushes as cons cell,
  12034. containing the regular expression and the callback, onto the list.
  12035. The list can contain several entries if `org-occur' has been called
  12036. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  12037. will only contain one set of parameters. When the highlights are
  12038. removed (for example with `C-c C-c', or with the next edit (depending
  12039. on `org-remove-highlights-with-change'), this variable is emptied
  12040. as well.")
  12041. (make-variable-buffer-local 'org-occur-parameters)
  12042. (defun org-occur (regexp &optional keep-previous callback)
  12043. "Make a compact tree which shows all matches of REGEXP.
  12044. The tree will show the lines where the regexp matches, and all higher
  12045. headlines above the match. It will also show the heading after the match,
  12046. to make sure editing the matching entry is easy.
  12047. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  12048. call to `org-occur' will be kept, to allow stacking of calls to this
  12049. command.
  12050. If CALLBACK is non-nil, it is a function which is called to confirm
  12051. that the match should indeed be shown."
  12052. (interactive "sRegexp: \nP")
  12053. (when (equal regexp "")
  12054. (error "Regexp cannot be empty"))
  12055. (unless keep-previous
  12056. (org-remove-occur-highlights nil nil t))
  12057. (push (cons regexp callback) org-occur-parameters)
  12058. (let ((cnt 0))
  12059. (save-excursion
  12060. (goto-char (point-min))
  12061. (if (or (not keep-previous) ; do not want to keep
  12062. (not org-occur-highlights)) ; no previous matches
  12063. ;; hide everything
  12064. (org-overview))
  12065. (while (re-search-forward regexp nil t)
  12066. (when (or (not callback)
  12067. (save-match-data (funcall callback)))
  12068. (setq cnt (1+ cnt))
  12069. (when org-highlight-sparse-tree-matches
  12070. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  12071. (org-show-context 'occur-tree))))
  12072. (when org-remove-highlights-with-change
  12073. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  12074. nil 'local))
  12075. (unless org-sparse-tree-open-archived-trees
  12076. (org-hide-archived-subtrees (point-min) (point-max)))
  12077. (run-hooks 'org-occur-hook)
  12078. (if (org-called-interactively-p 'interactive)
  12079. (message "%d match(es) for regexp %s" cnt regexp))
  12080. cnt))
  12081. (defun org-occur-next-match (&optional n reset)
  12082. "Function for `next-error-function' to find sparse tree matches.
  12083. N is the number of matches to move, when negative move backwards.
  12084. RESET is entirely ignored - this function always goes back to the
  12085. starting point when no match is found."
  12086. (let* ((limit (if (< n 0) (point-min) (point-max)))
  12087. (search-func (if (< n 0)
  12088. 'previous-single-char-property-change
  12089. 'next-single-char-property-change))
  12090. (n (abs n))
  12091. (pos (point))
  12092. p1)
  12093. (catch 'exit
  12094. (while (setq p1 (funcall search-func (point) 'org-type))
  12095. (when (equal p1 limit)
  12096. (goto-char pos)
  12097. (error "No more matches"))
  12098. (when (equal (get-char-property p1 'org-type) 'org-occur)
  12099. (setq n (1- n))
  12100. (when (= n 0)
  12101. (goto-char p1)
  12102. (throw 'exit (point))))
  12103. (goto-char p1))
  12104. (goto-char p1)
  12105. (error "No more matches"))))
  12106. (defun org-show-context (&optional key)
  12107. "Make sure point and context are visible.
  12108. How much context is shown depends upon the variables
  12109. `org-show-hierarchy-above', `org-show-following-heading',
  12110. `org-show-entry-below' and `org-show-siblings'."
  12111. (let ((heading-p (org-at-heading-p t))
  12112. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  12113. (following-p (org-get-alist-option org-show-following-heading key))
  12114. (entry-p (org-get-alist-option org-show-entry-below key))
  12115. (siblings-p (org-get-alist-option org-show-siblings key)))
  12116. ;; Show heading or entry text
  12117. (if (and heading-p (not entry-p))
  12118. (org-flag-heading nil) ; only show the heading
  12119. (and (or entry-p (outline-invisible-p) (org-invisible-p2))
  12120. (org-show-hidden-entry))) ; show entire entry
  12121. (when following-p
  12122. ;; Show next sibling, or heading below text
  12123. (save-excursion
  12124. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  12125. (org-flag-heading nil))))
  12126. (when siblings-p (org-show-siblings))
  12127. (when hierarchy-p
  12128. ;; show all higher headings, possibly with siblings
  12129. (save-excursion
  12130. (while (and (condition-case nil
  12131. (progn (org-up-heading-all 1) t)
  12132. (error nil))
  12133. (not (bobp)))
  12134. (org-flag-heading nil)
  12135. (when siblings-p (org-show-siblings)))))
  12136. (unless (eq key 'agenda) (org-fix-ellipsis-at-bol))))
  12137. (defvar org-reveal-start-hook nil
  12138. "Hook run before revealing a location.")
  12139. (defun org-reveal (&optional siblings)
  12140. "Show current entry, hierarchy above it, and the following headline.
  12141. This can be used to show a consistent set of context around locations
  12142. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  12143. not t for the search context.
  12144. With optional argument SIBLINGS, on each level of the hierarchy all
  12145. siblings are shown. This repairs the tree structure to what it would
  12146. look like when opened with hierarchical calls to `org-cycle'.
  12147. With double optional argument \\[universal-argument] \\[universal-argument], \
  12148. go to the parent and show the
  12149. entire tree."
  12150. (interactive "P")
  12151. (run-hooks 'org-reveal-start-hook)
  12152. (let ((org-show-hierarchy-above t)
  12153. (org-show-following-heading t)
  12154. (org-show-siblings (if siblings t org-show-siblings)))
  12155. (org-show-context nil))
  12156. (when (equal siblings '(16))
  12157. (save-excursion
  12158. (when (org-up-heading-safe)
  12159. (org-show-subtree)
  12160. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  12161. (defun org-highlight-new-match (beg end)
  12162. "Highlight from BEG to END and mark the highlight is an occur headline."
  12163. (let ((ov (make-overlay beg end)))
  12164. (overlay-put ov 'face 'secondary-selection)
  12165. (overlay-put ov 'org-type 'org-occur)
  12166. (push ov org-occur-highlights)))
  12167. (defun org-remove-occur-highlights (&optional beg end noremove)
  12168. "Remove the occur highlights from the buffer.
  12169. BEG and END are ignored. If NOREMOVE is nil, remove this function
  12170. from the `before-change-functions' in the current buffer."
  12171. (interactive)
  12172. (unless org-inhibit-highlight-removal
  12173. (mapc 'delete-overlay org-occur-highlights)
  12174. (setq org-occur-highlights nil)
  12175. (setq org-occur-parameters nil)
  12176. (unless noremove
  12177. (remove-hook 'before-change-functions
  12178. 'org-remove-occur-highlights 'local))))
  12179. ;;;; Priorities
  12180. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  12181. "Regular expression matching the priority indicator.")
  12182. (defvar org-remove-priority-next-time nil)
  12183. (defun org-priority-up ()
  12184. "Increase the priority of the current item."
  12185. (interactive)
  12186. (org-priority 'up))
  12187. (defun org-priority-down ()
  12188. "Decrease the priority of the current item."
  12189. (interactive)
  12190. (org-priority 'down))
  12191. (defun org-priority (&optional action show)
  12192. "Change the priority of an item.
  12193. ACTION can be `set', `up', `down', or a character."
  12194. (interactive "P")
  12195. (if (equal action '(4))
  12196. (org-show-priority)
  12197. (unless org-enable-priority-commands
  12198. (error "Priority commands are disabled"))
  12199. (setq action (or action 'set))
  12200. (let (current new news have remove)
  12201. (save-excursion
  12202. (org-back-to-heading t)
  12203. (if (looking-at org-priority-regexp)
  12204. (setq current (string-to-char (match-string 2))
  12205. have t))
  12206. (cond
  12207. ((eq action 'remove)
  12208. (setq remove t new ?\ ))
  12209. ((or (eq action 'set)
  12210. (if (featurep 'xemacs) (characterp action) (integerp action)))
  12211. (if (not (eq action 'set))
  12212. (setq new action)
  12213. (message "Priority %c-%c, SPC to remove: "
  12214. org-highest-priority org-lowest-priority)
  12215. (save-match-data
  12216. (setq new (read-char-exclusive))))
  12217. (if (and (= (upcase org-highest-priority) org-highest-priority)
  12218. (= (upcase org-lowest-priority) org-lowest-priority))
  12219. (setq new (upcase new)))
  12220. (cond ((equal new ?\ ) (setq remove t))
  12221. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  12222. (error "Priority must be between `%c' and `%c'"
  12223. org-highest-priority org-lowest-priority))))
  12224. ((eq action 'up)
  12225. (setq new (if have
  12226. (1- current) ; normal cycling
  12227. ;; last priority was empty
  12228. (if (eq last-command this-command)
  12229. org-lowest-priority ; wrap around empty to lowest
  12230. ;; default
  12231. (if org-priority-start-cycle-with-default
  12232. org-default-priority
  12233. (1- org-default-priority))))))
  12234. ((eq action 'down)
  12235. (setq new (if have
  12236. (1+ current) ; normal cycling
  12237. ;; last priority was empty
  12238. (if (eq last-command this-command)
  12239. org-highest-priority ; wrap around empty to highest
  12240. ;; default
  12241. (if org-priority-start-cycle-with-default
  12242. org-default-priority
  12243. (1+ org-default-priority))))))
  12244. (t (error "Invalid action")))
  12245. (if (or (< (upcase new) org-highest-priority)
  12246. (> (upcase new) org-lowest-priority))
  12247. (if (and (memq action '(up down))
  12248. (not have) (not (eq last-command this-command)))
  12249. ;; `new' is from default priority
  12250. (error
  12251. "The default can not be set, see `org-default-priority' why")
  12252. ;; normal cycling: `new' is beyond highest/lowest priority
  12253. ;; and is wrapped around to the empty priority
  12254. (setq remove t)))
  12255. (setq news (format "%c" new))
  12256. (if have
  12257. (if remove
  12258. (replace-match "" t t nil 1)
  12259. (replace-match news t t nil 2))
  12260. (if remove
  12261. (error "No priority cookie found in line")
  12262. (let ((case-fold-search nil))
  12263. (looking-at org-todo-line-regexp))
  12264. (if (match-end 2)
  12265. (progn
  12266. (goto-char (match-end 2))
  12267. (insert " [#" news "]"))
  12268. (goto-char (match-beginning 3))
  12269. (insert "[#" news "] "))))
  12270. (org-preserve-lc (org-set-tags nil 'align)))
  12271. (if remove
  12272. (message "Priority removed")
  12273. (message "Priority of current item set to %s" news)))))
  12274. (defun org-show-priority ()
  12275. "Show the priority of the current item.
  12276. This priority is composed of the main priority given with the [#A] cookies,
  12277. and by additional input from the age of a schedules or deadline entry."
  12278. (interactive)
  12279. (let ((pri (if (eq major-mode 'org-agenda-mode)
  12280. (org-get-at-bol 'priority)
  12281. (save-excursion
  12282. (save-match-data
  12283. (beginning-of-line)
  12284. (and (looking-at org-heading-regexp)
  12285. (org-get-priority (match-string 0))))))))
  12286. (message "Priority is %d" (if pri pri -1000))))
  12287. (defun org-get-priority (s)
  12288. "Find priority cookie and return priority."
  12289. (save-match-data
  12290. (if (functionp org-get-priority-function)
  12291. (funcall org-get-priority-function)
  12292. (if (not (string-match org-priority-regexp s))
  12293. (* 1000 (- org-lowest-priority org-default-priority))
  12294. (* 1000 (- org-lowest-priority
  12295. (string-to-char (match-string 2 s))))))))
  12296. ;;;; Tags
  12297. (defvar org-agenda-archives-mode)
  12298. (defvar org-map-continue-from nil
  12299. "Position from where mapping should continue.
  12300. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  12301. (defvar org-scanner-tags nil
  12302. "The current tag list while the tags scanner is running.")
  12303. (defvar org-trust-scanner-tags nil
  12304. "Should `org-get-tags-at' use the tags for the scanner.
  12305. This is for internal dynamical scoping only.
  12306. When this is non-nil, the function `org-get-tags-at' will return the value
  12307. of `org-scanner-tags' instead of building the list by itself. This
  12308. can lead to large speed-ups when the tags scanner is used in a file with
  12309. many entries, and when the list of tags is retrieved, for example to
  12310. obtain a list of properties. Building the tags list for each entry in such
  12311. a file becomes an N^2 operation - but with this variable set, it scales
  12312. as N.")
  12313. (defun org-scan-tags (action matcher todo-only &optional start-level)
  12314. "Sca headline tags with inheritance and produce output ACTION.
  12315. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  12316. or `agenda' to produce an entry list for an agenda view. It can also be
  12317. a Lisp form or a function that should be called at each matched headline, in
  12318. this case the return value is a list of all return values from these calls.
  12319. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  12320. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  12321. only lines with a not-done TODO keyword are included in the output.
  12322. This should be the same variable that was scoped into
  12323. and set by `org-make-tags-matcher' when it constructed MATCHER.
  12324. START-LEVEL can be a string with asterisks, reducing the scope to
  12325. headlines matching this string."
  12326. (require 'org-agenda)
  12327. (let* ((re (concat "^"
  12328. (if start-level
  12329. ;; Get the correct level to match
  12330. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  12331. org-outline-regexp)
  12332. " *\\(\\<\\("
  12333. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  12334. (org-re "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  12335. (props (list 'face 'default
  12336. 'done-face 'org-agenda-done
  12337. 'undone-face 'default
  12338. 'mouse-face 'highlight
  12339. 'org-not-done-regexp org-not-done-regexp
  12340. 'org-todo-regexp org-todo-regexp
  12341. 'org-complex-heading-regexp org-complex-heading-regexp
  12342. 'help-echo
  12343. (format "mouse-2 or RET jump to org file %s"
  12344. (abbreviate-file-name
  12345. (or (buffer-file-name (buffer-base-buffer))
  12346. (buffer-name (buffer-base-buffer)))))))
  12347. (case-fold-search nil)
  12348. (org-map-continue-from nil)
  12349. lspos tags tags-list
  12350. (tags-alist (list (cons 0 org-file-tags)))
  12351. (llast 0) rtn rtn1 level category i txt
  12352. todo marker entry priority)
  12353. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  12354. (setq action (list 'lambda nil action)))
  12355. (save-excursion
  12356. (goto-char (point-min))
  12357. (when (eq action 'sparse-tree)
  12358. (org-overview)
  12359. (org-remove-occur-highlights))
  12360. (while (re-search-forward re nil t)
  12361. (setq org-map-continue-from nil)
  12362. (catch :skip
  12363. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  12364. tags (if (match-end 4) (org-match-string-no-properties 4)))
  12365. (goto-char (setq lspos (match-beginning 0)))
  12366. (setq level (org-reduced-level (org-outline-level))
  12367. category (org-get-category))
  12368. (setq i llast llast level)
  12369. ;; remove tag lists from same and sublevels
  12370. (while (>= i level)
  12371. (when (setq entry (assoc i tags-alist))
  12372. (setq tags-alist (delete entry tags-alist)))
  12373. (setq i (1- i)))
  12374. ;; add the next tags
  12375. (when tags
  12376. (setq tags (org-split-string tags ":")
  12377. tags-alist
  12378. (cons (cons level tags) tags-alist)))
  12379. ;; compile tags for current headline
  12380. (setq tags-list
  12381. (if org-use-tag-inheritance
  12382. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  12383. tags)
  12384. org-scanner-tags tags-list)
  12385. (when org-use-tag-inheritance
  12386. (setcdr (car tags-alist)
  12387. (mapcar (lambda (x)
  12388. (setq x (copy-sequence x))
  12389. (org-add-prop-inherited x))
  12390. (cdar tags-alist))))
  12391. (when (and tags org-use-tag-inheritance
  12392. (or (not (eq t org-use-tag-inheritance))
  12393. org-tags-exclude-from-inheritance))
  12394. ;; selective inheritance, remove uninherited ones
  12395. (setcdr (car tags-alist)
  12396. (org-remove-uninherited-tags (cdar tags-alist))))
  12397. (when (and
  12398. ;; eval matcher only when the todo condition is OK
  12399. (and (or (not todo-only) (member todo org-not-done-keywords))
  12400. (let ((case-fold-search t) (org-trust-scanner-tags t))
  12401. (eval matcher)))
  12402. ;; Call the skipper, but return t if it does not skip,
  12403. ;; so that the `and' form continues evaluating
  12404. (progn
  12405. (unless (eq action 'sparse-tree) (org-agenda-skip))
  12406. t)
  12407. ;; Check if timestamps are deselecting this entry
  12408. (or (not todo-only)
  12409. (and (member todo org-not-done-keywords)
  12410. (or (not org-agenda-tags-todo-honor-ignore-options)
  12411. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item))))))
  12412. ;; select this headline
  12413. (cond
  12414. ((eq action 'sparse-tree)
  12415. (and org-highlight-sparse-tree-matches
  12416. (org-get-heading) (match-end 0)
  12417. (org-highlight-new-match
  12418. (match-beginning 1) (match-end 1)))
  12419. (org-show-context 'tags-tree))
  12420. ((eq action 'agenda)
  12421. (setq txt (org-agenda-format-item
  12422. ""
  12423. (concat
  12424. (if (eq org-tags-match-list-sublevels 'indented)
  12425. (make-string (1- level) ?.) "")
  12426. (org-get-heading))
  12427. level category
  12428. tags-list)
  12429. priority (org-get-priority txt))
  12430. (goto-char lspos)
  12431. (setq marker (org-agenda-new-marker))
  12432. (org-add-props txt props
  12433. 'org-marker marker 'org-hd-marker marker 'org-category category
  12434. 'todo-state todo
  12435. 'priority priority 'type "tagsmatch")
  12436. (push txt rtn))
  12437. ((functionp action)
  12438. (setq org-map-continue-from nil)
  12439. (save-excursion
  12440. (setq rtn1 (funcall action))
  12441. (push rtn1 rtn)))
  12442. (t (error "Invalid action")))
  12443. ;; if we are to skip sublevels, jump to end of subtree
  12444. (unless org-tags-match-list-sublevels
  12445. (org-end-of-subtree t)
  12446. (backward-char 1))))
  12447. ;; Get the correct position from where to continue
  12448. (if org-map-continue-from
  12449. (goto-char org-map-continue-from)
  12450. (and (= (point) lspos) (end-of-line 1)))))
  12451. (when (and (eq action 'sparse-tree)
  12452. (not org-sparse-tree-open-archived-trees))
  12453. (org-hide-archived-subtrees (point-min) (point-max)))
  12454. (nreverse rtn)))
  12455. (defun org-remove-uninherited-tags (tags)
  12456. "Remove all tags that are not inherited from the list TAGS."
  12457. (cond
  12458. ((eq org-use-tag-inheritance t)
  12459. (if org-tags-exclude-from-inheritance
  12460. (org-delete-all org-tags-exclude-from-inheritance tags)
  12461. tags))
  12462. ((not org-use-tag-inheritance) nil)
  12463. ((stringp org-use-tag-inheritance)
  12464. (delq nil (mapcar
  12465. (lambda (x)
  12466. (if (and (string-match org-use-tag-inheritance x)
  12467. (not (member x org-tags-exclude-from-inheritance)))
  12468. x nil))
  12469. tags)))
  12470. ((listp org-use-tag-inheritance)
  12471. (delq nil (mapcar
  12472. (lambda (x)
  12473. (if (member x org-use-tag-inheritance) x nil))
  12474. tags)))))
  12475. (defun org-match-sparse-tree (&optional todo-only match)
  12476. "Create a sparse tree according to tags string MATCH.
  12477. MATCH can contain positive and negative selection of tags, like
  12478. \"+WORK+URGENT-WITHBOSS\".
  12479. If optional argument TODO-ONLY is non-nil, only select lines that are
  12480. also TODO lines."
  12481. (interactive "P")
  12482. (org-agenda-prepare-buffers (list (current-buffer)))
  12483. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  12484. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  12485. (defvar org-cached-props nil)
  12486. (defun org-cached-entry-get (pom property)
  12487. (if (or (eq t org-use-property-inheritance)
  12488. (and (stringp org-use-property-inheritance)
  12489. (string-match org-use-property-inheritance property))
  12490. (and (listp org-use-property-inheritance)
  12491. (member property org-use-property-inheritance)))
  12492. ;; Caching is not possible, check it directly
  12493. (org-entry-get pom property 'inherit)
  12494. ;; Get all properties, so that we can do complicated checks easily
  12495. (cdr (assoc property (or org-cached-props
  12496. (setq org-cached-props
  12497. (org-entry-properties pom)))))))
  12498. (defun org-global-tags-completion-table (&optional files)
  12499. "Return the list of all tags in all agenda buffer/files.
  12500. Optional FILES argument is a list of files which can be used
  12501. instead of the agenda files."
  12502. (save-excursion
  12503. (org-uniquify
  12504. (delq nil
  12505. (apply 'append
  12506. (mapcar
  12507. (lambda (file)
  12508. (set-buffer (find-file-noselect file))
  12509. (append (org-get-buffer-tags)
  12510. (mapcar (lambda (x) (if (stringp (car-safe x))
  12511. (list (car-safe x)) nil))
  12512. org-tag-alist)))
  12513. (if (and files (car files))
  12514. files
  12515. (org-agenda-files))))))))
  12516. (defun org-make-tags-matcher (match)
  12517. "Create the TAGS/TODO matcher form for the selection string MATCH.
  12518. The variable `todo-only' is scoped dynamically into this function.
  12519. It will be set to t if the matcher restricts matching to TODO entries,
  12520. otherwise will not be touched.
  12521. Returns a cons of the selection string MATCH and the constructed
  12522. lisp form implementing the matcher. The matcher is to be evaluated
  12523. at an Org entry, with point on the headline, and returns t if the
  12524. entry matches the selection string MATCH. The returned lisp form
  12525. references two variables with information about the entry, which
  12526. must be bound around the form's evaluation: todo, the TODO keyword
  12527. at the entry (or nil of none); and tags-list, the list of all tags
  12528. at the entry including inherited ones. Additionally, the category
  12529. of the entry (if any) must be specified as the text property
  12530. 'org-category on the headline.
  12531. See also `org-scan-tags'.
  12532. "
  12533. (declare (special todo-only))
  12534. (unless (boundp 'todo-only)
  12535. (error "org-make-tags-matcher expects todo-only to be scoped in"))
  12536. (unless match
  12537. ;; Get a new match request, with completion
  12538. (let ((org-last-tags-completion-table
  12539. (org-global-tags-completion-table)))
  12540. (setq match (org-completing-read-no-i
  12541. "Match: " 'org-tags-completion-function nil nil nil
  12542. 'org-tags-history))))
  12543. ;; Parse the string and create a lisp form
  12544. (let ((match0 match)
  12545. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  12546. minus tag mm
  12547. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  12548. orterms term orlist re-p str-p level-p level-op time-p
  12549. prop-p pn pv po gv rest)
  12550. (if (string-match "/+" match)
  12551. ;; match contains also a todo-matching request
  12552. (progn
  12553. (setq tagsmatch (substring match 0 (match-beginning 0))
  12554. todomatch (substring match (match-end 0)))
  12555. (if (string-match "^!" todomatch)
  12556. (setq todo-only t todomatch (substring todomatch 1)))
  12557. (if (string-match "^\\s-*$" todomatch)
  12558. (setq todomatch nil)))
  12559. ;; only matching tags
  12560. (setq tagsmatch match todomatch nil))
  12561. ;; Make the tags matcher
  12562. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  12563. (setq tagsmatcher t)
  12564. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  12565. (while (setq term (pop orterms))
  12566. (while (and (equal (substring term -1) "\\") orterms)
  12567. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  12568. (while (string-match re term)
  12569. (setq rest (substring term (match-end 0))
  12570. minus (and (match-end 1)
  12571. (equal (match-string 1 term) "-"))
  12572. tag (save-match-data (replace-regexp-in-string
  12573. "\\\\-" "-"
  12574. (match-string 2 term)))
  12575. re-p (equal (string-to-char tag) ?{)
  12576. level-p (match-end 4)
  12577. prop-p (match-end 5)
  12578. mm (cond
  12579. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  12580. (level-p
  12581. (setq level-op (org-op-to-function (match-string 3 term)))
  12582. `(,level-op level ,(string-to-number
  12583. (match-string 4 term))))
  12584. (prop-p
  12585. (setq pn (match-string 5 term)
  12586. po (match-string 6 term)
  12587. pv (match-string 7 term)
  12588. re-p (equal (string-to-char pv) ?{)
  12589. str-p (equal (string-to-char pv) ?\")
  12590. time-p (save-match-data
  12591. (string-match "^\"[[<].*[]>]\"$" pv))
  12592. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  12593. (if time-p (setq pv (org-matcher-time pv)))
  12594. (setq po (org-op-to-function po (if time-p 'time str-p)))
  12595. (cond
  12596. ((equal pn "CATEGORY")
  12597. (setq gv '(get-text-property (point) 'org-category)))
  12598. ((equal pn "TODO")
  12599. (setq gv 'todo))
  12600. (t
  12601. (setq gv `(org-cached-entry-get nil ,pn))))
  12602. (if re-p
  12603. (if (eq po 'org<>)
  12604. `(not (string-match ,pv (or ,gv "")))
  12605. `(string-match ,pv (or ,gv "")))
  12606. (if str-p
  12607. `(,po (or ,gv "") ,pv)
  12608. `(,po (string-to-number (or ,gv ""))
  12609. ,(string-to-number pv) ))))
  12610. (t `(member ,tag tags-list)))
  12611. mm (if minus (list 'not mm) mm)
  12612. term rest)
  12613. (push mm tagsmatcher))
  12614. (push (if (> (length tagsmatcher) 1)
  12615. (cons 'and tagsmatcher)
  12616. (car tagsmatcher))
  12617. orlist)
  12618. (setq tagsmatcher nil))
  12619. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  12620. (setq tagsmatcher
  12621. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  12622. ;; Make the todo matcher
  12623. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  12624. (setq todomatcher t)
  12625. (setq orterms (org-split-string todomatch "|") orlist nil)
  12626. (while (setq term (pop orterms))
  12627. (while (string-match re term)
  12628. (setq minus (and (match-end 1)
  12629. (equal (match-string 1 term) "-"))
  12630. kwd (match-string 2 term)
  12631. re-p (equal (string-to-char kwd) ?{)
  12632. term (substring term (match-end 0))
  12633. mm (if re-p
  12634. `(string-match ,(substring kwd 1 -1) todo)
  12635. (list 'equal 'todo kwd))
  12636. mm (if minus (list 'not mm) mm))
  12637. (push mm todomatcher))
  12638. (push (if (> (length todomatcher) 1)
  12639. (cons 'and todomatcher)
  12640. (car todomatcher))
  12641. orlist)
  12642. (setq todomatcher nil))
  12643. (setq todomatcher (if (> (length orlist) 1)
  12644. (cons 'or orlist) (car orlist))))
  12645. ;; Return the string and lisp forms of the matcher
  12646. (setq matcher (if todomatcher
  12647. (list 'and tagsmatcher todomatcher)
  12648. tagsmatcher))
  12649. (when todo-only
  12650. (setq matcher (list 'and '(member todo org-not-done-keywords)
  12651. matcher)))
  12652. (cons match0 matcher)))
  12653. (defun org-op-to-function (op &optional stringp)
  12654. "Turn an operator into the appropriate function."
  12655. (setq op
  12656. (cond
  12657. ((equal op "<" ) '(< string< org-time<))
  12658. ((equal op ">" ) '(> org-string> org-time>))
  12659. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  12660. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  12661. ((member op '("=" "==")) '(= string= org-time=))
  12662. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  12663. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  12664. (defun org<> (a b) (not (= a b)))
  12665. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  12666. (defun org-string>= (a b) (not (string< a b)))
  12667. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  12668. (defun org-string<> (a b) (not (string= a b)))
  12669. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  12670. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  12671. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  12672. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  12673. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  12674. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  12675. (defun org-2ft (s)
  12676. "Convert S to a floating point time.
  12677. If S is already a number, just return it. If it is a string, parse
  12678. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  12679. (cond
  12680. ((numberp s) s)
  12681. ((stringp s)
  12682. (condition-case nil
  12683. (float-time (apply 'encode-time (org-parse-time-string s)))
  12684. (error 0.)))
  12685. (t 0.)))
  12686. (defun org-time-today ()
  12687. "Time in seconds today at 0:00.
  12688. Returns the float number of seconds since the beginning of the
  12689. epoch to the beginning of today (00:00)."
  12690. (float-time (apply 'encode-time
  12691. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  12692. (defun org-matcher-time (s)
  12693. "Interpret a time comparison value."
  12694. (save-match-data
  12695. (cond
  12696. ((string= s "<now>") (float-time))
  12697. ((string= s "<today>") (org-time-today))
  12698. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  12699. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  12700. ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
  12701. (+ (org-time-today)
  12702. (* (string-to-number (match-string 1 s))
  12703. (cdr (assoc (match-string 2 s)
  12704. '(("d" . 86400.0) ("w" . 604800.0)
  12705. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  12706. (t (org-2ft s)))))
  12707. (defun org-match-any-p (re list)
  12708. "Does re match any element of list?"
  12709. (setq list (mapcar (lambda (x) (string-match re x)) list))
  12710. (delq nil list))
  12711. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  12712. (defvar org-tags-overlay (make-overlay 1 1))
  12713. (org-detach-overlay org-tags-overlay)
  12714. (defun org-get-local-tags-at (&optional pos)
  12715. "Get a list of tags defined in the current headline."
  12716. (org-get-tags-at pos 'local))
  12717. (defun org-get-local-tags ()
  12718. "Get a list of tags defined in the current headline."
  12719. (org-get-tags-at nil 'local))
  12720. (defun org-get-tags-at (&optional pos local)
  12721. "Get a list of all headline tags applicable at POS.
  12722. POS defaults to point. If tags are inherited, the list contains
  12723. the targets in the same sequence as the headlines appear, i.e.
  12724. the tags of the current headline come last.
  12725. When LOCAL is non-nil, only return tags from the current headline,
  12726. ignore inherited ones."
  12727. (interactive)
  12728. (if (and org-trust-scanner-tags
  12729. (or (not pos) (equal pos (point)))
  12730. (not local))
  12731. org-scanner-tags
  12732. (let (tags ltags lastpos parent)
  12733. (save-excursion
  12734. (save-restriction
  12735. (widen)
  12736. (goto-char (or pos (point)))
  12737. (save-match-data
  12738. (catch 'done
  12739. (condition-case nil
  12740. (progn
  12741. (org-back-to-heading t)
  12742. (while (not (equal lastpos (point)))
  12743. (setq lastpos (point))
  12744. (when (looking-at
  12745. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  12746. (setq ltags (org-split-string
  12747. (org-match-string-no-properties 1) ":"))
  12748. (when parent
  12749. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  12750. (setq tags (append
  12751. (if parent
  12752. (org-remove-uninherited-tags ltags)
  12753. ltags)
  12754. tags)))
  12755. (or org-use-tag-inheritance (throw 'done t))
  12756. (if local (throw 'done t))
  12757. (or (org-up-heading-safe) (error nil))
  12758. (setq parent t)))
  12759. (error nil)))))
  12760. (if local
  12761. tags
  12762. (reverse (delete-dups
  12763. (reverse (append
  12764. (org-remove-uninherited-tags
  12765. org-file-tags) tags)))))))))
  12766. (defun org-add-prop-inherited (s)
  12767. (add-text-properties 0 (length s) '(inherited t) s)
  12768. s)
  12769. (defun org-toggle-tag (tag &optional onoff)
  12770. "Toggle the tag TAG for the current line.
  12771. If ONOFF is `on' or `off', don't toggle but set to this state."
  12772. (let (res current)
  12773. (save-excursion
  12774. (org-back-to-heading t)
  12775. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  12776. (point-at-eol) t)
  12777. (progn
  12778. (setq current (match-string 1))
  12779. (replace-match ""))
  12780. (setq current ""))
  12781. (setq current (nreverse (org-split-string current ":")))
  12782. (cond
  12783. ((eq onoff 'on)
  12784. (setq res t)
  12785. (or (member tag current) (push tag current)))
  12786. ((eq onoff 'off)
  12787. (or (not (member tag current)) (setq current (delete tag current))))
  12788. (t (if (member tag current)
  12789. (setq current (delete tag current))
  12790. (setq res t)
  12791. (push tag current))))
  12792. (end-of-line 1)
  12793. (if current
  12794. (progn
  12795. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  12796. (org-set-tags nil t))
  12797. (delete-horizontal-space))
  12798. (run-hooks 'org-after-tags-change-hook))
  12799. res))
  12800. (defun org-align-tags-here (to-col)
  12801. ;; Assumes that this is a headline
  12802. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  12803. (beginning-of-line 1)
  12804. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12805. (< pos (match-beginning 2)))
  12806. (progn
  12807. (setq tags-l (- (match-end 2) (match-beginning 2)))
  12808. (goto-char (match-beginning 1))
  12809. (insert " ")
  12810. (delete-region (point) (1+ (match-beginning 2)))
  12811. (setq ncol (max (current-column)
  12812. (1+ col)
  12813. (if (> to-col 0)
  12814. to-col
  12815. (- (abs to-col) tags-l))))
  12816. (setq p (point))
  12817. (insert (make-string (- ncol (current-column)) ?\ ))
  12818. (setq ncol (current-column))
  12819. (when indent-tabs-mode (tabify p (point-at-eol)))
  12820. (org-move-to-column (min ncol col) t))
  12821. (goto-char pos))))
  12822. (defun org-set-tags-command (&optional arg just-align)
  12823. "Call the set-tags command for the current entry."
  12824. (interactive "P")
  12825. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  12826. (org-set-tags arg just-align)
  12827. (save-excursion
  12828. (unless (and (org-region-active-p)
  12829. org-loop-over-headlines-in-active-region)
  12830. (org-back-to-heading t))
  12831. (org-set-tags arg just-align))))
  12832. (defun org-set-tags-to (data)
  12833. "Set the tags of the current entry to DATA, replacing the current tags.
  12834. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  12835. If DATA is nil or the empty string, any tags will be removed."
  12836. (interactive "sTags: ")
  12837. (setq data
  12838. (cond
  12839. ((eq data nil) "")
  12840. ((equal data "") "")
  12841. ((stringp data)
  12842. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  12843. ":"))
  12844. ((listp data)
  12845. (concat ":" (mapconcat 'identity data ":") ":"))))
  12846. (when data
  12847. (save-excursion
  12848. (org-back-to-heading t)
  12849. (when (looking-at org-complex-heading-regexp)
  12850. (if (match-end 5)
  12851. (progn
  12852. (goto-char (match-beginning 5))
  12853. (insert data)
  12854. (delete-region (point) (point-at-eol))
  12855. (org-set-tags nil 'align))
  12856. (goto-char (point-at-eol))
  12857. (insert " " data)
  12858. (org-set-tags nil 'align)))
  12859. (beginning-of-line 1)
  12860. (if (looking-at ".*?\\([ \t]+\\)$")
  12861. (delete-region (match-beginning 1) (match-end 1))))))
  12862. (defun org-align-all-tags ()
  12863. "Align the tags i all headings."
  12864. (interactive)
  12865. (save-excursion
  12866. (or (ignore-errors (org-back-to-heading t))
  12867. (outline-next-heading))
  12868. (if (org-at-heading-p)
  12869. (org-set-tags t)
  12870. (message "No headings"))))
  12871. (defvar org-indent-indentation-per-level)
  12872. (defun org-set-tags (&optional arg just-align)
  12873. "Set the tags for the current headline.
  12874. With prefix ARG, realign all tags in headings in the current buffer."
  12875. (interactive "P")
  12876. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  12877. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  12878. 'region-start-level 'region))
  12879. org-loop-over-headlines-in-active-region)
  12880. (org-map-entries
  12881. ;; We don't use ARG and JUST-ALIGN here these args are not
  12882. ;; useful when looping over headlines
  12883. `(org-set-tags)
  12884. org-loop-over-headlines-in-active-region
  12885. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  12886. (let* ((re org-outline-regexp-bol)
  12887. (current (unless arg (org-get-tags-string)))
  12888. (col (current-column))
  12889. (org-setting-tags t)
  12890. table current-tags inherited-tags ; computed below when needed
  12891. tags p0 c0 c1 rpl di tc level)
  12892. (if arg
  12893. (save-excursion
  12894. (goto-char (point-min))
  12895. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  12896. (while (re-search-forward re nil t)
  12897. (org-set-tags nil t)
  12898. (end-of-line 1)))
  12899. (message "All tags realigned to column %d" org-tags-column))
  12900. (if just-align
  12901. (setq tags current)
  12902. ;; Get a new set of tags from the user
  12903. (save-excursion
  12904. (setq table (append org-tag-persistent-alist
  12905. (or org-tag-alist (org-get-buffer-tags))
  12906. (and
  12907. org-complete-tags-always-offer-all-agenda-tags
  12908. (org-global-tags-completion-table
  12909. (org-agenda-files))))
  12910. org-last-tags-completion-table table
  12911. current-tags (org-split-string current ":")
  12912. inherited-tags (nreverse
  12913. (nthcdr (length current-tags)
  12914. (nreverse (org-get-tags-at))))
  12915. tags
  12916. (if (or (eq t org-use-fast-tag-selection)
  12917. (and org-use-fast-tag-selection
  12918. (delq nil (mapcar 'cdr table))))
  12919. (org-fast-tag-selection
  12920. current-tags inherited-tags table
  12921. (if org-fast-tag-selection-include-todo
  12922. org-todo-key-alist))
  12923. (let ((org-add-colon-after-tag-completion (< 1 (length table))))
  12924. (org-trim
  12925. (org-icompleting-read "Tags: "
  12926. 'org-tags-completion-function
  12927. nil nil current 'org-tags-history))))))
  12928. (while (string-match "[-+&]+" tags)
  12929. ;; No boolean logic, just a list
  12930. (setq tags (replace-match ":" t t tags))))
  12931. (setq tags (replace-regexp-in-string "[,]" ":" tags))
  12932. (if org-tags-sort-function
  12933. (setq tags (mapconcat 'identity
  12934. (sort (org-split-string
  12935. tags (org-re "[^[:alnum:]_@#%]+"))
  12936. org-tags-sort-function) ":")))
  12937. (if (string-match "\\`[\t ]*\\'" tags)
  12938. (setq tags "")
  12939. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  12940. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  12941. ;; Insert new tags at the correct column
  12942. (beginning-of-line 1)
  12943. (setq level (or (and (looking-at org-outline-regexp)
  12944. (- (match-end 0) (point) 1))
  12945. 1))
  12946. (cond
  12947. ((and (equal current "") (equal tags "")))
  12948. ((re-search-forward
  12949. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  12950. (point-at-eol) t)
  12951. (if (equal tags "")
  12952. (setq rpl "")
  12953. (goto-char (match-beginning 0))
  12954. (setq c0 (current-column)
  12955. ;; compute offset for the case of org-indent-mode active
  12956. di (if org-indent-mode
  12957. (* (1- org-indent-indentation-per-level) (1- level))
  12958. 0)
  12959. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  12960. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  12961. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
  12962. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  12963. (replace-match rpl t t)
  12964. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  12965. tags)
  12966. (t (error "Tags alignment failed")))
  12967. (org-move-to-column col)
  12968. (unless just-align
  12969. (run-hooks 'org-after-tags-change-hook))))))
  12970. (defun org-change-tag-in-region (beg end tag off)
  12971. "Add or remove TAG for each entry in the region.
  12972. This works in the agenda, and also in an org-mode buffer."
  12973. (interactive
  12974. (list (region-beginning) (region-end)
  12975. (let ((org-last-tags-completion-table
  12976. (if (derived-mode-p 'org-mode)
  12977. (org-get-buffer-tags)
  12978. (org-global-tags-completion-table))))
  12979. (org-icompleting-read
  12980. "Tag: " 'org-tags-completion-function nil nil nil
  12981. 'org-tags-history))
  12982. (progn
  12983. (message "[s]et or [r]emove? ")
  12984. (equal (read-char-exclusive) ?r))))
  12985. (if (fboundp 'deactivate-mark) (deactivate-mark))
  12986. (let ((agendap (equal major-mode 'org-agenda-mode))
  12987. l1 l2 m buf pos newhead (cnt 0))
  12988. (goto-char end)
  12989. (setq l2 (1- (org-current-line)))
  12990. (goto-char beg)
  12991. (setq l1 (org-current-line))
  12992. (loop for l from l1 to l2 do
  12993. (org-goto-line l)
  12994. (setq m (get-text-property (point) 'org-hd-marker))
  12995. (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
  12996. (and agendap m))
  12997. (setq buf (if agendap (marker-buffer m) (current-buffer))
  12998. pos (if agendap m (point)))
  12999. (with-current-buffer buf
  13000. (save-excursion
  13001. (save-restriction
  13002. (goto-char pos)
  13003. (setq cnt (1+ cnt))
  13004. (org-toggle-tag tag (if off 'off 'on))
  13005. (setq newhead (org-get-heading)))))
  13006. (and agendap (org-agenda-change-all-lines newhead m))))
  13007. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  13008. (defun org-tags-completion-function (string predicate &optional flag)
  13009. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  13010. (confirm (lambda (x) (stringp (car x)))))
  13011. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  13012. (setq s1 (match-string 1 string)
  13013. s2 (match-string 2 string))
  13014. (setq s1 "" s2 string))
  13015. (cond
  13016. ((eq flag nil)
  13017. ;; try completion
  13018. (setq rtn (try-completion s2 ctable confirm))
  13019. (if (stringp rtn)
  13020. (setq rtn
  13021. (concat s1 s2 (substring rtn (length s2))
  13022. (if (and org-add-colon-after-tag-completion
  13023. (assoc rtn ctable))
  13024. ":" ""))))
  13025. rtn)
  13026. ((eq flag t)
  13027. ;; all-completions
  13028. (all-completions s2 ctable confirm)
  13029. )
  13030. ((eq flag 'lambda)
  13031. ;; exact match?
  13032. (assoc s2 ctable)))
  13033. ))
  13034. (defun org-fast-tag-insert (kwd tags face &optional end)
  13035. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  13036. (insert (format "%-12s" (concat kwd ":"))
  13037. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  13038. (or end "")))
  13039. (defun org-fast-tag-show-exit (flag)
  13040. (save-excursion
  13041. (org-goto-line 3)
  13042. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  13043. (replace-match ""))
  13044. (when flag
  13045. (end-of-line 1)
  13046. (org-move-to-column (- (window-width) 19) t)
  13047. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  13048. (defun org-set-current-tags-overlay (current prefix)
  13049. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  13050. (if (featurep 'xemacs)
  13051. (org-overlay-display org-tags-overlay (concat prefix s)
  13052. 'secondary-selection)
  13053. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  13054. (org-overlay-display org-tags-overlay (concat prefix s)))))
  13055. (defvar org-last-tag-selection-key nil)
  13056. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  13057. "Fast tag selection with single keys.
  13058. CURRENT is the current list of tags in the headline, INHERITED is the
  13059. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  13060. possibly with grouping information. TODO-TABLE is a similar table with
  13061. TODO keywords, should these have keys assigned to them.
  13062. If the keys are nil, a-z are automatically assigned.
  13063. Returns the new tags string, or nil to not change the current settings."
  13064. (let* ((fulltable (append table todo-table))
  13065. (maxlen (apply 'max (mapcar
  13066. (lambda (x)
  13067. (if (stringp (car x)) (string-width (car x)) 0))
  13068. fulltable)))
  13069. (buf (current-buffer))
  13070. (expert (eq org-fast-tag-selection-single-key 'expert))
  13071. (buffer-tags nil)
  13072. (fwidth (+ maxlen 3 1 3))
  13073. (ncol (/ (- (window-width) 4) fwidth))
  13074. (i-face 'org-done)
  13075. (c-face 'org-todo)
  13076. tg cnt e c char c1 c2 ntable tbl rtn
  13077. ov-start ov-end ov-prefix
  13078. (exit-after-next org-fast-tag-selection-single-key)
  13079. (done-keywords org-done-keywords)
  13080. groups ingroup)
  13081. (save-excursion
  13082. (beginning-of-line 1)
  13083. (if (looking-at
  13084. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13085. (setq ov-start (match-beginning 1)
  13086. ov-end (match-end 1)
  13087. ov-prefix "")
  13088. (setq ov-start (1- (point-at-eol))
  13089. ov-end (1+ ov-start))
  13090. (skip-chars-forward "^\n\r")
  13091. (setq ov-prefix
  13092. (concat
  13093. (buffer-substring (1- (point)) (point))
  13094. (if (> (current-column) org-tags-column)
  13095. " "
  13096. (make-string (- org-tags-column (current-column)) ?\ ))))))
  13097. (move-overlay org-tags-overlay ov-start ov-end)
  13098. (save-window-excursion
  13099. (if expert
  13100. (set-buffer (get-buffer-create " *Org tags*"))
  13101. (delete-other-windows)
  13102. (split-window-vertically)
  13103. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  13104. (erase-buffer)
  13105. (org-set-local 'org-done-keywords done-keywords)
  13106. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  13107. (org-fast-tag-insert "Current" current c-face "\n\n")
  13108. (org-fast-tag-show-exit exit-after-next)
  13109. (org-set-current-tags-overlay current ov-prefix)
  13110. (setq tbl fulltable char ?a cnt 0)
  13111. (while (setq e (pop tbl))
  13112. (cond
  13113. ((equal (car e) :startgroup)
  13114. (push '() groups) (setq ingroup t)
  13115. (when (not (= cnt 0))
  13116. (setq cnt 0)
  13117. (insert "\n"))
  13118. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  13119. ((equal (car e) :endgroup)
  13120. (setq ingroup nil cnt 0)
  13121. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  13122. ((equal e '(:newline))
  13123. (when (not (= cnt 0))
  13124. (setq cnt 0)
  13125. (insert "\n")
  13126. (setq e (car tbl))
  13127. (while (equal (car tbl) '(:newline))
  13128. (insert "\n")
  13129. (setq tbl (cdr tbl)))))
  13130. (t
  13131. (setq tg (copy-sequence (car e)) c2 nil)
  13132. (if (cdr e)
  13133. (setq c (cdr e))
  13134. ;; automatically assign a character.
  13135. (setq c1 (string-to-char
  13136. (downcase (substring
  13137. tg (if (= (string-to-char tg) ?@) 1 0)))))
  13138. (if (or (rassoc c1 ntable) (rassoc c1 table))
  13139. (while (or (rassoc char ntable) (rassoc char table))
  13140. (setq char (1+ char)))
  13141. (setq c2 c1))
  13142. (setq c (or c2 char)))
  13143. (if ingroup (push tg (car groups)))
  13144. (setq tg (org-add-props tg nil 'face
  13145. (cond
  13146. ((not (assoc tg table))
  13147. (org-get-todo-face tg))
  13148. ((member tg current) c-face)
  13149. ((member tg inherited) i-face))))
  13150. (if (and (= cnt 0) (not ingroup)) (insert " "))
  13151. (insert "[" c "] " tg (make-string
  13152. (- fwidth 4 (length tg)) ?\ ))
  13153. (push (cons tg c) ntable)
  13154. (when (= (setq cnt (1+ cnt)) ncol)
  13155. (insert "\n")
  13156. (if ingroup (insert " "))
  13157. (setq cnt 0)))))
  13158. (setq ntable (nreverse ntable))
  13159. (insert "\n")
  13160. (goto-char (point-min))
  13161. (if (not expert) (org-fit-window-to-buffer))
  13162. (setq rtn
  13163. (catch 'exit
  13164. (while t
  13165. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  13166. (if (not groups) "no " "")
  13167. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  13168. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  13169. (setq org-last-tag-selection-key c)
  13170. (cond
  13171. ((= c ?\r) (throw 'exit t))
  13172. ((= c ?!)
  13173. (setq groups (not groups))
  13174. (goto-char (point-min))
  13175. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  13176. ((= c ?\C-c)
  13177. (if (not expert)
  13178. (org-fast-tag-show-exit
  13179. (setq exit-after-next (not exit-after-next)))
  13180. (setq expert nil)
  13181. (delete-other-windows)
  13182. (set-window-buffer (split-window-vertically) " *Org tags*")
  13183. (org-switch-to-buffer-other-window " *Org tags*")
  13184. (org-fit-window-to-buffer)))
  13185. ((or (= c ?\C-g)
  13186. (and (= c ?q) (not (rassoc c ntable))))
  13187. (org-detach-overlay org-tags-overlay)
  13188. (setq quit-flag t))
  13189. ((= c ?\ )
  13190. (setq current nil)
  13191. (if exit-after-next (setq exit-after-next 'now)))
  13192. ((= c ?\t)
  13193. (condition-case nil
  13194. (setq tg (org-icompleting-read
  13195. "Tag: "
  13196. (or buffer-tags
  13197. (with-current-buffer buf
  13198. (org-get-buffer-tags)))))
  13199. (quit (setq tg "")))
  13200. (when (string-match "\\S-" tg)
  13201. (add-to-list 'buffer-tags (list tg))
  13202. (if (member tg current)
  13203. (setq current (delete tg current))
  13204. (push tg current)))
  13205. (if exit-after-next (setq exit-after-next 'now)))
  13206. ((setq e (rassoc c todo-table) tg (car e))
  13207. (with-current-buffer buf
  13208. (save-excursion (org-todo tg)))
  13209. (if exit-after-next (setq exit-after-next 'now)))
  13210. ((setq e (rassoc c ntable) tg (car e))
  13211. (if (member tg current)
  13212. (setq current (delete tg current))
  13213. (loop for g in groups do
  13214. (if (member tg g)
  13215. (mapc (lambda (x)
  13216. (setq current (delete x current)))
  13217. g)))
  13218. (push tg current))
  13219. (if exit-after-next (setq exit-after-next 'now))))
  13220. ;; Create a sorted list
  13221. (setq current
  13222. (sort current
  13223. (lambda (a b)
  13224. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  13225. (if (eq exit-after-next 'now) (throw 'exit t))
  13226. (goto-char (point-min))
  13227. (beginning-of-line 2)
  13228. (delete-region (point) (point-at-eol))
  13229. (org-fast-tag-insert "Current" current c-face)
  13230. (org-set-current-tags-overlay current ov-prefix)
  13231. (while (re-search-forward
  13232. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  13233. (setq tg (match-string 1))
  13234. (add-text-properties
  13235. (match-beginning 1) (match-end 1)
  13236. (list 'face
  13237. (cond
  13238. ((member tg current) c-face)
  13239. ((member tg inherited) i-face)
  13240. (t (get-text-property (match-beginning 1) 'face))))))
  13241. (goto-char (point-min)))))
  13242. (org-detach-overlay org-tags-overlay)
  13243. (if rtn
  13244. (mapconcat 'identity current ":")
  13245. nil))))
  13246. (defun org-get-tags-string ()
  13247. "Get the TAGS string in the current headline."
  13248. (unless (org-at-heading-p t)
  13249. (error "Not on a heading"))
  13250. (save-excursion
  13251. (beginning-of-line 1)
  13252. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13253. (org-match-string-no-properties 1)
  13254. "")))
  13255. (defun org-get-tags ()
  13256. "Get the list of tags specified in the current headline."
  13257. (org-split-string (org-get-tags-string) ":"))
  13258. (defun org-get-buffer-tags ()
  13259. "Get a table of all tags used in the buffer, for completion."
  13260. (let (tags)
  13261. (save-excursion
  13262. (goto-char (point-min))
  13263. (while (re-search-forward
  13264. (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
  13265. (when (equal (char-after (point-at-bol 0)) ?*)
  13266. (mapc (lambda (x) (add-to-list 'tags x))
  13267. (org-split-string (org-match-string-no-properties 1) ":")))))
  13268. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  13269. (mapcar 'list tags)))
  13270. ;;;; The mapping API
  13271. (defun org-map-entries (func &optional match scope &rest skip)
  13272. "Call FUNC at each headline selected by MATCH in SCOPE.
  13273. FUNC is a function or a lisp form. The function will be called without
  13274. arguments, with the cursor positioned at the beginning of the headline.
  13275. The return values of all calls to the function will be collected and
  13276. returned as a list.
  13277. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  13278. does not need to preserve point. After evaluation, the cursor will be
  13279. moved to the end of the line (presumably of the headline of the
  13280. processed entry) and search continues from there. Under some
  13281. circumstances, this may not produce the wanted results. For example,
  13282. if you have removed (e.g. archived) the current (sub)tree it could
  13283. mean that the next entry will be skipped entirely. In such cases, you
  13284. can specify the position from where search should continue by making
  13285. FUNC set the variable `org-map-continue-from' to the desired buffer
  13286. position.
  13287. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  13288. Only headlines that are matched by this query will be considered during
  13289. the iteration. When MATCH is nil or t, all headlines will be
  13290. visited by the iteration.
  13291. SCOPE determines the scope of this command. It can be any of:
  13292. nil The current buffer, respecting the restriction if any
  13293. tree The subtree started with the entry at point
  13294. region The entries within the active region, if any
  13295. region-start-level
  13296. The entries within the active region, but only those at
  13297. the same level than the first one.
  13298. file The current buffer, without restriction
  13299. file-with-archives
  13300. The current buffer, and any archives associated with it
  13301. agenda All agenda files
  13302. agenda-with-archives
  13303. All agenda files with any archive files associated with them
  13304. \(file1 file2 ...)
  13305. If this is a list, all files in the list will be scanned
  13306. The remaining args are treated as settings for the skipping facilities of
  13307. the scanner. The following items can be given here:
  13308. archive skip trees with the archive tag
  13309. comment skip trees with the COMMENT keyword
  13310. function or Emacs Lisp form:
  13311. will be used as value for `org-agenda-skip-function', so
  13312. whenever the function returns a position, FUNC will not be
  13313. called for that entry and search will continue from the
  13314. position returned
  13315. If your function needs to retrieve the tags including inherited tags
  13316. at the *current* entry, you can use the value of the variable
  13317. `org-scanner-tags' which will be much faster than getting the value
  13318. with `org-get-tags-at'. If your function gets properties with
  13319. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  13320. to t around the call to `org-entry-properties' to get the same speedup.
  13321. Note that if your function moves around to retrieve tags and properties at
  13322. a *different* entry, you cannot use these techniques."
  13323. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  13324. (not (org-region-active-p)))
  13325. (let* ((org-agenda-archives-mode nil) ; just to make sure
  13326. (org-agenda-skip-archived-trees (memq 'archive skip))
  13327. (org-agenda-skip-comment-trees (memq 'comment skip))
  13328. (org-agenda-skip-function
  13329. (car (org-delete-all '(comment archive) skip)))
  13330. (org-tags-match-list-sublevels t)
  13331. (start-level (eq scope 'region-start-level))
  13332. matcher file res
  13333. org-todo-keywords-for-agenda
  13334. org-done-keywords-for-agenda
  13335. org-todo-keyword-alist-for-agenda
  13336. org-drawers-for-agenda
  13337. org-tag-alist-for-agenda
  13338. todo-only)
  13339. (cond
  13340. ((eq match t) (setq matcher t))
  13341. ((eq match nil) (setq matcher t))
  13342. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  13343. (save-excursion
  13344. (save-restriction
  13345. (cond ((eq scope 'tree)
  13346. (org-back-to-heading t)
  13347. (org-narrow-to-subtree)
  13348. (setq scope nil))
  13349. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  13350. (org-region-active-p))
  13351. ;; If needed, set start-level to a string like "2"
  13352. (when start-level
  13353. (save-excursion
  13354. (goto-char (region-beginning))
  13355. (unless (org-at-heading-p) (outline-next-heading))
  13356. (setq start-level (org-current-level))))
  13357. (narrow-to-region (region-beginning)
  13358. (save-excursion
  13359. (goto-char (region-end))
  13360. (unless (and (bolp) (org-at-heading-p))
  13361. (outline-next-heading))
  13362. (point)))
  13363. (setq scope nil)))
  13364. (if (not scope)
  13365. (progn
  13366. (org-agenda-prepare-buffers
  13367. (list (buffer-file-name (current-buffer))))
  13368. (setq res (org-scan-tags func matcher todo-only start-level)))
  13369. ;; Get the right scope
  13370. (cond
  13371. ((and scope (listp scope) (symbolp (car scope)))
  13372. (setq scope (eval scope)))
  13373. ((eq scope 'agenda)
  13374. (setq scope (org-agenda-files t)))
  13375. ((eq scope 'agenda-with-archives)
  13376. (setq scope (org-agenda-files t))
  13377. (setq scope (org-add-archive-files scope)))
  13378. ((eq scope 'file)
  13379. (setq scope (list (buffer-file-name))))
  13380. ((eq scope 'file-with-archives)
  13381. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  13382. (org-agenda-prepare-buffers scope)
  13383. (while (setq file (pop scope))
  13384. (with-current-buffer (org-find-base-buffer-visiting file)
  13385. (save-excursion
  13386. (save-restriction
  13387. (widen)
  13388. (goto-char (point-min))
  13389. (setq res (append res (org-scan-tags func matcher todo-only))))))))))
  13390. res)))
  13391. ;;;; Properties
  13392. ;;; Setting and retrieving properties
  13393. (defconst org-special-properties
  13394. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  13395. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM" "CLOCKSUM_T")
  13396. "The special properties valid in Org-mode.
  13397. These are properties that are not defined in the property drawer,
  13398. but in some other way.")
  13399. (defconst org-default-properties
  13400. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  13401. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  13402. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  13403. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  13404. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  13405. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  13406. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  13407. "Some properties that are used by Org-mode for various purposes.
  13408. Being in this list makes sure that they are offered for completion.")
  13409. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  13410. "Regular expression matching the first line of a property drawer.")
  13411. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  13412. "Regular expression matching the last line of a property drawer.")
  13413. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  13414. "Regular expression matching the first line of a property drawer.")
  13415. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  13416. "Regular expression matching the first line of a property drawer.")
  13417. (defconst org-property-drawer-re
  13418. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  13419. org-property-end-re "\\)\n?")
  13420. "Matches an entire property drawer.")
  13421. (defconst org-clock-drawer-re
  13422. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  13423. org-property-end-re "\\)\n?")
  13424. "Matches an entire clock drawer.")
  13425. (defsubst org-re-property (property)
  13426. "Return a regexp matching a PROPERTY line.
  13427. Match group 1 will be set to the value."
  13428. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)"))
  13429. (defsubst org-re-property-keyword (property)
  13430. "Return a regexp matching a PROPERTY line, possibly with no
  13431. value for the property."
  13432. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)?"))
  13433. (defun org-property-action ()
  13434. "Do an action on properties."
  13435. (interactive)
  13436. (let (c)
  13437. (org-at-property-p)
  13438. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  13439. (setq c (read-char-exclusive))
  13440. (cond
  13441. ((equal c ?s)
  13442. (call-interactively 'org-set-property))
  13443. ((equal c ?d)
  13444. (call-interactively 'org-delete-property))
  13445. ((equal c ?D)
  13446. (call-interactively 'org-delete-property-globally))
  13447. ((equal c ?c)
  13448. (call-interactively 'org-compute-property-at-point))
  13449. (t (error "No such property action %c" c)))))
  13450. (defun org-inc-effort ()
  13451. "Increment the value of the effort property in the current entry."
  13452. (interactive)
  13453. (org-set-effort nil t))
  13454. (defvar org-clock-effort) ;; Defined in org-clock.el
  13455. (defvar org-clock-current-task) ;; Defined in org-clock.el
  13456. (defun org-set-effort (&optional value increment)
  13457. "Set the effort property of the current entry.
  13458. With numerical prefix arg, use the nth allowed value, 0 stands for the
  13459. 10th allowed value.
  13460. When INCREMENT is non-nil, set the property to the next allowed value."
  13461. (interactive "P")
  13462. (if (equal value 0) (setq value 10))
  13463. (let* ((completion-ignore-case t)
  13464. (prop org-effort-property)
  13465. (cur (org-entry-get nil prop))
  13466. (allowed (org-property-get-allowed-values nil prop 'table))
  13467. (existing (mapcar 'list (org-property-values prop)))
  13468. (heading (nth 4 (org-heading-components)))
  13469. rpl
  13470. (val (cond
  13471. ((stringp value) value)
  13472. ((and allowed (integerp value))
  13473. (or (car (nth (1- value) allowed))
  13474. (car (org-last allowed))))
  13475. ((and allowed increment)
  13476. (or (caadr (member (list cur) allowed))
  13477. (error "Allowed effort values are not set")))
  13478. (allowed
  13479. (message "Select 1-9,0, [RET%s]: %s"
  13480. (if cur (concat "=" cur) "")
  13481. (mapconcat 'car allowed " "))
  13482. (setq rpl (read-char-exclusive))
  13483. (if (equal rpl ?\r)
  13484. cur
  13485. (setq rpl (- rpl ?0))
  13486. (if (equal rpl 0) (setq rpl 10))
  13487. (if (and (> rpl 0) (<= rpl (length allowed)))
  13488. (car (nth (1- rpl) allowed))
  13489. (org-completing-read "Effort: " allowed nil))))
  13490. (t
  13491. (let (org-completion-use-ido org-completion-use-iswitchb)
  13492. (org-completing-read
  13493. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  13494. (concat "[" cur "]") "")
  13495. ": ")
  13496. existing nil nil "" nil cur))))))
  13497. (unless (equal (org-entry-get nil prop) val)
  13498. (org-entry-put nil prop val))
  13499. (save-excursion
  13500. (org-back-to-heading t)
  13501. (put-text-property (point-at-bol) (point-at-eol) 'org-effort val))
  13502. (when (string= heading org-clock-current-task)
  13503. (setq org-clock-effort (get-text-property (point-at-bol) 'org-effort))
  13504. (org-clock-update-mode-line))
  13505. (message "%s is now %s" prop val)))
  13506. (defun org-at-property-p ()
  13507. "Is cursor inside a property drawer?"
  13508. (save-excursion
  13509. (beginning-of-line 1)
  13510. (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
  13511. (save-match-data ;; Used by calling procedures
  13512. (let ((p (point))
  13513. (range (unless (org-before-first-heading-p)
  13514. (org-get-property-block))))
  13515. (and range (<= (car range) p) (< p (cdr range))))))))
  13516. (defun org-get-property-block (&optional beg end force)
  13517. "Return the (beg . end) range of the body of the property drawer.
  13518. BEG and END are the beginning and end of the current subtree, or of
  13519. the part before the first headline. If they are not given, they will
  13520. be found. If the drawer does not exist and FORCE is non-nil, create
  13521. the drawer."
  13522. (catch 'exit
  13523. (save-excursion
  13524. (let* ((beg (or beg (and (org-before-first-heading-p) (point-min))
  13525. (progn (org-back-to-heading t) (point))))
  13526. (end (or end (and (not (outline-next-heading)) (point-max))
  13527. (point))))
  13528. (goto-char beg)
  13529. (if (re-search-forward org-property-start-re end t)
  13530. (setq beg (1+ (match-end 0)))
  13531. (if force
  13532. (save-excursion
  13533. (org-insert-property-drawer)
  13534. (setq end (progn (outline-next-heading) (point))))
  13535. (throw 'exit nil))
  13536. (goto-char beg)
  13537. (if (re-search-forward org-property-start-re end t)
  13538. (setq beg (1+ (match-end 0)))))
  13539. (if (re-search-forward org-property-end-re end t)
  13540. (setq end (match-beginning 0))
  13541. (or force (throw 'exit nil))
  13542. (goto-char beg)
  13543. (setq end beg)
  13544. (org-indent-line)
  13545. (insert ":END:\n"))
  13546. (cons beg end)))))
  13547. (defun org-entry-properties (&optional pom which specific)
  13548. "Get all properties of the entry at point-or-marker POM.
  13549. This includes the TODO keyword, the tags, time strings for deadline,
  13550. scheduled, and clocking, and any additional properties defined in the
  13551. entry. The return value is an alist, keys may occur multiple times
  13552. if the property key was used several times.
  13553. POM may also be nil, in which case the current entry is used.
  13554. If WHICH is nil or `all', get all properties. If WHICH is
  13555. `special' or `standard', only get that subclass. If WHICH
  13556. is a string only get exactly this property. SPECIFIC can be a string, the
  13557. specific property we are interested in. Specifying it can speed
  13558. things up because then unnecessary parsing is avoided."
  13559. (setq which (or which 'all))
  13560. (org-with-point-at pom
  13561. (let ((clockstr (substring org-clock-string 0 -1))
  13562. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  13563. (case-fold-search nil)
  13564. beg end range props sum-props key key1 value string clocksum clocksumt)
  13565. (save-excursion
  13566. (when (condition-case nil
  13567. (and (derived-mode-p 'org-mode) (org-back-to-heading t))
  13568. (error nil))
  13569. (setq beg (point))
  13570. (setq sum-props (get-text-property (point) 'org-summaries))
  13571. (setq clocksum (get-text-property (point) :org-clock-minutes)
  13572. clocksumt (get-text-property (point) :org-clock-minutes-today))
  13573. (outline-next-heading)
  13574. (setq end (point))
  13575. (when (memq which '(all special))
  13576. ;; Get the special properties, like TODO and tags
  13577. (goto-char beg)
  13578. (when (and (or (not specific) (string= specific "TODO"))
  13579. (looking-at org-todo-line-regexp) (match-end 2))
  13580. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  13581. (when (and (or (not specific) (string= specific "PRIORITY"))
  13582. (looking-at org-priority-regexp))
  13583. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  13584. (when (or (not specific) (string= specific "FILE"))
  13585. (push (cons "FILE" buffer-file-name) props))
  13586. (when (and (or (not specific) (string= specific "TAGS"))
  13587. (setq value (org-get-tags-string))
  13588. (string-match "\\S-" value))
  13589. (push (cons "TAGS" value) props))
  13590. (when (and (or (not specific) (string= specific "ALLTAGS"))
  13591. (setq value (org-get-tags-at)))
  13592. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  13593. ":"))
  13594. props))
  13595. (when (or (not specific) (string= specific "BLOCKED"))
  13596. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  13597. (when (or (not specific)
  13598. (member specific
  13599. '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
  13600. "TIMESTAMP" "TIMESTAMP_IA")))
  13601. (catch 'match
  13602. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  13603. (setq key (if (match-end 1)
  13604. (substring (org-match-string-no-properties 1)
  13605. 0 -1))
  13606. string (if (equal key clockstr)
  13607. (org-trim
  13608. (buffer-substring-no-properties
  13609. (match-beginning 3) (goto-char
  13610. (point-at-eol))))
  13611. (substring (org-match-string-no-properties 3)
  13612. 1 -1)))
  13613. ;; Get the correct property name from the key. This is
  13614. ;; necessary if the user has configured time keywords.
  13615. (setq key1 (concat key ":"))
  13616. (cond
  13617. ((not key)
  13618. (setq key
  13619. (if (= (char-after (match-beginning 3)) ?\[)
  13620. "TIMESTAMP_IA" "TIMESTAMP")))
  13621. ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
  13622. ((equal key1 org-deadline-string) (setq key "DEADLINE"))
  13623. ((equal key1 org-closed-string) (setq key "CLOSED"))
  13624. ((equal key1 org-clock-string) (setq key "CLOCK")))
  13625. (if (and specific (equal key specific) (not (equal key "CLOCK")))
  13626. (progn
  13627. (push (cons key string) props)
  13628. ;; no need to search further if match is found
  13629. (throw 'match t))
  13630. (when (or (equal key "CLOCK") (not (assoc key props)))
  13631. (push (cons key string) props)))))))
  13632. (when (memq which '(all standard))
  13633. ;; Get the standard properties, like :PROP: ...
  13634. (setq range (org-get-property-block beg end))
  13635. (when range
  13636. (goto-char (car range))
  13637. (while (re-search-forward
  13638. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  13639. (cdr range) t)
  13640. (setq key (org-match-string-no-properties 1)
  13641. value (org-trim (or (org-match-string-no-properties 2) "")))
  13642. (unless (member key excluded)
  13643. (push (cons key (or value "")) props)))))
  13644. (if clocksum
  13645. (push (cons "CLOCKSUM"
  13646. (org-columns-number-to-string (/ (float clocksum) 60.)
  13647. 'add_times))
  13648. props))
  13649. (if clocksumt
  13650. (push (cons "CLOCKSUM_T"
  13651. (org-columns-number-to-string (/ (float clocksumt) 60.)
  13652. 'add_times))
  13653. props))
  13654. (unless (assoc "CATEGORY" props)
  13655. (push (cons "CATEGORY" (org-get-category)) props))
  13656. (append sum-props (nreverse props)))))))
  13657. (defun org-entry-get (pom property &optional inherit literal-nil)
  13658. "Get value of PROPERTY for entry or content at point-or-marker POM.
  13659. If INHERIT is non-nil and the entry does not have the property,
  13660. then also check higher levels of the hierarchy.
  13661. If INHERIT is the symbol `selective', use inheritance only if the setting
  13662. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  13663. If the property is present but empty, the return value is the empty string.
  13664. If the property is not present at all, nil is returned.
  13665. If LITERAL-NIL is set, return the string value \"nil\" as a string,
  13666. do not interpret it as the list atom nil. This is used for inheritance
  13667. when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
  13668. (org-with-point-at pom
  13669. (if (and inherit (if (eq inherit 'selective)
  13670. (org-property-inherit-p property)
  13671. t))
  13672. (org-entry-get-with-inheritance property literal-nil)
  13673. (if (member property org-special-properties)
  13674. ;; We need a special property. Use `org-entry-properties' to
  13675. ;; retrieve it, but specify the wanted property
  13676. (cdr (assoc property (org-entry-properties nil 'special property)))
  13677. (let ((range (org-get-property-block)))
  13678. (when (and range (not (eq (car range) (cdr range))))
  13679. (let* ((props (list (or (assoc property org-file-properties)
  13680. (assoc property org-global-properties)
  13681. (assoc property org-global-properties-fixed))))
  13682. (ap (lambda (key)
  13683. (when (re-search-forward
  13684. (org-re-property key) (cdr range) t)
  13685. (setq props
  13686. (org-update-property-plist
  13687. key
  13688. (if (match-end 1)
  13689. (org-match-string-no-properties 1) "")
  13690. props)))))
  13691. val)
  13692. (goto-char (car range))
  13693. (funcall ap property)
  13694. (goto-char (car range))
  13695. (while (funcall ap (concat property "+")))
  13696. (setq val (cdr (assoc property props)))
  13697. (when val (if literal-nil val (org-not-nil val))))))))))
  13698. (defun org-property-or-variable-value (var &optional inherit)
  13699. "Check if there is a property fixing the value of VAR.
  13700. If yes, return this value. If not, return the current value of the variable."
  13701. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  13702. (if (and prop (stringp prop) (string-match "\\S-" prop))
  13703. (read prop)
  13704. (symbol-value var))))
  13705. (defun org-entry-delete (pom property &optional delete-empty-drawer)
  13706. "Delete the property PROPERTY from entry at point-or-marker POM.
  13707. When optional argument DELETE-EMPTY-DRAWER is a string, it defines
  13708. an empty drawer to delete."
  13709. (org-with-point-at pom
  13710. (if (member property org-special-properties)
  13711. nil ; cannot delete these properties.
  13712. (let ((range (org-get-property-block)))
  13713. (if (and range
  13714. (goto-char (car range))
  13715. (re-search-forward
  13716. (org-re-property property)
  13717. (cdr range) t))
  13718. (progn
  13719. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  13720. (and delete-empty-drawer
  13721. (org-remove-empty-drawer-at
  13722. delete-empty-drawer (car range)))
  13723. t)
  13724. nil)))))
  13725. ;; Multi-values properties are properties that contain multiple values
  13726. ;; These values are assumed to be single words, separated by whitespace.
  13727. (defun org-entry-add-to-multivalued-property (pom property value)
  13728. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  13729. (let* ((old (org-entry-get pom property))
  13730. (values (and old (org-split-string old "[ \t]"))))
  13731. (setq value (org-entry-protect-space value))
  13732. (unless (member value values)
  13733. (setq values (cons value values))
  13734. (org-entry-put pom property
  13735. (mapconcat 'identity values " ")))))
  13736. (defun org-entry-remove-from-multivalued-property (pom property value)
  13737. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  13738. (let* ((old (org-entry-get pom property))
  13739. (values (and old (org-split-string old "[ \t]"))))
  13740. (setq value (org-entry-protect-space value))
  13741. (when (member value values)
  13742. (setq values (delete value values))
  13743. (org-entry-put pom property
  13744. (mapconcat 'identity values " ")))))
  13745. (defun org-entry-member-in-multivalued-property (pom property value)
  13746. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  13747. (let* ((old (org-entry-get pom property))
  13748. (values (and old (org-split-string old "[ \t]"))))
  13749. (setq value (org-entry-protect-space value))
  13750. (member value values)))
  13751. (defun org-entry-get-multivalued-property (pom property)
  13752. "Return a list of values in a multivalued property."
  13753. (let* ((value (org-entry-get pom property))
  13754. (values (and value (org-split-string value "[ \t]"))))
  13755. (mapcar 'org-entry-restore-space values)))
  13756. (defun org-entry-put-multivalued-property (pom property &rest values)
  13757. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  13758. VALUES should be a list of strings. Spaces will be protected."
  13759. (org-entry-put pom property
  13760. (mapconcat 'org-entry-protect-space values " "))
  13761. (let* ((value (org-entry-get pom property))
  13762. (values (and value (org-split-string value "[ \t]"))))
  13763. (mapcar 'org-entry-restore-space values)))
  13764. (defun org-entry-protect-space (s)
  13765. "Protect spaces and newline in string S."
  13766. (while (string-match " " s)
  13767. (setq s (replace-match "%20" t t s)))
  13768. (while (string-match "\n" s)
  13769. (setq s (replace-match "%0A" t t s)))
  13770. s)
  13771. (defun org-entry-restore-space (s)
  13772. "Restore spaces and newline in string S."
  13773. (while (string-match "%20" s)
  13774. (setq s (replace-match " " t t s)))
  13775. (while (string-match "%0A" s)
  13776. (setq s (replace-match "\n" t t s)))
  13777. s)
  13778. (defvar org-entry-property-inherited-from (make-marker)
  13779. "Marker pointing to the entry from where a property was inherited.
  13780. Each call to `org-entry-get-with-inheritance' will set this marker to the
  13781. location of the entry where the inheritance search matched. If there was
  13782. no match, the marker will point nowhere.
  13783. Note that also `org-entry-get' calls this function, if the INHERIT flag
  13784. is set.")
  13785. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  13786. "Get PROPERTY of entry or content at point, search higher levels if needed.
  13787. The search will stop at the first ancestor which has the property defined.
  13788. If the value found is \"nil\", return nil to show that the property
  13789. should be considered as undefined (this is the meaning of nil here).
  13790. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  13791. (move-marker org-entry-property-inherited-from nil)
  13792. (let (tmp)
  13793. (save-excursion
  13794. (save-restriction
  13795. (widen)
  13796. (catch 'ex
  13797. (while t
  13798. (when (setq tmp (org-entry-get nil property nil 'literal-nil))
  13799. (or (ignore-errors (org-back-to-heading t))
  13800. (goto-char (point-min)))
  13801. (move-marker org-entry-property-inherited-from (point))
  13802. (throw 'ex tmp))
  13803. (or (ignore-errors (org-up-heading-safe))
  13804. (throw 'ex nil))))))
  13805. (setq tmp (or tmp
  13806. (cdr (assoc property org-file-properties))
  13807. (cdr (assoc property org-global-properties))
  13808. (cdr (assoc property org-global-properties-fixed))))
  13809. (if literal-nil tmp (org-not-nil tmp))))
  13810. (defvar org-property-changed-functions nil
  13811. "Hook called when the value of a property has changed.
  13812. Each hook function should accept two arguments, the name of the property
  13813. and the new value.")
  13814. (defun org-entry-put (pom property value)
  13815. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  13816. (org-with-point-at pom
  13817. (org-back-to-heading t)
  13818. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  13819. range)
  13820. (cond
  13821. ((equal property "TODO")
  13822. (when (and (stringp value) (string-match "\\S-" value)
  13823. (not (member value org-todo-keywords-1)))
  13824. (error "\"%s\" is not a valid TODO state" value))
  13825. (if (or (not value)
  13826. (not (string-match "\\S-" value)))
  13827. (setq value 'none))
  13828. (org-todo value)
  13829. (org-set-tags nil 'align))
  13830. ((equal property "PRIORITY")
  13831. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  13832. (string-to-char value) ?\ ))
  13833. (org-set-tags nil 'align))
  13834. ((equal property "CLOCKSUM")
  13835. (if (not (re-search-forward
  13836. (concat org-clock-string ".*\\]--\\(\\[[^]]+\\]\\)") nil t))
  13837. (error "Cannot find a clock log")
  13838. (goto-char (- (match-end 1) 2))
  13839. (cond
  13840. ((eq value 'earlier) (org-timestamp-down))
  13841. ((eq value 'later) (org-timestamp-up)))
  13842. (org-clock-sum-current-item)))
  13843. ((equal property "SCHEDULED")
  13844. (if (re-search-forward org-scheduled-time-regexp end t)
  13845. (cond
  13846. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13847. ((eq value 'later) (org-timestamp-change 1 'day))
  13848. (t (call-interactively 'org-schedule)))
  13849. (call-interactively 'org-schedule)))
  13850. ((equal property "DEADLINE")
  13851. (if (re-search-forward org-deadline-time-regexp end t)
  13852. (cond
  13853. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13854. ((eq value 'later) (org-timestamp-change 1 'day))
  13855. (t (call-interactively 'org-deadline)))
  13856. (call-interactively 'org-deadline)))
  13857. ((member property org-special-properties)
  13858. (error "The %s property can not yet be set with `org-entry-put'"
  13859. property))
  13860. (t ; a non-special property
  13861. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  13862. (setq range (org-get-property-block beg end 'force))
  13863. (goto-char (car range))
  13864. (if (re-search-forward
  13865. (org-re-property-keyword property) (cdr range) t)
  13866. (progn
  13867. (delete-region (match-beginning 0) (match-end 0))
  13868. (goto-char (match-beginning 0)))
  13869. (goto-char (cdr range))
  13870. (insert "\n")
  13871. (backward-char 1)
  13872. (org-indent-line))
  13873. (insert ":" property ":")
  13874. (and value (insert " " value))
  13875. (org-indent-line)))))
  13876. (run-hook-with-args 'org-property-changed-functions property value)))
  13877. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  13878. "Get all property keys in the current buffer.
  13879. With INCLUDE-SPECIALS, also list the special properties that reflect things
  13880. like tags and TODO state.
  13881. With INCLUDE-DEFAULTS, also include properties that has special meaning
  13882. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
  13883. and others.
  13884. With INCLUDE-COLUMNS, also include property names given in COLUMN
  13885. formats in the current buffer."
  13886. (let (rtn range cfmt s p)
  13887. (save-excursion
  13888. (save-restriction
  13889. (widen)
  13890. (goto-char (point-min))
  13891. (while (re-search-forward org-property-start-re nil t)
  13892. (setq range (org-get-property-block))
  13893. (goto-char (car range))
  13894. (while (re-search-forward
  13895. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  13896. (cdr range) t)
  13897. (add-to-list 'rtn (org-match-string-no-properties 1)))
  13898. (outline-next-heading))))
  13899. (when include-specials
  13900. (setq rtn (append org-special-properties rtn)))
  13901. (when include-defaults
  13902. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  13903. (add-to-list 'rtn org-effort-property))
  13904. (when include-columns
  13905. (save-excursion
  13906. (save-restriction
  13907. (widen)
  13908. (goto-char (point-min))
  13909. (while (re-search-forward
  13910. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  13911. nil t)
  13912. (setq cfmt (match-string 2) s 0)
  13913. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  13914. cfmt s)
  13915. (setq s (match-end 0)
  13916. p (match-string 1 cfmt))
  13917. (unless (or (equal p "ITEM")
  13918. (member p org-special-properties))
  13919. (add-to-list 'rtn (match-string 1 cfmt))))))))
  13920. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  13921. (defun org-property-values (key)
  13922. "Return a list of all values of property KEY in the current buffer."
  13923. (save-excursion
  13924. (save-restriction
  13925. (widen)
  13926. (goto-char (point-min))
  13927. (let ((re (org-re-property key))
  13928. values)
  13929. (while (re-search-forward re nil t)
  13930. (add-to-list 'values (org-trim (match-string 1))))
  13931. (delete "" values)))))
  13932. (defun org-insert-property-drawer ()
  13933. "Insert a property drawer into the current entry."
  13934. (org-back-to-heading t)
  13935. (looking-at org-outline-regexp)
  13936. (let ((indent (if org-adapt-indentation
  13937. (- (match-end 0) (match-beginning 0))
  13938. 0))
  13939. (beg (point))
  13940. (re (concat "^[ \t]*" org-keyword-time-regexp))
  13941. end hiddenp)
  13942. (outline-next-heading)
  13943. (setq end (point))
  13944. (goto-char beg)
  13945. (while (re-search-forward re end t))
  13946. (setq hiddenp (outline-invisible-p))
  13947. (end-of-line 1)
  13948. (and (equal (char-after) ?\n) (forward-char 1))
  13949. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  13950. (if (member (match-string 1) '("CLOCK:" ":END:"))
  13951. ;; just skip this line
  13952. (beginning-of-line 2)
  13953. ;; Drawer start, find the end
  13954. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  13955. (beginning-of-line 1)))
  13956. (org-skip-over-state-notes)
  13957. (skip-chars-backward " \t\n\r")
  13958. (if (eq (char-before) ?*) (forward-char 1))
  13959. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  13960. (beginning-of-line 0)
  13961. (org-indent-to-column indent)
  13962. (beginning-of-line 2)
  13963. (org-indent-to-column indent)
  13964. (beginning-of-line 0)
  13965. (if hiddenp
  13966. (save-excursion
  13967. (org-back-to-heading t)
  13968. (hide-entry))
  13969. (org-flag-drawer t))))
  13970. (defun org-insert-drawer (&optional arg drawer)
  13971. "Insert a drawer at point.
  13972. Optional argument DRAWER, when non-nil, is a string representing
  13973. drawer's name. Otherwise, the user is prompted for a name.
  13974. If a region is active, insert the drawer around that region
  13975. instead.
  13976. Point is left between drawer's boundaries."
  13977. (interactive "P")
  13978. (let* ((logbook (if (stringp org-log-into-drawer) org-log-into-drawer
  13979. "LOGBOOK"))
  13980. ;; SYSTEM-DRAWERS is a list of drawer names that are used
  13981. ;; internally by Org. They are meant to be inserted
  13982. ;; automatically.
  13983. (system-drawers `("CLOCK" ,logbook "PROPERTIES"))
  13984. ;; Remove system drawers from list. Note: For some reason,
  13985. ;; `org-completing-read' ignores the predicate while
  13986. ;; `completing-read' handles it fine.
  13987. (drawer (if arg "PROPERTIES"
  13988. (or drawer
  13989. (completing-read
  13990. "Drawer: " org-drawers
  13991. (lambda (d) (not (member d system-drawers))))))))
  13992. (cond
  13993. ;; With C-u, fall back on `org-insert-property-drawer'
  13994. (arg (org-insert-property-drawer))
  13995. ;; With an active region, insert a drawer at point.
  13996. ((not (org-region-active-p))
  13997. (progn
  13998. (unless (bolp) (insert "\n"))
  13999. (insert (format ":%s:\n\n:END:\n" drawer))
  14000. (forward-line -2)))
  14001. ;; Otherwise, insert the drawer at point
  14002. (t
  14003. (let ((rbeg (region-beginning))
  14004. (rend (copy-marker (region-end))))
  14005. (unwind-protect
  14006. (progn
  14007. (goto-char rbeg)
  14008. (beginning-of-line)
  14009. (when (save-excursion
  14010. (re-search-forward org-outline-regexp-bol rend t))
  14011. (error "Drawers cannot contain headlines"))
  14012. ;; Position point at the beginning of the first
  14013. ;; non-blank line in region. Insert drawer's opening
  14014. ;; there, then indent it.
  14015. (org-skip-whitespace)
  14016. (beginning-of-line)
  14017. (insert ":" drawer ":\n")
  14018. (forward-line -1)
  14019. (indent-for-tab-command)
  14020. ;; Move point to the beginning of the first blank line
  14021. ;; after the last non-blank line in region. Insert
  14022. ;; drawer's closing, then indent it.
  14023. (goto-char rend)
  14024. (skip-chars-backward " \r\t\n")
  14025. (insert "\n:END:")
  14026. (deactivate-mark t)
  14027. (indent-for-tab-command)
  14028. (unless (eolp) (insert "\n")))
  14029. ;; Clear marker, whatever the outcome of insertion is.
  14030. (set-marker rend nil)))))))
  14031. (defvar org-property-set-functions-alist nil
  14032. "Property set function alist.
  14033. Each entry should have the following format:
  14034. (PROPERTY . READ-FUNCTION)
  14035. The read function will be called with the same argument as
  14036. `org-completing-read'.")
  14037. (defun org-set-property-function (property)
  14038. "Get the function that should be used to set PROPERTY.
  14039. This is computed according to `org-property-set-functions-alist'."
  14040. (or (cdr (assoc property org-property-set-functions-alist))
  14041. 'org-completing-read))
  14042. (defun org-read-property-value (property)
  14043. "Read PROPERTY value from user."
  14044. (let* ((completion-ignore-case t)
  14045. (allowed (org-property-get-allowed-values nil property 'table))
  14046. (cur (org-entry-get nil property))
  14047. (prompt (concat property " value"
  14048. (if (and cur (string-match "\\S-" cur))
  14049. (concat " [" cur "]") "") ": "))
  14050. (set-function (org-set-property-function property))
  14051. (val (if allowed
  14052. (funcall set-function prompt allowed nil
  14053. (not (get-text-property 0 'org-unrestricted
  14054. (caar allowed))))
  14055. (let (org-completion-use-ido org-completion-use-iswitchb)
  14056. (funcall set-function prompt
  14057. (mapcar 'list (org-property-values property))
  14058. nil nil "" nil cur)))))
  14059. (if (equal val "")
  14060. cur
  14061. val)))
  14062. (defvar org-last-set-property nil)
  14063. (defvar org-last-set-property-value nil)
  14064. (defun org-read-property-name ()
  14065. "Read a property name."
  14066. (let* ((completion-ignore-case t)
  14067. (keys (org-buffer-property-keys nil t t))
  14068. (default-prop (or (save-excursion
  14069. (save-match-data
  14070. (beginning-of-line)
  14071. (and (looking-at "^\\s-*:\\([^:\n]+\\):")
  14072. (null (string= (match-string 1) "END"))
  14073. (match-string 1))))
  14074. org-last-set-property))
  14075. (property (org-icompleting-read
  14076. (concat "Property"
  14077. (if default-prop (concat " [" default-prop "]") "")
  14078. ": ")
  14079. (mapcar 'list keys)
  14080. nil nil nil nil
  14081. default-prop)))
  14082. (if (member property keys)
  14083. property
  14084. (or (cdr (assoc (downcase property)
  14085. (mapcar (lambda (x) (cons (downcase x) x))
  14086. keys)))
  14087. property))))
  14088. (defun org-set-property-and-value (use-last)
  14089. "Allow to set [PROPERTY]: [value] direction from prompt.
  14090. When use-default, don't even ask, just use the last
  14091. \"[PROPERTY]: [value]\" string from the history."
  14092. (interactive "P")
  14093. (let* ((completion-ignore-case t)
  14094. (pv (or (and use-last org-last-set-property-value)
  14095. (org-completing-read
  14096. "Enter a \"[Property]: [value]\" pair: "
  14097. nil nil nil nil nil
  14098. org-last-set-property-value)))
  14099. prop val)
  14100. (when (string-match "^[ \t]*\\([^:]+\\):[ \t]*\\(.*\\)[ \t]*$" pv)
  14101. (setq prop (match-string 1 pv)
  14102. val (match-string 2 pv))
  14103. (org-set-property prop val))))
  14104. (defun org-set-property (property value)
  14105. "In the current entry, set PROPERTY to VALUE.
  14106. When called interactively, this will prompt for a property name, offering
  14107. completion on existing and default properties. And then it will prompt
  14108. for a value, offering completion either on allowed values (via an inherited
  14109. xxx_ALL property) or on existing values in other instances of this property
  14110. in the current file."
  14111. (interactive (list nil nil))
  14112. (let* ((property (or property (org-read-property-name)))
  14113. (value (or value (org-read-property-value property)))
  14114. (fn (cdr (assoc property org-properties-postprocess-alist))))
  14115. (setq org-last-set-property property)
  14116. (setq org-last-set-property-value (concat property ": " value))
  14117. ;; Possibly postprocess the inserted value:
  14118. (when fn (setq value (funcall fn value)))
  14119. (unless (equal (org-entry-get nil property) value)
  14120. (org-entry-put nil property value))))
  14121. (defun org-delete-property (property &optional delete-empty-drawer)
  14122. "In the current entry, delete PROPERTY.
  14123. When optional argument DELETE-EMPTY-DRAWER is a string, it defines
  14124. an empty drawer to delete."
  14125. (interactive
  14126. (let* ((completion-ignore-case t)
  14127. (prop (org-icompleting-read "Property: "
  14128. (org-entry-properties nil 'standard))))
  14129. (list prop)))
  14130. (message "Property %s %s" property
  14131. (if (org-entry-delete nil property delete-empty-drawer)
  14132. "deleted"
  14133. "was not present in the entry")))
  14134. (defun org-delete-property-globally (property)
  14135. "Remove PROPERTY globally, from all entries."
  14136. (interactive
  14137. (let* ((completion-ignore-case t)
  14138. (prop (org-icompleting-read
  14139. "Globally remove property: "
  14140. (mapcar 'list (org-buffer-property-keys)))))
  14141. (list prop)))
  14142. (save-excursion
  14143. (save-restriction
  14144. (widen)
  14145. (goto-char (point-min))
  14146. (let ((cnt 0))
  14147. (while (re-search-forward
  14148. (org-re-property property)
  14149. nil t)
  14150. (setq cnt (1+ cnt))
  14151. (delete-region (match-beginning 0) (1+ (point-at-eol))))
  14152. (message "Property \"%s\" removed from %d entries" property cnt)))))
  14153. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  14154. (defun org-compute-property-at-point ()
  14155. "Compute the property at point.
  14156. This looks for an enclosing column format, extracts the operator and
  14157. then applies it to the property in the column format's scope."
  14158. (interactive)
  14159. (unless (org-at-property-p)
  14160. (error "Not at a property"))
  14161. (let ((prop (org-match-string-no-properties 2)))
  14162. (org-columns-get-format-and-top-level)
  14163. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  14164. (error "No operator defined for property %s" prop))
  14165. (org-columns-compute prop)))
  14166. (defvar org-property-allowed-value-functions nil
  14167. "Hook for functions supplying allowed values for a specific property.
  14168. The functions must take a single argument, the name of the property, and
  14169. return a flat list of allowed values. If \":ETC\" is one of
  14170. the values, this means that these values are intended as defaults for
  14171. completion, but that other values should be allowed too.
  14172. The functions must return nil if they are not responsible for this
  14173. property.")
  14174. (defun org-property-get-allowed-values (pom property &optional table)
  14175. "Get allowed values for the property PROPERTY.
  14176. When TABLE is non-nil, return an alist that can directly be used for
  14177. completion."
  14178. (let (vals)
  14179. (cond
  14180. ((equal property "TODO")
  14181. (setq vals (org-with-point-at pom
  14182. (append org-todo-keywords-1 '("")))))
  14183. ((equal property "PRIORITY")
  14184. (let ((n org-lowest-priority))
  14185. (while (>= n org-highest-priority)
  14186. (push (char-to-string n) vals)
  14187. (setq n (1- n)))))
  14188. ((member property org-special-properties))
  14189. ((setq vals (run-hook-with-args-until-success
  14190. 'org-property-allowed-value-functions property)))
  14191. (t
  14192. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  14193. (when (and vals (string-match "\\S-" vals))
  14194. (setq vals (car (read-from-string (concat "(" vals ")"))))
  14195. (setq vals (mapcar (lambda (x)
  14196. (cond ((stringp x) x)
  14197. ((numberp x) (number-to-string x))
  14198. ((symbolp x) (symbol-name x))
  14199. (t "???")))
  14200. vals)))))
  14201. (when (member ":ETC" vals)
  14202. (setq vals (remove ":ETC" vals))
  14203. (org-add-props (car vals) '(org-unrestricted t)))
  14204. (if table (mapcar 'list vals) vals)))
  14205. (defun org-property-previous-allowed-value (&optional previous)
  14206. "Switch to the next allowed value for this property."
  14207. (interactive)
  14208. (org-property-next-allowed-value t))
  14209. (defun org-property-next-allowed-value (&optional previous)
  14210. "Switch to the next allowed value for this property."
  14211. (interactive)
  14212. (unless (org-at-property-p)
  14213. (error "Not at a property"))
  14214. (let* ((prop (car (save-match-data (org-split-string (match-string 1) ":"))))
  14215. (key (match-string 2))
  14216. (value (match-string 3))
  14217. (allowed (or (org-property-get-allowed-values (point) key)
  14218. (and (member value '("[ ]" "[-]" "[X]"))
  14219. '("[ ]" "[X]"))))
  14220. (heading (save-match-data (nth 4 (org-heading-components))))
  14221. nval)
  14222. (unless allowed
  14223. (error "Allowed values for this property have not been defined"))
  14224. (if previous (setq allowed (reverse allowed)))
  14225. (if (member value allowed)
  14226. (setq nval (car (cdr (member value allowed)))))
  14227. (setq nval (or nval (car allowed)))
  14228. (if (equal nval value)
  14229. (error "Only one allowed value for this property"))
  14230. (org-at-property-p)
  14231. (replace-match (concat " :" key ": " nval) t t)
  14232. (org-indent-line)
  14233. (beginning-of-line 1)
  14234. (skip-chars-forward " \t")
  14235. (when (equal prop org-effort-property)
  14236. (save-excursion
  14237. (org-back-to-heading t)
  14238. (put-text-property (point-at-bol) (point-at-eol) 'org-effort nval))
  14239. (when (string= org-clock-current-task heading)
  14240. (setq org-clock-effort nval)
  14241. (org-clock-update-mode-line)))
  14242. (run-hook-with-args 'org-property-changed-functions key nval)))
  14243. (defun org-find-olp (path &optional this-buffer)
  14244. "Return a marker pointing to the entry at outline path OLP.
  14245. If anything goes wrong, throw an error.
  14246. You can wrap this call to catch the error like this:
  14247. (condition-case msg
  14248. (org-mobile-locate-entry (match-string 4))
  14249. (error (nth 1 msg)))
  14250. The return value will then be either a string with the error message,
  14251. or a marker if everything is OK.
  14252. If THIS-BUFFER is set, the outline path does not contain a file,
  14253. only headings."
  14254. (let* ((file (if this-buffer buffer-file-name (pop path)))
  14255. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  14256. (level 1)
  14257. (lmin 1)
  14258. (lmax 1)
  14259. limit re end found pos heading cnt flevel)
  14260. (unless buffer (error "File not found :%s" file))
  14261. (with-current-buffer buffer
  14262. (save-excursion
  14263. (save-restriction
  14264. (widen)
  14265. (setq limit (point-max))
  14266. (goto-char (point-min))
  14267. (while (setq heading (pop path))
  14268. (setq re (format org-complex-heading-regexp-format
  14269. (regexp-quote heading)))
  14270. (setq cnt 0 pos (point))
  14271. (while (re-search-forward re end t)
  14272. (setq level (- (match-end 1) (match-beginning 1)))
  14273. (if (and (>= level lmin) (<= level lmax))
  14274. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  14275. (when (= cnt 0) (error "Heading not found on level %d: %s"
  14276. lmax heading))
  14277. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  14278. lmax heading))
  14279. (goto-char found)
  14280. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  14281. (setq end (save-excursion (org-end-of-subtree t t))))
  14282. (when (org-at-heading-p)
  14283. (point-marker)))))))
  14284. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  14285. "Find node HEADING in BUFFER.
  14286. Return a marker to the heading if it was found, or nil if not.
  14287. If POS-ONLY is set, return just the position instead of a marker.
  14288. The heading text must match exact, but it may have a TODO keyword,
  14289. a priority cookie and tags in the standard locations."
  14290. (with-current-buffer (or buffer (current-buffer))
  14291. (save-excursion
  14292. (save-restriction
  14293. (widen)
  14294. (goto-char (point-min))
  14295. (let (case-fold-search)
  14296. (if (re-search-forward
  14297. (format org-complex-heading-regexp-format
  14298. (regexp-quote heading)) nil t)
  14299. (if pos-only
  14300. (match-beginning 0)
  14301. (move-marker (make-marker) (match-beginning 0)))))))))
  14302. (defun org-find-exact-heading-in-directory (heading &optional dir)
  14303. "Find Org node headline HEADING in all .org files in directory DIR.
  14304. When the target headline is found, return a marker to this location."
  14305. (let ((files (directory-files (or dir default-directory)
  14306. nil "\\`[^.#].*\\.org\\'"))
  14307. file visiting m buffer)
  14308. (catch 'found
  14309. (while (setq file (pop files))
  14310. (message "trying %s" file)
  14311. (setq visiting (org-find-base-buffer-visiting file))
  14312. (setq buffer (or visiting (find-file-noselect file)))
  14313. (setq m (org-find-exact-headline-in-buffer
  14314. heading buffer))
  14315. (when (and (not m) (not visiting)) (kill-buffer buffer))
  14316. (and m (throw 'found m))))))
  14317. (defun org-find-entry-with-id (ident)
  14318. "Locate the entry that contains the ID property with exact value IDENT.
  14319. IDENT can be a string, a symbol or a number, this function will search for
  14320. the string representation of it.
  14321. Return the position where this entry starts, or nil if there is no such entry."
  14322. (interactive "sID: ")
  14323. (let ((id (cond
  14324. ((stringp ident) ident)
  14325. ((symbol-name ident) (symbol-name ident))
  14326. ((numberp ident) (number-to-string ident))
  14327. (t (error "IDENT %s must be a string, symbol or number" ident))))
  14328. (case-fold-search nil))
  14329. (save-excursion
  14330. (save-restriction
  14331. (widen)
  14332. (goto-char (point-min))
  14333. (when (re-search-forward
  14334. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  14335. nil t)
  14336. (org-back-to-heading t)
  14337. (point))))))
  14338. ;;;; Timestamps
  14339. (defvar org-last-changed-timestamp nil)
  14340. (defvar org-last-inserted-timestamp nil
  14341. "The last time stamp inserted with `org-insert-time-stamp'.")
  14342. (defvar org-time-was-given) ; dynamically scoped parameter
  14343. (defvar org-end-time-was-given) ; dynamically scoped parameter
  14344. (defvar org-ts-what) ; dynamically scoped parameter
  14345. (defun org-time-stamp (arg &optional inactive)
  14346. "Prompt for a date/time and insert a time stamp.
  14347. If the user specifies a time like HH:MM or if this command is
  14348. called with at least one prefix argument, the time stamp contains
  14349. the date and the time. Otherwise, only the date is be included.
  14350. All parts of a date not specified by the user is filled in from
  14351. the current date/time. So if you just press return without
  14352. typing anything, the time stamp will represent the current
  14353. date/time.
  14354. If there is already a timestamp at the cursor, it will be
  14355. modified.
  14356. With two universal prefix arguments, insert an active timestamp
  14357. with the current time without prompting the user."
  14358. (interactive "P")
  14359. (let* ((ts nil)
  14360. (default-time
  14361. ;; Default time is either today, or, when entering a range,
  14362. ;; the range start.
  14363. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  14364. (save-excursion
  14365. (re-search-backward
  14366. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  14367. (- (point) 20) t)))
  14368. (apply 'encode-time (org-parse-time-string (match-string 1)))
  14369. (current-time)))
  14370. (default-input (and ts (org-get-compact-tod ts)))
  14371. (repeater (save-excursion
  14372. (save-match-data
  14373. (beginning-of-line)
  14374. (when (re-search-forward
  14375. "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  14376. (save-excursion (progn (end-of-line) (point))) t)
  14377. (match-string 0)))))
  14378. org-time-was-given org-end-time-was-given time)
  14379. (cond
  14380. ((and (org-at-timestamp-p t)
  14381. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  14382. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  14383. (insert "--")
  14384. (setq time (let ((this-command this-command))
  14385. (org-read-date arg 'totime nil nil
  14386. default-time default-input inactive)))
  14387. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  14388. ((org-at-timestamp-p t)
  14389. (setq time (let ((this-command this-command))
  14390. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14391. (when (org-at-timestamp-p t) ; just to get the match data
  14392. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  14393. (replace-match "")
  14394. (setq org-last-changed-timestamp
  14395. (org-insert-time-stamp
  14396. time (or org-time-was-given arg)
  14397. inactive nil nil (list org-end-time-was-given)))
  14398. (when repeater (goto-char (1- (point))) (insert " " repeater)
  14399. (setq org-last-changed-timestamp
  14400. (concat (substring org-last-inserted-timestamp 0 -1)
  14401. " " repeater ">"))))
  14402. (message "Timestamp updated"))
  14403. ((equal arg '(16))
  14404. (org-insert-time-stamp (current-time) t))
  14405. (t
  14406. (setq time (let ((this-command this-command))
  14407. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14408. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  14409. nil nil (list org-end-time-was-given))))))
  14410. ;; FIXME: can we use this for something else, like computing time differences?
  14411. (defun org-get-compact-tod (s)
  14412. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  14413. (let* ((t1 (match-string 1 s))
  14414. (h1 (string-to-number (match-string 2 s)))
  14415. (m1 (string-to-number (match-string 3 s)))
  14416. (t2 (and (match-end 4) (match-string 5 s)))
  14417. (h2 (and t2 (string-to-number (match-string 6 s))))
  14418. (m2 (and t2 (string-to-number (match-string 7 s))))
  14419. dh dm)
  14420. (if (not t2)
  14421. t1
  14422. (setq dh (- h2 h1) dm (- m2 m1))
  14423. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  14424. (concat t1 "+" (number-to-string dh)
  14425. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  14426. (defun org-time-stamp-inactive (&optional arg)
  14427. "Insert an inactive time stamp.
  14428. An inactive time stamp is enclosed in square brackets instead of angle
  14429. brackets. It is inactive in the sense that it does not trigger agenda entries,
  14430. does not link to the calendar and cannot be changed with the S-cursor keys.
  14431. So these are more for recording a certain time/date."
  14432. (interactive "P")
  14433. (org-time-stamp arg 'inactive))
  14434. (defvar org-date-ovl (make-overlay 1 1))
  14435. (overlay-put org-date-ovl 'face 'org-date-selected)
  14436. (org-detach-overlay org-date-ovl)
  14437. (defvar org-ans1) ; dynamically scoped parameter
  14438. (defvar org-ans2) ; dynamically scoped parameter
  14439. (defvar org-plain-time-of-day-regexp) ; defined below
  14440. (defvar org-overriding-default-time nil) ; dynamically scoped
  14441. (defvar org-read-date-overlay nil)
  14442. (defvar org-dcst nil) ; dynamically scoped
  14443. (defvar org-read-date-history nil)
  14444. (defvar org-read-date-final-answer nil)
  14445. (defvar org-read-date-analyze-futurep nil)
  14446. (defvar org-read-date-analyze-forced-year nil)
  14447. (defvar org-read-date-inactive)
  14448. (defvar org-read-date-minibuffer-local-map
  14449. (let ((map (make-sparse-keymap)))
  14450. (set-keymap-parent map minibuffer-local-map)
  14451. (org-defkey map (kbd ".")
  14452. (lambda () (interactive)
  14453. (org-eval-in-calendar '(calendar-goto-today))))
  14454. (org-defkey map [(meta shift left)]
  14455. (lambda () (interactive)
  14456. (org-eval-in-calendar '(calendar-backward-month 1))))
  14457. (org-defkey map [(meta shift right)]
  14458. (lambda () (interactive)
  14459. (org-eval-in-calendar '(calendar-forward-month 1))))
  14460. (org-defkey map [(meta shift up)]
  14461. (lambda () (interactive)
  14462. (org-eval-in-calendar '(calendar-backward-year 1))))
  14463. (org-defkey map [(meta shift down)]
  14464. (lambda () (interactive)
  14465. (org-eval-in-calendar '(calendar-forward-year 1))))
  14466. (org-defkey map [?\e (shift left)]
  14467. (lambda () (interactive)
  14468. (org-eval-in-calendar '(calendar-backward-month 1))))
  14469. (org-defkey map [?\e (shift right)]
  14470. (lambda () (interactive)
  14471. (org-eval-in-calendar '(calendar-forward-month 1))))
  14472. (org-defkey map [?\e (shift up)]
  14473. (lambda () (interactive)
  14474. (org-eval-in-calendar '(calendar-backward-year 1))))
  14475. (org-defkey map [?\e (shift down)]
  14476. (lambda () (interactive)
  14477. (org-eval-in-calendar '(calendar-forward-year 1))))
  14478. (org-defkey map [(shift up)]
  14479. (lambda () (interactive)
  14480. (org-eval-in-calendar '(calendar-backward-week 1))))
  14481. (org-defkey map [(shift down)]
  14482. (lambda () (interactive)
  14483. (org-eval-in-calendar '(calendar-forward-week 1))))
  14484. (org-defkey map [(shift left)]
  14485. (lambda () (interactive)
  14486. (org-eval-in-calendar '(calendar-backward-day 1))))
  14487. (org-defkey map [(shift right)]
  14488. (lambda () (interactive)
  14489. (org-eval-in-calendar '(calendar-forward-day 1))))
  14490. (org-defkey map "!"
  14491. (lambda () (interactive)
  14492. (org-eval-in-calendar '(diary-view-entries))
  14493. (message "")))
  14494. (org-defkey map ">"
  14495. (lambda () (interactive)
  14496. (org-eval-in-calendar '(scroll-calendar-left 1))))
  14497. (org-defkey map "<"
  14498. (lambda () (interactive)
  14499. (org-eval-in-calendar '(scroll-calendar-right 1))))
  14500. (org-defkey map "\C-v"
  14501. (lambda () (interactive)
  14502. (org-eval-in-calendar
  14503. '(calendar-scroll-left-three-months 1))))
  14504. (org-defkey map "\M-v"
  14505. (lambda () (interactive)
  14506. (org-eval-in-calendar
  14507. '(calendar-scroll-right-three-months 1))))
  14508. map)
  14509. "Keymap for minibuffer commands when using `org-read-date'.")
  14510. (defun org-read-date (&optional org-with-time to-time from-string prompt
  14511. default-time default-input inactive)
  14512. "Read a date, possibly a time, and make things smooth for the user.
  14513. The prompt will suggest to enter an ISO date, but you can also enter anything
  14514. which will at least partially be understood by `parse-time-string'.
  14515. Unrecognized parts of the date will default to the current day, month, year,
  14516. hour and minute. If this command is called to replace a timestamp at point,
  14517. or to enter the second timestamp of a range, the default time is taken
  14518. from the existing stamp. Furthermore, the command prefers the future,
  14519. so if you are giving a date where the year is not given, and the day-month
  14520. combination is already past in the current year, it will assume you
  14521. mean next year. For details, see the manual. A few examples:
  14522. 3-2-5 --> 2003-02-05
  14523. feb 15 --> currentyear-02-15
  14524. 2/15 --> currentyear-02-15
  14525. sep 12 9 --> 2009-09-12
  14526. 12:45 --> today 12:45
  14527. 22 sept 0:34 --> currentyear-09-22 0:34
  14528. 12 --> currentyear-currentmonth-12
  14529. Fri --> nearest Friday (today or later)
  14530. etc.
  14531. Furthermore you can specify a relative date by giving, as the *first* thing
  14532. in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
  14533. change in days weeks, months, years.
  14534. With a single plus or minus, the date is relative to today. With a double
  14535. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  14536. +4d --> four days from today
  14537. +4 --> same as above
  14538. +2w --> two weeks from today
  14539. ++5 --> five days from default date
  14540. The function understands only English month and weekday abbreviations.
  14541. While prompting, a calendar is popped up - you can also select the
  14542. date with the mouse (button 1). The calendar shows a period of three
  14543. months. To scroll it to other months, use the keys `>' and `<'.
  14544. If you don't like the calendar, turn it off with
  14545. \(setq org-read-date-popup-calendar nil)
  14546. With optional argument TO-TIME, the date will immediately be converted
  14547. to an internal time.
  14548. With an optional argument ORG-WITH-TIME, the prompt will suggest to
  14549. also insert a time. Note that when ORG-WITH-TIME is not set, you can
  14550. still enter a time, and this function will inform the calling routine
  14551. about this change. The calling routine may then choose to change the
  14552. format used to insert the time stamp into the buffer to include the time.
  14553. With optional argument FROM-STRING, read from this string instead from
  14554. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  14555. the time/date that is used for everything that is not specified by the
  14556. user."
  14557. (require 'parse-time)
  14558. (let* ((org-time-stamp-rounding-minutes
  14559. (if (equal org-with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  14560. (org-dcst org-display-custom-times)
  14561. (ct (org-current-time))
  14562. (org-def (or org-overriding-default-time default-time ct))
  14563. (org-defdecode (decode-time org-def))
  14564. (dummy (progn
  14565. (when (< (nth 2 org-defdecode) org-extend-today-until)
  14566. (setcar (nthcdr 2 org-defdecode) -1)
  14567. (setcar (nthcdr 1 org-defdecode) 59)
  14568. (setq org-def (apply 'encode-time org-defdecode)
  14569. org-defdecode (decode-time org-def)))))
  14570. (mouse-autoselect-window nil) ; Don't let the mouse jump
  14571. (calendar-frame-setup nil)
  14572. (calendar-setup nil)
  14573. (calendar-move-hook nil)
  14574. (calendar-view-diary-initially-flag nil)
  14575. (calendar-view-holidays-initially-flag nil)
  14576. (timestr (format-time-string
  14577. (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") org-def))
  14578. (prompt (concat (if prompt (concat prompt " ") "")
  14579. (format "Date+time [%s]: " timestr)))
  14580. ans (org-ans0 "") org-ans1 org-ans2 final)
  14581. (cond
  14582. (from-string (setq ans from-string))
  14583. (org-read-date-popup-calendar
  14584. (save-excursion
  14585. (save-window-excursion
  14586. (calendar)
  14587. (org-eval-in-calendar '(setq cursor-type nil) t)
  14588. (unwind-protect
  14589. (progn
  14590. (calendar-forward-day (- (time-to-days org-def)
  14591. (calendar-absolute-from-gregorian
  14592. (calendar-current-date))))
  14593. (org-eval-in-calendar nil t)
  14594. (let* ((old-map (current-local-map))
  14595. (map (copy-keymap calendar-mode-map))
  14596. (minibuffer-local-map
  14597. (copy-keymap org-read-date-minibuffer-local-map)))
  14598. (org-defkey map (kbd "RET") 'org-calendar-select)
  14599. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  14600. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  14601. (unwind-protect
  14602. (progn
  14603. (use-local-map map)
  14604. (setq org-read-date-inactive inactive)
  14605. (add-hook 'post-command-hook 'org-read-date-display)
  14606. (setq org-ans0 (read-string prompt default-input
  14607. 'org-read-date-history nil))
  14608. ;; org-ans0: from prompt
  14609. ;; org-ans1: from mouse click
  14610. ;; org-ans2: from calendar motion
  14611. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  14612. (remove-hook 'post-command-hook 'org-read-date-display)
  14613. (use-local-map old-map)
  14614. (when org-read-date-overlay
  14615. (delete-overlay org-read-date-overlay)
  14616. (setq org-read-date-overlay nil)))))
  14617. (bury-buffer "*Calendar*")))))
  14618. (t ; Naked prompt only
  14619. (unwind-protect
  14620. (setq ans (read-string prompt default-input
  14621. 'org-read-date-history timestr))
  14622. (when org-read-date-overlay
  14623. (delete-overlay org-read-date-overlay)
  14624. (setq org-read-date-overlay nil)))))
  14625. (setq final (org-read-date-analyze ans org-def org-defdecode))
  14626. (when org-read-date-analyze-forced-year
  14627. (message "Year was forced into %s"
  14628. (if org-read-date-force-compatible-dates
  14629. "compatible range (1970-2037)"
  14630. "range representable on this machine"))
  14631. (ding))
  14632. ;; One round trip to get rid of 34th of August and stuff like that....
  14633. (setq final (decode-time (apply 'encode-time final)))
  14634. (setq org-read-date-final-answer ans)
  14635. (if to-time
  14636. (apply 'encode-time final)
  14637. (if (and (boundp 'org-time-was-given) org-time-was-given)
  14638. (format "%04d-%02d-%02d %02d:%02d"
  14639. (nth 5 final) (nth 4 final) (nth 3 final)
  14640. (nth 2 final) (nth 1 final))
  14641. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  14642. (defvar org-def)
  14643. (defvar org-defdecode)
  14644. (defvar org-with-time)
  14645. (defun org-read-date-display ()
  14646. "Display the current date prompt interpretation in the minibuffer."
  14647. (when org-read-date-display-live
  14648. (when org-read-date-overlay
  14649. (delete-overlay org-read-date-overlay))
  14650. (when (minibufferp (current-buffer))
  14651. (save-excursion
  14652. (end-of-line 1)
  14653. (while (not (equal (buffer-substring
  14654. (max (point-min) (- (point) 4)) (point))
  14655. " "))
  14656. (insert " ")))
  14657. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  14658. " " (or org-ans1 org-ans2)))
  14659. (org-end-time-was-given nil)
  14660. (f (org-read-date-analyze ans org-def org-defdecode))
  14661. (fmts (if org-dcst
  14662. org-time-stamp-custom-formats
  14663. org-time-stamp-formats))
  14664. (fmt (if (or org-with-time
  14665. (and (boundp 'org-time-was-given) org-time-was-given))
  14666. (cdr fmts)
  14667. (car fmts)))
  14668. (txt (format-time-string fmt (apply 'encode-time f)))
  14669. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  14670. (txt (concat "=> " txt)))
  14671. (when (and org-end-time-was-given
  14672. (string-match org-plain-time-of-day-regexp txt))
  14673. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  14674. org-end-time-was-given
  14675. (substring txt (match-end 0)))))
  14676. (when org-read-date-analyze-futurep
  14677. (setq txt (concat txt " (=>F)")))
  14678. (setq org-read-date-overlay
  14679. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  14680. (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
  14681. (defun org-read-date-analyze (ans org-def org-defdecode)
  14682. "Analyze the combined answer of the date prompt."
  14683. ;; FIXME: cleanup and comment
  14684. (let ((nowdecode (decode-time (current-time)))
  14685. delta deltan deltaw deltadef year month day
  14686. hour minute second wday pm h2 m2 tl wday1
  14687. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  14688. (setq org-read-date-analyze-futurep nil
  14689. org-read-date-analyze-forced-year nil)
  14690. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  14691. (setq ans "+0"))
  14692. (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
  14693. (setq ans (replace-match "" t t ans)
  14694. deltan (car delta)
  14695. deltaw (nth 1 delta)
  14696. deltadef (nth 2 delta)))
  14697. ;; Check if there is an iso week date in there. If yes, store the
  14698. ;; info and postpone interpreting it until the rest of the parsing
  14699. ;; is done.
  14700. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  14701. (setq iso-year (if (match-end 1)
  14702. (org-small-year-to-year
  14703. (string-to-number (match-string 1 ans))))
  14704. iso-weekday (if (match-end 3)
  14705. (string-to-number (match-string 3 ans)))
  14706. iso-week (string-to-number (match-string 2 ans)))
  14707. (setq ans (replace-match "" t t ans)))
  14708. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  14709. (when (string-match
  14710. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  14711. (setq year (if (match-end 2)
  14712. (string-to-number (match-string 2 ans))
  14713. (progn (setq kill-year t)
  14714. (string-to-number (format-time-string "%Y"))))
  14715. month (string-to-number (match-string 3 ans))
  14716. day (string-to-number (match-string 4 ans)))
  14717. (if (< year 100) (setq year (+ 2000 year)))
  14718. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  14719. t nil ans)))
  14720. ;; Help matching dotted european dates
  14721. (when (string-match
  14722. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\.\\( ?[1-9][0-9]\\{3\\}\\)?" ans)
  14723. (setq year (if (match-end 3) (string-to-number (match-string 3 ans))
  14724. (setq kill-year t)
  14725. (string-to-number (format-time-string "%Y")))
  14726. day (string-to-number (match-string 1 ans))
  14727. month (string-to-number (match-string 2 ans))
  14728. ans (replace-match (format "%04d-%02d-%02d" year month day)
  14729. t nil ans)))
  14730. ;; Help matching american dates, like 5/30 or 5/30/7
  14731. (when (string-match
  14732. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  14733. (setq year (if (match-end 4)
  14734. (string-to-number (match-string 4 ans))
  14735. (progn (setq kill-year t)
  14736. (string-to-number (format-time-string "%Y"))))
  14737. month (string-to-number (match-string 1 ans))
  14738. day (string-to-number (match-string 2 ans)))
  14739. (if (< year 100) (setq year (+ 2000 year)))
  14740. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  14741. t nil ans)))
  14742. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  14743. ;; If there is a time with am/pm, and *no* time without it, we convert
  14744. ;; so that matching will be successful.
  14745. (loop for i from 1 to 2 do ; twice, for end time as well
  14746. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  14747. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  14748. (setq hour (string-to-number (match-string 1 ans))
  14749. minute (if (match-end 3)
  14750. (string-to-number (match-string 3 ans))
  14751. 0)
  14752. pm (equal ?p
  14753. (string-to-char (downcase (match-string 4 ans)))))
  14754. (if (and (= hour 12) (not pm))
  14755. (setq hour 0)
  14756. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  14757. (setq ans (replace-match (format "%02d:%02d" hour minute)
  14758. t t ans))))
  14759. ;; Check if a time range is given as a duration
  14760. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  14761. (setq hour (string-to-number (match-string 1 ans))
  14762. h2 (+ hour (string-to-number (match-string 3 ans)))
  14763. minute (string-to-number (match-string 2 ans))
  14764. m2 (+ minute (if (match-end 5) (string-to-number
  14765. (match-string 5 ans))0)))
  14766. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  14767. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  14768. t t ans)))
  14769. ;; Check if there is a time range
  14770. (when (boundp 'org-end-time-was-given)
  14771. (setq org-time-was-given nil)
  14772. (when (and (string-match org-plain-time-of-day-regexp ans)
  14773. (match-end 8))
  14774. (setq org-end-time-was-given (match-string 8 ans))
  14775. (setq ans (concat (substring ans 0 (match-beginning 7))
  14776. (substring ans (match-end 7))))))
  14777. (setq tl (parse-time-string ans)
  14778. day (or (nth 3 tl) (nth 3 org-defdecode))
  14779. month (or (nth 4 tl)
  14780. (if (and org-read-date-prefer-future
  14781. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  14782. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  14783. (nth 4 org-defdecode)))
  14784. year (or (and (not kill-year) (nth 5 tl))
  14785. (if (and org-read-date-prefer-future
  14786. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  14787. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  14788. (nth 5 org-defdecode)))
  14789. hour (or (nth 2 tl) (nth 2 org-defdecode))
  14790. minute (or (nth 1 tl) (nth 1 org-defdecode))
  14791. second (or (nth 0 tl) 0)
  14792. wday (nth 6 tl))
  14793. (when (and (eq org-read-date-prefer-future 'time)
  14794. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  14795. (equal day (nth 3 nowdecode))
  14796. (equal month (nth 4 nowdecode))
  14797. (equal year (nth 5 nowdecode))
  14798. (nth 2 tl)
  14799. (or (< (nth 2 tl) (nth 2 nowdecode))
  14800. (and (= (nth 2 tl) (nth 2 nowdecode))
  14801. (nth 1 tl)
  14802. (< (nth 1 tl) (nth 1 nowdecode)))))
  14803. (setq day (1+ day)
  14804. futurep t))
  14805. ;; Special date definitions below
  14806. (cond
  14807. (iso-week
  14808. ;; There was an iso week
  14809. (require 'cal-iso)
  14810. (setq futurep nil)
  14811. (setq year (or iso-year year)
  14812. day (or iso-weekday wday 1)
  14813. wday nil ; to make sure that the trigger below does not match
  14814. iso-date (calendar-gregorian-from-absolute
  14815. (calendar-absolute-from-iso
  14816. (list iso-week day year))))
  14817. ; FIXME: Should we also push ISO weeks into the future?
  14818. ; (when (and org-read-date-prefer-future
  14819. ; (not iso-year)
  14820. ; (< (calendar-absolute-from-gregorian iso-date)
  14821. ; (time-to-days (current-time))))
  14822. ; (setq year (1+ year)
  14823. ; iso-date (calendar-gregorian-from-absolute
  14824. ; (calendar-absolute-from-iso
  14825. ; (list iso-week day year)))))
  14826. (setq month (car iso-date)
  14827. year (nth 2 iso-date)
  14828. day (nth 1 iso-date)))
  14829. (deltan
  14830. (setq futurep nil)
  14831. (unless deltadef
  14832. (let ((now (decode-time (current-time))))
  14833. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  14834. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  14835. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  14836. ((equal deltaw "m") (setq month (+ month deltan)))
  14837. ((equal deltaw "y") (setq year (+ year deltan)))))
  14838. ((and wday (not (nth 3 tl)))
  14839. ;; Weekday was given, but no day, so pick that day in the week
  14840. ;; on or after the derived date.
  14841. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  14842. (unless (equal wday wday1)
  14843. (setq day (+ day (% (- wday wday1 -7) 7))))))
  14844. (if (and (boundp 'org-time-was-given)
  14845. (nth 2 tl))
  14846. (setq org-time-was-given t))
  14847. (if (< year 100) (setq year (+ 2000 year)))
  14848. ;; Check of the date is representable
  14849. (if org-read-date-force-compatible-dates
  14850. (progn
  14851. (if (< year 1970)
  14852. (setq year 1970 org-read-date-analyze-forced-year t))
  14853. (if (> year 2037)
  14854. (setq year 2037 org-read-date-analyze-forced-year t)))
  14855. (condition-case nil
  14856. (ignore (encode-time second minute hour day month year))
  14857. (error
  14858. (setq year (nth 5 org-defdecode))
  14859. (setq org-read-date-analyze-forced-year t))))
  14860. (setq org-read-date-analyze-futurep futurep)
  14861. (list second minute hour day month year)))
  14862. (defvar parse-time-weekdays)
  14863. (defun org-read-date-get-relative (s today default)
  14864. "Check string S for special relative date string.
  14865. TODAY and DEFAULT are internal times, for today and for a default.
  14866. Return shift list (N what def-flag)
  14867. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  14868. N is the number of WHATs to shift.
  14869. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  14870. the DEFAULT date rather than TODAY."
  14871. (require 'parse-time)
  14872. (when (and
  14873. (string-match
  14874. (concat
  14875. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  14876. "\\([0-9]+\\)?"
  14877. "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  14878. "\\([ \t]\\|$\\)") s)
  14879. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  14880. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  14881. (string-to-char (substring (match-string 1 s) -1))
  14882. ?+))
  14883. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  14884. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  14885. (what (if (match-end 3) (match-string 3 s) "d"))
  14886. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  14887. (date (if rel default today))
  14888. (wday (nth 6 (decode-time date)))
  14889. delta)
  14890. (if wday1
  14891. (progn
  14892. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  14893. (if (= dir ?-) (setq delta (- delta 7)))
  14894. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  14895. (list delta "d" rel))
  14896. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  14897. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  14898. "Turn a user-specified date into the internal representation.
  14899. The internal representation needed by the calendar is (month day year).
  14900. This is a wrapper to handle the brain-dead convention in calendar that
  14901. user function argument order change dependent on argument order."
  14902. (if (boundp 'calendar-date-style)
  14903. (cond
  14904. ((eq calendar-date-style 'american)
  14905. (list arg1 arg2 arg3))
  14906. ((eq calendar-date-style 'european)
  14907. (list arg2 arg1 arg3))
  14908. ((eq calendar-date-style 'iso)
  14909. (list arg2 arg3 arg1)))
  14910. (org-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  14911. (if (org-bound-and-true-p european-calendar-style)
  14912. (list arg2 arg1 arg3)
  14913. (list arg1 arg2 arg3)))))
  14914. (defun org-eval-in-calendar (form &optional keepdate)
  14915. "Eval FORM in the calendar window and return to current window.
  14916. When KEEPDATE is non-nil, update `org-ans2' from the cursor date,
  14917. otherwise stick to the current value of `org-ans2'."
  14918. (let ((sf (selected-frame))
  14919. (sw (selected-window)))
  14920. (select-window (get-buffer-window "*Calendar*" t))
  14921. (eval form)
  14922. (when (and (not keepdate) (calendar-cursor-to-date))
  14923. (let* ((date (calendar-cursor-to-date))
  14924. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14925. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  14926. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  14927. (select-window sw)
  14928. (org-select-frame-set-input-focus sf)))
  14929. (defun org-calendar-select ()
  14930. "Return to `org-read-date' with the date currently selected.
  14931. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  14932. (interactive)
  14933. (when (calendar-cursor-to-date)
  14934. (let* ((date (calendar-cursor-to-date))
  14935. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14936. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  14937. (if (active-minibuffer-window) (exit-minibuffer))))
  14938. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  14939. "Insert a date stamp for the date given by the internal TIME.
  14940. WITH-HM means use the stamp format that includes the time of the day.
  14941. INACTIVE means use square brackets instead of angular ones, so that the
  14942. stamp will not contribute to the agenda.
  14943. PRE and POST are optional strings to be inserted before and after the
  14944. stamp.
  14945. The command returns the inserted time stamp."
  14946. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  14947. stamp)
  14948. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  14949. (insert-before-markers (or pre ""))
  14950. (when (listp extra)
  14951. (setq extra (car extra))
  14952. (if (and (stringp extra)
  14953. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  14954. (setq extra (format "-%02d:%02d"
  14955. (string-to-number (match-string 1 extra))
  14956. (string-to-number (match-string 2 extra))))
  14957. (setq extra nil)))
  14958. (when extra
  14959. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  14960. (insert-before-markers (setq stamp (format-time-string fmt time)))
  14961. (insert-before-markers (or post ""))
  14962. (setq org-last-inserted-timestamp stamp)))
  14963. (defun org-toggle-time-stamp-overlays ()
  14964. "Toggle the use of custom time stamp formats."
  14965. (interactive)
  14966. (setq org-display-custom-times (not org-display-custom-times))
  14967. (unless org-display-custom-times
  14968. (let ((p (point-min)) (bmp (buffer-modified-p)))
  14969. (while (setq p (next-single-property-change p 'display))
  14970. (if (and (get-text-property p 'display)
  14971. (eq (get-text-property p 'face) 'org-date))
  14972. (remove-text-properties
  14973. p (setq p (next-single-property-change p 'display))
  14974. '(display t))))
  14975. (set-buffer-modified-p bmp)))
  14976. (if (featurep 'xemacs)
  14977. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  14978. (org-restart-font-lock)
  14979. (setq org-table-may-need-update t)
  14980. (if org-display-custom-times
  14981. (message "Time stamps are overlaid with custom format")
  14982. (message "Time stamp overlays removed")))
  14983. (defun org-display-custom-time (beg end)
  14984. "Overlay modified time stamp format over timestamp between BEG and END."
  14985. (let* ((ts (buffer-substring beg end))
  14986. t1 w1 with-hm tf time str w2 (off 0))
  14987. (save-match-data
  14988. (setq t1 (org-parse-time-string ts t))
  14989. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
  14990. (setq off (- (match-end 0) (match-beginning 0)))))
  14991. (setq end (- end off))
  14992. (setq w1 (- end beg)
  14993. with-hm (and (nth 1 t1) (nth 2 t1))
  14994. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  14995. time (org-fix-decoded-time t1)
  14996. str (org-add-props
  14997. (format-time-string
  14998. (substring tf 1 -1) (apply 'encode-time time))
  14999. nil 'mouse-face 'highlight)
  15000. w2 (length str))
  15001. (if (not (= w2 w1))
  15002. (add-text-properties (1+ beg) (+ 2 beg)
  15003. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  15004. (if (featurep 'xemacs)
  15005. (progn
  15006. (put-text-property beg end 'invisible t)
  15007. (put-text-property beg end 'end-glyph (make-glyph str)))
  15008. (put-text-property beg end 'display str))))
  15009. (defun org-translate-time (string)
  15010. "Translate all timestamps in STRING to custom format.
  15011. But do this only if the variable `org-display-custom-times' is set."
  15012. (when org-display-custom-times
  15013. (save-match-data
  15014. (let* ((start 0)
  15015. (re org-ts-regexp-both)
  15016. t1 with-hm inactive tf time str beg end)
  15017. (while (setq start (string-match re string start))
  15018. (setq beg (match-beginning 0)
  15019. end (match-end 0)
  15020. t1 (save-match-data
  15021. (org-parse-time-string (substring string beg end) t))
  15022. with-hm (and (nth 1 t1) (nth 2 t1))
  15023. inactive (equal (substring string beg (1+ beg)) "[")
  15024. tf (funcall (if with-hm 'cdr 'car)
  15025. org-time-stamp-custom-formats)
  15026. time (org-fix-decoded-time t1)
  15027. str (format-time-string
  15028. (concat
  15029. (if inactive "[" "<") (substring tf 1 -1)
  15030. (if inactive "]" ">"))
  15031. (apply 'encode-time time))
  15032. string (replace-match str t t string)
  15033. start (+ start (length str)))))))
  15034. string)
  15035. (defun org-fix-decoded-time (time)
  15036. "Set 0 instead of nil for the first 6 elements of time.
  15037. Don't touch the rest."
  15038. (let ((n 0))
  15039. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  15040. (define-obsolete-function-alias 'org-days-to-time 'org-time-stamp-to-now "24.4")
  15041. (defun org-time-stamp-to-now (timestamp-string &optional seconds)
  15042. "Difference between TIMESTAMP-STRING and now in days.
  15043. If SECONDS is non-nil, return the difference in seconds."
  15044. (let ((fdiff (if seconds 'org-float-time 'time-to-days)))
  15045. (- (funcall fdiff (org-time-string-to-time timestamp-string))
  15046. (funcall fdiff (current-time)))))
  15047. (defun org-deadline-close (timestamp-string &optional ndays)
  15048. "Is the time in TIMESTAMP-STRING close to the current date?"
  15049. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  15050. (and (< (org-time-stamp-to-now timestamp-string) ndays)
  15051. (not (org-entry-is-done-p))))
  15052. (defun org-get-wdays (ts &optional delay zero-delay)
  15053. "Get the deadline lead time appropriate for timestring TS.
  15054. When DELAY is non-nil, get the delay time for scheduled items
  15055. instead of the deadline lead time. When ZERO-DELAY is non-nil
  15056. and `org-scheduled-delay-days' is 0, enforce 0 as the delay,
  15057. don't try to find the delay cookie in the scheduled timestamp."
  15058. (let ((tv (if delay org-scheduled-delay-days
  15059. org-deadline-warning-days)))
  15060. (cond
  15061. ((or (and delay (< tv 0))
  15062. (and delay zero-delay (<= tv 0))
  15063. (and (not delay) (<= tv 0)))
  15064. ;; Enforce this value no matter what
  15065. (- tv))
  15066. ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
  15067. ;; lead time is specified.
  15068. (floor (* (string-to-number (match-string 1 ts))
  15069. (cdr (assoc (match-string 2 ts)
  15070. '(("d" . 1) ("w" . 7)
  15071. ("m" . 30.4) ("y" . 365.25)
  15072. ("h" . 0.041667)))))))
  15073. ;; go for the default.
  15074. (t tv))))
  15075. (defun org-calendar-select-mouse (ev)
  15076. "Return to `org-read-date' with the date currently selected.
  15077. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15078. (interactive "e")
  15079. (mouse-set-point ev)
  15080. (when (calendar-cursor-to-date)
  15081. (let* ((date (calendar-cursor-to-date))
  15082. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15083. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15084. (if (active-minibuffer-window) (exit-minibuffer))))
  15085. (defun org-check-deadlines (ndays)
  15086. "Check if there are any deadlines due or past due.
  15087. A deadline is considered due if it happens within `org-deadline-warning-days'
  15088. days from today's date. If the deadline appears in an entry marked DONE,
  15089. it is not shown. The prefix arg NDAYS can be used to test that many
  15090. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  15091. (interactive "P")
  15092. (let* ((org-warn-days
  15093. (cond
  15094. ((equal ndays '(4)) 100000)
  15095. (ndays (prefix-numeric-value ndays))
  15096. (t (abs org-deadline-warning-days))))
  15097. (case-fold-search nil)
  15098. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15099. (callback
  15100. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  15101. (message "%d deadlines past-due or due within %d days"
  15102. (org-occur regexp nil callback)
  15103. org-warn-days)))
  15104. (defsubst org-re-timestamp (type)
  15105. "Return a regexp for timestamp TYPE.
  15106. Allowed values for TYPE are:
  15107. all: all timestamps
  15108. active: only active timestamps (<...>)
  15109. inactive: only inactive timestamps ([...])
  15110. scheduled: only scheduled timestamps
  15111. deadline: only deadline timestamps
  15112. When TYPE is nil, fall back on returning a regexp that matches
  15113. both scheduled and deadline timestamps."
  15114. (cond ((eq type 'all) "\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}\\(?: +[^]+0-9> \n -]+\\)?\\(?: +[0-9]\\{1,2\\}:[0-9]\\{2\\}\\)?\\)")
  15115. ((eq type 'active) org-ts-regexp)
  15116. ((eq type 'inactive) "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^ \n>]*?\\)\\]")
  15117. ((eq type 'scheduled) (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>"))
  15118. ((eq type 'deadline) (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15119. ((eq type 'scheduled-or-deadline)
  15120. (concat "\\<\\(?:" org-deadline-string "\\|" org-scheduled-string "\\) *<\\([^>]+\\)>"))))
  15121. (defun org-check-before-date (date)
  15122. "Check if there are deadlines or scheduled entries before DATE."
  15123. (interactive (list (org-read-date)))
  15124. (let ((case-fold-search nil)
  15125. (regexp (org-re-timestamp org-ts-type))
  15126. (callback
  15127. (lambda () (time-less-p
  15128. (org-time-string-to-time (match-string 1))
  15129. (org-time-string-to-time date)))))
  15130. (message "%d entries before %s"
  15131. (org-occur regexp nil callback) date)))
  15132. (defun org-check-after-date (date)
  15133. "Check if there are deadlines or scheduled entries after DATE."
  15134. (interactive (list (org-read-date)))
  15135. (let ((case-fold-search nil)
  15136. (regexp (org-re-timestamp org-ts-type))
  15137. (callback
  15138. (lambda () (not
  15139. (time-less-p
  15140. (org-time-string-to-time (match-string 1))
  15141. (org-time-string-to-time date))))))
  15142. (message "%d entries after %s"
  15143. (org-occur regexp nil callback) date)))
  15144. (defun org-check-dates-range (start-date end-date)
  15145. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  15146. (interactive (list (org-read-date nil nil nil "Range starts")
  15147. (org-read-date nil nil nil "Range end")))
  15148. (let ((case-fold-search nil)
  15149. (regexp (org-re-timestamp org-ts-type))
  15150. (callback
  15151. (lambda ()
  15152. (let ((match (match-string 1)))
  15153. (and
  15154. (not (time-less-p
  15155. (org-time-string-to-time match)
  15156. (org-time-string-to-time start-date)))
  15157. (time-less-p
  15158. (org-time-string-to-time match)
  15159. (org-time-string-to-time end-date)))))))
  15160. (message "%d entries between %s and %s"
  15161. (org-occur regexp nil callback) start-date end-date)))
  15162. (defun org-evaluate-time-range (&optional to-buffer)
  15163. "Evaluate a time range by computing the difference between start and end.
  15164. Normally the result is just printed in the echo area, but with prefix arg
  15165. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  15166. If the time range is actually in a table, the result is inserted into the
  15167. next column.
  15168. For time difference computation, a year is assumed to be exactly 365
  15169. days in order to avoid rounding problems."
  15170. (interactive "P")
  15171. (or
  15172. (org-clock-update-time-maybe)
  15173. (save-excursion
  15174. (unless (org-at-date-range-p t)
  15175. (goto-char (point-at-bol))
  15176. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  15177. (if (not (org-at-date-range-p t))
  15178. (error "Not at a time-stamp range, and none found in current line")))
  15179. (let* ((ts1 (match-string 1))
  15180. (ts2 (match-string 2))
  15181. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  15182. (match-end (match-end 0))
  15183. (time1 (org-time-string-to-time ts1))
  15184. (time2 (org-time-string-to-time ts2))
  15185. (t1 (org-float-time time1))
  15186. (t2 (org-float-time time2))
  15187. (diff (abs (- t2 t1)))
  15188. (negative (< (- t2 t1) 0))
  15189. ;; (ys (floor (* 365 24 60 60)))
  15190. (ds (* 24 60 60))
  15191. (hs (* 60 60))
  15192. (fy "%dy %dd %02d:%02d")
  15193. (fy1 "%dy %dd")
  15194. (fd "%dd %02d:%02d")
  15195. (fd1 "%dd")
  15196. (fh "%02d:%02d")
  15197. y d h m align)
  15198. (if havetime
  15199. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15200. y 0
  15201. d (floor (/ diff ds)) diff (mod diff ds)
  15202. h (floor (/ diff hs)) diff (mod diff hs)
  15203. m (floor (/ diff 60)))
  15204. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15205. y 0
  15206. d (floor (+ (/ diff ds) 0.5))
  15207. h 0 m 0))
  15208. (if (not to-buffer)
  15209. (message "%s" (org-make-tdiff-string y d h m))
  15210. (if (org-at-table-p)
  15211. (progn
  15212. (goto-char match-end)
  15213. (setq align t)
  15214. (and (looking-at " *|") (goto-char (match-end 0))))
  15215. (goto-char match-end))
  15216. (if (looking-at
  15217. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  15218. (replace-match ""))
  15219. (if negative (insert " -"))
  15220. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  15221. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  15222. (insert " " (format fh h m))))
  15223. (if align (org-table-align))
  15224. (message "Time difference inserted")))))
  15225. (defun org-make-tdiff-string (y d h m)
  15226. (let ((fmt "")
  15227. (l nil))
  15228. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  15229. l (push y l)))
  15230. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  15231. l (push d l)))
  15232. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  15233. l (push h l)))
  15234. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  15235. l (push m l)))
  15236. (apply 'format fmt (nreverse l))))
  15237. (defun org-time-string-to-time (s &optional buffer pos)
  15238. "Convert a timestamp string into internal time."
  15239. (condition-case errdata
  15240. (apply 'encode-time (org-parse-time-string s))
  15241. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15242. s (if (not (and buffer pos))
  15243. ""
  15244. (format " at %d in buffer `%s'" pos buffer))
  15245. (cdr errdata)))))
  15246. (defun org-time-string-to-seconds (s)
  15247. "Convert a timestamp string to a number of seconds."
  15248. (org-float-time (org-time-string-to-time s)))
  15249. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  15250. "Convert a time stamp to an absolute day number.
  15251. If there is a specifier for a cyclic time stamp, get the closest
  15252. date to DAYNR.
  15253. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  15254. The variable date is bound by the calendar when this is called."
  15255. (cond
  15256. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  15257. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  15258. daynr
  15259. (+ daynr 1000)))
  15260. ((and daynr (string-match "\\+[0-9]+[hdwmy]" s))
  15261. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  15262. (time-to-days (current-time))) (match-string 0 s)
  15263. prefer show-all))
  15264. (t (time-to-days
  15265. (condition-case errdata
  15266. (apply 'encode-time (org-parse-time-string s))
  15267. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15268. s (if (not (and buffer pos))
  15269. ""
  15270. (format " at %d in buffer `%s'" pos buffer))
  15271. (cdr errdata))))))))
  15272. (defun org-days-to-iso-week (days)
  15273. "Return the iso week number."
  15274. (require 'cal-iso)
  15275. (car (calendar-iso-from-absolute days)))
  15276. (defun org-small-year-to-year (year)
  15277. "Convert 2-digit years into 4-digit years.
  15278. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  15279. The year 2000 cannot be abbreviated. Any year larger than 99
  15280. is returned unchanged."
  15281. (if (< year 38)
  15282. (setq year (+ 2000 year))
  15283. (if (< year 100)
  15284. (setq year (+ 1900 year))))
  15285. year)
  15286. (defun org-time-from-absolute (d)
  15287. "Return the time corresponding to date D.
  15288. D may be an absolute day number, or a calendar-type list (month day year)."
  15289. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  15290. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  15291. (defun org-calendar-holiday ()
  15292. "List of holidays, for Diary display in Org-mode."
  15293. (require 'holidays)
  15294. (let ((hl (funcall
  15295. (if (fboundp 'calendar-check-holidays)
  15296. 'calendar-check-holidays 'check-calendar-holidays) date)))
  15297. (if hl (mapconcat 'identity hl "; "))))
  15298. (defun org-diary-sexp-entry (sexp entry date)
  15299. "Process a SEXP diary ENTRY for DATE."
  15300. (require 'diary-lib)
  15301. (let ((result (if calendar-debug-sexp
  15302. (let ((stack-trace-on-error t))
  15303. (eval (car (read-from-string sexp))))
  15304. (condition-case nil
  15305. (eval (car (read-from-string sexp)))
  15306. (error
  15307. (beep)
  15308. (message "Bad sexp at line %d in %s: %s"
  15309. (org-current-line)
  15310. (buffer-file-name) sexp)
  15311. (sleep-for 2))))))
  15312. (cond ((stringp result) (split-string result "; "))
  15313. ((and (consp result)
  15314. (not (consp (cdr result)))
  15315. (stringp (cdr result))) (cdr result))
  15316. ((and (consp result)
  15317. (stringp (car result))) result)
  15318. (result entry))))
  15319. (defun org-diary-to-ical-string (frombuf)
  15320. "Get iCalendar entries from diary entries in buffer FROMBUF.
  15321. This uses the icalendar.el library."
  15322. (let* ((tmpdir (if (featurep 'xemacs)
  15323. (temp-directory)
  15324. temporary-file-directory))
  15325. (tmpfile (make-temp-name
  15326. (expand-file-name "orgics" tmpdir)))
  15327. buf rtn b e)
  15328. (with-current-buffer frombuf
  15329. (icalendar-export-region (point-min) (point-max) tmpfile)
  15330. (setq buf (find-buffer-visiting tmpfile))
  15331. (set-buffer buf)
  15332. (goto-char (point-min))
  15333. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  15334. (setq b (match-beginning 0)))
  15335. (goto-char (point-max))
  15336. (if (re-search-backward "^END:VEVENT" nil t)
  15337. (setq e (match-end 0)))
  15338. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  15339. (kill-buffer buf)
  15340. (delete-file tmpfile)
  15341. rtn))
  15342. (defun org-closest-date (start current change prefer show-all)
  15343. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  15344. When PREFER is `past', return a date that is either CURRENT or past.
  15345. When PREFER is `future', return a date that is either CURRENT or future.
  15346. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  15347. ;; Make the proper lists from the dates
  15348. (catch 'exit
  15349. (let ((a1 '(("h" . hour)
  15350. ("d" . day)
  15351. ("w" . week)
  15352. ("m" . month)
  15353. ("y" . year)))
  15354. (shour (nth 2 (org-parse-time-string start)))
  15355. dn dw sday cday n1 n2 n0
  15356. d m y y1 y2 date1 date2 nmonths nm ny m2)
  15357. (setq start (org-date-to-gregorian start)
  15358. current (org-date-to-gregorian
  15359. (if show-all
  15360. current
  15361. (time-to-days (current-time))))
  15362. sday (calendar-absolute-from-gregorian start)
  15363. cday (calendar-absolute-from-gregorian current))
  15364. (if (<= cday sday) (throw 'exit sday))
  15365. (if (string-match "\\(\\+[0-9]+\\)\\([hdwmy]\\)" change)
  15366. (setq dn (string-to-number (match-string 1 change))
  15367. dw (cdr (assoc (match-string 2 change) a1)))
  15368. (error "Invalid change specifier: %s" change))
  15369. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  15370. (cond
  15371. ((eq dw 'hour)
  15372. (let ((missing-hours
  15373. (mod (+ (- (* 24 (- cday sday)) shour) org-extend-today-until)
  15374. dn)))
  15375. (setq n1 (if (zerop missing-hours) cday
  15376. (- cday (1+ (floor (/ missing-hours 24)))))
  15377. n2 (+ cday (floor (/ (- dn missing-hours) 24))))))
  15378. ((eq dw 'day)
  15379. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  15380. n2 (+ n1 dn)))
  15381. ((eq dw 'year)
  15382. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  15383. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  15384. (setq date1 (list m d y1)
  15385. n1 (calendar-absolute-from-gregorian date1)
  15386. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  15387. n2 (calendar-absolute-from-gregorian date2)))
  15388. ((eq dw 'month)
  15389. ;; approx number of month between the two dates
  15390. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  15391. ;; How often does dn fit in there?
  15392. (setq d (nth 1 start) m (car start) y (nth 2 start)
  15393. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  15394. m (+ m nm)
  15395. ny (floor (/ m 12))
  15396. y (+ y ny)
  15397. m (- m (* ny 12)))
  15398. (while (> m 12) (setq m (- m 12) y (1+ y)))
  15399. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  15400. (setq m2 (+ m dn) y2 y)
  15401. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15402. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  15403. (while (<= n2 cday)
  15404. (setq n1 n2 m m2 y y2)
  15405. (setq m2 (+ m dn) y2 y)
  15406. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15407. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  15408. ;; Make sure n1 is the earlier date
  15409. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  15410. (if show-all
  15411. (cond
  15412. ((eq prefer 'past) (if (= cday n2) n2 n1))
  15413. ((eq prefer 'future) (if (= cday n1) n1 n2))
  15414. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  15415. (cond
  15416. ((eq prefer 'past) (if (= cday n2) n2 n1))
  15417. ((eq prefer 'future) (if (= cday n1) n1 n2))
  15418. (t (if (= cday n1) n1 n2)))))))
  15419. (defun org-date-to-gregorian (date)
  15420. "Turn any specification of DATE into a Gregorian date for the calendar."
  15421. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  15422. ((and (listp date) (= (length date) 3)) date)
  15423. ((stringp date)
  15424. (setq date (org-parse-time-string date))
  15425. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  15426. ((listp date)
  15427. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  15428. (defun org-parse-time-string (s &optional nodefault)
  15429. "Parse the standard Org-mode time string.
  15430. This should be a lot faster than the normal `parse-time-string'.
  15431. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  15432. hour and minute fields will be nil if not given."
  15433. (cond ((string-match org-ts-regexp0 s)
  15434. (list 0
  15435. (if (or (match-beginning 8) (not nodefault))
  15436. (string-to-number (or (match-string 8 s) "0")))
  15437. (if (or (match-beginning 7) (not nodefault))
  15438. (string-to-number (or (match-string 7 s) "0")))
  15439. (string-to-number (match-string 4 s))
  15440. (string-to-number (match-string 3 s))
  15441. (string-to-number (match-string 2 s))
  15442. nil nil nil))
  15443. ((string-match "^<[^>]+>$" s)
  15444. (decode-time (seconds-to-time (org-matcher-time s))))
  15445. (t (error "Not a standard Org-mode time string: %s" s))))
  15446. (defun org-timestamp-up (&optional arg)
  15447. "Increase the date item at the cursor by one.
  15448. If the cursor is on the year, change the year. If it is on the month,
  15449. the day or the time, change that.
  15450. With prefix ARG, change by that many units."
  15451. (interactive "p")
  15452. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  15453. (defun org-timestamp-down (&optional arg)
  15454. "Decrease the date item at the cursor by one.
  15455. If the cursor is on the year, change the year. If it is on the month,
  15456. the day or the time, change that.
  15457. With prefix ARG, change by that many units."
  15458. (interactive "p")
  15459. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  15460. (defun org-timestamp-up-day (&optional arg)
  15461. "Increase the date in the time stamp by one day.
  15462. With prefix ARG, change that many days."
  15463. (interactive "p")
  15464. (if (and (not (org-at-timestamp-p t))
  15465. (org-at-heading-p))
  15466. (org-todo 'up)
  15467. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  15468. (defun org-timestamp-down-day (&optional arg)
  15469. "Decrease the date in the time stamp by one day.
  15470. With prefix ARG, change that many days."
  15471. (interactive "p")
  15472. (if (and (not (org-at-timestamp-p t))
  15473. (org-at-heading-p))
  15474. (org-todo 'down)
  15475. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  15476. (defun org-at-timestamp-p (&optional inactive-ok)
  15477. "Determine if the cursor is in or at a timestamp."
  15478. (interactive)
  15479. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  15480. (pos (point))
  15481. (ans (or (looking-at tsr)
  15482. (save-excursion
  15483. (skip-chars-backward "^[<\n\r\t")
  15484. (if (> (point) (point-min)) (backward-char 1))
  15485. (and (looking-at tsr)
  15486. (> (- (match-end 0) pos) -1))))))
  15487. (and ans
  15488. (boundp 'org-ts-what)
  15489. (setq org-ts-what
  15490. (cond
  15491. ((= pos (match-beginning 0)) 'bracket)
  15492. ;; Point is considered to be "on the bracket" whether
  15493. ;; it's really on it or right after it.
  15494. ((= pos (1- (match-end 0))) 'bracket)
  15495. ((= pos (match-end 0)) 'after)
  15496. ((org-pos-in-match-range pos 2) 'year)
  15497. ((org-pos-in-match-range pos 3) 'month)
  15498. ((org-pos-in-match-range pos 7) 'hour)
  15499. ((org-pos-in-match-range pos 8) 'minute)
  15500. ((or (org-pos-in-match-range pos 4)
  15501. (org-pos-in-match-range pos 5)) 'day)
  15502. ((and (> pos (or (match-end 8) (match-end 5)))
  15503. (< pos (match-end 0)))
  15504. (- pos (or (match-end 8) (match-end 5))))
  15505. (t 'day))))
  15506. ans))
  15507. (defun org-toggle-timestamp-type ()
  15508. "Toggle the type (<active> or [inactive]) of a time stamp."
  15509. (interactive)
  15510. (when (org-at-timestamp-p t)
  15511. (let ((beg (match-beginning 0)) (end (match-end 0))
  15512. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  15513. (save-excursion
  15514. (goto-char beg)
  15515. (while (re-search-forward "[][<>]" end t)
  15516. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  15517. t t)))
  15518. (message "Timestamp is now %sactive"
  15519. (if (equal (char-after beg) ?<) "" "in")))))
  15520. (defun org-at-clock-log-p nil
  15521. "Is the cursor on the clock log line?"
  15522. (save-excursion
  15523. (move-beginning-of-line 1)
  15524. (looking-at "^[ \t]*CLOCK:")))
  15525. (defvar org-clock-history) ; defined in org-clock.el
  15526. (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
  15527. (defun org-timestamp-change (n &optional what updown suppress-tmp-delay)
  15528. "Change the date in the time stamp at point.
  15529. The date will be changed by N times WHAT. WHAT can be `day', `month',
  15530. `year', `minute', `second'. If WHAT is not given, the cursor position
  15531. in the timestamp determines what will be changed.
  15532. When SUPPRESS-TMP-DELAY is non-nil, suppress delays like \"--2d\"."
  15533. (let ((origin (point)) origin-cat
  15534. with-hm inactive
  15535. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  15536. org-ts-what
  15537. extra rem
  15538. ts time time0 fixnext clrgx)
  15539. (if (not (org-at-timestamp-p t))
  15540. (error "Not at a timestamp"))
  15541. (if (and (not what) (eq org-ts-what 'bracket))
  15542. (org-toggle-timestamp-type)
  15543. ;; Point isn't on brackets. Remember the part of the time-stamp
  15544. ;; the point was in. Indeed, size of time-stamps may change,
  15545. ;; but point must be kept in the same category nonetheless.
  15546. (setq origin-cat org-ts-what)
  15547. (if (and (not what) (not (eq org-ts-what 'day))
  15548. org-display-custom-times
  15549. (get-text-property (point) 'display)
  15550. (not (get-text-property (1- (point)) 'display)))
  15551. (setq org-ts-what 'day))
  15552. (setq org-ts-what (or what org-ts-what)
  15553. inactive (= (char-after (match-beginning 0)) ?\[)
  15554. ts (match-string 0))
  15555. (replace-match "")
  15556. (when (string-match
  15557. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?-?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
  15558. ts)
  15559. (setq extra (match-string 1 ts))
  15560. (if suppress-tmp-delay
  15561. (setq extra (replace-regexp-in-string " --[0-9]+[hdwmy]" "" extra))))
  15562. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  15563. (setq with-hm t))
  15564. (setq time0 (org-parse-time-string ts))
  15565. (when (and updown
  15566. (eq org-ts-what 'minute)
  15567. (not current-prefix-arg))
  15568. ;; This looks like s-up and s-down. Change by one rounding step.
  15569. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  15570. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  15571. (setcar (cdr time0) (+ (nth 1 time0)
  15572. (if (> n 0) (- rem) (- dm rem))))))
  15573. (setq time
  15574. (encode-time (or (car time0) 0)
  15575. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  15576. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  15577. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  15578. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  15579. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  15580. (nthcdr 6 time0)))
  15581. (when (and (member org-ts-what '(hour minute))
  15582. extra
  15583. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  15584. (setq extra (org-modify-ts-extra
  15585. extra
  15586. (if (eq org-ts-what 'hour) 2 5)
  15587. n dm)))
  15588. (when (integerp org-ts-what)
  15589. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  15590. (if (eq what 'calendar)
  15591. (let ((cal-date (org-get-date-from-calendar)))
  15592. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  15593. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  15594. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  15595. (setcar time0 (or (car time0) 0))
  15596. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  15597. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  15598. (setq time (apply 'encode-time time0))))
  15599. ;; Insert the new time-stamp, and ensure point stays in the same
  15600. ;; category as before (i.e. not after the last position in that
  15601. ;; category).
  15602. (let ((pos (point)))
  15603. ;; Stay before inserted string. `save-excursion' is of no use.
  15604. (setq org-last-changed-timestamp
  15605. (org-insert-time-stamp time with-hm inactive nil nil extra))
  15606. (goto-char pos))
  15607. (save-match-data
  15608. (looking-at org-ts-regexp3)
  15609. (goto-char (cond
  15610. ;; `day' category ends before `hour' if any, or at
  15611. ;; the end of the day name.
  15612. ((eq origin-cat 'day)
  15613. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  15614. ((eq origin-cat 'hour) (min (match-end 7) origin))
  15615. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  15616. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  15617. ;; `year' and `month' have both fixed size: point
  15618. ;; couldn't have moved into another part.
  15619. (t origin))))
  15620. ;; Update clock if on a CLOCK line.
  15621. (org-clock-update-time-maybe)
  15622. ;; Maybe adjust the closest clock in `org-clock-history'
  15623. (when org-clock-adjust-closest
  15624. (if (not (and (org-at-clock-log-p)
  15625. (< 1 (length (delq nil (mapcar (lambda(m) (marker-position m))
  15626. org-clock-history))))))
  15627. (message "No clock to adjust")
  15628. (cond ((save-excursion ; fix previous clock?
  15629. (re-search-backward org-ts-regexp0 nil t)
  15630. (org-looking-back (concat org-clock-string " \\[")))
  15631. (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
  15632. ((save-excursion ; fix next clock?
  15633. (re-search-backward org-ts-regexp0 nil t)
  15634. (looking-at (concat org-ts-regexp0 "\\] =>")))
  15635. (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
  15636. (save-window-excursion
  15637. ;; Find closest clock to point, adjust the previous/next one in history
  15638. (let* ((p (save-excursion (org-back-to-heading t)))
  15639. (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
  15640. (clfixnth
  15641. (+ fixnext (- (length cl) (or (length (member (apply #'min cl) cl)) 100))))
  15642. (clfixpos (if (> 0 clfixnth) nil (nth clfixnth org-clock-history))))
  15643. (if (not clfixpos)
  15644. (message "No clock to adjust")
  15645. (save-excursion
  15646. (org-goto-marker-or-bmk clfixpos)
  15647. (org-show-subtree)
  15648. (when (re-search-forward clrgx nil t)
  15649. (goto-char (match-beginning 1))
  15650. (let (org-clock-adjust-closest)
  15651. (org-timestamp-change n org-ts-what updown))
  15652. (message "Clock adjusted in %s for heading: %s"
  15653. (file-name-nondirectory (buffer-file-name))
  15654. (org-get-heading t t)))))))))
  15655. ;; Try to recenter the calendar window, if any.
  15656. (if (and org-calendar-follow-timestamp-change
  15657. (get-buffer-window "*Calendar*" t)
  15658. (memq org-ts-what '(day month year)))
  15659. (org-recenter-calendar (time-to-days time))))))
  15660. (defun org-modify-ts-extra (s pos n dm)
  15661. "Change the different parts of the lead-time and repeat fields in timestamp."
  15662. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  15663. ng h m new rem)
  15664. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  15665. (cond
  15666. ((or (org-pos-in-match-range pos 2)
  15667. (org-pos-in-match-range pos 3))
  15668. (setq m (string-to-number (match-string 3 s))
  15669. h (string-to-number (match-string 2 s)))
  15670. (if (org-pos-in-match-range pos 2)
  15671. (setq h (+ h n))
  15672. (setq n (* dm (org-no-warnings (signum n))))
  15673. (when (not (= 0 (setq rem (% m dm))))
  15674. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  15675. (setq m (+ m n)))
  15676. (if (< m 0) (setq m (+ m 60) h (1- h)))
  15677. (if (> m 59) (setq m (- m 60) h (1+ h)))
  15678. (setq h (min 24 (max 0 h)))
  15679. (setq ng 1 new (format "-%02d:%02d" h m)))
  15680. ((org-pos-in-match-range pos 6)
  15681. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  15682. ((org-pos-in-match-range pos 5)
  15683. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  15684. ((org-pos-in-match-range pos 9)
  15685. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  15686. ((org-pos-in-match-range pos 8)
  15687. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  15688. (when ng
  15689. (setq s (concat
  15690. (substring s 0 (match-beginning ng))
  15691. new
  15692. (substring s (match-end ng))))))
  15693. s))
  15694. (defun org-recenter-calendar (date)
  15695. "If the calendar is visible, recenter it to DATE."
  15696. (let ((cwin (get-buffer-window "*Calendar*" t)))
  15697. (when cwin
  15698. (let ((calendar-move-hook nil))
  15699. (with-selected-window cwin
  15700. (calendar-goto-date (if (listp date) date
  15701. (calendar-gregorian-from-absolute date))))))))
  15702. (defun org-goto-calendar (&optional arg)
  15703. "Go to the Emacs calendar at the current date.
  15704. If there is a time stamp in the current line, go to that date.
  15705. A prefix ARG can be used to force the current date."
  15706. (interactive "P")
  15707. (let ((tsr org-ts-regexp) diff
  15708. (calendar-move-hook nil)
  15709. (calendar-view-holidays-initially-flag nil)
  15710. (calendar-view-diary-initially-flag nil))
  15711. (if (or (org-at-timestamp-p)
  15712. (save-excursion
  15713. (beginning-of-line 1)
  15714. (looking-at (concat ".*" tsr))))
  15715. (let ((d1 (time-to-days (current-time)))
  15716. (d2 (time-to-days
  15717. (org-time-string-to-time (match-string 1)))))
  15718. (setq diff (- d2 d1))))
  15719. (calendar)
  15720. (calendar-goto-today)
  15721. (if (and diff (not arg)) (calendar-forward-day diff))))
  15722. (defun org-get-date-from-calendar ()
  15723. "Return a list (month day year) of date at point in calendar."
  15724. (with-current-buffer "*Calendar*"
  15725. (save-match-data
  15726. (calendar-cursor-to-date))))
  15727. (defun org-date-from-calendar ()
  15728. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  15729. If there is already a time stamp at the cursor position, update it."
  15730. (interactive)
  15731. (if (org-at-timestamp-p t)
  15732. (org-timestamp-change 0 'calendar)
  15733. (let ((cal-date (org-get-date-from-calendar)))
  15734. (org-insert-time-stamp
  15735. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  15736. (defcustom org-effort-durations
  15737. `(("h" . 60)
  15738. ("d" . ,(* 60 8))
  15739. ("w" . ,(* 60 8 5))
  15740. ("m" . ,(* 60 8 5 4))
  15741. ("y" . ,(* 60 8 5 40)))
  15742. "Conversion factor to minutes for an effort modifier.
  15743. Each entry has the form (MODIFIER . MINUTES).
  15744. In an effort string, a number followed by MODIFIER is multiplied
  15745. by the specified number of MINUTES to obtain an effort in
  15746. minutes.
  15747. For example, if the value of this variable is ((\"hours\" . 60)), then an
  15748. effort string \"2hours\" is equivalent to 120 minutes."
  15749. :group 'org-agenda
  15750. :version "24.1"
  15751. :type '(alist :key-type (string :tag "Modifier")
  15752. :value-type (number :tag "Minutes")))
  15753. (defun org-minutes-to-clocksum-string (m)
  15754. "Format number of minutes as a clocksum string.
  15755. The format is determined by `org-time-clocksum-format',
  15756. `org-time-clocksum-use-fractional' and
  15757. `org-time-clocksum-fractional-format' and
  15758. `org-time-clocksum-use-effort-durations'."
  15759. (let ((clocksum "") h d w mo y fmt n)
  15760. (setq h (if org-time-clocksum-use-effort-durations
  15761. (cdr (assoc "h" org-effort-durations)) 60)
  15762. d (if org-time-clocksum-use-effort-durations
  15763. (/ (cdr (assoc "d" org-effort-durations)) h) 24)
  15764. w (if org-time-clocksum-use-effort-durations
  15765. (/ (cdr (assoc "w" org-effort-durations)) (* d h)) 7)
  15766. mo (if org-time-clocksum-use-effort-durations
  15767. (/ (cdr (assoc "m" org-effort-durations)) (* d h)) 30)
  15768. y (if org-time-clocksum-use-effort-durations
  15769. (/ (cdr (assoc "y" org-effort-durations)) (* d h)) 365))
  15770. ;; fractional format
  15771. (if org-time-clocksum-use-fractional
  15772. (cond
  15773. ;; single format string
  15774. ((stringp org-time-clocksum-fractional-format)
  15775. (format org-time-clocksum-fractional-format (/ m (float h))))
  15776. ;; choice of fractional formats for different time units
  15777. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :years))
  15778. (> (/ (truncate m) (* y d h)) 0))
  15779. (format fmt (/ m (* y d (float h)))))
  15780. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :months))
  15781. (> (/ (truncate m) (* mo d h)) 0))
  15782. (format fmt (/ m (* mo d (float h)))))
  15783. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  15784. (> (/ (truncate m) (* w d h)) 0))
  15785. (format fmt (/ m (* w d (float h)))))
  15786. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :days))
  15787. (> (/ (truncate m) (* d h)) 0))
  15788. (format fmt (/ m (* d (float h)))))
  15789. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  15790. (> (/ (truncate m) h) 0))
  15791. (format fmt (/ m (float h))))
  15792. ((setq fmt (plist-get org-time-clocksum-fractional-format :minutes))
  15793. (format fmt m))
  15794. ;; fall back to smallest time unit with a format
  15795. ((setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  15796. (format fmt (/ m (float h))))
  15797. ((setq fmt (plist-get org-time-clocksum-fractional-format :days))
  15798. (format fmt (/ m (* d (float h)))))
  15799. ((setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  15800. (format fmt (/ m (* w d (float h)))))
  15801. ((setq fmt (plist-get org-time-clocksum-fractional-format :months))
  15802. (format fmt (/ m (* mo d (float h)))))
  15803. ((setq fmt (plist-get org-time-clocksum-fractional-format :years))
  15804. (format fmt (/ m (* y d (float h))))))
  15805. ;; standard (non-fractional) format, with single format string
  15806. (if (stringp org-time-clocksum-format)
  15807. (format org-time-clocksum-format (setq n (/ m h)) (- m (* h n)))
  15808. ;; separate formats components
  15809. (and (setq fmt (plist-get org-time-clocksum-format :years))
  15810. (or (> (setq n (/ (truncate m) (* y d h))) 0)
  15811. (plist-get org-time-clocksum-format :require-years))
  15812. (setq clocksum (concat clocksum (format fmt n))
  15813. m (- m (* n y d h))))
  15814. (and (setq fmt (plist-get org-time-clocksum-format :months))
  15815. (or (> (setq n (/ (truncate m) (* mo d h))) 0)
  15816. (plist-get org-time-clocksum-format :require-months))
  15817. (setq clocksum (concat clocksum (format fmt n))
  15818. m (- m (* n mo d h))))
  15819. (and (setq fmt (plist-get org-time-clocksum-format :weeks))
  15820. (or (> (setq n (/ (truncate m) (* w d h))) 0)
  15821. (plist-get org-time-clocksum-format :require-weeks))
  15822. (setq clocksum (concat clocksum (format fmt n))
  15823. m (- m (* n w d h))))
  15824. (and (setq fmt (plist-get org-time-clocksum-format :days))
  15825. (or (> (setq n (/ (truncate m) (* d h))) 0)
  15826. (plist-get org-time-clocksum-format :require-days))
  15827. (setq clocksum (concat clocksum (format fmt n))
  15828. m (- m (* n d h))))
  15829. (and (setq fmt (plist-get org-time-clocksum-format :hours))
  15830. (or (> (setq n (/ (truncate m) h)) 0)
  15831. (plist-get org-time-clocksum-format :require-hours))
  15832. (setq clocksum (concat clocksum (format fmt n))
  15833. m (- m (* n h))))
  15834. (and (setq fmt (plist-get org-time-clocksum-format :minutes))
  15835. (or (> m 0) (plist-get org-time-clocksum-format :require-minutes))
  15836. (setq clocksum (concat clocksum (format fmt m))))
  15837. ;; return formatted time duration
  15838. clocksum))))
  15839. (defalias 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string)
  15840. (make-obsolete 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string
  15841. "Org mode version 8.0")
  15842. (defun org-hours-to-clocksum-string (n)
  15843. (org-minutes-to-clocksum-string (* n 60)))
  15844. (defun org-hh:mm-string-to-minutes (s)
  15845. "Convert a string H:MM to a number of minutes.
  15846. If the string is just a number, interpret it as minutes.
  15847. In fact, the first hh:mm or number in the string will be taken,
  15848. there can be extra stuff in the string.
  15849. If no number is found, the return value is 0."
  15850. (cond
  15851. ((integerp s) s)
  15852. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  15853. (+ (* (string-to-number (match-string 1 s)) 60)
  15854. (string-to-number (match-string 2 s))))
  15855. ((string-match "\\([0-9]+\\)" s)
  15856. (string-to-number (match-string 1 s)))
  15857. (t 0)))
  15858. (defcustom org-image-actual-width t
  15859. "Should we use the actual width of images when inlining them?
  15860. When set to `t', always use the image width.
  15861. When set to a number, use imagemagick (when available) to set
  15862. the image's width to this value.
  15863. When set to a number in a list, try to get the width from the
  15864. #+ATTR.* keyword if it matches a width specification like
  15865. width=\"[0-9]+\", and fall back on that number if none is found.
  15866. When set to nil, try to get the width from an #+ATTR.* keyword
  15867. and fall back on the original width if none is found.
  15868. This requires Emacs >= 24.1, build with imagemagick support."
  15869. :group 'org-appearance
  15870. :version "24.4"
  15871. :package-version '(Org . "8.0")
  15872. :type '(choice
  15873. (const :tag "Use the image width" t)
  15874. (integer :tag "Use a number of pixels")
  15875. (list :tag "Use #+ATTR* or a number of pixels" (integer))
  15876. (const :tag "Use #+ATTR* or don't resize" nil)))
  15877. (defcustom org-agenda-inhibit-startup nil
  15878. "Inhibit startup when preparing agenda buffers.
  15879. When this variable is `t' (the default), the initialization of
  15880. the Org agenda buffers is inhibited: e.g. the visibility state
  15881. is not set, the tables are not re-aligned, etc."
  15882. :type 'boolean
  15883. :version "24.3"
  15884. :group 'org-agenda)
  15885. (defun org-duration-string-to-minutes (s &optional output-to-string)
  15886. "Convert a duration string S to minutes.
  15887. A bare number is interpreted as minutes, modifiers can be set by
  15888. customizing `org-effort-durations' (which see).
  15889. Entries containing a colon are interpreted as H:MM by
  15890. `org-hh:mm-string-to-minutes'."
  15891. (let ((result 0)
  15892. (re (concat "\\([0-9.]+\\) *\\("
  15893. (regexp-opt (mapcar 'car org-effort-durations))
  15894. "\\)")))
  15895. (while (string-match re s)
  15896. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  15897. (string-to-number (match-string 1 s))))
  15898. (setq s (replace-match "" nil t s)))
  15899. (setq result (floor result))
  15900. (incf result (org-hh:mm-string-to-minutes s))
  15901. (if output-to-string (number-to-string result) result)))
  15902. ;;;; Files
  15903. (defun org-save-all-org-buffers ()
  15904. "Save all Org-mode buffers without user confirmation."
  15905. (interactive)
  15906. (message "Saving all Org-mode buffers...")
  15907. (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
  15908. (when (featurep 'org-id) (org-id-locations-save))
  15909. (message "Saving all Org-mode buffers... done"))
  15910. (defun org-revert-all-org-buffers ()
  15911. "Revert all Org-mode buffers.
  15912. Prompt for confirmation when there are unsaved changes.
  15913. Be sure you know what you are doing before letting this function
  15914. overwrite your changes.
  15915. This function is useful in a setup where one tracks org files
  15916. with a version control system, to revert on one machine after pulling
  15917. changes from another. I believe the procedure must be like this:
  15918. 1. M-x org-save-all-org-buffers
  15919. 2. Pull changes from the other machine, resolve conflicts
  15920. 3. M-x org-revert-all-org-buffers"
  15921. (interactive)
  15922. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  15923. (error "Abort"))
  15924. (save-excursion
  15925. (save-window-excursion
  15926. (mapc
  15927. (lambda (b)
  15928. (when (and (with-current-buffer b (derived-mode-p 'org-mode))
  15929. (with-current-buffer b buffer-file-name))
  15930. (org-pop-to-buffer-same-window b)
  15931. (revert-buffer t 'no-confirm)))
  15932. (buffer-list))
  15933. (when (and (featurep 'org-id) org-id-track-globally)
  15934. (org-id-locations-load)))))
  15935. ;;;; Agenda files
  15936. ;;;###autoload
  15937. (defun org-switchb (&optional arg)
  15938. "Switch between Org buffers.
  15939. With one prefix argument, restrict available buffers to files.
  15940. With two prefix arguments, restrict available buffers to agenda files.
  15941. Defaults to `iswitchb' for buffer name completion.
  15942. Set `org-completion-use-ido' to make it use ido instead."
  15943. (interactive "P")
  15944. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  15945. ((equal arg '(16)) (org-buffer-list 'agenda))
  15946. (t (org-buffer-list))))
  15947. (org-completion-use-iswitchb org-completion-use-iswitchb)
  15948. (org-completion-use-ido org-completion-use-ido))
  15949. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  15950. (setq org-completion-use-iswitchb t))
  15951. (org-pop-to-buffer-same-window
  15952. (org-icompleting-read "Org buffer: "
  15953. (mapcar 'list (mapcar 'buffer-name blist))
  15954. nil t))))
  15955. ;;; Define some older names previously used for this functionality
  15956. ;;;###autoload
  15957. (defalias 'org-ido-switchb 'org-switchb)
  15958. ;;;###autoload
  15959. (defalias 'org-iswitchb 'org-switchb)
  15960. (defun org-buffer-list (&optional predicate exclude-tmp)
  15961. "Return a list of Org buffers.
  15962. PREDICATE can be `export', `files' or `agenda'.
  15963. export restrict the list to Export buffers.
  15964. files restrict the list to buffers visiting Org files.
  15965. agenda restrict the list to buffers visiting agenda files.
  15966. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  15967. (let* ((bfn nil)
  15968. (agenda-files (and (eq predicate 'agenda)
  15969. (mapcar 'file-truename (org-agenda-files t))))
  15970. (filter
  15971. (cond
  15972. ((eq predicate 'files)
  15973. (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
  15974. ((eq predicate 'export)
  15975. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  15976. ((eq predicate 'agenda)
  15977. (lambda (b)
  15978. (with-current-buffer b
  15979. (and (derived-mode-p 'org-mode)
  15980. (setq bfn (buffer-file-name b))
  15981. (member (file-truename bfn) agenda-files)))))
  15982. (t (lambda (b) (with-current-buffer b
  15983. (or (derived-mode-p 'org-mode)
  15984. (string-match "\*Org .*Export"
  15985. (buffer-name b)))))))))
  15986. (delq nil
  15987. (mapcar
  15988. (lambda(b)
  15989. (if (and (funcall filter b)
  15990. (or (not exclude-tmp)
  15991. (not (string-match "tmp" (buffer-name b)))))
  15992. b
  15993. nil))
  15994. (buffer-list)))))
  15995. (defun org-agenda-files (&optional unrestricted archives)
  15996. "Get the list of agenda files.
  15997. Optional UNRESTRICTED means return the full list even if a restriction
  15998. is currently in place.
  15999. When ARCHIVES is t, include all archive files that are really being
  16000. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  16001. `org-agenda-archives-mode' is t."
  16002. (let ((files
  16003. (cond
  16004. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  16005. ((stringp org-agenda-files) (org-read-agenda-file-list))
  16006. ((listp org-agenda-files) org-agenda-files)
  16007. (t (error "Invalid value of `org-agenda-files'")))))
  16008. (setq files (apply 'append
  16009. (mapcar (lambda (f)
  16010. (if (file-directory-p f)
  16011. (directory-files
  16012. f t org-agenda-file-regexp)
  16013. (list f)))
  16014. files)))
  16015. (when org-agenda-skip-unavailable-files
  16016. (setq files (delq nil
  16017. (mapcar (function
  16018. (lambda (file)
  16019. (and (file-readable-p file) file)))
  16020. files))))
  16021. (when (or (eq archives t)
  16022. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  16023. (setq files (org-add-archive-files files)))
  16024. files))
  16025. (defun org-agenda-file-p (&optional file)
  16026. "Return non-nil, if FILE is an agenda file.
  16027. If FILE is omitted, use the file associated with the current
  16028. buffer."
  16029. (member (or file (buffer-file-name))
  16030. (org-agenda-files t)))
  16031. (defun org-edit-agenda-file-list ()
  16032. "Edit the list of agenda files.
  16033. Depending on setup, this either uses customize to edit the variable
  16034. `org-agenda-files', or it visits the file that is holding the list. In the
  16035. latter case, the buffer is set up in a way that saving it automatically kills
  16036. the buffer and restores the previous window configuration."
  16037. (interactive)
  16038. (if (stringp org-agenda-files)
  16039. (let ((cw (current-window-configuration)))
  16040. (find-file org-agenda-files)
  16041. (org-set-local 'org-window-configuration cw)
  16042. (org-add-hook 'after-save-hook
  16043. (lambda ()
  16044. (set-window-configuration
  16045. (prog1 org-window-configuration
  16046. (kill-buffer (current-buffer))))
  16047. (org-install-agenda-files-menu)
  16048. (message "New agenda file list installed"))
  16049. nil 'local)
  16050. (message "%s" (substitute-command-keys
  16051. "Edit list and finish with \\[save-buffer]")))
  16052. (customize-variable 'org-agenda-files)))
  16053. (defun org-store-new-agenda-file-list (list)
  16054. "Set new value for the agenda file list and save it correctly."
  16055. (if (stringp org-agenda-files)
  16056. (let ((fe (org-read-agenda-file-list t)) b u)
  16057. (while (setq b (find-buffer-visiting org-agenda-files))
  16058. (kill-buffer b))
  16059. (with-temp-file org-agenda-files
  16060. (insert
  16061. (mapconcat
  16062. (lambda (f) ;; Keep un-expanded entries.
  16063. (if (setq u (assoc f fe))
  16064. (cdr u)
  16065. f))
  16066. list "\n")
  16067. "\n")))
  16068. (let ((org-mode-hook nil) (org-inhibit-startup t)
  16069. (org-insert-mode-line-in-empty-file nil))
  16070. (setq org-agenda-files list)
  16071. (customize-save-variable 'org-agenda-files org-agenda-files))))
  16072. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  16073. "Read the list of agenda files from a file.
  16074. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  16075. filenames, used by `org-store-new-agenda-file-list' to write back
  16076. un-expanded file names."
  16077. (when (file-directory-p org-agenda-files)
  16078. (error "`org-agenda-files' cannot be a single directory"))
  16079. (when (stringp org-agenda-files)
  16080. (with-temp-buffer
  16081. (insert-file-contents org-agenda-files)
  16082. (mapcar
  16083. (lambda (f)
  16084. (let ((e (expand-file-name (substitute-in-file-name f)
  16085. org-directory)))
  16086. (if pair-with-expansion
  16087. (cons e f)
  16088. e)))
  16089. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  16090. ;;;###autoload
  16091. (defun org-cycle-agenda-files ()
  16092. "Cycle through the files in `org-agenda-files'.
  16093. If the current buffer visits an agenda file, find the next one in the list.
  16094. If the current buffer does not, find the first agenda file."
  16095. (interactive)
  16096. (let* ((fs (org-agenda-files t))
  16097. (files (append fs (list (car fs))))
  16098. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  16099. file)
  16100. (unless files (error "No agenda files"))
  16101. (catch 'exit
  16102. (while (setq file (pop files))
  16103. (if (equal (file-truename file) tcf)
  16104. (when (car files)
  16105. (find-file (car files))
  16106. (throw 'exit t))))
  16107. (find-file (car fs)))
  16108. (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  16109. (defun org-agenda-file-to-front (&optional to-end)
  16110. "Move/add the current file to the top of the agenda file list.
  16111. If the file is not present in the list, it is added to the front. If it is
  16112. present, it is moved there. With optional argument TO-END, add/move to the
  16113. end of the list."
  16114. (interactive "P")
  16115. (let ((org-agenda-skip-unavailable-files nil)
  16116. (file-alist (mapcar (lambda (x)
  16117. (cons (file-truename x) x))
  16118. (org-agenda-files t)))
  16119. (ctf (file-truename
  16120. (or buffer-file-name
  16121. (error "Please save the current buffer to a file"))))
  16122. x had)
  16123. (setq x (assoc ctf file-alist) had x)
  16124. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  16125. (if to-end
  16126. (setq file-alist (append (delq x file-alist) (list x)))
  16127. (setq file-alist (cons x (delq x file-alist))))
  16128. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  16129. (org-install-agenda-files-menu)
  16130. (message "File %s to %s of agenda file list"
  16131. (if had "moved" "added") (if to-end "end" "front"))))
  16132. (defun org-remove-file (&optional file)
  16133. "Remove current file from the list of files in variable `org-agenda-files'.
  16134. These are the files which are being checked for agenda entries.
  16135. Optional argument FILE means use this file instead of the current."
  16136. (interactive)
  16137. (let* ((org-agenda-skip-unavailable-files nil)
  16138. (file (or file buffer-file-name
  16139. (error "Current buffer does not visit a file")))
  16140. (true-file (file-truename file))
  16141. (afile (abbreviate-file-name file))
  16142. (files (delq nil (mapcar
  16143. (lambda (x)
  16144. (if (equal true-file
  16145. (file-truename x))
  16146. nil x))
  16147. (org-agenda-files t)))))
  16148. (if (not (= (length files) (length (org-agenda-files t))))
  16149. (progn
  16150. (org-store-new-agenda-file-list files)
  16151. (org-install-agenda-files-menu)
  16152. (message "Removed file: %s" afile))
  16153. (message "File was not in list: %s (not removed)" afile))))
  16154. (defun org-file-menu-entry (file)
  16155. (vector file (list 'find-file file) t))
  16156. (defun org-check-agenda-file (file)
  16157. "Make sure FILE exists. If not, ask user what to do."
  16158. (when (not (file-exists-p file))
  16159. (message "Non-existent agenda file %s. [R]emove from list or [A]bort?"
  16160. (abbreviate-file-name file))
  16161. (let ((r (downcase (read-char-exclusive))))
  16162. (cond
  16163. ((equal r ?r)
  16164. (org-remove-file file)
  16165. (throw 'nextfile t))
  16166. (t (error "Abort"))))))
  16167. (defun org-get-agenda-file-buffer (file)
  16168. "Get a buffer visiting FILE. If the buffer needs to be created, add
  16169. it to the list of buffers which might be released later."
  16170. (let ((buf (org-find-base-buffer-visiting file)))
  16171. (if buf
  16172. buf ; just return it
  16173. ;; Make a new buffer and remember it
  16174. (setq buf (find-file-noselect file))
  16175. (if buf (push buf org-agenda-new-buffers))
  16176. buf)))
  16177. (defun org-release-buffers (blist)
  16178. "Release all buffers in list, asking the user for confirmation when needed.
  16179. When a buffer is unmodified, it is just killed. When modified, it is saved
  16180. \(if the user agrees) and then killed."
  16181. (let (buf file)
  16182. (while (setq buf (pop blist))
  16183. (setq file (buffer-file-name buf))
  16184. (when (and (buffer-modified-p buf)
  16185. file
  16186. (y-or-n-p (format "Save file %s? " file)))
  16187. (with-current-buffer buf (save-buffer)))
  16188. (kill-buffer buf))))
  16189. (defun org-agenda-prepare-buffers (files)
  16190. "Create buffers for all agenda files, protect archived trees and comments."
  16191. (interactive)
  16192. (let ((pa '(:org-archived t))
  16193. (pc '(:org-comment t))
  16194. (pall '(:org-archived t :org-comment t))
  16195. (inhibit-read-only t)
  16196. (org-inhibit-startup org-agenda-inhibit-startup)
  16197. (rea (concat ":" org-archive-tag ":"))
  16198. file re)
  16199. (save-excursion
  16200. (save-restriction
  16201. (while (setq file (pop files))
  16202. (catch 'nextfile
  16203. (if (bufferp file)
  16204. (set-buffer file)
  16205. (org-check-agenda-file file)
  16206. (set-buffer (org-get-agenda-file-buffer file)))
  16207. (widen)
  16208. (org-refresh-category-properties)
  16209. (org-refresh-properties org-effort-property 'org-effort)
  16210. (org-refresh-properties "APPT_WARNTIME" 'org-appt-warntime)
  16211. (setq org-todo-keywords-for-agenda
  16212. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  16213. (setq org-done-keywords-for-agenda
  16214. (append org-done-keywords-for-agenda org-done-keywords))
  16215. (setq org-todo-keyword-alist-for-agenda
  16216. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  16217. (setq org-drawers-for-agenda
  16218. (append org-drawers-for-agenda org-drawers))
  16219. (setq org-tag-alist-for-agenda
  16220. (append org-tag-alist-for-agenda org-tag-alist))
  16221. (org-with-silent-modifications
  16222. (save-excursion
  16223. (remove-text-properties (point-min) (point-max) pall)
  16224. (when org-agenda-skip-archived-trees
  16225. (goto-char (point-min))
  16226. (while (re-search-forward rea nil t)
  16227. (if (org-at-heading-p t)
  16228. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  16229. (goto-char (point-min))
  16230. (setq re (format org-heading-keyword-regexp-format
  16231. org-comment-string))
  16232. (while (re-search-forward re nil t)
  16233. (add-text-properties
  16234. (match-beginning 0) (org-end-of-subtree t) pc))))))))
  16235. (setq org-todo-keywords-for-agenda
  16236. (org-uniquify org-todo-keywords-for-agenda))
  16237. (setq org-todo-keyword-alist-for-agenda
  16238. (org-uniquify org-todo-keyword-alist-for-agenda)
  16239. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  16240. ;;;; CDLaTeX minor mode
  16241. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  16242. "Keymap for the minor `org-cdlatex-mode'.")
  16243. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  16244. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  16245. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  16246. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  16247. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  16248. (defvar org-cdlatex-texmathp-advice-is-done nil
  16249. "Flag remembering if we have applied the advice to texmathp already.")
  16250. (define-minor-mode org-cdlatex-mode
  16251. "Toggle the minor `org-cdlatex-mode'.
  16252. This mode supports entering LaTeX environment and math in LaTeX fragments
  16253. in Org-mode.
  16254. \\{org-cdlatex-mode-map}"
  16255. nil " OCDL" nil
  16256. (when org-cdlatex-mode
  16257. (require 'cdlatex)
  16258. (run-hooks 'cdlatex-mode-hook)
  16259. (cdlatex-compute-tables))
  16260. (unless org-cdlatex-texmathp-advice-is-done
  16261. (setq org-cdlatex-texmathp-advice-is-done t)
  16262. (defadvice texmathp (around org-math-always-on activate)
  16263. "Always return t in org-mode buffers.
  16264. This is because we want to insert math symbols without dollars even outside
  16265. the LaTeX math segments. If Orgmode thinks that point is actually inside
  16266. an embedded LaTeX fragment, let texmathp do its job.
  16267. \\[org-cdlatex-mode-map]"
  16268. (interactive)
  16269. (let (p)
  16270. (cond
  16271. ((not (derived-mode-p 'org-mode)) ad-do-it)
  16272. ((eq this-command 'cdlatex-math-symbol)
  16273. (setq ad-return-value t
  16274. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  16275. (t
  16276. (let ((p (org-inside-LaTeX-fragment-p)))
  16277. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  16278. (setq ad-return-value t
  16279. texmathp-why '("Org-mode embedded math" . 0))
  16280. (if p ad-do-it)))))))))
  16281. (defun turn-on-org-cdlatex ()
  16282. "Unconditionally turn on `org-cdlatex-mode'."
  16283. (org-cdlatex-mode 1))
  16284. (defun org-try-cdlatex-tab ()
  16285. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  16286. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  16287. - inside a LaTeX fragment, or
  16288. - after the first word in a line, where an abbreviation expansion could
  16289. insert a LaTeX environment."
  16290. (when org-cdlatex-mode
  16291. (cond
  16292. ;; Before any word on the line: No expansion possible.
  16293. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  16294. ;; Just after first word on the line: Expand it. Make sure it
  16295. ;; cannot happen on headlines, though.
  16296. ((save-excursion
  16297. (skip-chars-backward "a-zA-Z0-9*")
  16298. (skip-chars-backward " \t")
  16299. (and (bolp) (not (org-at-heading-p))))
  16300. (cdlatex-tab) t)
  16301. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  16302. (defun org-cdlatex-underscore-caret (&optional arg)
  16303. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  16304. Revert to the normal definition outside of these fragments."
  16305. (interactive "P")
  16306. (if (org-inside-LaTeX-fragment-p)
  16307. (call-interactively 'cdlatex-sub-superscript)
  16308. (let (org-cdlatex-mode)
  16309. (call-interactively (key-binding (vector last-input-event))))))
  16310. (defun org-cdlatex-math-modify (&optional arg)
  16311. "Execute `cdlatex-math-modify' in LaTeX fragments.
  16312. Revert to the normal definition outside of these fragments."
  16313. (interactive "P")
  16314. (if (org-inside-LaTeX-fragment-p)
  16315. (call-interactively 'cdlatex-math-modify)
  16316. (let (org-cdlatex-mode)
  16317. (call-interactively (key-binding (vector last-input-event))))))
  16318. ;;;; LaTeX fragments
  16319. (defvar org-latex-regexps
  16320. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  16321. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  16322. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  16323. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  16324. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  16325. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  16326. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  16327. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  16328. "Regular expressions for matching embedded LaTeX.")
  16329. (defun org-inside-LaTeX-fragment-p ()
  16330. "Test if point is inside a LaTeX fragment.
  16331. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  16332. sequence appearing also before point.
  16333. Even though the matchers for math are configurable, this function assumes
  16334. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  16335. delimiters are skipped when they have been removed by customization.
  16336. The return value is nil, or a cons cell with the delimiter and the
  16337. position of this delimiter.
  16338. This function does a reasonably good job, but can locally be fooled by
  16339. for example currency specifications. For example it will assume being in
  16340. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  16341. fragments that are properly closed, but during editing, we have to live
  16342. with the uncertainty caused by missing closing delimiters. This function
  16343. looks only before point, not after."
  16344. (catch 'exit
  16345. (let ((pos (point))
  16346. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  16347. (lim (progn
  16348. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  16349. (point)))
  16350. dd-on str (start 0) m re)
  16351. (goto-char pos)
  16352. (when dodollar
  16353. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  16354. re (nth 1 (assoc "$" org-latex-regexps)))
  16355. (while (string-match re str start)
  16356. (cond
  16357. ((= (match-end 0) (length str))
  16358. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  16359. ((= (match-end 0) (- (length str) 5))
  16360. (throw 'exit nil))
  16361. (t (setq start (match-end 0))))))
  16362. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  16363. (goto-char pos)
  16364. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  16365. (and (match-beginning 2) (throw 'exit nil))
  16366. ;; count $$
  16367. (while (re-search-backward "\\$\\$" lim t)
  16368. (setq dd-on (not dd-on)))
  16369. (goto-char pos)
  16370. (if dd-on (cons "$$" m))))))
  16371. (defun org-inside-latex-macro-p ()
  16372. "Is point inside a LaTeX macro or its arguments?"
  16373. (save-match-data
  16374. (org-in-regexp
  16375. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  16376. (defvar org-latex-fragment-image-overlays nil
  16377. "List of overlays carrying the images of latex fragments.")
  16378. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  16379. (defun org-remove-latex-fragment-image-overlays ()
  16380. "Remove all overlays with LaTeX fragment images in current buffer."
  16381. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  16382. (setq org-latex-fragment-image-overlays nil))
  16383. (defun org-preview-latex-fragment (&optional subtree)
  16384. "Preview the LaTeX fragment at point, or all locally or globally.
  16385. If the cursor is in a LaTeX fragment, create the image and overlay
  16386. it over the source code. If there is no fragment at point, display
  16387. all fragments in the current text, from one headline to the next. With
  16388. prefix SUBTREE, display all fragments in the current subtree. With a
  16389. double prefix arg \\[universal-argument] \\[universal-argument], or when \
  16390. the cursor is before the first headline,
  16391. display all fragments in the buffer.
  16392. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  16393. (interactive "P")
  16394. (unless buffer-file-name
  16395. (error "Can't preview LaTeX fragment in a non-file buffer"))
  16396. (org-remove-latex-fragment-image-overlays)
  16397. (save-excursion
  16398. (save-restriction
  16399. (let (beg end at msg)
  16400. (cond
  16401. ((or (equal subtree '(16))
  16402. (not (save-excursion
  16403. (re-search-backward org-outline-regexp-bol nil t))))
  16404. (setq beg (point-min) end (point-max)
  16405. msg "Creating images for buffer...%s"))
  16406. ((equal subtree '(4))
  16407. (org-back-to-heading)
  16408. (setq beg (point) end (org-end-of-subtree t)
  16409. msg "Creating images for subtree...%s"))
  16410. (t
  16411. (if (setq at (org-inside-LaTeX-fragment-p))
  16412. (goto-char (max (point-min) (- (cdr at) 2)))
  16413. (org-back-to-heading))
  16414. (setq beg (point) end (progn (outline-next-heading) (point))
  16415. msg (if at "Creating image...%s"
  16416. "Creating images for entry...%s"))))
  16417. (message msg "")
  16418. (narrow-to-region beg end)
  16419. (goto-char beg)
  16420. (org-format-latex
  16421. (concat org-latex-preview-ltxpng-directory (file-name-sans-extension
  16422. (file-name-nondirectory
  16423. buffer-file-name)))
  16424. default-directory 'overlays msg at 'forbuffer
  16425. org-latex-create-formula-image-program)
  16426. (message msg "done. Use `C-c C-c' to remove images.")))))
  16427. (defun org-format-latex (prefix &optional dir overlays msg at
  16428. forbuffer processing-type)
  16429. "Replace LaTeX fragments with links to an image, and produce images.
  16430. Some of the options can be changed using the variable
  16431. `org-format-latex-options'."
  16432. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  16433. (let* ((prefixnodir (file-name-nondirectory prefix))
  16434. (absprefix (expand-file-name prefix dir))
  16435. (todir (file-name-directory absprefix))
  16436. (opt org-format-latex-options)
  16437. (optnew org-format-latex-options)
  16438. (matchers (plist-get opt :matchers))
  16439. (re-list org-latex-regexps)
  16440. (cnt 0) txt hash link beg end re e checkdir
  16441. string
  16442. m n block-type block linkfile movefile ov)
  16443. ;; Check the different regular expressions
  16444. (while (setq e (pop re-list))
  16445. (setq m (car e) re (nth 1 e) n (nth 2 e) block-type (nth 3 e)
  16446. block (if block-type "\n\n" ""))
  16447. (when (member m matchers)
  16448. (goto-char (point-min))
  16449. (while (re-search-forward re nil t)
  16450. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  16451. (or (not overlays)
  16452. (not (eq (get-char-property (match-beginning n)
  16453. 'org-overlay-type)
  16454. 'org-latex-overlay))))
  16455. (cond
  16456. ((eq processing-type 'verbatim))
  16457. ((eq processing-type 'mathjax)
  16458. ;; Prepare for MathJax processing.
  16459. (setq string (match-string n))
  16460. (when (member m '("$" "$1"))
  16461. (save-excursion
  16462. (delete-region (match-beginning n) (match-end n))
  16463. (goto-char (match-beginning n))
  16464. (insert (concat "\\(" (substring string 1 -1) "\\)")))))
  16465. ((or (eq processing-type 'dvipng)
  16466. (eq processing-type 'imagemagick))
  16467. ;; Process to an image.
  16468. (setq txt (match-string n)
  16469. beg (match-beginning n) end (match-end n)
  16470. cnt (1+ cnt))
  16471. (let ((face (face-at-point))
  16472. (fg (plist-get opt :foreground))
  16473. (bg (plist-get opt :background))
  16474. ;; Ensure full list is printed.
  16475. print-length print-level)
  16476. (when forbuffer
  16477. ;; Get the colors from the face at point.
  16478. (goto-char beg)
  16479. (when (eq fg 'auto)
  16480. (setq fg (face-attribute face :foreground nil 'default)))
  16481. (when (eq bg 'auto)
  16482. (setq bg (face-attribute face :background nil 'default)))
  16483. (setq optnew (copy-sequence opt))
  16484. (plist-put optnew :foreground fg)
  16485. (plist-put optnew :background bg))
  16486. (setq hash (sha1 (prin1-to-string
  16487. (list org-format-latex-header
  16488. org-latex-default-packages-alist
  16489. org-latex-packages-alist
  16490. org-format-latex-options
  16491. forbuffer txt fg bg)))
  16492. linkfile (format "%s_%s.png" prefix hash)
  16493. movefile (format "%s_%s.png" absprefix hash)))
  16494. (setq link (concat block "[[file:" linkfile "]]" block))
  16495. (if msg (message msg cnt))
  16496. (goto-char beg)
  16497. (unless checkdir ; Ensure the directory exists.
  16498. (setq checkdir t)
  16499. (or (file-directory-p todir) (make-directory todir t)))
  16500. (unless (file-exists-p movefile)
  16501. (org-create-formula-image
  16502. txt movefile optnew forbuffer processing-type))
  16503. (if overlays
  16504. (progn
  16505. (mapc (lambda (o)
  16506. (if (eq (overlay-get o 'org-overlay-type)
  16507. 'org-latex-overlay)
  16508. (delete-overlay o)))
  16509. (overlays-in beg end))
  16510. (setq ov (make-overlay beg end))
  16511. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  16512. (if (featurep 'xemacs)
  16513. (progn
  16514. (overlay-put ov 'invisible t)
  16515. (overlay-put
  16516. ov 'end-glyph
  16517. (make-glyph (vector 'png :file movefile))))
  16518. (overlay-put
  16519. ov 'display
  16520. (list 'image :type 'png :file movefile :ascent 'center)))
  16521. (push ov org-latex-fragment-image-overlays)
  16522. (goto-char end))
  16523. (delete-region beg end)
  16524. (insert (org-add-props link
  16525. (list 'org-latex-src
  16526. (replace-regexp-in-string
  16527. "\"" "" txt)
  16528. 'org-latex-src-embed-type
  16529. (if block-type 'paragraph 'character))))))
  16530. ((eq processing-type 'mathml)
  16531. ;; Process to MathML
  16532. (unless (save-match-data (org-format-latex-mathml-available-p))
  16533. (error "LaTeX to MathML converter not configured"))
  16534. (setq txt (match-string n)
  16535. beg (match-beginning n) end (match-end n)
  16536. cnt (1+ cnt))
  16537. (if msg (message msg cnt))
  16538. (goto-char beg)
  16539. (delete-region beg end)
  16540. (insert (org-format-latex-as-mathml
  16541. txt block-type prefix dir)))
  16542. (t
  16543. (error "Unknown conversion type %s for latex fragments"
  16544. processing-type)))))))))
  16545. (defun org-create-math-formula (latex-frag &optional mathml-file)
  16546. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  16547. Use `org-latex-to-mathml-convert-command'. If the conversion is
  16548. sucessful, return the portion between \"<math...> </math>\"
  16549. elements otherwise return nil. When MATHML-FILE is specified,
  16550. write the results in to that file. When invoked as an
  16551. interactive command, prompt for LATEX-FRAG, with initial value
  16552. set to the current active region and echo the results for user
  16553. inspection."
  16554. (interactive (list (let ((frag (when (org-region-active-p)
  16555. (buffer-substring-no-properties
  16556. (region-beginning) (region-end)))))
  16557. (read-string "LaTeX Fragment: " frag nil frag))))
  16558. (unless latex-frag (error "Invalid latex-frag"))
  16559. (let* ((tmp-in-file (file-relative-name
  16560. (make-temp-name (expand-file-name "ltxmathml-in"))))
  16561. (ignore (write-region latex-frag nil tmp-in-file))
  16562. (tmp-out-file (file-relative-name
  16563. (make-temp-name (expand-file-name "ltxmathml-out"))))
  16564. (cmd (format-spec
  16565. org-latex-to-mathml-convert-command
  16566. `((?j . ,(shell-quote-argument
  16567. (expand-file-name org-latex-to-mathml-jar-file)))
  16568. (?I . ,(shell-quote-argument tmp-in-file))
  16569. (?o . ,(shell-quote-argument tmp-out-file)))))
  16570. mathml shell-command-output)
  16571. (when (org-called-interactively-p 'any)
  16572. (unless (org-format-latex-mathml-available-p)
  16573. (error "LaTeX to MathML converter not configured")))
  16574. (message "Running %s" cmd)
  16575. (setq shell-command-output (shell-command-to-string cmd))
  16576. (setq mathml
  16577. (when (file-readable-p tmp-out-file)
  16578. (with-current-buffer (find-file-noselect tmp-out-file t)
  16579. (goto-char (point-min))
  16580. (when (re-search-forward
  16581. (concat
  16582. (regexp-quote
  16583. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
  16584. "\\(.\\|\n\\)*"
  16585. (regexp-quote "</math>")) nil t)
  16586. (prog1 (match-string 0) (kill-buffer))))))
  16587. (cond
  16588. (mathml
  16589. (setq mathml
  16590. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  16591. (when mathml-file
  16592. (write-region mathml nil mathml-file))
  16593. (when (org-called-interactively-p 'any)
  16594. (message mathml)))
  16595. ((message "LaTeX to MathML conversion failed")
  16596. (message shell-command-output)))
  16597. (delete-file tmp-in-file)
  16598. (when (file-exists-p tmp-out-file)
  16599. (delete-file tmp-out-file))
  16600. mathml))
  16601. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  16602. prefix &optional dir)
  16603. "Use `org-create-math-formula' but check local cache first."
  16604. (let* ((absprefix (expand-file-name prefix dir))
  16605. (print-length nil) (print-level nil)
  16606. (formula-id (concat
  16607. "formula-"
  16608. (sha1
  16609. (prin1-to-string
  16610. (list latex-frag
  16611. org-latex-to-mathml-convert-command)))))
  16612. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  16613. (formula-cache-dir (file-name-directory formula-cache)))
  16614. (unless (file-directory-p formula-cache-dir)
  16615. (make-directory formula-cache-dir t))
  16616. (unless (file-exists-p formula-cache)
  16617. (org-create-math-formula latex-frag formula-cache))
  16618. (if (file-exists-p formula-cache)
  16619. ;; Successful conversion. Return the link to MathML file.
  16620. (org-add-props
  16621. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  16622. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  16623. 'org-latex-src-embed-type (if latex-frag-type
  16624. 'paragraph 'character)))
  16625. ;; Failed conversion. Return the LaTeX fragment verbatim
  16626. latex-frag)))
  16627. (defun org-create-formula-image (string tofile options buffer &optional type)
  16628. "Create an image from LaTeX source using dvipng or convert.
  16629. This function calls either `org-create-formula-image-with-dvipng'
  16630. or `org-create-formula-image-with-imagemagick' depending on the
  16631. value of `org-latex-create-formula-image-program' or on the value
  16632. of the optional TYPE variable.
  16633. Note: ultimately these two function should be combined as they
  16634. share a good deal of logic."
  16635. (org-check-external-command
  16636. "latex" "needed to convert LaTeX fragments to images")
  16637. (funcall
  16638. (case (or type org-latex-create-formula-image-program)
  16639. ('dvipng
  16640. (org-check-external-command
  16641. "dvipng" "needed to convert LaTeX fragments to images")
  16642. #'org-create-formula-image-with-dvipng)
  16643. ('imagemagick
  16644. (org-check-external-command
  16645. "convert" "you need to install imagemagick")
  16646. #'org-create-formula-image-with-imagemagick)
  16647. (t (error
  16648. "invalid value of `org-latex-create-formula-image-program'")))
  16649. string tofile options buffer))
  16650. (declare-function org-export--get-global-options "ox" (&optional backend))
  16651. (declare-function org-export--get-inbuffer-options "ox" (&optional backend))
  16652. (defun org-create-formula--latex-header ()
  16653. "Return LaTeX header appropriate for previewing a LaTeX snippet."
  16654. (org-latex-guess-inputenc
  16655. (org-splice-latex-header
  16656. org-format-latex-header
  16657. org-latex-default-packages-alist
  16658. org-latex-packages-alist t
  16659. (plist-get
  16660. (org-combine-plists
  16661. (org-export--get-global-options 'latex)
  16662. (org-export--get-inbuffer-options 'latex))
  16663. :latex-header-extra))))
  16664. ;; This function borrows from Ganesh Swami's latex2png.el
  16665. (defun org-create-formula-image-with-dvipng (string tofile options buffer)
  16666. "This calls dvipng."
  16667. (require 'ox-latex)
  16668. (let* ((tmpdir (if (featurep 'xemacs)
  16669. (temp-directory)
  16670. temporary-file-directory))
  16671. (texfilebase (make-temp-name
  16672. (expand-file-name "orgtex" tmpdir)))
  16673. (texfile (concat texfilebase ".tex"))
  16674. (dvifile (concat texfilebase ".dvi"))
  16675. (pngfile (concat texfilebase ".png"))
  16676. (fnh (if (featurep 'xemacs)
  16677. (font-height (face-font 'default))
  16678. (face-attribute 'default :height nil)))
  16679. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  16680. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  16681. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  16682. "Black"))
  16683. (bg (or (plist-get options (if buffer :background :html-background))
  16684. "Transparent")))
  16685. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground))
  16686. (unless (string= fg "Transparent") (setq fg (org-dvipng-color-format fg))))
  16687. (if (eq bg 'default) (setq bg (org-dvipng-color :background))
  16688. (unless (string= bg "Transparent") (setq bg (org-dvipng-color-format bg))))
  16689. (let ((latex-header (org-create-formula--latex-header)))
  16690. (with-temp-file texfile
  16691. (insert latex-header)
  16692. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")))
  16693. (let ((dir default-directory))
  16694. (condition-case nil
  16695. (progn
  16696. (cd tmpdir)
  16697. (call-process "latex" nil nil nil texfile))
  16698. (error nil))
  16699. (cd dir))
  16700. (if (not (file-exists-p dvifile))
  16701. (progn (message "Failed to create dvi file from %s" texfile) nil)
  16702. (condition-case nil
  16703. (if (featurep 'xemacs)
  16704. (call-process "dvipng" nil nil nil
  16705. "-fg" fg "-bg" bg
  16706. "-T" "tight"
  16707. "-o" pngfile
  16708. dvifile)
  16709. (call-process "dvipng" nil nil nil
  16710. "-fg" fg "-bg" bg
  16711. "-D" dpi
  16712. ;;"-x" scale "-y" scale
  16713. "-T" "tight"
  16714. "-o" pngfile
  16715. dvifile))
  16716. (error nil))
  16717. (if (not (file-exists-p pngfile))
  16718. (if org-format-latex-signal-error
  16719. (error "Failed to create png file from %s" texfile)
  16720. (message "Failed to create png file from %s" texfile)
  16721. nil)
  16722. ;; Use the requested file name and clean up
  16723. (copy-file pngfile tofile 'replace)
  16724. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png" ".out") do
  16725. (if (file-exists-p (concat texfilebase e))
  16726. (delete-file (concat texfilebase e))))
  16727. pngfile))))
  16728. (declare-function org-latex-compile "ox-latex" (texfile &optional snippet))
  16729. (defun org-create-formula-image-with-imagemagick (string tofile options buffer)
  16730. "This calls convert, which is included into imagemagick."
  16731. (require 'ox-latex)
  16732. (let* ((tmpdir (if (featurep 'xemacs)
  16733. (temp-directory)
  16734. temporary-file-directory))
  16735. (texfilebase (make-temp-name
  16736. (expand-file-name "orgtex" tmpdir)))
  16737. (texfile (concat texfilebase ".tex"))
  16738. (pdffile (concat texfilebase ".pdf"))
  16739. (pngfile (concat texfilebase ".png"))
  16740. (fnh (if (featurep 'xemacs)
  16741. (font-height (face-font 'default))
  16742. (face-attribute 'default :height nil)))
  16743. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  16744. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  16745. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  16746. "black"))
  16747. (bg (or (plist-get options (if buffer :background :html-background))
  16748. "white")))
  16749. (if (eq fg 'default) (setq fg (org-latex-color :foreground))
  16750. (setq fg (org-latex-color-format fg)))
  16751. (if (eq bg 'default) (setq bg (org-latex-color :background))
  16752. (setq bg (org-latex-color-format
  16753. (if (string= bg "Transparent") "white" bg))))
  16754. (let ((latex-header (org-create-formula--latex-header)))
  16755. (with-temp-file texfile
  16756. (insert latex-header)
  16757. (insert "\n\\begin{document}\n"
  16758. "\\definecolor{fg}{rgb}{" fg "}\n"
  16759. "\\definecolor{bg}{rgb}{" bg "}\n"
  16760. "\n\\pagecolor{bg}\n"
  16761. "\n{\\color{fg}\n"
  16762. string
  16763. "\n}\n"
  16764. "\n\\end{document}\n")))
  16765. (org-latex-compile texfile t)
  16766. (if (not (file-exists-p pdffile))
  16767. (progn (message "Failed to create pdf file from %s" texfile) nil)
  16768. (condition-case nil
  16769. (if (featurep 'xemacs)
  16770. (call-process "convert" nil nil nil
  16771. "-density" "96"
  16772. "-trim"
  16773. "-antialias"
  16774. pdffile
  16775. "-quality" "100"
  16776. ;; "-sharpen" "0x1.0"
  16777. pngfile)
  16778. (call-process "convert" nil nil nil
  16779. "-density" dpi
  16780. "-trim"
  16781. "-antialias"
  16782. pdffile
  16783. "-quality" "100"
  16784. ;; "-sharpen" "0x1.0"
  16785. pngfile))
  16786. (error nil))
  16787. (if (not (file-exists-p pngfile))
  16788. (if org-format-latex-signal-error
  16789. (error "Failed to create png file from %s" texfile)
  16790. (message "Failed to create png file from %s" texfile)
  16791. nil)
  16792. ;; Use the requested file name and clean up
  16793. (copy-file pngfile tofile 'replace)
  16794. (loop for e in '(".pdf" ".tex" ".aux" ".log" ".png") do
  16795. (if (file-exists-p (concat texfilebase e))
  16796. (delete-file (concat texfilebase e))))
  16797. pngfile))))
  16798. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  16799. "Fill a LaTeX header template TPL.
  16800. In the template, the following place holders will be recognized:
  16801. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  16802. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  16803. [PACKAGES] \\usepackage statements for PKG
  16804. [NO-PACKAGES] do not include PKG
  16805. [EXTRA] the string EXTRA
  16806. [NO-EXTRA] do not include EXTRA
  16807. For backward compatibility, if both the positive and the negative place
  16808. holder is missing, the positive one (without the \"NO-\") will be
  16809. assumed to be present at the end of the template.
  16810. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  16811. EXTRA is a string.
  16812. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  16813. (let (rpl (end ""))
  16814. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  16815. (setq rpl (if (or (match-end 1) (not def-pkg))
  16816. "" (org-latex-packages-to-string def-pkg snippets-p t))
  16817. tpl (replace-match rpl t t tpl))
  16818. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  16819. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  16820. (setq rpl (if (or (match-end 1) (not pkg))
  16821. "" (org-latex-packages-to-string pkg snippets-p t))
  16822. tpl (replace-match rpl t t tpl))
  16823. (if pkg (setq end
  16824. (concat end "\n"
  16825. (org-latex-packages-to-string pkg snippets-p)))))
  16826. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  16827. (setq rpl (if (or (match-end 1) (not extra))
  16828. "" (concat extra "\n"))
  16829. tpl (replace-match rpl t t tpl))
  16830. (if (and extra (string-match "\\S-" extra))
  16831. (setq end (concat end "\n" extra))))
  16832. (if (string-match "\\S-" end)
  16833. (concat tpl "\n" end)
  16834. tpl)))
  16835. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  16836. "Turn an alist of packages into a string with the \\usepackage macros."
  16837. (setq pkg (mapconcat (lambda(p)
  16838. (cond
  16839. ((stringp p) p)
  16840. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  16841. (format "%% Package %s omitted" (cadr p)))
  16842. ((equal "" (car p))
  16843. (format "\\usepackage{%s}" (cadr p)))
  16844. (t
  16845. (format "\\usepackage[%s]{%s}"
  16846. (car p) (cadr p)))))
  16847. pkg
  16848. "\n"))
  16849. (if newline (concat pkg "\n") pkg))
  16850. (defun org-dvipng-color (attr)
  16851. "Return a RGB color specification for dvipng."
  16852. (apply 'format "rgb %s %s %s"
  16853. (mapcar 'org-normalize-color
  16854. (if (featurep 'xemacs)
  16855. (color-rgb-components
  16856. (face-property 'default
  16857. (cond ((eq attr :foreground) 'foreground)
  16858. ((eq attr :background) 'background))))
  16859. (color-values (face-attribute 'default attr nil))))))
  16860. (defun org-dvipng-color-format (color-name)
  16861. "Convert COLOR-NAME to a RGB color value for dvipng."
  16862. (apply 'format "rgb %s %s %s"
  16863. (mapcar 'org-normalize-color
  16864. (color-values color-name))))
  16865. (defun org-latex-color (attr)
  16866. "Return a RGB color for the LaTeX color package."
  16867. (apply 'format "%s,%s,%s"
  16868. (mapcar 'org-normalize-color
  16869. (if (featurep 'xemacs)
  16870. (color-rgb-components
  16871. (face-property 'default
  16872. (cond ((eq attr :foreground) 'foreground)
  16873. ((eq attr :background) 'background))))
  16874. (color-values (face-attribute 'default attr nil))))))
  16875. (defun org-latex-color-format (color-name)
  16876. "Convert COLOR-NAME to a RGB color value."
  16877. (apply 'format "%s,%s,%s"
  16878. (mapcar 'org-normalize-color
  16879. (color-values color-name))))
  16880. (defun org-normalize-color (value)
  16881. "Return string to be used as color value for an RGB component."
  16882. (format "%g" (/ value 65535.0)))
  16883. ;; Image display
  16884. (defvar org-inline-image-overlays nil)
  16885. (make-variable-buffer-local 'org-inline-image-overlays)
  16886. (defun org-toggle-inline-images (&optional include-linked)
  16887. "Toggle the display of inline images.
  16888. INCLUDE-LINKED is passed to `org-display-inline-images'."
  16889. (interactive "P")
  16890. (if org-inline-image-overlays
  16891. (progn
  16892. (org-remove-inline-images)
  16893. (message "Inline image display turned off"))
  16894. (org-display-inline-images include-linked)
  16895. (if (and (org-called-interactively-p)
  16896. org-inline-image-overlays)
  16897. (message "%d images displayed inline"
  16898. (length org-inline-image-overlays))
  16899. (message "No images to display inline"))))
  16900. (defun org-redisplay-inline-images ()
  16901. "Refresh the display of inline images."
  16902. (interactive)
  16903. (if (not org-inline-image-overlays)
  16904. (org-toggle-inline-images)
  16905. (org-toggle-inline-images)
  16906. (org-toggle-inline-images)))
  16907. (defun org-display-inline-images (&optional include-linked refresh beg end)
  16908. "Display inline images.
  16909. Normally only links without a description part are inlined, because this
  16910. is how it will work for export. When INCLUDE-LINKED is set, also links
  16911. with a description part will be inlined. This can be nice for a quick
  16912. look at those images, but it does not reflect what exported files will look
  16913. like.
  16914. When REFRESH is set, refresh existing images between BEG and END.
  16915. This will create new image displays only if necessary.
  16916. BEG and END default to the buffer boundaries."
  16917. (interactive "P")
  16918. (unless refresh
  16919. (org-remove-inline-images)
  16920. (if (fboundp 'clear-image-cache) (clear-image-cache)))
  16921. (save-excursion
  16922. (save-restriction
  16923. (widen)
  16924. (setq beg (or beg (point-min)) end (or end (point-max)))
  16925. (goto-char beg)
  16926. (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
  16927. (substring (org-image-file-name-regexp) 0 -2)
  16928. "\\)\\]" (if include-linked "" "\\]")))
  16929. old file ov img type attrwidth width)
  16930. (while (re-search-forward re end t)
  16931. (setq old (get-char-property-and-overlay (match-beginning 1)
  16932. 'org-image-overlay)
  16933. file (expand-file-name
  16934. (concat (or (match-string 3) "") (match-string 4))))
  16935. (when (image-type-available-p 'imagemagick)
  16936. (setq attrwidth (if (or (listp org-image-actual-width)
  16937. (null org-image-actual-width))
  16938. (save-excursion
  16939. (save-match-data
  16940. (when (re-search-backward
  16941. "#\\+ATTR.*width=\"\\([^\"]+\\)\""
  16942. (save-excursion
  16943. (re-search-backward "^[ \t]*$\\|\\`" nil t)) t)
  16944. (string-to-number (match-string 1))))))
  16945. width (cond ((eq org-image-actual-width t) nil)
  16946. ((null org-image-actual-width) attrwidth)
  16947. ((numberp org-image-actual-width)
  16948. org-image-actual-width)
  16949. ((listp org-image-actual-width)
  16950. (or attrwidth (car org-image-actual-width))))
  16951. type (if width 'imagemagick)))
  16952. (when (file-exists-p file)
  16953. (if (and (car-safe old) refresh)
  16954. (image-refresh (overlay-get (cdr old) 'display))
  16955. (setq img (save-match-data (create-image file type nil :width width)))
  16956. (when img
  16957. (setq ov (make-overlay (match-beginning 0) (match-end 0)))
  16958. (overlay-put ov 'display img)
  16959. (overlay-put ov 'face 'default)
  16960. (overlay-put ov 'org-image-overlay t)
  16961. (overlay-put ov 'modification-hooks
  16962. (list 'org-display-inline-remove-overlay))
  16963. (push ov org-inline-image-overlays)))))))))
  16964. (define-obsolete-function-alias
  16965. 'org-display-inline-modification-hook 'org-display-inline-remove-overlay "24.3")
  16966. (defun org-display-inline-remove-overlay (ov after beg end &optional len)
  16967. "Remove inline-display overlay if a corresponding region is modified."
  16968. (let ((inhibit-modification-hooks t))
  16969. (when (and ov after)
  16970. (delete ov org-inline-image-overlays)
  16971. (delete-overlay ov))))
  16972. (defun org-remove-inline-images ()
  16973. "Remove inline display of images."
  16974. (interactive)
  16975. (mapc 'delete-overlay org-inline-image-overlays)
  16976. (setq org-inline-image-overlays nil))
  16977. ;;;; Key bindings
  16978. ;; Outline functions from `outline-mode-prefix-map'
  16979. ;; that can be remapped in Org:
  16980. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  16981. (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
  16982. (define-key org-mode-map [remap outline-forward-same-level]
  16983. 'org-forward-heading-same-level)
  16984. (define-key org-mode-map [remap outline-backward-same-level]
  16985. 'org-backward-heading-same-level)
  16986. (define-key org-mode-map [remap show-branches]
  16987. 'org-kill-note-or-show-branches)
  16988. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  16989. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  16990. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  16991. ;; Outline functions from `outline-mode-prefix-map' that can not
  16992. ;; be remapped in Org:
  16993. ;;
  16994. ;; - the column "key binding" shows whether the Outline function is still
  16995. ;; available in Org mode on the same key that it has been bound to in
  16996. ;; Outline mode:
  16997. ;; - "overridden": key used for a different functionality in Org mode
  16998. ;; - else: key still bound to the same Outline function in Org mode
  16999. ;;
  17000. ;; | Outline function | key binding | Org replacement |
  17001. ;; |------------------------------------+-------------+-----------------------|
  17002. ;; | `outline-next-visible-heading' | `C-c C-n' | still same function |
  17003. ;; | `outline-previous-visible-heading' | `C-c C-p' | still same function |
  17004. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  17005. ;; | `outline-move-subtree-up' | overridden | better: org-shiftup |
  17006. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  17007. ;; | `show-entry' | overridden | no replacement |
  17008. ;; | `show-children' | `C-c C-i' | visibility cycling |
  17009. ;; | `show-branches' | `C-c C-k' | still same function |
  17010. ;; | `show-subtree' | overridden | visibility cycling |
  17011. ;; | `show-all' | overridden | no replacement |
  17012. ;; | `hide-subtree' | overridden | visibility cycling |
  17013. ;; | `hide-body' | overridden | no replacement |
  17014. ;; | `hide-entry' | overridden | visibility cycling |
  17015. ;; | `hide-leaves' | overridden | no replacement |
  17016. ;; | `hide-sublevels' | overridden | no replacement |
  17017. ;; | `hide-other' | overridden | no replacement |
  17018. ;; Make `C-c C-x' a prefix key
  17019. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  17020. ;; TAB key with modifiers
  17021. (org-defkey org-mode-map "\C-i" 'org-cycle)
  17022. (org-defkey org-mode-map [(tab)] 'org-cycle)
  17023. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  17024. (org-defkey org-mode-map "\M-\t" 'pcomplete)
  17025. ;; The following line is necessary under Suse GNU/Linux
  17026. (unless (featurep 'xemacs)
  17027. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  17028. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  17029. (define-key org-mode-map [backtab] 'org-shifttab)
  17030. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  17031. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  17032. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  17033. ;; Cursor keys with modifiers
  17034. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  17035. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  17036. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  17037. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  17038. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  17039. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  17040. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  17041. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  17042. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  17043. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  17044. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  17045. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  17046. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  17047. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  17048. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  17049. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  17050. ;; Babel keys
  17051. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  17052. (mapc (lambda (pair)
  17053. (define-key org-babel-map (car pair) (cdr pair)))
  17054. org-babel-key-bindings)
  17055. ;;; Extra keys for tty access.
  17056. ;; We only set them when really needed because otherwise the
  17057. ;; menus don't show the simple keys
  17058. (when (or org-use-extra-keys
  17059. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  17060. (not window-system))
  17061. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  17062. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  17063. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  17064. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  17065. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  17066. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  17067. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  17068. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  17069. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  17070. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  17071. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  17072. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  17073. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  17074. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  17075. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  17076. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  17077. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  17078. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  17079. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  17080. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  17081. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  17082. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  17083. (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
  17084. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  17085. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  17086. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  17087. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  17088. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  17089. ;; All the other keys
  17090. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  17091. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  17092. (if (boundp 'narrow-map)
  17093. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  17094. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  17095. (if (boundp 'narrow-map)
  17096. (org-defkey narrow-map "b" 'org-narrow-to-block)
  17097. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  17098. (if (boundp 'narrow-map)
  17099. (org-defkey narrow-map "e" 'org-narrow-to-element)
  17100. (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
  17101. (org-defkey org-mode-map "\C-\M-t" 'org-transpose-element)
  17102. (org-defkey org-mode-map "\M-}" 'org-forward-element)
  17103. (org-defkey org-mode-map "\M-{" 'org-backward-element)
  17104. (org-defkey org-mode-map "\C-c\C-^" 'org-up-element)
  17105. (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
  17106. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
  17107. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
  17108. (org-defkey org-mode-map "\C-c\M-f" 'org-next-block)
  17109. (org-defkey org-mode-map "\C-c\M-b" 'org-previous-block)
  17110. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  17111. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  17112. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  17113. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  17114. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  17115. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  17116. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  17117. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  17118. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  17119. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  17120. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  17121. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  17122. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  17123. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  17124. (org-defkey org-mode-map "\C-c\M-w" 'org-copy)
  17125. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  17126. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  17127. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  17128. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  17129. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  17130. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  17131. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  17132. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  17133. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  17134. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  17135. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  17136. (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
  17137. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  17138. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  17139. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  17140. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  17141. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  17142. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  17143. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  17144. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  17145. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  17146. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  17147. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  17148. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  17149. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  17150. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  17151. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  17152. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  17153. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  17154. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  17155. (org-defkey org-mode-map "\C-c^" 'org-sort)
  17156. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  17157. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  17158. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  17159. (org-defkey org-mode-map "\C-m" 'org-return)
  17160. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  17161. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  17162. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  17163. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  17164. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  17165. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  17166. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  17167. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  17168. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  17169. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  17170. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  17171. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  17172. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  17173. (org-defkey org-mode-map "\C-c\C-e" 'org-export-dispatch)
  17174. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  17175. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  17176. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  17177. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  17178. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  17179. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  17180. (org-defkey org-mode-map "\M-h" 'org-mark-element)
  17181. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  17182. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  17183. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  17184. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  17185. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  17186. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  17187. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  17188. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
  17189. (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
  17190. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  17191. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  17192. (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
  17193. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  17194. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  17195. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  17196. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  17197. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  17198. (org-defkey org-mode-map "\C-c\C-x\C-\M-v" 'org-redisplay-inline-images)
  17199. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  17200. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  17201. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  17202. (org-defkey org-mode-map "\C-c\C-xP" 'org-set-property-and-value)
  17203. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  17204. (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
  17205. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  17206. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  17207. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  17208. (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
  17209. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  17210. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  17211. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  17212. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  17213. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  17214. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  17215. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  17216. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  17217. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  17218. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  17219. (when (featurep 'xemacs)
  17220. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  17221. (defconst org-speed-commands-default
  17222. '(
  17223. ("Outline Navigation")
  17224. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  17225. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  17226. ("f" . (org-speed-move-safe 'org-forward-heading-same-level))
  17227. ("b" . (org-speed-move-safe 'org-backward-heading-same-level))
  17228. ("F" . org-next-block)
  17229. ("B" . org-previous-block)
  17230. ("u" . (org-speed-move-safe 'outline-up-heading))
  17231. ("j" . org-goto)
  17232. ("g" . (org-refile t))
  17233. ("Outline Visibility")
  17234. ("c" . org-cycle)
  17235. ("C" . org-shifttab)
  17236. (" " . org-display-outline-path)
  17237. ("=" . org-columns)
  17238. ("Outline Structure Editing")
  17239. ("U" . org-shiftmetaup)
  17240. ("D" . org-shiftmetadown)
  17241. ("r" . org-metaright)
  17242. ("l" . org-metaleft)
  17243. ("R" . org-shiftmetaright)
  17244. ("L" . org-shiftmetaleft)
  17245. ("i" . (progn (forward-char 1) (call-interactively
  17246. 'org-insert-heading-respect-content)))
  17247. ("^" . org-sort)
  17248. ("w" . org-refile)
  17249. ("a" . org-archive-subtree-default-with-confirmation)
  17250. ("@" . org-mark-subtree)
  17251. ("#" . org-toggle-comment)
  17252. ("Clock Commands")
  17253. ("I" . org-clock-in)
  17254. ("O" . org-clock-out)
  17255. ("Meta Data Editing")
  17256. ("t" . org-todo)
  17257. ("," . (org-priority))
  17258. ("0" . (org-priority ?\ ))
  17259. ("1" . (org-priority ?A))
  17260. ("2" . (org-priority ?B))
  17261. ("3" . (org-priority ?C))
  17262. (":" . org-set-tags-command)
  17263. ("e" . org-set-effort)
  17264. ("E" . org-inc-effort)
  17265. ("W" . (lambda(m) (interactive "sMinutes before warning: ")
  17266. (org-entry-put (point) "APPT_WARNTIME" m)))
  17267. ("Agenda Views etc")
  17268. ("v" . org-agenda)
  17269. ("/" . org-sparse-tree)
  17270. ("Misc")
  17271. ("o" . org-open-at-point)
  17272. ("?" . org-speed-command-help)
  17273. ("<" . (org-agenda-set-restriction-lock 'subtree))
  17274. (">" . (org-agenda-remove-restriction-lock))
  17275. )
  17276. "The default speed commands.")
  17277. (defun org-print-speed-command (e)
  17278. (if (> (length (car e)) 1)
  17279. (progn
  17280. (princ "\n")
  17281. (princ (car e))
  17282. (princ "\n")
  17283. (princ (make-string (length (car e)) ?-))
  17284. (princ "\n"))
  17285. (princ (car e))
  17286. (princ " ")
  17287. (if (symbolp (cdr e))
  17288. (princ (symbol-name (cdr e)))
  17289. (prin1 (cdr e)))
  17290. (princ "\n")))
  17291. (defun org-speed-command-help ()
  17292. "Show the available speed commands."
  17293. (interactive)
  17294. (if (not org-use-speed-commands)
  17295. (error "Speed commands are not activated, customize `org-use-speed-commands'")
  17296. (with-output-to-temp-buffer "*Help*"
  17297. (princ "User-defined Speed commands\n===========================\n")
  17298. (mapc 'org-print-speed-command org-speed-commands-user)
  17299. (princ "\n")
  17300. (princ "Built-in Speed commands\n=======================\n")
  17301. (mapc 'org-print-speed-command org-speed-commands-default))
  17302. (with-current-buffer "*Help*"
  17303. (setq truncate-lines t))))
  17304. (defun org-speed-move-safe (cmd)
  17305. "Execute CMD, but make sure that the cursor always ends up in a headline.
  17306. If not, return to the original position and throw an error."
  17307. (interactive)
  17308. (let ((pos (point)))
  17309. (call-interactively cmd)
  17310. (unless (and (bolp) (org-at-heading-p))
  17311. (goto-char pos)
  17312. (error "Boundary reached while executing %s" cmd))))
  17313. (defvar org-self-insert-command-undo-counter 0)
  17314. (defvar org-table-auto-blank-field) ; defined in org-table.el
  17315. (defvar org-speed-command nil)
  17316. (define-obsolete-function-alias
  17317. 'org-speed-command-default-hook 'org-speed-command-activate "24.3")
  17318. (defun org-speed-command-activate (keys)
  17319. "Hook for activating single-letter speed commands.
  17320. `org-speed-commands-default' specifies a minimal command set.
  17321. Use `org-speed-commands-user' for further customization."
  17322. (when (or (and (bolp) (looking-at org-outline-regexp))
  17323. (and (functionp org-use-speed-commands)
  17324. (funcall org-use-speed-commands)))
  17325. (cdr (assoc keys (append org-speed-commands-user
  17326. org-speed-commands-default)))))
  17327. (define-obsolete-function-alias
  17328. 'org-babel-speed-command-hook 'org-babel-speed-command-activate "24.3")
  17329. (defun org-babel-speed-command-activate (keys)
  17330. "Hook for activating single-letter code block commands."
  17331. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  17332. (cdr (assoc keys org-babel-key-bindings))))
  17333. (defcustom org-speed-command-hook
  17334. '(org-speed-command-default-hook org-babel-speed-command-hook)
  17335. "Hook for activating speed commands at strategic locations.
  17336. Hook functions are called in sequence until a valid handler is
  17337. found.
  17338. Each hook takes a single argument, a user-pressed command key
  17339. which is also a `self-insert-command' from the global map.
  17340. Within the hook, examine the cursor position and the command key
  17341. and return nil or a valid handler as appropriate. Handler could
  17342. be one of an interactive command, a function, or a form.
  17343. Set `org-use-speed-commands' to non-nil value to enable this
  17344. hook. The default setting is `org-speed-command-activate'."
  17345. :group 'org-structure
  17346. :version "24.1"
  17347. :type 'hook)
  17348. (defun org-self-insert-command (N)
  17349. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  17350. If the cursor is in a table looking at whitespace, the whitespace is
  17351. overwritten, and the table is not marked as requiring realignment."
  17352. (interactive "p")
  17353. (org-check-before-invisible-edit 'insert)
  17354. (cond
  17355. ((and org-use-speed-commands
  17356. (setq org-speed-command
  17357. (run-hook-with-args-until-success
  17358. 'org-speed-command-hook (this-command-keys))))
  17359. (cond
  17360. ((commandp org-speed-command)
  17361. (setq this-command org-speed-command)
  17362. (call-interactively org-speed-command))
  17363. ((functionp org-speed-command)
  17364. (funcall org-speed-command))
  17365. ((and org-speed-command (listp org-speed-command))
  17366. (eval org-speed-command))
  17367. (t (let (org-use-speed-commands)
  17368. (call-interactively 'org-self-insert-command)))))
  17369. ((and
  17370. (org-table-p)
  17371. (progn
  17372. ;; check if we blank the field, and if that triggers align
  17373. (and (featurep 'org-table) org-table-auto-blank-field
  17374. (member last-command
  17375. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  17376. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  17377. ;; got extra space, this field does not determine column width
  17378. (let (org-table-may-need-update) (org-table-blank-field))
  17379. ;; no extra space, this field may determine column width
  17380. (org-table-blank-field)))
  17381. t)
  17382. (eq N 1)
  17383. (looking-at "[^|\n]* |"))
  17384. (let (org-table-may-need-update)
  17385. (goto-char (1- (match-end 0)))
  17386. (backward-delete-char 1)
  17387. (goto-char (match-beginning 0))
  17388. (self-insert-command N)))
  17389. (t
  17390. (setq org-table-may-need-update t)
  17391. (self-insert-command N)
  17392. (org-fix-tags-on-the-fly)
  17393. (if org-self-insert-cluster-for-undo
  17394. (if (not (eq last-command 'org-self-insert-command))
  17395. (setq org-self-insert-command-undo-counter 1)
  17396. (if (>= org-self-insert-command-undo-counter 20)
  17397. (setq org-self-insert-command-undo-counter 1)
  17398. (and (> org-self-insert-command-undo-counter 0)
  17399. buffer-undo-list (listp buffer-undo-list)
  17400. (not (cadr buffer-undo-list)) ; remove nil entry
  17401. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  17402. (setq org-self-insert-command-undo-counter
  17403. (1+ org-self-insert-command-undo-counter))))))))
  17404. (defun org-check-before-invisible-edit (kind)
  17405. "Check is editing if kind KIND would be dangerous with invisible text around.
  17406. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  17407. ;; First, try to get out of here as quickly as possible, to reduce overhead
  17408. (if (and org-catch-invisible-edits
  17409. (or (not (boundp 'visible-mode)) (not visible-mode))
  17410. (or (get-char-property (point) 'invisible)
  17411. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  17412. ;; OK, we need to take a closer look
  17413. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  17414. (invisible-before-point (if (bobp) nil (get-char-property
  17415. (1- (point)) 'invisible)))
  17416. (border-and-ok-direction
  17417. (or
  17418. ;; Check if we are acting predictably before invisible text
  17419. (and invisible-at-point (not invisible-before-point)
  17420. (memq kind '(insert delete-backward)))
  17421. ;; Check if we are acting predictably after invisible text
  17422. ;; This works not well, and I have turned it off. It seems
  17423. ;; better to always show and stop after invisible text.
  17424. ;; (and (not invisible-at-point) invisible-before-point
  17425. ;; (memq kind '(insert delete)))
  17426. )))
  17427. (when (or (memq invisible-at-point '(outline org-hide-block t))
  17428. (memq invisible-before-point '(outline org-hide-block t)))
  17429. (if (eq org-catch-invisible-edits 'error)
  17430. (error "Editing in invisible areas is prohibited - make visible first"))
  17431. (if (and org-custom-properties-overlays
  17432. (y-or-n-p "Display invisible properties in this buffer? "))
  17433. (org-toggle-custom-properties-visibility)
  17434. ;; Make the area visible
  17435. (save-excursion
  17436. (if invisible-before-point
  17437. (goto-char (previous-single-char-property-change
  17438. (point) 'invisible)))
  17439. (org-cycle))
  17440. (cond
  17441. ((eq org-catch-invisible-edits 'show)
  17442. ;; That's it, we do the edit after showing
  17443. (message
  17444. "Unfolding invisible region around point before editing")
  17445. (sit-for 1))
  17446. ((and (eq org-catch-invisible-edits 'smart)
  17447. border-and-ok-direction)
  17448. (message "Unfolding invisible region around point before editing"))
  17449. (t
  17450. ;; Don't do the edit, make the user repeat it in full visibility
  17451. (error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
  17452. (defun org-fix-tags-on-the-fly ()
  17453. (when (and (equal (char-after (point-at-bol)) ?*)
  17454. (org-at-heading-p))
  17455. (org-align-tags-here org-tags-column)))
  17456. (defun org-delete-backward-char (N)
  17457. "Like `delete-backward-char', insert whitespace at field end in tables.
  17458. When deleting backwards, in tables this function will insert whitespace in
  17459. front of the next \"|\" separator, to keep the table aligned. The table will
  17460. still be marked for re-alignment if the field did fill the entire column,
  17461. because, in this case the deletion might narrow the column."
  17462. (interactive "p")
  17463. (save-match-data
  17464. (org-check-before-invisible-edit 'delete-backward)
  17465. (if (and (org-table-p)
  17466. (eq N 1)
  17467. (string-match "|" (buffer-substring (point-at-bol) (point)))
  17468. (looking-at ".*?|"))
  17469. (let ((pos (point))
  17470. (noalign (looking-at "[^|\n\r]* |"))
  17471. (c org-table-may-need-update))
  17472. (backward-delete-char N)
  17473. (if (not overwrite-mode)
  17474. (progn
  17475. (skip-chars-forward "^|")
  17476. (insert " ")
  17477. (goto-char (1- pos))))
  17478. ;; noalign: if there were two spaces at the end, this field
  17479. ;; does not determine the width of the column.
  17480. (if noalign (setq org-table-may-need-update c)))
  17481. (backward-delete-char N)
  17482. (org-fix-tags-on-the-fly))))
  17483. (defun org-delete-char (N)
  17484. "Like `delete-char', but insert whitespace at field end in tables.
  17485. When deleting characters, in tables this function will insert whitespace in
  17486. front of the next \"|\" separator, to keep the table aligned. The table will
  17487. still be marked for re-alignment if the field did fill the entire column,
  17488. because, in this case the deletion might narrow the column."
  17489. (interactive "p")
  17490. (save-match-data
  17491. (org-check-before-invisible-edit 'delete)
  17492. (if (and (org-table-p)
  17493. (not (bolp))
  17494. (not (= (char-after) ?|))
  17495. (eq N 1))
  17496. (if (looking-at ".*?|")
  17497. (let ((pos (point))
  17498. (noalign (looking-at "[^|\n\r]* |"))
  17499. (c org-table-may-need-update))
  17500. (replace-match (concat
  17501. (substring (match-string 0) 1 -1)
  17502. " |"))
  17503. (goto-char pos)
  17504. ;; noalign: if there were two spaces at the end, this field
  17505. ;; does not determine the width of the column.
  17506. (if noalign (setq org-table-may-need-update c)))
  17507. (delete-char N))
  17508. (delete-char N)
  17509. (org-fix-tags-on-the-fly))))
  17510. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  17511. (put 'org-self-insert-command 'delete-selection t)
  17512. (put 'orgtbl-self-insert-command 'delete-selection t)
  17513. (put 'org-delete-char 'delete-selection 'supersede)
  17514. (put 'org-delete-backward-char 'delete-selection 'supersede)
  17515. (put 'org-yank 'delete-selection 'yank)
  17516. ;; Make `flyspell-mode' delay after some commands
  17517. (put 'org-self-insert-command 'flyspell-delayed t)
  17518. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  17519. (put 'org-delete-char 'flyspell-delayed t)
  17520. (put 'org-delete-backward-char 'flyspell-delayed t)
  17521. ;; Make pabbrev-mode expand after org-mode commands
  17522. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  17523. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  17524. ;; How to do this: Measure non-white length of current string
  17525. ;; If equal to column width, we should realign.
  17526. (defun org-remap (map &rest commands)
  17527. "In MAP, remap the functions given in COMMANDS.
  17528. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  17529. (let (new old)
  17530. (while commands
  17531. (setq old (pop commands) new (pop commands))
  17532. (if (fboundp 'command-remapping)
  17533. (org-defkey map (vector 'remap old) new)
  17534. (substitute-key-definition old new map global-map)))))
  17535. (defun org-transpose-words ()
  17536. "Transpose words for Org.
  17537. This uses the `org-mode-transpose-word-syntax-table' syntax
  17538. table, which interprets characters in `org-emphasis-alist' as
  17539. word constituants."
  17540. (interactive)
  17541. (with-syntax-table org-mode-transpose-word-syntax-table
  17542. (call-interactively 'transpose-words)))
  17543. (org-remap org-mode-map 'transpose-words 'org-transpose-words)
  17544. (when (eq org-enable-table-editor 'optimized)
  17545. ;; If the user wants maximum table support, we need to hijack
  17546. ;; some standard editing functions
  17547. (org-remap org-mode-map
  17548. 'self-insert-command 'org-self-insert-command
  17549. 'delete-char 'org-delete-char
  17550. 'delete-backward-char 'org-delete-backward-char)
  17551. (org-defkey org-mode-map "|" 'org-force-self-insert))
  17552. (defvar org-ctrl-c-ctrl-c-hook nil
  17553. "Hook for functions attaching themselves to `C-c C-c'.
  17554. This can be used to add additional functionality to the C-c C-c
  17555. key which executes context-dependent commands. This hook is run
  17556. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  17557. run after the last test.
  17558. Each function will be called with no arguments. The function
  17559. must check if the context is appropriate for it to act. If yes,
  17560. it should do its thing and then return a non-nil value. If the
  17561. context is wrong, just do nothing and return nil.")
  17562. (defvar org-ctrl-c-ctrl-c-final-hook nil
  17563. "Hook for functions attaching themselves to `C-c C-c'.
  17564. This can be used to add additional functionality to the C-c C-c
  17565. key which executes context-dependent commands. This hook is run
  17566. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  17567. before the first test.
  17568. Each function will be called with no arguments. The function
  17569. must check if the context is appropriate for it to act. If yes,
  17570. it should do its thing and then return a non-nil value. If the
  17571. context is wrong, just do nothing and return nil.")
  17572. (defvar org-tab-first-hook nil
  17573. "Hook for functions to attach themselves to TAB.
  17574. See `org-ctrl-c-ctrl-c-hook' for more information.
  17575. This hook runs as the first action when TAB is pressed, even before
  17576. `org-cycle' messes around with the `outline-regexp' to cater for
  17577. inline tasks and plain list item folding.
  17578. If any function in this hook returns t, any other actions that
  17579. would have been caused by TAB (such as table field motion or visibility
  17580. cycling) will not occur.")
  17581. (defvar org-tab-after-check-for-table-hook nil
  17582. "Hook for functions to attach themselves to TAB.
  17583. See `org-ctrl-c-ctrl-c-hook' for more information.
  17584. This hook runs after it has been established that the cursor is not in a
  17585. table, but before checking if the cursor is in a headline or if global cycling
  17586. should be done.
  17587. If any function in this hook returns t, not other actions like visibility
  17588. cycling will be done.")
  17589. (defvar org-tab-after-check-for-cycling-hook nil
  17590. "Hook for functions to attach themselves to TAB.
  17591. See `org-ctrl-c-ctrl-c-hook' for more information.
  17592. This hook runs after it has been established that not table field motion and
  17593. not visibility should be done because of current context. This is probably
  17594. the place where a package like yasnippets can hook in.")
  17595. (defvar org-tab-before-tab-emulation-hook nil
  17596. "Hook for functions to attach themselves to TAB.
  17597. See `org-ctrl-c-ctrl-c-hook' for more information.
  17598. This hook runs after every other options for TAB have been exhausted, but
  17599. before indentation and \t insertion takes place.")
  17600. (defvar org-metaleft-hook nil
  17601. "Hook for functions attaching themselves to `M-left'.
  17602. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17603. (defvar org-metaright-hook nil
  17604. "Hook for functions attaching themselves to `M-right'.
  17605. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17606. (defvar org-metaup-hook nil
  17607. "Hook for functions attaching themselves to `M-up'.
  17608. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17609. (defvar org-metadown-hook nil
  17610. "Hook for functions attaching themselves to `M-down'.
  17611. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17612. (defvar org-shiftmetaleft-hook nil
  17613. "Hook for functions attaching themselves to `M-S-left'.
  17614. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17615. (defvar org-shiftmetaright-hook nil
  17616. "Hook for functions attaching themselves to `M-S-right'.
  17617. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17618. (defvar org-shiftmetaup-hook nil
  17619. "Hook for functions attaching themselves to `M-S-up'.
  17620. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17621. (defvar org-shiftmetadown-hook nil
  17622. "Hook for functions attaching themselves to `M-S-down'.
  17623. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17624. (defvar org-metareturn-hook nil
  17625. "Hook for functions attaching themselves to `M-RET'.
  17626. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17627. (defvar org-shiftup-hook nil
  17628. "Hook for functions attaching themselves to `S-up'.
  17629. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17630. (defvar org-shiftup-final-hook nil
  17631. "Hook for functions attaching themselves to `S-up'.
  17632. This one runs after all other options except shift-select have been excluded.
  17633. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17634. (defvar org-shiftdown-hook nil
  17635. "Hook for functions attaching themselves to `S-down'.
  17636. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17637. (defvar org-shiftdown-final-hook nil
  17638. "Hook for functions attaching themselves to `S-down'.
  17639. This one runs after all other options except shift-select have been excluded.
  17640. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17641. (defvar org-shiftleft-hook nil
  17642. "Hook for functions attaching themselves to `S-left'.
  17643. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17644. (defvar org-shiftleft-final-hook nil
  17645. "Hook for functions attaching themselves to `S-left'.
  17646. This one runs after all other options except shift-select have been excluded.
  17647. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17648. (defvar org-shiftright-hook nil
  17649. "Hook for functions attaching themselves to `S-right'.
  17650. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17651. (defvar org-shiftright-final-hook nil
  17652. "Hook for functions attaching themselves to `S-right'.
  17653. This one runs after all other options except shift-select have been excluded.
  17654. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17655. (defun org-modifier-cursor-error ()
  17656. "Throw an error, a modified cursor command was applied in wrong context."
  17657. (error "This command is active in special context like tables, headlines or items"))
  17658. (defun org-shiftselect-error ()
  17659. "Throw an error because Shift-Cursor command was applied in wrong context."
  17660. (if (and (boundp 'shift-select-mode) shift-select-mode)
  17661. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  17662. (error "This command works only in special context like headlines or timestamps")))
  17663. (defun org-call-for-shift-select (cmd)
  17664. (let ((this-command-keys-shift-translated t))
  17665. (call-interactively cmd)))
  17666. (defun org-shifttab (&optional arg)
  17667. "Global visibility cycling or move to previous table field.
  17668. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  17669. on context.
  17670. See the individual commands for more information."
  17671. (interactive "P")
  17672. (cond
  17673. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  17674. ((integerp arg)
  17675. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  17676. (message "Content view to level: %d" arg)
  17677. (org-content (prefix-numeric-value arg2))
  17678. (setq org-cycle-global-status 'overview)))
  17679. (t (call-interactively 'org-global-cycle))))
  17680. (defun org-shiftmetaleft ()
  17681. "Promote subtree or delete table column.
  17682. Calls `org-promote-subtree', `org-outdent-item-tree', or
  17683. `org-table-delete-column', depending on context. See the
  17684. individual commands for more information."
  17685. (interactive)
  17686. (cond
  17687. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  17688. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  17689. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  17690. ((if (not (org-region-active-p)) (org-at-item-p)
  17691. (save-excursion (goto-char (region-beginning))
  17692. (org-at-item-p)))
  17693. (call-interactively 'org-outdent-item-tree))
  17694. (t (org-modifier-cursor-error))))
  17695. (defun org-shiftmetaright ()
  17696. "Demote subtree or insert table column.
  17697. Calls `org-demote-subtree', `org-indent-item-tree', or
  17698. `org-table-insert-column', depending on context. See the
  17699. individual commands for more information."
  17700. (interactive)
  17701. (cond
  17702. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  17703. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  17704. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  17705. ((if (not (org-region-active-p)) (org-at-item-p)
  17706. (save-excursion (goto-char (region-beginning))
  17707. (org-at-item-p)))
  17708. (call-interactively 'org-indent-item-tree))
  17709. (t (org-modifier-cursor-error))))
  17710. (defun org-shiftmetaup (&optional arg)
  17711. "Move subtree up or kill table row.
  17712. Calls `org-move-subtree-up' or `org-table-kill-row' or
  17713. `org-move-item-up' or `org-timestamp-up', depending on context.
  17714. See the individual commands for more information."
  17715. (interactive "P")
  17716. (cond
  17717. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  17718. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  17719. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  17720. ((org-at-item-p) (call-interactively 'org-move-item-up))
  17721. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  17722. (call-interactively 'org-timestamp-up)))
  17723. (t (call-interactively 'org-drag-line-backward))))
  17724. (defun org-shiftmetadown (&optional arg)
  17725. "Move subtree down or insert table row.
  17726. Calls `org-move-subtree-down' or `org-table-insert-row' or
  17727. `org-move-item-down' or `org-timestamp-up', depending on context.
  17728. See the individual commands for more information."
  17729. (interactive "P")
  17730. (cond
  17731. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  17732. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  17733. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  17734. ((org-at-item-p) (call-interactively 'org-move-item-down))
  17735. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  17736. (call-interactively 'org-timestamp-down)))
  17737. (t (call-interactively 'org-drag-line-forward))))
  17738. (defsubst org-hidden-tree-error ()
  17739. (error
  17740. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  17741. (defun org-metaleft (&optional arg)
  17742. "Promote heading or move table column to left.
  17743. Calls `org-do-promote' or `org-table-move-column', depending on context.
  17744. With no specific context, calls the Emacs default `backward-word'.
  17745. See the individual commands for more information."
  17746. (interactive "P")
  17747. (cond
  17748. ((run-hook-with-args-until-success 'org-metaleft-hook))
  17749. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  17750. ((org-with-limited-levels
  17751. (or (org-at-heading-p)
  17752. (and (org-region-active-p)
  17753. (save-excursion
  17754. (goto-char (region-beginning))
  17755. (org-at-heading-p)))))
  17756. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  17757. (call-interactively 'org-do-promote))
  17758. ;; At an inline task.
  17759. ((org-at-heading-p)
  17760. (call-interactively 'org-inlinetask-promote))
  17761. ((or (org-at-item-p)
  17762. (and (org-region-active-p)
  17763. (save-excursion
  17764. (goto-char (region-beginning))
  17765. (org-at-item-p))))
  17766. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  17767. (call-interactively 'org-outdent-item))
  17768. (t (call-interactively 'backward-word))))
  17769. (defun org-metaright (&optional arg)
  17770. "Demote a subtree, a list item or move table column to right.
  17771. In front of a drawer or a block keyword, indent it correctly.
  17772. With no specific context, calls the Emacs default `forward-word'.
  17773. See the individual commands for more information."
  17774. (interactive "P")
  17775. (cond
  17776. ((run-hook-with-args-until-success 'org-metaright-hook))
  17777. ((org-at-table-p) (call-interactively 'org-table-move-column))
  17778. ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
  17779. ((org-at-block-p) (call-interactively 'org-indent-block))
  17780. ((org-with-limited-levels
  17781. (or (org-at-heading-p)
  17782. (and (org-region-active-p)
  17783. (save-excursion
  17784. (goto-char (region-beginning))
  17785. (org-at-heading-p)))))
  17786. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  17787. (call-interactively 'org-do-demote))
  17788. ;; At an inline task.
  17789. ((org-at-heading-p)
  17790. (call-interactively 'org-inlinetask-demote))
  17791. ((or (org-at-item-p)
  17792. (and (org-region-active-p)
  17793. (save-excursion
  17794. (goto-char (region-beginning))
  17795. (org-at-item-p))))
  17796. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  17797. (call-interactively 'org-indent-item))
  17798. (t (call-interactively 'forward-word))))
  17799. (defun org-check-for-hidden (what)
  17800. "Check if there are hidden headlines/items in the current visual line.
  17801. WHAT can be either `headlines' or `items'. If the current line is
  17802. an outline or item heading and it has a folded subtree below it,
  17803. this function returns t, nil otherwise."
  17804. (let ((re (cond
  17805. ((eq what 'headlines) org-outline-regexp-bol)
  17806. ((eq what 'items) (org-item-beginning-re))
  17807. (t (error "This should not happen"))))
  17808. beg end)
  17809. (save-excursion
  17810. (catch 'exit
  17811. (unless (org-region-active-p)
  17812. (setq beg (point-at-bol))
  17813. (beginning-of-line 2)
  17814. (while (and (not (eobp)) ;; this is like `next-line'
  17815. (get-char-property (1- (point)) 'invisible))
  17816. (beginning-of-line 2))
  17817. (setq end (point))
  17818. (goto-char beg)
  17819. (goto-char (point-at-eol))
  17820. (setq end (max end (point)))
  17821. (while (re-search-forward re end t)
  17822. (if (get-char-property (match-beginning 0) 'invisible)
  17823. (throw 'exit t))))
  17824. nil))))
  17825. (defun org-metaup (&optional arg)
  17826. "Move subtree up or move table row up.
  17827. Calls `org-move-subtree-up' or `org-table-move-row' or
  17828. `org-move-item-up', depending on context. See the individual commands
  17829. for more information."
  17830. (interactive "P")
  17831. (cond
  17832. ((run-hook-with-args-until-success 'org-metaup-hook))
  17833. ((org-region-active-p)
  17834. (let* ((a (min (region-beginning) (region-end)))
  17835. (b (1- (max (region-beginning) (region-end))))
  17836. (c (save-excursion (goto-char a)
  17837. (move-beginning-of-line 0)))
  17838. (d (save-excursion (goto-char a)
  17839. (move-end-of-line 0) (point))))
  17840. (transpose-regions a b c d)
  17841. (goto-char c)))
  17842. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  17843. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  17844. ((org-at-item-p) (call-interactively 'org-move-item-up))
  17845. (t (org-drag-element-backward))))
  17846. (defun org-metadown (&optional arg)
  17847. "Move subtree down or move table row down.
  17848. Calls `org-move-subtree-down' or `org-table-move-row' or
  17849. `org-move-item-down', depending on context. See the individual
  17850. commands for more information."
  17851. (interactive "P")
  17852. (cond
  17853. ((run-hook-with-args-until-success 'org-metadown-hook))
  17854. ((org-region-active-p)
  17855. (let* ((a (min (region-beginning) (region-end)))
  17856. (b (max (region-beginning) (region-end)))
  17857. (c (save-excursion (goto-char b)
  17858. (move-beginning-of-line 1)))
  17859. (d (save-excursion (goto-char b)
  17860. (move-end-of-line 1) (1+ (point)))))
  17861. (transpose-regions a b c d)
  17862. (goto-char d)))
  17863. ((org-at-table-p) (call-interactively 'org-table-move-row))
  17864. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  17865. ((org-at-item-p) (call-interactively 'org-move-item-down))
  17866. (t (org-drag-element-forward))))
  17867. (defun org-shiftup (&optional arg)
  17868. "Increase item in timestamp or increase priority of current headline.
  17869. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  17870. depending on context. See the individual commands for more information."
  17871. (interactive "P")
  17872. (cond
  17873. ((run-hook-with-args-until-success 'org-shiftup-hook))
  17874. ((and org-support-shift-select (org-region-active-p))
  17875. (org-call-for-shift-select 'previous-line))
  17876. ((org-at-timestamp-p t)
  17877. (call-interactively (if org-edit-timestamp-down-means-later
  17878. 'org-timestamp-down 'org-timestamp-up)))
  17879. ((and (not (eq org-support-shift-select 'always))
  17880. org-enable-priority-commands
  17881. (org-at-heading-p))
  17882. (call-interactively 'org-priority-up))
  17883. ((and (not org-support-shift-select) (org-at-item-p))
  17884. (call-interactively 'org-previous-item))
  17885. ((org-clocktable-try-shift 'up arg))
  17886. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  17887. (org-support-shift-select
  17888. (org-call-for-shift-select 'previous-line))
  17889. (t (org-shiftselect-error))))
  17890. (defun org-shiftdown (&optional arg)
  17891. "Decrease item in timestamp or decrease priority of current headline.
  17892. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  17893. depending on context. See the individual commands for more information."
  17894. (interactive "P")
  17895. (cond
  17896. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  17897. ((and org-support-shift-select (org-region-active-p))
  17898. (org-call-for-shift-select 'next-line))
  17899. ((org-at-timestamp-p t)
  17900. (call-interactively (if org-edit-timestamp-down-means-later
  17901. 'org-timestamp-up 'org-timestamp-down)))
  17902. ((and (not (eq org-support-shift-select 'always))
  17903. org-enable-priority-commands
  17904. (org-at-heading-p))
  17905. (call-interactively 'org-priority-down))
  17906. ((and (not org-support-shift-select) (org-at-item-p))
  17907. (call-interactively 'org-next-item))
  17908. ((org-clocktable-try-shift 'down arg))
  17909. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  17910. (org-support-shift-select
  17911. (org-call-for-shift-select 'next-line))
  17912. (t (org-shiftselect-error))))
  17913. (defun org-shiftright (&optional arg)
  17914. "Cycle the thing at point or in the current line, depending on context.
  17915. Depending on context, this does one of the following:
  17916. - switch a timestamp at point one day into the future
  17917. - on a headline, switch to the next TODO keyword.
  17918. - on an item, switch entire list to the next bullet type
  17919. - on a property line, switch to the next allowed value
  17920. - on a clocktable definition line, move time block into the future"
  17921. (interactive "P")
  17922. (cond
  17923. ((run-hook-with-args-until-success 'org-shiftright-hook))
  17924. ((and org-support-shift-select (org-region-active-p))
  17925. (org-call-for-shift-select 'forward-char))
  17926. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  17927. ((and (not (eq org-support-shift-select 'always))
  17928. (org-at-heading-p))
  17929. (let ((org-inhibit-logging
  17930. (not org-treat-S-cursor-todo-selection-as-state-change))
  17931. (org-inhibit-blocking
  17932. (not org-treat-S-cursor-todo-selection-as-state-change)))
  17933. (org-call-with-arg 'org-todo 'right)))
  17934. ((or (and org-support-shift-select
  17935. (not (eq org-support-shift-select 'always))
  17936. (org-at-item-bullet-p))
  17937. (and (not org-support-shift-select) (org-at-item-p)))
  17938. (org-call-with-arg 'org-cycle-list-bullet nil))
  17939. ((and (not (eq org-support-shift-select 'always))
  17940. (org-at-property-p))
  17941. (call-interactively 'org-property-next-allowed-value))
  17942. ((org-clocktable-try-shift 'right arg))
  17943. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  17944. (org-support-shift-select
  17945. (org-call-for-shift-select 'forward-char))
  17946. (t (org-shiftselect-error))))
  17947. (defun org-shiftleft (&optional arg)
  17948. "Cycle the thing at point or in the current line, depending on context.
  17949. Depending on context, this does one of the following:
  17950. - switch a timestamp at point one day into the past
  17951. - on a headline, switch to the previous TODO keyword.
  17952. - on an item, switch entire list to the previous bullet type
  17953. - on a property line, switch to the previous allowed value
  17954. - on a clocktable definition line, move time block into the past"
  17955. (interactive "P")
  17956. (cond
  17957. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  17958. ((and org-support-shift-select (org-region-active-p))
  17959. (org-call-for-shift-select 'backward-char))
  17960. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  17961. ((and (not (eq org-support-shift-select 'always))
  17962. (org-at-heading-p))
  17963. (let ((org-inhibit-logging
  17964. (not org-treat-S-cursor-todo-selection-as-state-change))
  17965. (org-inhibit-blocking
  17966. (not org-treat-S-cursor-todo-selection-as-state-change)))
  17967. (org-call-with-arg 'org-todo 'left)))
  17968. ((or (and org-support-shift-select
  17969. (not (eq org-support-shift-select 'always))
  17970. (org-at-item-bullet-p))
  17971. (and (not org-support-shift-select) (org-at-item-p)))
  17972. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  17973. ((and (not (eq org-support-shift-select 'always))
  17974. (org-at-property-p))
  17975. (call-interactively 'org-property-previous-allowed-value))
  17976. ((org-clocktable-try-shift 'left arg))
  17977. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  17978. (org-support-shift-select
  17979. (org-call-for-shift-select 'backward-char))
  17980. (t (org-shiftselect-error))))
  17981. (defun org-shiftcontrolright ()
  17982. "Switch to next TODO set."
  17983. (interactive)
  17984. (cond
  17985. ((and org-support-shift-select (org-region-active-p))
  17986. (org-call-for-shift-select 'forward-word))
  17987. ((and (not (eq org-support-shift-select 'always))
  17988. (org-at-heading-p))
  17989. (org-call-with-arg 'org-todo 'nextset))
  17990. (org-support-shift-select
  17991. (org-call-for-shift-select 'forward-word))
  17992. (t (org-shiftselect-error))))
  17993. (defun org-shiftcontrolleft ()
  17994. "Switch to previous TODO set."
  17995. (interactive)
  17996. (cond
  17997. ((and org-support-shift-select (org-region-active-p))
  17998. (org-call-for-shift-select 'backward-word))
  17999. ((and (not (eq org-support-shift-select 'always))
  18000. (org-at-heading-p))
  18001. (org-call-with-arg 'org-todo 'previousset))
  18002. (org-support-shift-select
  18003. (org-call-for-shift-select 'backward-word))
  18004. (t (org-shiftselect-error))))
  18005. (defun org-shiftcontrolup (&optional n)
  18006. "Change timestamps synchronously up in CLOCK log lines.
  18007. Optional argument N tells to change by that many units."
  18008. (interactive "P")
  18009. (cond ((and (not org-support-shift-select)
  18010. (org-at-clock-log-p)
  18011. (org-at-timestamp-p t))
  18012. (org-clock-timestamps-up n))
  18013. (t (org-shiftselect-error))))
  18014. (defun org-shiftcontroldown (&optional n)
  18015. "Change timestamps synchronously down in CLOCK log lines.
  18016. Optional argument N tells to change by that many units."
  18017. (interactive "P")
  18018. (cond ((and (not org-support-shift-select)
  18019. (org-at-clock-log-p)
  18020. (org-at-timestamp-p t))
  18021. (org-clock-timestamps-down n))
  18022. (t (org-shiftselect-error))))
  18023. (defun org-ctrl-c-ret ()
  18024. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  18025. (interactive)
  18026. (cond
  18027. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  18028. (t (call-interactively 'org-insert-heading))))
  18029. (defun org-find-visible ()
  18030. (let ((s (point)))
  18031. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  18032. (get-char-property s 'invisible)))
  18033. s))
  18034. (defun org-find-invisible ()
  18035. (let ((s (point)))
  18036. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  18037. (not (get-char-property s 'invisible))))
  18038. s))
  18039. (defun org-copy-visible (beg end)
  18040. "Copy the visible parts of the region."
  18041. (interactive "r")
  18042. (let (snippets s)
  18043. (save-excursion
  18044. (save-restriction
  18045. (narrow-to-region beg end)
  18046. (setq s (goto-char (point-min)))
  18047. (while (not (= (point) (point-max)))
  18048. (goto-char (org-find-invisible))
  18049. (push (buffer-substring s (point)) snippets)
  18050. (setq s (goto-char (org-find-visible))))))
  18051. (kill-new (apply 'concat (nreverse snippets)))))
  18052. (defun org-copy-special ()
  18053. "Copy region in table or copy current subtree.
  18054. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  18055. See the individual commands for more information."
  18056. (interactive)
  18057. (call-interactively
  18058. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  18059. (defun org-cut-special ()
  18060. "Cut region in table or cut current subtree.
  18061. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  18062. See the individual commands for more information."
  18063. (interactive)
  18064. (call-interactively
  18065. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  18066. (defun org-paste-special (arg)
  18067. "Paste rectangular region into table, or past subtree relative to level.
  18068. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  18069. See the individual commands for more information."
  18070. (interactive "P")
  18071. (if (org-at-table-p)
  18072. (org-table-paste-rectangle)
  18073. (org-paste-subtree arg)))
  18074. (defsubst org-in-fixed-width-region-p ()
  18075. "Is point in a fixed-width region?"
  18076. (save-match-data
  18077. (eq 'fixed-width (org-element-type (org-element-at-point)))))
  18078. (defun org-edit-special (&optional arg)
  18079. "Call a special editor for the element at point.
  18080. When at a table, call the formula editor with `org-table-edit-formulas'.
  18081. When in a source code block, call `org-edit-src-code'.
  18082. When in a fixed-width region, call `org-edit-fixed-width-region'.
  18083. When at an #+INCLUDE keyword, visit the included file.
  18084. On a link, call `ffap' to visit the link at point.
  18085. Otherwise, return a user error."
  18086. (interactive)
  18087. (let ((element (org-element-at-point)))
  18088. (assert (not buffer-read-only) nil
  18089. "Buffer is read-only: %s" (buffer-name))
  18090. (case (org-element-type element)
  18091. (src-block
  18092. (if (not arg) (org-edit-src-code)
  18093. (let* ((info (org-babel-get-src-block-info))
  18094. (lang (nth 0 info))
  18095. (params (nth 2 info))
  18096. (session (cdr (assq :session params))))
  18097. (if (not session) (org-edit-src-code)
  18098. ;; At a src-block with a session and function called with
  18099. ;; an ARG: switch to the buffer related to the inferior
  18100. ;; process.
  18101. (funcall (intern (concat "org-babel-prep-session:" lang))
  18102. session params)))))
  18103. (keyword
  18104. (if (member (org-element-property :key element) '("INCLUDE" "SETUPFILE"))
  18105. (find-file
  18106. (org-remove-double-quotes
  18107. (car (org-split-string (org-element-property :value element)))))
  18108. (user-error "No special environment to edit here")))
  18109. (table
  18110. (if (eq (org-element-property :type element) 'table.el)
  18111. (org-edit-src-code)
  18112. (call-interactively 'org-table-edit-formulas)))
  18113. ;; Only Org tables contain `table-row' type elements.
  18114. (table-row (call-interactively 'org-table-edit-formulas))
  18115. ((example-block export-block) (org-edit-src-code))
  18116. (fixed-width (org-edit-fixed-width-region))
  18117. (otherwise
  18118. ;; No notable element at point. Though, we may be at a link,
  18119. ;; which is an object. Thus, scan deeper.
  18120. (if (eq (org-element-type (org-element-context element)) 'link)
  18121. (call-interactively 'ffap)
  18122. (user-error "No special environment to edit here"))))))
  18123. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  18124. (defun org-ctrl-c-ctrl-c (&optional arg)
  18125. "Set tags in headline, or update according to changed information at point.
  18126. This command does many different things, depending on context:
  18127. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  18128. this is what we do.
  18129. - If the cursor is on a statistics cookie, update it.
  18130. - If the cursor is in a headline, prompt for tags and insert them
  18131. into the current line, aligned to `org-tags-column'. When called
  18132. with prefix arg, realign all tags in the current buffer.
  18133. - If the cursor is in one of the special #+KEYWORD lines, this
  18134. triggers scanning the buffer for these lines and updating the
  18135. information.
  18136. - If the cursor is inside a table, realign the table. This command
  18137. works even if the automatic table editor has been turned off.
  18138. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  18139. the entire table.
  18140. - If the cursor is at a footnote reference or definition, jump to
  18141. the corresponding definition or references, respectively.
  18142. - If the cursor is a the beginning of a dynamic block, update it.
  18143. - If the current buffer is a capture buffer, close note and file it.
  18144. - If the cursor is on a <<<target>>>, update radio targets and
  18145. corresponding links in this buffer.
  18146. - If the cursor is on a numbered item in a plain list, renumber the
  18147. ordered list.
  18148. - If the cursor is on a checkbox, toggle it.
  18149. - If the cursor is on a code block, evaluate it. The variable
  18150. `org-confirm-babel-evaluate' can be used to control prompting
  18151. before code block evaluation, by default every code block
  18152. evaluation requires confirmation. Code block evaluation can be
  18153. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  18154. (interactive "P")
  18155. (cond
  18156. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  18157. org-occur-highlights
  18158. org-latex-fragment-image-overlays)
  18159. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  18160. (org-remove-occur-highlights)
  18161. (org-remove-latex-fragment-image-overlays)
  18162. (message "Temporary highlights/overlays removed from current buffer"))
  18163. ((and (local-variable-p 'org-finish-function (current-buffer))
  18164. (fboundp org-finish-function))
  18165. (funcall org-finish-function))
  18166. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  18167. (t
  18168. (let* ((context (org-element-context)) (type (org-element-type context)))
  18169. ;; Test if point is within a blank line.
  18170. (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
  18171. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  18172. (user-error "C-c C-c can do nothing useful at this location"))
  18173. ;; For convenience: at the first line of a paragraph on the
  18174. ;; same line as an item, apply function on that item instead.
  18175. (when (eq type 'paragraph)
  18176. (let ((parent (org-element-property :parent context)))
  18177. (when (and (eq (org-element-type parent) 'item)
  18178. (= (point-at-bol) (org-element-property :begin parent)))
  18179. (setq context parent type 'item))))
  18180. ;; Act according to type of element or object at point.
  18181. (case type
  18182. (clock (org-clock-update-time-maybe))
  18183. (dynamic-block
  18184. (save-excursion
  18185. (goto-char (org-element-property :post-affiliated context))
  18186. (org-update-dblock)))
  18187. (footnote-definition
  18188. (goto-char (org-element-property :post-affiliated context))
  18189. (call-interactively 'org-footnote-action))
  18190. (footnote-reference (call-interactively 'org-footnote-action))
  18191. ((headline inlinetask)
  18192. (save-excursion (goto-char (org-element-property :begin context))
  18193. (call-interactively 'org-set-tags)))
  18194. (item
  18195. ;; At an item: a double C-u set checkbox to "[-]"
  18196. ;; unconditionally, whereas a single one will toggle its
  18197. ;; presence. Without an universal argument, if the item
  18198. ;; has a checkbox, toggle it. Otherwise repair the list.
  18199. (let* ((box (org-element-property :checkbox context))
  18200. (struct (org-element-property :structure context))
  18201. (old-struct (copy-tree struct))
  18202. (parents (org-list-parents-alist struct))
  18203. (prevs (org-list-prevs-alist struct))
  18204. (orderedp (org-not-nil (org-entry-get nil "ORDERED"))))
  18205. (org-list-set-checkbox
  18206. (org-element-property :begin context) struct
  18207. (cond ((equal arg '(16)) "[-]")
  18208. ((and (not box) (equal arg '(4))) "[ ]")
  18209. ((or (not box) (equal arg '(4))) nil)
  18210. ((eq box 'on) "[ ]")
  18211. (t "[X]")))
  18212. ;; Mimic `org-list-write-struct' but with grabbing
  18213. ;; a return value from `org-list-struct-fix-box'.
  18214. (org-list-struct-fix-ind struct parents 2)
  18215. (org-list-struct-fix-item-end struct)
  18216. (org-list-struct-fix-bul struct prevs)
  18217. (org-list-struct-fix-ind struct parents)
  18218. (let ((block-item
  18219. (org-list-struct-fix-box struct parents prevs orderedp)))
  18220. (if (and box (equal struct old-struct))
  18221. (if (equal arg '(16))
  18222. (message "Checkboxes already reset")
  18223. (user-error "Cannot toggle this checkbox: %s"
  18224. (if (eq box 'on)
  18225. "all subitems checked"
  18226. "unchecked subitems")))
  18227. (org-list-struct-apply-struct struct old-struct)
  18228. (org-update-checkbox-count-maybe))
  18229. (when block-item
  18230. (message "Checkboxes were removed due to empty box at line %d"
  18231. (org-current-line block-item))))))
  18232. (keyword
  18233. (let ((org-inhibit-startup-visibility-stuff t)
  18234. (org-startup-align-all-tables nil))
  18235. (when (boundp 'org-table-coordinate-overlays)
  18236. (mapc 'delete-overlay org-table-coordinate-overlays)
  18237. (setq org-table-coordinate-overlays nil))
  18238. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  18239. (message "Local setup has been refreshed"))
  18240. (plain-list
  18241. ;; At a plain list, with a double C-u argument, set
  18242. ;; checkboxes of each item to "[-]", whereas a single one
  18243. ;; will toggle their presence according to the state of the
  18244. ;; first item in the list. Without an argument, repair the
  18245. ;; list.
  18246. (let* ((begin (org-element-property :contents-begin context))
  18247. (struct (org-element-property :structure context))
  18248. (old-struct (copy-tree struct))
  18249. (first-box (save-excursion
  18250. (goto-char begin)
  18251. (looking-at org-list-full-item-re)
  18252. (match-string-no-properties 3)))
  18253. (new-box (cond ((equal arg '(16)) "[-]")
  18254. ((equal arg '(4)) (unless first-box "[ ]"))
  18255. ((equal first-box "[X]") "[ ]")
  18256. (t "[X]"))))
  18257. (cond
  18258. (arg
  18259. (mapc (lambda (pos) (org-list-set-checkbox pos struct new-box))
  18260. (org-list-get-all-items
  18261. begin struct (org-list-prevs-alist struct))))
  18262. ((and first-box (eq (point) begin))
  18263. ;; For convenience, when point is at bol on the first
  18264. ;; item of the list and no argument is provided, simply
  18265. ;; toggle checkbox of that item, if any.
  18266. (org-list-set-checkbox begin struct new-box)))
  18267. (org-list-write-struct
  18268. struct (org-list-parents-alist struct) old-struct)
  18269. (org-update-checkbox-count-maybe)
  18270. (save-excursion (goto-char begin) (org-list-send-list 'maybe))))
  18271. ((property-drawer node-property)
  18272. (call-interactively 'org-property-action))
  18273. ((radio-target target)
  18274. (call-interactively 'org-update-radio-target-regexp))
  18275. (statistics-cookie
  18276. (call-interactively 'org-update-statistics-cookies))
  18277. ((table table-cell table-row)
  18278. ;; At a table, recalculate every field and align it. Also
  18279. ;; send the table if necessary. If the table has
  18280. ;; a `table.el' type, just give up. At a table row or
  18281. ;; cell, maybe recalculate line but always align table.
  18282. (if (eq (org-element-property :type context) 'table.el)
  18283. (message "Use C-c ' to edit table.el tables")
  18284. (let ((org-enable-table-editor t))
  18285. (if (or (eq type 'table)
  18286. ;; Check if point is at a TBLFM line.
  18287. (and (eq type 'table-row)
  18288. (= (point) (org-element-property :end context))))
  18289. (save-excursion
  18290. (goto-char (org-element-property :contents-begin context))
  18291. (org-call-with-arg 'org-table-recalculate (or arg t))
  18292. (orgtbl-send-table 'maybe))
  18293. (org-table-maybe-eval-formula)
  18294. (cond (arg (call-interactively 'org-table-recalculate))
  18295. ((org-table-maybe-recalculate-line))
  18296. (t (org-table-align)))))))
  18297. (timestamp (org-timestamp-change 0 'day))
  18298. (otherwise
  18299. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  18300. (user-error
  18301. "C-c C-c can do nothing useful at this location")))))))))
  18302. (defun org-mode-restart ()
  18303. "Restart Org-mode, to scan again for special lines.
  18304. Also updates the keyword regular expressions."
  18305. (interactive)
  18306. (org-mode)
  18307. (message "Org-mode restarted"))
  18308. (defun org-kill-note-or-show-branches ()
  18309. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  18310. (interactive)
  18311. (if (not org-finish-function)
  18312. (progn
  18313. (hide-subtree)
  18314. (call-interactively 'show-branches))
  18315. (let ((org-note-abort t))
  18316. (funcall org-finish-function))))
  18317. (defun org-return (&optional indent)
  18318. "Goto next table row or insert a newline.
  18319. Calls `org-table-next-row' or `newline', depending on context.
  18320. See the individual commands for more information."
  18321. (interactive)
  18322. (let (org-ts-what)
  18323. (cond
  18324. ((or (bobp) (org-in-src-block-p))
  18325. (if indent (newline-and-indent) (newline)))
  18326. ((org-at-table-p)
  18327. (org-table-justify-field-maybe)
  18328. (call-interactively 'org-table-next-row))
  18329. ;; when `newline-and-indent' is called within a list, make sure
  18330. ;; text moved stays inside the item.
  18331. ((and (org-in-item-p) indent)
  18332. (if (and (org-at-item-p) (>= (point) (match-end 0)))
  18333. (progn
  18334. (save-match-data (newline))
  18335. (org-indent-line-to (length (match-string 0))))
  18336. (let ((ind (org-get-indentation)))
  18337. (newline)
  18338. (if (org-looking-back org-list-end-re)
  18339. (org-indent-line)
  18340. (org-indent-line-to ind)))))
  18341. ((and org-return-follows-link
  18342. (org-at-timestamp-p t)
  18343. (not (eq org-ts-what 'after)))
  18344. (org-follow-timestamp-link))
  18345. ((and org-return-follows-link
  18346. (let ((tprop (get-text-property (point) 'face)))
  18347. (or (eq tprop 'org-link)
  18348. (and (listp tprop) (memq 'org-link tprop)))))
  18349. (call-interactively 'org-open-at-point))
  18350. ((and (org-at-heading-p)
  18351. (looking-at
  18352. (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
  18353. (org-show-entry)
  18354. (end-of-line 1)
  18355. (newline))
  18356. (t (if indent (newline-and-indent) (newline))))))
  18357. (defun org-return-indent ()
  18358. "Goto next table row or insert a newline and indent.
  18359. Calls `org-table-next-row' or `newline-and-indent', depending on
  18360. context. See the individual commands for more information."
  18361. (interactive)
  18362. (org-return t))
  18363. (defun org-ctrl-c-star ()
  18364. "Compute table, or change heading status of lines.
  18365. Calls `org-table-recalculate' or `org-toggle-heading',
  18366. depending on context."
  18367. (interactive)
  18368. (cond
  18369. ((org-at-table-p)
  18370. (call-interactively 'org-table-recalculate))
  18371. (t
  18372. ;; Convert all lines in region to list items
  18373. (call-interactively 'org-toggle-heading))))
  18374. (defun org-ctrl-c-minus ()
  18375. "Insert separator line in table or modify bullet status of line.
  18376. Also turns a plain line or a region of lines into list items.
  18377. Calls `org-table-insert-hline', `org-toggle-item', or
  18378. `org-cycle-list-bullet', depending on context."
  18379. (interactive)
  18380. (cond
  18381. ((org-at-table-p)
  18382. (call-interactively 'org-table-insert-hline))
  18383. ((org-region-active-p)
  18384. (call-interactively 'org-toggle-item))
  18385. ((org-in-item-p)
  18386. (call-interactively 'org-cycle-list-bullet))
  18387. (t
  18388. (call-interactively 'org-toggle-item))))
  18389. (defun org-toggle-item (arg)
  18390. "Convert headings or normal lines to items, items to normal lines.
  18391. If there is no active region, only the current line is considered.
  18392. If the first non blank line in the region is a headline, convert
  18393. all headlines to items, shifting text accordingly.
  18394. If it is an item, convert all items to normal lines.
  18395. If it is normal text, change region into an item. With a prefix
  18396. argument ARG, change each line in region into an item."
  18397. (interactive "P")
  18398. (let ((shift-text
  18399. (function
  18400. ;; Shift text in current section to IND, from point to END.
  18401. ;; The function leaves point to END line.
  18402. (lambda (ind end)
  18403. (let ((min-i 1000) (end (copy-marker end)))
  18404. ;; First determine the minimum indentation (MIN-I) of
  18405. ;; the text.
  18406. (save-excursion
  18407. (catch 'exit
  18408. (while (< (point) end)
  18409. (let ((i (org-get-indentation)))
  18410. (cond
  18411. ;; Skip blank lines and inline tasks.
  18412. ((looking-at "^[ \t]*$"))
  18413. ((looking-at org-outline-regexp-bol))
  18414. ;; We can't find less than 0 indentation.
  18415. ((zerop i) (throw 'exit (setq min-i 0)))
  18416. ((< i min-i) (setq min-i i))))
  18417. (forward-line))))
  18418. ;; Then indent each line so that a line indented to
  18419. ;; MIN-I becomes indented to IND. Ignore blank lines
  18420. ;; and inline tasks in the process.
  18421. (let ((delta (- ind min-i)))
  18422. (while (< (point) end)
  18423. (unless (or (looking-at "^[ \t]*$")
  18424. (looking-at org-outline-regexp-bol))
  18425. (org-indent-line-to (+ (org-get-indentation) delta)))
  18426. (forward-line)))))))
  18427. (skip-blanks
  18428. (function
  18429. ;; Return beginning of first non-blank line, starting from
  18430. ;; line at POS.
  18431. (lambda (pos)
  18432. (save-excursion
  18433. (goto-char pos)
  18434. (skip-chars-forward " \r\t\n")
  18435. (point-at-bol)))))
  18436. beg end)
  18437. ;; Determine boundaries of changes.
  18438. (if (org-region-active-p)
  18439. (setq beg (funcall skip-blanks (region-beginning))
  18440. end (copy-marker (region-end)))
  18441. (setq beg (funcall skip-blanks (point-at-bol))
  18442. end (copy-marker (point-at-eol))))
  18443. ;; Depending on the starting line, choose an action on the text
  18444. ;; between BEG and END.
  18445. (org-with-limited-levels
  18446. (save-excursion
  18447. (goto-char beg)
  18448. (cond
  18449. ;; Case 1. Start at an item: de-itemize. Note that it only
  18450. ;; happens when a region is active: `org-ctrl-c-minus'
  18451. ;; would call `org-cycle-list-bullet' otherwise.
  18452. ((org-at-item-p)
  18453. (while (< (point) end)
  18454. (when (org-at-item-p)
  18455. (skip-chars-forward " \t")
  18456. (delete-region (point) (match-end 0)))
  18457. (forward-line)))
  18458. ;; Case 2. Start at an heading: convert to items.
  18459. ((org-at-heading-p)
  18460. (let* ((bul (org-list-bullet-string "-"))
  18461. (bul-len (length bul))
  18462. ;; Indentation of the first heading. It should be
  18463. ;; relative to the indentation of its parent, if any.
  18464. (start-ind (save-excursion
  18465. (cond
  18466. ((not org-adapt-indentation) 0)
  18467. ((not (outline-previous-heading)) 0)
  18468. (t (length (match-string 0))))))
  18469. ;; Level of first heading. Further headings will be
  18470. ;; compared to it to determine hierarchy in the list.
  18471. (ref-level (org-reduced-level (org-outline-level))))
  18472. (while (< (point) end)
  18473. (let* ((level (org-reduced-level (org-outline-level)))
  18474. (delta (max 0 (- level ref-level))))
  18475. ;; If current headline is less indented than the first
  18476. ;; one, set it as reference, in order to preserve
  18477. ;; subtrees.
  18478. (when (< level ref-level) (setq ref-level level))
  18479. (replace-match bul t t)
  18480. (org-indent-line-to (+ start-ind (* delta bul-len)))
  18481. ;; Ensure all text down to END (or SECTION-END) belongs
  18482. ;; to the newly created item.
  18483. (let ((section-end (save-excursion
  18484. (or (outline-next-heading) (point)))))
  18485. (forward-line)
  18486. (funcall shift-text
  18487. (+ start-ind (* (1+ delta) bul-len))
  18488. (min end section-end)))))))
  18489. ;; Case 3. Normal line with ARG: turn each non-item line into
  18490. ;; an item.
  18491. (arg
  18492. (while (< (point) end)
  18493. (unless (or (org-at-heading-p) (org-at-item-p))
  18494. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  18495. (replace-match
  18496. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  18497. (forward-line)))
  18498. ;; Case 4. Normal line without ARG: make the first line of
  18499. ;; region an item, and shift indentation of others
  18500. ;; lines to set them as item's body.
  18501. (t (let* ((bul (org-list-bullet-string "-"))
  18502. (bul-len (length bul))
  18503. (ref-ind (org-get-indentation)))
  18504. (skip-chars-forward " \t")
  18505. (insert bul)
  18506. (forward-line)
  18507. (while (< (point) end)
  18508. ;; Ensure that lines less indented than first one
  18509. ;; still get included in item body.
  18510. (funcall shift-text
  18511. (+ ref-ind bul-len)
  18512. (min end (save-excursion (or (outline-next-heading)
  18513. (point)))))
  18514. (forward-line)))))))))
  18515. (defun org-toggle-heading (&optional nstars)
  18516. "Convert headings to normal text, or items or text to headings.
  18517. If there is no active region, only the current line is considered.
  18518. With a \\[universal-argument] prefix, convert the whole list at
  18519. point into heading.
  18520. In a region:
  18521. - If the first non blank line is a headline, remove the stars
  18522. from all headlines in the region.
  18523. - If it is a normal line turn each and every normal line (i.e. not an
  18524. heading or an item) in the region into a heading.
  18525. - If it is a plain list item, turn all plain list items into headings.
  18526. When converting a line into a heading, the number of stars is chosen
  18527. such that the lines become children of the current entry. However,
  18528. when a prefix argument is given, its value determines the number of
  18529. stars to add."
  18530. (interactive "P")
  18531. (let ((skip-blanks
  18532. (function
  18533. ;; Return beginning of first non-blank line, starting from
  18534. ;; line at POS.
  18535. (lambda (pos)
  18536. (save-excursion
  18537. (goto-char pos)
  18538. (while (org-at-comment-p) (forward-line))
  18539. (skip-chars-forward " \r\t\n")
  18540. (point-at-bol)))))
  18541. beg end toggled)
  18542. ;; Determine boundaries of changes. If a universal prefix has
  18543. ;; been given, put the list in a region. If region ends at a bol,
  18544. ;; do not consider the last line to be in the region.
  18545. (when (and current-prefix-arg (org-at-item-p))
  18546. (if (equal current-prefix-arg '(4)) (setq current-prefix-arg 1))
  18547. (org-mark-element))
  18548. (if (org-region-active-p)
  18549. (setq beg (funcall skip-blanks (region-beginning))
  18550. end (copy-marker (save-excursion
  18551. (goto-char (region-end))
  18552. (if (bolp) (point) (point-at-eol)))))
  18553. (setq beg (funcall skip-blanks (point-at-bol))
  18554. end (copy-marker (point-at-eol))))
  18555. ;; Ensure inline tasks don't count as headings.
  18556. (org-with-limited-levels
  18557. (save-excursion
  18558. (goto-char beg)
  18559. (cond
  18560. ;; Case 1. Started at an heading: de-star headings.
  18561. ((org-at-heading-p)
  18562. (while (< (point) end)
  18563. (when (org-at-heading-p t)
  18564. (looking-at org-outline-regexp) (replace-match "")
  18565. (setq toggled t))
  18566. (forward-line)))
  18567. ;; Case 2. Started at an item: change items into headlines.
  18568. ;; One star will be added by `org-list-to-subtree'.
  18569. ((org-at-item-p)
  18570. (let* ((stars (make-string
  18571. (if nstars
  18572. ;; subtract the star that will be added again by
  18573. ;; `org-list-to-subtree'
  18574. (1- (prefix-numeric-value current-prefix-arg))
  18575. (or (org-current-level) 0))
  18576. ?*))
  18577. (add-stars
  18578. (cond (nstars "") ; stars from prefix only
  18579. ((equal stars "") "") ; before first heading
  18580. (org-odd-levels-only "*") ; inside heading, odd
  18581. (t "")))) ; inside heading, oddeven
  18582. (while (< (point) end)
  18583. (when (org-at-item-p)
  18584. ;; Pay attention to cases when region ends before list.
  18585. (let* ((struct (org-list-struct))
  18586. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  18587. (save-restriction
  18588. (narrow-to-region (point) list-end)
  18589. (insert
  18590. (org-list-to-subtree
  18591. (org-list-parse-list t)
  18592. '(:istart (concat stars add-stars (funcall get-stars depth))
  18593. :icount (concat stars add-stars (funcall get-stars depth)))))))
  18594. (setq toggled t))
  18595. (forward-line))))
  18596. ;; Case 3. Started at normal text: make every line an heading,
  18597. ;; skipping headlines and items.
  18598. (t (let* ((stars (make-string
  18599. (if nstars
  18600. (prefix-numeric-value current-prefix-arg)
  18601. (or (org-current-level) 0))
  18602. ?*))
  18603. (add-stars
  18604. (cond (nstars "") ; stars from prefix only
  18605. ((equal stars "") "*") ; before first heading
  18606. (org-odd-levels-only "**") ; inside heading, odd
  18607. (t "*"))) ; inside heading, oddeven
  18608. (rpl (concat stars add-stars " ")))
  18609. (while (< (point) end)
  18610. (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
  18611. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  18612. (replace-match (concat rpl (match-string 2))) (setq toggled t))
  18613. (forward-line)))))))
  18614. (unless toggled (message "Cannot toggle heading from here"))))
  18615. (defun org-meta-return (&optional arg)
  18616. "Insert a new heading or wrap a region in a table.
  18617. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  18618. See the individual commands for more information."
  18619. (interactive "P")
  18620. (cond
  18621. ((run-hook-with-args-until-success 'org-metareturn-hook))
  18622. ((or (org-at-drawer-p) (org-at-property-p))
  18623. (newline-and-indent))
  18624. ((org-at-table-p)
  18625. (call-interactively 'org-table-wrap-region))
  18626. (t (call-interactively 'org-insert-heading))))
  18627. ;;; Menu entries
  18628. (defsubst org-in-subtree-not-table-p ()
  18629. "Are we in a subtree and not in a table?"
  18630. (and (not (org-before-first-heading-p))
  18631. (not (org-at-table-p))))
  18632. ;; Define the Org-mode menus
  18633. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  18634. '("Tbl"
  18635. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  18636. ["Next Field" org-cycle (org-at-table-p)]
  18637. ["Previous Field" org-shifttab (org-at-table-p)]
  18638. ["Next Row" org-return (org-at-table-p)]
  18639. "--"
  18640. ["Blank Field" org-table-blank-field (org-at-table-p)]
  18641. ["Edit Field" org-table-edit-field (org-at-table-p)]
  18642. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  18643. "--"
  18644. ("Column"
  18645. ["Move Column Left" org-metaleft (org-at-table-p)]
  18646. ["Move Column Right" org-metaright (org-at-table-p)]
  18647. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  18648. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  18649. ("Row"
  18650. ["Move Row Up" org-metaup (org-at-table-p)]
  18651. ["Move Row Down" org-metadown (org-at-table-p)]
  18652. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  18653. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  18654. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  18655. "--"
  18656. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  18657. ("Rectangle"
  18658. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  18659. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  18660. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  18661. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  18662. "--"
  18663. ("Calculate"
  18664. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  18665. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  18666. ["Edit Formulas" org-edit-special (org-at-table-p)]
  18667. "--"
  18668. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  18669. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  18670. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  18671. "--"
  18672. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  18673. "--"
  18674. ["Sum Column/Rectangle" org-table-sum
  18675. (or (org-at-table-p) (org-region-active-p))]
  18676. ["Which Column?" org-table-current-column (org-at-table-p)])
  18677. ["Debug Formulas"
  18678. org-table-toggle-formula-debugger
  18679. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  18680. ["Show Col/Row Numbers"
  18681. org-table-toggle-coordinate-overlays
  18682. :style toggle
  18683. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  18684. "--"
  18685. ["Create" org-table-create (and (not (org-at-table-p))
  18686. org-enable-table-editor)]
  18687. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  18688. ["Import from File" org-table-import (not (org-at-table-p))]
  18689. ["Export to File" org-table-export (org-at-table-p)]
  18690. "--"
  18691. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  18692. (easy-menu-define org-org-menu org-mode-map "Org menu"
  18693. '("Org"
  18694. ("Show/Hide"
  18695. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  18696. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  18697. ["Sparse Tree..." org-sparse-tree t]
  18698. ["Reveal Context" org-reveal t]
  18699. ["Show All" show-all t]
  18700. "--"
  18701. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  18702. "--"
  18703. ["New Heading" org-insert-heading t]
  18704. ("Navigate Headings"
  18705. ["Up" outline-up-heading t]
  18706. ["Next" outline-next-visible-heading t]
  18707. ["Previous" outline-previous-visible-heading t]
  18708. ["Next Same Level" outline-forward-same-level t]
  18709. ["Previous Same Level" outline-backward-same-level t]
  18710. "--"
  18711. ["Jump" org-goto t])
  18712. ("Edit Structure"
  18713. ["Refile Subtree" org-refile (org-in-subtree-not-table-p)]
  18714. "--"
  18715. ["Move Subtree Up" org-shiftmetaup (org-in-subtree-not-table-p)]
  18716. ["Move Subtree Down" org-shiftmetadown (org-in-subtree-not-table-p)]
  18717. "--"
  18718. ["Copy Subtree" org-copy-special (org-in-subtree-not-table-p)]
  18719. ["Cut Subtree" org-cut-special (org-in-subtree-not-table-p)]
  18720. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  18721. "--"
  18722. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  18723. "--"
  18724. ["Copy visible text" org-copy-visible t]
  18725. "--"
  18726. ["Promote Heading" org-metaleft (org-in-subtree-not-table-p)]
  18727. ["Promote Subtree" org-shiftmetaleft (org-in-subtree-not-table-p)]
  18728. ["Demote Heading" org-metaright (org-in-subtree-not-table-p)]
  18729. ["Demote Subtree" org-shiftmetaright (org-in-subtree-not-table-p)]
  18730. "--"
  18731. ["Sort Region/Children" org-sort t]
  18732. "--"
  18733. ["Convert to odd levels" org-convert-to-odd-levels t]
  18734. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  18735. ("Editing"
  18736. ["Emphasis..." org-emphasize t]
  18737. ["Edit Source Example" org-edit-special t]
  18738. "--"
  18739. ["Footnote new/jump" org-footnote-action t]
  18740. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  18741. ("Archive"
  18742. ["Archive (default method)" org-archive-subtree-default (org-in-subtree-not-table-p)]
  18743. "--"
  18744. ["Move Subtree to Archive file" org-advertized-archive-subtree (org-in-subtree-not-table-p)]
  18745. ["Toggle ARCHIVE tag" org-toggle-archive-tag (org-in-subtree-not-table-p)]
  18746. ["Move subtree to Archive sibling" org-archive-to-archive-sibling (org-in-subtree-not-table-p)]
  18747. )
  18748. "--"
  18749. ("Hyperlinks"
  18750. ["Store Link (Global)" org-store-link t]
  18751. ["Find existing link to here" org-occur-link-in-agenda-files t]
  18752. ["Insert Link" org-insert-link t]
  18753. ["Follow Link" org-open-at-point t]
  18754. "--"
  18755. ["Next link" org-next-link t]
  18756. ["Previous link" org-previous-link t]
  18757. "--"
  18758. ["Descriptive Links"
  18759. org-toggle-link-display
  18760. :style radio
  18761. :selected org-descriptive-links
  18762. ]
  18763. ["Literal Links"
  18764. org-toggle-link-display
  18765. :style radio
  18766. :selected (not org-descriptive-links)])
  18767. "--"
  18768. ("TODO Lists"
  18769. ["TODO/DONE/-" org-todo t]
  18770. ("Select keyword"
  18771. ["Next keyword" org-shiftright (org-at-heading-p)]
  18772. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  18773. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  18774. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  18775. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  18776. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  18777. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  18778. "--"
  18779. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  18780. :selected org-enforce-todo-dependencies :style toggle :active t]
  18781. "Settings for tree at point"
  18782. ["Do Children sequentially" org-toggle-ordered-property :style radio
  18783. :selected (org-entry-get nil "ORDERED")
  18784. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  18785. ["Do Children parallel" org-toggle-ordered-property :style radio
  18786. :selected (not (org-entry-get nil "ORDERED"))
  18787. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  18788. "--"
  18789. ["Set Priority" org-priority t]
  18790. ["Priority Up" org-shiftup t]
  18791. ["Priority Down" org-shiftdown t]
  18792. "--"
  18793. ["Get news from all feeds" org-feed-update-all t]
  18794. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  18795. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  18796. ("TAGS and Properties"
  18797. ["Set Tags" org-set-tags-command (not (org-before-first-heading-p))]
  18798. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  18799. "--"
  18800. ["Set property" org-set-property (not (org-before-first-heading-p))]
  18801. ["Column view of properties" org-columns t]
  18802. ["Insert Column View DBlock" org-insert-columns-dblock t])
  18803. ("Dates and Scheduling"
  18804. ["Timestamp" org-time-stamp (not (org-before-first-heading-p))]
  18805. ["Timestamp (inactive)" org-time-stamp-inactive (not (org-before-first-heading-p))]
  18806. ("Change Date"
  18807. ["1 Day Later" org-shiftright (org-at-timestamp-p)]
  18808. ["1 Day Earlier" org-shiftleft (org-at-timestamp-p)]
  18809. ["1 ... Later" org-shiftup (org-at-timestamp-p)]
  18810. ["1 ... Earlier" org-shiftdown (org-at-timestamp-p)])
  18811. ["Compute Time Range" org-evaluate-time-range t]
  18812. ["Schedule Item" org-schedule (not (org-before-first-heading-p))]
  18813. ["Deadline" org-deadline (not (org-before-first-heading-p))]
  18814. "--"
  18815. ["Custom time format" org-toggle-time-stamp-overlays
  18816. :style radio :selected org-display-custom-times]
  18817. "--"
  18818. ["Goto Calendar" org-goto-calendar t]
  18819. ["Date from Calendar" org-date-from-calendar t]
  18820. "--"
  18821. ["Start/Restart Timer" org-timer-start t]
  18822. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  18823. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  18824. ["Insert Timer String" org-timer t]
  18825. ["Insert Timer Item" org-timer-item t])
  18826. ("Logging work"
  18827. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  18828. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  18829. ["Clock out" org-clock-out t]
  18830. ["Clock cancel" org-clock-cancel t]
  18831. "--"
  18832. ["Mark as default task" org-clock-mark-default-task t]
  18833. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  18834. ["Goto running clock" org-clock-goto t]
  18835. "--"
  18836. ["Display times" org-clock-display t]
  18837. ["Create clock table" org-clock-report t]
  18838. "--"
  18839. ["Record DONE time"
  18840. (progn (setq org-log-done (not org-log-done))
  18841. (message "Switching to %s will %s record a timestamp"
  18842. (car org-done-keywords)
  18843. (if org-log-done "automatically" "not")))
  18844. :style toggle :selected org-log-done])
  18845. "--"
  18846. ["Agenda Command..." org-agenda t]
  18847. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  18848. ("File List for Agenda")
  18849. ("Special views current file"
  18850. ["TODO Tree" org-show-todo-tree t]
  18851. ["Check Deadlines" org-check-deadlines t]
  18852. ["Timeline" org-timeline t]
  18853. ["Tags/Property tree" org-match-sparse-tree t])
  18854. "--"
  18855. ["Export/Publish..." org-export-dispatch t]
  18856. ("LaTeX"
  18857. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  18858. :selected org-cdlatex-mode]
  18859. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  18860. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  18861. ["Modify math symbol" org-cdlatex-math-modify
  18862. (org-inside-LaTeX-fragment-p)]
  18863. ["Insert citation" org-reftex-citation t]
  18864. "--"
  18865. ["Template for BEAMER" (org-beamer-insert-options-template) t])
  18866. "--"
  18867. ("MobileOrg"
  18868. ["Push Files and Views" org-mobile-push t]
  18869. ["Get Captured and Flagged" org-mobile-pull t]
  18870. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  18871. "--"
  18872. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  18873. "--"
  18874. ("Documentation"
  18875. ["Show Version" org-version t]
  18876. ["Info Documentation" org-info t])
  18877. ("Customize"
  18878. ["Browse Org Group" org-customize t]
  18879. "--"
  18880. ["Expand This Menu" org-create-customize-menu
  18881. (fboundp 'customize-menu-create)])
  18882. ["Send bug report" org-submit-bug-report t]
  18883. "--"
  18884. ("Refresh/Reload"
  18885. ["Refresh setup current buffer" org-mode-restart t]
  18886. ["Reload Org (after update)" org-reload t]
  18887. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x !"])
  18888. ))
  18889. (defun org-info (&optional node)
  18890. "Read documentation for Org-mode in the info system.
  18891. With optional NODE, go directly to that node."
  18892. (interactive)
  18893. (info (format "(org)%s" (or node ""))))
  18894. ;;;###autoload
  18895. (defun org-submit-bug-report ()
  18896. "Submit a bug report on Org-mode via mail.
  18897. Don't hesitate to report any problems or inaccurate documentation.
  18898. If you don't have setup sending mail from (X)Emacs, please copy the
  18899. output buffer into your mail program, as it gives us important
  18900. information about your Org-mode version and configuration."
  18901. (interactive)
  18902. (require 'reporter)
  18903. (org-load-modules-maybe)
  18904. (org-require-autoloaded-modules)
  18905. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  18906. (reporter-submit-bug-report
  18907. "emacs-orgmode@gnu.org"
  18908. (org-version nil 'full)
  18909. (let (list)
  18910. (save-window-excursion
  18911. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  18912. (delete-other-windows)
  18913. (erase-buffer)
  18914. (insert "You are about to submit a bug report to the Org-mode mailing list.
  18915. We would like to add your full Org-mode and Outline configuration to the
  18916. bug report. This greatly simplifies the work of the maintainer and
  18917. other experts on the mailing list.
  18918. HOWEVER, some variables you have customized may contain private
  18919. information. The names of customers, colleagues, or friends, might
  18920. appear in the form of file names, tags, todo states, or search strings.
  18921. If you answer yes to the prompt, you might want to check and remove
  18922. such private information before sending the email.")
  18923. (add-text-properties (point-min) (point-max) '(face org-warning))
  18924. (when (yes-or-no-p "Include your Org-mode configuration ")
  18925. (mapatoms
  18926. (lambda (v)
  18927. (and (boundp v)
  18928. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  18929. (or (and (symbol-value v)
  18930. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  18931. (and
  18932. (get v 'custom-type) (get v 'standard-value)
  18933. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  18934. (push v list)))))
  18935. (kill-buffer (get-buffer "*Warn about privacy*"))
  18936. list))
  18937. nil nil
  18938. "Remember to cover the basics, that is, what you expected to happen and
  18939. what in fact did happen. You don't know how to make a good report? See
  18940. http://orgmode.org/manual/Feedback.html#Feedback
  18941. Your bug report will be posted to the Org-mode mailing list.
  18942. ------------------------------------------------------------------------")
  18943. (save-excursion
  18944. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  18945. (replace-match "\\1Bug: \\3 [\\2]")))))
  18946. (defun org-install-agenda-files-menu ()
  18947. (let ((bl (buffer-list)))
  18948. (save-excursion
  18949. (while bl
  18950. (set-buffer (pop bl))
  18951. (if (derived-mode-p 'org-mode) (setq bl nil)))
  18952. (when (derived-mode-p 'org-mode)
  18953. (easy-menu-change
  18954. '("Org") "File List for Agenda"
  18955. (append
  18956. (list
  18957. ["Edit File List" (org-edit-agenda-file-list) t]
  18958. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  18959. ["Remove Current File from List" org-remove-file t]
  18960. ["Cycle through agenda files" org-cycle-agenda-files t]
  18961. ["Occur in all agenda files" org-occur-in-agenda-files t]
  18962. "--")
  18963. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  18964. ;;;; Documentation
  18965. (defun org-require-autoloaded-modules ()
  18966. (interactive)
  18967. (mapc 'require
  18968. '(org-agenda org-archive org-attach org-clock org-colview org-id
  18969. org-remember org-table org-timer)))
  18970. ;;;###autoload
  18971. (defun org-reload (&optional uncompiled)
  18972. "Reload all org lisp files.
  18973. With prefix arg UNCOMPILED, load the uncompiled versions."
  18974. (interactive "P")
  18975. (require 'loadhist)
  18976. (let* ((org-dir (org-find-library-dir "org"))
  18977. (contrib-dir (or (org-find-library-dir "org-contribdir") org-dir))
  18978. (feature-re "^\\(org\\|ob\\|ox\\)\\(-.*\\)?")
  18979. (remove-re (mapconcat 'identity
  18980. (mapcar (lambda (f) (concat "^" f "$"))
  18981. (list (if (featurep 'xemacs)
  18982. "org-colview"
  18983. "org-colview-xemacs")
  18984. "org" "org-loaddefs" "org-version"))
  18985. "\\|"))
  18986. (feats (delete-dups
  18987. (mapcar 'file-name-sans-extension
  18988. (mapcar 'file-name-nondirectory
  18989. (delq nil
  18990. (mapcar 'feature-file
  18991. features))))))
  18992. (lfeat (append
  18993. (sort
  18994. (setq feats
  18995. (delq nil (mapcar
  18996. (lambda (f)
  18997. (if (and (string-match feature-re f)
  18998. (not (string-match remove-re f)))
  18999. f nil))
  19000. feats)))
  19001. 'string-lessp)
  19002. (list "org-version" "org")))
  19003. (load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  19004. (load-suffixes (if uncompiled (reverse load-suffixes) load-suffixes))
  19005. load-uncore load-misses)
  19006. (setq load-misses
  19007. (delq 't
  19008. (mapcar (lambda (f)
  19009. (or (org-load-noerror-mustsuffix (concat org-dir f))
  19010. (and (string= org-dir contrib-dir)
  19011. (org-load-noerror-mustsuffix (concat contrib-dir f)))
  19012. (and (org-load-noerror-mustsuffix (concat (org-find-library-dir f) f))
  19013. (add-to-list 'load-uncore f 'append)
  19014. 't)
  19015. f))
  19016. lfeat)))
  19017. (if load-uncore
  19018. (message "The following feature%s found in load-path, please check if that's correct:\n%s"
  19019. (if (> (length load-uncore) 1) "s were" " was") load-uncore))
  19020. (if load-misses
  19021. (message "Some error occured while reloading Org feature%s\n%s\nPlease check *Messages*!\n%s"
  19022. (if (> (length load-misses) 1) "s" "") load-misses (org-version nil 'full))
  19023. (message "Successfully reloaded Org\n%s" (org-version nil 'full)))))
  19024. ;;;###autoload
  19025. (defun org-customize ()
  19026. "Call the customize function with org as argument."
  19027. (interactive)
  19028. (org-load-modules-maybe)
  19029. (org-require-autoloaded-modules)
  19030. (customize-browse 'org))
  19031. (defun org-create-customize-menu ()
  19032. "Create a full customization menu for Org-mode, insert it into the menu."
  19033. (interactive)
  19034. (org-load-modules-maybe)
  19035. (org-require-autoloaded-modules)
  19036. (if (fboundp 'customize-menu-create)
  19037. (progn
  19038. (easy-menu-change
  19039. '("Org") "Customize"
  19040. `(["Browse Org group" org-customize t]
  19041. "--"
  19042. ,(customize-menu-create 'org)
  19043. ["Set" Custom-set t]
  19044. ["Save" Custom-save t]
  19045. ["Reset to Current" Custom-reset-current t]
  19046. ["Reset to Saved" Custom-reset-saved t]
  19047. ["Reset to Standard Settings" Custom-reset-standard t]))
  19048. (message "\"Org\"-menu now contains full customization menu"))
  19049. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  19050. ;;;; Miscellaneous stuff
  19051. ;;; Generally useful functions
  19052. (defun org-get-at-bol (property)
  19053. "Get text property PROPERTY at beginning of line."
  19054. (get-text-property (point-at-bol) property))
  19055. (defun org-find-text-property-in-string (prop s)
  19056. "Return the first non-nil value of property PROP in string S."
  19057. (or (get-text-property 0 prop s)
  19058. (get-text-property (or (next-single-property-change 0 prop s) 0)
  19059. prop s)))
  19060. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  19061. "Display the given MESSAGE as a warning."
  19062. (if (fboundp 'display-warning)
  19063. (display-warning 'org message
  19064. (if (featurep 'xemacs) 'warning :warning))
  19065. (let ((buf (get-buffer-create "*Org warnings*")))
  19066. (with-current-buffer buf
  19067. (goto-char (point-max))
  19068. (insert "Warning (Org): " message)
  19069. (unless (bolp)
  19070. (newline)))
  19071. (display-buffer buf)
  19072. (sit-for 0))))
  19073. (defun org-eval (form)
  19074. "Eval FORM and return result."
  19075. (condition-case error
  19076. (eval form)
  19077. (error (format "%%![Error: %s]" error))))
  19078. (defun org-in-clocktable-p ()
  19079. "Check if the cursor is in a clocktable."
  19080. (let ((pos (point)) start)
  19081. (save-excursion
  19082. (end-of-line 1)
  19083. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  19084. (setq start (match-beginning 0))
  19085. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  19086. (>= (match-end 0) pos)
  19087. start))))
  19088. (defun org-in-commented-line ()
  19089. "Is point in a line starting with `#'?"
  19090. (equal (char-after (point-at-bol)) ?#))
  19091. (defun org-in-indented-comment-line ()
  19092. "Is point in a line starting with `#' after some white space?"
  19093. (save-excursion
  19094. (save-match-data
  19095. (goto-char (point-at-bol))
  19096. (looking-at "[ \t]*#"))))
  19097. (defun org-in-verbatim-emphasis ()
  19098. (save-match-data
  19099. (and (org-in-regexp org-emph-re 2)
  19100. (>= (point) (match-beginning 3))
  19101. (<= (point) (match-end 4))
  19102. (member (match-string 3) '("=" "~")))))
  19103. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  19104. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  19105. (if (and marker (marker-buffer marker)
  19106. (buffer-live-p (marker-buffer marker)))
  19107. (progn
  19108. (org-pop-to-buffer-same-window (marker-buffer marker))
  19109. (if (or (> marker (point-max)) (< marker (point-min)))
  19110. (widen))
  19111. (goto-char marker)
  19112. (org-show-context 'org-goto))
  19113. (if bookmark
  19114. (bookmark-jump bookmark)
  19115. (error "Cannot find location"))))
  19116. (defun org-quote-csv-field (s)
  19117. "Quote field for inclusion in CSV material."
  19118. (if (string-match "[\",]" s)
  19119. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  19120. s))
  19121. (defun org-force-self-insert (N)
  19122. "Needed to enforce self-insert under remapping."
  19123. (interactive "p")
  19124. (self-insert-command N))
  19125. (defun org-string-width (s)
  19126. "Compute width of string, ignoring invisible characters.
  19127. This ignores character with invisibility property `org-link', and also
  19128. characters with property `org-cwidth', because these will become invisible
  19129. upon the next fontification round."
  19130. (let (b l)
  19131. (when (or (eq t buffer-invisibility-spec)
  19132. (assq 'org-link buffer-invisibility-spec))
  19133. (while (setq b (text-property-any 0 (length s)
  19134. 'invisible 'org-link s))
  19135. (setq s (concat (substring s 0 b)
  19136. (substring s (or (next-single-property-change
  19137. b 'invisible s) (length s)))))))
  19138. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  19139. (setq s (concat (substring s 0 b)
  19140. (substring s (or (next-single-property-change
  19141. b 'org-cwidth s) (length s))))))
  19142. (setq l (string-width s) b -1)
  19143. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  19144. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  19145. l))
  19146. (defun org-shorten-string (s maxlength)
  19147. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  19148. If the string is shorter or has length MAXLENGTH, just return the
  19149. original string. If it is longer, the functions finds a space in the
  19150. string, breaks this string off at that locations and adds three dots
  19151. as ellipsis. Including the ellipsis, the string will not be longer
  19152. than MAXLENGTH. If finding a good breaking point in the string does
  19153. not work, the string is just chopped off in the middle of a word
  19154. if necessary."
  19155. (if (<= (length s) maxlength)
  19156. s
  19157. (let* ((n (max (- maxlength 4) 1))
  19158. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  19159. (if (string-match re s)
  19160. (concat (match-string 1 s) "...")
  19161. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  19162. (defun org-get-indentation (&optional line)
  19163. "Get the indentation of the current line, interpreting tabs.
  19164. When LINE is given, assume it represents a line and compute its indentation."
  19165. (if line
  19166. (if (string-match "^ *" (org-remove-tabs line))
  19167. (match-end 0))
  19168. (save-excursion
  19169. (beginning-of-line 1)
  19170. (skip-chars-forward " \t")
  19171. (current-column))))
  19172. (defun org-get-string-indentation (s)
  19173. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  19174. (let ((n -1) (i 0) (w tab-width) c)
  19175. (catch 'exit
  19176. (while (< (setq n (1+ n)) (length s))
  19177. (setq c (aref s n))
  19178. (cond ((= c ?\ ) (setq i (1+ i)))
  19179. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  19180. (t (throw 'exit t)))))
  19181. i))
  19182. (defun org-remove-tabs (s &optional width)
  19183. "Replace tabulators in S with spaces.
  19184. Assumes that s is a single line, starting in column 0."
  19185. (setq width (or width tab-width))
  19186. (while (string-match "\t" s)
  19187. (setq s (replace-match
  19188. (make-string
  19189. (- (* width (/ (+ (match-beginning 0) width) width))
  19190. (match-beginning 0)) ?\ )
  19191. t t s)))
  19192. s)
  19193. (defun org-fix-indentation (line ind)
  19194. "Fix indentation in LINE.
  19195. IND is a cons cell with target and minimum indentation.
  19196. If the current indentation in LINE is smaller than the minimum,
  19197. leave it alone. If it is larger than ind, set it to the target."
  19198. (let* ((l (org-remove-tabs line))
  19199. (i (org-get-indentation l))
  19200. (i1 (car ind)) (i2 (cdr ind)))
  19201. (if (>= i i2) (setq l (substring line i2)))
  19202. (if (> i1 0)
  19203. (concat (make-string i1 ?\ ) l)
  19204. l)))
  19205. (defun org-remove-indentation (code &optional n)
  19206. "Remove the maximum common indentation from the lines in CODE.
  19207. N may optionally be the number of spaces to remove."
  19208. (with-temp-buffer
  19209. (insert code)
  19210. (org-do-remove-indentation n)
  19211. (buffer-string)))
  19212. (defun org-do-remove-indentation (&optional n)
  19213. "Remove the maximum common indentation from the buffer."
  19214. (untabify (point-min) (point-max))
  19215. (let ((min 10000) re)
  19216. (if n
  19217. (setq min n)
  19218. (goto-char (point-min))
  19219. (while (re-search-forward "^ *[^ \n]" nil t)
  19220. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  19221. (unless (or (= min 0) (= min 10000))
  19222. (setq re (format "^ \\{%d\\}" min))
  19223. (goto-char (point-min))
  19224. (while (re-search-forward re nil t)
  19225. (replace-match "")
  19226. (end-of-line 1))
  19227. min)))
  19228. (defun org-fill-template (template alist)
  19229. "Find each %key of ALIST in TEMPLATE and replace it."
  19230. (let ((case-fold-search nil)
  19231. entry key value)
  19232. (setq alist (sort (copy-sequence alist)
  19233. (lambda (a b) (< (length (car a)) (length (car b))))))
  19234. (while (setq entry (pop alist))
  19235. (setq template
  19236. (replace-regexp-in-string
  19237. (concat "%" (regexp-quote (car entry)))
  19238. (or (cdr entry) "") template t t)))
  19239. template))
  19240. (defun org-base-buffer (buffer)
  19241. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  19242. (if (not buffer)
  19243. buffer
  19244. (or (buffer-base-buffer buffer)
  19245. buffer)))
  19246. (defun org-trim (s)
  19247. "Remove whitespace at beginning and end of string."
  19248. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  19249. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  19250. s)
  19251. (defun org-wrap (string &optional width lines)
  19252. "Wrap string to either a number of lines, or a width in characters.
  19253. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  19254. that costs. If there is a word longer than WIDTH, the text is actually
  19255. wrapped to the length of that word.
  19256. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  19257. many lines, whatever width that takes.
  19258. The return value is a list of lines, without newlines at the end."
  19259. (let* ((words (org-split-string string "[ \t\n]+"))
  19260. (maxword (apply 'max (mapcar 'org-string-width words)))
  19261. w ll)
  19262. (cond (width
  19263. (org-do-wrap words (max maxword width)))
  19264. (lines
  19265. (setq w maxword)
  19266. (setq ll (org-do-wrap words maxword))
  19267. (if (<= (length ll) lines)
  19268. ll
  19269. (setq ll words)
  19270. (while (> (length ll) lines)
  19271. (setq w (1+ w))
  19272. (setq ll (org-do-wrap words w)))
  19273. ll))
  19274. (t (error "Cannot wrap this")))))
  19275. (defun org-do-wrap (words width)
  19276. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  19277. (let (lines line)
  19278. (while words
  19279. (setq line (pop words))
  19280. (while (and words (< (+ (length line) (length (car words))) width))
  19281. (setq line (concat line " " (pop words))))
  19282. (setq lines (push line lines)))
  19283. (nreverse lines)))
  19284. (defun org-split-string (string &optional separators)
  19285. "Splits STRING into substrings at SEPARATORS.
  19286. No empty strings are returned if there are matches at the beginning
  19287. and end of string."
  19288. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  19289. (start 0)
  19290. notfirst
  19291. (list nil))
  19292. (while (and (string-match rexp string
  19293. (if (and notfirst
  19294. (= start (match-beginning 0))
  19295. (< start (length string)))
  19296. (1+ start) start))
  19297. (< (match-beginning 0) (length string)))
  19298. (setq notfirst t)
  19299. (or (eq (match-beginning 0) 0)
  19300. (and (eq (match-beginning 0) (match-end 0))
  19301. (eq (match-beginning 0) start))
  19302. (setq list
  19303. (cons (substring string start (match-beginning 0))
  19304. list)))
  19305. (setq start (match-end 0)))
  19306. (or (eq start (length string))
  19307. (setq list
  19308. (cons (substring string start)
  19309. list)))
  19310. (nreverse list)))
  19311. (defun org-quote-vert (s)
  19312. "Replace \"|\" with \"\\vert\"."
  19313. (while (string-match "|" s)
  19314. (setq s (replace-match "\\vert" t t s)))
  19315. s)
  19316. (defun org-uuidgen-p (s)
  19317. "Is S an ID created by UUIDGEN?"
  19318. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  19319. (defun org-in-src-block-p (&optional inside)
  19320. "Whether point is in a code source block.
  19321. When INSIDE is non-nil, don't consider we are within a src block
  19322. when point is at #+BEGIN_SRC or #+END_SRC."
  19323. (let ((case-fold-search t) ov)
  19324. (or (and (setq ov (overlays-at (point)))
  19325. (memq 'org-block-background
  19326. (overlay-properties (car ov))))
  19327. (and (not inside)
  19328. (save-match-data
  19329. (save-excursion
  19330. (beginning-of-line)
  19331. (looking-at ".*#\\+\\(begin\\|end\\)_src")))))))
  19332. (defun org-context ()
  19333. "Return a list of contexts of the current cursor position.
  19334. If several contexts apply, all are returned.
  19335. Each context entry is a list with a symbol naming the context, and
  19336. two positions indicating start and end of the context. Possible
  19337. contexts are:
  19338. :headline anywhere in a headline
  19339. :headline-stars on the leading stars in a headline
  19340. :todo-keyword on a TODO keyword (including DONE) in a headline
  19341. :tags on the TAGS in a headline
  19342. :priority on the priority cookie in a headline
  19343. :item on the first line of a plain list item
  19344. :item-bullet on the bullet/number of a plain list item
  19345. :checkbox on the checkbox in a plain list item
  19346. :table in an org-mode table
  19347. :table-special on a special filed in a table
  19348. :table-table in a table.el table
  19349. :clocktable in a clocktable
  19350. :src-block in a source block
  19351. :link on a hyperlink
  19352. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT, QUOTE.
  19353. :target on a <<target>>
  19354. :radio-target on a <<<radio-target>>>
  19355. :latex-fragment on a LaTeX fragment
  19356. :latex-preview on a LaTeX fragment with overlaid preview image
  19357. This function expects the position to be visible because it uses font-lock
  19358. faces as a help to recognize the following contexts: :table-special, :link,
  19359. and :keyword."
  19360. (let* ((f (get-text-property (point) 'face))
  19361. (faces (if (listp f) f (list f)))
  19362. (case-fold-search t)
  19363. (p (point)) clist o)
  19364. ;; First the large context
  19365. (cond
  19366. ((org-at-heading-p t)
  19367. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  19368. (when (progn
  19369. (beginning-of-line 1)
  19370. (looking-at org-todo-line-tags-regexp))
  19371. (push (org-point-in-group p 1 :headline-stars) clist)
  19372. (push (org-point-in-group p 2 :todo-keyword) clist)
  19373. (push (org-point-in-group p 4 :tags) clist))
  19374. (goto-char p)
  19375. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  19376. (if (looking-at "\\[#[A-Z0-9]\\]")
  19377. (push (org-point-in-group p 0 :priority) clist)))
  19378. ((org-at-item-p)
  19379. (push (org-point-in-group p 2 :item-bullet) clist)
  19380. (push (list :item (point-at-bol)
  19381. (save-excursion (org-end-of-item) (point)))
  19382. clist)
  19383. (and (org-at-item-checkbox-p)
  19384. (push (org-point-in-group p 0 :checkbox) clist)))
  19385. ((org-at-table-p)
  19386. (push (list :table (org-table-begin) (org-table-end)) clist)
  19387. (if (memq 'org-formula faces)
  19388. (push (list :table-special
  19389. (previous-single-property-change p 'face)
  19390. (next-single-property-change p 'face)) clist)))
  19391. ((org-at-table-p 'any)
  19392. (push (list :table-table) clist)))
  19393. (goto-char p)
  19394. (let ((case-fold-search t))
  19395. ;; New the "medium" contexts: clocktables, source blocks
  19396. (cond ((org-in-clocktable-p)
  19397. (push (list :clocktable
  19398. (and (or (looking-at "#\\+BEGIN: clocktable")
  19399. (search-backward "#+BEGIN: clocktable" nil t))
  19400. (match-beginning 0))
  19401. (and (re-search-forward "#\\+END:?" nil t)
  19402. (match-end 0))) clist))
  19403. ((org-in-src-block-p)
  19404. (push (list :src-block
  19405. (and (or (looking-at "#\\+BEGIN_SRC")
  19406. (search-backward "#+BEGIN_SRC" nil t))
  19407. (match-beginning 0))
  19408. (and (search-forward "#+END_SRC" nil t)
  19409. (match-beginning 0))) clist))))
  19410. (goto-char p)
  19411. ;; Now the small context
  19412. (cond
  19413. ((org-at-timestamp-p)
  19414. (push (org-point-in-group p 0 :timestamp) clist))
  19415. ((memq 'org-link faces)
  19416. (push (list :link
  19417. (previous-single-property-change p 'face)
  19418. (next-single-property-change p 'face)) clist))
  19419. ((memq 'org-special-keyword faces)
  19420. (push (list :keyword
  19421. (previous-single-property-change p 'face)
  19422. (next-single-property-change p 'face)) clist))
  19423. ((org-at-target-p)
  19424. (push (org-point-in-group p 0 :target) clist)
  19425. (goto-char (1- (match-beginning 0)))
  19426. (if (looking-at org-radio-target-regexp)
  19427. (push (org-point-in-group p 0 :radio-target) clist))
  19428. (goto-char p))
  19429. ((setq o (car (delq nil
  19430. (mapcar
  19431. (lambda (x)
  19432. (if (memq x org-latex-fragment-image-overlays) x))
  19433. (overlays-at (point))))))
  19434. (push (list :latex-fragment
  19435. (overlay-start o) (overlay-end o)) clist)
  19436. (push (list :latex-preview
  19437. (overlay-start o) (overlay-end o)) clist))
  19438. ((org-inside-LaTeX-fragment-p)
  19439. ;; FIXME: positions wrong.
  19440. (push (list :latex-fragment (point) (point)) clist)))
  19441. (setq clist (nreverse (delq nil clist)))
  19442. clist))
  19443. ;; FIXME: Compare with at-regexp-p Do we need both?
  19444. (defun org-in-regexp (re &optional nlines visually)
  19445. "Check if point is inside a match of regexp.
  19446. Normally only the current line is checked, but you can include NLINES extra
  19447. lines both before and after point into the search.
  19448. If VISUALLY is set, require that the cursor is not after the match but
  19449. really on, so that the block visually is on the match."
  19450. (catch 'exit
  19451. (let ((pos (point))
  19452. (eol (point-at-eol (+ 1 (or nlines 0))))
  19453. (inc (if visually 1 0)))
  19454. (save-excursion
  19455. (beginning-of-line (- 1 (or nlines 0)))
  19456. (while (re-search-forward re eol t)
  19457. (if (and (<= (match-beginning 0) pos)
  19458. (>= (+ inc (match-end 0)) pos))
  19459. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  19460. (defun org-at-regexp-p (regexp)
  19461. "Is point inside a match of REGEXP in the current line?"
  19462. (catch 'exit
  19463. (save-excursion
  19464. (let ((pos (point)) (end (point-at-eol)))
  19465. (beginning-of-line 1)
  19466. (while (re-search-forward regexp end t)
  19467. (if (and (<= (match-beginning 0) pos)
  19468. (>= (match-end 0) pos))
  19469. (throw 'exit t)))
  19470. nil))))
  19471. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  19472. "Non-nil when point is between matches of START-RE and END-RE.
  19473. Also return a non-nil value when point is on one of the matches.
  19474. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  19475. buffer positions. Default values are the positions of headlines
  19476. surrounding the point.
  19477. The functions returns a cons cell whose car (resp. cdr) is the
  19478. position before START-RE (resp. after END-RE)."
  19479. (save-match-data
  19480. (let ((pos (point))
  19481. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  19482. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  19483. beg end)
  19484. (save-excursion
  19485. ;; Point is on a block when on START-RE or if START-RE can be
  19486. ;; found before it...
  19487. (and (or (org-at-regexp-p start-re)
  19488. (re-search-backward start-re limit-up t))
  19489. (setq beg (match-beginning 0))
  19490. ;; ... and END-RE after it...
  19491. (goto-char (match-end 0))
  19492. (re-search-forward end-re limit-down t)
  19493. (> (setq end (match-end 0)) pos)
  19494. ;; ... without another START-RE in-between.
  19495. (goto-char (match-beginning 0))
  19496. (not (re-search-backward start-re (1+ beg) t))
  19497. ;; Return value.
  19498. (cons beg end))))))
  19499. (defun org-in-block-p (names)
  19500. "Non-nil when point belongs to a block whose name belongs to NAMES.
  19501. NAMES is a list of strings containing names of blocks.
  19502. Return first block name matched, or nil. Beware that in case of
  19503. nested blocks, the returned name may not belong to the closest
  19504. block from point."
  19505. (save-match-data
  19506. (catch 'exit
  19507. (let ((case-fold-search t)
  19508. (lim-up (save-excursion (outline-previous-heading)))
  19509. (lim-down (save-excursion (outline-next-heading))))
  19510. (mapc (lambda (name)
  19511. (let ((n (regexp-quote name)))
  19512. (when (org-between-regexps-p
  19513. (concat "^[ \t]*#\\+begin_" n)
  19514. (concat "^[ \t]*#\\+end_" n)
  19515. lim-up lim-down)
  19516. (throw 'exit n))))
  19517. names))
  19518. nil)))
  19519. (defun org-occur-in-agenda-files (regexp &optional nlines)
  19520. "Call `multi-occur' with buffers for all agenda files."
  19521. (interactive "sOrg-files matching: \np")
  19522. (let* ((files (org-agenda-files))
  19523. (tnames (mapcar 'file-truename files))
  19524. (extra org-agenda-text-search-extra-files)
  19525. f)
  19526. (when (eq (car extra) 'agenda-archives)
  19527. (setq extra (cdr extra))
  19528. (setq files (org-add-archive-files files)))
  19529. (while (setq f (pop extra))
  19530. (unless (member (file-truename f) tnames)
  19531. (add-to-list 'files f 'append)
  19532. (add-to-list 'tnames (file-truename f) 'append)))
  19533. (multi-occur
  19534. (mapcar (lambda (x)
  19535. (with-current-buffer
  19536. (or (get-file-buffer x) (find-file-noselect x))
  19537. (widen)
  19538. (current-buffer)))
  19539. files)
  19540. regexp)))
  19541. (if (boundp 'occur-mode-find-occurrence-hook)
  19542. ;; Emacs 23
  19543. (add-hook 'occur-mode-find-occurrence-hook
  19544. (lambda ()
  19545. (when (derived-mode-p 'org-mode)
  19546. (org-reveal))))
  19547. ;; Emacs 22
  19548. (defadvice occur-mode-goto-occurrence
  19549. (after org-occur-reveal activate)
  19550. (and (derived-mode-p 'org-mode) (org-reveal)))
  19551. (defadvice occur-mode-goto-occurrence-other-window
  19552. (after org-occur-reveal activate)
  19553. (and (derived-mode-p 'org-mode) (org-reveal)))
  19554. (defadvice occur-mode-display-occurrence
  19555. (after org-occur-reveal activate)
  19556. (when (derived-mode-p 'org-mode)
  19557. (let ((pos (occur-mode-find-occurrence)))
  19558. (with-current-buffer (marker-buffer pos)
  19559. (save-excursion
  19560. (goto-char pos)
  19561. (org-reveal)))))))
  19562. (defun org-occur-link-in-agenda-files ()
  19563. "Create a link and search for it in the agendas.
  19564. The link is not stored in `org-stored-links', it is just created
  19565. for the search purpose."
  19566. (interactive)
  19567. (let ((link (condition-case nil
  19568. (org-store-link nil)
  19569. (error "Unable to create a link to here"))))
  19570. (org-occur-in-agenda-files (regexp-quote link))))
  19571. (defun org-reverse-string (string)
  19572. "Return the reverse of STRING."
  19573. (apply 'string (reverse (string-to-list string))))
  19574. (defun org-uniquify (list)
  19575. "Remove duplicate elements from LIST."
  19576. (let (res)
  19577. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  19578. res))
  19579. (defun org-delete-all (elts list)
  19580. "Remove all elements in ELTS from LIST."
  19581. (while elts
  19582. (setq list (delete (pop elts) list)))
  19583. list)
  19584. (defun org-count (cl-item cl-seq)
  19585. "Count the number of occurrences of ITEM in SEQ.
  19586. Taken from `count' in cl-seq.el with all keyword arguments removed."
  19587. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  19588. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  19589. (while (< cl-start cl-end)
  19590. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  19591. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  19592. (setq cl-start (1+ cl-start)))
  19593. cl-count))
  19594. (defun org-remove-if (predicate seq)
  19595. "Remove everything from SEQ that fulfills PREDICATE."
  19596. (let (res e)
  19597. (while seq
  19598. (setq e (pop seq))
  19599. (if (not (funcall predicate e)) (push e res)))
  19600. (nreverse res)))
  19601. (defun org-remove-if-not (predicate seq)
  19602. "Remove everything from SEQ that does not fulfill PREDICATE."
  19603. (let (res e)
  19604. (while seq
  19605. (setq e (pop seq))
  19606. (if (funcall predicate e) (push e res)))
  19607. (nreverse res)))
  19608. (defun org-reduce (cl-func cl-seq &rest cl-keys)
  19609. "Reduce two-argument FUNCTION across SEQ.
  19610. Taken from `reduce' in cl-seq.el with all keyword arguments but
  19611. \":initial-value\" removed."
  19612. (let ((cl-accum (cond ((memq :initial-value cl-keys)
  19613. (cadr (memq :initial-value cl-keys)))
  19614. (cl-seq (pop cl-seq))
  19615. (t (funcall cl-func)))))
  19616. (while cl-seq
  19617. (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
  19618. cl-accum))
  19619. (defun org-back-over-empty-lines ()
  19620. "Move backwards over whitespace, to the beginning of the first empty line.
  19621. Returns the number of empty lines passed."
  19622. (let ((pos (point)))
  19623. (if (cdr (assoc 'heading org-blank-before-new-entry))
  19624. (skip-chars-backward " \t\n\r")
  19625. (unless (eobp)
  19626. (forward-line -1)))
  19627. (beginning-of-line 2)
  19628. (goto-char (min (point) pos))
  19629. (count-lines (point) pos)))
  19630. (defun org-skip-whitespace ()
  19631. (skip-chars-forward " \t\n\r"))
  19632. (defun org-point-in-group (point group &optional context)
  19633. "Check if POINT is in match-group GROUP.
  19634. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  19635. match. If the match group does not exist or point is not inside it,
  19636. return nil."
  19637. (and (match-beginning group)
  19638. (>= point (match-beginning group))
  19639. (<= point (match-end group))
  19640. (if context
  19641. (list context (match-beginning group) (match-end group))
  19642. t)))
  19643. (defun org-switch-to-buffer-other-window (&rest args)
  19644. "Switch to buffer in a second window on the current frame.
  19645. In particular, do not allow pop-up frames.
  19646. Returns the newly created buffer."
  19647. (org-no-popups
  19648. (apply 'switch-to-buffer-other-window args)))
  19649. (defun org-combine-plists (&rest plists)
  19650. "Create a single property list from all plists in PLISTS.
  19651. The process starts by copying the first list, and then setting properties
  19652. from the other lists. Settings in the last list are the most significant
  19653. ones and overrule settings in the other lists."
  19654. (let ((rtn (copy-sequence (pop plists)))
  19655. p v ls)
  19656. (while plists
  19657. (setq ls (pop plists))
  19658. (while ls
  19659. (setq p (pop ls) v (pop ls))
  19660. (setq rtn (plist-put rtn p v))))
  19661. rtn))
  19662. (defun org-replace-escapes (string table)
  19663. "Replace %-escapes in STRING with values in TABLE.
  19664. TABLE is an association list with keys like \"%a\" and string values.
  19665. The sequences in STRING may contain normal field width and padding information,
  19666. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  19667. so values can contain further %-escapes if they are define later in TABLE."
  19668. (let ((tbl (copy-alist table))
  19669. (case-fold-search nil)
  19670. (pchg 0)
  19671. e re rpl)
  19672. (while (setq e (pop tbl))
  19673. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  19674. (when (and (cdr e) (string-match re (cdr e)))
  19675. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  19676. (safe "SREF"))
  19677. (add-text-properties 0 3 (list 'sref sref) safe)
  19678. (setcdr e (replace-match safe t t (cdr e)))))
  19679. (while (string-match re string)
  19680. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  19681. (cdr e)))
  19682. (setq string (replace-match rpl t t string))))
  19683. (while (setq pchg (next-property-change pchg string))
  19684. (let ((sref (get-text-property pchg 'sref string)))
  19685. (when (and sref (string-match "SREF" string pchg))
  19686. (setq string (replace-match sref t t string)))))
  19687. string))
  19688. (defun org-sublist (list start end)
  19689. "Return a section of LIST, from START to END.
  19690. Counting starts at 1."
  19691. (let (rtn (c start))
  19692. (setq list (nthcdr (1- start) list))
  19693. (while (and list (<= c end))
  19694. (push (pop list) rtn)
  19695. (setq c (1+ c)))
  19696. (nreverse rtn)))
  19697. (defun org-find-base-buffer-visiting (file)
  19698. "Like `find-buffer-visiting' but always return the base buffer and
  19699. not an indirect buffer."
  19700. (let ((buf (or (get-file-buffer file)
  19701. (find-buffer-visiting file))))
  19702. (if buf
  19703. (or (buffer-base-buffer buf) buf)
  19704. nil)))
  19705. (defun org-image-file-name-regexp (&optional extensions)
  19706. "Return regexp matching the file names of images.
  19707. If EXTENSIONS is given, only match these."
  19708. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  19709. (image-file-name-regexp)
  19710. (let ((image-file-name-extensions
  19711. (or extensions
  19712. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  19713. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  19714. (concat "\\."
  19715. (regexp-opt (nconc (mapcar 'upcase
  19716. image-file-name-extensions)
  19717. image-file-name-extensions)
  19718. t)
  19719. "\\'"))))
  19720. (defun org-file-image-p (file &optional extensions)
  19721. "Return non-nil if FILE is an image."
  19722. (save-match-data
  19723. (string-match (org-image-file-name-regexp extensions) file)))
  19724. (defun org-get-cursor-date (&optional with-time)
  19725. "Return the date at cursor in as a time.
  19726. This works in the calendar and in the agenda, anywhere else it just
  19727. returns the current time.
  19728. If WITH-TIME is non-nil, returns the time of the event at point (in
  19729. the agenda) or the current time of the day."
  19730. (let (date day defd tp tm hod mod)
  19731. (when with-time
  19732. (setq tp (get-text-property (point) 'time))
  19733. (when (and tp (string-match "\\([0-9][0-9]\\):\\([0-9][0-9]\\)" tp))
  19734. (setq hod (string-to-number (match-string 1 tp))
  19735. mod (string-to-number (match-string 2 tp))))
  19736. (or tp (setq hod (nth 2 (decode-time (current-time)))
  19737. mod (nth 1 (decode-time (current-time))))))
  19738. (cond
  19739. ((eq major-mode 'calendar-mode)
  19740. (setq date (calendar-cursor-to-date)
  19741. defd (encode-time 0 (or mod 0) (or hod 0)
  19742. (nth 1 date) (nth 0 date) (nth 2 date))))
  19743. ((eq major-mode 'org-agenda-mode)
  19744. (setq day (get-text-property (point) 'day))
  19745. (if day
  19746. (setq date (calendar-gregorian-from-absolute day)
  19747. defd (encode-time 0 (or mod 0) (or hod 0)
  19748. (nth 1 date) (nth 0 date) (nth 2 date))))))
  19749. (or defd (current-time))))
  19750. (defun org-mark-subtree (&optional up)
  19751. "Mark the current subtree.
  19752. This puts point at the start of the current subtree, and mark at
  19753. the end. If a numeric prefix UP is given, move up into the
  19754. hierarchy of headlines by UP levels before marking the subtree."
  19755. (interactive "P")
  19756. (org-with-limited-levels
  19757. (cond ((org-at-heading-p) (beginning-of-line))
  19758. ((org-before-first-heading-p) (error "Not in a subtree"))
  19759. (t (outline-previous-visible-heading 1))))
  19760. (when up (while (and (> up 0) (org-up-heading-safe)) (decf up)))
  19761. (if (org-called-interactively-p 'any)
  19762. (call-interactively 'org-mark-element)
  19763. (org-mark-element)))
  19764. ;;; Indentation
  19765. (defun org-indent-line ()
  19766. "Indent line depending on context."
  19767. (interactive)
  19768. (let* ((pos (point))
  19769. (itemp (org-at-item-p))
  19770. (case-fold-search t)
  19771. (org-drawer-regexp (or org-drawer-regexp "\000"))
  19772. (inline-task-p (and (featurep 'org-inlinetask)
  19773. (org-inlinetask-in-task-p)))
  19774. (inline-re (and inline-task-p
  19775. (org-inlinetask-outline-regexp)))
  19776. column)
  19777. (if (and orgstruct-is-++ (eq pos (point)))
  19778. (let ((indent-line-function (cadadr (assoc 'indent-line-function org-fb-vars))))
  19779. (indent-according-to-mode))
  19780. (beginning-of-line 1)
  19781. (cond
  19782. ;; Headings
  19783. ((looking-at org-outline-regexp) (setq column 0))
  19784. ;; Footnote definition
  19785. ((looking-at org-footnote-definition-re) (setq column 0))
  19786. ;; Literal examples
  19787. ((looking-at "[ \t]*:\\( \\|$\\)")
  19788. (setq column (org-get-indentation))) ; do nothing
  19789. ;; Lists
  19790. ((ignore-errors (goto-char (org-in-item-p)))
  19791. (setq column (if itemp
  19792. (org-get-indentation)
  19793. (org-list-item-body-column (point))))
  19794. (goto-char pos))
  19795. ;; Drawers
  19796. ((and (looking-at "[ \t]*:END:")
  19797. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  19798. (save-excursion
  19799. (goto-char (1- (match-beginning 1)))
  19800. (setq column (current-column))))
  19801. ;; Special blocks
  19802. ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
  19803. (save-excursion
  19804. (re-search-backward
  19805. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  19806. (setq column (org-get-indentation (match-string 0))))
  19807. ((and (not (looking-at "[ \t]*#\\+begin_"))
  19808. (org-between-regexps-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
  19809. (save-excursion
  19810. (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
  19811. (setq column
  19812. (cond ((equal (downcase (match-string 1)) "src")
  19813. ;; src blocks: let `org-edit-src-exit' handle them
  19814. (org-get-indentation))
  19815. ((equal (downcase (match-string 1)) "example")
  19816. (max (org-get-indentation)
  19817. (org-get-indentation (match-string 0))))
  19818. (t
  19819. (org-get-indentation (match-string 0))))))
  19820. ;; This line has nothing special, look at the previous relevant
  19821. ;; line to compute indentation
  19822. (t
  19823. (beginning-of-line 0)
  19824. (while (and (not (bobp))
  19825. (not (looking-at org-table-line-regexp))
  19826. (not (looking-at org-drawer-regexp))
  19827. ;; When point started in an inline task, do not move
  19828. ;; above task starting line.
  19829. (not (and inline-task-p (looking-at inline-re)))
  19830. ;; Skip drawers, blocks, empty lines, verbatim,
  19831. ;; comments, tables, footnotes definitions, lists,
  19832. ;; inline tasks.
  19833. (or (and (looking-at "[ \t]*:END:")
  19834. (re-search-backward org-drawer-regexp nil t))
  19835. (and (looking-at "[ \t]*#\\+end_")
  19836. (re-search-backward "[ \t]*#\\+begin_"nil t))
  19837. (looking-at "[ \t]*[\n:#|]")
  19838. (looking-at org-footnote-definition-re)
  19839. (and (ignore-errors (goto-char (org-in-item-p)))
  19840. (goto-char
  19841. (org-list-get-top-point (org-list-struct))))
  19842. (and (not inline-task-p)
  19843. (featurep 'org-inlinetask)
  19844. (org-inlinetask-in-task-p)
  19845. (or (org-inlinetask-goto-beginning) t))))
  19846. (beginning-of-line 0))
  19847. (cond
  19848. ;; There was an heading above.
  19849. ((looking-at "\\*+[ \t]+")
  19850. (if (not org-adapt-indentation)
  19851. (setq column 0)
  19852. (goto-char (match-end 0))
  19853. (setq column (current-column))))
  19854. ;; A drawer had started and is unfinished
  19855. ((looking-at org-drawer-regexp)
  19856. (goto-char (1- (match-beginning 1)))
  19857. (setq column (current-column)))
  19858. ;; Else, nothing noticeable found: get indentation and go on.
  19859. (t (setq column (org-get-indentation))))))
  19860. ;; Now apply indentation and move cursor accordingly
  19861. (goto-char pos)
  19862. (if (<= (current-column) (current-indentation))
  19863. (org-indent-line-to column)
  19864. (save-excursion (org-indent-line-to column)))
  19865. ;; Special polishing for properties, see `org-property-format'
  19866. (setq column (current-column))
  19867. (beginning-of-line 1)
  19868. (if (looking-at
  19869. "\\([ \t]*\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  19870. (replace-match (concat (match-string 1)
  19871. (format org-property-format
  19872. (match-string 2) (match-string 3)))
  19873. t t))
  19874. (org-move-to-column column))))
  19875. (defun org-indent-drawer ()
  19876. "Indent the drawer at point."
  19877. (interactive)
  19878. (let ((p (point))
  19879. (e (and (save-excursion (re-search-forward ":END:" nil t))
  19880. (match-end 0)))
  19881. (folded
  19882. (save-excursion
  19883. (end-of-line)
  19884. (when (overlays-at (point))
  19885. (member 'invisible (overlay-properties
  19886. (car (overlays-at (point)))))))))
  19887. (when folded (org-cycle))
  19888. (indent-for-tab-command)
  19889. (while (and (move-beginning-of-line 2) (< (point) e))
  19890. (indent-for-tab-command))
  19891. (goto-char p)
  19892. (when folded (org-cycle)))
  19893. (message "Drawer at point indented"))
  19894. (defun org-indent-block ()
  19895. "Indent the block at point."
  19896. (interactive)
  19897. (let ((p (point))
  19898. (case-fold-search t)
  19899. (e (and (save-excursion (re-search-forward "#\\+end_?\\(?:[a-z]+\\)?" nil t))
  19900. (match-end 0)))
  19901. (folded
  19902. (save-excursion
  19903. (end-of-line)
  19904. (when (overlays-at (point))
  19905. (member 'invisible (overlay-properties
  19906. (car (overlays-at (point)))))))))
  19907. (when folded (org-cycle))
  19908. (indent-for-tab-command)
  19909. (while (and (move-beginning-of-line 2) (< (point) e))
  19910. (indent-for-tab-command))
  19911. (goto-char p)
  19912. (when folded (org-cycle)))
  19913. (message "Block at point indented"))
  19914. (defun org-indent-region (start end)
  19915. "Indent region."
  19916. (interactive "r")
  19917. (save-excursion
  19918. (let ((line-end (org-current-line end)))
  19919. (goto-char start)
  19920. (while (< (org-current-line) line-end)
  19921. (cond ((org-in-src-block-p) (org-src-native-tab-command-maybe))
  19922. (t (call-interactively 'org-indent-line)))
  19923. (move-beginning-of-line 2)))))
  19924. ;;; Filling
  19925. ;; We use our own fill-paragraph and auto-fill functions.
  19926. ;; `org-fill-paragraph' relies on adaptive filling and context
  19927. ;; checking. Appropriate `fill-prefix' is computed with
  19928. ;; `org-adaptive-fill-function'.
  19929. ;; `org-auto-fill-function' takes care of auto-filling. It calls
  19930. ;; `do-auto-fill' only on valid areas with `fill-prefix' shadowed with
  19931. ;; `org-adaptive-fill-function' value. Internally,
  19932. ;; `org-comment-line-break-function' breaks the line.
  19933. ;; `org-setup-filling' installs filling and auto-filling related
  19934. ;; variables during `org-mode' initialization.
  19935. (defun org-setup-filling ()
  19936. (interactive)
  19937. ;; Prevent auto-fill from inserting unwanted new items.
  19938. (when (boundp 'fill-nobreak-predicate)
  19939. (org-set-local
  19940. 'fill-nobreak-predicate
  19941. (org-uniquify
  19942. (append fill-nobreak-predicate
  19943. '(org-fill-paragraph-separate-nobreak-p
  19944. org-fill-line-break-nobreak-p
  19945. org-fill-paragraph-with-timestamp-nobreak-p)))))
  19946. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  19947. (org-set-local 'auto-fill-inhibit-regexp nil)
  19948. (org-set-local 'adaptive-fill-function 'org-adaptive-fill-function)
  19949. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  19950. (org-set-local 'comment-line-break-function 'org-comment-line-break-function))
  19951. (defvar org-element-paragraph-separate) ; org-element.el
  19952. (defun org-fill-paragraph-separate-nobreak-p ()
  19953. "Non-nil when a line break at point would insert a new item."
  19954. (looking-at (substring org-element-paragraph-separate 1)))
  19955. (defun org-fill-line-break-nobreak-p ()
  19956. "Non-nil when a line break at point would create an Org line break."
  19957. (save-excursion
  19958. (skip-chars-backward "[ \t]")
  19959. (skip-chars-backward "\\\\")
  19960. (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
  19961. (defun org-fill-paragraph-with-timestamp-nobreak-p ()
  19962. "Non-nil when a line break at point would insert a new item."
  19963. (and (org-at-timestamp-p t)
  19964. (not (looking-at org-ts-regexp-both))))
  19965. (declare-function message-in-body-p "message" ())
  19966. (defvar orgtbl-line-start-regexp) ; From org-table.el
  19967. (defun org-adaptive-fill-function ()
  19968. "Compute a fill prefix for the current line.
  19969. Return fill prefix, as a string, or nil if current line isn't
  19970. meant to be filled."
  19971. (let (prefix)
  19972. (catch 'exit
  19973. (when (derived-mode-p 'message-mode)
  19974. (save-excursion
  19975. (beginning-of-line)
  19976. (cond ((or (not (message-in-body-p))
  19977. (looking-at orgtbl-line-start-regexp))
  19978. (throw 'exit nil))
  19979. ((looking-at message-cite-prefix-regexp)
  19980. (throw 'exit (match-string-no-properties 0)))
  19981. ((looking-at org-outline-regexp)
  19982. (throw 'exit (make-string (length (match-string 0)) ? ))))))
  19983. (org-with-wide-buffer
  19984. (let* ((p (line-beginning-position))
  19985. (element (save-excursion (beginning-of-line)
  19986. (org-element-at-point)))
  19987. (type (org-element-type element))
  19988. (post-affiliated (org-element-property :post-affiliated element)))
  19989. (unless (and post-affiliated (< p post-affiliated))
  19990. (case type
  19991. (comment (looking-at "[ \t]*# ?") (match-string 0))
  19992. (footnote-definition "")
  19993. ((item plain-list)
  19994. (make-string (org-list-item-body-column
  19995. (or post-affiliated
  19996. (org-element-property :begin element)))
  19997. ? ))
  19998. (paragraph
  19999. ;; Fill prefix is usually the same as the current line,
  20000. ;; except if the paragraph is at the beginning of an item.
  20001. (let ((parent (org-element-property :parent element)))
  20002. (cond ((eq (org-element-type parent) 'item)
  20003. (make-string (org-list-item-body-column
  20004. (org-element-property :begin parent))
  20005. ? ))
  20006. ((save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  20007. (match-string 0))
  20008. (t ""))))
  20009. (comment-block
  20010. ;; Only fill contents if P is within block boundaries.
  20011. (let* ((cbeg (save-excursion (goto-char post-affiliated)
  20012. (forward-line)
  20013. (point)))
  20014. (cend (save-excursion
  20015. (goto-char (org-element-property :end element))
  20016. (skip-chars-backward " \r\t\n")
  20017. (line-beginning-position))))
  20018. (when (and (>= p cbeg) (< p cend))
  20019. (if (save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  20020. (match-string 0)
  20021. "")))))))))))
  20022. (declare-function message-goto-body "message" ())
  20023. (defvar message-cite-prefix-regexp) ; From message.el
  20024. (defun org-fill-paragraph (&optional justify)
  20025. "Fill element at point, when applicable.
  20026. This function only applies to comment blocks, comments, example
  20027. blocks and paragraphs. Also, as a special case, re-align table
  20028. when point is at one.
  20029. If JUSTIFY is non-nil (interactively, with prefix argument),
  20030. justify as well. If `sentence-end-double-space' is non-nil, then
  20031. period followed by one space does not end a sentence, so don't
  20032. break a line there. The variable `fill-column' controls the
  20033. width for filling.
  20034. For convenience, when point is at a plain list, an item or
  20035. a footnote definition, try to fill the first paragraph within."
  20036. (interactive)
  20037. (if (and (derived-mode-p 'message-mode)
  20038. (or (not (message-in-body-p))
  20039. (save-excursion (move-beginning-of-line 1)
  20040. (looking-at message-cite-prefix-regexp))))
  20041. ;; First ensure filling is correct in message-mode.
  20042. (let ((fill-paragraph-function
  20043. (cadadr (assoc 'fill-paragraph-function org-fb-vars)))
  20044. (fill-prefix (cadadr (assoc 'fill-prefix org-fb-vars)))
  20045. (paragraph-start (cadadr (assoc 'paragraph-start org-fb-vars)))
  20046. (paragraph-separate
  20047. (cadadr (assoc 'paragraph-separate org-fb-vars))))
  20048. (fill-paragraph nil))
  20049. (with-syntax-table org-mode-transpose-word-syntax-table
  20050. ;; Move to end of line in order to get the first paragraph
  20051. ;; within a plain list or a footnote definition.
  20052. (let ((element (save-excursion (end-of-line) (org-element-at-point))))
  20053. ;; First check if point is in a blank line at the beginning of
  20054. ;; the buffer. In that case, ignore filling.
  20055. (case (org-element-type element)
  20056. ;; Use major mode filling function is src blocks.
  20057. (src-block (org-babel-do-key-sequence-in-edit-buffer (kbd "M-q")))
  20058. ;; Align Org tables, leave table.el tables as-is.
  20059. (table-row (org-table-align) t)
  20060. (table
  20061. (when (eq (org-element-property :type element) 'org)
  20062. (org-table-align))
  20063. t)
  20064. (paragraph
  20065. ;; Paragraphs may contain `line-break' type objects.
  20066. (let ((beg (max (point-min)
  20067. (org-element-property :contents-begin element)))
  20068. (end (min (point-max)
  20069. (org-element-property :contents-end element))))
  20070. ;; Do nothing if point is at an affiliated keyword.
  20071. (if (< (line-end-position) beg) t
  20072. (when (derived-mode-p 'message-mode)
  20073. ;; In `message-mode', do not fill following citation
  20074. ;; in current paragraph nor text before message body.
  20075. (let ((body-start (save-excursion (message-goto-body))))
  20076. (when body-start (setq beg (max body-start beg))))
  20077. (when (save-excursion
  20078. (re-search-forward
  20079. (concat "^" message-cite-prefix-regexp) end t))
  20080. (setq end (match-beginning 0))))
  20081. ;; Fill paragraph, taking line breaks into account.
  20082. ;; For that, slice the paragraph using line breaks as
  20083. ;; separators, and fill the parts in reverse order to
  20084. ;; avoid messing with markers.
  20085. (save-excursion
  20086. (goto-char end)
  20087. (mapc
  20088. (lambda (pos)
  20089. (fill-region-as-paragraph pos (point) justify)
  20090. (goto-char pos))
  20091. ;; Find the list of ending positions for line breaks
  20092. ;; in the current paragraph. Add paragraph
  20093. ;; beginning to include first slice.
  20094. (nreverse
  20095. (cons beg
  20096. (org-element-map
  20097. (org-element--parse-objects
  20098. beg end nil (org-element-restriction 'paragraph))
  20099. 'line-break
  20100. (lambda (lb) (org-element-property :end lb)))))))
  20101. t)))
  20102. ;; Contents of `comment-block' type elements should be
  20103. ;; filled as plain text, but only if point is within block
  20104. ;; markers.
  20105. (comment-block
  20106. (let* ((case-fold-search t)
  20107. (beg (save-excursion
  20108. (goto-char (org-element-property :begin element))
  20109. (re-search-forward "^[ \t]*#\\+begin_comment" nil t)
  20110. (forward-line)
  20111. (point)))
  20112. (end (save-excursion
  20113. (goto-char (org-element-property :end element))
  20114. (re-search-backward "^[ \t]*#\\+end_comment" nil t)
  20115. (line-beginning-position))))
  20116. (when (and (>= (point) beg) (< (point) end))
  20117. (fill-region-as-paragraph
  20118. (save-excursion
  20119. (end-of-line)
  20120. (re-search-backward "^[ \t]*$" beg 'move)
  20121. (line-beginning-position))
  20122. (save-excursion
  20123. (beginning-of-line)
  20124. (re-search-forward "^[ \t]*$" end 'move)
  20125. (line-beginning-position))
  20126. justify)))
  20127. t)
  20128. ;; Fill comments.
  20129. (comment (fill-comment-paragraph justify))
  20130. ;; Ignore every other element.
  20131. (otherwise t))))))
  20132. (defun org-auto-fill-function ()
  20133. "Auto-fill function."
  20134. ;; Check if auto-filling is meaningful.
  20135. (let ((fc (current-fill-column)))
  20136. (when (and fc (> (current-column) fc))
  20137. (let* ((fill-prefix (org-adaptive-fill-function))
  20138. ;; Enforce empty fill prefix, if required. Otherwise, it
  20139. ;; will be computed again.
  20140. (adaptive-fill-mode (not (equal fill-prefix ""))))
  20141. (when fill-prefix (do-auto-fill))))))
  20142. (defun org-comment-line-break-function (&optional soft)
  20143. "Break line at point and indent, continuing comment if within one.
  20144. The inserted newline is marked hard if variable
  20145. `use-hard-newlines' is true, unless optional argument SOFT is
  20146. non-nil."
  20147. (if soft (insert-and-inherit ?\n) (newline 1))
  20148. (save-excursion (forward-char -1) (delete-horizontal-space))
  20149. (delete-horizontal-space)
  20150. (indent-to-left-margin)
  20151. (insert-before-markers-and-inherit fill-prefix))
  20152. ;;; Comments
  20153. ;; Org comments syntax is quite complex. It requires the entire line
  20154. ;; to be just a comment. Also, even with the right syntax at the
  20155. ;; beginning of line, some some elements (i.e. verse-block or
  20156. ;; example-block) don't accept comments. Usual Emacs comment commands
  20157. ;; cannot cope with those requirements. Therefore, Org replaces them.
  20158. ;; Org still relies on `comment-dwim', but cannot trust
  20159. ;; `comment-only-p'. So, `comment-region-function' and
  20160. ;; `uncomment-region-function' both point
  20161. ;; to`org-comment-or-uncomment-region'. Eventually,
  20162. ;; `org-insert-comment' takes care of insertion of comments at the
  20163. ;; beginning of line.
  20164. ;; `org-setup-comments-handling' install comments related variables
  20165. ;; during `org-mode' initialization.
  20166. (defun org-setup-comments-handling ()
  20167. (interactive)
  20168. (org-set-local 'comment-use-syntax nil)
  20169. (org-set-local 'comment-start "# ")
  20170. (org-set-local 'comment-start-skip "^\\s-*#\\(?: \\|$\\)")
  20171. (org-set-local 'comment-insert-comment-function 'org-insert-comment)
  20172. (org-set-local 'comment-region-function 'org-comment-or-uncomment-region)
  20173. (org-set-local 'uncomment-region-function 'org-comment-or-uncomment-region))
  20174. (defun org-insert-comment ()
  20175. "Insert an empty comment above current line.
  20176. If the line is empty, insert comment at its beginning."
  20177. (beginning-of-line)
  20178. (if (looking-at "\\s-*$") (replace-match "") (open-line 1))
  20179. (org-indent-line)
  20180. (insert "# "))
  20181. (defvar comment-empty-lines) ; From newcomment.el.
  20182. (defun org-comment-or-uncomment-region (beg end &rest ignore)
  20183. "Comment or uncomment each non-blank line in the region.
  20184. Uncomment each non-blank line between BEG and END if it only
  20185. contains commented lines. Otherwise, comment them."
  20186. (save-restriction
  20187. ;; Restrict region
  20188. (narrow-to-region (save-excursion (goto-char beg)
  20189. (skip-chars-forward " \r\t\n" end)
  20190. (line-beginning-position))
  20191. (save-excursion (goto-char end)
  20192. (skip-chars-backward " \r\t\n" beg)
  20193. (line-end-position)))
  20194. (let ((uncommentp
  20195. ;; UNCOMMENTP is non-nil when every non blank line between
  20196. ;; BEG and END is a comment.
  20197. (save-excursion
  20198. (goto-char (point-min))
  20199. (while (and (not (eobp))
  20200. (let ((element (org-element-at-point)))
  20201. (and (eq (org-element-type element) 'comment)
  20202. (goto-char (min (point-max)
  20203. (org-element-property
  20204. :end element)))))))
  20205. (eobp))))
  20206. (if uncommentp
  20207. ;; Only blank lines and comments in region: uncomment it.
  20208. (save-excursion
  20209. (goto-char (point-min))
  20210. (while (not (eobp))
  20211. (when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
  20212. (replace-match "" nil nil nil 1))
  20213. (forward-line)))
  20214. ;; Comment each line in region.
  20215. (let ((min-indent (point-max)))
  20216. ;; First find the minimum indentation across all lines.
  20217. (save-excursion
  20218. (goto-char (point-min))
  20219. (while (and (not (eobp)) (not (zerop min-indent)))
  20220. (unless (looking-at "[ \t]*$")
  20221. (setq min-indent (min min-indent (current-indentation))))
  20222. (forward-line)))
  20223. ;; Then loop over all lines.
  20224. (save-excursion
  20225. (goto-char (point-min))
  20226. (while (not (eobp))
  20227. (unless (and (not comment-empty-lines) (looking-at "[ \t]*$"))
  20228. (org-move-to-column min-indent t)
  20229. (insert comment-start))
  20230. (forward-line))))))))
  20231. ;;; Planning
  20232. ;; This section contains tools to operate on timestamp objects, as
  20233. ;; returned by, e.g. `org-element-context'.
  20234. (defun org-timestamp-has-time-p (timestamp)
  20235. "Non-nil when TIMESTAMP has a time specified."
  20236. (org-element-property :hour-start timestamp))
  20237. (defun org-timestamp-format (timestamp format &optional end utc)
  20238. "Format a TIMESTAMP element into a string.
  20239. FORMAT is a format specifier to be passed to
  20240. `format-time-string'.
  20241. When optional argument END is non-nil, use end of date-range or
  20242. time-range, if possible.
  20243. When optional argument UTC is non-nil, time will be expressed as
  20244. Universal Time."
  20245. (format-time-string
  20246. format
  20247. (apply 'encode-time
  20248. (cons 0
  20249. (mapcar
  20250. (lambda (prop) (or (org-element-property prop timestamp) 0))
  20251. (if end '(:minute-end :hour-end :day-end :month-end :year-end)
  20252. '(:minute-start :hour-start :day-start :month-start
  20253. :year-start)))))
  20254. utc))
  20255. (defun org-timestamp-split-range (timestamp &optional end)
  20256. "Extract a timestamp object from a date or time range.
  20257. TIMESTAMP is a timestamp object. END, when non-nil, means extract
  20258. the end of the range. Otherwise, extract its start.
  20259. Return a new timestamp object sharing the same parent as
  20260. TIMESTAMP."
  20261. (let ((type (org-element-property :type timestamp)))
  20262. (if (memq type '(active inactive diary)) timestamp
  20263. (let ((split-ts (list 'timestamp (copy-sequence (nth 1 timestamp)))))
  20264. ;; Set new type.
  20265. (org-element-put-property
  20266. split-ts :type (if (eq type 'active-range) 'active 'inactive))
  20267. ;; Copy start properties over end properties if END is
  20268. ;; non-nil. Otherwise, copy end properties over `start' ones.
  20269. (let ((p-alist '((:minute-start . :minute-end)
  20270. (:hour-start . :hour-end)
  20271. (:day-start . :day-end)
  20272. (:month-start . :month-end)
  20273. (:year-start . :year-end))))
  20274. (dolist (p-cell p-alist)
  20275. (org-element-put-property
  20276. split-ts
  20277. (funcall (if end 'car 'cdr) p-cell)
  20278. (org-element-property
  20279. (funcall (if end 'cdr 'car) p-cell) split-ts)))
  20280. ;; Eventually refresh `:raw-value'.
  20281. (org-element-put-property split-ts :raw-value nil)
  20282. (org-element-put-property
  20283. split-ts :raw-value (org-element-interpret-data split-ts)))))))
  20284. (defun org-timestamp-translate (timestamp &optional boundary)
  20285. "Apply `org-translate-time' on a TIMESTAMP object.
  20286. When optional argument BOUNDARY is non-nil, it is either the
  20287. symbol `start' or `end'. In this case, only translate the
  20288. starting or ending part of TIMESTAMP if it is a date or time
  20289. range. Otherwise, translate both parts."
  20290. (if (and (not boundary)
  20291. (memq (org-element-property :type timestamp)
  20292. '(active-range inactive-range)))
  20293. (concat
  20294. (org-translate-time
  20295. (org-element-property :raw-value
  20296. (org-timestamp-split-range timestamp)))
  20297. "--"
  20298. (org-translate-time
  20299. (org-element-property :raw-value
  20300. (org-timestamp-split-range timestamp t))))
  20301. (org-translate-time
  20302. (org-element-property
  20303. :raw-value
  20304. (if (not boundary) timestamp
  20305. (org-timestamp-split-range timestamp (eq boundary 'end)))))))
  20306. ;;; Other stuff.
  20307. (defun org-toggle-fixed-width-section (arg)
  20308. "Toggle the fixed-width export.
  20309. If there is no active region, the QUOTE keyword at the current headline is
  20310. inserted or removed. When present, it causes the text between this headline
  20311. and the next to be exported as fixed-width text, and unmodified.
  20312. If there is an active region, this command adds or removes a colon as the
  20313. first character of this line. If the first character of a line is a colon,
  20314. this line is also exported in fixed-width font."
  20315. (interactive "P")
  20316. (let* ((cc 0)
  20317. (regionp (org-region-active-p))
  20318. (beg (if regionp (region-beginning) (point)))
  20319. (end (if regionp (region-end)))
  20320. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  20321. (case-fold-search nil)
  20322. (re "[ \t]*\\(:\\(?: \\|$\\)\\)")
  20323. off)
  20324. (if regionp
  20325. (save-excursion
  20326. (goto-char beg)
  20327. (setq cc (current-column))
  20328. (beginning-of-line 1)
  20329. (setq off (looking-at re))
  20330. (while (> nlines 0)
  20331. (setq nlines (1- nlines))
  20332. (beginning-of-line 1)
  20333. (cond
  20334. (arg
  20335. (org-move-to-column cc t)
  20336. (insert ": \n")
  20337. (forward-line -1))
  20338. ((and off (looking-at re))
  20339. (replace-match "" t t nil 1))
  20340. ((not off) (org-move-to-column cc t) (insert ": ")))
  20341. (forward-line 1)))
  20342. (save-excursion
  20343. (org-back-to-heading)
  20344. (cond
  20345. ((looking-at (format org-heading-keyword-regexp-format
  20346. org-quote-string))
  20347. (goto-char (match-end 1))
  20348. (looking-at (concat " +" org-quote-string))
  20349. (replace-match "" t t)
  20350. (when (eolp) (insert " ")))
  20351. ((looking-at org-outline-regexp)
  20352. (goto-char (match-end 0))
  20353. (insert org-quote-string " ")))))))
  20354. (defun org-reftex-citation ()
  20355. "Use reftex-citation to insert a citation into the buffer.
  20356. This looks for a line like
  20357. #+BIBLIOGRAPHY: foo plain option:-d
  20358. and derives from it that foo.bib is the bibliography file relevant
  20359. for this document. It then installs the necessary environment for RefTeX
  20360. to work in this buffer and calls `reftex-citation' to insert a citation
  20361. into the buffer.
  20362. Export of such citations to both LaTeX and HTML is handled by the contributed
  20363. package org-exp-bibtex by Taru Karttunen."
  20364. (interactive)
  20365. (let ((reftex-docstruct-symbol 'rds)
  20366. (reftex-cite-format "\\cite{%l}")
  20367. rds bib)
  20368. (save-excursion
  20369. (save-restriction
  20370. (widen)
  20371. (let ((case-fold-search t)
  20372. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  20373. (if (not (save-excursion
  20374. (or (re-search-forward re nil t)
  20375. (re-search-backward re nil t))))
  20376. (error "No bibliography defined in file")
  20377. (setq bib (concat (match-string 1) ".bib")
  20378. rds (list (list 'bib bib)))))))
  20379. (call-interactively 'reftex-citation)))
  20380. ;;;; Functions extending outline functionality
  20381. (defun org-beginning-of-line (&optional arg)
  20382. "Go to the beginning of the current line. If that is invisible, continue
  20383. to a visible line beginning. This makes the function of C-a more intuitive.
  20384. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  20385. first attempt, and only move to after the tags when the cursor is already
  20386. beyond the end of the headline."
  20387. (interactive "P")
  20388. (let ((pos (point))
  20389. (special (if (consp org-special-ctrl-a/e)
  20390. (car org-special-ctrl-a/e)
  20391. org-special-ctrl-a/e))
  20392. refpos)
  20393. (if (org-bound-and-true-p visual-line-mode)
  20394. (beginning-of-visual-line 1)
  20395. (beginning-of-line 1))
  20396. (if (and arg (fboundp 'move-beginning-of-line))
  20397. (call-interactively 'move-beginning-of-line)
  20398. (if (bobp)
  20399. nil
  20400. (backward-char 1)
  20401. (if (org-truely-invisible-p)
  20402. (while (and (not (bobp)) (org-truely-invisible-p))
  20403. (backward-char 1)
  20404. (beginning-of-line 1))
  20405. (forward-char 1))))
  20406. (when special
  20407. (cond
  20408. ((and (looking-at org-complex-heading-regexp)
  20409. (= (char-after (match-end 1)) ?\ ))
  20410. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  20411. (point-at-eol)))
  20412. (goto-char
  20413. (if (eq special t)
  20414. (cond ((> pos refpos) refpos)
  20415. ((= pos (point)) refpos)
  20416. (t (point)))
  20417. (cond ((> pos (point)) (point))
  20418. ((not (eq last-command this-command)) (point))
  20419. (t refpos)))))
  20420. ((org-at-item-p)
  20421. ;; Being at an item and not looking at an the item means point
  20422. ;; was previously moved to beginning of a visual line, which
  20423. ;; doesn't contain the item. Therefore, do nothing special,
  20424. ;; just stay here.
  20425. (when (looking-at org-list-full-item-re)
  20426. ;; Set special position at first white space character after
  20427. ;; bullet, and check-box, if any.
  20428. (let ((after-bullet
  20429. (let ((box (match-end 3)))
  20430. (if (not box) (match-end 1)
  20431. (let ((after (char-after box)))
  20432. (if (and after (= after ? )) (1+ box) box))))))
  20433. ;; Special case: Move point to special position when
  20434. ;; currently after it or at beginning of line.
  20435. (if (eq special t)
  20436. (when (or (> pos after-bullet) (= (point) pos))
  20437. (goto-char after-bullet))
  20438. ;; Reversed case: Move point to special position when
  20439. ;; point was already at beginning of line and command is
  20440. ;; repeated.
  20441. (when (and (= (point) pos) (eq last-command this-command))
  20442. (goto-char after-bullet))))))))
  20443. (org-no-warnings
  20444. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  20445. (defun org-end-of-line (&optional arg)
  20446. "Go to the end of the line.
  20447. If this is a headline, and `org-special-ctrl-a/e' is set, ignore
  20448. tags on the first attempt, and only move to after the tags when
  20449. the cursor is already beyond the end of the headline."
  20450. (interactive "P")
  20451. (let ((special (if (consp org-special-ctrl-a/e) (cdr org-special-ctrl-a/e)
  20452. org-special-ctrl-a/e))
  20453. (move-fun (cond ((org-bound-and-true-p visual-line-mode)
  20454. 'end-of-visual-line)
  20455. ((fboundp 'move-end-of-line) 'move-end-of-line)
  20456. (t 'end-of-line))))
  20457. (if (or (not special) arg) (call-interactively move-fun)
  20458. (let* ((element (save-excursion (beginning-of-line)
  20459. (org-element-at-point)))
  20460. (type (org-element-type element)))
  20461. (cond
  20462. ((memq type '(headline inlinetask))
  20463. (let ((pos (point)))
  20464. (beginning-of-line 1)
  20465. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  20466. (if (eq special t)
  20467. (if (or (< pos (match-beginning 1)) (= pos (match-end 0)))
  20468. (goto-char (match-beginning 1))
  20469. (goto-char (match-end 0)))
  20470. (if (or (< pos (match-end 0))
  20471. (not (eq this-command last-command)))
  20472. (goto-char (match-end 0))
  20473. (goto-char (match-beginning 1))))
  20474. (call-interactively move-fun))))
  20475. ((org-element-property :hiddenp element)
  20476. ;; If element is hidden, `move-end-of-line' would put point
  20477. ;; after it. Use `end-of-line' to stay on current line.
  20478. (call-interactively 'end-of-line))
  20479. (t (call-interactively move-fun)))))
  20480. (org-no-warnings (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  20481. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  20482. (define-key org-mode-map "\C-e" 'org-end-of-line)
  20483. (defun org-backward-sentence (&optional arg)
  20484. "Go to beginning of sentence, or beginning of table field.
  20485. This will call `backward-sentence' or `org-table-beginning-of-field',
  20486. depending on context."
  20487. (interactive "P")
  20488. (cond
  20489. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  20490. (t (call-interactively 'backward-sentence))))
  20491. (defun org-forward-sentence (&optional arg)
  20492. "Go to end of sentence, or end of table field.
  20493. This will call `forward-sentence' or `org-table-end-of-field',
  20494. depending on context."
  20495. (interactive "P")
  20496. (cond
  20497. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  20498. (t (call-interactively 'forward-sentence))))
  20499. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  20500. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  20501. (defun org-kill-line (&optional arg)
  20502. "Kill line, to tags or end of line."
  20503. (interactive "P")
  20504. (cond
  20505. ((or (not org-special-ctrl-k)
  20506. (bolp)
  20507. (not (org-at-heading-p)))
  20508. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  20509. org-ctrl-k-protect-subtree)
  20510. (if (or (eq org-ctrl-k-protect-subtree 'error)
  20511. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  20512. (error "C-k aborted - would kill hidden subtree")))
  20513. (call-interactively
  20514. (if (org-bound-and-true-p visual-line-mode) 'kill-visual-line 'kill-line)))
  20515. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  20516. (kill-region (point) (match-beginning 1))
  20517. (org-set-tags nil t))
  20518. (t (kill-region (point) (point-at-eol)))))
  20519. (define-key org-mode-map "\C-k" 'org-kill-line)
  20520. (defun org-yank (&optional arg)
  20521. "Yank. If the kill is a subtree, treat it specially.
  20522. This command will look at the current kill and check if is a single
  20523. subtree, or a series of subtrees[1]. If it passes the test, and if the
  20524. cursor is at the beginning of a line or after the stars of a currently
  20525. empty headline, then the yank is handled specially. How exactly depends
  20526. on the value of the following variables, both set by default.
  20527. org-yank-folded-subtrees
  20528. When set, the subtree(s) will be folded after insertion, but only
  20529. if doing so would now swallow text after the yanked text.
  20530. org-yank-adjusted-subtrees
  20531. When set, the subtree will be promoted or demoted in order to
  20532. fit into the local outline tree structure, which means that the level
  20533. will be adjusted so that it becomes the smaller one of the two
  20534. *visible* surrounding headings.
  20535. Any prefix to this command will cause `yank' to be called directly with
  20536. no special treatment. In particular, a simple \\[universal-argument] prefix \
  20537. will just
  20538. plainly yank the text as it is.
  20539. \[1] The test checks if the first non-white line is a heading
  20540. and if there are no other headings with fewer stars."
  20541. (interactive "P")
  20542. (org-yank-generic 'yank arg))
  20543. (defun org-yank-generic (command arg)
  20544. "Perform some yank-like command.
  20545. This function implements the behavior described in the `org-yank'
  20546. documentation. However, it has been generalized to work for any
  20547. interactive command with similar behavior."
  20548. ;; pretend to be command COMMAND
  20549. (setq this-command command)
  20550. (if arg
  20551. (call-interactively command)
  20552. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  20553. (and (org-kill-is-subtree-p)
  20554. (or (bolp)
  20555. (and (looking-at "[ \t]*$")
  20556. (string-match
  20557. "\\`\\*+\\'"
  20558. (buffer-substring (point-at-bol) (point)))))))
  20559. swallowp)
  20560. (cond
  20561. ((and subtreep org-yank-folded-subtrees)
  20562. (let ((beg (point))
  20563. end)
  20564. (if (and subtreep org-yank-adjusted-subtrees)
  20565. (org-paste-subtree nil nil 'for-yank)
  20566. (call-interactively command))
  20567. (setq end (point))
  20568. (goto-char beg)
  20569. (when (and (bolp) subtreep
  20570. (not (setq swallowp
  20571. (org-yank-folding-would-swallow-text beg end))))
  20572. (org-with-limited-levels
  20573. (or (looking-at org-outline-regexp)
  20574. (re-search-forward org-outline-regexp-bol end t))
  20575. (while (and (< (point) end) (looking-at org-outline-regexp))
  20576. (hide-subtree)
  20577. (org-cycle-show-empty-lines 'folded)
  20578. (condition-case nil
  20579. (outline-forward-same-level 1)
  20580. (error (goto-char end))))))
  20581. (when swallowp
  20582. (message
  20583. "Inserted text not folded because that would swallow text"))
  20584. (goto-char end)
  20585. (skip-chars-forward " \t\n\r")
  20586. (beginning-of-line 1)
  20587. (push-mark beg 'nomsg)))
  20588. ((and subtreep org-yank-adjusted-subtrees)
  20589. (let ((beg (point-at-bol)))
  20590. (org-paste-subtree nil nil 'for-yank)
  20591. (push-mark beg 'nomsg)))
  20592. (t
  20593. (call-interactively command))))))
  20594. (defun org-yank-folding-would-swallow-text (beg end)
  20595. "Would hide-subtree at BEG swallow any text after END?"
  20596. (let (level)
  20597. (org-with-limited-levels
  20598. (save-excursion
  20599. (goto-char beg)
  20600. (when (or (looking-at org-outline-regexp)
  20601. (re-search-forward org-outline-regexp-bol end t))
  20602. (setq level (org-outline-level)))
  20603. (goto-char end)
  20604. (skip-chars-forward " \t\r\n\v\f")
  20605. (if (or (eobp)
  20606. (and (bolp) (looking-at org-outline-regexp)
  20607. (<= (org-outline-level) level)))
  20608. nil ; Nothing would be swallowed
  20609. t))))) ; something would swallow
  20610. (define-key org-mode-map "\C-y" 'org-yank)
  20611. (defun org-truely-invisible-p ()
  20612. "Check if point is at a character currently not visible.
  20613. This version does not only check the character property, but also
  20614. `visible-mode'."
  20615. ;; Early versions of noutline don't have `outline-invisible-p'.
  20616. (if (org-bound-and-true-p visible-mode)
  20617. nil
  20618. (outline-invisible-p)))
  20619. (defun org-invisible-p2 ()
  20620. "Check if point is at a character currently not visible."
  20621. (save-excursion
  20622. (if (and (eolp) (not (bobp))) (backward-char 1))
  20623. ;; Early versions of noutline don't have `outline-invisible-p'.
  20624. (outline-invisible-p)))
  20625. (defun org-back-to-heading (&optional invisible-ok)
  20626. "Call `outline-back-to-heading', but provide a better error message."
  20627. (condition-case nil
  20628. (outline-back-to-heading invisible-ok)
  20629. (error (error "Before first headline at position %d in buffer %s"
  20630. (point) (current-buffer)))))
  20631. (defun org-before-first-heading-p ()
  20632. "Before first heading?"
  20633. (save-excursion
  20634. (end-of-line)
  20635. (null (re-search-backward org-outline-regexp-bol nil t))))
  20636. (defun org-at-heading-p (&optional ignored)
  20637. (outline-on-heading-p t))
  20638. ;; Compatibility alias with Org versions < 7.8.03
  20639. (defalias 'org-on-heading-p 'org-at-heading-p)
  20640. (defun org-at-comment-p nil
  20641. "Is cursor in a line starting with a # character?"
  20642. (save-excursion
  20643. (beginning-of-line)
  20644. (looking-at "^#")))
  20645. (defun org-at-drawer-p nil
  20646. "Is cursor at a drawer keyword?"
  20647. (save-excursion
  20648. (move-beginning-of-line 1)
  20649. (looking-at org-drawer-regexp)))
  20650. (defun org-at-block-p nil
  20651. "Is cursor at a block keyword?"
  20652. (save-excursion
  20653. (move-beginning-of-line 1)
  20654. (looking-at org-block-regexp)))
  20655. (defun org-point-at-end-of-empty-headline ()
  20656. "If point is at the end of an empty headline, return t, else nil.
  20657. If the heading only contains a TODO keyword, it is still still considered
  20658. empty."
  20659. (and (looking-at "[ \t]*$")
  20660. (when org-todo-line-regexp
  20661. (save-excursion
  20662. (beginning-of-line 1)
  20663. (let ((case-fold-search nil))
  20664. (looking-at org-todo-line-regexp)
  20665. (string= (match-string 3) ""))))))
  20666. (defun org-at-heading-or-item-p ()
  20667. (or (org-at-heading-p) (org-at-item-p)))
  20668. (defun org-at-target-p ()
  20669. (or (org-in-regexp org-radio-target-regexp)
  20670. (org-in-regexp org-target-regexp)))
  20671. ;; Compatibility alias with Org versions < 7.8.03
  20672. (defalias 'org-on-target-p 'org-at-target-p)
  20673. (defun org-up-heading-all (arg)
  20674. "Move to the heading line of which the present line is a subheading.
  20675. This function considers both visible and invisible heading lines.
  20676. With argument, move up ARG levels."
  20677. (if (fboundp 'outline-up-heading-all)
  20678. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  20679. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  20680. (defun org-up-heading-safe ()
  20681. "Move to the heading line of which the present line is a subheading.
  20682. This version will not throw an error. It will return the level of the
  20683. headline found, or nil if no higher level is found.
  20684. Also, this function will be a lot faster than `outline-up-heading',
  20685. because it relies on stars being the outline starters. This can really
  20686. make a significant difference in outlines with very many siblings."
  20687. (let (start-level re)
  20688. (org-back-to-heading t)
  20689. (setq start-level (funcall outline-level))
  20690. (if (equal start-level 1)
  20691. nil
  20692. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  20693. (if (re-search-backward re nil t)
  20694. (funcall outline-level)))))
  20695. (defun org-first-sibling-p ()
  20696. "Is this heading the first child of its parents?"
  20697. (interactive)
  20698. (let ((re org-outline-regexp-bol)
  20699. level l)
  20700. (unless (org-at-heading-p t)
  20701. (error "Not at a heading"))
  20702. (setq level (funcall outline-level))
  20703. (save-excursion
  20704. (if (not (re-search-backward re nil t))
  20705. t
  20706. (setq l (funcall outline-level))
  20707. (< l level)))))
  20708. (defun org-goto-sibling (&optional previous)
  20709. "Goto the next sibling, even if it is invisible.
  20710. When PREVIOUS is set, go to the previous sibling instead. Returns t
  20711. when a sibling was found. When none is found, return nil and don't
  20712. move point."
  20713. (let ((fun (if previous 're-search-backward 're-search-forward))
  20714. (pos (point))
  20715. (re org-outline-regexp-bol)
  20716. level l)
  20717. (when (condition-case nil (org-back-to-heading t) (error nil))
  20718. (setq level (funcall outline-level))
  20719. (catch 'exit
  20720. (or previous (forward-char 1))
  20721. (while (funcall fun re nil t)
  20722. (setq l (funcall outline-level))
  20723. (when (< l level) (goto-char pos) (throw 'exit nil))
  20724. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  20725. (goto-char pos)
  20726. nil))))
  20727. (defun org-show-siblings ()
  20728. "Show all siblings of the current headline."
  20729. (save-excursion
  20730. (while (org-goto-sibling) (org-flag-heading nil)))
  20731. (save-excursion
  20732. (while (org-goto-sibling 'previous)
  20733. (org-flag-heading nil))))
  20734. (defun org-goto-first-child ()
  20735. "Goto the first child, even if it is invisible.
  20736. Return t when a child was found. Otherwise don't move point and
  20737. return nil."
  20738. (let (level (pos (point)) (re org-outline-regexp-bol))
  20739. (when (condition-case nil (org-back-to-heading t) (error nil))
  20740. (setq level (outline-level))
  20741. (forward-char 1)
  20742. (if (and (re-search-forward re nil t) (> (outline-level) level))
  20743. (progn (goto-char (match-beginning 0)) t)
  20744. (goto-char pos) nil))))
  20745. (defun org-show-hidden-entry ()
  20746. "Show an entry where even the heading is hidden."
  20747. (save-excursion
  20748. (org-show-entry)))
  20749. (defun org-flag-heading (flag &optional entry)
  20750. "Flag the current heading. FLAG non-nil means make invisible.
  20751. When ENTRY is non-nil, show the entire entry."
  20752. (save-excursion
  20753. (org-back-to-heading t)
  20754. ;; Check if we should show the entire entry
  20755. (if entry
  20756. (progn
  20757. (org-show-entry)
  20758. (save-excursion
  20759. (and (outline-next-heading)
  20760. (org-flag-heading nil))))
  20761. (outline-flag-region (max (point-min) (1- (point)))
  20762. (save-excursion (outline-end-of-heading) (point))
  20763. flag))))
  20764. (defun org-get-next-sibling ()
  20765. "Move to next heading of the same level, and return point.
  20766. If there is no such heading, return nil.
  20767. This is like outline-next-sibling, but invisible headings are ok."
  20768. (let ((level (funcall outline-level)))
  20769. (outline-next-heading)
  20770. (while (and (not (eobp)) (> (funcall outline-level) level))
  20771. (outline-next-heading))
  20772. (if (or (eobp) (< (funcall outline-level) level))
  20773. nil
  20774. (point))))
  20775. (defun org-get-last-sibling ()
  20776. "Move to previous heading of the same level, and return point.
  20777. If there is no such heading, return nil."
  20778. (let ((opoint (point))
  20779. (level (funcall outline-level)))
  20780. (outline-previous-heading)
  20781. (when (and (/= (point) opoint) (outline-on-heading-p t))
  20782. (while (and (> (funcall outline-level) level)
  20783. (not (bobp)))
  20784. (outline-previous-heading))
  20785. (if (< (funcall outline-level) level)
  20786. nil
  20787. (point)))))
  20788. (defun org-end-of-subtree (&optional invisible-ok to-heading)
  20789. "Goto to the end of a subtree."
  20790. ;; This contains an exact copy of the original function, but it uses
  20791. ;; `org-back-to-heading', to make it work also in invisible
  20792. ;; trees. And is uses an invisible-ok argument.
  20793. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  20794. ;; Furthermore, when used inside Org, finding the end of a large subtree
  20795. ;; with many children and grandchildren etc, this can be much faster
  20796. ;; than the outline version.
  20797. (org-back-to-heading invisible-ok)
  20798. (let ((first t)
  20799. (level (funcall outline-level)))
  20800. (if (and (derived-mode-p 'org-mode) (< level 1000))
  20801. ;; A true heading (not a plain list item), in Org-mode
  20802. ;; This means we can easily find the end by looking
  20803. ;; only for the right number of stars. Using a regexp to do
  20804. ;; this is so much faster than using a Lisp loop.
  20805. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  20806. (forward-char 1)
  20807. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  20808. ;; something else, do it the slow way
  20809. (while (and (not (eobp))
  20810. (or first (> (funcall outline-level) level)))
  20811. (setq first nil)
  20812. (outline-next-heading)))
  20813. (unless to-heading
  20814. (if (memq (preceding-char) '(?\n ?\^M))
  20815. (progn
  20816. ;; Go to end of line before heading
  20817. (forward-char -1)
  20818. (if (memq (preceding-char) '(?\n ?\^M))
  20819. ;; leave blank line before heading
  20820. (forward-char -1))))))
  20821. (point))
  20822. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  20823. "Use Org version in org-mode, for dramatic speed-up."
  20824. (if (derived-mode-p 'org-mode)
  20825. (progn
  20826. (org-end-of-subtree nil t)
  20827. (unless (eobp) (backward-char 1)))
  20828. ad-do-it))
  20829. (defun org-end-of-meta-data-and-drawers ()
  20830. "Jump to the first text after meta data and drawers in the current entry.
  20831. This will move over empty lines, lines with planning time stamps,
  20832. clocking lines, and drawers."
  20833. (org-back-to-heading t)
  20834. (let ((end (save-excursion (outline-next-heading) (point)))
  20835. (re (concat "\\(" org-drawer-regexp "\\)"
  20836. "\\|" "[ \t]*" org-keyword-time-regexp)))
  20837. (forward-line 1)
  20838. (while (re-search-forward re end t)
  20839. (if (not (match-end 1))
  20840. ;; empty or planning line
  20841. (forward-line 1)
  20842. ;; a drawer, find the end
  20843. (re-search-forward "^[ \t]*:END:" end 'move)
  20844. (forward-line 1)))
  20845. (and (re-search-forward "[^\n]" nil t) (backward-char 1))
  20846. (point)))
  20847. (defun org-forward-heading-same-level (arg &optional invisible-ok)
  20848. "Move forward to the ARG'th subheading at same level as this one.
  20849. Stop at the first and last subheadings of a superior heading.
  20850. Normally this only looks at visible headings, but when INVISIBLE-OK is
  20851. non-nil it will also look at invisible ones."
  20852. (interactive "p")
  20853. (if (not (ignore-errors (org-back-to-heading invisible-ok)))
  20854. (if (and arg (< arg 0))
  20855. (goto-char (point-min))
  20856. (outline-next-heading))
  20857. (org-at-heading-p)
  20858. (let ((level (- (match-end 0) (match-beginning 0) 1))
  20859. (f (if (and arg (< arg 0))
  20860. 're-search-backward
  20861. 're-search-forward))
  20862. (count (if arg (abs arg) 1))
  20863. (result (point)))
  20864. (forward-char (if (and arg (< arg 0)) -1 1))
  20865. (while (and (> count 0)
  20866. (funcall f org-outline-regexp-bol nil 'move))
  20867. (let ((l (- (match-end 0) (match-beginning 0) 1)))
  20868. (cond ((< l level) (setq count 0))
  20869. ((and (= l level)
  20870. (or invisible-ok
  20871. (progn
  20872. (goto-char (line-beginning-position))
  20873. (not (outline-invisible-p)))))
  20874. (setq count (1- count))
  20875. (when (eq l level)
  20876. (setq result (point)))))))
  20877. (goto-char result))
  20878. (beginning-of-line 1)))
  20879. (defun org-backward-heading-same-level (arg &optional invisible-ok)
  20880. "Move backward to the ARG'th subheading at same level as this one.
  20881. Stop at the first and last subheadings of a superior heading."
  20882. (interactive "p")
  20883. (org-forward-heading-same-level (if arg (- arg) -1) invisible-ok))
  20884. (defun org-next-block (arg &optional backward block-regexp)
  20885. "Jump to the next block.
  20886. With a prefix argument ARG, jump forward ARG many source blocks.
  20887. When BACKWARD is non-nil, jump to the previous block.
  20888. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  20889. (interactive "p")
  20890. (let ((re (or block-regexp org-block-regexp))
  20891. (re-search-fn (or (and backward 're-search-backward)
  20892. 're-search-forward)))
  20893. (if (looking-at re) (forward-char 1))
  20894. (condition-case nil
  20895. (funcall re-search-fn re nil nil arg)
  20896. (error (error "No %s code blocks" (if backward "previous" "further" ))))
  20897. (goto-char (match-beginning 0)) (org-show-context)))
  20898. (defun org-previous-block (arg &optional block-regexp)
  20899. "Jump to the previous block.
  20900. With a prefix argument ARG, jump backward ARG many source blocks.
  20901. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  20902. (interactive "p")
  20903. (org-next-block arg t block-regexp))
  20904. (defun org-forward-element ()
  20905. "Move forward by one element.
  20906. Move to the next element at the same level, when possible."
  20907. (interactive)
  20908. (cond ((eobp) (user-error "Cannot move further down"))
  20909. ((org-with-limited-levels (org-at-heading-p))
  20910. (let ((origin (point)))
  20911. (goto-char (org-end-of-subtree nil t))
  20912. (unless (org-with-limited-levels (org-at-heading-p))
  20913. (goto-char origin)
  20914. (user-error "Cannot move further down"))))
  20915. (t
  20916. (let* ((elem (org-element-at-point))
  20917. (end (org-element-property :end elem))
  20918. (parent (org-element-property :parent elem)))
  20919. (if (and parent (= (org-element-property :contents-end parent) end))
  20920. (goto-char (org-element-property :end parent))
  20921. (goto-char end))))))
  20922. (defun org-backward-element ()
  20923. "Move backward by one element.
  20924. Move to the previous element at the same level, when possible."
  20925. (interactive)
  20926. (cond ((bobp) (user-error "Cannot move further up"))
  20927. ((org-with-limited-levels (org-at-heading-p))
  20928. ;; At a headline, move to the previous one, if any, or stay
  20929. ;; here.
  20930. (let ((origin (point)))
  20931. (org-with-limited-levels (org-backward-heading-same-level 1))
  20932. ;; When current headline has no sibling above, move to its
  20933. ;; parent.
  20934. (when (= (point) origin)
  20935. (or (org-with-limited-levels (org-up-heading-safe))
  20936. (progn (goto-char origin)
  20937. (user-error "Cannot move further up"))))))
  20938. (t
  20939. (let* ((trail (org-element-at-point 'keep-trail))
  20940. (elem (car trail))
  20941. (prev-elem (nth 1 trail))
  20942. (beg (org-element-property :begin elem)))
  20943. (cond
  20944. ;; Move to beginning of current element if point isn't
  20945. ;; there already.
  20946. ((/= (point) beg) (goto-char beg))
  20947. (prev-elem (goto-char (org-element-property :begin prev-elem)))
  20948. ((org-before-first-heading-p) (goto-char (point-min)))
  20949. (t (org-back-to-heading)))))))
  20950. (defun org-up-element ()
  20951. "Move to upper element."
  20952. (interactive)
  20953. (if (org-with-limited-levels (org-at-heading-p))
  20954. (unless (org-up-heading-safe) (error "No surrounding element"))
  20955. (let* ((elem (org-element-at-point))
  20956. (parent (org-element-property :parent elem)))
  20957. (if parent (goto-char (org-element-property :begin parent))
  20958. (if (org-with-limited-levels (org-before-first-heading-p))
  20959. (error "No surrounding element")
  20960. (org-with-limited-levels (org-back-to-heading)))))))
  20961. (defvar org-element-greater-elements)
  20962. (defun org-down-element ()
  20963. "Move to inner element."
  20964. (interactive)
  20965. (let ((element (org-element-at-point)))
  20966. (cond
  20967. ((memq (org-element-type element) '(plain-list table))
  20968. (goto-char (org-element-property :contents-begin element))
  20969. (forward-char))
  20970. ((memq (org-element-type element) org-element-greater-elements)
  20971. ;; If contents are hidden, first disclose them.
  20972. (when (org-element-property :hiddenp element) (org-cycle))
  20973. (goto-char (or (org-element-property :contents-begin element)
  20974. (error "No content for this element"))))
  20975. (t (error "No inner element")))))
  20976. (defun org-drag-element-backward ()
  20977. "Move backward element at point."
  20978. (interactive)
  20979. (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
  20980. (let* ((trail (org-element-at-point 'keep-trail))
  20981. (elem (car trail))
  20982. (prev-elem (nth 1 trail)))
  20983. ;; Error out if no previous element or previous element is
  20984. ;; a parent of the current one.
  20985. (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
  20986. (error "Cannot drag element backward")
  20987. (let ((pos (point)))
  20988. (org-element-swap-A-B prev-elem elem)
  20989. (goto-char (+ (org-element-property :begin prev-elem)
  20990. (- pos (org-element-property :begin elem)))))))))
  20991. (defun org-drag-element-forward ()
  20992. "Move forward element at point."
  20993. (interactive)
  20994. (let* ((pos (point))
  20995. (elem (org-element-at-point)))
  20996. (when (= (point-max) (org-element-property :end elem))
  20997. (error "Cannot drag element forward"))
  20998. (goto-char (org-element-property :end elem))
  20999. (let ((next-elem (org-element-at-point)))
  21000. (when (or (org-element-nested-p elem next-elem)
  21001. (and (eq (org-element-type next-elem) 'headline)
  21002. (not (eq (org-element-type elem) 'headline))))
  21003. (goto-char pos)
  21004. (error "Cannot drag element forward"))
  21005. ;; Compute new position of point: it's shifted by NEXT-ELEM
  21006. ;; body's length (without final blanks) and by the length of
  21007. ;; blanks between ELEM and NEXT-ELEM.
  21008. (let ((size-next (- (save-excursion
  21009. (goto-char (org-element-property :end next-elem))
  21010. (skip-chars-backward " \r\t\n")
  21011. (forward-line)
  21012. ;; Small correction if buffer doesn't end
  21013. ;; with a newline character.
  21014. (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
  21015. (org-element-property :begin next-elem)))
  21016. (size-blank (- (org-element-property :end elem)
  21017. (save-excursion
  21018. (goto-char (org-element-property :end elem))
  21019. (skip-chars-backward " \r\t\n")
  21020. (forward-line)
  21021. (point)))))
  21022. (org-element-swap-A-B elem next-elem)
  21023. (goto-char (+ pos size-next size-blank))))))
  21024. (defun org-drag-line-forward (arg)
  21025. "Drag the line at point ARG lines forward."
  21026. (interactive "p")
  21027. (dotimes (n (abs arg))
  21028. (let ((c (current-column)))
  21029. (if (< 0 arg)
  21030. (progn
  21031. (beginning-of-line 2)
  21032. (transpose-lines 1)
  21033. (beginning-of-line 0))
  21034. (transpose-lines 1)
  21035. (beginning-of-line -1))
  21036. (org-move-to-column c))))
  21037. (defun org-drag-line-backward (arg)
  21038. "Drag the line at point ARG lines backward."
  21039. (interactive "p")
  21040. (org-drag-line-forward (- arg)))
  21041. (defun org-mark-element ()
  21042. "Put point at beginning of this element, mark at end.
  21043. Interactively, if this command is repeated or (in Transient Mark
  21044. mode) if the mark is active, it marks the next element after the
  21045. ones already marked."
  21046. (interactive)
  21047. (let (deactivate-mark)
  21048. (if (and (org-called-interactively-p 'any)
  21049. (or (and (eq last-command this-command) (mark t))
  21050. (and transient-mark-mode mark-active)))
  21051. (set-mark
  21052. (save-excursion
  21053. (goto-char (mark))
  21054. (goto-char (org-element-property :end (org-element-at-point)))))
  21055. (let ((element (org-element-at-point)))
  21056. (end-of-line)
  21057. (push-mark (org-element-property :end element) t t)
  21058. (goto-char (org-element-property :begin element))))))
  21059. (defun org-narrow-to-element ()
  21060. "Narrow buffer to current element."
  21061. (interactive)
  21062. (let ((elem (org-element-at-point)))
  21063. (cond
  21064. ((eq (car elem) 'headline)
  21065. (narrow-to-region
  21066. (org-element-property :begin elem)
  21067. (org-element-property :end elem)))
  21068. ((memq (car elem) org-element-greater-elements)
  21069. (narrow-to-region
  21070. (org-element-property :contents-begin elem)
  21071. (org-element-property :contents-end elem)))
  21072. (t
  21073. (narrow-to-region
  21074. (org-element-property :begin elem)
  21075. (org-element-property :end elem))))))
  21076. (defun org-transpose-element ()
  21077. "Transpose current and previous elements, keeping blank lines between.
  21078. Point is moved after both elements."
  21079. (interactive)
  21080. (org-skip-whitespace)
  21081. (let ((end (org-element-property :end (org-element-at-point))))
  21082. (org-drag-element-backward)
  21083. (goto-char end)))
  21084. (defun org-unindent-buffer ()
  21085. "Un-indent the visible part of the buffer.
  21086. Relative indentation (between items, inside blocks, etc.) isn't
  21087. modified."
  21088. (interactive)
  21089. (unless (eq major-mode 'org-mode)
  21090. (error "Cannot un-indent a buffer not in Org mode"))
  21091. (let* ((parse-tree (org-element-parse-buffer 'greater-element))
  21092. unindent-tree ; For byte-compiler.
  21093. (unindent-tree
  21094. (function
  21095. (lambda (contents)
  21096. (mapc
  21097. (lambda (element)
  21098. (if (memq (org-element-type element) '(headline section))
  21099. (funcall unindent-tree (org-element-contents element))
  21100. (save-excursion
  21101. (save-restriction
  21102. (narrow-to-region
  21103. (org-element-property :begin element)
  21104. (org-element-property :end element))
  21105. (org-do-remove-indentation)))))
  21106. (reverse contents))))))
  21107. (funcall unindent-tree (org-element-contents parse-tree))))
  21108. (defun org-show-subtree ()
  21109. "Show everything after this heading at deeper levels."
  21110. (interactive)
  21111. (outline-flag-region
  21112. (point)
  21113. (save-excursion
  21114. (org-end-of-subtree t t))
  21115. nil))
  21116. (defun org-show-entry ()
  21117. "Show the body directly following this heading.
  21118. Show the heading too, if it is currently invisible."
  21119. (interactive)
  21120. (save-excursion
  21121. (condition-case nil
  21122. (progn
  21123. (org-back-to-heading t)
  21124. (outline-flag-region
  21125. (max (point-min) (1- (point)))
  21126. (save-excursion
  21127. (if (re-search-forward
  21128. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  21129. (match-beginning 1)
  21130. (point-max)))
  21131. nil)
  21132. (org-cycle-hide-drawers 'children))
  21133. (error nil))))
  21134. (defun org-make-options-regexp (kwds &optional extra)
  21135. "Make a regular expression for keyword lines."
  21136. (concat
  21137. "^#\\+\\("
  21138. (mapconcat 'regexp-quote kwds "\\|")
  21139. (if extra (concat "\\|" extra))
  21140. "\\):[ \t]*\\(.*\\)"))
  21141. ;; Make isearch reveal the necessary context
  21142. (defun org-isearch-end ()
  21143. "Reveal context after isearch exits."
  21144. (when isearch-success ; only if search was successful
  21145. (if (featurep 'xemacs)
  21146. ;; Under XEmacs, the hook is run in the correct place,
  21147. ;; we directly show the context.
  21148. (org-show-context 'isearch)
  21149. ;; In Emacs the hook runs *before* restoring the overlays.
  21150. ;; So we have to use a one-time post-command-hook to do this.
  21151. ;; (Emacs 22 has a special variable, see function `org-mode')
  21152. (unless (and (boundp 'isearch-mode-end-hook-quit)
  21153. isearch-mode-end-hook-quit)
  21154. ;; Only when the isearch was not quitted.
  21155. (org-add-hook 'post-command-hook 'org-isearch-post-command
  21156. 'append 'local)))
  21157. (org-fix-ellipsis-at-bol)))
  21158. (defun org-isearch-post-command ()
  21159. "Remove self from hook, and show context."
  21160. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  21161. (org-show-context 'isearch))
  21162. ;;;; Integration with and fixes for other packages
  21163. ;;; Imenu support
  21164. (defvar org-imenu-markers nil
  21165. "All markers currently used by Imenu.")
  21166. (make-variable-buffer-local 'org-imenu-markers)
  21167. (defun org-imenu-new-marker (&optional pos)
  21168. "Return a new marker for use by Imenu, and remember the marker."
  21169. (let ((m (make-marker)))
  21170. (move-marker m (or pos (point)))
  21171. (push m org-imenu-markers)
  21172. m))
  21173. (defun org-imenu-get-tree ()
  21174. "Produce the index for Imenu."
  21175. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  21176. (setq org-imenu-markers nil)
  21177. (let* ((n org-imenu-depth)
  21178. (re (concat "^" (org-get-limited-outline-regexp)))
  21179. (subs (make-vector (1+ n) nil))
  21180. (last-level 0)
  21181. m level head0 head)
  21182. (save-excursion
  21183. (save-restriction
  21184. (widen)
  21185. (goto-char (point-max))
  21186. (while (re-search-backward re nil t)
  21187. (setq level (org-reduced-level (funcall outline-level)))
  21188. (when (and (<= level n)
  21189. (looking-at org-complex-heading-regexp)
  21190. (setq head0 (org-match-string-no-properties 4)))
  21191. (setq head (org-link-display-format head0)
  21192. m (org-imenu-new-marker))
  21193. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  21194. (if (>= level last-level)
  21195. (push (cons head m) (aref subs level))
  21196. (push (cons head (aref subs (1+ level))) (aref subs level))
  21197. (loop for i from (1+ level) to n do (aset subs i nil)))
  21198. (setq last-level level)))))
  21199. (aref subs 1)))
  21200. (eval-after-load "imenu"
  21201. '(progn
  21202. (add-hook 'imenu-after-jump-hook
  21203. (lambda ()
  21204. (if (derived-mode-p 'org-mode)
  21205. (org-show-context 'org-goto))))))
  21206. (defun org-link-display-format (link)
  21207. "Replace a link with either the description, or the link target
  21208. if no description is present"
  21209. (save-match-data
  21210. (if (string-match org-bracket-link-analytic-regexp link)
  21211. (replace-match (if (match-end 5)
  21212. (match-string 5 link)
  21213. (concat (match-string 1 link)
  21214. (match-string 3 link)))
  21215. nil t link)
  21216. link)))
  21217. (defun org-toggle-link-display ()
  21218. "Toggle the literal or descriptive display of links."
  21219. (interactive)
  21220. (if org-descriptive-links
  21221. (progn (org-remove-from-invisibility-spec '(org-link))
  21222. (org-restart-font-lock)
  21223. (setq org-descriptive-links nil))
  21224. (progn (add-to-invisibility-spec '(org-link))
  21225. (org-restart-font-lock)
  21226. (setq org-descriptive-links t))))
  21227. ;; Speedbar support
  21228. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  21229. "Overlay marking the agenda restriction line in speedbar.")
  21230. (overlay-put org-speedbar-restriction-lock-overlay
  21231. 'face 'org-agenda-restriction-lock)
  21232. (overlay-put org-speedbar-restriction-lock-overlay
  21233. 'help-echo "Agendas are currently limited to this item.")
  21234. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  21235. (defun org-speedbar-set-agenda-restriction ()
  21236. "Restrict future agenda commands to the location at point in speedbar.
  21237. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  21238. (interactive)
  21239. (require 'org-agenda)
  21240. (let (p m tp np dir txt)
  21241. (cond
  21242. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  21243. 'org-imenu t))
  21244. (setq m (get-text-property p 'org-imenu-marker))
  21245. (with-current-buffer (marker-buffer m)
  21246. (goto-char m)
  21247. (org-agenda-set-restriction-lock 'subtree)))
  21248. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  21249. 'speedbar-function 'speedbar-find-file))
  21250. (setq tp (previous-single-property-change
  21251. (1+ p) 'speedbar-function)
  21252. np (next-single-property-change
  21253. tp 'speedbar-function)
  21254. dir (speedbar-line-directory)
  21255. txt (buffer-substring-no-properties (or tp (point-min))
  21256. (or np (point-max))))
  21257. (with-current-buffer (find-file-noselect
  21258. (let ((default-directory dir))
  21259. (expand-file-name txt)))
  21260. (unless (derived-mode-p 'org-mode)
  21261. (error "Cannot restrict to non-Org-mode file"))
  21262. (org-agenda-set-restriction-lock 'file)))
  21263. (t (error "Don't know how to restrict Org-mode's agenda")))
  21264. (move-overlay org-speedbar-restriction-lock-overlay
  21265. (point-at-bol) (point-at-eol))
  21266. (setq current-prefix-arg nil)
  21267. (org-agenda-maybe-redo)))
  21268. (eval-after-load "speedbar"
  21269. '(progn
  21270. (speedbar-add-supported-extension ".org")
  21271. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  21272. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  21273. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  21274. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  21275. (add-hook 'speedbar-visiting-tag-hook
  21276. (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
  21277. ;;; Fixes and Hacks for problems with other packages
  21278. ;; Make flyspell not check words in links, to not mess up our keymap
  21279. (defvar org-element-affiliated-keywords) ; From org-element.el
  21280. (defvar org-element-block-name-alist) ; From org-element.el
  21281. (defun org-mode-flyspell-verify ()
  21282. "Don't let flyspell put overlays at active buttons, or on
  21283. {todo,all-time,additional-option-like}-keywords."
  21284. (let ((pos (max (1- (point)) (point-min)))
  21285. (word (thing-at-point 'word)))
  21286. (and (not (get-text-property pos 'keymap))
  21287. (not (get-text-property pos 'org-no-flyspell))
  21288. (not (member word org-todo-keywords-1))
  21289. (not (member word org-all-time-keywords))
  21290. (not (member word org-options-keywords))
  21291. (not (member word (mapcar 'car org-startup-options)))
  21292. (not (member-ignore-case word org-element-affiliated-keywords))
  21293. (not (member-ignore-case word (org-get-export-keywords)))
  21294. (not (member-ignore-case
  21295. word (mapcar 'car org-element-block-name-alist)))
  21296. (not (member-ignore-case word '("BEGIN" "END" "ATTR"))))))
  21297. (defun org-remove-flyspell-overlays-in (beg end)
  21298. "Remove flyspell overlays in region."
  21299. (and (org-bound-and-true-p flyspell-mode)
  21300. (fboundp 'flyspell-delete-region-overlays)
  21301. (flyspell-delete-region-overlays beg end))
  21302. (add-text-properties beg end '(org-no-flyspell t)))
  21303. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  21304. (eval-after-load "bookmark"
  21305. '(if (boundp 'bookmark-after-jump-hook)
  21306. ;; We can use the hook
  21307. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  21308. ;; Hook not available, use advice
  21309. (defadvice bookmark-jump (after org-make-visible activate)
  21310. "Make the position visible."
  21311. (org-bookmark-jump-unhide))))
  21312. ;; Make sure saveplace shows the location if it was hidden
  21313. (eval-after-load "saveplace"
  21314. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  21315. "Make the position visible."
  21316. (org-bookmark-jump-unhide)))
  21317. ;; Make sure ecb shows the location if it was hidden
  21318. (eval-after-load "ecb"
  21319. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  21320. "Make hierarchy visible when jumping into location from ECB tree buffer."
  21321. (if (derived-mode-p 'org-mode)
  21322. (org-show-context))))
  21323. (defun org-bookmark-jump-unhide ()
  21324. "Unhide the current position, to show the bookmark location."
  21325. (and (derived-mode-p 'org-mode)
  21326. (or (outline-invisible-p)
  21327. (save-excursion (goto-char (max (point-min) (1- (point))))
  21328. (outline-invisible-p)))
  21329. (org-show-context 'bookmark-jump)))
  21330. ;; Make session.el ignore our circular variable
  21331. (eval-after-load "session"
  21332. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  21333. ;;;; Experimental code
  21334. (defun org-closed-in-range ()
  21335. "Sparse tree of items closed in a certain time range.
  21336. Still experimental, may disappear in the future."
  21337. (interactive)
  21338. ;; Get the time interval from the user.
  21339. (let* ((time1 (org-float-time
  21340. (org-read-date nil 'to-time nil "Starting date: ")))
  21341. (time2 (org-float-time
  21342. (org-read-date nil 'to-time nil "End date:")))
  21343. ;; callback function
  21344. (callback (lambda ()
  21345. (let ((time
  21346. (org-float-time
  21347. (apply 'encode-time
  21348. (org-parse-time-string
  21349. (match-string 1))))))
  21350. ;; check if time in interval
  21351. (and (>= time time1) (<= time time2))))))
  21352. ;; make tree, check each match with the callback
  21353. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  21354. ;;;; Finish up
  21355. (provide 'org)
  21356. (run-hooks 'org-load-hook)
  21357. ;;; org.el ends here