org.el 894 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096230972309823099231002310123102231032310423105231062310723108231092311023111231122311323114231152311623117231182311923120231212312223123231242312523126231272312823129231302313123132231332313423135231362313723138231392314023141231422314323144231452314623147231482314923150231512315223153231542315523156231572315823159231602316123162231632316423165231662316723168231692317023171231722317323174231752317623177231782317923180231812318223183231842318523186231872318823189231902319123192231932319423195231962319723198231992320023201232022320323204232052320623207232082320923210232112321223213232142321523216232172321823219232202322123222232232322423225232262322723228232292323023231232322323323234232352323623237232382323923240232412324223243232442324523246232472324823249232502325123252232532325423255232562325723258232592326023261232622326323264232652326623267232682326923270232712327223273232742327523276232772327823279232802328123282232832328423285232862328723288232892329023291232922329323294232952329623297232982329923300233012330223303233042330523306233072330823309233102331123312233132331423315233162331723318233192332023321233222332323324233252332623327233282332923330233312333223333233342333523336233372333823339233402334123342233432334423345233462334723348233492335023351233522335323354233552335623357233582335923360233612336223363233642336523366233672336823369233702337123372233732337423375233762337723378233792338023381233822338323384233852338623387233882338923390233912339223393233942339523396233972339823399234002340123402234032340423405234062340723408234092341023411234122341323414234152341623417234182341923420234212342223423234242342523426234272342823429234302343123432234332343423435234362343723438234392344023441234422344323444234452344623447234482344923450234512345223453234542345523456234572345823459234602346123462234632346423465234662346723468234692347023471234722347323474234752347623477234782347923480234812348223483234842348523486234872348823489234902349123492234932349423495234962349723498234992350023501235022350323504235052350623507235082350923510235112351223513235142351523516235172351823519235202352123522235232352423525235262352723528235292353023531235322353323534235352353623537235382353923540235412354223543235442354523546235472354823549235502355123552235532355423555235562355723558235592356023561235622356323564235652356623567235682356923570235712357223573235742357523576235772357823579235802358123582235832358423585235862358723588235892359023591235922359323594235952359623597235982359923600236012360223603236042360523606236072360823609236102361123612236132361423615236162361723618236192362023621236222362323624236252362623627236282362923630236312363223633236342363523636236372363823639236402364123642236432364423645236462364723648236492365023651236522365323654236552365623657236582365923660236612366223663236642366523666236672366823669
  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: Carsten Dominik <carsten at orgmode 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. ;;; Commentary:
  24. ;;
  25. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  26. ;; project planning with a fast and effective plain-text system.
  27. ;;
  28. ;; Org-mode develops organizational tasks around NOTES files that contain
  29. ;; information about projects as plain text. Org-mode is implemented on
  30. ;; top of outline-mode, which makes it possible to keep the content of
  31. ;; large files well structured. Visibility cycling and structure editing
  32. ;; help to work with the tree. Tables are easily created with a built-in
  33. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  34. ;; and scheduling. It dynamically compiles entries into an agenda that
  35. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  36. ;; Plain text URL-like links connect to websites, emails, Usenet
  37. ;; messages, BBDB entries, and any files related to the projects. For
  38. ;; printing and sharing of notes, an Org-mode file can be exported as a
  39. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  40. ;; iCalendar file. It can also serve as a publishing tool for a set of
  41. ;; linked webpages.
  42. ;;
  43. ;; Installation and Activation
  44. ;; ---------------------------
  45. ;; See the corresponding sections in the manual at
  46. ;;
  47. ;; http://orgmode.org/org.html#Installation
  48. ;;
  49. ;; Documentation
  50. ;; -------------
  51. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  52. ;; distribution also contains a PDF version of it. At the homepage of
  53. ;; Org-mode, you can read the same text online as HTML. There is also an
  54. ;; excellent reference card made by Philip Rooke. This card can be found
  55. ;; in the etc/ directory of Emacs 22.
  56. ;;
  57. ;; A list of recent changes can be found at
  58. ;; http://orgmode.org/Changes.html
  59. ;;
  60. ;;; Code:
  61. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  62. (defvar org-table-formula-constants-local nil
  63. "Local version of `org-table-formula-constants'.")
  64. (make-variable-buffer-local 'org-table-formula-constants-local)
  65. ;;;; Require other packages
  66. (eval-when-compile
  67. (require 'cl)
  68. (require 'gnus-sum))
  69. (require 'calendar)
  70. (require 'find-func)
  71. (require 'format-spec)
  72. (load "org-loaddefs.el" t t t)
  73. (require 'org-macs)
  74. (require 'org-compat)
  75. ;; `org-outline-regexp' ought to be a defconst but is let-binding in
  76. ;; some places -- e.g. see the macro `org-with-limited-levels'.
  77. ;;
  78. ;; In Org buffers, the value of `outline-regexp' is that of
  79. ;; `org-outline-regexp'. The only function still directly relying on
  80. ;; `outline-regexp' is `org-overview' so that `org-cycle' can do its
  81. ;; job when `orgstruct-mode' is active.
  82. (defvar org-outline-regexp "\\*+ "
  83. "Regexp to match Org headlines.")
  84. (defvar org-outline-regexp-bol "^\\*+ "
  85. "Regexp to match Org headlines.
  86. This is similar to `org-outline-regexp' but additionally makes
  87. sure that we are at the beginning of the line.")
  88. (defvar org-heading-regexp "^\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  89. "Matches a headline, putting stars and text into groups.
  90. Stars are put in group 1 and the trimmed body in group 2.")
  91. ;; Emacs 22 calendar compatibility: Make sure the new variables are available
  92. (unless (boundp 'calendar-view-holidays-initially-flag)
  93. (org-defvaralias 'calendar-view-holidays-initially-flag
  94. 'view-calendar-holidays-initially))
  95. (unless (boundp 'calendar-view-diary-initially-flag)
  96. (org-defvaralias 'calendar-view-diary-initially-flag
  97. 'view-diary-entries-initially))
  98. (unless (boundp 'diary-fancy-buffer)
  99. (org-defvaralias 'diary-fancy-buffer 'fancy-diary-buffer))
  100. (declare-function org-inlinetask-at-task-p "org-inlinetask" ())
  101. (declare-function org-inlinetask-outline-regexp "org-inlinetask" ())
  102. (declare-function org-inlinetask-toggle-visibility "org-inlinetask" ())
  103. (declare-function org-pop-to-buffer-same-window "org-compat" (&optional buffer-or-name norecord label))
  104. (declare-function org-clock-get-last-clock-out-time "org-clock" ())
  105. (declare-function org-clock-timestamps-up "org-clock" (&optional n))
  106. (declare-function org-clock-timestamps-down "org-clock" (&optional n))
  107. (declare-function org-clock-sum-current-item "org-clock" (&optional tstart))
  108. (declare-function orgtbl-mode "org-table" (&optional arg))
  109. (declare-function org-clock-out "org-clock" (&optional switch-to-state fail-quietly at-time))
  110. (declare-function org-beamer-mode "ox-beamer" ())
  111. (declare-function org-table-edit-field "org-table" (arg))
  112. (declare-function org-table-justify-field-maybe "org-table" (&optional new))
  113. (declare-function org-table-set-constants "org-table" ())
  114. (declare-function org-table-calc-current-TBLFM "org-table" (&optional arg))
  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-agenda-redo "org-agenda" (&optional all))
  120. (declare-function org-table-align "org-table" ())
  121. (declare-function org-table-paste-rectangle "org-table" ())
  122. (declare-function org-table-maybe-eval-formula "org-table" ())
  123. (declare-function org-table-maybe-recalculate-line "org-table" ())
  124. (declare-function org-element--parse-objects "org-element"
  125. (beg end acc restriction))
  126. (declare-function org-element-at-point "org-element" (&optional keep-trail))
  127. (declare-function org-element-contents "org-element" (element))
  128. (declare-function org-element-context "org-element" (&optional element))
  129. (declare-function org-element-interpret-data "org-element"
  130. (data &optional parent))
  131. (declare-function org-element-map "org-element"
  132. (data types fun &optional info first-match no-recursion))
  133. (declare-function org-element-nested-p "org-element" (elem-a elem-b))
  134. (declare-function org-element-parse-buffer "org-element"
  135. (&optional granularity visible-only))
  136. (declare-function org-element-property "org-element" (property element))
  137. (declare-function org-element-put-property "org-element"
  138. (element property value))
  139. (declare-function org-element-swap-A-B "org-element" (elem-a elem-b))
  140. (declare-function org-element--parse-objects "org-element"
  141. (beg end acc restriction))
  142. (declare-function org-element-parse-buffer "org-element"
  143. (&optional granularity visible-only))
  144. (declare-function org-element-restriction "org-element" (element))
  145. (declare-function org-element-type "org-element" (element))
  146. ;; load languages based on value of `org-babel-load-languages'
  147. (defvar org-babel-load-languages)
  148. ;;;###autoload
  149. (defun org-babel-do-load-languages (sym value)
  150. "Load the languages defined in `org-babel-load-languages'."
  151. (set-default sym value)
  152. (mapc (lambda (pair)
  153. (let ((active (cdr pair)) (lang (symbol-name (car pair))))
  154. (if active
  155. (progn
  156. (require (intern (concat "ob-" lang))))
  157. (progn
  158. (funcall 'fmakunbound
  159. (intern (concat "org-babel-execute:" lang)))
  160. (funcall 'fmakunbound
  161. (intern (concat "org-babel-expand-body:" lang)))))))
  162. org-babel-load-languages))
  163. ;;;###autoload
  164. (defun org-babel-load-file (file &optional compile)
  165. "Load Emacs Lisp source code blocks in the Org-mode FILE.
  166. This function exports the source code using `org-babel-tangle'
  167. and then loads the resulting file using `load-file'. With prefix
  168. arg (noninteractively: 2nd arg) COMPILE the tangled Emacs Lisp
  169. file to byte-code before it is loaded."
  170. (interactive "fFile to load: \nP")
  171. (require 'ob-core)
  172. (let* ((age (lambda (file)
  173. (float-time
  174. (time-subtract (current-time)
  175. (nth 5 (or (file-attributes (file-truename file))
  176. (file-attributes file)))))))
  177. (base-name (file-name-sans-extension file))
  178. (exported-file (concat base-name ".el")))
  179. ;; tangle if the org-mode file is newer than the elisp file
  180. (unless (and (file-exists-p exported-file)
  181. (> (funcall age file) (funcall age exported-file)))
  182. (org-babel-tangle-file file exported-file "emacs-lisp"))
  183. (message "%s %s"
  184. (if compile
  185. (progn (byte-compile-file exported-file 'load)
  186. "Compiled and loaded")
  187. (progn (load-file exported-file) "Loaded"))
  188. exported-file)))
  189. (defcustom org-babel-load-languages '((emacs-lisp . t))
  190. "Languages which can be evaluated in Org-mode buffers.
  191. This list can be used to load support for any of the languages
  192. below, note that each language will depend on a different set of
  193. system executables and/or Emacs modes. When a language is
  194. \"loaded\", then code blocks in that language can be evaluated
  195. with `org-babel-execute-src-block' bound by default to C-c
  196. C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
  197. be set to remove code block evaluation from the C-c C-c
  198. keybinding. By default only Emacs Lisp (which has no
  199. requirements) is loaded."
  200. :group 'org-babel
  201. :set 'org-babel-do-load-languages
  202. :version "24.1"
  203. :type '(alist :tag "Babel Languages"
  204. :key-type
  205. (choice
  206. (const :tag "Awk" awk)
  207. (const :tag "C" C)
  208. (const :tag "R" R)
  209. (const :tag "Asymptote" asymptote)
  210. (const :tag "Calc" calc)
  211. (const :tag "Clojure" clojure)
  212. (const :tag "CSS" css)
  213. (const :tag "Ditaa" ditaa)
  214. (const :tag "Dot" dot)
  215. (const :tag "Emacs Lisp" emacs-lisp)
  216. (const :tag "Fortran" fortran)
  217. (const :tag "Gnuplot" gnuplot)
  218. (const :tag "Haskell" haskell)
  219. (const :tag "IO" io)
  220. (const :tag "Java" java)
  221. (const :tag "Javascript" js)
  222. (const :tag "LaTeX" latex)
  223. (const :tag "Ledger" ledger)
  224. (const :tag "Lilypond" lilypond)
  225. (const :tag "Lisp" lisp)
  226. (const :tag "Makefile" makefile)
  227. (const :tag "Maxima" maxima)
  228. (const :tag "Matlab" matlab)
  229. (const :tag "Mscgen" mscgen)
  230. (const :tag "Ocaml" ocaml)
  231. (const :tag "Octave" octave)
  232. (const :tag "Org" org)
  233. (const :tag "Perl" perl)
  234. (const :tag "Pico Lisp" picolisp)
  235. (const :tag "PlantUML" plantuml)
  236. (const :tag "Python" python)
  237. (const :tag "Ruby" ruby)
  238. (const :tag "Sass" sass)
  239. (const :tag "Scala" scala)
  240. (const :tag "Scheme" scheme)
  241. (const :tag "Screen" screen)
  242. (const :tag "Shell Script" sh)
  243. (const :tag "Shen" shen)
  244. (const :tag "Sql" sql)
  245. (const :tag "Sqlite" sqlite))
  246. :value-type (boolean :tag "Activate" :value t)))
  247. ;;;; Customization variables
  248. (defcustom org-clone-delete-id nil
  249. "Remove ID property of clones of a subtree.
  250. When non-nil, clones of a subtree don't inherit the ID property.
  251. Otherwise they inherit the ID property with a new unique
  252. identifier."
  253. :type 'boolean
  254. :version "24.1"
  255. :group 'org-id)
  256. ;;; Version
  257. (org-check-version)
  258. ;;;###autoload
  259. (defun org-version (&optional here full message)
  260. "Show the org-mode version in the echo area.
  261. With prefix argument HERE, insert it at point.
  262. When FULL is non-nil, use a verbose version string.
  263. When MESSAGE is non-nil, display a message with the version."
  264. (interactive "P")
  265. (let* ((org-dir (ignore-errors (org-find-library-dir "org")))
  266. (save-load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  267. (load-suffixes (list ".el"))
  268. (org-install-dir (ignore-errors (org-find-library-dir "org-loaddefs")))
  269. (org-trash (or
  270. (and (fboundp 'org-release) (fboundp 'org-git-version))
  271. (org-load-noerror-mustsuffix (concat org-dir "org-version"))))
  272. (load-suffixes save-load-suffixes)
  273. (org-version (org-release))
  274. (git-version (org-git-version))
  275. (version (format "Org-mode version %s (%s @ %s)"
  276. org-version
  277. git-version
  278. (if org-install-dir
  279. (if (string= org-dir org-install-dir)
  280. org-install-dir
  281. (concat "mixed installation! " org-install-dir " and " org-dir))
  282. "org-loaddefs.el can not be found!")))
  283. (_version (if full version org-version)))
  284. (if (org-called-interactively-p 'interactive)
  285. (if here
  286. (insert version)
  287. (message version))
  288. (if message (message _version))
  289. _version)))
  290. (defconst org-version (org-version))
  291. ;;; Compatibility constants
  292. ;;; The custom variables
  293. (defgroup org nil
  294. "Outline-based notes management and organizer."
  295. :tag "Org"
  296. :group 'outlines
  297. :group 'calendar)
  298. (defcustom org-mode-hook nil
  299. "Mode hook for Org-mode, run after the mode was turned on."
  300. :group 'org
  301. :type 'hook)
  302. (defcustom org-load-hook nil
  303. "Hook that is run after org.el has been loaded."
  304. :group 'org
  305. :type 'hook)
  306. (defcustom org-log-buffer-setup-hook nil
  307. "Hook that is run after an Org log buffer is created."
  308. :group 'org
  309. :version "24.1"
  310. :type 'hook)
  311. (defvar org-modules) ; defined below
  312. (defvar org-modules-loaded nil
  313. "Have the modules been loaded already?")
  314. (defun org-load-modules-maybe (&optional force)
  315. "Load all extensions listed in `org-modules'."
  316. (when (or force (not org-modules-loaded))
  317. (mapc (lambda (ext)
  318. (condition-case nil (require ext)
  319. (error (message "Problems while trying to load feature `%s'" ext))))
  320. org-modules)
  321. (setq org-modules-loaded t)))
  322. (defun org-set-modules (var value)
  323. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  324. (set var value)
  325. (when (featurep 'org)
  326. (org-load-modules-maybe 'force)))
  327. (defcustom org-modules '(org-w3m org-bbdb org-bibtex org-docview org-gnus org-info org-irc org-mhe org-rmail)
  328. "Modules that should always be loaded together with org.el.
  329. If a description starts with <C>, the file is not part of Emacs
  330. and loading it will require that you have downloaded and properly
  331. installed the Org mode distribution.
  332. You can also use this system to load external packages (i.e. neither Org
  333. core modules, nor modules from the CONTRIB directory). Just add symbols
  334. to the end of the list. If the package is called org-xyz.el, then you need
  335. to add the symbol `xyz', and the package must have a call to:
  336. \(provide 'org-xyz)
  337. For export specific modules, see also `org-export-backends'."
  338. :group 'org
  339. :set 'org-set-modules
  340. :version "24.4"
  341. :package-version '(Org . "8.0")
  342. :type
  343. '(set :greedy t
  344. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  345. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  346. (const :tag " crypt: Encryption of subtrees" org-crypt)
  347. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  348. (const :tag " docview: Links to doc-view buffers" org-docview)
  349. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  350. (const :tag " habit: Track your consistency with habits" org-habit)
  351. (const :tag " id: Global IDs for identifying entries" org-id)
  352. (const :tag " info: Links to Info nodes" org-info)
  353. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  354. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  355. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  356. (const :tag " mouse: Additional mouse support" org-mouse)
  357. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  358. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  359. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  360. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  361. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  362. (const :tag "C bullets: Add overlays to headlines stars" org-bullets)
  363. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  364. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  365. (const :tag "C collector: Collect properties into tables" org-collector)
  366. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  367. (const :tag "C drill: Flashcards and spaced repetition for Org-mode" org-drill)
  368. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  369. (const :tag "C eshell Support for links to working directories in eshell" org-eshell)
  370. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  371. (const :tag "C eval: Include command output as text" org-eval)
  372. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  373. (const :tag "C favtable: Lookup table of favorite references and links" org-favtable)
  374. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  375. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  376. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  377. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  378. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  379. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  380. (const :tag "C mac-link-grabber Grab links and URLs from various Mac applications" org-mac-link-grabber)
  381. (const :tag "C mac-message: Links to messages in Apple Mail" org-mac-message)
  382. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  383. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  384. (const :tag "C mew: Links to Mew folders/messages" org-mew)
  385. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  386. (const :tag "C notmuch: Provide org links to notmuch searches or messages" org-notmuch)
  387. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  388. (const :tag "C registry: A registry for Org-mode links" org-registry)
  389. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  390. (const :tag "C secretary: Team management with org-mode" org-secretary)
  391. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  392. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  393. (const :tag "C track: Keep up with Org-mode development" org-track)
  394. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  395. (const :tag "C vm: Links to VM folders/messages" org-vm)
  396. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  397. (const :tag "C wl: Links to Wanderlust folders/messages" org-wl)
  398. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  399. (defvar org-export-registered-backends) ; From ox.el
  400. (declare-function org-export-derived-backend-p "ox" (backend &rest backends))
  401. (defcustom org-export-backends '(ascii html icalendar latex)
  402. "List of export back-ends that should be always available.
  403. If a description starts with <C>, the file is not part of Emacs
  404. and loading it will require that you have downloaded and properly
  405. installed the Org mode distribution.
  406. Unlike to `org-modules', libraries in this list will not be
  407. loaded along with Org, but only once the export framework is
  408. needed.
  409. This variable needs to be set before org.el is loaded. If you
  410. need to make a change while Emacs is running, use the customize
  411. interface or run the following code, where VALUE stands for the
  412. new value of the variable, after updating it:
  413. \(progn
  414. \(setq org-export-registered-backends
  415. \(org-remove-if-not
  416. \(lambda (backend)
  417. \(or (memq backend val)
  418. \(catch 'parentp
  419. \(mapc
  420. \(lambda (b)
  421. \(and (org-export-derived-backend-p b (car backend))
  422. \(throw 'parentp t)))
  423. val)
  424. nil)))
  425. org-export-registered-backends))
  426. \(let ((new-list (mapcar 'car org-export-registered-backends)))
  427. \(dolist (backend val)
  428. \(cond
  429. \((not (load (format \"ox-%s\" backend) t t))
  430. \(message \"Problems while trying to load export back-end `%s'\"
  431. backend))
  432. \((not (memq backend new-list)) (push backend new-list))))
  433. \(set-default var new-list)))
  434. Adding a back-end to this list will also pull the back-end it
  435. depends on, if any."
  436. :group 'org
  437. :group 'org-export
  438. :version "24.4"
  439. :package-version '(Org . "8.0")
  440. :initialize 'custom-initialize-set
  441. :set (lambda (var val)
  442. (if (not (featurep 'ox)) (set-default var val)
  443. ;; Any back-end not required anymore (not present in VAL and not
  444. ;; a parent of any back-end in the new value) is removed from the
  445. ;; list of registered back-ends.
  446. (setq org-export-registered-backends
  447. (org-remove-if-not
  448. (lambda (backend)
  449. (or (memq backend val)
  450. (catch 'parentp
  451. (mapc
  452. (lambda (b)
  453. (and (org-export-derived-backend-p b (car backend))
  454. (throw 'parentp t)))
  455. val)
  456. nil)))
  457. org-export-registered-backends))
  458. ;; Now build NEW-LIST of both new back-ends and required
  459. ;; parents.
  460. (let ((new-list (mapcar 'car org-export-registered-backends)))
  461. (dolist (backend val)
  462. (cond
  463. ((not (load (format "ox-%s" backend) t t))
  464. (message "Problems while trying to load export back-end `%s'"
  465. backend))
  466. ((not (memq backend new-list)) (push backend new-list))))
  467. ;; Set VAR to that list with fixed dependencies.
  468. (set-default var new-list))))
  469. :type '(set :greedy t
  470. (const :tag " ascii Export buffer to ASCII format" ascii)
  471. (const :tag " beamer Export buffer to Beamer presentation" beamer)
  472. (const :tag " html Export buffer to HTML format" html)
  473. (const :tag " icalendar Export buffer to iCalendar format" icalendar)
  474. (const :tag " latex Export buffer to LaTeX format" latex)
  475. (const :tag " man Export buffer to MAN format" man)
  476. (const :tag " md Export buffer to Markdown format" md)
  477. (const :tag " odt Export buffer to ODT format" odt)
  478. (const :tag " org Export buffer to Org format" org)
  479. (const :tag " texinfo Export buffer to Texinfo format" texinfo)
  480. (const :tag "C confluence Export buffer to Confluence Wiki format" confluence)
  481. (const :tag "C deck Export buffer to deck.js presentations" deck)
  482. (const :tag "C freemind Export buffer to Freemind mindmap format" freemind)
  483. (const :tag "C groff Export buffer to Groff format" groff)
  484. (const :tag "C koma-letter Export buffer to KOMA Scrlttrl2 format" koma-letter)
  485. (const :tag "C RSS 2.0 Export buffer to RSS 2.0 format" rss)
  486. (const :tag "C s5 Export buffer to s5 presentations" s5)
  487. (const :tag "C taskjuggler Export buffer to TaskJuggler format" taskjuggler)))
  488. (eval-after-load 'ox
  489. '(mapc
  490. (lambda (backend)
  491. (condition-case nil (require (intern (format "ox-%s" backend)))
  492. (error (message "Problems while trying to load export back-end `%s'"
  493. backend))))
  494. org-export-backends))
  495. (defcustom org-support-shift-select nil
  496. "Non-nil means make shift-cursor commands select text when possible.
  497. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
  498. start selecting a region, or enlarge regions started in this way.
  499. In Org-mode, in special contexts, these same keys are used for
  500. other purposes, important enough to compete with shift selection.
  501. Org tries to balance these needs by supporting `shift-select-mode'
  502. outside these special contexts, under control of this variable.
  503. The default of this variable is nil, to avoid confusing behavior. Shifted
  504. cursor keys will then execute Org commands in the following contexts:
  505. - on a headline, changing TODO state (left/right) and priority (up/down)
  506. - on a time stamp, changing the time
  507. - in a plain list item, changing the bullet type
  508. - in a property definition line, switching between allowed values
  509. - in the BEGIN line of a clock table (changing the time block).
  510. Outside these contexts, the commands will throw an error.
  511. When this variable is t and the cursor is not in a special
  512. context, Org-mode will support shift-selection for making and
  513. enlarging regions. To make this more effective, the bullet
  514. cycling will no longer happen anywhere in an item line, but only
  515. if the cursor is exactly on the bullet.
  516. If you set this variable to the symbol `always', then the keys
  517. will not be special in headlines, property lines, and item lines,
  518. to make shift selection work there as well. If this is what you
  519. want, you can use the following alternative commands: `C-c C-t'
  520. and `C-c ,' to change TODO state and priority, `C-u C-u C-c C-t'
  521. can be used to switch TODO sets, `C-c -' to cycle item bullet
  522. types, and properties can be edited by hand or in column view.
  523. However, when the cursor is on a timestamp, shift-cursor commands
  524. will still edit the time stamp - this is just too good to give up.
  525. XEmacs user should have this variable set to nil, because
  526. `shift-select-mode' is in Emacs 23 or later only."
  527. :group 'org
  528. :type '(choice
  529. (const :tag "Never" nil)
  530. (const :tag "When outside special context" t)
  531. (const :tag "Everywhere except timestamps" always)))
  532. (defcustom org-loop-over-headlines-in-active-region nil
  533. "Shall some commands act upon headlines in the active region?
  534. When set to `t', some commands will be performed in all headlines
  535. within the active region.
  536. When set to `start-level', some commands will be performed in all
  537. headlines within the active region, provided that these headlines
  538. are of the same level than the first one.
  539. When set to a string, those commands will be performed on the
  540. matching headlines within the active region. Such string must be
  541. a tags/property/todo match as it is used in the agenda tags view.
  542. The list of commands is: `org-schedule', `org-deadline',
  543. `org-todo', `org-archive-subtree', `org-archive-set-tag' and
  544. `org-archive-to-archive-sibling'. The archiving commands skip
  545. already archived entries."
  546. :type '(choice (const :tag "Don't loop" nil)
  547. (const :tag "All headlines in active region" t)
  548. (const :tag "In active region, headlines at the same level than the first one" 'start-level)
  549. (string :tag "Tags/Property/Todo matcher"))
  550. :version "24.1"
  551. :group 'org-todo
  552. :group 'org-archive)
  553. (defgroup org-startup nil
  554. "Options concerning startup of Org-mode."
  555. :tag "Org Startup"
  556. :group 'org)
  557. (defcustom org-startup-folded t
  558. "Non-nil means entering Org-mode will switch to OVERVIEW.
  559. This can also be configured on a per-file basis by adding one of
  560. the following lines anywhere in the buffer:
  561. #+STARTUP: fold (or `overview', this is equivalent)
  562. #+STARTUP: nofold (or `showall', this is equivalent)
  563. #+STARTUP: content
  564. #+STARTUP: showeverything
  565. By default, this option is ignored when Org opens agenda files
  566. for the first time. If you want the agenda to honor the startup
  567. option, set `org-agenda-inhibit-startup' to nil."
  568. :group 'org-startup
  569. :type '(choice
  570. (const :tag "nofold: show all" nil)
  571. (const :tag "fold: overview" t)
  572. (const :tag "content: all headlines" content)
  573. (const :tag "show everything, even drawers" showeverything)))
  574. (defcustom org-startup-truncated t
  575. "Non-nil means entering Org-mode will set `truncate-lines'.
  576. This is useful since some lines containing links can be very long and
  577. uninteresting. Also tables look terrible when wrapped."
  578. :group 'org-startup
  579. :type 'boolean)
  580. (defcustom org-startup-indented nil
  581. "Non-nil means turn on `org-indent-mode' on startup.
  582. This can also be configured on a per-file basis by adding one of
  583. the following lines anywhere in the buffer:
  584. #+STARTUP: indent
  585. #+STARTUP: noindent"
  586. :group 'org-structure
  587. :type '(choice
  588. (const :tag "Not" nil)
  589. (const :tag "Globally (slow on startup in large files)" t)))
  590. (defcustom org-use-sub-superscripts t
  591. "Non-nil means interpret \"_\" and \"^\" for display.
  592. When this option is turned on, you can use TeX-like syntax for sub- and
  593. superscripts. Several characters after \"_\" or \"^\" will be
  594. considered as a single item - so grouping with {} is normally not
  595. needed. For example, the following things will be parsed as single
  596. sub- or superscripts.
  597. 10^24 or 10^tau several digits will be considered 1 item.
  598. 10^-12 or 10^-tau a leading sign with digits or a word
  599. x^2-y^3 will be read as x^2 - y^3, because items are
  600. terminated by almost any nonword/nondigit char.
  601. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  602. Still, ambiguity is possible - so when in doubt use {} to enclose
  603. the sub/superscript. If you set this variable to the symbol
  604. `{}', the braces are *required* in order to trigger
  605. interpretations as sub/superscript. This can be helpful in
  606. documents that need \"_\" frequently in plain text."
  607. :group 'org-startup
  608. :version "24.1"
  609. :type '(choice
  610. (const :tag "Always interpret" t)
  611. (const :tag "Only with braces" {})
  612. (const :tag "Never interpret" nil)))
  613. (defcustom org-startup-with-beamer-mode nil
  614. "Non-nil means turn on `org-beamer-mode' on startup.
  615. This can also be configured on a per-file basis by adding one of
  616. the following lines anywhere in the buffer:
  617. #+STARTUP: beamer"
  618. :group 'org-startup
  619. :version "24.1"
  620. :type 'boolean)
  621. (defcustom org-startup-align-all-tables nil
  622. "Non-nil means align all tables when visiting a file.
  623. This is useful when the column width in tables is forced with <N> cookies
  624. in table fields. Such tables will look correct only after the first re-align.
  625. This can also be configured on a per-file basis by adding one of
  626. the following lines anywhere in the buffer:
  627. #+STARTUP: align
  628. #+STARTUP: noalign"
  629. :group 'org-startup
  630. :type 'boolean)
  631. (defcustom org-startup-with-inline-images nil
  632. "Non-nil means show inline images when loading a new Org file.
  633. This can also be configured on a per-file basis by adding one of
  634. the following lines anywhere in the buffer:
  635. #+STARTUP: inlineimages
  636. #+STARTUP: noinlineimages"
  637. :group 'org-startup
  638. :version "24.1"
  639. :type 'boolean)
  640. (defcustom org-startup-with-latex-preview nil
  641. "Non-nil means preview LaTeX fragments when loading a new Org file.
  642. This can also be configured on a per-file basis by adding one of
  643. the followinglines anywhere in the buffer:
  644. #+STARTUP: latexpreview
  645. #+STARTUP: nolatexpreview"
  646. :group 'org-startup
  647. :version "24.4"
  648. :package-version '(Org . "8.0")
  649. :type 'boolean)
  650. (defcustom org-insert-mode-line-in-empty-file nil
  651. "Non-nil means insert the first line setting Org-mode in empty files.
  652. When the function `org-mode' is called interactively in an empty file, this
  653. normally means that the file name does not automatically trigger Org-mode.
  654. To ensure that the file will always be in Org-mode in the future, a
  655. line enforcing Org-mode will be inserted into the buffer, if this option
  656. has been set."
  657. :group 'org-startup
  658. :type 'boolean)
  659. (defcustom org-replace-disputed-keys nil
  660. "Non-nil means use alternative key bindings for some keys.
  661. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  662. These keys are also used by other packages like shift-selection-mode'
  663. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  664. If you want to use Org-mode together with one of these other modes,
  665. or more generally if you would like to move some Org-mode commands to
  666. other keys, set this variable and configure the keys with the variable
  667. `org-disputed-keys'.
  668. This option is only relevant at load-time of Org-mode, and must be set
  669. *before* org.el is loaded. Changing it requires a restart of Emacs to
  670. become effective."
  671. :group 'org-startup
  672. :type 'boolean)
  673. (defcustom org-use-extra-keys nil
  674. "Non-nil means use extra key sequence definitions for certain commands.
  675. This happens automatically if you run XEmacs or if `window-system'
  676. is nil. This variable lets you do the same manually. You must
  677. set it before loading org.
  678. Example: on Carbon Emacs 22 running graphically, with an external
  679. keyboard on a Powerbook, the default way of setting M-left might
  680. not work for either Alt or ESC. Setting this variable will make
  681. it work for ESC."
  682. :group 'org-startup
  683. :type 'boolean)
  684. (org-defvaralias 'org-CUA-compatible 'org-replace-disputed-keys)
  685. (defcustom org-disputed-keys
  686. '(([(shift up)] . [(meta p)])
  687. ([(shift down)] . [(meta n)])
  688. ([(shift left)] . [(meta -)])
  689. ([(shift right)] . [(meta +)])
  690. ([(control shift right)] . [(meta shift +)])
  691. ([(control shift left)] . [(meta shift -)]))
  692. "Keys for which Org-mode and other modes compete.
  693. This is an alist, cars are the default keys, second element specifies
  694. the alternative to use when `org-replace-disputed-keys' is t.
  695. Keys can be specified in any syntax supported by `define-key'.
  696. The value of this option takes effect only at Org-mode's startup,
  697. therefore you'll have to restart Emacs to apply it after changing."
  698. :group 'org-startup
  699. :type 'alist)
  700. (defun org-key (key)
  701. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  702. Or return the original if not disputed.
  703. Also apply the translations defined in `org-xemacs-key-equivalents'."
  704. (when org-replace-disputed-keys
  705. (let* ((nkey (key-description key))
  706. (x (org-find-if (lambda (x)
  707. (equal (key-description (car x)) nkey))
  708. org-disputed-keys)))
  709. (setq key (if x (cdr x) key))))
  710. (when (featurep 'xemacs)
  711. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  712. key)
  713. (defun org-find-if (predicate seq)
  714. (catch 'exit
  715. (while seq
  716. (if (funcall predicate (car seq))
  717. (throw 'exit (car seq))
  718. (pop seq)))))
  719. (defun org-defkey (keymap key def)
  720. "Define a key, possibly translated, as returned by `org-key'."
  721. (define-key keymap (org-key key) def))
  722. (defcustom org-ellipsis nil
  723. "The ellipsis to use in the Org-mode outline.
  724. When nil, just use the standard three dots. When a string, use that instead,
  725. When a face, use the standard 3 dots, but with the specified face.
  726. The change affects only Org-mode (which will then use its own display table).
  727. Changing this requires executing `M-x org-mode' in a buffer to become
  728. effective."
  729. :group 'org-startup
  730. :type '(choice (const :tag "Default" nil)
  731. (face :tag "Face" :value org-warning)
  732. (string :tag "String" :value "...#")))
  733. (defvar org-display-table nil
  734. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  735. (defgroup org-keywords nil
  736. "Keywords in Org-mode."
  737. :tag "Org Keywords"
  738. :group 'org)
  739. (defcustom org-deadline-string "DEADLINE:"
  740. "String to mark deadline entries.
  741. A deadline is this string, followed by a time stamp. Should be a word,
  742. terminated by a colon. You can insert a schedule keyword and
  743. a timestamp with \\[org-deadline].
  744. Changes become only effective after restarting Emacs."
  745. :group 'org-keywords
  746. :type 'string)
  747. (defcustom org-scheduled-string "SCHEDULED:"
  748. "String to mark scheduled TODO entries.
  749. A schedule is this string, followed by a time stamp. Should be a word,
  750. terminated by a colon. You can insert a schedule keyword and
  751. a timestamp with \\[org-schedule].
  752. Changes become only effective after restarting Emacs."
  753. :group 'org-keywords
  754. :type 'string)
  755. (defcustom org-closed-string "CLOSED:"
  756. "String used as the prefix for timestamps logging closing a TODO entry."
  757. :group 'org-keywords
  758. :type 'string)
  759. (defcustom org-clock-string "CLOCK:"
  760. "String used as prefix for timestamps clocking work hours on an item."
  761. :group 'org-keywords
  762. :type 'string)
  763. (defcustom org-closed-keep-when-no-todo nil
  764. "Remove CLOSED: time-stamp when switching back to a non-todo state?"
  765. :group 'org-todo
  766. :group 'org-keywords
  767. :version "24.4"
  768. :package-version '(Org . "8.0")
  769. :type 'boolean)
  770. (defconst org-planning-or-clock-line-re (concat "^[ \t]*\\("
  771. org-scheduled-string "\\|"
  772. org-deadline-string "\\|"
  773. org-closed-string "\\|"
  774. org-clock-string "\\)")
  775. "Matches a line with planning or clock info.")
  776. (defcustom org-comment-string "COMMENT"
  777. "Entries starting with this keyword will never be exported.
  778. An entry can be toggled between COMMENT and normal with
  779. \\[org-toggle-comment].
  780. Changes become only effective after restarting Emacs."
  781. :group 'org-keywords
  782. :type 'string)
  783. (defcustom org-quote-string "QUOTE"
  784. "Entries starting with this keyword will be exported in fixed-width font.
  785. Quoting applies only to the text in the entry following the headline, and does
  786. not extend beyond the next headline, even if that is lower level.
  787. An entry can be toggled between QUOTE and normal with
  788. \\[org-toggle-fixed-width-section]."
  789. :group 'org-keywords
  790. :type 'string)
  791. (defconst org-repeat-re
  792. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)"
  793. "Regular expression for specifying repeated events.
  794. After a match, group 1 contains the repeat expression.")
  795. (defgroup org-structure nil
  796. "Options concerning the general structure of Org-mode files."
  797. :tag "Org Structure"
  798. :group 'org)
  799. (defgroup org-reveal-location nil
  800. "Options about how to make context of a location visible."
  801. :tag "Org Reveal Location"
  802. :group 'org-structure)
  803. (defconst org-context-choice
  804. '(choice
  805. (const :tag "Always" t)
  806. (const :tag "Never" nil)
  807. (repeat :greedy t :tag "Individual contexts"
  808. (cons
  809. (choice :tag "Context"
  810. (const agenda)
  811. (const org-goto)
  812. (const occur-tree)
  813. (const tags-tree)
  814. (const link-search)
  815. (const mark-goto)
  816. (const bookmark-jump)
  817. (const isearch)
  818. (const default))
  819. (boolean))))
  820. "Contexts for the reveal options.")
  821. (defcustom org-show-hierarchy-above '((default . t))
  822. "Non-nil means show full hierarchy when revealing a location.
  823. Org-mode often shows locations in an org-mode file which might have
  824. been invisible before. When this is set, the hierarchy of headings
  825. above the exposed location is shown.
  826. Turning this off for example for sparse trees makes them very compact.
  827. Instead of t, this can also be an alist specifying this option for different
  828. contexts. Valid contexts are
  829. agenda when exposing an entry from the agenda
  830. org-goto when using the command `org-goto' on key C-c C-j
  831. occur-tree when using the command `org-occur' on key C-c /
  832. tags-tree when constructing a sparse tree based on tags matches
  833. link-search when exposing search matches associated with a link
  834. mark-goto when exposing the jump goal of a mark
  835. bookmark-jump when exposing a bookmark location
  836. isearch when exiting from an incremental search
  837. default default for all contexts not set explicitly"
  838. :group 'org-reveal-location
  839. :type org-context-choice)
  840. (defcustom org-show-following-heading '((default . nil))
  841. "Non-nil means show following heading when revealing a location.
  842. Org-mode often shows locations in an org-mode file which might have
  843. been invisible before. When this is set, the heading following the
  844. match is shown.
  845. Turning this off for example for sparse trees makes them very compact,
  846. but makes it harder to edit the location of the match. In such a case,
  847. use the command \\[org-reveal] to show more context.
  848. Instead of t, this can also be an alist specifying this option for different
  849. contexts. See `org-show-hierarchy-above' for valid contexts."
  850. :group 'org-reveal-location
  851. :type org-context-choice)
  852. (defcustom org-show-siblings '((default . nil) (isearch t) (bookmark-jump t))
  853. "Non-nil means show all sibling heading when revealing a location.
  854. Org-mode often shows locations in an org-mode file which might have
  855. been invisible before. When this is set, the sibling of the current entry
  856. heading are all made visible. If `org-show-hierarchy-above' is t,
  857. the same happens on each level of the hierarchy above the current entry.
  858. By default this is on for the isearch context, off for all other contexts.
  859. Turning this off for example for sparse trees makes them very compact,
  860. but makes it harder to edit the location of the match. In such a case,
  861. use the command \\[org-reveal] to show more context.
  862. Instead of t, this can also be an alist specifying this option for different
  863. contexts. See `org-show-hierarchy-above' for valid contexts."
  864. :group 'org-reveal-location
  865. :type org-context-choice
  866. :version "24.4"
  867. :package-version '(Org . "8.0"))
  868. (defcustom org-show-entry-below '((default . nil))
  869. "Non-nil means show the entry below a headline when revealing a location.
  870. Org-mode often shows locations in an org-mode file which might have
  871. been invisible before. When this is set, the text below the headline that is
  872. exposed is also shown.
  873. By default this is off for all contexts.
  874. Instead of t, this can also be an alist specifying this option for different
  875. contexts. See `org-show-hierarchy-above' for valid contexts."
  876. :group 'org-reveal-location
  877. :type org-context-choice)
  878. (defcustom org-indirect-buffer-display 'other-window
  879. "How should indirect tree buffers be displayed?
  880. This applies to indirect buffers created with the commands
  881. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  882. Valid values are:
  883. current-window Display in the current window
  884. other-window Just display in another window.
  885. dedicated-frame Create one new frame, and re-use it each time.
  886. new-frame Make a new frame each time. Note that in this case
  887. previously-made indirect buffers are kept, and you need to
  888. kill these buffers yourself."
  889. :group 'org-structure
  890. :group 'org-agenda-windows
  891. :type '(choice
  892. (const :tag "In current window" current-window)
  893. (const :tag "In current frame, other window" other-window)
  894. (const :tag "Each time a new frame" new-frame)
  895. (const :tag "One dedicated frame" dedicated-frame)))
  896. (defcustom org-use-speed-commands nil
  897. "Non-nil means activate single letter commands at beginning of a headline.
  898. This may also be a function to test for appropriate locations where speed
  899. commands should be active."
  900. :group 'org-structure
  901. :type '(choice
  902. (const :tag "Never" nil)
  903. (const :tag "At beginning of headline stars" t)
  904. (function)))
  905. (defcustom org-speed-commands-user nil
  906. "Alist of additional speed commands.
  907. This list will be checked before `org-speed-commands-default'
  908. when the variable `org-use-speed-commands' is non-nil
  909. and when the cursor is at the beginning of a headline.
  910. The car if each entry is a string with a single letter, which must
  911. be assigned to `self-insert-command' in the global map.
  912. The cdr is either a command to be called interactively, a function
  913. to be called, or a form to be evaluated.
  914. An entry that is just a list with a single string will be interpreted
  915. as a descriptive headline that will be added when listing the speed
  916. commands in the Help buffer using the `?' speed command."
  917. :group 'org-structure
  918. :type '(repeat :value ("k" . ignore)
  919. (choice :value ("k" . ignore)
  920. (list :tag "Descriptive Headline" (string :tag "Headline"))
  921. (cons :tag "Letter and Command"
  922. (string :tag "Command letter")
  923. (choice
  924. (function)
  925. (sexp))))))
  926. (defgroup org-cycle nil
  927. "Options concerning visibility cycling in Org-mode."
  928. :tag "Org Cycle"
  929. :group 'org-structure)
  930. (defcustom org-cycle-skip-children-state-if-no-children t
  931. "Non-nil means skip CHILDREN state in entries that don't have any."
  932. :group 'org-cycle
  933. :type 'boolean)
  934. (defcustom org-cycle-max-level nil
  935. "Maximum level which should still be subject to visibility cycling.
  936. Levels higher than this will, for cycling, be treated as text, not a headline.
  937. When `org-odd-levels-only' is set, a value of N in this variable actually
  938. means 2N-1 stars as the limiting headline.
  939. When nil, cycle all levels.
  940. Note that the limiting level of cycling is also influenced by
  941. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  942. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  943. than its value."
  944. :group 'org-cycle
  945. :type '(choice
  946. (const :tag "No limit" nil)
  947. (integer :tag "Maximum level")))
  948. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK" "RESULTS")
  949. "Names of drawers. Drawers are not opened by cycling on the headline above.
  950. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  951. this:
  952. :DRAWERNAME:
  953. .....
  954. :END:
  955. The drawer \"PROPERTIES\" is special for capturing properties through
  956. the property API.
  957. Drawers can be defined on the per-file basis with a line like:
  958. #+DRAWERS: HIDDEN STATE PROPERTIES"
  959. :group 'org-structure
  960. :group 'org-cycle
  961. :type '(repeat (string :tag "Drawer Name")))
  962. (defcustom org-hide-block-startup nil
  963. "Non-nil means entering Org-mode will fold all blocks.
  964. This can also be set in on a per-file basis with
  965. #+STARTUP: hideblocks
  966. #+STARTUP: showblocks"
  967. :group 'org-startup
  968. :group 'org-cycle
  969. :type 'boolean)
  970. (defcustom org-cycle-global-at-bob nil
  971. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  972. This makes it possible to do global cycling without having to use S-TAB or
  973. \\[universal-argument] TAB. For this special case to work, the first line
  974. of the buffer must not be a headline -- it may be empty or some other text.
  975. When used in this way, `org-cycle-hook' is disabled temporarily to make
  976. sure the cursor stays at the beginning of the buffer. When this option is
  977. nil, don't do anything special at the beginning of the buffer."
  978. :group 'org-cycle
  979. :type 'boolean)
  980. (defcustom org-cycle-level-after-item/entry-creation t
  981. "Non-nil means cycle entry level or item indentation in new empty entries.
  982. When the cursor is at the end of an empty headline, i.e., with only stars
  983. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  984. and then all possible ancestor states, before returning to the original state.
  985. This makes data entry extremely fast: M-RET to create a new headline,
  986. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  987. When the cursor is at the end of an empty plain list item, one TAB will
  988. make it a subitem, two or more tabs will back up to make this an item
  989. higher up in the item hierarchy."
  990. :group 'org-cycle
  991. :type 'boolean)
  992. (defcustom org-cycle-emulate-tab t
  993. "Where should `org-cycle' emulate TAB.
  994. nil Never
  995. white Only in completely white lines
  996. whitestart Only at the beginning of lines, before the first non-white char
  997. t Everywhere except in headlines
  998. exc-hl-bol Everywhere except at the start of a headline
  999. If TAB is used in a place where it does not emulate TAB, the current subtree
  1000. visibility is cycled."
  1001. :group 'org-cycle
  1002. :type '(choice (const :tag "Never" nil)
  1003. (const :tag "Only in completely white lines" white)
  1004. (const :tag "Before first char in a line" whitestart)
  1005. (const :tag "Everywhere except in headlines" t)
  1006. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)))
  1007. (defcustom org-cycle-separator-lines 2
  1008. "Number of empty lines needed to keep an empty line between collapsed trees.
  1009. If you leave an empty line between the end of a subtree and the following
  1010. headline, this empty line is hidden when the subtree is folded.
  1011. Org-mode will leave (exactly) one empty line visible if the number of
  1012. empty lines is equal or larger to the number given in this variable.
  1013. So the default 2 means at least 2 empty lines after the end of a subtree
  1014. are needed to produce free space between a collapsed subtree and the
  1015. following headline.
  1016. If the number is negative, and the number of empty lines is at least -N,
  1017. all empty lines are shown.
  1018. Special case: when 0, never leave empty lines in collapsed view."
  1019. :group 'org-cycle
  1020. :type 'integer)
  1021. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  1022. (defcustom org-pre-cycle-hook nil
  1023. "Hook that is run before visibility cycling is happening.
  1024. The function(s) in this hook must accept a single argument which indicates
  1025. the new state that will be set right after running this hook. The
  1026. argument is a symbol. Before a global state change, it can have the values
  1027. `overview', `content', or `all'. Before a local state change, it can have
  1028. the values `folded', `children', or `subtree'."
  1029. :group 'org-cycle
  1030. :type 'hook)
  1031. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  1032. org-cycle-hide-drawers
  1033. org-cycle-hide-inline-tasks
  1034. org-cycle-show-empty-lines
  1035. org-optimize-window-after-visibility-change)
  1036. "Hook that is run after `org-cycle' has changed the buffer visibility.
  1037. The function(s) in this hook must accept a single argument which indicates
  1038. the new state that was set by the most recent `org-cycle' command. The
  1039. argument is a symbol. After a global state change, it can have the values
  1040. `overview', `contents', or `all'. After a local state change, it can have
  1041. the values `folded', `children', or `subtree'."
  1042. :group 'org-cycle
  1043. :type 'hook)
  1044. (defgroup org-edit-structure nil
  1045. "Options concerning structure editing in Org-mode."
  1046. :tag "Org Edit Structure"
  1047. :group 'org-structure)
  1048. (defcustom org-odd-levels-only nil
  1049. "Non-nil means skip even levels and only use odd levels for the outline.
  1050. This has the effect that two stars are being added/taken away in
  1051. promotion/demotion commands. It also influences how levels are
  1052. handled by the exporters.
  1053. Changing it requires restart of `font-lock-mode' to become effective
  1054. for fontification also in regions already fontified.
  1055. You may also set this on a per-file basis by adding one of the following
  1056. lines to the buffer:
  1057. #+STARTUP: odd
  1058. #+STARTUP: oddeven"
  1059. :group 'org-edit-structure
  1060. :group 'org-appearance
  1061. :type 'boolean)
  1062. (defcustom org-adapt-indentation t
  1063. "Non-nil means adapt indentation to outline node level.
  1064. When this variable is set, Org assumes that you write outlines by
  1065. indenting text in each node to align with the headline (after the stars).
  1066. The following issues are influenced by this variable:
  1067. - When this is set and the *entire* text in an entry is indented, the
  1068. indentation is increased by one space in a demotion command, and
  1069. decreased by one in a promotion command. If any line in the entry
  1070. body starts with text at column 0, indentation is not changed at all.
  1071. - Property drawers and planning information is inserted indented when
  1072. this variable s set. When nil, they will not be indented.
  1073. - TAB indents a line relative to context. The lines below a headline
  1074. will be indented when this variable is set.
  1075. Note that this is all about true indentation, by adding and removing
  1076. space characters. See also `org-indent.el' which does level-dependent
  1077. indentation in a virtual way, i.e. at display time in Emacs."
  1078. :group 'org-edit-structure
  1079. :type 'boolean)
  1080. (defcustom org-special-ctrl-a/e nil
  1081. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  1082. When t, `C-a' will bring back the cursor to the beginning of the
  1083. headline text, i.e. after the stars and after a possible TODO
  1084. keyword. In an item, this will be the position after bullet and
  1085. check-box, if any. When the cursor is already at that position,
  1086. another `C-a' will bring it to the beginning of the line.
  1087. `C-e' will jump to the end of the headline, ignoring the presence
  1088. of tags in the headline. A second `C-e' will then jump to the
  1089. true end of the line, after any tags. This also means that, when
  1090. this variable is non-nil, `C-e' also will never jump beyond the
  1091. end of the heading of a folded section, i.e. not after the
  1092. ellipses.
  1093. When set to the symbol `reversed', the first `C-a' or `C-e' works
  1094. normally, going to the true line boundary first. Only a directly
  1095. following, identical keypress will bring the cursor to the
  1096. special positions.
  1097. This may also be a cons cell where the behavior for `C-a' and
  1098. `C-e' is set separately."
  1099. :group 'org-edit-structure
  1100. :type '(choice
  1101. (const :tag "off" nil)
  1102. (const :tag "on: after stars/bullet and before tags first" t)
  1103. (const :tag "reversed: true line boundary first" reversed)
  1104. (cons :tag "Set C-a and C-e separately"
  1105. (choice :tag "Special C-a"
  1106. (const :tag "off" nil)
  1107. (const :tag "on: after stars/bullet first" t)
  1108. (const :tag "reversed: before stars/bullet first" reversed))
  1109. (choice :tag "Special C-e"
  1110. (const :tag "off" nil)
  1111. (const :tag "on: before tags first" t)
  1112. (const :tag "reversed: after tags first" reversed)))))
  1113. (org-defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e)
  1114. (defcustom org-special-ctrl-k nil
  1115. "Non-nil means `C-k' will behave specially in headlines.
  1116. When nil, `C-k' will call the default `kill-line' command.
  1117. When t, the following will happen while the cursor is in the headline:
  1118. - When the cursor is at the beginning of a headline, kill the entire
  1119. line and possible the folded subtree below the line.
  1120. - When in the middle of the headline text, kill the headline up to the tags.
  1121. - When after the headline text, kill the tags."
  1122. :group 'org-edit-structure
  1123. :type 'boolean)
  1124. (defcustom org-ctrl-k-protect-subtree nil
  1125. "Non-nil means, do not delete a hidden subtree with C-k.
  1126. When set to the symbol `error', simply throw an error when C-k is
  1127. used to kill (part-of) a headline that has hidden text behind it.
  1128. Any other non-nil value will result in a query to the user, if it is
  1129. OK to kill that hidden subtree. When nil, kill without remorse."
  1130. :group 'org-edit-structure
  1131. :version "24.1"
  1132. :type '(choice
  1133. (const :tag "Do not protect hidden subtrees" nil)
  1134. (const :tag "Protect hidden subtrees with a security query" t)
  1135. (const :tag "Never kill a hidden subtree with C-k" error)))
  1136. (defcustom org-catch-invisible-edits nil
  1137. "Check if in invisible region before inserting or deleting a character.
  1138. Valid values are:
  1139. nil Do not check, so just do invisible edits.
  1140. error Throw an error and do nothing.
  1141. show Make point visible, and do the requested edit.
  1142. show-and-error Make point visible, then throw an error and abort the edit.
  1143. smart Make point visible, and do insertion/deletion if it is
  1144. adjacent to visible text and the change feels predictable.
  1145. Never delete a previously invisible character or add in the
  1146. middle or right after an invisible region. Basically, this
  1147. allows insertion and backward-delete right before ellipses.
  1148. FIXME: maybe in this case we should not even show?"
  1149. :group 'org-edit-structure
  1150. :version "24.1"
  1151. :type '(choice
  1152. (const :tag "Do not check" nil)
  1153. (const :tag "Throw error when trying to edit" error)
  1154. (const :tag "Unhide, but do not do the edit" show-and-error)
  1155. (const :tag "Show invisible part and do the edit" show)
  1156. (const :tag "Be smart and do the right thing" smart)))
  1157. (defcustom org-yank-folded-subtrees t
  1158. "Non-nil means when yanking subtrees, fold them.
  1159. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  1160. it starts with a heading and all other headings in it are either children
  1161. or siblings, then fold all the subtrees. However, do this only if no
  1162. text after the yank would be swallowed into a folded tree by this action."
  1163. :group 'org-edit-structure
  1164. :type 'boolean)
  1165. (defcustom org-yank-adjusted-subtrees nil
  1166. "Non-nil means when yanking subtrees, adjust the level.
  1167. With this setting, `org-paste-subtree' is used to insert the subtree, see
  1168. this function for details."
  1169. :group 'org-edit-structure
  1170. :type 'boolean)
  1171. (defcustom org-M-RET-may-split-line '((default . t))
  1172. "Non-nil means M-RET will split the line at the cursor position.
  1173. When nil, it will go to the end of the line before making a
  1174. new line.
  1175. You may also set this option in a different way for different
  1176. contexts. Valid contexts are:
  1177. headline when creating a new headline
  1178. item when creating a new item
  1179. table in a table field
  1180. default the value to be used for all contexts not explicitly
  1181. customized"
  1182. :group 'org-structure
  1183. :group 'org-table
  1184. :type '(choice
  1185. (const :tag "Always" t)
  1186. (const :tag "Never" nil)
  1187. (repeat :greedy t :tag "Individual contexts"
  1188. (cons
  1189. (choice :tag "Context"
  1190. (const headline)
  1191. (const item)
  1192. (const table)
  1193. (const default))
  1194. (boolean)))))
  1195. (defcustom org-insert-heading-respect-content nil
  1196. "Non-nil means insert new headings after the current subtree.
  1197. When nil, the new heading is created directly after the current line.
  1198. The commands \\[org-insert-heading-respect-content] and \\[org-insert-todo-heading-respect-content] turn
  1199. this variable on for the duration of the command."
  1200. :group 'org-structure
  1201. :type 'boolean)
  1202. (defcustom org-blank-before-new-entry '((heading . auto)
  1203. (plain-list-item . auto))
  1204. "Should `org-insert-heading' leave a blank line before new heading/item?
  1205. The value is an alist, with `heading' and `plain-list-item' as CAR,
  1206. and a boolean flag as CDR. The cdr may also be the symbol `auto', in
  1207. which case Org will look at the surrounding headings/items and try to
  1208. make an intelligent decision whether to insert a blank line or not.
  1209. For plain lists, if `org-list-empty-line-terminates-plain-lists' is set,
  1210. the setting here is ignored and no empty line is inserted to avoid breaking
  1211. the list structure."
  1212. :group 'org-edit-structure
  1213. :type '(list
  1214. (cons (const heading)
  1215. (choice (const :tag "Never" nil)
  1216. (const :tag "Always" t)
  1217. (const :tag "Auto" auto)))
  1218. (cons (const plain-list-item)
  1219. (choice (const :tag "Never" nil)
  1220. (const :tag "Always" t)
  1221. (const :tag "Auto" auto)))))
  1222. (defcustom org-insert-heading-hook nil
  1223. "Hook being run after inserting a new heading."
  1224. :group 'org-edit-structure
  1225. :type 'hook)
  1226. (defcustom org-enable-fixed-width-editor t
  1227. "Non-nil means lines starting with \":\" are treated as fixed-width.
  1228. This currently only means they are never auto-wrapped.
  1229. When nil, such lines will be treated like ordinary lines.
  1230. See also the QUOTE keyword."
  1231. :group 'org-edit-structure
  1232. :type 'boolean)
  1233. (defcustom org-goto-auto-isearch t
  1234. "Non-nil means typing characters in `org-goto' starts incremental search.
  1235. When nil, you can use these keybindings to navigate the buffer:
  1236. q Quit the org-goto interface
  1237. n Go to the next visible heading
  1238. p Go to the previous visible heading
  1239. f Go one heading forward on same level
  1240. b Go one heading backward on same level
  1241. u Go one heading up"
  1242. :group 'org-edit-structure
  1243. :type 'boolean)
  1244. (defgroup org-sparse-trees nil
  1245. "Options concerning sparse trees in Org-mode."
  1246. :tag "Org Sparse Trees"
  1247. :group 'org-structure)
  1248. (defcustom org-highlight-sparse-tree-matches t
  1249. "Non-nil means highlight all matches that define a sparse tree.
  1250. The highlights will automatically disappear the next time the buffer is
  1251. changed by an edit command."
  1252. :group 'org-sparse-trees
  1253. :type 'boolean)
  1254. (defcustom org-remove-highlights-with-change t
  1255. "Non-nil means any change to the buffer will remove temporary highlights.
  1256. Such highlights are created by `org-occur' and `org-clock-display'.
  1257. When nil, `C-c C-c needs to be used to get rid of the highlights.
  1258. The highlights created by `org-preview-latex-fragment' always need
  1259. `C-c C-c' to be removed."
  1260. :group 'org-sparse-trees
  1261. :group 'org-time
  1262. :type 'boolean)
  1263. (defcustom org-occur-hook '(org-first-headline-recenter)
  1264. "Hook that is run after `org-occur' has constructed a sparse tree.
  1265. This can be used to recenter the window to show as much of the structure
  1266. as possible."
  1267. :group 'org-sparse-trees
  1268. :type 'hook)
  1269. (defgroup org-imenu-and-speedbar nil
  1270. "Options concerning imenu and speedbar in Org-mode."
  1271. :tag "Org Imenu and Speedbar"
  1272. :group 'org-structure)
  1273. (defcustom org-imenu-depth 2
  1274. "The maximum level for Imenu access to Org-mode headlines.
  1275. This also applied for speedbar access."
  1276. :group 'org-imenu-and-speedbar
  1277. :type 'integer)
  1278. (defgroup org-table nil
  1279. "Options concerning tables in Org-mode."
  1280. :tag "Org Table"
  1281. :group 'org)
  1282. (defcustom org-enable-table-editor 'optimized
  1283. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1284. When nil, such lines will be treated like ordinary lines.
  1285. When equal to the symbol `optimized', the table editor will be optimized to
  1286. do the following:
  1287. - Automatic overwrite mode in front of whitespace in table fields.
  1288. This makes the structure of the table stay in tact as long as the edited
  1289. field does not exceed the column width.
  1290. - Minimize the number of realigns. Normally, the table is aligned each time
  1291. TAB or RET are pressed to move to another field. With optimization this
  1292. happens only if changes to a field might have changed the column width.
  1293. Optimization requires replacing the functions `self-insert-command',
  1294. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1295. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1296. very good at guessing when a re-align will be necessary, but you can always
  1297. force one with \\[org-ctrl-c-ctrl-c].
  1298. If you would like to use the optimized version in Org-mode, but the
  1299. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1300. This variable can be used to turn on and off the table editor during a session,
  1301. but in order to toggle optimization, a restart is required.
  1302. See also the variable `org-table-auto-blank-field'."
  1303. :group 'org-table
  1304. :type '(choice
  1305. (const :tag "off" nil)
  1306. (const :tag "on" t)
  1307. (const :tag "on, optimized" optimized)))
  1308. (defcustom org-self-insert-cluster-for-undo (or (featurep 'xemacs)
  1309. (version<= emacs-version "24.1"))
  1310. "Non-nil means cluster self-insert commands for undo when possible.
  1311. If this is set, then, like in the Emacs command loop, 20 consecutive
  1312. characters will be undone together.
  1313. This is configurable, because there is some impact on typing performance."
  1314. :group 'org-table
  1315. :type 'boolean)
  1316. (defcustom org-table-tab-recognizes-table.el t
  1317. "Non-nil means TAB will automatically notice a table.el table.
  1318. When it sees such a table, it moves point into it and - if necessary -
  1319. calls `table-recognize-table'."
  1320. :group 'org-table-editing
  1321. :type 'boolean)
  1322. (defgroup org-link nil
  1323. "Options concerning links in Org-mode."
  1324. :tag "Org Link"
  1325. :group 'org)
  1326. (defvar org-link-abbrev-alist-local nil
  1327. "Buffer-local version of `org-link-abbrev-alist', which see.
  1328. The value of this is taken from the #+LINK lines.")
  1329. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1330. (defcustom org-link-abbrev-alist nil
  1331. "Alist of link abbreviations.
  1332. The car of each element is a string, to be replaced at the start of a link.
  1333. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1334. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1335. [[linkkey:tag][description]]
  1336. The 'linkkey' must be a word word, starting with a letter, followed
  1337. by letters, numbers, '-' or '_'.
  1338. If REPLACE is a string, the tag will simply be appended to create the link.
  1339. If the string contains \"%s\", the tag will be inserted there. If the string
  1340. contains \"%h\", it will cause a url-encoded version of the tag to be inserted
  1341. at that point (see the function `url-hexify-string'). If the string contains
  1342. the specifier \"%(my-function)\", then the custom function `my-function' will
  1343. be invoked: this function takes the tag as its only argument and must return
  1344. a string.
  1345. REPLACE may also be a function that will be called with the tag as the
  1346. only argument to create the link, which should be returned as a string.
  1347. See the manual for examples."
  1348. :group 'org-link
  1349. :type '(repeat
  1350. (cons
  1351. (string :tag "Protocol")
  1352. (choice
  1353. (string :tag "Format")
  1354. (function)))))
  1355. (defcustom org-descriptive-links t
  1356. "Non-nil means Org will display descriptive links.
  1357. E.g. [[http://orgmode.org][Org website]] will be displayed as
  1358. \"Org Website\", hiding the link itself and just displaying its
  1359. description. When set to `nil', Org will display the full links
  1360. literally.
  1361. You can interactively set the value of this variable by calling
  1362. `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
  1363. :group 'org-link
  1364. :type 'boolean)
  1365. (defcustom org-link-file-path-type 'adaptive
  1366. "How the path name in file links should be stored.
  1367. Valid values are:
  1368. relative Relative to the current directory, i.e. the directory of the file
  1369. into which the link is being inserted.
  1370. absolute Absolute path, if possible with ~ for home directory.
  1371. noabbrev Absolute path, no abbreviation of home directory.
  1372. adaptive Use relative path for files in the current directory and sub-
  1373. directories of it. For other files, use an absolute path."
  1374. :group 'org-link
  1375. :type '(choice
  1376. (const relative)
  1377. (const absolute)
  1378. (const noabbrev)
  1379. (const adaptive)))
  1380. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  1381. "Types of links that should be activated in Org-mode files.
  1382. This is a list of symbols, each leading to the activation of a certain link
  1383. type. In principle, it does not hurt to turn on most link types - there may
  1384. be a small gain when turning off unused link types. The types are:
  1385. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1386. angle Links in angular brackets that may contain whitespace like
  1387. <bbdb:Carsten Dominik>.
  1388. plain Plain links in normal text, no whitespace, like http://google.com.
  1389. radio Text that is matched by a radio target, see manual for details.
  1390. tag Tag settings in a headline (link to tag search).
  1391. date Time stamps (link to calendar).
  1392. footnote Footnote labels.
  1393. Changing this variable requires a restart of Emacs to become effective."
  1394. :group 'org-link
  1395. :type '(set :greedy t
  1396. (const :tag "Double bracket links" bracket)
  1397. (const :tag "Angular bracket links" angle)
  1398. (const :tag "Plain text links" plain)
  1399. (const :tag "Radio target matches" radio)
  1400. (const :tag "Tags" tag)
  1401. (const :tag "Timestamps" date)
  1402. (const :tag "Footnotes" footnote)))
  1403. (defcustom org-make-link-description-function nil
  1404. "Function to use for generating link descriptions from links.
  1405. When nil, the link location will be used. This function must take
  1406. two parameters: the first one is the link, the second one is the
  1407. description generated by `org-insert-link'. The function should
  1408. return the description to use."
  1409. :group 'org-link
  1410. :type '(choice (const nil) (function)))
  1411. (defgroup org-link-store nil
  1412. "Options concerning storing links in Org-mode."
  1413. :tag "Org Store Link"
  1414. :group 'org-link)
  1415. (defcustom org-url-hexify-p t
  1416. "When non-nil, hexify URL when creating a link."
  1417. :type 'boolean
  1418. :version "24.3"
  1419. :group 'org-link-store)
  1420. (defcustom org-email-link-description-format "Email %c: %.30s"
  1421. "Format of the description part of a link to an email or usenet message.
  1422. The following %-escapes will be replaced by corresponding information:
  1423. %F full \"From\" field
  1424. %f name, taken from \"From\" field, address if no name
  1425. %T full \"To\" field
  1426. %t first name in \"To\" field, address if no name
  1427. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1428. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1429. %s subject
  1430. %d date
  1431. %m message-id.
  1432. You may use normal field width specification between the % and the letter.
  1433. This is for example useful to limit the length of the subject.
  1434. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1435. :group 'org-link-store
  1436. :type 'string)
  1437. (defcustom org-from-is-user-regexp
  1438. (let (r1 r2)
  1439. (when (and user-mail-address (not (string= user-mail-address "")))
  1440. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1441. (when (and user-full-name (not (string= user-full-name "")))
  1442. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1443. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1444. "Regexp matched against the \"From:\" header of an email or usenet message.
  1445. It should match if the message is from the user him/herself."
  1446. :group 'org-link-store
  1447. :type 'regexp)
  1448. (defcustom org-context-in-file-links t
  1449. "Non-nil means file links from `org-store-link' contain context.
  1450. A search string will be added to the file name with :: as separator and
  1451. used to find the context when the link is activated by the command
  1452. `org-open-at-point'. When this option is t, the entire active region
  1453. will be placed in the search string of the file link. If set to a
  1454. positive integer, only the first n lines of context will be stored.
  1455. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1456. negates this setting for the duration of the command."
  1457. :group 'org-link-store
  1458. :type '(choice boolean integer))
  1459. (defcustom org-keep-stored-link-after-insertion nil
  1460. "Non-nil means keep link in list for entire session.
  1461. The command `org-store-link' adds a link pointing to the current
  1462. location to an internal list. These links accumulate during a session.
  1463. The command `org-insert-link' can be used to insert links into any
  1464. Org-mode file (offering completion for all stored links). When this
  1465. option is nil, every link which has been inserted once using \\[org-insert-link]
  1466. will be removed from the list, to make completing the unused links
  1467. more efficient."
  1468. :group 'org-link-store
  1469. :type 'boolean)
  1470. (defgroup org-link-follow nil
  1471. "Options concerning following links in Org-mode."
  1472. :tag "Org Follow Link"
  1473. :group 'org-link)
  1474. (defcustom org-link-translation-function nil
  1475. "Function to translate links with different syntax to Org syntax.
  1476. This can be used to translate links created for example by the Planner
  1477. or emacs-wiki packages to Org syntax.
  1478. The function must accept two parameters, a TYPE containing the link
  1479. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1480. which is everything after the link protocol. It should return a cons
  1481. with possibly modified values of type and path.
  1482. Org contains a function for this, so if you set this variable to
  1483. `org-translate-link-from-planner', you should be able follow many
  1484. links created by planner."
  1485. :group 'org-link-follow
  1486. :type '(choice (const nil) (function)))
  1487. (defcustom org-follow-link-hook nil
  1488. "Hook that is run after a link has been followed."
  1489. :group 'org-link-follow
  1490. :type 'hook)
  1491. (defcustom org-tab-follows-link nil
  1492. "Non-nil means on links TAB will follow the link.
  1493. Needs to be set before org.el is loaded.
  1494. This really should not be used, it does not make sense, and the
  1495. implementation is bad."
  1496. :group 'org-link-follow
  1497. :type 'boolean)
  1498. (defcustom org-return-follows-link nil
  1499. "Non-nil means on links RET will follow the link.
  1500. In tables, the special behavior of RET has precedence."
  1501. :group 'org-link-follow
  1502. :type 'boolean)
  1503. (defcustom org-mouse-1-follows-link
  1504. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1505. "Non-nil means mouse-1 on a link will follow the link.
  1506. A longer mouse click will still set point. Does not work on XEmacs.
  1507. Needs to be set before org.el is loaded."
  1508. :group 'org-link-follow
  1509. :type 'boolean)
  1510. (defcustom org-mark-ring-length 4
  1511. "Number of different positions to be recorded in the ring.
  1512. Changing this requires a restart of Emacs to work correctly."
  1513. :group 'org-link-follow
  1514. :type 'integer)
  1515. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1516. "Non-nil means internal links in Org files must exactly match a headline.
  1517. When nil, the link search tries to match a phrase with all words
  1518. in the search text."
  1519. :group 'org-link-follow
  1520. :version "24.1"
  1521. :type '(choice
  1522. (const :tag "Use fuzzy text search" nil)
  1523. (const :tag "Match only exact headline" t)
  1524. (const :tag "Match exact headline or query to create it"
  1525. query-to-create)))
  1526. (defcustom org-link-frame-setup
  1527. '((vm . vm-visit-folder-other-frame)
  1528. (vm-imap . vm-visit-imap-folder-other-frame)
  1529. (gnus . org-gnus-no-new-news)
  1530. (file . find-file-other-window)
  1531. (wl . wl-other-frame))
  1532. "Setup the frame configuration for following links.
  1533. When following a link with Emacs, it may often be useful to display
  1534. this link in another window or frame. This variable can be used to
  1535. set this up for the different types of links.
  1536. For VM, use any of
  1537. `vm-visit-folder'
  1538. `vm-visit-folder-other-window'
  1539. `vm-visit-folder-other-frame'
  1540. For Gnus, use any of
  1541. `gnus'
  1542. `gnus-other-frame'
  1543. `org-gnus-no-new-news'
  1544. For FILE, use any of
  1545. `find-file'
  1546. `find-file-other-window'
  1547. `find-file-other-frame'
  1548. For Wanderlust use any of
  1549. `wl'
  1550. `wl-other-frame'
  1551. For the calendar, use the variable `calendar-setup'.
  1552. For BBDB, it is currently only possible to display the matches in
  1553. another window."
  1554. :group 'org-link-follow
  1555. :type '(list
  1556. (cons (const vm)
  1557. (choice
  1558. (const vm-visit-folder)
  1559. (const vm-visit-folder-other-window)
  1560. (const vm-visit-folder-other-frame)))
  1561. (cons (const vm-imap)
  1562. (choice
  1563. (const vm-visit-imap-folder)
  1564. (const vm-visit-imap-folder-other-window)
  1565. (const vm-visit-imap-folder-other-frame)))
  1566. (cons (const gnus)
  1567. (choice
  1568. (const gnus)
  1569. (const gnus-other-frame)
  1570. (const org-gnus-no-new-news)))
  1571. (cons (const file)
  1572. (choice
  1573. (const find-file)
  1574. (const find-file-other-window)
  1575. (const find-file-other-frame)))
  1576. (cons (const wl)
  1577. (choice
  1578. (const wl)
  1579. (const wl-other-frame)))))
  1580. (defcustom org-display-internal-link-with-indirect-buffer nil
  1581. "Non-nil means use indirect buffer to display infile links.
  1582. Activating internal links (from one location in a file to another location
  1583. in the same file) normally just jumps to the location. When the link is
  1584. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1585. is displayed in
  1586. another window. When this option is set, the other window actually displays
  1587. an indirect buffer clone of the current buffer, to avoid any visibility
  1588. changes to the current buffer."
  1589. :group 'org-link-follow
  1590. :type 'boolean)
  1591. (defcustom org-open-non-existing-files nil
  1592. "Non-nil means `org-open-file' will open non-existing files.
  1593. When nil, an error will be generated.
  1594. This variable applies only to external applications because they
  1595. might choke on non-existing files. If the link is to a file that
  1596. will be opened in Emacs, the variable is ignored."
  1597. :group 'org-link-follow
  1598. :type 'boolean)
  1599. (defcustom org-open-directory-means-index-dot-org nil
  1600. "Non-nil means a link to a directory really means to index.org.
  1601. When nil, following a directory link will run dired or open a finder/explorer
  1602. window on that directory."
  1603. :group 'org-link-follow
  1604. :type 'boolean)
  1605. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1606. "Function and arguments to call for following mailto links.
  1607. This is a list with the first element being a Lisp function, and the
  1608. remaining elements being arguments to the function. In string arguments,
  1609. %a will be replaced by the address, and %s will be replaced by the subject
  1610. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1611. :group 'org-link-follow
  1612. :type '(choice
  1613. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1614. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1615. (const :tag "message-mail" (message-mail "%a" "%s"))
  1616. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1617. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1618. "Non-nil means ask for confirmation before executing shell links.
  1619. Shell links can be dangerous: just think about a link
  1620. [[shell:rm -rf ~/*][Google Search]]
  1621. This link would show up in your Org-mode document as \"Google Search\",
  1622. but really it would remove your entire home directory.
  1623. Therefore we advise against setting this variable to nil.
  1624. Just change it to `y-or-n-p' if you want to confirm with a
  1625. single keystroke rather than having to type \"yes\"."
  1626. :group 'org-link-follow
  1627. :type '(choice
  1628. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1629. (const :tag "with y-or-n (faster)" y-or-n-p)
  1630. (const :tag "no confirmation (dangerous)" nil)))
  1631. (put 'org-confirm-shell-link-function
  1632. 'safe-local-variable
  1633. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1634. (defcustom org-confirm-shell-link-not-regexp ""
  1635. "A regexp to skip confirmation for shell links."
  1636. :group 'org-link-follow
  1637. :version "24.1"
  1638. :type 'regexp)
  1639. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1640. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1641. Elisp links can be dangerous: just think about a link
  1642. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1643. This link would show up in your Org-mode document as \"Google Search\",
  1644. but really it would remove your entire home directory.
  1645. Therefore we advise against setting this variable to nil.
  1646. Just change it to `y-or-n-p' if you want to confirm with a
  1647. single keystroke rather than having to type \"yes\"."
  1648. :group 'org-link-follow
  1649. :type '(choice
  1650. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1651. (const :tag "with y-or-n (faster)" y-or-n-p)
  1652. (const :tag "no confirmation (dangerous)" nil)))
  1653. (put 'org-confirm-shell-link-function
  1654. 'safe-local-variable
  1655. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1656. (defcustom org-confirm-elisp-link-not-regexp ""
  1657. "A regexp to skip confirmation for Elisp links."
  1658. :group 'org-link-follow
  1659. :version "24.1"
  1660. :type 'regexp)
  1661. (defconst org-file-apps-defaults-gnu
  1662. '((remote . emacs)
  1663. (system . mailcap)
  1664. (t . mailcap))
  1665. "Default file applications on a UNIX or GNU/Linux system.
  1666. See `org-file-apps'.")
  1667. (defconst org-file-apps-defaults-macosx
  1668. '((remote . emacs)
  1669. (t . "open %s")
  1670. (system . "open %s")
  1671. ("ps.gz" . "gv %s")
  1672. ("eps.gz" . "gv %s")
  1673. ("dvi" . "xdvi %s")
  1674. ("fig" . "xfig %s"))
  1675. "Default file applications on a MacOS X system.
  1676. The system \"open\" is known as a default, but we use X11 applications
  1677. for some files for which the OS does not have a good default.
  1678. See `org-file-apps'.")
  1679. (defconst org-file-apps-defaults-windowsnt
  1680. (list
  1681. '(remote . emacs)
  1682. (cons t
  1683. (list (if (featurep 'xemacs)
  1684. 'mswindows-shell-execute
  1685. 'w32-shell-execute)
  1686. "open" 'file))
  1687. (cons 'system
  1688. (list (if (featurep 'xemacs)
  1689. 'mswindows-shell-execute
  1690. 'w32-shell-execute)
  1691. "open" 'file)))
  1692. "Default file applications on a Windows NT system.
  1693. The system \"open\" is used for most files.
  1694. See `org-file-apps'.")
  1695. (defcustom org-file-apps
  1696. '((auto-mode . emacs)
  1697. ("\\.mm\\'" . default)
  1698. ("\\.x?html?\\'" . default)
  1699. ("\\.pdf\\'" . default))
  1700. "External applications for opening `file:path' items in a document.
  1701. Org-mode uses system defaults for different file types, but
  1702. you can use this variable to set the application for a given file
  1703. extension. The entries in this list are cons cells where the car identifies
  1704. files and the cdr the corresponding command. Possible values for the
  1705. file identifier are
  1706. \"string\" A string as a file identifier can be interpreted in different
  1707. ways, depending on its contents:
  1708. - Alphanumeric characters only:
  1709. Match links with this file extension.
  1710. Example: (\"pdf\" . \"evince %s\")
  1711. to open PDFs with evince.
  1712. - Regular expression: Match links where the
  1713. filename matches the regexp. If you want to
  1714. use groups here, use shy groups.
  1715. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1716. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1717. to open *.html and *.xhtml with firefox.
  1718. - Regular expression which contains (non-shy) groups:
  1719. Match links where the whole link, including \"::\", and
  1720. anything after that, matches the regexp.
  1721. In a custom command string, %1, %2, etc. are replaced with
  1722. the parts of the link that were matched by the groups.
  1723. For backwards compatibility, if a command string is given
  1724. that does not use any of the group matches, this case is
  1725. handled identically to the second one (i.e. match against
  1726. file name only).
  1727. In a custom lisp form, you can access the group matches with
  1728. (match-string n link).
  1729. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1730. to open [[file:document.pdf::5]] with evince at page 5.
  1731. `directory' Matches a directory
  1732. `remote' Matches a remote file, accessible through tramp or efs.
  1733. Remote files most likely should be visited through Emacs
  1734. because external applications cannot handle such paths.
  1735. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1736. so all files Emacs knows how to handle. Using this with
  1737. command `emacs' will open most files in Emacs. Beware that this
  1738. will also open html files inside Emacs, unless you add
  1739. (\"html\" . default) to the list as well.
  1740. t Default for files not matched by any of the other options.
  1741. `system' The system command to open files, like `open' on Windows
  1742. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1743. that will be selected if you call `C-c C-o' with a double
  1744. \\[universal-argument] \\[universal-argument] prefix.
  1745. Possible values for the command are:
  1746. `emacs' The file will be visited by the current Emacs process.
  1747. `default' Use the default application for this file type, which is the
  1748. association for t in the list, most likely in the system-specific
  1749. part.
  1750. This can be used to overrule an unwanted setting in the
  1751. system-specific variable.
  1752. `system' Use the system command for opening files, like \"open\".
  1753. This command is specified by the entry whose car is `system'.
  1754. Most likely, the system-specific version of this variable
  1755. does define this command, but you can overrule/replace it
  1756. here.
  1757. string A command to be executed by a shell; %s will be replaced
  1758. by the path to the file.
  1759. sexp A Lisp form which will be evaluated. The file path will
  1760. be available in the Lisp variable `file'.
  1761. For more examples, see the system specific constants
  1762. `org-file-apps-defaults-macosx'
  1763. `org-file-apps-defaults-windowsnt'
  1764. `org-file-apps-defaults-gnu'."
  1765. :group 'org-link-follow
  1766. :type '(repeat
  1767. (cons (choice :value ""
  1768. (string :tag "Extension")
  1769. (const :tag "System command to open files" system)
  1770. (const :tag "Default for unrecognized files" t)
  1771. (const :tag "Remote file" remote)
  1772. (const :tag "Links to a directory" directory)
  1773. (const :tag "Any files that have Emacs modes"
  1774. auto-mode))
  1775. (choice :value ""
  1776. (const :tag "Visit with Emacs" emacs)
  1777. (const :tag "Use default" default)
  1778. (const :tag "Use the system command" system)
  1779. (string :tag "Command")
  1780. (sexp :tag "Lisp form")))))
  1781. (defcustom org-doi-server-url "http://dx.doi.org/"
  1782. "The URL of the DOI server."
  1783. :type 'string
  1784. :version "24.3"
  1785. :group 'org-link-follow)
  1786. (defgroup org-refile nil
  1787. "Options concerning refiling entries in Org-mode."
  1788. :tag "Org Refile"
  1789. :group 'org)
  1790. (defcustom org-directory "~/org"
  1791. "Directory with org files.
  1792. This is just a default location to look for Org files. There is no need
  1793. at all to put your files into this directory. It is only used in the
  1794. following situations:
  1795. 1. When a capture template specifies a target file that is not an
  1796. absolute path. The path will then be interpreted relative to
  1797. `org-directory'
  1798. 2. When a capture note is filed away in an interactive way (when exiting the
  1799. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1800. with `org-directory' as the default path."
  1801. :group 'org-refile
  1802. :group 'org-remember
  1803. :group 'org-capture
  1804. :type 'directory)
  1805. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1806. "Default target for storing notes.
  1807. Used as a fall back file for org-remember.el and org-capture.el, for
  1808. templates that do not specify a target file."
  1809. :group 'org-refile
  1810. :group 'org-remember
  1811. :group 'org-capture
  1812. :type '(choice
  1813. (const :tag "Default from remember-data-file" nil)
  1814. file))
  1815. (defcustom org-goto-interface 'outline
  1816. "The default interface to be used for `org-goto'.
  1817. Allowed values are:
  1818. outline The interface shows an outline of the relevant file
  1819. and the correct heading is found by moving through
  1820. the outline or by searching with incremental search.
  1821. outline-path-completion Headlines in the current buffer are offered via
  1822. completion. This is the interface also used by
  1823. the refile command."
  1824. :group 'org-refile
  1825. :type '(choice
  1826. (const :tag "Outline" outline)
  1827. (const :tag "Outline-path-completion" outline-path-completion)))
  1828. (defcustom org-goto-max-level 5
  1829. "Maximum target level when running `org-goto' with refile interface."
  1830. :group 'org-refile
  1831. :type 'integer)
  1832. (defcustom org-reverse-note-order nil
  1833. "Non-nil means store new notes at the beginning of a file or entry.
  1834. When nil, new notes will be filed to the end of a file or entry.
  1835. This can also be a list with cons cells of regular expressions that
  1836. are matched against file names, and values."
  1837. :group 'org-remember
  1838. :group 'org-capture
  1839. :group 'org-refile
  1840. :type '(choice
  1841. (const :tag "Reverse always" t)
  1842. (const :tag "Reverse never" nil)
  1843. (repeat :tag "By file name regexp"
  1844. (cons regexp boolean))))
  1845. (defcustom org-log-refile nil
  1846. "Information to record when a task is refiled.
  1847. Possible values are:
  1848. nil Don't add anything
  1849. time Add a time stamp to the task
  1850. note Prompt for a note and add it with template `org-log-note-headings'
  1851. This option can also be set with on a per-file-basis with
  1852. #+STARTUP: nologrefile
  1853. #+STARTUP: logrefile
  1854. #+STARTUP: lognoterefile
  1855. You can have local logging settings for a subtree by setting the LOGGING
  1856. property to one or more of these keywords.
  1857. When bulk-refiling from the agenda, the value `note' is forbidden and
  1858. will temporarily be changed to `time'."
  1859. :group 'org-refile
  1860. :group 'org-progress
  1861. :version "24.1"
  1862. :type '(choice
  1863. (const :tag "No logging" nil)
  1864. (const :tag "Record timestamp" time)
  1865. (const :tag "Record timestamp with note." note)))
  1866. (defcustom org-refile-targets nil
  1867. "Targets for refiling entries with \\[org-refile].
  1868. This is a list of cons cells. Each cell contains:
  1869. - a specification of the files to be considered, either a list of files,
  1870. or a symbol whose function or variable value will be used to retrieve
  1871. a file name or a list of file names. If you use `org-agenda-files' for
  1872. that, all agenda files will be scanned for targets. Nil means consider
  1873. headings in the current buffer.
  1874. - A specification of how to find candidate refile targets. This may be
  1875. any of:
  1876. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1877. This tag has to be present in all target headlines, inheritance will
  1878. not be considered.
  1879. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1880. todo keyword.
  1881. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1882. headlines that are refiling targets.
  1883. - a cons cell (:level . N). Any headline of level N is considered a target.
  1884. Note that, when `org-odd-levels-only' is set, level corresponds to
  1885. order in hierarchy, not to the number of stars.
  1886. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1887. Note that, when `org-odd-levels-only' is set, level corresponds to
  1888. order in hierarchy, not to the number of stars.
  1889. Each element of this list generates a set of possible targets.
  1890. The union of these sets is presented (with completion) to
  1891. the user by `org-refile'.
  1892. You can set the variable `org-refile-target-verify-function' to a function
  1893. to verify each headline found by the simple criteria above.
  1894. When this variable is nil, all top-level headlines in the current buffer
  1895. are used, equivalent to the value `((nil . (:level . 1))'."
  1896. :group 'org-refile
  1897. :type '(repeat
  1898. (cons
  1899. (choice :value org-agenda-files
  1900. (const :tag "All agenda files" org-agenda-files)
  1901. (const :tag "Current buffer" nil)
  1902. (function) (variable) (file))
  1903. (choice :tag "Identify target headline by"
  1904. (cons :tag "Specific tag" (const :value :tag) (string))
  1905. (cons :tag "TODO keyword" (const :value :todo) (string))
  1906. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1907. (cons :tag "Level number" (const :value :level) (integer))
  1908. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1909. (defcustom org-refile-target-verify-function nil
  1910. "Function to verify if the headline at point should be a refile target.
  1911. The function will be called without arguments, with point at the
  1912. beginning of the headline. It should return t and leave point
  1913. where it is if the headline is a valid target for refiling.
  1914. If the target should not be selected, the function must return nil.
  1915. In addition to this, it may move point to a place from where the search
  1916. should be continued. For example, the function may decide that the entire
  1917. subtree of the current entry should be excluded and move point to the end
  1918. of the subtree."
  1919. :group 'org-refile
  1920. :type '(choice
  1921. (const nil)
  1922. (function)))
  1923. (defcustom org-refile-use-cache nil
  1924. "Non-nil means cache refile targets to speed up the process.
  1925. The cache for a particular file will be updated automatically when
  1926. the buffer has been killed, or when any of the marker used for flagging
  1927. refile targets no longer points at a live buffer.
  1928. If you have added new entries to a buffer that might themselves be targets,
  1929. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  1930. find that easier, `C-u C-u C-u C-c C-w'."
  1931. :group 'org-refile
  1932. :version "24.1"
  1933. :type 'boolean)
  1934. (defcustom org-refile-use-outline-path nil
  1935. "Non-nil means provide refile targets as paths.
  1936. So a level 3 headline will be available as level1/level2/level3.
  1937. When the value is `file', also include the file name (without directory)
  1938. into the path. In this case, you can also stop the completion after
  1939. the file name, to get entries inserted as top level in the file.
  1940. When `full-file-path', include the full file path."
  1941. :group 'org-refile
  1942. :type '(choice
  1943. (const :tag "Not" nil)
  1944. (const :tag "Yes" t)
  1945. (const :tag "Start with file name" file)
  1946. (const :tag "Start with full file path" full-file-path)))
  1947. (defcustom org-outline-path-complete-in-steps t
  1948. "Non-nil means complete the outline path in hierarchical steps.
  1949. When Org-mode uses the refile interface to select an outline path
  1950. \(see variable `org-refile-use-outline-path'), the completion of
  1951. the path can be done is a single go, or if can be done in steps down
  1952. the headline hierarchy. Going in steps is probably the best if you
  1953. do not use a special completion package like `ido' or `icicles'.
  1954. However, when using these packages, going in one step can be very
  1955. fast, while still showing the whole path to the entry."
  1956. :group 'org-refile
  1957. :type 'boolean)
  1958. (defcustom org-refile-allow-creating-parent-nodes nil
  1959. "Non-nil means allow to create new nodes as refile targets.
  1960. New nodes are then created by adding \"/new node name\" to the completion
  1961. of an existing node. When the value of this variable is `confirm',
  1962. new node creation must be confirmed by the user (recommended)
  1963. When nil, the completion must match an existing entry.
  1964. Note that, if the new heading is not seen by the criteria
  1965. listed in `org-refile-targets', multiple instances of the same
  1966. heading would be created by trying again to file under the new
  1967. heading."
  1968. :group 'org-refile
  1969. :type '(choice
  1970. (const :tag "Never" nil)
  1971. (const :tag "Always" t)
  1972. (const :tag "Prompt for confirmation" confirm)))
  1973. (defcustom org-refile-active-region-within-subtree nil
  1974. "Non-nil means also refile active region within a subtree.
  1975. By default `org-refile' doesn't allow refiling regions if they
  1976. don't contain a set of subtrees, but it might be convenient to
  1977. do so sometimes: in that case, the first line of the region is
  1978. converted to a headline before refiling."
  1979. :group 'org-refile
  1980. :version "24.1"
  1981. :type 'boolean)
  1982. (defgroup org-todo nil
  1983. "Options concerning TODO items in Org-mode."
  1984. :tag "Org TODO"
  1985. :group 'org)
  1986. (defgroup org-progress nil
  1987. "Options concerning Progress logging in Org-mode."
  1988. :tag "Org Progress"
  1989. :group 'org-time)
  1990. (defvar org-todo-interpretation-widgets
  1991. '((:tag "Sequence (cycling hits every state)" sequence)
  1992. (:tag "Type (cycling directly to DONE)" type))
  1993. "The available interpretation symbols for customizing `org-todo-keywords'.
  1994. Interested libraries should add to this list.")
  1995. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1996. "List of TODO entry keyword sequences and their interpretation.
  1997. \\<org-mode-map>This is a list of sequences.
  1998. Each sequence starts with a symbol, either `sequence' or `type',
  1999. indicating if the keywords should be interpreted as a sequence of
  2000. action steps, or as different types of TODO items. The first
  2001. keywords are states requiring action - these states will select a headline
  2002. for inclusion into the global TODO list Org-mode produces. If one of
  2003. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  2004. signify that no further action is necessary. If \"|\" is not found,
  2005. the last keyword is treated as the only DONE state of the sequence.
  2006. The command \\[org-todo] cycles an entry through these states, and one
  2007. additional state where no keyword is present. For details about this
  2008. cycling, see the manual.
  2009. TODO keywords and interpretation can also be set on a per-file basis with
  2010. the special #+SEQ_TODO and #+TYP_TODO lines.
  2011. Each keyword can optionally specify a character for fast state selection
  2012. \(in combination with the variable `org-use-fast-todo-selection')
  2013. and specifiers for state change logging, using the same syntax that
  2014. is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says that
  2015. the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  2016. indicates to record a time stamp each time this state is selected.
  2017. Each keyword may also specify if a timestamp or a note should be
  2018. recorded when entering or leaving the state, by adding additional
  2019. characters in the parenthesis after the keyword. This looks like this:
  2020. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  2021. record only the time of the state change. With X and Y being either
  2022. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  2023. Y when leaving the state if and only if the *target* state does not
  2024. define X. You may omit any of the fast-selection key or X or /Y,
  2025. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  2026. For backward compatibility, this variable may also be just a list
  2027. of keywords. In this case the interpretation (sequence or type) will be
  2028. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  2029. :group 'org-todo
  2030. :group 'org-keywords
  2031. :type '(choice
  2032. (repeat :tag "Old syntax, just keywords"
  2033. (string :tag "Keyword"))
  2034. (repeat :tag "New syntax"
  2035. (cons
  2036. (choice
  2037. :tag "Interpretation"
  2038. ;;Quick and dirty way to see
  2039. ;;`org-todo-interpretations'. This takes the
  2040. ;;place of item arguments
  2041. :convert-widget
  2042. (lambda (widget)
  2043. (widget-put widget
  2044. :args (mapcar
  2045. #'(lambda (x)
  2046. (widget-convert
  2047. (cons 'const x)))
  2048. org-todo-interpretation-widgets))
  2049. widget))
  2050. (repeat
  2051. (string :tag "Keyword"))))))
  2052. (defvar org-todo-keywords-1 nil
  2053. "All TODO and DONE keywords active in a buffer.")
  2054. (make-variable-buffer-local 'org-todo-keywords-1)
  2055. (defvar org-todo-keywords-for-agenda nil)
  2056. (defvar org-done-keywords-for-agenda nil)
  2057. (defvar org-drawers-for-agenda nil)
  2058. (defvar org-todo-keyword-alist-for-agenda nil)
  2059. (defvar org-tag-alist-for-agenda nil
  2060. "Alist of all tags from all agenda files.")
  2061. (defvar org-tag-groups-alist-for-agenda nil
  2062. "Alist of all groups tags from all current agenda files.")
  2063. (defvar org-tag-groups-alist nil)
  2064. (make-variable-buffer-local 'org-tag-groups-alist)
  2065. (defvar org-agenda-contributing-files nil)
  2066. (defvar org-not-done-keywords nil)
  2067. (make-variable-buffer-local 'org-not-done-keywords)
  2068. (defvar org-done-keywords nil)
  2069. (make-variable-buffer-local 'org-done-keywords)
  2070. (defvar org-todo-heads nil)
  2071. (make-variable-buffer-local 'org-todo-heads)
  2072. (defvar org-todo-sets nil)
  2073. (make-variable-buffer-local 'org-todo-sets)
  2074. (defvar org-todo-log-states nil)
  2075. (make-variable-buffer-local 'org-todo-log-states)
  2076. (defvar org-todo-kwd-alist nil)
  2077. (make-variable-buffer-local 'org-todo-kwd-alist)
  2078. (defvar org-todo-key-alist nil)
  2079. (make-variable-buffer-local 'org-todo-key-alist)
  2080. (defvar org-todo-key-trigger nil)
  2081. (make-variable-buffer-local 'org-todo-key-trigger)
  2082. (defcustom org-todo-interpretation 'sequence
  2083. "Controls how TODO keywords are interpreted.
  2084. This variable is in principle obsolete and is only used for
  2085. backward compatibility, if the interpretation of todo keywords is
  2086. not given already in `org-todo-keywords'. See that variable for
  2087. more information."
  2088. :group 'org-todo
  2089. :group 'org-keywords
  2090. :type '(choice (const sequence)
  2091. (const type)))
  2092. (defcustom org-use-fast-todo-selection t
  2093. "Non-nil means use the fast todo selection scheme with C-c C-t.
  2094. This variable describes if and under what circumstances the cycling
  2095. mechanism for TODO keywords will be replaced by a single-key, direct
  2096. selection scheme.
  2097. When nil, fast selection is never used.
  2098. When the symbol `prefix', it will be used when `org-todo' is called
  2099. with a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and
  2100. `C-u t' in an agenda buffer.
  2101. When t, fast selection is used by default. In this case, the prefix
  2102. argument forces cycling instead.
  2103. In all cases, the special interface is only used if access keys have
  2104. actually been assigned by the user, i.e. if keywords in the configuration
  2105. are followed by a letter in parenthesis, like TODO(t)."
  2106. :group 'org-todo
  2107. :type '(choice
  2108. (const :tag "Never" nil)
  2109. (const :tag "By default" t)
  2110. (const :tag "Only with C-u C-c C-t" prefix)))
  2111. (defcustom org-provide-todo-statistics t
  2112. "Non-nil means update todo statistics after insert and toggle.
  2113. ALL-HEADLINES means update todo statistics by including headlines
  2114. with no TODO keyword as well, counting them as not done.
  2115. A list of TODO keywords means the same, but skip keywords that are
  2116. not in this list.
  2117. When this is set, todo statistics is updated in the parent of the
  2118. current entry each time a todo state is changed."
  2119. :group 'org-todo
  2120. :type '(choice
  2121. (const :tag "Yes, only for TODO entries" t)
  2122. (const :tag "Yes, including all entries" 'all-headlines)
  2123. (repeat :tag "Yes, for TODOs in this list"
  2124. (string :tag "TODO keyword"))
  2125. (other :tag "No TODO statistics" nil)))
  2126. (defcustom org-hierarchical-todo-statistics t
  2127. "Non-nil means TODO statistics covers just direct children.
  2128. When nil, all entries in the subtree are considered.
  2129. This has only an effect if `org-provide-todo-statistics' is set.
  2130. To set this to nil for only a single subtree, use a COOKIE_DATA
  2131. property and include the word \"recursive\" into the value."
  2132. :group 'org-todo
  2133. :type 'boolean)
  2134. (defcustom org-after-todo-state-change-hook nil
  2135. "Hook which is run after the state of a TODO item was changed.
  2136. The new state (a string with a TODO keyword, or nil) is available in the
  2137. Lisp variable `org-state'."
  2138. :group 'org-todo
  2139. :type 'hook)
  2140. (defvar org-blocker-hook nil
  2141. "Hook for functions that are allowed to block a state change.
  2142. Functions in this hook should not modify the buffer.
  2143. Each function gets as its single argument a property list,
  2144. see `org-trigger-hook' for more information about this list.
  2145. If any of the functions in this hook returns nil, the state change
  2146. is blocked.")
  2147. (defvar org-trigger-hook nil
  2148. "Hook for functions that are triggered by a state change.
  2149. Each function gets as its single argument a property list with at
  2150. least the following elements:
  2151. (:type type-of-change :position pos-at-entry-start
  2152. :from old-state :to new-state)
  2153. Depending on the type, more properties may be present.
  2154. This mechanism is currently implemented for:
  2155. TODO state changes
  2156. ------------------
  2157. :type todo-state-change
  2158. :from previous state (keyword as a string), or nil, or a symbol
  2159. 'todo' or 'done', to indicate the general type of state.
  2160. :to new state, like in :from")
  2161. (defcustom org-enforce-todo-dependencies nil
  2162. "Non-nil means undone TODO entries will block switching the parent to DONE.
  2163. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  2164. be blocked if any prior sibling is not yet done.
  2165. Finally, if the parent is blocked because of ordered siblings of its own,
  2166. the child will also be blocked."
  2167. :set (lambda (var val)
  2168. (set var val)
  2169. (if val
  2170. (add-hook 'org-blocker-hook
  2171. 'org-block-todo-from-children-or-siblings-or-parent)
  2172. (remove-hook 'org-blocker-hook
  2173. 'org-block-todo-from-children-or-siblings-or-parent)))
  2174. :group 'org-todo
  2175. :type 'boolean)
  2176. (defcustom org-enforce-todo-checkbox-dependencies nil
  2177. "Non-nil means unchecked boxes will block switching the parent to DONE.
  2178. When this is nil, checkboxes have no influence on switching TODO states.
  2179. When non-nil, you first need to check off all check boxes before the TODO
  2180. entry can be switched to DONE.
  2181. This variable needs to be set before org.el is loaded, and you need to
  2182. restart Emacs after a change to make the change effective. The only way
  2183. to change is while Emacs is running is through the customize interface."
  2184. :set (lambda (var val)
  2185. (set var val)
  2186. (if val
  2187. (add-hook 'org-blocker-hook
  2188. 'org-block-todo-from-checkboxes)
  2189. (remove-hook 'org-blocker-hook
  2190. 'org-block-todo-from-checkboxes)))
  2191. :group 'org-todo
  2192. :type 'boolean)
  2193. (defcustom org-treat-insert-todo-heading-as-state-change nil
  2194. "Non-nil means inserting a TODO heading is treated as state change.
  2195. So when the command \\[org-insert-todo-heading] is used, state change
  2196. logging will apply if appropriate. When nil, the new TODO item will
  2197. be inserted directly, and no logging will take place."
  2198. :group 'org-todo
  2199. :type 'boolean)
  2200. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  2201. "Non-nil means switching TODO states with S-cursor counts as state change.
  2202. This is the default behavior. However, setting this to nil allows a
  2203. convenient way to select a TODO state and bypass any logging associated
  2204. with that."
  2205. :group 'org-todo
  2206. :type 'boolean)
  2207. (defcustom org-todo-state-tags-triggers nil
  2208. "Tag changes that should be triggered by TODO state changes.
  2209. This is a list. Each entry is
  2210. (state-change (tag . flag) .......)
  2211. State-change can be a string with a state, and empty string to indicate the
  2212. state that has no TODO keyword, or it can be one of the symbols `todo'
  2213. or `done', meaning any not-done or done state, respectively."
  2214. :group 'org-todo
  2215. :group 'org-tags
  2216. :type '(repeat
  2217. (cons (choice :tag "When changing to"
  2218. (const :tag "Not-done state" todo)
  2219. (const :tag "Done state" done)
  2220. (string :tag "State"))
  2221. (repeat
  2222. (cons :tag "Tag action"
  2223. (string :tag "Tag")
  2224. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  2225. (defcustom org-log-done nil
  2226. "Information to record when a task moves to the DONE state.
  2227. Possible values are:
  2228. nil Don't add anything, just change the keyword
  2229. time Add a time stamp to the task
  2230. note Prompt for a note and add it with template `org-log-note-headings'
  2231. This option can also be set with on a per-file-basis with
  2232. #+STARTUP: nologdone
  2233. #+STARTUP: logdone
  2234. #+STARTUP: lognotedone
  2235. You can have local logging settings for a subtree by setting the LOGGING
  2236. property to one or more of these keywords."
  2237. :group 'org-todo
  2238. :group 'org-progress
  2239. :type '(choice
  2240. (const :tag "No logging" nil)
  2241. (const :tag "Record CLOSED timestamp" time)
  2242. (const :tag "Record CLOSED timestamp with note." note)))
  2243. ;; Normalize old uses of org-log-done.
  2244. (cond
  2245. ((eq org-log-done t) (setq org-log-done 'time))
  2246. ((and (listp org-log-done) (memq 'done org-log-done))
  2247. (setq org-log-done 'note)))
  2248. (defcustom org-log-reschedule nil
  2249. "Information to record when the scheduling date of a tasks is modified.
  2250. Possible values are:
  2251. nil Don't add anything, just change the date
  2252. time Add a time stamp to the task
  2253. note Prompt for a note and add it with template `org-log-note-headings'
  2254. This option can also be set with on a per-file-basis with
  2255. #+STARTUP: nologreschedule
  2256. #+STARTUP: logreschedule
  2257. #+STARTUP: lognotereschedule"
  2258. :group 'org-todo
  2259. :group 'org-progress
  2260. :type '(choice
  2261. (const :tag "No logging" nil)
  2262. (const :tag "Record timestamp" time)
  2263. (const :tag "Record timestamp with note." note)))
  2264. (defcustom org-log-redeadline nil
  2265. "Information to record when the deadline date of a tasks is modified.
  2266. Possible values are:
  2267. nil Don't add anything, just change the date
  2268. time Add a time stamp to the task
  2269. note Prompt for a note and add it with template `org-log-note-headings'
  2270. This option can also be set with on a per-file-basis with
  2271. #+STARTUP: nologredeadline
  2272. #+STARTUP: logredeadline
  2273. #+STARTUP: lognoteredeadline
  2274. You can have local logging settings for a subtree by setting the LOGGING
  2275. property to one or more of these keywords."
  2276. :group 'org-todo
  2277. :group 'org-progress
  2278. :type '(choice
  2279. (const :tag "No logging" nil)
  2280. (const :tag "Record timestamp" time)
  2281. (const :tag "Record timestamp with note." note)))
  2282. (defcustom org-log-note-clock-out nil
  2283. "Non-nil means record a note when clocking out of an item.
  2284. This can also be configured on a per-file basis by adding one of
  2285. the following lines anywhere in the buffer:
  2286. #+STARTUP: lognoteclock-out
  2287. #+STARTUP: nolognoteclock-out"
  2288. :group 'org-todo
  2289. :group 'org-progress
  2290. :type 'boolean)
  2291. (defcustom org-log-done-with-time t
  2292. "Non-nil means the CLOSED time stamp will contain date and time.
  2293. When nil, only the date will be recorded."
  2294. :group 'org-progress
  2295. :type 'boolean)
  2296. (defcustom org-log-note-headings
  2297. '((done . "CLOSING NOTE %t")
  2298. (state . "State %-12s from %-12S %t")
  2299. (note . "Note taken on %t")
  2300. (reschedule . "Rescheduled from %S on %t")
  2301. (delschedule . "Not scheduled, was %S on %t")
  2302. (redeadline . "New deadline from %S on %t")
  2303. (deldeadline . "Removed deadline, was %S on %t")
  2304. (refile . "Refiled on %t")
  2305. (clock-out . ""))
  2306. "Headings for notes added to entries.
  2307. The value is an alist, with the car being a symbol indicating the note
  2308. context, and the cdr is the heading to be used. The heading may also be the
  2309. empty string.
  2310. %t in the heading will be replaced by a time stamp.
  2311. %T will be an active time stamp instead the default inactive one
  2312. %d will be replaced by a short-format time stamp.
  2313. %D will be replaced by an active short-format time stamp.
  2314. %s will be replaced by the new TODO state, in double quotes.
  2315. %S will be replaced by the old TODO state, in double quotes.
  2316. %u will be replaced by the user name.
  2317. %U will be replaced by the full user name.
  2318. In fact, it is not a good idea to change the `state' entry, because
  2319. agenda log mode depends on the format of these entries."
  2320. :group 'org-todo
  2321. :group 'org-progress
  2322. :type '(list :greedy t
  2323. (cons (const :tag "Heading when closing an item" done) string)
  2324. (cons (const :tag
  2325. "Heading when changing todo state (todo sequence only)"
  2326. state) string)
  2327. (cons (const :tag "Heading when just taking a note" note) string)
  2328. (cons (const :tag "Heading when clocking out" clock-out) string)
  2329. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2330. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2331. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2332. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2333. (cons (const :tag "Heading when refiling" refile) string)))
  2334. (unless (assq 'note org-log-note-headings)
  2335. (push '(note . "%t") org-log-note-headings))
  2336. (defcustom org-log-into-drawer nil
  2337. "Non-nil means insert state change notes and time stamps into a drawer.
  2338. When nil, state changes notes will be inserted after the headline and
  2339. any scheduling and clock lines, but not inside a drawer.
  2340. The value of this variable should be the name of the drawer to use.
  2341. LOGBOOK is proposed as the default drawer for this purpose, you can
  2342. also set this to a string to define the drawer of your choice.
  2343. A value of t is also allowed, representing \"LOGBOOK\".
  2344. A value of t or nil can also be set with on a per-file-basis with
  2345. #+STARTUP: logdrawer
  2346. #+STARTUP: nologdrawer
  2347. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2348. will be ignored.
  2349. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2350. a subtree."
  2351. :group 'org-todo
  2352. :group 'org-progress
  2353. :type '(choice
  2354. (const :tag "Not into a drawer" nil)
  2355. (const :tag "LOGBOOK" t)
  2356. (string :tag "Other")))
  2357. (org-defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer)
  2358. (defun org-log-into-drawer ()
  2359. "Return the value of `org-log-into-drawer', but let properties overrule.
  2360. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  2361. used instead of the default value."
  2362. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit t)))
  2363. (cond
  2364. ((not p) org-log-into-drawer)
  2365. ((equal p "nil") nil)
  2366. ((equal p "t") "LOGBOOK")
  2367. (t p))))
  2368. (defcustom org-log-state-notes-insert-after-drawers nil
  2369. "Non-nil means insert state change notes after any drawers in entry.
  2370. Only the drawers that *immediately* follow the headline and the
  2371. deadline/scheduled line are skipped.
  2372. When nil, insert notes right after the heading and perhaps the line
  2373. with deadline/scheduling if present.
  2374. This variable will have no effect if `org-log-into-drawer' is
  2375. set."
  2376. :group 'org-todo
  2377. :group 'org-progress
  2378. :type 'boolean)
  2379. (defcustom org-log-states-order-reversed t
  2380. "Non-nil means the latest state note will be directly after heading.
  2381. When nil, the state change notes will be ordered according to time.
  2382. This option can also be set with on a per-file-basis with
  2383. #+STARTUP: logstatesreversed
  2384. #+STARTUP: nologstatesreversed"
  2385. :group 'org-todo
  2386. :group 'org-progress
  2387. :type 'boolean)
  2388. (defcustom org-todo-repeat-to-state nil
  2389. "The TODO state to which a repeater should return the repeating task.
  2390. By default this is the first task in a TODO sequence, or the previous state
  2391. in a TODO_TYP set. But you can specify another task here.
  2392. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2393. :group 'org-todo
  2394. :version "24.1"
  2395. :type '(choice (const :tag "Head of sequence" nil)
  2396. (string :tag "Specific state")))
  2397. (defcustom org-log-repeat 'time
  2398. "Non-nil means record moving through the DONE state when triggering repeat.
  2399. An auto-repeating task is immediately switched back to TODO when
  2400. marked DONE. If you are not logging state changes (by adding \"@\"
  2401. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2402. record a closing note, there will be no record of the task moving
  2403. through DONE. This variable forces taking a note anyway.
  2404. nil Don't force a record
  2405. time Record a time stamp
  2406. note Prompt for a note and add it with template `org-log-note-headings'
  2407. This option can also be set with on a per-file-basis with
  2408. #+STARTUP: nologrepeat
  2409. #+STARTUP: logrepeat
  2410. #+STARTUP: lognoterepeat
  2411. You can have local logging settings for a subtree by setting the LOGGING
  2412. property to one or more of these keywords."
  2413. :group 'org-todo
  2414. :group 'org-progress
  2415. :type '(choice
  2416. (const :tag "Don't force a record" nil)
  2417. (const :tag "Force recording the DONE state" time)
  2418. (const :tag "Force recording a note with the DONE state" note)))
  2419. (defgroup org-priorities nil
  2420. "Priorities in Org-mode."
  2421. :tag "Org Priorities"
  2422. :group 'org-todo)
  2423. (defcustom org-enable-priority-commands t
  2424. "Non-nil means priority commands are active.
  2425. When nil, these commands will be disabled, so that you never accidentally
  2426. set a priority."
  2427. :group 'org-priorities
  2428. :type 'boolean)
  2429. (defcustom org-highest-priority ?A
  2430. "The highest priority of TODO items. A character like ?A, ?B etc.
  2431. Must have a smaller ASCII number than `org-lowest-priority'."
  2432. :group 'org-priorities
  2433. :type 'character)
  2434. (defcustom org-lowest-priority ?C
  2435. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2436. Must have a larger ASCII number than `org-highest-priority'."
  2437. :group 'org-priorities
  2438. :type 'character)
  2439. (defcustom org-default-priority ?B
  2440. "The default priority of TODO items.
  2441. This is the priority an item gets if no explicit priority is given.
  2442. When starting to cycle on an empty priority the first step in the cycle
  2443. depends on `org-priority-start-cycle-with-default'. The resulting first
  2444. step priority must not exceed the range from `org-highest-priority' to
  2445. `org-lowest-priority' which means that `org-default-priority' has to be
  2446. in this range exclusive or inclusive the range boundaries. Else the
  2447. first step refuses to set the default and the second will fall back
  2448. to (depending on the command used) the highest or lowest priority."
  2449. :group 'org-priorities
  2450. :type 'character)
  2451. (defcustom org-priority-start-cycle-with-default t
  2452. "Non-nil means start with default priority when starting to cycle.
  2453. When this is nil, the first step in the cycle will be (depending on the
  2454. command used) one higher or lower than the default priority.
  2455. See also `org-default-priority'."
  2456. :group 'org-priorities
  2457. :type 'boolean)
  2458. (defcustom org-get-priority-function nil
  2459. "Function to extract the priority from a string.
  2460. The string is normally the headline. If this is nil Org computes the
  2461. priority from the priority cookie like [#A] in the headline. It returns
  2462. an integer, increasing by 1000 for each priority level.
  2463. The user can set a different function here, which should take a string
  2464. as an argument and return the numeric priority."
  2465. :group 'org-priorities
  2466. :version "24.1"
  2467. :type '(choice
  2468. (const nil)
  2469. (function)))
  2470. (defgroup org-time nil
  2471. "Options concerning time stamps and deadlines in Org-mode."
  2472. :tag "Org Time"
  2473. :group 'org)
  2474. (defcustom org-insert-labeled-timestamps-at-point nil
  2475. "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
  2476. When nil, these labeled time stamps are forces into the second line of an
  2477. entry, just after the headline. When scheduling from the global TODO list,
  2478. the time stamp will always be forced into the second line."
  2479. :group 'org-time
  2480. :type 'boolean)
  2481. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  2482. "Formats for `format-time-string' which are used for time stamps.
  2483. It is not recommended to change this constant.")
  2484. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2485. "Number of minutes to round time stamps to.
  2486. These are two values, the first applies when first creating a time stamp.
  2487. The second applies when changing it with the commands `S-up' and `S-down'.
  2488. When changing the time stamp, this means that it will change in steps
  2489. of N minutes, as given by the second value.
  2490. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2491. numbers should be factors of 60, so for example 5, 10, 15.
  2492. When this is larger than 1, you can still force an exact time stamp by using
  2493. a double prefix argument to a time stamp command like `C-c .' or `C-c !',
  2494. and by using a prefix arg to `S-up/down' to specify the exact number
  2495. of minutes to shift."
  2496. :group 'org-time
  2497. :get #'(lambda (var) ; Make sure both elements are there
  2498. (if (integerp (default-value var))
  2499. (list (default-value var) 5)
  2500. (default-value var)))
  2501. :type '(list
  2502. (integer :tag "when inserting times")
  2503. (integer :tag "when modifying times")))
  2504. ;; Normalize old customizations of this variable.
  2505. (when (integerp org-time-stamp-rounding-minutes)
  2506. (setq org-time-stamp-rounding-minutes
  2507. (list org-time-stamp-rounding-minutes
  2508. org-time-stamp-rounding-minutes)))
  2509. (defcustom org-display-custom-times nil
  2510. "Non-nil means overlay custom formats over all time stamps.
  2511. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2512. To turn this on on a per-file basis, insert anywhere in the file:
  2513. #+STARTUP: customtime"
  2514. :group 'org-time
  2515. :set 'set-default
  2516. :type 'sexp)
  2517. (make-variable-buffer-local 'org-display-custom-times)
  2518. (defcustom org-time-stamp-custom-formats
  2519. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2520. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2521. These are overlaid over the default ISO format if the variable
  2522. `org-display-custom-times' is set. Time like %H:%M should be at the
  2523. end of the second format. The custom formats are also honored by export
  2524. commands, if custom time display is turned on at the time of export."
  2525. :group 'org-time
  2526. :type 'sexp)
  2527. (defun org-time-stamp-format (&optional long inactive)
  2528. "Get the right format for a time string."
  2529. (let ((f (if long (cdr org-time-stamp-formats)
  2530. (car org-time-stamp-formats))))
  2531. (if inactive
  2532. (concat "[" (substring f 1 -1) "]")
  2533. f)))
  2534. (defcustom org-time-clocksum-format
  2535. '(:days "%dd " :hours "%d" :require-hours t :minutes ":%02d" :require-minutes t)
  2536. "The format string used when creating CLOCKSUM lines.
  2537. This is also used when Org mode generates a time duration.
  2538. The value can be a single format string containing two
  2539. %-sequences, which will be filled with the number of hours and
  2540. minutes in that order.
  2541. Alternatively, the value can be a plist associating any of the
  2542. keys :years, :months, :weeks, :days, :hours or :minutes with
  2543. format strings. The time duration is formatted using only the
  2544. time components that are needed and concatenating the results.
  2545. If a time unit in absent, it falls back to the next smallest
  2546. unit.
  2547. The keys :require-years, :require-months, :require-days,
  2548. :require-weeks, :require-hours, :require-minutes are also
  2549. meaningful. A non-nil value for these keys indicates that the
  2550. corresponding time component should always be included, even if
  2551. its value is 0.
  2552. For example,
  2553. \(:days \"%dd\" :hours \"%d\" :require-hours t :minutes \":%02d\"
  2554. :require-minutes t)
  2555. means durations longer than a day will be expressed in days,
  2556. hours and minutes, and durations less than a day will always be
  2557. expressed in hours and minutes (even for durations less than an
  2558. hour).
  2559. The value
  2560. \(:days \"%dd\" :minutes \"%dm\")
  2561. means durations longer than a day will be expressed in days and
  2562. minutes, and durations less than a day will be expressed entirely
  2563. in minutes (even for durations longer than an hour)."
  2564. :group 'org-time
  2565. :group 'org-clock
  2566. :version "24.4"
  2567. :package-version '(Org . "8.0")
  2568. :type '(choice (string :tag "Format string")
  2569. (set :tag "Plist"
  2570. (group :inline t (const :tag "Years" :years)
  2571. (string :tag "Format string"))
  2572. (group :inline t
  2573. (const :tag "Always show years" :require-years)
  2574. (const t))
  2575. (group :inline t (const :tag "Months" :months)
  2576. (string :tag "Format string"))
  2577. (group :inline t
  2578. (const :tag "Always show months" :require-months)
  2579. (const t))
  2580. (group :inline t (const :tag "Weeks" :weeks)
  2581. (string :tag "Format string"))
  2582. (group :inline t
  2583. (const :tag "Always show weeks" :require-weeks)
  2584. (const t))
  2585. (group :inline t (const :tag "Days" :days)
  2586. (string :tag "Format string"))
  2587. (group :inline t
  2588. (const :tag "Always show days" :require-days)
  2589. (const t))
  2590. (group :inline t (const :tag "Hours" :hours)
  2591. (string :tag "Format string"))
  2592. (group :inline t
  2593. (const :tag "Always show hours" :require-hours)
  2594. (const t))
  2595. (group :inline t (const :tag "Minutes" :minutes)
  2596. (string :tag "Format string"))
  2597. (group :inline t
  2598. (const :tag "Always show minutes" :require-minutes)
  2599. (const t)))))
  2600. (defcustom org-time-clocksum-use-fractional nil
  2601. "When non-nil, \\[org-clock-display] uses fractional times.
  2602. See `org-time-clocksum-format' for more on time clock formats."
  2603. :group 'org-time
  2604. :group 'org-clock
  2605. :version "24.3"
  2606. :type 'boolean)
  2607. (defcustom org-time-clocksum-use-effort-durations nil
  2608. "When non-nil, \\[org-clock-display] uses effort durations.
  2609. E.g. by default, one day is considered to be a 8 hours effort,
  2610. so a task that has been clocked for 16 hours will be displayed
  2611. as during 2 days in the clock display or in the clocktable.
  2612. See `org-effort-durations' on how to set effort durations
  2613. and `org-time-clocksum-format' for more on time clock formats."
  2614. :group 'org-time
  2615. :group 'org-clock
  2616. :version "24.4"
  2617. :package-version '(Org . "8.0")
  2618. :type 'boolean)
  2619. (defcustom org-time-clocksum-fractional-format "%.2f"
  2620. "The format string used when creating CLOCKSUM lines,
  2621. or when Org mode generates a time duration, if
  2622. `org-time-clocksum-use-fractional' is enabled.
  2623. The value can be a single format string containing one
  2624. %-sequence, which will be filled with the number of hours as
  2625. a float.
  2626. Alternatively, the value can be a plist associating any of the
  2627. keys :years, :months, :weeks, :days, :hours or :minutes with
  2628. a format string. The time duration is formatted using the
  2629. largest time unit which gives a non-zero integer part. If all
  2630. specified formats have zero integer part, the smallest time unit
  2631. is used."
  2632. :group 'org-time
  2633. :type '(choice (string :tag "Format string")
  2634. (set (group :inline t (const :tag "Years" :years)
  2635. (string :tag "Format string"))
  2636. (group :inline t (const :tag "Months" :months)
  2637. (string :tag "Format string"))
  2638. (group :inline t (const :tag "Weeks" :weeks)
  2639. (string :tag "Format string"))
  2640. (group :inline t (const :tag "Days" :days)
  2641. (string :tag "Format string"))
  2642. (group :inline t (const :tag "Hours" :hours)
  2643. (string :tag "Format string"))
  2644. (group :inline t (const :tag "Minutes" :minutes)
  2645. (string :tag "Format string")))))
  2646. (defcustom org-deadline-warning-days 14
  2647. "Number of days before expiration during which a deadline becomes active.
  2648. This variable governs the display in sparse trees and in the agenda.
  2649. When 0 or negative, it means use this number (the absolute value of it)
  2650. even if a deadline has a different individual lead time specified.
  2651. Custom commands can set this variable in the options section."
  2652. :group 'org-time
  2653. :group 'org-agenda-daily/weekly
  2654. :type 'integer)
  2655. (defcustom org-scheduled-delay-days 0
  2656. "Number of days before a scheduled item becomes active.
  2657. This variable governs the display in sparse trees and in the agenda.
  2658. The default value (i.e. 0) means: don't delay scheduled item.
  2659. When negative, it means use this number (the absolute value of it)
  2660. even if a scheduled item has a different individual delay time
  2661. specified.
  2662. Custom commands can set this variable in the options section."
  2663. :group 'org-time
  2664. :group 'org-agenda-daily/weekly
  2665. :version "24.4"
  2666. :package-version '(Org . "8.0")
  2667. :type 'integer)
  2668. (defcustom org-read-date-prefer-future t
  2669. "Non-nil means assume future for incomplete date input from user.
  2670. This affects the following situations:
  2671. 1. The user gives a month but not a year.
  2672. For example, if it is April and you enter \"feb 2\", this will be read
  2673. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2674. 2. The user gives a day, but no month.
  2675. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2676. read this as the third of *next* month. However, if you enter \"17\",
  2677. it will be considered as *this* month.
  2678. If you set this variable to the symbol `time', then also the following
  2679. will work:
  2680. 3. If the user gives a time.
  2681. If the time is before now, it will be interpreted as tomorrow.
  2682. Currently none of this works for ISO week specifications.
  2683. When this option is nil, the current day, month and year will always be
  2684. used as defaults.
  2685. See also `org-agenda-jump-prefer-future'."
  2686. :group 'org-time
  2687. :type '(choice
  2688. (const :tag "Never" nil)
  2689. (const :tag "Check month and day" t)
  2690. (const :tag "Check month, day, and time" time)))
  2691. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2692. "Should the agenda jump command prefer the future for incomplete dates?
  2693. The default is to do the same as configured in `org-read-date-prefer-future'.
  2694. But you can also set a deviating value here.
  2695. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2696. :group 'org-agenda
  2697. :group 'org-time
  2698. :version "24.1"
  2699. :type '(choice
  2700. (const :tag "Use org-read-date-prefer-future"
  2701. org-read-date-prefer-future)
  2702. (const :tag "Never" nil)
  2703. (const :tag "Always" t)))
  2704. (defcustom org-read-date-force-compatible-dates t
  2705. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2706. Depending on the system Emacs is running on, certain dates cannot
  2707. be represented with the type used internally to represent time.
  2708. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2709. correctly. Some systems allow for earlier dates, some for later,
  2710. some for both. One way to find out it to insert any date into an
  2711. Org buffer, putting the cursor on the year and hitting S-up and
  2712. S-down to test the range.
  2713. When this variable is set to t, the date/time prompt will not let
  2714. you specify dates outside the 1970-2037 range, so it is certain that
  2715. these dates will work in whatever version of Emacs you are
  2716. running, and also that you can move a file from one Emacs implementation
  2717. to another. WHenever Org is forcing the year for you, it will display
  2718. a message and beep.
  2719. When this variable is nil, Org will check if the date is
  2720. representable in the specific Emacs implementation you are using.
  2721. If not, it will force a year, usually the current year, and beep
  2722. to remind you. Currently this setting is not recommended because
  2723. the likelihood that you will open your Org files in an Emacs that
  2724. has limited date range is not negligible.
  2725. A workaround for this problem is to use diary sexp dates for time
  2726. stamps outside of this range."
  2727. :group 'org-time
  2728. :version "24.1"
  2729. :type 'boolean)
  2730. (defcustom org-read-date-display-live t
  2731. "Non-nil means display current interpretation of date prompt live.
  2732. This display will be in an overlay, in the minibuffer."
  2733. :group 'org-time
  2734. :type 'boolean)
  2735. (defcustom org-read-date-popup-calendar t
  2736. "Non-nil means pop up a calendar when prompting for a date.
  2737. In the calendar, the date can be selected with mouse-1. However, the
  2738. minibuffer will also be active, and you can simply enter the date as well.
  2739. When nil, only the minibuffer will be available."
  2740. :group 'org-time
  2741. :type 'boolean)
  2742. (org-defvaralias 'org-popup-calendar-for-date-prompt
  2743. 'org-read-date-popup-calendar)
  2744. (make-obsolete-variable
  2745. 'org-read-date-minibuffer-setup-hook
  2746. "Set `org-read-date-minibuffer-local-map' instead." "24.4")
  2747. (defcustom org-read-date-minibuffer-setup-hook nil
  2748. "Hook to be used to set up keys for the date/time interface.
  2749. Add key definitions to `minibuffer-local-map', which will be a
  2750. temporary copy.
  2751. WARNING: This option is obsolete, you should use
  2752. `org-read-date-minibuffer-local-map' to set up keys."
  2753. :group 'org-time
  2754. :type 'hook)
  2755. (defcustom org-extend-today-until 0
  2756. "The hour when your day really ends. Must be an integer.
  2757. This has influence for the following applications:
  2758. - When switching the agenda to \"today\". It it is still earlier than
  2759. the time given here, the day recognized as TODAY is actually yesterday.
  2760. - When a date is read from the user and it is still before the time given
  2761. here, the current date and time will be assumed to be yesterday, 23:59.
  2762. Also, timestamps inserted in capture templates follow this rule.
  2763. IMPORTANT: This is a feature whose implementation is and likely will
  2764. remain incomplete. Really, it is only here because past midnight seems to
  2765. be the favorite working time of John Wiegley :-)"
  2766. :group 'org-time
  2767. :type 'integer)
  2768. (defcustom org-use-effective-time nil
  2769. "If non-nil, consider `org-extend-today-until' when creating timestamps.
  2770. For example, if `org-extend-today-until' is 8, and it's 4am, then the
  2771. \"effective time\" of any timestamps between midnight and 8am will be
  2772. 23:59 of the previous day."
  2773. :group 'org-time
  2774. :version "24.1"
  2775. :type 'boolean)
  2776. (defcustom org-use-last-clock-out-time-as-effective-time nil
  2777. "When non-nil, use the last clock out time for `org-todo'.
  2778. Note that this option has precedence over the combined use of
  2779. `org-use-effective-time' and `org-extend-today-until'."
  2780. :group 'org-time
  2781. :version "24.4"
  2782. :package-version '(Org . "8.0")
  2783. :type 'boolean)
  2784. (defcustom org-edit-timestamp-down-means-later nil
  2785. "Non-nil means S-down will increase the time in a time stamp.
  2786. When nil, S-up will increase."
  2787. :group 'org-time
  2788. :type 'boolean)
  2789. (defcustom org-calendar-follow-timestamp-change t
  2790. "Non-nil means make the calendar window follow timestamp changes.
  2791. When a timestamp is modified and the calendar window is visible, it will be
  2792. moved to the new date."
  2793. :group 'org-time
  2794. :type 'boolean)
  2795. (defgroup org-tags nil
  2796. "Options concerning tags in Org-mode."
  2797. :tag "Org Tags"
  2798. :group 'org)
  2799. (defcustom org-tag-alist nil
  2800. "List of tags allowed in Org-mode files.
  2801. When this list is nil, Org-mode will base TAG input on what is already in the
  2802. buffer.
  2803. The value of this variable is an alist, the car of each entry must be a
  2804. keyword as a string, the cdr may be a character that is used to select
  2805. that tag through the fast-tag-selection interface.
  2806. See the manual for details."
  2807. :group 'org-tags
  2808. :type '(repeat
  2809. (choice
  2810. (cons (string :tag "Tag name")
  2811. (character :tag "Access char"))
  2812. (list :tag "Start radio group"
  2813. (const :startgroup)
  2814. (option (string :tag "Group description")))
  2815. (list :tag "Group tags delimiter"
  2816. (const :grouptags))
  2817. (list :tag "End radio group"
  2818. (const :endgroup)
  2819. (option (string :tag "Group description")))
  2820. (const :tag "New line" (:newline)))))
  2821. (defcustom org-tag-persistent-alist nil
  2822. "List of tags that will always appear in all Org-mode files.
  2823. This is in addition to any in buffer settings or customizations
  2824. of `org-tag-alist'.
  2825. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2826. The value of this variable is an alist, the car of each entry must be a
  2827. keyword as a string, the cdr may be a character that is used to select
  2828. that tag through the fast-tag-selection interface.
  2829. See the manual for details.
  2830. To disable these tags on a per-file basis, insert anywhere in the file:
  2831. #+STARTUP: noptag"
  2832. :group 'org-tags
  2833. :type '(repeat
  2834. (choice
  2835. (cons (string :tag "Tag name")
  2836. (character :tag "Access char"))
  2837. (const :tag "Start radio group" (:startgroup))
  2838. (const :tag "Group tags delimiter" (:grouptags))
  2839. (const :tag "End radio group" (:endgroup))
  2840. (const :tag "New line" (:newline)))))
  2841. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2842. "If non-nil, always offer completion for all tags of all agenda files.
  2843. Instead of customizing this variable directly, you might want to
  2844. set it locally for capture buffers, because there no list of
  2845. tags in that file can be created dynamically (there are none).
  2846. (add-hook 'org-capture-mode-hook
  2847. (lambda ()
  2848. (set (make-local-variable
  2849. 'org-complete-tags-always-offer-all-agenda-tags)
  2850. t)))"
  2851. :group 'org-tags
  2852. :version "24.1"
  2853. :type 'boolean)
  2854. (defvar org-file-tags nil
  2855. "List of tags that can be inherited by all entries in the file.
  2856. The tags will be inherited if the variable `org-use-tag-inheritance'
  2857. says they should be.
  2858. This variable is populated from #+FILETAGS lines.")
  2859. (defcustom org-use-fast-tag-selection 'auto
  2860. "Non-nil means use fast tag selection scheme.
  2861. This is a special interface to select and deselect tags with single keys.
  2862. When nil, fast selection is never used.
  2863. When the symbol `auto', fast selection is used if and only if selection
  2864. characters for tags have been configured, either through the variable
  2865. `org-tag-alist' or through a #+TAGS line in the buffer.
  2866. When t, fast selection is always used and selection keys are assigned
  2867. automatically if necessary."
  2868. :group 'org-tags
  2869. :type '(choice
  2870. (const :tag "Always" t)
  2871. (const :tag "Never" nil)
  2872. (const :tag "When selection characters are configured" auto)))
  2873. (defcustom org-fast-tag-selection-single-key nil
  2874. "Non-nil means fast tag selection exits after first change.
  2875. When nil, you have to press RET to exit it.
  2876. During fast tag selection, you can toggle this flag with `C-c'.
  2877. This variable can also have the value `expert'. In this case, the window
  2878. displaying the tags menu is not even shown, until you press C-c again."
  2879. :group 'org-tags
  2880. :type '(choice
  2881. (const :tag "No" nil)
  2882. (const :tag "Yes" t)
  2883. (const :tag "Expert" expert)))
  2884. (defvar org-fast-tag-selection-include-todo nil
  2885. "Non-nil means fast tags selection interface will also offer TODO states.
  2886. This is an undocumented feature, you should not rely on it.")
  2887. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2888. "The column to which tags should be indented in a headline.
  2889. If this number is positive, it specifies the column. If it is negative,
  2890. it means that the tags should be flushright to that column. For example,
  2891. -80 works well for a normal 80 character screen.
  2892. When 0, place tags directly after headline text, with only one space in
  2893. between."
  2894. :group 'org-tags
  2895. :type 'integer)
  2896. (defcustom org-auto-align-tags t
  2897. "Non-nil keeps tags aligned when modifying headlines.
  2898. Some operations (i.e. demoting) change the length of a headline and
  2899. therefore shift the tags around. With this option turned on, after
  2900. each such operation the tags are again aligned to `org-tags-column'."
  2901. :group 'org-tags
  2902. :type 'boolean)
  2903. (defcustom org-use-tag-inheritance t
  2904. "Non-nil means tags in levels apply also for sublevels.
  2905. When nil, only the tags directly given in a specific line apply there.
  2906. This may also be a list of tags that should be inherited, or a regexp that
  2907. matches tags that should be inherited. Additional control is possible
  2908. with the variable `org-tags-exclude-from-inheritance' which gives an
  2909. explicit list of tags to be excluded from inheritance, even if the value of
  2910. `org-use-tag-inheritance' would select it for inheritance.
  2911. If this option is t, a match early-on in a tree can lead to a large
  2912. number of matches in the subtree when constructing the agenda or creating
  2913. a sparse tree. If you only want to see the first match in a tree during
  2914. a search, check out the variable `org-tags-match-list-sublevels'."
  2915. :group 'org-tags
  2916. :type '(choice
  2917. (const :tag "Not" nil)
  2918. (const :tag "Always" t)
  2919. (repeat :tag "Specific tags" (string :tag "Tag"))
  2920. (regexp :tag "Tags matched by regexp")))
  2921. (defcustom org-tags-exclude-from-inheritance nil
  2922. "List of tags that should never be inherited.
  2923. This is a way to exclude a few tags from inheritance. For way to do
  2924. the opposite, to actively allow inheritance for selected tags,
  2925. see the variable `org-use-tag-inheritance'."
  2926. :group 'org-tags
  2927. :type '(repeat (string :tag "Tag")))
  2928. (defun org-tag-inherit-p (tag)
  2929. "Check if TAG is one that should be inherited."
  2930. (cond
  2931. ((member tag org-tags-exclude-from-inheritance) nil)
  2932. ((eq org-use-tag-inheritance t) t)
  2933. ((not org-use-tag-inheritance) nil)
  2934. ((stringp org-use-tag-inheritance)
  2935. (string-match org-use-tag-inheritance tag))
  2936. ((listp org-use-tag-inheritance)
  2937. (member tag org-use-tag-inheritance))
  2938. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2939. (defcustom org-tags-match-list-sublevels t
  2940. "Non-nil means list also sublevels of headlines matching a search.
  2941. This variable applies to tags/property searches, and also to stuck
  2942. projects because this search is based on a tags match as well.
  2943. When set to the symbol `indented', sublevels are indented with
  2944. leading dots.
  2945. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2946. the sublevels of a headline matching a tag search often also match
  2947. the same search. Listing all of them can create very long lists.
  2948. Setting this variable to nil causes subtrees of a match to be skipped.
  2949. This variable is semi-obsolete and probably should always be true. It
  2950. is better to limit inheritance to certain tags using the variables
  2951. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2952. :group 'org-tags
  2953. :type '(choice
  2954. (const :tag "No, don't list them" nil)
  2955. (const :tag "Yes, do list them" t)
  2956. (const :tag "List them, indented with leading dots" indented)))
  2957. (defcustom org-tags-sort-function nil
  2958. "When set, tags are sorted using this function as a comparator."
  2959. :group 'org-tags
  2960. :type '(choice
  2961. (const :tag "No sorting" nil)
  2962. (const :tag "Alphabetical" string<)
  2963. (const :tag "Reverse alphabetical" string>)
  2964. (function :tag "Custom function" nil)))
  2965. (defvar org-tags-history nil
  2966. "History of minibuffer reads for tags.")
  2967. (defvar org-last-tags-completion-table nil
  2968. "The last used completion table for tags.")
  2969. (defvar org-after-tags-change-hook nil
  2970. "Hook that is run after the tags in a line have changed.")
  2971. (defgroup org-properties nil
  2972. "Options concerning properties in Org-mode."
  2973. :tag "Org Properties"
  2974. :group 'org)
  2975. (defcustom org-property-format "%-10s %s"
  2976. "How property key/value pairs should be formatted by `indent-line'.
  2977. When `indent-line' hits a property definition, it will format the line
  2978. according to this format, mainly to make sure that the values are
  2979. lined-up with respect to each other."
  2980. :group 'org-properties
  2981. :type 'string)
  2982. (defcustom org-properties-postprocess-alist nil
  2983. "Alist of properties and functions to adjust inserted values.
  2984. Elements of this alist must be of the form
  2985. ([string] [function])
  2986. where [string] must be a property name and [function] must be a
  2987. lambda expression: this lambda expression must take one argument,
  2988. the value to adjust, and return the new value as a string.
  2989. For example, this element will allow the property \"Remaining\"
  2990. to be updated wrt the relation between the \"Effort\" property
  2991. and the clock summary:
  2992. ((\"Remaining\" (lambda(value)
  2993. (let ((clocksum (org-clock-sum-current-item))
  2994. (effort (org-duration-string-to-minutes
  2995. (org-entry-get (point) \"Effort\"))))
  2996. (org-minutes-to-clocksum-string (- effort clocksum))))))"
  2997. :group 'org-properties
  2998. :version "24.1"
  2999. :type '(alist :key-type (string :tag "Property")
  3000. :value-type (function :tag "Function")))
  3001. (defcustom org-use-property-inheritance nil
  3002. "Non-nil means properties apply also for sublevels.
  3003. This setting is chiefly used during property searches. Turning it on can
  3004. cause significant overhead when doing a search, which is why it is not
  3005. on by default.
  3006. When nil, only the properties directly given in the current entry count.
  3007. When t, every property is inherited. The value may also be a list of
  3008. properties that should have inheritance, or a regular expression matching
  3009. properties that should be inherited.
  3010. However, note that some special properties use inheritance under special
  3011. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  3012. and the properties ending in \"_ALL\" when they are used as descriptor
  3013. for valid values of a property.
  3014. Note for programmers:
  3015. When querying an entry with `org-entry-get', you can control if inheritance
  3016. should be used. By default, `org-entry-get' looks only at the local
  3017. properties. You can request inheritance by setting the inherit argument
  3018. to t (to force inheritance) or to `selective' (to respect the setting
  3019. in this variable)."
  3020. :group 'org-properties
  3021. :type '(choice
  3022. (const :tag "Not" nil)
  3023. (const :tag "Always" t)
  3024. (repeat :tag "Specific properties" (string :tag "Property"))
  3025. (regexp :tag "Properties matched by regexp")))
  3026. (defun org-property-inherit-p (property)
  3027. "Check if PROPERTY is one that should be inherited."
  3028. (cond
  3029. ((eq org-use-property-inheritance t) t)
  3030. ((not org-use-property-inheritance) nil)
  3031. ((stringp org-use-property-inheritance)
  3032. (string-match org-use-property-inheritance property))
  3033. ((listp org-use-property-inheritance)
  3034. (member property org-use-property-inheritance))
  3035. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  3036. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  3037. "The default column format, if no other format has been defined.
  3038. This variable can be set on the per-file basis by inserting a line
  3039. #+COLUMNS: %25ITEM ....."
  3040. :group 'org-properties
  3041. :type 'string)
  3042. (defcustom org-columns-ellipses ".."
  3043. "The ellipses to be used when a field in column view is truncated.
  3044. When this is the empty string, as many characters as possible are shown,
  3045. but then there will be no visual indication that the field has been truncated.
  3046. When this is a string of length N, the last N characters of a truncated
  3047. field are replaced by this string. If the column is narrower than the
  3048. ellipses string, only part of the ellipses string will be shown."
  3049. :group 'org-properties
  3050. :type 'string)
  3051. (defcustom org-columns-modify-value-for-display-function nil
  3052. "Function that modifies values for display in column view.
  3053. For example, it can be used to cut out a certain part from a time stamp.
  3054. The function must take 2 arguments:
  3055. column-title The title of the column (*not* the property name)
  3056. value The value that should be modified.
  3057. The function should return the value that should be displayed,
  3058. or nil if the normal value should be used."
  3059. :group 'org-properties
  3060. :type '(choice (const nil) (function)))
  3061. (defcustom org-effort-property "Effort"
  3062. "The property that is being used to keep track of effort estimates.
  3063. Effort estimates given in this property need to have the format H:MM."
  3064. :group 'org-properties
  3065. :group 'org-progress
  3066. :type '(string :tag "Property"))
  3067. (defconst org-global-properties-fixed
  3068. '(("VISIBILITY_ALL" . "folded children content all")
  3069. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  3070. "List of property/value pairs that can be inherited by any entry.
  3071. These are fixed values, for the preset properties. The user variable
  3072. that can be used to add to this list is `org-global-properties'.
  3073. The entries in this list are cons cells where the car is a property
  3074. name and cdr is a string with the value. If the value represents
  3075. multiple items like an \"_ALL\" property, separate the items by
  3076. spaces.")
  3077. (defcustom org-global-properties nil
  3078. "List of property/value pairs that can be inherited by any entry.
  3079. This list will be combined with the constant `org-global-properties-fixed'.
  3080. The entries in this list are cons cells where the car is a property
  3081. name and cdr is a string with the value.
  3082. You can set buffer-local values for the same purpose in the variable
  3083. `org-file-properties' this by adding lines like
  3084. #+PROPERTY: NAME VALUE"
  3085. :group 'org-properties
  3086. :type '(repeat
  3087. (cons (string :tag "Property")
  3088. (string :tag "Value"))))
  3089. (defvar org-file-properties nil
  3090. "List of property/value pairs that can be inherited by any entry.
  3091. Valid for the current buffer.
  3092. This variable is populated from #+PROPERTY lines.")
  3093. (make-variable-buffer-local 'org-file-properties)
  3094. (defgroup org-agenda nil
  3095. "Options concerning agenda views in Org-mode."
  3096. :tag "Org Agenda"
  3097. :group 'org)
  3098. (defvar org-category nil
  3099. "Variable used by org files to set a category for agenda display.
  3100. Such files should use a file variable to set it, for example
  3101. # -*- mode: org; org-category: \"ELisp\"
  3102. or contain a special line
  3103. #+CATEGORY: ELisp
  3104. If the file does not specify a category, then file's base name
  3105. is used instead.")
  3106. (make-variable-buffer-local 'org-category)
  3107. (put 'org-category 'safe-local-variable #'(lambda (x) (or (symbolp x) (stringp x))))
  3108. (defcustom org-agenda-files nil
  3109. "The files to be used for agenda display.
  3110. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  3111. \\[org-remove-file]. You can also use customize to edit the list.
  3112. If an entry is a directory, all files in that directory that are matched by
  3113. `org-agenda-file-regexp' will be part of the file list.
  3114. If the value of the variable is not a list but a single file name, then
  3115. the list of agenda files is actually stored and maintained in that file, one
  3116. agenda file per line. In this file paths can be given relative to
  3117. `org-directory'. Tilde expansion and environment variable substitution
  3118. are also made."
  3119. :group 'org-agenda
  3120. :type '(choice
  3121. (repeat :tag "List of files and directories" file)
  3122. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  3123. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  3124. "Regular expression to match files for `org-agenda-files'.
  3125. If any element in the list in that variable contains a directory instead
  3126. of a normal file, all files in that directory that are matched by this
  3127. regular expression will be included."
  3128. :group 'org-agenda
  3129. :type 'regexp)
  3130. (defcustom org-agenda-text-search-extra-files nil
  3131. "List of extra files to be searched by text search commands.
  3132. These files will be searched in addition to the agenda files by the
  3133. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  3134. Note that these files will only be searched for text search commands,
  3135. not for the other agenda views like todo lists, tag searches or the weekly
  3136. agenda. This variable is intended to list notes and possibly archive files
  3137. that should also be searched by these two commands.
  3138. In fact, if the first element in the list is the symbol `agenda-archives',
  3139. then all archive files of all agenda files will be added to the search
  3140. scope."
  3141. :group 'org-agenda
  3142. :type '(set :greedy t
  3143. (const :tag "Agenda Archives" agenda-archives)
  3144. (repeat :inline t (file))))
  3145. (org-defvaralias 'org-agenda-multi-occur-extra-files
  3146. 'org-agenda-text-search-extra-files)
  3147. (defcustom org-agenda-skip-unavailable-files nil
  3148. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  3149. A nil value means to remove them, after a query, from the list."
  3150. :group 'org-agenda
  3151. :type 'boolean)
  3152. (defcustom org-calendar-to-agenda-key [?c]
  3153. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  3154. The command `org-calendar-goto-agenda' will be bound to this key. The
  3155. default is the character `c' because then `c' can be used to switch back and
  3156. forth between agenda and calendar."
  3157. :group 'org-agenda
  3158. :type 'sexp)
  3159. (defcustom org-calendar-insert-diary-entry-key [?i]
  3160. "The key to be installed in `calendar-mode-map' for adding diary entries.
  3161. This option is irrelevant until `org-agenda-diary-file' has been configured
  3162. to point to an Org-mode file. When that is the case, the command
  3163. `org-agenda-diary-entry' will be bound to the key given here, by default
  3164. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  3165. if you want to continue doing this, you need to change this to a different
  3166. key."
  3167. :group 'org-agenda
  3168. :type 'sexp)
  3169. (defcustom org-agenda-diary-file 'diary-file
  3170. "File to which to add new entries with the `i' key in agenda and calendar.
  3171. When this is the symbol `diary-file', the functionality in the Emacs
  3172. calendar will be used to add entries to the `diary-file'. But when this
  3173. points to a file, `org-agenda-diary-entry' will be used instead."
  3174. :group 'org-agenda
  3175. :type '(choice
  3176. (const :tag "The standard Emacs diary file" diary-file)
  3177. (file :tag "Special Org file diary entries")))
  3178. (eval-after-load "calendar"
  3179. '(progn
  3180. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  3181. 'org-calendar-goto-agenda)
  3182. (add-hook 'calendar-mode-hook
  3183. (lambda ()
  3184. (unless (eq org-agenda-diary-file 'diary-file)
  3185. (define-key calendar-mode-map
  3186. org-calendar-insert-diary-entry-key
  3187. 'org-agenda-diary-entry))))))
  3188. (defgroup org-latex nil
  3189. "Options for embedding LaTeX code into Org-mode."
  3190. :tag "Org LaTeX"
  3191. :group 'org)
  3192. (defcustom org-format-latex-options
  3193. '(:foreground default :background default :scale 1.0
  3194. :html-foreground "Black" :html-background "Transparent"
  3195. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  3196. "Options for creating images from LaTeX fragments.
  3197. This is a property list with the following properties:
  3198. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  3199. `default' means use the foreground of the default face.
  3200. `auto' means use the foreground from the text face.
  3201. :background the background color, or \"Transparent\".
  3202. `default' means use the background of the default face.
  3203. `auto' means use the background from the text face.
  3204. :scale a scaling factor for the size of the images, to get more pixels
  3205. :html-foreground, :html-background, :html-scale
  3206. the same numbers for HTML export.
  3207. :matchers a list indicating which matchers should be used to
  3208. find LaTeX fragments. Valid members of this list are:
  3209. \"begin\" find environments
  3210. \"$1\" find single characters surrounded by $.$
  3211. \"$\" find math expressions surrounded by $...$
  3212. \"$$\" find math expressions surrounded by $$....$$
  3213. \"\\(\" find math expressions surrounded by \\(...\\)
  3214. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  3215. :group 'org-latex
  3216. :type 'plist)
  3217. (defcustom org-format-latex-signal-error t
  3218. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  3219. When nil, just push out a message."
  3220. :group 'org-latex
  3221. :version "24.1"
  3222. :type 'boolean)
  3223. (defcustom org-latex-to-mathml-jar-file nil
  3224. "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
  3225. Use this to specify additional executable file say a jar file.
  3226. When using MathToWeb as the converter, specify the full-path to
  3227. your mathtoweb.jar file."
  3228. :group 'org-latex
  3229. :version "24.1"
  3230. :type '(choice
  3231. (const :tag "None" nil)
  3232. (file :tag "JAR file" :must-match t)))
  3233. (defcustom org-latex-to-mathml-convert-command nil
  3234. "Command to convert LaTeX fragments to MathML.
  3235. Replace format-specifiers in the command as noted below and use
  3236. `shell-command' to convert LaTeX to MathML.
  3237. %j: Executable file in fully expanded form as specified by
  3238. `org-latex-to-mathml-jar-file'.
  3239. %I: Input LaTeX file in fully expanded form
  3240. %o: Output MathML file
  3241. This command is used by `org-create-math-formula'.
  3242. When using MathToWeb as the converter, set this to
  3243. \"java -jar %j -unicode -force -df %o %I\"."
  3244. :group 'org-latex
  3245. :version "24.1"
  3246. :type '(choice
  3247. (const :tag "None" nil)
  3248. (string :tag "\nShell command")))
  3249. (defcustom org-latex-create-formula-image-program 'dvipng
  3250. "Program to convert LaTeX fragments with.
  3251. dvipng Process the LaTeX fragments to dvi file, then convert
  3252. dvi files to png files using dvipng.
  3253. This will also include processing of non-math environments.
  3254. imagemagick Convert the LaTeX fragments to pdf files and use imagemagick
  3255. to convert pdf files to png files"
  3256. :group 'org-latex
  3257. :version "24.1"
  3258. :type '(choice
  3259. (const :tag "dvipng" dvipng)
  3260. (const :tag "imagemagick" imagemagick)))
  3261. (defcustom org-latex-preview-ltxpng-directory "ltxpng/"
  3262. "Path to store latex preview images.
  3263. A relative path here creates many directories relative to the
  3264. processed org files paths. An absolute path puts all preview
  3265. images at the same place."
  3266. :group 'org-latex
  3267. :version "24.3"
  3268. :type 'string)
  3269. (defun org-format-latex-mathml-available-p ()
  3270. "Return t if `org-latex-to-mathml-convert-command' is usable."
  3271. (save-match-data
  3272. (when (and (boundp 'org-latex-to-mathml-convert-command)
  3273. org-latex-to-mathml-convert-command)
  3274. (let ((executable (car (split-string
  3275. org-latex-to-mathml-convert-command))))
  3276. (when (executable-find executable)
  3277. (if (string-match
  3278. "%j" org-latex-to-mathml-convert-command)
  3279. (file-readable-p org-latex-to-mathml-jar-file)
  3280. t))))))
  3281. (defcustom org-format-latex-header "\\documentclass{article}
  3282. \\usepackage[usenames]{color}
  3283. \\usepackage{amsmath}
  3284. \\usepackage[mathscr]{eucal}
  3285. \\pagestyle{empty} % do not remove
  3286. \[PACKAGES]
  3287. \[DEFAULT-PACKAGES]
  3288. % The settings below are copied from fullpage.sty
  3289. \\setlength{\\textwidth}{\\paperwidth}
  3290. \\addtolength{\\textwidth}{-3cm}
  3291. \\setlength{\\oddsidemargin}{1.5cm}
  3292. \\addtolength{\\oddsidemargin}{-2.54cm}
  3293. \\setlength{\\evensidemargin}{\\oddsidemargin}
  3294. \\setlength{\\textheight}{\\paperheight}
  3295. \\addtolength{\\textheight}{-\\headheight}
  3296. \\addtolength{\\textheight}{-\\headsep}
  3297. \\addtolength{\\textheight}{-\\footskip}
  3298. \\addtolength{\\textheight}{-3cm}
  3299. \\setlength{\\topmargin}{1.5cm}
  3300. \\addtolength{\\topmargin}{-2.54cm}"
  3301. "The document header used for processing LaTeX fragments.
  3302. It is imperative that this header make sure that no page number
  3303. appears on the page. The package defined in the variables
  3304. `org-latex-default-packages-alist' and `org-latex-packages-alist'
  3305. will either replace the placeholder \"[PACKAGES]\" in this
  3306. header, or they will be appended."
  3307. :group 'org-latex
  3308. :type 'string)
  3309. (defun org-set-packages-alist (var val)
  3310. "Set the packages alist and make sure it has 3 elements per entry."
  3311. (set var (mapcar (lambda (x)
  3312. (if (and (consp x) (= (length x) 2))
  3313. (list (car x) (nth 1 x) t)
  3314. x))
  3315. val)))
  3316. (defun org-get-packages-alist (var)
  3317. "Get the packages alist and make sure it has 3 elements per entry."
  3318. (mapcar (lambda (x)
  3319. (if (and (consp x) (= (length x) 2))
  3320. (list (car x) (nth 1 x) t)
  3321. x))
  3322. (default-value var)))
  3323. (defcustom org-latex-default-packages-alist
  3324. '(("AUTO" "inputenc" t)
  3325. ("T1" "fontenc" t)
  3326. ("" "fixltx2e" nil)
  3327. ("" "graphicx" t)
  3328. ("" "longtable" nil)
  3329. ("" "float" nil)
  3330. ("" "wrapfig" nil)
  3331. ("" "soul" t)
  3332. ("" "textcomp" t)
  3333. ("" "marvosym" t)
  3334. ("" "wasysym" t)
  3335. ("" "latexsym" t)
  3336. ("" "amssymb" t)
  3337. ("" "amstext" nil)
  3338. ("" "hyperref" nil)
  3339. "\\tolerance=1000")
  3340. "Alist of default packages to be inserted in the header.
  3341. Change this only if one of the packages here causes an
  3342. incompatibility with another package you are using.
  3343. The packages in this list are needed by one part or another of
  3344. Org mode to function properly:
  3345. - inputenc, fontenc: for basic font and character selection
  3346. - amstext: for subscript and superscript
  3347. - textcomp, marvosymb, wasysym, latexsym, amssym: for various
  3348. symbols used for interpreting the entities in `org-entities'.
  3349. You can skip some of these packages if you don't use any of the
  3350. symbols in it.
  3351. - soul: for underline and strike-through
  3352. - graphicx: for including images
  3353. - float, wrapfig: for figure placement
  3354. - longtable: for long tables
  3355. - hyperref: for cross references
  3356. Therefore you should not modify this variable unless you know
  3357. what you are doing. The one reason to change it anyway is that
  3358. you might be loading some other package that conflicts with one
  3359. of the default packages. Each cell is of the format
  3360. \( \"options\" \"package\" snippet-flag). If SNIPPET-FLAG is t,
  3361. the package also needs to be included when compiling LaTeX
  3362. snippets into images for inclusion into non-LaTeX output."
  3363. :group 'org-latex
  3364. :group 'org-export-latex
  3365. :set 'org-set-packages-alist
  3366. :get 'org-get-packages-alist
  3367. :version "24.1"
  3368. :type '(repeat
  3369. (choice
  3370. (list :tag "options/package pair"
  3371. (string :tag "options")
  3372. (string :tag "package")
  3373. (boolean :tag "Snippet"))
  3374. (string :tag "A line of LaTeX"))))
  3375. (defcustom org-latex-packages-alist nil
  3376. "Alist of packages to be inserted in every LaTeX header.
  3377. These will be inserted after `org-latex-default-packages-alist'.
  3378. Each cell is of the format:
  3379. \(\"options\" \"package\" snippet-flag)
  3380. SNIPPET-FLAG, when t, indicates that this package is also needed
  3381. when turning LaTeX snippets into images for inclusion into
  3382. non-LaTeX output.
  3383. Make sure that you only list packages here which:
  3384. - you want in every file
  3385. - do not conflict with the setup in `org-format-latex-header'.
  3386. - do not conflict with the default packages in
  3387. `org-latex-default-packages-alist'."
  3388. :group 'org-latex
  3389. :group 'org-export-latex
  3390. :set 'org-set-packages-alist
  3391. :get 'org-get-packages-alist
  3392. :type '(repeat
  3393. (choice
  3394. (list :tag "options/package pair"
  3395. (string :tag "options")
  3396. (string :tag "package")
  3397. (boolean :tag "Snippet"))
  3398. (string :tag "A line of LaTeX"))))
  3399. (defgroup org-appearance nil
  3400. "Settings for Org-mode appearance."
  3401. :tag "Org Appearance"
  3402. :group 'org)
  3403. (defcustom org-level-color-stars-only nil
  3404. "Non-nil means fontify only the stars in each headline.
  3405. When nil, the entire headline is fontified.
  3406. Changing it requires restart of `font-lock-mode' to become effective
  3407. also in regions already fontified."
  3408. :group 'org-appearance
  3409. :type 'boolean)
  3410. (defcustom org-hide-leading-stars nil
  3411. "Non-nil means hide the first N-1 stars in a headline.
  3412. This works by using the face `org-hide' for these stars. This
  3413. face is white for a light background, and black for a dark
  3414. background. You may have to customize the face `org-hide' to
  3415. make this work.
  3416. Changing it requires restart of `font-lock-mode' to become effective
  3417. also in regions already fontified.
  3418. You may also set this on a per-file basis by adding one of the following
  3419. lines to the buffer:
  3420. #+STARTUP: hidestars
  3421. #+STARTUP: showstars"
  3422. :group 'org-appearance
  3423. :type 'boolean)
  3424. (defcustom org-hidden-keywords nil
  3425. "List of symbols corresponding to keywords to be hidden the org buffer.
  3426. For example, a value '(title) for this list will make the document's title
  3427. appear in the buffer without the initial #+TITLE: keyword."
  3428. :group 'org-appearance
  3429. :version "24.1"
  3430. :type '(set (const :tag "#+AUTHOR" author)
  3431. (const :tag "#+DATE" date)
  3432. (const :tag "#+EMAIL" email)
  3433. (const :tag "#+TITLE" title)))
  3434. (defcustom org-custom-properties nil
  3435. "List of properties (as strings) with a special meaning.
  3436. The default use of these custom properties is to let the user
  3437. hide them with `org-toggle-custom-properties-visibility'."
  3438. :group 'org-properties
  3439. :group 'org-appearance
  3440. :version "24.3"
  3441. :type '(repeat (string :tag "Property Name")))
  3442. (defcustom org-fontify-done-headline nil
  3443. "Non-nil means change the face of a headline if it is marked DONE.
  3444. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3445. When this is non-nil, the headline after the keyword is set to the
  3446. `org-headline-done' as an additional indication."
  3447. :group 'org-appearance
  3448. :type 'boolean)
  3449. (defcustom org-fontify-emphasized-text t
  3450. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3451. Changing this variable requires a restart of Emacs to take effect."
  3452. :group 'org-appearance
  3453. :type 'boolean)
  3454. (defcustom org-fontify-whole-heading-line nil
  3455. "Non-nil means fontify the whole line for headings.
  3456. This is useful when setting a background color for the
  3457. org-level-* faces."
  3458. :group 'org-appearance
  3459. :type 'boolean)
  3460. (defcustom org-highlight-latex-and-related nil
  3461. "Non-nil means highlight LaTeX related syntax in the buffer.
  3462. When non nil, the value should be a list containing any of the
  3463. following symbols:
  3464. `latex' Highlight LaTeX snippets and environments.
  3465. `script' Highlight subscript and superscript.
  3466. `entities' Highlight entities."
  3467. :group 'org-appearance
  3468. :version "24.4"
  3469. :package-version '(Org . "8.0")
  3470. :type '(choice
  3471. (const :tag "No highlighting" nil)
  3472. (set :greedy t :tag "Highlight"
  3473. (const :tag "LaTeX snippets and environments" latex)
  3474. (const :tag "Subscript and superscript" script)
  3475. (const :tag "Entities" entities))))
  3476. (defcustom org-hide-emphasis-markers nil
  3477. "Non-nil mean font-lock should hide the emphasis marker characters."
  3478. :group 'org-appearance
  3479. :type 'boolean)
  3480. (defcustom org-pretty-entities nil
  3481. "Non-nil means show entities as UTF8 characters.
  3482. When nil, the \\name form remains in the buffer."
  3483. :group 'org-appearance
  3484. :version "24.1"
  3485. :type 'boolean)
  3486. (defcustom org-pretty-entities-include-sub-superscripts t
  3487. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  3488. :group 'org-appearance
  3489. :version "24.1"
  3490. :type 'boolean)
  3491. (defvar org-emph-re nil
  3492. "Regular expression for matching emphasis.
  3493. After a match, the match groups contain these elements:
  3494. 0 The match of the full regular expression, including the characters
  3495. before and after the proper match
  3496. 1 The character before the proper match, or empty at beginning of line
  3497. 2 The proper match, including the leading and trailing markers
  3498. 3 The leading marker like * or /, indicating the type of highlighting
  3499. 4 The text between the emphasis markers, not including the markers
  3500. 5 The character after the match, empty at the end of a line")
  3501. (defvar org-verbatim-re nil
  3502. "Regular expression for matching verbatim text.")
  3503. (defvar org-emphasis-regexp-components) ; defined just below
  3504. (defvar org-emphasis-alist) ; defined just below
  3505. (defun org-set-emph-re (var val)
  3506. "Set variable and compute the emphasis regular expression."
  3507. (set var val)
  3508. (when (and (boundp 'org-emphasis-alist)
  3509. (boundp 'org-emphasis-regexp-components)
  3510. org-emphasis-alist org-emphasis-regexp-components)
  3511. (let* ((e org-emphasis-regexp-components)
  3512. (pre (car e))
  3513. (post (nth 1 e))
  3514. (border (nth 2 e))
  3515. (body (nth 3 e))
  3516. (nl (nth 4 e))
  3517. (body1 (concat body "*?"))
  3518. (markers (mapconcat 'car org-emphasis-alist ""))
  3519. (vmarkers (mapconcat
  3520. (lambda (x) (if (eq (nth 2 x) 'verbatim) (car x) ""))
  3521. org-emphasis-alist "")))
  3522. ;; make sure special characters appear at the right position in the class
  3523. (if (string-match "\\^" markers)
  3524. (setq markers (concat (replace-match "" t t markers) "^")))
  3525. (if (string-match "-" markers)
  3526. (setq markers (concat (replace-match "" t t markers) "-")))
  3527. (if (string-match "\\^" vmarkers)
  3528. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3529. (if (string-match "-" vmarkers)
  3530. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3531. (if (> nl 0)
  3532. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3533. (int-to-string nl) "\\}")))
  3534. ;; Make the regexp
  3535. (setq org-emph-re
  3536. (concat "\\([" pre "]\\|^\\)"
  3537. "\\("
  3538. "\\([" markers "]\\)"
  3539. "\\("
  3540. "[^" border "]\\|"
  3541. "[^" border "]"
  3542. body1
  3543. "[^" border "]"
  3544. "\\)"
  3545. "\\3\\)"
  3546. "\\([" post "]\\|$\\)"))
  3547. (setq org-verbatim-re
  3548. (concat "\\([" pre "]\\|^\\)"
  3549. "\\("
  3550. "\\([" vmarkers "]\\)"
  3551. "\\("
  3552. "[^" border "]\\|"
  3553. "[^" border "]"
  3554. body1
  3555. "[^" border "]"
  3556. "\\)"
  3557. "\\3\\)"
  3558. "\\([" post "]\\|$\\)")))))
  3559. ;; This used to be a defcustom (Org <8.0) but allowing the users to
  3560. ;; set this option proved cumbersome. See this message/thread:
  3561. ;; http://article.gmane.org/gmane.emacs.orgmode/68681
  3562. (defvar org-emphasis-regexp-components
  3563. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  3564. "Components used to build the regular expression for emphasis.
  3565. This is a list with five entries. Terminology: In an emphasis string
  3566. like \" *strong word* \", we call the initial space PREMATCH, the final
  3567. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3568. and \"trong wor\" is the body. The different components in this variable
  3569. specify what is allowed/forbidden in each part:
  3570. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3571. post Chars allowed as postmatch. End of line will be allowed too.
  3572. border The chars *forbidden* as border characters.
  3573. body-regexp A regexp like \".\" to match a body character. Don't use
  3574. non-shy groups here, and don't allow newline here.
  3575. newline The maximum number of newlines allowed in an emphasis exp.
  3576. You need to reload Org or to restart Emacs after customizing this.")
  3577. (defcustom org-emphasis-alist
  3578. `(("*" bold)
  3579. ("/" italic)
  3580. ("_" underline)
  3581. ("=" org-code verbatim)
  3582. ("~" org-verbatim verbatim)
  3583. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))))
  3584. "Alist of characters and faces to emphasize text.
  3585. Text starting and ending with a special character will be emphasized,
  3586. for example *bold*, _underlined_ and /italic/. This variable sets the
  3587. marker characters and the face to be used by font-lock for highlighting
  3588. in Org-mode Emacs buffers.
  3589. You need to reload Org or to restart Emacs after customizing this."
  3590. :group 'org-appearance
  3591. :set 'org-set-emph-re
  3592. :version "24.4"
  3593. :package-version '(Org . "8.0")
  3594. :type '(repeat
  3595. (list
  3596. (string :tag "Marker character")
  3597. (choice
  3598. (face :tag "Font-lock-face")
  3599. (plist :tag "Face property list"))
  3600. (option (const verbatim)))))
  3601. (defvar org-protecting-blocks
  3602. '("src" "example" "latex" "ascii" "html" "ditaa" "dot" "r" "R")
  3603. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3604. This is needed for font-lock setup.")
  3605. ;;; Miscellaneous options
  3606. (defgroup org-completion nil
  3607. "Completion in Org-mode."
  3608. :tag "Org Completion"
  3609. :group 'org)
  3610. (defcustom org-completion-use-ido nil
  3611. "Non-nil means use ido completion wherever possible.
  3612. Note that `ido-mode' must be active for this variable to be relevant.
  3613. If you decide to turn this variable on, you might well want to turn off
  3614. `org-outline-path-complete-in-steps'.
  3615. See also `org-completion-use-iswitchb'."
  3616. :group 'org-completion
  3617. :type 'boolean)
  3618. (defcustom org-completion-use-iswitchb nil
  3619. "Non-nil means use iswitchb completion wherever possible.
  3620. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3621. If you decide to turn this variable on, you might well want to turn off
  3622. `org-outline-path-complete-in-steps'.
  3623. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3624. :group 'org-completion
  3625. :type 'boolean)
  3626. (defcustom org-completion-fallback-command 'hippie-expand
  3627. "The expansion command called by \\[pcomplete] in normal context.
  3628. Normal means, no org-mode-specific context."
  3629. :group 'org-completion
  3630. :type 'function)
  3631. ;;; Functions and variables from their packages
  3632. ;; Declared here to avoid compiler warnings
  3633. ;; XEmacs only
  3634. (defvar outline-mode-menu-heading)
  3635. (defvar outline-mode-menu-show)
  3636. (defvar outline-mode-menu-hide)
  3637. (defvar zmacs-regions) ; XEmacs regions
  3638. ;; Emacs only
  3639. (defvar mark-active)
  3640. ;; Various packages
  3641. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  3642. (declare-function calendar-forward-day "cal-move" (arg))
  3643. (declare-function calendar-goto-date "cal-move" (date))
  3644. (declare-function calendar-goto-today "cal-move" ())
  3645. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3646. (defvar calc-embedded-close-formula)
  3647. (defvar calc-embedded-open-formula)
  3648. (declare-function cdlatex-tab "ext:cdlatex" ())
  3649. (declare-function cdlatex-compute-tables "ext:cdlatex" ())
  3650. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3651. (defvar font-lock-unfontify-region-function)
  3652. (declare-function iswitchb-read-buffer "iswitchb"
  3653. (prompt &optional default require-match start matches-set))
  3654. (defvar iswitchb-temp-buflist)
  3655. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3656. (defvar org-agenda-tags-todo-honor-ignore-options)
  3657. (declare-function org-agenda-skip "org-agenda" ())
  3658. (declare-function
  3659. org-agenda-format-item "org-agenda"
  3660. (extra txt &optional level category tags dotime noprefix remove-re habitp))
  3661. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3662. (declare-function org-agenda-change-all-lines "org-agenda"
  3663. (newhead hdmarker &optional fixface just-this))
  3664. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3665. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3666. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3667. (beg end))
  3668. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3669. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3670. "org-agenda" (&optional end))
  3671. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3672. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3673. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3674. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3675. (declare-function org-indent-mode "org-indent" (&optional arg))
  3676. (declare-function parse-time-string "parse-time" (string))
  3677. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3678. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3679. (defvar remember-data-file)
  3680. (defvar texmathp-why)
  3681. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3682. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3683. (defvar org-latex-regexps)
  3684. ;;; Autoload and prepare some org modules
  3685. ;; Some table stuff that needs to be defined here, because it is used
  3686. ;; by the functions setting up org-mode or checking for table context.
  3687. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  3688. "Detect an org-type or table-type table.")
  3689. (defconst org-table-line-regexp "^[ \t]*|"
  3690. "Detect an org-type table line.")
  3691. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  3692. "Detect an org-type table line.")
  3693. (defconst org-table-hline-regexp "^[ \t]*|-"
  3694. "Detect an org-type table hline.")
  3695. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  3696. "Detect a table-type table hline.")
  3697. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  3698. "Detect the first line outside a table when searching from within it.
  3699. This works for both table types.")
  3700. ;; Autoload the functions in org-table.el that are needed by functions here.
  3701. (eval-and-compile
  3702. (org-autoload "org-table"
  3703. '(org-table-begin org-table-blank-field org-table-end)))
  3704. (defconst org-TBLFM-regexp "^[ \t]*#\\+TBLFM: "
  3705. "Detect a #+TBLFM line.")
  3706. ;;;###autoload
  3707. (defun turn-on-orgtbl ()
  3708. "Unconditionally turn on `orgtbl-mode'."
  3709. (require 'org-table)
  3710. (orgtbl-mode 1))
  3711. (defun org-at-table-p (&optional table-type)
  3712. "Return t if the cursor is inside an org-type table.
  3713. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  3714. (if org-enable-table-editor
  3715. (save-excursion
  3716. (beginning-of-line 1)
  3717. (looking-at (if table-type org-table-any-line-regexp
  3718. org-table-line-regexp)))
  3719. nil))
  3720. (defsubst org-table-p () (org-at-table-p))
  3721. (defun org-at-table.el-p ()
  3722. "Return t if and only if we are at a table.el table."
  3723. (and (org-at-table-p 'any)
  3724. (save-excursion
  3725. (goto-char (org-table-begin 'any))
  3726. (looking-at org-table1-hline-regexp))))
  3727. (defun org-table-recognize-table.el ()
  3728. "If there is a table.el table nearby, recognize it and move into it."
  3729. (if org-table-tab-recognizes-table.el
  3730. (if (org-at-table.el-p)
  3731. (progn
  3732. (beginning-of-line 1)
  3733. (if (looking-at org-table-dataline-regexp)
  3734. nil
  3735. (if (looking-at org-table1-hline-regexp)
  3736. (progn
  3737. (beginning-of-line 2)
  3738. (if (looking-at org-table-any-border-regexp)
  3739. (beginning-of-line -1)))))
  3740. (if (re-search-forward "|" (org-table-end t) t)
  3741. (progn
  3742. (require 'table)
  3743. (if (table--at-cell-p (point))
  3744. t
  3745. (message "recognizing table.el table...")
  3746. (table-recognize-table)
  3747. (message "recognizing table.el table...done")))
  3748. (error "This should not happen"))
  3749. t)
  3750. nil)
  3751. nil))
  3752. (defun org-at-table-hline-p ()
  3753. "Return t if the cursor is inside a hline in a table."
  3754. (if org-enable-table-editor
  3755. (save-excursion
  3756. (beginning-of-line 1)
  3757. (looking-at org-table-hline-regexp))
  3758. nil))
  3759. (defun org-table-map-tables (function &optional quietly)
  3760. "Apply FUNCTION to the start of all tables in the buffer."
  3761. (save-excursion
  3762. (save-restriction
  3763. (widen)
  3764. (goto-char (point-min))
  3765. (while (re-search-forward org-table-any-line-regexp nil t)
  3766. (unless quietly
  3767. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
  3768. (beginning-of-line 1)
  3769. (when (and (looking-at org-table-line-regexp)
  3770. ;; Exclude tables in src/example/verbatim/clocktable blocks
  3771. (not (org-in-block-p '("src" "example" "verbatim" "clocktable"))))
  3772. (save-excursion (funcall function))
  3773. (or (looking-at org-table-line-regexp)
  3774. (forward-char 1)))
  3775. (re-search-forward org-table-any-border-regexp nil 1))))
  3776. (unless quietly (message "Mapping tables: done")))
  3777. ;; Declare and autoload functions from ox.el and al.
  3778. (declare-function org-export-get-environment "ox"
  3779. (&optional backend subtreep ext-plist))
  3780. (declare-function org-latex-guess-inputenc "ox-latex" (header))
  3781. ;; Declare and autoload functions from org-agenda.el
  3782. (eval-and-compile
  3783. (org-autoload "org-agenda"
  3784. '(org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  3785. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock" (beg end))
  3786. (declare-function org-clock-update-mode-line "org-clock" ())
  3787. (declare-function org-resolve-clocks "org-clock"
  3788. (&optional also-non-dangling-p prompt last-valid))
  3789. (defun org-at-TBLFM-p (&optional pos)
  3790. "Return t when point (or POS) is in #+TBLFM line."
  3791. (save-excursion
  3792. (let ((pos pos)))
  3793. (goto-char (or pos (point)))
  3794. (beginning-of-line 1)
  3795. (looking-at org-TBLFM-regexp)))
  3796. (defvar org-clock-start-time)
  3797. (defvar org-clock-marker (make-marker)
  3798. "Marker recording the last clock-in.")
  3799. (defvar org-clock-hd-marker (make-marker)
  3800. "Marker recording the last clock-in, but the headline position.")
  3801. (defvar org-clock-heading ""
  3802. "The heading of the current clock entry.")
  3803. (defun org-clock-is-active ()
  3804. "Return non-nil if clock is currently running.
  3805. The return value is actually the clock marker."
  3806. (marker-buffer org-clock-marker))
  3807. (eval-and-compile
  3808. (org-autoload "org-clock" '(org-clock-remove-overlays
  3809. org-clock-update-time-maybe
  3810. org-clocktable-shift)))
  3811. (defun org-check-running-clock ()
  3812. "Check if the current buffer contains the running clock.
  3813. If yes, offer to stop it and to save the buffer with the changes."
  3814. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3815. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3816. (buffer-name))))
  3817. (org-clock-out)
  3818. (when (y-or-n-p "Save changed buffer?")
  3819. (save-buffer))))
  3820. (defun org-clocktable-try-shift (dir n)
  3821. "Check if this line starts a clock table, if yes, shift the time block."
  3822. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  3823. (org-clocktable-shift dir n)))
  3824. ;;;###autoload
  3825. (defun org-clock-persistence-insinuate ()
  3826. "Set up hooks for clock persistence."
  3827. (require 'org-clock)
  3828. (add-hook 'org-mode-hook 'org-clock-load)
  3829. (add-hook 'kill-emacs-hook 'org-clock-save))
  3830. ;; Define the variable already here, to make sure we have it.
  3831. (defvar org-indent-mode nil
  3832. "Non-nil if Org-Indent mode is enabled.
  3833. Use the command `org-indent-mode' to change this variable.")
  3834. ;; Autoload archiving code
  3835. ;; The stuff that is needed for cycling and tags has to be defined here.
  3836. (defgroup org-archive nil
  3837. "Options concerning archiving in Org-mode."
  3838. :tag "Org Archive"
  3839. :group 'org-structure)
  3840. (defcustom org-archive-location "%s_archive::"
  3841. "The location where subtrees should be archived.
  3842. The value of this variable is a string, consisting of two parts,
  3843. separated by a double-colon. The first part is a filename and
  3844. the second part is a headline.
  3845. When the filename is omitted, archiving happens in the same file.
  3846. %s in the filename will be replaced by the current file
  3847. name (without the directory part). Archiving to a different file
  3848. is useful to keep archived entries from contributing to the
  3849. Org-mode Agenda.
  3850. The archived entries will be filed as subtrees of the specified
  3851. headline. When the headline is omitted, the subtrees are simply
  3852. filed away at the end of the file, as top-level entries. Also in
  3853. the heading you can use %s to represent the file name, this can be
  3854. useful when using the same archive for a number of different files.
  3855. Here are a few examples:
  3856. \"%s_archive::\"
  3857. If the current file is Projects.org, archive in file
  3858. Projects.org_archive, as top-level trees. This is the default.
  3859. \"::* Archived Tasks\"
  3860. Archive in the current file, under the top-level headline
  3861. \"* Archived Tasks\".
  3862. \"~/org/archive.org::\"
  3863. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3864. \"~/org/archive.org::* From %s\"
  3865. Archive in file ~/org/archive.org (absolute path), under headlines
  3866. \"From FILENAME\" where file name is the current file name.
  3867. \"~/org/datetree.org::datetree/* Finished Tasks\"
  3868. The \"datetree/\" string is special, signifying to archive
  3869. items to the datetree. Items are placed in either the CLOSED
  3870. date of the item, or the current date if there is no CLOSED date.
  3871. The heading will be a subentry to the current date. There doesn't
  3872. need to be a heading, but there always needs to be a slash after
  3873. datetree. For example, to store archived items directly in the
  3874. datetree, use \"~/org/datetree.org::datetree/\".
  3875. \"basement::** Finished Tasks\"
  3876. Archive in file ./basement (relative path), as level 3 trees
  3877. below the level 2 heading \"** Finished Tasks\".
  3878. You may set this option on a per-file basis by adding to the buffer a
  3879. line like
  3880. #+ARCHIVE: basement::** Finished Tasks
  3881. You may also define it locally for a subtree by setting an ARCHIVE property
  3882. in the entry. If such a property is found in an entry, or anywhere up
  3883. the hierarchy, it will be used."
  3884. :group 'org-archive
  3885. :type 'string)
  3886. (defcustom org-archive-tag "ARCHIVE"
  3887. "The tag that marks a subtree as archived.
  3888. An archived subtree does not open during visibility cycling, and does
  3889. not contribute to the agenda listings.
  3890. After changing this, font-lock must be restarted in the relevant buffers to
  3891. get the proper fontification."
  3892. :group 'org-archive
  3893. :group 'org-keywords
  3894. :type 'string)
  3895. (defcustom org-agenda-skip-archived-trees t
  3896. "Non-nil means the agenda will skip any items located in archived trees.
  3897. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3898. variable is no longer recommended, you should leave it at the value t.
  3899. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3900. :group 'org-archive
  3901. :group 'org-agenda-skip
  3902. :type 'boolean)
  3903. (defcustom org-columns-skip-archived-trees t
  3904. "Non-nil means ignore archived trees when creating column view."
  3905. :group 'org-archive
  3906. :group 'org-properties
  3907. :type 'boolean)
  3908. (defcustom org-cycle-open-archived-trees nil
  3909. "Non-nil means `org-cycle' will open archived trees.
  3910. An archived tree is a tree marked with the tag ARCHIVE.
  3911. When nil, archived trees will stay folded. You can still open them with
  3912. normal outline commands like `show-all', but not with the cycling commands."
  3913. :group 'org-archive
  3914. :group 'org-cycle
  3915. :type 'boolean)
  3916. (defcustom org-sparse-tree-open-archived-trees nil
  3917. "Non-nil means sparse tree construction shows matches in archived trees.
  3918. When nil, matches in these trees are highlighted, but the trees are kept in
  3919. collapsed state."
  3920. :group 'org-archive
  3921. :group 'org-sparse-trees
  3922. :type 'boolean)
  3923. (defcustom org-sparse-tree-default-date-type 'scheduled-or-deadline
  3924. "The default date type when building a sparse tree.
  3925. When this is nil, a date is a scheduled or a deadline timestamp.
  3926. Otherwise, these types are allowed:
  3927. all: all timestamps
  3928. active: only active timestamps (<...>)
  3929. inactive: only inactive timestamps (<...)
  3930. scheduled: only scheduled timestamps
  3931. deadline: only deadline timestamps"
  3932. :type '(choice (const :tag "Scheduled or deadline" scheduled-or-deadline)
  3933. (const :tag "All timestamps" all)
  3934. (const :tag "Only active timestamps" active)
  3935. (const :tag "Only inactive timestamps" inactive)
  3936. (const :tag "Only scheduled timestamps" scheduled)
  3937. (const :tag "Only deadline timestamps" deadline)
  3938. (const :tag "Only closed timestamps" closed))
  3939. :version "24.3"
  3940. :group 'org-sparse-trees)
  3941. (defun org-cycle-hide-archived-subtrees (state)
  3942. "Re-hide all archived subtrees after a visibility state change."
  3943. (when (and (not org-cycle-open-archived-trees)
  3944. (not (memq state '(overview folded))))
  3945. (save-excursion
  3946. (let* ((globalp (memq state '(contents all)))
  3947. (beg (if globalp (point-min) (point)))
  3948. (end (if globalp (point-max) (org-end-of-subtree t))))
  3949. (org-hide-archived-subtrees beg end)
  3950. (goto-char beg)
  3951. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3952. (message "%s" (substitute-command-keys
  3953. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3954. (defun org-force-cycle-archived ()
  3955. "Cycle subtree even if it is archived."
  3956. (interactive)
  3957. (setq this-command 'org-cycle)
  3958. (let ((org-cycle-open-archived-trees t))
  3959. (call-interactively 'org-cycle)))
  3960. (defun org-hide-archived-subtrees (beg end)
  3961. "Re-hide all archived subtrees after a visibility state change."
  3962. (save-excursion
  3963. (let* ((re (concat ":" org-archive-tag ":")))
  3964. (goto-char beg)
  3965. (while (re-search-forward re end t)
  3966. (when (org-at-heading-p)
  3967. (org-flag-subtree t)
  3968. (org-end-of-subtree t))))))
  3969. (declare-function outline-end-of-heading "outline" ())
  3970. (declare-function outline-flag-region "outline" (from to flag))
  3971. (defun org-flag-subtree (flag)
  3972. (save-excursion
  3973. (org-back-to-heading t)
  3974. (outline-end-of-heading)
  3975. (outline-flag-region (point)
  3976. (progn (org-end-of-subtree t) (point))
  3977. flag)))
  3978. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3979. (eval-and-compile
  3980. (org-autoload "org-archive"
  3981. '(org-add-archive-files)))
  3982. ;; Autoload Column View Code
  3983. (declare-function org-columns-number-to-string "org-colview" (n fmt &optional printf))
  3984. (declare-function org-columns-get-format-and-top-level "org-colview" ())
  3985. (declare-function org-columns-compute "org-colview" (property))
  3986. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  3987. '(org-columns-number-to-string
  3988. org-columns-get-format-and-top-level
  3989. org-columns-compute
  3990. org-columns-remove-overlays))
  3991. ;; Autoload ID code
  3992. (declare-function org-id-store-link "org-id")
  3993. (declare-function org-id-locations-load "org-id")
  3994. (declare-function org-id-locations-save "org-id")
  3995. (defvar org-id-track-globally)
  3996. (org-autoload "org-id"
  3997. '(org-id-new
  3998. org-id-copy
  3999. org-id-get-with-outline-path-completion
  4000. org-id-get-with-outline-drilling))
  4001. ;;; Variables for pre-computed regular expressions, all buffer local
  4002. (defvar org-drawer-regexp "^[ \t]*:PROPERTIES:[ \t]*$"
  4003. "Matches first line of a hidden block.")
  4004. (make-variable-buffer-local 'org-drawer-regexp)
  4005. (defvar org-todo-regexp nil
  4006. "Matches any of the TODO state keywords.")
  4007. (make-variable-buffer-local 'org-todo-regexp)
  4008. (defvar org-not-done-regexp nil
  4009. "Matches any of the TODO state keywords except the last one.")
  4010. (make-variable-buffer-local 'org-not-done-regexp)
  4011. (defvar org-not-done-heading-regexp nil
  4012. "Matches a TODO headline that is not done.")
  4013. (make-variable-buffer-local 'org-not-done-regexp)
  4014. (defvar org-todo-line-regexp nil
  4015. "Matches a headline and puts TODO state into group 2 if present.")
  4016. (make-variable-buffer-local 'org-todo-line-regexp)
  4017. (defvar org-complex-heading-regexp nil
  4018. "Matches a headline and puts everything into groups:
  4019. group 1: the stars
  4020. group 2: The todo keyword, maybe
  4021. group 3: Priority cookie
  4022. group 4: True headline
  4023. group 5: Tags")
  4024. (make-variable-buffer-local 'org-complex-heading-regexp)
  4025. (defvar org-complex-heading-regexp-format nil
  4026. "Printf format to make regexp to match an exact headline.
  4027. This regexp will match the headline of any node which has the
  4028. exact headline text that is put into the format, but may have any
  4029. TODO state, priority and tags.")
  4030. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  4031. (defvar org-todo-line-tags-regexp nil
  4032. "Matches a headline and puts TODO state into group 2 if present.
  4033. Also put tags into group 4 if tags are present.")
  4034. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  4035. (defvar org-ds-keyword-length 12
  4036. "Maximum length of the DEADLINE and SCHEDULED keywords.")
  4037. (make-variable-buffer-local 'org-ds-keyword-length)
  4038. (defvar org-deadline-regexp nil
  4039. "Matches the DEADLINE keyword.")
  4040. (make-variable-buffer-local 'org-deadline-regexp)
  4041. (defvar org-deadline-time-regexp nil
  4042. "Matches the DEADLINE keyword together with a time stamp.")
  4043. (make-variable-buffer-local 'org-deadline-time-regexp)
  4044. (defvar org-deadline-time-hour-regexp nil
  4045. "Matches the DEADLINE keyword together with a time-and-hour stamp.")
  4046. (make-variable-buffer-local 'org-deadline-time-hour-regexp)
  4047. (defvar org-deadline-line-regexp nil
  4048. "Matches the DEADLINE keyword and the rest of the line.")
  4049. (make-variable-buffer-local 'org-deadline-line-regexp)
  4050. (defvar org-scheduled-regexp nil
  4051. "Matches the SCHEDULED keyword.")
  4052. (make-variable-buffer-local 'org-scheduled-regexp)
  4053. (defvar org-scheduled-time-regexp nil
  4054. "Matches the SCHEDULED keyword together with a time stamp.")
  4055. (make-variable-buffer-local 'org-scheduled-time-regexp)
  4056. (defvar org-scheduled-time-hour-regexp nil
  4057. "Matches the SCHEDULED keyword together with a time-and-hour stamp.")
  4058. (make-variable-buffer-local 'org-scheduled-time-hour-regexp)
  4059. (defvar org-closed-time-regexp nil
  4060. "Matches the CLOSED keyword together with a time stamp.")
  4061. (make-variable-buffer-local 'org-closed-time-regexp)
  4062. (defvar org-keyword-time-regexp nil
  4063. "Matches any of the 4 keywords, together with the time stamp.")
  4064. (make-variable-buffer-local 'org-keyword-time-regexp)
  4065. (defvar org-keyword-time-not-clock-regexp nil
  4066. "Matches any of the 3 keywords, together with the time stamp.")
  4067. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  4068. (defvar org-maybe-keyword-time-regexp nil
  4069. "Matches a timestamp, possibly preceded by a keyword.")
  4070. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  4071. (defvar org-all-time-keywords nil
  4072. "List of time keywords.")
  4073. (make-variable-buffer-local 'org-all-time-keywords)
  4074. (defconst org-plain-time-of-day-regexp
  4075. (concat
  4076. "\\(\\<[012]?[0-9]"
  4077. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4078. "\\(--?"
  4079. "\\(\\<[012]?[0-9]"
  4080. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4081. "\\)?")
  4082. "Regular expression to match a plain time or time range.
  4083. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4084. groups carry important information:
  4085. 0 the full match
  4086. 1 the first time, range or not
  4087. 8 the second time, if it is a range.")
  4088. (defconst org-plain-time-extension-regexp
  4089. (concat
  4090. "\\(\\<[012]?[0-9]"
  4091. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4092. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  4093. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  4094. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4095. groups carry important information:
  4096. 0 the full match
  4097. 7 hours of duration
  4098. 9 minutes of duration")
  4099. (defconst org-stamp-time-of-day-regexp
  4100. (concat
  4101. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  4102. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  4103. "\\(--?"
  4104. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  4105. "Regular expression to match a timestamp time or time range.
  4106. After a match, the following groups carry important information:
  4107. 0 the full match
  4108. 1 date plus weekday, for back referencing to make sure both times are on the same day
  4109. 2 the first time, range or not
  4110. 4 the second time, if it is a range.")
  4111. (defconst org-startup-options
  4112. '(("fold" org-startup-folded t)
  4113. ("overview" org-startup-folded t)
  4114. ("nofold" org-startup-folded nil)
  4115. ("showall" org-startup-folded nil)
  4116. ("showeverything" org-startup-folded showeverything)
  4117. ("content" org-startup-folded content)
  4118. ("indent" org-startup-indented t)
  4119. ("noindent" org-startup-indented nil)
  4120. ("hidestars" org-hide-leading-stars t)
  4121. ("showstars" org-hide-leading-stars nil)
  4122. ("odd" org-odd-levels-only t)
  4123. ("oddeven" org-odd-levels-only nil)
  4124. ("align" org-startup-align-all-tables t)
  4125. ("noalign" org-startup-align-all-tables nil)
  4126. ("inlineimages" org-startup-with-inline-images t)
  4127. ("noinlineimages" org-startup-with-inline-images nil)
  4128. ("latexpreview" org-startup-with-latex-preview t)
  4129. ("nolatexpreview" org-startup-with-latex-preview nil)
  4130. ("customtime" org-display-custom-times t)
  4131. ("logdone" org-log-done time)
  4132. ("lognotedone" org-log-done note)
  4133. ("nologdone" org-log-done nil)
  4134. ("lognoteclock-out" org-log-note-clock-out t)
  4135. ("nolognoteclock-out" org-log-note-clock-out nil)
  4136. ("logrepeat" org-log-repeat state)
  4137. ("lognoterepeat" org-log-repeat note)
  4138. ("logdrawer" org-log-into-drawer t)
  4139. ("nologdrawer" org-log-into-drawer nil)
  4140. ("logstatesreversed" org-log-states-order-reversed t)
  4141. ("nologstatesreversed" org-log-states-order-reversed nil)
  4142. ("nologrepeat" org-log-repeat nil)
  4143. ("logreschedule" org-log-reschedule time)
  4144. ("lognotereschedule" org-log-reschedule note)
  4145. ("nologreschedule" org-log-reschedule nil)
  4146. ("logredeadline" org-log-redeadline time)
  4147. ("lognoteredeadline" org-log-redeadline note)
  4148. ("nologredeadline" org-log-redeadline nil)
  4149. ("logrefile" org-log-refile time)
  4150. ("lognoterefile" org-log-refile note)
  4151. ("nologrefile" org-log-refile nil)
  4152. ("fninline" org-footnote-define-inline t)
  4153. ("nofninline" org-footnote-define-inline nil)
  4154. ("fnlocal" org-footnote-section nil)
  4155. ("fnauto" org-footnote-auto-label t)
  4156. ("fnprompt" org-footnote-auto-label nil)
  4157. ("fnconfirm" org-footnote-auto-label confirm)
  4158. ("fnplain" org-footnote-auto-label plain)
  4159. ("fnadjust" org-footnote-auto-adjust t)
  4160. ("nofnadjust" org-footnote-auto-adjust nil)
  4161. ("constcgs" constants-unit-system cgs)
  4162. ("constSI" constants-unit-system SI)
  4163. ("noptag" org-tag-persistent-alist nil)
  4164. ("hideblocks" org-hide-block-startup t)
  4165. ("nohideblocks" org-hide-block-startup nil)
  4166. ("beamer" org-startup-with-beamer-mode t)
  4167. ("entitiespretty" org-pretty-entities t)
  4168. ("entitiesplain" org-pretty-entities nil))
  4169. "Variable associated with STARTUP options for org-mode.
  4170. Each element is a list of three items: the startup options (as written
  4171. in the #+STARTUP line), the corresponding variable, and the value to set
  4172. this variable to if the option is found. An optional forth element PUSH
  4173. means to push this value onto the list in the variable.")
  4174. (defun org-update-property-plist (key val props)
  4175. "Update PROPS with KEY and VAL."
  4176. (let* ((appending (string= "+" (substring key (- (length key) 1))))
  4177. (key (if appending (substring key 0 (- (length key) 1)) key))
  4178. (remainder (org-remove-if (lambda (p) (string= (car p) key)) props))
  4179. (previous (cdr (assoc key props))))
  4180. (if appending
  4181. (cons (cons key (if previous (concat previous " " val) val)) remainder)
  4182. (cons (cons key val) remainder))))
  4183. (defconst org-block-regexp
  4184. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  4185. "Regular expression for hiding blocks.")
  4186. (defconst org-heading-keyword-regexp-format
  4187. "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  4188. "Printf format for a regexp matching a headline with some keyword.
  4189. This regexp will match the headline of any node which has the
  4190. exact keyword that is put into the format. The keyword isn't in
  4191. any group by default, but the stars and the body are.")
  4192. (defconst org-heading-keyword-maybe-regexp-format
  4193. "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
  4194. "Printf format for a regexp matching a headline, possibly with some keyword.
  4195. This regexp can match any headline with the specified keyword, or
  4196. without a keyword. The keyword isn't in any group by default,
  4197. but the stars and the body are.")
  4198. (defcustom org-group-tags t
  4199. "When non-nil (the default), use group tags.
  4200. This can be turned on/off through `org-toggle-tags-groups'."
  4201. :group 'org-tags
  4202. :group 'org-startup
  4203. :type 'boolean)
  4204. (defun org-toggle-tags-groups ()
  4205. "Toggle support for group tags.
  4206. Support for group tags is controlled by the option
  4207. `org-group-tags', which is non-nil by default."
  4208. (interactive)
  4209. (setq org-group-tags (not org-group-tags))
  4210. (cond ((and (derived-mode-p 'org-agenda-mode)
  4211. org-group-tags)
  4212. (org-agenda-redo))
  4213. ((derived-mode-p 'org-mode)
  4214. (let ((org-inhibit-startup t)) (org-mode))))
  4215. (message "Groups tags support has been turned %s"
  4216. (if org-group-tags "on" "off")))
  4217. (defun org-set-regexps-and-options-for-tags ()
  4218. "Precompute regular expressions used for tags in the current buffer."
  4219. (when (derived-mode-p 'org-mode)
  4220. (org-set-local 'org-file-tags nil)
  4221. (let ((re (org-make-options-regexp '("FILETAGS" "TAGS")))
  4222. (splitre "[ \t]+")
  4223. (start 0)
  4224. tags ftags key value)
  4225. (save-excursion
  4226. (save-restriction
  4227. (widen)
  4228. (goto-char (point-min))
  4229. (while (re-search-forward re nil t)
  4230. (setq key (upcase (org-match-string-no-properties 1))
  4231. value (org-match-string-no-properties 2))
  4232. (if (stringp value) (setq value (org-trim value)))
  4233. (cond
  4234. ((equal key "TAGS")
  4235. (setq tags (append tags (if tags '("\\n") nil)
  4236. (org-split-string value splitre))))
  4237. ((equal key "FILETAGS")
  4238. (when (string-match "\\S-" value)
  4239. (setq ftags
  4240. (append
  4241. ftags
  4242. (apply 'append
  4243. (mapcar (lambda (x) (org-split-string x ":"))
  4244. (org-split-string value)))))))))))
  4245. ;; Process the file tags.
  4246. (and ftags (org-set-local 'org-file-tags
  4247. (mapcar 'org-add-prop-inherited ftags)))
  4248. (org-set-local 'org-tag-groups-alist nil)
  4249. ;; Process the tags.
  4250. ;; FIXME
  4251. (when tags
  4252. (let (e tgs g)
  4253. (while (setq e (pop tags))
  4254. (cond
  4255. ((equal e "{")
  4256. (progn (push '(:startgroup) tgs)
  4257. (when (equal (nth 1 tags) ":")
  4258. (push (list (replace-regexp-in-string
  4259. "(.+)$" "" (nth 0 tags)))
  4260. org-tag-groups-alist)
  4261. (setq g 0))))
  4262. ((equal e ":") (push '(:grouptags) tgs))
  4263. ((equal e "}") (push '(:endgroup) tgs) (if g (setq g nil)))
  4264. ((equal e "\\n") (push '(:newline) tgs))
  4265. ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e)
  4266. (push (cons (match-string 1 e)
  4267. (string-to-char (match-string 2 e))) tgs)
  4268. (if (and g (> g 0))
  4269. (setcar org-tag-groups-alist
  4270. (append (car org-tag-groups-alist)
  4271. (list (match-string 1 e)))))
  4272. (if g (setq g (1+ g))))
  4273. (t (push (list e) tgs)
  4274. (if (and g (> g 0))
  4275. (setcar org-tag-groups-alist
  4276. (append (car org-tag-groups-alist) (list e))))
  4277. (if g (setq g (1+ g))))))
  4278. (org-set-local 'org-tag-alist nil)
  4279. (while (setq e (pop tgs))
  4280. (or (and (stringp (car e))
  4281. (assoc (car e) org-tag-alist))
  4282. (push e org-tag-alist)))
  4283. ;; Return a list with tag variables
  4284. (list org-file-tags org-tag-alist org-tag-groups-alist))))))
  4285. (defun org-set-regexps-and-options ()
  4286. "Precompute regular expressions used in the current buffer."
  4287. (when (derived-mode-p 'org-mode)
  4288. (org-set-local 'org-todo-kwd-alist nil)
  4289. (org-set-local 'org-todo-key-alist nil)
  4290. (org-set-local 'org-todo-key-trigger nil)
  4291. (org-set-local 'org-todo-keywords-1 nil)
  4292. (org-set-local 'org-done-keywords nil)
  4293. (org-set-local 'org-todo-heads nil)
  4294. (org-set-local 'org-todo-sets nil)
  4295. (org-set-local 'org-todo-log-states nil)
  4296. (org-set-local 'org-file-properties nil)
  4297. (let ((re (org-make-options-regexp
  4298. '("CATEGORY" "TODO" "COLUMNS" "STARTUP" "ARCHIVE"
  4299. "LINK" "PRIORITIES" "CONSTANTS" "PROPERTY" "DRAWERS"
  4300. "SETUPFILE" "OPTIONS")
  4301. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  4302. (splitre "[ \t]+")
  4303. (scripts org-use-sub-superscripts)
  4304. kwds kws0 kwsa key log value cat arch const links hw dws
  4305. tail sep kws1 prio props drawers ext-setup-or-nil setup-contents
  4306. (start 0))
  4307. (save-excursion
  4308. (save-restriction
  4309. (widen)
  4310. (goto-char (point-min))
  4311. (while
  4312. (or (and
  4313. ext-setup-or-nil
  4314. (let (ret)
  4315. (with-temp-buffer
  4316. (insert ext-setup-or-nil)
  4317. (let ((major-mode 'org-mode))
  4318. (setq ret (save-match-data
  4319. (org-set-regexps-and-options-for-tags)))))
  4320. ;; Append setupfile tags to existing tags
  4321. (setq org-file-tags
  4322. (delq nil (append org-file-tags (nth 0 ret)))
  4323. org-tag-alist
  4324. (delq nil (append org-tag-alist (nth 1 ret)))
  4325. org-tag-groups-alist
  4326. (delq nil (append org-tag-groups-alist (nth 2 ret))))))
  4327. (and ext-setup-or-nil
  4328. (string-match re ext-setup-or-nil start)
  4329. (setq start (match-end 0)))
  4330. (and (setq ext-setup-or-nil nil start 0)
  4331. (re-search-forward re nil t)))
  4332. (setq key (upcase (match-string 1 ext-setup-or-nil))
  4333. value (org-match-string-no-properties 2 ext-setup-or-nil))
  4334. (if (stringp value) (setq value (org-trim value)))
  4335. (cond
  4336. ((equal key "CATEGORY")
  4337. (setq cat value))
  4338. ((member key '("SEQ_TODO" "TODO"))
  4339. (push (cons 'sequence (org-split-string value splitre)) kwds))
  4340. ((equal key "TYP_TODO")
  4341. (push (cons 'type (org-split-string value splitre)) kwds))
  4342. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  4343. ;; general TODO-like setup
  4344. (push (cons (intern (downcase (match-string 1 key)))
  4345. (org-split-string value splitre)) kwds))
  4346. ((equal key "COLUMNS")
  4347. (org-set-local 'org-columns-default-format value))
  4348. ((equal key "LINK")
  4349. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  4350. (push (cons (match-string 1 value)
  4351. (org-trim (match-string 2 value)))
  4352. links)))
  4353. ((equal key "PRIORITIES")
  4354. (setq prio (org-split-string value " +")))
  4355. ((equal key "PROPERTY")
  4356. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4357. (setq props (org-update-property-plist (match-string 1 value)
  4358. (match-string 2 value)
  4359. props))))
  4360. ((equal key "DRAWERS")
  4361. (setq drawers (delete-dups (append org-drawers (org-split-string value splitre)))))
  4362. ((equal key "CONSTANTS")
  4363. (org-table-set-constants))
  4364. ((equal key "STARTUP")
  4365. (let ((opts (org-split-string value splitre))
  4366. l var val)
  4367. (while (setq l (pop opts))
  4368. (when (setq l (assoc l org-startup-options))
  4369. (setq var (nth 1 l) val (nth 2 l))
  4370. (if (not (nth 3 l))
  4371. (set (make-local-variable var) val)
  4372. (if (not (listp (symbol-value var)))
  4373. (set (make-local-variable var) nil))
  4374. (set (make-local-variable var) (symbol-value var))
  4375. (add-to-list var val))))))
  4376. ((equal key "ARCHIVE")
  4377. (setq arch value)
  4378. (remove-text-properties 0 (length arch)
  4379. '(face t fontified t) arch))
  4380. ((equal key "OPTIONS")
  4381. (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value)
  4382. (setq scripts (read (match-string 2 value)))))
  4383. ((and (equal key "SETUPFILE")
  4384. ;; Prevent checking in Gnus messages
  4385. (not buffer-read-only))
  4386. (setq setup-contents (org-file-contents
  4387. (expand-file-name
  4388. (org-remove-double-quotes value))
  4389. 'noerror))
  4390. (if (not ext-setup-or-nil)
  4391. (setq ext-setup-or-nil setup-contents start 0)
  4392. (setq ext-setup-or-nil
  4393. (concat (substring ext-setup-or-nil 0 start)
  4394. "\n" setup-contents "\n"
  4395. (substring ext-setup-or-nil start)))))))
  4396. ;; search for property blocks
  4397. (goto-char (point-min))
  4398. (while (re-search-forward org-block-regexp nil t)
  4399. (when (equal "PROPERTY" (upcase (match-string 1)))
  4400. (setq value (replace-regexp-in-string
  4401. "[\n\r]" " " (match-string 4)))
  4402. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4403. (setq props (org-update-property-plist (match-string 1 value)
  4404. (match-string 2 value)
  4405. props)))))))
  4406. (org-set-local 'org-use-sub-superscripts scripts)
  4407. (when cat
  4408. (org-set-local 'org-category (intern cat))
  4409. (push (cons "CATEGORY" cat) props))
  4410. (when prio
  4411. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  4412. (setq prio (mapcar 'string-to-char prio))
  4413. (org-set-local 'org-highest-priority (nth 0 prio))
  4414. (org-set-local 'org-lowest-priority (nth 1 prio))
  4415. (org-set-local 'org-default-priority (nth 2 prio)))
  4416. (and props (org-set-local 'org-file-properties (nreverse props)))
  4417. (and drawers (org-set-local 'org-drawers drawers))
  4418. (and arch (org-set-local 'org-archive-location arch))
  4419. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  4420. ;; Process the TODO keywords
  4421. (unless kwds
  4422. ;; Use the global values as if they had been given locally.
  4423. (setq kwds (default-value 'org-todo-keywords))
  4424. (if (stringp (car kwds))
  4425. (setq kwds (list (cons org-todo-interpretation
  4426. (default-value 'org-todo-keywords)))))
  4427. (setq kwds (reverse kwds)))
  4428. (setq kwds (nreverse kwds))
  4429. (let (inter kws kw)
  4430. (while (setq kws (pop kwds))
  4431. (let ((kws (or
  4432. (run-hook-with-args-until-success
  4433. 'org-todo-setup-filter-hook kws)
  4434. kws)))
  4435. (setq inter (pop kws) sep (member "|" kws)
  4436. kws0 (delete "|" (copy-sequence kws))
  4437. kwsa nil
  4438. kws1 (mapcar
  4439. (lambda (x)
  4440. ;; 1 2
  4441. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  4442. (progn
  4443. (setq kw (match-string 1 x)
  4444. key (and (match-end 2) (match-string 2 x))
  4445. log (org-extract-log-state-settings x))
  4446. (push (cons kw (and key (string-to-char key))) kwsa)
  4447. (and log (push log org-todo-log-states))
  4448. kw)
  4449. (error "Invalid TODO keyword %s" x)))
  4450. kws0)
  4451. kwsa (if kwsa (append '((:startgroup))
  4452. (nreverse kwsa)
  4453. '((:endgroup))))
  4454. hw (car kws1)
  4455. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  4456. tail (list inter hw (car dws) (org-last dws))))
  4457. (add-to-list 'org-todo-heads hw 'append)
  4458. (push kws1 org-todo-sets)
  4459. (setq org-done-keywords (append org-done-keywords dws nil))
  4460. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  4461. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  4462. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  4463. (setq org-todo-sets (nreverse org-todo-sets)
  4464. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4465. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  4466. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  4467. ;; Compute the regular expressions and other local variables.
  4468. ;; Using `org-outline-regexp-bol' would complicate them much,
  4469. ;; because of the fixed white space at the end of that string.
  4470. (if (not org-done-keywords)
  4471. (setq org-done-keywords (and org-todo-keywords-1
  4472. (list (org-last org-todo-keywords-1)))))
  4473. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  4474. (length org-scheduled-string)
  4475. (length org-clock-string)
  4476. (length org-closed-string)))
  4477. org-drawer-regexp
  4478. (concat "^[ \t]*:\\("
  4479. (mapconcat 'regexp-quote org-drawers "\\|")
  4480. "\\):[ \t]*$")
  4481. org-not-done-keywords
  4482. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  4483. org-todo-regexp
  4484. (concat "\\("
  4485. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4486. "\\)")
  4487. org-not-done-regexp
  4488. (concat "\\("
  4489. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  4490. "\\)")
  4491. org-not-done-heading-regexp
  4492. (format org-heading-keyword-regexp-format org-not-done-regexp)
  4493. org-todo-line-regexp
  4494. (format org-heading-keyword-maybe-regexp-format org-todo-regexp)
  4495. org-complex-heading-regexp
  4496. (concat "^\\(\\*+\\)"
  4497. "\\(?: +" org-todo-regexp "\\)?"
  4498. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4499. "\\(?: +\\(.*?\\)\\)??"
  4500. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
  4501. "[ \t]*$")
  4502. org-complex-heading-regexp-format
  4503. (concat "^\\(\\*+\\)"
  4504. "\\(?: +" org-todo-regexp "\\)?"
  4505. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4506. "\\(?: +"
  4507. ;; Stats cookies can be stuck to body.
  4508. "\\(?:\\[[0-9%%/]+\\] *\\)?"
  4509. "\\(%s\\)"
  4510. "\\(?: *\\[[0-9%%/]+\\]\\)?"
  4511. "\\)"
  4512. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
  4513. "[ \t]*$")
  4514. org-todo-line-tags-regexp
  4515. (concat "^\\(\\*+\\)"
  4516. "\\(?: +" org-todo-regexp "\\)?"
  4517. "\\(?: +\\(.*?\\)\\)??"
  4518. (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
  4519. "[ \t]*$")
  4520. org-deadline-regexp (concat "\\<" org-deadline-string)
  4521. org-deadline-time-regexp
  4522. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  4523. org-deadline-time-hour-regexp
  4524. (concat "\\<" org-deadline-string
  4525. " *<\\([^>]+[0-9]\\{1,2\\}:[0-9]\\{2\\}[0-9-+:hdwmy \t.]*\\)>")
  4526. org-deadline-line-regexp
  4527. (concat "\\<\\(" org-deadline-string "\\).*")
  4528. org-scheduled-regexp
  4529. (concat "\\<" org-scheduled-string)
  4530. org-scheduled-time-regexp
  4531. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  4532. org-scheduled-time-hour-regexp
  4533. (concat "\\<" org-scheduled-string
  4534. " *<\\([^>]+[0-9]\\{1,2\\}:[0-9]\\{2\\}[0-9-+:hdwmy \t.]*\\)>")
  4535. org-closed-time-regexp
  4536. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  4537. org-keyword-time-regexp
  4538. (concat "\\<\\(" org-scheduled-string
  4539. "\\|" org-deadline-string
  4540. "\\|" org-closed-string
  4541. "\\|" org-clock-string "\\)"
  4542. " *[[<]\\([^]>]+\\)[]>]")
  4543. org-keyword-time-not-clock-regexp
  4544. (concat "\\<\\(" org-scheduled-string
  4545. "\\|" org-deadline-string
  4546. "\\|" org-closed-string
  4547. "\\)"
  4548. " *[[<]\\([^]>]+\\)[]>]")
  4549. org-maybe-keyword-time-regexp
  4550. (concat "\\(\\<\\(" org-scheduled-string
  4551. "\\|" org-deadline-string
  4552. "\\|" org-closed-string
  4553. "\\|" org-clock-string "\\)\\)?"
  4554. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4555. org-all-time-keywords
  4556. (mapcar (lambda (w) (substring w 0 -1))
  4557. (list org-scheduled-string org-deadline-string
  4558. org-clock-string org-closed-string)))
  4559. (org-compute-latex-and-related-regexp)
  4560. (org-set-font-lock-defaults))))
  4561. (defun org-file-contents (file &optional noerror)
  4562. "Return the contents of FILE, as a string."
  4563. (if (or (not file)
  4564. (not (file-readable-p file)))
  4565. (if noerror
  4566. (message "Cannot read file \"%s\"" file)
  4567. (error "Cannot read file \"%s\"" file))
  4568. (with-temp-buffer
  4569. (insert-file-contents file)
  4570. (buffer-string))))
  4571. (defun org-extract-log-state-settings (x)
  4572. "Extract the log state setting from a TODO keyword string.
  4573. This will extract info from a string like \"WAIT(w@/!)\"."
  4574. (let (kw key log1 log2)
  4575. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4576. (setq kw (match-string 1 x)
  4577. key (and (match-end 2) (match-string 2 x))
  4578. log1 (and (match-end 3) (match-string 3 x))
  4579. log2 (and (match-end 4) (match-string 4 x)))
  4580. (and (or log1 log2)
  4581. (list kw
  4582. (and log1 (if (equal log1 "!") 'time 'note))
  4583. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4584. (defun org-remove-keyword-keys (list)
  4585. "Remove a pair of parenthesis at the end of each string in LIST."
  4586. (mapcar (lambda (x)
  4587. (if (string-match "(.*)$" x)
  4588. (substring x 0 (match-beginning 0))
  4589. x))
  4590. list))
  4591. (defun org-assign-fast-keys (alist)
  4592. "Assign fast keys to a keyword-key alist.
  4593. Respect keys that are already there."
  4594. (let (new e (alt ?0))
  4595. (while (setq e (pop alist))
  4596. (if (or (memq (car e) '(:newline :grouptags :endgroup :startgroup))
  4597. (cdr e)) ;; Key already assigned.
  4598. (push e new)
  4599. (let ((clist (string-to-list (downcase (car e))))
  4600. (used (append new alist)))
  4601. (when (= (car clist) ?@)
  4602. (pop clist))
  4603. (while (and clist (rassoc (car clist) used))
  4604. (pop clist))
  4605. (unless clist
  4606. (while (rassoc alt used)
  4607. (incf alt)))
  4608. (push (cons (car e) (or (car clist) alt)) new))))
  4609. (nreverse new)))
  4610. ;;; Some variables used in various places
  4611. (defvar org-window-configuration nil
  4612. "Used in various places to store a window configuration.")
  4613. (defvar org-selected-window nil
  4614. "Used in various places to store a window configuration.")
  4615. (defvar org-finish-function nil
  4616. "Function to be called when `C-c C-c' is used.
  4617. This is for getting out of special buffers like capture.")
  4618. ;; FIXME: Occasionally check by commenting these, to make sure
  4619. ;; no other functions uses these, forgetting to let-bind them.
  4620. (org-no-warnings (defvar entry)) ;; unprefixed, from calendar.el
  4621. (defvar org-last-state)
  4622. (org-no-warnings (defvar date)) ;; unprefixed, from calendar.el
  4623. ;; Defined somewhere in this file, but used before definition.
  4624. (defvar org-entities) ;; defined in org-entities.el
  4625. (defvar org-struct-menu)
  4626. (defvar org-org-menu)
  4627. (defvar org-tbl-menu)
  4628. ;;;; Define the Org-mode
  4629. ;; We use a before-change function to check if a table might need
  4630. ;; an update.
  4631. (defvar org-table-may-need-update t
  4632. "Indicates that a table might need an update.
  4633. This variable is set by `org-before-change-function'.
  4634. `org-table-align' sets it back to nil.")
  4635. (defun org-before-change-function (beg end)
  4636. "Every change indicates that a table might need an update."
  4637. (setq org-table-may-need-update t))
  4638. (defvar org-mode-map)
  4639. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4640. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4641. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4642. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4643. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4644. (defvar org-table-buffer-is-an nil)
  4645. (defvar bidi-paragraph-direction)
  4646. (defvar buffer-face-mode-face)
  4647. (require 'outline)
  4648. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4649. (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"))
  4650. (require 'noutline "noutline" 'noerror) ;; stock XEmacs does not have it
  4651. ;; Other stuff we need.
  4652. (require 'time-date)
  4653. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  4654. (require 'easymenu)
  4655. (require 'overlay)
  4656. ;; (require 'org-macs) moved higher up in the file before it is first used
  4657. (require 'org-entities)
  4658. ;; (require 'org-compat) moved higher up in the file before it is first used
  4659. (require 'org-faces)
  4660. (require 'org-list)
  4661. (require 'org-pcomplete)
  4662. (require 'org-src)
  4663. (require 'org-footnote)
  4664. (require 'org-macro)
  4665. ;; babel
  4666. (require 'ob)
  4667. ;;;###autoload
  4668. (define-derived-mode org-mode outline-mode "Org"
  4669. "Outline-based notes management and organizer, alias
  4670. \"Carsten's outline-mode for keeping track of everything.\"
  4671. Org-mode develops organizational tasks around a NOTES file which
  4672. contains information about projects as plain text. Org-mode is
  4673. implemented on top of outline-mode, which is ideal to keep the content
  4674. of large files well structured. It supports ToDo items, deadlines and
  4675. time stamps, which magically appear in the diary listing of the Emacs
  4676. calendar. Tables are easily created with a built-in table editor.
  4677. Plain text URL-like links connect to websites, emails (VM), Usenet
  4678. messages (Gnus), BBDB entries, and any files related to the project.
  4679. For printing and sharing of notes, an Org-mode file (or a part of it)
  4680. can be exported as a structured ASCII or HTML file.
  4681. The following commands are available:
  4682. \\{org-mode-map}"
  4683. ;; Get rid of Outline menus, they are not needed
  4684. ;; Need to do this here because define-derived-mode sets up
  4685. ;; the keymap so late. Still, it is a waste to call this each time
  4686. ;; we switch another buffer into org-mode.
  4687. (if (featurep 'xemacs)
  4688. (when (boundp 'outline-mode-menu-heading)
  4689. ;; Assume this is Greg's port, it uses easymenu
  4690. (easy-menu-remove outline-mode-menu-heading)
  4691. (easy-menu-remove outline-mode-menu-show)
  4692. (easy-menu-remove outline-mode-menu-hide))
  4693. (define-key org-mode-map [menu-bar headings] 'undefined)
  4694. (define-key org-mode-map [menu-bar hide] 'undefined)
  4695. (define-key org-mode-map [menu-bar show] 'undefined))
  4696. (org-load-modules-maybe)
  4697. (easy-menu-add org-org-menu)
  4698. (easy-menu-add org-tbl-menu)
  4699. (org-install-agenda-files-menu)
  4700. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4701. (add-to-invisibility-spec '(org-cwidth))
  4702. (add-to-invisibility-spec '(org-hide-block . t))
  4703. (when (featurep 'xemacs)
  4704. (org-set-local 'line-move-ignore-invisible t))
  4705. (org-set-local 'outline-regexp org-outline-regexp)
  4706. (org-set-local 'outline-level 'org-outline-level)
  4707. (setq bidi-paragraph-direction 'left-to-right)
  4708. ;; FIXME Circumvent a bug in outline.el (Emacs <24.4)
  4709. (set (make-local-variable 'paragraph-start) " \\|[ \t]*$\\|\\*+ ")
  4710. (when (and org-ellipsis
  4711. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4712. (fboundp 'make-glyph-code))
  4713. (unless org-display-table
  4714. (setq org-display-table (make-display-table)))
  4715. (set-display-table-slot
  4716. org-display-table 4
  4717. (vconcat (mapcar
  4718. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4719. org-ellipsis)))
  4720. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4721. (setq buffer-display-table org-display-table))
  4722. (org-set-regexps-and-options-for-tags)
  4723. (org-set-regexps-and-options)
  4724. (when (and org-tag-faces (not org-tags-special-faces-re))
  4725. ;; tag faces set outside customize.... force initialization.
  4726. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4727. ;; Calc embedded
  4728. (org-set-local 'calc-embedded-open-mode "# ")
  4729. ;; Modify a few syntax entries
  4730. (modify-syntax-entry ?@ "w")
  4731. (modify-syntax-entry ?\" "\"")
  4732. (if org-startup-truncated (setq truncate-lines t))
  4733. (when org-startup-indented (require 'org-indent) (org-indent-mode 1))
  4734. (org-set-local 'font-lock-unfontify-region-function
  4735. 'org-unfontify-region)
  4736. ;; Activate before-change-function
  4737. (org-set-local 'org-table-may-need-update t)
  4738. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4739. 'local)
  4740. ;; Check for running clock before killing a buffer
  4741. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4742. ;; Initialize macros templates.
  4743. (org-macro-initialize-templates)
  4744. ;; Initialize radio targets.
  4745. (org-update-radio-target-regexp)
  4746. ;; Indentation.
  4747. (org-set-local 'indent-line-function 'org-indent-line)
  4748. (org-set-local 'indent-region-function 'org-indent-region)
  4749. ;; Filling and auto-filling.
  4750. (org-setup-filling)
  4751. ;; Comments.
  4752. (org-setup-comments-handling)
  4753. ;; Beginning/end of defun
  4754. (org-set-local 'beginning-of-defun-function 'org-backward-element)
  4755. (org-set-local 'end-of-defun-function 'org-forward-element)
  4756. ;; Next error for sparse trees
  4757. (org-set-local 'next-error-function 'org-occur-next-match)
  4758. ;; Make sure dependence stuff works reliably, even for users who set it
  4759. ;; too late :-(
  4760. (if org-enforce-todo-dependencies
  4761. (add-hook 'org-blocker-hook
  4762. 'org-block-todo-from-children-or-siblings-or-parent)
  4763. (remove-hook 'org-blocker-hook
  4764. 'org-block-todo-from-children-or-siblings-or-parent))
  4765. (if org-enforce-todo-checkbox-dependencies
  4766. (add-hook 'org-blocker-hook
  4767. 'org-block-todo-from-checkboxes)
  4768. (remove-hook 'org-blocker-hook
  4769. 'org-block-todo-from-checkboxes))
  4770. ;; Align options lines
  4771. (org-set-local
  4772. 'align-mode-rules-list
  4773. '((org-in-buffer-settings
  4774. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4775. (modes . '(org-mode)))))
  4776. ;; Imenu
  4777. (org-set-local 'imenu-create-index-function
  4778. 'org-imenu-get-tree)
  4779. ;; Make isearch reveal context
  4780. (if (or (featurep 'xemacs)
  4781. (not (boundp 'outline-isearch-open-invisible-function)))
  4782. ;; Emacs 21 and XEmacs make use of the hook
  4783. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4784. ;; Emacs 22 deals with this through a special variable
  4785. (org-set-local 'outline-isearch-open-invisible-function
  4786. (lambda (&rest ignore) (org-show-context 'isearch)))
  4787. (org-add-hook 'isearch-mode-end-hook 'org-fix-ellipsis-at-bol 'append 'local))
  4788. ;; Setup the pcomplete hooks
  4789. (set (make-local-variable 'pcomplete-command-completion-function)
  4790. 'org-pcomplete-initial)
  4791. (set (make-local-variable 'pcomplete-command-name-function)
  4792. 'org-command-at-point)
  4793. (set (make-local-variable 'pcomplete-default-completion-function)
  4794. 'ignore)
  4795. (set (make-local-variable 'pcomplete-parse-arguments-function)
  4796. 'org-parse-arguments)
  4797. (set (make-local-variable 'pcomplete-termination-string) "")
  4798. (when (>= emacs-major-version 23)
  4799. (set (make-local-variable 'buffer-face-mode-face) 'org-default))
  4800. ;; If empty file that did not turn on org-mode automatically, make it to.
  4801. (if (and org-insert-mode-line-in-empty-file
  4802. (org-called-interactively-p 'any)
  4803. (= (point-min) (point-max)))
  4804. (insert "# -*- mode: org -*-\n\n"))
  4805. (unless org-inhibit-startup
  4806. (org-unmodified
  4807. (and org-startup-with-beamer-mode (org-beamer-mode))
  4808. (when org-startup-align-all-tables
  4809. (org-table-map-tables 'org-table-align 'quietly))
  4810. (when org-startup-with-inline-images
  4811. (org-display-inline-images))
  4812. (when org-startup-with-latex-preview
  4813. (org-preview-latex-fragment))
  4814. (unless org-inhibit-startup-visibility-stuff
  4815. (org-set-startup-visibility))))
  4816. ;; Try to set org-hide correctly
  4817. (set-face-foreground 'org-hide (org-find-invisible-foreground)))
  4818. ;; Update `customize-package-emacs-version-alist'
  4819. (add-to-list 'customize-package-emacs-version-alist
  4820. '(Org ("6.21b" . "23.1") ("6.33x" . "23.2")
  4821. ("7.8.11" . "24.1") ("7.9.4" . "24.3")
  4822. ("8.0" . "24.4")))
  4823. (defvar org-mode-transpose-word-syntax-table
  4824. (let ((st (make-syntax-table)))
  4825. (mapc (lambda(c) (modify-syntax-entry
  4826. (string-to-char (car c)) "w p" st))
  4827. org-emphasis-alist)
  4828. st))
  4829. (when (fboundp 'abbrev-table-put)
  4830. (abbrev-table-put org-mode-abbrev-table
  4831. :parents (list text-mode-abbrev-table)))
  4832. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4833. (defsubst org-fix-ellipsis-at-bol ()
  4834. (save-excursion (goto-char (window-start)) (recenter 0)))
  4835. (defun org-find-invisible-foreground ()
  4836. (let ((candidates (remove
  4837. "unspecified-bg"
  4838. (nconc
  4839. (list (face-background 'default)
  4840. (face-background 'org-default))
  4841. (mapcar
  4842. (lambda (alist)
  4843. (when (boundp alist)
  4844. (cdr (assoc 'background-color (symbol-value alist)))))
  4845. '(default-frame-alist initial-frame-alist window-system-default-frame-alist))
  4846. (list (face-foreground 'org-hide))))))
  4847. (car (remove nil candidates))))
  4848. (defun org-current-time (&optional rounding-minutes past)
  4849. "Current time, possibly rounded to ROUNDING-MINUTES.
  4850. When ROUNDING-MINUTES is not an integer, fall back on the car of
  4851. `org-time-stamp-rounding-minutes'. When PAST is non-nil, ensure
  4852. the rounding returns a past time."
  4853. (let ((r (or (and (integerp rounding-minutes) rounding-minutes)
  4854. (car org-time-stamp-rounding-minutes)))
  4855. (time (decode-time)) res)
  4856. (if (< r 1)
  4857. (current-time)
  4858. (setq res
  4859. (apply 'encode-time
  4860. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4861. (nthcdr 2 time))))
  4862. (if (and past (< (org-float-time (time-subtract (current-time) res)) 0))
  4863. (seconds-to-time (- (org-float-time res) (* r 60)))
  4864. res))))
  4865. (defun org-today ()
  4866. "Return today date, considering `org-extend-today-until'."
  4867. (time-to-days
  4868. (time-subtract (current-time)
  4869. (list 0 (* 3600 org-extend-today-until) 0))))
  4870. ;;;; Font-Lock stuff, including the activators
  4871. (defvar org-mouse-map (make-sparse-keymap))
  4872. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4873. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4874. (when org-mouse-1-follows-link
  4875. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4876. (when org-tab-follows-link
  4877. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4878. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4879. (require 'font-lock)
  4880. (defconst org-non-link-chars "]\t\n\r<>")
  4881. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  4882. "shell" "elisp" "doi" "message"))
  4883. (defvar org-link-types-re nil
  4884. "Matches a link that has a url-like prefix like \"http:\"")
  4885. (defvar org-link-re-with-space nil
  4886. "Matches a link with spaces, optional angular brackets around it.")
  4887. (defvar org-link-re-with-space2 nil
  4888. "Matches a link with spaces, optional angular brackets around it.")
  4889. (defvar org-link-re-with-space3 nil
  4890. "Matches a link with spaces, only for internal part in bracket links.")
  4891. (defvar org-angle-link-re nil
  4892. "Matches link with angular brackets, spaces are allowed.")
  4893. (defvar org-plain-link-re nil
  4894. "Matches plain link, without spaces.")
  4895. (defvar org-bracket-link-regexp nil
  4896. "Matches a link in double brackets.")
  4897. (defvar org-bracket-link-analytic-regexp nil
  4898. "Regular expression used to analyze links.
  4899. Here is what the match groups contain after a match:
  4900. 1: http:
  4901. 2: http
  4902. 3: path
  4903. 4: [desc]
  4904. 5: desc")
  4905. (defvar org-bracket-link-analytic-regexp++ nil
  4906. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4907. (defvar org-any-link-re nil
  4908. "Regular expression matching any link.")
  4909. (defconst org-match-sexp-depth 3
  4910. "Number of stacked braces for sub/superscript matching.")
  4911. (defun org-create-multibrace-regexp (left right n)
  4912. "Create a regular expression which will match a balanced sexp.
  4913. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4914. as single character strings.
  4915. The regexp returned will match the entire expression including the
  4916. delimiters. It will also define a single group which contains the
  4917. match except for the outermost delimiters. The maximum depth of
  4918. stacked delimiters is N. Escaping delimiters is not possible."
  4919. (let* ((nothing (concat "[^" left right "]*?"))
  4920. (or "\\|")
  4921. (re nothing)
  4922. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4923. (while (> n 1)
  4924. (setq n (1- n)
  4925. re (concat re or next)
  4926. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4927. (concat left "\\(" re "\\)" right)))
  4928. (defvar org-match-substring-regexp
  4929. (concat
  4930. "\\(\\S-\\)\\([_^]\\)\\("
  4931. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4932. "\\|"
  4933. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4934. "\\|"
  4935. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  4936. "The regular expression matching a sub- or superscript.")
  4937. (defvar org-match-substring-with-braces-regexp
  4938. (concat
  4939. "\\(\\S-\\)\\([_^]\\)\\("
  4940. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4941. "\\)")
  4942. "The regular expression matching a sub- or superscript, forcing braces.")
  4943. (defun org-make-link-regexps ()
  4944. "Update the link regular expressions.
  4945. This should be called after the variable `org-link-types' has changed."
  4946. (setq org-link-types-re
  4947. (concat
  4948. "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
  4949. org-link-re-with-space
  4950. (concat
  4951. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4952. "\\([^" org-non-link-chars " ]"
  4953. "[^" org-non-link-chars "]*"
  4954. "[^" org-non-link-chars " ]\\)>?")
  4955. org-link-re-with-space2
  4956. (concat
  4957. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4958. "\\([^" org-non-link-chars " ]"
  4959. "[^\t\n\r]*"
  4960. "[^" org-non-link-chars " ]\\)>?")
  4961. org-link-re-with-space3
  4962. (concat
  4963. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4964. "\\([^" org-non-link-chars " ]"
  4965. "[^\t\n\r]*\\)")
  4966. org-angle-link-re
  4967. (concat
  4968. "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4969. "\\([^" org-non-link-chars " ]"
  4970. "[^" org-non-link-chars "]*"
  4971. "\\)>")
  4972. org-plain-link-re
  4973. (concat
  4974. "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4975. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  4976. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4977. org-bracket-link-regexp
  4978. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4979. org-bracket-link-analytic-regexp
  4980. (concat
  4981. "\\[\\["
  4982. "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
  4983. "\\([^]]+\\)"
  4984. "\\]"
  4985. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4986. "\\]")
  4987. org-bracket-link-analytic-regexp++
  4988. (concat
  4989. "\\[\\["
  4990. "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  4991. "\\([^]]+\\)"
  4992. "\\]"
  4993. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4994. "\\]")
  4995. org-any-link-re
  4996. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4997. org-angle-link-re "\\)\\|\\("
  4998. org-plain-link-re "\\)")))
  4999. (org-make-link-regexps)
  5000. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
  5001. "Regular expression for fast time stamp matching.")
  5002. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
  5003. "Regular expression for fast time stamp matching.")
  5004. (defconst org-ts-regexp0
  5005. "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  5006. "Regular expression matching time strings for analysis.
  5007. This one does not require the space after the date, so it can be used
  5008. on a string that terminates immediately after the date.")
  5009. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  5010. "Regular expression matching time strings for analysis.")
  5011. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  5012. "Regular expression matching time stamps, with groups.")
  5013. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  5014. "Regular expression matching time stamps (also [..]), with groups.")
  5015. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  5016. "Regular expression matching a time stamp range.")
  5017. (defconst org-tr-regexp-both
  5018. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  5019. "Regular expression matching a time stamp range.")
  5020. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  5021. org-ts-regexp "\\)?")
  5022. "Regular expression matching a time stamp or time stamp range.")
  5023. (defconst org-tsr-regexp-both
  5024. (concat org-ts-regexp-both "\\(--?-?"
  5025. org-ts-regexp-both "\\)?")
  5026. "Regular expression matching a time stamp or time stamp range.
  5027. The time stamps may be either active or inactive.")
  5028. (defvar org-emph-face nil)
  5029. (defun org-do-emphasis-faces (limit)
  5030. "Run through the buffer and add overlays to emphasized strings."
  5031. (let (rtn a)
  5032. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  5033. (if (not (= (char-after (match-beginning 3))
  5034. (char-after (match-beginning 4))))
  5035. (progn
  5036. (setq rtn t)
  5037. (setq a (assoc (match-string 3) org-emphasis-alist))
  5038. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  5039. 'face
  5040. (nth 1 a))
  5041. (and (nth 4 a)
  5042. (org-remove-flyspell-overlays-in
  5043. (match-beginning 0) (match-end 0)))
  5044. (add-text-properties (match-beginning 2) (match-end 2)
  5045. '(font-lock-multiline t org-emphasis t))
  5046. (when org-hide-emphasis-markers
  5047. (add-text-properties (match-end 4) (match-beginning 5)
  5048. '(invisible org-link))
  5049. (add-text-properties (match-beginning 3) (match-end 3)
  5050. '(invisible org-link)))))
  5051. (backward-char 1))
  5052. rtn))
  5053. (defun org-emphasize (&optional char)
  5054. "Insert or change an emphasis, i.e. a font like bold or italic.
  5055. If there is an active region, change that region to a new emphasis.
  5056. If there is no region, just insert the marker characters and position
  5057. the cursor between them.
  5058. CHAR should be the marker character. If it is a space, it means to
  5059. remove the emphasis of the selected region.
  5060. If CHAR is not given (for example in an interactive call) it will be
  5061. prompted for."
  5062. (interactive)
  5063. (let ((erc org-emphasis-regexp-components)
  5064. (prompt "")
  5065. (string "") beg end move c s)
  5066. (if (org-region-active-p)
  5067. (setq beg (region-beginning) end (region-end)
  5068. string (buffer-substring beg end))
  5069. (setq move t))
  5070. (unless char
  5071. (message "Emphasis marker or tag: [%s]"
  5072. (mapconcat (lambda(e) (car e)) org-emphasis-alist ""))
  5073. (setq char (read-char-exclusive)))
  5074. (if (equal char ?\ )
  5075. (setq s "" move nil)
  5076. (unless (assoc (char-to-string char) org-emphasis-alist)
  5077. (user-error "No such emphasis marker: \"%c\"" char))
  5078. (setq s (char-to-string char)))
  5079. (while (and (> (length string) 1)
  5080. (equal (substring string 0 1) (substring string -1))
  5081. (assoc (substring string 0 1) org-emphasis-alist))
  5082. (setq string (substring string 1 -1)))
  5083. (setq string (concat s string s))
  5084. (if beg (delete-region beg end))
  5085. (unless (or (bolp)
  5086. (string-match (concat "[" (nth 0 erc) "\n]")
  5087. (char-to-string (char-before (point)))))
  5088. (insert " "))
  5089. (unless (or (eobp)
  5090. (string-match (concat "[" (nth 1 erc) "\n]")
  5091. (char-to-string (char-after (point)))))
  5092. (insert " ") (backward-char 1))
  5093. (insert string)
  5094. (and move (backward-char 1))))
  5095. (defconst org-nonsticky-props
  5096. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text htmlize-link))
  5097. (defsubst org-rear-nonsticky-at (pos)
  5098. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  5099. (defun org-activate-plain-links (limit)
  5100. "Run through the buffer and add overlays to links."
  5101. (let (f hl)
  5102. (when (and (re-search-forward (concat org-plain-link-re) limit t)
  5103. (not (org-in-src-block-p)))
  5104. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5105. (setq f (get-text-property (match-beginning 0) 'face))
  5106. (setq hl (org-match-string-no-properties 0))
  5107. (if (or (eq f 'org-tag)
  5108. (and (listp f) (memq 'org-tag f)))
  5109. nil
  5110. (add-text-properties (match-beginning 0) (match-end 0)
  5111. (list 'mouse-face 'highlight
  5112. 'face 'org-link
  5113. 'htmlize-link `(:uri ,hl)
  5114. 'keymap org-mouse-map))
  5115. (org-rear-nonsticky-at (match-end 0)))
  5116. t)))
  5117. (defun org-activate-code (limit)
  5118. (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  5119. (progn
  5120. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5121. (remove-text-properties (match-beginning 0) (match-end 0)
  5122. '(display t invisible t intangible t))
  5123. t)))
  5124. (defcustom org-src-fontify-natively nil
  5125. "When non-nil, fontify code in code blocks."
  5126. :type 'boolean
  5127. :version "24.1"
  5128. :group 'org-appearance
  5129. :group 'org-babel)
  5130. (defcustom org-allow-promoting-top-level-subtree nil
  5131. "When non-nil, allow promoting a top level subtree.
  5132. The leading star of the top level headline will be replaced
  5133. by a #."
  5134. :type 'boolean
  5135. :version "24.1"
  5136. :group 'org-appearance)
  5137. (defun org-fontify-meta-lines-and-blocks (limit)
  5138. (condition-case nil
  5139. (org-fontify-meta-lines-and-blocks-1 limit)
  5140. (error (message "org-mode fontification error"))))
  5141. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  5142. "Fontify #+ lines and blocks."
  5143. (let ((case-fold-search t))
  5144. (if (re-search-forward
  5145. "^\\([ \t]*#\\(\\(\\+[a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  5146. limit t)
  5147. (let ((beg (match-beginning 0))
  5148. (block-start (match-end 0))
  5149. (block-end nil)
  5150. (lang (match-string 7))
  5151. (beg1 (line-beginning-position 2))
  5152. (dc1 (downcase (match-string 2)))
  5153. (dc3 (downcase (match-string 3)))
  5154. end end1 quoting block-type ovl)
  5155. (cond
  5156. ((member dc1 '("+html:" "+ascii:" "+latex:"))
  5157. ;; a single line of backend-specific content
  5158. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5159. (remove-text-properties (match-beginning 0) (match-end 0)
  5160. '(display t invisible t intangible t))
  5161. (add-text-properties (match-beginning 1) (match-end 3)
  5162. '(font-lock-fontified t face org-meta-line))
  5163. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  5164. '(font-lock-fontified t face org-block))
  5165. ; for backend-specific code
  5166. t)
  5167. ((and (match-end 4) (equal dc3 "+begin"))
  5168. ;; Truly a block
  5169. (setq block-type (downcase (match-string 5))
  5170. quoting (member block-type org-protecting-blocks))
  5171. (when (re-search-forward
  5172. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  5173. nil t) ;; on purpose, we look further than LIMIT
  5174. (setq end (min (point-max) (match-end 0))
  5175. end1 (min (point-max) (1- (match-beginning 0))))
  5176. (setq block-end (match-beginning 0))
  5177. (when quoting
  5178. (remove-text-properties beg end
  5179. '(display t invisible t intangible t)))
  5180. (add-text-properties
  5181. beg end
  5182. '(font-lock-fontified t font-lock-multiline t))
  5183. (add-text-properties beg beg1 '(face org-meta-line))
  5184. (add-text-properties end1 (min (point-max) (1+ end))
  5185. '(face org-meta-line)) ; for end_src
  5186. (cond
  5187. ((and lang (not (string= lang "")) org-src-fontify-natively)
  5188. (org-src-font-lock-fontify-block lang block-start block-end)
  5189. ;; remove old background overlays
  5190. (mapc (lambda (ov)
  5191. (if (eq (overlay-get ov 'face) 'org-block-background)
  5192. (delete-overlay ov)))
  5193. (overlays-at (/ (+ beg1 block-end) 2)))
  5194. ;; add a background overlay
  5195. (setq ovl (make-overlay beg1 block-end))
  5196. (overlay-put ovl 'face 'org-block-background)
  5197. (overlay-put ovl 'evaporate t)) ;; make it go away when empty
  5198. (quoting
  5199. (add-text-properties beg1 (min (point-max) (1+ end1))
  5200. '(face org-block))) ; end of source block
  5201. ((not org-fontify-quote-and-verse-blocks))
  5202. ((string= block-type "quote")
  5203. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-quote)))
  5204. ((string= block-type "verse")
  5205. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-verse))))
  5206. (add-text-properties beg beg1 '(face org-block-begin-line))
  5207. (add-text-properties (min (point-max) (1+ end)) (min (point-max) (1+ end1))
  5208. '(face org-block-end-line))
  5209. t))
  5210. ((member dc1 '("+title:" "+author:" "+email:" "+date:"))
  5211. (add-text-properties
  5212. beg (match-end 3)
  5213. (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
  5214. '(font-lock-fontified t invisible t)
  5215. '(font-lock-fontified t face org-document-info-keyword)))
  5216. (add-text-properties
  5217. (match-beginning 6) (min (point-max) (1+ (match-end 6)))
  5218. (if (string-equal dc1 "+title:")
  5219. '(font-lock-fontified t face org-document-title)
  5220. '(font-lock-fontified t face org-document-info))))
  5221. ((or (equal dc1 "+results")
  5222. (member dc1 '("+begin:" "+end:" "+caption:" "+label:"
  5223. "+orgtbl:" "+tblfm:" "+tblname:" "+results:"
  5224. "+call:" "+header:" "+headers:" "+name:"))
  5225. (and (match-end 4) (equal dc3 "+attr")))
  5226. (add-text-properties
  5227. beg (match-end 0)
  5228. '(font-lock-fontified t face org-meta-line))
  5229. t)
  5230. ((member dc3 '(" " ""))
  5231. (add-text-properties
  5232. beg (match-end 0)
  5233. '(font-lock-fontified t face font-lock-comment-face)))
  5234. ((not (member (char-after beg) '(?\ ?\t)))
  5235. ;; just any other in-buffer setting, but not indented
  5236. (add-text-properties
  5237. beg (match-end 0)
  5238. '(font-lock-fontified t face org-meta-line))
  5239. t)
  5240. (t nil))))))
  5241. (defun org-activate-angle-links (limit)
  5242. "Run through the buffer and add overlays to links."
  5243. (if (and (re-search-forward org-angle-link-re limit t)
  5244. (not (org-in-src-block-p)))
  5245. (progn
  5246. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5247. (add-text-properties (match-beginning 0) (match-end 0)
  5248. (list 'mouse-face 'highlight
  5249. 'keymap org-mouse-map))
  5250. (org-rear-nonsticky-at (match-end 0))
  5251. t)))
  5252. (defun org-activate-footnote-links (limit)
  5253. "Run through the buffer and add overlays to footnotes."
  5254. (let ((fn (org-footnote-next-reference-or-definition limit)))
  5255. (when fn
  5256. (let ((beg (nth 1 fn)) (end (nth 2 fn)))
  5257. (org-remove-flyspell-overlays-in beg end)
  5258. (add-text-properties beg end
  5259. (list 'mouse-face 'highlight
  5260. 'keymap org-mouse-map
  5261. 'help-echo
  5262. (if (= (point-at-bol) beg)
  5263. "Footnote definition"
  5264. "Footnote reference")
  5265. 'font-lock-fontified t
  5266. 'font-lock-multiline t
  5267. 'face 'org-footnote))))))
  5268. (defun org-activate-bracket-links (limit)
  5269. "Run through the buffer and add overlays to bracketed links."
  5270. (if (and (re-search-forward org-bracket-link-regexp limit t)
  5271. (not (org-in-src-block-p)))
  5272. (let* ((hl (org-match-string-no-properties 1))
  5273. (help (concat "LINK: " (save-match-data (org-link-unescape hl))))
  5274. (ip (org-maybe-intangible
  5275. (list 'invisible 'org-link
  5276. 'keymap org-mouse-map 'mouse-face 'highlight
  5277. 'font-lock-multiline t 'help-echo help
  5278. 'htmlize-link `(:uri ,hl))))
  5279. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  5280. 'font-lock-multiline t 'help-echo help
  5281. 'htmlize-link `(:uri ,hl))))
  5282. ;; We need to remove the invisible property here. Table narrowing
  5283. ;; may have made some of this invisible.
  5284. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5285. (remove-text-properties (match-beginning 0) (match-end 0)
  5286. '(invisible nil))
  5287. (if (match-end 3)
  5288. (progn
  5289. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  5290. (org-rear-nonsticky-at (match-beginning 3))
  5291. (add-text-properties (match-beginning 3) (match-end 3) vp)
  5292. (org-rear-nonsticky-at (match-end 3))
  5293. (add-text-properties (match-end 3) (match-end 0) ip)
  5294. (org-rear-nonsticky-at (match-end 0)))
  5295. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  5296. (org-rear-nonsticky-at (match-beginning 1))
  5297. (add-text-properties (match-beginning 1) (match-end 1) vp)
  5298. (org-rear-nonsticky-at (match-end 1))
  5299. (add-text-properties (match-end 1) (match-end 0) ip)
  5300. (org-rear-nonsticky-at (match-end 0)))
  5301. t)))
  5302. (defun org-activate-dates (limit)
  5303. "Run through the buffer and add overlays to dates."
  5304. (if (and (re-search-forward org-tsr-regexp-both limit t)
  5305. (not (equal (char-before (match-beginning 0)) 91)))
  5306. (progn
  5307. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5308. (add-text-properties (match-beginning 0) (match-end 0)
  5309. (list 'mouse-face 'highlight
  5310. 'keymap org-mouse-map))
  5311. (org-rear-nonsticky-at (match-end 0))
  5312. (when org-display-custom-times
  5313. (if (match-end 3)
  5314. (org-display-custom-time (match-beginning 3) (match-end 3)))
  5315. (org-display-custom-time (match-beginning 1) (match-end 1)))
  5316. t)))
  5317. (defvar org-target-link-regexp nil
  5318. "Regular expression matching radio targets in plain text.")
  5319. (make-variable-buffer-local 'org-target-link-regexp)
  5320. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  5321. "Regular expression matching a link target.")
  5322. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  5323. "Regular expression matching a radio target.")
  5324. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  5325. "Regular expression matching any target.")
  5326. (defun org-activate-target-links (limit)
  5327. "Run through the buffer and add overlays to target matches."
  5328. (when org-target-link-regexp
  5329. (let ((case-fold-search t))
  5330. (if (re-search-forward org-target-link-regexp limit t)
  5331. (progn
  5332. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5333. (add-text-properties (match-beginning 0) (match-end 0)
  5334. (list 'mouse-face 'highlight
  5335. 'keymap org-mouse-map
  5336. 'help-echo "Radio target link"
  5337. 'org-linked-text t))
  5338. (org-rear-nonsticky-at (match-end 0))
  5339. t)))))
  5340. (defun org-update-radio-target-regexp ()
  5341. "Find all radio targets in this file and update the regular expression."
  5342. (interactive)
  5343. (when (memq 'radio org-activate-links)
  5344. (setq org-target-link-regexp
  5345. (org-make-target-link-regexp (org-all-targets 'radio)))
  5346. (org-restart-font-lock)))
  5347. (defun org-hide-wide-columns (limit)
  5348. (let (s e)
  5349. (setq s (text-property-any (point) (or limit (point-max))
  5350. 'org-cwidth t))
  5351. (when s
  5352. (setq e (next-single-property-change s 'org-cwidth))
  5353. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  5354. (goto-char e)
  5355. t)))
  5356. (defvar org-latex-and-related-regexp nil
  5357. "Regular expression for highlighting LaTeX, entities and sub/superscript.")
  5358. (defvar org-match-substring-regexp)
  5359. (defvar org-match-substring-with-braces-regexp)
  5360. (defun org-compute-latex-and-related-regexp ()
  5361. "Compute regular expression for LaTeX, entities and sub/superscript.
  5362. Result depends on variable `org-highlight-latex-and-related'."
  5363. (org-set-local
  5364. 'org-latex-and-related-regexp
  5365. (let* ((re-sub
  5366. (cond ((not (memq 'script org-highlight-latex-and-related)) nil)
  5367. ((eq org-use-sub-superscripts '{})
  5368. (list org-match-substring-with-braces-regexp))
  5369. (org-use-sub-superscripts (list org-match-substring-regexp))))
  5370. (re-latex
  5371. (when (memq 'latex org-highlight-latex-and-related)
  5372. (let ((matchers (plist-get org-format-latex-options :matchers)))
  5373. (delq nil
  5374. (mapcar (lambda (x)
  5375. (and (member (car x) matchers) (nth 1 x)))
  5376. org-latex-regexps)))))
  5377. (re-entities
  5378. (when (memq 'entities org-highlight-latex-and-related)
  5379. (list "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))))
  5380. (mapconcat 'identity (append re-latex re-entities re-sub) "\\|"))))
  5381. (defun org-do-latex-and-related (limit)
  5382. "Highlight LaTeX snippets and environments, entities and sub/superscript.
  5383. LIMIT bounds the search for syntax to highlight. Stop at first
  5384. highlighted object, if any. Return t if some highlighting was
  5385. done, nil otherwise."
  5386. (when (org-string-nw-p org-latex-and-related-regexp)
  5387. (catch 'found
  5388. (while (re-search-forward org-latex-and-related-regexp limit t)
  5389. (unless (memq (car-safe (get-text-property (1+ (match-beginning 0))
  5390. 'face))
  5391. '(org-code org-verbatim underline))
  5392. (let ((offset (if (memq (char-after (1+ (match-beginning 0)))
  5393. '(?_ ?^))
  5394. 1
  5395. 0)))
  5396. (font-lock-prepend-text-property
  5397. (+ offset (match-beginning 0)) (match-end 0)
  5398. 'face 'org-latex-and-related)
  5399. (add-text-properties (+ offset (match-beginning 0)) (match-end 0)
  5400. '(font-lock-multiline t)))
  5401. (throw 'found t)))
  5402. nil)))
  5403. (defun org-restart-font-lock ()
  5404. "Restart `font-lock-mode', to force refontification."
  5405. (when (and (boundp 'font-lock-mode) font-lock-mode)
  5406. (font-lock-mode -1)
  5407. (font-lock-mode 1)))
  5408. (defun org-all-targets (&optional radio)
  5409. "Return a list of all targets in this file.
  5410. When optional argument RADIO is non-nil, only find radio
  5411. targets."
  5412. (let ((re (if radio org-radio-target-regexp org-target-regexp)) rtn)
  5413. (save-excursion
  5414. (goto-char (point-min))
  5415. (while (re-search-forward re nil t)
  5416. ;; Make sure point is really within the object.
  5417. (backward-char)
  5418. (let ((obj (org-element-context)))
  5419. (when (memq (org-element-type obj) '(radio-target target))
  5420. (add-to-list 'rtn (downcase (org-element-property :value obj))))))
  5421. rtn)))
  5422. (defun org-make-target-link-regexp (targets)
  5423. "Make regular expression matching all strings in TARGETS.
  5424. The regular expression finds the targets also if there is a line break
  5425. between words."
  5426. (and targets
  5427. (concat
  5428. "\\<\\("
  5429. (mapconcat
  5430. (lambda (x)
  5431. (setq x (regexp-quote x))
  5432. (while (string-match " +" x)
  5433. (setq x (replace-match "\\s-+" t t x)))
  5434. x)
  5435. targets
  5436. "\\|")
  5437. "\\)\\>")))
  5438. (defun org-activate-tags (limit)
  5439. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  5440. (progn
  5441. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5442. (add-text-properties (match-beginning 1) (match-end 1)
  5443. (list 'mouse-face 'highlight
  5444. 'keymap org-mouse-map))
  5445. (org-rear-nonsticky-at (match-end 1))
  5446. t)))
  5447. (defun org-outline-level ()
  5448. "Compute the outline level of the heading at point.
  5449. If this is called at a normal headline, the level is the number of stars.
  5450. Use `org-reduced-level' to remove the effect of `org-odd-levels'."
  5451. (save-excursion
  5452. (if (not (condition-case nil
  5453. (org-back-to-heading t)
  5454. (error nil)))
  5455. 0
  5456. (looking-at org-outline-regexp)
  5457. (1- (- (match-end 0) (match-beginning 0))))))
  5458. (defvar org-font-lock-keywords nil)
  5459. (defconst org-property-re
  5460. "^\\(?4:[ \t]*\\)\\(?1::\\(?2:.*?\\):\\)[ \t]+\\(?3:[^ \t\r\n].*?\\)\\(?5:[ \t]*\\)$"
  5461. "Regular expression matching a property line.
  5462. There are four matching groups:
  5463. 1: :PROPKEY: including the leading and trailing colon,
  5464. 2: PROPKEY without the leading and trailing colon,
  5465. 3: PROPVAL without leading or trailing spaces,
  5466. 4: the indentation of the current line,
  5467. 5: trailing whitespace.")
  5468. (defvar org-font-lock-hook nil
  5469. "Functions to be called for special font lock stuff.")
  5470. (defvar org-font-lock-set-keywords-hook nil
  5471. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5472. This is called after `org-font-lock-extra-keywords' is defined, but before
  5473. it is installed to be used by font lock. This can be useful if something
  5474. needs to be inserted at a specific position in the font-lock sequence.")
  5475. (defun org-font-lock-hook (limit)
  5476. "Run `org-font-lock-hook' within LIMIT."
  5477. (run-hook-with-args 'org-font-lock-hook limit))
  5478. (defun org-set-font-lock-defaults ()
  5479. "Set font lock defaults for the current buffer."
  5480. (let* ((em org-fontify-emphasized-text)
  5481. (lk org-activate-links)
  5482. (org-font-lock-extra-keywords
  5483. (list
  5484. ;; Call the hook
  5485. '(org-font-lock-hook)
  5486. ;; Headlines
  5487. `(,(if org-fontify-whole-heading-line
  5488. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5489. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5490. (1 (org-get-level-face 1))
  5491. (2 (org-get-level-face 2))
  5492. (3 (org-get-level-face 3)))
  5493. ;; Table lines
  5494. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5495. (1 'org-table t))
  5496. ;; Table internals
  5497. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5498. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5499. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5500. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5501. ;; Drawers
  5502. (list org-drawer-regexp '(0 'org-special-keyword t))
  5503. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  5504. ;; Properties
  5505. (list org-property-re
  5506. '(1 'org-special-keyword t)
  5507. '(3 'org-property-value t))
  5508. ;; Links
  5509. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5510. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5511. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  5512. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5513. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  5514. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5515. (if (memq 'footnote lk) '(org-activate-footnote-links))
  5516. ;; Targets.
  5517. (list org-any-target-regexp '(0 'org-target t))
  5518. ;; Diary sexps.
  5519. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5520. ;; Macro
  5521. '("{{{.+}}}" (0 'org-macro t))
  5522. '(org-hide-wide-columns (0 nil append))
  5523. ;; TODO keyword
  5524. (list (format org-heading-keyword-regexp-format
  5525. org-todo-regexp)
  5526. '(2 (org-get-todo-face 2) t))
  5527. ;; DONE
  5528. (if org-fontify-done-headline
  5529. (list (format org-heading-keyword-regexp-format
  5530. (concat
  5531. "\\(?:"
  5532. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5533. "\\)"))
  5534. '(2 'org-headline-done t))
  5535. nil)
  5536. ;; Priorities
  5537. '(org-font-lock-add-priority-faces)
  5538. ;; Tags
  5539. '(org-font-lock-add-tag-faces)
  5540. ;; Tags groups
  5541. (if (and org-group-tags org-tag-groups-alist)
  5542. (list (concat org-outline-regexp-bol ".+\\(:"
  5543. (regexp-opt (mapcar 'car org-tag-groups-alist))
  5544. ":\\).*$")
  5545. '(1 'org-tag-group prepend)))
  5546. ;; Special keywords
  5547. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5548. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5549. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5550. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5551. ;; Emphasis
  5552. (if em
  5553. (if (featurep 'xemacs)
  5554. '(org-do-emphasis-faces (0 nil append))
  5555. '(org-do-emphasis-faces)))
  5556. ;; Checkboxes
  5557. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5558. 1 'org-checkbox prepend)
  5559. (if (cdr (assq 'checkbox org-list-automatic-rules))
  5560. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5561. (0 (org-get-checkbox-statistics-face) t)))
  5562. ;; Description list items
  5563. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5564. 1 'org-list-dt prepend)
  5565. ;; ARCHIVEd headings
  5566. (list (concat
  5567. org-outline-regexp-bol
  5568. "\\(.*:" org-archive-tag ":.*\\)")
  5569. '(1 'org-archived prepend))
  5570. ;; Specials
  5571. '(org-do-latex-and-related)
  5572. '(org-fontify-entities)
  5573. '(org-raise-scripts)
  5574. ;; Code
  5575. '(org-activate-code (1 'org-code t))
  5576. ;; COMMENT
  5577. (list (format org-heading-keyword-regexp-format
  5578. (concat "\\("
  5579. org-comment-string "\\|" org-quote-string
  5580. "\\)"))
  5581. '(2 'org-special-keyword t))
  5582. ;; Blocks and meta lines
  5583. '(org-fontify-meta-lines-and-blocks))))
  5584. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5585. (run-hooks 'org-font-lock-set-keywords-hook)
  5586. ;; Now set the full font-lock-keywords
  5587. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5588. (org-set-local 'font-lock-defaults
  5589. '(org-font-lock-keywords t nil nil backward-paragraph))
  5590. (kill-local-variable 'font-lock-keywords) nil))
  5591. (defun org-toggle-pretty-entities ()
  5592. "Toggle the composition display of entities as UTF8 characters."
  5593. (interactive)
  5594. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5595. (org-restart-font-lock)
  5596. (if org-pretty-entities
  5597. (message "Entities are now displayed as UTF8 characters")
  5598. (save-restriction
  5599. (widen)
  5600. (org-decompose-region (point-min) (point-max))
  5601. (message "Entities are now displayed as plain text"))))
  5602. (defvar org-custom-properties-overlays nil
  5603. "List of overlays used for custom properties.")
  5604. (make-variable-buffer-local 'org-custom-properties-overlays)
  5605. (defun org-toggle-custom-properties-visibility ()
  5606. "Display or hide properties in `org-custom-properties'."
  5607. (interactive)
  5608. (if org-custom-properties-overlays
  5609. (progn (mapc 'delete-overlay org-custom-properties-overlays)
  5610. (setq org-custom-properties-overlays nil))
  5611. (unless (not org-custom-properties)
  5612. (save-excursion
  5613. (save-restriction
  5614. (widen)
  5615. (goto-char (point-min))
  5616. (while (re-search-forward org-property-re nil t)
  5617. (mapc (lambda(p)
  5618. (when (equal p (substring (match-string 1) 1 -1))
  5619. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5620. (overlay-put o 'invisible t)
  5621. (overlay-put o 'org-custom-property t)
  5622. (push o org-custom-properties-overlays))))
  5623. org-custom-properties)))))))
  5624. (defun org-fontify-entities (limit)
  5625. "Find an entity to fontify."
  5626. (let (ee)
  5627. (when org-pretty-entities
  5628. (catch 'match
  5629. (while (re-search-forward
  5630. "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
  5631. limit t)
  5632. (if (and (not (org-in-indented-comment-line))
  5633. (setq ee (org-entity-get (match-string 1)))
  5634. (= (length (nth 6 ee)) 1))
  5635. (let*
  5636. ((end (if (equal (match-string 2) "{}")
  5637. (match-end 2)
  5638. (match-end 1))))
  5639. (add-text-properties
  5640. (match-beginning 0) end
  5641. (list 'font-lock-fontified t))
  5642. (compose-region (match-beginning 0) end
  5643. (nth 6 ee) nil)
  5644. (backward-char 1)
  5645. (throw 'match t))))
  5646. nil))))
  5647. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5648. "Fontify string S like in Org-mode."
  5649. (with-temp-buffer
  5650. (insert s)
  5651. (let ((org-odd-levels-only odd-levels))
  5652. (org-mode)
  5653. (font-lock-fontify-buffer)
  5654. (buffer-string))))
  5655. (defvar org-m nil)
  5656. (defvar org-l nil)
  5657. (defvar org-f nil)
  5658. (defun org-get-level-face (n)
  5659. "Get the right face for match N in font-lock matching of headlines."
  5660. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5661. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5662. (if org-cycle-level-faces
  5663. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5664. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5665. (cond
  5666. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5667. ((eq n 2) org-f)
  5668. (t (if org-level-color-stars-only nil org-f))))
  5669. (defun org-get-todo-face (kwd)
  5670. "Get the right face for a TODO keyword KWD.
  5671. If KWD is a number, get the corresponding match group."
  5672. (if (numberp kwd) (setq kwd (match-string kwd)))
  5673. (or (org-face-from-face-or-color
  5674. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5675. (and (member kwd org-done-keywords) 'org-done)
  5676. 'org-todo))
  5677. (defun org-face-from-face-or-color (context inherit face-or-color)
  5678. "Create a face list that inherits INHERIT, but sets the foreground color.
  5679. When FACE-OR-COLOR is not a string, just return it."
  5680. (if (stringp face-or-color)
  5681. (list :inherit inherit
  5682. (cdr (assoc context org-faces-easy-properties))
  5683. face-or-color)
  5684. face-or-color))
  5685. (defun org-font-lock-add-tag-faces (limit)
  5686. "Add the special tag faces."
  5687. (when (and org-tag-faces org-tags-special-faces-re)
  5688. (while (re-search-forward org-tags-special-faces-re limit t)
  5689. (add-text-properties (match-beginning 1) (match-end 1)
  5690. (list 'face (org-get-tag-face 1)
  5691. 'font-lock-fontified t))
  5692. (backward-char 1))))
  5693. (defun org-font-lock-add-priority-faces (limit)
  5694. "Add the special priority faces."
  5695. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  5696. (when (save-match-data (org-at-heading-p))
  5697. (add-text-properties
  5698. (match-beginning 0) (match-end 0)
  5699. (list 'face (or (org-face-from-face-or-color
  5700. 'priority 'org-priority
  5701. (cdr (assoc (char-after (match-beginning 1))
  5702. org-priority-faces)))
  5703. 'org-priority)
  5704. 'font-lock-fontified t)))))
  5705. (defun org-get-tag-face (kwd)
  5706. "Get the right face for a TODO keyword KWD.
  5707. If KWD is a number, get the corresponding match group."
  5708. (if (numberp kwd) (setq kwd (match-string kwd)))
  5709. (or (org-face-from-face-or-color
  5710. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  5711. 'org-tag))
  5712. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5713. "Remove fontification and activation overlays from links."
  5714. (font-lock-default-unfontify-region beg end)
  5715. (let* ((buffer-undo-list t)
  5716. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5717. (inhibit-modification-hooks t)
  5718. deactivate-mark buffer-file-name buffer-file-truename)
  5719. (org-decompose-region beg end)
  5720. (remove-text-properties beg end
  5721. '(mouse-face t keymap t org-linked-text t
  5722. invisible t intangible t
  5723. org-no-flyspell t org-emphasis t))
  5724. (org-remove-font-lock-display-properties beg end)))
  5725. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5726. ((raise 0.3) (height 0.7))
  5727. ((raise -0.5))
  5728. ((raise 0.5)))
  5729. "Display properties for showing superscripts and subscripts.")
  5730. (defun org-remove-font-lock-display-properties (beg end)
  5731. "Remove specific display properties that have been added by font lock.
  5732. The will remove the raise properties that are used to show superscripts
  5733. and subscripts."
  5734. (let (next prop)
  5735. (while (< beg end)
  5736. (setq next (next-single-property-change beg 'display nil end)
  5737. prop (get-text-property beg 'display))
  5738. (if (member prop org-script-display)
  5739. (put-text-property beg next 'display nil))
  5740. (setq beg next))))
  5741. (defun org-raise-scripts (limit)
  5742. "Add raise properties to sub/superscripts."
  5743. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5744. (if (re-search-forward
  5745. (if (eq org-use-sub-superscripts t)
  5746. org-match-substring-regexp
  5747. org-match-substring-with-braces-regexp)
  5748. limit t)
  5749. (let* ((pos (point)) table-p comment-p
  5750. (mpos (match-beginning 3))
  5751. (emph-p (get-text-property mpos 'org-emphasis))
  5752. (link-p (get-text-property mpos 'mouse-face))
  5753. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5754. (goto-char (point-at-bol))
  5755. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5756. comment-p (org-looking-at-p "^[ \t]*#[ +]"))
  5757. (goto-char pos)
  5758. ;; Handle a_b^c
  5759. (if (member (char-after) '(?_ ?^)) (goto-char (1- pos)))
  5760. (if (or comment-p emph-p link-p keyw-p)
  5761. t
  5762. (put-text-property (match-beginning 3) (match-end 0)
  5763. 'display
  5764. (if (equal (char-after (match-beginning 2)) ?^)
  5765. (nth (if table-p 3 1) org-script-display)
  5766. (nth (if table-p 2 0) org-script-display)))
  5767. (add-text-properties (match-beginning 2) (match-end 2)
  5768. (list 'invisible t
  5769. 'org-dwidth t 'org-dwidth-n 1))
  5770. (if (and (eq (char-after (match-beginning 3)) ?{)
  5771. (eq (char-before (match-end 3)) ?}))
  5772. (progn
  5773. (add-text-properties
  5774. (match-beginning 3) (1+ (match-beginning 3))
  5775. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5776. (add-text-properties
  5777. (1- (match-end 3)) (match-end 3)
  5778. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5779. t)))))
  5780. ;;;; Visibility cycling, including org-goto and indirect buffer
  5781. ;;; Cycling
  5782. (defvar org-cycle-global-status nil)
  5783. (make-variable-buffer-local 'org-cycle-global-status)
  5784. (put 'org-cycle-global-status 'org-state t)
  5785. (defvar org-cycle-subtree-status nil)
  5786. (make-variable-buffer-local 'org-cycle-subtree-status)
  5787. (put 'org-cycle-subtree-status 'org-state t)
  5788. (defvar org-inlinetask-min-level)
  5789. ;;;###autoload
  5790. (defun org-cycle (&optional arg)
  5791. "TAB-action and visibility cycling for Org-mode.
  5792. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5793. is outline visibility cycling, but it also invokes other actions
  5794. in special contexts.
  5795. - When this function is called with a prefix argument, rotate the entire
  5796. buffer through 3 states (global cycling)
  5797. 1. OVERVIEW: Show only top-level headlines.
  5798. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5799. 3. SHOW ALL: Show everything.
  5800. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5801. determined by the variable `org-startup-folded', and by any VISIBILITY
  5802. properties in the buffer.
  5803. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5804. including any drawers.
  5805. - When inside a table, re-align the table and move to the next field.
  5806. - When point is at the beginning of a headline, rotate the subtree started
  5807. by this line through 3 different states (local cycling)
  5808. 1. FOLDED: Only the main headline is shown.
  5809. 2. CHILDREN: The main headline and the direct children are shown.
  5810. From this state, you can move to one of the children
  5811. and zoom in further.
  5812. 3. SUBTREE: Show the entire subtree, including body text.
  5813. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5814. - When point is at the beginning of an empty headline and the variable
  5815. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5816. of the headline by demoting and promoting it to likely levels. This
  5817. speeds up creation document structure by pressing TAB once or several
  5818. times right after creating a new headline.
  5819. - When there is a numeric prefix, go up to a heading with level ARG, do
  5820. a `show-subtree' and return to the previous cursor position. If ARG
  5821. is negative, go up that many levels.
  5822. - When point is not at the beginning of a headline, execute the global
  5823. binding for TAB, which is re-indenting the line. See the option
  5824. `org-cycle-emulate-tab' for details.
  5825. - Special case: if point is at the beginning of the buffer and there is
  5826. no headline in line 1, this function will act as if called with prefix arg
  5827. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5828. But only if also the variable `org-cycle-global-at-bob' is t."
  5829. (interactive "P")
  5830. (org-load-modules-maybe)
  5831. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5832. (and org-cycle-level-after-item/entry-creation
  5833. (or (org-cycle-level)
  5834. (org-cycle-item-indentation))))
  5835. (let* (message-log-max ; Don't populate the *Messages* buffer
  5836. (limit-level
  5837. (or org-cycle-max-level
  5838. (and (boundp 'org-inlinetask-min-level)
  5839. org-inlinetask-min-level
  5840. (1- org-inlinetask-min-level))))
  5841. (nstars (and limit-level
  5842. (if org-odd-levels-only
  5843. (and limit-level (1- (* limit-level 2)))
  5844. limit-level)))
  5845. (org-outline-regexp
  5846. (if (not (derived-mode-p 'org-mode))
  5847. outline-regexp
  5848. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5849. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5850. (not (looking-at org-outline-regexp))))
  5851. (org-cycle-hook
  5852. (if bob-special
  5853. (delq 'org-optimize-window-after-visibility-change
  5854. (copy-sequence org-cycle-hook))
  5855. org-cycle-hook))
  5856. (pos (point)))
  5857. (if (or bob-special (equal arg '(4)))
  5858. ;; special case: use global cycling
  5859. (setq arg t))
  5860. (cond
  5861. ((equal arg '(16))
  5862. (setq last-command 'dummy)
  5863. (org-set-startup-visibility)
  5864. (message "Startup visibility, plus VISIBILITY properties"))
  5865. ((equal arg '(64))
  5866. (show-all)
  5867. (message "Entire buffer visible, including drawers"))
  5868. ;; Table: enter it or move to the next field.
  5869. ((org-at-table-p 'any)
  5870. (if (org-at-table.el-p)
  5871. (message "Use C-c ' to edit table.el tables")
  5872. (if arg (org-table-edit-field t)
  5873. (org-table-justify-field-maybe)
  5874. (call-interactively 'org-table-next-field))))
  5875. ((run-hook-with-args-until-success
  5876. 'org-tab-after-check-for-table-hook))
  5877. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5878. ((eq arg t) (org-cycle-internal-global))
  5879. ;; Drawers: delegate to `org-flag-drawer'.
  5880. ((and org-drawers org-drawer-regexp
  5881. (save-excursion
  5882. (beginning-of-line 1)
  5883. (looking-at org-drawer-regexp)))
  5884. (org-flag-drawer ; toggle block visibility
  5885. (not (get-char-property (match-end 0) 'invisible))))
  5886. ;; Show-subtree, ARG levels up from here.
  5887. ((integerp arg)
  5888. (save-excursion
  5889. (org-back-to-heading)
  5890. (outline-up-heading (if (< arg 0) (- arg)
  5891. (- (funcall outline-level) arg)))
  5892. (org-show-subtree)))
  5893. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5894. ((and (featurep 'org-inlinetask)
  5895. (org-inlinetask-at-task-p)
  5896. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5897. (org-inlinetask-toggle-visibility))
  5898. ((org-try-cdlatex-tab))
  5899. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5900. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5901. (save-excursion (beginning-of-line 1)
  5902. (looking-at org-outline-regexp)))
  5903. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5904. (org-cycle-internal-local))
  5905. ;; From there: TAB emulation and template completion.
  5906. (buffer-read-only (org-back-to-heading))
  5907. ((run-hook-with-args-until-success
  5908. 'org-tab-after-check-for-cycling-hook))
  5909. ((org-try-structure-completion))
  5910. ((run-hook-with-args-until-success
  5911. 'org-tab-before-tab-emulation-hook))
  5912. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5913. (or (not (bolp))
  5914. (not (looking-at org-outline-regexp))))
  5915. (call-interactively (global-key-binding "\t")))
  5916. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5917. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5918. (or (and (eq org-cycle-emulate-tab 'white)
  5919. (= (match-end 0) (point-at-eol)))
  5920. (and (eq org-cycle-emulate-tab 'whitestart)
  5921. (>= (match-end 0) pos))))
  5922. t
  5923. (eq org-cycle-emulate-tab t))
  5924. (call-interactively (global-key-binding "\t")))
  5925. (t (save-excursion
  5926. (org-back-to-heading)
  5927. (org-cycle)))))))
  5928. (defun org-cycle-internal-global ()
  5929. "Do the global cycling action."
  5930. ;; Hack to avoid display of messages for .org attachments in Gnus
  5931. (let (message-log-max ; Don't populate the *Messages* buffer
  5932. (ga (string-match "\\*fontification" (buffer-name))))
  5933. (cond
  5934. ((and (eq last-command this-command)
  5935. (eq org-cycle-global-status 'overview))
  5936. ;; We just created the overview - now do table of contents
  5937. ;; This can be slow in very large buffers, so indicate action
  5938. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  5939. (unless ga (message "CONTENTS..."))
  5940. (org-content)
  5941. (unless ga (message "CONTENTS...done"))
  5942. (setq org-cycle-global-status 'contents)
  5943. (run-hook-with-args 'org-cycle-hook 'contents))
  5944. ((and (eq last-command this-command)
  5945. (eq org-cycle-global-status 'contents))
  5946. ;; We just showed the table of contents - now show everything
  5947. (run-hook-with-args 'org-pre-cycle-hook 'all)
  5948. (show-all)
  5949. (unless ga (message "SHOW ALL"))
  5950. (setq org-cycle-global-status 'all)
  5951. (run-hook-with-args 'org-cycle-hook 'all))
  5952. (t
  5953. ;; Default action: go to overview
  5954. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  5955. (org-overview)
  5956. (unless ga (message "OVERVIEW"))
  5957. (setq org-cycle-global-status 'overview)
  5958. (run-hook-with-args 'org-cycle-hook 'overview)))))
  5959. (defun org-cycle-internal-local ()
  5960. "Do the local cycling action."
  5961. (let (message-log-max ; Don't populate the *Messages* buffer
  5962. (goal-column 0) eoh eol eos has-children children-skipped struct)
  5963. ;; First, determine end of headline (EOH), end of subtree or item
  5964. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  5965. (save-excursion
  5966. (if (org-at-item-p)
  5967. (progn
  5968. (beginning-of-line)
  5969. (setq struct (org-list-struct))
  5970. (setq eoh (point-at-eol))
  5971. (setq eos (org-list-get-item-end-before-blank (point) struct))
  5972. (setq has-children (org-list-has-child-p (point) struct)))
  5973. (org-back-to-heading)
  5974. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  5975. (setq eos (save-excursion (1- (org-end-of-subtree t t))))
  5976. (setq has-children
  5977. (or (save-excursion
  5978. (let ((level (funcall outline-level)))
  5979. (outline-next-heading)
  5980. (and (org-at-heading-p t)
  5981. (> (funcall outline-level) level))))
  5982. (save-excursion
  5983. (org-list-search-forward (org-item-beginning-re) eos t)))))
  5984. ;; Determine end invisible part of buffer (EOL)
  5985. (beginning-of-line 2)
  5986. ;; XEmacs doesn't have `next-single-char-property-change'
  5987. (if (featurep 'xemacs)
  5988. (while (and (not (eobp)) ;; this is like `next-line'
  5989. (get-char-property (1- (point)) 'invisible))
  5990. (beginning-of-line 2))
  5991. (while (and (not (eobp)) ;; this is like `next-line'
  5992. (get-char-property (1- (point)) 'invisible))
  5993. (goto-char (next-single-char-property-change (point) 'invisible))
  5994. (and (eolp) (beginning-of-line 2))))
  5995. (setq eol (point)))
  5996. ;; Find out what to do next and set `this-command'
  5997. (cond
  5998. ((= eos eoh)
  5999. ;; Nothing is hidden behind this heading
  6000. (unless (org-before-first-heading-p)
  6001. (run-hook-with-args 'org-pre-cycle-hook 'empty))
  6002. (message "EMPTY ENTRY")
  6003. (setq org-cycle-subtree-status nil)
  6004. (save-excursion
  6005. (goto-char eos)
  6006. (outline-next-heading)
  6007. (if (outline-invisible-p) (org-flag-heading nil))))
  6008. ((and (or (>= eol eos)
  6009. (not (string-match "\\S-" (buffer-substring eol eos))))
  6010. (or has-children
  6011. (not (setq children-skipped
  6012. org-cycle-skip-children-state-if-no-children))))
  6013. ;; Entire subtree is hidden in one line: children view
  6014. (unless (org-before-first-heading-p)
  6015. (run-hook-with-args 'org-pre-cycle-hook 'children))
  6016. (if (org-at-item-p)
  6017. (org-list-set-item-visibility (point-at-bol) struct 'children)
  6018. (org-show-entry)
  6019. (org-with-limited-levels (show-children))
  6020. ;; FIXME: This slows down the func way too much.
  6021. ;; How keep drawers hidden in subtree anyway?
  6022. ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
  6023. ;; (org-cycle-hide-drawers 'subtree))
  6024. ;; Fold every list in subtree to top-level items.
  6025. (when (eq org-cycle-include-plain-lists 'integrate)
  6026. (save-excursion
  6027. (org-back-to-heading)
  6028. (while (org-list-search-forward (org-item-beginning-re) eos t)
  6029. (beginning-of-line 1)
  6030. (let* ((struct (org-list-struct))
  6031. (prevs (org-list-prevs-alist struct))
  6032. (end (org-list-get-bottom-point struct)))
  6033. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  6034. (org-list-get-all-items (point) struct prevs))
  6035. (goto-char (if (< end eos) end eos)))))))
  6036. (message "CHILDREN")
  6037. (save-excursion
  6038. (goto-char eos)
  6039. (outline-next-heading)
  6040. (if (outline-invisible-p) (org-flag-heading nil)))
  6041. (setq org-cycle-subtree-status 'children)
  6042. (unless (org-before-first-heading-p)
  6043. (run-hook-with-args 'org-cycle-hook 'children)))
  6044. ((or children-skipped
  6045. (and (eq last-command this-command)
  6046. (eq org-cycle-subtree-status 'children)))
  6047. ;; We just showed the children, or no children are there,
  6048. ;; now show everything.
  6049. (unless (org-before-first-heading-p)
  6050. (run-hook-with-args 'org-pre-cycle-hook 'subtree))
  6051. (outline-flag-region eoh eos nil)
  6052. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  6053. (setq org-cycle-subtree-status 'subtree)
  6054. (unless (org-before-first-heading-p)
  6055. (run-hook-with-args 'org-cycle-hook 'subtree)))
  6056. (t
  6057. ;; Default action: hide the subtree.
  6058. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  6059. (outline-flag-region eoh eos t)
  6060. (message "FOLDED")
  6061. (setq org-cycle-subtree-status 'folded)
  6062. (unless (org-before-first-heading-p)
  6063. (run-hook-with-args 'org-cycle-hook 'folded))))))
  6064. ;;;###autoload
  6065. (defun org-global-cycle (&optional arg)
  6066. "Cycle the global visibility. For details see `org-cycle'.
  6067. With \\[universal-argument] prefix arg, switch to startup visibility.
  6068. With a numeric prefix, show all headlines up to that level."
  6069. (interactive "P")
  6070. (let ((org-cycle-include-plain-lists
  6071. (if (derived-mode-p 'org-mode) org-cycle-include-plain-lists nil)))
  6072. (cond
  6073. ((integerp arg)
  6074. (show-all)
  6075. (hide-sublevels arg)
  6076. (setq org-cycle-global-status 'contents))
  6077. ((equal arg '(4))
  6078. (org-set-startup-visibility)
  6079. (message "Startup visibility, plus VISIBILITY properties."))
  6080. (t
  6081. (org-cycle '(4))))))
  6082. (defun org-set-startup-visibility ()
  6083. "Set the visibility required by startup options and properties."
  6084. (cond
  6085. ((eq org-startup-folded t)
  6086. (org-cycle '(4)))
  6087. ((eq org-startup-folded 'content)
  6088. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  6089. (org-cycle '(4)) (org-cycle '(4)))))
  6090. (unless (eq org-startup-folded 'showeverything)
  6091. (if org-hide-block-startup (org-hide-block-all))
  6092. (org-set-visibility-according-to-property 'no-cleanup)
  6093. (org-cycle-hide-archived-subtrees 'all)
  6094. (org-cycle-hide-drawers 'all)
  6095. (org-cycle-show-empty-lines t)))
  6096. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  6097. "Switch subtree visibilities according to :VISIBILITY: property."
  6098. (interactive)
  6099. (let (org-show-entry-below state)
  6100. (save-excursion
  6101. (goto-char (point-min))
  6102. (while (re-search-forward
  6103. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  6104. nil t)
  6105. (setq state (match-string 1))
  6106. (save-excursion
  6107. (org-back-to-heading t)
  6108. (hide-subtree)
  6109. (org-reveal)
  6110. (cond
  6111. ((equal state '("fold" "folded"))
  6112. (hide-subtree))
  6113. ((equal state "children")
  6114. (org-show-hidden-entry)
  6115. (show-children))
  6116. ((equal state "content")
  6117. (save-excursion
  6118. (save-restriction
  6119. (org-narrow-to-subtree)
  6120. (org-content))))
  6121. ((member state '("all" "showall"))
  6122. (show-subtree)))))
  6123. (unless no-cleanup
  6124. (org-cycle-hide-archived-subtrees 'all)
  6125. (org-cycle-hide-drawers 'all)
  6126. (org-cycle-show-empty-lines 'all)))))
  6127. ;; This function uses outline-regexp instead of the more fundamental
  6128. ;; org-outline-regexp so that org-cycle-global works outside of Org
  6129. ;; buffers, where outline-regexp is needed.
  6130. (defun org-overview ()
  6131. "Switch to overview mode, showing only top-level headlines.
  6132. Really, this shows all headlines with level equal or greater than the level
  6133. of the first headline in the buffer. This is important, because if the
  6134. first headline is not level one, then (hide-sublevels 1) gives confusing
  6135. results."
  6136. (interactive)
  6137. (let ((l (org-current-line))
  6138. (level (save-excursion
  6139. (goto-char (point-min))
  6140. (if (re-search-forward (concat "^" outline-regexp) nil t)
  6141. (progn
  6142. (goto-char (match-beginning 0))
  6143. (funcall outline-level))))))
  6144. (and level (hide-sublevels level))
  6145. (recenter '(4))
  6146. (org-goto-line l)))
  6147. (defun org-content (&optional arg)
  6148. "Show all headlines in the buffer, like a table of contents.
  6149. With numerical argument N, show content up to level N."
  6150. (interactive "P")
  6151. (save-excursion
  6152. ;; Visit all headings and show their offspring
  6153. (and (integerp arg) (org-overview))
  6154. (goto-char (point-max))
  6155. (catch 'exit
  6156. (while (and (progn (condition-case nil
  6157. (outline-previous-visible-heading 1)
  6158. (error (goto-char (point-min))))
  6159. t)
  6160. (looking-at org-outline-regexp))
  6161. (if (integerp arg)
  6162. (show-children (1- arg))
  6163. (show-branches))
  6164. (if (bobp) (throw 'exit nil))))))
  6165. (defun org-optimize-window-after-visibility-change (state)
  6166. "Adjust the window after a change in outline visibility.
  6167. This function is the default value of the hook `org-cycle-hook'."
  6168. (when (get-buffer-window (current-buffer))
  6169. (cond
  6170. ((eq state 'content) nil)
  6171. ((eq state 'all) nil)
  6172. ((eq state 'folded) nil)
  6173. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  6174. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  6175. (defun org-remove-empty-overlays-at (pos)
  6176. "Remove outline overlays that do not contain non-white stuff."
  6177. (mapc
  6178. (lambda (o)
  6179. (and (eq 'outline (overlay-get o 'invisible))
  6180. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  6181. (overlay-end o))))
  6182. (delete-overlay o)))
  6183. (overlays-at pos)))
  6184. (defun org-clean-visibility-after-subtree-move ()
  6185. "Fix visibility issues after moving a subtree."
  6186. ;; First, find a reasonable region to look at:
  6187. ;; Start two siblings above, end three below
  6188. (let* ((beg (save-excursion
  6189. (and (org-get-last-sibling)
  6190. (org-get-last-sibling))
  6191. (point)))
  6192. (end (save-excursion
  6193. (and (org-get-next-sibling)
  6194. (org-get-next-sibling)
  6195. (org-get-next-sibling))
  6196. (if (org-at-heading-p)
  6197. (point-at-eol)
  6198. (point))))
  6199. (level (looking-at "\\*+"))
  6200. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  6201. (save-excursion
  6202. (save-restriction
  6203. (narrow-to-region beg end)
  6204. (when re
  6205. ;; Properly fold already folded siblings
  6206. (goto-char (point-min))
  6207. (while (re-search-forward re nil t)
  6208. (if (and (not (outline-invisible-p))
  6209. (save-excursion
  6210. (goto-char (point-at-eol)) (outline-invisible-p)))
  6211. (hide-entry))))
  6212. (org-cycle-show-empty-lines 'overview)
  6213. (org-cycle-hide-drawers 'overview)))))
  6214. (defun org-cycle-show-empty-lines (state)
  6215. "Show empty lines above all visible headlines.
  6216. The region to be covered depends on STATE when called through
  6217. `org-cycle-hook'. Lisp program can use t for STATE to get the
  6218. entire buffer covered. Note that an empty line is only shown if there
  6219. are at least `org-cycle-separator-lines' empty lines before the headline."
  6220. (when (not (= org-cycle-separator-lines 0))
  6221. (save-excursion
  6222. (let* ((n (abs org-cycle-separator-lines))
  6223. (re (cond
  6224. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  6225. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  6226. (t (let ((ns (number-to-string (- n 2))))
  6227. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  6228. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  6229. beg end b e)
  6230. (cond
  6231. ((memq state '(overview contents t))
  6232. (setq beg (point-min) end (point-max)))
  6233. ((memq state '(children folded))
  6234. (setq beg (point) end (progn (org-end-of-subtree t t)
  6235. (beginning-of-line 2)
  6236. (point)))))
  6237. (when beg
  6238. (goto-char beg)
  6239. (while (re-search-forward re end t)
  6240. (unless (get-char-property (match-end 1) 'invisible)
  6241. (setq e (match-end 1))
  6242. (if (< org-cycle-separator-lines 0)
  6243. (setq b (save-excursion
  6244. (goto-char (match-beginning 0))
  6245. (org-back-over-empty-lines)
  6246. (if (save-excursion
  6247. (goto-char (max (point-min) (1- (point))))
  6248. (org-at-heading-p))
  6249. (1- (point))
  6250. (point))))
  6251. (setq b (match-beginning 1)))
  6252. (outline-flag-region b e nil)))))))
  6253. ;; Never hide empty lines at the end of the file.
  6254. (save-excursion
  6255. (goto-char (point-max))
  6256. (outline-previous-heading)
  6257. (outline-end-of-heading)
  6258. (if (and (looking-at "[ \t\n]+")
  6259. (= (match-end 0) (point-max)))
  6260. (outline-flag-region (point) (match-end 0) nil))))
  6261. (defun org-show-empty-lines-in-parent ()
  6262. "Move to the parent and re-show empty lines before visible headlines."
  6263. (save-excursion
  6264. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  6265. (org-cycle-show-empty-lines context))))
  6266. (defun org-files-list ()
  6267. "Return `org-agenda-files' list, plus all open org-mode files.
  6268. This is useful for operations that need to scan all of a user's
  6269. open and agenda-wise Org files."
  6270. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  6271. (dolist (buf (buffer-list))
  6272. (with-current-buffer buf
  6273. (if (and (derived-mode-p 'org-mode) (buffer-file-name))
  6274. (let ((file (expand-file-name (buffer-file-name))))
  6275. (unless (member file files)
  6276. (push file files))))))
  6277. files))
  6278. (defsubst org-entry-beginning-position ()
  6279. "Return the beginning position of the current entry."
  6280. (save-excursion (outline-back-to-heading t) (point)))
  6281. (defsubst org-entry-end-position ()
  6282. "Return the end position of the current entry."
  6283. (save-excursion (outline-next-heading) (point)))
  6284. (defun org-cycle-hide-drawers (state)
  6285. "Re-hide all drawers after a visibility state change."
  6286. (when (and (derived-mode-p 'org-mode)
  6287. (not (memq state '(overview folded contents))))
  6288. (save-excursion
  6289. (let* ((globalp (memq state '(contents all)))
  6290. (beg (if globalp (point-min) (point)))
  6291. (end (if globalp (point-max)
  6292. (if (eq state 'children)
  6293. (save-excursion (outline-next-heading) (point))
  6294. (org-end-of-subtree t)))))
  6295. (goto-char beg)
  6296. (while (re-search-forward org-drawer-regexp end t)
  6297. (org-flag-drawer t))))))
  6298. (defun org-cycle-hide-inline-tasks (state)
  6299. "Re-hide inline task when switching to 'contents visibility state."
  6300. (when (and (eq state 'contents)
  6301. (boundp 'org-inlinetask-min-level)
  6302. org-inlinetask-min-level)
  6303. (hide-sublevels (1- org-inlinetask-min-level))))
  6304. (defun org-flag-drawer (flag)
  6305. "When FLAG is non-nil, hide the drawer we are within.
  6306. Otherwise make it visible."
  6307. (save-excursion
  6308. (beginning-of-line 1)
  6309. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  6310. (let ((b (match-end 0)))
  6311. (if (re-search-forward
  6312. "^[ \t]*:END:"
  6313. (save-excursion (outline-next-heading) (point)) t)
  6314. (outline-flag-region b (point-at-eol) flag)
  6315. (user-error ":END: line missing at position %s" b))))))
  6316. (defun org-subtree-end-visible-p ()
  6317. "Is the end of the current subtree visible?"
  6318. (pos-visible-in-window-p
  6319. (save-excursion (org-end-of-subtree t) (point))))
  6320. (defun org-first-headline-recenter (&optional N)
  6321. "Move cursor to the first headline and recenter the headline.
  6322. Optional argument N means put the headline into the Nth line of the window."
  6323. (goto-char (point-min))
  6324. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  6325. (beginning-of-line)
  6326. (recenter (prefix-numeric-value N))))
  6327. ;;; Saving and restoring visibility
  6328. (defun org-outline-overlay-data (&optional use-markers)
  6329. "Return a list of the locations of all outline overlays.
  6330. These are overlays with the `invisible' property value `outline'.
  6331. The return value is a list of cons cells, with start and stop
  6332. positions for each overlay.
  6333. If USE-MARKERS is set, return the positions as markers."
  6334. (let (beg end)
  6335. (save-excursion
  6336. (save-restriction
  6337. (widen)
  6338. (delq nil
  6339. (mapcar (lambda (o)
  6340. (when (eq (overlay-get o 'invisible) 'outline)
  6341. (setq beg (overlay-start o)
  6342. end (overlay-end o))
  6343. (and beg end (> end beg)
  6344. (if use-markers
  6345. (cons (move-marker (make-marker) beg)
  6346. (move-marker (make-marker) end))
  6347. (cons beg end)))))
  6348. (overlays-in (point-min) (point-max))))))))
  6349. (defun org-set-outline-overlay-data (data)
  6350. "Create visibility overlays for all positions in DATA.
  6351. DATA should have been made by `org-outline-overlay-data'."
  6352. (let (o)
  6353. (save-excursion
  6354. (save-restriction
  6355. (widen)
  6356. (show-all)
  6357. (mapc (lambda (c)
  6358. (outline-flag-region (car c) (cdr c) t))
  6359. data)))))
  6360. ;;; Folding of blocks
  6361. (defvar org-hide-block-overlays nil
  6362. "Overlays hiding blocks.")
  6363. (make-variable-buffer-local 'org-hide-block-overlays)
  6364. (defun org-block-map (function &optional start end)
  6365. "Call FUNCTION at the head of all source blocks in the current buffer.
  6366. Optional arguments START and END can be used to limit the range."
  6367. (let ((start (or start (point-min)))
  6368. (end (or end (point-max))))
  6369. (save-excursion
  6370. (goto-char start)
  6371. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  6372. (save-excursion
  6373. (save-match-data
  6374. (goto-char (match-beginning 0))
  6375. (funcall function)))))))
  6376. (defun org-hide-block-toggle-all ()
  6377. "Toggle the visibility of all blocks in the current buffer."
  6378. (org-block-map #'org-hide-block-toggle))
  6379. (defun org-hide-block-all ()
  6380. "Fold all blocks in the current buffer."
  6381. (interactive)
  6382. (org-show-block-all)
  6383. (org-block-map #'org-hide-block-toggle-maybe))
  6384. (defun org-show-block-all ()
  6385. "Unfold all blocks in the current buffer."
  6386. (interactive)
  6387. (mapc 'delete-overlay org-hide-block-overlays)
  6388. (setq org-hide-block-overlays nil))
  6389. (defun org-hide-block-toggle-maybe ()
  6390. "Toggle visibility of block at point."
  6391. (interactive)
  6392. (let ((case-fold-search t))
  6393. (if (save-excursion
  6394. (beginning-of-line 1)
  6395. (looking-at org-block-regexp))
  6396. (progn (org-hide-block-toggle)
  6397. t) ;; to signal that we took action
  6398. nil))) ;; to signal that we did not
  6399. (defun org-hide-block-toggle (&optional force)
  6400. "Toggle the visibility of the current block."
  6401. (interactive)
  6402. (save-excursion
  6403. (beginning-of-line)
  6404. (if (re-search-forward org-block-regexp nil t)
  6405. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  6406. (end (match-end 0)) ;; end of entire body
  6407. ov)
  6408. (if (memq t (mapcar (lambda (overlay)
  6409. (eq (overlay-get overlay 'invisible)
  6410. 'org-hide-block))
  6411. (overlays-at start)))
  6412. (if (or (not force) (eq force 'off))
  6413. (mapc (lambda (ov)
  6414. (when (member ov org-hide-block-overlays)
  6415. (setq org-hide-block-overlays
  6416. (delq ov org-hide-block-overlays)))
  6417. (when (eq (overlay-get ov 'invisible)
  6418. 'org-hide-block)
  6419. (delete-overlay ov)))
  6420. (overlays-at start)))
  6421. (setq ov (make-overlay start end))
  6422. (overlay-put ov 'invisible 'org-hide-block)
  6423. ;; make the block accessible to isearch
  6424. (overlay-put
  6425. ov 'isearch-open-invisible
  6426. (lambda (ov)
  6427. (when (member ov org-hide-block-overlays)
  6428. (setq org-hide-block-overlays
  6429. (delq ov org-hide-block-overlays)))
  6430. (when (eq (overlay-get ov 'invisible)
  6431. 'org-hide-block)
  6432. (delete-overlay ov))))
  6433. (push ov org-hide-block-overlays)))
  6434. (user-error "Not looking at a source block"))))
  6435. ;; org-tab-after-check-for-cycling-hook
  6436. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  6437. ;; Remove overlays when changing major mode
  6438. (add-hook 'org-mode-hook
  6439. (lambda () (org-add-hook 'change-major-mode-hook
  6440. 'org-show-block-all 'append 'local)))
  6441. ;;; Org-goto
  6442. (defvar org-goto-window-configuration nil)
  6443. (defvar org-goto-marker nil)
  6444. (defvar org-goto-map)
  6445. (defun org-goto-map ()
  6446. "Set the keymap `org-goto'."
  6447. (setq org-goto-map
  6448. (let ((map (make-sparse-keymap)))
  6449. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command
  6450. mouse-drag-region universal-argument org-occur))
  6451. cmd)
  6452. (while (setq cmd (pop cmds))
  6453. (substitute-key-definition cmd cmd map global-map)))
  6454. (suppress-keymap map)
  6455. (org-defkey map "\C-m" 'org-goto-ret)
  6456. (org-defkey map [(return)] 'org-goto-ret)
  6457. (org-defkey map [(left)] 'org-goto-left)
  6458. (org-defkey map [(right)] 'org-goto-right)
  6459. (org-defkey map [(control ?g)] 'org-goto-quit)
  6460. (org-defkey map "\C-i" 'org-cycle)
  6461. (org-defkey map [(tab)] 'org-cycle)
  6462. (org-defkey map [(down)] 'outline-next-visible-heading)
  6463. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6464. (if org-goto-auto-isearch
  6465. (if (fboundp 'define-key-after)
  6466. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6467. nil)
  6468. (org-defkey map "q" 'org-goto-quit)
  6469. (org-defkey map "n" 'outline-next-visible-heading)
  6470. (org-defkey map "p" 'outline-previous-visible-heading)
  6471. (org-defkey map "f" 'outline-forward-same-level)
  6472. (org-defkey map "b" 'outline-backward-same-level)
  6473. (org-defkey map "u" 'outline-up-heading))
  6474. (org-defkey map "/" 'org-occur)
  6475. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6476. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6477. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6478. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6479. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6480. map)))
  6481. (defconst org-goto-help
  6482. "Browse buffer copy, to find location or copy text.%s
  6483. RET=jump to location C-g=quit and return to previous location
  6484. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6485. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6486. (defun org-goto (&optional alternative-interface)
  6487. "Look up a different location in the current file, keeping current visibility.
  6488. When you want look-up or go to a different location in a
  6489. document, the fastest way is often to fold the entire buffer and
  6490. then dive into the tree. This method has the disadvantage, that
  6491. the previous location will be folded, which may not be what you
  6492. want.
  6493. This command works around this by showing a copy of the current
  6494. buffer in an indirect buffer, in overview mode. You can dive
  6495. into the tree in that copy, use org-occur and incremental search
  6496. to find a location. When pressing RET or `Q', the command
  6497. returns to the original buffer in which the visibility is still
  6498. unchanged. After RET it will also jump to the location selected
  6499. in the indirect buffer and expose the headline hierarchy above.
  6500. With a prefix argument, use the alternative interface: e.g. if
  6501. `org-goto-interface' is 'outline use 'outline-path-completion."
  6502. (interactive "P")
  6503. (org-goto-map)
  6504. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6505. (org-refile-use-outline-path t)
  6506. (org-refile-target-verify-function nil)
  6507. (interface
  6508. (if (not alternative-interface)
  6509. org-goto-interface
  6510. (if (eq org-goto-interface 'outline)
  6511. 'outline-path-completion
  6512. 'outline)))
  6513. (org-goto-start-pos (point))
  6514. (selected-point
  6515. (if (eq interface 'outline)
  6516. (car (org-get-location (current-buffer) org-goto-help))
  6517. (let ((pa (org-refile-get-location "Goto" nil nil t)))
  6518. (org-refile-check-position pa)
  6519. (nth 3 pa)))))
  6520. (if selected-point
  6521. (progn
  6522. (org-mark-ring-push org-goto-start-pos)
  6523. (goto-char selected-point)
  6524. (if (or (outline-invisible-p) (org-invisible-p2))
  6525. (org-show-context 'org-goto)))
  6526. (message "Quit"))))
  6527. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6528. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6529. (defvar org-goto-local-auto-isearch-map) ; defined below
  6530. (defun org-get-location (buf help)
  6531. "Let the user select a location in the Org-mode buffer BUF.
  6532. This function uses a recursive edit. It returns the selected position
  6533. or nil."
  6534. (org-no-popups
  6535. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6536. (isearch-hide-immediately nil)
  6537. (isearch-search-fun-function
  6538. (lambda () 'org-goto-local-search-headings))
  6539. (org-goto-selected-point org-goto-exit-command))
  6540. (save-excursion
  6541. (save-window-excursion
  6542. (delete-other-windows)
  6543. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6544. (org-pop-to-buffer-same-window
  6545. (condition-case nil
  6546. (make-indirect-buffer (current-buffer) "*org-goto*")
  6547. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6548. (with-output-to-temp-buffer "*Org Help*"
  6549. (princ (format help (if org-goto-auto-isearch
  6550. " Just type for auto-isearch."
  6551. " n/p/f/b/u to navigate, q to quit."))))
  6552. (org-fit-window-to-buffer (get-buffer-window "*Org Help*"))
  6553. (setq buffer-read-only nil)
  6554. (let ((org-startup-truncated t)
  6555. (org-startup-folded nil)
  6556. (org-startup-align-all-tables nil))
  6557. (org-mode)
  6558. (org-overview))
  6559. (setq buffer-read-only t)
  6560. (if (and (boundp 'org-goto-start-pos)
  6561. (integer-or-marker-p org-goto-start-pos))
  6562. (let ((org-show-hierarchy-above t)
  6563. (org-show-siblings t)
  6564. (org-show-following-heading t))
  6565. (goto-char org-goto-start-pos)
  6566. (and (outline-invisible-p) (org-show-context)))
  6567. (goto-char (point-min)))
  6568. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6569. (message "Select location and press RET")
  6570. (use-local-map org-goto-map)
  6571. (recursive-edit)))
  6572. (kill-buffer "*org-goto*")
  6573. (cons org-goto-selected-point org-goto-exit-command))))
  6574. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6575. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6576. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6577. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  6578. (defun org-goto-local-search-headings (string bound noerror)
  6579. "Search and make sure that any matches are in headlines."
  6580. (catch 'return
  6581. (while (if isearch-forward
  6582. (search-forward string bound noerror)
  6583. (search-backward string bound noerror))
  6584. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  6585. (and (member :headline context)
  6586. (not (member :tags context))))
  6587. (throw 'return (point))))))
  6588. (defun org-goto-local-auto-isearch ()
  6589. "Start isearch."
  6590. (interactive)
  6591. (goto-char (point-min))
  6592. (let ((keys (this-command-keys)))
  6593. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6594. (isearch-mode t)
  6595. (isearch-process-search-char (string-to-char keys)))))
  6596. (defun org-goto-ret (&optional arg)
  6597. "Finish `org-goto' by going to the new location."
  6598. (interactive "P")
  6599. (setq org-goto-selected-point (point)
  6600. org-goto-exit-command 'return)
  6601. (throw 'exit nil))
  6602. (defun org-goto-left ()
  6603. "Finish `org-goto' by going to the new location."
  6604. (interactive)
  6605. (if (org-at-heading-p)
  6606. (progn
  6607. (beginning-of-line 1)
  6608. (setq org-goto-selected-point (point)
  6609. org-goto-exit-command 'left)
  6610. (throw 'exit nil))
  6611. (user-error "Not on a heading")))
  6612. (defun org-goto-right ()
  6613. "Finish `org-goto' by going to the new location."
  6614. (interactive)
  6615. (if (org-at-heading-p)
  6616. (progn
  6617. (setq org-goto-selected-point (point)
  6618. org-goto-exit-command 'right)
  6619. (throw 'exit nil))
  6620. (user-error "Not on a heading")))
  6621. (defun org-goto-quit ()
  6622. "Finish `org-goto' without cursor motion."
  6623. (interactive)
  6624. (setq org-goto-selected-point nil)
  6625. (setq org-goto-exit-command 'quit)
  6626. (throw 'exit nil))
  6627. ;;; Indirect buffer display of subtrees
  6628. (defvar org-indirect-dedicated-frame nil
  6629. "This is the frame being used for indirect tree display.")
  6630. (defvar org-last-indirect-buffer nil)
  6631. (defun org-tree-to-indirect-buffer (&optional arg)
  6632. "Create indirect buffer and narrow it to current subtree.
  6633. With a numerical prefix ARG, go up to this level and then take that tree.
  6634. If ARG is negative, go up that many levels.
  6635. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6636. indirect buffer previously made with this command, to avoid proliferation of
  6637. indirect buffers. However, when you call the command with a \
  6638. \\[universal-argument] prefix, or
  6639. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6640. is kept so that you can work with several indirect buffers at the same time.
  6641. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6642. \\[universal-argument] prefix also
  6643. requests that a new frame be made for the new buffer, so that the dedicated
  6644. frame is not changed."
  6645. (interactive "P")
  6646. (let ((cbuf (current-buffer))
  6647. (cwin (selected-window))
  6648. (pos (point))
  6649. beg end level heading ibuf)
  6650. (save-excursion
  6651. (org-back-to-heading t)
  6652. (when (numberp arg)
  6653. (setq level (org-outline-level))
  6654. (if (< arg 0) (setq arg (+ level arg)))
  6655. (while (> (setq level (org-outline-level)) arg)
  6656. (org-up-heading-safe)))
  6657. (setq beg (point)
  6658. heading (org-get-heading))
  6659. (org-end-of-subtree t t)
  6660. (if (org-at-heading-p) (backward-char 1))
  6661. (setq end (point)))
  6662. (if (and (buffer-live-p org-last-indirect-buffer)
  6663. (not (eq org-indirect-buffer-display 'new-frame))
  6664. (not arg))
  6665. (kill-buffer org-last-indirect-buffer))
  6666. (setq ibuf (org-get-indirect-buffer cbuf)
  6667. org-last-indirect-buffer ibuf)
  6668. (cond
  6669. ((or (eq org-indirect-buffer-display 'new-frame)
  6670. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6671. (select-frame (make-frame))
  6672. (delete-other-windows)
  6673. (org-pop-to-buffer-same-window ibuf)
  6674. (org-set-frame-title heading))
  6675. ((eq org-indirect-buffer-display 'dedicated-frame)
  6676. (raise-frame
  6677. (select-frame (or (and org-indirect-dedicated-frame
  6678. (frame-live-p org-indirect-dedicated-frame)
  6679. org-indirect-dedicated-frame)
  6680. (setq org-indirect-dedicated-frame (make-frame)))))
  6681. (delete-other-windows)
  6682. (org-pop-to-buffer-same-window ibuf)
  6683. (org-set-frame-title (concat "Indirect: " heading)))
  6684. ((eq org-indirect-buffer-display 'current-window)
  6685. (org-pop-to-buffer-same-window ibuf))
  6686. ((eq org-indirect-buffer-display 'other-window)
  6687. (pop-to-buffer ibuf))
  6688. (t (error "Invalid value")))
  6689. (if (featurep 'xemacs)
  6690. (save-excursion (org-mode) (turn-on-font-lock)))
  6691. (narrow-to-region beg end)
  6692. (show-all)
  6693. (goto-char pos)
  6694. (run-hook-with-args 'org-cycle-hook 'all)
  6695. (and (window-live-p cwin) (select-window cwin))))
  6696. (defun org-get-indirect-buffer (&optional buffer)
  6697. (setq buffer (or buffer (current-buffer)))
  6698. (let ((n 1) (base (buffer-name buffer)) bname)
  6699. (while (buffer-live-p
  6700. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  6701. (setq n (1+ n)))
  6702. (condition-case nil
  6703. (make-indirect-buffer buffer bname 'clone)
  6704. (error (make-indirect-buffer buffer bname)))))
  6705. (defun org-set-frame-title (title)
  6706. "Set the title of the current frame to the string TITLE."
  6707. ;; FIXME: how to name a single frame in XEmacs???
  6708. (unless (featurep 'xemacs)
  6709. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6710. ;;;; Structure editing
  6711. ;;; Inserting headlines
  6712. (defun org-previous-line-empty-p (&optional next)
  6713. "Is the previous line a blank line?
  6714. When NEXT is non-nil, check the next line instead."
  6715. (save-excursion
  6716. (and (not (bobp))
  6717. (or (beginning-of-line (if next 2 0)) t)
  6718. (save-match-data
  6719. (looking-at "[ \t]*$")))))
  6720. (defun org-insert-heading (&optional arg invisible-ok)
  6721. "Insert a new heading or item with same depth at point.
  6722. If point is in a plain list and ARG is nil, create a new list item.
  6723. With one universal prefix argument, insert a heading even in lists.
  6724. With two universal prefix arguments, insert the heading at the end
  6725. of the parent subtree.
  6726. If point is at the beginning of a headline, insert a sibling before
  6727. the current headline. If point is not at the beginning, split the line
  6728. and create a new headline with the text in the current line after point
  6729. \(see `org-M-RET-may-split-line' on how to modify this behavior).
  6730. When INVISIBLE-OK is set, stop at invisible headlines when going back.
  6731. This is important for non-interactive uses of the command."
  6732. (interactive "P")
  6733. (if (org-called-interactively-p 'any) (org-reveal))
  6734. (let ((itemp (org-in-item-p)))
  6735. (cond
  6736. ((or (= (buffer-size) 0)
  6737. (and (not (save-excursion
  6738. (and (ignore-errors (org-back-to-heading invisible-ok))
  6739. (org-at-heading-p))))
  6740. (or arg (not itemp))))
  6741. (insert
  6742. (if (org-previous-line-empty-p) "" "\n")
  6743. (if (org-in-src-block-p) ",* " "* "))
  6744. (run-hooks 'org-insert-heading-hook))
  6745. ((or arg
  6746. (and (not itemp) org-insert-heading-respect-content)
  6747. (not (org-insert-item
  6748. (save-excursion
  6749. (and itemp
  6750. (goto-char itemp)
  6751. (looking-at org-list-full-item-re)
  6752. (match-string 3))))))
  6753. (let (begn endn)
  6754. (when (org-buffer-narrowed-p)
  6755. (setq begn (point-min) endn (point-max))
  6756. (widen))
  6757. (let* ((empty-line-p nil)
  6758. (eops (equal arg '(16))) ; insert at end of parent subtree
  6759. (org-insert-heading-respect-content
  6760. (or (not (null arg)) org-insert-heading-respect-content))
  6761. (level nil)
  6762. (on-heading (org-at-heading-p))
  6763. ;; Get a level to fall back on
  6764. (fix-level
  6765. (save-excursion
  6766. (org-back-to-heading t)
  6767. (looking-at org-outline-regexp)
  6768. (make-string (1- (length (match-string 0))) ?*)))
  6769. (on-empty-line
  6770. (save-excursion (beginning-of-line 1) (looking-at "^\\s-*$")))
  6771. (head (save-excursion
  6772. (condition-case nil
  6773. (progn
  6774. (org-back-to-heading invisible-ok)
  6775. (when (and (not on-heading)
  6776. (featurep 'org-inlinetask)
  6777. (integerp org-inlinetask-min-level)
  6778. (>= (length (match-string 0))
  6779. org-inlinetask-min-level))
  6780. ;; Find a heading level before the inline task
  6781. (while (and (setq level (org-up-heading-safe))
  6782. (>= level org-inlinetask-min-level)))
  6783. (if (org-at-heading-p)
  6784. (org-back-to-heading invisible-ok)
  6785. (error "This should not happen")))
  6786. (unless (and (save-excursion
  6787. (save-match-data
  6788. (org-backward-heading-same-level 1 invisible-ok))
  6789. (= (point) (match-beginning 0)))
  6790. (not (org-previous-line-empty-p t)))
  6791. (setq empty-line-p (org-previous-line-empty-p)))
  6792. (match-string 0))
  6793. (error (or fix-level "* ")))))
  6794. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6795. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6796. pos hide-previous previous-pos)
  6797. (if ;; At the beginning of a heading, open a new line for insertion
  6798. (and (bolp) (org-at-heading-p)
  6799. (not eops)
  6800. (or (bobp)
  6801. (save-excursion (backward-char 1) (not (outline-invisible-p)))))
  6802. (open-line (if blank 2 1))
  6803. (save-excursion
  6804. (setq previous-pos (point-at-bol))
  6805. (end-of-line)
  6806. (setq hide-previous (outline-invisible-p)))
  6807. (and org-insert-heading-respect-content
  6808. (save-excursion
  6809. (while (outline-invisible-p)
  6810. (org-show-subtree)
  6811. (org-up-heading-safe))))
  6812. (let ((split
  6813. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  6814. (save-excursion
  6815. (let ((p (point)))
  6816. (goto-char (point-at-bol))
  6817. (and (looking-at org-complex-heading-regexp)
  6818. (match-beginning 4)
  6819. (> p (match-beginning 4)))))))
  6820. tags pos)
  6821. (cond
  6822. ;; Insert a new line, possibly at end of parent subtree
  6823. ((and (not arg) (not on-heading) (not on-empty-line)
  6824. (not (save-excursion
  6825. (beginning-of-line 1)
  6826. (or (looking-at org-list-full-item-re)
  6827. ;; Don't convert :end: lines to headline
  6828. (looking-at "^\\s-*:end:")
  6829. (looking-at "^\\s-*#\\+end_?")))))
  6830. (beginning-of-line 1))
  6831. (org-insert-heading-respect-content
  6832. (if (not eops)
  6833. (progn
  6834. (org-end-of-subtree nil t)
  6835. (and (looking-at "^\\*") (backward-char 1))
  6836. (while (and (not (bobp))
  6837. ;; Don't delete spaces in empty headlines
  6838. (not (looking-back org-outline-regexp))
  6839. (member (char-before) '(?\ ?\t ?\n)))
  6840. (backward-delete-char 1)))
  6841. (let ((p (point)))
  6842. (org-up-heading-safe)
  6843. (if (= p (point))
  6844. (goto-char (point-max))
  6845. (org-end-of-subtree nil t))))
  6846. (when (featurep 'org-inlinetask)
  6847. (while (and (not (eobp))
  6848. (looking-at "\\(\\*+\\)[ \t]+")
  6849. (>= (length (match-string 1))
  6850. org-inlinetask-min-level))
  6851. (org-end-of-subtree nil t)))
  6852. (or (bolp) (newline))
  6853. (or (org-previous-line-empty-p)
  6854. (and blank (newline)))
  6855. (if (or empty-line-p eops) (open-line 1)))
  6856. ;; Insert a headling containing text after point
  6857. ((org-at-heading-p)
  6858. (when hide-previous
  6859. (show-children)
  6860. (org-show-entry))
  6861. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
  6862. (setq tags (and (match-end 2) (match-string 2)))
  6863. (and (match-end 1)
  6864. (delete-region (match-beginning 1) (match-end 1)))
  6865. (setq pos (point-at-bol))
  6866. (or split (end-of-line 1))
  6867. (delete-horizontal-space)
  6868. (if (string-match "\\`\\*+\\'"
  6869. (buffer-substring (point-at-bol) (point)))
  6870. (insert " "))
  6871. (newline (if blank 2 1))
  6872. (when tags
  6873. (save-excursion
  6874. (goto-char pos)
  6875. (end-of-line 1)
  6876. (insert " " tags)
  6877. (org-set-tags nil 'align))))
  6878. (t
  6879. (or split (end-of-line 1))
  6880. (newline (cond ((and blank (not on-empty-line)) 2)
  6881. (blank 1)
  6882. (on-empty-line 0) (t 1)))))))
  6883. (insert head) (just-one-space)
  6884. (setq pos (point))
  6885. (end-of-line 1)
  6886. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  6887. (when (and org-insert-heading-respect-content hide-previous)
  6888. (save-excursion
  6889. (goto-char previous-pos)
  6890. (hide-subtree)))
  6891. (when (and begn endn)
  6892. (narrow-to-region (min (point) begn) (max (point) endn)))
  6893. (run-hooks 'org-insert-heading-hook)))))))
  6894. (defun org-get-heading (&optional no-tags no-todo)
  6895. "Return the heading of the current entry, without the stars.
  6896. When NO-TAGS is non-nil, don't include tags.
  6897. When NO-TODO is non-nil, don't include TODO keywords."
  6898. (save-excursion
  6899. (org-back-to-heading t)
  6900. (cond
  6901. ((and no-tags no-todo)
  6902. (looking-at org-complex-heading-regexp)
  6903. (match-string 4))
  6904. (no-tags
  6905. (looking-at (concat org-outline-regexp
  6906. "\\(.*?\\)"
  6907. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  6908. (match-string 1))
  6909. (no-todo
  6910. (looking-at org-todo-line-regexp)
  6911. (match-string 3))
  6912. (t (looking-at org-heading-regexp)
  6913. (match-string 2)))))
  6914. (defvar orgstruct-mode) ; defined below
  6915. (defun org-heading-components ()
  6916. "Return the components of the current heading.
  6917. This is a list with the following elements:
  6918. - the level as an integer
  6919. - the reduced level, different if `org-odd-levels-only' is set.
  6920. - the TODO keyword, or nil
  6921. - the priority character, like ?A, or nil if no priority is given
  6922. - the headline text itself, or the tags string if no headline text
  6923. - the tags string, or nil."
  6924. (save-excursion
  6925. (org-back-to-heading t)
  6926. (if (let (case-fold-search)
  6927. (looking-at
  6928. (if orgstruct-mode
  6929. org-heading-regexp
  6930. org-complex-heading-regexp)))
  6931. (if orgstruct-mode
  6932. (list (length (match-string 1))
  6933. (org-reduced-level (length (match-string 1)))
  6934. nil
  6935. nil
  6936. (match-string 2)
  6937. nil)
  6938. (list (length (match-string 1))
  6939. (org-reduced-level (length (match-string 1)))
  6940. (org-match-string-no-properties 2)
  6941. (and (match-end 3) (aref (match-string 3) 2))
  6942. (org-match-string-no-properties 4)
  6943. (org-match-string-no-properties 5))))))
  6944. (defun org-get-entry ()
  6945. "Get the entry text, after heading, entire subtree."
  6946. (save-excursion
  6947. (org-back-to-heading t)
  6948. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  6949. (defun org-insert-heading-after-current ()
  6950. "Insert a new heading with same level as current, after current subtree."
  6951. (interactive)
  6952. (org-back-to-heading)
  6953. (org-insert-heading)
  6954. (org-move-subtree-down)
  6955. (end-of-line 1))
  6956. (defun org-insert-heading-respect-content (&optional arg invisible-ok)
  6957. "Insert heading with `org-insert-heading-respect-content' set to t."
  6958. (interactive "P")
  6959. (let ((org-insert-heading-respect-content t))
  6960. (org-insert-heading arg invisible-ok)))
  6961. (defun org-insert-todo-heading-respect-content (&optional force-state)
  6962. "Insert TODO heading with `org-insert-heading-respect-content' set to t."
  6963. (interactive "P")
  6964. (let ((org-insert-heading-respect-content t))
  6965. (org-insert-todo-heading force-state t)))
  6966. (defun org-insert-todo-heading (arg &optional force-heading)
  6967. "Insert a new heading with the same level and TODO state as current heading.
  6968. If the heading has no TODO state, or if the state is DONE, use the first
  6969. state (TODO by default). Also one prefix arg, force first state. With two
  6970. prefix args, force inserting at the end of the parent subtree."
  6971. (interactive "P")
  6972. (when (or force-heading (not (org-insert-item 'checkbox)))
  6973. (org-insert-heading (or (and (equal arg '(16)) '(16))
  6974. force-heading))
  6975. (save-excursion
  6976. (org-back-to-heading)
  6977. (outline-previous-heading)
  6978. (looking-at org-todo-line-regexp))
  6979. (let*
  6980. ((new-mark-x
  6981. (if (or arg
  6982. (not (match-beginning 2))
  6983. (member (match-string 2) org-done-keywords))
  6984. (car org-todo-keywords-1)
  6985. (match-string 2)))
  6986. (new-mark
  6987. (or
  6988. (run-hook-with-args-until-success
  6989. 'org-todo-get-default-hook new-mark-x nil)
  6990. new-mark-x)))
  6991. (beginning-of-line 1)
  6992. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  6993. (if org-treat-insert-todo-heading-as-state-change
  6994. (org-todo new-mark)
  6995. (insert new-mark " "))))
  6996. (when org-provide-todo-statistics
  6997. (org-update-parent-todo-statistics))))
  6998. (defun org-insert-subheading (arg)
  6999. "Insert a new subheading and demote it.
  7000. Works for outline headings and for plain lists alike."
  7001. (interactive "P")
  7002. (org-insert-heading arg)
  7003. (cond
  7004. ((org-at-heading-p) (org-do-demote))
  7005. ((org-at-item-p) (org-indent-item))))
  7006. (defun org-insert-todo-subheading (arg)
  7007. "Insert a new subheading with TODO keyword or checkbox and demote it.
  7008. Works for outline headings and for plain lists alike."
  7009. (interactive "P")
  7010. (org-insert-todo-heading arg)
  7011. (cond
  7012. ((org-at-heading-p) (org-do-demote))
  7013. ((org-at-item-p) (org-indent-item))))
  7014. ;;; Promotion and Demotion
  7015. (defvar org-after-demote-entry-hook nil
  7016. "Hook run after an entry has been demoted.
  7017. The cursor will be at the beginning of the entry.
  7018. When a subtree is being demoted, the hook will be called for each node.")
  7019. (defvar org-after-promote-entry-hook nil
  7020. "Hook run after an entry has been promoted.
  7021. The cursor will be at the beginning of the entry.
  7022. When a subtree is being promoted, the hook will be called for each node.")
  7023. (defun org-promote-subtree ()
  7024. "Promote the entire subtree.
  7025. See also `org-promote'."
  7026. (interactive)
  7027. (save-excursion
  7028. (org-with-limited-levels (org-map-tree 'org-promote)))
  7029. (org-fix-position-after-promote))
  7030. (defun org-demote-subtree ()
  7031. "Demote the entire subtree. See `org-demote'.
  7032. See also `org-promote'."
  7033. (interactive)
  7034. (save-excursion
  7035. (org-with-limited-levels (org-map-tree 'org-demote)))
  7036. (org-fix-position-after-promote))
  7037. (defun org-do-promote ()
  7038. "Promote the current heading higher up the tree.
  7039. If the region is active in `transient-mark-mode', promote all headings
  7040. in the region."
  7041. (interactive)
  7042. (save-excursion
  7043. (if (org-region-active-p)
  7044. (org-map-region 'org-promote (region-beginning) (region-end))
  7045. (org-promote)))
  7046. (org-fix-position-after-promote))
  7047. (defun org-do-demote ()
  7048. "Demote the current heading lower down the tree.
  7049. If the region is active in `transient-mark-mode', demote all headings
  7050. in the region."
  7051. (interactive)
  7052. (save-excursion
  7053. (if (org-region-active-p)
  7054. (org-map-region 'org-demote (region-beginning) (region-end))
  7055. (org-demote)))
  7056. (org-fix-position-after-promote))
  7057. (defun org-fix-position-after-promote ()
  7058. "Make sure that after pro/demotion cursor position is right."
  7059. (let ((pos (point)))
  7060. (when (save-excursion
  7061. (beginning-of-line 1)
  7062. (looking-at org-todo-line-regexp)
  7063. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  7064. (cond ((eobp) (insert " "))
  7065. ((eolp) (insert " "))
  7066. ((equal (char-after) ?\ ) (forward-char 1))))))
  7067. (defun org-current-level ()
  7068. "Return the level of the current entry, or nil if before the first headline.
  7069. The level is the number of stars at the beginning of the headline."
  7070. (save-excursion
  7071. (org-with-limited-levels
  7072. (if (ignore-errors (org-back-to-heading t))
  7073. (funcall outline-level)))))
  7074. (defun org-get-previous-line-level ()
  7075. "Return the outline depth of the last headline before the current line.
  7076. Returns 0 for the first headline in the buffer, and nil if before the
  7077. first headline."
  7078. (let ((current-level (org-current-level))
  7079. (prev-level (when (> (line-number-at-pos) 1)
  7080. (save-excursion
  7081. (beginning-of-line 0)
  7082. (org-current-level)))))
  7083. (cond ((null current-level) nil) ; Before first headline
  7084. ((null prev-level) 0) ; At first headline
  7085. (prev-level))))
  7086. (defun org-reduced-level (l)
  7087. "Compute the effective level of a heading.
  7088. This takes into account the setting of `org-odd-levels-only'."
  7089. (cond
  7090. ((zerop l) 0)
  7091. (org-odd-levels-only (1+ (floor (/ l 2))))
  7092. (t l)))
  7093. (defun org-level-increment ()
  7094. "Return the number of stars that will be added or removed at a
  7095. time to headlines when structure editing, based on the value of
  7096. `org-odd-levels-only'."
  7097. (if org-odd-levels-only 2 1))
  7098. (defun org-get-valid-level (level &optional change)
  7099. "Rectify a level change under the influence of `org-odd-levels-only'
  7100. LEVEL is a current level, CHANGE is by how much the level should be
  7101. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  7102. even level numbers will become the next higher odd number."
  7103. (if org-odd-levels-only
  7104. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  7105. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  7106. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  7107. (max 1 (+ level (or change 0)))))
  7108. (if (boundp 'define-obsolete-function-alias)
  7109. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  7110. (define-obsolete-function-alias 'org-get-legal-level
  7111. 'org-get-valid-level)
  7112. (define-obsolete-function-alias 'org-get-legal-level
  7113. 'org-get-valid-level "23.1")))
  7114. (defvar org-called-with-limited-levels nil) ;; Dynamically bound in
  7115. ;; ̀org-with-limited-levels'
  7116. (defun org-promote ()
  7117. "Promote the current heading higher up the tree.
  7118. If the region is active in `transient-mark-mode', promote all headings
  7119. in the region."
  7120. (org-back-to-heading t)
  7121. (let* ((level (save-match-data (funcall outline-level)))
  7122. (after-change-functions (remove 'flyspell-after-change-function
  7123. after-change-functions))
  7124. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  7125. (diff (abs (- level (length up-head) -1))))
  7126. (cond ((and (= level 1) org-called-with-limited-levels
  7127. org-allow-promoting-top-level-subtree)
  7128. (replace-match "# " nil t))
  7129. ((= level 1)
  7130. (user-error "Cannot promote to level 0. UNDO to recover if necessary"))
  7131. (t (replace-match up-head nil t)))
  7132. ;; Fixup tag positioning
  7133. (unless (= level 1)
  7134. (and org-auto-align-tags (org-set-tags nil t))
  7135. (if org-adapt-indentation (org-fixup-indentation (- diff))))
  7136. (run-hooks 'org-after-promote-entry-hook)))
  7137. (defun org-demote ()
  7138. "Demote the current heading lower down the tree.
  7139. If the region is active in `transient-mark-mode', demote all headings
  7140. in the region."
  7141. (org-back-to-heading t)
  7142. (let* ((level (save-match-data (funcall outline-level)))
  7143. (after-change-functions (remove 'flyspell-after-change-function
  7144. after-change-functions))
  7145. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  7146. (diff (abs (- level (length down-head) -1))))
  7147. (replace-match down-head nil t)
  7148. ;; Fixup tag positioning
  7149. (and org-auto-align-tags (org-set-tags nil t))
  7150. (if org-adapt-indentation (org-fixup-indentation diff))
  7151. (run-hooks 'org-after-demote-entry-hook)))
  7152. (defun org-cycle-level ()
  7153. "Cycle the level of an empty headline through possible states.
  7154. This goes first to child, then to parent, level, then up the hierarchy.
  7155. After top level, it switches back to sibling level."
  7156. (interactive)
  7157. (let ((org-adapt-indentation nil))
  7158. (when (org-point-at-end-of-empty-headline)
  7159. (setq this-command 'org-cycle-level) ; Only needed for caching
  7160. (let ((cur-level (org-current-level))
  7161. (prev-level (org-get-previous-line-level)))
  7162. (cond
  7163. ;; If first headline in file, promote to top-level.
  7164. ((= prev-level 0)
  7165. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7166. do (org-do-promote)))
  7167. ;; If same level as prev, demote one.
  7168. ((= prev-level cur-level)
  7169. (org-do-demote))
  7170. ;; If parent is top-level, promote to top level if not already.
  7171. ((= prev-level 1)
  7172. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7173. do (org-do-promote)))
  7174. ;; If top-level, return to prev-level.
  7175. ((= cur-level 1)
  7176. (loop repeat (/ (- prev-level 1) (org-level-increment))
  7177. do (org-do-demote)))
  7178. ;; If less than prev-level, promote one.
  7179. ((< cur-level prev-level)
  7180. (org-do-promote))
  7181. ;; If deeper than prev-level, promote until higher than
  7182. ;; prev-level.
  7183. ((> cur-level prev-level)
  7184. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  7185. do (org-do-promote))))
  7186. t))))
  7187. (defun org-map-tree (fun)
  7188. "Call FUN for every heading underneath the current one."
  7189. (org-back-to-heading)
  7190. (let ((level (funcall outline-level)))
  7191. (save-excursion
  7192. (funcall fun)
  7193. (while (and (progn
  7194. (outline-next-heading)
  7195. (> (funcall outline-level) level))
  7196. (not (eobp)))
  7197. (funcall fun)))))
  7198. (defun org-map-region (fun beg end)
  7199. "Call FUN for every heading between BEG and END."
  7200. (let ((org-ignore-region t))
  7201. (save-excursion
  7202. (setq end (copy-marker end))
  7203. (goto-char beg)
  7204. (if (and (re-search-forward org-outline-regexp-bol nil t)
  7205. (< (point) end))
  7206. (funcall fun))
  7207. (while (and (progn
  7208. (outline-next-heading)
  7209. (< (point) end))
  7210. (not (eobp)))
  7211. (funcall fun)))))
  7212. (defvar org-property-end-re) ; silence byte-compiler
  7213. (defun org-fixup-indentation (diff)
  7214. "Change the indentation in the current entry by DIFF.
  7215. However, if any line in the current entry has no indentation, or if it
  7216. would end up with no indentation after the change, nothing at all is done."
  7217. (save-excursion
  7218. (let ((end (save-excursion (outline-next-heading)
  7219. (point-marker)))
  7220. (prohibit (if (> diff 0)
  7221. "^\\S-"
  7222. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  7223. col)
  7224. (unless (save-excursion (end-of-line 1)
  7225. (re-search-forward prohibit end t))
  7226. (while (and (< (point) end)
  7227. (re-search-forward "^[ \t]+" end t))
  7228. (goto-char (match-end 0))
  7229. (setq col (current-column))
  7230. (if (< diff 0) (replace-match ""))
  7231. (org-indent-to-column (+ diff col))))
  7232. (move-marker end nil))))
  7233. (defun org-convert-to-odd-levels ()
  7234. "Convert an org-mode file with all levels allowed to one with odd levels.
  7235. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  7236. level 5 etc."
  7237. (interactive)
  7238. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  7239. (let ((outline-level 'org-outline-level)
  7240. (org-odd-levels-only nil) n)
  7241. (save-excursion
  7242. (goto-char (point-min))
  7243. (while (re-search-forward "^\\*\\*+ " nil t)
  7244. (setq n (- (length (match-string 0)) 2))
  7245. (while (>= (setq n (1- n)) 0)
  7246. (org-demote))
  7247. (end-of-line 1))))))
  7248. (defun org-convert-to-oddeven-levels ()
  7249. "Convert an org-mode file with only odd levels to one with odd/even levels.
  7250. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  7251. file contains a section with an even level, conversion would
  7252. destroy the structure of the file. An error is signaled in this
  7253. case."
  7254. (interactive)
  7255. (goto-char (point-min))
  7256. ;; First check if there are no even levels
  7257. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  7258. (org-show-context t)
  7259. (error "Not all levels are odd in this file. Conversion not possible"))
  7260. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  7261. (let ((outline-regexp org-outline-regexp)
  7262. (outline-level 'org-outline-level)
  7263. (org-odd-levels-only nil) n)
  7264. (save-excursion
  7265. (goto-char (point-min))
  7266. (while (re-search-forward "^\\*\\*+ " nil t)
  7267. (setq n (/ (1- (length (match-string 0))) 2))
  7268. (while (>= (setq n (1- n)) 0)
  7269. (org-promote))
  7270. (end-of-line 1))))))
  7271. (defun org-tr-level (n)
  7272. "Make N odd if required."
  7273. (if org-odd-levels-only (1+ (/ n 2)) n))
  7274. ;;; Vertical tree motion, cutting and pasting of subtrees
  7275. (defun org-move-subtree-up (&optional arg)
  7276. "Move the current subtree up past ARG headlines of the same level."
  7277. (interactive "p")
  7278. (org-move-subtree-down (- (prefix-numeric-value arg))))
  7279. (defun org-move-subtree-down (&optional arg)
  7280. "Move the current subtree down past ARG headlines of the same level."
  7281. (interactive "p")
  7282. (setq arg (prefix-numeric-value arg))
  7283. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  7284. 'org-get-last-sibling))
  7285. (ins-point (make-marker))
  7286. (cnt (abs arg))
  7287. (col (current-column))
  7288. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  7289. ;; Select the tree
  7290. (org-back-to-heading)
  7291. (setq beg0 (point))
  7292. (save-excursion
  7293. (setq ne-beg (org-back-over-empty-lines))
  7294. (setq beg (point)))
  7295. (save-match-data
  7296. (save-excursion (outline-end-of-heading)
  7297. (setq folded (outline-invisible-p)))
  7298. (outline-end-of-subtree))
  7299. (outline-next-heading)
  7300. (setq ne-end (org-back-over-empty-lines))
  7301. (setq end (point))
  7302. (goto-char beg0)
  7303. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  7304. ;; include less whitespace
  7305. (save-excursion
  7306. (goto-char beg)
  7307. (forward-line (- ne-beg ne-end))
  7308. (setq beg (point))))
  7309. ;; Find insertion point, with error handling
  7310. (while (> cnt 0)
  7311. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  7312. (progn (goto-char beg0)
  7313. (user-error "Cannot move past superior level or buffer limit")))
  7314. (setq cnt (1- cnt)))
  7315. (if (> arg 0)
  7316. ;; Moving forward - still need to move over subtree
  7317. (progn (org-end-of-subtree t t)
  7318. (save-excursion
  7319. (org-back-over-empty-lines)
  7320. (or (bolp) (newline)))))
  7321. (setq ne-ins (org-back-over-empty-lines))
  7322. (move-marker ins-point (point))
  7323. (setq txt (buffer-substring beg end))
  7324. (org-save-markers-in-region beg end)
  7325. (delete-region beg end)
  7326. (org-remove-empty-overlays-at beg)
  7327. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  7328. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  7329. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  7330. (let ((bbb (point)))
  7331. (insert-before-markers txt)
  7332. (org-reinstall-markers-in-region bbb)
  7333. (move-marker ins-point bbb))
  7334. (or (bolp) (insert "\n"))
  7335. (setq ins-end (point))
  7336. (goto-char ins-point)
  7337. (org-skip-whitespace)
  7338. (when (and (< arg 0)
  7339. (org-first-sibling-p)
  7340. (> ne-ins ne-beg))
  7341. ;; Move whitespace back to beginning
  7342. (save-excursion
  7343. (goto-char ins-end)
  7344. (let ((kill-whole-line t))
  7345. (kill-line (- ne-ins ne-beg)) (point)))
  7346. (insert (make-string (- ne-ins ne-beg) ?\n)))
  7347. (move-marker ins-point nil)
  7348. (if folded
  7349. (hide-subtree)
  7350. (org-show-entry)
  7351. (show-children)
  7352. (org-cycle-hide-drawers 'children))
  7353. (org-clean-visibility-after-subtree-move)
  7354. ;; move back to the initial column we were at
  7355. (move-to-column col)))
  7356. (defvar org-subtree-clip ""
  7357. "Clipboard for cut and paste of subtrees.
  7358. This is actually only a copy of the kill, because we use the normal kill
  7359. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  7360. (defvar org-subtree-clip-folded nil
  7361. "Was the last copied subtree folded?
  7362. This is used to fold the tree back after pasting.")
  7363. (defun org-cut-subtree (&optional n)
  7364. "Cut the current subtree into the clipboard.
  7365. With prefix arg N, cut this many sequential subtrees.
  7366. This is a short-hand for marking the subtree and then cutting it."
  7367. (interactive "p")
  7368. (org-copy-subtree n 'cut))
  7369. (defun org-copy-subtree (&optional n cut force-store-markers nosubtrees)
  7370. "Cut the current subtree into the clipboard.
  7371. With prefix arg N, cut this many sequential subtrees.
  7372. This is a short-hand for marking the subtree and then copying it.
  7373. If CUT is non-nil, actually cut the subtree.
  7374. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  7375. of some markers in the region, even if CUT is non-nil. This is
  7376. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  7377. (interactive "p")
  7378. (let (beg end folded (beg0 (point)))
  7379. (if (org-called-interactively-p 'any)
  7380. (org-back-to-heading nil) ; take what looks like a subtree
  7381. (org-back-to-heading t)) ; take what is really there
  7382. (setq beg (point))
  7383. (skip-chars-forward " \t\r\n")
  7384. (save-match-data
  7385. (if nosubtrees
  7386. (outline-next-heading)
  7387. (save-excursion (outline-end-of-heading)
  7388. (setq folded (outline-invisible-p)))
  7389. (condition-case nil
  7390. (org-forward-heading-same-level (1- n) t)
  7391. (error nil))
  7392. (org-end-of-subtree t t)))
  7393. (setq end (point))
  7394. (goto-char beg0)
  7395. (when (> end beg)
  7396. (setq org-subtree-clip-folded folded)
  7397. (when (or cut force-store-markers)
  7398. (org-save-markers-in-region beg end))
  7399. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  7400. (setq org-subtree-clip (current-kill 0))
  7401. (message "%s: Subtree(s) with %d characters"
  7402. (if cut "Cut" "Copied")
  7403. (length org-subtree-clip)))))
  7404. (defun org-paste-subtree (&optional level tree for-yank)
  7405. "Paste the clipboard as a subtree, with modification of headline level.
  7406. The entire subtree is promoted or demoted in order to match a new headline
  7407. level.
  7408. If the cursor is at the beginning of a headline, the same level as
  7409. that headline is used to paste the tree.
  7410. If not, the new level is derived from the *visible* headings
  7411. before and after the insertion point, and taken to be the inferior headline
  7412. level of the two. So if the previous visible heading is level 3 and the
  7413. next is level 4 (or vice versa), level 4 will be used for insertion.
  7414. This makes sure that the subtree remains an independent subtree and does
  7415. not swallow low level entries.
  7416. You can also force a different level, either by using a numeric prefix
  7417. argument, or by inserting the heading marker by hand. For example, if the
  7418. cursor is after \"*****\", then the tree will be shifted to level 5.
  7419. If optional TREE is given, use this text instead of the kill ring.
  7420. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  7421. move back over whitespace before inserting, and move point to the end of
  7422. the inserted text when done."
  7423. (interactive "P")
  7424. (setq tree (or tree (and kill-ring (current-kill 0))))
  7425. (unless (org-kill-is-subtree-p tree)
  7426. (user-error "%s"
  7427. (substitute-command-keys
  7428. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  7429. (org-with-limited-levels
  7430. (let* ((visp (not (outline-invisible-p)))
  7431. (txt tree)
  7432. (^re_ "\\(\\*+\\)[ \t]*")
  7433. (old-level (if (string-match org-outline-regexp-bol txt)
  7434. (- (match-end 0) (match-beginning 0) 1)
  7435. -1))
  7436. (force-level (cond (level (prefix-numeric-value level))
  7437. ((and (looking-at "[ \t]*$")
  7438. (string-match
  7439. "^\\*+$" (buffer-substring
  7440. (point-at-bol) (point))))
  7441. (- (match-end 1) (match-beginning 1)))
  7442. ((and (bolp)
  7443. (looking-at org-outline-regexp))
  7444. (- (match-end 0) (point) 1))))
  7445. (previous-level (save-excursion
  7446. (condition-case nil
  7447. (progn
  7448. (outline-previous-visible-heading 1)
  7449. (if (looking-at ^re_)
  7450. (- (match-end 0) (match-beginning 0) 1)
  7451. 1))
  7452. (error 1))))
  7453. (next-level (save-excursion
  7454. (condition-case nil
  7455. (progn
  7456. (or (looking-at org-outline-regexp)
  7457. (outline-next-visible-heading 1))
  7458. (if (looking-at ^re_)
  7459. (- (match-end 0) (match-beginning 0) 1)
  7460. 1))
  7461. (error 1))))
  7462. (new-level (or force-level (max previous-level next-level)))
  7463. (shift (if (or (= old-level -1)
  7464. (= new-level -1)
  7465. (= old-level new-level))
  7466. 0
  7467. (- new-level old-level)))
  7468. (delta (if (> shift 0) -1 1))
  7469. (func (if (> shift 0) 'org-demote 'org-promote))
  7470. (org-odd-levels-only nil)
  7471. beg end newend)
  7472. ;; Remove the forced level indicator
  7473. (if force-level
  7474. (delete-region (point-at-bol) (point)))
  7475. ;; Paste
  7476. (beginning-of-line (if (bolp) 1 2))
  7477. (setq beg (point))
  7478. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  7479. (insert-before-markers txt)
  7480. (unless (string-match "\n\\'" txt) (insert "\n"))
  7481. (setq newend (point))
  7482. (org-reinstall-markers-in-region beg)
  7483. (setq end (point))
  7484. (goto-char beg)
  7485. (skip-chars-forward " \t\n\r")
  7486. (setq beg (point))
  7487. (if (and (outline-invisible-p) visp)
  7488. (save-excursion (outline-show-heading)))
  7489. ;; Shift if necessary
  7490. (unless (= shift 0)
  7491. (save-restriction
  7492. (narrow-to-region beg end)
  7493. (while (not (= shift 0))
  7494. (org-map-region func (point-min) (point-max))
  7495. (setq shift (+ delta shift)))
  7496. (goto-char (point-min))
  7497. (setq newend (point-max))))
  7498. (when (or (org-called-interactively-p 'interactive) for-yank)
  7499. (message "Clipboard pasted as level %d subtree" new-level))
  7500. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  7501. kill-ring
  7502. (eq org-subtree-clip (current-kill 0))
  7503. org-subtree-clip-folded)
  7504. ;; The tree was folded before it was killed/copied
  7505. (hide-subtree))
  7506. (and for-yank (goto-char newend)))))
  7507. (defun org-kill-is-subtree-p (&optional txt)
  7508. "Check if the current kill is an outline subtree, or a set of trees.
  7509. Returns nil if kill does not start with a headline, or if the first
  7510. headline level is not the largest headline level in the tree.
  7511. So this will actually accept several entries of equal levels as well,
  7512. which is OK for `org-paste-subtree'.
  7513. If optional TXT is given, check this string instead of the current kill."
  7514. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  7515. (re (org-get-limited-outline-regexp))
  7516. (^re (concat "^" re))
  7517. (start-level (and kill
  7518. (string-match
  7519. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7520. kill)
  7521. (- (match-end 2) (match-beginning 2) 1)))
  7522. (start (1+ (or (match-beginning 2) -1))))
  7523. (if (not start-level)
  7524. (progn
  7525. nil) ;; does not even start with a heading
  7526. (catch 'exit
  7527. (while (setq start (string-match ^re kill (1+ start)))
  7528. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7529. (throw 'exit nil)))
  7530. t))))
  7531. (defvar org-markers-to-move nil
  7532. "Markers that should be moved with a cut-and-paste operation.
  7533. Those markers are stored together with their positions relative to
  7534. the start of the region.")
  7535. (defun org-save-markers-in-region (beg end)
  7536. "Check markers in region.
  7537. If these markers are between BEG and END, record their position relative
  7538. to BEG, so that after moving the block of text, we can put the markers back
  7539. into place.
  7540. This function gets called just before an entry or tree gets cut from the
  7541. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7542. called immediately, to move the markers with the entries."
  7543. (setq org-markers-to-move nil)
  7544. (when (featurep 'org-clock)
  7545. (org-clock-save-markers-for-cut-and-paste beg end))
  7546. (when (featurep 'org-agenda)
  7547. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7548. (defun org-check-and-save-marker (marker beg end)
  7549. "Check if MARKER is between BEG and END.
  7550. If yes, remember the marker and the distance to BEG."
  7551. (when (and (marker-buffer marker)
  7552. (equal (marker-buffer marker) (current-buffer)))
  7553. (if (and (>= marker beg) (< marker end))
  7554. (push (cons marker (- marker beg)) org-markers-to-move))))
  7555. (defun org-reinstall-markers-in-region (beg)
  7556. "Move all remembered markers to their position relative to BEG."
  7557. (mapc (lambda (x)
  7558. (move-marker (car x) (+ beg (cdr x))))
  7559. org-markers-to-move)
  7560. (setq org-markers-to-move nil))
  7561. (defun org-narrow-to-subtree ()
  7562. "Narrow buffer to the current subtree."
  7563. (interactive)
  7564. (save-excursion
  7565. (save-match-data
  7566. (org-with-limited-levels
  7567. (narrow-to-region
  7568. (progn (org-back-to-heading t) (point))
  7569. (progn (org-end-of-subtree t t)
  7570. (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7571. (point)))))))
  7572. (defun org-narrow-to-block ()
  7573. "Narrow buffer to the current block."
  7574. (interactive)
  7575. (let* ((case-fold-search t)
  7576. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7577. "^[ \t]*#\\+end_.*")))
  7578. (if blockp
  7579. (narrow-to-region (car blockp) (cdr blockp))
  7580. (user-error "Not in a block"))))
  7581. (eval-when-compile
  7582. (defvar org-property-drawer-re))
  7583. (defvar org-property-start-re) ;; defined below
  7584. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7585. "Clone the task (subtree) at point N times.
  7586. The clones will be inserted as siblings.
  7587. In interactive use, the user will be prompted for the number of
  7588. clones to be produced. If the entry has a timestamp, the user
  7589. will also be prompted for a time shift, which may be a repeater
  7590. as used in time stamps, for example `+3d'. To disable this,
  7591. you can call the function with a universal prefix argument.
  7592. When a valid repeater is given and the entry contains any time
  7593. stamps, the clones will become a sequence in time, with time
  7594. stamps in the subtree shifted for each clone produced. If SHIFT
  7595. is nil or the empty string, time stamps will be left alone. The
  7596. ID property of the original subtree is removed.
  7597. If the original subtree did contain time stamps with a repeater,
  7598. the following will happen:
  7599. - the repeater will be removed in each clone
  7600. - an additional clone will be produced, with the current, unshifted
  7601. date(s) in the entry.
  7602. - the original entry will be placed *after* all the clones, with
  7603. repeater intact.
  7604. - the start days in the repeater in the original entry will be shifted
  7605. to past the last clone.
  7606. In this way you can spell out a number of instances of a repeating task,
  7607. and still retain the repeater to cover future instances of the task."
  7608. (interactive "nNumber of clones to produce: ")
  7609. (let ((shift
  7610. (or shift
  7611. (if (and (not (equal current-prefix-arg '(4)))
  7612. (save-excursion
  7613. (re-search-forward org-ts-regexp-both
  7614. (save-excursion
  7615. (org-end-of-subtree t)
  7616. (point)) t)))
  7617. (read-from-minibuffer
  7618. "Date shift per clone (e.g. +1w, empty to copy unchanged): ")
  7619. ""))) ;; No time shift
  7620. (n-no-remove -1)
  7621. (drawer-re org-drawer-regexp)
  7622. beg end template task idprop
  7623. shift-n shift-what doshift nmin nmax)
  7624. (if (not (and (integerp n) (> n 0)))
  7625. (error "Invalid number of replications %s" n))
  7626. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7627. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
  7628. shift)))
  7629. (error "Invalid shift specification %s" shift))
  7630. (when doshift
  7631. (setq shift-n (string-to-number (match-string 1 shift))
  7632. shift-what (cdr (assoc (match-string 2 shift)
  7633. '(("d" . day) ("w" . week)
  7634. ("m" . month) ("y" . year))))))
  7635. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7636. (setq nmin 1 nmax n)
  7637. (org-back-to-heading t)
  7638. (setq beg (point))
  7639. (setq idprop (org-entry-get nil "ID"))
  7640. (org-end-of-subtree t t)
  7641. (or (bolp) (insert "\n"))
  7642. (setq end (point))
  7643. (setq template (buffer-substring beg end))
  7644. (when (and doshift
  7645. (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
  7646. (delete-region beg end)
  7647. (setq end beg)
  7648. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7649. (goto-char end)
  7650. (loop for n from nmin to nmax do
  7651. ;; prepare clone
  7652. (with-temp-buffer
  7653. (insert template)
  7654. (org-mode)
  7655. (goto-char (point-min))
  7656. (org-show-subtree)
  7657. (and idprop (if org-clone-delete-id
  7658. (org-entry-delete nil "ID")
  7659. (org-id-get-create t)))
  7660. (unless (= n 0)
  7661. (while (re-search-forward "^[ \t]*CLOCK:.*$" nil t)
  7662. (kill-whole-line))
  7663. (goto-char (point-min))
  7664. (while (re-search-forward drawer-re nil t)
  7665. (mapc (lambda (d)
  7666. (org-remove-empty-drawer-at d (point))) org-drawers)))
  7667. (goto-char (point-min))
  7668. (when doshift
  7669. (while (re-search-forward org-ts-regexp-both nil t)
  7670. (org-timestamp-change (* n shift-n) shift-what))
  7671. (unless (= n n-no-remove)
  7672. (goto-char (point-min))
  7673. (while (re-search-forward org-ts-regexp nil t)
  7674. (save-excursion
  7675. (goto-char (match-beginning 0))
  7676. (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
  7677. (delete-region (match-beginning 1) (match-end 1)))))))
  7678. (setq task (buffer-string)))
  7679. (insert task))
  7680. (goto-char beg)))
  7681. ;;; Outline Sorting
  7682. (defun org-sort (with-case)
  7683. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7684. Optional argument WITH-CASE means sort case-sensitively."
  7685. (interactive "P")
  7686. (cond
  7687. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7688. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7689. (t
  7690. (org-call-with-arg 'org-sort-entries with-case))))
  7691. (defun org-sort-remove-invisible (s)
  7692. "Remove invisible links from string S."
  7693. (remove-text-properties 0 (length s) org-rm-props s)
  7694. (while (string-match org-bracket-link-regexp s)
  7695. (setq s (replace-match (if (match-end 2)
  7696. (match-string 3 s)
  7697. (match-string 1 s)) t t s)))
  7698. (let ((st (format " %s " s)))
  7699. (while (string-match org-emph-re st)
  7700. (setq st (replace-match (format " %s " (match-string 4 st)) t t st)))
  7701. (setq s (substring st 1 -1)))
  7702. s)
  7703. (defvar org-priority-regexp) ; defined later in the file
  7704. (defvar org-after-sorting-entries-or-items-hook nil
  7705. "Hook that is run after a bunch of entries or items have been sorted.
  7706. When children are sorted, the cursor is in the parent line when this
  7707. hook gets called. When a region or a plain list is sorted, the cursor
  7708. will be in the first entry of the sorted region/list.")
  7709. (defun org-sort-entries
  7710. (&optional with-case sorting-type getkey-func compare-func property)
  7711. "Sort entries on a certain level of an outline tree.
  7712. If there is an active region, the entries in the region are sorted.
  7713. Else, if the cursor is before the first entry, sort the top-level items.
  7714. Else, the children of the entry at point are sorted.
  7715. Sorting can be alphabetically, numerically, by date/time as given by
  7716. a time stamp, by a property, by priority order, or by a custom function.
  7717. The command prompts for the sorting type unless it has been given to the
  7718. function through the SORTING-TYPE argument, which needs to be a character,
  7719. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?o ?O ?r ?R ?f ?F). Here is the
  7720. precise meaning of each character:
  7721. n Numerically, by converting the beginning of the entry/item to a number.
  7722. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7723. o By order of TODO keywords.
  7724. t By date/time, either the first active time stamp in the entry, or, if
  7725. none exist, by the first inactive one.
  7726. s By the scheduled date/time.
  7727. d By deadline date/time.
  7728. c By creation time, which is assumed to be the first inactive time stamp
  7729. at the beginning of a line.
  7730. p By priority according to the cookie.
  7731. r By the value of a property.
  7732. Capital letters will reverse the sort order.
  7733. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7734. called with point at the beginning of the record. It must return either
  7735. a string or a number that should serve as the sorting key for that record.
  7736. Comparing entries ignores case by default. However, with an optional argument
  7737. WITH-CASE, the sorting considers case as well.
  7738. Sorting is done against the visible part of the headlines, it ignores hidden
  7739. links."
  7740. (interactive "P")
  7741. (let ((case-func (if with-case 'identity 'downcase))
  7742. (cmstr
  7743. ;; The clock marker is lost when using `sort-subr', let's
  7744. ;; store the clocking string.
  7745. (when (equal (marker-buffer org-clock-marker) (current-buffer))
  7746. (save-excursion
  7747. (goto-char org-clock-marker)
  7748. (looking-back "^.*") (match-string-no-properties 0))))
  7749. start beg end stars re re2
  7750. txt what tmp)
  7751. ;; Find beginning and end of region to sort
  7752. (cond
  7753. ((org-region-active-p)
  7754. ;; we will sort the region
  7755. (setq end (region-end)
  7756. what "region")
  7757. (goto-char (region-beginning))
  7758. (if (not (org-at-heading-p)) (outline-next-heading))
  7759. (setq start (point)))
  7760. ((or (org-at-heading-p)
  7761. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  7762. ;; we will sort the children of the current headline
  7763. (org-back-to-heading)
  7764. (setq start (point)
  7765. end (progn (org-end-of-subtree t t)
  7766. (or (bolp) (insert "\n"))
  7767. (org-back-over-empty-lines)
  7768. (point))
  7769. what "children")
  7770. (goto-char start)
  7771. (show-subtree)
  7772. (outline-next-heading))
  7773. (t
  7774. ;; we will sort the top-level entries in this file
  7775. (goto-char (point-min))
  7776. (or (org-at-heading-p) (outline-next-heading))
  7777. (setq start (point))
  7778. (goto-char (point-max))
  7779. (beginning-of-line 1)
  7780. (when (looking-at ".*?\\S-")
  7781. ;; File ends in a non-white line
  7782. (end-of-line 1)
  7783. (insert "\n"))
  7784. (setq end (point-max))
  7785. (setq what "top-level")
  7786. (goto-char start)
  7787. (show-all)))
  7788. (setq beg (point))
  7789. (if (>= beg end) (user-error "Nothing to sort"))
  7790. (looking-at "\\(\\*+\\)")
  7791. (setq stars (match-string 1)
  7792. re (concat "^" (regexp-quote stars) " +")
  7793. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  7794. txt (buffer-substring beg end))
  7795. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  7796. (if (and (not (equal stars "*")) (string-match re2 txt))
  7797. (user-error "Region to sort contains a level above the first entry"))
  7798. (unless sorting-type
  7799. (message
  7800. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  7801. [t]ime [s]cheduled [d]eadline [c]reated
  7802. A/N/P/R/O/F/T/S/D/C means reversed:"
  7803. what)
  7804. (setq sorting-type (read-char-exclusive))
  7805. (unless getkey-func
  7806. (and (= (downcase sorting-type) ?f)
  7807. (setq getkey-func
  7808. (org-icompleting-read "Sort using function: "
  7809. obarray 'fboundp t nil nil))
  7810. (setq getkey-func (intern getkey-func))))
  7811. (and (= (downcase sorting-type) ?r)
  7812. (not property)
  7813. (setq property
  7814. (org-icompleting-read "Property: "
  7815. (mapcar 'list (org-buffer-property-keys t))
  7816. nil t))))
  7817. (message "Sorting entries...")
  7818. (save-restriction
  7819. (narrow-to-region start end)
  7820. (let ((dcst (downcase sorting-type))
  7821. (case-fold-search nil)
  7822. (now (current-time)))
  7823. (sort-subr
  7824. (/= dcst sorting-type)
  7825. ;; This function moves to the beginning character of the "record" to
  7826. ;; be sorted.
  7827. (lambda nil
  7828. (if (re-search-forward re nil t)
  7829. (goto-char (match-beginning 0))
  7830. (goto-char (point-max))))
  7831. ;; This function moves to the last character of the "record" being
  7832. ;; sorted.
  7833. (lambda nil
  7834. (save-match-data
  7835. (condition-case nil
  7836. (outline-forward-same-level 1)
  7837. (error
  7838. (goto-char (point-max))))))
  7839. ;; This function returns the value that gets sorted against.
  7840. (lambda nil
  7841. (cond
  7842. ((= dcst ?n)
  7843. (if (looking-at org-complex-heading-regexp)
  7844. (string-to-number (org-sort-remove-invisible (match-string 4)))
  7845. nil))
  7846. ((= dcst ?a)
  7847. (if (looking-at org-complex-heading-regexp)
  7848. (funcall case-func (org-sort-remove-invisible (match-string 4)))
  7849. nil))
  7850. ((= dcst ?t)
  7851. (let ((end (save-excursion (outline-next-heading) (point))))
  7852. (if (or (re-search-forward org-ts-regexp end t)
  7853. (re-search-forward org-ts-regexp-both end t))
  7854. (org-time-string-to-seconds (match-string 0))
  7855. (org-float-time now))))
  7856. ((= dcst ?c)
  7857. (let ((end (save-excursion (outline-next-heading) (point))))
  7858. (if (re-search-forward
  7859. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  7860. end t)
  7861. (org-time-string-to-seconds (match-string 0))
  7862. (org-float-time now))))
  7863. ((= dcst ?s)
  7864. (let ((end (save-excursion (outline-next-heading) (point))))
  7865. (if (re-search-forward org-scheduled-time-regexp end t)
  7866. (org-time-string-to-seconds (match-string 1))
  7867. (org-float-time now))))
  7868. ((= dcst ?d)
  7869. (let ((end (save-excursion (outline-next-heading) (point))))
  7870. (if (re-search-forward org-deadline-time-regexp end t)
  7871. (org-time-string-to-seconds (match-string 1))
  7872. (org-float-time now))))
  7873. ((= dcst ?p)
  7874. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  7875. (string-to-char (match-string 2))
  7876. org-default-priority))
  7877. ((= dcst ?r)
  7878. (or (org-entry-get nil property) ""))
  7879. ((= dcst ?o)
  7880. (if (looking-at org-complex-heading-regexp)
  7881. (- 9999 (length (member (match-string 2)
  7882. org-todo-keywords-1)))))
  7883. ((= dcst ?f)
  7884. (if getkey-func
  7885. (progn
  7886. (setq tmp (funcall getkey-func))
  7887. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  7888. tmp)
  7889. (error "Invalid key function `%s'" getkey-func)))
  7890. (t (error "Invalid sorting type `%c'" sorting-type))))
  7891. nil
  7892. (cond
  7893. ((= dcst ?a) 'string<)
  7894. ((= dcst ?f) compare-func)
  7895. ((member dcst '(?p ?t ?s ?d ?c)) '<)))))
  7896. (run-hooks 'org-after-sorting-entries-or-items-hook)
  7897. ;; Reset the clock marker if needed
  7898. (when cmstr
  7899. (save-excursion
  7900. (goto-char start)
  7901. (search-forward cmstr nil t)
  7902. (move-marker org-clock-marker (point))))
  7903. (message "Sorting entries...done")))
  7904. (defun org-do-sort (table what &optional with-case sorting-type)
  7905. "Sort TABLE of WHAT according to SORTING-TYPE.
  7906. The user will be prompted for the SORTING-TYPE if the call to this
  7907. function does not specify it. WHAT is only for the prompt, to indicate
  7908. what is being sorted. The sorting key will be extracted from
  7909. the car of the elements of the table.
  7910. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  7911. (unless sorting-type
  7912. (message
  7913. "Sort %s: [a]lphabetic, [n]umeric, [t]ime. A/N/T means reversed:"
  7914. what)
  7915. (setq sorting-type (read-char-exclusive)))
  7916. (let ((dcst (downcase sorting-type))
  7917. extractfun comparefun)
  7918. ;; Define the appropriate functions
  7919. (cond
  7920. ((= dcst ?n)
  7921. (setq extractfun 'string-to-number
  7922. comparefun (if (= dcst sorting-type) '< '>)))
  7923. ((= dcst ?a)
  7924. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  7925. (lambda(x) (downcase (org-sort-remove-invisible x))))
  7926. comparefun (if (= dcst sorting-type)
  7927. 'string<
  7928. (lambda (a b) (and (not (string< a b))
  7929. (not (string= a b)))))))
  7930. ((= dcst ?t)
  7931. (setq extractfun
  7932. (lambda (x)
  7933. (if (or (string-match org-ts-regexp x)
  7934. (string-match org-ts-regexp-both x))
  7935. (org-float-time
  7936. (org-time-string-to-time (match-string 0 x)))
  7937. 0))
  7938. comparefun (if (= dcst sorting-type) '< '>)))
  7939. (t (error "Invalid sorting type `%c'" sorting-type)))
  7940. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  7941. table)
  7942. (lambda (a b) (funcall comparefun (car a) (car b))))))
  7943. ;;; The orgstruct minor mode
  7944. ;; Define a minor mode which can be used in other modes in order to
  7945. ;; integrate the org-mode structure editing commands.
  7946. ;; This is really a hack, because the org-mode structure commands use
  7947. ;; keys which normally belong to the major mode. Here is how it
  7948. ;; works: The minor mode defines all the keys necessary to operate the
  7949. ;; structure commands, but wraps the commands into a function which
  7950. ;; tests if the cursor is currently at a headline or a plain list
  7951. ;; item. If that is the case, the structure command is used,
  7952. ;; temporarily setting many Org-mode variables like regular
  7953. ;; expressions for filling etc. However, when any of those keys is
  7954. ;; used at a different location, function uses `key-binding' to look
  7955. ;; up if the key has an associated command in another currently active
  7956. ;; keymap (minor modes, major mode, global), and executes that
  7957. ;; command. There might be problems if any of the keys is otherwise
  7958. ;; used as a prefix key.
  7959. (defcustom orgstruct-heading-prefix-regexp nil
  7960. "Regexp that matches the custom prefix of Org headlines in
  7961. orgstruct(++)-mode."
  7962. :group 'org
  7963. :version "24.4"
  7964. :package-version '(Org . "8.0")
  7965. :type 'string)
  7966. ;;;###autoload(put 'orgstruct-heading-prefix-regexp 'safe-local-variable 'stringp)
  7967. (defcustom orgstruct-setup-hook nil
  7968. "Hook run after orgstruct-mode-map is filled."
  7969. :group 'org
  7970. :version "24.4"
  7971. :package-version '(Org . "8.0")
  7972. :type 'hook)
  7973. (defvar orgstruct-initialized nil)
  7974. (defvar org-local-vars nil
  7975. "List of local variables, for use by `orgstruct-mode'.")
  7976. ;;;###autoload
  7977. (define-minor-mode orgstruct-mode
  7978. "Toggle the minor mode `orgstruct-mode'.
  7979. This mode is for using Org-mode structure commands in other
  7980. modes. The following keys behave as if Org-mode were active, if
  7981. the cursor is on a headline, or on a plain list item (both as
  7982. defined by Org-mode)."
  7983. nil " OrgStruct" (make-sparse-keymap)
  7984. (funcall (if orgstruct-mode
  7985. 'add-to-invisibility-spec
  7986. 'remove-from-invisibility-spec)
  7987. '(outline . t))
  7988. (when orgstruct-mode
  7989. (org-load-modules-maybe)
  7990. (unless orgstruct-initialized
  7991. (orgstruct-setup)
  7992. (setq orgstruct-initialized t))))
  7993. ;;;###autoload
  7994. (defun turn-on-orgstruct ()
  7995. "Unconditionally turn on `orgstruct-mode'."
  7996. (orgstruct-mode 1))
  7997. (defvar org-fb-vars nil)
  7998. (make-variable-buffer-local 'org-fb-vars)
  7999. (defun orgstruct++-mode (&optional arg)
  8000. "Toggle `orgstruct-mode', the enhanced version of it.
  8001. In addition to setting orgstruct-mode, this also exports all
  8002. indentation and autofilling variables from org-mode into the
  8003. buffer. It will also recognize item context in multiline items."
  8004. (interactive "P")
  8005. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  8006. (if (< arg 1)
  8007. (progn (orgstruct-mode -1)
  8008. (mapc (lambda(v)
  8009. (org-set-local (car v)
  8010. (if (eq (car-safe (cadr v)) 'quote) (cadadr v) (cadr v))))
  8011. org-fb-vars))
  8012. (orgstruct-mode 1)
  8013. (setq org-fb-vars nil)
  8014. (unless org-local-vars
  8015. (setq org-local-vars (org-get-local-variables)))
  8016. (let (var val)
  8017. (mapc
  8018. (lambda (x)
  8019. (when (string-match
  8020. "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|fill-prefix\\|indent-\\)"
  8021. (symbol-name (car x)))
  8022. (setq var (car x) val (nth 1 x))
  8023. (push (list var `(quote ,(eval var))) org-fb-vars)
  8024. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  8025. org-local-vars)
  8026. (org-set-local 'orgstruct-is-++ t))))
  8027. (defvar orgstruct-is-++ nil
  8028. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  8029. (make-variable-buffer-local 'orgstruct-is-++)
  8030. ;;;###autoload
  8031. (defun turn-on-orgstruct++ ()
  8032. "Unconditionally turn on `orgstruct++-mode'."
  8033. (orgstruct++-mode 1))
  8034. (defun orgstruct-error ()
  8035. "Error when there is no default binding for a structure key."
  8036. (interactive)
  8037. (funcall (if (fboundp 'user-error)
  8038. 'user-error
  8039. 'error)
  8040. "This key has no function outside structure elements"))
  8041. (defun orgstruct-setup ()
  8042. "Setup orgstruct keymap."
  8043. (dolist (cell '((org-demote . t)
  8044. (org-metaleft . t)
  8045. (org-metaright . t)
  8046. (org-promote . t)
  8047. (org-shiftmetaleft . t)
  8048. (org-shiftmetaright . t)
  8049. org-backward-element
  8050. org-backward-heading-same-level
  8051. org-ctrl-c-ret
  8052. org-ctrl-c-minus
  8053. org-ctrl-c-star
  8054. org-cycle
  8055. org-forward-heading-same-level
  8056. org-insert-heading
  8057. org-insert-heading-respect-content
  8058. org-kill-note-or-show-branches
  8059. org-mark-subtree
  8060. org-meta-return
  8061. org-metadown
  8062. org-metaup
  8063. org-narrow-to-subtree
  8064. org-promote-subtree
  8065. org-reveal
  8066. org-shiftdown
  8067. org-shiftleft
  8068. org-shiftmetadown
  8069. org-shiftmetaup
  8070. org-shiftright
  8071. org-shifttab
  8072. org-shifttab
  8073. org-shiftup
  8074. org-show-subtree
  8075. org-sort
  8076. org-up-element
  8077. outline-demote
  8078. outline-next-visible-heading
  8079. outline-previous-visible-heading
  8080. outline-promote
  8081. outline-up-heading
  8082. show-children))
  8083. (let ((f (or (car-safe cell) cell))
  8084. (disable-when-heading-prefix (cdr-safe cell)))
  8085. (when (fboundp f)
  8086. (let ((new-bindings))
  8087. (dolist (binding (nconc (where-is-internal f org-mode-map)
  8088. (where-is-internal f outline-mode-map)))
  8089. (push binding new-bindings)
  8090. ;; TODO use local-function-key-map
  8091. (dolist (rep '(("<tab>" . "TAB")
  8092. ("<return>" . "RET")
  8093. ("<escape>" . "ESC")
  8094. ("<delete>" . "DEL")))
  8095. (setq binding (read-kbd-macro
  8096. (let ((case-fold-search))
  8097. (replace-regexp-in-string
  8098. (regexp-quote (cdr rep))
  8099. (car rep)
  8100. (key-description binding)))))
  8101. (pushnew binding new-bindings :test 'equal)))
  8102. (dolist (binding new-bindings)
  8103. (let ((key (lookup-key orgstruct-mode-map binding)))
  8104. (when (or (not key) (numberp key))
  8105. (condition-case nil
  8106. (org-defkey orgstruct-mode-map
  8107. binding
  8108. (orgstruct-make-binding f binding disable-when-heading-prefix))
  8109. (error nil)))))))))
  8110. (run-hooks 'orgstruct-setup-hook))
  8111. (defun orgstruct-make-binding (fun key disable-when-heading-prefix)
  8112. "Create a function for binding in the structure minor mode.
  8113. FUN is the command to call inside a table. KEY is the key that
  8114. should be checked in for a command to execute outside of tables.
  8115. Non-nil DISABLE-WHEN-HEADING-PREFIX means to disable the command
  8116. if `orgstruct-heading-prefix-regexp' is non-nil."
  8117. (let ((name (concat "orgstruct-hijacker-" (symbol-name fun))))
  8118. (let ((nname name)
  8119. (i 0))
  8120. (while (fboundp (intern nname))
  8121. (setq nname (format "%s-%d" name (setq i (1+ i)))))
  8122. (setq name (intern nname)))
  8123. (eval
  8124. (let ((bindings '((org-heading-regexp
  8125. (concat "^"
  8126. orgstruct-heading-prefix-regexp
  8127. "\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ ]*$"))
  8128. (org-outline-regexp
  8129. (concat orgstruct-heading-prefix-regexp "\\*+ "))
  8130. (org-outline-regexp-bol
  8131. (concat "^" org-outline-regexp))
  8132. (outline-regexp org-outline-regexp)
  8133. (outline-heading-end-regexp "\n")
  8134. (outline-level 'org-outline-level)
  8135. (outline-heading-alist))))
  8136. `(defun ,name (arg)
  8137. ,(concat "In Structure, run `" (symbol-name fun) "'.\n"
  8138. "Outside of structure, run the binding of `"
  8139. (key-description key) "'."
  8140. (when disable-when-heading-prefix
  8141. (concat
  8142. "\nIf `orgstruct-heading-prefix-regexp' is non-nil, this command will always fall\n"
  8143. "back to the default binding due to limitations of Org's implementation of\n"
  8144. "`" (symbol-name fun) "'.")))
  8145. (interactive "p")
  8146. (let* ((disable
  8147. ,(when disable-when-heading-prefix
  8148. '(and orgstruct-heading-prefix-regexp
  8149. (not (string= orgstruct-heading-prefix-regexp "")))))
  8150. (fallback
  8151. (or disable
  8152. (not
  8153. (let* ,bindings
  8154. (org-context-p 'headline 'item
  8155. ,(when (memq fun
  8156. '(org-insert-heading
  8157. org-insert-heading-respect-content
  8158. org-meta-return))
  8159. '(when orgstruct-is-++
  8160. 'item-body))))))))
  8161. (if fallback
  8162. (let* ((orgstruct-mode)
  8163. (binding
  8164. (loop with key = ,key
  8165. for rep in
  8166. '(nil
  8167. ("<\\([^>]*\\)tab>" . "\\1TAB")
  8168. ("<\\([^>]*\\)return>" . "\\1RET")
  8169. ("<\\([^>]*\\)escape>" . "\\1ESC")
  8170. ("<\\([^>]*\\)delete>" . "\\1DEL"))
  8171. do
  8172. (when rep
  8173. (setq key (read-kbd-macro
  8174. (let ((case-fold-search))
  8175. (replace-regexp-in-string
  8176. (car rep)
  8177. (cdr rep)
  8178. (key-description key))))))
  8179. thereis (key-binding key))))
  8180. (if (keymapp binding)
  8181. (set-temporary-overlay-map binding)
  8182. (let ((func (or binding
  8183. (unless disable
  8184. 'orgstruct-error))))
  8185. (when func
  8186. (call-interactively func)))))
  8187. (org-run-like-in-org-mode
  8188. (lambda ()
  8189. (interactive)
  8190. (let* ,bindings
  8191. (call-interactively ',fun)))))))))
  8192. name))
  8193. (defun org-contextualize-keys (alist contexts)
  8194. "Return valid elements in ALIST depending on CONTEXTS.
  8195. `org-agenda-custom-commands' or `org-capture-templates' are the
  8196. values used for ALIST, and `org-agenda-custom-commands-contexts'
  8197. or `org-capture-templates-contexts' are the associated contexts
  8198. definitions."
  8199. (let ((contexts
  8200. ;; normalize contexts
  8201. (mapcar
  8202. (lambda(c) (cond ((listp (cadr c))
  8203. (list (car c) (car c) (cadr c)))
  8204. ((string= "" (cadr c))
  8205. (list (car c) (car c) (caddr c)))
  8206. (t c))) contexts))
  8207. (a alist) c r s)
  8208. ;; loop over all commands or templates
  8209. (while (setq c (pop a))
  8210. (let (vrules repl)
  8211. (cond
  8212. ((not (assoc (car c) contexts))
  8213. (push c r))
  8214. ((and (assoc (car c) contexts)
  8215. (setq vrules (org-contextualize-validate-key
  8216. (car c) contexts)))
  8217. (mapc (lambda (vr)
  8218. (when (not (equal (car vr) (cadr vr)))
  8219. (setq repl vr))) vrules)
  8220. (if (not repl) (push c r)
  8221. (push (cadr repl) s)
  8222. (push
  8223. (cons (car c)
  8224. (cdr (or (assoc (cadr repl) alist)
  8225. (error "Undefined key `%s' as contextual replacement for `%s'"
  8226. (cadr repl) (car c)))))
  8227. r))))))
  8228. ;; Return limited ALIST, possibly with keys modified, and deduplicated
  8229. (delq
  8230. nil
  8231. (delete-dups
  8232. (mapcar (lambda (x)
  8233. (let ((tpl (car x)))
  8234. (when (not (delq
  8235. nil
  8236. (mapcar (lambda(y)
  8237. (equal y tpl)) s))) x)))
  8238. (reverse r))))))
  8239. (defun org-contextualize-validate-key (key contexts)
  8240. "Check CONTEXTS for agenda or capture KEY."
  8241. (let (r rr res)
  8242. (while (setq r (pop contexts))
  8243. (mapc
  8244. (lambda (rr)
  8245. (when
  8246. (and (equal key (car r))
  8247. (if (functionp rr) (funcall rr)
  8248. (or (and (eq (car rr) 'in-file)
  8249. (buffer-file-name)
  8250. (string-match (cdr rr) (buffer-file-name)))
  8251. (and (eq (car rr) 'in-mode)
  8252. (string-match (cdr rr) (symbol-name major-mode)))
  8253. (and (eq (car rr) 'in-buffer)
  8254. (string-match (cdr rr) (buffer-name)))
  8255. (when (and (eq (car rr) 'not-in-file)
  8256. (buffer-file-name))
  8257. (not (string-match (cdr rr) (buffer-file-name))))
  8258. (when (eq (car rr) 'not-in-mode)
  8259. (not (string-match (cdr rr) (symbol-name major-mode))))
  8260. (when (eq (car rr) 'not-in-buffer)
  8261. (not (string-match (cdr rr) (buffer-name)))))))
  8262. (push r res)))
  8263. (car (last r))))
  8264. (delete-dups (delq nil res))))
  8265. (defun org-context-p (&rest contexts)
  8266. "Check if local context is any of CONTEXTS.
  8267. Possible values in the list of contexts are `table', `headline', and `item'."
  8268. (let ((pos (point)))
  8269. (goto-char (point-at-bol))
  8270. (prog1 (or (and (memq 'table contexts)
  8271. (looking-at "[ \t]*|"))
  8272. (and (memq 'headline contexts)
  8273. (looking-at org-outline-regexp))
  8274. (and (memq 'item contexts)
  8275. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  8276. (and (memq 'item-body contexts)
  8277. (org-in-item-p)))
  8278. (goto-char pos))))
  8279. (defun org-get-local-variables ()
  8280. "Return a list of all local variables in an Org mode buffer."
  8281. (let (varlist)
  8282. (with-current-buffer (get-buffer-create "*Org tmp*")
  8283. (erase-buffer)
  8284. (org-mode)
  8285. (setq varlist (buffer-local-variables)))
  8286. (kill-buffer "*Org tmp*")
  8287. (delq nil
  8288. (mapcar
  8289. (lambda (x)
  8290. (setq x
  8291. (if (symbolp x)
  8292. (list x)
  8293. (list (car x) (cdr x))))
  8294. (if (and (not (get (car x) 'org-state))
  8295. (string-match
  8296. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
  8297. (symbol-name (car x))))
  8298. x nil))
  8299. varlist))))
  8300. (defun org-clone-local-variables (from-buffer &optional regexp)
  8301. "Clone local variables from FROM-BUFFER.
  8302. Optional argument REGEXP selects variables to clone."
  8303. (mapc
  8304. (lambda (pair)
  8305. (and (symbolp (car pair))
  8306. (or (null regexp)
  8307. (string-match regexp (symbol-name (car pair))))
  8308. (set (make-local-variable (car pair))
  8309. (cdr pair))))
  8310. (buffer-local-variables from-buffer)))
  8311. ;;;###autoload
  8312. (defun org-run-like-in-org-mode (cmd)
  8313. "Run a command, pretending that the current buffer is in Org-mode.
  8314. This will temporarily bind local variables that are typically bound in
  8315. Org-mode to the values they have in Org-mode, and then interactively
  8316. call CMD."
  8317. (org-load-modules-maybe)
  8318. (unless org-local-vars
  8319. (setq org-local-vars (org-get-local-variables)))
  8320. (let (binds)
  8321. (dolist (var org-local-vars)
  8322. (when (or (not (boundp (car var)))
  8323. (eq (symbol-value (car var))
  8324. (default-value (car var))))
  8325. (push (list (car var) `(quote ,(cadr var))) binds)))
  8326. (eval `(let ,binds
  8327. (call-interactively (quote ,cmd))))))
  8328. ;;;; Archiving
  8329. (defun org-get-category (&optional pos force-refresh)
  8330. "Get the category applying to position POS."
  8331. (save-match-data
  8332. (if force-refresh (org-refresh-category-properties))
  8333. (let ((pos (or pos (point))))
  8334. (or (get-text-property pos 'org-category)
  8335. (progn (org-refresh-category-properties)
  8336. (get-text-property pos 'org-category))))))
  8337. (defun org-refresh-category-properties ()
  8338. "Refresh category text properties in the buffer."
  8339. (let ((case-fold-search t)
  8340. (inhibit-read-only t)
  8341. (def-cat (cond
  8342. ((null org-category)
  8343. (if buffer-file-name
  8344. (file-name-sans-extension
  8345. (file-name-nondirectory buffer-file-name))
  8346. "???"))
  8347. ((symbolp org-category) (symbol-name org-category))
  8348. (t org-category)))
  8349. beg end cat pos optionp)
  8350. (org-with-silent-modifications
  8351. (save-excursion
  8352. (save-restriction
  8353. (widen)
  8354. (goto-char (point-min))
  8355. (put-text-property (point) (point-max) 'org-category def-cat)
  8356. (while (re-search-forward
  8357. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  8358. (setq pos (match-end 0)
  8359. optionp (equal (char-after (match-beginning 0)) ?#)
  8360. cat (org-trim (match-string 2)))
  8361. (if optionp
  8362. (setq beg (point-at-bol) end (point-max))
  8363. (org-back-to-heading t)
  8364. (setq beg (point) end (org-end-of-subtree t t)))
  8365. (put-text-property beg end 'org-category cat)
  8366. (put-text-property beg end 'org-category-position beg)
  8367. (goto-char pos)))))))
  8368. (defun org-refresh-properties (dprop tprop)
  8369. "Refresh buffer text properties.
  8370. DPROP is the drawer property and TPROP is the corresponding text
  8371. property to set."
  8372. (let ((case-fold-search t)
  8373. (inhibit-read-only t) p)
  8374. (org-with-silent-modifications
  8375. (save-excursion
  8376. (save-restriction
  8377. (widen)
  8378. (goto-char (point-min))
  8379. (while (re-search-forward (concat "^[ \t]*:" dprop ": +\\(.*\\)[ \t]*$") nil t)
  8380. (setq p (org-match-string-no-properties 1))
  8381. (save-excursion
  8382. (org-back-to-heading t)
  8383. (put-text-property
  8384. (point-at-bol) (org-end-of-subtree t t) tprop p))))))))
  8385. ;;;; Link Stuff
  8386. ;;; Link abbreviations
  8387. (defun org-link-expand-abbrev (link)
  8388. "Apply replacements as defined in `org-link-abbrev-alist'."
  8389. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  8390. (let* ((key (match-string 1 link))
  8391. (as (or (assoc key org-link-abbrev-alist-local)
  8392. (assoc key org-link-abbrev-alist)))
  8393. (tag (and (match-end 2) (match-string 3 link)))
  8394. rpl)
  8395. (if (not as)
  8396. link
  8397. (setq rpl (cdr as))
  8398. (cond
  8399. ((symbolp rpl) (funcall rpl tag))
  8400. ((string-match "%(\\([^)]+\\))" rpl)
  8401. (replace-match
  8402. (save-match-data
  8403. (funcall (intern-soft (match-string 1 rpl)) tag)) t t rpl))
  8404. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  8405. ((string-match "%h" rpl)
  8406. (replace-match (url-hexify-string (or tag "")) t t rpl))
  8407. (t (concat rpl tag)))))
  8408. link))
  8409. ;;; Storing and inserting links
  8410. (defvar org-insert-link-history nil
  8411. "Minibuffer history for links inserted with `org-insert-link'.")
  8412. (defvar org-stored-links nil
  8413. "Contains the links stored with `org-store-link'.")
  8414. (defvar org-store-link-plist nil
  8415. "Plist with info about the most recently link created with `org-store-link'.")
  8416. (defvar org-link-protocols nil
  8417. "Link protocols added to Org-mode using `org-add-link-type'.")
  8418. (defvar org-store-link-functions nil
  8419. "List of functions that are called to create and store a link.
  8420. Each function will be called in turn until one returns a non-nil
  8421. value. Each function should check if it is responsible for creating
  8422. this link (for example by looking at the major mode).
  8423. If not, it must exit and return nil.
  8424. If yes, it should return a non-nil value after a calling
  8425. `org-store-link-props' with a list of properties and values.
  8426. Special properties are:
  8427. :type The link prefix, like \"http\". This must be given.
  8428. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  8429. This is obligatory as well.
  8430. :description Optional default description for the second pair
  8431. of brackets in an Org-mode link. The user can still change
  8432. this when inserting this link into an Org-mode buffer.
  8433. In addition to these, any additional properties can be specified
  8434. and then used in capture templates.")
  8435. (defun org-add-link-type (type &optional follow export)
  8436. "Add TYPE to the list of `org-link-types'.
  8437. Re-compute all regular expressions depending on `org-link-types'
  8438. FOLLOW and EXPORT are two functions.
  8439. FOLLOW should take the link path as the single argument and do whatever
  8440. is necessary to follow the link, for example find a file or display
  8441. a mail message.
  8442. EXPORT should format the link path for export to one of the export formats.
  8443. It should be a function accepting three arguments:
  8444. path the path of the link, the text after the prefix (like \"http:\")
  8445. desc the description of the link, if any, or a description added by
  8446. org-export-normalize-links if there is none
  8447. format the export format, a symbol like `html' or `latex' or `ascii'..
  8448. The function may use the FORMAT information to return different values
  8449. depending on the format. The return value will be put literally into
  8450. the exported file. If the return value is nil, this means Org should
  8451. do what it normally does with links which do not have EXPORT defined.
  8452. Org-mode has a built-in default for exporting links. If you are happy with
  8453. this default, there is no need to define an export function for the link
  8454. type. For a simple example of an export function, see `org-bbdb.el'."
  8455. (add-to-list 'org-link-types type t)
  8456. (org-make-link-regexps)
  8457. (if (assoc type org-link-protocols)
  8458. (setcdr (assoc type org-link-protocols) (list follow export))
  8459. (push (list type follow export) org-link-protocols)))
  8460. (defvar org-agenda-buffer-name) ; Defined in org-agenda.el
  8461. (defvar org-id-link-to-org-use-id) ; Defined in org-id.el
  8462. ;;;###autoload
  8463. (defun org-store-link (arg)
  8464. "\\<org-mode-map>Store an org-link to the current location.
  8465. This link is added to `org-stored-links' and can later be inserted
  8466. into an org-buffer with \\[org-insert-link].
  8467. For some link types, a prefix arg is interpreted.
  8468. For links to Usenet articles, arg negates `org-gnus-prefer-web-links'.
  8469. For file links, arg negates `org-context-in-file-links'.
  8470. A double prefix arg force skipping storing functions that are not
  8471. part of Org's core.
  8472. A triple prefix arg force storing a link for each line in the
  8473. active region."
  8474. (interactive "P")
  8475. (org-load-modules-maybe)
  8476. (if (and (equal arg '(64)) (org-region-active-p))
  8477. (save-excursion
  8478. (let ((end (region-end)))
  8479. (goto-char (region-beginning))
  8480. (set-mark (point))
  8481. (while (< (point-at-eol) end)
  8482. (move-end-of-line 1) (activate-mark)
  8483. (let (current-prefix-arg)
  8484. (call-interactively 'org-store-link))
  8485. (move-beginning-of-line 2)
  8486. (set-mark (point)))))
  8487. (org-with-limited-levels
  8488. (setq org-store-link-plist nil)
  8489. (let (link cpltxt desc description search
  8490. txt custom-id agenda-link sfuns sfunsn)
  8491. (cond
  8492. ;; Store a link using an external link type
  8493. ((and (not (equal arg '(16)))
  8494. (setq sfuns
  8495. (delq
  8496. nil (mapcar (lambda (f)
  8497. (let (fs) (if (funcall f) (push f fs))))
  8498. org-store-link-functions))
  8499. sfunsn (mapcar (lambda (fu) (symbol-name (car fu))) sfuns))
  8500. (or (and (cdr sfuns)
  8501. (funcall (intern
  8502. (completing-read
  8503. "Which function for creating the link? "
  8504. sfunsn t (car sfunsn)))))
  8505. (funcall (caar sfuns)))
  8506. (setq link (plist-get org-store-link-plist :link)
  8507. desc (or (plist-get org-store-link-plist
  8508. :description) link))))
  8509. ;; Store a link from a source code buffer
  8510. ((org-src-edit-buffer-p)
  8511. (let (label gc)
  8512. (while (or (not label)
  8513. (save-excursion
  8514. (save-restriction
  8515. (widen)
  8516. (goto-char (point-min))
  8517. (re-search-forward
  8518. (regexp-quote (format org-coderef-label-format label))
  8519. nil t))))
  8520. (when label (message "Label exists already") (sit-for 2))
  8521. (setq label (read-string "Code line label: " label)))
  8522. (end-of-line 1)
  8523. (setq link (format org-coderef-label-format label))
  8524. (setq gc (- 79 (length link)))
  8525. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  8526. (insert link)
  8527. (setq link (concat "(" label ")") desc nil)))
  8528. ;; We are in the agenda, link to referenced location
  8529. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  8530. (let ((m (or (get-text-property (point) 'org-hd-marker)
  8531. (get-text-property (point) 'org-marker))))
  8532. (when m
  8533. (org-with-point-at m
  8534. (setq agenda-link
  8535. (if (org-called-interactively-p 'any)
  8536. (call-interactively 'org-store-link)
  8537. (org-store-link nil)))))))
  8538. ((eq major-mode 'calendar-mode)
  8539. (let ((cd (calendar-cursor-to-date)))
  8540. (setq link
  8541. (format-time-string
  8542. (car org-time-stamp-formats)
  8543. (apply 'encode-time
  8544. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  8545. nil nil nil))))
  8546. (org-store-link-props :type "calendar" :date cd)))
  8547. ((eq major-mode 'help-mode)
  8548. (setq link (concat "help:" (save-excursion
  8549. (goto-char (point-min))
  8550. (looking-at "^[^ ]+")
  8551. (match-string 0))))
  8552. (org-store-link-props :type "help"))
  8553. ((eq major-mode 'w3-mode)
  8554. (setq cpltxt (if (and (buffer-name)
  8555. (not (string-match "Untitled" (buffer-name))))
  8556. (buffer-name)
  8557. (url-view-url t))
  8558. link (url-view-url t))
  8559. (org-store-link-props :type "w3" :url (url-view-url t)))
  8560. ((eq major-mode 'image-mode)
  8561. (setq cpltxt (concat "file:"
  8562. (abbreviate-file-name buffer-file-name))
  8563. link cpltxt)
  8564. (org-store-link-props :type "image" :file buffer-file-name))
  8565. ;; In dired, store a link to the file of the current line
  8566. ((eq major-mode 'dired-mode)
  8567. (let ((file (dired-get-filename nil t)))
  8568. (setq file (if file
  8569. (abbreviate-file-name
  8570. (expand-file-name (dired-get-filename nil t)))
  8571. ;; otherwise, no file so use current directory.
  8572. default-directory))
  8573. (setq cpltxt (concat "file:" file)
  8574. link cpltxt)))
  8575. ((setq search (run-hook-with-args-until-success
  8576. 'org-create-file-search-functions))
  8577. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  8578. "::" search))
  8579. (setq cpltxt (or description link)))
  8580. ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
  8581. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  8582. (cond
  8583. ;; Store a link using the target at point
  8584. ((org-in-regexp "[^<]<<\\([^<>]+\\)>>[^>]" 1)
  8585. (setq cpltxt
  8586. (concat "file:"
  8587. (abbreviate-file-name
  8588. (buffer-file-name (buffer-base-buffer)))
  8589. "::" (match-string 1))
  8590. link cpltxt))
  8591. ((and (featurep 'org-id)
  8592. (or (eq org-id-link-to-org-use-id t)
  8593. (and (org-called-interactively-p 'any)
  8594. (or (eq org-id-link-to-org-use-id 'create-if-interactive)
  8595. (and (eq org-id-link-to-org-use-id
  8596. 'create-if-interactive-and-no-custom-id)
  8597. (not custom-id))))
  8598. (and org-id-link-to-org-use-id (org-entry-get nil "ID"))))
  8599. ;; Store a link using the ID at point
  8600. (setq link (condition-case nil
  8601. (prog1 (org-id-store-link)
  8602. (setq desc (plist-get org-store-link-plist
  8603. :description)))
  8604. (error
  8605. ;; Probably before first headline, link only to file
  8606. (concat "file:"
  8607. (abbreviate-file-name
  8608. (buffer-file-name (buffer-base-buffer))))))))
  8609. (t
  8610. ;; Just link to current headline
  8611. (setq cpltxt (concat "file:"
  8612. (abbreviate-file-name
  8613. (buffer-file-name (buffer-base-buffer)))))
  8614. ;; Add a context search string
  8615. (when (org-xor org-context-in-file-links arg)
  8616. (let* ((ee (org-element-at-point))
  8617. (et (org-element-type ee))
  8618. (ev (plist-get (cadr ee) :value))
  8619. (ek (plist-get (cadr ee) :key))
  8620. (eok (and (stringp ek) (string-match "name" ek))))
  8621. (setq txt (cond
  8622. ((org-at-heading-p) nil)
  8623. ((and (eq et 'keyword) eok) ev)
  8624. ((org-region-active-p)
  8625. (buffer-substring (region-beginning) (region-end)))))
  8626. (when (or (null txt) (string-match "\\S-" txt))
  8627. (setq cpltxt
  8628. (concat cpltxt "::"
  8629. (condition-case nil
  8630. (org-make-org-heading-search-string txt)
  8631. (error "")))
  8632. desc (or (and (eq et 'keyword) eok ev)
  8633. (nth 4 (ignore-errors (org-heading-components)))
  8634. "NONE")))))
  8635. (if (string-match "::\\'" cpltxt)
  8636. (setq cpltxt (substring cpltxt 0 -2)))
  8637. (setq link cpltxt))))
  8638. ((buffer-file-name (buffer-base-buffer))
  8639. ;; Just link to this file here.
  8640. (setq cpltxt (concat "file:"
  8641. (abbreviate-file-name
  8642. (buffer-file-name (buffer-base-buffer)))))
  8643. ;; Add a context string.
  8644. (when (org-xor org-context-in-file-links arg)
  8645. (setq txt (if (org-region-active-p)
  8646. (buffer-substring (region-beginning) (region-end))
  8647. (buffer-substring (point-at-bol) (point-at-eol))))
  8648. ;; Only use search option if there is some text.
  8649. (when (string-match "\\S-" txt)
  8650. (setq cpltxt
  8651. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  8652. desc "NONE")))
  8653. (setq link cpltxt))
  8654. ((org-called-interactively-p 'interactive)
  8655. (user-error "No method for storing a link from this buffer"))
  8656. (t (setq link nil)))
  8657. ;; We're done setting link and desc, clean up
  8658. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  8659. (setq link (or link cpltxt)
  8660. desc (or desc cpltxt))
  8661. (cond ((equal desc "NONE") (setq desc nil))
  8662. ((string-match org-bracket-link-analytic-regexp desc)
  8663. (let ((d0 (match-string 3 desc))
  8664. (p0 (match-string 5 desc)))
  8665. (setq desc
  8666. (replace-regexp-in-string
  8667. org-bracket-link-regexp
  8668. (concat (or p0 d0)
  8669. (if (equal (length (match-string 0 desc))
  8670. (length desc)) "*" "")) desc)))))
  8671. ;; Return the link
  8672. (if (not (and (or (org-called-interactively-p 'any)
  8673. executing-kbd-macro) link))
  8674. (or agenda-link (and link (org-make-link-string link desc)))
  8675. (push (list link desc) org-stored-links)
  8676. (message "Stored: %s" (or desc link))
  8677. (when custom-id
  8678. (setq link (concat "file:" (abbreviate-file-name
  8679. (buffer-file-name)) "::#" custom-id))
  8680. (push (list link desc) org-stored-links)))))))
  8681. (defun org-store-link-props (&rest plist)
  8682. "Store link properties, extract names and addresses."
  8683. (let (x adr)
  8684. (when (setq x (plist-get plist :from))
  8685. (setq adr (mail-extract-address-components x))
  8686. (setq plist (plist-put plist :fromname (car adr)))
  8687. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  8688. (when (setq x (plist-get plist :to))
  8689. (setq adr (mail-extract-address-components x))
  8690. (setq plist (plist-put plist :toname (car adr)))
  8691. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  8692. (let ((from (plist-get plist :from))
  8693. (to (plist-get plist :to)))
  8694. (when (and from to org-from-is-user-regexp)
  8695. (setq plist
  8696. (plist-put plist :fromto
  8697. (if (string-match org-from-is-user-regexp from)
  8698. (concat "to %t")
  8699. (concat "from %f"))))))
  8700. (setq org-store-link-plist plist))
  8701. (defun org-add-link-props (&rest plist)
  8702. "Add these properties to the link property list."
  8703. (let (key value)
  8704. (while plist
  8705. (setq key (pop plist) value (pop plist))
  8706. (setq org-store-link-plist
  8707. (plist-put org-store-link-plist key value)))))
  8708. (defun org-email-link-description (&optional fmt)
  8709. "Return the description part of an email link.
  8710. This takes information from `org-store-link-plist' and formats it
  8711. according to FMT (default from `org-email-link-description-format')."
  8712. (setq fmt (or fmt org-email-link-description-format))
  8713. (let* ((p org-store-link-plist)
  8714. (to (plist-get p :toaddress))
  8715. (from (plist-get p :fromaddress))
  8716. (table
  8717. (list
  8718. (cons "%c" (plist-get p :fromto))
  8719. (cons "%F" (plist-get p :from))
  8720. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  8721. (cons "%T" (plist-get p :to))
  8722. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  8723. (cons "%s" (plist-get p :subject))
  8724. (cons "%d" (plist-get p :date))
  8725. (cons "%m" (plist-get p :message-id)))))
  8726. (when (string-match "%c" fmt)
  8727. ;; Check if the user wrote this message
  8728. (if (and org-from-is-user-regexp from to
  8729. (save-match-data (string-match org-from-is-user-regexp from)))
  8730. (setq fmt (replace-match "to %t" t t fmt))
  8731. (setq fmt (replace-match "from %f" t t fmt))))
  8732. (org-replace-escapes fmt table)))
  8733. (defun org-make-org-heading-search-string (&optional string)
  8734. "Make search string for the current headline or STRING."
  8735. (let ((s (or string
  8736. (and (derived-mode-p 'org-mode)
  8737. (save-excursion
  8738. (org-back-to-heading t)
  8739. (org-element-property :raw-value (org-element-at-point))))))
  8740. (lines org-context-in-file-links))
  8741. (or string (setq s (concat "*" s))) ; Add * for headlines
  8742. (setq s (replace-regexp-in-string "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" "" s))
  8743. (when (and string (integerp lines) (> lines 0))
  8744. (let ((slines (org-split-string s "\n")))
  8745. (when (< lines (length slines))
  8746. (setq s (mapconcat
  8747. 'identity
  8748. (reverse (nthcdr (- (length slines) lines)
  8749. (reverse slines))) "\n")))))
  8750. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  8751. (defun org-make-link-string (link &optional description)
  8752. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8753. (unless (string-match "\\S-" link)
  8754. (error "Empty link"))
  8755. (when (and description
  8756. (stringp description)
  8757. (not (string-match "\\S-" description)))
  8758. (setq description nil))
  8759. (when (stringp description)
  8760. ;; Remove brackets from the description, they are fatal.
  8761. (while (string-match "\\[" description)
  8762. (setq description (replace-match "{" t t description)))
  8763. (while (string-match "\\]" description)
  8764. (setq description (replace-match "}" t t description))))
  8765. (when (equal link description)
  8766. ;; No description needed, it is identical
  8767. (setq description nil))
  8768. (when (and (not description)
  8769. (not (string-match (org-image-file-name-regexp) link))
  8770. (not (equal link (org-link-escape link))))
  8771. (setq description (org-extract-attributes link)))
  8772. (setq link
  8773. (cond ((string-match (org-image-file-name-regexp) link) link)
  8774. ((string-match org-link-types-re link)
  8775. (concat (match-string 1 link)
  8776. (org-link-escape (substring link (match-end 1)))))
  8777. (t (org-link-escape link))))
  8778. (concat "[[" link "]"
  8779. (if description (concat "[" description "]") "")
  8780. "]"))
  8781. (defconst org-link-escape-chars
  8782. '(?\ ?\[ ?\] ?\; ?\= ?\+)
  8783. "List of characters that should be escaped in link.
  8784. This is the list that is used for internal purposes.")
  8785. (defconst org-link-escape-chars-browser
  8786. '(?\ ?\")
  8787. "List of escapes for characters that are problematic in links.
  8788. This is the list that is used before handing over to the browser.")
  8789. (defun org-link-escape (text &optional table merge)
  8790. "Return percent escaped representation of TEXT.
  8791. TEXT is a string with the text to escape.
  8792. Optional argument TABLE is a list with characters that should be
  8793. escaped. When nil, `org-link-escape-chars' is used.
  8794. If optional argument MERGE is set, merge TABLE into
  8795. `org-link-escape-chars'."
  8796. (cond
  8797. ((and table merge)
  8798. (mapc (lambda (defchr)
  8799. (unless (member defchr table)
  8800. (setq table (cons defchr table)))) org-link-escape-chars))
  8801. ((null table)
  8802. (setq table org-link-escape-chars)))
  8803. (mapconcat
  8804. (lambda (char)
  8805. (if (or (member char table)
  8806. (and (or (< char 32) (= char 37) (> char 126))
  8807. org-url-hexify-p))
  8808. (mapconcat (lambda (sequence-element)
  8809. (format "%%%.2X" sequence-element))
  8810. (or (encode-coding-char char 'utf-8)
  8811. (error "Unable to percent escape character: %s"
  8812. (char-to-string char))) "")
  8813. (char-to-string char))) text ""))
  8814. (defun org-link-unescape (str)
  8815. "Unhex hexified Unicode strings as returned from the JavaScript function
  8816. encodeURIComponent. E.g. `%C3%B6' is the german o-Umlaut."
  8817. (unless (and (null str) (string= "" str))
  8818. (let ((pos 0) (case-fold-search t) unhexed)
  8819. (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
  8820. (setq unhexed (org-link-unescape-compound (match-string 0 str)))
  8821. (setq str (replace-match unhexed t t str))
  8822. (setq pos (+ pos (length unhexed))))))
  8823. str)
  8824. (defun org-link-unescape-compound (hex)
  8825. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German o-Umlaut.
  8826. Note: this function also decodes single byte encodings like
  8827. `%E1' (a-acute) if not followed by another `%[A-F0-9]{2}' group."
  8828. (save-match-data
  8829. (let* ((bytes (cdr (split-string hex "%")))
  8830. (ret "")
  8831. (eat 0)
  8832. (sum 0))
  8833. (while bytes
  8834. (let* ((val (string-to-number (pop bytes) 16))
  8835. (shift-xor
  8836. (if (= 0 eat)
  8837. (cond
  8838. ((>= val 252) (cons 6 252))
  8839. ((>= val 248) (cons 5 248))
  8840. ((>= val 240) (cons 4 240))
  8841. ((>= val 224) (cons 3 224))
  8842. ((>= val 192) (cons 2 192))
  8843. (t (cons 0 0)))
  8844. (cons 6 128))))
  8845. (if (>= val 192) (setq eat (car shift-xor)))
  8846. (setq val (logxor val (cdr shift-xor)))
  8847. (setq sum (+ (lsh sum (car shift-xor)) val))
  8848. (if (> eat 0) (setq eat (- eat 1)))
  8849. (cond
  8850. ((= 0 eat) ;multi byte
  8851. (setq ret (concat ret (org-char-to-string sum)))
  8852. (setq sum 0))
  8853. ((not bytes) ; single byte(s)
  8854. (setq ret (org-link-unescape-single-byte-sequence hex))))
  8855. )) ;; end (while bytes
  8856. ret )))
  8857. (defun org-link-unescape-single-byte-sequence (hex)
  8858. "Unhexify hex-encoded single byte character sequences."
  8859. (mapconcat (lambda (byte)
  8860. (char-to-string (string-to-number byte 16)))
  8861. (cdr (split-string hex "%")) ""))
  8862. (defun org-xor (a b)
  8863. "Exclusive or."
  8864. (if a (not b) b))
  8865. (defun org-fixup-message-id-for-http (s)
  8866. "Replace special characters in a message id, so it can be used in an http query."
  8867. (when (string-match "%" s)
  8868. (setq s (mapconcat (lambda (c)
  8869. (if (eq c ?%)
  8870. "%25"
  8871. (char-to-string c)))
  8872. s "")))
  8873. (while (string-match "<" s)
  8874. (setq s (replace-match "%3C" t t s)))
  8875. (while (string-match ">" s)
  8876. (setq s (replace-match "%3E" t t s)))
  8877. (while (string-match "@" s)
  8878. (setq s (replace-match "%40" t t s)))
  8879. s)
  8880. (defun org-link-prettify (link)
  8881. "Return a human-readable representation of LINK.
  8882. The car of LINK must be a raw link the cdr of LINK must be either
  8883. a link description or nil."
  8884. (let ((desc (or (cadr link) "<no description>")))
  8885. (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
  8886. "<" (car link) ">")))
  8887. ;;;###autoload
  8888. (defun org-insert-link-global ()
  8889. "Insert a link like Org-mode does.
  8890. This command can be called in any mode to insert a link in Org-mode syntax."
  8891. (interactive)
  8892. (org-load-modules-maybe)
  8893. (org-run-like-in-org-mode 'org-insert-link))
  8894. (defun org-insert-all-links (&optional keep)
  8895. "Insert all links in `org-stored-links'."
  8896. (interactive "P")
  8897. (let ((links (copy-sequence org-stored-links)) l)
  8898. (while (setq l (if keep (pop links) (pop org-stored-links)))
  8899. (insert "- ")
  8900. (org-insert-link nil (car l) (or (cadr l) "<no description>"))
  8901. (insert "\n"))))
  8902. (defun org-link-fontify-links-to-this-file ()
  8903. "Fontify links to the current file in `org-stored-links'."
  8904. (let ((f (buffer-file-name)) a b)
  8905. (setq a (mapcar (lambda(l)
  8906. (let ((ll (car l)))
  8907. (when (and (string-match "^file:\\(.+\\)::" ll)
  8908. (equal f (expand-file-name (match-string 1 ll))))
  8909. ll)))
  8910. org-stored-links))
  8911. (when (featurep 'org-id)
  8912. (setq b (mapcar (lambda(l)
  8913. (let ((ll (car l)))
  8914. (when (and (string-match "^id:\\(.+\\)$" ll)
  8915. (equal f (expand-file-name
  8916. (or (org-id-find-id-file
  8917. (match-string 1 ll)) ""))))
  8918. ll)))
  8919. org-stored-links)))
  8920. (mapcar (lambda(l)
  8921. (put-text-property 0 (length l) 'face 'font-lock-comment-face l))
  8922. (delq nil (append a b)))))
  8923. (defvar org-link-links-in-this-file nil)
  8924. (defun org-insert-link (&optional complete-file link-location default-description)
  8925. "Insert a link. At the prompt, enter the link.
  8926. Completion can be used to insert any of the link protocol prefixes like
  8927. http or ftp in use.
  8928. The history can be used to select a link previously stored with
  8929. `org-store-link'. When the empty string is entered (i.e. if you just
  8930. press RET at the prompt), the link defaults to the most recently
  8931. stored link. As SPC triggers completion in the minibuffer, you need to
  8932. use M-SPC or C-q SPC to force the insertion of a space character.
  8933. You will also be prompted for a description, and if one is given, it will
  8934. be displayed in the buffer instead of the link.
  8935. If there is already a link at point, this command will allow you to edit link
  8936. and description parts.
  8937. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  8938. be selected using completion. The path to the file will be relative to the
  8939. current directory if the file is in the current directory or a subdirectory.
  8940. Otherwise, the link will be the absolute path as completed in the minibuffer
  8941. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  8942. option `org-link-file-path-type'.
  8943. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  8944. the current directory or below.
  8945. With three \\[universal-argument] prefixes, negate the meaning of
  8946. `org-keep-stored-link-after-insertion'.
  8947. If `org-make-link-description-function' is non-nil, this function will be
  8948. called with the link target, and the result will be the default
  8949. link description.
  8950. If the LINK-LOCATION parameter is non-nil, this value will be
  8951. used as the link location instead of reading one interactively.
  8952. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  8953. be used as the default description."
  8954. (interactive "P")
  8955. (let* ((wcf (current-window-configuration))
  8956. (origbuf (current-buffer))
  8957. (region (if (org-region-active-p)
  8958. (buffer-substring (region-beginning) (region-end))))
  8959. (remove (and region (list (region-beginning) (region-end))))
  8960. (desc region)
  8961. tmphist ; byte-compile incorrectly complains about this
  8962. (link link-location)
  8963. (abbrevs org-link-abbrev-alist-local)
  8964. entry file all-prefixes auto-desc)
  8965. (cond
  8966. (link-location) ; specified by arg, just use it.
  8967. ((org-in-regexp org-bracket-link-regexp 1)
  8968. ;; We do have a link at point, and we are going to edit it.
  8969. (setq remove (list (match-beginning 0) (match-end 0)))
  8970. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  8971. (setq link (read-string "Link: "
  8972. (org-link-unescape
  8973. (org-match-string-no-properties 1)))))
  8974. ((or (org-in-regexp org-angle-link-re)
  8975. (org-in-regexp org-plain-link-re))
  8976. ;; Convert to bracket link
  8977. (setq remove (list (match-beginning 0) (match-end 0))
  8978. link (read-string "Link: "
  8979. (org-remove-angle-brackets (match-string 0)))))
  8980. ((member complete-file '((4) (16)))
  8981. ;; Completing read for file names.
  8982. (setq link (org-file-complete-link complete-file)))
  8983. (t
  8984. ;; Read link, with completion for stored links.
  8985. (org-link-fontify-links-to-this-file)
  8986. (org-switch-to-buffer-other-window "*Org Links*")
  8987. (with-current-buffer "*Org Links*"
  8988. (erase-buffer)
  8989. (insert "Insert a link.
  8990. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  8991. (when org-stored-links
  8992. (insert "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  8993. (insert (mapconcat 'org-link-prettify
  8994. (reverse org-stored-links) "\n")))
  8995. (goto-char (point-min)))
  8996. (let ((cw (selected-window)))
  8997. (select-window (get-buffer-window "*Org Links*" 'visible))
  8998. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  8999. (unless (pos-visible-in-window-p (point-max))
  9000. (org-fit-window-to-buffer))
  9001. (and (window-live-p cw) (select-window cw)))
  9002. ;; Fake a link history, containing the stored links.
  9003. (setq tmphist (append (mapcar 'car org-stored-links)
  9004. org-insert-link-history))
  9005. (setq all-prefixes (append (mapcar 'car abbrevs)
  9006. (mapcar 'car org-link-abbrev-alist)
  9007. org-link-types))
  9008. (unwind-protect
  9009. (progn
  9010. (setq link
  9011. (org-completing-read
  9012. "Link: "
  9013. (append
  9014. (mapcar (lambda (x) (concat x ":"))
  9015. all-prefixes)
  9016. (mapcar 'car org-stored-links))
  9017. nil nil nil
  9018. 'tmphist
  9019. (caar org-stored-links)))
  9020. (if (not (string-match "\\S-" link))
  9021. (user-error "No link selected"))
  9022. (mapc (lambda(l)
  9023. (when (equal link (cadr l)) (setq link (car l) auto-desc t)))
  9024. org-stored-links)
  9025. (if (or (member link all-prefixes)
  9026. (and (equal ":" (substring link -1))
  9027. (member (substring link 0 -1) all-prefixes)
  9028. (setq link (substring link 0 -1))))
  9029. (setq link (with-current-buffer origbuf
  9030. (org-link-try-special-completion link)))))
  9031. (set-window-configuration wcf)
  9032. (kill-buffer "*Org Links*"))
  9033. (setq entry (assoc link org-stored-links))
  9034. (or entry (push link org-insert-link-history))
  9035. (setq desc (or desc (nth 1 entry)))))
  9036. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  9037. (not org-keep-stored-link-after-insertion))
  9038. (setq org-stored-links (delq (assoc link org-stored-links)
  9039. org-stored-links)))
  9040. (if (string-match org-plain-link-re link)
  9041. ;; URL-like link, normalize the use of angular brackets.
  9042. (setq link (org-remove-angle-brackets link)))
  9043. ;; Check if we are linking to the current file with a search
  9044. ;; option If yes, simplify the link by using only the search
  9045. ;; option.
  9046. (when (and buffer-file-name
  9047. (string-match "^file:\\(.+?\\)::\\(.+\\)" link))
  9048. (let* ((path (match-string 1 link))
  9049. (case-fold-search nil)
  9050. (search (match-string 2 link)))
  9051. (save-match-data
  9052. (if (equal (file-truename buffer-file-name) (file-truename path))
  9053. ;; We are linking to this same file, with a search option
  9054. (setq link search)))))
  9055. ;; Check if we can/should use a relative path. If yes, simplify the link
  9056. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  9057. (let* ((type (match-string 1 link))
  9058. (path (match-string 2 link))
  9059. (origpath path)
  9060. (case-fold-search nil))
  9061. (cond
  9062. ((or (eq org-link-file-path-type 'absolute)
  9063. (equal complete-file '(16)))
  9064. (setq path (abbreviate-file-name (expand-file-name path))))
  9065. ((eq org-link-file-path-type 'noabbrev)
  9066. (setq path (expand-file-name path)))
  9067. ((eq org-link-file-path-type 'relative)
  9068. (setq path (file-relative-name path)))
  9069. (t
  9070. (save-match-data
  9071. (if (string-match (concat "^" (regexp-quote
  9072. (expand-file-name
  9073. (file-name-as-directory
  9074. default-directory))))
  9075. (expand-file-name path))
  9076. ;; We are linking a file with relative path name.
  9077. (setq path (substring (expand-file-name path)
  9078. (match-end 0)))
  9079. (setq path (abbreviate-file-name (expand-file-name path)))))))
  9080. (setq link (concat type path))
  9081. (if (equal desc origpath)
  9082. (setq desc path))))
  9083. (if org-make-link-description-function
  9084. (setq desc
  9085. (or (condition-case nil
  9086. (funcall org-make-link-description-function link desc)
  9087. (error (progn (message "Can't get link description from `%s'"
  9088. (symbol-name org-make-link-description-function))
  9089. (sit-for 2) nil)))
  9090. (read-string "Description: " default-description)))
  9091. (if default-description (setq desc default-description)
  9092. (setq desc (or (and auto-desc desc)
  9093. (read-string "Description: " desc)))))
  9094. (unless (string-match "\\S-" desc) (setq desc nil))
  9095. (if remove (apply 'delete-region remove))
  9096. (insert (org-make-link-string link desc))))
  9097. (defun org-link-try-special-completion (type)
  9098. "If there is completion support for link type TYPE, offer it."
  9099. (let ((fun (intern (concat "org-" type "-complete-link"))))
  9100. (if (functionp fun)
  9101. (funcall fun)
  9102. (read-string "Link (no completion support): " (concat type ":")))))
  9103. (defun org-file-complete-link (&optional arg)
  9104. "Create a file link using completion."
  9105. (let (file link)
  9106. (setq file (org-iread-file-name "File: "))
  9107. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  9108. (pwd1 (file-name-as-directory (abbreviate-file-name
  9109. (expand-file-name ".")))))
  9110. (cond
  9111. ((equal arg '(16))
  9112. (setq link (concat
  9113. "file:"
  9114. (abbreviate-file-name (expand-file-name file)))))
  9115. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  9116. (setq link (concat "file:" (match-string 1 file))))
  9117. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  9118. (expand-file-name file))
  9119. (setq link (concat
  9120. "file:" (match-string 1 (expand-file-name file)))))
  9121. (t (setq link (concat "file:" file)))))
  9122. link))
  9123. (defun org-iread-file-name (&rest args)
  9124. "Read-file-name using `ido-mode' speedup if available.
  9125. ARGS are arguments that may be passed to `ido-read-file-name' or `read-file-name'.
  9126. See `read-file-name' for a description of parameters."
  9127. (org-without-partial-completion
  9128. (if (and org-completion-use-ido
  9129. (fboundp 'ido-read-file-name)
  9130. (boundp 'ido-mode) ido-mode
  9131. (listp (second args)))
  9132. (let ((ido-enter-matching-directory nil))
  9133. (apply 'ido-read-file-name args))
  9134. (apply 'read-file-name args))))
  9135. (defun org-completing-read (&rest args)
  9136. "Completing-read with SPACE being a normal character."
  9137. (let ((enable-recursive-minibuffers t)
  9138. (minibuffer-local-completion-map
  9139. (copy-keymap minibuffer-local-completion-map)))
  9140. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  9141. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  9142. (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
  9143. (apply 'org-icompleting-read args)))
  9144. (defun org-completing-read-no-i (&rest args)
  9145. (let (org-completion-use-ido org-completion-use-iswitchb)
  9146. (apply 'org-completing-read args)))
  9147. (defun org-iswitchb-completing-read (prompt choices &rest args)
  9148. "Use iswitch as a completing-read replacement to choose from choices.
  9149. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  9150. from."
  9151. (let* ((iswitchb-use-virtual-buffers nil)
  9152. (iswitchb-make-buflist-hook
  9153. (lambda ()
  9154. (setq iswitchb-temp-buflist choices))))
  9155. (iswitchb-read-buffer prompt)))
  9156. (defun org-icompleting-read (&rest args)
  9157. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  9158. (org-without-partial-completion
  9159. (if (and org-completion-use-ido
  9160. (fboundp 'ido-completing-read)
  9161. (boundp 'ido-mode) ido-mode
  9162. (listp (second args)))
  9163. (let ((ido-enter-matching-directory nil))
  9164. (apply 'ido-completing-read (concat (car args))
  9165. (if (consp (car (nth 1 args)))
  9166. (mapcar 'car (nth 1 args))
  9167. (nth 1 args))
  9168. (cddr args)))
  9169. (if (and org-completion-use-iswitchb
  9170. (boundp 'iswitchb-mode) iswitchb-mode
  9171. (listp (second args)))
  9172. (apply 'org-iswitchb-completing-read (concat (car args))
  9173. (if (consp (car (nth 1 args)))
  9174. (mapcar 'car (nth 1 args))
  9175. (nth 1 args))
  9176. (cddr args))
  9177. (apply 'completing-read args)))))
  9178. (defun org-extract-attributes (s)
  9179. "Extract the attributes cookie from a string and set as text property."
  9180. (let (a attr (start 0) key value)
  9181. (save-match-data
  9182. (when (string-match "{{\\([^}]+\\)}}$" s)
  9183. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  9184. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  9185. (setq key (match-string 1 a) value (match-string 2 a)
  9186. start (match-end 0)
  9187. attr (plist-put attr (intern key) value))))
  9188. (org-add-props s nil 'org-attr attr))
  9189. s))
  9190. ;;; Opening/following a link
  9191. (defvar org-link-search-failed nil)
  9192. (defvar org-open-link-functions nil
  9193. "Hook for functions finding a plain text link.
  9194. These functions must take a single argument, the link content.
  9195. They will be called for links that look like [[link text][description]]
  9196. when LINK TEXT does not have a protocol like \"http:\" and does not look
  9197. like a filename (e.g. \"./blue.png\").
  9198. These functions will be called *before* Org attempts to resolve the
  9199. link by doing text searches in the current buffer - so if you want a
  9200. link \"[[target]]\" to still find \"<<target>>\", your function should
  9201. handle this as a special case.
  9202. When the function does handle the link, it must return a non-nil value.
  9203. If it decides that it is not responsible for this link, it must return
  9204. nil to indicate that that Org-mode can continue with other options
  9205. like exact and fuzzy text search.")
  9206. (defun org-next-link (&optional search-backward)
  9207. "Move forward to the next link.
  9208. If the link is in hidden text, expose it."
  9209. (interactive "P")
  9210. (when (and org-link-search-failed (eq this-command last-command))
  9211. (goto-char (point-min))
  9212. (message "Link search wrapped back to beginning of buffer"))
  9213. (setq org-link-search-failed nil)
  9214. (let* ((pos (point))
  9215. (ct (org-context))
  9216. (a (assoc :link ct))
  9217. (srch-fun (if search-backward 're-search-backward 're-search-forward)))
  9218. (cond (a (goto-char (nth (if search-backward 1 2) a)))
  9219. ((looking-at org-any-link-re)
  9220. ;; Don't stay stuck at link without an org-link face
  9221. (forward-char (if search-backward -1 1))))
  9222. (if (funcall srch-fun org-any-link-re nil t)
  9223. (progn
  9224. (goto-char (match-beginning 0))
  9225. (if (outline-invisible-p) (org-show-context)))
  9226. (goto-char pos)
  9227. (setq org-link-search-failed t)
  9228. (message "No further link found"))))
  9229. (defun org-previous-link ()
  9230. "Move backward to the previous link.
  9231. If the link is in hidden text, expose it."
  9232. (interactive)
  9233. (funcall 'org-next-link t))
  9234. (defun org-translate-link (s)
  9235. "Translate a link string if a translation function has been defined."
  9236. (if (and org-link-translation-function
  9237. (fboundp org-link-translation-function)
  9238. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  9239. (progn
  9240. (setq s (funcall org-link-translation-function
  9241. (match-string 1 s) (match-string 2 s)))
  9242. (concat (car s) ":" (cdr s)))
  9243. s))
  9244. (defun org-translate-link-from-planner (type path)
  9245. "Translate a link from Emacs Planner syntax so that Org can follow it.
  9246. This is still an experimental function, your mileage may vary."
  9247. (cond
  9248. ((member type '("http" "https" "news" "ftp"))
  9249. ;; standard Internet links are the same.
  9250. nil)
  9251. ((and (equal type "irc") (string-match "^//" path))
  9252. ;; Planner has two / at the beginning of an irc link, we have 1.
  9253. ;; We should have zero, actually....
  9254. (setq path (substring path 1)))
  9255. ((and (equal type "lisp") (string-match "^/" path))
  9256. ;; Planner has a slash, we do not.
  9257. (setq type "elisp" path (substring path 1)))
  9258. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  9259. ;; A typical message link. Planner has the id after the final slash,
  9260. ;; we separate it with a hash mark
  9261. (setq path (concat (match-string 1 path) "#"
  9262. (org-remove-angle-brackets (match-string 2 path))))))
  9263. (cons type path))
  9264. (defun org-find-file-at-mouse (ev)
  9265. "Open file link or URL at mouse."
  9266. (interactive "e")
  9267. (mouse-set-point ev)
  9268. (org-open-at-point 'in-emacs))
  9269. (defun org-open-at-mouse (ev)
  9270. "Open file link or URL at mouse.
  9271. See the docstring of `org-open-file' for details."
  9272. (interactive "e")
  9273. (mouse-set-point ev)
  9274. (if (eq major-mode 'org-agenda-mode)
  9275. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  9276. (org-open-at-point))
  9277. (defvar org-window-config-before-follow-link nil
  9278. "The window configuration before following a link.
  9279. This is saved in case the need arises to restore it.")
  9280. (defvar org-open-link-marker (make-marker)
  9281. "Marker pointing to the location where `org-open-at-point; was called.")
  9282. ;;;###autoload
  9283. (defun org-open-at-point-global ()
  9284. "Follow a link like Org-mode does.
  9285. This command can be called in any mode to follow a link that has
  9286. Org-mode syntax."
  9287. (interactive)
  9288. (org-run-like-in-org-mode 'org-open-at-point))
  9289. ;;;###autoload
  9290. (defun org-open-link-from-string (s &optional arg reference-buffer)
  9291. "Open a link in the string S, as if it was in Org-mode."
  9292. (interactive "sLink: \nP")
  9293. (let ((reference-buffer (or reference-buffer (current-buffer))))
  9294. (with-temp-buffer
  9295. (let ((org-inhibit-startup (not reference-buffer)))
  9296. (org-mode)
  9297. (insert s)
  9298. (goto-char (point-min))
  9299. (when reference-buffer
  9300. (setq org-link-abbrev-alist-local
  9301. (with-current-buffer reference-buffer
  9302. org-link-abbrev-alist-local)))
  9303. (org-open-at-point arg reference-buffer)))))
  9304. (defvar org-open-at-point-functions nil
  9305. "Hook that is run when following a link at point.
  9306. Functions in this hook must return t if they identify and follow
  9307. a link at point. If they don't find anything interesting at point,
  9308. they must return nil.")
  9309. (defvar clean-buffer-list-kill-buffer-names) ; Defined in midnight.el
  9310. (defun org-open-at-point (&optional arg reference-buffer)
  9311. "Open link at or after point.
  9312. If there is no link at point, this function will search forward up to
  9313. the end of the current line.
  9314. Normally, files will be opened by an appropriate application. If the
  9315. optional prefix argument ARG is non-nil, Emacs will visit the file.
  9316. With a double prefix argument, try to open outside of Emacs, in the
  9317. application the system uses for this file type."
  9318. (interactive "P")
  9319. ;; if in a code block, then open the block's results
  9320. (unless (call-interactively #'org-babel-open-src-block-result)
  9321. (org-load-modules-maybe)
  9322. (move-marker org-open-link-marker (point))
  9323. (setq org-window-config-before-follow-link (current-window-configuration))
  9324. (org-remove-occur-highlights nil nil t)
  9325. (cond
  9326. ((and (org-at-heading-p)
  9327. (not (org-at-timestamp-p t))
  9328. (not (org-in-regexp
  9329. (concat org-plain-link-re "\\|"
  9330. org-bracket-link-regexp "\\|"
  9331. org-angle-link-re "\\|"
  9332. "[ \t]:[^ \t\n]+:[ \t]*$")))
  9333. (not (get-text-property (point) 'org-linked-text)))
  9334. (or (let* ((lkall (org-offer-links-in-entry (current-buffer) (point) arg))
  9335. (lk0 (car lkall))
  9336. (lk (if (stringp lk0) (list lk0) lk0))
  9337. (lkend (cdr lkall)))
  9338. (mapcar (lambda(l)
  9339. (search-forward l nil lkend)
  9340. (goto-char (match-beginning 0))
  9341. (org-open-at-point))
  9342. lk))
  9343. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  9344. ((run-hook-with-args-until-success 'org-open-at-point-functions))
  9345. ((and (org-at-timestamp-p t)
  9346. (not (org-in-regexp org-bracket-link-regexp)))
  9347. (org-follow-timestamp-link))
  9348. ((and (or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  9349. (not (org-in-regexp org-any-link-re)))
  9350. (org-footnote-action))
  9351. (t
  9352. (let (type path link line search (pos (point)))
  9353. (catch 'match
  9354. (save-excursion
  9355. (skip-chars-forward "^]\n\r")
  9356. (when (org-in-regexp org-bracket-link-regexp 1)
  9357. (setq link (org-extract-attributes
  9358. (org-link-unescape (org-match-string-no-properties 1))))
  9359. (while (string-match " *\n *" link)
  9360. (setq link (replace-match " " t t link)))
  9361. (setq link (org-link-expand-abbrev link))
  9362. (cond
  9363. ((or (file-name-absolute-p link)
  9364. (string-match "^\\.\\.?/" link))
  9365. (setq type "file" path link))
  9366. ((string-match org-link-re-with-space3 link)
  9367. (setq type (match-string 1 link) path (match-string 2 link)))
  9368. ((string-match "^help:+\\(.+\\)" link)
  9369. (setq type "help" path (match-string 1 link)))
  9370. (t (setq type "thisfile" path link)))
  9371. (throw 'match t)))
  9372. (when (get-text-property (point) 'org-linked-text)
  9373. (setq type "thisfile"
  9374. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  9375. (1+ (point)) (point))
  9376. path (buffer-substring
  9377. (or (previous-single-property-change pos 'org-linked-text)
  9378. (point-min))
  9379. (or (next-single-property-change pos 'org-linked-text)
  9380. (point-max)))
  9381. ;; Ensure we will search for a <<<radio>>> link, not
  9382. ;; a simple reference like <<ref>>
  9383. path (concat "<" path))
  9384. (throw 'match t))
  9385. (save-excursion
  9386. (when (or (org-in-regexp org-angle-link-re)
  9387. (let ((match (org-in-regexp org-plain-link-re)))
  9388. ;; Check a plain link is not within a bracket link
  9389. (and match
  9390. (save-excursion
  9391. (progn
  9392. (goto-char (car match))
  9393. (not (org-in-regexp org-bracket-link-regexp))))))
  9394. (let ((line_ending (save-excursion (end-of-line) (point))))
  9395. ;; We are in a line before a plain or bracket link
  9396. (or (re-search-forward org-plain-link-re line_ending t)
  9397. (re-search-forward org-bracket-link-regexp line_ending t))))
  9398. (setq type (match-string 1)
  9399. path (org-link-unescape (match-string 2)))
  9400. (throw 'match t)))
  9401. (save-excursion
  9402. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
  9403. (setq type "tags"
  9404. path (match-string 1))
  9405. (while (string-match ":" path)
  9406. (setq path (replace-match "+" t t path)))
  9407. (throw 'match t)))
  9408. (when (org-in-regexp "<\\([^><\n]+\\)>")
  9409. (setq type "tree-match"
  9410. path (match-string 1))
  9411. (throw 'match t)))
  9412. (unless path
  9413. (user-error "No link found"))
  9414. ;; switch back to reference buffer
  9415. ;; needed when if called in a temporary buffer through
  9416. ;; org-open-link-from-string
  9417. (with-current-buffer (or reference-buffer (current-buffer))
  9418. ;; Remove any trailing spaces in path
  9419. (if (string-match " +\\'" path)
  9420. (setq path (replace-match "" t t path)))
  9421. (if (and org-link-translation-function
  9422. (fboundp org-link-translation-function))
  9423. ;; Check if we need to translate the link
  9424. (let ((tmp (funcall org-link-translation-function type path)))
  9425. (setq type (car tmp) path (cdr tmp))))
  9426. (cond
  9427. ((assoc type org-link-protocols)
  9428. (funcall (nth 1 (assoc type org-link-protocols)) path))
  9429. ((equal type "help")
  9430. (let ((f-or-v (intern path)))
  9431. (cond ((fboundp f-or-v)
  9432. (describe-function f-or-v))
  9433. ((boundp f-or-v)
  9434. (describe-variable f-or-v))
  9435. (t (error "Not a known function or variable")))))
  9436. ((equal type "mailto")
  9437. (let ((cmd (car org-link-mailto-program))
  9438. (args (cdr org-link-mailto-program)) args1
  9439. (address path) (subject "") a)
  9440. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  9441. (setq address (match-string 1 path)
  9442. subject (org-link-escape (match-string 2 path))))
  9443. (while args
  9444. (cond
  9445. ((not (stringp (car args))) (push (pop args) args1))
  9446. (t (setq a (pop args))
  9447. (if (string-match "%a" a)
  9448. (setq a (replace-match address t t a)))
  9449. (if (string-match "%s" a)
  9450. (setq a (replace-match subject t t a)))
  9451. (push a args1))))
  9452. (apply cmd (nreverse args1))))
  9453. ((member type '("http" "https" "ftp" "news"))
  9454. (browse-url
  9455. (concat type ":"
  9456. (if (org-string-match-p
  9457. (concat "[[:nonascii:]"
  9458. org-link-escape-chars-browser "]")
  9459. path)
  9460. (org-link-escape path org-link-escape-chars-browser)
  9461. path))))
  9462. ((string= type "doi")
  9463. (browse-url
  9464. (concat org-doi-server-url
  9465. (if (org-string-match-p
  9466. (concat "[[:nonascii:]"
  9467. org-link-escape-chars-browser "]")
  9468. path)
  9469. (org-link-escape path org-link-escape-chars-browser)
  9470. path))))
  9471. ((member type '("message"))
  9472. (browse-url (concat type ":" path)))
  9473. ((string= type "tags")
  9474. (org-tags-view arg path))
  9475. ((string= type "tree-match")
  9476. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  9477. ((string= type "file")
  9478. (if (string-match "::\\([0-9]+\\)\\'" path)
  9479. (setq line (string-to-number (match-string 1 path))
  9480. path (substring path 0 (match-beginning 0)))
  9481. (if (string-match "::\\(.+\\)\\'" path)
  9482. (setq search (match-string 1 path)
  9483. path (substring path 0 (match-beginning 0)))))
  9484. (if (string-match "[*?{]" (file-name-nondirectory path))
  9485. (dired path)
  9486. (org-open-file path arg line search)))
  9487. ((string= type "shell")
  9488. (let ((buf (generate-new-buffer "*Org Shell Output"))
  9489. (cmd path))
  9490. (if (or (and (not (string= org-confirm-shell-link-not-regexp ""))
  9491. (string-match org-confirm-shell-link-not-regexp cmd))
  9492. (not org-confirm-shell-link-function)
  9493. (funcall org-confirm-shell-link-function
  9494. (format "Execute \"%s\" in shell? "
  9495. (org-add-props cmd nil
  9496. 'face 'org-warning))))
  9497. (progn
  9498. (message "Executing %s" cmd)
  9499. (shell-command cmd buf)
  9500. (if (featurep 'midnight)
  9501. (setq clean-buffer-list-kill-buffer-names
  9502. (cons buf clean-buffer-list-kill-buffer-names))))
  9503. (error "Abort"))))
  9504. ((string= type "elisp")
  9505. (let ((cmd path))
  9506. (if (or (and (not (string= org-confirm-elisp-link-not-regexp ""))
  9507. (string-match org-confirm-elisp-link-not-regexp cmd))
  9508. (not org-confirm-elisp-link-function)
  9509. (funcall org-confirm-elisp-link-function
  9510. (format "Execute \"%s\" as elisp? "
  9511. (org-add-props cmd nil
  9512. 'face 'org-warning))))
  9513. (message "%s => %s" cmd
  9514. (if (equal (string-to-char cmd) ?\()
  9515. (eval (read cmd))
  9516. (call-interactively (read cmd))))
  9517. (error "Abort"))))
  9518. ((and (string= type "thisfile")
  9519. (run-hook-with-args-until-success
  9520. 'org-open-link-functions path)))
  9521. ((string= type "thisfile")
  9522. (if arg
  9523. (switch-to-buffer-other-window
  9524. (org-get-buffer-for-internal-link (current-buffer)))
  9525. (org-mark-ring-push))
  9526. (let ((cmd `(org-link-search
  9527. ,path
  9528. ,(cond ((equal arg '(4)) ''occur)
  9529. ((equal arg '(16)) ''org-occur))
  9530. ,pos)))
  9531. (condition-case nil (let ((org-link-search-inhibit-query t))
  9532. (eval cmd))
  9533. (error (progn (widen) (eval cmd))))))
  9534. (t (browse-url-at-point)))))))
  9535. (move-marker org-open-link-marker nil)
  9536. (run-hook-with-args 'org-follow-link-hook)))
  9537. (defun org-offer-links-in-entry (buffer marker &optional nth zero)
  9538. "Offer links in the current entry and return the selected link.
  9539. If there is only one link, return it.
  9540. If NTH is an integer, return the NTH link found.
  9541. If ZERO is a string, check also this string for a link, and if
  9542. there is one, return it."
  9543. (with-current-buffer buffer
  9544. (save-excursion
  9545. (save-restriction
  9546. (widen)
  9547. (goto-char marker)
  9548. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  9549. "\\(" org-angle-link-re "\\)\\|"
  9550. "\\(" org-plain-link-re "\\)"))
  9551. (cnt ?0)
  9552. (in-emacs (if (integerp nth) nil nth))
  9553. have-zero end links link c)
  9554. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  9555. (push (match-string 0 zero) links)
  9556. (setq cnt (1- cnt) have-zero t))
  9557. (save-excursion
  9558. (org-back-to-heading t)
  9559. (setq end (save-excursion (outline-next-heading) (point)))
  9560. (while (re-search-forward re end t)
  9561. (push (match-string 0) links))
  9562. (setq links (org-uniquify (reverse links))))
  9563. (cond
  9564. ((null links)
  9565. (message "No links"))
  9566. ((equal (length links) 1)
  9567. (setq link (car links)))
  9568. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  9569. (setq link (nth (if have-zero nth (1- nth)) links)))
  9570. (t ; we have to select a link
  9571. (save-excursion
  9572. (save-window-excursion
  9573. (delete-other-windows)
  9574. (with-output-to-temp-buffer "*Select Link*"
  9575. (mapc (lambda (l)
  9576. (if (not (string-match org-bracket-link-regexp l))
  9577. (princ (format "[%c] %s\n" (incf cnt)
  9578. (org-remove-angle-brackets l)))
  9579. (if (match-end 3)
  9580. (princ (format "[%c] %s (%s)\n" (incf cnt)
  9581. (match-string 3 l) (match-string 1 l)))
  9582. (princ (format "[%c] %s\n" (incf cnt)
  9583. (match-string 1 l))))))
  9584. links))
  9585. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  9586. (message "Select link to open, RET to open all:")
  9587. (setq c (read-char-exclusive))
  9588. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  9589. (when (equal c ?q) (error "Abort"))
  9590. (if (equal c ?\C-m)
  9591. (setq link links)
  9592. (setq nth (- c ?0))
  9593. (if have-zero (setq nth (1+ nth)))
  9594. (unless (and (integerp nth) (>= (length links) nth))
  9595. (user-error "Invalid link selection"))
  9596. (setq link (nth (1- nth) links)))))
  9597. (cons link end))))))
  9598. ;; Add special file links that specify the way of opening
  9599. (org-add-link-type "file+sys" 'org-open-file-with-system)
  9600. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  9601. (defun org-open-file-with-system (path)
  9602. "Open file at PATH using the system way of opening it."
  9603. (org-open-file path 'system))
  9604. (defun org-open-file-with-emacs (path)
  9605. "Open file at PATH in Emacs."
  9606. (org-open-file path 'emacs))
  9607. ;;; File search
  9608. (defvar org-create-file-search-functions nil
  9609. "List of functions to construct the right search string for a file link.
  9610. These functions are called in turn with point at the location to
  9611. which the link should point.
  9612. A function in the hook should first test if it would like to
  9613. handle this file type, for example by checking the `major-mode'
  9614. or the file extension. If it decides not to handle this file, it
  9615. should just return nil to give other functions a chance. If it
  9616. does handle the file, it must return the search string to be used
  9617. when following the link. The search string will be part of the
  9618. file link, given after a double colon, and `org-open-at-point'
  9619. will automatically search for it. If special measures must be
  9620. taken to make the search successful, another function should be
  9621. added to the companion hook `org-execute-file-search-functions',
  9622. which see.
  9623. A function in this hook may also use `setq' to set the variable
  9624. `description' to provide a suggestion for the descriptive text to
  9625. be used for this link when it gets inserted into an Org-mode
  9626. buffer with \\[org-insert-link].")
  9627. (defvar org-execute-file-search-functions nil
  9628. "List of functions to execute a file search triggered by a link.
  9629. Functions added to this hook must accept a single argument, the
  9630. search string that was part of the file link, the part after the
  9631. double colon. The function must first check if it would like to
  9632. handle this search, for example by checking the `major-mode' or
  9633. the file extension. If it decides not to handle this search, it
  9634. should just return nil to give other functions a chance. If it
  9635. does handle the search, it must return a non-nil value to keep
  9636. other functions from trying.
  9637. Each function can access the current prefix argument through the
  9638. variable `current-prefix-arg'. Note that a single prefix is used
  9639. to force opening a link in Emacs, so it may be good to only use a
  9640. numeric or double prefix to guide the search function.
  9641. In case this is needed, a function in this hook can also restore
  9642. the window configuration before `org-open-at-point' was called using:
  9643. (set-window-configuration org-window-config-before-follow-link)")
  9644. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  9645. (defun org-link-search (s &optional type avoid-pos stealth)
  9646. "Search for a link search option.
  9647. If S is surrounded by forward slashes, it is interpreted as a
  9648. regular expression. In org-mode files, this will create an `org-occur'
  9649. sparse tree. In ordinary files, `occur' will be used to list matches.
  9650. If the current buffer is in `dired-mode', grep will be used to search
  9651. in all files. If AVOID-POS is given, ignore matches near that position.
  9652. When optional argument STEALTH is non-nil, do not modify
  9653. visibility around point, thus ignoring
  9654. `org-show-hierarchy-above', `org-show-following-heading' and
  9655. `org-show-siblings' variables."
  9656. (let ((case-fold-search t)
  9657. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  9658. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  9659. (append '(("") (" ") ("\t") ("\n"))
  9660. org-emphasis-alist)
  9661. "\\|") "\\)"))
  9662. (pos (point))
  9663. (pre nil) (post nil)
  9664. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  9665. (cond
  9666. ;; First check if there are any special search functions
  9667. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  9668. ;; Now try the builtin stuff
  9669. ((and (equal (string-to-char s0) ?#)
  9670. (> (length s0) 1)
  9671. (save-excursion
  9672. (goto-char (point-min))
  9673. (and
  9674. (re-search-forward
  9675. (concat "^[ \t]*:CUSTOM_ID:[ \t]+"
  9676. (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  9677. (setq type 'dedicated
  9678. pos (match-beginning 0))))
  9679. ;; There is an exact target for this
  9680. (goto-char pos)
  9681. (org-back-to-heading t)))
  9682. ((save-excursion
  9683. (goto-char (point-min))
  9684. (and
  9685. (re-search-forward
  9686. (concat "<<" (regexp-quote s0) ">>") nil t)
  9687. (setq type 'dedicated
  9688. pos (match-beginning 0))))
  9689. ;; There is an exact target for this
  9690. (goto-char pos))
  9691. ((save-excursion
  9692. (goto-char (point-min))
  9693. (and
  9694. (re-search-forward
  9695. (format "^[ \t]*#\\+NAME: %s" (regexp-quote s0)) nil t)
  9696. (setq type 'dedicated pos (match-beginning 0))))
  9697. ;; Found an element with a matching #+name affiliated keyword.
  9698. (goto-char pos))
  9699. ((and (string-match "^(\\(.*\\))$" s0)
  9700. (save-excursion
  9701. (goto-char (point-min))
  9702. (and
  9703. (re-search-forward
  9704. (concat "[^[]" (regexp-quote
  9705. (format org-coderef-label-format
  9706. (match-string 1 s0))))
  9707. nil t)
  9708. (setq type 'dedicated
  9709. pos (1+ (match-beginning 0))))))
  9710. ;; There is a coderef target for this
  9711. (goto-char pos))
  9712. ((string-match "^/\\(.*\\)/$" s)
  9713. ;; A regular expression
  9714. (cond
  9715. ((derived-mode-p 'org-mode)
  9716. (org-occur (match-string 1 s)))
  9717. (t (org-do-occur (match-string 1 s)))))
  9718. ((and (derived-mode-p 'org-mode) org-link-search-must-match-exact-headline)
  9719. (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  9720. (goto-char (point-min))
  9721. (cond
  9722. ((let (case-fold-search)
  9723. (re-search-forward (format org-complex-heading-regexp-format
  9724. (regexp-quote s))
  9725. nil t))
  9726. ;; OK, found a match
  9727. (setq type 'dedicated)
  9728. (goto-char (match-beginning 0)))
  9729. ((and (not org-link-search-inhibit-query)
  9730. (eq org-link-search-must-match-exact-headline 'query-to-create)
  9731. (y-or-n-p "No match - create this as a new heading? "))
  9732. (goto-char (point-max))
  9733. (or (bolp) (newline))
  9734. (insert "* " s "\n")
  9735. (beginning-of-line 0))
  9736. (t
  9737. (goto-char pos)
  9738. (error "No match"))))
  9739. (t
  9740. ;; A normal search string
  9741. (when (equal (string-to-char s) ?*)
  9742. ;; Anchor on headlines, post may include tags.
  9743. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  9744. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
  9745. s (substring s 1)))
  9746. (remove-text-properties
  9747. 0 (length s)
  9748. '(face nil mouse-face nil keymap nil fontified nil) s)
  9749. ;; Make a series of regular expressions to find a match
  9750. (setq words (org-split-string s "[ \n\r\t]+")
  9751. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  9752. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  9753. "\\)" markers)
  9754. re2a_ (concat "\\(" (mapconcat 'downcase words
  9755. "[ \t\r\n]+") "\\)[ \t\r\n]")
  9756. re2a (concat "[ \t\r\n]" re2a_)
  9757. re4_ (concat "\\(" (mapconcat 'downcase words
  9758. "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  9759. re4 (concat "[^a-zA-Z_]" re4_)
  9760. re1 (concat pre re2 post)
  9761. re3 (concat pre (if pre re4_ re4) post)
  9762. re5 (concat pre ".*" re4)
  9763. re2 (concat pre re2)
  9764. re2a (concat pre (if pre re2a_ re2a))
  9765. re4 (concat pre (if pre re4_ re4))
  9766. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  9767. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  9768. re5 "\\)"))
  9769. (cond
  9770. ((eq type 'org-occur) (org-occur reall))
  9771. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  9772. (t (goto-char (point-min))
  9773. (setq type 'fuzzy)
  9774. (if (or (and (org-search-not-self 1 re0 nil t)
  9775. (setq type 'dedicated))
  9776. (org-search-not-self 1 re1 nil t)
  9777. (org-search-not-self 1 re2 nil t)
  9778. (org-search-not-self 1 re2a nil t)
  9779. (org-search-not-self 1 re3 nil t)
  9780. (org-search-not-self 1 re4 nil t)
  9781. (org-search-not-self 1 re5 nil t))
  9782. (goto-char (match-beginning 1))
  9783. (goto-char pos)
  9784. (error "No match"))))))
  9785. (and (derived-mode-p 'org-mode)
  9786. (not stealth)
  9787. (org-show-context 'link-search))
  9788. type))
  9789. (defun org-search-not-self (group &rest args)
  9790. "Execute `re-search-forward', but only accept matches that do not
  9791. enclose the position of `org-open-link-marker'."
  9792. (let ((m org-open-link-marker))
  9793. (catch 'exit
  9794. (while (apply 're-search-forward args)
  9795. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  9796. (goto-char (match-end group))
  9797. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  9798. (> (match-beginning 0) (marker-position m))
  9799. (< (match-end 0) (marker-position m)))
  9800. (save-match-data
  9801. (or (not (org-in-regexp
  9802. org-bracket-link-analytic-regexp 1))
  9803. (not (match-end 4)) ; no description
  9804. (and (<= (match-beginning 4) (point))
  9805. (>= (match-end 4) (point))))))
  9806. (throw 'exit (point))))))))
  9807. (defun org-get-buffer-for-internal-link (buffer)
  9808. "Return a buffer to be used for displaying the link target of internal links."
  9809. (cond
  9810. ((not org-display-internal-link-with-indirect-buffer)
  9811. buffer)
  9812. ((string-match "(Clone)$" (buffer-name buffer))
  9813. (message "Buffer is already a clone, not making another one")
  9814. ;; we also do not modify visibility in this case
  9815. buffer)
  9816. (t ; make a new indirect buffer for displaying the link
  9817. (let* ((bn (buffer-name buffer))
  9818. (ibn (concat bn "(Clone)"))
  9819. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  9820. (with-current-buffer ib (org-overview))
  9821. ib))))
  9822. (defun org-do-occur (regexp &optional cleanup)
  9823. "Call the Emacs command `occur'.
  9824. If CLEANUP is non-nil, remove the printout of the regular expression
  9825. in the *Occur* buffer. This is useful if the regex is long and not useful
  9826. to read."
  9827. (occur regexp)
  9828. (when cleanup
  9829. (let ((cwin (selected-window)) win beg end)
  9830. (when (setq win (get-buffer-window "*Occur*"))
  9831. (select-window win))
  9832. (goto-char (point-min))
  9833. (when (re-search-forward "match[a-z]+" nil t)
  9834. (setq beg (match-end 0))
  9835. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  9836. (setq end (1- (match-beginning 0)))))
  9837. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  9838. (goto-char (point-min))
  9839. (select-window cwin))))
  9840. ;;; The mark ring for links jumps
  9841. (defvar org-mark-ring nil
  9842. "Mark ring for positions before jumps in Org-mode.")
  9843. (defvar org-mark-ring-last-goto nil
  9844. "Last position in the mark ring used to go back.")
  9845. ;; Fill and close the ring
  9846. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  9847. (loop for i from 1 to org-mark-ring-length do
  9848. (push (make-marker) org-mark-ring))
  9849. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  9850. org-mark-ring)
  9851. (defun org-mark-ring-push (&optional pos buffer)
  9852. "Put the current position or POS into the mark ring and rotate it."
  9853. (interactive)
  9854. (setq pos (or pos (point)))
  9855. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  9856. (move-marker (car org-mark-ring)
  9857. (or pos (point))
  9858. (or buffer (current-buffer)))
  9859. (message "%s"
  9860. (substitute-command-keys
  9861. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  9862. (defun org-mark-ring-goto (&optional n)
  9863. "Jump to the previous position in the mark ring.
  9864. With prefix arg N, jump back that many stored positions. When
  9865. called several times in succession, walk through the entire ring.
  9866. Org-mode commands jumping to a different position in the current file,
  9867. or to another Org-mode file, automatically push the old position
  9868. onto the ring."
  9869. (interactive "p")
  9870. (let (p m)
  9871. (if (eq last-command this-command)
  9872. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  9873. (setq p org-mark-ring))
  9874. (setq org-mark-ring-last-goto p)
  9875. (setq m (car p))
  9876. (org-pop-to-buffer-same-window (marker-buffer m))
  9877. (goto-char m)
  9878. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  9879. (defun org-remove-angle-brackets (s)
  9880. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  9881. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  9882. s)
  9883. (defun org-add-angle-brackets (s)
  9884. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  9885. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  9886. s)
  9887. (defun org-remove-double-quotes (s)
  9888. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  9889. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  9890. s)
  9891. ;;; Following specific links
  9892. (defun org-follow-timestamp-link ()
  9893. "Open an agenda view for the time-stamp date/range at point."
  9894. (cond
  9895. ((org-at-date-range-p t)
  9896. (let ((org-agenda-start-on-weekday)
  9897. (t1 (match-string 1))
  9898. (t2 (match-string 2)) tt1 tt2)
  9899. (setq tt1 (time-to-days (org-time-string-to-time t1))
  9900. tt2 (time-to-days (org-time-string-to-time t2)))
  9901. (let ((org-agenda-buffer-tmp-name
  9902. (format "*Org Agenda(a:%s)"
  9903. (concat (substring t1 0 10) "--" (substring t2 0 10)))))
  9904. (org-agenda-list nil tt1 (1+ (- tt2 tt1))))))
  9905. ((org-at-timestamp-p t)
  9906. (let ((org-agenda-buffer-tmp-name
  9907. (format "*Org Agenda(a:%s)" (substring (match-string 1) 0 10))))
  9908. (org-agenda-list nil (time-to-days (org-time-string-to-time
  9909. (substring (match-string 1) 0 10)))
  9910. 1)))
  9911. (t (error "This should not happen"))))
  9912. ;;; Following file links
  9913. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  9914. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  9915. (declare-function mailcap-mime-info
  9916. "mailcap" (string &optional request no-decode))
  9917. (defvar org-wait nil)
  9918. (defun org-open-file (path &optional in-emacs line search)
  9919. "Open the file at PATH.
  9920. First, this expands any special file name abbreviations. Then the
  9921. configuration variable `org-file-apps' is checked if it contains an
  9922. entry for this file type, and if yes, the corresponding command is launched.
  9923. If no application is found, Emacs simply visits the file.
  9924. With optional prefix argument IN-EMACS, Emacs will visit the file.
  9925. With a double \\[universal-argument] \\[universal-argument] \
  9926. prefix arg, Org tries to avoid opening in Emacs
  9927. and to use an external application to visit the file.
  9928. Optional LINE specifies a line to go to, optional SEARCH a string
  9929. to search for. If LINE or SEARCH is given, the file will be
  9930. opened in Emacs, unless an entry from org-file-apps that makes
  9931. use of groups in a regexp matches.
  9932. If you want to change the way frames are used when following a
  9933. link, please customize `org-link-frame-setup'.
  9934. If the file does not exist, an error is thrown."
  9935. (let* ((file (if (equal path "")
  9936. buffer-file-name
  9937. (substitute-in-file-name (expand-file-name path))))
  9938. (file-apps (append org-file-apps (org-default-apps)))
  9939. (apps (org-remove-if
  9940. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9941. (apps-dlink (org-remove-if-not
  9942. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9943. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  9944. (dirp (if remp nil (file-directory-p file)))
  9945. (file (if (and dirp org-open-directory-means-index-dot-org)
  9946. (concat (file-name-as-directory file) "index.org")
  9947. file))
  9948. (a-m-a-p (assq 'auto-mode apps))
  9949. (dfile (downcase file))
  9950. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  9951. (link (cond ((and (eq line nil)
  9952. (eq search nil))
  9953. file)
  9954. (line
  9955. (concat file "::" (number-to-string line)))
  9956. (search
  9957. (concat file "::" search))))
  9958. (dlink (downcase link))
  9959. (old-buffer (current-buffer))
  9960. (old-pos (point))
  9961. (old-mode major-mode)
  9962. ext cmd link-match-data)
  9963. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  9964. (setq ext (match-string 1 dfile))
  9965. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  9966. (setq ext (match-string 1 dfile))))
  9967. (cond
  9968. ((member in-emacs '((16) system))
  9969. (setq cmd (cdr (assoc 'system apps))))
  9970. (in-emacs (setq cmd 'emacs))
  9971. (t
  9972. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  9973. (and dirp (cdr (assoc 'directory apps)))
  9974. ; first, try matching against apps-dlink
  9975. ; if we get a match here, store the match data for later
  9976. (let ((match (assoc-default dlink apps-dlink
  9977. 'string-match)))
  9978. (if match
  9979. (progn (setq link-match-data (match-data))
  9980. match)
  9981. (progn (setq in-emacs (or in-emacs line search))
  9982. nil))) ; if we have no match in apps-dlink,
  9983. ; always open the file in emacs if line or search
  9984. ; is given (for backwards compatibility)
  9985. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  9986. 'string-match)
  9987. (cdr (assoc ext apps))
  9988. (cdr (assoc t apps))))))
  9989. (when (eq cmd 'system)
  9990. (setq cmd (cdr (assoc 'system apps))))
  9991. (when (eq cmd 'default)
  9992. (setq cmd (cdr (assoc t apps))))
  9993. (when (eq cmd 'mailcap)
  9994. (require 'mailcap)
  9995. (mailcap-parse-mailcaps)
  9996. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  9997. (command (mailcap-mime-info mime-type)))
  9998. (if (stringp command)
  9999. (setq cmd command)
  10000. (setq cmd 'emacs))))
  10001. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  10002. (not (file-exists-p file))
  10003. (not org-open-non-existing-files))
  10004. (user-error "No such file: %s" file))
  10005. (cond
  10006. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  10007. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  10008. (while (string-match "['\"]%s['\"]" cmd)
  10009. (setq cmd (replace-match "%s" t t cmd)))
  10010. (while (string-match "%s" cmd)
  10011. (setq cmd (replace-match
  10012. (save-match-data
  10013. (shell-quote-argument
  10014. (convert-standard-filename file)))
  10015. t t cmd)))
  10016. ;; Replace "%1", "%2" etc. in command with group matches from regex
  10017. (save-match-data
  10018. (let ((match-index 1)
  10019. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  10020. (set-match-data link-match-data)
  10021. (while (<= match-index number-of-groups)
  10022. (let ((regex (concat "%" (number-to-string match-index)))
  10023. (replace-with (match-string match-index dlink)))
  10024. (while (string-match regex cmd)
  10025. (setq cmd (replace-match replace-with t t cmd))))
  10026. (setq match-index (+ match-index 1)))))
  10027. (save-window-excursion
  10028. (message "Running %s...done" cmd)
  10029. (start-process-shell-command cmd nil cmd)
  10030. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))))
  10031. ((or (stringp cmd)
  10032. (eq cmd 'emacs))
  10033. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  10034. (widen)
  10035. (if line (org-goto-line line)
  10036. (if search (org-link-search search))))
  10037. ((consp cmd)
  10038. (let ((file (convert-standard-filename file)))
  10039. (save-match-data
  10040. (set-match-data link-match-data)
  10041. (eval cmd))))
  10042. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  10043. (and (derived-mode-p 'org-mode) (eq old-mode 'org-mode)
  10044. (or (not (equal old-buffer (current-buffer)))
  10045. (not (equal old-pos (point))))
  10046. (org-mark-ring-push old-pos old-buffer))))
  10047. (defun org-file-apps-entry-match-against-dlink-p (entry)
  10048. "This function returns non-nil if `entry' uses a regular
  10049. expression which should be matched against the whole link by
  10050. org-open-file.
  10051. It assumes that is the case when the entry uses a regular
  10052. expression which has at least one grouping construct and the
  10053. action is either a lisp form or a command string containing
  10054. '%1', i.e. using at least one subexpression match as a
  10055. parameter."
  10056. (let ((selector (car entry))
  10057. (action (cdr entry)))
  10058. (if (stringp selector)
  10059. (and (> (regexp-opt-depth selector) 0)
  10060. (or (and (stringp action)
  10061. (string-match "%[0-9]" action))
  10062. (consp action)))
  10063. nil)))
  10064. (defun org-default-apps ()
  10065. "Return the default applications for this operating system."
  10066. (cond
  10067. ((eq system-type 'darwin)
  10068. org-file-apps-defaults-macosx)
  10069. ((eq system-type 'windows-nt)
  10070. org-file-apps-defaults-windowsnt)
  10071. (t org-file-apps-defaults-gnu)))
  10072. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  10073. "Convert extensions to regular expressions in the cars of LIST.
  10074. Also, weed out any non-string entries, because the return value is used
  10075. only for regexp matching.
  10076. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  10077. point to the symbol `emacs', indicating that the file should
  10078. be opened in Emacs."
  10079. (append
  10080. (delq nil
  10081. (mapcar (lambda (x)
  10082. (if (not (stringp (car x)))
  10083. nil
  10084. (if (string-match "\\W" (car x))
  10085. x
  10086. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  10087. list))
  10088. (if add-auto-mode
  10089. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  10090. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  10091. (defun org-file-remote-p (file)
  10092. "Test whether FILE specifies a location on a remote system.
  10093. Return non-nil if the location is indeed remote.
  10094. For example, the filename \"/user@host:/foo\" specifies a location
  10095. on the system \"/user@host:\"."
  10096. (cond ((fboundp 'file-remote-p)
  10097. (file-remote-p file))
  10098. ((fboundp 'tramp-handle-file-remote-p)
  10099. (tramp-handle-file-remote-p file))
  10100. ((and (boundp 'ange-ftp-name-format)
  10101. (string-match (car ange-ftp-name-format) file))
  10102. t)))
  10103. ;;;; Refiling
  10104. (defun org-get-org-file ()
  10105. "Read a filename, with default directory `org-directory'."
  10106. (let ((default (or org-default-notes-file remember-data-file)))
  10107. (read-file-name (format "File name [%s]: " default)
  10108. (file-name-as-directory org-directory)
  10109. default)))
  10110. (defun org-notes-order-reversed-p ()
  10111. "Check if the current file should receive notes in reversed order."
  10112. (cond
  10113. ((not org-reverse-note-order) nil)
  10114. ((eq t org-reverse-note-order) t)
  10115. ((not (listp org-reverse-note-order)) nil)
  10116. (t (catch 'exit
  10117. (let ((all org-reverse-note-order)
  10118. entry)
  10119. (while (setq entry (pop all))
  10120. (if (string-match (car entry) buffer-file-name)
  10121. (throw 'exit (cdr entry))))
  10122. nil)))))
  10123. (defvar org-refile-target-table nil
  10124. "The list of refile targets, created by `org-refile'.")
  10125. (defvar org-agenda-new-buffers nil
  10126. "Buffers created to visit agenda files.")
  10127. (defvar org-refile-cache nil
  10128. "Cache for refile targets.")
  10129. (defvar org-refile-markers nil
  10130. "All the markers used for caching refile locations.")
  10131. (defun org-refile-marker (pos)
  10132. "Get a new refile marker, but only if caching is in use."
  10133. (if (not org-refile-use-cache)
  10134. pos
  10135. (let ((m (make-marker)))
  10136. (move-marker m pos)
  10137. (push m org-refile-markers)
  10138. m)))
  10139. (defun org-refile-cache-clear ()
  10140. "Clear the refile cache and disable all the markers."
  10141. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  10142. (setq org-refile-markers nil)
  10143. (setq org-refile-cache nil)
  10144. (message "Refile cache has been cleared"))
  10145. (defun org-refile-cache-check-set (set)
  10146. "Check if all the markers in the cache still have live buffers."
  10147. (let (marker)
  10148. (catch 'exit
  10149. (while (and set (setq marker (nth 3 (pop set))))
  10150. ;; If `org-refile-use-outline-path' is 'file, marker may be nil
  10151. (when (and marker (null (marker-buffer marker)))
  10152. (message "Please regenerate the refile cache with `C-0 C-c C-w'")
  10153. (sit-for 3)
  10154. (throw 'exit nil)))
  10155. t)))
  10156. (defun org-refile-cache-put (set &rest identifiers)
  10157. "Push the refile targets SET into the cache, under IDENTIFIERS."
  10158. (let* ((key (sha1 (prin1-to-string identifiers)))
  10159. (entry (assoc key org-refile-cache)))
  10160. (if entry
  10161. (setcdr entry set)
  10162. (push (cons key set) org-refile-cache))))
  10163. (defun org-refile-cache-get (&rest identifiers)
  10164. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  10165. (cond
  10166. ((not org-refile-cache) nil)
  10167. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  10168. (t
  10169. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  10170. org-refile-cache))))
  10171. (and set (org-refile-cache-check-set set) set)))))
  10172. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  10173. "Produce a table with refile targets."
  10174. (let ((case-fold-search nil)
  10175. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  10176. (entries (or org-refile-targets '((nil . (:level . 1)))))
  10177. targets tgs txt re files f desc descre fast-path-p level pos0)
  10178. (message "Getting targets...")
  10179. (with-current-buffer (or default-buffer (current-buffer))
  10180. (while (setq entry (pop entries))
  10181. (setq files (car entry) desc (cdr entry))
  10182. (setq fast-path-p nil)
  10183. (cond
  10184. ((null files) (setq files (list (current-buffer))))
  10185. ((eq files 'org-agenda-files)
  10186. (setq files (org-agenda-files 'unrestricted)))
  10187. ((and (symbolp files) (fboundp files))
  10188. (setq files (funcall files)))
  10189. ((and (symbolp files) (boundp files))
  10190. (setq files (symbol-value files))))
  10191. (if (stringp files) (setq files (list files)))
  10192. (cond
  10193. ((eq (car desc) :tag)
  10194. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  10195. ((eq (car desc) :todo)
  10196. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  10197. ((eq (car desc) :regexp)
  10198. (setq descre (cdr desc)))
  10199. ((eq (car desc) :level)
  10200. (setq descre (concat "^\\*\\{" (number-to-string
  10201. (if org-odd-levels-only
  10202. (1- (* 2 (cdr desc)))
  10203. (cdr desc)))
  10204. "\\}[ \t]")))
  10205. ((eq (car desc) :maxlevel)
  10206. (setq fast-path-p t)
  10207. (setq descre (concat "^\\*\\{1," (number-to-string
  10208. (if org-odd-levels-only
  10209. (1- (* 2 (cdr desc)))
  10210. (cdr desc)))
  10211. "\\}[ \t]")))
  10212. (t (error "Bad refiling target description %s" desc)))
  10213. (while (setq f (pop files))
  10214. (with-current-buffer
  10215. (if (bufferp f) f (org-get-agenda-file-buffer f))
  10216. (or
  10217. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  10218. (progn
  10219. (if (bufferp f) (setq f (buffer-file-name
  10220. (buffer-base-buffer f))))
  10221. (setq f (and f (expand-file-name f)))
  10222. (if (eq org-refile-use-outline-path 'file)
  10223. (push (list (file-name-nondirectory f) f nil nil) tgs))
  10224. (save-excursion
  10225. (save-restriction
  10226. (widen)
  10227. (goto-char (point-min))
  10228. (while (re-search-forward descre nil t)
  10229. (goto-char (setq pos0 (point-at-bol)))
  10230. (catch 'next
  10231. (when org-refile-target-verify-function
  10232. (save-match-data
  10233. (or (funcall org-refile-target-verify-function)
  10234. (throw 'next t))))
  10235. (when (and (looking-at org-complex-heading-regexp)
  10236. (not (member (match-string 4) excluded-entries))
  10237. (match-string 4))
  10238. (setq level (org-reduced-level
  10239. (- (match-end 1) (match-beginning 1)))
  10240. txt (org-link-display-format (match-string 4))
  10241. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  10242. re (format org-complex-heading-regexp-format
  10243. (regexp-quote (match-string 4))))
  10244. (when org-refile-use-outline-path
  10245. (setq txt (mapconcat
  10246. 'org-protect-slash
  10247. (append
  10248. (if (eq org-refile-use-outline-path
  10249. 'file)
  10250. (list (file-name-nondirectory
  10251. (buffer-file-name
  10252. (buffer-base-buffer))))
  10253. (if (eq org-refile-use-outline-path
  10254. 'full-file-path)
  10255. (list (buffer-file-name
  10256. (buffer-base-buffer)))))
  10257. (org-get-outline-path fast-path-p
  10258. level txt)
  10259. (list txt))
  10260. "/")))
  10261. (push (list txt f re (org-refile-marker (point)))
  10262. tgs)))
  10263. (when (= (point) pos0)
  10264. ;; verification function has not moved point
  10265. (goto-char (point-at-eol))))))))
  10266. (when org-refile-use-cache
  10267. (org-refile-cache-put tgs (buffer-file-name) descre))
  10268. (setq targets (append tgs targets))))))
  10269. (message "Getting targets...done")
  10270. (nreverse targets)))
  10271. (defun org-protect-slash (s)
  10272. (while (string-match "/" s)
  10273. (setq s (replace-match "\\" t t s)))
  10274. s)
  10275. (defvar org-olpa (make-vector 20 nil))
  10276. (defun org-get-outline-path (&optional fastp level heading)
  10277. "Return the outline path to the current entry, as a list.
  10278. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  10279. routine which makes outline path derivations for an entire file,
  10280. avoiding backtracing. Refile target collection makes use of that."
  10281. (if fastp
  10282. (progn
  10283. (if (> level 19)
  10284. (error "Outline path failure, more than 19 levels"))
  10285. (loop for i from level upto 19 do
  10286. (aset org-olpa i nil))
  10287. (prog1
  10288. (delq nil (append org-olpa nil))
  10289. (aset org-olpa level heading)))
  10290. (let (rtn case-fold-search)
  10291. (save-excursion
  10292. (save-restriction
  10293. (widen)
  10294. (while (org-up-heading-safe)
  10295. (when (looking-at org-complex-heading-regexp)
  10296. (push (org-trim
  10297. (replace-regexp-in-string
  10298. ;; Remove statistical/checkboxes cookies
  10299. "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" ""
  10300. (org-match-string-no-properties 4)))
  10301. rtn)))
  10302. rtn)))))
  10303. (defun org-format-outline-path (path &optional width prefix separator)
  10304. "Format the outline path PATH for display.
  10305. WIDTH is the maximum number of characters that is available.
  10306. PREFIX is a prefix to be included in the returned string,
  10307. such as the file name.
  10308. SEPARATOR is inserted between the different parts of the path,
  10309. the default is \"/\"."
  10310. (setq width (or width 79))
  10311. (if prefix (setq width (- width (length prefix))))
  10312. (if (not path)
  10313. (or prefix "")
  10314. (let* ((nsteps (length path))
  10315. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  10316. (maxwidth (if (<= total-width width)
  10317. 10000 ;; everything fits
  10318. ;; we need to shorten the level headings
  10319. (/ (- width nsteps) nsteps)))
  10320. (org-odd-levels-only nil)
  10321. (n 0)
  10322. (total (1+ (length prefix))))
  10323. (setq maxwidth (max maxwidth 10))
  10324. (concat prefix
  10325. (if prefix (or separator "/"))
  10326. (mapconcat
  10327. (lambda (h)
  10328. (setq n (1+ n))
  10329. (if (and (= n nsteps) (< maxwidth 10000))
  10330. (setq maxwidth (- total-width total)))
  10331. (if (< (length h) maxwidth)
  10332. (progn (setq total (+ total (length h) 1)) h)
  10333. (setq h (substring h 0 (- maxwidth 2))
  10334. total (+ total maxwidth 1))
  10335. (if (string-match "[ \t]+\\'" h)
  10336. (setq h (substring h 0 (match-beginning 0))))
  10337. (setq h (concat h "..")))
  10338. (org-add-props h nil 'face
  10339. (nth (% (1- n) org-n-level-faces)
  10340. org-level-faces))
  10341. h)
  10342. path (or separator "/"))))))
  10343. (defun org-display-outline-path (&optional file current separator just-return-string)
  10344. "Display the current outline path in the echo area.
  10345. If FILE is non-nil, prepend the output with the file name.
  10346. If CURRENT is non-nil, append the current heading to the output.
  10347. SEPARATOR is passed through to `org-format-outline-path'. It separates
  10348. the different parts of the path and defaults to \"/\".
  10349. If JUST-RETURN-STRING is non-nil, return a string, don't display a message."
  10350. (interactive "P")
  10351. (let* (case-fold-search
  10352. message-log-max ; Don't populate the *Messages* buffer
  10353. (bfn (buffer-file-name (buffer-base-buffer)))
  10354. (path (and (derived-mode-p 'org-mode) (org-get-outline-path)))
  10355. res)
  10356. (if current (setq path (append path
  10357. (save-excursion
  10358. (org-back-to-heading t)
  10359. (if (looking-at org-complex-heading-regexp)
  10360. (list (match-string 4)))))))
  10361. (setq res
  10362. (org-format-outline-path
  10363. path
  10364. (1- (frame-width))
  10365. (and file bfn (concat (file-name-nondirectory bfn) separator))
  10366. separator))
  10367. (if just-return-string
  10368. (org-no-properties res)
  10369. (message "%s" res))))
  10370. (defvar org-refile-history nil
  10371. "History for refiling operations.")
  10372. (defvar org-after-refile-insert-hook nil
  10373. "Hook run after `org-refile' has inserted its stuff at the new location.
  10374. Note that this is still *before* the stuff will be removed from
  10375. the *old* location.")
  10376. (defvar org-capture-last-stored-marker)
  10377. (defvar org-refile-keep nil
  10378. "Non-nil means `org-refile' will copy instead of refile.")
  10379. (defun org-copy ()
  10380. "Like `org-refile', but copy."
  10381. (interactive)
  10382. (let ((org-refile-keep t))
  10383. (funcall 'org-refile nil nil nil "Copy")))
  10384. (defun org-refile (&optional goto default-buffer rfloc msg)
  10385. "Move the entry or entries at point to another heading.
  10386. The list of target headings is compiled using the information in
  10387. `org-refile-targets', which see.
  10388. At the target location, the entry is filed as a subitem of the target
  10389. heading. Depending on `org-reverse-note-order', the new subitem will
  10390. either be the first or the last subitem.
  10391. If there is an active region, all entries in that region will be moved.
  10392. However, the region must fulfill the requirement that the first heading
  10393. is the first one sets the top-level of the moved text - at most siblings
  10394. below it are allowed.
  10395. With prefix arg GOTO, the command will only visit the target location
  10396. and not actually move anything.
  10397. With a double prefix arg \\[universal-argument] \\[universal-argument], \
  10398. go to the location where the last refiling operation has put the subtree.
  10399. With a prefix argument of `2', refile to the running clock.
  10400. RFLOC can be a refile location obtained in a different way.
  10401. MSG is a string to replace \"Refile\" in the default prompt with
  10402. another verb. E.g. `org-copy' sets this parameter to \"Copy\".
  10403. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  10404. If you are using target caching (see `org-refile-use-cache'),
  10405. you have to clear the target cache in order to find new targets.
  10406. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  10407. prefix argument (`C-u C-u C-u C-c C-w')."
  10408. (interactive "P")
  10409. (if (member goto '(0 (64)))
  10410. (org-refile-cache-clear)
  10411. (let* ((actionmsg (or msg "Refile"))
  10412. (cbuf (current-buffer))
  10413. (regionp (org-region-active-p))
  10414. (region-start (and regionp (region-beginning)))
  10415. (region-end (and regionp (region-end)))
  10416. (region-length (and regionp (- region-end region-start)))
  10417. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  10418. pos it nbuf file re level reversed)
  10419. (setq last-command nil)
  10420. (when regionp
  10421. (goto-char region-start)
  10422. (or (bolp) (goto-char (point-at-bol)))
  10423. (setq region-start (point))
  10424. (unless (or (org-kill-is-subtree-p
  10425. (buffer-substring region-start region-end))
  10426. (prog1 org-refile-active-region-within-subtree
  10427. (org-toggle-heading)))
  10428. (user-error "The region is not a (sequence of) subtree(s)")))
  10429. (if (equal goto '(16))
  10430. (org-refile-goto-last-stored)
  10431. (when (or
  10432. (and (equal goto 2)
  10433. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  10434. (prog1
  10435. (setq it (list (or org-clock-heading "running clock")
  10436. (buffer-file-name
  10437. (marker-buffer org-clock-hd-marker))
  10438. ""
  10439. (marker-position org-clock-hd-marker)))
  10440. (setq goto nil)))
  10441. (setq it (or rfloc
  10442. (let (heading-text)
  10443. (save-excursion
  10444. (unless goto
  10445. (org-back-to-heading t)
  10446. (setq heading-text
  10447. (nth 4 (org-heading-components))))
  10448. (org-refile-get-location
  10449. (cond (goto "Goto")
  10450. (regionp (concat actionmsg " region to"))
  10451. (t (concat actionmsg " subtree \""
  10452. heading-text "\" to")))
  10453. default-buffer
  10454. (and (not (equal '(4) goto))
  10455. org-refile-allow-creating-parent-nodes)
  10456. goto))))))
  10457. (setq file (nth 1 it)
  10458. re (nth 2 it)
  10459. pos (nth 3 it))
  10460. (if (and (not goto)
  10461. pos
  10462. (equal (buffer-file-name) file)
  10463. (if regionp
  10464. (and (>= pos region-start)
  10465. (<= pos region-end))
  10466. (and (>= pos (point))
  10467. (< pos (save-excursion
  10468. (org-end-of-subtree t t))))))
  10469. (error "Cannot refile to position inside the tree or region"))
  10470. (setq nbuf (or (find-buffer-visiting file)
  10471. (find-file-noselect file)))
  10472. (if goto
  10473. (progn
  10474. (org-pop-to-buffer-same-window nbuf)
  10475. (goto-char pos)
  10476. (org-show-context 'org-goto))
  10477. (if regionp
  10478. (progn
  10479. (org-kill-new (buffer-substring region-start region-end))
  10480. (org-save-markers-in-region region-start region-end))
  10481. (org-copy-subtree 1 nil t))
  10482. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  10483. (find-file-noselect file)))
  10484. (setq reversed (org-notes-order-reversed-p))
  10485. (save-excursion
  10486. (save-restriction
  10487. (widen)
  10488. (if pos
  10489. (progn
  10490. (goto-char pos)
  10491. (looking-at org-outline-regexp)
  10492. (setq level (org-get-valid-level (funcall outline-level) 1))
  10493. (goto-char
  10494. (if reversed
  10495. (or (outline-next-heading) (point-max))
  10496. (or (save-excursion (org-get-next-sibling))
  10497. (org-end-of-subtree t t)
  10498. (point-max)))))
  10499. (setq level 1)
  10500. (if (not reversed)
  10501. (goto-char (point-max))
  10502. (goto-char (point-min))
  10503. (or (outline-next-heading) (goto-char (point-max)))))
  10504. (if (not (bolp)) (newline))
  10505. (org-paste-subtree level)
  10506. (when org-log-refile
  10507. (org-add-log-setup 'refile nil nil 'findpos
  10508. org-log-refile)
  10509. (unless (eq org-log-refile 'note)
  10510. (save-excursion (org-add-log-note))))
  10511. (and org-auto-align-tags
  10512. (let ((org-loop-over-headlines-in-active-region nil))
  10513. (org-set-tags nil t)))
  10514. (with-demoted-errors
  10515. (bookmark-set "org-refile-last-stored"))
  10516. ;; If we are refiling for capture, make sure that the
  10517. ;; last-capture pointers point here
  10518. (when (org-bound-and-true-p org-refile-for-capture)
  10519. (with-demoted-errors
  10520. (bookmark-set "org-capture-last-stored-marker"))
  10521. (move-marker org-capture-last-stored-marker (point)))
  10522. (if (fboundp 'deactivate-mark) (deactivate-mark))
  10523. (run-hooks 'org-after-refile-insert-hook))))
  10524. (unless org-refile-keep
  10525. (if regionp
  10526. (delete-region (point) (+ (point) region-length))
  10527. (org-cut-subtree)))
  10528. (when (featurep 'org-inlinetask)
  10529. (org-inlinetask-remove-END-maybe))
  10530. (setq org-markers-to-move nil)
  10531. (message (concat actionmsg " to \"%s\" in file %s: done") (car it) file)))))))
  10532. (defun org-refile-goto-last-stored ()
  10533. "Go to the location where the last refile was stored."
  10534. (interactive)
  10535. (bookmark-jump "org-refile-last-stored")
  10536. (message "This is the location of the last refile"))
  10537. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  10538. no-exclude)
  10539. "Prompt the user for a refile location, using PROMPT.
  10540. PROMPT should not be suffixed with a colon and a space, because
  10541. this function appends the default value from
  10542. `org-refile-history' automatically, if that is not empty.
  10543. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  10544. this is used for the GOTO interface."
  10545. (let ((org-refile-targets org-refile-targets)
  10546. (org-refile-use-outline-path org-refile-use-outline-path)
  10547. excluded-entries)
  10548. (when (and (derived-mode-p 'org-mode)
  10549. (not org-refile-use-cache)
  10550. (not no-exclude))
  10551. (org-map-tree
  10552. (lambda()
  10553. (setq excluded-entries
  10554. (append excluded-entries (list (org-get-heading t t)))))))
  10555. (setq org-refile-target-table
  10556. (org-refile-get-targets default-buffer excluded-entries)))
  10557. (unless org-refile-target-table
  10558. (user-error "No refile targets"))
  10559. (let* ((cbuf (current-buffer))
  10560. (partial-completion-mode nil)
  10561. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  10562. (cfunc (if (and org-refile-use-outline-path
  10563. org-outline-path-complete-in-steps)
  10564. 'org-olpath-completing-read
  10565. 'org-icompleting-read))
  10566. (extra (if org-refile-use-outline-path "/" ""))
  10567. (cbnex (concat (buffer-name) extra))
  10568. (filename (and cfn (expand-file-name cfn)))
  10569. (tbl (mapcar
  10570. (lambda (x)
  10571. (if (and (not (member org-refile-use-outline-path
  10572. '(file full-file-path)))
  10573. (not (equal filename (nth 1 x))))
  10574. (cons (concat (car x) extra " ("
  10575. (file-name-nondirectory (nth 1 x)) ")")
  10576. (cdr x))
  10577. (cons (concat (car x) extra) (cdr x))))
  10578. org-refile-target-table))
  10579. (completion-ignore-case t)
  10580. cdef
  10581. (prompt (concat prompt
  10582. (or (and (car org-refile-history)
  10583. (concat " (default " (car org-refile-history) ")"))
  10584. (and (assoc cbnex tbl) (setq cdef cbnex)
  10585. (concat " (default " cbnex ")"))) ": "))
  10586. pa answ parent-target child parent old-hist)
  10587. (setq old-hist org-refile-history)
  10588. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  10589. nil 'org-refile-history (or cdef (car org-refile-history))))
  10590. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  10591. (org-refile-check-position pa)
  10592. (if pa
  10593. (progn
  10594. (when (or (not org-refile-history)
  10595. (not (eq old-hist org-refile-history))
  10596. (not (equal (car pa) (car org-refile-history))))
  10597. (setq org-refile-history
  10598. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  10599. org-refile-history
  10600. (cdr org-refile-history))))
  10601. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  10602. (pop org-refile-history)))
  10603. pa)
  10604. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  10605. (progn
  10606. (setq parent (match-string 1 answ)
  10607. child (match-string 2 answ))
  10608. (setq parent-target (or (assoc parent tbl)
  10609. (assoc (concat parent "/") tbl)))
  10610. (when (and parent-target
  10611. (or (eq new-nodes t)
  10612. (and (eq new-nodes 'confirm)
  10613. (y-or-n-p (format "Create new node \"%s\"? "
  10614. child)))))
  10615. (org-refile-new-child parent-target child)))
  10616. (user-error "Invalid target location")))))
  10617. (declare-function org-string-nw-p "org-macs" (s))
  10618. (defun org-refile-check-position (refile-pointer)
  10619. "Check if the refile pointer matches the headline to which it points."
  10620. (let* ((file (nth 1 refile-pointer))
  10621. (re (nth 2 refile-pointer))
  10622. (pos (nth 3 refile-pointer))
  10623. buffer)
  10624. (if (and (not (markerp pos)) (not file))
  10625. (user-error "Please save the buffer to a file before refiling")
  10626. (when (org-string-nw-p re)
  10627. (setq buffer (if (markerp pos)
  10628. (marker-buffer pos)
  10629. (or (find-buffer-visiting file)
  10630. (find-file-noselect file))))
  10631. (with-current-buffer buffer
  10632. (save-excursion
  10633. (save-restriction
  10634. (widen)
  10635. (goto-char pos)
  10636. (beginning-of-line 1)
  10637. (unless (org-looking-at-p re)
  10638. (user-error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling")))))))))
  10639. (defun org-refile-new-child (parent-target child)
  10640. "Use refile target PARENT-TARGET to add new CHILD below it."
  10641. (unless parent-target
  10642. (error "Cannot find parent for new node"))
  10643. (let ((file (nth 1 parent-target))
  10644. (pos (nth 3 parent-target))
  10645. level)
  10646. (with-current-buffer (or (find-buffer-visiting file)
  10647. (find-file-noselect file))
  10648. (save-excursion
  10649. (save-restriction
  10650. (widen)
  10651. (if pos
  10652. (goto-char pos)
  10653. (goto-char (point-max))
  10654. (if (not (bolp)) (newline)))
  10655. (when (looking-at org-outline-regexp)
  10656. (setq level (funcall outline-level))
  10657. (org-end-of-subtree t t))
  10658. (org-back-over-empty-lines)
  10659. (insert "\n" (make-string
  10660. (if pos (org-get-valid-level level 1) 1) ?*)
  10661. " " child "\n")
  10662. (beginning-of-line 0)
  10663. (list (concat (car parent-target) "/" child) file "" (point)))))))
  10664. (defun org-olpath-completing-read (prompt collection &rest args)
  10665. "Read an outline path like a file name."
  10666. (let ((thetable collection)
  10667. (org-completion-use-ido nil) ; does not work with ido.
  10668. (org-completion-use-iswitchb nil)) ; or iswitchb
  10669. (apply
  10670. 'org-icompleting-read prompt
  10671. (lambda (string predicate &optional flag)
  10672. (let (rtn r f (l (length string)))
  10673. (cond
  10674. ((eq flag nil)
  10675. ;; try completion
  10676. (try-completion string thetable))
  10677. ((eq flag t)
  10678. ;; all-completions
  10679. (setq rtn (all-completions string thetable predicate))
  10680. (mapcar
  10681. (lambda (x)
  10682. (setq r (substring x l))
  10683. (if (string-match " ([^)]*)$" x)
  10684. (setq f (match-string 0 x))
  10685. (setq f ""))
  10686. (if (string-match "/" r)
  10687. (concat string (substring r 0 (match-end 0)) f)
  10688. x))
  10689. rtn))
  10690. ((eq flag 'lambda)
  10691. ;; exact match?
  10692. (assoc string thetable)))))
  10693. args)))
  10694. ;;;; Dynamic blocks
  10695. (defun org-find-dblock (name)
  10696. "Find the first dynamic block with name NAME in the buffer.
  10697. If not found, stay at current position and return nil."
  10698. (let ((case-fold-search t) pos)
  10699. (save-excursion
  10700. (goto-char (point-min))
  10701. (setq pos (and (re-search-forward
  10702. (concat "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+" name "\\>") nil t)
  10703. (match-beginning 0))))
  10704. (if pos (goto-char pos))
  10705. pos))
  10706. (defconst org-dblock-start-re
  10707. "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  10708. "Matches the start line of a dynamic block, with parameters.")
  10709. (defconst org-dblock-end-re "^[ \t]*#\\+\\(?:END\\|end\\)\\([: \t\r\n]\\|$\\)"
  10710. "Matches the end of a dynamic block.")
  10711. (defun org-create-dblock (plist)
  10712. "Create a dynamic block section, with parameters taken from PLIST.
  10713. PLIST must contain a :name entry which is used as name of the block."
  10714. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  10715. (end-of-line 1)
  10716. (newline))
  10717. (let ((col (current-column))
  10718. (name (plist-get plist :name)))
  10719. (insert "#+BEGIN: " name)
  10720. (while plist
  10721. (if (eq (car plist) :name)
  10722. (setq plist (cddr plist))
  10723. (insert " " (prin1-to-string (pop plist)))))
  10724. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  10725. (beginning-of-line -2)))
  10726. (defun org-prepare-dblock ()
  10727. "Prepare dynamic block for refresh.
  10728. This empties the block, puts the cursor at the insert position and returns
  10729. the property list including an extra property :name with the block name."
  10730. (unless (looking-at org-dblock-start-re)
  10731. (user-error "Not at a dynamic block"))
  10732. (let* ((begdel (1+ (match-end 0)))
  10733. (name (org-no-properties (match-string 1)))
  10734. (params (append (list :name name)
  10735. (read (concat "(" (match-string 3) ")")))))
  10736. (save-excursion
  10737. (beginning-of-line 1)
  10738. (skip-chars-forward " \t")
  10739. (setq params (plist-put params :indentation-column (current-column))))
  10740. (unless (re-search-forward org-dblock-end-re nil t)
  10741. (error "Dynamic block not terminated"))
  10742. (setq params
  10743. (append params
  10744. (list :content (buffer-substring
  10745. begdel (match-beginning 0)))))
  10746. (delete-region begdel (match-beginning 0))
  10747. (goto-char begdel)
  10748. (open-line 1)
  10749. params))
  10750. (defun org-map-dblocks (&optional command)
  10751. "Apply COMMAND to all dynamic blocks in the current buffer.
  10752. If COMMAND is not given, use `org-update-dblock'."
  10753. (let ((cmd (or command 'org-update-dblock)))
  10754. (save-excursion
  10755. (goto-char (point-min))
  10756. (while (re-search-forward org-dblock-start-re nil t)
  10757. (goto-char (match-beginning 0))
  10758. (save-excursion
  10759. (condition-case nil
  10760. (funcall cmd)
  10761. (error (message "Error during update of dynamic block"))))
  10762. (unless (re-search-forward org-dblock-end-re nil t)
  10763. (error "Dynamic block not terminated"))))))
  10764. (defun org-dblock-update (&optional arg)
  10765. "User command for updating dynamic blocks.
  10766. Update the dynamic block at point. With prefix ARG, update all dynamic
  10767. blocks in the buffer."
  10768. (interactive "P")
  10769. (if arg
  10770. (org-update-all-dblocks)
  10771. (or (looking-at org-dblock-start-re)
  10772. (org-beginning-of-dblock))
  10773. (org-update-dblock)))
  10774. (defun org-update-dblock ()
  10775. "Update the dynamic block at point.
  10776. This means to empty the block, parse for parameters and then call
  10777. the correct writing function."
  10778. (interactive)
  10779. (save-window-excursion
  10780. (let* ((pos (point))
  10781. (line (org-current-line))
  10782. (params (org-prepare-dblock))
  10783. (name (plist-get params :name))
  10784. (indent (plist-get params :indentation-column))
  10785. (cmd (intern (concat "org-dblock-write:" name))))
  10786. (message "Updating dynamic block `%s' at line %d..." name line)
  10787. (funcall cmd params)
  10788. (message "Updating dynamic block `%s' at line %d...done" name line)
  10789. (goto-char pos)
  10790. (when (and indent (> indent 0))
  10791. (setq indent (make-string indent ?\ ))
  10792. (save-excursion
  10793. (org-beginning-of-dblock)
  10794. (forward-line 1)
  10795. (while (not (looking-at org-dblock-end-re))
  10796. (insert indent)
  10797. (beginning-of-line 2))
  10798. (when (looking-at org-dblock-end-re)
  10799. (and (looking-at "[ \t]+")
  10800. (replace-match ""))
  10801. (insert indent)))))))
  10802. (defun org-beginning-of-dblock ()
  10803. "Find the beginning of the dynamic block at point.
  10804. Error if there is no such block at point."
  10805. (let ((pos (point))
  10806. beg)
  10807. (end-of-line 1)
  10808. (if (and (re-search-backward org-dblock-start-re nil t)
  10809. (setq beg (match-beginning 0))
  10810. (re-search-forward org-dblock-end-re nil t)
  10811. (> (match-end 0) pos))
  10812. (goto-char beg)
  10813. (goto-char pos)
  10814. (error "Not in a dynamic block"))))
  10815. (defun org-update-all-dblocks ()
  10816. "Update all dynamic blocks in the buffer.
  10817. This function can be used in a hook."
  10818. (interactive)
  10819. (when (derived-mode-p 'org-mode)
  10820. (org-map-dblocks 'org-update-dblock)))
  10821. ;;;; Completion
  10822. (defun org-get-export-keywords ()
  10823. "Return a list of all currently understood export keywords.
  10824. Export keywords include options, block names, attributes and
  10825. keywords relative to each registered export back-end."
  10826. (delq nil
  10827. (let (keywords)
  10828. (mapc
  10829. (lambda (back-end)
  10830. (let ((props (cdr back-end)))
  10831. ;; Back-end name (for keywords, like #+LATEX:)
  10832. (push (upcase (symbol-name (car back-end))) keywords)
  10833. ;; Back-end options.
  10834. (mapc (lambda (option) (push (cadr option) keywords))
  10835. (plist-get (cdr back-end) :options-alist))))
  10836. (org-bound-and-true-p org-export-registered-backends))
  10837. keywords)))
  10838. (defconst org-options-keywords
  10839. '("ARCHIVE:" "AUTHOR:" "BIND:" "CATEGORY:" "COLUMNS:" "CREATOR:" "DATE:"
  10840. "DESCRIPTION:" "DRAWERS:" "EMAIL:" "EXCLUDE_TAGS:" "FILETAGS:" "INCLUDE:"
  10841. "INDEX:" "KEYWORDS:" "LANGUAGE:" "MACRO:" "OPTIONS:" "PROPERTY:"
  10842. "PRIORITIES:" "SELECT_TAGS:" "SEQ_TODO:" "SETUPFILE:" "STARTUP:" "TAGS:"
  10843. "TITLE:" "TODO:" "TYP_TODO:" "SELECT_TAGS:" "EXCLUDE_TAGS:"))
  10844. (defcustom org-structure-template-alist
  10845. '(("s" "#+BEGIN_SRC ?\n\n#+END_SRC"
  10846. "<src lang=\"?\">\n\n</src>")
  10847. ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE"
  10848. "<example>\n?\n</example>")
  10849. ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE"
  10850. "<quote>\n?\n</quote>")
  10851. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE"
  10852. "<verse>\n?\n</verse>")
  10853. ("V" "#+BEGIN_VERBATIM\n?\n#+END_VERBATIM"
  10854. "<verbatim>\n?\n</verbatim>")
  10855. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER"
  10856. "<center>\n?\n</center>")
  10857. ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX"
  10858. "<literal style=\"latex\">\n?\n</literal>")
  10859. ("L" "#+LaTeX: "
  10860. "<literal style=\"latex\">?</literal>")
  10861. ("h" "#+BEGIN_HTML\n?\n#+END_HTML"
  10862. "<literal style=\"html\">\n?\n</literal>")
  10863. ("H" "#+HTML: "
  10864. "<literal style=\"html\">?</literal>")
  10865. ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII")
  10866. ("A" "#+ASCII: ")
  10867. ("i" "#+INDEX: ?"
  10868. "#+INDEX: ?")
  10869. ("I" "#+INCLUDE: %file ?"
  10870. "<include file=%file markup=\"?\">"))
  10871. "Structure completion elements.
  10872. This is a list of abbreviation keys and values. The value gets inserted
  10873. if you type `<' followed by the key and then press the completion key,
  10874. usually `M-TAB'. %file will be replaced by a file name after prompting
  10875. for the file using completion. The cursor will be placed at the position
  10876. of the `?` in the template.
  10877. There are two templates for each key, the first uses the original Org syntax,
  10878. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  10879. the default when the /org-mtags.el/ module has been loaded. See also the
  10880. variable `org-mtags-prefer-muse-templates'."
  10881. :group 'org-completion
  10882. :type '(repeat
  10883. (string :tag "Key")
  10884. (string :tag "Template")
  10885. (string :tag "Muse Template")))
  10886. (defun org-try-structure-completion ()
  10887. "Try to complete a structure template before point.
  10888. This looks for strings like \"<e\" on an otherwise empty line and
  10889. expands them."
  10890. (let ((l (buffer-substring (point-at-bol) (point)))
  10891. a)
  10892. (when (and (looking-at "[ \t]*$")
  10893. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  10894. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  10895. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  10896. (match-beginning 1)) a)
  10897. t)))
  10898. (defun org-complete-expand-structure-template (start cell)
  10899. "Expand a structure template."
  10900. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  10901. (rpl (nth (if musep 2 1) cell))
  10902. (ind ""))
  10903. (delete-region start (point))
  10904. (when (string-match "\\`#\\+" rpl)
  10905. (cond
  10906. ((bolp))
  10907. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  10908. (setq ind (buffer-substring (point-at-bol) (point))))
  10909. (t (newline))))
  10910. (setq start (point))
  10911. (if (string-match "%file" rpl)
  10912. (setq rpl (replace-match
  10913. (concat
  10914. "\""
  10915. (save-match-data
  10916. (abbreviate-file-name (read-file-name "Include file: ")))
  10917. "\"")
  10918. t t rpl)))
  10919. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  10920. (concat "\n" ind)))
  10921. (insert rpl)
  10922. (if (re-search-backward "\\?" start t) (delete-char 1))))
  10923. ;;;; TODO, DEADLINE, Comments
  10924. (defun org-toggle-comment ()
  10925. "Change the COMMENT state of an entry."
  10926. (interactive)
  10927. (save-excursion
  10928. (org-back-to-heading)
  10929. (let (case-fold-search)
  10930. (cond
  10931. ((looking-at (format org-heading-keyword-regexp-format
  10932. org-comment-string))
  10933. (goto-char (match-end 1))
  10934. (looking-at (concat " +" org-comment-string))
  10935. (replace-match "" t t)
  10936. (when (eolp) (insert " ")))
  10937. ((looking-at org-outline-regexp)
  10938. (goto-char (match-end 0))
  10939. (insert org-comment-string " "))))))
  10940. (defvar org-last-todo-state-is-todo nil
  10941. "This is non-nil when the last TODO state change led to a TODO state.
  10942. If the last change removed the TODO tag or switched to DONE, then
  10943. this is nil.")
  10944. (defvar org-setting-tags nil) ; dynamically skipped
  10945. (defvar org-todo-setup-filter-hook nil
  10946. "Hook for functions that pre-filter todo specs.
  10947. Each function takes a todo spec and returns either nil or the spec
  10948. transformed into canonical form." )
  10949. (defvar org-todo-get-default-hook nil
  10950. "Hook for functions that get a default item for todo.
  10951. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  10952. nil or a string to be used for the todo mark." )
  10953. (defvar org-agenda-headline-snapshot-before-repeat)
  10954. (defun org-current-effective-time ()
  10955. "Return current time adjusted for `org-extend-today-until' variable."
  10956. (let* ((ct (org-current-time))
  10957. (dct (decode-time ct))
  10958. (ct1
  10959. (cond
  10960. (org-use-last-clock-out-time-as-effective-time
  10961. (or (org-clock-get-last-clock-out-time) ct))
  10962. ((and org-use-effective-time (< (nth 2 dct) org-extend-today-until))
  10963. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct)))
  10964. (t ct))))
  10965. ct1))
  10966. (defun org-todo-yesterday (&optional arg)
  10967. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  10968. (interactive "P")
  10969. (if (eq major-mode 'org-agenda-mode)
  10970. (apply 'org-agenda-todo-yesterday arg)
  10971. (let* ((hour (third (decode-time
  10972. (org-current-time))))
  10973. (org-extend-today-until (1+ hour)))
  10974. (org-todo arg))))
  10975. (defvar org-block-entry-blocking ""
  10976. "First entry preventing the TODO state change.")
  10977. (defun org-todo (&optional arg)
  10978. "Change the TODO state of an item.
  10979. The state of an item is given by a keyword at the start of the heading,
  10980. like
  10981. *** TODO Write paper
  10982. *** DONE Call mom
  10983. The different keywords are specified in the variable `org-todo-keywords'.
  10984. By default the available states are \"TODO\" and \"DONE\".
  10985. So for this example: when the item starts with TODO, it is changed to DONE.
  10986. When it starts with DONE, the DONE is removed. And when neither TODO nor
  10987. DONE are present, add TODO at the beginning of the heading.
  10988. With \\[universal-argument] prefix arg, use completion to determine the new \
  10989. state.
  10990. With numeric prefix arg, switch to that state.
  10991. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  10992. keywords (nextset).
  10993. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  10994. With a numeric prefix arg of 0, inhibit note taking for the change.
  10995. For calling through lisp, arg is also interpreted in the following way:
  10996. 'none -> empty state
  10997. \"\"(empty string) -> switch to empty state
  10998. 'done -> switch to DONE
  10999. 'nextset -> switch to the next set of keywords
  11000. 'previousset -> switch to the previous set of keywords
  11001. \"WAITING\" -> switch to the specified keyword, but only if it
  11002. really is a member of `org-todo-keywords'."
  11003. (interactive "P")
  11004. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11005. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11006. 'region-start-level 'region))
  11007. org-loop-over-headlines-in-active-region)
  11008. (org-map-entries
  11009. `(org-todo ,arg)
  11010. org-loop-over-headlines-in-active-region
  11011. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11012. (if (equal arg '(16)) (setq arg 'nextset))
  11013. (let ((org-blocker-hook org-blocker-hook)
  11014. commentp
  11015. case-fold-search)
  11016. (when (equal arg '(64))
  11017. (setq arg nil org-blocker-hook nil))
  11018. (when (and org-blocker-hook
  11019. (or org-inhibit-blocking
  11020. (org-entry-get nil "NOBLOCKING")))
  11021. (setq org-blocker-hook nil))
  11022. (save-excursion
  11023. (catch 'exit
  11024. (org-back-to-heading t)
  11025. (when (looking-at (concat "^\\*+ " org-comment-string))
  11026. (org-toggle-comment)
  11027. (setq commentp t))
  11028. (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  11029. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  11030. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  11031. (let* ((match-data (match-data))
  11032. (startpos (point-at-bol))
  11033. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  11034. (org-log-done org-log-done)
  11035. (org-log-repeat org-log-repeat)
  11036. (org-todo-log-states org-todo-log-states)
  11037. (org-inhibit-logging
  11038. (if (equal arg 0)
  11039. (progn (setq arg nil) 'note) org-inhibit-logging))
  11040. (this (match-string 1))
  11041. (hl-pos (match-beginning 0))
  11042. (head (org-get-todo-sequence-head this))
  11043. (ass (assoc head org-todo-kwd-alist))
  11044. (interpret (nth 1 ass))
  11045. (done-word (nth 3 ass))
  11046. (final-done-word (nth 4 ass))
  11047. (org-last-state (or this ""))
  11048. (completion-ignore-case t)
  11049. (member (member this org-todo-keywords-1))
  11050. (tail (cdr member))
  11051. (org-state (cond
  11052. ((and org-todo-key-trigger
  11053. (or (and (equal arg '(4))
  11054. (eq org-use-fast-todo-selection 'prefix))
  11055. (and (not arg) org-use-fast-todo-selection
  11056. (not (eq org-use-fast-todo-selection
  11057. 'prefix)))))
  11058. ;; Use fast selection
  11059. (org-fast-todo-selection))
  11060. ((and (equal arg '(4))
  11061. (or (not org-use-fast-todo-selection)
  11062. (not org-todo-key-trigger)))
  11063. ;; Read a state with completion
  11064. (org-icompleting-read
  11065. "State: " (mapcar 'list org-todo-keywords-1)
  11066. nil t))
  11067. ((eq arg 'right)
  11068. (if this
  11069. (if tail (car tail) nil)
  11070. (car org-todo-keywords-1)))
  11071. ((eq arg 'left)
  11072. (if (equal member org-todo-keywords-1)
  11073. nil
  11074. (if this
  11075. (nth (- (length org-todo-keywords-1)
  11076. (length tail) 2)
  11077. org-todo-keywords-1)
  11078. (org-last org-todo-keywords-1))))
  11079. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  11080. (setq arg nil))) ; hack to fall back to cycling
  11081. (arg
  11082. ;; user or caller requests a specific state
  11083. (cond
  11084. ((equal arg "") nil)
  11085. ((eq arg 'none) nil)
  11086. ((eq arg 'done) (or done-word (car org-done-keywords)))
  11087. ((eq arg 'nextset)
  11088. (or (car (cdr (member head org-todo-heads)))
  11089. (car org-todo-heads)))
  11090. ((eq arg 'previousset)
  11091. (let ((org-todo-heads (reverse org-todo-heads)))
  11092. (or (car (cdr (member head org-todo-heads)))
  11093. (car org-todo-heads))))
  11094. ((car (member arg org-todo-keywords-1)))
  11095. ((stringp arg)
  11096. (user-error "State `%s' not valid in this file" arg))
  11097. ((nth (1- (prefix-numeric-value arg))
  11098. org-todo-keywords-1))))
  11099. ((null member) (or head (car org-todo-keywords-1)))
  11100. ((equal this final-done-word) nil) ;; -> make empty
  11101. ((null tail) nil) ;; -> first entry
  11102. ((memq interpret '(type priority))
  11103. (if (eq this-command last-command)
  11104. (car tail)
  11105. (if (> (length tail) 0)
  11106. (or done-word (car org-done-keywords))
  11107. nil)))
  11108. (t
  11109. (car tail))))
  11110. (org-state (or
  11111. (run-hook-with-args-until-success
  11112. 'org-todo-get-default-hook org-state org-last-state)
  11113. org-state))
  11114. (next (if org-state (concat " " org-state " ") " "))
  11115. (change-plist (list :type 'todo-state-change :from this :to org-state
  11116. :position startpos))
  11117. dolog now-done-p)
  11118. (when org-blocker-hook
  11119. (setq org-last-todo-state-is-todo
  11120. (not (member this org-done-keywords)))
  11121. (unless (save-excursion
  11122. (save-match-data
  11123. (org-with-wide-buffer
  11124. (run-hook-with-args-until-failure
  11125. 'org-blocker-hook change-plist))))
  11126. (if (org-called-interactively-p 'interactive)
  11127. (user-error "TODO state change from %s to %s blocked (by \"%s\")"
  11128. this org-state org-block-entry-blocking)
  11129. ;; fail silently
  11130. (message "TODO state change from %s to %s blocked (by \"%s\")"
  11131. this org-state org-block-entry-blocking)
  11132. (throw 'exit nil))))
  11133. (store-match-data match-data)
  11134. (replace-match next t t)
  11135. (unless (pos-visible-in-window-p hl-pos)
  11136. (message "TODO state changed to %s" (org-trim next)))
  11137. (unless head
  11138. (setq head (org-get-todo-sequence-head org-state)
  11139. ass (assoc head org-todo-kwd-alist)
  11140. interpret (nth 1 ass)
  11141. done-word (nth 3 ass)
  11142. final-done-word (nth 4 ass)))
  11143. (when (memq arg '(nextset previousset))
  11144. (message "Keyword-Set %d/%d: %s"
  11145. (- (length org-todo-sets) -1
  11146. (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
  11147. (length org-todo-sets)
  11148. (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
  11149. (setq org-last-todo-state-is-todo
  11150. (not (member org-state org-done-keywords)))
  11151. (setq now-done-p (and (member org-state org-done-keywords)
  11152. (not (member this org-done-keywords))))
  11153. (and logging (org-local-logging logging))
  11154. (when (and (or org-todo-log-states org-log-done)
  11155. (not (eq org-inhibit-logging t))
  11156. (not (memq arg '(nextset previousset))))
  11157. ;; we need to look at recording a time and note
  11158. (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
  11159. (nth 2 (assoc this org-todo-log-states))))
  11160. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  11161. (setq dolog 'time))
  11162. (when (or (and (not org-state) (not org-closed-keep-when-no-todo))
  11163. (and org-state
  11164. (member org-state org-not-done-keywords)
  11165. (not (member this org-not-done-keywords))))
  11166. ;; This is now a todo state and was not one before
  11167. ;; If there was a CLOSED time stamp, get rid of it.
  11168. (org-add-planning-info nil nil 'closed))
  11169. (when (and now-done-p org-log-done)
  11170. ;; It is now done, and it was not done before
  11171. (org-add-planning-info 'closed (org-current-effective-time))
  11172. (if (and (not dolog) (eq 'note org-log-done))
  11173. (org-add-log-setup 'done org-state this 'findpos 'note)))
  11174. (when (and org-state dolog)
  11175. ;; This is a non-nil state, and we need to log it
  11176. (org-add-log-setup 'state org-state this 'findpos dolog)))
  11177. ;; Fixup tag positioning
  11178. (org-todo-trigger-tag-changes org-state)
  11179. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  11180. (when org-provide-todo-statistics
  11181. (org-update-parent-todo-statistics))
  11182. (run-hooks 'org-after-todo-state-change-hook)
  11183. (if (and arg (not (member org-state org-done-keywords)))
  11184. (setq head (org-get-todo-sequence-head org-state)))
  11185. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  11186. ;; Do we need to trigger a repeat?
  11187. (when now-done-p
  11188. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  11189. ;; This is for the agenda, take a snapshot of the headline.
  11190. (save-match-data
  11191. (setq org-agenda-headline-snapshot-before-repeat
  11192. (org-get-heading))))
  11193. (org-auto-repeat-maybe org-state))
  11194. ;; Fixup cursor location if close to the keyword
  11195. (if (and (outline-on-heading-p)
  11196. (not (bolp))
  11197. (save-excursion (beginning-of-line 1)
  11198. (looking-at org-todo-line-regexp))
  11199. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  11200. (progn
  11201. (goto-char (or (match-end 2) (match-end 1)))
  11202. (and (looking-at " ") (just-one-space))))
  11203. (when org-trigger-hook
  11204. (save-excursion
  11205. (run-hook-with-args 'org-trigger-hook change-plist)))
  11206. (when commentp (org-toggle-comment))))))))
  11207. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  11208. "Block turning an entry into a TODO, using the hierarchy.
  11209. This checks whether the current task should be blocked from state
  11210. changes. Such blocking occurs when:
  11211. 1. The task has children which are not all in a completed state.
  11212. 2. A task has a parent with the property :ORDERED:, and there
  11213. are siblings prior to the current task with incomplete
  11214. status.
  11215. 3. The parent of the task is blocked because it has siblings that should
  11216. be done first, or is child of a block grandparent TODO entry."
  11217. (if (not org-enforce-todo-dependencies)
  11218. t ; if locally turned off don't block
  11219. (catch 'dont-block
  11220. ;; If this is not a todo state change, or if this entry is already DONE,
  11221. ;; do not block
  11222. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11223. (member (plist-get change-plist :from)
  11224. (cons 'done org-done-keywords))
  11225. (member (plist-get change-plist :to)
  11226. (cons 'todo org-not-done-keywords))
  11227. (not (plist-get change-plist :to)))
  11228. (throw 'dont-block t))
  11229. ;; If this task has children, and any are undone, it's blocked
  11230. (save-excursion
  11231. (org-back-to-heading t)
  11232. (let ((this-level (funcall outline-level)))
  11233. (outline-next-heading)
  11234. (let ((child-level (funcall outline-level)))
  11235. (while (and (not (eobp))
  11236. (> child-level this-level))
  11237. ;; this todo has children, check whether they are all
  11238. ;; completed
  11239. (if (and (not (org-entry-is-done-p))
  11240. (org-entry-is-todo-p))
  11241. (progn (setq org-block-entry-blocking (org-get-heading))
  11242. (throw 'dont-block nil)))
  11243. (outline-next-heading)
  11244. (setq child-level (funcall outline-level))))))
  11245. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  11246. ;; any previous siblings are undone, it's blocked
  11247. (save-excursion
  11248. (org-back-to-heading t)
  11249. (let* ((pos (point))
  11250. (parent-pos (and (org-up-heading-safe) (point))))
  11251. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11252. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11253. (forward-line 1)
  11254. (re-search-forward org-not-done-heading-regexp pos t))
  11255. (setq org-block-entry-blocking (match-string 0))
  11256. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  11257. ;; Search further up the hierarchy, to see if an ancestor is blocked
  11258. (while t
  11259. (goto-char parent-pos)
  11260. (if (not (looking-at org-not-done-heading-regexp))
  11261. (throw 'dont-block t)) ; do not block, parent is not a TODO
  11262. (setq pos (point))
  11263. (setq parent-pos (and (org-up-heading-safe) (point)))
  11264. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11265. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11266. (forward-line 1)
  11267. (re-search-forward org-not-done-heading-regexp pos t)
  11268. (setq org-block-entry-blocking (org-get-heading)))
  11269. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  11270. (defcustom org-track-ordered-property-with-tag nil
  11271. "Should the ORDERED property also be shown as a tag?
  11272. The ORDERED property decides if an entry should require subtasks to be
  11273. completed in sequence. Since a property is not very visible, setting
  11274. this option means that toggling the ORDERED property with the command
  11275. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  11276. not relevant for the behavior, but it makes things more visible.
  11277. Note that toggling the tag with tags commands will not change the property
  11278. and therefore not influence behavior!
  11279. This can be t, meaning the tag ORDERED should be used, It can also be a
  11280. string to select a different tag for this task."
  11281. :group 'org-todo
  11282. :type '(choice
  11283. (const :tag "No tracking" nil)
  11284. (const :tag "Track with ORDERED tag" t)
  11285. (string :tag "Use other tag")))
  11286. (defun org-toggle-ordered-property ()
  11287. "Toggle the ORDERED property of the current entry.
  11288. For better visibility, you can track the value of this property with a tag.
  11289. See variable `org-track-ordered-property-with-tag'."
  11290. (interactive)
  11291. (let* ((t1 org-track-ordered-property-with-tag)
  11292. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  11293. (save-excursion
  11294. (org-back-to-heading)
  11295. (if (org-entry-get nil "ORDERED")
  11296. (progn
  11297. (org-delete-property "ORDERED" "PROPERTIES")
  11298. (and tag (org-toggle-tag tag 'off))
  11299. (message "Subtasks can be completed in arbitrary order"))
  11300. (org-entry-put nil "ORDERED" "t")
  11301. (and tag (org-toggle-tag tag 'on))
  11302. (message "Subtasks must be completed in sequence")))))
  11303. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  11304. (defun org-block-todo-from-checkboxes (change-plist)
  11305. "Block turning an entry into a TODO, using checkboxes.
  11306. This checks whether the current task should be blocked from state
  11307. changes because there are unchecked boxes in this entry."
  11308. (if (not org-enforce-todo-checkbox-dependencies)
  11309. t ; if locally turned off don't block
  11310. (catch 'dont-block
  11311. ;; If this is not a todo state change, or if this entry is already DONE,
  11312. ;; do not block
  11313. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11314. (member (plist-get change-plist :from)
  11315. (cons 'done org-done-keywords))
  11316. (member (plist-get change-plist :to)
  11317. (cons 'todo org-not-done-keywords))
  11318. (not (plist-get change-plist :to)))
  11319. (throw 'dont-block t))
  11320. ;; If this task has checkboxes that are not checked, it's blocked
  11321. (save-excursion
  11322. (org-back-to-heading t)
  11323. (let ((beg (point)) end)
  11324. (outline-next-heading)
  11325. (setq end (point))
  11326. (goto-char beg)
  11327. (if (org-list-search-forward
  11328. (concat (org-item-beginning-re)
  11329. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  11330. "\\[[- ]\\]")
  11331. end t)
  11332. (progn
  11333. (if (boundp 'org-blocked-by-checkboxes)
  11334. (setq org-blocked-by-checkboxes t))
  11335. (throw 'dont-block nil)))))
  11336. t))) ; do not block
  11337. (defun org-entry-blocked-p ()
  11338. "Is the current entry blocked?"
  11339. (org-with-silent-modifications
  11340. (if (org-entry-get nil "NOBLOCKING")
  11341. nil ;; Never block this entry
  11342. (not (run-hook-with-args-until-failure
  11343. 'org-blocker-hook
  11344. (list :type 'todo-state-change
  11345. :position (point)
  11346. :from 'todo
  11347. :to 'done))))))
  11348. (defun org-update-statistics-cookies (all)
  11349. "Update the statistics cookie, either from TODO or from checkboxes.
  11350. This should be called with the cursor in a line with a statistics cookie."
  11351. (interactive "P")
  11352. (if all
  11353. (progn
  11354. (org-update-checkbox-count 'all)
  11355. (org-map-entries 'org-update-parent-todo-statistics))
  11356. (if (not (org-at-heading-p))
  11357. (org-update-checkbox-count)
  11358. (let ((pos (point-marker))
  11359. end l1 l2)
  11360. (ignore-errors (org-back-to-heading t))
  11361. (if (not (org-at-heading-p))
  11362. (org-update-checkbox-count)
  11363. (setq l1 (org-outline-level))
  11364. (setq end (save-excursion
  11365. (outline-next-heading)
  11366. (if (org-at-heading-p) (setq l2 (org-outline-level)))
  11367. (point)))
  11368. (if (and (save-excursion
  11369. (re-search-forward
  11370. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  11371. (not (save-excursion (re-search-forward
  11372. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  11373. (org-update-checkbox-count)
  11374. (if (and l2 (> l2 l1))
  11375. (progn
  11376. (goto-char end)
  11377. (org-update-parent-todo-statistics))
  11378. (goto-char pos)
  11379. (beginning-of-line 1)
  11380. (while (re-search-forward
  11381. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  11382. (point-at-eol) t)
  11383. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  11384. (goto-char pos)
  11385. (move-marker pos nil)))))
  11386. (defvar org-entry-property-inherited-from) ;; defined below
  11387. (defun org-update-parent-todo-statistics ()
  11388. "Update any statistics cookie in the parent of the current headline.
  11389. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  11390. the entire subtree and this will travel up the hierarchy and update
  11391. statistics everywhere."
  11392. (let* ((prop (save-excursion (org-up-heading-safe)
  11393. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  11394. (recursive (or (not org-hierarchical-todo-statistics)
  11395. (and prop (string-match "\\<recursive\\>" prop))))
  11396. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  11397. 0))
  11398. (first t)
  11399. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  11400. level ltoggle l1 new ndel
  11401. (cnt-all 0) (cnt-done 0) is-percent kwd
  11402. checkbox-beg ov ovs ove cookie-present)
  11403. (catch 'exit
  11404. (save-excursion
  11405. (beginning-of-line 1)
  11406. (setq ltoggle (funcall outline-level))
  11407. ;; Three situations are to consider:
  11408. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  11409. ;; to the top-level ancestor on the headline;
  11410. ;; 2. If parent has "recursive" property, repeat up to the
  11411. ;; headline setting that property, taking inheritance into
  11412. ;; account;
  11413. ;; 3. Else, move up to direct parent and proceed only once.
  11414. (while (and (setq level (org-up-heading-safe))
  11415. (or recursive first)
  11416. (>= (point) lim))
  11417. (setq first nil cookie-present nil)
  11418. (unless (and level
  11419. (not (string-match
  11420. "\\<checkbox\\>"
  11421. (downcase (or (org-entry-get nil "COOKIE_DATA")
  11422. "")))))
  11423. (throw 'exit nil))
  11424. (while (re-search-forward box-re (point-at-eol) t)
  11425. (setq cnt-all 0 cnt-done 0 cookie-present t)
  11426. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  11427. (save-match-data
  11428. (unless (outline-next-heading) (throw 'exit nil))
  11429. (while (and (looking-at org-complex-heading-regexp)
  11430. (> (setq l1 (length (match-string 1))) level))
  11431. (setq kwd (and (or recursive (= l1 ltoggle))
  11432. (match-string 2)))
  11433. (if (or (eq org-provide-todo-statistics 'all-headlines)
  11434. (and (listp org-provide-todo-statistics)
  11435. (or (member kwd org-provide-todo-statistics)
  11436. (member kwd org-done-keywords))))
  11437. (setq cnt-all (1+ cnt-all))
  11438. (if (eq org-provide-todo-statistics t)
  11439. (and kwd (setq cnt-all (1+ cnt-all)))))
  11440. (and (member kwd org-done-keywords)
  11441. (setq cnt-done (1+ cnt-done)))
  11442. (outline-next-heading)))
  11443. (setq new
  11444. (if is-percent
  11445. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  11446. (format "[%d/%d]" cnt-done cnt-all))
  11447. ndel (- (match-end 0) checkbox-beg))
  11448. ;; handle overlays when updating cookie from column view
  11449. (when (setq ov (car (overlays-at checkbox-beg)))
  11450. (setq ovs (overlay-start ov) ove (overlay-end ov))
  11451. (delete-overlay ov))
  11452. (goto-char checkbox-beg)
  11453. (insert new)
  11454. (delete-region (point) (+ (point) ndel))
  11455. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  11456. (when ov (move-overlay ov ovs ove)))
  11457. (when cookie-present
  11458. (run-hook-with-args 'org-after-todo-statistics-hook
  11459. cnt-done (- cnt-all cnt-done))))))
  11460. (run-hooks 'org-todo-statistics-hook)))
  11461. (defvar org-after-todo-statistics-hook nil
  11462. "Hook that is called after a TODO statistics cookie has been updated.
  11463. Each function is called with two arguments: the number of not-done entries
  11464. and the number of done entries.
  11465. For example, the following function, when added to this hook, will switch
  11466. an entry to DONE when all children are done, and back to TODO when new
  11467. entries are set to a TODO status. Note that this hook is only called
  11468. when there is a statistics cookie in the headline!
  11469. (defun org-summary-todo (n-done n-not-done)
  11470. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  11471. (let (org-log-done org-log-states) ; turn off logging
  11472. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  11473. ")
  11474. (defvar org-todo-statistics-hook nil
  11475. "Hook that is run whenever Org thinks TODO statistics should be updated.
  11476. This hook runs even if there is no statistics cookie present, in which case
  11477. `org-after-todo-statistics-hook' would not run.")
  11478. (defun org-todo-trigger-tag-changes (state)
  11479. "Apply the changes defined in `org-todo-state-tags-triggers'."
  11480. (let ((l org-todo-state-tags-triggers)
  11481. changes)
  11482. (when (or (not state) (equal state ""))
  11483. (setq changes (append changes (cdr (assoc "" l)))))
  11484. (when (and (stringp state) (> (length state) 0))
  11485. (setq changes (append changes (cdr (assoc state l)))))
  11486. (when (member state org-not-done-keywords)
  11487. (setq changes (append changes (cdr (assoc 'todo l)))))
  11488. (when (member state org-done-keywords)
  11489. (setq changes (append changes (cdr (assoc 'done l)))))
  11490. (dolist (c changes)
  11491. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  11492. (defun org-local-logging (value)
  11493. "Get logging settings from a property VALUE."
  11494. (let* (words w a)
  11495. ;; directly set the variables, they are already local.
  11496. (setq org-log-done nil
  11497. org-log-repeat nil
  11498. org-todo-log-states nil)
  11499. (setq words (org-split-string value))
  11500. (while (setq w (pop words))
  11501. (cond
  11502. ((setq a (assoc w org-startup-options))
  11503. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  11504. (set (nth 1 a) (nth 2 a))))
  11505. ((setq a (org-extract-log-state-settings w))
  11506. (and (member (car a) org-todo-keywords-1)
  11507. (push a org-todo-log-states)))))))
  11508. (defun org-get-todo-sequence-head (kwd)
  11509. "Return the head of the TODO sequence to which KWD belongs.
  11510. If KWD is not set, check if there is a text property remembering the
  11511. right sequence."
  11512. (let (p)
  11513. (cond
  11514. ((not kwd)
  11515. (or (get-text-property (point-at-bol) 'org-todo-head)
  11516. (progn
  11517. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  11518. nil (point-at-eol)))
  11519. (get-text-property p 'org-todo-head))))
  11520. ((not (member kwd org-todo-keywords-1))
  11521. (car org-todo-keywords-1))
  11522. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  11523. (defun org-fast-todo-selection ()
  11524. "Fast TODO keyword selection with single keys.
  11525. Returns the new TODO keyword, or nil if no state change should occur."
  11526. (let* ((fulltable org-todo-key-alist)
  11527. (done-keywords org-done-keywords) ;; needed for the faces.
  11528. (maxlen (apply 'max (mapcar
  11529. (lambda (x)
  11530. (if (stringp (car x)) (string-width (car x)) 0))
  11531. fulltable)))
  11532. (expert nil)
  11533. (fwidth (+ maxlen 3 1 3))
  11534. (ncol (/ (- (window-width) 4) fwidth))
  11535. tg cnt e c tbl
  11536. groups ingroup)
  11537. (save-excursion
  11538. (save-window-excursion
  11539. (if expert
  11540. (set-buffer (get-buffer-create " *Org todo*"))
  11541. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  11542. (erase-buffer)
  11543. (org-set-local 'org-done-keywords done-keywords)
  11544. (setq tbl fulltable cnt 0)
  11545. (while (setq e (pop tbl))
  11546. (cond
  11547. ((equal e '(:startgroup))
  11548. (push '() groups) (setq ingroup t)
  11549. (when (not (= cnt 0))
  11550. (setq cnt 0)
  11551. (insert "\n"))
  11552. (insert "{ "))
  11553. ((equal e '(:endgroup))
  11554. (setq ingroup nil cnt 0)
  11555. (insert "}\n"))
  11556. ((equal e '(:newline))
  11557. (when (not (= cnt 0))
  11558. (setq cnt 0)
  11559. (insert "\n")
  11560. (setq e (car tbl))
  11561. (while (equal (car tbl) '(:newline))
  11562. (insert "\n")
  11563. (setq tbl (cdr tbl)))))
  11564. (t
  11565. (setq tg (car e) c (cdr e))
  11566. (if ingroup (push tg (car groups)))
  11567. (setq tg (org-add-props tg nil 'face
  11568. (org-get-todo-face tg)))
  11569. (if (and (= cnt 0) (not ingroup)) (insert " "))
  11570. (insert "[" c "] " tg (make-string
  11571. (- fwidth 4 (length tg)) ?\ ))
  11572. (when (= (setq cnt (1+ cnt)) ncol)
  11573. (insert "\n")
  11574. (if ingroup (insert " "))
  11575. (setq cnt 0)))))
  11576. (insert "\n")
  11577. (goto-char (point-min))
  11578. (if (not expert) (org-fit-window-to-buffer))
  11579. (message "[a-z..]:Set [SPC]:clear")
  11580. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11581. (cond
  11582. ((or (= c ?\C-g)
  11583. (and (= c ?q) (not (rassoc c fulltable))))
  11584. (setq quit-flag t))
  11585. ((= c ?\ ) nil)
  11586. ((setq e (rassoc c fulltable) tg (car e))
  11587. tg)
  11588. (t (setq quit-flag t)))))))
  11589. (defun org-entry-is-todo-p ()
  11590. (member (org-get-todo-state) org-not-done-keywords))
  11591. (defun org-entry-is-done-p ()
  11592. (member (org-get-todo-state) org-done-keywords))
  11593. (defun org-get-todo-state ()
  11594. "Return the TODO keyword of the current subtree."
  11595. (save-excursion
  11596. (org-back-to-heading t)
  11597. (and (looking-at org-todo-line-regexp)
  11598. (match-end 2)
  11599. (match-string 2))))
  11600. (defun org-at-date-range-p (&optional inactive-ok)
  11601. "Is the cursor inside a date range?"
  11602. (interactive)
  11603. (save-excursion
  11604. (catch 'exit
  11605. (let ((pos (point)))
  11606. (skip-chars-backward "^[<\r\n")
  11607. (skip-chars-backward "<[")
  11608. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11609. (>= (match-end 0) pos)
  11610. (throw 'exit t))
  11611. (skip-chars-backward "^<[\r\n")
  11612. (skip-chars-backward "<[")
  11613. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11614. (>= (match-end 0) pos)
  11615. (throw 'exit t)))
  11616. nil)))
  11617. (defun org-get-repeat (&optional tagline)
  11618. "Check if there is a deadline/schedule with repeater in this entry."
  11619. (save-match-data
  11620. (save-excursion
  11621. (org-back-to-heading t)
  11622. (and (re-search-forward (if tagline
  11623. (concat tagline "\\s-*" org-repeat-re)
  11624. org-repeat-re)
  11625. (org-entry-end-position) t)
  11626. (match-string-no-properties 1)))))
  11627. (defvar org-last-changed-timestamp)
  11628. (defvar org-last-inserted-timestamp)
  11629. (defvar org-log-post-message)
  11630. (defvar org-log-note-purpose)
  11631. (defvar org-log-note-how)
  11632. (defvar org-log-note-extra)
  11633. (defun org-auto-repeat-maybe (done-word)
  11634. "Check if the current headline contains a repeated deadline/schedule.
  11635. If yes, set TODO state back to what it was and change the base date
  11636. of repeating deadline/scheduled time stamps to new date.
  11637. This function is run automatically after each state change to a DONE state."
  11638. ;; last-state is dynamically scoped into this function
  11639. (let* ((repeat (org-get-repeat))
  11640. (aa (assoc org-last-state org-todo-kwd-alist))
  11641. (interpret (nth 1 aa))
  11642. (head (nth 2 aa))
  11643. (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
  11644. (msg "Entry repeats: ")
  11645. (org-log-done nil)
  11646. (org-todo-log-states nil)
  11647. re type n what ts time to-state)
  11648. (when repeat
  11649. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  11650. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  11651. org-todo-repeat-to-state))
  11652. (unless (and to-state (member to-state org-todo-keywords-1))
  11653. (setq to-state (if (eq interpret 'type) org-last-state head)))
  11654. (org-todo to-state)
  11655. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  11656. (org-entry-put nil "LAST_REPEAT" (format-time-string
  11657. (org-time-stamp-format t t))))
  11658. (when org-log-repeat
  11659. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  11660. (memq 'org-add-log-note post-command-hook))
  11661. ;; OK, we are already setup for some record
  11662. (if (eq org-log-repeat 'note)
  11663. ;; make sure we take a note, not only a time stamp
  11664. (setq org-log-note-how 'note))
  11665. ;; Set up for taking a record
  11666. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  11667. org-last-state
  11668. 'findpos org-log-repeat)))
  11669. (org-back-to-heading t)
  11670. (org-add-planning-info nil nil 'closed)
  11671. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11672. org-deadline-time-regexp "\\)\\|\\("
  11673. org-ts-regexp "\\)"))
  11674. (while (re-search-forward
  11675. re (save-excursion (outline-next-heading) (point)) t)
  11676. (setq type (if (match-end 1) org-scheduled-string
  11677. (if (match-end 3) org-deadline-string "Plain:"))
  11678. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  11679. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts)
  11680. (setq n (string-to-number (match-string 2 ts))
  11681. what (match-string 3 ts))
  11682. (if (equal what "w") (setq n (* n 7) what "d"))
  11683. (if (and (equal what "h") (not (string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts)))
  11684. (user-error "Cannot repeat in Repeat in %d hour(s) because no hour has been set" n))
  11685. ;; Preparation, see if we need to modify the start date for the change
  11686. (when (match-end 1)
  11687. (setq time (save-match-data (org-time-string-to-time ts)))
  11688. (cond
  11689. ((equal (match-string 1 ts) ".")
  11690. ;; Shift starting date to today
  11691. (org-timestamp-change
  11692. (- (org-today) (time-to-days time))
  11693. 'day))
  11694. ((equal (match-string 1 ts) "+")
  11695. (let ((nshiftmax 10) (nshift 0))
  11696. (while (or (= nshift 0)
  11697. (<= (time-to-days time)
  11698. (time-to-days (current-time))))
  11699. (when (= (incf nshift) nshiftmax)
  11700. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  11701. (error "Abort")))
  11702. (org-timestamp-change n (cdr (assoc what whata)))
  11703. (org-at-timestamp-p t)
  11704. (setq ts (match-string 1))
  11705. (setq time (save-match-data (org-time-string-to-time ts)))))
  11706. (org-timestamp-change (- n) (cdr (assoc what whata)))
  11707. ;; rematch, so that we have everything in place for the real shift
  11708. (org-at-timestamp-p t)
  11709. (setq ts (match-string 1))
  11710. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))))
  11711. (save-excursion (org-timestamp-change n (cdr (assoc what whata)) nil t))
  11712. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  11713. (setq org-log-post-message msg)
  11714. (message "%s" msg))))
  11715. (defun org-show-todo-tree (arg)
  11716. "Make a compact tree which shows all headlines marked with TODO.
  11717. The tree will show the lines where the regexp matches, and all higher
  11718. headlines above the match.
  11719. With a \\[universal-argument] prefix, prompt for a regexp to match.
  11720. With a numeric prefix N, construct a sparse tree for the Nth element
  11721. of `org-todo-keywords-1'."
  11722. (interactive "P")
  11723. (let ((case-fold-search nil)
  11724. (kwd-re
  11725. (cond ((null arg) org-not-done-regexp)
  11726. ((equal arg '(4))
  11727. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  11728. (mapcar 'list org-todo-keywords-1))))
  11729. (concat "\\("
  11730. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  11731. "\\)\\>")))
  11732. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  11733. (regexp-quote (nth (1- (prefix-numeric-value arg))
  11734. org-todo-keywords-1)))
  11735. (t (user-error "Invalid prefix argument: %s" arg)))))
  11736. (message "%d TODO entries found"
  11737. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  11738. (defun org-deadline (arg &optional time)
  11739. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  11740. With one universal prefix argument, remove any deadline from the item.
  11741. With two universal prefix arguments, prompt for a warning delay.
  11742. With argument TIME, set the deadline at the corresponding date. TIME
  11743. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11744. (interactive "P")
  11745. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11746. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11747. 'region-start-level 'region))
  11748. org-loop-over-headlines-in-active-region)
  11749. (org-map-entries
  11750. `(org-deadline ',arg ,time)
  11751. org-loop-over-headlines-in-active-region
  11752. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11753. (let* ((old-date (org-entry-get nil "DEADLINE"))
  11754. (old-date-time (if old-date (org-time-string-to-time old-date)))
  11755. (repeater (and old-date
  11756. (string-match
  11757. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11758. old-date)
  11759. (match-string 1 old-date))))
  11760. (cond
  11761. ((equal arg '(4))
  11762. (when (and old-date org-log-redeadline)
  11763. (org-add-log-setup 'deldeadline nil old-date 'findpos
  11764. org-log-redeadline))
  11765. (org-remove-timestamp-with-keyword org-deadline-string)
  11766. (message "Item no longer has a deadline."))
  11767. ((equal arg '(16))
  11768. (save-excursion
  11769. (if (re-search-forward
  11770. org-deadline-time-regexp
  11771. (save-excursion (outline-next-heading) (point)) t)
  11772. (let* ((rpl0 (match-string 1))
  11773. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  11774. (replace-match
  11775. (concat org-deadline-string
  11776. " <" rpl
  11777. (format " -%dd"
  11778. (abs
  11779. (- (time-to-days
  11780. (save-match-data
  11781. (org-read-date nil t nil "Warn starting from" old-date-time)))
  11782. (time-to-days old-date-time))))
  11783. ">") t t))
  11784. (user-error "No deadline information to update"))))
  11785. (t
  11786. (org-add-planning-info 'deadline time 'closed)
  11787. (when (and old-date org-log-redeadline
  11788. (not (equal old-date
  11789. (substring org-last-inserted-timestamp 1 -1))))
  11790. (org-add-log-setup 'redeadline nil old-date 'findpos
  11791. org-log-redeadline))
  11792. (when repeater
  11793. (save-excursion
  11794. (org-back-to-heading t)
  11795. (when (re-search-forward (concat org-deadline-string " "
  11796. org-last-inserted-timestamp)
  11797. (save-excursion
  11798. (outline-next-heading) (point)) t)
  11799. (goto-char (1- (match-end 0)))
  11800. (insert " " repeater)
  11801. (setq org-last-inserted-timestamp
  11802. (concat (substring org-last-inserted-timestamp 0 -1)
  11803. " " repeater
  11804. (substring org-last-inserted-timestamp -1))))))
  11805. (message "Deadline on %s" org-last-inserted-timestamp))))))
  11806. (defun org-schedule (arg &optional time)
  11807. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  11808. With one universal prefix argument, remove any scheduling date from the item.
  11809. With two universal prefix arguments, prompt for a delay cookie.
  11810. With argument TIME, scheduled at the corresponding date. TIME can
  11811. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11812. (interactive "P")
  11813. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11814. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11815. 'region-start-level 'region))
  11816. org-loop-over-headlines-in-active-region)
  11817. (org-map-entries
  11818. `(org-schedule ',arg ,time)
  11819. org-loop-over-headlines-in-active-region
  11820. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11821. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  11822. (old-date-time (if old-date (org-time-string-to-time old-date)))
  11823. (repeater (and old-date
  11824. (string-match
  11825. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11826. old-date)
  11827. (match-string 1 old-date))))
  11828. (cond
  11829. ((equal arg '(4))
  11830. (progn
  11831. (when (and old-date org-log-reschedule)
  11832. (org-add-log-setup 'delschedule nil old-date 'findpos
  11833. org-log-reschedule))
  11834. (org-remove-timestamp-with-keyword org-scheduled-string)
  11835. (message "Item is no longer scheduled.")))
  11836. ((equal arg '(16))
  11837. (save-excursion
  11838. (if (re-search-forward
  11839. org-scheduled-time-regexp
  11840. (save-excursion (outline-next-heading) (point)) t)
  11841. (let* ((rpl0 (match-string 1))
  11842. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  11843. (replace-match
  11844. (concat org-scheduled-string
  11845. " <" rpl
  11846. (format " -%dd"
  11847. (abs
  11848. (- (time-to-days
  11849. (save-match-data
  11850. (org-read-date nil t nil "Delay until" old-date-time)))
  11851. (time-to-days old-date-time))))
  11852. ">") t t))
  11853. (user-error "No scheduled information to update"))))
  11854. (t
  11855. (org-add-planning-info 'scheduled time 'closed)
  11856. (when (and old-date org-log-reschedule
  11857. (not (equal old-date
  11858. (substring org-last-inserted-timestamp 1 -1))))
  11859. (org-add-log-setup 'reschedule nil old-date 'findpos
  11860. org-log-reschedule))
  11861. (when repeater
  11862. (save-excursion
  11863. (org-back-to-heading t)
  11864. (when (re-search-forward (concat org-scheduled-string " "
  11865. org-last-inserted-timestamp)
  11866. (save-excursion
  11867. (outline-next-heading) (point)) t)
  11868. (goto-char (1- (match-end 0)))
  11869. (insert " " repeater)
  11870. (setq org-last-inserted-timestamp
  11871. (concat (substring org-last-inserted-timestamp 0 -1)
  11872. " " repeater
  11873. (substring org-last-inserted-timestamp -1))))))
  11874. (message "Scheduled to %s" org-last-inserted-timestamp))))))
  11875. (defun org-get-scheduled-time (pom &optional inherit)
  11876. "Get the scheduled time as a time tuple, of a format suitable
  11877. for calling org-schedule with, or if there is no scheduling,
  11878. returns nil."
  11879. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  11880. (when time
  11881. (apply 'encode-time (org-parse-time-string time)))))
  11882. (defun org-get-deadline-time (pom &optional inherit)
  11883. "Get the deadline as a time tuple, of a format suitable for
  11884. calling org-deadline with, or if there is no scheduling, returns
  11885. nil."
  11886. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  11887. (when time
  11888. (apply 'encode-time (org-parse-time-string time)))))
  11889. (defun org-remove-timestamp-with-keyword (keyword)
  11890. "Remove all time stamps with KEYWORD in the current entry."
  11891. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  11892. beg)
  11893. (save-excursion
  11894. (org-back-to-heading t)
  11895. (setq beg (point))
  11896. (outline-next-heading)
  11897. (while (re-search-backward re beg t)
  11898. (replace-match "")
  11899. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  11900. (equal (char-before) ?\ ))
  11901. (backward-delete-char 1)
  11902. (if (string-match "^[ \t]*$" (buffer-substring
  11903. (point-at-bol) (point-at-eol)))
  11904. (delete-region (point-at-bol)
  11905. (min (point-max) (1+ (point-at-eol))))))))))
  11906. (defun org-add-planning-info (what &optional time &rest remove)
  11907. "Insert new timestamp with keyword in the line directly after the headline.
  11908. WHAT indicates what kind of time stamp to add. TIME indicates the time to use.
  11909. If non is given, the user is prompted for a date.
  11910. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  11911. be removed."
  11912. (interactive)
  11913. (let (org-time-was-given org-end-time-was-given ts
  11914. end default-time default-input)
  11915. (catch 'exit
  11916. (when (and (memq what '(scheduled deadline))
  11917. (or (not time)
  11918. (and (stringp time)
  11919. (string-match "^[-+]+[0-9]" time))))
  11920. ;; Try to get a default date/time from existing timestamp
  11921. (save-excursion
  11922. (org-back-to-heading t)
  11923. (setq end (save-excursion (outline-next-heading) (point)))
  11924. (when (re-search-forward (if (eq what 'scheduled)
  11925. org-scheduled-time-regexp
  11926. org-deadline-time-regexp)
  11927. end t)
  11928. (setq ts (match-string 1)
  11929. default-time
  11930. (apply 'encode-time (org-parse-time-string ts))
  11931. default-input (and ts (org-get-compact-tod ts))))))
  11932. (when what
  11933. (setq time
  11934. (if (stringp time)
  11935. ;; This is a string (relative or absolute), set proper date
  11936. (apply 'encode-time
  11937. (org-read-date-analyze
  11938. time default-time (decode-time default-time)))
  11939. ;; If necessary, get the time from the user
  11940. (or time (org-read-date nil 'to-time nil nil
  11941. default-time default-input)))))
  11942. (when (and org-insert-labeled-timestamps-at-point
  11943. (member what '(scheduled deadline)))
  11944. (insert
  11945. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  11946. (org-insert-time-stamp time org-time-was-given
  11947. nil nil nil (list org-end-time-was-given))
  11948. (setq what nil))
  11949. (save-excursion
  11950. (save-restriction
  11951. (let (col list elt ts buffer-invisibility-spec)
  11952. (org-back-to-heading t)
  11953. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
  11954. (goto-char (match-end 1))
  11955. (setq col (current-column))
  11956. (goto-char (match-end 0))
  11957. (if (eobp) (insert "\n") (forward-char 1))
  11958. (when (and (not what)
  11959. (not (looking-at
  11960. (concat "[ \t]*"
  11961. org-keyword-time-not-clock-regexp))))
  11962. ;; Nothing to add, nothing to remove...... :-)
  11963. (throw 'exit nil))
  11964. (if (and (not (looking-at org-outline-regexp))
  11965. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  11966. "[^\r\n]*"))
  11967. (not (equal (match-string 1) org-clock-string)))
  11968. (narrow-to-region (match-beginning 0) (match-end 0))
  11969. (insert-before-markers "\n")
  11970. (backward-char 1)
  11971. (narrow-to-region (point) (point))
  11972. (and org-adapt-indentation (org-indent-to-column col)))
  11973. ;; Check if we have to remove something.
  11974. (setq list (cons what remove))
  11975. (while list
  11976. (setq elt (pop list))
  11977. (when (or (and (eq elt 'scheduled)
  11978. (re-search-forward org-scheduled-time-regexp nil t))
  11979. (and (eq elt 'deadline)
  11980. (re-search-forward org-deadline-time-regexp nil t))
  11981. (and (eq elt 'closed)
  11982. (re-search-forward org-closed-time-regexp nil t)))
  11983. (replace-match "")
  11984. (if (looking-at "--+<[^>]+>") (replace-match ""))))
  11985. (and (looking-at "[ \t]+") (replace-match ""))
  11986. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  11987. (when what
  11988. (insert
  11989. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  11990. (cond ((eq what 'scheduled) org-scheduled-string)
  11991. ((eq what 'deadline) org-deadline-string)
  11992. ((eq what 'closed) org-closed-string))
  11993. " ")
  11994. (setq ts (org-insert-time-stamp
  11995. time
  11996. (or org-time-was-given
  11997. (and (eq what 'closed) org-log-done-with-time))
  11998. (eq what 'closed)
  11999. nil nil (list org-end-time-was-given)))
  12000. (insert
  12001. (if (not (or (bolp) (eq (char-before) ?\ )
  12002. (memq (char-after) '(32 10))
  12003. (eobp))) " " ""))
  12004. (end-of-line 1))
  12005. (goto-char (point-min))
  12006. (widen)
  12007. (if (and (looking-at "[ \t]*\n")
  12008. (equal (char-before) ?\n))
  12009. (delete-region (1- (point)) (point-at-eol)))
  12010. ts))))))
  12011. (defvar org-log-note-marker (make-marker))
  12012. (defvar org-log-note-purpose nil)
  12013. (defvar org-log-note-state nil)
  12014. (defvar org-log-note-previous-state nil)
  12015. (defvar org-log-note-how nil)
  12016. (defvar org-log-note-extra nil)
  12017. (defvar org-log-note-window-configuration nil)
  12018. (defvar org-log-note-return-to (make-marker))
  12019. (defvar org-log-note-effective-time nil
  12020. "Remembered current time so that dynamically scoped
  12021. `org-extend-today-until' affects tha timestamps in state change
  12022. log")
  12023. (defvar org-log-post-message nil
  12024. "Message to be displayed after a log note has been stored.
  12025. The auto-repeater uses this.")
  12026. (defun org-add-note ()
  12027. "Add a note to the current entry.
  12028. This is done in the same way as adding a state change note."
  12029. (interactive)
  12030. (org-add-log-setup 'note nil nil 'findpos nil))
  12031. (defvar org-property-end-re)
  12032. (defun org-add-log-setup (&optional purpose state prev-state
  12033. findpos how extra)
  12034. "Set up the post command hook to take a note.
  12035. If this is about to TODO state change, the new state is expected in STATE.
  12036. When FINDPOS is non-nil, find the correct position for the note in
  12037. the current entry. If not, assume that it can be inserted at point.
  12038. HOW is an indicator what kind of note should be created.
  12039. EXTRA is additional text that will be inserted into the notes buffer."
  12040. (let* ((org-log-into-drawer (org-log-into-drawer))
  12041. (drawer (cond ((stringp org-log-into-drawer)
  12042. org-log-into-drawer)
  12043. (org-log-into-drawer "LOGBOOK"))))
  12044. (save-restriction
  12045. (save-excursion
  12046. (when findpos
  12047. (org-back-to-heading t)
  12048. (narrow-to-region (point) (save-excursion
  12049. (outline-next-heading) (point)))
  12050. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"
  12051. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  12052. "[^\r\n]*\\)?"))
  12053. (goto-char (match-end 0))
  12054. (cond
  12055. (drawer
  12056. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  12057. nil t)
  12058. (progn
  12059. (goto-char (match-end 0))
  12060. (or org-log-states-order-reversed
  12061. (and (re-search-forward org-property-end-re nil t)
  12062. (goto-char (1- (match-beginning 0))))))
  12063. (insert "\n:" drawer ":\n:END:")
  12064. (beginning-of-line 0)
  12065. (org-indent-line)
  12066. (beginning-of-line 2)
  12067. (org-indent-line)
  12068. (end-of-line 0)))
  12069. ((and org-log-state-notes-insert-after-drawers
  12070. (save-excursion
  12071. (forward-line) (looking-at org-drawer-regexp)))
  12072. (forward-line)
  12073. (while (looking-at org-drawer-regexp)
  12074. (goto-char (match-end 0))
  12075. (re-search-forward org-property-end-re (point-max) t)
  12076. (forward-line))
  12077. (forward-line -1)))
  12078. (unless org-log-states-order-reversed
  12079. (and (= (char-after) ?\n) (forward-char 1))
  12080. (org-skip-over-state-notes)
  12081. (skip-chars-backward " \t\n\r")))
  12082. (move-marker org-log-note-marker (point))
  12083. (setq org-log-note-purpose purpose
  12084. org-log-note-state state
  12085. org-log-note-previous-state prev-state
  12086. org-log-note-how how
  12087. org-log-note-extra extra
  12088. org-log-note-effective-time (org-current-effective-time))
  12089. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  12090. (defun org-skip-over-state-notes ()
  12091. "Skip past the list of State notes in an entry."
  12092. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  12093. (when (ignore-errors (goto-char (org-in-item-p)))
  12094. (let* ((struct (org-list-struct))
  12095. (prevs (org-list-prevs-alist struct)))
  12096. (while (looking-at "[ \t]*- State")
  12097. (goto-char (or (org-list-get-next-item (point) struct prevs)
  12098. (org-list-get-item-end (point) struct)))))))
  12099. (defun org-add-log-note (&optional purpose)
  12100. "Pop up a window for taking a note, and add this note later at point."
  12101. (remove-hook 'post-command-hook 'org-add-log-note)
  12102. (setq org-log-note-window-configuration (current-window-configuration))
  12103. (delete-other-windows)
  12104. (move-marker org-log-note-return-to (point))
  12105. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  12106. (goto-char org-log-note-marker)
  12107. (org-switch-to-buffer-other-window "*Org Note*")
  12108. (erase-buffer)
  12109. (if (memq org-log-note-how '(time state))
  12110. (let (current-prefix-arg) (org-store-log-note))
  12111. (let ((org-inhibit-startup t)) (org-mode))
  12112. (insert (format "# Insert note for %s.
  12113. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  12114. (cond
  12115. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  12116. ((eq org-log-note-purpose 'done) "closed todo item")
  12117. ((eq org-log-note-purpose 'state)
  12118. (format "state change from \"%s\" to \"%s\""
  12119. (or org-log-note-previous-state "")
  12120. (or org-log-note-state "")))
  12121. ((eq org-log-note-purpose 'reschedule)
  12122. "rescheduling")
  12123. ((eq org-log-note-purpose 'delschedule)
  12124. "no longer scheduled")
  12125. ((eq org-log-note-purpose 'redeadline)
  12126. "changing deadline")
  12127. ((eq org-log-note-purpose 'deldeadline)
  12128. "removing deadline")
  12129. ((eq org-log-note-purpose 'refile)
  12130. "refiling")
  12131. ((eq org-log-note-purpose 'note)
  12132. "this entry")
  12133. (t (error "This should not happen")))))
  12134. (if org-log-note-extra (insert org-log-note-extra))
  12135. (org-set-local 'org-finish-function 'org-store-log-note)
  12136. (run-hooks 'org-log-buffer-setup-hook)))
  12137. (defvar org-note-abort nil) ; dynamically scoped
  12138. (defun org-store-log-note ()
  12139. "Finish taking a log note, and insert it to where it belongs."
  12140. (let ((txt (buffer-string)))
  12141. (kill-buffer (current-buffer))
  12142. (let ((note (cdr (assq org-log-note-purpose org-log-note-headings)))
  12143. lines ind bul)
  12144. (while (string-match "\\`# .*\n[ \t\n]*" txt)
  12145. (setq txt (replace-match "" t t txt)))
  12146. (if (string-match "\\s-+\\'" txt)
  12147. (setq txt (replace-match "" t t txt)))
  12148. (setq lines (org-split-string txt "\n"))
  12149. (when (and note (string-match "\\S-" note))
  12150. (setq note
  12151. (org-replace-escapes
  12152. note
  12153. (list (cons "%u" (user-login-name))
  12154. (cons "%U" user-full-name)
  12155. (cons "%t" (format-time-string
  12156. (org-time-stamp-format 'long 'inactive)
  12157. org-log-note-effective-time))
  12158. (cons "%T" (format-time-string
  12159. (org-time-stamp-format 'long nil)
  12160. org-log-note-effective-time))
  12161. (cons "%d" (format-time-string
  12162. (org-time-stamp-format nil 'inactive)
  12163. org-log-note-effective-time))
  12164. (cons "%D" (format-time-string
  12165. (org-time-stamp-format nil nil)
  12166. org-log-note-effective-time))
  12167. (cons "%s" (if org-log-note-state
  12168. (concat "\"" org-log-note-state "\"")
  12169. ""))
  12170. (cons "%S" (if org-log-note-previous-state
  12171. (concat "\"" org-log-note-previous-state "\"")
  12172. "\"\"")))))
  12173. (if lines (setq note (concat note " \\\\")))
  12174. (push note lines))
  12175. (when (or current-prefix-arg org-note-abort)
  12176. (when org-log-into-drawer
  12177. (org-remove-empty-drawer-at
  12178. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  12179. org-log-note-marker))
  12180. (setq lines nil))
  12181. (when lines
  12182. (with-current-buffer (marker-buffer org-log-note-marker)
  12183. (save-excursion
  12184. (goto-char org-log-note-marker)
  12185. (move-marker org-log-note-marker nil)
  12186. (end-of-line 1)
  12187. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  12188. (setq ind (save-excursion
  12189. (if (ignore-errors (goto-char (org-in-item-p)))
  12190. (let ((struct (org-list-struct)))
  12191. (org-list-get-ind
  12192. (org-list-get-top-point struct) struct))
  12193. (skip-chars-backward " \r\t\n")
  12194. (cond
  12195. ((and (org-at-heading-p)
  12196. org-adapt-indentation)
  12197. (1+ (org-current-level)))
  12198. ((org-at-heading-p) 0)
  12199. (t (org-get-indentation))))))
  12200. (setq bul (org-list-bullet-string "-"))
  12201. (org-indent-line-to ind)
  12202. (insert bul (pop lines))
  12203. (let ((ind-body (+ (length bul) ind)))
  12204. (while lines
  12205. (insert "\n")
  12206. (org-indent-line-to ind-body)
  12207. (insert (pop lines))))
  12208. (message "Note stored")
  12209. (org-back-to-heading t)
  12210. (org-cycle-hide-drawers 'children))
  12211. ;; Fix `buffer-undo-list' when `org-store-log-note' is called
  12212. ;; from within `org-add-log-note' because `buffer-undo-list'
  12213. ;; is then modified outside of `org-with-remote-undo'.
  12214. (when (eq this-command 'org-agenda-todo)
  12215. (setcdr buffer-undo-list (cddr buffer-undo-list)))))))
  12216. ;; Don't add undo information when called from `org-agenda-todo'
  12217. (let ((buffer-undo-list (eq this-command 'org-agenda-todo)))
  12218. (set-window-configuration org-log-note-window-configuration)
  12219. (with-current-buffer (marker-buffer org-log-note-return-to)
  12220. (goto-char org-log-note-return-to))
  12221. (move-marker org-log-note-return-to nil)
  12222. (and org-log-post-message (message "%s" org-log-post-message))))
  12223. (defun org-remove-empty-drawer-at (drawer pos)
  12224. "Remove an empty drawer DRAWER at position POS.
  12225. POS may also be a marker."
  12226. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  12227. (save-excursion
  12228. (save-restriction
  12229. (widen)
  12230. (goto-char pos)
  12231. (if (org-in-regexp
  12232. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  12233. (replace-match ""))))))
  12234. (defvar org-ts-type nil)
  12235. (defun org-sparse-tree (&optional arg type)
  12236. "Create a sparse tree, prompt for the details.
  12237. This command can create sparse trees. You first need to select the type
  12238. of match used to create the tree:
  12239. t Show all TODO entries.
  12240. T Show entries with a specific TODO keyword.
  12241. m Show entries selected by a tags/property match.
  12242. p Enter a property name and its value (both with completion on existing
  12243. names/values) and show entries with that property.
  12244. r Show entries matching a regular expression (`/' can be used as well).
  12245. b Show deadlines and scheduled items before a date.
  12246. a Show deadlines and scheduled items after a date.
  12247. d Show deadlines due within `org-deadline-warning-days'.
  12248. D Show deadlines and scheduled items between a date range."
  12249. (interactive "P")
  12250. (let (ans kwd value ts-type)
  12251. (setq type (or type org-sparse-tree-default-date-type))
  12252. (setq org-ts-type type)
  12253. (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"
  12254. (cond ((eq type 'all) "all timestamps")
  12255. ((eq type 'scheduled) "only scheduled")
  12256. ((eq type 'deadline) "only deadline")
  12257. ((eq type 'active) "only active timestamps")
  12258. ((eq type 'inactive) "only inactive timestamps")
  12259. ((eq type 'scheduled-or-deadline) "scheduled/deadline")
  12260. ((eq type 'closed) "with a closed time-stamp")
  12261. (t "scheduled/deadline")))
  12262. (setq ans (read-char-exclusive))
  12263. (cond
  12264. ((equal ans ?c)
  12265. (org-sparse-tree
  12266. arg (cadr (member type '(scheduled-or-deadline
  12267. all scheduled deadline active inactive closed)))))
  12268. ((equal ans ?d)
  12269. (call-interactively 'org-check-deadlines))
  12270. ((equal ans ?b)
  12271. (call-interactively 'org-check-before-date))
  12272. ((equal ans ?a)
  12273. (call-interactively 'org-check-after-date))
  12274. ((equal ans ?D)
  12275. (call-interactively 'org-check-dates-range))
  12276. ((equal ans ?t)
  12277. (call-interactively 'org-show-todo-tree))
  12278. ((equal ans ?T)
  12279. (org-show-todo-tree '(4)))
  12280. ((member ans '(?T ?m))
  12281. (call-interactively 'org-match-sparse-tree))
  12282. ((member ans '(?p ?P))
  12283. (setq kwd (org-icompleting-read "Property: "
  12284. (mapcar 'list (org-buffer-property-keys))))
  12285. (setq value (org-icompleting-read "Value: "
  12286. (mapcar 'list (org-property-values kwd))))
  12287. (unless (string-match "\\`{.*}\\'" value)
  12288. (setq value (concat "\"" value "\"")))
  12289. (org-match-sparse-tree arg (concat kwd "=" value)))
  12290. ((member ans '(?r ?R ?/))
  12291. (call-interactively 'org-occur))
  12292. (t (user-error "No such sparse tree command \"%c\"" ans)))))
  12293. (defvar org-occur-highlights nil
  12294. "List of overlays used for occur matches.")
  12295. (make-variable-buffer-local 'org-occur-highlights)
  12296. (defvar org-occur-parameters nil
  12297. "Parameters of the active org-occur calls.
  12298. This is a list, each call to org-occur pushes as cons cell,
  12299. containing the regular expression and the callback, onto the list.
  12300. The list can contain several entries if `org-occur' has been called
  12301. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  12302. will only contain one set of parameters. When the highlights are
  12303. removed (for example with `C-c C-c', or with the next edit (depending
  12304. on `org-remove-highlights-with-change'), this variable is emptied
  12305. as well.")
  12306. (make-variable-buffer-local 'org-occur-parameters)
  12307. (defun org-occur (regexp &optional keep-previous callback)
  12308. "Make a compact tree which shows all matches of REGEXP.
  12309. The tree will show the lines where the regexp matches, and all higher
  12310. headlines above the match. It will also show the heading after the match,
  12311. to make sure editing the matching entry is easy.
  12312. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  12313. call to `org-occur' will be kept, to allow stacking of calls to this
  12314. command.
  12315. If CALLBACK is non-nil, it is a function which is called to confirm
  12316. that the match should indeed be shown."
  12317. (interactive "sRegexp: \nP")
  12318. (when (equal regexp "")
  12319. (user-error "Regexp cannot be empty"))
  12320. (unless keep-previous
  12321. (org-remove-occur-highlights nil nil t))
  12322. (push (cons regexp callback) org-occur-parameters)
  12323. (let ((cnt 0))
  12324. (save-excursion
  12325. (goto-char (point-min))
  12326. (if (or (not keep-previous) ; do not want to keep
  12327. (not org-occur-highlights)) ; no previous matches
  12328. ;; hide everything
  12329. (org-overview))
  12330. (while (re-search-forward regexp nil t)
  12331. (when (or (not callback)
  12332. (save-match-data (funcall callback)))
  12333. (setq cnt (1+ cnt))
  12334. (when org-highlight-sparse-tree-matches
  12335. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  12336. (org-show-context 'occur-tree))))
  12337. (when org-remove-highlights-with-change
  12338. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  12339. nil 'local))
  12340. (unless org-sparse-tree-open-archived-trees
  12341. (org-hide-archived-subtrees (point-min) (point-max)))
  12342. (run-hooks 'org-occur-hook)
  12343. (if (org-called-interactively-p 'interactive)
  12344. (message "%d match(es) for regexp %s" cnt regexp))
  12345. cnt))
  12346. (defun org-occur-next-match (&optional n reset)
  12347. "Function for `next-error-function' to find sparse tree matches.
  12348. N is the number of matches to move, when negative move backwards.
  12349. RESET is entirely ignored - this function always goes back to the
  12350. starting point when no match is found."
  12351. (let* ((limit (if (< n 0) (point-min) (point-max)))
  12352. (search-func (if (< n 0)
  12353. 'previous-single-char-property-change
  12354. 'next-single-char-property-change))
  12355. (n (abs n))
  12356. (pos (point))
  12357. p1)
  12358. (catch 'exit
  12359. (while (setq p1 (funcall search-func (point) 'org-type))
  12360. (when (equal p1 limit)
  12361. (goto-char pos)
  12362. (error "No more matches"))
  12363. (when (equal (get-char-property p1 'org-type) 'org-occur)
  12364. (setq n (1- n))
  12365. (when (= n 0)
  12366. (goto-char p1)
  12367. (throw 'exit (point))))
  12368. (goto-char p1))
  12369. (goto-char p1)
  12370. (error "No more matches"))))
  12371. (defun org-show-context (&optional key)
  12372. "Make sure point and context are visible.
  12373. How much context is shown depends upon the variables
  12374. `org-show-hierarchy-above', `org-show-following-heading',
  12375. `org-show-entry-below' and `org-show-siblings'."
  12376. (let ((heading-p (org-at-heading-p t))
  12377. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  12378. (following-p (org-get-alist-option org-show-following-heading key))
  12379. (entry-p (org-get-alist-option org-show-entry-below key))
  12380. (siblings-p (org-get-alist-option org-show-siblings key)))
  12381. ;; Show heading or entry text
  12382. (if (and heading-p (not entry-p))
  12383. (org-flag-heading nil) ; only show the heading
  12384. (and (or entry-p (outline-invisible-p) (org-invisible-p2))
  12385. (org-show-hidden-entry))) ; show entire entry
  12386. (when following-p
  12387. ;; Show next sibling, or heading below text
  12388. (save-excursion
  12389. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  12390. (org-flag-heading nil))))
  12391. (when siblings-p (org-show-siblings))
  12392. (when hierarchy-p
  12393. ;; show all higher headings, possibly with siblings
  12394. (save-excursion
  12395. (while (and (condition-case nil
  12396. (progn (org-up-heading-all 1) t)
  12397. (error nil))
  12398. (not (bobp)))
  12399. (org-flag-heading nil)
  12400. (when siblings-p (org-show-siblings)))))
  12401. (unless (eq key 'agenda) (org-fix-ellipsis-at-bol))))
  12402. (defvar org-reveal-start-hook nil
  12403. "Hook run before revealing a location.")
  12404. (defun org-reveal (&optional siblings)
  12405. "Show current entry, hierarchy above it, and the following headline.
  12406. This can be used to show a consistent set of context around locations
  12407. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  12408. not t for the search context.
  12409. With optional argument SIBLINGS, on each level of the hierarchy all
  12410. siblings are shown. This repairs the tree structure to what it would
  12411. look like when opened with hierarchical calls to `org-cycle'.
  12412. With double optional argument \\[universal-argument] \\[universal-argument], \
  12413. go to the parent and show the
  12414. entire tree."
  12415. (interactive "P")
  12416. (run-hooks 'org-reveal-start-hook)
  12417. (let ((org-show-hierarchy-above t)
  12418. (org-show-following-heading t)
  12419. (org-show-siblings (if siblings t org-show-siblings)))
  12420. (org-show-context nil))
  12421. (when (equal siblings '(16))
  12422. (save-excursion
  12423. (when (org-up-heading-safe)
  12424. (org-show-subtree)
  12425. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  12426. (defun org-highlight-new-match (beg end)
  12427. "Highlight from BEG to END and mark the highlight is an occur headline."
  12428. (let ((ov (make-overlay beg end)))
  12429. (overlay-put ov 'face 'secondary-selection)
  12430. (overlay-put ov 'org-type 'org-occur)
  12431. (push ov org-occur-highlights)))
  12432. (defun org-remove-occur-highlights (&optional beg end noremove)
  12433. "Remove the occur highlights from the buffer.
  12434. BEG and END are ignored. If NOREMOVE is nil, remove this function
  12435. from the `before-change-functions' in the current buffer."
  12436. (interactive)
  12437. (unless org-inhibit-highlight-removal
  12438. (mapc 'delete-overlay org-occur-highlights)
  12439. (setq org-occur-highlights nil)
  12440. (setq org-occur-parameters nil)
  12441. (unless noremove
  12442. (remove-hook 'before-change-functions
  12443. 'org-remove-occur-highlights 'local))))
  12444. ;;;; Priorities
  12445. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  12446. "Regular expression matching the priority indicator.")
  12447. (defvar org-remove-priority-next-time nil)
  12448. (defun org-priority-up ()
  12449. "Increase the priority of the current item."
  12450. (interactive)
  12451. (org-priority 'up))
  12452. (defun org-priority-down ()
  12453. "Decrease the priority of the current item."
  12454. (interactive)
  12455. (org-priority 'down))
  12456. (defun org-priority (&optional action show)
  12457. "Change the priority of an item.
  12458. ACTION can be `set', `up', `down', or a character."
  12459. (interactive "P")
  12460. (if (equal action '(4))
  12461. (org-show-priority)
  12462. (unless org-enable-priority-commands
  12463. (user-error "Priority commands are disabled"))
  12464. (setq action (or action 'set))
  12465. (let (current new news have remove)
  12466. (save-excursion
  12467. (org-back-to-heading t)
  12468. (if (looking-at org-priority-regexp)
  12469. (setq current (string-to-char (match-string 2))
  12470. have t))
  12471. (cond
  12472. ((eq action 'remove)
  12473. (setq remove t new ?\ ))
  12474. ((or (eq action 'set)
  12475. (if (featurep 'xemacs) (characterp action) (integerp action)))
  12476. (if (not (eq action 'set))
  12477. (setq new action)
  12478. (message "Priority %c-%c, SPC to remove: "
  12479. org-highest-priority org-lowest-priority)
  12480. (save-match-data
  12481. (setq new (read-char-exclusive))))
  12482. (if (and (= (upcase org-highest-priority) org-highest-priority)
  12483. (= (upcase org-lowest-priority) org-lowest-priority))
  12484. (setq new (upcase new)))
  12485. (cond ((equal new ?\ ) (setq remove t))
  12486. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  12487. (user-error "Priority must be between `%c' and `%c'"
  12488. org-highest-priority org-lowest-priority))))
  12489. ((eq action 'up)
  12490. (setq new (if have
  12491. (1- current) ; normal cycling
  12492. ;; last priority was empty
  12493. (if (eq last-command this-command)
  12494. org-lowest-priority ; wrap around empty to lowest
  12495. ;; default
  12496. (if org-priority-start-cycle-with-default
  12497. org-default-priority
  12498. (1- org-default-priority))))))
  12499. ((eq action 'down)
  12500. (setq new (if have
  12501. (1+ current) ; normal cycling
  12502. ;; last priority was empty
  12503. (if (eq last-command this-command)
  12504. org-highest-priority ; wrap around empty to highest
  12505. ;; default
  12506. (if org-priority-start-cycle-with-default
  12507. org-default-priority
  12508. (1+ org-default-priority))))))
  12509. (t (user-error "Invalid action")))
  12510. (if (or (< (upcase new) org-highest-priority)
  12511. (> (upcase new) org-lowest-priority))
  12512. (if (and (memq action '(up down))
  12513. (not have) (not (eq last-command this-command)))
  12514. ;; `new' is from default priority
  12515. (error
  12516. "The default can not be set, see `org-default-priority' why")
  12517. ;; normal cycling: `new' is beyond highest/lowest priority
  12518. ;; and is wrapped around to the empty priority
  12519. (setq remove t)))
  12520. (setq news (format "%c" new))
  12521. (if have
  12522. (if remove
  12523. (replace-match "" t t nil 1)
  12524. (replace-match news t t nil 2))
  12525. (if remove
  12526. (user-error "No priority cookie found in line")
  12527. (let ((case-fold-search nil))
  12528. (looking-at org-todo-line-regexp))
  12529. (if (match-end 2)
  12530. (progn
  12531. (goto-char (match-end 2))
  12532. (insert " [#" news "]"))
  12533. (goto-char (match-beginning 3))
  12534. (insert "[#" news "] "))))
  12535. (org-preserve-lc (org-set-tags nil 'align)))
  12536. (if remove
  12537. (message "Priority removed")
  12538. (message "Priority of current item set to %s" news)))))
  12539. (defun org-show-priority ()
  12540. "Show the priority of the current item.
  12541. This priority is composed of the main priority given with the [#A] cookies,
  12542. and by additional input from the age of a schedules or deadline entry."
  12543. (interactive)
  12544. (let ((pri (if (eq major-mode 'org-agenda-mode)
  12545. (org-get-at-bol 'priority)
  12546. (save-excursion
  12547. (save-match-data
  12548. (beginning-of-line)
  12549. (and (looking-at org-heading-regexp)
  12550. (org-get-priority (match-string 0))))))))
  12551. (message "Priority is %d" (if pri pri -1000))))
  12552. (defun org-get-priority (s)
  12553. "Find priority cookie and return priority."
  12554. (save-match-data
  12555. (if (functionp org-get-priority-function)
  12556. (funcall org-get-priority-function)
  12557. (if (not (string-match org-priority-regexp s))
  12558. (* 1000 (- org-lowest-priority org-default-priority))
  12559. (* 1000 (- org-lowest-priority
  12560. (string-to-char (match-string 2 s))))))))
  12561. ;;;; Tags
  12562. (defvar org-agenda-archives-mode)
  12563. (defvar org-map-continue-from nil
  12564. "Position from where mapping should continue.
  12565. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  12566. (defvar org-scanner-tags nil
  12567. "The current tag list while the tags scanner is running.")
  12568. (defvar org-trust-scanner-tags nil
  12569. "Should `org-get-tags-at' use the tags for the scanner.
  12570. This is for internal dynamical scoping only.
  12571. When this is non-nil, the function `org-get-tags-at' will return the value
  12572. of `org-scanner-tags' instead of building the list by itself. This
  12573. can lead to large speed-ups when the tags scanner is used in a file with
  12574. many entries, and when the list of tags is retrieved, for example to
  12575. obtain a list of properties. Building the tags list for each entry in such
  12576. a file becomes an N^2 operation - but with this variable set, it scales
  12577. as N.")
  12578. (defun org-scan-tags (action matcher todo-only &optional start-level)
  12579. "Sca headline tags with inheritance and produce output ACTION.
  12580. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  12581. or `agenda' to produce an entry list for an agenda view. It can also be
  12582. a Lisp form or a function that should be called at each matched headline, in
  12583. this case the return value is a list of all return values from these calls.
  12584. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  12585. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  12586. only lines with a not-done TODO keyword are included in the output.
  12587. This should be the same variable that was scoped into
  12588. and set by `org-make-tags-matcher' when it constructed MATCHER.
  12589. START-LEVEL can be a string with asterisks, reducing the scope to
  12590. headlines matching this string."
  12591. (require 'org-agenda)
  12592. (let* ((re (concat "^"
  12593. (if start-level
  12594. ;; Get the correct level to match
  12595. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  12596. org-outline-regexp)
  12597. " *\\(\\<\\("
  12598. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  12599. (org-re "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  12600. (props (list 'face 'default
  12601. 'done-face 'org-agenda-done
  12602. 'undone-face 'default
  12603. 'mouse-face 'highlight
  12604. 'org-not-done-regexp org-not-done-regexp
  12605. 'org-todo-regexp org-todo-regexp
  12606. 'org-complex-heading-regexp org-complex-heading-regexp
  12607. 'help-echo
  12608. (format "mouse-2 or RET jump to org file %s"
  12609. (abbreviate-file-name
  12610. (or (buffer-file-name (buffer-base-buffer))
  12611. (buffer-name (buffer-base-buffer)))))))
  12612. (case-fold-search nil)
  12613. (org-map-continue-from nil)
  12614. lspos tags tags-list
  12615. (tags-alist (list (cons 0 org-file-tags)))
  12616. (llast 0) rtn rtn1 level category i txt
  12617. todo marker entry priority)
  12618. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  12619. (setq action (list 'lambda nil action)))
  12620. (save-excursion
  12621. (goto-char (point-min))
  12622. (when (eq action 'sparse-tree)
  12623. (org-overview)
  12624. (org-remove-occur-highlights))
  12625. (while (re-search-forward re nil t)
  12626. (setq org-map-continue-from nil)
  12627. (catch :skip
  12628. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  12629. tags (if (match-end 4) (org-match-string-no-properties 4)))
  12630. (goto-char (setq lspos (match-beginning 0)))
  12631. (setq level (org-reduced-level (org-outline-level))
  12632. category (org-get-category))
  12633. (setq i llast llast level)
  12634. ;; remove tag lists from same and sublevels
  12635. (while (>= i level)
  12636. (when (setq entry (assoc i tags-alist))
  12637. (setq tags-alist (delete entry tags-alist)))
  12638. (setq i (1- i)))
  12639. ;; add the next tags
  12640. (when tags
  12641. (setq tags (org-split-string tags ":")
  12642. tags-alist
  12643. (cons (cons level tags) tags-alist)))
  12644. ;; compile tags for current headline
  12645. (setq tags-list
  12646. (if org-use-tag-inheritance
  12647. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  12648. tags)
  12649. org-scanner-tags tags-list)
  12650. (when org-use-tag-inheritance
  12651. (setcdr (car tags-alist)
  12652. (mapcar (lambda (x)
  12653. (setq x (copy-sequence x))
  12654. (org-add-prop-inherited x))
  12655. (cdar tags-alist))))
  12656. (when (and tags org-use-tag-inheritance
  12657. (or (not (eq t org-use-tag-inheritance))
  12658. org-tags-exclude-from-inheritance))
  12659. ;; selective inheritance, remove uninherited ones
  12660. (setcdr (car tags-alist)
  12661. (org-remove-uninherited-tags (cdar tags-alist))))
  12662. (when (and
  12663. ;; eval matcher only when the todo condition is OK
  12664. (and (or (not todo-only) (member todo org-not-done-keywords))
  12665. (let ((case-fold-search t) (org-trust-scanner-tags t))
  12666. (eval matcher)))
  12667. ;; Call the skipper, but return t if it does not skip,
  12668. ;; so that the `and' form continues evaluating
  12669. (progn
  12670. (unless (eq action 'sparse-tree) (org-agenda-skip))
  12671. t)
  12672. ;; Check if timestamps are deselecting this entry
  12673. (or (not todo-only)
  12674. (and (member todo org-not-done-keywords)
  12675. (or (not org-agenda-tags-todo-honor-ignore-options)
  12676. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item))))))
  12677. ;; select this headline
  12678. (cond
  12679. ((eq action 'sparse-tree)
  12680. (and org-highlight-sparse-tree-matches
  12681. (org-get-heading) (match-end 0)
  12682. (org-highlight-new-match
  12683. (match-beginning 1) (match-end 1)))
  12684. (org-show-context 'tags-tree))
  12685. ((eq action 'agenda)
  12686. (setq txt (org-agenda-format-item
  12687. ""
  12688. (concat
  12689. (if (eq org-tags-match-list-sublevels 'indented)
  12690. (make-string (1- level) ?.) "")
  12691. (org-get-heading))
  12692. level category
  12693. tags-list)
  12694. priority (org-get-priority txt))
  12695. (goto-char lspos)
  12696. (setq marker (org-agenda-new-marker))
  12697. (org-add-props txt props
  12698. 'org-marker marker 'org-hd-marker marker 'org-category category
  12699. 'todo-state todo
  12700. 'priority priority 'type "tagsmatch")
  12701. (push txt rtn))
  12702. ((functionp action)
  12703. (setq org-map-continue-from nil)
  12704. (save-excursion
  12705. (setq rtn1 (funcall action))
  12706. (push rtn1 rtn)))
  12707. (t (user-error "Invalid action")))
  12708. ;; if we are to skip sublevels, jump to end of subtree
  12709. (unless org-tags-match-list-sublevels
  12710. (org-end-of-subtree t)
  12711. (backward-char 1))))
  12712. ;; Get the correct position from where to continue
  12713. (if org-map-continue-from
  12714. (goto-char org-map-continue-from)
  12715. (and (= (point) lspos) (end-of-line 1)))))
  12716. (when (and (eq action 'sparse-tree)
  12717. (not org-sparse-tree-open-archived-trees))
  12718. (org-hide-archived-subtrees (point-min) (point-max)))
  12719. (nreverse rtn)))
  12720. (defun org-remove-uninherited-tags (tags)
  12721. "Remove all tags that are not inherited from the list TAGS."
  12722. (cond
  12723. ((eq org-use-tag-inheritance t)
  12724. (if org-tags-exclude-from-inheritance
  12725. (org-delete-all org-tags-exclude-from-inheritance tags)
  12726. tags))
  12727. ((not org-use-tag-inheritance) nil)
  12728. ((stringp org-use-tag-inheritance)
  12729. (delq nil (mapcar
  12730. (lambda (x)
  12731. (if (and (string-match org-use-tag-inheritance x)
  12732. (not (member x org-tags-exclude-from-inheritance)))
  12733. x nil))
  12734. tags)))
  12735. ((listp org-use-tag-inheritance)
  12736. (delq nil (mapcar
  12737. (lambda (x)
  12738. (if (member x org-use-tag-inheritance) x nil))
  12739. tags)))))
  12740. (defun org-match-sparse-tree (&optional todo-only match)
  12741. "Create a sparse tree according to tags string MATCH.
  12742. MATCH can contain positive and negative selection of tags, like
  12743. \"+WORK+URGENT-WITHBOSS\".
  12744. If optional argument TODO-ONLY is non-nil, only select lines that are
  12745. also TODO lines."
  12746. (interactive "P")
  12747. (org-agenda-prepare-buffers (list (current-buffer)))
  12748. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  12749. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  12750. (defvar org-cached-props nil)
  12751. (defun org-cached-entry-get (pom property)
  12752. (if (or (eq t org-use-property-inheritance)
  12753. (and (stringp org-use-property-inheritance)
  12754. (string-match org-use-property-inheritance property))
  12755. (and (listp org-use-property-inheritance)
  12756. (member property org-use-property-inheritance)))
  12757. ;; Caching is not possible, check it directly
  12758. (org-entry-get pom property 'inherit)
  12759. ;; Get all properties, so that we can do complicated checks easily
  12760. (cdr (assoc property (or org-cached-props
  12761. (setq org-cached-props
  12762. (org-entry-properties pom)))))))
  12763. (defun org-global-tags-completion-table (&optional files)
  12764. "Return the list of all tags in all agenda buffer/files.
  12765. Optional FILES argument is a list of files which can be used
  12766. instead of the agenda files."
  12767. (save-excursion
  12768. (org-uniquify
  12769. (delq nil
  12770. (apply 'append
  12771. (mapcar
  12772. (lambda (file)
  12773. (set-buffer (find-file-noselect file))
  12774. (append (org-get-buffer-tags)
  12775. (mapcar (lambda (x) (if (stringp (car-safe x))
  12776. (list (car-safe x)) nil))
  12777. org-tag-alist)))
  12778. (if (and files (car files))
  12779. files
  12780. (org-agenda-files))))))))
  12781. (defun org-make-tags-matcher (match)
  12782. "Create the TAGS/TODO matcher form for the selection string MATCH.
  12783. The variable `todo-only' is scoped dynamically into this function.
  12784. It will be set to t if the matcher restricts matching to TODO entries,
  12785. otherwise will not be touched.
  12786. Returns a cons of the selection string MATCH and the constructed
  12787. lisp form implementing the matcher. The matcher is to be evaluated
  12788. at an Org entry, with point on the headline, and returns t if the
  12789. entry matches the selection string MATCH. The returned lisp form
  12790. references two variables with information about the entry, which
  12791. must be bound around the form's evaluation: todo, the TODO keyword
  12792. at the entry (or nil of none); and tags-list, the list of all tags
  12793. at the entry including inherited ones. Additionally, the category
  12794. of the entry (if any) must be specified as the text property
  12795. 'org-category on the headline.
  12796. See also `org-scan-tags'.
  12797. "
  12798. (declare (special todo-only))
  12799. (unless (boundp 'todo-only)
  12800. (error "`org-make-tags-matcher' expects todo-only to be scoped in"))
  12801. (unless match
  12802. ;; Get a new match request, with completion against the global
  12803. ;; tags table and the local tags in current buffer
  12804. (let ((org-last-tags-completion-table
  12805. (org-uniquify
  12806. (delq nil (append (org-get-buffer-tags)
  12807. (org-global-tags-completion-table))))))
  12808. (setq match (org-completing-read-no-i
  12809. "Match: " 'org-tags-completion-function nil nil nil
  12810. 'org-tags-history))))
  12811. ;; Parse the string and create a lisp form
  12812. (let ((match0 match)
  12813. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  12814. minus tag mm
  12815. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  12816. orterms term orlist re-p str-p level-p level-op time-p
  12817. prop-p pn pv po gv rest)
  12818. ;; Expand group tags
  12819. (setq match (org-tags-expand match))
  12820. (if (string-match "/+" match)
  12821. ;; match contains also a todo-matching request
  12822. (progn
  12823. (setq tagsmatch (substring match 0 (match-beginning 0))
  12824. todomatch (substring match (match-end 0)))
  12825. (if (string-match "^!" todomatch)
  12826. (setq todo-only t todomatch (substring todomatch 1)))
  12827. (if (string-match "^\\s-*$" todomatch)
  12828. (setq todomatch nil)))
  12829. ;; only matching tags
  12830. (setq tagsmatch match todomatch nil))
  12831. ;; Make the tags matcher
  12832. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  12833. (setq tagsmatcher t)
  12834. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  12835. (while (setq term (pop orterms))
  12836. (while (and (equal (substring term -1) "\\") orterms)
  12837. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  12838. (while (string-match re term)
  12839. (setq rest (substring term (match-end 0))
  12840. minus (and (match-end 1)
  12841. (equal (match-string 1 term) "-"))
  12842. tag (save-match-data (replace-regexp-in-string
  12843. "\\\\-" "-"
  12844. (match-string 2 term)))
  12845. re-p (equal (string-to-char tag) ?{)
  12846. level-p (match-end 4)
  12847. prop-p (match-end 5)
  12848. mm (cond
  12849. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  12850. (level-p
  12851. (setq level-op (org-op-to-function (match-string 3 term)))
  12852. `(,level-op level ,(string-to-number
  12853. (match-string 4 term))))
  12854. (prop-p
  12855. (setq pn (match-string 5 term)
  12856. po (match-string 6 term)
  12857. pv (match-string 7 term)
  12858. re-p (equal (string-to-char pv) ?{)
  12859. str-p (equal (string-to-char pv) ?\")
  12860. time-p (save-match-data
  12861. (string-match "^\"[[<].*[]>]\"$" pv))
  12862. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  12863. (if time-p (setq pv (org-matcher-time pv)))
  12864. (setq po (org-op-to-function po (if time-p 'time str-p)))
  12865. (cond
  12866. ((equal pn "CATEGORY")
  12867. (setq gv '(get-text-property (point) 'org-category)))
  12868. ((equal pn "TODO")
  12869. (setq gv 'todo))
  12870. (t
  12871. (setq gv `(org-cached-entry-get nil ,pn))))
  12872. (if re-p
  12873. (if (eq po 'org<>)
  12874. `(not (string-match ,pv (or ,gv "")))
  12875. `(string-match ,pv (or ,gv "")))
  12876. (if str-p
  12877. `(,po (or ,gv "") ,pv)
  12878. `(,po (string-to-number (or ,gv ""))
  12879. ,(string-to-number pv) ))))
  12880. (t `(member ,tag tags-list)))
  12881. mm (if minus (list 'not mm) mm)
  12882. term rest)
  12883. (push mm tagsmatcher))
  12884. (push (if (> (length tagsmatcher) 1)
  12885. (cons 'and tagsmatcher)
  12886. (car tagsmatcher))
  12887. orlist)
  12888. (setq tagsmatcher nil))
  12889. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  12890. (setq tagsmatcher
  12891. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  12892. ;; Make the todo matcher
  12893. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  12894. (setq todomatcher t)
  12895. (setq orterms (org-split-string todomatch "|") orlist nil)
  12896. (while (setq term (pop orterms))
  12897. (while (string-match re term)
  12898. (setq minus (and (match-end 1)
  12899. (equal (match-string 1 term) "-"))
  12900. kwd (match-string 2 term)
  12901. re-p (equal (string-to-char kwd) ?{)
  12902. term (substring term (match-end 0))
  12903. mm (if re-p
  12904. `(string-match ,(substring kwd 1 -1) todo)
  12905. (list 'equal 'todo kwd))
  12906. mm (if minus (list 'not mm) mm))
  12907. (push mm todomatcher))
  12908. (push (if (> (length todomatcher) 1)
  12909. (cons 'and todomatcher)
  12910. (car todomatcher))
  12911. orlist)
  12912. (setq todomatcher nil))
  12913. (setq todomatcher (if (> (length orlist) 1)
  12914. (cons 'or orlist) (car orlist))))
  12915. ;; Return the string and lisp forms of the matcher
  12916. (setq matcher (if todomatcher
  12917. (list 'and tagsmatcher todomatcher)
  12918. tagsmatcher))
  12919. (when todo-only
  12920. (setq matcher (list 'and '(member todo org-not-done-keywords)
  12921. matcher)))
  12922. (cons match0 matcher)))
  12923. (defun org-tags-expand (match &optional single-as-list downcased)
  12924. "Expand group tags in MATCH.
  12925. This replaces every group tag in MATCH with a regexp tag search.
  12926. For example, a group tag \"Work\" defined as { Work : Lab Conf }
  12927. will be replaced like this:
  12928. Work => {\\(?:Work\\|Lab\\|Conf\\)}
  12929. +Work => +{\\(?:Work\\|Lab\\|Conf\\)}
  12930. -Work => -{\\(?:Work\\|Lab\\|Conf\\)}
  12931. Replacing by a regexp preserves the structure of the match.
  12932. E.g., this expansion
  12933. Work|Home => {\\(?:Work\\|Lab\\|Conf\\}|Home
  12934. will match anything tagged with \"Lab\" and \"Home\", or tagged
  12935. with \"Conf\" and \"Home\" or tagged with \"Work\" and \"home\".
  12936. When the optional argument SINGLE-AS-LIST is non-nil, MATCH is
  12937. assumed to be a single group tag, and the function will return
  12938. the list of tags in this group.
  12939. When DOWNCASE is non-nil, expand downcased TAGS."
  12940. (if org-group-tags
  12941. (let* ((case-fold-search t)
  12942. (stable org-mode-syntax-table)
  12943. (tal (or org-tag-groups-alist-for-agenda
  12944. org-tag-groups-alist))
  12945. (tal (if downcased
  12946. (mapcar (lambda(tg) (mapcar 'downcase tg)) tal) tal))
  12947. (tml (mapcar 'car tal))
  12948. (rtnmatch match) rpl)
  12949. ;; @ and _ are allowed as word-components in tags
  12950. (modify-syntax-entry ?@ "w" stable)
  12951. (modify-syntax-entry ?_ "w" stable)
  12952. (while (and tml
  12953. (string-match
  12954. (concat "\\(?1:[+-]?\\)\\(?2:\\<"
  12955. (regexp-opt tml) "\\>\\)") rtnmatch))
  12956. (let* ((dir (match-string 1 rtnmatch))
  12957. (tag (match-string 2 rtnmatch))
  12958. (tag (if downcased (downcase tag) tag)))
  12959. (setq tml (delete tag tml))
  12960. (when (not (get-text-property 0 'grouptag (match-string 2 rtnmatch)))
  12961. (setq rpl (append (org-uniquify rpl) (assoc tag tal)))
  12962. (setq rpl (concat dir "{\\<" (regexp-opt rpl) "\\>}"))
  12963. (if (stringp rpl) (org-add-props rpl '(grouptag t)))
  12964. (setq rtnmatch (replace-match rpl t t rtnmatch)))))
  12965. (if single-as-list
  12966. (or (reverse rpl) (list rtnmatch))
  12967. rtnmatch))
  12968. (if single-as-list (list (if downcased (downcase match) match))
  12969. match)))
  12970. (defun org-op-to-function (op &optional stringp)
  12971. "Turn an operator into the appropriate function."
  12972. (setq op
  12973. (cond
  12974. ((equal op "<" ) '(< string< org-time<))
  12975. ((equal op ">" ) '(> org-string> org-time>))
  12976. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  12977. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  12978. ((member op '("=" "==")) '(= string= org-time=))
  12979. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  12980. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  12981. (defun org<> (a b) (not (= a b)))
  12982. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  12983. (defun org-string>= (a b) (not (string< a b)))
  12984. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  12985. (defun org-string<> (a b) (not (string= a b)))
  12986. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  12987. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  12988. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  12989. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  12990. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  12991. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  12992. (defun org-2ft (s)
  12993. "Convert S to a floating point time.
  12994. If S is already a number, just return it. If it is a string, parse
  12995. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  12996. (cond
  12997. ((numberp s) s)
  12998. ((stringp s)
  12999. (condition-case nil
  13000. (float-time (apply 'encode-time (org-parse-time-string s)))
  13001. (error 0.)))
  13002. (t 0.)))
  13003. (defun org-time-today ()
  13004. "Time in seconds today at 0:00.
  13005. Returns the float number of seconds since the beginning of the
  13006. epoch to the beginning of today (00:00)."
  13007. (float-time (apply 'encode-time
  13008. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  13009. (defun org-matcher-time (s)
  13010. "Interpret a time comparison value."
  13011. (save-match-data
  13012. (cond
  13013. ((string= s "<now>") (float-time))
  13014. ((string= s "<today>") (org-time-today))
  13015. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  13016. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  13017. ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
  13018. (+ (org-time-today)
  13019. (* (string-to-number (match-string 1 s))
  13020. (cdr (assoc (match-string 2 s)
  13021. '(("d" . 86400.0) ("w" . 604800.0)
  13022. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  13023. (t (org-2ft s)))))
  13024. (defun org-match-any-p (re list)
  13025. "Does re match any element of list?"
  13026. (setq list (mapcar (lambda (x) (string-match re x)) list))
  13027. (delq nil list))
  13028. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  13029. (defvar org-tags-overlay (make-overlay 1 1))
  13030. (org-detach-overlay org-tags-overlay)
  13031. (defun org-get-local-tags-at (&optional pos)
  13032. "Get a list of tags defined in the current headline."
  13033. (org-get-tags-at pos 'local))
  13034. (defun org-get-local-tags ()
  13035. "Get a list of tags defined in the current headline."
  13036. (org-get-tags-at nil 'local))
  13037. (defun org-get-tags-at (&optional pos local)
  13038. "Get a list of all headline tags applicable at POS.
  13039. POS defaults to point. If tags are inherited, the list contains
  13040. the targets in the same sequence as the headlines appear, i.e.
  13041. the tags of the current headline come last.
  13042. When LOCAL is non-nil, only return tags from the current headline,
  13043. ignore inherited ones."
  13044. (interactive)
  13045. (if (and org-trust-scanner-tags
  13046. (or (not pos) (equal pos (point)))
  13047. (not local))
  13048. org-scanner-tags
  13049. (let (tags ltags lastpos parent)
  13050. (save-excursion
  13051. (save-restriction
  13052. (widen)
  13053. (goto-char (or pos (point)))
  13054. (save-match-data
  13055. (catch 'done
  13056. (condition-case nil
  13057. (progn
  13058. (org-back-to-heading t)
  13059. (while (not (equal lastpos (point)))
  13060. (setq lastpos (point))
  13061. (when (looking-at
  13062. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  13063. (setq ltags (org-split-string
  13064. (org-match-string-no-properties 1) ":"))
  13065. (when parent
  13066. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  13067. (setq tags (append
  13068. (if parent
  13069. (org-remove-uninherited-tags ltags)
  13070. ltags)
  13071. tags)))
  13072. (or org-use-tag-inheritance (throw 'done t))
  13073. (if local (throw 'done t))
  13074. (or (org-up-heading-safe) (error nil))
  13075. (setq parent t)))
  13076. (error nil)))))
  13077. (if local
  13078. tags
  13079. (reverse (delete-dups
  13080. (reverse (append
  13081. (org-remove-uninherited-tags
  13082. org-file-tags) tags)))))))))
  13083. (defun org-add-prop-inherited (s)
  13084. (add-text-properties 0 (length s) '(inherited t) s)
  13085. s)
  13086. (defun org-toggle-tag (tag &optional onoff)
  13087. "Toggle the tag TAG for the current line.
  13088. If ONOFF is `on' or `off', don't toggle but set to this state."
  13089. (let (res current)
  13090. (save-excursion
  13091. (org-back-to-heading t)
  13092. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  13093. (point-at-eol) t)
  13094. (progn
  13095. (setq current (match-string 1))
  13096. (replace-match ""))
  13097. (setq current ""))
  13098. (setq current (nreverse (org-split-string current ":")))
  13099. (cond
  13100. ((eq onoff 'on)
  13101. (setq res t)
  13102. (or (member tag current) (push tag current)))
  13103. ((eq onoff 'off)
  13104. (or (not (member tag current)) (setq current (delete tag current))))
  13105. (t (if (member tag current)
  13106. (setq current (delete tag current))
  13107. (setq res t)
  13108. (push tag current))))
  13109. (end-of-line 1)
  13110. (if current
  13111. (progn
  13112. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  13113. (org-set-tags nil t))
  13114. (delete-horizontal-space))
  13115. (run-hooks 'org-after-tags-change-hook))
  13116. res))
  13117. (defun org-align-tags-here (to-col)
  13118. ;; Assumes that this is a headline
  13119. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  13120. (beginning-of-line 1)
  13121. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13122. (< pos (match-beginning 2)))
  13123. (progn
  13124. (setq tags-l (- (match-end 2) (match-beginning 2)))
  13125. (goto-char (match-beginning 1))
  13126. (insert " ")
  13127. (delete-region (point) (1+ (match-beginning 2)))
  13128. (setq ncol (max (current-column)
  13129. (1+ col)
  13130. (if (> to-col 0)
  13131. to-col
  13132. (- (abs to-col) tags-l))))
  13133. (setq p (point))
  13134. (insert (make-string (- ncol (current-column)) ?\ ))
  13135. (setq ncol (current-column))
  13136. (when indent-tabs-mode (tabify p (point-at-eol)))
  13137. (org-move-to-column (min ncol col) t))
  13138. (goto-char pos))))
  13139. (defun org-set-tags-command (&optional arg just-align)
  13140. "Call the set-tags command for the current entry."
  13141. (interactive "P")
  13142. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  13143. (org-set-tags arg just-align)
  13144. (save-excursion
  13145. (unless (and (org-region-active-p)
  13146. org-loop-over-headlines-in-active-region)
  13147. (org-back-to-heading t))
  13148. (org-set-tags arg just-align))))
  13149. (defun org-set-tags-to (data)
  13150. "Set the tags of the current entry to DATA, replacing the current tags.
  13151. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  13152. If DATA is nil or the empty string, any tags will be removed."
  13153. (interactive "sTags: ")
  13154. (setq data
  13155. (cond
  13156. ((eq data nil) "")
  13157. ((equal data "") "")
  13158. ((stringp data)
  13159. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  13160. ":"))
  13161. ((listp data)
  13162. (concat ":" (mapconcat 'identity data ":") ":"))))
  13163. (when data
  13164. (save-excursion
  13165. (org-back-to-heading t)
  13166. (when (looking-at org-complex-heading-regexp)
  13167. (if (match-end 5)
  13168. (progn
  13169. (goto-char (match-beginning 5))
  13170. (insert data)
  13171. (delete-region (point) (point-at-eol))
  13172. (org-set-tags nil 'align))
  13173. (goto-char (point-at-eol))
  13174. (insert " " data)
  13175. (org-set-tags nil 'align)))
  13176. (beginning-of-line 1)
  13177. (if (looking-at ".*?\\([ \t]+\\)$")
  13178. (delete-region (match-beginning 1) (match-end 1))))))
  13179. (defun org-align-all-tags ()
  13180. "Align the tags i all headings."
  13181. (interactive)
  13182. (save-excursion
  13183. (or (ignore-errors (org-back-to-heading t))
  13184. (outline-next-heading))
  13185. (if (org-at-heading-p)
  13186. (org-set-tags t)
  13187. (message "No headings"))))
  13188. (defvar org-indent-indentation-per-level)
  13189. (defun org-set-tags (&optional arg just-align)
  13190. "Set the tags for the current headline.
  13191. With prefix ARG, realign all tags in headings in the current buffer."
  13192. (interactive "P")
  13193. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  13194. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  13195. 'region-start-level 'region))
  13196. org-loop-over-headlines-in-active-region)
  13197. (org-map-entries
  13198. ;; We don't use ARG and JUST-ALIGN here these args are not
  13199. ;; useful when looping over headlines
  13200. `(org-set-tags)
  13201. org-loop-over-headlines-in-active-region
  13202. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  13203. (let* ((re org-outline-regexp-bol)
  13204. (current (unless arg (org-get-tags-string)))
  13205. (col (current-column))
  13206. (org-setting-tags t)
  13207. table current-tags inherited-tags ; computed below when needed
  13208. tags p0 c0 c1 rpl di tc level)
  13209. (if arg
  13210. (save-excursion
  13211. (goto-char (point-min))
  13212. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  13213. (while (re-search-forward re nil t)
  13214. (org-set-tags nil t)
  13215. (end-of-line 1)))
  13216. (message "All tags realigned to column %d" org-tags-column))
  13217. (if just-align
  13218. (setq tags current)
  13219. ;; Get a new set of tags from the user
  13220. (save-excursion
  13221. (setq table (append org-tag-persistent-alist
  13222. (or org-tag-alist (org-get-buffer-tags))
  13223. (and
  13224. org-complete-tags-always-offer-all-agenda-tags
  13225. (org-global-tags-completion-table
  13226. (org-agenda-files))))
  13227. org-last-tags-completion-table table
  13228. current-tags (org-split-string current ":")
  13229. inherited-tags (nreverse
  13230. (nthcdr (length current-tags)
  13231. (nreverse (org-get-tags-at))))
  13232. tags
  13233. (if (or (eq t org-use-fast-tag-selection)
  13234. (and org-use-fast-tag-selection
  13235. (delq nil (mapcar 'cdr table))))
  13236. (org-fast-tag-selection
  13237. current-tags inherited-tags table
  13238. (if org-fast-tag-selection-include-todo
  13239. org-todo-key-alist))
  13240. (let ((org-add-colon-after-tag-completion (< 1 (length table))))
  13241. (org-trim
  13242. (org-icompleting-read "Tags: "
  13243. 'org-tags-completion-function
  13244. nil nil current 'org-tags-history))))))
  13245. (while (string-match "[-+&]+" tags)
  13246. ;; No boolean logic, just a list
  13247. (setq tags (replace-match ":" t t tags))))
  13248. (setq tags (replace-regexp-in-string "[,]" ":" tags))
  13249. (if org-tags-sort-function
  13250. (setq tags (mapconcat 'identity
  13251. (sort (org-split-string
  13252. tags (org-re "[^[:alnum:]_@#%]+"))
  13253. org-tags-sort-function) ":")))
  13254. (if (string-match "\\`[\t ]*\\'" tags)
  13255. (setq tags "")
  13256. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  13257. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  13258. ;; Insert new tags at the correct column
  13259. (beginning-of-line 1)
  13260. (setq level (or (and (looking-at org-outline-regexp)
  13261. (- (match-end 0) (point) 1))
  13262. 1))
  13263. (cond
  13264. ((and (equal current "") (equal tags "")))
  13265. ((re-search-forward
  13266. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  13267. (point-at-eol) t)
  13268. (if (equal tags "")
  13269. (setq rpl "")
  13270. (goto-char (match-beginning 0))
  13271. (setq c0 (current-column)
  13272. ;; compute offset for the case of org-indent-mode active
  13273. di (if org-indent-mode
  13274. (* (1- org-indent-indentation-per-level) (1- level))
  13275. 0)
  13276. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  13277. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  13278. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
  13279. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  13280. (replace-match rpl t t)
  13281. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  13282. tags)
  13283. (t (error "Tags alignment failed")))
  13284. (org-move-to-column col)
  13285. (unless just-align
  13286. (run-hooks 'org-after-tags-change-hook))))))
  13287. (defun org-change-tag-in-region (beg end tag off)
  13288. "Add or remove TAG for each entry in the region.
  13289. This works in the agenda, and also in an org-mode buffer."
  13290. (interactive
  13291. (list (region-beginning) (region-end)
  13292. (let ((org-last-tags-completion-table
  13293. (if (derived-mode-p 'org-mode)
  13294. (org-uniquify
  13295. (delq nil (append (org-get-buffer-tags)
  13296. (org-global-tags-completion-table))))
  13297. (org-global-tags-completion-table))))
  13298. (org-icompleting-read
  13299. "Tag: " 'org-tags-completion-function nil nil nil
  13300. 'org-tags-history))
  13301. (progn
  13302. (message "[s]et or [r]emove? ")
  13303. (equal (read-char-exclusive) ?r))))
  13304. (if (fboundp 'deactivate-mark) (deactivate-mark))
  13305. (let ((agendap (equal major-mode 'org-agenda-mode))
  13306. l1 l2 m buf pos newhead (cnt 0))
  13307. (goto-char end)
  13308. (setq l2 (1- (org-current-line)))
  13309. (goto-char beg)
  13310. (setq l1 (org-current-line))
  13311. (loop for l from l1 to l2 do
  13312. (org-goto-line l)
  13313. (setq m (get-text-property (point) 'org-hd-marker))
  13314. (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
  13315. (and agendap m))
  13316. (setq buf (if agendap (marker-buffer m) (current-buffer))
  13317. pos (if agendap m (point)))
  13318. (with-current-buffer buf
  13319. (save-excursion
  13320. (save-restriction
  13321. (goto-char pos)
  13322. (setq cnt (1+ cnt))
  13323. (org-toggle-tag tag (if off 'off 'on))
  13324. (setq newhead (org-get-heading)))))
  13325. (and agendap (org-agenda-change-all-lines newhead m))))
  13326. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  13327. (defun org-tags-completion-function (string predicate &optional flag)
  13328. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  13329. (confirm (lambda (x) (stringp (car x)))))
  13330. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  13331. (setq s1 (match-string 1 string)
  13332. s2 (match-string 2 string))
  13333. (setq s1 "" s2 string))
  13334. (cond
  13335. ((eq flag nil)
  13336. ;; try completion
  13337. (setq rtn (try-completion s2 ctable confirm))
  13338. (if (stringp rtn)
  13339. (setq rtn
  13340. (concat s1 s2 (substring rtn (length s2))
  13341. (if (and org-add-colon-after-tag-completion
  13342. (assoc rtn ctable))
  13343. ":" ""))))
  13344. rtn)
  13345. ((eq flag t)
  13346. ;; all-completions
  13347. (all-completions s2 ctable confirm))
  13348. ((eq flag 'lambda)
  13349. ;; exact match?
  13350. (assoc s2 ctable)))))
  13351. (defun org-fast-tag-insert (kwd tags face &optional end)
  13352. "Insert KDW, and the TAGS, the latter with face FACE.
  13353. Also insert END."
  13354. (insert (format "%-12s" (concat kwd ":"))
  13355. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  13356. (or end "")))
  13357. (defun org-fast-tag-show-exit (flag)
  13358. (save-excursion
  13359. (org-goto-line 3)
  13360. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  13361. (replace-match ""))
  13362. (when flag
  13363. (end-of-line 1)
  13364. (org-move-to-column (- (window-width) 19) t)
  13365. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  13366. (defun org-set-current-tags-overlay (current prefix)
  13367. "Add an overlay to CURRENT tag with PREFIX."
  13368. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  13369. (if (featurep 'xemacs)
  13370. (org-overlay-display org-tags-overlay (concat prefix s)
  13371. 'secondary-selection)
  13372. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  13373. (org-overlay-display org-tags-overlay (concat prefix s)))))
  13374. (defvar org-last-tag-selection-key nil)
  13375. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  13376. "Fast tag selection with single keys.
  13377. CURRENT is the current list of tags in the headline, INHERITED is the
  13378. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  13379. possibly with grouping information. TODO-TABLE is a similar table with
  13380. TODO keywords, should these have keys assigned to them.
  13381. If the keys are nil, a-z are automatically assigned.
  13382. Returns the new tags string, or nil to not change the current settings."
  13383. (let* ((fulltable (append table todo-table))
  13384. (maxlen (apply 'max (mapcar
  13385. (lambda (x)
  13386. (if (stringp (car x)) (string-width (car x)) 0))
  13387. fulltable)))
  13388. (buf (current-buffer))
  13389. (expert (eq org-fast-tag-selection-single-key 'expert))
  13390. (buffer-tags nil)
  13391. (fwidth (+ maxlen 3 1 3))
  13392. (ncol (/ (- (window-width) 4) fwidth))
  13393. (i-face 'org-done)
  13394. (c-face 'org-todo)
  13395. tg cnt e c char c1 c2 ntable tbl rtn
  13396. ov-start ov-end ov-prefix
  13397. (exit-after-next org-fast-tag-selection-single-key)
  13398. (done-keywords org-done-keywords)
  13399. groups ingroup)
  13400. (save-excursion
  13401. (beginning-of-line 1)
  13402. (if (looking-at
  13403. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13404. (setq ov-start (match-beginning 1)
  13405. ov-end (match-end 1)
  13406. ov-prefix "")
  13407. (setq ov-start (1- (point-at-eol))
  13408. ov-end (1+ ov-start))
  13409. (skip-chars-forward "^\n\r")
  13410. (setq ov-prefix
  13411. (concat
  13412. (buffer-substring (1- (point)) (point))
  13413. (if (> (current-column) org-tags-column)
  13414. " "
  13415. (make-string (- org-tags-column (current-column)) ?\ ))))))
  13416. (move-overlay org-tags-overlay ov-start ov-end)
  13417. (save-window-excursion
  13418. (if expert
  13419. (set-buffer (get-buffer-create " *Org tags*"))
  13420. (delete-other-windows)
  13421. (split-window-vertically)
  13422. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  13423. (erase-buffer)
  13424. (org-set-local 'org-done-keywords done-keywords)
  13425. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  13426. (org-fast-tag-insert "Current" current c-face "\n\n")
  13427. (org-fast-tag-show-exit exit-after-next)
  13428. (org-set-current-tags-overlay current ov-prefix)
  13429. (setq tbl fulltable char ?a cnt 0)
  13430. (while (setq e (pop tbl))
  13431. (cond
  13432. ((equal (car e) :startgroup)
  13433. (push '() groups) (setq ingroup t)
  13434. (when (not (= cnt 0))
  13435. (setq cnt 0)
  13436. (insert "\n"))
  13437. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  13438. ((equal (car e) :endgroup)
  13439. (setq ingroup nil cnt 0)
  13440. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  13441. ((equal e '(:newline))
  13442. (when (not (= cnt 0))
  13443. (setq cnt 0)
  13444. (insert "\n")
  13445. (setq e (car tbl))
  13446. (while (equal (car tbl) '(:newline))
  13447. (insert "\n")
  13448. (setq tbl (cdr tbl)))))
  13449. ((equal e '(:grouptags)) nil)
  13450. (t
  13451. (setq tg (copy-sequence (car e)) c2 nil)
  13452. (if (cdr e)
  13453. (setq c (cdr e))
  13454. ;; automatically assign a character.
  13455. (setq c1 (string-to-char
  13456. (downcase (substring
  13457. tg (if (= (string-to-char tg) ?@) 1 0)))))
  13458. (if (or (rassoc c1 ntable) (rassoc c1 table))
  13459. (while (or (rassoc char ntable) (rassoc char table))
  13460. (setq char (1+ char)))
  13461. (setq c2 c1))
  13462. (setq c (or c2 char)))
  13463. (if ingroup (push tg (car groups)))
  13464. (setq tg (org-add-props tg nil 'face
  13465. (cond
  13466. ((not (assoc tg table))
  13467. (org-get-todo-face tg))
  13468. ((member tg current) c-face)
  13469. ((member tg inherited) i-face))))
  13470. (if (equal (caar tbl) :grouptags)
  13471. (org-add-props tg nil 'face 'org-tag-group))
  13472. (if (and (= cnt 0) (not ingroup)) (insert " "))
  13473. (insert "[" c "] " tg (make-string
  13474. (- fwidth 4 (length tg)) ?\ ))
  13475. (push (cons tg c) ntable)
  13476. (when (= (setq cnt (1+ cnt)) ncol)
  13477. (insert "\n")
  13478. (if ingroup (insert " "))
  13479. (setq cnt 0)))))
  13480. (setq ntable (nreverse ntable))
  13481. (insert "\n")
  13482. (goto-char (point-min))
  13483. (if (not expert) (org-fit-window-to-buffer))
  13484. (setq rtn
  13485. (catch 'exit
  13486. (while t
  13487. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  13488. (if (not groups) "no " "")
  13489. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  13490. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  13491. (setq org-last-tag-selection-key c)
  13492. (cond
  13493. ((= c ?\r) (throw 'exit t))
  13494. ((= c ?!)
  13495. (setq groups (not groups))
  13496. (goto-char (point-min))
  13497. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  13498. ((= c ?\C-c)
  13499. (if (not expert)
  13500. (org-fast-tag-show-exit
  13501. (setq exit-after-next (not exit-after-next)))
  13502. (setq expert nil)
  13503. (delete-other-windows)
  13504. (set-window-buffer (split-window-vertically) " *Org tags*")
  13505. (org-switch-to-buffer-other-window " *Org tags*")
  13506. (org-fit-window-to-buffer)))
  13507. ((or (= c ?\C-g)
  13508. (and (= c ?q) (not (rassoc c ntable))))
  13509. (org-detach-overlay org-tags-overlay)
  13510. (setq quit-flag t))
  13511. ((= c ?\ )
  13512. (setq current nil)
  13513. (if exit-after-next (setq exit-after-next 'now)))
  13514. ((= c ?\t)
  13515. (condition-case nil
  13516. (setq tg (org-icompleting-read
  13517. "Tag: "
  13518. (or buffer-tags
  13519. (with-current-buffer buf
  13520. (org-get-buffer-tags)))))
  13521. (quit (setq tg "")))
  13522. (when (string-match "\\S-" tg)
  13523. (add-to-list 'buffer-tags (list tg))
  13524. (if (member tg current)
  13525. (setq current (delete tg current))
  13526. (push tg current)))
  13527. (if exit-after-next (setq exit-after-next 'now)))
  13528. ((setq e (rassoc c todo-table) tg (car e))
  13529. (with-current-buffer buf
  13530. (save-excursion (org-todo tg)))
  13531. (if exit-after-next (setq exit-after-next 'now)))
  13532. ((setq e (rassoc c ntable) tg (car e))
  13533. (if (member tg current)
  13534. (setq current (delete tg current))
  13535. (loop for g in groups do
  13536. (if (member tg g)
  13537. (mapc (lambda (x)
  13538. (setq current (delete x current)))
  13539. g)))
  13540. (push tg current))
  13541. (if exit-after-next (setq exit-after-next 'now))))
  13542. ;; Create a sorted list
  13543. (setq current
  13544. (sort current
  13545. (lambda (a b)
  13546. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  13547. (if (eq exit-after-next 'now) (throw 'exit t))
  13548. (goto-char (point-min))
  13549. (beginning-of-line 2)
  13550. (delete-region (point) (point-at-eol))
  13551. (org-fast-tag-insert "Current" current c-face)
  13552. (org-set-current-tags-overlay current ov-prefix)
  13553. (while (re-search-forward
  13554. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  13555. (setq tg (match-string 1))
  13556. (add-text-properties
  13557. (match-beginning 1) (match-end 1)
  13558. (list 'face
  13559. (cond
  13560. ((member tg current) c-face)
  13561. ((member tg inherited) i-face)
  13562. (t (get-text-property (match-beginning 1) 'face))))))
  13563. (goto-char (point-min)))))
  13564. (org-detach-overlay org-tags-overlay)
  13565. (if rtn
  13566. (mapconcat 'identity current ":")
  13567. nil))))
  13568. (defun org-get-tags-string ()
  13569. "Get the TAGS string in the current headline."
  13570. (unless (org-at-heading-p t)
  13571. (user-error "Not on a heading"))
  13572. (save-excursion
  13573. (beginning-of-line 1)
  13574. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13575. (org-match-string-no-properties 1)
  13576. "")))
  13577. (defun org-get-tags ()
  13578. "Get the list of tags specified in the current headline."
  13579. (org-split-string (org-get-tags-string) ":"))
  13580. (defun org-get-buffer-tags ()
  13581. "Get a table of all tags used in the buffer, for completion."
  13582. (let (tags)
  13583. (save-excursion
  13584. (goto-char (point-min))
  13585. (while (re-search-forward
  13586. (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
  13587. (when (equal (char-after (point-at-bol 0)) ?*)
  13588. (mapc (lambda (x) (add-to-list 'tags x))
  13589. (org-split-string (org-match-string-no-properties 1) ":")))))
  13590. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  13591. (mapcar 'list tags)))
  13592. ;;;; The mapping API
  13593. (defun org-map-entries (func &optional match scope &rest skip)
  13594. "Call FUNC at each headline selected by MATCH in SCOPE.
  13595. FUNC is a function or a lisp form. The function will be called without
  13596. arguments, with the cursor positioned at the beginning of the headline.
  13597. The return values of all calls to the function will be collected and
  13598. returned as a list.
  13599. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  13600. does not need to preserve point. After evaluation, the cursor will be
  13601. moved to the end of the line (presumably of the headline of the
  13602. processed entry) and search continues from there. Under some
  13603. circumstances, this may not produce the wanted results. For example,
  13604. if you have removed (e.g. archived) the current (sub)tree it could
  13605. mean that the next entry will be skipped entirely. In such cases, you
  13606. can specify the position from where search should continue by making
  13607. FUNC set the variable `org-map-continue-from' to the desired buffer
  13608. position.
  13609. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  13610. Only headlines that are matched by this query will be considered during
  13611. the iteration. When MATCH is nil or t, all headlines will be
  13612. visited by the iteration.
  13613. SCOPE determines the scope of this command. It can be any of:
  13614. nil The current buffer, respecting the restriction if any
  13615. tree The subtree started with the entry at point
  13616. region The entries within the active region, if any
  13617. region-start-level
  13618. The entries within the active region, but only those at
  13619. the same level than the first one.
  13620. file The current buffer, without restriction
  13621. file-with-archives
  13622. The current buffer, and any archives associated with it
  13623. agenda All agenda files
  13624. agenda-with-archives
  13625. All agenda files with any archive files associated with them
  13626. \(file1 file2 ...)
  13627. If this is a list, all files in the list will be scanned
  13628. The remaining args are treated as settings for the skipping facilities of
  13629. the scanner. The following items can be given here:
  13630. archive skip trees with the archive tag
  13631. comment skip trees with the COMMENT keyword
  13632. function or Emacs Lisp form:
  13633. will be used as value for `org-agenda-skip-function', so
  13634. whenever the function returns a position, FUNC will not be
  13635. called for that entry and search will continue from the
  13636. position returned
  13637. If your function needs to retrieve the tags including inherited tags
  13638. at the *current* entry, you can use the value of the variable
  13639. `org-scanner-tags' which will be much faster than getting the value
  13640. with `org-get-tags-at'. If your function gets properties with
  13641. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  13642. to t around the call to `org-entry-properties' to get the same speedup.
  13643. Note that if your function moves around to retrieve tags and properties at
  13644. a *different* entry, you cannot use these techniques."
  13645. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  13646. (not (org-region-active-p)))
  13647. (let* ((org-agenda-archives-mode nil) ; just to make sure
  13648. (org-agenda-skip-archived-trees (memq 'archive skip))
  13649. (org-agenda-skip-comment-trees (memq 'comment skip))
  13650. (org-agenda-skip-function
  13651. (car (org-delete-all '(comment archive) skip)))
  13652. (org-tags-match-list-sublevels t)
  13653. (start-level (eq scope 'region-start-level))
  13654. matcher file res
  13655. org-todo-keywords-for-agenda
  13656. org-done-keywords-for-agenda
  13657. org-todo-keyword-alist-for-agenda
  13658. org-drawers-for-agenda
  13659. org-tag-alist-for-agenda
  13660. todo-only)
  13661. (cond
  13662. ((eq match t) (setq matcher t))
  13663. ((eq match nil) (setq matcher t))
  13664. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  13665. (save-window-excursion
  13666. (save-restriction
  13667. (cond ((eq scope 'tree)
  13668. (org-back-to-heading t)
  13669. (org-narrow-to-subtree)
  13670. (setq scope nil))
  13671. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  13672. (org-region-active-p))
  13673. ;; If needed, set start-level to a string like "2"
  13674. (when start-level
  13675. (save-excursion
  13676. (goto-char (region-beginning))
  13677. (unless (org-at-heading-p) (outline-next-heading))
  13678. (setq start-level (org-current-level))))
  13679. (narrow-to-region (region-beginning)
  13680. (save-excursion
  13681. (goto-char (region-end))
  13682. (unless (and (bolp) (org-at-heading-p))
  13683. (outline-next-heading))
  13684. (point)))
  13685. (setq scope nil)))
  13686. (if (not scope)
  13687. (progn
  13688. (org-agenda-prepare-buffers
  13689. (list (buffer-file-name (current-buffer))))
  13690. (setq res (org-scan-tags func matcher todo-only start-level)))
  13691. ;; Get the right scope
  13692. (cond
  13693. ((and scope (listp scope) (symbolp (car scope)))
  13694. (setq scope (eval scope)))
  13695. ((eq scope 'agenda)
  13696. (setq scope (org-agenda-files t)))
  13697. ((eq scope 'agenda-with-archives)
  13698. (setq scope (org-agenda-files t))
  13699. (setq scope (org-add-archive-files scope)))
  13700. ((eq scope 'file)
  13701. (setq scope (list (buffer-file-name))))
  13702. ((eq scope 'file-with-archives)
  13703. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  13704. (org-agenda-prepare-buffers scope)
  13705. (while (setq file (pop scope))
  13706. (with-current-buffer (org-find-base-buffer-visiting file)
  13707. (save-excursion
  13708. (save-restriction
  13709. (widen)
  13710. (goto-char (point-min))
  13711. (setq res (append res (org-scan-tags func matcher todo-only))))))))))
  13712. res)))
  13713. ;;;; Properties
  13714. ;;; Setting and retrieving properties
  13715. (defconst org-special-properties
  13716. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  13717. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM" "CLOCKSUM_T")
  13718. "The special properties valid in Org-mode.
  13719. These are properties that are not defined in the property drawer,
  13720. but in some other way.")
  13721. (defconst org-default-properties
  13722. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  13723. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  13724. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  13725. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  13726. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  13727. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  13728. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  13729. "Some properties that are used by Org-mode for various purposes.
  13730. Being in this list makes sure that they are offered for completion.")
  13731. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  13732. "Regular expression matching the first line of a property drawer.")
  13733. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  13734. "Regular expression matching the last line of a property drawer.")
  13735. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  13736. "Regular expression matching the first line of a property drawer.")
  13737. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  13738. "Regular expression matching the first line of a property drawer.")
  13739. (defconst org-property-drawer-re
  13740. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  13741. org-property-end-re "\\)\n?")
  13742. "Matches an entire property drawer.")
  13743. (defconst org-clock-drawer-re
  13744. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  13745. org-property-end-re "\\)\n?")
  13746. "Matches an entire clock drawer.")
  13747. (defsubst org-re-property (property)
  13748. "Return a regexp matching a PROPERTY line.
  13749. Match group 1 will be set to the value."
  13750. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)"))
  13751. (defsubst org-re-property-keyword (property)
  13752. "Return a regexp matching a PROPERTY line, possibly with no
  13753. value for the property."
  13754. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)?"))
  13755. (defun org-property-action ()
  13756. "Do an action on properties."
  13757. (interactive)
  13758. (let (c)
  13759. (org-at-property-p)
  13760. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  13761. (setq c (read-char-exclusive))
  13762. (cond
  13763. ((equal c ?s)
  13764. (call-interactively 'org-set-property))
  13765. ((equal c ?d)
  13766. (call-interactively 'org-delete-property))
  13767. ((equal c ?D)
  13768. (call-interactively 'org-delete-property-globally))
  13769. ((equal c ?c)
  13770. (call-interactively 'org-compute-property-at-point))
  13771. (t (user-error "No such property action %c" c)))))
  13772. (defun org-inc-effort ()
  13773. "Increment the value of the effort property in the current entry."
  13774. (interactive)
  13775. (org-set-effort nil t))
  13776. (defvar org-clock-effort) ;; Defined in org-clock.el
  13777. (defvar org-clock-current-task) ;; Defined in org-clock.el
  13778. (defun org-set-effort (&optional value increment)
  13779. "Set the effort property of the current entry.
  13780. With numerical prefix arg, use the nth allowed value, 0 stands for the
  13781. 10th allowed value.
  13782. When INCREMENT is non-nil, set the property to the next allowed value."
  13783. (interactive "P")
  13784. (if (equal value 0) (setq value 10))
  13785. (let* ((completion-ignore-case t)
  13786. (prop org-effort-property)
  13787. (cur (org-entry-get nil prop))
  13788. (allowed (org-property-get-allowed-values nil prop 'table))
  13789. (existing (mapcar 'list (org-property-values prop)))
  13790. (heading (nth 4 (org-heading-components)))
  13791. rpl
  13792. (val (cond
  13793. ((stringp value) value)
  13794. ((and allowed (integerp value))
  13795. (or (car (nth (1- value) allowed))
  13796. (car (org-last allowed))))
  13797. ((and allowed increment)
  13798. (or (caadr (member (list cur) allowed))
  13799. (user-error "Allowed effort values are not set")))
  13800. (allowed
  13801. (message "Select 1-9,0, [RET%s]: %s"
  13802. (if cur (concat "=" cur) "")
  13803. (mapconcat 'car allowed " "))
  13804. (setq rpl (read-char-exclusive))
  13805. (if (equal rpl ?\r)
  13806. cur
  13807. (setq rpl (- rpl ?0))
  13808. (if (equal rpl 0) (setq rpl 10))
  13809. (if (and (> rpl 0) (<= rpl (length allowed)))
  13810. (car (nth (1- rpl) allowed))
  13811. (org-completing-read "Effort: " allowed nil))))
  13812. (t
  13813. (let (org-completion-use-ido org-completion-use-iswitchb)
  13814. (org-completing-read
  13815. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  13816. (concat "[" cur "]") "")
  13817. ": ")
  13818. existing nil nil "" nil cur))))))
  13819. (unless (equal (org-entry-get nil prop) val)
  13820. (org-entry-put nil prop val))
  13821. (save-excursion
  13822. (org-back-to-heading t)
  13823. (put-text-property (point-at-bol) (point-at-eol) 'org-effort val))
  13824. (when (string= heading org-clock-current-task)
  13825. (setq org-clock-effort (get-text-property (point-at-bol) 'org-effort))
  13826. (org-clock-update-mode-line))
  13827. (message "%s is now %s" prop val)))
  13828. (defun org-at-property-p ()
  13829. "Is cursor inside a property drawer?"
  13830. (save-excursion
  13831. (when (equal 'node-property (car (org-element-at-point)))
  13832. (beginning-of-line 1)
  13833. (looking-at org-property-re))))
  13834. (defun org-get-property-block (&optional beg end force)
  13835. "Return the (beg . end) range of the body of the property drawer.
  13836. BEG and END are the beginning and end of the current subtree, or of
  13837. the part before the first headline. If they are not given, they will
  13838. be found. If the drawer does not exist and FORCE is non-nil, create
  13839. the drawer."
  13840. (catch 'exit
  13841. (save-excursion
  13842. (let* ((beg (or beg (and (org-before-first-heading-p) (point-min))
  13843. (progn (org-back-to-heading t) (point))))
  13844. (end (or end (and (not (outline-next-heading)) (point-max))
  13845. (point))))
  13846. (goto-char beg)
  13847. (if (re-search-forward org-property-start-re end t)
  13848. (setq beg (1+ (match-end 0)))
  13849. (if force
  13850. (save-excursion
  13851. (org-insert-property-drawer)
  13852. (setq end (progn (outline-next-heading) (point))))
  13853. (throw 'exit nil))
  13854. (goto-char beg)
  13855. (if (re-search-forward org-property-start-re end t)
  13856. (setq beg (1+ (match-end 0)))))
  13857. (if (re-search-forward org-property-end-re end t)
  13858. (setq end (match-beginning 0))
  13859. (or force (throw 'exit nil))
  13860. (goto-char beg)
  13861. (setq end beg)
  13862. (org-indent-line)
  13863. (insert ":END:\n"))
  13864. (cons beg end)))))
  13865. (defun org-entry-properties (&optional pom which specific)
  13866. "Get all properties of the entry at point-or-marker POM.
  13867. This includes the TODO keyword, the tags, time strings for deadline,
  13868. scheduled, and clocking, and any additional properties defined in the
  13869. entry. The return value is an alist, keys may occur multiple times
  13870. if the property key was used several times.
  13871. POM may also be nil, in which case the current entry is used.
  13872. If WHICH is nil or `all', get all properties. If WHICH is
  13873. `special' or `standard', only get that subclass. If WHICH
  13874. is a string only get exactly this property. SPECIFIC can be a string, the
  13875. specific property we are interested in. Specifying it can speed
  13876. things up because then unnecessary parsing is avoided."
  13877. (setq which (or which 'all))
  13878. (org-with-point-at pom
  13879. (let ((clockstr (substring org-clock-string 0 -1))
  13880. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  13881. (case-fold-search nil)
  13882. beg end range props sum-props key key1 value string clocksum clocksumt)
  13883. (save-excursion
  13884. (when (condition-case nil
  13885. (and (derived-mode-p 'org-mode) (org-back-to-heading t))
  13886. (error nil))
  13887. (setq beg (point))
  13888. (setq sum-props (get-text-property (point) 'org-summaries))
  13889. (setq clocksum (get-text-property (point) :org-clock-minutes)
  13890. clocksumt (get-text-property (point) :org-clock-minutes-today))
  13891. (outline-next-heading)
  13892. (setq end (point))
  13893. (when (memq which '(all special))
  13894. ;; Get the special properties, like TODO and tags
  13895. (goto-char beg)
  13896. (when (and (or (not specific) (string= specific "TODO"))
  13897. (looking-at org-todo-line-regexp) (match-end 2))
  13898. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  13899. (when (and (or (not specific) (string= specific "PRIORITY"))
  13900. (looking-at org-priority-regexp))
  13901. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  13902. (when (or (not specific) (string= specific "FILE"))
  13903. (push (cons "FILE" buffer-file-name) props))
  13904. (when (and (or (not specific) (string= specific "TAGS"))
  13905. (setq value (org-get-tags-string))
  13906. (string-match "\\S-" value))
  13907. (push (cons "TAGS" value) props))
  13908. (when (and (or (not specific) (string= specific "ALLTAGS"))
  13909. (setq value (org-get-tags-at)))
  13910. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  13911. ":"))
  13912. props))
  13913. (when (or (not specific) (string= specific "BLOCKED"))
  13914. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  13915. (when (or (not specific)
  13916. (member specific
  13917. '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
  13918. "TIMESTAMP" "TIMESTAMP_IA")))
  13919. (catch 'match
  13920. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  13921. (setq key (if (match-end 1)
  13922. (substring (org-match-string-no-properties 1)
  13923. 0 -1))
  13924. string (if (equal key clockstr)
  13925. (org-trim
  13926. (buffer-substring-no-properties
  13927. (match-beginning 3) (goto-char
  13928. (point-at-eol))))
  13929. (substring (org-match-string-no-properties 3)
  13930. 1 -1)))
  13931. ;; Get the correct property name from the key. This is
  13932. ;; necessary if the user has configured time keywords.
  13933. (setq key1 (concat key ":"))
  13934. (cond
  13935. ((not key)
  13936. (setq key
  13937. (if (= (char-after (match-beginning 3)) ?\[)
  13938. "TIMESTAMP_IA" "TIMESTAMP")))
  13939. ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
  13940. ((equal key1 org-deadline-string) (setq key "DEADLINE"))
  13941. ((equal key1 org-closed-string) (setq key "CLOSED"))
  13942. ((equal key1 org-clock-string) (setq key "CLOCK")))
  13943. (if (and specific (equal key specific) (not (equal key "CLOCK")))
  13944. (progn
  13945. (push (cons key string) props)
  13946. ;; no need to search further if match is found
  13947. (throw 'match t))
  13948. (when (or (equal key "CLOCK") (not (assoc key props)))
  13949. (push (cons key string) props)))))))
  13950. (when (memq which '(all standard))
  13951. ;; Get the standard properties, like :PROP: ...
  13952. (setq range (org-get-property-block beg end))
  13953. (when range
  13954. (goto-char (car range))
  13955. (while (re-search-forward org-property-re
  13956. (cdr range) t)
  13957. (setq key (org-match-string-no-properties 2)
  13958. value (org-trim (or (org-match-string-no-properties 3) "")))
  13959. (unless (member key excluded)
  13960. (push (cons key (or value "")) props)))))
  13961. (if clocksum
  13962. (push (cons "CLOCKSUM"
  13963. (org-columns-number-to-string (/ (float clocksum) 60.)
  13964. 'add_times))
  13965. props))
  13966. (if clocksumt
  13967. (push (cons "CLOCKSUM_T"
  13968. (org-columns-number-to-string (/ (float clocksumt) 60.)
  13969. 'add_times))
  13970. props))
  13971. (unless (assoc "CATEGORY" props)
  13972. (push (cons "CATEGORY" (org-get-category)) props))
  13973. (append sum-props (nreverse props)))))))
  13974. (defun org-entry-get (pom property &optional inherit literal-nil)
  13975. "Get value of PROPERTY for entry or content at point-or-marker POM.
  13976. If INHERIT is non-nil and the entry does not have the property,
  13977. then also check higher levels of the hierarchy.
  13978. If INHERIT is the symbol `selective', use inheritance only if the setting
  13979. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  13980. If the property is present but empty, the return value is the empty string.
  13981. If the property is not present at all, nil is returned.
  13982. If LITERAL-NIL is set, return the string value \"nil\" as a string,
  13983. do not interpret it as the list atom nil. This is used for inheritance
  13984. when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
  13985. (org-with-point-at pom
  13986. (if (and inherit (if (eq inherit 'selective)
  13987. (org-property-inherit-p property)
  13988. t))
  13989. (org-entry-get-with-inheritance property literal-nil)
  13990. (if (member property org-special-properties)
  13991. ;; We need a special property. Use `org-entry-properties' to
  13992. ;; retrieve it, but specify the wanted property
  13993. (cdr (assoc property (org-entry-properties nil 'special property)))
  13994. (let ((range (org-get-property-block)))
  13995. (when (and range (not (eq (car range) (cdr range))))
  13996. (let* ((props (list (or (assoc property org-file-properties)
  13997. (assoc property org-global-properties)
  13998. (assoc property org-global-properties-fixed))))
  13999. (ap (lambda (key)
  14000. (when (re-search-forward
  14001. (org-re-property key) (cdr range) t)
  14002. (setq props
  14003. (org-update-property-plist
  14004. key
  14005. (if (match-end 1)
  14006. (org-match-string-no-properties 1) "")
  14007. props)))))
  14008. val)
  14009. (goto-char (car range))
  14010. (funcall ap property)
  14011. (goto-char (car range))
  14012. (while (funcall ap (concat property "+")))
  14013. (setq val (cdr (assoc property props)))
  14014. (when val (if literal-nil val (org-not-nil val))))))))))
  14015. (defun org-property-or-variable-value (var &optional inherit)
  14016. "Check if there is a property fixing the value of VAR.
  14017. If yes, return this value. If not, return the current value of the variable."
  14018. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  14019. (if (and prop (stringp prop) (string-match "\\S-" prop))
  14020. (read prop)
  14021. (symbol-value var))))
  14022. (defun org-entry-delete (pom property &optional delete-empty-drawer)
  14023. "Delete the property PROPERTY from entry at point-or-marker POM.
  14024. When optional argument DELETE-EMPTY-DRAWER is a string, it defines
  14025. an empty drawer to delete."
  14026. (org-with-point-at pom
  14027. (if (member property org-special-properties)
  14028. nil ; cannot delete these properties.
  14029. (let ((range (org-get-property-block)))
  14030. (if (and range
  14031. (goto-char (car range))
  14032. (re-search-forward
  14033. (org-re-property property)
  14034. (cdr range) t))
  14035. (progn
  14036. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  14037. (and delete-empty-drawer
  14038. (org-remove-empty-drawer-at
  14039. delete-empty-drawer (car range)))
  14040. t)
  14041. nil)))))
  14042. ;; Multi-values properties are properties that contain multiple values
  14043. ;; These values are assumed to be single words, separated by whitespace.
  14044. (defun org-entry-add-to-multivalued-property (pom property value)
  14045. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  14046. (let* ((old (org-entry-get pom property))
  14047. (values (and old (org-split-string old "[ \t]"))))
  14048. (setq value (org-entry-protect-space value))
  14049. (unless (member value values)
  14050. (setq values (append values (list value)))
  14051. (org-entry-put pom property
  14052. (mapconcat 'identity values " ")))))
  14053. (defun org-entry-remove-from-multivalued-property (pom property value)
  14054. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  14055. (let* ((old (org-entry-get pom property))
  14056. (values (and old (org-split-string old "[ \t]"))))
  14057. (setq value (org-entry-protect-space value))
  14058. (when (member value values)
  14059. (setq values (delete value values))
  14060. (org-entry-put pom property
  14061. (mapconcat 'identity values " ")))))
  14062. (defun org-entry-member-in-multivalued-property (pom property value)
  14063. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  14064. (let* ((old (org-entry-get pom property))
  14065. (values (and old (org-split-string old "[ \t]"))))
  14066. (setq value (org-entry-protect-space value))
  14067. (member value values)))
  14068. (defun org-entry-get-multivalued-property (pom property)
  14069. "Return a list of values in a multivalued property."
  14070. (let* ((value (org-entry-get pom property))
  14071. (values (and value (org-split-string value "[ \t]"))))
  14072. (mapcar 'org-entry-restore-space values)))
  14073. (defun org-entry-put-multivalued-property (pom property &rest values)
  14074. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  14075. VALUES should be a list of strings. Spaces will be protected."
  14076. (org-entry-put pom property
  14077. (mapconcat 'org-entry-protect-space values " "))
  14078. (let* ((value (org-entry-get pom property))
  14079. (values (and value (org-split-string value "[ \t]"))))
  14080. (mapcar 'org-entry-restore-space values)))
  14081. (defun org-entry-protect-space (s)
  14082. "Protect spaces and newline in string S."
  14083. (while (string-match " " s)
  14084. (setq s (replace-match "%20" t t s)))
  14085. (while (string-match "\n" s)
  14086. (setq s (replace-match "%0A" t t s)))
  14087. s)
  14088. (defun org-entry-restore-space (s)
  14089. "Restore spaces and newline in string S."
  14090. (while (string-match "%20" s)
  14091. (setq s (replace-match " " t t s)))
  14092. (while (string-match "%0A" s)
  14093. (setq s (replace-match "\n" t t s)))
  14094. s)
  14095. (defvar org-entry-property-inherited-from (make-marker)
  14096. "Marker pointing to the entry from where a property was inherited.
  14097. Each call to `org-entry-get-with-inheritance' will set this marker to the
  14098. location of the entry where the inheritance search matched. If there was
  14099. no match, the marker will point nowhere.
  14100. Note that also `org-entry-get' calls this function, if the INHERIT flag
  14101. is set.")
  14102. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  14103. "Get PROPERTY of entry or content at point, search higher levels if needed.
  14104. The search will stop at the first ancestor which has the property defined.
  14105. If the value found is \"nil\", return nil to show that the property
  14106. should be considered as undefined (this is the meaning of nil here).
  14107. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  14108. (move-marker org-entry-property-inherited-from nil)
  14109. (let (tmp)
  14110. (save-excursion
  14111. (save-restriction
  14112. (widen)
  14113. (catch 'ex
  14114. (while t
  14115. (when (setq tmp (org-entry-get nil property nil 'literal-nil))
  14116. (or (ignore-errors (org-back-to-heading t))
  14117. (goto-char (point-min)))
  14118. (move-marker org-entry-property-inherited-from (point))
  14119. (throw 'ex tmp))
  14120. (or (ignore-errors (org-up-heading-safe))
  14121. (throw 'ex nil))))))
  14122. (setq tmp (or tmp
  14123. (cdr (assoc property org-file-properties))
  14124. (cdr (assoc property org-global-properties))
  14125. (cdr (assoc property org-global-properties-fixed))))
  14126. (if literal-nil tmp (org-not-nil tmp))))
  14127. (defvar org-property-changed-functions nil
  14128. "Hook called when the value of a property has changed.
  14129. Each hook function should accept two arguments, the name of the property
  14130. and the new value.")
  14131. (defun org-entry-put (pom property value)
  14132. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  14133. (org-with-point-at pom
  14134. (org-back-to-heading t)
  14135. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  14136. range)
  14137. (cond
  14138. ((equal property "TODO")
  14139. (when (and (stringp value) (string-match "\\S-" value)
  14140. (not (member value org-todo-keywords-1)))
  14141. (user-error "\"%s\" is not a valid TODO state" value))
  14142. (if (or (not value)
  14143. (not (string-match "\\S-" value)))
  14144. (setq value 'none))
  14145. (org-todo value)
  14146. (org-set-tags nil 'align))
  14147. ((equal property "PRIORITY")
  14148. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  14149. (string-to-char value) ?\ ))
  14150. (org-set-tags nil 'align))
  14151. ((equal property "CLOCKSUM")
  14152. (if (not (re-search-forward
  14153. (concat org-clock-string ".*\\]--\\(\\[[^]]+\\]\\)") nil t))
  14154. (error "Cannot find a clock log")
  14155. (goto-char (- (match-end 1) 2))
  14156. (cond
  14157. ((eq value 'earlier) (org-timestamp-down))
  14158. ((eq value 'later) (org-timestamp-up)))
  14159. (org-clock-sum-current-item)))
  14160. ((equal property "SCHEDULED")
  14161. (if (re-search-forward org-scheduled-time-regexp end t)
  14162. (cond
  14163. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14164. ((eq value 'later) (org-timestamp-change 1 'day))
  14165. (t (call-interactively 'org-schedule)))
  14166. (call-interactively 'org-schedule)))
  14167. ((equal property "DEADLINE")
  14168. (if (re-search-forward org-deadline-time-regexp end t)
  14169. (cond
  14170. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14171. ((eq value 'later) (org-timestamp-change 1 'day))
  14172. (t (call-interactively 'org-deadline)))
  14173. (call-interactively 'org-deadline)))
  14174. ((member property org-special-properties)
  14175. (error "The %s property can not yet be set with `org-entry-put'"
  14176. property))
  14177. (t ; a non-special property
  14178. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  14179. (setq range (org-get-property-block beg end 'force))
  14180. (goto-char (car range))
  14181. (if (re-search-forward
  14182. (org-re-property-keyword property) (cdr range) t)
  14183. (progn
  14184. (delete-region (match-beginning 0) (match-end 0))
  14185. (goto-char (match-beginning 0)))
  14186. (goto-char (cdr range))
  14187. (insert "\n")
  14188. (backward-char 1)
  14189. (org-indent-line))
  14190. (insert ":" property ":")
  14191. (and value (insert " " value))
  14192. (org-indent-line)))))
  14193. (run-hook-with-args 'org-property-changed-functions property value)))
  14194. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  14195. "Get all property keys in the current buffer.
  14196. With INCLUDE-SPECIALS, also list the special properties that reflect things
  14197. like tags and TODO state.
  14198. With INCLUDE-DEFAULTS, also include properties that has special meaning
  14199. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
  14200. and others.
  14201. With INCLUDE-COLUMNS, also include property names given in COLUMN
  14202. formats in the current buffer."
  14203. (let (rtn range cfmt s p)
  14204. (save-excursion
  14205. (save-restriction
  14206. (widen)
  14207. (goto-char (point-min))
  14208. (while (re-search-forward org-property-start-re nil t)
  14209. (setq range (org-get-property-block))
  14210. (goto-char (car range))
  14211. (while (re-search-forward org-property-re
  14212. (cdr range) t)
  14213. (add-to-list 'rtn (org-match-string-no-properties 2)))
  14214. (outline-next-heading))))
  14215. (when include-specials
  14216. (setq rtn (append org-special-properties rtn)))
  14217. (when include-defaults
  14218. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  14219. (add-to-list 'rtn org-effort-property))
  14220. (when include-columns
  14221. (save-excursion
  14222. (save-restriction
  14223. (widen)
  14224. (goto-char (point-min))
  14225. (while (re-search-forward
  14226. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  14227. nil t)
  14228. (setq cfmt (match-string 2) s 0)
  14229. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  14230. cfmt s)
  14231. (setq s (match-end 0)
  14232. p (match-string 1 cfmt))
  14233. (unless (or (equal p "ITEM")
  14234. (member p org-special-properties))
  14235. (add-to-list 'rtn (match-string 1 cfmt))))))))
  14236. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  14237. (defun org-property-values (key)
  14238. "Return a list of all values of property KEY in the current buffer."
  14239. (save-excursion
  14240. (save-restriction
  14241. (widen)
  14242. (goto-char (point-min))
  14243. (let ((re (org-re-property key))
  14244. values)
  14245. (while (re-search-forward re nil t)
  14246. (add-to-list 'values (org-trim (match-string 1))))
  14247. (delete "" values)))))
  14248. (defun org-insert-property-drawer ()
  14249. "Insert a property drawer into the current entry."
  14250. (org-back-to-heading t)
  14251. (looking-at org-outline-regexp)
  14252. (let ((indent (if org-adapt-indentation
  14253. (- (match-end 0) (match-beginning 0))
  14254. 0))
  14255. (beg (point))
  14256. (re (concat "^[ \t]*" org-keyword-time-regexp))
  14257. end hiddenp)
  14258. (outline-next-heading)
  14259. (setq end (point))
  14260. (goto-char beg)
  14261. (while (re-search-forward re end t))
  14262. (setq hiddenp (outline-invisible-p))
  14263. (end-of-line 1)
  14264. (and (equal (char-after) ?\n) (forward-char 1))
  14265. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  14266. (if (member (match-string 1) '("CLOCK:" ":END:"))
  14267. ;; just skip this line
  14268. (beginning-of-line 2)
  14269. ;; Drawer start, find the end
  14270. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  14271. (beginning-of-line 1)))
  14272. (org-skip-over-state-notes)
  14273. (skip-chars-backward " \t\n\r")
  14274. (if (eq (char-before) ?*) (forward-char 1))
  14275. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  14276. (beginning-of-line 0)
  14277. (org-indent-to-column indent)
  14278. (beginning-of-line 2)
  14279. (org-indent-to-column indent)
  14280. (beginning-of-line 0)
  14281. (if hiddenp
  14282. (save-excursion
  14283. (org-back-to-heading t)
  14284. (hide-entry))
  14285. (org-flag-drawer t))))
  14286. (defun org-insert-drawer (&optional arg drawer)
  14287. "Insert a drawer at point.
  14288. Optional argument DRAWER, when non-nil, is a string representing
  14289. drawer's name. Otherwise, the user is prompted for a name.
  14290. If a region is active, insert the drawer around that region
  14291. instead.
  14292. Point is left between drawer's boundaries."
  14293. (interactive "P")
  14294. (let* ((logbook (if (stringp org-log-into-drawer) org-log-into-drawer
  14295. "LOGBOOK"))
  14296. ;; SYSTEM-DRAWERS is a list of drawer names that are used
  14297. ;; internally by Org. They are meant to be inserted
  14298. ;; automatically.
  14299. (system-drawers `("CLOCK" ,logbook "PROPERTIES"))
  14300. ;; Remove system drawers from list. Note: For some reason,
  14301. ;; `org-completing-read' ignores the predicate while
  14302. ;; `completing-read' handles it fine.
  14303. (drawer (if arg "PROPERTIES"
  14304. (or drawer
  14305. (completing-read
  14306. "Drawer: " org-drawers
  14307. (lambda (d) (not (member d system-drawers))))))))
  14308. (cond
  14309. ;; With C-u, fall back on `org-insert-property-drawer'
  14310. (arg (org-insert-property-drawer))
  14311. ;; With an active region, insert a drawer at point.
  14312. ((not (org-region-active-p))
  14313. (progn
  14314. (unless (bolp) (insert "\n"))
  14315. (insert (format ":%s:\n\n:END:\n" drawer))
  14316. (forward-line -2)))
  14317. ;; Otherwise, insert the drawer at point
  14318. (t
  14319. (let ((rbeg (region-beginning))
  14320. (rend (copy-marker (region-end))))
  14321. (unwind-protect
  14322. (progn
  14323. (goto-char rbeg)
  14324. (beginning-of-line)
  14325. (when (save-excursion
  14326. (re-search-forward org-outline-regexp-bol rend t))
  14327. (user-error "Drawers cannot contain headlines"))
  14328. ;; Position point at the beginning of the first
  14329. ;; non-blank line in region. Insert drawer's opening
  14330. ;; there, then indent it.
  14331. (org-skip-whitespace)
  14332. (beginning-of-line)
  14333. (insert ":" drawer ":\n")
  14334. (forward-line -1)
  14335. (indent-for-tab-command)
  14336. ;; Move point to the beginning of the first blank line
  14337. ;; after the last non-blank line in region. Insert
  14338. ;; drawer's closing, then indent it.
  14339. (goto-char rend)
  14340. (skip-chars-backward " \r\t\n")
  14341. (insert "\n:END:")
  14342. (deactivate-mark t)
  14343. (indent-for-tab-command)
  14344. (unless (eolp) (insert "\n")))
  14345. ;; Clear marker, whatever the outcome of insertion is.
  14346. (set-marker rend nil)))))))
  14347. (defvar org-property-set-functions-alist nil
  14348. "Property set function alist.
  14349. Each entry should have the following format:
  14350. (PROPERTY . READ-FUNCTION)
  14351. The read function will be called with the same argument as
  14352. `org-completing-read'.")
  14353. (defun org-set-property-function (property)
  14354. "Get the function that should be used to set PROPERTY.
  14355. This is computed according to `org-property-set-functions-alist'."
  14356. (or (cdr (assoc property org-property-set-functions-alist))
  14357. 'org-completing-read))
  14358. (defun org-read-property-value (property)
  14359. "Read PROPERTY value from user."
  14360. (let* ((completion-ignore-case t)
  14361. (allowed (org-property-get-allowed-values nil property 'table))
  14362. (cur (org-entry-get nil property))
  14363. (prompt (concat property " value"
  14364. (if (and cur (string-match "\\S-" cur))
  14365. (concat " [" cur "]") "") ": "))
  14366. (set-function (org-set-property-function property))
  14367. (val (if allowed
  14368. (funcall set-function prompt allowed nil
  14369. (not (get-text-property 0 'org-unrestricted
  14370. (caar allowed))))
  14371. (let (org-completion-use-ido org-completion-use-iswitchb)
  14372. (funcall set-function prompt
  14373. (mapcar 'list (org-property-values property))
  14374. nil nil "" nil cur)))))
  14375. (if (equal val "")
  14376. cur
  14377. val)))
  14378. (defvar org-last-set-property nil)
  14379. (defvar org-last-set-property-value nil)
  14380. (defun org-read-property-name ()
  14381. "Read a property name."
  14382. (let* ((completion-ignore-case t)
  14383. (keys (org-buffer-property-keys nil t t))
  14384. (default-prop (or (save-excursion
  14385. (save-match-data
  14386. (beginning-of-line)
  14387. (and (looking-at "^\\s-*:\\([^:\n]+\\):")
  14388. (null (string= (match-string 1) "END"))
  14389. (match-string 1))))
  14390. org-last-set-property))
  14391. (property (org-icompleting-read
  14392. (concat "Property"
  14393. (if default-prop (concat " [" default-prop "]") "")
  14394. ": ")
  14395. (mapcar 'list keys)
  14396. nil nil nil nil
  14397. default-prop)))
  14398. (if (member property keys)
  14399. property
  14400. (or (cdr (assoc (downcase property)
  14401. (mapcar (lambda (x) (cons (downcase x) x))
  14402. keys)))
  14403. property))))
  14404. (defun org-set-property-and-value (use-last)
  14405. "Allow to set [PROPERTY]: [value] direction from prompt.
  14406. When use-default, don't even ask, just use the last
  14407. \"[PROPERTY]: [value]\" string from the history."
  14408. (interactive "P")
  14409. (let* ((completion-ignore-case t)
  14410. (pv (or (and use-last org-last-set-property-value)
  14411. (org-completing-read
  14412. "Enter a \"[Property]: [value]\" pair: "
  14413. nil nil nil nil nil
  14414. org-last-set-property-value)))
  14415. prop val)
  14416. (when (string-match "^[ \t]*\\([^:]+\\):[ \t]*\\(.*\\)[ \t]*$" pv)
  14417. (setq prop (match-string 1 pv)
  14418. val (match-string 2 pv))
  14419. (org-set-property prop val))))
  14420. (defun org-set-property (property value)
  14421. "In the current entry, set PROPERTY to VALUE.
  14422. When called interactively, this will prompt for a property name, offering
  14423. completion on existing and default properties. And then it will prompt
  14424. for a value, offering completion either on allowed values (via an inherited
  14425. xxx_ALL property) or on existing values in other instances of this property
  14426. in the current file."
  14427. (interactive (list nil nil))
  14428. (let* ((property (or property (org-read-property-name)))
  14429. (value (or value (org-read-property-value property)))
  14430. (fn (cdr (assoc property org-properties-postprocess-alist))))
  14431. (setq org-last-set-property property)
  14432. (setq org-last-set-property-value (concat property ": " value))
  14433. ;; Possibly postprocess the inserted value:
  14434. (when fn (setq value (funcall fn value)))
  14435. (unless (equal (org-entry-get nil property) value)
  14436. (org-entry-put nil property value))))
  14437. (defun org-delete-property (property &optional delete-empty-drawer)
  14438. "In the current entry, delete PROPERTY.
  14439. When optional argument DELETE-EMPTY-DRAWER is a string, it defines
  14440. an empty drawer to delete."
  14441. (interactive
  14442. (let* ((completion-ignore-case t)
  14443. (prop (org-icompleting-read "Property: "
  14444. (org-entry-properties nil 'standard))))
  14445. (list prop)))
  14446. (message "Property %s %s" property
  14447. (if (org-entry-delete nil property delete-empty-drawer)
  14448. "deleted"
  14449. "was not present in the entry")))
  14450. (defun org-delete-property-globally (property)
  14451. "Remove PROPERTY globally, from all entries."
  14452. (interactive
  14453. (let* ((completion-ignore-case t)
  14454. (prop (org-icompleting-read
  14455. "Globally remove property: "
  14456. (mapcar 'list (org-buffer-property-keys)))))
  14457. (list prop)))
  14458. (save-excursion
  14459. (save-restriction
  14460. (widen)
  14461. (goto-char (point-min))
  14462. (let ((cnt 0))
  14463. (while (re-search-forward
  14464. (org-re-property property)
  14465. nil t)
  14466. (setq cnt (1+ cnt))
  14467. (delete-region (match-beginning 0) (1+ (point-at-eol))))
  14468. (message "Property \"%s\" removed from %d entries" property cnt)))))
  14469. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  14470. (defun org-compute-property-at-point ()
  14471. "Compute the property at point.
  14472. This looks for an enclosing column format, extracts the operator and
  14473. then applies it to the property in the column format's scope."
  14474. (interactive)
  14475. (unless (org-at-property-p)
  14476. (user-error "Not at a property"))
  14477. (let ((prop (org-match-string-no-properties 2)))
  14478. (org-columns-get-format-and-top-level)
  14479. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  14480. (user-error "No operator defined for property %s" prop))
  14481. (org-columns-compute prop)))
  14482. (defvar org-property-allowed-value-functions nil
  14483. "Hook for functions supplying allowed values for a specific property.
  14484. The functions must take a single argument, the name of the property, and
  14485. return a flat list of allowed values. If \":ETC\" is one of
  14486. the values, this means that these values are intended as defaults for
  14487. completion, but that other values should be allowed too.
  14488. The functions must return nil if they are not responsible for this
  14489. property.")
  14490. (defun org-property-get-allowed-values (pom property &optional table)
  14491. "Get allowed values for the property PROPERTY.
  14492. When TABLE is non-nil, return an alist that can directly be used for
  14493. completion."
  14494. (let (vals)
  14495. (cond
  14496. ((equal property "TODO")
  14497. (setq vals (org-with-point-at pom
  14498. (append org-todo-keywords-1 '("")))))
  14499. ((equal property "PRIORITY")
  14500. (let ((n org-lowest-priority))
  14501. (while (>= n org-highest-priority)
  14502. (push (char-to-string n) vals)
  14503. (setq n (1- n)))))
  14504. ((member property org-special-properties))
  14505. ((setq vals (run-hook-with-args-until-success
  14506. 'org-property-allowed-value-functions property)))
  14507. (t
  14508. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  14509. (when (and vals (string-match "\\S-" vals))
  14510. (setq vals (car (read-from-string (concat "(" vals ")"))))
  14511. (setq vals (mapcar (lambda (x)
  14512. (cond ((stringp x) x)
  14513. ((numberp x) (number-to-string x))
  14514. ((symbolp x) (symbol-name x))
  14515. (t "???")))
  14516. vals)))))
  14517. (when (member ":ETC" vals)
  14518. (setq vals (remove ":ETC" vals))
  14519. (org-add-props (car vals) '(org-unrestricted t)))
  14520. (if table (mapcar 'list vals) vals)))
  14521. (defun org-property-previous-allowed-value (&optional previous)
  14522. "Switch to the next allowed value for this property."
  14523. (interactive)
  14524. (org-property-next-allowed-value t))
  14525. (defun org-property-next-allowed-value (&optional previous)
  14526. "Switch to the next allowed value for this property."
  14527. (interactive)
  14528. (unless (org-at-property-p)
  14529. (user-error "Not at a property"))
  14530. (let* ((prop (car (save-match-data (org-split-string (match-string 1) ":"))))
  14531. (key (match-string 2))
  14532. (value (match-string 3))
  14533. (allowed (or (org-property-get-allowed-values (point) key)
  14534. (and (member value '("[ ]" "[-]" "[X]"))
  14535. '("[ ]" "[X]"))))
  14536. (heading (save-match-data (nth 4 (org-heading-components))))
  14537. nval)
  14538. (unless allowed
  14539. (user-error "Allowed values for this property have not been defined"))
  14540. (if previous (setq allowed (reverse allowed)))
  14541. (if (member value allowed)
  14542. (setq nval (car (cdr (member value allowed)))))
  14543. (setq nval (or nval (car allowed)))
  14544. (if (equal nval value)
  14545. (user-error "Only one allowed value for this property"))
  14546. (org-at-property-p)
  14547. (replace-match (concat " :" key ": " nval) t t)
  14548. (org-indent-line)
  14549. (beginning-of-line 1)
  14550. (skip-chars-forward " \t")
  14551. (when (equal prop org-effort-property)
  14552. (save-excursion
  14553. (org-back-to-heading t)
  14554. (put-text-property (point-at-bol) (point-at-eol) 'org-effort nval))
  14555. (when (string= org-clock-current-task heading)
  14556. (setq org-clock-effort nval)
  14557. (org-clock-update-mode-line)))
  14558. (run-hook-with-args 'org-property-changed-functions key nval)))
  14559. (defun org-find-olp (path &optional this-buffer)
  14560. "Return a marker pointing to the entry at outline path OLP.
  14561. If anything goes wrong, throw an error.
  14562. You can wrap this call to catch the error like this:
  14563. (condition-case msg
  14564. (org-mobile-locate-entry (match-string 4))
  14565. (error (nth 1 msg)))
  14566. The return value will then be either a string with the error message,
  14567. or a marker if everything is OK.
  14568. If THIS-BUFFER is set, the outline path does not contain a file,
  14569. only headings."
  14570. (let* ((file (if this-buffer buffer-file-name (pop path)))
  14571. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  14572. (level 1)
  14573. (lmin 1)
  14574. (lmax 1)
  14575. limit re end found pos heading cnt flevel)
  14576. (unless buffer (error "File not found :%s" file))
  14577. (with-current-buffer buffer
  14578. (save-excursion
  14579. (save-restriction
  14580. (widen)
  14581. (setq limit (point-max))
  14582. (goto-char (point-min))
  14583. (while (setq heading (pop path))
  14584. (setq re (format org-complex-heading-regexp-format
  14585. (regexp-quote heading)))
  14586. (setq cnt 0 pos (point))
  14587. (while (re-search-forward re end t)
  14588. (setq level (- (match-end 1) (match-beginning 1)))
  14589. (if (and (>= level lmin) (<= level lmax))
  14590. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  14591. (when (= cnt 0) (error "Heading not found on level %d: %s"
  14592. lmax heading))
  14593. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  14594. lmax heading))
  14595. (goto-char found)
  14596. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  14597. (setq end (save-excursion (org-end-of-subtree t t))))
  14598. (when (org-at-heading-p)
  14599. (point-marker)))))))
  14600. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  14601. "Find node HEADING in BUFFER.
  14602. Return a marker to the heading if it was found, or nil if not.
  14603. If POS-ONLY is set, return just the position instead of a marker.
  14604. The heading text must match exact, but it may have a TODO keyword,
  14605. a priority cookie and tags in the standard locations."
  14606. (with-current-buffer (or buffer (current-buffer))
  14607. (save-excursion
  14608. (save-restriction
  14609. (widen)
  14610. (goto-char (point-min))
  14611. (let (case-fold-search)
  14612. (if (re-search-forward
  14613. (format org-complex-heading-regexp-format
  14614. (regexp-quote heading)) nil t)
  14615. (if pos-only
  14616. (match-beginning 0)
  14617. (move-marker (make-marker) (match-beginning 0)))))))))
  14618. (defun org-find-exact-heading-in-directory (heading &optional dir)
  14619. "Find Org node headline HEADING in all .org files in directory DIR.
  14620. When the target headline is found, return a marker to this location."
  14621. (let ((files (directory-files (or dir default-directory)
  14622. nil "\\`[^.#].*\\.org\\'"))
  14623. file visiting m buffer)
  14624. (catch 'found
  14625. (while (setq file (pop files))
  14626. (message "trying %s" file)
  14627. (setq visiting (org-find-base-buffer-visiting file))
  14628. (setq buffer (or visiting (find-file-noselect file)))
  14629. (setq m (org-find-exact-headline-in-buffer
  14630. heading buffer))
  14631. (when (and (not m) (not visiting)) (kill-buffer buffer))
  14632. (and m (throw 'found m))))))
  14633. (defun org-find-entry-with-id (ident)
  14634. "Locate the entry that contains the ID property with exact value IDENT.
  14635. IDENT can be a string, a symbol or a number, this function will search for
  14636. the string representation of it.
  14637. Return the position where this entry starts, or nil if there is no such entry."
  14638. (interactive "sID: ")
  14639. (let ((id (cond
  14640. ((stringp ident) ident)
  14641. ((symbol-name ident) (symbol-name ident))
  14642. ((numberp ident) (number-to-string ident))
  14643. (t (error "IDENT %s must be a string, symbol or number" ident))))
  14644. (case-fold-search nil))
  14645. (save-excursion
  14646. (save-restriction
  14647. (widen)
  14648. (goto-char (point-min))
  14649. (when (re-search-forward
  14650. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  14651. nil t)
  14652. (org-back-to-heading t)
  14653. (point))))))
  14654. ;;;; Timestamps
  14655. (defvar org-last-changed-timestamp nil)
  14656. (defvar org-last-inserted-timestamp nil
  14657. "The last time stamp inserted with `org-insert-time-stamp'.")
  14658. (defvar org-time-was-given) ; dynamically scoped parameter
  14659. (defvar org-end-time-was-given) ; dynamically scoped parameter
  14660. (defvar org-ts-what) ; dynamically scoped parameter
  14661. (defun org-time-stamp (arg &optional inactive)
  14662. "Prompt for a date/time and insert a time stamp.
  14663. If the user specifies a time like HH:MM or if this command is
  14664. called with at least one prefix argument, the time stamp contains
  14665. the date and the time. Otherwise, only the date is be included.
  14666. All parts of a date not specified by the user is filled in from
  14667. the current date/time. So if you just press return without
  14668. typing anything, the time stamp will represent the current
  14669. date/time.
  14670. If there is already a timestamp at the cursor, it will be
  14671. modified.
  14672. With two universal prefix arguments, insert an active timestamp
  14673. with the current time without prompting the user."
  14674. (interactive "P")
  14675. (let* ((ts nil)
  14676. (default-time
  14677. ;; Default time is either today, or, when entering a range,
  14678. ;; the range start.
  14679. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  14680. (save-excursion
  14681. (re-search-backward
  14682. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  14683. (- (point) 20) t)))
  14684. (apply 'encode-time (org-parse-time-string (match-string 1)))
  14685. (current-time)))
  14686. (default-input (and ts (org-get-compact-tod ts)))
  14687. (repeater (save-excursion
  14688. (save-match-data
  14689. (beginning-of-line)
  14690. (when (re-search-forward
  14691. "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  14692. (save-excursion (progn (end-of-line) (point))) t)
  14693. (match-string 0)))))
  14694. org-time-was-given org-end-time-was-given time)
  14695. (cond
  14696. ((and (org-at-timestamp-p t)
  14697. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  14698. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  14699. (insert "--")
  14700. (setq time (let ((this-command this-command))
  14701. (org-read-date arg 'totime nil nil
  14702. default-time default-input inactive)))
  14703. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  14704. ((org-at-timestamp-p t)
  14705. (setq time (let ((this-command this-command))
  14706. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14707. (when (org-at-timestamp-p t) ; just to get the match data
  14708. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  14709. (replace-match "")
  14710. (setq org-last-changed-timestamp
  14711. (org-insert-time-stamp
  14712. time (or org-time-was-given arg)
  14713. inactive nil nil (list org-end-time-was-given)))
  14714. (when repeater (goto-char (1- (point))) (insert " " repeater)
  14715. (setq org-last-changed-timestamp
  14716. (concat (substring org-last-inserted-timestamp 0 -1)
  14717. " " repeater ">"))))
  14718. (message "Timestamp updated"))
  14719. ((equal arg '(16))
  14720. (org-insert-time-stamp (current-time) t))
  14721. (t
  14722. (setq time (let ((this-command this-command))
  14723. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14724. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  14725. nil nil (list org-end-time-was-given))))))
  14726. ;; FIXME: can we use this for something else, like computing time differences?
  14727. (defun org-get-compact-tod (s)
  14728. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  14729. (let* ((t1 (match-string 1 s))
  14730. (h1 (string-to-number (match-string 2 s)))
  14731. (m1 (string-to-number (match-string 3 s)))
  14732. (t2 (and (match-end 4) (match-string 5 s)))
  14733. (h2 (and t2 (string-to-number (match-string 6 s))))
  14734. (m2 (and t2 (string-to-number (match-string 7 s))))
  14735. dh dm)
  14736. (if (not t2)
  14737. t1
  14738. (setq dh (- h2 h1) dm (- m2 m1))
  14739. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  14740. (concat t1 "+" (number-to-string dh)
  14741. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  14742. (defun org-time-stamp-inactive (&optional arg)
  14743. "Insert an inactive time stamp.
  14744. An inactive time stamp is enclosed in square brackets instead of angle
  14745. brackets. It is inactive in the sense that it does not trigger agenda entries,
  14746. does not link to the calendar and cannot be changed with the S-cursor keys.
  14747. So these are more for recording a certain time/date."
  14748. (interactive "P")
  14749. (org-time-stamp arg 'inactive))
  14750. (defvar org-date-ovl (make-overlay 1 1))
  14751. (overlay-put org-date-ovl 'face 'org-date-selected)
  14752. (org-detach-overlay org-date-ovl)
  14753. (defvar org-ans1) ; dynamically scoped parameter
  14754. (defvar org-ans2) ; dynamically scoped parameter
  14755. (defvar org-plain-time-of-day-regexp) ; defined below
  14756. (defvar org-overriding-default-time nil) ; dynamically scoped
  14757. (defvar org-read-date-overlay nil)
  14758. (defvar org-dcst nil) ; dynamically scoped
  14759. (defvar org-read-date-history nil)
  14760. (defvar org-read-date-final-answer nil)
  14761. (defvar org-read-date-analyze-futurep nil)
  14762. (defvar org-read-date-analyze-forced-year nil)
  14763. (defvar org-read-date-inactive)
  14764. (defvar org-read-date-minibuffer-local-map
  14765. (let ((map (make-sparse-keymap)))
  14766. (set-keymap-parent map minibuffer-local-map)
  14767. (org-defkey map (kbd ".")
  14768. (lambda () (interactive)
  14769. ;; Are we at the beginning of the prompt?
  14770. (if (looking-back "^[^:]+: ")
  14771. (org-eval-in-calendar '(calendar-goto-today))
  14772. (insert "."))))
  14773. (org-defkey map (kbd "C-.")
  14774. (lambda () (interactive)
  14775. (org-eval-in-calendar '(calendar-goto-today))))
  14776. (org-defkey map [(meta shift left)]
  14777. (lambda () (interactive)
  14778. (org-eval-in-calendar '(calendar-backward-month 1))))
  14779. (org-defkey map [(meta shift right)]
  14780. (lambda () (interactive)
  14781. (org-eval-in-calendar '(calendar-forward-month 1))))
  14782. (org-defkey map [(meta shift up)]
  14783. (lambda () (interactive)
  14784. (org-eval-in-calendar '(calendar-backward-year 1))))
  14785. (org-defkey map [(meta shift down)]
  14786. (lambda () (interactive)
  14787. (org-eval-in-calendar '(calendar-forward-year 1))))
  14788. (org-defkey map [?\e (shift left)]
  14789. (lambda () (interactive)
  14790. (org-eval-in-calendar '(calendar-backward-month 1))))
  14791. (org-defkey map [?\e (shift right)]
  14792. (lambda () (interactive)
  14793. (org-eval-in-calendar '(calendar-forward-month 1))))
  14794. (org-defkey map [?\e (shift up)]
  14795. (lambda () (interactive)
  14796. (org-eval-in-calendar '(calendar-backward-year 1))))
  14797. (org-defkey map [?\e (shift down)]
  14798. (lambda () (interactive)
  14799. (org-eval-in-calendar '(calendar-forward-year 1))))
  14800. (org-defkey map [(shift up)]
  14801. (lambda () (interactive)
  14802. (org-eval-in-calendar '(calendar-backward-week 1))))
  14803. (org-defkey map [(shift down)]
  14804. (lambda () (interactive)
  14805. (org-eval-in-calendar '(calendar-forward-week 1))))
  14806. (org-defkey map [(shift left)]
  14807. (lambda () (interactive)
  14808. (org-eval-in-calendar '(calendar-backward-day 1))))
  14809. (org-defkey map [(shift right)]
  14810. (lambda () (interactive)
  14811. (org-eval-in-calendar '(calendar-forward-day 1))))
  14812. (org-defkey map "!"
  14813. (lambda () (interactive)
  14814. (org-eval-in-calendar '(diary-view-entries))
  14815. (message "")))
  14816. (org-defkey map ">"
  14817. (lambda () (interactive)
  14818. (org-eval-in-calendar '(scroll-calendar-left 1))))
  14819. (org-defkey map "<"
  14820. (lambda () (interactive)
  14821. (org-eval-in-calendar '(scroll-calendar-right 1))))
  14822. (org-defkey map "\C-v"
  14823. (lambda () (interactive)
  14824. (org-eval-in-calendar
  14825. '(calendar-scroll-left-three-months 1))))
  14826. (org-defkey map "\M-v"
  14827. (lambda () (interactive)
  14828. (org-eval-in-calendar
  14829. '(calendar-scroll-right-three-months 1))))
  14830. map)
  14831. "Keymap for minibuffer commands when using `org-read-date'.")
  14832. (defun org-read-date (&optional org-with-time to-time from-string prompt
  14833. default-time default-input inactive)
  14834. "Read a date, possibly a time, and make things smooth for the user.
  14835. The prompt will suggest to enter an ISO date, but you can also enter anything
  14836. which will at least partially be understood by `parse-time-string'.
  14837. Unrecognized parts of the date will default to the current day, month, year,
  14838. hour and minute. If this command is called to replace a timestamp at point,
  14839. or to enter the second timestamp of a range, the default time is taken
  14840. from the existing stamp. Furthermore, the command prefers the future,
  14841. so if you are giving a date where the year is not given, and the day-month
  14842. combination is already past in the current year, it will assume you
  14843. mean next year. For details, see the manual. A few examples:
  14844. 3-2-5 --> 2003-02-05
  14845. feb 15 --> currentyear-02-15
  14846. 2/15 --> currentyear-02-15
  14847. sep 12 9 --> 2009-09-12
  14848. 12:45 --> today 12:45
  14849. 22 sept 0:34 --> currentyear-09-22 0:34
  14850. 12 --> currentyear-currentmonth-12
  14851. Fri --> nearest Friday after today
  14852. -Tue --> last Tuesday
  14853. etc.
  14854. Furthermore you can specify a relative date by giving, as the *first* thing
  14855. in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
  14856. change in days weeks, months, years.
  14857. With a single plus or minus, the date is relative to today. With a double
  14858. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  14859. +4d --> four days from today
  14860. +4 --> same as above
  14861. +2w --> two weeks from today
  14862. ++5 --> five days from default date
  14863. The function understands only English month and weekday abbreviations.
  14864. While prompting, a calendar is popped up - you can also select the
  14865. date with the mouse (button 1). The calendar shows a period of three
  14866. months. To scroll it to other months, use the keys `>' and `<'.
  14867. If you don't like the calendar, turn it off with
  14868. \(setq org-read-date-popup-calendar nil)
  14869. With optional argument TO-TIME, the date will immediately be converted
  14870. to an internal time.
  14871. With an optional argument ORG-WITH-TIME, the prompt will suggest to
  14872. also insert a time. Note that when ORG-WITH-TIME is not set, you can
  14873. still enter a time, and this function will inform the calling routine
  14874. about this change. The calling routine may then choose to change the
  14875. format used to insert the time stamp into the buffer to include the time.
  14876. With optional argument FROM-STRING, read from this string instead from
  14877. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  14878. the time/date that is used for everything that is not specified by the
  14879. user."
  14880. (require 'parse-time)
  14881. (let* ((org-time-stamp-rounding-minutes
  14882. (if (equal org-with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  14883. (org-dcst org-display-custom-times)
  14884. (ct (org-current-time))
  14885. (org-def (or org-overriding-default-time default-time ct))
  14886. (org-defdecode (decode-time org-def))
  14887. (dummy (progn
  14888. (when (< (nth 2 org-defdecode) org-extend-today-until)
  14889. (setcar (nthcdr 2 org-defdecode) -1)
  14890. (setcar (nthcdr 1 org-defdecode) 59)
  14891. (setq org-def (apply 'encode-time org-defdecode)
  14892. org-defdecode (decode-time org-def)))))
  14893. (mouse-autoselect-window nil) ; Don't let the mouse jump
  14894. (calendar-frame-setup nil)
  14895. (calendar-setup nil)
  14896. (calendar-move-hook nil)
  14897. (calendar-view-diary-initially-flag nil)
  14898. (calendar-view-holidays-initially-flag nil)
  14899. (timestr (format-time-string
  14900. (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") org-def))
  14901. (prompt (concat (if prompt (concat prompt " ") "")
  14902. (format "Date+time [%s]: " timestr)))
  14903. ans (org-ans0 "") org-ans1 org-ans2 final)
  14904. (cond
  14905. (from-string (setq ans from-string))
  14906. (org-read-date-popup-calendar
  14907. (save-excursion
  14908. (save-window-excursion
  14909. (calendar)
  14910. (org-eval-in-calendar '(setq cursor-type nil) t)
  14911. (unwind-protect
  14912. (progn
  14913. (calendar-forward-day (- (time-to-days org-def)
  14914. (calendar-absolute-from-gregorian
  14915. (calendar-current-date))))
  14916. (org-eval-in-calendar nil t)
  14917. (let* ((old-map (current-local-map))
  14918. (map (copy-keymap calendar-mode-map))
  14919. (minibuffer-local-map
  14920. (copy-keymap org-read-date-minibuffer-local-map)))
  14921. (org-defkey map (kbd "RET") 'org-calendar-select)
  14922. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  14923. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  14924. (unwind-protect
  14925. (progn
  14926. (use-local-map map)
  14927. (setq org-read-date-inactive inactive)
  14928. (add-hook 'post-command-hook 'org-read-date-display)
  14929. (setq org-ans0 (read-string prompt default-input
  14930. 'org-read-date-history nil))
  14931. ;; org-ans0: from prompt
  14932. ;; org-ans1: from mouse click
  14933. ;; org-ans2: from calendar motion
  14934. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  14935. (remove-hook 'post-command-hook 'org-read-date-display)
  14936. (use-local-map old-map)
  14937. (when org-read-date-overlay
  14938. (delete-overlay org-read-date-overlay)
  14939. (setq org-read-date-overlay nil)))))
  14940. (bury-buffer "*Calendar*")))))
  14941. (t ; Naked prompt only
  14942. (unwind-protect
  14943. (setq ans (read-string prompt default-input
  14944. 'org-read-date-history timestr))
  14945. (when org-read-date-overlay
  14946. (delete-overlay org-read-date-overlay)
  14947. (setq org-read-date-overlay nil)))))
  14948. (setq final (org-read-date-analyze ans org-def org-defdecode))
  14949. (when org-read-date-analyze-forced-year
  14950. (message "Year was forced into %s"
  14951. (if org-read-date-force-compatible-dates
  14952. "compatible range (1970-2037)"
  14953. "range representable on this machine"))
  14954. (ding))
  14955. ;; One round trip to get rid of 34th of August and stuff like that....
  14956. (setq final (decode-time (apply 'encode-time final)))
  14957. (setq org-read-date-final-answer ans)
  14958. (if to-time
  14959. (apply 'encode-time final)
  14960. (if (and (boundp 'org-time-was-given) org-time-was-given)
  14961. (format "%04d-%02d-%02d %02d:%02d"
  14962. (nth 5 final) (nth 4 final) (nth 3 final)
  14963. (nth 2 final) (nth 1 final))
  14964. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  14965. (defvar org-def)
  14966. (defvar org-defdecode)
  14967. (defvar org-with-time)
  14968. (defun org-read-date-display ()
  14969. "Display the current date prompt interpretation in the minibuffer."
  14970. (when org-read-date-display-live
  14971. (when org-read-date-overlay
  14972. (delete-overlay org-read-date-overlay))
  14973. (when (minibufferp (current-buffer))
  14974. (save-excursion
  14975. (end-of-line 1)
  14976. (while (not (equal (buffer-substring
  14977. (max (point-min) (- (point) 4)) (point))
  14978. " "))
  14979. (insert " ")))
  14980. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  14981. " " (or org-ans1 org-ans2)))
  14982. (org-end-time-was-given nil)
  14983. (f (org-read-date-analyze ans org-def org-defdecode))
  14984. (fmts (if org-dcst
  14985. org-time-stamp-custom-formats
  14986. org-time-stamp-formats))
  14987. (fmt (if (or org-with-time
  14988. (and (boundp 'org-time-was-given) org-time-was-given))
  14989. (cdr fmts)
  14990. (car fmts)))
  14991. (txt (format-time-string fmt (apply 'encode-time f)))
  14992. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  14993. (txt (concat "=> " txt)))
  14994. (when (and org-end-time-was-given
  14995. (string-match org-plain-time-of-day-regexp txt))
  14996. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  14997. org-end-time-was-given
  14998. (substring txt (match-end 0)))))
  14999. (when org-read-date-analyze-futurep
  15000. (setq txt (concat txt " (=>F)")))
  15001. (setq org-read-date-overlay
  15002. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  15003. (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
  15004. (defun org-read-date-analyze (ans org-def org-defdecode)
  15005. "Analyze the combined answer of the date prompt."
  15006. ;; FIXME: cleanup and comment
  15007. (let ((nowdecode (decode-time (current-time)))
  15008. delta deltan deltaw deltadef year month day
  15009. hour minute second wday pm h2 m2 tl wday1
  15010. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  15011. (setq org-read-date-analyze-futurep nil
  15012. org-read-date-analyze-forced-year nil)
  15013. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  15014. (setq ans "+0"))
  15015. (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
  15016. (setq ans (replace-match "" t t ans)
  15017. deltan (car delta)
  15018. deltaw (nth 1 delta)
  15019. deltadef (nth 2 delta)))
  15020. ;; Check if there is an iso week date in there. If yes, store the
  15021. ;; info and postpone interpreting it until the rest of the parsing
  15022. ;; is done.
  15023. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  15024. (setq iso-year (if (match-end 1)
  15025. (org-small-year-to-year
  15026. (string-to-number (match-string 1 ans))))
  15027. iso-weekday (if (match-end 3)
  15028. (string-to-number (match-string 3 ans)))
  15029. iso-week (string-to-number (match-string 2 ans)))
  15030. (setq ans (replace-match "" t t ans)))
  15031. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  15032. (when (string-match
  15033. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  15034. (setq year (if (match-end 2)
  15035. (string-to-number (match-string 2 ans))
  15036. (progn (setq kill-year t)
  15037. (string-to-number (format-time-string "%Y"))))
  15038. month (string-to-number (match-string 3 ans))
  15039. day (string-to-number (match-string 4 ans)))
  15040. (if (< year 100) (setq year (+ 2000 year)))
  15041. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15042. t nil ans)))
  15043. ;; Help matching dotted european dates
  15044. (when (string-match
  15045. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\.\\( ?[1-9][0-9]\\{3\\}\\)?" ans)
  15046. (setq year (if (match-end 3) (string-to-number (match-string 3 ans))
  15047. (setq kill-year t)
  15048. (string-to-number (format-time-string "%Y")))
  15049. day (string-to-number (match-string 1 ans))
  15050. month (string-to-number (match-string 2 ans))
  15051. ans (replace-match (format "%04d-%02d-%02d" year month day)
  15052. t nil ans)))
  15053. ;; Help matching american dates, like 5/30 or 5/30/7
  15054. (when (string-match
  15055. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  15056. (setq year (if (match-end 4)
  15057. (string-to-number (match-string 4 ans))
  15058. (progn (setq kill-year t)
  15059. (string-to-number (format-time-string "%Y"))))
  15060. month (string-to-number (match-string 1 ans))
  15061. day (string-to-number (match-string 2 ans)))
  15062. (if (< year 100) (setq year (+ 2000 year)))
  15063. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15064. t nil ans)))
  15065. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  15066. ;; If there is a time with am/pm, and *no* time without it, we convert
  15067. ;; so that matching will be successful.
  15068. (loop for i from 1 to 2 do ; twice, for end time as well
  15069. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  15070. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  15071. (setq hour (string-to-number (match-string 1 ans))
  15072. minute (if (match-end 3)
  15073. (string-to-number (match-string 3 ans))
  15074. 0)
  15075. pm (equal ?p
  15076. (string-to-char (downcase (match-string 4 ans)))))
  15077. (if (and (= hour 12) (not pm))
  15078. (setq hour 0)
  15079. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  15080. (setq ans (replace-match (format "%02d:%02d" hour minute)
  15081. t t ans))))
  15082. ;; Check if a time range is given as a duration
  15083. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  15084. (setq hour (string-to-number (match-string 1 ans))
  15085. h2 (+ hour (string-to-number (match-string 3 ans)))
  15086. minute (string-to-number (match-string 2 ans))
  15087. m2 (+ minute (if (match-end 5) (string-to-number
  15088. (match-string 5 ans))0)))
  15089. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  15090. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  15091. t t ans)))
  15092. ;; Check if there is a time range
  15093. (when (boundp 'org-end-time-was-given)
  15094. (setq org-time-was-given nil)
  15095. (when (and (string-match org-plain-time-of-day-regexp ans)
  15096. (match-end 8))
  15097. (setq org-end-time-was-given (match-string 8 ans))
  15098. (setq ans (concat (substring ans 0 (match-beginning 7))
  15099. (substring ans (match-end 7))))))
  15100. (setq tl (parse-time-string ans)
  15101. day (or (nth 3 tl) (nth 3 org-defdecode))
  15102. month (or (nth 4 tl)
  15103. (if (and org-read-date-prefer-future
  15104. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  15105. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  15106. (nth 4 org-defdecode)))
  15107. year (or (and (not kill-year) (nth 5 tl))
  15108. (if (and org-read-date-prefer-future
  15109. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  15110. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  15111. (nth 5 org-defdecode)))
  15112. hour (or (nth 2 tl) (nth 2 org-defdecode))
  15113. minute (or (nth 1 tl) (nth 1 org-defdecode))
  15114. second (or (nth 0 tl) 0)
  15115. wday (nth 6 tl))
  15116. (when (and (eq org-read-date-prefer-future 'time)
  15117. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  15118. (equal day (nth 3 nowdecode))
  15119. (equal month (nth 4 nowdecode))
  15120. (equal year (nth 5 nowdecode))
  15121. (nth 2 tl)
  15122. (or (< (nth 2 tl) (nth 2 nowdecode))
  15123. (and (= (nth 2 tl) (nth 2 nowdecode))
  15124. (nth 1 tl)
  15125. (< (nth 1 tl) (nth 1 nowdecode)))))
  15126. (setq day (1+ day)
  15127. futurep t))
  15128. ;; Special date definitions below
  15129. (cond
  15130. (iso-week
  15131. ;; There was an iso week
  15132. (require 'cal-iso)
  15133. (setq futurep nil)
  15134. (setq year (or iso-year year)
  15135. day (or iso-weekday wday 1)
  15136. wday nil ; to make sure that the trigger below does not match
  15137. iso-date (calendar-gregorian-from-absolute
  15138. (calendar-absolute-from-iso
  15139. (list iso-week day year))))
  15140. ; FIXME: Should we also push ISO weeks into the future?
  15141. ; (when (and org-read-date-prefer-future
  15142. ; (not iso-year)
  15143. ; (< (calendar-absolute-from-gregorian iso-date)
  15144. ; (time-to-days (current-time))))
  15145. ; (setq year (1+ year)
  15146. ; iso-date (calendar-gregorian-from-absolute
  15147. ; (calendar-absolute-from-iso
  15148. ; (list iso-week day year)))))
  15149. (setq month (car iso-date)
  15150. year (nth 2 iso-date)
  15151. day (nth 1 iso-date)))
  15152. (deltan
  15153. (setq futurep nil)
  15154. (unless deltadef
  15155. (let ((now (decode-time (current-time))))
  15156. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  15157. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  15158. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  15159. ((equal deltaw "m") (setq month (+ month deltan)))
  15160. ((equal deltaw "y") (setq year (+ year deltan)))))
  15161. ((and wday (not (nth 3 tl)))
  15162. ;; Weekday was given, but no day, so pick that day in the week
  15163. ;; on or after the derived date.
  15164. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  15165. (unless (equal wday wday1)
  15166. (setq day (+ day (% (- wday wday1 -7) 7))))))
  15167. (if (and (boundp 'org-time-was-given)
  15168. (nth 2 tl))
  15169. (setq org-time-was-given t))
  15170. (if (< year 100) (setq year (+ 2000 year)))
  15171. ;; Check of the date is representable
  15172. (if org-read-date-force-compatible-dates
  15173. (progn
  15174. (if (< year 1970)
  15175. (setq year 1970 org-read-date-analyze-forced-year t))
  15176. (if (> year 2037)
  15177. (setq year 2037 org-read-date-analyze-forced-year t)))
  15178. (condition-case nil
  15179. (ignore (encode-time second minute hour day month year))
  15180. (error
  15181. (setq year (nth 5 org-defdecode))
  15182. (setq org-read-date-analyze-forced-year t))))
  15183. (setq org-read-date-analyze-futurep futurep)
  15184. (list second minute hour day month year)))
  15185. (defvar parse-time-weekdays)
  15186. (defun org-read-date-get-relative (s today default)
  15187. "Check string S for special relative date string.
  15188. TODAY and DEFAULT are internal times, for today and for a default.
  15189. Return shift list (N what def-flag)
  15190. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  15191. N is the number of WHATs to shift.
  15192. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  15193. the DEFAULT date rather than TODAY."
  15194. (require 'parse-time)
  15195. (when (and
  15196. (string-match
  15197. (concat
  15198. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  15199. "\\([0-9]+\\)?"
  15200. "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  15201. "\\([ \t]\\|$\\)") s)
  15202. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  15203. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  15204. (string-to-char (substring (match-string 1 s) -1))
  15205. ?+))
  15206. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  15207. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  15208. (what (if (match-end 3) (match-string 3 s) "d"))
  15209. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  15210. (date (if rel default today))
  15211. (wday (nth 6 (decode-time date)))
  15212. delta)
  15213. (if wday1
  15214. (progn
  15215. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  15216. (if (= delta 0) (setq delta 7))
  15217. (if (= dir ?-)
  15218. (progn
  15219. (setq delta (- delta 7))
  15220. (if (= delta 0) (setq delta -7))))
  15221. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  15222. (list delta "d" rel))
  15223. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  15224. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  15225. "Turn a user-specified date into the internal representation.
  15226. The internal representation needed by the calendar is (month day year).
  15227. This is a wrapper to handle the brain-dead convention in calendar that
  15228. user function argument order change dependent on argument order."
  15229. (if (boundp 'calendar-date-style)
  15230. (cond
  15231. ((eq calendar-date-style 'american)
  15232. (list arg1 arg2 arg3))
  15233. ((eq calendar-date-style 'european)
  15234. (list arg2 arg1 arg3))
  15235. ((eq calendar-date-style 'iso)
  15236. (list arg2 arg3 arg1)))
  15237. (org-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  15238. (if (org-bound-and-true-p european-calendar-style)
  15239. (list arg2 arg1 arg3)
  15240. (list arg1 arg2 arg3)))))
  15241. (defun org-eval-in-calendar (form &optional keepdate)
  15242. "Eval FORM in the calendar window and return to current window.
  15243. When KEEPDATE is non-nil, update `org-ans2' from the cursor date,
  15244. otherwise stick to the current value of `org-ans2'."
  15245. (let ((sf (selected-frame))
  15246. (sw (selected-window)))
  15247. (select-window (get-buffer-window "*Calendar*" t))
  15248. (eval form)
  15249. (when (and (not keepdate) (calendar-cursor-to-date))
  15250. (let* ((date (calendar-cursor-to-date))
  15251. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15252. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  15253. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  15254. (select-window sw)
  15255. (org-select-frame-set-input-focus sf)))
  15256. (defun org-calendar-select ()
  15257. "Return to `org-read-date' with the date currently selected.
  15258. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15259. (interactive)
  15260. (when (calendar-cursor-to-date)
  15261. (let* ((date (calendar-cursor-to-date))
  15262. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15263. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15264. (if (active-minibuffer-window) (exit-minibuffer))))
  15265. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  15266. "Insert a date stamp for the date given by the internal TIME.
  15267. WITH-HM means use the stamp format that includes the time of the day.
  15268. INACTIVE means use square brackets instead of angular ones, so that the
  15269. stamp will not contribute to the agenda.
  15270. PRE and POST are optional strings to be inserted before and after the
  15271. stamp.
  15272. The command returns the inserted time stamp."
  15273. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  15274. stamp)
  15275. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  15276. (insert-before-markers (or pre ""))
  15277. (when (listp extra)
  15278. (setq extra (car extra))
  15279. (if (and (stringp extra)
  15280. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  15281. (setq extra (format "-%02d:%02d"
  15282. (string-to-number (match-string 1 extra))
  15283. (string-to-number (match-string 2 extra))))
  15284. (setq extra nil)))
  15285. (when extra
  15286. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  15287. (insert-before-markers (setq stamp (format-time-string fmt time)))
  15288. (insert-before-markers (or post ""))
  15289. (setq org-last-inserted-timestamp stamp)))
  15290. (defun org-toggle-time-stamp-overlays ()
  15291. "Toggle the use of custom time stamp formats."
  15292. (interactive)
  15293. (setq org-display-custom-times (not org-display-custom-times))
  15294. (unless org-display-custom-times
  15295. (let ((p (point-min)) (bmp (buffer-modified-p)))
  15296. (while (setq p (next-single-property-change p 'display))
  15297. (if (and (get-text-property p 'display)
  15298. (eq (get-text-property p 'face) 'org-date))
  15299. (remove-text-properties
  15300. p (setq p (next-single-property-change p 'display))
  15301. '(display t))))
  15302. (set-buffer-modified-p bmp)))
  15303. (if (featurep 'xemacs)
  15304. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  15305. (org-restart-font-lock)
  15306. (setq org-table-may-need-update t)
  15307. (if org-display-custom-times
  15308. (message "Time stamps are overlaid with custom format")
  15309. (message "Time stamp overlays removed")))
  15310. (defun org-display-custom-time (beg end)
  15311. "Overlay modified time stamp format over timestamp between BEG and END."
  15312. (let* ((ts (buffer-substring beg end))
  15313. t1 w1 with-hm tf time str w2 (off 0))
  15314. (save-match-data
  15315. (setq t1 (org-parse-time-string ts t))
  15316. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
  15317. (setq off (- (match-end 0) (match-beginning 0)))))
  15318. (setq end (- end off))
  15319. (setq w1 (- end beg)
  15320. with-hm (and (nth 1 t1) (nth 2 t1))
  15321. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  15322. time (org-fix-decoded-time t1)
  15323. str (org-add-props
  15324. (format-time-string
  15325. (substring tf 1 -1) (apply 'encode-time time))
  15326. nil 'mouse-face 'highlight)
  15327. w2 (length str))
  15328. (if (not (= w2 w1))
  15329. (add-text-properties (1+ beg) (+ 2 beg)
  15330. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  15331. (if (featurep 'xemacs)
  15332. (progn
  15333. (put-text-property beg end 'invisible t)
  15334. (put-text-property beg end 'end-glyph (make-glyph str)))
  15335. (put-text-property beg end 'display str))))
  15336. (defun org-translate-time (string)
  15337. "Translate all timestamps in STRING to custom format.
  15338. But do this only if the variable `org-display-custom-times' is set."
  15339. (when org-display-custom-times
  15340. (save-match-data
  15341. (let* ((start 0)
  15342. (re org-ts-regexp-both)
  15343. t1 with-hm inactive tf time str beg end)
  15344. (while (setq start (string-match re string start))
  15345. (setq beg (match-beginning 0)
  15346. end (match-end 0)
  15347. t1 (save-match-data
  15348. (org-parse-time-string (substring string beg end) t))
  15349. with-hm (and (nth 1 t1) (nth 2 t1))
  15350. inactive (equal (substring string beg (1+ beg)) "[")
  15351. tf (funcall (if with-hm 'cdr 'car)
  15352. org-time-stamp-custom-formats)
  15353. time (org-fix-decoded-time t1)
  15354. str (format-time-string
  15355. (concat
  15356. (if inactive "[" "<") (substring tf 1 -1)
  15357. (if inactive "]" ">"))
  15358. (apply 'encode-time time))
  15359. string (replace-match str t t string)
  15360. start (+ start (length str)))))))
  15361. string)
  15362. (defun org-fix-decoded-time (time)
  15363. "Set 0 instead of nil for the first 6 elements of time.
  15364. Don't touch the rest."
  15365. (let ((n 0))
  15366. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  15367. (define-obsolete-function-alias 'org-days-to-time 'org-time-stamp-to-now "24.4")
  15368. (defun org-time-stamp-to-now (timestamp-string &optional seconds)
  15369. "Difference between TIMESTAMP-STRING and now in days.
  15370. If SECONDS is non-nil, return the difference in seconds."
  15371. (let ((fdiff (if seconds 'org-float-time 'time-to-days)))
  15372. (- (funcall fdiff (org-time-string-to-time timestamp-string))
  15373. (funcall fdiff (current-time)))))
  15374. (defun org-deadline-close (timestamp-string &optional ndays)
  15375. "Is the time in TIMESTAMP-STRING close to the current date?"
  15376. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  15377. (and (< (org-time-stamp-to-now timestamp-string) ndays)
  15378. (not (org-entry-is-done-p))))
  15379. (defun org-get-wdays (ts &optional delay zero-delay)
  15380. "Get the deadline lead time appropriate for timestring TS.
  15381. When DELAY is non-nil, get the delay time for scheduled items
  15382. instead of the deadline lead time. When ZERO-DELAY is non-nil
  15383. and `org-scheduled-delay-days' is 0, enforce 0 as the delay,
  15384. don't try to find the delay cookie in the scheduled timestamp."
  15385. (let ((tv (if delay org-scheduled-delay-days
  15386. org-deadline-warning-days)))
  15387. (cond
  15388. ((or (and delay (< tv 0))
  15389. (and delay zero-delay (<= tv 0))
  15390. (and (not delay) (<= tv 0)))
  15391. ;; Enforce this value no matter what
  15392. (- tv))
  15393. ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
  15394. ;; lead time is specified.
  15395. (floor (* (string-to-number (match-string 1 ts))
  15396. (cdr (assoc (match-string 2 ts)
  15397. '(("d" . 1) ("w" . 7)
  15398. ("m" . 30.4) ("y" . 365.25)
  15399. ("h" . 0.041667)))))))
  15400. ;; go for the default.
  15401. (t tv))))
  15402. (defun org-calendar-select-mouse (ev)
  15403. "Return to `org-read-date' with the date currently selected.
  15404. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15405. (interactive "e")
  15406. (mouse-set-point ev)
  15407. (when (calendar-cursor-to-date)
  15408. (let* ((date (calendar-cursor-to-date))
  15409. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15410. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15411. (if (active-minibuffer-window) (exit-minibuffer))))
  15412. (defun org-check-deadlines (ndays)
  15413. "Check if there are any deadlines due or past due.
  15414. A deadline is considered due if it happens within `org-deadline-warning-days'
  15415. days from today's date. If the deadline appears in an entry marked DONE,
  15416. it is not shown. The prefix arg NDAYS can be used to test that many
  15417. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  15418. (interactive "P")
  15419. (let* ((org-warn-days
  15420. (cond
  15421. ((equal ndays '(4)) 100000)
  15422. (ndays (prefix-numeric-value ndays))
  15423. (t (abs org-deadline-warning-days))))
  15424. (case-fold-search nil)
  15425. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15426. (callback
  15427. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  15428. (message "%d deadlines past-due or due within %d days"
  15429. (org-occur regexp nil callback)
  15430. org-warn-days)))
  15431. (defsubst org-re-timestamp (type)
  15432. "Return a regexp for timestamp TYPE.
  15433. Allowed values for TYPE are:
  15434. all: all timestamps
  15435. active: only active timestamps (<...>)
  15436. inactive: only inactive timestamps ([...])
  15437. scheduled: only scheduled timestamps
  15438. deadline: only deadline timestamps
  15439. closed: only closed time-stamps
  15440. When TYPE is nil, fall back on returning a regexp that matches
  15441. both scheduled and deadline timestamps."
  15442. (cond ((eq type 'all) "\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}\\(?: +[^]+0-9> \n -]+\\)?\\(?: +[0-9]\\{1,2\\}:[0-9]\\{2\\}\\)?\\)")
  15443. ((eq type 'active) org-ts-regexp)
  15444. ((eq type 'inactive) "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^ \n>]*?\\)\\]")
  15445. ((eq type 'scheduled) (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>"))
  15446. ((eq type 'deadline) (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15447. ((eq type 'closed) (concat org-closed-string " \\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^ \n>]*?\\)\\]"))
  15448. ((eq type 'scheduled-or-deadline)
  15449. (concat "\\<\\(?:" org-deadline-string "\\|" org-scheduled-string "\\) *<\\([^>]+\\)>"))))
  15450. (defun org-check-before-date (date)
  15451. "Check if there are deadlines or scheduled entries before DATE."
  15452. (interactive (list (org-read-date)))
  15453. (let ((case-fold-search nil)
  15454. (regexp (org-re-timestamp org-ts-type))
  15455. (callback
  15456. (lambda () (time-less-p
  15457. (org-time-string-to-time (match-string 1))
  15458. (org-time-string-to-time date)))))
  15459. (message "%d entries before %s"
  15460. (org-occur regexp nil callback) date)))
  15461. (defun org-check-after-date (date)
  15462. "Check if there are deadlines or scheduled entries after DATE."
  15463. (interactive (list (org-read-date)))
  15464. (let ((case-fold-search nil)
  15465. (regexp (org-re-timestamp org-ts-type))
  15466. (callback
  15467. (lambda () (not
  15468. (time-less-p
  15469. (org-time-string-to-time (match-string 1))
  15470. (org-time-string-to-time date))))))
  15471. (message "%d entries after %s"
  15472. (org-occur regexp nil callback) date)))
  15473. (defun org-check-dates-range (start-date end-date)
  15474. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  15475. (interactive (list (org-read-date nil nil nil "Range starts")
  15476. (org-read-date nil nil nil "Range end")))
  15477. (let ((case-fold-search nil)
  15478. (regexp (org-re-timestamp org-ts-type))
  15479. (callback
  15480. (lambda ()
  15481. (let ((match (match-string 1)))
  15482. (and
  15483. (not (time-less-p
  15484. (org-time-string-to-time match)
  15485. (org-time-string-to-time start-date)))
  15486. (time-less-p
  15487. (org-time-string-to-time match)
  15488. (org-time-string-to-time end-date)))))))
  15489. (message "%d entries between %s and %s"
  15490. (org-occur regexp nil callback) start-date end-date)))
  15491. (defun org-evaluate-time-range (&optional to-buffer)
  15492. "Evaluate a time range by computing the difference between start and end.
  15493. Normally the result is just printed in the echo area, but with prefix arg
  15494. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  15495. If the time range is actually in a table, the result is inserted into the
  15496. next column.
  15497. For time difference computation, a year is assumed to be exactly 365
  15498. days in order to avoid rounding problems."
  15499. (interactive "P")
  15500. (or
  15501. (org-clock-update-time-maybe)
  15502. (save-excursion
  15503. (unless (org-at-date-range-p t)
  15504. (goto-char (point-at-bol))
  15505. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  15506. (if (not (org-at-date-range-p t))
  15507. (user-error "Not at a time-stamp range, and none found in current line")))
  15508. (let* ((ts1 (match-string 1))
  15509. (ts2 (match-string 2))
  15510. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  15511. (match-end (match-end 0))
  15512. (time1 (org-time-string-to-time ts1))
  15513. (time2 (org-time-string-to-time ts2))
  15514. (t1 (org-float-time time1))
  15515. (t2 (org-float-time time2))
  15516. (diff (abs (- t2 t1)))
  15517. (negative (< (- t2 t1) 0))
  15518. ;; (ys (floor (* 365 24 60 60)))
  15519. (ds (* 24 60 60))
  15520. (hs (* 60 60))
  15521. (fy "%dy %dd %02d:%02d")
  15522. (fy1 "%dy %dd")
  15523. (fd "%dd %02d:%02d")
  15524. (fd1 "%dd")
  15525. (fh "%02d:%02d")
  15526. y d h m align)
  15527. (if havetime
  15528. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15529. y 0
  15530. d (floor (/ diff ds)) diff (mod diff ds)
  15531. h (floor (/ diff hs)) diff (mod diff hs)
  15532. m (floor (/ diff 60)))
  15533. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15534. y 0
  15535. d (floor (+ (/ diff ds) 0.5))
  15536. h 0 m 0))
  15537. (if (not to-buffer)
  15538. (message "%s" (org-make-tdiff-string y d h m))
  15539. (if (org-at-table-p)
  15540. (progn
  15541. (goto-char match-end)
  15542. (setq align t)
  15543. (and (looking-at " *|") (goto-char (match-end 0))))
  15544. (goto-char match-end))
  15545. (if (looking-at
  15546. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  15547. (replace-match ""))
  15548. (if negative (insert " -"))
  15549. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  15550. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  15551. (insert " " (format fh h m))))
  15552. (if align (org-table-align))
  15553. (message "Time difference inserted")))))
  15554. (defun org-make-tdiff-string (y d h m)
  15555. (let ((fmt "")
  15556. (l nil))
  15557. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  15558. l (push y l)))
  15559. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  15560. l (push d l)))
  15561. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  15562. l (push h l)))
  15563. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  15564. l (push m l)))
  15565. (apply 'format fmt (nreverse l))))
  15566. (defun org-time-string-to-time (s &optional buffer pos)
  15567. "Convert a timestamp string into internal time."
  15568. (condition-case errdata
  15569. (apply 'encode-time (org-parse-time-string s))
  15570. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15571. s (if (not (and buffer pos))
  15572. ""
  15573. (format " at %d in buffer `%s'" pos buffer))
  15574. (cdr errdata)))))
  15575. (defun org-time-string-to-seconds (s)
  15576. "Convert a timestamp string to a number of seconds."
  15577. (org-float-time (org-time-string-to-time s)))
  15578. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  15579. "Convert a time stamp to an absolute day number.
  15580. If there is a specifier for a cyclic time stamp, get the closest
  15581. date to DAYNR.
  15582. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  15583. The variable `date' is bound by the calendar when this is called."
  15584. (cond
  15585. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  15586. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  15587. daynr
  15588. (+ daynr 1000)))
  15589. ((and daynr (string-match "\\+[0-9]+[hdwmy]" s))
  15590. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  15591. (time-to-days (current-time))) (match-string 0 s)
  15592. prefer show-all))
  15593. (t (time-to-days
  15594. (condition-case errdata
  15595. (apply 'encode-time (org-parse-time-string s))
  15596. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15597. s (if (not (and buffer pos))
  15598. ""
  15599. (format " at %d in buffer `%s'" pos buffer))
  15600. (cdr errdata))))))))
  15601. (defun org-days-to-iso-week (days)
  15602. "Return the iso week number."
  15603. (require 'cal-iso)
  15604. (car (calendar-iso-from-absolute days)))
  15605. (defun org-small-year-to-year (year)
  15606. "Convert 2-digit years into 4-digit years.
  15607. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2037.
  15608. The year 2000 cannot be abbreviated. Any year larger than 99
  15609. is returned unchanged."
  15610. (if (< year 38)
  15611. (setq year (+ 2000 year))
  15612. (if (< year 100)
  15613. (setq year (+ 1900 year))))
  15614. year)
  15615. (defun org-time-from-absolute (d)
  15616. "Return the time corresponding to date D.
  15617. D may be an absolute day number, or a calendar-type list (month day year)."
  15618. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  15619. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  15620. (defun org-calendar-holiday ()
  15621. "List of holidays, for Diary display in Org-mode."
  15622. (require 'holidays)
  15623. (let ((hl (funcall
  15624. (if (fboundp 'calendar-check-holidays)
  15625. 'calendar-check-holidays 'check-calendar-holidays) date)))
  15626. (if hl (mapconcat 'identity hl "; "))))
  15627. (defun org-diary-sexp-entry (sexp entry date)
  15628. "Process a SEXP diary ENTRY for DATE."
  15629. (require 'diary-lib)
  15630. (let ((result (if calendar-debug-sexp
  15631. (let ((stack-trace-on-error t))
  15632. (eval (car (read-from-string sexp))))
  15633. (condition-case nil
  15634. (eval (car (read-from-string sexp)))
  15635. (error
  15636. (beep)
  15637. (message "Bad sexp at line %d in %s: %s"
  15638. (org-current-line)
  15639. (buffer-file-name) sexp)
  15640. (sleep-for 2))))))
  15641. (cond ((stringp result) (split-string result "; "))
  15642. ((and (consp result)
  15643. (not (consp (cdr result)))
  15644. (stringp (cdr result))) (cdr result))
  15645. ((and (consp result)
  15646. (stringp (car result))) result)
  15647. (result entry))))
  15648. (defun org-diary-to-ical-string (frombuf)
  15649. "Get iCalendar entries from diary entries in buffer FROMBUF.
  15650. This uses the icalendar.el library."
  15651. (let* ((tmpdir (if (featurep 'xemacs)
  15652. (temp-directory)
  15653. temporary-file-directory))
  15654. (tmpfile (make-temp-name
  15655. (expand-file-name "orgics" tmpdir)))
  15656. buf rtn b e)
  15657. (with-current-buffer frombuf
  15658. (icalendar-export-region (point-min) (point-max) tmpfile)
  15659. (setq buf (find-buffer-visiting tmpfile))
  15660. (set-buffer buf)
  15661. (goto-char (point-min))
  15662. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  15663. (setq b (match-beginning 0)))
  15664. (goto-char (point-max))
  15665. (if (re-search-backward "^END:VEVENT" nil t)
  15666. (setq e (match-end 0)))
  15667. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  15668. (kill-buffer buf)
  15669. (delete-file tmpfile)
  15670. rtn))
  15671. (defun org-closest-date (start current change prefer show-all)
  15672. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  15673. When PREFER is `past', return a date that is either CURRENT or past.
  15674. When PREFER is `future', return a date that is either CURRENT or future.
  15675. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  15676. ;; Make the proper lists from the dates
  15677. (catch 'exit
  15678. (let ((a1 '(("h" . hour)
  15679. ("d" . day)
  15680. ("w" . week)
  15681. ("m" . month)
  15682. ("y" . year)))
  15683. (shour (nth 2 (org-parse-time-string start)))
  15684. dn dw sday cday n1 n2 n0
  15685. d m y y1 y2 date1 date2 nmonths nm ny m2)
  15686. (setq start (org-date-to-gregorian start)
  15687. current (org-date-to-gregorian
  15688. (if show-all
  15689. current
  15690. (time-to-days (current-time))))
  15691. sday (calendar-absolute-from-gregorian start)
  15692. cday (calendar-absolute-from-gregorian current))
  15693. (if (<= cday sday) (throw 'exit sday))
  15694. (if (string-match "\\(\\+[0-9]+\\)\\([hdwmy]\\)" change)
  15695. (setq dn (string-to-number (match-string 1 change))
  15696. dw (cdr (assoc (match-string 2 change) a1)))
  15697. (user-error "Invalid change specifier: %s" change))
  15698. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  15699. (cond
  15700. ((eq dw 'hour)
  15701. (let ((missing-hours
  15702. (mod (+ (- (* 24 (- cday sday)) shour) org-extend-today-until)
  15703. dn)))
  15704. (setq n1 (if (zerop missing-hours) cday
  15705. (- cday (1+ (floor (/ missing-hours 24)))))
  15706. n2 (+ cday (floor (/ (- dn missing-hours) 24))))))
  15707. ((eq dw 'day)
  15708. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  15709. n2 (+ n1 dn)))
  15710. ((eq dw 'year)
  15711. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  15712. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  15713. (setq date1 (list m d y1)
  15714. n1 (calendar-absolute-from-gregorian date1)
  15715. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  15716. n2 (calendar-absolute-from-gregorian date2)))
  15717. ((eq dw 'month)
  15718. ;; approx number of month between the two dates
  15719. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  15720. ;; How often does dn fit in there?
  15721. (setq d (nth 1 start) m (car start) y (nth 2 start)
  15722. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  15723. m (+ m nm)
  15724. ny (floor (/ m 12))
  15725. y (+ y ny)
  15726. m (- m (* ny 12)))
  15727. (while (> m 12) (setq m (- m 12) y (1+ y)))
  15728. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  15729. (setq m2 (+ m dn) y2 y)
  15730. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15731. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  15732. (while (<= n2 cday)
  15733. (setq n1 n2 m m2 y y2)
  15734. (setq m2 (+ m dn) y2 y)
  15735. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15736. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  15737. ;; Make sure n1 is the earlier date
  15738. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  15739. (if show-all
  15740. (cond
  15741. ((eq prefer 'past) (if (= cday n2) n2 n1))
  15742. ((eq prefer 'future) (if (= cday n1) n1 n2))
  15743. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  15744. (cond
  15745. ((eq prefer 'past) (if (= cday n2) n2 n1))
  15746. ((eq prefer 'future) (if (= cday n1) n1 n2))
  15747. (t (if (= cday n1) n1 n2)))))))
  15748. (defun org-date-to-gregorian (date)
  15749. "Turn any specification of DATE into a Gregorian date for the calendar."
  15750. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  15751. ((and (listp date) (= (length date) 3)) date)
  15752. ((stringp date)
  15753. (setq date (org-parse-time-string date))
  15754. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  15755. ((listp date)
  15756. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  15757. (defun org-parse-time-string (s &optional nodefault)
  15758. "Parse the standard Org-mode time string.
  15759. This should be a lot faster than the normal `parse-time-string'.
  15760. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  15761. hour and minute fields will be nil if not given."
  15762. (cond ((string-match org-ts-regexp0 s)
  15763. (list 0
  15764. (if (or (match-beginning 8) (not nodefault))
  15765. (string-to-number (or (match-string 8 s) "0")))
  15766. (if (or (match-beginning 7) (not nodefault))
  15767. (string-to-number (or (match-string 7 s) "0")))
  15768. (string-to-number (match-string 4 s))
  15769. (string-to-number (match-string 3 s))
  15770. (string-to-number (match-string 2 s))
  15771. nil nil nil))
  15772. ((string-match "^<[^>]+>$" s)
  15773. (decode-time (seconds-to-time (org-matcher-time s))))
  15774. (t (error "Not a standard Org-mode time string: %s" s))))
  15775. (defun org-timestamp-up (&optional arg)
  15776. "Increase the date item at the cursor by one.
  15777. If the cursor is on the year, change the year. If it is on the month,
  15778. the day or the time, change that.
  15779. With prefix ARG, change by that many units."
  15780. (interactive "p")
  15781. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  15782. (defun org-timestamp-down (&optional arg)
  15783. "Decrease the date item at the cursor by one.
  15784. If the cursor is on the year, change the year. If it is on the month,
  15785. the day or the time, change that.
  15786. With prefix ARG, change by that many units."
  15787. (interactive "p")
  15788. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  15789. (defun org-timestamp-up-day (&optional arg)
  15790. "Increase the date in the time stamp by one day.
  15791. With prefix ARG, change that many days."
  15792. (interactive "p")
  15793. (if (and (not (org-at-timestamp-p t))
  15794. (org-at-heading-p))
  15795. (org-todo 'up)
  15796. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  15797. (defun org-timestamp-down-day (&optional arg)
  15798. "Decrease the date in the time stamp by one day.
  15799. With prefix ARG, change that many days."
  15800. (interactive "p")
  15801. (if (and (not (org-at-timestamp-p t))
  15802. (org-at-heading-p))
  15803. (org-todo 'down)
  15804. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  15805. (defun org-at-timestamp-p (&optional inactive-ok)
  15806. "Determine if the cursor is in or at a timestamp."
  15807. (interactive)
  15808. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  15809. (pos (point))
  15810. (ans (or (looking-at tsr)
  15811. (save-excursion
  15812. (skip-chars-backward "^[<\n\r\t")
  15813. (if (> (point) (point-min)) (backward-char 1))
  15814. (and (looking-at tsr)
  15815. (> (- (match-end 0) pos) -1))))))
  15816. (and ans
  15817. (boundp 'org-ts-what)
  15818. (setq org-ts-what
  15819. (cond
  15820. ((= pos (match-beginning 0)) 'bracket)
  15821. ;; Point is considered to be "on the bracket" whether
  15822. ;; it's really on it or right after it.
  15823. ((= pos (1- (match-end 0))) 'bracket)
  15824. ((= pos (match-end 0)) 'after)
  15825. ((org-pos-in-match-range pos 2) 'year)
  15826. ((org-pos-in-match-range pos 3) 'month)
  15827. ((org-pos-in-match-range pos 7) 'hour)
  15828. ((org-pos-in-match-range pos 8) 'minute)
  15829. ((or (org-pos-in-match-range pos 4)
  15830. (org-pos-in-match-range pos 5)) 'day)
  15831. ((and (> pos (or (match-end 8) (match-end 5)))
  15832. (< pos (match-end 0)))
  15833. (- pos (or (match-end 8) (match-end 5))))
  15834. (t 'day))))
  15835. ans))
  15836. (defun org-toggle-timestamp-type ()
  15837. "Toggle the type (<active> or [inactive]) of a time stamp."
  15838. (interactive)
  15839. (when (org-at-timestamp-p t)
  15840. (let ((beg (match-beginning 0)) (end (match-end 0))
  15841. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  15842. (save-excursion
  15843. (goto-char beg)
  15844. (while (re-search-forward "[][<>]" end t)
  15845. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  15846. t t)))
  15847. (message "Timestamp is now %sactive"
  15848. (if (equal (char-after beg) ?<) "" "in")))))
  15849. (defun org-at-clock-log-p nil
  15850. "Is the cursor on the clock log line?"
  15851. (save-excursion
  15852. (move-beginning-of-line 1)
  15853. (looking-at "^[ \t]*CLOCK:")))
  15854. (defvar org-clock-history) ; defined in org-clock.el
  15855. (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
  15856. (defun org-timestamp-change (n &optional what updown suppress-tmp-delay)
  15857. "Change the date in the time stamp at point.
  15858. The date will be changed by N times WHAT. WHAT can be `day', `month',
  15859. `year', `minute', `second'. If WHAT is not given, the cursor position
  15860. in the timestamp determines what will be changed.
  15861. When SUPPRESS-TMP-DELAY is non-nil, suppress delays like \"--2d\"."
  15862. (let ((origin (point)) origin-cat
  15863. with-hm inactive
  15864. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  15865. org-ts-what
  15866. extra rem
  15867. ts time time0 fixnext clrgx)
  15868. (if (not (org-at-timestamp-p t))
  15869. (user-error "Not at a timestamp"))
  15870. (if (and (not what) (eq org-ts-what 'bracket))
  15871. (org-toggle-timestamp-type)
  15872. ;; Point isn't on brackets. Remember the part of the time-stamp
  15873. ;; the point was in. Indeed, size of time-stamps may change,
  15874. ;; but point must be kept in the same category nonetheless.
  15875. (setq origin-cat org-ts-what)
  15876. (if (and (not what) (not (eq org-ts-what 'day))
  15877. org-display-custom-times
  15878. (get-text-property (point) 'display)
  15879. (not (get-text-property (1- (point)) 'display)))
  15880. (setq org-ts-what 'day))
  15881. (setq org-ts-what (or what org-ts-what)
  15882. inactive (= (char-after (match-beginning 0)) ?\[)
  15883. ts (match-string 0))
  15884. (replace-match "")
  15885. (when (string-match
  15886. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?-?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
  15887. ts)
  15888. (setq extra (match-string 1 ts))
  15889. (if suppress-tmp-delay
  15890. (setq extra (replace-regexp-in-string " --[0-9]+[hdwmy]" "" extra))))
  15891. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  15892. (setq with-hm t))
  15893. (setq time0 (org-parse-time-string ts))
  15894. (when (and updown
  15895. (eq org-ts-what 'minute)
  15896. (not current-prefix-arg))
  15897. ;; This looks like s-up and s-down. Change by one rounding step.
  15898. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  15899. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  15900. (setcar (cdr time0) (+ (nth 1 time0)
  15901. (if (> n 0) (- rem) (- dm rem))))))
  15902. (setq time
  15903. (encode-time (or (car time0) 0)
  15904. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  15905. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  15906. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  15907. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  15908. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  15909. (nthcdr 6 time0)))
  15910. (when (and (member org-ts-what '(hour minute))
  15911. extra
  15912. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  15913. (setq extra (org-modify-ts-extra
  15914. extra
  15915. (if (eq org-ts-what 'hour) 2 5)
  15916. n dm)))
  15917. (when (integerp org-ts-what)
  15918. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  15919. (if (eq what 'calendar)
  15920. (let ((cal-date (org-get-date-from-calendar)))
  15921. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  15922. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  15923. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  15924. (setcar time0 (or (car time0) 0))
  15925. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  15926. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  15927. (setq time (apply 'encode-time time0))))
  15928. ;; Insert the new time-stamp, and ensure point stays in the same
  15929. ;; category as before (i.e. not after the last position in that
  15930. ;; category).
  15931. (let ((pos (point)))
  15932. ;; Stay before inserted string. `save-excursion' is of no use.
  15933. (setq org-last-changed-timestamp
  15934. (org-insert-time-stamp time with-hm inactive nil nil extra))
  15935. (goto-char pos))
  15936. (save-match-data
  15937. (looking-at org-ts-regexp3)
  15938. (goto-char (cond
  15939. ;; `day' category ends before `hour' if any, or at
  15940. ;; the end of the day name.
  15941. ((eq origin-cat 'day)
  15942. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  15943. ((eq origin-cat 'hour) (min (match-end 7) origin))
  15944. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  15945. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  15946. ;; `year' and `month' have both fixed size: point
  15947. ;; couldn't have moved into another part.
  15948. (t origin))))
  15949. ;; Update clock if on a CLOCK line.
  15950. (org-clock-update-time-maybe)
  15951. ;; Maybe adjust the closest clock in `org-clock-history'
  15952. (when org-clock-adjust-closest
  15953. (if (not (and (org-at-clock-log-p)
  15954. (< 1 (length (delq nil (mapcar 'marker-position
  15955. org-clock-history))))))
  15956. (message "No clock to adjust")
  15957. (cond ((save-excursion ; fix previous clock?
  15958. (re-search-backward org-ts-regexp0 nil t)
  15959. (org-looking-back (concat org-clock-string " \\[")))
  15960. (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
  15961. ((save-excursion ; fix next clock?
  15962. (re-search-backward org-ts-regexp0 nil t)
  15963. (looking-at (concat org-ts-regexp0 "\\] =>")))
  15964. (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
  15965. (save-window-excursion
  15966. ;; Find closest clock to point, adjust the previous/next one in history
  15967. (let* ((p (save-excursion (org-back-to-heading t)))
  15968. (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
  15969. (clfixnth
  15970. (+ fixnext (- (length cl) (or (length (member (apply #'min cl) cl)) 100))))
  15971. (clfixpos (if (> 0 clfixnth) nil (nth clfixnth org-clock-history))))
  15972. (if (not clfixpos)
  15973. (message "No clock to adjust")
  15974. (save-excursion
  15975. (org-goto-marker-or-bmk clfixpos)
  15976. (org-show-subtree)
  15977. (when (re-search-forward clrgx nil t)
  15978. (goto-char (match-beginning 1))
  15979. (let (org-clock-adjust-closest)
  15980. (org-timestamp-change n org-ts-what updown))
  15981. (message "Clock adjusted in %s for heading: %s"
  15982. (file-name-nondirectory (buffer-file-name))
  15983. (org-get-heading t t)))))))))
  15984. ;; Try to recenter the calendar window, if any.
  15985. (if (and org-calendar-follow-timestamp-change
  15986. (get-buffer-window "*Calendar*" t)
  15987. (memq org-ts-what '(day month year)))
  15988. (org-recenter-calendar (time-to-days time))))))
  15989. (defun org-modify-ts-extra (s pos n dm)
  15990. "Change the different parts of the lead-time and repeat fields in timestamp."
  15991. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  15992. ng h m new rem)
  15993. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  15994. (cond
  15995. ((or (org-pos-in-match-range pos 2)
  15996. (org-pos-in-match-range pos 3))
  15997. (setq m (string-to-number (match-string 3 s))
  15998. h (string-to-number (match-string 2 s)))
  15999. (if (org-pos-in-match-range pos 2)
  16000. (setq h (+ h n))
  16001. (setq n (* dm (org-no-warnings (signum n))))
  16002. (when (not (= 0 (setq rem (% m dm))))
  16003. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  16004. (setq m (+ m n)))
  16005. (if (< m 0) (setq m (+ m 60) h (1- h)))
  16006. (if (> m 59) (setq m (- m 60) h (1+ h)))
  16007. (setq h (min 24 (max 0 h)))
  16008. (setq ng 1 new (format "-%02d:%02d" h m)))
  16009. ((org-pos-in-match-range pos 6)
  16010. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  16011. ((org-pos-in-match-range pos 5)
  16012. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  16013. ((org-pos-in-match-range pos 9)
  16014. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  16015. ((org-pos-in-match-range pos 8)
  16016. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  16017. (when ng
  16018. (setq s (concat
  16019. (substring s 0 (match-beginning ng))
  16020. new
  16021. (substring s (match-end ng))))))
  16022. s))
  16023. (defun org-recenter-calendar (date)
  16024. "If the calendar is visible, recenter it to DATE."
  16025. (let ((cwin (get-buffer-window "*Calendar*" t)))
  16026. (when cwin
  16027. (let ((calendar-move-hook nil))
  16028. (with-selected-window cwin
  16029. (calendar-goto-date (if (listp date) date
  16030. (calendar-gregorian-from-absolute date))))))))
  16031. (defun org-goto-calendar (&optional arg)
  16032. "Go to the Emacs calendar at the current date.
  16033. If there is a time stamp in the current line, go to that date.
  16034. A prefix ARG can be used to force the current date."
  16035. (interactive "P")
  16036. (let ((tsr org-ts-regexp) diff
  16037. (calendar-move-hook nil)
  16038. (calendar-view-holidays-initially-flag nil)
  16039. (calendar-view-diary-initially-flag nil))
  16040. (if (or (org-at-timestamp-p)
  16041. (save-excursion
  16042. (beginning-of-line 1)
  16043. (looking-at (concat ".*" tsr))))
  16044. (let ((d1 (time-to-days (current-time)))
  16045. (d2 (time-to-days
  16046. (org-time-string-to-time (match-string 1)))))
  16047. (setq diff (- d2 d1))))
  16048. (calendar)
  16049. (calendar-goto-today)
  16050. (if (and diff (not arg)) (calendar-forward-day diff))))
  16051. (defun org-get-date-from-calendar ()
  16052. "Return a list (month day year) of date at point in calendar."
  16053. (with-current-buffer "*Calendar*"
  16054. (save-match-data
  16055. (calendar-cursor-to-date))))
  16056. (defun org-date-from-calendar ()
  16057. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  16058. If there is already a time stamp at the cursor position, update it."
  16059. (interactive)
  16060. (if (org-at-timestamp-p t)
  16061. (org-timestamp-change 0 'calendar)
  16062. (let ((cal-date (org-get-date-from-calendar)))
  16063. (org-insert-time-stamp
  16064. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  16065. (defcustom org-effort-durations
  16066. `(("h" . 60)
  16067. ("d" . ,(* 60 8))
  16068. ("w" . ,(* 60 8 5))
  16069. ("m" . ,(* 60 8 5 4))
  16070. ("y" . ,(* 60 8 5 40)))
  16071. "Conversion factor to minutes for an effort modifier.
  16072. Each entry has the form (MODIFIER . MINUTES).
  16073. In an effort string, a number followed by MODIFIER is multiplied
  16074. by the specified number of MINUTES to obtain an effort in
  16075. minutes.
  16076. For example, if the value of this variable is ((\"hours\" . 60)), then an
  16077. effort string \"2hours\" is equivalent to 120 minutes."
  16078. :group 'org-agenda
  16079. :version "24.1"
  16080. :type '(alist :key-type (string :tag "Modifier")
  16081. :value-type (number :tag "Minutes")))
  16082. (defun org-minutes-to-clocksum-string (m)
  16083. "Format number of minutes as a clocksum string.
  16084. The format is determined by `org-time-clocksum-format',
  16085. `org-time-clocksum-use-fractional' and
  16086. `org-time-clocksum-fractional-format' and
  16087. `org-time-clocksum-use-effort-durations'."
  16088. (let ((clocksum "")
  16089. (m (round m)) ; Don't allow fractions of minutes
  16090. h d w mo y fmt n)
  16091. (setq h (if org-time-clocksum-use-effort-durations
  16092. (cdr (assoc "h" org-effort-durations)) 60)
  16093. d (if org-time-clocksum-use-effort-durations
  16094. (/ (cdr (assoc "d" org-effort-durations)) h) 24)
  16095. w (if org-time-clocksum-use-effort-durations
  16096. (/ (cdr (assoc "w" org-effort-durations)) (* d h)) 7)
  16097. mo (if org-time-clocksum-use-effort-durations
  16098. (/ (cdr (assoc "m" org-effort-durations)) (* d h)) 30)
  16099. y (if org-time-clocksum-use-effort-durations
  16100. (/ (cdr (assoc "y" org-effort-durations)) (* d h)) 365))
  16101. ;; fractional format
  16102. (if org-time-clocksum-use-fractional
  16103. (cond
  16104. ;; single format string
  16105. ((stringp org-time-clocksum-fractional-format)
  16106. (format org-time-clocksum-fractional-format (/ m (float h))))
  16107. ;; choice of fractional formats for different time units
  16108. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16109. (> (/ (truncate m) (* y d h)) 0))
  16110. (format fmt (/ m (* y d (float h)))))
  16111. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16112. (> (/ (truncate m) (* mo d h)) 0))
  16113. (format fmt (/ m (* mo d (float h)))))
  16114. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16115. (> (/ (truncate m) (* w d h)) 0))
  16116. (format fmt (/ m (* w d (float h)))))
  16117. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16118. (> (/ (truncate m) (* d h)) 0))
  16119. (format fmt (/ m (* d (float h)))))
  16120. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16121. (> (/ (truncate m) h) 0))
  16122. (format fmt (/ m (float h))))
  16123. ((setq fmt (plist-get org-time-clocksum-fractional-format :minutes))
  16124. (format fmt m))
  16125. ;; fall back to smallest time unit with a format
  16126. ((setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16127. (format fmt (/ m (float h))))
  16128. ((setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16129. (format fmt (/ m (* d (float h)))))
  16130. ((setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16131. (format fmt (/ m (* w d (float h)))))
  16132. ((setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16133. (format fmt (/ m (* mo d (float h)))))
  16134. ((setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16135. (format fmt (/ m (* y d (float h))))))
  16136. ;; standard (non-fractional) format, with single format string
  16137. (if (stringp org-time-clocksum-format)
  16138. (format org-time-clocksum-format (setq n (/ m h)) (- m (* h n)))
  16139. ;; separate formats components
  16140. (and (setq fmt (plist-get org-time-clocksum-format :years))
  16141. (or (> (setq n (/ (truncate m) (* y d h))) 0)
  16142. (plist-get org-time-clocksum-format :require-years))
  16143. (setq clocksum (concat clocksum (format fmt n))
  16144. m (- m (* n y d h))))
  16145. (and (setq fmt (plist-get org-time-clocksum-format :months))
  16146. (or (> (setq n (/ (truncate m) (* mo d h))) 0)
  16147. (plist-get org-time-clocksum-format :require-months))
  16148. (setq clocksum (concat clocksum (format fmt n))
  16149. m (- m (* n mo d h))))
  16150. (and (setq fmt (plist-get org-time-clocksum-format :weeks))
  16151. (or (> (setq n (/ (truncate m) (* w d h))) 0)
  16152. (plist-get org-time-clocksum-format :require-weeks))
  16153. (setq clocksum (concat clocksum (format fmt n))
  16154. m (- m (* n w d h))))
  16155. (and (setq fmt (plist-get org-time-clocksum-format :days))
  16156. (or (> (setq n (/ (truncate m) (* d h))) 0)
  16157. (plist-get org-time-clocksum-format :require-days))
  16158. (setq clocksum (concat clocksum (format fmt n))
  16159. m (- m (* n d h))))
  16160. (and (setq fmt (plist-get org-time-clocksum-format :hours))
  16161. (or (> (setq n (/ (truncate m) h)) 0)
  16162. (plist-get org-time-clocksum-format :require-hours))
  16163. (setq clocksum (concat clocksum (format fmt n))
  16164. m (- m (* n h))))
  16165. (and (setq fmt (plist-get org-time-clocksum-format :minutes))
  16166. (or (> m 0) (plist-get org-time-clocksum-format :require-minutes))
  16167. (setq clocksum (concat clocksum (format fmt m))))
  16168. ;; return formatted time duration
  16169. clocksum))))
  16170. (defalias 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string)
  16171. (make-obsolete 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string
  16172. "Org mode version 8.0")
  16173. (defun org-hours-to-clocksum-string (n)
  16174. (org-minutes-to-clocksum-string (* n 60)))
  16175. (defun org-hh:mm-string-to-minutes (s)
  16176. "Convert a string H:MM to a number of minutes.
  16177. If the string is just a number, interpret it as minutes.
  16178. In fact, the first hh:mm or number in the string will be taken,
  16179. there can be extra stuff in the string.
  16180. If no number is found, the return value is 0."
  16181. (cond
  16182. ((integerp s) s)
  16183. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  16184. (+ (* (string-to-number (match-string 1 s)) 60)
  16185. (string-to-number (match-string 2 s))))
  16186. ((string-match "\\([0-9]+\\)" s)
  16187. (string-to-number (match-string 1 s)))
  16188. (t 0)))
  16189. (defcustom org-image-actual-width t
  16190. "Should we use the actual width of images when inlining them?
  16191. When set to `t', always use the image width.
  16192. When set to a number, use imagemagick (when available) to set
  16193. the image's width to this value.
  16194. When set to a number in a list, try to get the width from any
  16195. #+ATTR.* keyword if it matches a width specification like
  16196. #+ATTR_HTML: :width 300px
  16197. and fall back on that number if none is found.
  16198. When set to nil, try to get the width from an #+ATTR.* keyword
  16199. and fall back on the original width if none is found.
  16200. This requires Emacs >= 24.1, build with imagemagick support."
  16201. :group 'org-appearance
  16202. :version "24.4"
  16203. :package-version '(Org . "8.0")
  16204. :type '(choice
  16205. (const :tag "Use the image width" t)
  16206. (integer :tag "Use a number of pixels")
  16207. (list :tag "Use #+ATTR* or a number of pixels" (integer))
  16208. (const :tag "Use #+ATTR* or don't resize" nil)))
  16209. (defcustom org-agenda-inhibit-startup nil
  16210. "Inhibit startup when preparing agenda buffers.
  16211. When this variable is `t' (the default), the initialization of
  16212. the Org agenda buffers is inhibited: e.g. the visibility state
  16213. is not set, the tables are not re-aligned, etc."
  16214. :type 'boolean
  16215. :version "24.3"
  16216. :group 'org-agenda)
  16217. (defun org-duration-string-to-minutes (s &optional output-to-string)
  16218. "Convert a duration string S to minutes.
  16219. A bare number is interpreted as minutes, modifiers can be set by
  16220. customizing `org-effort-durations' (which see).
  16221. Entries containing a colon are interpreted as H:MM by
  16222. `org-hh:mm-string-to-minutes'."
  16223. (let ((result 0)
  16224. (re (concat "\\([0-9.]+\\) *\\("
  16225. (regexp-opt (mapcar 'car org-effort-durations))
  16226. "\\)")))
  16227. (while (string-match re s)
  16228. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  16229. (string-to-number (match-string 1 s))))
  16230. (setq s (replace-match "" nil t s)))
  16231. (setq result (floor result))
  16232. (incf result (org-hh:mm-string-to-minutes s))
  16233. (if output-to-string (number-to-string result) result)))
  16234. ;;;; Files
  16235. (defun org-save-all-org-buffers ()
  16236. "Save all Org-mode buffers without user confirmation."
  16237. (interactive)
  16238. (message "Saving all Org-mode buffers...")
  16239. (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
  16240. (when (featurep 'org-id) (org-id-locations-save))
  16241. (message "Saving all Org-mode buffers... done"))
  16242. (defun org-revert-all-org-buffers ()
  16243. "Revert all Org-mode buffers.
  16244. Prompt for confirmation when there are unsaved changes.
  16245. Be sure you know what you are doing before letting this function
  16246. overwrite your changes.
  16247. This function is useful in a setup where one tracks org files
  16248. with a version control system, to revert on one machine after pulling
  16249. changes from another. I believe the procedure must be like this:
  16250. 1. M-x org-save-all-org-buffers
  16251. 2. Pull changes from the other machine, resolve conflicts
  16252. 3. M-x org-revert-all-org-buffers"
  16253. (interactive)
  16254. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  16255. (user-error "Abort"))
  16256. (save-excursion
  16257. (save-window-excursion
  16258. (mapc
  16259. (lambda (b)
  16260. (when (and (with-current-buffer b (derived-mode-p 'org-mode))
  16261. (with-current-buffer b buffer-file-name))
  16262. (org-pop-to-buffer-same-window b)
  16263. (revert-buffer t 'no-confirm)))
  16264. (buffer-list))
  16265. (when (and (featurep 'org-id) org-id-track-globally)
  16266. (org-id-locations-load)))))
  16267. ;;;; Agenda files
  16268. ;;;###autoload
  16269. (defun org-switchb (&optional arg)
  16270. "Switch between Org buffers.
  16271. With one prefix argument, restrict available buffers to files.
  16272. With two prefix arguments, restrict available buffers to agenda files.
  16273. Defaults to `iswitchb' for buffer name completion.
  16274. Set `org-completion-use-ido' to make it use ido instead."
  16275. (interactive "P")
  16276. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  16277. ((equal arg '(16)) (org-buffer-list 'agenda))
  16278. (t (org-buffer-list))))
  16279. (org-completion-use-iswitchb org-completion-use-iswitchb)
  16280. (org-completion-use-ido org-completion-use-ido))
  16281. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  16282. (setq org-completion-use-iswitchb t))
  16283. (org-pop-to-buffer-same-window
  16284. (org-icompleting-read "Org buffer: "
  16285. (mapcar 'list (mapcar 'buffer-name blist))
  16286. nil t))))
  16287. ;;; Define some older names previously used for this functionality
  16288. ;;;###autoload
  16289. (defalias 'org-ido-switchb 'org-switchb)
  16290. ;;;###autoload
  16291. (defalias 'org-iswitchb 'org-switchb)
  16292. (defun org-buffer-list (&optional predicate exclude-tmp)
  16293. "Return a list of Org buffers.
  16294. PREDICATE can be `export', `files' or `agenda'.
  16295. export restrict the list to Export buffers.
  16296. files restrict the list to buffers visiting Org files.
  16297. agenda restrict the list to buffers visiting agenda files.
  16298. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  16299. (let* ((bfn nil)
  16300. (agenda-files (and (eq predicate 'agenda)
  16301. (mapcar 'file-truename (org-agenda-files t))))
  16302. (filter
  16303. (cond
  16304. ((eq predicate 'files)
  16305. (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
  16306. ((eq predicate 'export)
  16307. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  16308. ((eq predicate 'agenda)
  16309. (lambda (b)
  16310. (with-current-buffer b
  16311. (and (derived-mode-p 'org-mode)
  16312. (setq bfn (buffer-file-name b))
  16313. (member (file-truename bfn) agenda-files)))))
  16314. (t (lambda (b) (with-current-buffer b
  16315. (or (derived-mode-p 'org-mode)
  16316. (string-match "\*Org .*Export"
  16317. (buffer-name b)))))))))
  16318. (delq nil
  16319. (mapcar
  16320. (lambda(b)
  16321. (if (and (funcall filter b)
  16322. (or (not exclude-tmp)
  16323. (not (string-match "tmp" (buffer-name b)))))
  16324. b
  16325. nil))
  16326. (buffer-list)))))
  16327. (defun org-agenda-files (&optional unrestricted archives)
  16328. "Get the list of agenda files.
  16329. Optional UNRESTRICTED means return the full list even if a restriction
  16330. is currently in place.
  16331. When ARCHIVES is t, include all archive files that are really being
  16332. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  16333. `org-agenda-archives-mode' is t."
  16334. (let ((files
  16335. (cond
  16336. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  16337. ((stringp org-agenda-files) (org-read-agenda-file-list))
  16338. ((listp org-agenda-files) org-agenda-files)
  16339. (t (error "Invalid value of `org-agenda-files'")))))
  16340. (setq files (apply 'append
  16341. (mapcar (lambda (f)
  16342. (if (file-directory-p f)
  16343. (directory-files
  16344. f t org-agenda-file-regexp)
  16345. (list f)))
  16346. files)))
  16347. (when org-agenda-skip-unavailable-files
  16348. (setq files (delq nil
  16349. (mapcar (function
  16350. (lambda (file)
  16351. (and (file-readable-p file) file)))
  16352. files))))
  16353. (when (or (eq archives t)
  16354. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  16355. (setq files (org-add-archive-files files)))
  16356. files))
  16357. (defun org-agenda-file-p (&optional file)
  16358. "Return non-nil, if FILE is an agenda file.
  16359. If FILE is omitted, use the file associated with the current
  16360. buffer."
  16361. (member (or file (buffer-file-name))
  16362. (org-agenda-files t)))
  16363. (defun org-edit-agenda-file-list ()
  16364. "Edit the list of agenda files.
  16365. Depending on setup, this either uses customize to edit the variable
  16366. `org-agenda-files', or it visits the file that is holding the list. In the
  16367. latter case, the buffer is set up in a way that saving it automatically kills
  16368. the buffer and restores the previous window configuration."
  16369. (interactive)
  16370. (if (stringp org-agenda-files)
  16371. (let ((cw (current-window-configuration)))
  16372. (find-file org-agenda-files)
  16373. (org-set-local 'org-window-configuration cw)
  16374. (org-add-hook 'after-save-hook
  16375. (lambda ()
  16376. (set-window-configuration
  16377. (prog1 org-window-configuration
  16378. (kill-buffer (current-buffer))))
  16379. (org-install-agenda-files-menu)
  16380. (message "New agenda file list installed"))
  16381. nil 'local)
  16382. (message "%s" (substitute-command-keys
  16383. "Edit list and finish with \\[save-buffer]")))
  16384. (customize-variable 'org-agenda-files)))
  16385. (defun org-store-new-agenda-file-list (list)
  16386. "Set new value for the agenda file list and save it correctly."
  16387. (if (stringp org-agenda-files)
  16388. (let ((fe (org-read-agenda-file-list t)) b u)
  16389. (while (setq b (find-buffer-visiting org-agenda-files))
  16390. (kill-buffer b))
  16391. (with-temp-file org-agenda-files
  16392. (insert
  16393. (mapconcat
  16394. (lambda (f) ;; Keep un-expanded entries.
  16395. (if (setq u (assoc f fe))
  16396. (cdr u)
  16397. f))
  16398. list "\n")
  16399. "\n")))
  16400. (let ((org-mode-hook nil) (org-inhibit-startup t)
  16401. (org-insert-mode-line-in-empty-file nil))
  16402. (setq org-agenda-files list)
  16403. (customize-save-variable 'org-agenda-files org-agenda-files))))
  16404. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  16405. "Read the list of agenda files from a file.
  16406. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  16407. filenames, used by `org-store-new-agenda-file-list' to write back
  16408. un-expanded file names."
  16409. (when (file-directory-p org-agenda-files)
  16410. (error "`org-agenda-files' cannot be a single directory"))
  16411. (when (stringp org-agenda-files)
  16412. (with-temp-buffer
  16413. (insert-file-contents org-agenda-files)
  16414. (mapcar
  16415. (lambda (f)
  16416. (let ((e (expand-file-name (substitute-in-file-name f)
  16417. org-directory)))
  16418. (if pair-with-expansion
  16419. (cons e f)
  16420. e)))
  16421. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  16422. ;;;###autoload
  16423. (defun org-cycle-agenda-files ()
  16424. "Cycle through the files in `org-agenda-files'.
  16425. If the current buffer visits an agenda file, find the next one in the list.
  16426. If the current buffer does not, find the first agenda file."
  16427. (interactive)
  16428. (let* ((fs (org-agenda-files t))
  16429. (files (append fs (list (car fs))))
  16430. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  16431. file)
  16432. (unless files (user-error "No agenda files"))
  16433. (catch 'exit
  16434. (while (setq file (pop files))
  16435. (if (equal (file-truename file) tcf)
  16436. (when (car files)
  16437. (find-file (car files))
  16438. (throw 'exit t))))
  16439. (find-file (car fs)))
  16440. (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  16441. (defun org-agenda-file-to-front (&optional to-end)
  16442. "Move/add the current file to the top of the agenda file list.
  16443. If the file is not present in the list, it is added to the front. If it is
  16444. present, it is moved there. With optional argument TO-END, add/move to the
  16445. end of the list."
  16446. (interactive "P")
  16447. (let ((org-agenda-skip-unavailable-files nil)
  16448. (file-alist (mapcar (lambda (x)
  16449. (cons (file-truename x) x))
  16450. (org-agenda-files t)))
  16451. (ctf (file-truename
  16452. (or buffer-file-name
  16453. (user-error "Please save the current buffer to a file"))))
  16454. x had)
  16455. (setq x (assoc ctf file-alist) had x)
  16456. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  16457. (if to-end
  16458. (setq file-alist (append (delq x file-alist) (list x)))
  16459. (setq file-alist (cons x (delq x file-alist))))
  16460. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  16461. (org-install-agenda-files-menu)
  16462. (message "File %s to %s of agenda file list"
  16463. (if had "moved" "added") (if to-end "end" "front"))))
  16464. (defun org-remove-file (&optional file)
  16465. "Remove current file from the list of files in variable `org-agenda-files'.
  16466. These are the files which are being checked for agenda entries.
  16467. Optional argument FILE means use this file instead of the current."
  16468. (interactive)
  16469. (let* ((org-agenda-skip-unavailable-files nil)
  16470. (file (or file buffer-file-name
  16471. (user-error "Current buffer does not visit a file")))
  16472. (true-file (file-truename file))
  16473. (afile (abbreviate-file-name file))
  16474. (files (delq nil (mapcar
  16475. (lambda (x)
  16476. (if (equal true-file
  16477. (file-truename x))
  16478. nil x))
  16479. (org-agenda-files t)))))
  16480. (if (not (= (length files) (length (org-agenda-files t))))
  16481. (progn
  16482. (org-store-new-agenda-file-list files)
  16483. (org-install-agenda-files-menu)
  16484. (message "Removed file: %s" afile))
  16485. (message "File was not in list: %s (not removed)" afile))))
  16486. (defun org-file-menu-entry (file)
  16487. (vector file (list 'find-file file) t))
  16488. (defun org-check-agenda-file (file)
  16489. "Make sure FILE exists. If not, ask user what to do."
  16490. (when (not (file-exists-p file))
  16491. (message "Non-existent agenda file %s. [R]emove from list or [A]bort?"
  16492. (abbreviate-file-name file))
  16493. (let ((r (downcase (read-char-exclusive))))
  16494. (cond
  16495. ((equal r ?r)
  16496. (org-remove-file file)
  16497. (throw 'nextfile t))
  16498. (t (error "Abort"))))))
  16499. (defun org-get-agenda-file-buffer (file)
  16500. "Get a buffer visiting FILE. If the buffer needs to be created, add
  16501. it to the list of buffers which might be released later."
  16502. (let ((buf (org-find-base-buffer-visiting file)))
  16503. (if buf
  16504. buf ; just return it
  16505. ;; Make a new buffer and remember it
  16506. (setq buf (find-file-noselect file))
  16507. (if buf (push buf org-agenda-new-buffers))
  16508. buf)))
  16509. (defun org-release-buffers (blist)
  16510. "Release all buffers in list, asking the user for confirmation when needed.
  16511. When a buffer is unmodified, it is just killed. When modified, it is saved
  16512. \(if the user agrees) and then killed."
  16513. (let (buf file)
  16514. (while (setq buf (pop blist))
  16515. (setq file (buffer-file-name buf))
  16516. (when (and (buffer-modified-p buf)
  16517. file
  16518. (y-or-n-p (format "Save file %s? " file)))
  16519. (with-current-buffer buf (save-buffer)))
  16520. (kill-buffer buf))))
  16521. (defun org-agenda-prepare-buffers (files)
  16522. "Create buffers for all agenda files, protect archived trees and comments."
  16523. (interactive)
  16524. (let ((pa '(:org-archived t))
  16525. (pc '(:org-comment t))
  16526. (pall '(:org-archived t :org-comment t))
  16527. (inhibit-read-only t)
  16528. (org-inhibit-startup org-agenda-inhibit-startup)
  16529. (rea (concat ":" org-archive-tag ":"))
  16530. file re)
  16531. (setq org-tag-alist-for-agenda nil
  16532. org-tag-groups-alist-for-agenda nil)
  16533. (save-excursion
  16534. (save-restriction
  16535. (while (setq file (pop files))
  16536. (catch 'nextfile
  16537. (if (bufferp file)
  16538. (set-buffer file)
  16539. (org-check-agenda-file file)
  16540. (set-buffer (org-get-agenda-file-buffer file)))
  16541. (widen)
  16542. (org-set-regexps-and-options-for-tags)
  16543. (goto-char (point-min))
  16544. (let ((case-fold-search t))
  16545. (when (search-forward "#+setupfile" nil t)
  16546. ;; Don't set all regexps and options systematically as
  16547. ;; this is only run for setting agenda tags from setup
  16548. ;; file
  16549. (org-set-regexps-and-options)))
  16550. (org-refresh-category-properties)
  16551. (org-refresh-properties org-effort-property 'org-effort)
  16552. (org-refresh-properties "APPT_WARNTIME" 'org-appt-warntime)
  16553. (setq org-todo-keywords-for-agenda
  16554. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  16555. (setq org-done-keywords-for-agenda
  16556. (append org-done-keywords-for-agenda org-done-keywords))
  16557. (setq org-todo-keyword-alist-for-agenda
  16558. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  16559. (setq org-drawers-for-agenda
  16560. (append org-drawers-for-agenda org-drawers))
  16561. (setq org-tag-alist-for-agenda
  16562. (org-uniquify
  16563. (append org-tag-alist-for-agenda
  16564. org-tag-alist
  16565. org-tag-persistent-alist)))
  16566. (if org-group-tags
  16567. (setq org-tag-groups-alist-for-agenda
  16568. (org-uniquify-alist
  16569. (append org-tag-groups-alist-for-agenda org-tag-groups-alist))))
  16570. (org-with-silent-modifications
  16571. (save-excursion
  16572. (remove-text-properties (point-min) (point-max) pall)
  16573. (when org-agenda-skip-archived-trees
  16574. (goto-char (point-min))
  16575. (while (re-search-forward rea nil t)
  16576. (if (org-at-heading-p t)
  16577. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  16578. (goto-char (point-min))
  16579. (setq re (format org-heading-keyword-regexp-format
  16580. org-comment-string))
  16581. (while (re-search-forward re nil t)
  16582. (add-text-properties
  16583. (match-beginning 0) (org-end-of-subtree t) pc))))))))
  16584. (setq org-todo-keywords-for-agenda
  16585. (org-uniquify org-todo-keywords-for-agenda))
  16586. (setq org-todo-keyword-alist-for-agenda
  16587. (org-uniquify org-todo-keyword-alist-for-agenda))))
  16588. ;;;; CDLaTeX minor mode
  16589. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  16590. "Keymap for the minor `org-cdlatex-mode'.")
  16591. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  16592. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  16593. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  16594. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  16595. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  16596. (defvar org-cdlatex-texmathp-advice-is-done nil
  16597. "Flag remembering if we have applied the advice to texmathp already.")
  16598. (define-minor-mode org-cdlatex-mode
  16599. "Toggle the minor `org-cdlatex-mode'.
  16600. This mode supports entering LaTeX environment and math in LaTeX fragments
  16601. in Org-mode.
  16602. \\{org-cdlatex-mode-map}"
  16603. nil " OCDL" nil
  16604. (when org-cdlatex-mode
  16605. (require 'cdlatex)
  16606. (run-hooks 'cdlatex-mode-hook)
  16607. (cdlatex-compute-tables))
  16608. (unless org-cdlatex-texmathp-advice-is-done
  16609. (setq org-cdlatex-texmathp-advice-is-done t)
  16610. (defadvice texmathp (around org-math-always-on activate)
  16611. "Always return t in org-mode buffers.
  16612. This is because we want to insert math symbols without dollars even outside
  16613. the LaTeX math segments. If Orgmode thinks that point is actually inside
  16614. an embedded LaTeX fragment, let texmathp do its job.
  16615. \\[org-cdlatex-mode-map]"
  16616. (interactive)
  16617. (let (p)
  16618. (cond
  16619. ((not (derived-mode-p 'org-mode)) ad-do-it)
  16620. ((eq this-command 'cdlatex-math-symbol)
  16621. (setq ad-return-value t
  16622. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  16623. (t
  16624. (let ((p (org-inside-LaTeX-fragment-p)))
  16625. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  16626. (setq ad-return-value t
  16627. texmathp-why '("Org-mode embedded math" . 0))
  16628. (if p ad-do-it)))))))))
  16629. (defun turn-on-org-cdlatex ()
  16630. "Unconditionally turn on `org-cdlatex-mode'."
  16631. (org-cdlatex-mode 1))
  16632. (defun org-try-cdlatex-tab ()
  16633. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  16634. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  16635. - inside a LaTeX fragment, or
  16636. - after the first word in a line, where an abbreviation expansion could
  16637. insert a LaTeX environment."
  16638. (when org-cdlatex-mode
  16639. (cond
  16640. ;; Before any word on the line: No expansion possible.
  16641. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  16642. ;; Just after first word on the line: Expand it. Make sure it
  16643. ;; cannot happen on headlines, though.
  16644. ((save-excursion
  16645. (skip-chars-backward "a-zA-Z0-9*")
  16646. (skip-chars-backward " \t")
  16647. (and (bolp) (not (org-at-heading-p))))
  16648. (cdlatex-tab) t)
  16649. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  16650. (defun org-cdlatex-underscore-caret (&optional arg)
  16651. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  16652. Revert to the normal definition outside of these fragments."
  16653. (interactive "P")
  16654. (if (org-inside-LaTeX-fragment-p)
  16655. (call-interactively 'cdlatex-sub-superscript)
  16656. (let (org-cdlatex-mode)
  16657. (call-interactively (key-binding (vector last-input-event))))))
  16658. (defun org-cdlatex-math-modify (&optional arg)
  16659. "Execute `cdlatex-math-modify' in LaTeX fragments.
  16660. Revert to the normal definition outside of these fragments."
  16661. (interactive "P")
  16662. (if (org-inside-LaTeX-fragment-p)
  16663. (call-interactively 'cdlatex-math-modify)
  16664. (let (org-cdlatex-mode)
  16665. (call-interactively (key-binding (vector last-input-event))))))
  16666. ;;;; LaTeX fragments
  16667. (defvar org-latex-regexps
  16668. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  16669. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  16670. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  16671. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  16672. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  16673. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  16674. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  16675. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  16676. "Regular expressions for matching embedded LaTeX.")
  16677. (defun org-inside-LaTeX-fragment-p ()
  16678. "Test if point is inside a LaTeX fragment.
  16679. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  16680. sequence appearing also before point.
  16681. Even though the matchers for math are configurable, this function assumes
  16682. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  16683. delimiters are skipped when they have been removed by customization.
  16684. The return value is nil, or a cons cell with the delimiter and the
  16685. position of this delimiter.
  16686. This function does a reasonably good job, but can locally be fooled by
  16687. for example currency specifications. For example it will assume being in
  16688. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  16689. fragments that are properly closed, but during editing, we have to live
  16690. with the uncertainty caused by missing closing delimiters. This function
  16691. looks only before point, not after."
  16692. (catch 'exit
  16693. (let ((pos (point))
  16694. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  16695. (lim (progn
  16696. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  16697. (point)))
  16698. dd-on str (start 0) m re)
  16699. (goto-char pos)
  16700. (when dodollar
  16701. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  16702. re (nth 1 (assoc "$" org-latex-regexps)))
  16703. (while (string-match re str start)
  16704. (cond
  16705. ((= (match-end 0) (length str))
  16706. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  16707. ((= (match-end 0) (- (length str) 5))
  16708. (throw 'exit nil))
  16709. (t (setq start (match-end 0))))))
  16710. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  16711. (goto-char pos)
  16712. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  16713. (and (match-beginning 2) (throw 'exit nil))
  16714. ;; count $$
  16715. (while (re-search-backward "\\$\\$" lim t)
  16716. (setq dd-on (not dd-on)))
  16717. (goto-char pos)
  16718. (if dd-on (cons "$$" m))))))
  16719. (defun org-inside-latex-macro-p ()
  16720. "Is point inside a LaTeX macro or its arguments?"
  16721. (save-match-data
  16722. (org-in-regexp
  16723. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  16724. (defvar org-latex-fragment-image-overlays nil
  16725. "List of overlays carrying the images of latex fragments.")
  16726. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  16727. (defun org-remove-latex-fragment-image-overlays ()
  16728. "Remove all overlays with LaTeX fragment images in current buffer."
  16729. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  16730. (setq org-latex-fragment-image-overlays nil))
  16731. (defun org-preview-latex-fragment (&optional subtree)
  16732. "Preview the LaTeX fragment at point, or all locally or globally.
  16733. If the cursor is in a LaTeX fragment, create the image and overlay
  16734. it over the source code. If there is no fragment at point, display
  16735. all fragments in the current text, from one headline to the next. With
  16736. prefix SUBTREE, display all fragments in the current subtree. With a
  16737. double prefix arg \\[universal-argument] \\[universal-argument], or when \
  16738. the cursor is before the first headline,
  16739. display all fragments in the buffer.
  16740. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  16741. (interactive "P")
  16742. (unless buffer-file-name
  16743. (user-error "Can't preview LaTeX fragment in a non-file buffer"))
  16744. (when (display-graphic-p)
  16745. (org-remove-latex-fragment-image-overlays)
  16746. (save-excursion
  16747. (save-restriction
  16748. (let (beg end at msg)
  16749. (cond
  16750. ((or (equal subtree '(16))
  16751. (not (save-excursion
  16752. (re-search-backward org-outline-regexp-bol nil t))))
  16753. (setq beg (point-min) end (point-max)
  16754. msg "Creating images for buffer...%s"))
  16755. ((equal subtree '(4))
  16756. (org-back-to-heading)
  16757. (setq beg (point) end (org-end-of-subtree t)
  16758. msg "Creating images for subtree...%s"))
  16759. (t
  16760. (if (setq at (org-inside-LaTeX-fragment-p))
  16761. (goto-char (max (point-min) (- (cdr at) 2)))
  16762. (org-back-to-heading))
  16763. (setq beg (point) end (progn (outline-next-heading) (point))
  16764. msg (if at "Creating image...%s"
  16765. "Creating images for entry...%s"))))
  16766. (message msg "")
  16767. (narrow-to-region beg end)
  16768. (goto-char beg)
  16769. (org-format-latex
  16770. (concat org-latex-preview-ltxpng-directory (file-name-sans-extension
  16771. (file-name-nondirectory
  16772. buffer-file-name)))
  16773. default-directory 'overlays msg at 'forbuffer
  16774. org-latex-create-formula-image-program)
  16775. (message msg "done. Use `C-c C-c' to remove images."))))))
  16776. (defun org-format-latex (prefix &optional dir overlays msg at
  16777. forbuffer processing-type)
  16778. "Replace LaTeX fragments with links to an image, and produce images.
  16779. Some of the options can be changed using the variable
  16780. `org-format-latex-options'."
  16781. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  16782. (let* ((prefixnodir (file-name-nondirectory prefix))
  16783. (absprefix (expand-file-name prefix dir))
  16784. (todir (file-name-directory absprefix))
  16785. (opt org-format-latex-options)
  16786. (optnew org-format-latex-options)
  16787. (matchers (plist-get opt :matchers))
  16788. (re-list org-latex-regexps)
  16789. (cnt 0) txt hash link beg end re e checkdir
  16790. string
  16791. m n block-type block linkfile movefile ov)
  16792. ;; Check the different regular expressions
  16793. (while (setq e (pop re-list))
  16794. (setq m (car e) re (nth 1 e) n (nth 2 e) block-type (nth 3 e)
  16795. block (if block-type "\n\n" ""))
  16796. (when (member m matchers)
  16797. (goto-char (point-min))
  16798. (while (re-search-forward re nil t)
  16799. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  16800. (or (not overlays)
  16801. (not (eq (get-char-property (match-beginning n)
  16802. 'org-overlay-type)
  16803. 'org-latex-overlay))))
  16804. (cond
  16805. ((eq processing-type 'verbatim))
  16806. ((eq processing-type 'mathjax)
  16807. ;; Prepare for MathJax processing.
  16808. (setq string (match-string n))
  16809. (when (member m '("$" "$1"))
  16810. (save-excursion
  16811. (delete-region (match-beginning n) (match-end n))
  16812. (goto-char (match-beginning n))
  16813. (insert (concat "\\(" (substring string 1 -1) "\\)")))))
  16814. ((or (eq processing-type 'dvipng)
  16815. (eq processing-type 'imagemagick))
  16816. ;; Process to an image.
  16817. (setq txt (match-string n)
  16818. beg (match-beginning n) end (match-end n)
  16819. cnt (1+ cnt))
  16820. (let ((face (face-at-point))
  16821. (fg (plist-get opt :foreground))
  16822. (bg (plist-get opt :background))
  16823. ;; Ensure full list is printed.
  16824. print-length print-level)
  16825. (when forbuffer
  16826. ;; Get the colors from the face at point.
  16827. (goto-char beg)
  16828. (when (eq fg 'auto)
  16829. (setq fg (face-attribute face :foreground nil 'default)))
  16830. (when (eq bg 'auto)
  16831. (setq bg (face-attribute face :background nil 'default)))
  16832. (setq optnew (copy-sequence opt))
  16833. (plist-put optnew :foreground fg)
  16834. (plist-put optnew :background bg))
  16835. (setq hash (sha1 (prin1-to-string
  16836. (list org-format-latex-header
  16837. org-latex-default-packages-alist
  16838. org-latex-packages-alist
  16839. org-format-latex-options
  16840. forbuffer txt fg bg)))
  16841. linkfile (format "%s_%s.png" prefix hash)
  16842. movefile (format "%s_%s.png" absprefix hash)))
  16843. (setq link (concat block "[[file:" linkfile "]]" block))
  16844. (if msg (message msg cnt))
  16845. (goto-char beg)
  16846. (unless checkdir ; Ensure the directory exists.
  16847. (setq checkdir t)
  16848. (or (file-directory-p todir) (make-directory todir t)))
  16849. (unless (file-exists-p movefile)
  16850. (org-create-formula-image
  16851. txt movefile optnew forbuffer processing-type))
  16852. (if overlays
  16853. (progn
  16854. (mapc (lambda (o)
  16855. (if (eq (overlay-get o 'org-overlay-type)
  16856. 'org-latex-overlay)
  16857. (delete-overlay o)))
  16858. (overlays-in beg end))
  16859. (setq ov (make-overlay beg end))
  16860. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  16861. (if (featurep 'xemacs)
  16862. (progn
  16863. (overlay-put ov 'invisible t)
  16864. (overlay-put
  16865. ov 'end-glyph
  16866. (make-glyph (vector 'png :file movefile))))
  16867. (overlay-put
  16868. ov 'display
  16869. (list 'image :type 'png :file movefile :ascent 'center)))
  16870. (push ov org-latex-fragment-image-overlays)
  16871. (goto-char end))
  16872. (delete-region beg end)
  16873. (insert (org-add-props link
  16874. (list 'org-latex-src
  16875. (replace-regexp-in-string
  16876. "\"" "" txt)
  16877. 'org-latex-src-embed-type
  16878. (if block-type 'paragraph 'character))))))
  16879. ((eq processing-type 'mathml)
  16880. ;; Process to MathML
  16881. (unless (save-match-data (org-format-latex-mathml-available-p))
  16882. (user-error "LaTeX to MathML converter not configured"))
  16883. (setq txt (match-string n)
  16884. beg (match-beginning n) end (match-end n)
  16885. cnt (1+ cnt))
  16886. (if msg (message msg cnt))
  16887. (goto-char beg)
  16888. (delete-region beg end)
  16889. (insert (org-format-latex-as-mathml
  16890. txt block-type prefix dir)))
  16891. (t
  16892. (error "Unknown conversion type %s for LaTeX fragments"
  16893. processing-type)))))))))
  16894. (defun org-create-math-formula (latex-frag &optional mathml-file)
  16895. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  16896. Use `org-latex-to-mathml-convert-command'. If the conversion is
  16897. sucessful, return the portion between \"<math...> </math>\"
  16898. elements otherwise return nil. When MATHML-FILE is specified,
  16899. write the results in to that file. When invoked as an
  16900. interactive command, prompt for LATEX-FRAG, with initial value
  16901. set to the current active region and echo the results for user
  16902. inspection."
  16903. (interactive (list (let ((frag (when (org-region-active-p)
  16904. (buffer-substring-no-properties
  16905. (region-beginning) (region-end)))))
  16906. (read-string "LaTeX Fragment: " frag nil frag))))
  16907. (unless latex-frag (error "Invalid LaTeX fragment"))
  16908. (let* ((tmp-in-file (file-relative-name
  16909. (make-temp-name (expand-file-name "ltxmathml-in"))))
  16910. (ignore (write-region latex-frag nil tmp-in-file))
  16911. (tmp-out-file (file-relative-name
  16912. (make-temp-name (expand-file-name "ltxmathml-out"))))
  16913. (cmd (format-spec
  16914. org-latex-to-mathml-convert-command
  16915. `((?j . ,(shell-quote-argument
  16916. (expand-file-name org-latex-to-mathml-jar-file)))
  16917. (?I . ,(shell-quote-argument tmp-in-file))
  16918. (?o . ,(shell-quote-argument tmp-out-file)))))
  16919. mathml shell-command-output)
  16920. (when (org-called-interactively-p 'any)
  16921. (unless (org-format-latex-mathml-available-p)
  16922. (user-error "LaTeX to MathML converter not configured")))
  16923. (message "Running %s" cmd)
  16924. (setq shell-command-output (shell-command-to-string cmd))
  16925. (setq mathml
  16926. (when (file-readable-p tmp-out-file)
  16927. (with-current-buffer (find-file-noselect tmp-out-file t)
  16928. (goto-char (point-min))
  16929. (when (re-search-forward
  16930. (concat
  16931. (regexp-quote
  16932. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
  16933. "\\(.\\|\n\\)*"
  16934. (regexp-quote "</math>")) nil t)
  16935. (prog1 (match-string 0) (kill-buffer))))))
  16936. (cond
  16937. (mathml
  16938. (setq mathml
  16939. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  16940. (when mathml-file
  16941. (write-region mathml nil mathml-file))
  16942. (when (org-called-interactively-p 'any)
  16943. (message mathml)))
  16944. ((message "LaTeX to MathML conversion failed")
  16945. (message shell-command-output)))
  16946. (delete-file tmp-in-file)
  16947. (when (file-exists-p tmp-out-file)
  16948. (delete-file tmp-out-file))
  16949. mathml))
  16950. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  16951. prefix &optional dir)
  16952. "Use `org-create-math-formula' but check local cache first."
  16953. (let* ((absprefix (expand-file-name prefix dir))
  16954. (print-length nil) (print-level nil)
  16955. (formula-id (concat
  16956. "formula-"
  16957. (sha1
  16958. (prin1-to-string
  16959. (list latex-frag
  16960. org-latex-to-mathml-convert-command)))))
  16961. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  16962. (formula-cache-dir (file-name-directory formula-cache)))
  16963. (unless (file-directory-p formula-cache-dir)
  16964. (make-directory formula-cache-dir t))
  16965. (unless (file-exists-p formula-cache)
  16966. (org-create-math-formula latex-frag formula-cache))
  16967. (if (file-exists-p formula-cache)
  16968. ;; Successful conversion. Return the link to MathML file.
  16969. (org-add-props
  16970. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  16971. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  16972. 'org-latex-src-embed-type (if latex-frag-type
  16973. 'paragraph 'character)))
  16974. ;; Failed conversion. Return the LaTeX fragment verbatim
  16975. latex-frag)))
  16976. (defun org-create-formula-image (string tofile options buffer &optional type)
  16977. "Create an image from LaTeX source using dvipng or convert.
  16978. This function calls either `org-create-formula-image-with-dvipng'
  16979. or `org-create-formula-image-with-imagemagick' depending on the
  16980. value of `org-latex-create-formula-image-program' or on the value
  16981. of the optional TYPE variable.
  16982. Note: ultimately these two function should be combined as they
  16983. share a good deal of logic."
  16984. (org-check-external-command
  16985. "latex" "needed to convert LaTeX fragments to images")
  16986. (funcall
  16987. (case (or type org-latex-create-formula-image-program)
  16988. ('dvipng
  16989. (org-check-external-command
  16990. "dvipng" "needed to convert LaTeX fragments to images")
  16991. #'org-create-formula-image-with-dvipng)
  16992. ('imagemagick
  16993. (org-check-external-command
  16994. "convert" "you need to install imagemagick")
  16995. #'org-create-formula-image-with-imagemagick)
  16996. (t (error
  16997. "Invalid value of `org-latex-create-formula-image-program'")))
  16998. string tofile options buffer))
  16999. (declare-function org-export--get-global-options "ox" (&optional backend))
  17000. (declare-function org-export--get-inbuffer-options "ox" (&optional backend))
  17001. (defun org-create-formula--latex-header ()
  17002. "Return LaTeX header appropriate for previewing a LaTeX snippet."
  17003. (org-latex-guess-inputenc
  17004. (org-splice-latex-header
  17005. org-format-latex-header
  17006. org-latex-default-packages-alist
  17007. org-latex-packages-alist t
  17008. (plist-get
  17009. (org-combine-plists
  17010. (org-export--get-global-options 'latex)
  17011. (org-export--get-inbuffer-options 'latex))
  17012. :latex-header))))
  17013. ;; This function borrows from Ganesh Swami's latex2png.el
  17014. (defun org-create-formula-image-with-dvipng (string tofile options buffer)
  17015. "This calls dvipng."
  17016. (require 'ox-latex)
  17017. (let* ((tmpdir (if (featurep 'xemacs)
  17018. (temp-directory)
  17019. temporary-file-directory))
  17020. (texfilebase (make-temp-name
  17021. (expand-file-name "orgtex" tmpdir)))
  17022. (texfile (concat texfilebase ".tex"))
  17023. (dvifile (concat texfilebase ".dvi"))
  17024. (pngfile (concat texfilebase ".png"))
  17025. (fnh (if (featurep 'xemacs)
  17026. (font-height (face-font 'default))
  17027. (face-attribute 'default :height nil)))
  17028. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  17029. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  17030. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  17031. "Black"))
  17032. (bg (or (plist-get options (if buffer :background :html-background))
  17033. "Transparent")))
  17034. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground))
  17035. (unless (string= fg "Transparent") (setq fg (org-dvipng-color-format fg))))
  17036. (if (eq bg 'default) (setq bg (org-dvipng-color :background))
  17037. (unless (string= bg "Transparent") (setq bg (org-dvipng-color-format bg))))
  17038. (let ((latex-header (org-create-formula--latex-header)))
  17039. (with-temp-file texfile
  17040. (insert latex-header)
  17041. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")))
  17042. (let ((dir default-directory))
  17043. (condition-case nil
  17044. (progn
  17045. (cd tmpdir)
  17046. (call-process "latex" nil nil nil texfile))
  17047. (error nil))
  17048. (cd dir))
  17049. (if (not (file-exists-p dvifile))
  17050. (progn (message "Failed to create dvi file from %s" texfile) nil)
  17051. (condition-case nil
  17052. (if (featurep 'xemacs)
  17053. (call-process "dvipng" nil nil nil
  17054. "-fg" fg "-bg" bg
  17055. "-T" "tight"
  17056. "-o" pngfile
  17057. dvifile)
  17058. (call-process "dvipng" nil nil nil
  17059. "-fg" fg "-bg" bg
  17060. "-D" dpi
  17061. ;;"-x" scale "-y" scale
  17062. "-T" "tight"
  17063. "-o" pngfile
  17064. dvifile))
  17065. (error nil))
  17066. (if (not (file-exists-p pngfile))
  17067. (if org-format-latex-signal-error
  17068. (error "Failed to create png file from %s" texfile)
  17069. (message "Failed to create png file from %s" texfile)
  17070. nil)
  17071. ;; Use the requested file name and clean up
  17072. (copy-file pngfile tofile 'replace)
  17073. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png" ".out") do
  17074. (if (file-exists-p (concat texfilebase e))
  17075. (delete-file (concat texfilebase e))))
  17076. pngfile))))
  17077. (declare-function org-latex-compile "ox-latex" (texfile &optional snippet))
  17078. (defun org-create-formula-image-with-imagemagick (string tofile options buffer)
  17079. "This calls convert, which is included into imagemagick."
  17080. (require 'ox-latex)
  17081. (let* ((tmpdir (if (featurep 'xemacs)
  17082. (temp-directory)
  17083. temporary-file-directory))
  17084. (texfilebase (make-temp-name
  17085. (expand-file-name "orgtex" tmpdir)))
  17086. (texfile (concat texfilebase ".tex"))
  17087. (pdffile (concat texfilebase ".pdf"))
  17088. (pngfile (concat texfilebase ".png"))
  17089. (fnh (if (featurep 'xemacs)
  17090. (font-height (face-font 'default))
  17091. (face-attribute 'default :height nil)))
  17092. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  17093. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  17094. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  17095. "black"))
  17096. (bg (or (plist-get options (if buffer :background :html-background))
  17097. "white")))
  17098. (if (eq fg 'default) (setq fg (org-latex-color :foreground))
  17099. (setq fg (org-latex-color-format fg)))
  17100. (if (eq bg 'default) (setq bg (org-latex-color :background))
  17101. (setq bg (org-latex-color-format
  17102. (if (string= bg "Transparent") "white" bg))))
  17103. (let ((latex-header (org-create-formula--latex-header)))
  17104. (with-temp-file texfile
  17105. (insert latex-header)
  17106. (insert "\n\\begin{document}\n"
  17107. "\\definecolor{fg}{rgb}{" fg "}\n"
  17108. "\\definecolor{bg}{rgb}{" bg "}\n"
  17109. "\n\\pagecolor{bg}\n"
  17110. "\n{\\color{fg}\n"
  17111. string
  17112. "\n}\n"
  17113. "\n\\end{document}\n")))
  17114. (org-latex-compile texfile t)
  17115. (if (not (file-exists-p pdffile))
  17116. (progn (message "Failed to create pdf file from %s" texfile) nil)
  17117. (condition-case nil
  17118. (if (featurep 'xemacs)
  17119. (call-process "convert" nil nil nil
  17120. "-density" "96"
  17121. "-trim"
  17122. "-antialias"
  17123. pdffile
  17124. "-quality" "100"
  17125. ;; "-sharpen" "0x1.0"
  17126. pngfile)
  17127. (call-process "convert" nil nil nil
  17128. "-density" dpi
  17129. "-trim"
  17130. "-antialias"
  17131. pdffile
  17132. "-quality" "100"
  17133. ;; "-sharpen" "0x1.0"
  17134. pngfile))
  17135. (error nil))
  17136. (if (not (file-exists-p pngfile))
  17137. (if org-format-latex-signal-error
  17138. (error "Failed to create png file from %s" texfile)
  17139. (message "Failed to create png file from %s" texfile)
  17140. nil)
  17141. ;; Use the requested file name and clean up
  17142. (copy-file pngfile tofile 'replace)
  17143. (loop for e in '(".pdf" ".tex" ".aux" ".log" ".png") do
  17144. (if (file-exists-p (concat texfilebase e))
  17145. (delete-file (concat texfilebase e))))
  17146. pngfile))))
  17147. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  17148. "Fill a LaTeX header template TPL.
  17149. In the template, the following place holders will be recognized:
  17150. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  17151. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  17152. [PACKAGES] \\usepackage statements for PKG
  17153. [NO-PACKAGES] do not include PKG
  17154. [EXTRA] the string EXTRA
  17155. [NO-EXTRA] do not include EXTRA
  17156. For backward compatibility, if both the positive and the negative place
  17157. holder is missing, the positive one (without the \"NO-\") will be
  17158. assumed to be present at the end of the template.
  17159. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  17160. EXTRA is a string.
  17161. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  17162. (let (rpl (end ""))
  17163. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  17164. (setq rpl (if (or (match-end 1) (not def-pkg))
  17165. "" (org-latex-packages-to-string def-pkg snippets-p t))
  17166. tpl (replace-match rpl t t tpl))
  17167. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  17168. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  17169. (setq rpl (if (or (match-end 1) (not pkg))
  17170. "" (org-latex-packages-to-string pkg snippets-p t))
  17171. tpl (replace-match rpl t t tpl))
  17172. (if pkg (setq end
  17173. (concat end "\n"
  17174. (org-latex-packages-to-string pkg snippets-p)))))
  17175. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  17176. (setq rpl (if (or (match-end 1) (not extra))
  17177. "" (concat extra "\n"))
  17178. tpl (replace-match rpl t t tpl))
  17179. (if (and extra (string-match "\\S-" extra))
  17180. (setq end (concat end "\n" extra))))
  17181. (if (string-match "\\S-" end)
  17182. (concat tpl "\n" end)
  17183. tpl)))
  17184. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  17185. "Turn an alist of packages into a string with the \\usepackage macros."
  17186. (setq pkg (mapconcat (lambda(p)
  17187. (cond
  17188. ((stringp p) p)
  17189. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  17190. (format "%% Package %s omitted" (cadr p)))
  17191. ((equal "" (car p))
  17192. (format "\\usepackage{%s}" (cadr p)))
  17193. (t
  17194. (format "\\usepackage[%s]{%s}"
  17195. (car p) (cadr p)))))
  17196. pkg
  17197. "\n"))
  17198. (if newline (concat pkg "\n") pkg))
  17199. (defun org-dvipng-color (attr)
  17200. "Return a RGB color specification for dvipng."
  17201. (apply 'format "rgb %s %s %s"
  17202. (mapcar 'org-normalize-color
  17203. (if (featurep 'xemacs)
  17204. (color-rgb-components
  17205. (face-property 'default
  17206. (cond ((eq attr :foreground) 'foreground)
  17207. ((eq attr :background) 'background))))
  17208. (color-values (face-attribute 'default attr nil))))))
  17209. (defun org-dvipng-color-format (color-name)
  17210. "Convert COLOR-NAME to a RGB color value for dvipng."
  17211. (apply 'format "rgb %s %s %s"
  17212. (mapcar 'org-normalize-color
  17213. (color-values color-name))))
  17214. (defun org-latex-color (attr)
  17215. "Return a RGB color for the LaTeX color package."
  17216. (apply 'format "%s,%s,%s"
  17217. (mapcar 'org-normalize-color
  17218. (if (featurep 'xemacs)
  17219. (color-rgb-components
  17220. (face-property 'default
  17221. (cond ((eq attr :foreground) 'foreground)
  17222. ((eq attr :background) 'background))))
  17223. (color-values (face-attribute 'default attr nil))))))
  17224. (defun org-latex-color-format (color-name)
  17225. "Convert COLOR-NAME to a RGB color value."
  17226. (apply 'format "%s,%s,%s"
  17227. (mapcar 'org-normalize-color
  17228. (color-values color-name))))
  17229. (defun org-normalize-color (value)
  17230. "Return string to be used as color value for an RGB component."
  17231. (format "%g" (/ value 65535.0)))
  17232. ;; Image display
  17233. (defvar org-inline-image-overlays nil)
  17234. (make-variable-buffer-local 'org-inline-image-overlays)
  17235. (defun org-toggle-inline-images (&optional include-linked)
  17236. "Toggle the display of inline images.
  17237. INCLUDE-LINKED is passed to `org-display-inline-images'."
  17238. (interactive "P")
  17239. (if org-inline-image-overlays
  17240. (progn
  17241. (org-remove-inline-images)
  17242. (message "Inline image display turned off"))
  17243. (org-display-inline-images include-linked)
  17244. (if (and (org-called-interactively-p)
  17245. org-inline-image-overlays)
  17246. (message "%d images displayed inline"
  17247. (length org-inline-image-overlays))
  17248. (message "No images to display inline"))))
  17249. (defun org-redisplay-inline-images ()
  17250. "Refresh the display of inline images."
  17251. (interactive)
  17252. (if (not org-inline-image-overlays)
  17253. (org-toggle-inline-images)
  17254. (org-toggle-inline-images)
  17255. (org-toggle-inline-images)))
  17256. (defun org-display-inline-images (&optional include-linked refresh beg end)
  17257. "Display inline images.
  17258. Normally only links without a description part are inlined, because this
  17259. is how it will work for export. When INCLUDE-LINKED is set, also links
  17260. with a description part will be inlined. This can be nice for a quick
  17261. look at those images, but it does not reflect what exported files will look
  17262. like.
  17263. When REFRESH is set, refresh existing images between BEG and END.
  17264. This will create new image displays only if necessary.
  17265. BEG and END default to the buffer boundaries."
  17266. (interactive "P")
  17267. (when (display-graphic-p)
  17268. (unless refresh
  17269. (org-remove-inline-images)
  17270. (if (fboundp 'clear-image-cache) (clear-image-cache)))
  17271. (save-excursion
  17272. (save-restriction
  17273. (widen)
  17274. (setq beg (or beg (point-min)) end (or end (point-max)))
  17275. (goto-char beg)
  17276. (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
  17277. (substring (org-image-file-name-regexp) 0 -2)
  17278. "\\)\\]" (if include-linked "" "\\]")))
  17279. (case-fold-search t)
  17280. old file ov img type attrwidth width)
  17281. (while (re-search-forward re end t)
  17282. (setq old (get-char-property-and-overlay (match-beginning 1)
  17283. 'org-image-overlay)
  17284. file (expand-file-name
  17285. (concat (or (match-string 3) "") (match-string 4))))
  17286. (when (image-type-available-p 'imagemagick)
  17287. (setq attrwidth (if (or (listp org-image-actual-width)
  17288. (null org-image-actual-width))
  17289. (save-excursion
  17290. (save-match-data
  17291. (when (re-search-backward
  17292. "#\\+attr.*:width[ \t]+\\([^ ]+\\)"
  17293. (save-excursion
  17294. (re-search-backward "^[ \t]*$\\|\\`" nil t)) t)
  17295. (string-to-number (match-string 1))))))
  17296. width (cond ((eq org-image-actual-width t) nil)
  17297. ((null org-image-actual-width) attrwidth)
  17298. ((numberp org-image-actual-width)
  17299. org-image-actual-width)
  17300. ((listp org-image-actual-width)
  17301. (or attrwidth (car org-image-actual-width))))
  17302. type (if width 'imagemagick)))
  17303. (when (file-exists-p file)
  17304. (if (and (car-safe old) refresh)
  17305. (image-refresh (overlay-get (cdr old) 'display))
  17306. (setq img (save-match-data (create-image file type nil :width width)))
  17307. (when img
  17308. (setq ov (make-overlay (match-beginning 0) (match-end 0)))
  17309. (overlay-put ov 'display img)
  17310. (overlay-put ov 'face 'default)
  17311. (overlay-put ov 'org-image-overlay t)
  17312. (overlay-put ov 'modification-hooks
  17313. (list 'org-display-inline-remove-overlay))
  17314. (push ov org-inline-image-overlays))))))))))
  17315. (define-obsolete-function-alias
  17316. 'org-display-inline-modification-hook 'org-display-inline-remove-overlay "24.3")
  17317. (defun org-display-inline-remove-overlay (ov after beg end &optional len)
  17318. "Remove inline-display overlay if a corresponding region is modified."
  17319. (let ((inhibit-modification-hooks t))
  17320. (when (and ov after)
  17321. (delete ov org-inline-image-overlays)
  17322. (delete-overlay ov))))
  17323. (defun org-remove-inline-images ()
  17324. "Remove inline display of images."
  17325. (interactive)
  17326. (mapc 'delete-overlay org-inline-image-overlays)
  17327. (setq org-inline-image-overlays nil))
  17328. ;;;; Key bindings
  17329. ;; Outline functions from `outline-mode-prefix-map'
  17330. ;; that can be remapped in Org:
  17331. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  17332. (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
  17333. (define-key org-mode-map [remap outline-forward-same-level]
  17334. 'org-forward-heading-same-level)
  17335. (define-key org-mode-map [remap outline-backward-same-level]
  17336. 'org-backward-heading-same-level)
  17337. (define-key org-mode-map [remap show-branches]
  17338. 'org-kill-note-or-show-branches)
  17339. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  17340. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  17341. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  17342. ;; Outline functions from `outline-mode-prefix-map' that can not
  17343. ;; be remapped in Org:
  17344. ;;
  17345. ;; - the column "key binding" shows whether the Outline function is still
  17346. ;; available in Org mode on the same key that it has been bound to in
  17347. ;; Outline mode:
  17348. ;; - "overridden": key used for a different functionality in Org mode
  17349. ;; - else: key still bound to the same Outline function in Org mode
  17350. ;;
  17351. ;; | Outline function | key binding | Org replacement |
  17352. ;; |------------------------------------+-------------+-----------------------|
  17353. ;; | `outline-next-visible-heading' | `C-c C-n' | still same function |
  17354. ;; | `outline-previous-visible-heading' | `C-c C-p' | still same function |
  17355. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  17356. ;; | `outline-move-subtree-up' | overridden | better: org-shiftup |
  17357. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  17358. ;; | `show-entry' | overridden | no replacement |
  17359. ;; | `show-children' | `C-c C-i' | visibility cycling |
  17360. ;; | `show-branches' | `C-c C-k' | still same function |
  17361. ;; | `show-subtree' | overridden | visibility cycling |
  17362. ;; | `show-all' | overridden | no replacement |
  17363. ;; | `hide-subtree' | overridden | visibility cycling |
  17364. ;; | `hide-body' | overridden | no replacement |
  17365. ;; | `hide-entry' | overridden | visibility cycling |
  17366. ;; | `hide-leaves' | overridden | no replacement |
  17367. ;; | `hide-sublevels' | overridden | no replacement |
  17368. ;; | `hide-other' | overridden | no replacement |
  17369. ;; Make `C-c C-x' a prefix key
  17370. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  17371. ;; TAB key with modifiers
  17372. (org-defkey org-mode-map "\C-i" 'org-cycle)
  17373. (org-defkey org-mode-map [(tab)] 'org-cycle)
  17374. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  17375. (org-defkey org-mode-map "\M-\t" 'pcomplete)
  17376. ;; The following line is necessary under Suse GNU/Linux
  17377. (unless (featurep 'xemacs)
  17378. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  17379. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  17380. (define-key org-mode-map [backtab] 'org-shifttab)
  17381. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  17382. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  17383. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  17384. ;; Cursor keys with modifiers
  17385. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  17386. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  17387. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  17388. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  17389. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  17390. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  17391. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  17392. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  17393. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  17394. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  17395. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  17396. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  17397. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  17398. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  17399. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  17400. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  17401. ;; Babel keys
  17402. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  17403. (mapc (lambda (pair)
  17404. (define-key org-babel-map (car pair) (cdr pair)))
  17405. org-babel-key-bindings)
  17406. ;;; Extra keys for tty access.
  17407. ;; We only set them when really needed because otherwise the
  17408. ;; menus don't show the simple keys
  17409. (when (or org-use-extra-keys
  17410. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  17411. (not window-system))
  17412. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  17413. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  17414. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  17415. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  17416. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  17417. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  17418. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  17419. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  17420. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  17421. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  17422. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  17423. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  17424. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  17425. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  17426. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  17427. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  17428. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  17429. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  17430. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  17431. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  17432. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  17433. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  17434. (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
  17435. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  17436. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  17437. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  17438. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  17439. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  17440. ;; All the other keys
  17441. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  17442. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  17443. (if (boundp 'narrow-map)
  17444. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  17445. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  17446. (if (boundp 'narrow-map)
  17447. (org-defkey narrow-map "b" 'org-narrow-to-block)
  17448. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  17449. (if (boundp 'narrow-map)
  17450. (org-defkey narrow-map "e" 'org-narrow-to-element)
  17451. (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
  17452. (org-defkey org-mode-map "\C-\M-t" 'org-transpose-element)
  17453. (org-defkey org-mode-map "\M-}" 'org-forward-element)
  17454. (org-defkey org-mode-map "\M-{" 'org-backward-element)
  17455. (org-defkey org-mode-map "\C-c\C-^" 'org-up-element)
  17456. (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
  17457. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
  17458. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
  17459. (org-defkey org-mode-map "\C-c\M-f" 'org-next-block)
  17460. (org-defkey org-mode-map "\C-c\M-b" 'org-previous-block)
  17461. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  17462. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  17463. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  17464. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  17465. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  17466. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  17467. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  17468. (org-defkey org-mode-map "\C-c\C-xq" 'org-toggle-tags-groups)
  17469. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  17470. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  17471. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  17472. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  17473. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  17474. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  17475. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  17476. (org-defkey org-mode-map "\C-c\M-w" 'org-copy)
  17477. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  17478. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  17479. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  17480. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  17481. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  17482. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  17483. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  17484. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  17485. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  17486. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  17487. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  17488. (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
  17489. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  17490. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  17491. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  17492. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  17493. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  17494. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  17495. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  17496. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  17497. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  17498. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  17499. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  17500. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  17501. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  17502. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  17503. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  17504. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  17505. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  17506. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  17507. (org-defkey org-mode-map "\C-c^" 'org-sort)
  17508. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  17509. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  17510. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  17511. (org-defkey org-mode-map [remap open-line] 'org-open-line)
  17512. (org-defkey org-mode-map "\C-m" 'org-return)
  17513. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  17514. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  17515. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  17516. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  17517. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  17518. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  17519. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  17520. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  17521. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  17522. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  17523. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  17524. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  17525. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  17526. (org-defkey org-mode-map "\C-c\C-e" 'org-export-dispatch)
  17527. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  17528. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  17529. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  17530. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  17531. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  17532. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  17533. (org-defkey org-mode-map "\M-h" 'org-mark-element)
  17534. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  17535. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  17536. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  17537. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  17538. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  17539. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  17540. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  17541. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
  17542. (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
  17543. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  17544. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  17545. (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
  17546. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  17547. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  17548. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  17549. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  17550. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  17551. (org-defkey org-mode-map "\C-c\C-x\C-\M-v" 'org-redisplay-inline-images)
  17552. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  17553. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  17554. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  17555. (org-defkey org-mode-map "\C-c\C-xP" 'org-set-property-and-value)
  17556. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  17557. (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
  17558. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  17559. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  17560. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  17561. (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
  17562. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  17563. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  17564. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  17565. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  17566. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  17567. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  17568. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  17569. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  17570. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  17571. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  17572. (when (featurep 'xemacs)
  17573. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  17574. (defconst org-speed-commands-default
  17575. '(
  17576. ("Outline Navigation")
  17577. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  17578. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  17579. ("f" . (org-speed-move-safe 'org-forward-heading-same-level))
  17580. ("b" . (org-speed-move-safe 'org-backward-heading-same-level))
  17581. ("F" . org-next-block)
  17582. ("B" . org-previous-block)
  17583. ("u" . (org-speed-move-safe 'outline-up-heading))
  17584. ("j" . org-goto)
  17585. ("g" . (org-refile t))
  17586. ("Outline Visibility")
  17587. ("c" . org-cycle)
  17588. ("C" . org-shifttab)
  17589. (" " . org-display-outline-path)
  17590. ("s" . org-narrow-to-subtree)
  17591. ("=" . org-columns)
  17592. ("Outline Structure Editing")
  17593. ("U" . org-shiftmetaup)
  17594. ("D" . org-shiftmetadown)
  17595. ("r" . org-metaright)
  17596. ("l" . org-metaleft)
  17597. ("R" . org-shiftmetaright)
  17598. ("L" . org-shiftmetaleft)
  17599. ("i" . (progn (forward-char 1) (call-interactively
  17600. 'org-insert-heading-respect-content)))
  17601. ("^" . org-sort)
  17602. ("w" . org-refile)
  17603. ("a" . org-archive-subtree-default-with-confirmation)
  17604. ("@" . org-mark-subtree)
  17605. ("#" . org-toggle-comment)
  17606. ("Clock Commands")
  17607. ("I" . org-clock-in)
  17608. ("O" . org-clock-out)
  17609. ("Meta Data Editing")
  17610. ("t" . org-todo)
  17611. ("," . (org-priority))
  17612. ("0" . (org-priority ?\ ))
  17613. ("1" . (org-priority ?A))
  17614. ("2" . (org-priority ?B))
  17615. ("3" . (org-priority ?C))
  17616. (":" . org-set-tags-command)
  17617. ("e" . org-set-effort)
  17618. ("E" . org-inc-effort)
  17619. ("W" . (lambda(m) (interactive "sMinutes before warning: ")
  17620. (org-entry-put (point) "APPT_WARNTIME" m)))
  17621. ("Agenda Views etc")
  17622. ("v" . org-agenda)
  17623. ("/" . org-sparse-tree)
  17624. ("Misc")
  17625. ("o" . org-open-at-point)
  17626. ("?" . org-speed-command-help)
  17627. ("<" . (org-agenda-set-restriction-lock 'subtree))
  17628. (">" . (org-agenda-remove-restriction-lock))
  17629. )
  17630. "The default speed commands.")
  17631. (defun org-print-speed-command (e)
  17632. (if (> (length (car e)) 1)
  17633. (progn
  17634. (princ "\n")
  17635. (princ (car e))
  17636. (princ "\n")
  17637. (princ (make-string (length (car e)) ?-))
  17638. (princ "\n"))
  17639. (princ (car e))
  17640. (princ " ")
  17641. (if (symbolp (cdr e))
  17642. (princ (symbol-name (cdr e)))
  17643. (prin1 (cdr e)))
  17644. (princ "\n")))
  17645. (defun org-speed-command-help ()
  17646. "Show the available speed commands."
  17647. (interactive)
  17648. (if (not org-use-speed-commands)
  17649. (user-error "Speed commands are not activated, customize `org-use-speed-commands'")
  17650. (with-output-to-temp-buffer "*Help*"
  17651. (princ "User-defined Speed commands\n===========================\n")
  17652. (mapc 'org-print-speed-command org-speed-commands-user)
  17653. (princ "\n")
  17654. (princ "Built-in Speed commands\n=======================\n")
  17655. (mapc 'org-print-speed-command org-speed-commands-default))
  17656. (with-current-buffer "*Help*"
  17657. (setq truncate-lines t))))
  17658. (defun org-speed-move-safe (cmd)
  17659. "Execute CMD, but make sure that the cursor always ends up in a headline.
  17660. If not, return to the original position and throw an error."
  17661. (interactive)
  17662. (let ((pos (point)))
  17663. (call-interactively cmd)
  17664. (unless (and (bolp) (org-at-heading-p))
  17665. (goto-char pos)
  17666. (error "Boundary reached while executing %s" cmd))))
  17667. (defvar org-self-insert-command-undo-counter 0)
  17668. (defvar org-table-auto-blank-field) ; defined in org-table.el
  17669. (defvar org-speed-command nil)
  17670. (define-obsolete-function-alias
  17671. 'org-speed-command-default-hook 'org-speed-command-activate "24.3")
  17672. (defun org-speed-command-activate (keys)
  17673. "Hook for activating single-letter speed commands.
  17674. `org-speed-commands-default' specifies a minimal command set.
  17675. Use `org-speed-commands-user' for further customization."
  17676. (when (or (and (bolp) (looking-at org-outline-regexp))
  17677. (and (functionp org-use-speed-commands)
  17678. (funcall org-use-speed-commands)))
  17679. (cdr (assoc keys (append org-speed-commands-user
  17680. org-speed-commands-default)))))
  17681. (define-obsolete-function-alias
  17682. 'org-babel-speed-command-hook 'org-babel-speed-command-activate "24.3")
  17683. (defun org-babel-speed-command-activate (keys)
  17684. "Hook for activating single-letter code block commands."
  17685. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  17686. (cdr (assoc keys org-babel-key-bindings))))
  17687. (defcustom org-speed-command-hook
  17688. '(org-speed-command-default-hook org-babel-speed-command-hook)
  17689. "Hook for activating speed commands at strategic locations.
  17690. Hook functions are called in sequence until a valid handler is
  17691. found.
  17692. Each hook takes a single argument, a user-pressed command key
  17693. which is also a `self-insert-command' from the global map.
  17694. Within the hook, examine the cursor position and the command key
  17695. and return nil or a valid handler as appropriate. Handler could
  17696. be one of an interactive command, a function, or a form.
  17697. Set `org-use-speed-commands' to non-nil value to enable this
  17698. hook. The default setting is `org-speed-command-activate'."
  17699. :group 'org-structure
  17700. :version "24.1"
  17701. :type 'hook)
  17702. (defun org-self-insert-command (N)
  17703. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  17704. If the cursor is in a table looking at whitespace, the whitespace is
  17705. overwritten, and the table is not marked as requiring realignment."
  17706. (interactive "p")
  17707. (org-check-before-invisible-edit 'insert)
  17708. (cond
  17709. ((and org-use-speed-commands
  17710. (setq org-speed-command
  17711. (run-hook-with-args-until-success
  17712. 'org-speed-command-hook (this-command-keys))))
  17713. (cond
  17714. ((commandp org-speed-command)
  17715. (setq this-command org-speed-command)
  17716. (call-interactively org-speed-command))
  17717. ((functionp org-speed-command)
  17718. (funcall org-speed-command))
  17719. ((and org-speed-command (listp org-speed-command))
  17720. (eval org-speed-command))
  17721. (t (let (org-use-speed-commands)
  17722. (call-interactively 'org-self-insert-command)))))
  17723. ((and
  17724. (org-table-p)
  17725. (progn
  17726. ;; check if we blank the field, and if that triggers align
  17727. (and (featurep 'org-table) org-table-auto-blank-field
  17728. (member last-command
  17729. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  17730. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  17731. ;; got extra space, this field does not determine column width
  17732. (let (org-table-may-need-update) (org-table-blank-field))
  17733. ;; no extra space, this field may determine column width
  17734. (org-table-blank-field)))
  17735. t)
  17736. (eq N 1)
  17737. (looking-at "[^|\n]* |"))
  17738. (let (org-table-may-need-update)
  17739. (goto-char (1- (match-end 0)))
  17740. (backward-delete-char 1)
  17741. (goto-char (match-beginning 0))
  17742. (self-insert-command N)))
  17743. (t
  17744. (setq org-table-may-need-update t)
  17745. (self-insert-command N)
  17746. (org-fix-tags-on-the-fly)
  17747. (if org-self-insert-cluster-for-undo
  17748. (if (not (eq last-command 'org-self-insert-command))
  17749. (setq org-self-insert-command-undo-counter 1)
  17750. (if (>= org-self-insert-command-undo-counter 20)
  17751. (setq org-self-insert-command-undo-counter 1)
  17752. (and (> org-self-insert-command-undo-counter 0)
  17753. buffer-undo-list (listp buffer-undo-list)
  17754. (not (cadr buffer-undo-list)) ; remove nil entry
  17755. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  17756. (setq org-self-insert-command-undo-counter
  17757. (1+ org-self-insert-command-undo-counter))))))))
  17758. (defun org-check-before-invisible-edit (kind)
  17759. "Check is editing if kind KIND would be dangerous with invisible text around.
  17760. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  17761. ;; First, try to get out of here as quickly as possible, to reduce overhead
  17762. (if (and org-catch-invisible-edits
  17763. (or (not (boundp 'visible-mode)) (not visible-mode))
  17764. (or (get-char-property (point) 'invisible)
  17765. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  17766. ;; OK, we need to take a closer look
  17767. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  17768. (invisible-before-point (if (bobp) nil (get-char-property
  17769. (1- (point)) 'invisible)))
  17770. (border-and-ok-direction
  17771. (or
  17772. ;; Check if we are acting predictably before invisible text
  17773. (and invisible-at-point (not invisible-before-point)
  17774. (memq kind '(insert delete-backward)))
  17775. ;; Check if we are acting predictably after invisible text
  17776. ;; This works not well, and I have turned it off. It seems
  17777. ;; better to always show and stop after invisible text.
  17778. ;; (and (not invisible-at-point) invisible-before-point
  17779. ;; (memq kind '(insert delete)))
  17780. )))
  17781. (when (or (memq invisible-at-point '(outline org-hide-block t))
  17782. (memq invisible-before-point '(outline org-hide-block t)))
  17783. (if (eq org-catch-invisible-edits 'error)
  17784. (user-error "Editing in invisible areas is prohibited, make them visible first"))
  17785. (if (and org-custom-properties-overlays
  17786. (y-or-n-p "Display invisible properties in this buffer? "))
  17787. (org-toggle-custom-properties-visibility)
  17788. ;; Make the area visible
  17789. (save-excursion
  17790. (if invisible-before-point
  17791. (goto-char (previous-single-char-property-change
  17792. (point) 'invisible)))
  17793. (org-cycle))
  17794. (cond
  17795. ((eq org-catch-invisible-edits 'show)
  17796. ;; That's it, we do the edit after showing
  17797. (message
  17798. "Unfolding invisible region around point before editing")
  17799. (sit-for 1))
  17800. ((and (eq org-catch-invisible-edits 'smart)
  17801. border-and-ok-direction)
  17802. (message "Unfolding invisible region around point before editing"))
  17803. (t
  17804. ;; Don't do the edit, make the user repeat it in full visibility
  17805. (user-error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
  17806. (defun org-fix-tags-on-the-fly ()
  17807. (when (and (equal (char-after (point-at-bol)) ?*)
  17808. (org-at-heading-p))
  17809. (org-align-tags-here org-tags-column)))
  17810. (defun org-delete-backward-char (N)
  17811. "Like `delete-backward-char', insert whitespace at field end in tables.
  17812. When deleting backwards, in tables this function will insert whitespace in
  17813. front of the next \"|\" separator, to keep the table aligned. The table will
  17814. still be marked for re-alignment if the field did fill the entire column,
  17815. because, in this case the deletion might narrow the column."
  17816. (interactive "p")
  17817. (save-match-data
  17818. (org-check-before-invisible-edit 'delete-backward)
  17819. (if (and (org-table-p)
  17820. (eq N 1)
  17821. (string-match "|" (buffer-substring (point-at-bol) (point)))
  17822. (looking-at ".*?|"))
  17823. (let ((pos (point))
  17824. (noalign (looking-at "[^|\n\r]* |"))
  17825. (c org-table-may-need-update))
  17826. (backward-delete-char N)
  17827. (if (not overwrite-mode)
  17828. (progn
  17829. (skip-chars-forward "^|")
  17830. (insert " ")
  17831. (goto-char (1- pos))))
  17832. ;; noalign: if there were two spaces at the end, this field
  17833. ;; does not determine the width of the column.
  17834. (if noalign (setq org-table-may-need-update c)))
  17835. (backward-delete-char N)
  17836. (org-fix-tags-on-the-fly))))
  17837. (defun org-delete-char (N)
  17838. "Like `delete-char', but insert whitespace at field end in tables.
  17839. When deleting characters, in tables this function will insert whitespace in
  17840. front of the next \"|\" separator, to keep the table aligned. The table will
  17841. still be marked for re-alignment if the field did fill the entire column,
  17842. because, in this case the deletion might narrow the column."
  17843. (interactive "p")
  17844. (save-match-data
  17845. (org-check-before-invisible-edit 'delete)
  17846. (if (and (org-table-p)
  17847. (not (bolp))
  17848. (not (= (char-after) ?|))
  17849. (eq N 1))
  17850. (if (looking-at ".*?|")
  17851. (let ((pos (point))
  17852. (noalign (looking-at "[^|\n\r]* |"))
  17853. (c org-table-may-need-update))
  17854. (replace-match
  17855. (concat (substring (match-string 0) 1 -1) " |") nil t)
  17856. (goto-char pos)
  17857. ;; noalign: if there were two spaces at the end, this field
  17858. ;; does not determine the width of the column.
  17859. (if noalign (setq org-table-may-need-update c)))
  17860. (delete-char N))
  17861. (delete-char N)
  17862. (org-fix-tags-on-the-fly))))
  17863. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  17864. (put 'org-self-insert-command 'delete-selection t)
  17865. (put 'orgtbl-self-insert-command 'delete-selection t)
  17866. (put 'org-delete-char 'delete-selection 'supersede)
  17867. (put 'org-delete-backward-char 'delete-selection 'supersede)
  17868. (put 'org-yank 'delete-selection 'yank)
  17869. ;; Make `flyspell-mode' delay after some commands
  17870. (put 'org-self-insert-command 'flyspell-delayed t)
  17871. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  17872. (put 'org-delete-char 'flyspell-delayed t)
  17873. (put 'org-delete-backward-char 'flyspell-delayed t)
  17874. ;; Make pabbrev-mode expand after org-mode commands
  17875. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  17876. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  17877. ;; How to do this: Measure non-white length of current string
  17878. ;; If equal to column width, we should realign.
  17879. (defun org-remap (map &rest commands)
  17880. "In MAP, remap the functions given in COMMANDS.
  17881. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  17882. (let (new old)
  17883. (while commands
  17884. (setq old (pop commands) new (pop commands))
  17885. (if (fboundp 'command-remapping)
  17886. (org-defkey map (vector 'remap old) new)
  17887. (substitute-key-definition old new map global-map)))))
  17888. (defun org-transpose-words ()
  17889. "Transpose words for Org.
  17890. This uses the `org-mode-transpose-word-syntax-table' syntax
  17891. table, which interprets characters in `org-emphasis-alist' as
  17892. word constituants."
  17893. (interactive)
  17894. (with-syntax-table org-mode-transpose-word-syntax-table
  17895. (call-interactively 'transpose-words)))
  17896. (org-remap org-mode-map 'transpose-words 'org-transpose-words)
  17897. (when (eq org-enable-table-editor 'optimized)
  17898. ;; If the user wants maximum table support, we need to hijack
  17899. ;; some standard editing functions
  17900. (org-remap org-mode-map
  17901. 'self-insert-command 'org-self-insert-command
  17902. 'delete-char 'org-delete-char
  17903. 'delete-backward-char 'org-delete-backward-char)
  17904. (org-defkey org-mode-map "|" 'org-force-self-insert))
  17905. (defvar org-ctrl-c-ctrl-c-hook nil
  17906. "Hook for functions attaching themselves to `C-c C-c'.
  17907. This can be used to add additional functionality to the C-c C-c
  17908. key which executes context-dependent commands. This hook is run
  17909. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  17910. run after the last test.
  17911. Each function will be called with no arguments. The function
  17912. must check if the context is appropriate for it to act. If yes,
  17913. it should do its thing and then return a non-nil value. If the
  17914. context is wrong, just do nothing and return nil.")
  17915. (defvar org-ctrl-c-ctrl-c-final-hook nil
  17916. "Hook for functions attaching themselves to `C-c C-c'.
  17917. This can be used to add additional functionality to the C-c C-c
  17918. key which executes context-dependent commands. This hook is run
  17919. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  17920. before the first test.
  17921. Each function will be called with no arguments. The function
  17922. must check if the context is appropriate for it to act. If yes,
  17923. it should do its thing and then return a non-nil value. If the
  17924. context is wrong, just do nothing and return nil.")
  17925. (defvar org-tab-first-hook nil
  17926. "Hook for functions to attach themselves to TAB.
  17927. See `org-ctrl-c-ctrl-c-hook' for more information.
  17928. This hook runs as the first action when TAB is pressed, even before
  17929. `org-cycle' messes around with the `outline-regexp' to cater for
  17930. inline tasks and plain list item folding.
  17931. If any function in this hook returns t, any other actions that
  17932. would have been caused by TAB (such as table field motion or visibility
  17933. cycling) will not occur.")
  17934. (defvar org-tab-after-check-for-table-hook nil
  17935. "Hook for functions to attach themselves to TAB.
  17936. See `org-ctrl-c-ctrl-c-hook' for more information.
  17937. This hook runs after it has been established that the cursor is not in a
  17938. table, but before checking if the cursor is in a headline or if global cycling
  17939. should be done.
  17940. If any function in this hook returns t, not other actions like visibility
  17941. cycling will be done.")
  17942. (defvar org-tab-after-check-for-cycling-hook nil
  17943. "Hook for functions to attach themselves to TAB.
  17944. See `org-ctrl-c-ctrl-c-hook' for more information.
  17945. This hook runs after it has been established that not table field motion and
  17946. not visibility should be done because of current context. This is probably
  17947. the place where a package like yasnippets can hook in.")
  17948. (defvar org-tab-before-tab-emulation-hook nil
  17949. "Hook for functions to attach themselves to TAB.
  17950. See `org-ctrl-c-ctrl-c-hook' for more information.
  17951. This hook runs after every other options for TAB have been exhausted, but
  17952. before indentation and \t insertion takes place.")
  17953. (defvar org-metaleft-hook nil
  17954. "Hook for functions attaching themselves to `M-left'.
  17955. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17956. (defvar org-metaright-hook nil
  17957. "Hook for functions attaching themselves to `M-right'.
  17958. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17959. (defvar org-metaup-hook nil
  17960. "Hook for functions attaching themselves to `M-up'.
  17961. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17962. (defvar org-metadown-hook nil
  17963. "Hook for functions attaching themselves to `M-down'.
  17964. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17965. (defvar org-shiftmetaleft-hook nil
  17966. "Hook for functions attaching themselves to `M-S-left'.
  17967. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17968. (defvar org-shiftmetaright-hook nil
  17969. "Hook for functions attaching themselves to `M-S-right'.
  17970. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17971. (defvar org-shiftmetaup-hook nil
  17972. "Hook for functions attaching themselves to `M-S-up'.
  17973. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17974. (defvar org-shiftmetadown-hook nil
  17975. "Hook for functions attaching themselves to `M-S-down'.
  17976. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17977. (defvar org-metareturn-hook nil
  17978. "Hook for functions attaching themselves to `M-RET'.
  17979. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17980. (defvar org-shiftup-hook nil
  17981. "Hook for functions attaching themselves to `S-up'.
  17982. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17983. (defvar org-shiftup-final-hook nil
  17984. "Hook for functions attaching themselves to `S-up'.
  17985. This one runs after all other options except shift-select have been excluded.
  17986. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17987. (defvar org-shiftdown-hook nil
  17988. "Hook for functions attaching themselves to `S-down'.
  17989. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17990. (defvar org-shiftdown-final-hook nil
  17991. "Hook for functions attaching themselves to `S-down'.
  17992. This one runs after all other options except shift-select have been excluded.
  17993. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17994. (defvar org-shiftleft-hook nil
  17995. "Hook for functions attaching themselves to `S-left'.
  17996. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17997. (defvar org-shiftleft-final-hook nil
  17998. "Hook for functions attaching themselves to `S-left'.
  17999. This one runs after all other options except shift-select have been excluded.
  18000. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18001. (defvar org-shiftright-hook nil
  18002. "Hook for functions attaching themselves to `S-right'.
  18003. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18004. (defvar org-shiftright-final-hook nil
  18005. "Hook for functions attaching themselves to `S-right'.
  18006. This one runs after all other options except shift-select have been excluded.
  18007. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18008. (defun org-modifier-cursor-error ()
  18009. "Throw an error, a modified cursor command was applied in wrong context."
  18010. (user-error "This command is active in special context like tables, headlines or items"))
  18011. (defun org-shiftselect-error ()
  18012. "Throw an error because Shift-Cursor command was applied in wrong context."
  18013. (if (and (boundp 'shift-select-mode) shift-select-mode)
  18014. (user-error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  18015. (user-error "This command works only in special context like headlines or timestamps")))
  18016. (defun org-call-for-shift-select (cmd)
  18017. (let ((this-command-keys-shift-translated t))
  18018. (call-interactively cmd)))
  18019. (defun org-shifttab (&optional arg)
  18020. "Global visibility cycling or move to previous table field.
  18021. Call `org-table-previous-field' within a table.
  18022. When ARG is nil, cycle globally through visibility states.
  18023. When ARG is a numeric prefix, show contents of this level."
  18024. (interactive "P")
  18025. (cond
  18026. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  18027. ((integerp arg)
  18028. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  18029. (message "Content view to level: %d" arg)
  18030. (org-content (prefix-numeric-value arg2))
  18031. (org-cycle-show-empty-lines t)
  18032. (setq org-cycle-global-status 'overview)))
  18033. (t (call-interactively 'org-global-cycle))))
  18034. (defun org-shiftmetaleft ()
  18035. "Promote subtree or delete table column.
  18036. Calls `org-promote-subtree', `org-outdent-item-tree', or
  18037. `org-table-delete-column', depending on context. See the
  18038. individual commands for more information."
  18039. (interactive)
  18040. (cond
  18041. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  18042. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  18043. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  18044. ((if (not (org-region-active-p)) (org-at-item-p)
  18045. (save-excursion (goto-char (region-beginning))
  18046. (org-at-item-p)))
  18047. (call-interactively 'org-outdent-item-tree))
  18048. (t (org-modifier-cursor-error))))
  18049. (defun org-shiftmetaright ()
  18050. "Demote subtree or insert table column.
  18051. Calls `org-demote-subtree', `org-indent-item-tree', or
  18052. `org-table-insert-column', depending on context. See the
  18053. individual commands for more information."
  18054. (interactive)
  18055. (cond
  18056. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  18057. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  18058. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  18059. ((if (not (org-region-active-p)) (org-at-item-p)
  18060. (save-excursion (goto-char (region-beginning))
  18061. (org-at-item-p)))
  18062. (call-interactively 'org-indent-item-tree))
  18063. (t (org-modifier-cursor-error))))
  18064. (defun org-shiftmetaup (&optional arg)
  18065. "Move subtree up or kill table row.
  18066. Calls `org-move-subtree-up' or `org-table-kill-row' or
  18067. `org-move-item-up' or `org-timestamp-up', depending on context.
  18068. See the individual commands for more information."
  18069. (interactive "P")
  18070. (cond
  18071. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  18072. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  18073. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  18074. ((org-at-item-p) (call-interactively 'org-move-item-up))
  18075. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18076. (call-interactively 'org-timestamp-up)))
  18077. (t (call-interactively 'org-drag-line-backward))))
  18078. (defun org-shiftmetadown (&optional arg)
  18079. "Move subtree down or insert table row.
  18080. Calls `org-move-subtree-down' or `org-table-insert-row' or
  18081. `org-move-item-down' or `org-timestamp-up', depending on context.
  18082. See the individual commands for more information."
  18083. (interactive "P")
  18084. (cond
  18085. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  18086. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  18087. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  18088. ((org-at-item-p) (call-interactively 'org-move-item-down))
  18089. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18090. (call-interactively 'org-timestamp-down)))
  18091. (t (call-interactively 'org-drag-line-forward))))
  18092. (defsubst org-hidden-tree-error ()
  18093. (user-error
  18094. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  18095. (defun org-metaleft (&optional arg)
  18096. "Promote heading or move table column to left.
  18097. Calls `org-do-promote' or `org-table-move-column', depending on context.
  18098. With no specific context, calls the Emacs default `backward-word'.
  18099. See the individual commands for more information."
  18100. (interactive "P")
  18101. (cond
  18102. ((run-hook-with-args-until-success 'org-metaleft-hook))
  18103. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  18104. ((org-with-limited-levels
  18105. (or (org-at-heading-p)
  18106. (and (org-region-active-p)
  18107. (save-excursion
  18108. (goto-char (region-beginning))
  18109. (org-at-heading-p)))))
  18110. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18111. (call-interactively 'org-do-promote))
  18112. ;; At an inline task.
  18113. ((org-at-heading-p)
  18114. (call-interactively 'org-inlinetask-promote))
  18115. ((or (org-at-item-p)
  18116. (and (org-region-active-p)
  18117. (save-excursion
  18118. (goto-char (region-beginning))
  18119. (org-at-item-p))))
  18120. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18121. (call-interactively 'org-outdent-item))
  18122. (t (call-interactively 'backward-word))))
  18123. (defun org-metaright (&optional arg)
  18124. "Demote a subtree, a list item or move table column to right.
  18125. In front of a drawer or a block keyword, indent it correctly.
  18126. With no specific context, calls the Emacs default `forward-word'.
  18127. See the individual commands for more information."
  18128. (interactive "P")
  18129. (cond
  18130. ((run-hook-with-args-until-success 'org-metaright-hook))
  18131. ((org-at-table-p) (call-interactively 'org-table-move-column))
  18132. ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
  18133. ((org-at-block-p) (call-interactively 'org-indent-block))
  18134. ((org-with-limited-levels
  18135. (or (org-at-heading-p)
  18136. (and (org-region-active-p)
  18137. (save-excursion
  18138. (goto-char (region-beginning))
  18139. (org-at-heading-p)))))
  18140. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18141. (call-interactively 'org-do-demote))
  18142. ;; At an inline task.
  18143. ((org-at-heading-p)
  18144. (call-interactively 'org-inlinetask-demote))
  18145. ((or (org-at-item-p)
  18146. (and (org-region-active-p)
  18147. (save-excursion
  18148. (goto-char (region-beginning))
  18149. (org-at-item-p))))
  18150. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18151. (call-interactively 'org-indent-item))
  18152. (t (call-interactively 'forward-word))))
  18153. (defun org-check-for-hidden (what)
  18154. "Check if there are hidden headlines/items in the current visual line.
  18155. WHAT can be either `headlines' or `items'. If the current line is
  18156. an outline or item heading and it has a folded subtree below it,
  18157. this function returns t, nil otherwise."
  18158. (let ((re (cond
  18159. ((eq what 'headlines) org-outline-regexp-bol)
  18160. ((eq what 'items) (org-item-beginning-re))
  18161. (t (error "This should not happen"))))
  18162. beg end)
  18163. (save-excursion
  18164. (catch 'exit
  18165. (unless (org-region-active-p)
  18166. (setq beg (point-at-bol))
  18167. (beginning-of-line 2)
  18168. (while (and (not (eobp)) ;; this is like `next-line'
  18169. (get-char-property (1- (point)) 'invisible))
  18170. (beginning-of-line 2))
  18171. (setq end (point))
  18172. (goto-char beg)
  18173. (goto-char (point-at-eol))
  18174. (setq end (max end (point)))
  18175. (while (re-search-forward re end t)
  18176. (if (get-char-property (match-beginning 0) 'invisible)
  18177. (throw 'exit t))))
  18178. nil))))
  18179. (defun org-metaup (&optional arg)
  18180. "Move subtree up or move table row up.
  18181. Calls `org-move-subtree-up' or `org-table-move-row' or
  18182. `org-move-item-up', depending on context. See the individual commands
  18183. for more information."
  18184. (interactive "P")
  18185. (cond
  18186. ((run-hook-with-args-until-success 'org-metaup-hook))
  18187. ((org-region-active-p)
  18188. (let* ((a (min (region-beginning) (region-end)))
  18189. (b (1- (max (region-beginning) (region-end))))
  18190. (c (save-excursion (goto-char a)
  18191. (move-beginning-of-line 0)))
  18192. (d (save-excursion (goto-char a)
  18193. (move-end-of-line 0) (point))))
  18194. (transpose-regions a b c d)
  18195. (goto-char c)))
  18196. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  18197. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  18198. ((org-at-item-p) (call-interactively 'org-move-item-up))
  18199. (t (org-drag-element-backward))))
  18200. (defun org-metadown (&optional arg)
  18201. "Move subtree down or move table row down.
  18202. Calls `org-move-subtree-down' or `org-table-move-row' or
  18203. `org-move-item-down', depending on context. See the individual
  18204. commands for more information."
  18205. (interactive "P")
  18206. (cond
  18207. ((run-hook-with-args-until-success 'org-metadown-hook))
  18208. ((org-region-active-p)
  18209. (let* ((a (min (region-beginning) (region-end)))
  18210. (b (max (region-beginning) (region-end)))
  18211. (c (save-excursion (goto-char b)
  18212. (move-beginning-of-line 1)))
  18213. (d (save-excursion (goto-char b)
  18214. (move-end-of-line 1) (1+ (point)))))
  18215. (transpose-regions a b c d)
  18216. (goto-char d)))
  18217. ((org-at-table-p) (call-interactively 'org-table-move-row))
  18218. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  18219. ((org-at-item-p) (call-interactively 'org-move-item-down))
  18220. (t (org-drag-element-forward))))
  18221. (defun org-shiftup (&optional arg)
  18222. "Increase item in timestamp or increase priority of current headline.
  18223. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  18224. depending on context. See the individual commands for more information."
  18225. (interactive "P")
  18226. (cond
  18227. ((run-hook-with-args-until-success 'org-shiftup-hook))
  18228. ((and org-support-shift-select (org-region-active-p))
  18229. (org-call-for-shift-select 'previous-line))
  18230. ((org-at-timestamp-p t)
  18231. (call-interactively (if org-edit-timestamp-down-means-later
  18232. 'org-timestamp-down 'org-timestamp-up)))
  18233. ((and (not (eq org-support-shift-select 'always))
  18234. org-enable-priority-commands
  18235. (org-at-heading-p))
  18236. (call-interactively 'org-priority-up))
  18237. ((and (not org-support-shift-select) (org-at-item-p))
  18238. (call-interactively 'org-previous-item))
  18239. ((org-clocktable-try-shift 'up arg))
  18240. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  18241. (org-support-shift-select
  18242. (org-call-for-shift-select 'previous-line))
  18243. (t (org-shiftselect-error))))
  18244. (defun org-shiftdown (&optional arg)
  18245. "Decrease item in timestamp or decrease priority of current headline.
  18246. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  18247. depending on context. See the individual commands for more information."
  18248. (interactive "P")
  18249. (cond
  18250. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  18251. ((and org-support-shift-select (org-region-active-p))
  18252. (org-call-for-shift-select 'next-line))
  18253. ((org-at-timestamp-p t)
  18254. (call-interactively (if org-edit-timestamp-down-means-later
  18255. 'org-timestamp-up 'org-timestamp-down)))
  18256. ((and (not (eq org-support-shift-select 'always))
  18257. org-enable-priority-commands
  18258. (org-at-heading-p))
  18259. (call-interactively 'org-priority-down))
  18260. ((and (not org-support-shift-select) (org-at-item-p))
  18261. (call-interactively 'org-next-item))
  18262. ((org-clocktable-try-shift 'down arg))
  18263. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  18264. (org-support-shift-select
  18265. (org-call-for-shift-select 'next-line))
  18266. (t (org-shiftselect-error))))
  18267. (defun org-shiftright (&optional arg)
  18268. "Cycle the thing at point or in the current line, depending on context.
  18269. Depending on context, this does one of the following:
  18270. - switch a timestamp at point one day into the future
  18271. - on a headline, switch to the next TODO keyword.
  18272. - on an item, switch entire list to the next bullet type
  18273. - on a property line, switch to the next allowed value
  18274. - on a clocktable definition line, move time block into the future"
  18275. (interactive "P")
  18276. (cond
  18277. ((run-hook-with-args-until-success 'org-shiftright-hook))
  18278. ((and org-support-shift-select (org-region-active-p))
  18279. (org-call-for-shift-select 'forward-char))
  18280. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  18281. ((and (not (eq org-support-shift-select 'always))
  18282. (org-at-heading-p))
  18283. (let ((org-inhibit-logging
  18284. (not org-treat-S-cursor-todo-selection-as-state-change))
  18285. (org-inhibit-blocking
  18286. (not org-treat-S-cursor-todo-selection-as-state-change)))
  18287. (org-call-with-arg 'org-todo 'right)))
  18288. ((or (and org-support-shift-select
  18289. (not (eq org-support-shift-select 'always))
  18290. (org-at-item-bullet-p))
  18291. (and (not org-support-shift-select) (org-at-item-p)))
  18292. (org-call-with-arg 'org-cycle-list-bullet nil))
  18293. ((and (not (eq org-support-shift-select 'always))
  18294. (org-at-property-p))
  18295. (call-interactively 'org-property-next-allowed-value))
  18296. ((org-clocktable-try-shift 'right arg))
  18297. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  18298. (org-support-shift-select
  18299. (org-call-for-shift-select 'forward-char))
  18300. (t (org-shiftselect-error))))
  18301. (defun org-shiftleft (&optional arg)
  18302. "Cycle the thing at point or in the current line, depending on context.
  18303. Depending on context, this does one of the following:
  18304. - switch a timestamp at point one day into the past
  18305. - on a headline, switch to the previous TODO keyword.
  18306. - on an item, switch entire list to the previous bullet type
  18307. - on a property line, switch to the previous allowed value
  18308. - on a clocktable definition line, move time block into the past"
  18309. (interactive "P")
  18310. (cond
  18311. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  18312. ((and org-support-shift-select (org-region-active-p))
  18313. (org-call-for-shift-select 'backward-char))
  18314. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  18315. ((and (not (eq org-support-shift-select 'always))
  18316. (org-at-heading-p))
  18317. (let ((org-inhibit-logging
  18318. (not org-treat-S-cursor-todo-selection-as-state-change))
  18319. (org-inhibit-blocking
  18320. (not org-treat-S-cursor-todo-selection-as-state-change)))
  18321. (org-call-with-arg 'org-todo 'left)))
  18322. ((or (and org-support-shift-select
  18323. (not (eq org-support-shift-select 'always))
  18324. (org-at-item-bullet-p))
  18325. (and (not org-support-shift-select) (org-at-item-p)))
  18326. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  18327. ((and (not (eq org-support-shift-select 'always))
  18328. (org-at-property-p))
  18329. (call-interactively 'org-property-previous-allowed-value))
  18330. ((org-clocktable-try-shift 'left arg))
  18331. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  18332. (org-support-shift-select
  18333. (org-call-for-shift-select 'backward-char))
  18334. (t (org-shiftselect-error))))
  18335. (defun org-shiftcontrolright ()
  18336. "Switch to next TODO set."
  18337. (interactive)
  18338. (cond
  18339. ((and org-support-shift-select (org-region-active-p))
  18340. (org-call-for-shift-select 'forward-word))
  18341. ((and (not (eq org-support-shift-select 'always))
  18342. (org-at-heading-p))
  18343. (org-call-with-arg 'org-todo 'nextset))
  18344. (org-support-shift-select
  18345. (org-call-for-shift-select 'forward-word))
  18346. (t (org-shiftselect-error))))
  18347. (defun org-shiftcontrolleft ()
  18348. "Switch to previous TODO set."
  18349. (interactive)
  18350. (cond
  18351. ((and org-support-shift-select (org-region-active-p))
  18352. (org-call-for-shift-select 'backward-word))
  18353. ((and (not (eq org-support-shift-select 'always))
  18354. (org-at-heading-p))
  18355. (org-call-with-arg 'org-todo 'previousset))
  18356. (org-support-shift-select
  18357. (org-call-for-shift-select 'backward-word))
  18358. (t (org-shiftselect-error))))
  18359. (defun org-shiftcontrolup (&optional n)
  18360. "Change timestamps synchronously up in CLOCK log lines.
  18361. Optional argument N tells to change by that many units."
  18362. (interactive "P")
  18363. (cond ((and (not org-support-shift-select)
  18364. (org-at-clock-log-p)
  18365. (org-at-timestamp-p t))
  18366. (org-clock-timestamps-up n))
  18367. (t (org-shiftselect-error))))
  18368. (defun org-shiftcontroldown (&optional n)
  18369. "Change timestamps synchronously down in CLOCK log lines.
  18370. Optional argument N tells to change by that many units."
  18371. (interactive "P")
  18372. (cond ((and (not org-support-shift-select)
  18373. (org-at-clock-log-p)
  18374. (org-at-timestamp-p t))
  18375. (org-clock-timestamps-down n))
  18376. (t (org-shiftselect-error))))
  18377. (defun org-ctrl-c-ret ()
  18378. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  18379. (interactive)
  18380. (cond
  18381. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  18382. (t (call-interactively 'org-insert-heading))))
  18383. (defun org-find-visible ()
  18384. (let ((s (point)))
  18385. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  18386. (get-char-property s 'invisible)))
  18387. s))
  18388. (defun org-find-invisible ()
  18389. (let ((s (point)))
  18390. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  18391. (not (get-char-property s 'invisible))))
  18392. s))
  18393. (defun org-copy-visible (beg end)
  18394. "Copy the visible parts of the region."
  18395. (interactive "r")
  18396. (let (snippets s)
  18397. (save-excursion
  18398. (save-restriction
  18399. (narrow-to-region beg end)
  18400. (setq s (goto-char (point-min)))
  18401. (while (not (= (point) (point-max)))
  18402. (goto-char (org-find-invisible))
  18403. (push (buffer-substring s (point)) snippets)
  18404. (setq s (goto-char (org-find-visible))))))
  18405. (kill-new (apply 'concat (nreverse snippets)))))
  18406. (defun org-copy-special ()
  18407. "Copy region in table or copy current subtree.
  18408. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  18409. See the individual commands for more information."
  18410. (interactive)
  18411. (call-interactively
  18412. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  18413. (defun org-cut-special ()
  18414. "Cut region in table or cut current subtree.
  18415. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  18416. See the individual commands for more information."
  18417. (interactive)
  18418. (call-interactively
  18419. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  18420. (defun org-paste-special (arg)
  18421. "Paste rectangular region into table, or past subtree relative to level.
  18422. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  18423. See the individual commands for more information."
  18424. (interactive "P")
  18425. (if (org-at-table-p)
  18426. (org-table-paste-rectangle)
  18427. (org-paste-subtree arg)))
  18428. (defsubst org-in-fixed-width-region-p ()
  18429. "Is point in a fixed-width region?"
  18430. (save-match-data
  18431. (eq 'fixed-width (org-element-type (org-element-at-point)))))
  18432. (defun org-edit-special (&optional arg)
  18433. "Call a special editor for the element at point.
  18434. When at a table, call the formula editor with `org-table-edit-formulas'.
  18435. When in a source code block, call `org-edit-src-code'.
  18436. When in a fixed-width region, call `org-edit-fixed-width-region'.
  18437. When at an #+INCLUDE keyword, visit the included file.
  18438. On a link, call `ffap' to visit the link at point.
  18439. Otherwise, return a user error."
  18440. (interactive "P")
  18441. (let ((element (org-element-at-point)))
  18442. (assert (not buffer-read-only) nil
  18443. "Buffer is read-only: %s" (buffer-name))
  18444. (case (org-element-type element)
  18445. (src-block
  18446. (if (not arg) (org-edit-src-code)
  18447. (let* ((info (org-babel-get-src-block-info))
  18448. (lang (nth 0 info))
  18449. (params (nth 2 info))
  18450. (session (cdr (assq :session params))))
  18451. (if (not session) (org-edit-src-code)
  18452. ;; At a src-block with a session and function called with
  18453. ;; an ARG: switch to the buffer related to the inferior
  18454. ;; process.
  18455. (switch-to-buffer
  18456. (funcall (intern (concat "org-babel-prep-session:" lang))
  18457. session params))))))
  18458. (keyword
  18459. (if (member (org-element-property :key element) '("INCLUDE" "SETUPFILE"))
  18460. (find-file
  18461. (org-remove-double-quotes
  18462. (car (org-split-string (org-element-property :value element)))))
  18463. (user-error "No special environment to edit here")))
  18464. (table
  18465. (if (eq (org-element-property :type element) 'table.el)
  18466. (org-edit-src-code)
  18467. (call-interactively 'org-table-edit-formulas)))
  18468. ;; Only Org tables contain `table-row' type elements.
  18469. (table-row (call-interactively 'org-table-edit-formulas))
  18470. ((example-block export-block) (org-edit-src-code))
  18471. (fixed-width (org-edit-fixed-width-region))
  18472. (otherwise
  18473. ;; No notable element at point. Though, we may be at a link,
  18474. ;; which is an object. Thus, scan deeper.
  18475. (if (eq (org-element-type (org-element-context element)) 'link)
  18476. (call-interactively 'ffap)
  18477. (user-error "No special environment to edit here"))))))
  18478. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  18479. (defun org-ctrl-c-ctrl-c (&optional arg)
  18480. "Set tags in headline, or update according to changed information at point.
  18481. This command does many different things, depending on context:
  18482. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  18483. this is what we do.
  18484. - If the cursor is on a statistics cookie, update it.
  18485. - If the cursor is in a headline, prompt for tags and insert them
  18486. into the current line, aligned to `org-tags-column'. When called
  18487. with prefix arg, realign all tags in the current buffer.
  18488. - If the cursor is in one of the special #+KEYWORD lines, this
  18489. triggers scanning the buffer for these lines and updating the
  18490. information.
  18491. - If the cursor is inside a table, realign the table. This command
  18492. works even if the automatic table editor has been turned off.
  18493. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  18494. the entire table.
  18495. - If the cursor is at a footnote reference or definition, jump to
  18496. the corresponding definition or references, respectively.
  18497. - If the cursor is a the beginning of a dynamic block, update it.
  18498. - If the current buffer is a capture buffer, close note and file it.
  18499. - If the cursor is on a <<<target>>>, update radio targets and
  18500. corresponding links in this buffer.
  18501. - If the cursor is on a numbered item in a plain list, renumber the
  18502. ordered list.
  18503. - If the cursor is on a checkbox, toggle it.
  18504. - If the cursor is on a code block, evaluate it. The variable
  18505. `org-confirm-babel-evaluate' can be used to control prompting
  18506. before code block evaluation, by default every code block
  18507. evaluation requires confirmation. Code block evaluation can be
  18508. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  18509. (interactive "P")
  18510. (cond
  18511. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  18512. org-occur-highlights
  18513. org-latex-fragment-image-overlays)
  18514. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  18515. (org-remove-occur-highlights)
  18516. (org-remove-latex-fragment-image-overlays)
  18517. (message "Temporary highlights/overlays removed from current buffer"))
  18518. ((and (local-variable-p 'org-finish-function (current-buffer))
  18519. (fboundp org-finish-function))
  18520. (funcall org-finish-function))
  18521. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  18522. (t
  18523. (let* ((context (org-element-context)) (type (org-element-type context)))
  18524. ;; Test if point is within a blank line.
  18525. (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
  18526. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  18527. (user-error "C-c C-c can do nothing useful at this location"))
  18528. ;; For convenience: at the first line of a paragraph on the
  18529. ;; same line as an item, apply function on that item instead.
  18530. (when (eq type 'paragraph)
  18531. (let ((parent (org-element-property :parent context)))
  18532. (when (and (eq (org-element-type parent) 'item)
  18533. (= (point-at-bol) (org-element-property :begin parent)))
  18534. (setq context parent type 'item))))
  18535. ;; Act according to type of element or object at point.
  18536. (case type
  18537. (clock (org-clock-update-time-maybe))
  18538. (dynamic-block
  18539. (save-excursion
  18540. (goto-char (org-element-property :post-affiliated context))
  18541. (org-update-dblock)))
  18542. (footnote-definition
  18543. (goto-char (org-element-property :post-affiliated context))
  18544. (call-interactively 'org-footnote-action))
  18545. (footnote-reference (call-interactively 'org-footnote-action))
  18546. ((headline inlinetask)
  18547. (save-excursion (goto-char (org-element-property :begin context))
  18548. (call-interactively 'org-set-tags)))
  18549. (item
  18550. ;; At an item: a double C-u set checkbox to "[-]"
  18551. ;; unconditionally, whereas a single one will toggle its
  18552. ;; presence. Without an universal argument, if the item
  18553. ;; has a checkbox, toggle it. Otherwise repair the list.
  18554. (let* ((box (org-element-property :checkbox context))
  18555. (struct (org-element-property :structure context))
  18556. (old-struct (copy-tree struct))
  18557. (parents (org-list-parents-alist struct))
  18558. (prevs (org-list-prevs-alist struct))
  18559. (orderedp (org-not-nil (org-entry-get nil "ORDERED"))))
  18560. (org-list-set-checkbox
  18561. (org-element-property :begin context) struct
  18562. (cond ((equal arg '(16)) "[-]")
  18563. ((and (not box) (equal arg '(4))) "[ ]")
  18564. ((or (not box) (equal arg '(4))) nil)
  18565. ((eq box 'on) "[ ]")
  18566. (t "[X]")))
  18567. ;; Mimic `org-list-write-struct' but with grabbing
  18568. ;; a return value from `org-list-struct-fix-box'.
  18569. (org-list-struct-fix-ind struct parents 2)
  18570. (org-list-struct-fix-item-end struct)
  18571. (org-list-struct-fix-bul struct prevs)
  18572. (org-list-struct-fix-ind struct parents)
  18573. (let ((block-item
  18574. (org-list-struct-fix-box struct parents prevs orderedp)))
  18575. (if (and box (equal struct old-struct))
  18576. (if (equal arg '(16))
  18577. (message "Checkboxes already reset")
  18578. (user-error "Cannot toggle this checkbox: %s"
  18579. (if (eq box 'on)
  18580. "all subitems checked"
  18581. "unchecked subitems")))
  18582. (org-list-struct-apply-struct struct old-struct)
  18583. (org-update-checkbox-count-maybe))
  18584. (when block-item
  18585. (message "Checkboxes were removed due to empty box at line %d"
  18586. (org-current-line block-item))))))
  18587. (keyword
  18588. (let ((org-inhibit-startup-visibility-stuff t)
  18589. (org-startup-align-all-tables nil))
  18590. (when (boundp 'org-table-coordinate-overlays)
  18591. (mapc 'delete-overlay org-table-coordinate-overlays)
  18592. (setq org-table-coordinate-overlays nil))
  18593. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  18594. (message "Local setup has been refreshed"))
  18595. (plain-list
  18596. ;; At a plain list, with a double C-u argument, set
  18597. ;; checkboxes of each item to "[-]", whereas a single one
  18598. ;; will toggle their presence according to the state of the
  18599. ;; first item in the list. Without an argument, repair the
  18600. ;; list.
  18601. (let* ((begin (org-element-property :contents-begin context))
  18602. (beginm (move-marker (make-marker) begin))
  18603. (struct (org-element-property :structure context))
  18604. (old-struct (copy-tree struct))
  18605. (first-box (save-excursion
  18606. (goto-char begin)
  18607. (looking-at org-list-full-item-re)
  18608. (match-string-no-properties 3)))
  18609. (new-box (cond ((equal arg '(16)) "[-]")
  18610. ((equal arg '(4)) (unless first-box "[ ]"))
  18611. ((equal first-box "[X]") "[ ]")
  18612. (t "[X]"))))
  18613. (cond
  18614. (arg
  18615. (mapc (lambda (pos) (org-list-set-checkbox pos struct new-box))
  18616. (org-list-get-all-items
  18617. begin struct (org-list-prevs-alist struct))))
  18618. ((and first-box (eq (point) begin))
  18619. ;; For convenience, when point is at bol on the first
  18620. ;; item of the list and no argument is provided, simply
  18621. ;; toggle checkbox of that item, if any.
  18622. (org-list-set-checkbox begin struct new-box)))
  18623. (org-list-write-struct
  18624. struct (org-list-parents-alist struct) old-struct)
  18625. (org-update-checkbox-count-maybe)
  18626. (save-excursion (goto-char beginm) (org-list-send-list 'maybe))))
  18627. ((property-drawer node-property)
  18628. (call-interactively 'org-property-action))
  18629. ((radio-target target)
  18630. (call-interactively 'org-update-radio-target-regexp))
  18631. (statistics-cookie
  18632. (call-interactively 'org-update-statistics-cookies))
  18633. ((table table-cell table-row)
  18634. ;; At a table, recalculate every field and align it. Also
  18635. ;; send the table if necessary. If the table has
  18636. ;; a `table.el' type, just give up. At a table row or
  18637. ;; cell, maybe recalculate line but always align table.
  18638. (if (eq (org-element-property :type context) 'table.el)
  18639. (message "Use C-c ' to edit table.el tables")
  18640. (let ((org-enable-table-editor t))
  18641. (if (or (eq type 'table)
  18642. ;; Check if point is at a TBLFM line.
  18643. (and (eq type 'table-row)
  18644. (= (point) (org-element-property :end context))))
  18645. (save-excursion
  18646. (if (org-at-TBLFM-p)
  18647. (progn (require 'org-table)
  18648. (org-table-calc-current-TBLFM))
  18649. (goto-char (org-element-property :contents-begin context))
  18650. (org-call-with-arg 'org-table-recalculate (or arg t))
  18651. (orgtbl-send-table 'maybe)))
  18652. (org-table-maybe-eval-formula)
  18653. (cond (arg (call-interactively 'org-table-recalculate))
  18654. ((org-table-maybe-recalculate-line))
  18655. (t (org-table-align)))))))
  18656. (timestamp (org-timestamp-change 0 'day))
  18657. (otherwise
  18658. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  18659. (user-error
  18660. "C-c C-c can do nothing useful at this location")))))))))
  18661. (defun org-mode-restart ()
  18662. "Restart Org-mode, to scan again for special lines.
  18663. Also updates the keyword regular expressions."
  18664. (interactive)
  18665. (org-mode)
  18666. (message "Org-mode restarted"))
  18667. (defun org-kill-note-or-show-branches ()
  18668. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  18669. (interactive)
  18670. (if (not org-finish-function)
  18671. (progn
  18672. (hide-subtree)
  18673. (call-interactively 'show-branches))
  18674. (let ((org-note-abort t))
  18675. (funcall org-finish-function))))
  18676. (defun org-open-line (n)
  18677. "Insert a new row in tables, call `open-line' elsewhere."
  18678. (interactive "*p")
  18679. (if (org-at-table-p)
  18680. (org-table-insert-row)
  18681. (open-line n)))
  18682. (defun org-return (&optional indent)
  18683. "Goto next table row or insert a newline.
  18684. Calls `org-table-next-row' or `newline', depending on context.
  18685. See the individual commands for more information."
  18686. (interactive)
  18687. (let (org-ts-what)
  18688. (cond
  18689. ((or (bobp) (org-in-src-block-p))
  18690. (if indent (newline-and-indent) (newline)))
  18691. ((org-at-table-p)
  18692. (org-table-justify-field-maybe)
  18693. (call-interactively 'org-table-next-row))
  18694. ;; when `newline-and-indent' is called within a list, make sure
  18695. ;; text moved stays inside the item.
  18696. ((and (org-in-item-p) indent)
  18697. (if (and (org-at-item-p) (>= (point) (match-end 0)))
  18698. (progn
  18699. (save-match-data (newline))
  18700. (org-indent-line-to (length (match-string 0))))
  18701. (let ((ind (org-get-indentation)))
  18702. (newline)
  18703. (if (org-looking-back org-list-end-re)
  18704. (org-indent-line)
  18705. (org-indent-line-to ind)))))
  18706. ((and org-return-follows-link
  18707. (org-at-timestamp-p t)
  18708. (not (eq org-ts-what 'after)))
  18709. (org-follow-timestamp-link))
  18710. ((and org-return-follows-link
  18711. (let ((tprop (get-text-property (point) 'face)))
  18712. (or (eq tprop 'org-link)
  18713. (and (listp tprop) (memq 'org-link tprop)))))
  18714. (call-interactively 'org-open-at-point))
  18715. ((and (org-at-heading-p)
  18716. (looking-at
  18717. (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
  18718. (org-show-entry)
  18719. (end-of-line 1)
  18720. (newline))
  18721. (t (if indent (newline-and-indent) (newline))))))
  18722. (defun org-return-indent ()
  18723. "Goto next table row or insert a newline and indent.
  18724. Calls `org-table-next-row' or `newline-and-indent', depending on
  18725. context. See the individual commands for more information."
  18726. (interactive)
  18727. (org-return t))
  18728. (defun org-ctrl-c-star ()
  18729. "Compute table, or change heading status of lines.
  18730. Calls `org-table-recalculate' or `org-toggle-heading',
  18731. depending on context."
  18732. (interactive)
  18733. (cond
  18734. ((org-at-table-p)
  18735. (call-interactively 'org-table-recalculate))
  18736. (t
  18737. ;; Convert all lines in region to list items
  18738. (call-interactively 'org-toggle-heading))))
  18739. (defun org-ctrl-c-minus ()
  18740. "Insert separator line in table or modify bullet status of line.
  18741. Also turns a plain line or a region of lines into list items.
  18742. Calls `org-table-insert-hline', `org-toggle-item', or
  18743. `org-cycle-list-bullet', depending on context."
  18744. (interactive)
  18745. (cond
  18746. ((org-at-table-p)
  18747. (call-interactively 'org-table-insert-hline))
  18748. ((org-region-active-p)
  18749. (call-interactively 'org-toggle-item))
  18750. ((org-in-item-p)
  18751. (call-interactively 'org-cycle-list-bullet))
  18752. (t
  18753. (call-interactively 'org-toggle-item))))
  18754. (defun org-toggle-item (arg)
  18755. "Convert headings or normal lines to items, items to normal lines.
  18756. If there is no active region, only the current line is considered.
  18757. If the first non blank line in the region is a headline, convert
  18758. all headlines to items, shifting text accordingly.
  18759. If it is an item, convert all items to normal lines.
  18760. If it is normal text, change region into a list of items.
  18761. With a prefix argument ARG, change the region in a single item."
  18762. (interactive "P")
  18763. (let ((shift-text
  18764. (function
  18765. ;; Shift text in current section to IND, from point to END.
  18766. ;; The function leaves point to END line.
  18767. (lambda (ind end)
  18768. (let ((min-i 1000) (end (copy-marker end)))
  18769. ;; First determine the minimum indentation (MIN-I) of
  18770. ;; the text.
  18771. (save-excursion
  18772. (catch 'exit
  18773. (while (< (point) end)
  18774. (let ((i (org-get-indentation)))
  18775. (cond
  18776. ;; Skip blank lines and inline tasks.
  18777. ((looking-at "^[ \t]*$"))
  18778. ((looking-at org-outline-regexp-bol))
  18779. ;; We can't find less than 0 indentation.
  18780. ((zerop i) (throw 'exit (setq min-i 0)))
  18781. ((< i min-i) (setq min-i i))))
  18782. (forward-line))))
  18783. ;; Then indent each line so that a line indented to
  18784. ;; MIN-I becomes indented to IND. Ignore blank lines
  18785. ;; and inline tasks in the process.
  18786. (let ((delta (- ind min-i)))
  18787. (while (< (point) end)
  18788. (unless (or (looking-at "^[ \t]*$")
  18789. (looking-at org-outline-regexp-bol))
  18790. (org-indent-line-to (+ (org-get-indentation) delta)))
  18791. (forward-line)))))))
  18792. (skip-blanks
  18793. (function
  18794. ;; Return beginning of first non-blank line, starting from
  18795. ;; line at POS.
  18796. (lambda (pos)
  18797. (save-excursion
  18798. (goto-char pos)
  18799. (skip-chars-forward " \r\t\n")
  18800. (point-at-bol)))))
  18801. beg end)
  18802. ;; Determine boundaries of changes.
  18803. (if (org-region-active-p)
  18804. (setq beg (funcall skip-blanks (region-beginning))
  18805. end (copy-marker (region-end)))
  18806. (setq beg (funcall skip-blanks (point-at-bol))
  18807. end (copy-marker (point-at-eol))))
  18808. ;; Depending on the starting line, choose an action on the text
  18809. ;; between BEG and END.
  18810. (org-with-limited-levels
  18811. (save-excursion
  18812. (goto-char beg)
  18813. (cond
  18814. ;; Case 1. Start at an item: de-itemize. Note that it only
  18815. ;; happens when a region is active: `org-ctrl-c-minus'
  18816. ;; would call `org-cycle-list-bullet' otherwise.
  18817. ((org-at-item-p)
  18818. (while (< (point) end)
  18819. (when (org-at-item-p)
  18820. (skip-chars-forward " \t")
  18821. (delete-region (point) (match-end 0)))
  18822. (forward-line)))
  18823. ;; Case 2. Start at an heading: convert to items.
  18824. ((org-at-heading-p)
  18825. (let* ((bul (org-list-bullet-string "-"))
  18826. (bul-len (length bul))
  18827. ;; Indentation of the first heading. It should be
  18828. ;; relative to the indentation of its parent, if any.
  18829. (start-ind (save-excursion
  18830. (cond
  18831. ((not org-adapt-indentation) 0)
  18832. ((not (outline-previous-heading)) 0)
  18833. (t (length (match-string 0))))))
  18834. ;; Level of first heading. Further headings will be
  18835. ;; compared to it to determine hierarchy in the list.
  18836. (ref-level (org-reduced-level (org-outline-level))))
  18837. (while (< (point) end)
  18838. (let* ((level (org-reduced-level (org-outline-level)))
  18839. (delta (max 0 (- level ref-level))))
  18840. ;; If current headline is less indented than the first
  18841. ;; one, set it as reference, in order to preserve
  18842. ;; subtrees.
  18843. (when (< level ref-level) (setq ref-level level))
  18844. (replace-match bul t t)
  18845. (org-indent-line-to (+ start-ind (* delta bul-len)))
  18846. ;; Ensure all text down to END (or SECTION-END) belongs
  18847. ;; to the newly created item.
  18848. (let ((section-end (save-excursion
  18849. (or (outline-next-heading) (point)))))
  18850. (forward-line)
  18851. (funcall shift-text
  18852. (+ start-ind (* (1+ delta) bul-len))
  18853. (min end section-end)))))))
  18854. ;; Case 3. Normal line with ARG: make the first line of region
  18855. ;; an item, and shift indentation of others lines to
  18856. ;; set them as item's body.
  18857. (arg (let* ((bul (org-list-bullet-string "-"))
  18858. (bul-len (length bul))
  18859. (ref-ind (org-get-indentation)))
  18860. (skip-chars-forward " \t")
  18861. (insert bul)
  18862. (forward-line)
  18863. (while (< (point) end)
  18864. ;; Ensure that lines less indented than first one
  18865. ;; still get included in item body.
  18866. (funcall shift-text
  18867. (+ ref-ind bul-len)
  18868. (min end (save-excursion (or (outline-next-heading)
  18869. (point)))))
  18870. (forward-line))))
  18871. ;; Case 4. Normal line without ARG: turn each non-item line
  18872. ;; into an item.
  18873. (t
  18874. (while (< (point) end)
  18875. (unless (or (org-at-heading-p) (org-at-item-p))
  18876. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  18877. (replace-match
  18878. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  18879. (forward-line))))))))
  18880. (defun org-toggle-heading (&optional nstars)
  18881. "Convert headings to normal text, or items or text to headings.
  18882. If there is no active region, only convert the current line.
  18883. With a \\[universal-argument] prefix, convert the whole list at
  18884. point into heading.
  18885. In a region:
  18886. - If the first non blank line is a headline, remove the stars
  18887. from all headlines in the region.
  18888. - If it is a normal line, turn each and every normal line (i.e.,
  18889. not an heading or an item) in the region into headings. If you
  18890. want to convert only the first line of this region, use one
  18891. universal prefix argument.
  18892. - If it is a plain list item, turn all plain list items into headings.
  18893. When converting a line into a heading, the number of stars is chosen
  18894. such that the lines become children of the current entry. However,
  18895. when a numeric prefix argument is given, its value determines the
  18896. number of stars to add."
  18897. (interactive "P")
  18898. (let ((skip-blanks
  18899. (function
  18900. ;; Return beginning of first non-blank line, starting from
  18901. ;; line at POS.
  18902. (lambda (pos)
  18903. (save-excursion
  18904. (goto-char pos)
  18905. (while (org-at-comment-p) (forward-line))
  18906. (skip-chars-forward " \r\t\n")
  18907. (point-at-bol)))))
  18908. beg end toggled)
  18909. ;; Determine boundaries of changes. If a universal prefix has
  18910. ;; been given, put the list in a region. If region ends at a bol,
  18911. ;; do not consider the last line to be in the region.
  18912. (when (and current-prefix-arg (org-at-item-p))
  18913. (if (listp current-prefix-arg) (setq current-prefix-arg 1))
  18914. (org-mark-element))
  18915. (if (org-region-active-p)
  18916. (setq beg (funcall skip-blanks (region-beginning))
  18917. end (copy-marker (save-excursion
  18918. (goto-char (region-end))
  18919. (if (bolp) (point) (point-at-eol)))))
  18920. (setq beg (funcall skip-blanks (point-at-bol))
  18921. end (copy-marker (point-at-eol))))
  18922. ;; Ensure inline tasks don't count as headings.
  18923. (org-with-limited-levels
  18924. (save-excursion
  18925. (goto-char beg)
  18926. (cond
  18927. ;; Case 1. Started at an heading: de-star headings.
  18928. ((org-at-heading-p)
  18929. (while (< (point) end)
  18930. (when (org-at-heading-p t)
  18931. (looking-at org-outline-regexp) (replace-match "")
  18932. (setq toggled t))
  18933. (forward-line)))
  18934. ;; Case 2. Started at an item: change items into headlines.
  18935. ;; One star will be added by `org-list-to-subtree'.
  18936. ((org-at-item-p)
  18937. (let* ((stars (make-string
  18938. ;; subtract the star that will be added again by
  18939. ;; `org-list-to-subtree'
  18940. (if (numberp nstars) (1- nstars)
  18941. (or (org-current-level) 0))
  18942. ?*))
  18943. (add-stars
  18944. (cond (nstars "") ; stars from prefix only
  18945. ((equal stars "") "") ; before first heading
  18946. (org-odd-levels-only "*") ; inside heading, odd
  18947. (t "")))) ; inside heading, oddeven
  18948. (while (< (point) end)
  18949. (when (org-at-item-p)
  18950. ;; Pay attention to cases when region ends before list.
  18951. (let* ((struct (org-list-struct))
  18952. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  18953. (save-restriction
  18954. (narrow-to-region (point) list-end)
  18955. (insert
  18956. (org-list-to-subtree
  18957. (org-list-parse-list t)
  18958. '(:istart (concat stars add-stars (funcall get-stars depth))
  18959. :icount (concat stars add-stars (funcall get-stars depth)))))))
  18960. (setq toggled t))
  18961. (forward-line))))
  18962. ;; Case 3. Started at normal text: make every line an heading,
  18963. ;; skipping headlines and items.
  18964. (t (let* ((stars
  18965. (make-string
  18966. (if (numberp nstars) nstars (or (org-current-level) 0)) ?*))
  18967. (add-stars
  18968. (cond (nstars "") ; stars from prefix only
  18969. ((equal stars "") "*") ; before first heading
  18970. (org-odd-levels-only "**") ; inside heading, odd
  18971. (t "*"))) ; inside heading, oddeven
  18972. (rpl (concat stars add-stars " "))
  18973. (lend (if (listp nstars) (save-excursion (end-of-line) (point)))))
  18974. (while (< (point) (if (equal nstars '(4)) lend end))
  18975. (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
  18976. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  18977. (replace-match (concat rpl (match-string 2))) (setq toggled t))
  18978. (forward-line)))))))
  18979. (unless toggled (message "Cannot toggle heading from here"))))
  18980. (defun org-meta-return (&optional arg)
  18981. "Insert a new heading or wrap a region in a table.
  18982. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  18983. See the individual commands for more information."
  18984. (interactive "P")
  18985. (org-check-before-invisible-edit 'insert)
  18986. (cond
  18987. ((run-hook-with-args-until-success 'org-metareturn-hook))
  18988. ((or (org-at-drawer-p) (org-in-drawer-p) (org-at-property-p))
  18989. (newline-and-indent))
  18990. ((org-at-table-p)
  18991. (call-interactively 'org-table-wrap-region))
  18992. (t (call-interactively 'org-insert-heading))))
  18993. ;;; Menu entries
  18994. (defsubst org-in-subtree-not-table-p ()
  18995. "Are we in a subtree and not in a table?"
  18996. (and (not (org-before-first-heading-p))
  18997. (not (org-at-table-p))))
  18998. ;; Define the Org-mode menus
  18999. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  19000. '("Tbl"
  19001. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  19002. ["Next Field" org-cycle (org-at-table-p)]
  19003. ["Previous Field" org-shifttab (org-at-table-p)]
  19004. ["Next Row" org-return (org-at-table-p)]
  19005. "--"
  19006. ["Blank Field" org-table-blank-field (org-at-table-p)]
  19007. ["Edit Field" org-table-edit-field (org-at-table-p)]
  19008. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  19009. "--"
  19010. ("Column"
  19011. ["Move Column Left" org-metaleft (org-at-table-p)]
  19012. ["Move Column Right" org-metaright (org-at-table-p)]
  19013. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  19014. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  19015. ("Row"
  19016. ["Move Row Up" org-metaup (org-at-table-p)]
  19017. ["Move Row Down" org-metadown (org-at-table-p)]
  19018. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  19019. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  19020. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  19021. "--"
  19022. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  19023. ("Rectangle"
  19024. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  19025. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  19026. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  19027. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  19028. "--"
  19029. ("Calculate"
  19030. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  19031. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  19032. ["Edit Formulas" org-edit-special (org-at-table-p)]
  19033. "--"
  19034. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  19035. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  19036. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  19037. "--"
  19038. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  19039. "--"
  19040. ["Sum Column/Rectangle" org-table-sum
  19041. (or (org-at-table-p) (org-region-active-p))]
  19042. ["Which Column?" org-table-current-column (org-at-table-p)])
  19043. ["Debug Formulas"
  19044. org-table-toggle-formula-debugger
  19045. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  19046. ["Show Col/Row Numbers"
  19047. org-table-toggle-coordinate-overlays
  19048. :style toggle
  19049. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  19050. "--"
  19051. ["Create" org-table-create (and (not (org-at-table-p))
  19052. org-enable-table-editor)]
  19053. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  19054. ["Import from File" org-table-import (not (org-at-table-p))]
  19055. ["Export to File" org-table-export (org-at-table-p)]
  19056. "--"
  19057. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  19058. (easy-menu-define org-org-menu org-mode-map "Org menu"
  19059. '("Org"
  19060. ("Show/Hide"
  19061. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  19062. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  19063. ["Sparse Tree..." org-sparse-tree t]
  19064. ["Reveal Context" org-reveal t]
  19065. ["Show All" show-all t]
  19066. "--"
  19067. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  19068. "--"
  19069. ["New Heading" org-insert-heading t]
  19070. ("Navigate Headings"
  19071. ["Up" outline-up-heading t]
  19072. ["Next" outline-next-visible-heading t]
  19073. ["Previous" outline-previous-visible-heading t]
  19074. ["Next Same Level" outline-forward-same-level t]
  19075. ["Previous Same Level" outline-backward-same-level t]
  19076. "--"
  19077. ["Jump" org-goto t])
  19078. ("Edit Structure"
  19079. ["Refile Subtree" org-refile (org-in-subtree-not-table-p)]
  19080. "--"
  19081. ["Move Subtree Up" org-shiftmetaup (org-in-subtree-not-table-p)]
  19082. ["Move Subtree Down" org-shiftmetadown (org-in-subtree-not-table-p)]
  19083. "--"
  19084. ["Copy Subtree" org-copy-special (org-in-subtree-not-table-p)]
  19085. ["Cut Subtree" org-cut-special (org-in-subtree-not-table-p)]
  19086. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  19087. "--"
  19088. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  19089. "--"
  19090. ["Copy visible text" org-copy-visible t]
  19091. "--"
  19092. ["Promote Heading" org-metaleft (org-in-subtree-not-table-p)]
  19093. ["Promote Subtree" org-shiftmetaleft (org-in-subtree-not-table-p)]
  19094. ["Demote Heading" org-metaright (org-in-subtree-not-table-p)]
  19095. ["Demote Subtree" org-shiftmetaright (org-in-subtree-not-table-p)]
  19096. "--"
  19097. ["Sort Region/Children" org-sort t]
  19098. "--"
  19099. ["Convert to odd levels" org-convert-to-odd-levels t]
  19100. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  19101. ("Editing"
  19102. ["Emphasis..." org-emphasize t]
  19103. ["Edit Source Example" org-edit-special t]
  19104. "--"
  19105. ["Footnote new/jump" org-footnote-action t]
  19106. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  19107. ("Archive"
  19108. ["Archive (default method)" org-archive-subtree-default (org-in-subtree-not-table-p)]
  19109. "--"
  19110. ["Move Subtree to Archive file" org-advertized-archive-subtree (org-in-subtree-not-table-p)]
  19111. ["Toggle ARCHIVE tag" org-toggle-archive-tag (org-in-subtree-not-table-p)]
  19112. ["Move subtree to Archive sibling" org-archive-to-archive-sibling (org-in-subtree-not-table-p)]
  19113. )
  19114. "--"
  19115. ("Hyperlinks"
  19116. ["Store Link (Global)" org-store-link t]
  19117. ["Find existing link to here" org-occur-link-in-agenda-files t]
  19118. ["Insert Link" org-insert-link t]
  19119. ["Follow Link" org-open-at-point t]
  19120. "--"
  19121. ["Next link" org-next-link t]
  19122. ["Previous link" org-previous-link t]
  19123. "--"
  19124. ["Descriptive Links"
  19125. org-toggle-link-display
  19126. :style radio
  19127. :selected org-descriptive-links
  19128. ]
  19129. ["Literal Links"
  19130. org-toggle-link-display
  19131. :style radio
  19132. :selected (not org-descriptive-links)])
  19133. "--"
  19134. ("TODO Lists"
  19135. ["TODO/DONE/-" org-todo t]
  19136. ("Select keyword"
  19137. ["Next keyword" org-shiftright (org-at-heading-p)]
  19138. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  19139. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  19140. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  19141. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  19142. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  19143. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  19144. "--"
  19145. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  19146. :selected org-enforce-todo-dependencies :style toggle :active t]
  19147. "Settings for tree at point"
  19148. ["Do Children sequentially" org-toggle-ordered-property :style radio
  19149. :selected (org-entry-get nil "ORDERED")
  19150. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  19151. ["Do Children parallel" org-toggle-ordered-property :style radio
  19152. :selected (not (org-entry-get nil "ORDERED"))
  19153. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  19154. "--"
  19155. ["Set Priority" org-priority t]
  19156. ["Priority Up" org-shiftup t]
  19157. ["Priority Down" org-shiftdown t]
  19158. "--"
  19159. ["Get news from all feeds" org-feed-update-all t]
  19160. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  19161. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  19162. ("TAGS and Properties"
  19163. ["Set Tags" org-set-tags-command (not (org-before-first-heading-p))]
  19164. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  19165. "--"
  19166. ["Set property" org-set-property (not (org-before-first-heading-p))]
  19167. ["Column view of properties" org-columns t]
  19168. ["Insert Column View DBlock" org-insert-columns-dblock t])
  19169. ("Dates and Scheduling"
  19170. ["Timestamp" org-time-stamp (not (org-before-first-heading-p))]
  19171. ["Timestamp (inactive)" org-time-stamp-inactive (not (org-before-first-heading-p))]
  19172. ("Change Date"
  19173. ["1 Day Later" org-shiftright (org-at-timestamp-p)]
  19174. ["1 Day Earlier" org-shiftleft (org-at-timestamp-p)]
  19175. ["1 ... Later" org-shiftup (org-at-timestamp-p)]
  19176. ["1 ... Earlier" org-shiftdown (org-at-timestamp-p)])
  19177. ["Compute Time Range" org-evaluate-time-range t]
  19178. ["Schedule Item" org-schedule (not (org-before-first-heading-p))]
  19179. ["Deadline" org-deadline (not (org-before-first-heading-p))]
  19180. "--"
  19181. ["Custom time format" org-toggle-time-stamp-overlays
  19182. :style radio :selected org-display-custom-times]
  19183. "--"
  19184. ["Goto Calendar" org-goto-calendar t]
  19185. ["Date from Calendar" org-date-from-calendar t]
  19186. "--"
  19187. ["Start/Restart Timer" org-timer-start t]
  19188. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  19189. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  19190. ["Insert Timer String" org-timer t]
  19191. ["Insert Timer Item" org-timer-item t])
  19192. ("Logging work"
  19193. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  19194. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  19195. ["Clock out" org-clock-out t]
  19196. ["Clock cancel" org-clock-cancel t]
  19197. "--"
  19198. ["Mark as default task" org-clock-mark-default-task t]
  19199. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  19200. ["Goto running clock" org-clock-goto t]
  19201. "--"
  19202. ["Display times" org-clock-display t]
  19203. ["Create clock table" org-clock-report t]
  19204. "--"
  19205. ["Record DONE time"
  19206. (progn (setq org-log-done (not org-log-done))
  19207. (message "Switching to %s will %s record a timestamp"
  19208. (car org-done-keywords)
  19209. (if org-log-done "automatically" "not")))
  19210. :style toggle :selected org-log-done])
  19211. "--"
  19212. ["Agenda Command..." org-agenda t]
  19213. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  19214. ("File List for Agenda")
  19215. ("Special views current file"
  19216. ["TODO Tree" org-show-todo-tree t]
  19217. ["Check Deadlines" org-check-deadlines t]
  19218. ["Timeline" org-timeline t]
  19219. ["Tags/Property tree" org-match-sparse-tree t])
  19220. "--"
  19221. ["Export/Publish..." org-export-dispatch t]
  19222. ("LaTeX"
  19223. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  19224. :selected org-cdlatex-mode]
  19225. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  19226. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  19227. ["Modify math symbol" org-cdlatex-math-modify
  19228. (org-inside-LaTeX-fragment-p)]
  19229. ["Insert citation" org-reftex-citation t]
  19230. "--"
  19231. ["Template for BEAMER" (org-beamer-insert-options-template) t])
  19232. "--"
  19233. ("MobileOrg"
  19234. ["Push Files and Views" org-mobile-push t]
  19235. ["Get Captured and Flagged" org-mobile-pull t]
  19236. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  19237. "--"
  19238. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  19239. "--"
  19240. ("Documentation"
  19241. ["Show Version" org-version t]
  19242. ["Info Documentation" org-info t])
  19243. ("Customize"
  19244. ["Browse Org Group" org-customize t]
  19245. "--"
  19246. ["Expand This Menu" org-create-customize-menu
  19247. (fboundp 'customize-menu-create)])
  19248. ["Send bug report" org-submit-bug-report t]
  19249. "--"
  19250. ("Refresh/Reload"
  19251. ["Refresh setup current buffer" org-mode-restart t]
  19252. ["Reload Org (after update)" org-reload t]
  19253. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x !"])
  19254. ))
  19255. (defun org-info (&optional node)
  19256. "Read documentation for Org-mode in the info system.
  19257. With optional NODE, go directly to that node."
  19258. (interactive)
  19259. (info (format "(org)%s" (or node ""))))
  19260. ;;;###autoload
  19261. (defun org-submit-bug-report ()
  19262. "Submit a bug report on Org-mode via mail.
  19263. Don't hesitate to report any problems or inaccurate documentation.
  19264. If you don't have setup sending mail from (X)Emacs, please copy the
  19265. output buffer into your mail program, as it gives us important
  19266. information about your Org-mode version and configuration."
  19267. (interactive)
  19268. (require 'reporter)
  19269. (org-load-modules-maybe)
  19270. (org-require-autoloaded-modules)
  19271. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  19272. (reporter-submit-bug-report
  19273. "emacs-orgmode@gnu.org"
  19274. (org-version nil 'full)
  19275. (let (list)
  19276. (save-window-excursion
  19277. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  19278. (delete-other-windows)
  19279. (erase-buffer)
  19280. (insert "You are about to submit a bug report to the Org-mode mailing list.
  19281. We would like to add your full Org-mode and Outline configuration to the
  19282. bug report. This greatly simplifies the work of the maintainer and
  19283. other experts on the mailing list.
  19284. HOWEVER, some variables you have customized may contain private
  19285. information. The names of customers, colleagues, or friends, might
  19286. appear in the form of file names, tags, todo states, or search strings.
  19287. If you answer yes to the prompt, you might want to check and remove
  19288. such private information before sending the email.")
  19289. (add-text-properties (point-min) (point-max) '(face org-warning))
  19290. (when (yes-or-no-p "Include your Org-mode configuration ")
  19291. (mapatoms
  19292. (lambda (v)
  19293. (and (boundp v)
  19294. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  19295. (or (and (symbol-value v)
  19296. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  19297. (and
  19298. (get v 'custom-type) (get v 'standard-value)
  19299. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  19300. (push v list)))))
  19301. (kill-buffer (get-buffer "*Warn about privacy*"))
  19302. list))
  19303. nil nil
  19304. "Remember to cover the basics, that is, what you expected to happen and
  19305. what in fact did happen. You don't know how to make a good report? See
  19306. http://orgmode.org/manual/Feedback.html#Feedback
  19307. Your bug report will be posted to the Org-mode mailing list.
  19308. ------------------------------------------------------------------------")
  19309. (save-excursion
  19310. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  19311. (replace-match "\\1Bug: \\3 [\\2]")))))
  19312. (defun org-install-agenda-files-menu ()
  19313. (let ((bl (buffer-list)))
  19314. (save-excursion
  19315. (while bl
  19316. (set-buffer (pop bl))
  19317. (if (derived-mode-p 'org-mode) (setq bl nil)))
  19318. (when (derived-mode-p 'org-mode)
  19319. (easy-menu-change
  19320. '("Org") "File List for Agenda"
  19321. (append
  19322. (list
  19323. ["Edit File List" (org-edit-agenda-file-list) t]
  19324. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  19325. ["Remove Current File from List" org-remove-file t]
  19326. ["Cycle through agenda files" org-cycle-agenda-files t]
  19327. ["Occur in all agenda files" org-occur-in-agenda-files t]
  19328. "--")
  19329. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  19330. ;;;; Documentation
  19331. (defun org-require-autoloaded-modules ()
  19332. (interactive)
  19333. (mapc 'require
  19334. '(org-agenda org-archive org-attach org-clock org-colview org-id
  19335. org-remember org-table org-timer)))
  19336. ;;;###autoload
  19337. (defun org-reload (&optional uncompiled)
  19338. "Reload all org lisp files.
  19339. With prefix arg UNCOMPILED, load the uncompiled versions."
  19340. (interactive "P")
  19341. (require 'loadhist)
  19342. (let* ((org-dir (org-find-library-dir "org"))
  19343. (contrib-dir (or (org-find-library-dir "org-contribdir") org-dir))
  19344. (feature-re "^\\(org\\|ob\\|ox\\)\\(-.*\\)?")
  19345. (remove-re (mapconcat 'identity
  19346. (mapcar (lambda (f) (concat "^" f "$"))
  19347. (list (if (featurep 'xemacs)
  19348. "org-colview"
  19349. "org-colview-xemacs")
  19350. "org" "org-loaddefs" "org-version"))
  19351. "\\|"))
  19352. (feats (delete-dups
  19353. (mapcar 'file-name-sans-extension
  19354. (mapcar 'file-name-nondirectory
  19355. (delq nil
  19356. (mapcar 'feature-file
  19357. features))))))
  19358. (lfeat (append
  19359. (sort
  19360. (setq feats
  19361. (delq nil (mapcar
  19362. (lambda (f)
  19363. (if (and (string-match feature-re f)
  19364. (not (string-match remove-re f)))
  19365. f nil))
  19366. feats)))
  19367. 'string-lessp)
  19368. (list "org-version" "org")))
  19369. (load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  19370. (load-suffixes (if uncompiled (reverse load-suffixes) load-suffixes))
  19371. load-uncore load-misses)
  19372. (setq load-misses
  19373. (delq 't
  19374. (mapcar (lambda (f)
  19375. (or (org-load-noerror-mustsuffix (concat org-dir f))
  19376. (and (string= org-dir contrib-dir)
  19377. (org-load-noerror-mustsuffix (concat contrib-dir f)))
  19378. (and (org-load-noerror-mustsuffix (concat (org-find-library-dir f) f))
  19379. (add-to-list 'load-uncore f 'append)
  19380. 't)
  19381. f))
  19382. lfeat)))
  19383. (if load-uncore
  19384. (message "The following feature%s found in load-path, please check if that's correct:\n%s"
  19385. (if (> (length load-uncore) 1) "s were" " was") load-uncore))
  19386. (if load-misses
  19387. (message "Some error occured while reloading Org feature%s\n%s\nPlease check *Messages*!\n%s"
  19388. (if (> (length load-misses) 1) "s" "") load-misses (org-version nil 'full))
  19389. (message "Successfully reloaded Org\n%s" (org-version nil 'full)))))
  19390. ;;;###autoload
  19391. (defun org-customize ()
  19392. "Call the customize function with org as argument."
  19393. (interactive)
  19394. (org-load-modules-maybe)
  19395. (org-require-autoloaded-modules)
  19396. (customize-browse 'org))
  19397. (defun org-create-customize-menu ()
  19398. "Create a full customization menu for Org-mode, insert it into the menu."
  19399. (interactive)
  19400. (org-load-modules-maybe)
  19401. (org-require-autoloaded-modules)
  19402. (if (fboundp 'customize-menu-create)
  19403. (progn
  19404. (easy-menu-change
  19405. '("Org") "Customize"
  19406. `(["Browse Org group" org-customize t]
  19407. "--"
  19408. ,(customize-menu-create 'org)
  19409. ["Set" Custom-set t]
  19410. ["Save" Custom-save t]
  19411. ["Reset to Current" Custom-reset-current t]
  19412. ["Reset to Saved" Custom-reset-saved t]
  19413. ["Reset to Standard Settings" Custom-reset-standard t]))
  19414. (message "\"Org\"-menu now contains full customization menu"))
  19415. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  19416. ;;;; Miscellaneous stuff
  19417. ;;; Generally useful functions
  19418. (defun org-get-at-bol (property)
  19419. "Get text property PROPERTY at beginning of line."
  19420. (get-text-property (point-at-bol) property))
  19421. (defun org-find-text-property-in-string (prop s)
  19422. "Return the first non-nil value of property PROP in string S."
  19423. (or (get-text-property 0 prop s)
  19424. (get-text-property (or (next-single-property-change 0 prop s) 0)
  19425. prop s)))
  19426. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  19427. "Display the given MESSAGE as a warning."
  19428. (if (fboundp 'display-warning)
  19429. (display-warning 'org message
  19430. (if (featurep 'xemacs) 'warning :warning))
  19431. (let ((buf (get-buffer-create "*Org warnings*")))
  19432. (with-current-buffer buf
  19433. (goto-char (point-max))
  19434. (insert "Warning (Org): " message)
  19435. (unless (bolp)
  19436. (newline)))
  19437. (display-buffer buf)
  19438. (sit-for 0))))
  19439. (defun org-eval (form)
  19440. "Eval FORM and return result."
  19441. (condition-case error
  19442. (eval form)
  19443. (error (format "%%![Error: %s]" error))))
  19444. (defun org-in-clocktable-p ()
  19445. "Check if the cursor is in a clocktable."
  19446. (let ((pos (point)) start)
  19447. (save-excursion
  19448. (end-of-line 1)
  19449. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  19450. (setq start (match-beginning 0))
  19451. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  19452. (>= (match-end 0) pos)
  19453. start))))
  19454. (defun org-in-commented-line ()
  19455. "Is point in a line starting with `#'?"
  19456. (equal (char-after (point-at-bol)) ?#))
  19457. (defun org-in-indented-comment-line ()
  19458. "Is point in a line starting with `#' after some white space?"
  19459. (save-excursion
  19460. (save-match-data
  19461. (goto-char (point-at-bol))
  19462. (looking-at "[ \t]*#"))))
  19463. (defun org-in-verbatim-emphasis ()
  19464. (save-match-data
  19465. (and (org-in-regexp org-emph-re 2)
  19466. (>= (point) (match-beginning 3))
  19467. (<= (point) (match-end 4))
  19468. (member (match-string 3) '("=" "~")))))
  19469. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  19470. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  19471. (if (and marker (marker-buffer marker)
  19472. (buffer-live-p (marker-buffer marker)))
  19473. (progn
  19474. (org-pop-to-buffer-same-window (marker-buffer marker))
  19475. (if (or (> marker (point-max)) (< marker (point-min)))
  19476. (widen))
  19477. (goto-char marker)
  19478. (org-show-context 'org-goto))
  19479. (if bookmark
  19480. (bookmark-jump bookmark)
  19481. (error "Cannot find location"))))
  19482. (defun org-quote-csv-field (s)
  19483. "Quote field for inclusion in CSV material."
  19484. (if (string-match "[\",]" s)
  19485. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  19486. s))
  19487. (defun org-force-self-insert (N)
  19488. "Needed to enforce self-insert under remapping."
  19489. (interactive "p")
  19490. (self-insert-command N))
  19491. (defun org-string-width (s)
  19492. "Compute width of string, ignoring invisible characters.
  19493. This ignores character with invisibility property `org-link', and also
  19494. characters with property `org-cwidth', because these will become invisible
  19495. upon the next fontification round."
  19496. (let (b l)
  19497. (when (or (eq t buffer-invisibility-spec)
  19498. (assq 'org-link buffer-invisibility-spec))
  19499. (while (setq b (text-property-any 0 (length s)
  19500. 'invisible 'org-link s))
  19501. (setq s (concat (substring s 0 b)
  19502. (substring s (or (next-single-property-change
  19503. b 'invisible s) (length s)))))))
  19504. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  19505. (setq s (concat (substring s 0 b)
  19506. (substring s (or (next-single-property-change
  19507. b 'org-cwidth s) (length s))))))
  19508. (setq l (string-width s) b -1)
  19509. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  19510. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  19511. l))
  19512. (defun org-shorten-string (s maxlength)
  19513. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  19514. If the string is shorter or has length MAXLENGTH, just return the
  19515. original string. If it is longer, the functions finds a space in the
  19516. string, breaks this string off at that locations and adds three dots
  19517. as ellipsis. Including the ellipsis, the string will not be longer
  19518. than MAXLENGTH. If finding a good breaking point in the string does
  19519. not work, the string is just chopped off in the middle of a word
  19520. if necessary."
  19521. (if (<= (length s) maxlength)
  19522. s
  19523. (let* ((n (max (- maxlength 4) 1))
  19524. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  19525. (if (string-match re s)
  19526. (concat (match-string 1 s) "...")
  19527. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  19528. (defun org-get-indentation (&optional line)
  19529. "Get the indentation of the current line, interpreting tabs.
  19530. When LINE is given, assume it represents a line and compute its indentation."
  19531. (if line
  19532. (if (string-match "^ *" (org-remove-tabs line))
  19533. (match-end 0))
  19534. (save-excursion
  19535. (beginning-of-line 1)
  19536. (skip-chars-forward " \t")
  19537. (current-column))))
  19538. (defun org-get-string-indentation (s)
  19539. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  19540. (let ((n -1) (i 0) (w tab-width) c)
  19541. (catch 'exit
  19542. (while (< (setq n (1+ n)) (length s))
  19543. (setq c (aref s n))
  19544. (cond ((= c ?\ ) (setq i (1+ i)))
  19545. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  19546. (t (throw 'exit t)))))
  19547. i))
  19548. (defun org-remove-tabs (s &optional width)
  19549. "Replace tabulators in S with spaces.
  19550. Assumes that s is a single line, starting in column 0."
  19551. (setq width (or width tab-width))
  19552. (while (string-match "\t" s)
  19553. (setq s (replace-match
  19554. (make-string
  19555. (- (* width (/ (+ (match-beginning 0) width) width))
  19556. (match-beginning 0)) ?\ )
  19557. t t s)))
  19558. s)
  19559. (defun org-fix-indentation (line ind)
  19560. "Fix indentation in LINE.
  19561. IND is a cons cell with target and minimum indentation.
  19562. If the current indentation in LINE is smaller than the minimum,
  19563. leave it alone. If it is larger than ind, set it to the target."
  19564. (let* ((l (org-remove-tabs line))
  19565. (i (org-get-indentation l))
  19566. (i1 (car ind)) (i2 (cdr ind)))
  19567. (if (>= i i2) (setq l (substring line i2)))
  19568. (if (> i1 0)
  19569. (concat (make-string i1 ?\ ) l)
  19570. l)))
  19571. (defun org-remove-indentation (code &optional n)
  19572. "Remove the maximum common indentation from the lines in CODE.
  19573. N may optionally be the number of spaces to remove."
  19574. (with-temp-buffer
  19575. (insert code)
  19576. (org-do-remove-indentation n)
  19577. (buffer-string)))
  19578. (defun org-do-remove-indentation (&optional n)
  19579. "Remove the maximum common indentation from the buffer."
  19580. (untabify (point-min) (point-max))
  19581. (let ((min 10000) re)
  19582. (if n
  19583. (setq min n)
  19584. (goto-char (point-min))
  19585. (while (re-search-forward "^ *[^ \n]" nil t)
  19586. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  19587. (unless (or (= min 0) (= min 10000))
  19588. (setq re (format "^ \\{%d\\}" min))
  19589. (goto-char (point-min))
  19590. (while (re-search-forward re nil t)
  19591. (replace-match "")
  19592. (end-of-line 1))
  19593. min)))
  19594. (defun org-fill-template (template alist)
  19595. "Find each %key of ALIST in TEMPLATE and replace it."
  19596. (let ((case-fold-search nil)
  19597. entry key value)
  19598. (setq alist (sort (copy-sequence alist)
  19599. (lambda (a b) (< (length (car a)) (length (car b))))))
  19600. (while (setq entry (pop alist))
  19601. (setq template
  19602. (replace-regexp-in-string
  19603. (concat "%" (regexp-quote (car entry)))
  19604. (or (cdr entry) "") template t t)))
  19605. template))
  19606. (defun org-base-buffer (buffer)
  19607. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  19608. (if (not buffer)
  19609. buffer
  19610. (or (buffer-base-buffer buffer)
  19611. buffer)))
  19612. (defun org-trim (s)
  19613. "Remove whitespace at beginning and end of string."
  19614. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  19615. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  19616. s)
  19617. (defun org-wrap (string &optional width lines)
  19618. "Wrap string to either a number of lines, or a width in characters.
  19619. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  19620. that costs. If there is a word longer than WIDTH, the text is actually
  19621. wrapped to the length of that word.
  19622. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  19623. many lines, whatever width that takes.
  19624. The return value is a list of lines, without newlines at the end."
  19625. (let* ((words (org-split-string string "[ \t\n]+"))
  19626. (maxword (apply 'max (mapcar 'org-string-width words)))
  19627. w ll)
  19628. (cond (width
  19629. (org-do-wrap words (max maxword width)))
  19630. (lines
  19631. (setq w maxword)
  19632. (setq ll (org-do-wrap words maxword))
  19633. (if (<= (length ll) lines)
  19634. ll
  19635. (setq ll words)
  19636. (while (> (length ll) lines)
  19637. (setq w (1+ w))
  19638. (setq ll (org-do-wrap words w)))
  19639. ll))
  19640. (t (error "Cannot wrap this")))))
  19641. (defun org-do-wrap (words width)
  19642. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  19643. (let (lines line)
  19644. (while words
  19645. (setq line (pop words))
  19646. (while (and words (< (+ (length line) (length (car words))) width))
  19647. (setq line (concat line " " (pop words))))
  19648. (setq lines (push line lines)))
  19649. (nreverse lines)))
  19650. (defun org-split-string (string &optional separators)
  19651. "Splits STRING into substrings at SEPARATORS.
  19652. No empty strings are returned if there are matches at the beginning
  19653. and end of string."
  19654. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  19655. (start 0)
  19656. notfirst
  19657. (list nil))
  19658. (while (and (string-match rexp string
  19659. (if (and notfirst
  19660. (= start (match-beginning 0))
  19661. (< start (length string)))
  19662. (1+ start) start))
  19663. (< (match-beginning 0) (length string)))
  19664. (setq notfirst t)
  19665. (or (eq (match-beginning 0) 0)
  19666. (and (eq (match-beginning 0) (match-end 0))
  19667. (eq (match-beginning 0) start))
  19668. (setq list
  19669. (cons (substring string start (match-beginning 0))
  19670. list)))
  19671. (setq start (match-end 0)))
  19672. (or (eq start (length string))
  19673. (setq list
  19674. (cons (substring string start)
  19675. list)))
  19676. (nreverse list)))
  19677. (defun org-quote-vert (s)
  19678. "Replace \"|\" with \"\\vert\"."
  19679. (while (string-match "|" s)
  19680. (setq s (replace-match "\\vert" t t s)))
  19681. s)
  19682. (defun org-uuidgen-p (s)
  19683. "Is S an ID created by UUIDGEN?"
  19684. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  19685. (defun org-in-src-block-p (&optional inside)
  19686. "Whether point is in a code source block.
  19687. When INSIDE is non-nil, don't consider we are within a src block
  19688. when point is at #+BEGIN_SRC or #+END_SRC."
  19689. (let ((case-fold-search t) ov)
  19690. (or (and (setq ov (overlays-at (point)))
  19691. (memq 'org-block-background
  19692. (overlay-properties (car ov))))
  19693. (and (not inside)
  19694. (save-match-data
  19695. (save-excursion
  19696. (beginning-of-line)
  19697. (looking-at ".*#\\+\\(begin\\|end\\)_src")))))))
  19698. (defun org-context ()
  19699. "Return a list of contexts of the current cursor position.
  19700. If several contexts apply, all are returned.
  19701. Each context entry is a list with a symbol naming the context, and
  19702. two positions indicating start and end of the context. Possible
  19703. contexts are:
  19704. :headline anywhere in a headline
  19705. :headline-stars on the leading stars in a headline
  19706. :todo-keyword on a TODO keyword (including DONE) in a headline
  19707. :tags on the TAGS in a headline
  19708. :priority on the priority cookie in a headline
  19709. :item on the first line of a plain list item
  19710. :item-bullet on the bullet/number of a plain list item
  19711. :checkbox on the checkbox in a plain list item
  19712. :table in an org-mode table
  19713. :table-special on a special filed in a table
  19714. :table-table in a table.el table
  19715. :clocktable in a clocktable
  19716. :src-block in a source block
  19717. :link on a hyperlink
  19718. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT, QUOTE.
  19719. :target on a <<target>>
  19720. :radio-target on a <<<radio-target>>>
  19721. :latex-fragment on a LaTeX fragment
  19722. :latex-preview on a LaTeX fragment with overlaid preview image
  19723. This function expects the position to be visible because it uses font-lock
  19724. faces as a help to recognize the following contexts: :table-special, :link,
  19725. and :keyword."
  19726. (let* ((f (get-text-property (point) 'face))
  19727. (faces (if (listp f) f (list f)))
  19728. (case-fold-search t)
  19729. (p (point)) clist o)
  19730. ;; First the large context
  19731. (cond
  19732. ((org-at-heading-p t)
  19733. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  19734. (when (progn
  19735. (beginning-of-line 1)
  19736. (looking-at org-todo-line-tags-regexp))
  19737. (push (org-point-in-group p 1 :headline-stars) clist)
  19738. (push (org-point-in-group p 2 :todo-keyword) clist)
  19739. (push (org-point-in-group p 4 :tags) clist))
  19740. (goto-char p)
  19741. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  19742. (if (looking-at "\\[#[A-Z0-9]\\]")
  19743. (push (org-point-in-group p 0 :priority) clist)))
  19744. ((org-at-item-p)
  19745. (push (org-point-in-group p 2 :item-bullet) clist)
  19746. (push (list :item (point-at-bol)
  19747. (save-excursion (org-end-of-item) (point)))
  19748. clist)
  19749. (and (org-at-item-checkbox-p)
  19750. (push (org-point-in-group p 0 :checkbox) clist)))
  19751. ((org-at-table-p)
  19752. (push (list :table (org-table-begin) (org-table-end)) clist)
  19753. (if (memq 'org-formula faces)
  19754. (push (list :table-special
  19755. (previous-single-property-change p 'face)
  19756. (next-single-property-change p 'face)) clist)))
  19757. ((org-at-table-p 'any)
  19758. (push (list :table-table) clist)))
  19759. (goto-char p)
  19760. (let ((case-fold-search t))
  19761. ;; New the "medium" contexts: clocktables, source blocks
  19762. (cond ((org-in-clocktable-p)
  19763. (push (list :clocktable
  19764. (and (or (looking-at "#\\+BEGIN: clocktable")
  19765. (search-backward "#+BEGIN: clocktable" nil t))
  19766. (match-beginning 0))
  19767. (and (re-search-forward "#\\+END:?" nil t)
  19768. (match-end 0))) clist))
  19769. ((org-in-src-block-p)
  19770. (push (list :src-block
  19771. (and (or (looking-at "#\\+BEGIN_SRC")
  19772. (search-backward "#+BEGIN_SRC" nil t))
  19773. (match-beginning 0))
  19774. (and (search-forward "#+END_SRC" nil t)
  19775. (match-beginning 0))) clist))))
  19776. (goto-char p)
  19777. ;; Now the small context
  19778. (cond
  19779. ((org-at-timestamp-p)
  19780. (push (org-point-in-group p 0 :timestamp) clist))
  19781. ((memq 'org-link faces)
  19782. (push (list :link
  19783. (previous-single-property-change p 'face)
  19784. (next-single-property-change p 'face)) clist))
  19785. ((memq 'org-special-keyword faces)
  19786. (push (list :keyword
  19787. (previous-single-property-change p 'face)
  19788. (next-single-property-change p 'face)) clist))
  19789. ((org-at-target-p)
  19790. (push (org-point-in-group p 0 :target) clist)
  19791. (goto-char (1- (match-beginning 0)))
  19792. (if (looking-at org-radio-target-regexp)
  19793. (push (org-point-in-group p 0 :radio-target) clist))
  19794. (goto-char p))
  19795. ((setq o (car (delq nil
  19796. (mapcar
  19797. (lambda (x)
  19798. (if (memq x org-latex-fragment-image-overlays) x))
  19799. (overlays-at (point))))))
  19800. (push (list :latex-fragment
  19801. (overlay-start o) (overlay-end o)) clist)
  19802. (push (list :latex-preview
  19803. (overlay-start o) (overlay-end o)) clist))
  19804. ((org-inside-LaTeX-fragment-p)
  19805. ;; FIXME: positions wrong.
  19806. (push (list :latex-fragment (point) (point)) clist)))
  19807. (setq clist (nreverse (delq nil clist)))
  19808. clist))
  19809. ;; FIXME: Compare with at-regexp-p Do we need both?
  19810. (defun org-in-regexp (re &optional nlines visually)
  19811. "Check if point is inside a match of regexp.
  19812. Normally only the current line is checked, but you can include NLINES extra
  19813. lines both before and after point into the search.
  19814. If VISUALLY is set, require that the cursor is not after the match but
  19815. really on, so that the block visually is on the match."
  19816. (catch 'exit
  19817. (let ((pos (point))
  19818. (eol (point-at-eol (+ 1 (or nlines 0))))
  19819. (inc (if visually 1 0)))
  19820. (save-excursion
  19821. (beginning-of-line (- 1 (or nlines 0)))
  19822. (while (re-search-forward re eol t)
  19823. (if (and (<= (match-beginning 0) pos)
  19824. (>= (+ inc (match-end 0)) pos))
  19825. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  19826. (defun org-at-regexp-p (regexp)
  19827. "Is point inside a match of REGEXP in the current line?"
  19828. (catch 'exit
  19829. (save-excursion
  19830. (let ((pos (point)) (end (point-at-eol)))
  19831. (beginning-of-line 1)
  19832. (while (re-search-forward regexp end t)
  19833. (if (and (<= (match-beginning 0) pos)
  19834. (>= (match-end 0) pos))
  19835. (throw 'exit t)))
  19836. nil))))
  19837. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  19838. "Non-nil when point is between matches of START-RE and END-RE.
  19839. Also return a non-nil value when point is on one of the matches.
  19840. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  19841. buffer positions. Default values are the positions of headlines
  19842. surrounding the point.
  19843. The functions returns a cons cell whose car (resp. cdr) is the
  19844. position before START-RE (resp. after END-RE)."
  19845. (save-match-data
  19846. (let ((pos (point))
  19847. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  19848. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  19849. beg end)
  19850. (save-excursion
  19851. ;; Point is on a block when on START-RE or if START-RE can be
  19852. ;; found before it...
  19853. (and (or (org-at-regexp-p start-re)
  19854. (re-search-backward start-re limit-up t))
  19855. (setq beg (match-beginning 0))
  19856. ;; ... and END-RE after it...
  19857. (goto-char (match-end 0))
  19858. (re-search-forward end-re limit-down t)
  19859. (> (setq end (match-end 0)) pos)
  19860. ;; ... without another START-RE in-between.
  19861. (goto-char (match-beginning 0))
  19862. (not (re-search-backward start-re (1+ beg) t))
  19863. ;; Return value.
  19864. (cons beg end))))))
  19865. (defun org-in-block-p (names)
  19866. "Non-nil when point belongs to a block whose name belongs to NAMES.
  19867. NAMES is a list of strings containing names of blocks.
  19868. Return first block name matched, or nil. Beware that in case of
  19869. nested blocks, the returned name may not belong to the closest
  19870. block from point."
  19871. (save-match-data
  19872. (catch 'exit
  19873. (let ((case-fold-search t)
  19874. (lim-up (save-excursion (outline-previous-heading)))
  19875. (lim-down (save-excursion (outline-next-heading))))
  19876. (mapc (lambda (name)
  19877. (let ((n (regexp-quote name)))
  19878. (when (org-between-regexps-p
  19879. (concat "^[ \t]*#\\+begin_" n)
  19880. (concat "^[ \t]*#\\+end_" n)
  19881. lim-up lim-down)
  19882. (throw 'exit n))))
  19883. names))
  19884. nil)))
  19885. (defun org-in-drawer-p ()
  19886. "Is point within a drawer?"
  19887. (save-match-data
  19888. (let ((case-fold-search t)
  19889. (lim-up (save-excursion (outline-previous-heading)))
  19890. (lim-down (save-excursion (outline-next-heading))))
  19891. (org-between-regexps-p
  19892. (concat "^[ \t]*:" (regexp-opt org-drawers) ":")
  19893. "^[ \t]*:end:.*$"
  19894. lim-up lim-down))))
  19895. (defun org-occur-in-agenda-files (regexp &optional nlines)
  19896. "Call `multi-occur' with buffers for all agenda files."
  19897. (interactive "sOrg-files matching: \np")
  19898. (let* ((files (org-agenda-files))
  19899. (tnames (mapcar 'file-truename files))
  19900. (extra org-agenda-text-search-extra-files)
  19901. f)
  19902. (when (eq (car extra) 'agenda-archives)
  19903. (setq extra (cdr extra))
  19904. (setq files (org-add-archive-files files)))
  19905. (while (setq f (pop extra))
  19906. (unless (member (file-truename f) tnames)
  19907. (add-to-list 'files f 'append)
  19908. (add-to-list 'tnames (file-truename f) 'append)))
  19909. (multi-occur
  19910. (mapcar (lambda (x)
  19911. (with-current-buffer
  19912. (or (get-file-buffer x) (find-file-noselect x))
  19913. (widen)
  19914. (current-buffer)))
  19915. files)
  19916. regexp)))
  19917. (if (boundp 'occur-mode-find-occurrence-hook)
  19918. ;; Emacs 23
  19919. (add-hook 'occur-mode-find-occurrence-hook
  19920. (lambda ()
  19921. (when (derived-mode-p 'org-mode)
  19922. (org-reveal))))
  19923. ;; Emacs 22
  19924. (defadvice occur-mode-goto-occurrence
  19925. (after org-occur-reveal activate)
  19926. (and (derived-mode-p 'org-mode) (org-reveal)))
  19927. (defadvice occur-mode-goto-occurrence-other-window
  19928. (after org-occur-reveal activate)
  19929. (and (derived-mode-p 'org-mode) (org-reveal)))
  19930. (defadvice occur-mode-display-occurrence
  19931. (after org-occur-reveal activate)
  19932. (when (derived-mode-p 'org-mode)
  19933. (let ((pos (occur-mode-find-occurrence)))
  19934. (with-current-buffer (marker-buffer pos)
  19935. (save-excursion
  19936. (goto-char pos)
  19937. (org-reveal)))))))
  19938. (defun org-occur-link-in-agenda-files ()
  19939. "Create a link and search for it in the agendas.
  19940. The link is not stored in `org-stored-links', it is just created
  19941. for the search purpose."
  19942. (interactive)
  19943. (let ((link (condition-case nil
  19944. (org-store-link nil)
  19945. (error "Unable to create a link to here"))))
  19946. (org-occur-in-agenda-files (regexp-quote link))))
  19947. (defun org-reverse-string (string)
  19948. "Return the reverse of STRING."
  19949. (apply 'string (reverse (string-to-list string))))
  19950. (defsubst org-uniquify (list)
  19951. "Non-destructively remove duplicate elements from LIST."
  19952. (let ((res (copy-sequence list))) (delete-dups res)))
  19953. (defun org-uniquify-alist (alist)
  19954. "Merge elements of ALIST with the same key.
  19955. For example, in this alist:
  19956. \(org-uniquify-alist '((a 1) (b 2) (a 3)))
  19957. => '((a 1 3) (b 2))
  19958. merge (a 1) and (a 3) into (a 1 3).
  19959. The function returns the new ALIST."
  19960. (let (rtn)
  19961. (mapc
  19962. (lambda (e)
  19963. (let (n)
  19964. (if (not (assoc (car e) rtn))
  19965. (push e rtn)
  19966. (setq n (cons (car e) (append (cdr (assoc (car e) rtn)) (cdr e))))
  19967. (setq rtn (assq-delete-all (car e) rtn))
  19968. (push n rtn))))
  19969. alist)
  19970. rtn))
  19971. (defun org-delete-all (elts list)
  19972. "Remove all elements in ELTS from LIST."
  19973. (while elts
  19974. (setq list (delete (pop elts) list)))
  19975. list)
  19976. (defun org-count (cl-item cl-seq)
  19977. "Count the number of occurrences of ITEM in SEQ.
  19978. Taken from `count' in cl-seq.el with all keyword arguments removed."
  19979. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  19980. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  19981. (while (< cl-start cl-end)
  19982. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  19983. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  19984. (setq cl-start (1+ cl-start)))
  19985. cl-count))
  19986. (defun org-remove-if (predicate seq)
  19987. "Remove everything from SEQ that fulfills PREDICATE."
  19988. (let (res e)
  19989. (while seq
  19990. (setq e (pop seq))
  19991. (if (not (funcall predicate e)) (push e res)))
  19992. (nreverse res)))
  19993. (defun org-remove-if-not (predicate seq)
  19994. "Remove everything from SEQ that does not fulfill PREDICATE."
  19995. (let (res e)
  19996. (while seq
  19997. (setq e (pop seq))
  19998. (if (funcall predicate e) (push e res)))
  19999. (nreverse res)))
  20000. (defun org-reduce (cl-func cl-seq &rest cl-keys)
  20001. "Reduce two-argument FUNCTION across SEQ.
  20002. Taken from `reduce' in cl-seq.el with all keyword arguments but
  20003. \":initial-value\" removed."
  20004. (let ((cl-accum (cond ((memq :initial-value cl-keys)
  20005. (cadr (memq :initial-value cl-keys)))
  20006. (cl-seq (pop cl-seq))
  20007. (t (funcall cl-func)))))
  20008. (while cl-seq
  20009. (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
  20010. cl-accum))
  20011. (defun org-every (pred seq)
  20012. "Return true if PREDICATE is true of every element of SEQ.
  20013. Adapted from `every' in cl.el."
  20014. (catch 'org-every
  20015. (mapc (lambda (e) (unless (funcall pred e) (throw 'org-every nil))) seq)
  20016. t))
  20017. (defun org-some (pred seq)
  20018. "Return true if PREDICATE is true of any element of SEQ.
  20019. Adapted from `some' in cl.el."
  20020. (catch 'org-some
  20021. (mapc (lambda (e) (when (funcall pred e) (throw 'org-some t))) seq)
  20022. nil))
  20023. (defun org-back-over-empty-lines ()
  20024. "Move backwards over whitespace, to the beginning of the first empty line.
  20025. Returns the number of empty lines passed."
  20026. (let ((pos (point)))
  20027. (if (cdr (assoc 'heading org-blank-before-new-entry))
  20028. (skip-chars-backward " \t\n\r")
  20029. (unless (eobp)
  20030. (forward-line -1)))
  20031. (beginning-of-line 2)
  20032. (goto-char (min (point) pos))
  20033. (count-lines (point) pos)))
  20034. (defun org-skip-whitespace ()
  20035. (skip-chars-forward " \t\n\r"))
  20036. (defun org-point-in-group (point group &optional context)
  20037. "Check if POINT is in match-group GROUP.
  20038. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  20039. match. If the match group does not exist or point is not inside it,
  20040. return nil."
  20041. (and (match-beginning group)
  20042. (>= point (match-beginning group))
  20043. (<= point (match-end group))
  20044. (if context
  20045. (list context (match-beginning group) (match-end group))
  20046. t)))
  20047. (defun org-switch-to-buffer-other-window (&rest args)
  20048. "Switch to buffer in a second window on the current frame.
  20049. In particular, do not allow pop-up frames.
  20050. Returns the newly created buffer."
  20051. (org-no-popups
  20052. (apply 'switch-to-buffer-other-window args)))
  20053. (defun org-combine-plists (&rest plists)
  20054. "Create a single property list from all plists in PLISTS.
  20055. The process starts by copying the first list, and then setting properties
  20056. from the other lists. Settings in the last list are the most significant
  20057. ones and overrule settings in the other lists."
  20058. (let ((rtn (copy-sequence (pop plists)))
  20059. p v ls)
  20060. (while plists
  20061. (setq ls (pop plists))
  20062. (while ls
  20063. (setq p (pop ls) v (pop ls))
  20064. (setq rtn (plist-put rtn p v))))
  20065. rtn))
  20066. (defun org-replace-escapes (string table)
  20067. "Replace %-escapes in STRING with values in TABLE.
  20068. TABLE is an association list with keys like \"%a\" and string values.
  20069. The sequences in STRING may contain normal field width and padding information,
  20070. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  20071. so values can contain further %-escapes if they are define later in TABLE."
  20072. (let ((tbl (copy-alist table))
  20073. (case-fold-search nil)
  20074. (pchg 0)
  20075. e re rpl)
  20076. (while (setq e (pop tbl))
  20077. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  20078. (when (and (cdr e) (string-match re (cdr e)))
  20079. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  20080. (safe "SREF"))
  20081. (add-text-properties 0 3 (list 'sref sref) safe)
  20082. (setcdr e (replace-match safe t t (cdr e)))))
  20083. (while (string-match re string)
  20084. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  20085. (cdr e)))
  20086. (setq string (replace-match rpl t t string))))
  20087. (while (setq pchg (next-property-change pchg string))
  20088. (let ((sref (get-text-property pchg 'sref string)))
  20089. (when (and sref (string-match "SREF" string pchg))
  20090. (setq string (replace-match sref t t string)))))
  20091. string))
  20092. (defun org-sublist (list start end)
  20093. "Return a section of LIST, from START to END.
  20094. Counting starts at 1."
  20095. (let (rtn (c start))
  20096. (setq list (nthcdr (1- start) list))
  20097. (while (and list (<= c end))
  20098. (push (pop list) rtn)
  20099. (setq c (1+ c)))
  20100. (nreverse rtn)))
  20101. (defun org-find-base-buffer-visiting (file)
  20102. "Like `find-buffer-visiting' but always return the base buffer and
  20103. not an indirect buffer."
  20104. (let ((buf (or (get-file-buffer file)
  20105. (find-buffer-visiting file))))
  20106. (if buf
  20107. (or (buffer-base-buffer buf) buf)
  20108. nil)))
  20109. (defun org-image-file-name-regexp (&optional extensions)
  20110. "Return regexp matching the file names of images.
  20111. If EXTENSIONS is given, only match these."
  20112. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  20113. (image-file-name-regexp)
  20114. (let ((image-file-name-extensions
  20115. (or extensions
  20116. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  20117. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  20118. (concat "\\."
  20119. (regexp-opt (nconc (mapcar 'upcase
  20120. image-file-name-extensions)
  20121. image-file-name-extensions)
  20122. t)
  20123. "\\'"))))
  20124. (defun org-file-image-p (file &optional extensions)
  20125. "Return non-nil if FILE is an image."
  20126. (save-match-data
  20127. (string-match (org-image-file-name-regexp extensions) file)))
  20128. (defun org-get-cursor-date (&optional with-time)
  20129. "Return the date at cursor in as a time.
  20130. This works in the calendar and in the agenda, anywhere else it just
  20131. returns the current time.
  20132. If WITH-TIME is non-nil, returns the time of the event at point (in
  20133. the agenda) or the current time of the day."
  20134. (let (date day defd tp tm hod mod)
  20135. (when with-time
  20136. (setq tp (get-text-property (point) 'time))
  20137. (when (and tp (string-match "\\([0-9][0-9]\\):\\([0-9][0-9]\\)" tp))
  20138. (setq hod (string-to-number (match-string 1 tp))
  20139. mod (string-to-number (match-string 2 tp))))
  20140. (or tp (setq hod (nth 2 (decode-time (current-time)))
  20141. mod (nth 1 (decode-time (current-time))))))
  20142. (cond
  20143. ((eq major-mode 'calendar-mode)
  20144. (setq date (calendar-cursor-to-date)
  20145. defd (encode-time 0 (or mod 0) (or hod 0)
  20146. (nth 1 date) (nth 0 date) (nth 2 date))))
  20147. ((eq major-mode 'org-agenda-mode)
  20148. (setq day (get-text-property (point) 'day))
  20149. (if day
  20150. (setq date (calendar-gregorian-from-absolute day)
  20151. defd (encode-time 0 (or mod 0) (or hod 0)
  20152. (nth 1 date) (nth 0 date) (nth 2 date))))))
  20153. (or defd (current-time))))
  20154. (defun org-mark-subtree (&optional up)
  20155. "Mark the current subtree.
  20156. This puts point at the start of the current subtree, and mark at
  20157. the end. If a numeric prefix UP is given, move up into the
  20158. hierarchy of headlines by UP levels before marking the subtree."
  20159. (interactive "P")
  20160. (org-with-limited-levels
  20161. (cond ((org-at-heading-p) (beginning-of-line))
  20162. ((org-before-first-heading-p) (user-error "Not in a subtree"))
  20163. (t (outline-previous-visible-heading 1))))
  20164. (when up (while (and (> up 0) (org-up-heading-safe)) (decf up)))
  20165. (if (org-called-interactively-p 'any)
  20166. (call-interactively 'org-mark-element)
  20167. (org-mark-element)))
  20168. ;;; Indentation
  20169. (defun org-indent-line ()
  20170. "Indent line depending on context."
  20171. (interactive)
  20172. (let* ((pos (point))
  20173. (itemp (org-at-item-p))
  20174. (case-fold-search t)
  20175. (org-drawer-regexp (or org-drawer-regexp "\000"))
  20176. (inline-task-p (and (featurep 'org-inlinetask)
  20177. (org-inlinetask-in-task-p)))
  20178. (inline-re (and inline-task-p
  20179. (org-inlinetask-outline-regexp)))
  20180. column)
  20181. (if (and orgstruct-is-++ (eq pos (point)))
  20182. (let ((indent-line-function (cadadr (assoc 'indent-line-function org-fb-vars))))
  20183. (indent-according-to-mode))
  20184. (beginning-of-line 1)
  20185. (cond
  20186. ;; Headings
  20187. ((looking-at org-outline-regexp) (setq column 0))
  20188. ;; Footnote definition
  20189. ((looking-at org-footnote-definition-re) (setq column 0))
  20190. ;; Literal examples
  20191. ((looking-at "[ \t]*:\\( \\|$\\)")
  20192. (setq column (org-get-indentation))) ; do nothing
  20193. ;; Lists
  20194. ((ignore-errors (goto-char (org-in-item-p)))
  20195. (setq column (if itemp
  20196. (org-get-indentation)
  20197. (org-list-item-body-column (point))))
  20198. (goto-char pos))
  20199. ;; Drawers
  20200. ((and (looking-at "[ \t]*:END:")
  20201. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  20202. (save-excursion
  20203. (goto-char (1- (match-beginning 1)))
  20204. (setq column (current-column))))
  20205. ;; Special blocks
  20206. ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
  20207. (save-excursion
  20208. (re-search-backward
  20209. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  20210. (setq column (org-get-indentation (match-string 0))))
  20211. ((and (not (looking-at "[ \t]*#\\+begin_"))
  20212. (org-between-regexps-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
  20213. (save-excursion
  20214. (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
  20215. (setq column
  20216. (cond ((equal (downcase (match-string 1)) "src")
  20217. ;; src blocks: let `org-edit-src-exit' handle them
  20218. (org-get-indentation))
  20219. ((equal (downcase (match-string 1)) "example")
  20220. (max (org-get-indentation)
  20221. (org-get-indentation (match-string 0))))
  20222. (t
  20223. (org-get-indentation (match-string 0))))))
  20224. ;; This line has nothing special, look at the previous relevant
  20225. ;; line to compute indentation
  20226. (t
  20227. (beginning-of-line 0)
  20228. (while (and (not (bobp))
  20229. (not (looking-at org-table-line-regexp))
  20230. (not (looking-at org-drawer-regexp))
  20231. ;; When point started in an inline task, do not move
  20232. ;; above task starting line.
  20233. (not (and inline-task-p (looking-at inline-re)))
  20234. ;; Skip drawers, blocks, empty lines, verbatim,
  20235. ;; comments, tables, footnotes definitions, lists,
  20236. ;; inline tasks.
  20237. (or (and (looking-at "[ \t]*:END:")
  20238. (re-search-backward org-drawer-regexp nil t))
  20239. (and (looking-at "[ \t]*#\\+end_")
  20240. (re-search-backward "[ \t]*#\\+begin_"nil t))
  20241. (looking-at "[ \t]*[\n:#|]")
  20242. (looking-at org-footnote-definition-re)
  20243. (and (not inline-task-p)
  20244. (featurep 'org-inlinetask)
  20245. (org-inlinetask-in-task-p)
  20246. (or (org-inlinetask-goto-beginning) t))))
  20247. (beginning-of-line 0))
  20248. (cond
  20249. ;; There was a list item above.
  20250. ((save-excursion
  20251. (and (ignore-errors (goto-char (org-in-item-p)))
  20252. (goto-char
  20253. (org-list-get-top-point (org-list-struct)))))
  20254. (looking-at org-list-full-item-re)
  20255. (setq column (length (match-string 0))))
  20256. ;; There was an heading above.
  20257. ((looking-at "\\*+[ \t]+")
  20258. (if (not org-adapt-indentation)
  20259. (setq column 0)
  20260. (goto-char (match-end 0))
  20261. (setq column (current-column))))
  20262. ;; A drawer had started and is unfinished
  20263. ((looking-at org-drawer-regexp)
  20264. (goto-char (1- (match-beginning 1)))
  20265. (setq column (current-column)))
  20266. ;; Else, nothing noticeable found: get indentation and go on.
  20267. (t (setq column (org-get-indentation))))))
  20268. ;; Now apply indentation and move cursor accordingly
  20269. (goto-char pos)
  20270. (if (<= (current-column) (current-indentation))
  20271. (org-indent-line-to column)
  20272. (save-excursion (org-indent-line-to column)))
  20273. ;; Special polishing for properties, see `org-property-format'
  20274. (setq column (current-column))
  20275. (beginning-of-line 1)
  20276. (if (looking-at org-property-re)
  20277. (replace-match (concat (match-string 4)
  20278. (format org-property-format
  20279. (match-string 1) (match-string 3)))
  20280. t t))
  20281. (org-move-to-column column))))
  20282. (defun org-indent-drawer ()
  20283. "Indent the drawer at point."
  20284. (interactive)
  20285. (let ((p (point))
  20286. (e (and (save-excursion (re-search-forward ":END:" nil t))
  20287. (match-end 0)))
  20288. (folded
  20289. (save-excursion
  20290. (end-of-line)
  20291. (when (overlays-at (point))
  20292. (member 'invisible (overlay-properties
  20293. (car (overlays-at (point)))))))))
  20294. (when folded (org-cycle))
  20295. (indent-for-tab-command)
  20296. (while (and (move-beginning-of-line 2) (< (point) e))
  20297. (indent-for-tab-command))
  20298. (goto-char p)
  20299. (when folded (org-cycle)))
  20300. (message "Drawer at point indented"))
  20301. (defun org-indent-block ()
  20302. "Indent the block at point."
  20303. (interactive)
  20304. (let ((p (point))
  20305. (case-fold-search t)
  20306. (e (and (save-excursion (re-search-forward "#\\+end_?\\(?:[a-z]+\\)?" nil t))
  20307. (match-end 0)))
  20308. (folded
  20309. (save-excursion
  20310. (end-of-line)
  20311. (when (overlays-at (point))
  20312. (member 'invisible (overlay-properties
  20313. (car (overlays-at (point)))))))))
  20314. (when folded (org-cycle))
  20315. (indent-for-tab-command)
  20316. (while (and (move-beginning-of-line 2) (< (point) e))
  20317. (indent-for-tab-command))
  20318. (goto-char p)
  20319. (when folded (org-cycle)))
  20320. (message "Block at point indented"))
  20321. (defun org-indent-region (start end)
  20322. "Indent region."
  20323. (interactive "r")
  20324. (save-excursion
  20325. (let ((line-end (org-current-line end)))
  20326. (goto-char start)
  20327. (while (< (org-current-line) line-end)
  20328. (cond ((org-in-src-block-p) (org-src-native-tab-command-maybe))
  20329. (t (call-interactively 'org-indent-line)))
  20330. (move-beginning-of-line 2)))))
  20331. ;;; Filling
  20332. ;; We use our own fill-paragraph and auto-fill functions.
  20333. ;; `org-fill-paragraph' relies on adaptive filling and context
  20334. ;; checking. Appropriate `fill-prefix' is computed with
  20335. ;; `org-adaptive-fill-function'.
  20336. ;; `org-auto-fill-function' takes care of auto-filling. It calls
  20337. ;; `do-auto-fill' only on valid areas with `fill-prefix' shadowed with
  20338. ;; `org-adaptive-fill-function' value. Internally,
  20339. ;; `org-comment-line-break-function' breaks the line.
  20340. ;; `org-setup-filling' installs filling and auto-filling related
  20341. ;; variables during `org-mode' initialization.
  20342. (defun org-setup-filling ()
  20343. (interactive)
  20344. ;; Prevent auto-fill from inserting unwanted new items.
  20345. (when (boundp 'fill-nobreak-predicate)
  20346. (org-set-local
  20347. 'fill-nobreak-predicate
  20348. (org-uniquify
  20349. (append fill-nobreak-predicate
  20350. '(org-fill-paragraph-separate-nobreak-p
  20351. org-fill-line-break-nobreak-p
  20352. org-fill-paragraph-with-timestamp-nobreak-p)))))
  20353. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  20354. (org-set-local 'auto-fill-inhibit-regexp nil)
  20355. (org-set-local 'adaptive-fill-function 'org-adaptive-fill-function)
  20356. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  20357. (org-set-local 'comment-line-break-function 'org-comment-line-break-function))
  20358. (defvar org-element-paragraph-separate) ; org-element.el
  20359. (defun org-fill-paragraph-separate-nobreak-p ()
  20360. "Non-nil when a new line at point would end current paragraph."
  20361. (looking-at (substring org-element-paragraph-separate 1)))
  20362. (defun org-fill-line-break-nobreak-p ()
  20363. "Non-nil when a new line at point would create an Org line break."
  20364. (save-excursion
  20365. (skip-chars-backward "[ \t]")
  20366. (skip-chars-backward "\\\\")
  20367. (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
  20368. (defun org-fill-paragraph-with-timestamp-nobreak-p ()
  20369. "Non-nil when a new line at point would split a timestamp."
  20370. (and (org-at-timestamp-p t)
  20371. (not (looking-at org-ts-regexp-both))))
  20372. (declare-function message-in-body-p "message" ())
  20373. (defvar orgtbl-line-start-regexp) ; From org-table.el
  20374. (defun org-adaptive-fill-function ()
  20375. "Compute a fill prefix for the current line.
  20376. Return fill prefix, as a string, or nil if current line isn't
  20377. meant to be filled."
  20378. (let (prefix)
  20379. (catch 'exit
  20380. (when (derived-mode-p 'message-mode)
  20381. (save-excursion
  20382. (beginning-of-line)
  20383. (cond ((or (not (message-in-body-p))
  20384. (looking-at orgtbl-line-start-regexp))
  20385. (throw 'exit nil))
  20386. ((looking-at message-cite-prefix-regexp)
  20387. (throw 'exit (match-string-no-properties 0)))
  20388. ((looking-at org-outline-regexp)
  20389. (throw 'exit (make-string (length (match-string 0)) ? ))))))
  20390. (org-with-wide-buffer
  20391. (let* ((p (line-beginning-position))
  20392. (element (save-excursion
  20393. (beginning-of-line)
  20394. (or (ignore-errors (org-element-at-point))
  20395. (user-error "An element cannot be parsed line %d"
  20396. (line-number-at-pos (point))))))
  20397. (type (org-element-type element))
  20398. (post-affiliated (org-element-property :post-affiliated element)))
  20399. (unless (and post-affiliated (< p post-affiliated))
  20400. (case type
  20401. (comment (looking-at "[ \t]*# ?") (match-string 0))
  20402. (footnote-definition "")
  20403. ((item plain-list)
  20404. (make-string (org-list-item-body-column
  20405. (or post-affiliated
  20406. (org-element-property :begin element)))
  20407. ? ))
  20408. (paragraph
  20409. ;; Fill prefix is usually the same as the current line,
  20410. ;; except if the paragraph is at the beginning of an item.
  20411. (let ((parent (org-element-property :parent element)))
  20412. (cond ((eq (org-element-type parent) 'item)
  20413. (make-string (org-list-item-body-column
  20414. (org-element-property :begin parent))
  20415. ? ))
  20416. ((save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  20417. (match-string 0))
  20418. (t ""))))
  20419. (comment-block
  20420. ;; Only fill contents if P is within block boundaries.
  20421. (let* ((cbeg (save-excursion (goto-char post-affiliated)
  20422. (forward-line)
  20423. (point)))
  20424. (cend (save-excursion
  20425. (goto-char (org-element-property :end element))
  20426. (skip-chars-backward " \r\t\n")
  20427. (line-beginning-position))))
  20428. (when (and (>= p cbeg) (< p cend))
  20429. (if (save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  20430. (match-string 0)
  20431. "")))))))))))
  20432. (declare-function message-goto-body "message" ())
  20433. (defvar message-cite-prefix-regexp) ; From message.el
  20434. (defun org-fill-paragraph (&optional justify)
  20435. "Fill element at point, when applicable.
  20436. This function only applies to comment blocks, comments, example
  20437. blocks and paragraphs. Also, as a special case, re-align table
  20438. when point is at one.
  20439. If JUSTIFY is non-nil (interactively, with prefix argument),
  20440. justify as well. If `sentence-end-double-space' is non-nil, then
  20441. period followed by one space does not end a sentence, so don't
  20442. break a line there. The variable `fill-column' controls the
  20443. width for filling.
  20444. For convenience, when point is at a plain list, an item or
  20445. a footnote definition, try to fill the first paragraph within."
  20446. (interactive)
  20447. (if (and (derived-mode-p 'message-mode)
  20448. (or (not (message-in-body-p))
  20449. (save-excursion (move-beginning-of-line 1)
  20450. (looking-at message-cite-prefix-regexp))))
  20451. ;; First ensure filling is correct in message-mode.
  20452. (let ((fill-paragraph-function
  20453. (cadadr (assoc 'fill-paragraph-function org-fb-vars)))
  20454. (fill-prefix (cadadr (assoc 'fill-prefix org-fb-vars)))
  20455. (paragraph-start (cadadr (assoc 'paragraph-start org-fb-vars)))
  20456. (paragraph-separate
  20457. (cadadr (assoc 'paragraph-separate org-fb-vars))))
  20458. (fill-paragraph nil))
  20459. (with-syntax-table org-mode-transpose-word-syntax-table
  20460. ;; Move to end of line in order to get the first paragraph
  20461. ;; within a plain list or a footnote definition.
  20462. (let ((element (save-excursion
  20463. (end-of-line)
  20464. (or (ignore-errors (org-element-at-point))
  20465. (user-error "An element cannot be parsed line %d"
  20466. (line-number-at-pos (point)))))))
  20467. ;; First check if point is in a blank line at the beginning of
  20468. ;; the buffer. In that case, ignore filling.
  20469. (case (org-element-type element)
  20470. ;; Use major mode filling function is src blocks.
  20471. (src-block (org-babel-do-key-sequence-in-edit-buffer (kbd "M-q")))
  20472. ;; Align Org tables, leave table.el tables as-is.
  20473. (table-row (org-table-align) t)
  20474. (table
  20475. (when (eq (org-element-property :type element) 'org)
  20476. (save-excursion
  20477. (goto-char (org-element-property :post-affiliated element))
  20478. (org-table-align)))
  20479. t)
  20480. (paragraph
  20481. ;; Paragraphs may contain `line-break' type objects.
  20482. (let ((beg (max (point-min)
  20483. (org-element-property :contents-begin element)))
  20484. (end (min (point-max)
  20485. (org-element-property :contents-end element))))
  20486. ;; Do nothing if point is at an affiliated keyword.
  20487. (if (< (line-end-position) beg) t
  20488. (when (derived-mode-p 'message-mode)
  20489. ;; In `message-mode', do not fill following citation
  20490. ;; in current paragraph nor text before message body.
  20491. (let ((body-start (save-excursion (message-goto-body))))
  20492. (when body-start (setq beg (max body-start beg))))
  20493. (when (save-excursion
  20494. (re-search-forward
  20495. (concat "^" message-cite-prefix-regexp) end t))
  20496. (setq end (match-beginning 0))))
  20497. ;; Fill paragraph, taking line breaks into account.
  20498. ;; For that, slice the paragraph using line breaks as
  20499. ;; separators, and fill the parts in reverse order to
  20500. ;; avoid messing with markers.
  20501. (save-excursion
  20502. (goto-char end)
  20503. (mapc
  20504. (lambda (pos)
  20505. (fill-region-as-paragraph pos (point) justify)
  20506. (goto-char pos))
  20507. ;; Find the list of ending positions for line breaks
  20508. ;; in the current paragraph. Add paragraph
  20509. ;; beginning to include first slice.
  20510. (nreverse
  20511. (cons beg
  20512. (org-element-map
  20513. (org-element--parse-objects
  20514. beg end nil (org-element-restriction 'paragraph))
  20515. 'line-break
  20516. (lambda (lb) (org-element-property :end lb)))))))
  20517. t)))
  20518. ;; Contents of `comment-block' type elements should be
  20519. ;; filled as plain text, but only if point is within block
  20520. ;; markers.
  20521. (comment-block
  20522. (let* ((case-fold-search t)
  20523. (beg (save-excursion
  20524. (goto-char (org-element-property :begin element))
  20525. (re-search-forward "^[ \t]*#\\+begin_comment" nil t)
  20526. (forward-line)
  20527. (point)))
  20528. (end (save-excursion
  20529. (goto-char (org-element-property :end element))
  20530. (re-search-backward "^[ \t]*#\\+end_comment" nil t)
  20531. (line-beginning-position))))
  20532. (when (and (>= (point) beg) (< (point) end))
  20533. (fill-region-as-paragraph
  20534. (save-excursion
  20535. (end-of-line)
  20536. (re-search-backward "^[ \t]*$" beg 'move)
  20537. (line-beginning-position))
  20538. (save-excursion
  20539. (beginning-of-line)
  20540. (re-search-forward "^[ \t]*$" end 'move)
  20541. (line-beginning-position))
  20542. justify)))
  20543. t)
  20544. ;; Fill comments.
  20545. (comment (fill-comment-paragraph justify))
  20546. ;; Ignore every other element.
  20547. (otherwise t))))))
  20548. (defun org-auto-fill-function ()
  20549. "Auto-fill function."
  20550. ;; Check if auto-filling is meaningful.
  20551. (let ((fc (current-fill-column)))
  20552. (when (and fc (> (current-column) fc))
  20553. (let* ((fill-prefix (org-adaptive-fill-function))
  20554. ;; Enforce empty fill prefix, if required. Otherwise, it
  20555. ;; will be computed again.
  20556. (adaptive-fill-mode (not (equal fill-prefix ""))))
  20557. (when fill-prefix (do-auto-fill))))))
  20558. (defun org-comment-line-break-function (&optional soft)
  20559. "Break line at point and indent, continuing comment if within one.
  20560. The inserted newline is marked hard if variable
  20561. `use-hard-newlines' is true, unless optional argument SOFT is
  20562. non-nil."
  20563. (if soft (insert-and-inherit ?\n) (newline 1))
  20564. (save-excursion (forward-char -1) (delete-horizontal-space))
  20565. (delete-horizontal-space)
  20566. (indent-to-left-margin)
  20567. (insert-before-markers-and-inherit fill-prefix))
  20568. ;;; Comments
  20569. ;; Org comments syntax is quite complex. It requires the entire line
  20570. ;; to be just a comment. Also, even with the right syntax at the
  20571. ;; beginning of line, some some elements (i.e. verse-block or
  20572. ;; example-block) don't accept comments. Usual Emacs comment commands
  20573. ;; cannot cope with those requirements. Therefore, Org replaces them.
  20574. ;; Org still relies on `comment-dwim', but cannot trust
  20575. ;; `comment-only-p'. So, `comment-region-function' and
  20576. ;; `uncomment-region-function' both point
  20577. ;; to`org-comment-or-uncomment-region'. Eventually,
  20578. ;; `org-insert-comment' takes care of insertion of comments at the
  20579. ;; beginning of line.
  20580. ;; `org-setup-comments-handling' install comments related variables
  20581. ;; during `org-mode' initialization.
  20582. (defun org-setup-comments-handling ()
  20583. (interactive)
  20584. (org-set-local 'comment-use-syntax nil)
  20585. (org-set-local 'comment-start "# ")
  20586. (org-set-local 'comment-start-skip "^\\s-*#\\(?: \\|$\\)")
  20587. (org-set-local 'comment-insert-comment-function 'org-insert-comment)
  20588. (org-set-local 'comment-region-function 'org-comment-or-uncomment-region)
  20589. (org-set-local 'uncomment-region-function 'org-comment-or-uncomment-region))
  20590. (defun org-insert-comment ()
  20591. "Insert an empty comment above current line.
  20592. If the line is empty, insert comment at its beginning."
  20593. (beginning-of-line)
  20594. (if (looking-at "\\s-*$") (replace-match "") (open-line 1))
  20595. (org-indent-line)
  20596. (insert "# "))
  20597. (defvar comment-empty-lines) ; From newcomment.el.
  20598. (defun org-comment-or-uncomment-region (beg end &rest ignore)
  20599. "Comment or uncomment each non-blank line in the region.
  20600. Uncomment each non-blank line between BEG and END if it only
  20601. contains commented lines. Otherwise, comment them."
  20602. (save-restriction
  20603. ;; Restrict region
  20604. (narrow-to-region (save-excursion (goto-char beg)
  20605. (skip-chars-forward " \r\t\n" end)
  20606. (line-beginning-position))
  20607. (save-excursion (goto-char end)
  20608. (skip-chars-backward " \r\t\n" beg)
  20609. (line-end-position)))
  20610. (let ((uncommentp
  20611. ;; UNCOMMENTP is non-nil when every non blank line between
  20612. ;; BEG and END is a comment.
  20613. (save-excursion
  20614. (goto-char (point-min))
  20615. (while (and (not (eobp))
  20616. (let ((element (org-element-at-point)))
  20617. (and (eq (org-element-type element) 'comment)
  20618. (goto-char (min (point-max)
  20619. (org-element-property
  20620. :end element)))))))
  20621. (eobp))))
  20622. (if uncommentp
  20623. ;; Only blank lines and comments in region: uncomment it.
  20624. (save-excursion
  20625. (goto-char (point-min))
  20626. (while (not (eobp))
  20627. (when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
  20628. (replace-match "" nil nil nil 1))
  20629. (forward-line)))
  20630. ;; Comment each line in region.
  20631. (let ((min-indent (point-max)))
  20632. ;; First find the minimum indentation across all lines.
  20633. (save-excursion
  20634. (goto-char (point-min))
  20635. (while (and (not (eobp)) (not (zerop min-indent)))
  20636. (unless (looking-at "[ \t]*$")
  20637. (setq min-indent (min min-indent (current-indentation))))
  20638. (forward-line)))
  20639. ;; Then loop over all lines.
  20640. (save-excursion
  20641. (goto-char (point-min))
  20642. (while (not (eobp))
  20643. (unless (and (not comment-empty-lines) (looking-at "[ \t]*$"))
  20644. ;; Don't get fooled by invisible text (e.g. link path)
  20645. ;; when moving to column MIN-INDENT.
  20646. (let ((buffer-invisibility-spec nil))
  20647. (org-move-to-column min-indent t))
  20648. (insert comment-start))
  20649. (forward-line))))))))
  20650. ;;; Planning
  20651. ;; This section contains tools to operate on timestamp objects, as
  20652. ;; returned by, e.g. `org-element-context'.
  20653. (defun org-timestamp-has-time-p (timestamp)
  20654. "Non-nil when TIMESTAMP has a time specified."
  20655. (org-element-property :hour-start timestamp))
  20656. (defun org-timestamp-format (timestamp format &optional end utc)
  20657. "Format a TIMESTAMP element into a string.
  20658. FORMAT is a format specifier to be passed to
  20659. `format-time-string'.
  20660. When optional argument END is non-nil, use end of date-range or
  20661. time-range, if possible.
  20662. When optional argument UTC is non-nil, time will be expressed as
  20663. Universal Time."
  20664. (format-time-string
  20665. format
  20666. (apply 'encode-time
  20667. (cons 0
  20668. (mapcar
  20669. (lambda (prop) (or (org-element-property prop timestamp) 0))
  20670. (if end '(:minute-end :hour-end :day-end :month-end :year-end)
  20671. '(:minute-start :hour-start :day-start :month-start
  20672. :year-start)))))
  20673. utc))
  20674. (defun org-timestamp-split-range (timestamp &optional end)
  20675. "Extract a timestamp object from a date or time range.
  20676. TIMESTAMP is a timestamp object. END, when non-nil, means extract
  20677. the end of the range. Otherwise, extract its start.
  20678. Return a new timestamp object sharing the same parent as
  20679. TIMESTAMP."
  20680. (let ((type (org-element-property :type timestamp)))
  20681. (if (memq type '(active inactive diary)) timestamp
  20682. (let ((split-ts (list 'timestamp (copy-sequence (nth 1 timestamp)))))
  20683. ;; Set new type.
  20684. (org-element-put-property
  20685. split-ts :type (if (eq type 'active-range) 'active 'inactive))
  20686. ;; Copy start properties over end properties if END is
  20687. ;; non-nil. Otherwise, copy end properties over `start' ones.
  20688. (let ((p-alist '((:minute-start . :minute-end)
  20689. (:hour-start . :hour-end)
  20690. (:day-start . :day-end)
  20691. (:month-start . :month-end)
  20692. (:year-start . :year-end))))
  20693. (dolist (p-cell p-alist)
  20694. (org-element-put-property
  20695. split-ts
  20696. (funcall (if end 'car 'cdr) p-cell)
  20697. (org-element-property
  20698. (funcall (if end 'cdr 'car) p-cell) split-ts)))
  20699. ;; Eventually refresh `:raw-value'.
  20700. (org-element-put-property split-ts :raw-value nil)
  20701. (org-element-put-property
  20702. split-ts :raw-value (org-element-interpret-data split-ts)))))))
  20703. (defun org-timestamp-translate (timestamp &optional boundary)
  20704. "Apply `org-translate-time' on a TIMESTAMP object.
  20705. When optional argument BOUNDARY is non-nil, it is either the
  20706. symbol `start' or `end'. In this case, only translate the
  20707. starting or ending part of TIMESTAMP if it is a date or time
  20708. range. Otherwise, translate both parts."
  20709. (if (and (not boundary)
  20710. (memq (org-element-property :type timestamp)
  20711. '(active-range inactive-range)))
  20712. (concat
  20713. (org-translate-time
  20714. (org-element-property :raw-value
  20715. (org-timestamp-split-range timestamp)))
  20716. "--"
  20717. (org-translate-time
  20718. (org-element-property :raw-value
  20719. (org-timestamp-split-range timestamp t))))
  20720. (org-translate-time
  20721. (org-element-property
  20722. :raw-value
  20723. (if (not boundary) timestamp
  20724. (org-timestamp-split-range timestamp (eq boundary 'end)))))))
  20725. ;;; Other stuff.
  20726. (defun org-toggle-fixed-width-section (arg)
  20727. "Toggle the fixed-width export.
  20728. If there is no active region, the QUOTE keyword at the current headline is
  20729. inserted or removed. When present, it causes the text between this headline
  20730. and the next to be exported as fixed-width text, and unmodified.
  20731. If there is an active region, this command adds or removes a colon as the
  20732. first character of this line. If the first character of a line is a colon,
  20733. this line is also exported in fixed-width font."
  20734. (interactive "P")
  20735. (let* ((cc 0)
  20736. (regionp (org-region-active-p))
  20737. (beg (if regionp (region-beginning) (point)))
  20738. (end (if regionp (region-end)))
  20739. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  20740. (case-fold-search nil)
  20741. (re "[ \t]*\\(:\\(?: \\|$\\)\\)")
  20742. off)
  20743. (if regionp
  20744. (save-excursion
  20745. (goto-char beg)
  20746. (setq cc (current-column))
  20747. (beginning-of-line 1)
  20748. (setq off (looking-at re))
  20749. (while (> nlines 0)
  20750. (setq nlines (1- nlines))
  20751. (beginning-of-line 1)
  20752. (cond
  20753. (arg
  20754. (org-move-to-column cc t)
  20755. (insert ": \n")
  20756. (forward-line -1))
  20757. ((and off (looking-at re))
  20758. (replace-match "" t t nil 1))
  20759. ((not off) (org-move-to-column cc t) (insert ": ")))
  20760. (forward-line 1)))
  20761. (save-excursion
  20762. (org-back-to-heading)
  20763. (cond
  20764. ((looking-at (format org-heading-keyword-regexp-format
  20765. org-quote-string))
  20766. (goto-char (match-end 1))
  20767. (looking-at (concat " +" org-quote-string))
  20768. (replace-match "" t t)
  20769. (when (eolp) (insert " ")))
  20770. ((looking-at org-outline-regexp)
  20771. (goto-char (match-end 0))
  20772. (insert org-quote-string " ")))))))
  20773. (defun org-reftex-citation ()
  20774. "Use reftex-citation to insert a citation into the buffer.
  20775. This looks for a line like
  20776. #+BIBLIOGRAPHY: foo plain option:-d
  20777. and derives from it that foo.bib is the bibliography file relevant
  20778. for this document. It then installs the necessary environment for RefTeX
  20779. to work in this buffer and calls `reftex-citation' to insert a citation
  20780. into the buffer.
  20781. Export of such citations to both LaTeX and HTML is handled by the contributed
  20782. package ox-bibtex by Taru Karttunen."
  20783. (interactive)
  20784. (let ((reftex-docstruct-symbol 'rds)
  20785. (reftex-cite-format "\\cite{%l}")
  20786. rds bib)
  20787. (save-excursion
  20788. (save-restriction
  20789. (widen)
  20790. (let ((case-fold-search t)
  20791. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  20792. (if (not (save-excursion
  20793. (or (re-search-forward re nil t)
  20794. (re-search-backward re nil t))))
  20795. (error "No bibliography defined in file")
  20796. (setq bib (concat (match-string 1) ".bib")
  20797. rds (list (list 'bib bib)))))))
  20798. (call-interactively 'reftex-citation)))
  20799. ;;;; Functions extending outline functionality
  20800. (defun org-beginning-of-line (&optional arg)
  20801. "Go to the beginning of the current line. If that is invisible, continue
  20802. to a visible line beginning. This makes the function of C-a more intuitive.
  20803. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  20804. first attempt, and only move to after the tags when the cursor is already
  20805. beyond the end of the headline."
  20806. (interactive "P")
  20807. (let ((pos (point))
  20808. (special (if (consp org-special-ctrl-a/e)
  20809. (car org-special-ctrl-a/e)
  20810. org-special-ctrl-a/e))
  20811. refpos)
  20812. (if (org-bound-and-true-p visual-line-mode)
  20813. (beginning-of-visual-line 1)
  20814. (beginning-of-line 1))
  20815. (if (and arg (fboundp 'move-beginning-of-line))
  20816. (call-interactively 'move-beginning-of-line)
  20817. (if (bobp)
  20818. nil
  20819. (backward-char 1)
  20820. (if (org-truely-invisible-p)
  20821. (while (and (not (bobp)) (org-truely-invisible-p))
  20822. (backward-char 1)
  20823. (beginning-of-line 1))
  20824. (forward-char 1))))
  20825. (when special
  20826. (cond
  20827. ((and (looking-at org-complex-heading-regexp)
  20828. (= (char-after (match-end 1)) ?\ ))
  20829. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  20830. (point-at-eol)))
  20831. (goto-char
  20832. (if (eq special t)
  20833. (cond ((> pos refpos) refpos)
  20834. ((= pos (point)) refpos)
  20835. (t (point)))
  20836. (cond ((> pos (point)) (point))
  20837. ((not (eq last-command this-command)) (point))
  20838. (t refpos)))))
  20839. ((org-at-item-p)
  20840. ;; Being at an item and not looking at an the item means point
  20841. ;; was previously moved to beginning of a visual line, which
  20842. ;; doesn't contain the item. Therefore, do nothing special,
  20843. ;; just stay here.
  20844. (when (looking-at org-list-full-item-re)
  20845. ;; Set special position at first white space character after
  20846. ;; bullet, and check-box, if any.
  20847. (let ((after-bullet
  20848. (let ((box (match-end 3)))
  20849. (if (not box) (match-end 1)
  20850. (let ((after (char-after box)))
  20851. (if (and after (= after ? )) (1+ box) box))))))
  20852. ;; Special case: Move point to special position when
  20853. ;; currently after it or at beginning of line.
  20854. (if (eq special t)
  20855. (when (or (> pos after-bullet) (= (point) pos))
  20856. (goto-char after-bullet))
  20857. ;; Reversed case: Move point to special position when
  20858. ;; point was already at beginning of line and command is
  20859. ;; repeated.
  20860. (when (and (= (point) pos) (eq last-command this-command))
  20861. (goto-char after-bullet))))))))
  20862. (org-no-warnings
  20863. (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  20864. (setq disable-point-adjustment
  20865. (or (not (invisible-p (point)))
  20866. (not (invisible-p (max (point-min) (1- (point))))))))
  20867. (defun org-end-of-line (&optional arg)
  20868. "Go to the end of the line.
  20869. If this is a headline, and `org-special-ctrl-a/e' is set, ignore
  20870. tags on the first attempt, and only move to after the tags when
  20871. the cursor is already beyond the end of the headline."
  20872. (interactive "P")
  20873. (let ((special (if (consp org-special-ctrl-a/e) (cdr org-special-ctrl-a/e)
  20874. org-special-ctrl-a/e))
  20875. (move-fun (cond ((org-bound-and-true-p visual-line-mode)
  20876. 'end-of-visual-line)
  20877. ((fboundp 'move-end-of-line) 'move-end-of-line)
  20878. (t 'end-of-line))))
  20879. (if (or (not special) arg) (call-interactively move-fun)
  20880. (let* ((element (save-excursion (beginning-of-line)
  20881. (org-element-at-point)))
  20882. (type (org-element-type element)))
  20883. (cond
  20884. ((memq type '(headline inlinetask))
  20885. (let ((pos (point)))
  20886. (beginning-of-line 1)
  20887. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  20888. (if (eq special t)
  20889. (if (or (< pos (match-beginning 1)) (= pos (match-end 0)))
  20890. (goto-char (match-beginning 1))
  20891. (goto-char (match-end 0)))
  20892. (if (or (< pos (match-end 0))
  20893. (not (eq this-command last-command)))
  20894. (goto-char (match-end 0))
  20895. (goto-char (match-beginning 1))))
  20896. (call-interactively move-fun))))
  20897. ((org-element-property :hiddenp element)
  20898. ;; If element is hidden, `move-end-of-line' would put point
  20899. ;; after it. Use `end-of-line' to stay on current line.
  20900. (call-interactively 'end-of-line))
  20901. (t (call-interactively move-fun)))))
  20902. (org-no-warnings (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  20903. (setq disable-point-adjustment
  20904. (or (not (invisible-p (point)))
  20905. (not (invisible-p (max (point-min) (1- (point))))))))
  20906. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  20907. (define-key org-mode-map "\C-e" 'org-end-of-line)
  20908. (defun org-backward-sentence (&optional arg)
  20909. "Go to beginning of sentence, or beginning of table field.
  20910. This will call `backward-sentence' or `org-table-beginning-of-field',
  20911. depending on context."
  20912. (interactive "P")
  20913. (cond
  20914. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  20915. (t (call-interactively 'backward-sentence))))
  20916. (defun org-forward-sentence (&optional arg)
  20917. "Go to end of sentence, or end of table field.
  20918. This will call `forward-sentence' or `org-table-end-of-field',
  20919. depending on context."
  20920. (interactive "P")
  20921. (cond
  20922. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  20923. (t (call-interactively 'forward-sentence))))
  20924. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  20925. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  20926. (defun org-kill-line (&optional arg)
  20927. "Kill line, to tags or end of line."
  20928. (interactive "P")
  20929. (cond
  20930. ((or (not org-special-ctrl-k)
  20931. (bolp)
  20932. (not (org-at-heading-p)))
  20933. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  20934. org-ctrl-k-protect-subtree)
  20935. (if (or (eq org-ctrl-k-protect-subtree 'error)
  20936. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  20937. (user-error "C-k aborted as it would kill a hidden subtree")))
  20938. (call-interactively
  20939. (if (org-bound-and-true-p visual-line-mode) 'kill-visual-line 'kill-line)))
  20940. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  20941. (kill-region (point) (match-beginning 1))
  20942. (org-set-tags nil t))
  20943. (t (kill-region (point) (point-at-eol)))))
  20944. (define-key org-mode-map "\C-k" 'org-kill-line)
  20945. (defun org-yank (&optional arg)
  20946. "Yank. If the kill is a subtree, treat it specially.
  20947. This command will look at the current kill and check if is a single
  20948. subtree, or a series of subtrees[1]. If it passes the test, and if the
  20949. cursor is at the beginning of a line or after the stars of a currently
  20950. empty headline, then the yank is handled specially. How exactly depends
  20951. on the value of the following variables, both set by default.
  20952. org-yank-folded-subtrees
  20953. When set, the subtree(s) will be folded after insertion, but only
  20954. if doing so would now swallow text after the yanked text.
  20955. org-yank-adjusted-subtrees
  20956. When set, the subtree will be promoted or demoted in order to
  20957. fit into the local outline tree structure, which means that the level
  20958. will be adjusted so that it becomes the smaller one of the two
  20959. *visible* surrounding headings.
  20960. Any prefix to this command will cause `yank' to be called directly with
  20961. no special treatment. In particular, a simple \\[universal-argument] prefix \
  20962. will just
  20963. plainly yank the text as it is.
  20964. \[1] The test checks if the first non-white line is a heading
  20965. and if there are no other headings with fewer stars."
  20966. (interactive "P")
  20967. (org-yank-generic 'yank arg))
  20968. (defun org-yank-generic (command arg)
  20969. "Perform some yank-like command.
  20970. This function implements the behavior described in the `org-yank'
  20971. documentation. However, it has been generalized to work for any
  20972. interactive command with similar behavior."
  20973. ;; pretend to be command COMMAND
  20974. (setq this-command command)
  20975. (if arg
  20976. (call-interactively command)
  20977. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  20978. (and (org-kill-is-subtree-p)
  20979. (or (bolp)
  20980. (and (looking-at "[ \t]*$")
  20981. (string-match
  20982. "\\`\\*+\\'"
  20983. (buffer-substring (point-at-bol) (point)))))))
  20984. swallowp)
  20985. (cond
  20986. ((and subtreep org-yank-folded-subtrees)
  20987. (let ((beg (point))
  20988. end)
  20989. (if (and subtreep org-yank-adjusted-subtrees)
  20990. (org-paste-subtree nil nil 'for-yank)
  20991. (call-interactively command))
  20992. (setq end (point))
  20993. (goto-char beg)
  20994. (when (and (bolp) subtreep
  20995. (not (setq swallowp
  20996. (org-yank-folding-would-swallow-text beg end))))
  20997. (org-with-limited-levels
  20998. (or (looking-at org-outline-regexp)
  20999. (re-search-forward org-outline-regexp-bol end t))
  21000. (while (and (< (point) end) (looking-at org-outline-regexp))
  21001. (hide-subtree)
  21002. (org-cycle-show-empty-lines 'folded)
  21003. (condition-case nil
  21004. (outline-forward-same-level 1)
  21005. (error (goto-char end))))))
  21006. (when swallowp
  21007. (message
  21008. "Inserted text not folded because that would swallow text"))
  21009. (goto-char end)
  21010. (skip-chars-forward " \t\n\r")
  21011. (beginning-of-line 1)
  21012. (push-mark beg 'nomsg)))
  21013. ((and subtreep org-yank-adjusted-subtrees)
  21014. (let ((beg (point-at-bol)))
  21015. (org-paste-subtree nil nil 'for-yank)
  21016. (push-mark beg 'nomsg)))
  21017. (t
  21018. (call-interactively command))))))
  21019. (defun org-yank-folding-would-swallow-text (beg end)
  21020. "Would hide-subtree at BEG swallow any text after END?"
  21021. (let (level)
  21022. (org-with-limited-levels
  21023. (save-excursion
  21024. (goto-char beg)
  21025. (when (or (looking-at org-outline-regexp)
  21026. (re-search-forward org-outline-regexp-bol end t))
  21027. (setq level (org-outline-level)))
  21028. (goto-char end)
  21029. (skip-chars-forward " \t\r\n\v\f")
  21030. (if (or (eobp)
  21031. (and (bolp) (looking-at org-outline-regexp)
  21032. (<= (org-outline-level) level)))
  21033. nil ; Nothing would be swallowed
  21034. t))))) ; something would swallow
  21035. (define-key org-mode-map "\C-y" 'org-yank)
  21036. (defun org-truely-invisible-p ()
  21037. "Check if point is at a character currently not visible.
  21038. This version does not only check the character property, but also
  21039. `visible-mode'."
  21040. ;; Early versions of noutline don't have `outline-invisible-p'.
  21041. (if (org-bound-and-true-p visible-mode)
  21042. nil
  21043. (outline-invisible-p)))
  21044. (defun org-invisible-p2 ()
  21045. "Check if point is at a character currently not visible."
  21046. (save-excursion
  21047. (if (and (eolp) (not (bobp))) (backward-char 1))
  21048. ;; Early versions of noutline don't have `outline-invisible-p'.
  21049. (outline-invisible-p)))
  21050. (defun org-back-to-heading (&optional invisible-ok)
  21051. "Call `outline-back-to-heading', but provide a better error message."
  21052. (condition-case nil
  21053. (outline-back-to-heading invisible-ok)
  21054. (error (error "Before first headline at position %d in buffer %s"
  21055. (point) (current-buffer)))))
  21056. (defun org-before-first-heading-p ()
  21057. "Before first heading?"
  21058. (save-excursion
  21059. (end-of-line)
  21060. (null (re-search-backward org-outline-regexp-bol nil t))))
  21061. (defun org-at-heading-p (&optional ignored)
  21062. (outline-on-heading-p t))
  21063. ;; Compatibility alias with Org versions < 7.8.03
  21064. (defalias 'org-on-heading-p 'org-at-heading-p)
  21065. (defun org-at-comment-p nil
  21066. "Is cursor in a line starting with a # character?"
  21067. (save-excursion
  21068. (beginning-of-line)
  21069. (looking-at "^#")))
  21070. (defun org-at-drawer-p nil
  21071. "Is cursor at a drawer keyword?"
  21072. (save-excursion
  21073. (move-beginning-of-line 1)
  21074. (looking-at org-drawer-regexp)))
  21075. (defun org-at-block-p nil
  21076. "Is cursor at a block keyword?"
  21077. (save-excursion
  21078. (move-beginning-of-line 1)
  21079. (looking-at org-block-regexp)))
  21080. (defun org-point-at-end-of-empty-headline ()
  21081. "If point is at the end of an empty headline, return t, else nil.
  21082. If the heading only contains a TODO keyword, it is still still considered
  21083. empty."
  21084. (and (looking-at "[ \t]*$")
  21085. (when org-todo-line-regexp
  21086. (save-excursion
  21087. (beginning-of-line 1)
  21088. (let ((case-fold-search nil))
  21089. (looking-at org-todo-line-regexp)
  21090. (string= (match-string 3) ""))))))
  21091. (defun org-at-heading-or-item-p ()
  21092. (or (org-at-heading-p) (org-at-item-p)))
  21093. (defun org-at-target-p ()
  21094. (or (org-in-regexp org-radio-target-regexp)
  21095. (org-in-regexp org-target-regexp)))
  21096. ;; Compatibility alias with Org versions < 7.8.03
  21097. (defalias 'org-on-target-p 'org-at-target-p)
  21098. (defun org-up-heading-all (arg)
  21099. "Move to the heading line of which the present line is a subheading.
  21100. This function considers both visible and invisible heading lines.
  21101. With argument, move up ARG levels."
  21102. (if (fboundp 'outline-up-heading-all)
  21103. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  21104. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  21105. (defun org-up-heading-safe ()
  21106. "Move to the heading line of which the present line is a subheading.
  21107. This version will not throw an error. It will return the level of the
  21108. headline found, or nil if no higher level is found.
  21109. Also, this function will be a lot faster than `outline-up-heading',
  21110. because it relies on stars being the outline starters. This can really
  21111. make a significant difference in outlines with very many siblings."
  21112. (let (start-level re)
  21113. (org-back-to-heading t)
  21114. (setq start-level (funcall outline-level))
  21115. (if (equal start-level 1)
  21116. nil
  21117. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  21118. (if (re-search-backward re nil t)
  21119. (funcall outline-level)))))
  21120. (defun org-first-sibling-p ()
  21121. "Is this heading the first child of its parents?"
  21122. (interactive)
  21123. (let ((re org-outline-regexp-bol)
  21124. level l)
  21125. (unless (org-at-heading-p t)
  21126. (user-error "Not at a heading"))
  21127. (setq level (funcall outline-level))
  21128. (save-excursion
  21129. (if (not (re-search-backward re nil t))
  21130. t
  21131. (setq l (funcall outline-level))
  21132. (< l level)))))
  21133. (defun org-goto-sibling (&optional previous)
  21134. "Goto the next sibling, even if it is invisible.
  21135. When PREVIOUS is set, go to the previous sibling instead. Returns t
  21136. when a sibling was found. When none is found, return nil and don't
  21137. move point."
  21138. (let ((fun (if previous 're-search-backward 're-search-forward))
  21139. (pos (point))
  21140. (re org-outline-regexp-bol)
  21141. level l)
  21142. (when (condition-case nil (org-back-to-heading t) (error nil))
  21143. (setq level (funcall outline-level))
  21144. (catch 'exit
  21145. (or previous (forward-char 1))
  21146. (while (funcall fun re nil t)
  21147. (setq l (funcall outline-level))
  21148. (when (< l level) (goto-char pos) (throw 'exit nil))
  21149. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  21150. (goto-char pos)
  21151. nil))))
  21152. (defun org-show-siblings ()
  21153. "Show all siblings of the current headline."
  21154. (save-excursion
  21155. (while (org-goto-sibling) (org-flag-heading nil)))
  21156. (save-excursion
  21157. (while (org-goto-sibling 'previous)
  21158. (org-flag-heading nil))))
  21159. (defun org-goto-first-child ()
  21160. "Goto the first child, even if it is invisible.
  21161. Return t when a child was found. Otherwise don't move point and
  21162. return nil."
  21163. (let (level (pos (point)) (re org-outline-regexp-bol))
  21164. (when (condition-case nil (org-back-to-heading t) (error nil))
  21165. (setq level (outline-level))
  21166. (forward-char 1)
  21167. (if (and (re-search-forward re nil t) (> (outline-level) level))
  21168. (progn (goto-char (match-beginning 0)) t)
  21169. (goto-char pos) nil))))
  21170. (defun org-show-hidden-entry ()
  21171. "Show an entry where even the heading is hidden."
  21172. (save-excursion
  21173. (org-show-entry)))
  21174. (defun org-flag-heading (flag &optional entry)
  21175. "Flag the current heading. FLAG non-nil means make invisible.
  21176. When ENTRY is non-nil, show the entire entry."
  21177. (save-excursion
  21178. (org-back-to-heading t)
  21179. ;; Check if we should show the entire entry
  21180. (if entry
  21181. (progn
  21182. (org-show-entry)
  21183. (save-excursion
  21184. (and (outline-next-heading)
  21185. (org-flag-heading nil))))
  21186. (outline-flag-region (max (point-min) (1- (point)))
  21187. (save-excursion (outline-end-of-heading) (point))
  21188. flag))))
  21189. (defun org-get-next-sibling ()
  21190. "Move to next heading of the same level, and return point.
  21191. If there is no such heading, return nil.
  21192. This is like outline-next-sibling, but invisible headings are ok."
  21193. (let ((level (funcall outline-level)))
  21194. (outline-next-heading)
  21195. (while (and (not (eobp)) (> (funcall outline-level) level))
  21196. (outline-next-heading))
  21197. (if (or (eobp) (< (funcall outline-level) level))
  21198. nil
  21199. (point))))
  21200. (defun org-get-last-sibling ()
  21201. "Move to previous heading of the same level, and return point.
  21202. If there is no such heading, return nil."
  21203. (let ((opoint (point))
  21204. (level (funcall outline-level)))
  21205. (outline-previous-heading)
  21206. (when (and (/= (point) opoint) (outline-on-heading-p t))
  21207. (while (and (> (funcall outline-level) level)
  21208. (not (bobp)))
  21209. (outline-previous-heading))
  21210. (if (< (funcall outline-level) level)
  21211. nil
  21212. (point)))))
  21213. (defun org-end-of-subtree (&optional invisible-ok to-heading)
  21214. "Goto to the end of a subtree."
  21215. ;; This contains an exact copy of the original function, but it uses
  21216. ;; `org-back-to-heading', to make it work also in invisible
  21217. ;; trees. And is uses an invisible-ok argument.
  21218. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  21219. ;; Furthermore, when used inside Org, finding the end of a large subtree
  21220. ;; with many children and grandchildren etc, this can be much faster
  21221. ;; than the outline version.
  21222. (org-back-to-heading invisible-ok)
  21223. (let ((first t)
  21224. (level (funcall outline-level)))
  21225. (if (and (derived-mode-p 'org-mode) (< level 1000))
  21226. ;; A true heading (not a plain list item), in Org-mode
  21227. ;; This means we can easily find the end by looking
  21228. ;; only for the right number of stars. Using a regexp to do
  21229. ;; this is so much faster than using a Lisp loop.
  21230. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  21231. (forward-char 1)
  21232. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  21233. ;; something else, do it the slow way
  21234. (while (and (not (eobp))
  21235. (or first (> (funcall outline-level) level)))
  21236. (setq first nil)
  21237. (outline-next-heading)))
  21238. (unless to-heading
  21239. (if (memq (preceding-char) '(?\n ?\^M))
  21240. (progn
  21241. ;; Go to end of line before heading
  21242. (forward-char -1)
  21243. (if (memq (preceding-char) '(?\n ?\^M))
  21244. ;; leave blank line before heading
  21245. (forward-char -1))))))
  21246. (point))
  21247. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  21248. "Use Org version in org-mode, for dramatic speed-up."
  21249. (if (derived-mode-p 'org-mode)
  21250. (progn
  21251. (org-end-of-subtree nil t)
  21252. (unless (eobp) (backward-char 1)))
  21253. ad-do-it))
  21254. (defun org-end-of-meta-data-and-drawers ()
  21255. "Jump to the first text after meta data and drawers in the current entry.
  21256. This will move over empty lines, lines with planning time stamps,
  21257. clocking lines, and drawers."
  21258. (org-back-to-heading t)
  21259. (let ((end (save-excursion (outline-next-heading) (point)))
  21260. (re (concat "\\(" org-drawer-regexp "\\)"
  21261. "\\|" "[ \t]*" org-keyword-time-regexp)))
  21262. (forward-line 1)
  21263. (while (re-search-forward re end t)
  21264. (if (not (match-end 1))
  21265. ;; empty or planning line
  21266. (forward-line 1)
  21267. ;; a drawer, find the end
  21268. (re-search-forward "^[ \t]*:END:" end 'move)
  21269. (forward-line 1)))
  21270. (and (re-search-forward "[^\n]" nil t) (backward-char 1))
  21271. (point)))
  21272. (defun org-forward-heading-same-level (arg &optional invisible-ok)
  21273. "Move forward to the ARG'th subheading at same level as this one.
  21274. Stop at the first and last subheadings of a superior heading.
  21275. Normally this only looks at visible headings, but when INVISIBLE-OK is
  21276. non-nil it will also look at invisible ones."
  21277. (interactive "p")
  21278. (if (not (ignore-errors (org-back-to-heading invisible-ok)))
  21279. (if (and arg (< arg 0))
  21280. (goto-char (point-min))
  21281. (outline-next-heading))
  21282. (org-at-heading-p)
  21283. (let ((level (- (match-end 0) (match-beginning 0) 1))
  21284. (f (if (and arg (< arg 0))
  21285. 're-search-backward
  21286. 're-search-forward))
  21287. (count (if arg (abs arg) 1))
  21288. (result (point)))
  21289. (while (and (prog1 (> count 0)
  21290. (forward-char (if (and arg (< arg 0)) -1 1)))
  21291. (funcall f org-outline-regexp-bol nil 'move))
  21292. (let ((l (- (match-end 0) (match-beginning 0) 1)))
  21293. (cond ((< l level) (setq count 0))
  21294. ((and (= l level)
  21295. (or invisible-ok
  21296. (progn
  21297. (goto-char (line-beginning-position))
  21298. (not (outline-invisible-p)))))
  21299. (setq count (1- count))
  21300. (when (eq l level)
  21301. (setq result (point)))))))
  21302. (goto-char result))
  21303. (beginning-of-line 1)))
  21304. (defun org-backward-heading-same-level (arg &optional invisible-ok)
  21305. "Move backward to the ARG'th subheading at same level as this one.
  21306. Stop at the first and last subheadings of a superior heading."
  21307. (interactive "p")
  21308. (org-forward-heading-same-level (if arg (- arg) -1) invisible-ok))
  21309. (defun org-next-block (arg &optional backward block-regexp)
  21310. "Jump to the next block.
  21311. With a prefix argument ARG, jump forward ARG many source blocks.
  21312. When BACKWARD is non-nil, jump to the previous block.
  21313. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  21314. (interactive "p")
  21315. (let ((re (or block-regexp org-block-regexp))
  21316. (re-search-fn (or (and backward 're-search-backward)
  21317. 're-search-forward)))
  21318. (if (looking-at re) (forward-char 1))
  21319. (condition-case nil
  21320. (funcall re-search-fn re nil nil arg)
  21321. (error (error "No %s code blocks" (if backward "previous" "further" ))))
  21322. (goto-char (match-beginning 0)) (org-show-context)))
  21323. (defun org-previous-block (arg &optional block-regexp)
  21324. "Jump to the previous block.
  21325. With a prefix argument ARG, jump backward ARG many source blocks.
  21326. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  21327. (interactive "p")
  21328. (org-next-block arg t block-regexp))
  21329. (defun org-forward-element ()
  21330. "Move forward by one element.
  21331. Move to the next element at the same level, when possible."
  21332. (interactive)
  21333. (cond ((eobp) (user-error "Cannot move further down"))
  21334. ((org-with-limited-levels (org-at-heading-p))
  21335. (let ((origin (point)))
  21336. (goto-char (org-end-of-subtree nil t))
  21337. (unless (org-with-limited-levels (org-at-heading-p))
  21338. (goto-char origin)
  21339. (user-error "Cannot move further down"))))
  21340. (t
  21341. (let* ((elem (org-element-at-point))
  21342. (end (org-element-property :end elem))
  21343. (parent (org-element-property :parent elem)))
  21344. (if (and parent (= (org-element-property :contents-end parent) end))
  21345. (goto-char (org-element-property :end parent))
  21346. (goto-char end))))))
  21347. (defun org-backward-element ()
  21348. "Move backward by one element.
  21349. Move to the previous element at the same level, when possible."
  21350. (interactive)
  21351. (cond ((bobp) (user-error "Cannot move further up"))
  21352. ((org-with-limited-levels (org-at-heading-p))
  21353. ;; At a headline, move to the previous one, if any, or stay
  21354. ;; here.
  21355. (let ((origin (point)))
  21356. (org-with-limited-levels (org-backward-heading-same-level 1))
  21357. ;; When current headline has no sibling above, move to its
  21358. ;; parent.
  21359. (when (= (point) origin)
  21360. (or (org-with-limited-levels (org-up-heading-safe))
  21361. (progn (goto-char origin)
  21362. (user-error "Cannot move further up"))))))
  21363. (t
  21364. (let* ((trail (org-element-at-point 'keep-trail))
  21365. (elem (car trail))
  21366. (prev-elem (nth 1 trail))
  21367. (beg (org-element-property :begin elem)))
  21368. (cond
  21369. ;; Move to beginning of current element if point isn't
  21370. ;; there already.
  21371. ((/= (point) beg) (goto-char beg))
  21372. (prev-elem (goto-char (org-element-property :begin prev-elem)))
  21373. ((org-before-first-heading-p) (goto-char (point-min)))
  21374. (t (org-back-to-heading)))))))
  21375. (defun org-up-element ()
  21376. "Move to upper element."
  21377. (interactive)
  21378. (if (org-with-limited-levels (org-at-heading-p))
  21379. (unless (org-up-heading-safe) (user-error "No surrounding element"))
  21380. (let* ((elem (org-element-at-point))
  21381. (parent (org-element-property :parent elem)))
  21382. (if parent (goto-char (org-element-property :begin parent))
  21383. (if (org-with-limited-levels (org-before-first-heading-p))
  21384. (user-error "No surrounding element")
  21385. (org-with-limited-levels (org-back-to-heading)))))))
  21386. (defvar org-element-greater-elements)
  21387. (defun org-down-element ()
  21388. "Move to inner element."
  21389. (interactive)
  21390. (let ((element (org-element-at-point)))
  21391. (cond
  21392. ((memq (org-element-type element) '(plain-list table))
  21393. (goto-char (org-element-property :contents-begin element))
  21394. (forward-char))
  21395. ((memq (org-element-type element) org-element-greater-elements)
  21396. ;; If contents are hidden, first disclose them.
  21397. (when (org-element-property :hiddenp element) (org-cycle))
  21398. (goto-char (or (org-element-property :contents-begin element)
  21399. (user-error "No content for this element"))))
  21400. (t (user-error "No inner element")))))
  21401. (defun org-drag-element-backward ()
  21402. "Move backward element at point."
  21403. (interactive)
  21404. (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
  21405. (let* ((trail (org-element-at-point 'keep-trail))
  21406. (elem (car trail))
  21407. (prev-elem (nth 1 trail)))
  21408. ;; Error out if no previous element or previous element is
  21409. ;; a parent of the current one.
  21410. (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
  21411. (user-error "Cannot drag element backward")
  21412. (let ((pos (point)))
  21413. (org-element-swap-A-B prev-elem elem)
  21414. (goto-char (+ (org-element-property :begin prev-elem)
  21415. (- pos (org-element-property :begin elem)))))))))
  21416. (defun org-drag-element-forward ()
  21417. "Move forward element at point."
  21418. (interactive)
  21419. (let* ((pos (point))
  21420. (elem (org-element-at-point)))
  21421. (when (= (point-max) (org-element-property :end elem))
  21422. (user-error "Cannot drag element forward"))
  21423. (goto-char (org-element-property :end elem))
  21424. (let ((next-elem (org-element-at-point)))
  21425. (when (or (org-element-nested-p elem next-elem)
  21426. (and (eq (org-element-type next-elem) 'headline)
  21427. (not (eq (org-element-type elem) 'headline))))
  21428. (goto-char pos)
  21429. (user-error "Cannot drag element forward"))
  21430. ;; Compute new position of point: it's shifted by NEXT-ELEM
  21431. ;; body's length (without final blanks) and by the length of
  21432. ;; blanks between ELEM and NEXT-ELEM.
  21433. (let ((size-next (- (save-excursion
  21434. (goto-char (org-element-property :end next-elem))
  21435. (skip-chars-backward " \r\t\n")
  21436. (forward-line)
  21437. ;; Small correction if buffer doesn't end
  21438. ;; with a newline character.
  21439. (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
  21440. (org-element-property :begin next-elem)))
  21441. (size-blank (- (org-element-property :end elem)
  21442. (save-excursion
  21443. (goto-char (org-element-property :end elem))
  21444. (skip-chars-backward " \r\t\n")
  21445. (forward-line)
  21446. (point)))))
  21447. (org-element-swap-A-B elem next-elem)
  21448. (goto-char (+ pos size-next size-blank))))))
  21449. (defun org-drag-line-forward (arg)
  21450. "Drag the line at point ARG lines forward."
  21451. (interactive "p")
  21452. (dotimes (n (abs arg))
  21453. (let ((c (current-column)))
  21454. (if (< 0 arg)
  21455. (progn
  21456. (beginning-of-line 2)
  21457. (transpose-lines 1)
  21458. (beginning-of-line 0))
  21459. (transpose-lines 1)
  21460. (beginning-of-line -1))
  21461. (org-move-to-column c))))
  21462. (defun org-drag-line-backward (arg)
  21463. "Drag the line at point ARG lines backward."
  21464. (interactive "p")
  21465. (org-drag-line-forward (- arg)))
  21466. (defun org-mark-element ()
  21467. "Put point at beginning of this element, mark at end.
  21468. Interactively, if this command is repeated or (in Transient Mark
  21469. mode) if the mark is active, it marks the next element after the
  21470. ones already marked."
  21471. (interactive)
  21472. (let (deactivate-mark)
  21473. (if (and (org-called-interactively-p 'any)
  21474. (or (and (eq last-command this-command) (mark t))
  21475. (and transient-mark-mode mark-active)))
  21476. (set-mark
  21477. (save-excursion
  21478. (goto-char (mark))
  21479. (goto-char (org-element-property :end (org-element-at-point)))))
  21480. (let ((element (org-element-at-point)))
  21481. (end-of-line)
  21482. (push-mark (org-element-property :end element) t t)
  21483. (goto-char (org-element-property :begin element))))))
  21484. (defun org-narrow-to-element ()
  21485. "Narrow buffer to current element."
  21486. (interactive)
  21487. (let ((elem (org-element-at-point)))
  21488. (cond
  21489. ((eq (car elem) 'headline)
  21490. (narrow-to-region
  21491. (org-element-property :begin elem)
  21492. (org-element-property :end elem)))
  21493. ((memq (car elem) org-element-greater-elements)
  21494. (narrow-to-region
  21495. (org-element-property :contents-begin elem)
  21496. (org-element-property :contents-end elem)))
  21497. (t
  21498. (narrow-to-region
  21499. (org-element-property :begin elem)
  21500. (org-element-property :end elem))))))
  21501. (defun org-transpose-element ()
  21502. "Transpose current and previous elements, keeping blank lines between.
  21503. Point is moved after both elements."
  21504. (interactive)
  21505. (org-skip-whitespace)
  21506. (let ((end (org-element-property :end (org-element-at-point))))
  21507. (org-drag-element-backward)
  21508. (goto-char end)))
  21509. (defun org-unindent-buffer ()
  21510. "Un-indent the visible part of the buffer.
  21511. Relative indentation (between items, inside blocks, etc.) isn't
  21512. modified."
  21513. (interactive)
  21514. (unless (eq major-mode 'org-mode)
  21515. (user-error "Cannot un-indent a buffer not in Org mode"))
  21516. (let* ((parse-tree (org-element-parse-buffer 'greater-element))
  21517. unindent-tree ; For byte-compiler.
  21518. (unindent-tree
  21519. (function
  21520. (lambda (contents)
  21521. (mapc
  21522. (lambda (element)
  21523. (if (memq (org-element-type element) '(headline section))
  21524. (funcall unindent-tree (org-element-contents element))
  21525. (save-excursion
  21526. (save-restriction
  21527. (narrow-to-region
  21528. (org-element-property :begin element)
  21529. (org-element-property :end element))
  21530. (org-do-remove-indentation)))))
  21531. (reverse contents))))))
  21532. (funcall unindent-tree (org-element-contents parse-tree))))
  21533. (defun org-show-subtree ()
  21534. "Show everything after this heading at deeper levels."
  21535. (interactive)
  21536. (outline-flag-region
  21537. (point)
  21538. (save-excursion
  21539. (org-end-of-subtree t t))
  21540. nil))
  21541. (defun org-show-entry ()
  21542. "Show the body directly following this heading.
  21543. Show the heading too, if it is currently invisible."
  21544. (interactive)
  21545. (save-excursion
  21546. (condition-case nil
  21547. (progn
  21548. (org-back-to-heading t)
  21549. (outline-flag-region
  21550. (max (point-min) (1- (point)))
  21551. (save-excursion
  21552. (if (re-search-forward
  21553. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  21554. (match-beginning 1)
  21555. (point-max)))
  21556. nil)
  21557. (org-cycle-hide-drawers 'children))
  21558. (error nil))))
  21559. (defun org-make-options-regexp (kwds &optional extra)
  21560. "Make a regular expression for keyword lines."
  21561. (concat
  21562. "^#\\+\\("
  21563. (mapconcat 'regexp-quote kwds "\\|")
  21564. (if extra (concat "\\|" extra))
  21565. "\\):[ \t]*\\(.*\\)"))
  21566. ;; Make isearch reveal the necessary context
  21567. (defun org-isearch-end ()
  21568. "Reveal context after isearch exits."
  21569. (when isearch-success ; only if search was successful
  21570. (if (featurep 'xemacs)
  21571. ;; Under XEmacs, the hook is run in the correct place,
  21572. ;; we directly show the context.
  21573. (org-show-context 'isearch)
  21574. ;; In Emacs the hook runs *before* restoring the overlays.
  21575. ;; So we have to use a one-time post-command-hook to do this.
  21576. ;; (Emacs 22 has a special variable, see function `org-mode')
  21577. (unless (and (boundp 'isearch-mode-end-hook-quit)
  21578. isearch-mode-end-hook-quit)
  21579. ;; Only when the isearch was not quitted.
  21580. (org-add-hook 'post-command-hook 'org-isearch-post-command
  21581. 'append 'local)))
  21582. (org-fix-ellipsis-at-bol)))
  21583. (defun org-isearch-post-command ()
  21584. "Remove self from hook, and show context."
  21585. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  21586. (org-show-context 'isearch))
  21587. ;;;; Integration with and fixes for other packages
  21588. ;;; Imenu support
  21589. (defvar org-imenu-markers nil
  21590. "All markers currently used by Imenu.")
  21591. (make-variable-buffer-local 'org-imenu-markers)
  21592. (defun org-imenu-new-marker (&optional pos)
  21593. "Return a new marker for use by Imenu, and remember the marker."
  21594. (let ((m (make-marker)))
  21595. (move-marker m (or pos (point)))
  21596. (push m org-imenu-markers)
  21597. m))
  21598. (defun org-imenu-get-tree ()
  21599. "Produce the index for Imenu."
  21600. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  21601. (setq org-imenu-markers nil)
  21602. (let* ((n org-imenu-depth)
  21603. (re (concat "^" (org-get-limited-outline-regexp)))
  21604. (subs (make-vector (1+ n) nil))
  21605. (last-level 0)
  21606. m level head0 head)
  21607. (save-excursion
  21608. (save-restriction
  21609. (widen)
  21610. (goto-char (point-max))
  21611. (while (re-search-backward re nil t)
  21612. (setq level (org-reduced-level (funcall outline-level)))
  21613. (when (and (<= level n)
  21614. (looking-at org-complex-heading-regexp)
  21615. (setq head0 (org-match-string-no-properties 4)))
  21616. (setq head (org-link-display-format head0)
  21617. m (org-imenu-new-marker))
  21618. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  21619. (if (>= level last-level)
  21620. (push (cons head m) (aref subs level))
  21621. (push (cons head (aref subs (1+ level))) (aref subs level))
  21622. (loop for i from (1+ level) to n do (aset subs i nil)))
  21623. (setq last-level level)))))
  21624. (aref subs 1)))
  21625. (eval-after-load "imenu"
  21626. '(progn
  21627. (add-hook 'imenu-after-jump-hook
  21628. (lambda ()
  21629. (if (derived-mode-p 'org-mode)
  21630. (org-show-context 'org-goto))))))
  21631. (defun org-link-display-format (link)
  21632. "Replace a link with its the description.
  21633. If there is no description, use the link target."
  21634. (save-match-data
  21635. (if (string-match org-bracket-link-analytic-regexp link)
  21636. (replace-match (if (match-end 5)
  21637. (match-string 5 link)
  21638. (concat (match-string 1 link)
  21639. (match-string 3 link)))
  21640. nil t link)
  21641. link)))
  21642. (defun org-toggle-link-display ()
  21643. "Toggle the literal or descriptive display of links."
  21644. (interactive)
  21645. (if org-descriptive-links
  21646. (progn (org-remove-from-invisibility-spec '(org-link))
  21647. (org-restart-font-lock)
  21648. (setq org-descriptive-links nil))
  21649. (progn (add-to-invisibility-spec '(org-link))
  21650. (org-restart-font-lock)
  21651. (setq org-descriptive-links t))))
  21652. ;; Speedbar support
  21653. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  21654. "Overlay marking the agenda restriction line in speedbar.")
  21655. (overlay-put org-speedbar-restriction-lock-overlay
  21656. 'face 'org-agenda-restriction-lock)
  21657. (overlay-put org-speedbar-restriction-lock-overlay
  21658. 'help-echo "Agendas are currently limited to this item.")
  21659. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  21660. (defun org-speedbar-set-agenda-restriction ()
  21661. "Restrict future agenda commands to the location at point in speedbar.
  21662. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  21663. (interactive)
  21664. (require 'org-agenda)
  21665. (let (p m tp np dir txt)
  21666. (cond
  21667. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  21668. 'org-imenu t))
  21669. (setq m (get-text-property p 'org-imenu-marker))
  21670. (with-current-buffer (marker-buffer m)
  21671. (goto-char m)
  21672. (org-agenda-set-restriction-lock 'subtree)))
  21673. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  21674. 'speedbar-function 'speedbar-find-file))
  21675. (setq tp (previous-single-property-change
  21676. (1+ p) 'speedbar-function)
  21677. np (next-single-property-change
  21678. tp 'speedbar-function)
  21679. dir (speedbar-line-directory)
  21680. txt (buffer-substring-no-properties (or tp (point-min))
  21681. (or np (point-max))))
  21682. (with-current-buffer (find-file-noselect
  21683. (let ((default-directory dir))
  21684. (expand-file-name txt)))
  21685. (unless (derived-mode-p 'org-mode)
  21686. (user-error "Cannot restrict to non-Org-mode file"))
  21687. (org-agenda-set-restriction-lock 'file)))
  21688. (t (user-error "Don't know how to restrict Org-mode's agenda")))
  21689. (move-overlay org-speedbar-restriction-lock-overlay
  21690. (point-at-bol) (point-at-eol))
  21691. (setq current-prefix-arg nil)
  21692. (org-agenda-maybe-redo)))
  21693. (eval-after-load "speedbar"
  21694. '(progn
  21695. (speedbar-add-supported-extension ".org")
  21696. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  21697. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  21698. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  21699. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  21700. (add-hook 'speedbar-visiting-tag-hook
  21701. (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
  21702. ;;; Fixes and Hacks for problems with other packages
  21703. ;; Make flyspell not check words in links, to not mess up our keymap
  21704. (defvar org-element-affiliated-keywords) ; From org-element.el
  21705. (defvar org-element-block-name-alist) ; From org-element.el
  21706. (defun org-mode-flyspell-verify ()
  21707. "Don't let flyspell put overlays at active buttons, or on
  21708. {todo,all-time,additional-option-like}-keywords."
  21709. (require 'org-element) ; For `org-element-affiliated-keywords'
  21710. (let ((pos (max (1- (point)) (point-min)))
  21711. (word (thing-at-point 'word)))
  21712. (and (not (get-text-property pos 'keymap))
  21713. (not (get-text-property pos 'org-no-flyspell))
  21714. (not (member word org-todo-keywords-1))
  21715. (not (member word org-all-time-keywords))
  21716. (not (member word org-options-keywords))
  21717. (not (member word (mapcar 'car org-startup-options)))
  21718. (not (member-ignore-case word org-element-affiliated-keywords))
  21719. (not (member-ignore-case word (org-get-export-keywords)))
  21720. (not (member-ignore-case
  21721. word (mapcar 'car org-element-block-name-alist)))
  21722. (not (member-ignore-case word '("BEGIN" "END" "ATTR")))
  21723. (not (org-in-src-block-p)))))
  21724. (defun org-remove-flyspell-overlays-in (beg end)
  21725. "Remove flyspell overlays in region."
  21726. (and (org-bound-and-true-p flyspell-mode)
  21727. (fboundp 'flyspell-delete-region-overlays)
  21728. (flyspell-delete-region-overlays beg end))
  21729. (add-text-properties beg end '(org-no-flyspell t)))
  21730. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  21731. (eval-after-load "bookmark"
  21732. '(if (boundp 'bookmark-after-jump-hook)
  21733. ;; We can use the hook
  21734. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  21735. ;; Hook not available, use advice
  21736. (defadvice bookmark-jump (after org-make-visible activate)
  21737. "Make the position visible."
  21738. (org-bookmark-jump-unhide))))
  21739. ;; Make sure saveplace shows the location if it was hidden
  21740. (eval-after-load "saveplace"
  21741. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  21742. "Make the position visible."
  21743. (org-bookmark-jump-unhide)))
  21744. ;; Make sure ecb shows the location if it was hidden
  21745. (eval-after-load "ecb"
  21746. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  21747. "Make hierarchy visible when jumping into location from ECB tree buffer."
  21748. (if (derived-mode-p 'org-mode)
  21749. (org-show-context))))
  21750. (defun org-bookmark-jump-unhide ()
  21751. "Unhide the current position, to show the bookmark location."
  21752. (and (derived-mode-p 'org-mode)
  21753. (or (outline-invisible-p)
  21754. (save-excursion (goto-char (max (point-min) (1- (point))))
  21755. (outline-invisible-p)))
  21756. (org-show-context 'bookmark-jump)))
  21757. ;; Make session.el ignore our circular variable
  21758. (eval-after-load "session"
  21759. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  21760. ;;;; Finish up
  21761. (provide 'org)
  21762. (run-hooks 'org-load-hook)
  21763. ;;; org.el ends here