org.el 910 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995229962299722998229992300023001230022300323004230052300623007230082300923010230112301223013230142301523016230172301823019230202302123022230232302423025230262302723028230292303023031230322303323034230352303623037230382303923040230412304223043230442304523046230472304823049230502305123052230532305423055230562305723058230592306023061230622306323064230652306623067230682306923070230712307223073230742307523076230772307823079230802308123082230832308423085230862308723088230892309023091230922309323094230952309623097230982309923100231012310223103231042310523106231072310823109231102311123112231132311423115231162311723118231192312023121231222312323124231252312623127231282312923130231312313223133231342313523136231372313823139231402314123142231432314423145231462314723148231492315023151231522315323154231552315623157231582315923160231612316223163231642316523166231672316823169231702317123172231732317423175231762317723178231792318023181231822318323184231852318623187231882318923190231912319223193231942319523196231972319823199232002320123202232032320423205232062320723208232092321023211232122321323214232152321623217232182321923220232212322223223232242322523226232272322823229232302323123232232332323423235232362323723238232392324023241232422324323244232452324623247232482324923250232512325223253232542325523256232572325823259232602326123262232632326423265232662326723268232692327023271232722327323274232752327623277232782327923280232812328223283232842328523286232872328823289232902329123292232932329423295232962329723298232992330023301233022330323304233052330623307233082330923310233112331223313233142331523316233172331823319233202332123322233232332423325233262332723328233292333023331233322333323334233352333623337233382333923340233412334223343233442334523346233472334823349233502335123352233532335423355233562335723358233592336023361233622336323364233652336623367233682336923370233712337223373233742337523376233772337823379233802338123382233832338423385233862338723388233892339023391233922339323394233952339623397233982339923400234012340223403234042340523406234072340823409234102341123412234132341423415234162341723418234192342023421234222342323424234252342623427234282342923430234312343223433234342343523436234372343823439234402344123442234432344423445234462344723448234492345023451234522345323454234552345623457234582345923460234612346223463234642346523466234672346823469234702347123472234732347423475234762347723478234792348023481234822348323484234852348623487234882348923490234912349223493234942349523496234972349823499235002350123502235032350423505235062350723508235092351023511235122351323514235152351623517235182351923520235212352223523235242352523526235272352823529235302353123532235332353423535235362353723538235392354023541235422354323544235452354623547235482354923550235512355223553235542355523556235572355823559235602356123562235632356423565235662356723568235692357023571235722357323574235752357623577235782357923580235812358223583235842358523586235872358823589235902359123592235932359423595235962359723598235992360023601236022360323604236052360623607236082360923610236112361223613236142361523616236172361823619236202362123622236232362423625236262362723628236292363023631236322363323634236352363623637236382363923640236412364223643236442364523646236472364823649236502365123652236532365423655236562365723658236592366023661236622366323664236652366623667236682366923670236712367223673236742367523676236772367823679236802368123682236832368423685236862368723688236892369023691236922369323694236952369623697236982369923700237012370223703237042370523706237072370823709237102371123712237132371423715237162371723718237192372023721237222372323724237252372623727237282372923730237312373223733237342373523736237372373823739237402374123742237432374423745237462374723748237492375023751237522375323754237552375623757237582375923760237612376223763237642376523766237672376823769237702377123772237732377423775237762377723778237792378023781237822378323784237852378623787237882378923790237912379223793237942379523796237972379823799238002380123802238032380423805238062380723808238092381023811238122381323814238152381623817238182381923820238212382223823238242382523826238272382823829238302383123832238332383423835238362383723838238392384023841238422384323844238452384623847238482384923850238512385223853238542385523856238572385823859238602386123862238632386423865238662386723868238692387023871238722387323874238752387623877238782387923880238812388223883238842388523886238872388823889238902389123892238932389423895238962389723898238992390023901239022390323904239052390623907239082390923910239112391223913239142391523916239172391823919239202392123922239232392423925239262392723928239292393023931239322393323934239352393623937239382393923940239412394223943239442394523946239472394823949239502395123952239532395423955239562395723958239592396023961239622396323964239652396623967239682396923970239712397223973239742397523976239772397823979239802398123982239832398423985239862398723988239892399023991239922399323994239952399623997239982399924000240012400224003240042400524006240072400824009240102401124012240132401424015240162401724018240192402024021240222402324024
  1. ;;; org.el --- Outline-based notes management and organizer
  2. ;; Carstens outline-mode for keeping track of everything.
  3. ;; Copyright (C) 2004-2014 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-bound 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-add-archive-files "org-archive" (files))
  101. (declare-function org-inlinetask-at-task-p "org-inlinetask" ())
  102. (declare-function org-inlinetask-outline-regexp "org-inlinetask" ())
  103. (declare-function org-inlinetask-toggle-visibility "org-inlinetask" ())
  104. (declare-function org-pop-to-buffer-same-window "org-compat" (&optional buffer-or-name norecord label))
  105. (declare-function org-clock-get-last-clock-out-time "org-clock" ())
  106. (declare-function org-clock-timestamps-up "org-clock" (&optional n))
  107. (declare-function org-clock-timestamps-down "org-clock" (&optional n))
  108. (declare-function org-clock-remove-overlays "org-clock" (&optional beg end noremove))
  109. (declare-function org-clock-sum-current-item "org-clock" (&optional tstart))
  110. (declare-function org-clock-update-time-maybe "org-clock" ())
  111. (declare-function org-clocktable-shift "org-clock" (dir n))
  112. (declare-function orgtbl-mode "org-table" (&optional arg))
  113. (declare-function org-clock-out "org-clock" (&optional switch-to-state fail-quietly at-time))
  114. (declare-function org-beamer-mode "ox-beamer" ())
  115. (declare-function org-table-edit-field "org-table" (arg))
  116. (declare-function org-table-justify-field-maybe "org-table" (&optional new))
  117. (declare-function org-table-set-constants "org-table" ())
  118. (declare-function org-table-calc-current-TBLFM "org-table" (&optional arg))
  119. (declare-function org-id-get-create "org-id" (&optional force))
  120. (declare-function org-id-find-id-file "org-id" (id))
  121. (declare-function org-tags-view "org-agenda" (&optional todo-only match))
  122. (declare-function org-agenda-list "org-agenda" (&optional arg start-day span))
  123. (declare-function org-agenda-redo "org-agenda" (&optional all))
  124. (declare-function org-table-align "org-table" ())
  125. (declare-function org-table-begin "org-table" (&optional table-type))
  126. (declare-function org-table-blank-field "org-table" ())
  127. (declare-function org-table-end "org-table" (&optional table-type))
  128. (declare-function org-table-insert-row "org-table" (&optional arg))
  129. (declare-function org-table-paste-rectangle "org-table" ())
  130. (declare-function org-table-maybe-eval-formula "org-table" ())
  131. (declare-function org-table-maybe-recalculate-line "org-table" ())
  132. (declare-function org-element--parse-objects "org-element"
  133. (beg end acc restriction))
  134. (declare-function org-element-at-point "org-element" (&optional keep-trail))
  135. (declare-function org-element-contents "org-element" (element))
  136. (declare-function org-element-context "org-element" (&optional element))
  137. (declare-function org-element-interpret-data "org-element"
  138. (data &optional parent))
  139. (declare-function org-element-map "org-element"
  140. (data types fun &optional info first-match no-recursion))
  141. (declare-function org-element-nested-p "org-element" (elem-a elem-b))
  142. (declare-function org-element-parse-buffer "org-element"
  143. (&optional granularity visible-only))
  144. (declare-function org-element-property "org-element" (property element))
  145. (declare-function org-element-put-property "org-element"
  146. (element property value))
  147. (declare-function org-element-swap-A-B "org-element" (elem-a elem-b))
  148. (declare-function org-element--parse-objects "org-element"
  149. (beg end acc restriction))
  150. (declare-function org-element-parse-buffer "org-element"
  151. (&optional granularity visible-only))
  152. (declare-function org-element-restriction "org-element" (element))
  153. (declare-function org-element-type "org-element" (element))
  154. ;; load languages based on value of `org-babel-load-languages'
  155. (defvar org-babel-load-languages)
  156. ;;;###autoload
  157. (defun org-babel-do-load-languages (sym value)
  158. "Load the languages defined in `org-babel-load-languages'."
  159. (set-default sym value)
  160. (mapc (lambda (pair)
  161. (let ((active (cdr pair)) (lang (symbol-name (car pair))))
  162. (if active
  163. (progn
  164. (require (intern (concat "ob-" lang))))
  165. (progn
  166. (funcall 'fmakunbound
  167. (intern (concat "org-babel-execute:" lang)))
  168. (funcall 'fmakunbound
  169. (intern (concat "org-babel-expand-body:" lang)))))))
  170. org-babel-load-languages))
  171. (declare-function org-babel-tangle-file "ob-tangle" (file &optional target-file lang))
  172. ;;;###autoload
  173. (defun org-babel-load-file (file &optional compile)
  174. "Load Emacs Lisp source code blocks in the Org-mode FILE.
  175. This function exports the source code using `org-babel-tangle'
  176. and then loads the resulting file using `load-file'. With prefix
  177. arg (noninteractively: 2nd arg) COMPILE the tangled Emacs Lisp
  178. file to byte-code before it is loaded."
  179. (interactive "fFile to load: \nP")
  180. (require 'ob-core)
  181. (let* ((age (lambda (file)
  182. (float-time
  183. (time-subtract (current-time)
  184. (nth 5 (or (file-attributes (file-truename file))
  185. (file-attributes file)))))))
  186. (base-name (file-name-sans-extension file))
  187. (exported-file (concat base-name ".el")))
  188. ;; tangle if the org-mode file is newer than the elisp file
  189. (unless (and (file-exists-p exported-file)
  190. (> (funcall age file) (funcall age exported-file)))
  191. (setq exported-file
  192. (car (org-babel-tangle-file file exported-file "emacs-lisp"))))
  193. (message "%s %s"
  194. (if compile
  195. (progn (byte-compile-file exported-file 'load)
  196. "Compiled and loaded")
  197. (progn (load-file exported-file) "Loaded"))
  198. exported-file)))
  199. (defcustom org-babel-load-languages '((emacs-lisp . t))
  200. "Languages which can be evaluated in Org-mode buffers.
  201. This list can be used to load support for any of the languages
  202. below, note that each language will depend on a different set of
  203. system executables and/or Emacs modes. When a language is
  204. \"loaded\", then code blocks in that language can be evaluated
  205. with `org-babel-execute-src-block' bound by default to C-c
  206. C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
  207. be set to remove code block evaluation from the C-c C-c
  208. keybinding. By default only Emacs Lisp (which has no
  209. requirements) is loaded."
  210. :group 'org-babel
  211. :set 'org-babel-do-load-languages
  212. :version "24.1"
  213. :type '(alist :tag "Babel Languages"
  214. :key-type
  215. (choice
  216. (const :tag "Awk" awk)
  217. (const :tag "C" C)
  218. (const :tag "R" R)
  219. (const :tag "Asymptote" asymptote)
  220. (const :tag "Calc" calc)
  221. (const :tag "Clojure" clojure)
  222. (const :tag "CSS" css)
  223. (const :tag "Ditaa" ditaa)
  224. (const :tag "Dot" dot)
  225. (const :tag "Emacs Lisp" emacs-lisp)
  226. (const :tag "Fortran" fortran)
  227. (const :tag "Gnuplot" gnuplot)
  228. (const :tag "Haskell" haskell)
  229. (const :tag "IO" io)
  230. (const :tag "Java" java)
  231. (const :tag "Javascript" js)
  232. (const :tag "LaTeX" latex)
  233. (const :tag "Ledger" ledger)
  234. (const :tag "Lilypond" lilypond)
  235. (const :tag "Lisp" lisp)
  236. (const :tag "Makefile" makefile)
  237. (const :tag "Maxima" maxima)
  238. (const :tag "Matlab" matlab)
  239. (const :tag "Mscgen" mscgen)
  240. (const :tag "Ocaml" ocaml)
  241. (const :tag "Octave" octave)
  242. (const :tag "Org" org)
  243. (const :tag "Perl" perl)
  244. (const :tag "Pico Lisp" picolisp)
  245. (const :tag "PlantUML" plantuml)
  246. (const :tag "Python" python)
  247. (const :tag "Ruby" ruby)
  248. (const :tag "Sass" sass)
  249. (const :tag "Scala" scala)
  250. (const :tag "Scheme" scheme)
  251. (const :tag "Screen" screen)
  252. (const :tag "Shell Script" sh)
  253. (const :tag "Shen" shen)
  254. (const :tag "Sql" sql)
  255. (const :tag "Sqlite" sqlite))
  256. :value-type (boolean :tag "Activate" :value t)))
  257. ;;;; Customization variables
  258. (defcustom org-clone-delete-id nil
  259. "Remove ID property of clones of a subtree.
  260. When non-nil, clones of a subtree don't inherit the ID property.
  261. Otherwise they inherit the ID property with a new unique
  262. identifier."
  263. :type 'boolean
  264. :version "24.1"
  265. :group 'org-id)
  266. ;;; Version
  267. (org-check-version)
  268. ;;;###autoload
  269. (defun org-version (&optional here full message)
  270. "Show the org-mode version in the echo area.
  271. With prefix argument HERE, insert it at point.
  272. When FULL is non-nil, use a verbose version string.
  273. When MESSAGE is non-nil, display a message with the version."
  274. (interactive "P")
  275. (let* ((org-dir (ignore-errors (org-find-library-dir "org")))
  276. (save-load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  277. (load-suffixes (list ".el"))
  278. (org-install-dir (ignore-errors (org-find-library-dir "org-loaddefs")))
  279. (org-trash (or
  280. (and (fboundp 'org-release) (fboundp 'org-git-version))
  281. (org-load-noerror-mustsuffix (concat org-dir "org-version"))))
  282. (load-suffixes save-load-suffixes)
  283. (org-version (org-release))
  284. (git-version (org-git-version))
  285. (version (format "Org-mode version %s (%s @ %s)"
  286. org-version
  287. git-version
  288. (if org-install-dir
  289. (if (string= org-dir org-install-dir)
  290. org-install-dir
  291. (concat "mixed installation! " org-install-dir " and " org-dir))
  292. "org-loaddefs.el can not be found!")))
  293. (version1 (if full version org-version)))
  294. (if (org-called-interactively-p 'interactive)
  295. (if here
  296. (insert version)
  297. (message version))
  298. (if message (message version1))
  299. version1)))
  300. (defconst org-version (org-version))
  301. ;;; Compatibility constants
  302. ;;; The custom variables
  303. (defgroup org nil
  304. "Outline-based notes management and organizer."
  305. :tag "Org"
  306. :group 'outlines
  307. :group 'calendar)
  308. (defcustom org-mode-hook nil
  309. "Mode hook for Org-mode, run after the mode was turned on."
  310. :group 'org
  311. :type 'hook)
  312. (defcustom org-load-hook nil
  313. "Hook that is run after org.el has been loaded."
  314. :group 'org
  315. :type 'hook)
  316. (defcustom org-log-buffer-setup-hook nil
  317. "Hook that is run after an Org log buffer is created."
  318. :group 'org
  319. :version "24.1"
  320. :type 'hook)
  321. (defvar org-modules) ; defined below
  322. (defvar org-modules-loaded nil
  323. "Have the modules been loaded already?")
  324. (defun org-load-modules-maybe (&optional force)
  325. "Load all extensions listed in `org-modules'."
  326. (when (or force (not org-modules-loaded))
  327. (mapc (lambda (ext)
  328. (condition-case nil (require ext)
  329. (error (message "Problems while trying to load feature `%s'" ext))))
  330. org-modules)
  331. (setq org-modules-loaded t)))
  332. (defun org-set-modules (var value)
  333. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  334. (set var value)
  335. (when (featurep 'org)
  336. (org-load-modules-maybe 'force)))
  337. (defcustom org-modules '(org-w3m org-bbdb org-bibtex org-docview org-gnus org-info org-irc org-mhe org-rmail)
  338. "Modules that should always be loaded together with org.el.
  339. If a description starts with <C>, the file is not part of Emacs
  340. and loading it will require that you have downloaded and properly
  341. installed the Org mode distribution.
  342. You can also use this system to load external packages (i.e. neither Org
  343. core modules, nor modules from the CONTRIB directory). Just add symbols
  344. to the end of the list. If the package is called org-xyz.el, then you need
  345. to add the symbol `xyz', and the package must have a call to:
  346. \(provide 'org-xyz)
  347. For export specific modules, see also `org-export-backends'."
  348. :group 'org
  349. :set 'org-set-modules
  350. :version "24.4"
  351. :package-version '(Org . "8.0")
  352. :type
  353. '(set :greedy t
  354. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  355. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  356. (const :tag " crypt: Encryption of subtrees" org-crypt)
  357. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  358. (const :tag " docview: Links to doc-view buffers" org-docview)
  359. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  360. (const :tag " habit: Track your consistency with habits" org-habit)
  361. (const :tag " id: Global IDs for identifying entries" org-id)
  362. (const :tag " info: Links to Info nodes" org-info)
  363. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  364. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  365. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  366. (const :tag " mouse: Additional mouse support" org-mouse)
  367. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  368. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  369. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  370. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  371. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  372. (const :tag "C bullets: Add overlays to headlines stars" org-bullets)
  373. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  374. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  375. (const :tag "C collector: Collect properties into tables" org-collector)
  376. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  377. (const :tag "C drill: Flashcards and spaced repetition for Org-mode" org-drill)
  378. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  379. (const :tag "C eshell Support for links to working directories in eshell" org-eshell)
  380. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  381. (const :tag "C eval: Include command output as text" org-eval)
  382. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  383. (const :tag "C favtable: Lookup table of favorite references and links" org-favtable)
  384. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  385. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  386. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  387. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  388. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  389. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  390. (const :tag "C mac-link: Grab links and url from various mac Applications" org-mac-link)
  391. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  392. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  393. (const :tag "C mew: Links to Mew folders/messages" org-mew)
  394. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  395. (const :tag "C notmuch: Provide org links to notmuch searches or messages" org-notmuch)
  396. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  397. (const :tag "C registry: A registry for Org-mode links" org-registry)
  398. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  399. (const :tag "C secretary: Team management with org-mode" org-secretary)
  400. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  401. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  402. (const :tag "C track: Keep up with Org-mode development" org-track)
  403. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  404. (const :tag "C vm: Links to VM folders/messages" org-vm)
  405. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  406. (const :tag "C wl: Links to Wanderlust folders/messages" org-wl)
  407. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  408. (defvar org-export--registered-backends) ; From ox.el.
  409. (declare-function org-export-derived-backend-p "ox" (backend &rest backends))
  410. (declare-function org-export-backend-name "ox" (backend))
  411. (defcustom org-export-backends '(ascii html icalendar latex)
  412. "List of export back-ends that should be always available.
  413. If a description starts with <C>, the file is not part of Emacs
  414. and loading it will require that you have downloaded and properly
  415. installed the Org mode distribution.
  416. Unlike to `org-modules', libraries in this list will not be
  417. loaded along with Org, but only once the export framework is
  418. needed.
  419. This variable needs to be set before org.el is loaded. If you
  420. need to make a change while Emacs is running, use the customize
  421. interface or run the following code, where VAL stands for the new
  422. value of the variable, after updating it:
  423. \(progn
  424. \(setq org-export--registered-backends
  425. \(org-remove-if-not
  426. \(lambda (backend)
  427. \(let ((name (org-export-backend-name backend)))
  428. \(or (memq name val)
  429. \(catch 'parentp
  430. \(dolist (b val)
  431. \(and (org-export-derived-backend-p b name)
  432. \(throw 'parentp t)))))))
  433. org-export--registered-backends))
  434. \(let ((new-list (mapcar 'org-export-backend-name
  435. org-export--registered-backends)))
  436. \(dolist (backend val)
  437. \(cond
  438. \((not (load (format \"ox-%s\" backend) t t))
  439. \(message \"Problems while trying to load export back-end `%s'\"
  440. backend))
  441. \((not (memq backend new-list)) (push backend new-list))))
  442. \(set-default 'org-export-backends new-list)))
  443. Adding a back-end to this list will also pull the back-end it
  444. depends on, if any."
  445. :group 'org
  446. :group 'org-export
  447. :version "24.4"
  448. :package-version '(Org . "8.0")
  449. :initialize 'custom-initialize-set
  450. :set (lambda (var val)
  451. (if (not (featurep 'ox)) (set-default var val)
  452. ;; Any back-end not required anymore (not present in VAL and not
  453. ;; a parent of any back-end in the new value) is removed from the
  454. ;; list of registered back-ends.
  455. (setq org-export--registered-backends
  456. (org-remove-if-not
  457. (lambda (backend)
  458. (let ((name (org-export-backend-name backend)))
  459. (or (memq name val)
  460. (catch 'parentp
  461. (dolist (b val)
  462. (and (org-export-derived-backend-p b name)
  463. (throw 'parentp t)))))))
  464. org-export--registered-backends))
  465. ;; Now build NEW-LIST of both new back-ends and required
  466. ;; parents.
  467. (let ((new-list (mapcar 'org-export-backend-name
  468. org-export--registered-backends)))
  469. (dolist (backend val)
  470. (cond
  471. ((not (load (format "ox-%s" backend) t t))
  472. (message "Problems while trying to load export back-end `%s'"
  473. backend))
  474. ((not (memq backend new-list)) (push backend new-list))))
  475. ;; Set VAR to that list with fixed dependencies.
  476. (set-default var new-list))))
  477. :type '(set :greedy t
  478. (const :tag " ascii Export buffer to ASCII format" ascii)
  479. (const :tag " beamer Export buffer to Beamer presentation" beamer)
  480. (const :tag " html Export buffer to HTML format" html)
  481. (const :tag " icalendar Export buffer to iCalendar format" icalendar)
  482. (const :tag " latex Export buffer to LaTeX format" latex)
  483. (const :tag " man Export buffer to MAN format" man)
  484. (const :tag " md Export buffer to Markdown format" md)
  485. (const :tag " odt Export buffer to ODT format" odt)
  486. (const :tag " org Export buffer to Org format" org)
  487. (const :tag " texinfo Export buffer to Texinfo format" texinfo)
  488. (const :tag "C confluence Export buffer to Confluence Wiki format" confluence)
  489. (const :tag "C deck Export buffer to deck.js presentations" deck)
  490. (const :tag "C freemind Export buffer to Freemind mindmap format" freemind)
  491. (const :tag "C groff Export buffer to Groff format" groff)
  492. (const :tag "C koma-letter Export buffer to KOMA Scrlttrl2 format" koma-letter)
  493. (const :tag "C RSS 2.0 Export buffer to RSS 2.0 format" rss)
  494. (const :tag "C s5 Export buffer to s5 presentations" s5)
  495. (const :tag "C taskjuggler Export buffer to TaskJuggler format" taskjuggler)))
  496. (eval-after-load 'ox
  497. '(mapc
  498. (lambda (backend)
  499. (condition-case nil (require (intern (format "ox-%s" backend)))
  500. (error (message "Problems while trying to load export back-end `%s'"
  501. backend))))
  502. org-export-backends))
  503. (defcustom org-support-shift-select nil
  504. "Non-nil means make shift-cursor commands select text when possible.
  505. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
  506. start selecting a region, or enlarge regions started in this way.
  507. In Org-mode, in special contexts, these same keys are used for
  508. other purposes, important enough to compete with shift selection.
  509. Org tries to balance these needs by supporting `shift-select-mode'
  510. outside these special contexts, under control of this variable.
  511. The default of this variable is nil, to avoid confusing behavior. Shifted
  512. cursor keys will then execute Org commands in the following contexts:
  513. - on a headline, changing TODO state (left/right) and priority (up/down)
  514. - on a time stamp, changing the time
  515. - in a plain list item, changing the bullet type
  516. - in a property definition line, switching between allowed values
  517. - in the BEGIN line of a clock table (changing the time block).
  518. Outside these contexts, the commands will throw an error.
  519. When this variable is t and the cursor is not in a special
  520. context, Org-mode will support shift-selection for making and
  521. enlarging regions. To make this more effective, the bullet
  522. cycling will no longer happen anywhere in an item line, but only
  523. if the cursor is exactly on the bullet.
  524. If you set this variable to the symbol `always', then the keys
  525. will not be special in headlines, property lines, and item lines,
  526. to make shift selection work there as well. If this is what you
  527. want, you can use the following alternative commands: `C-c C-t'
  528. and `C-c ,' to change TODO state and priority, `C-u C-u C-c C-t'
  529. can be used to switch TODO sets, `C-c -' to cycle item bullet
  530. types, and properties can be edited by hand or in column view.
  531. However, when the cursor is on a timestamp, shift-cursor commands
  532. will still edit the time stamp - this is just too good to give up.
  533. XEmacs user should have this variable set to nil, because
  534. `shift-select-mode' is in Emacs 23 or later only."
  535. :group 'org
  536. :type '(choice
  537. (const :tag "Never" nil)
  538. (const :tag "When outside special context" t)
  539. (const :tag "Everywhere except timestamps" always)))
  540. (defcustom org-loop-over-headlines-in-active-region nil
  541. "Shall some commands act upon headlines in the active region?
  542. When set to `t', some commands will be performed in all headlines
  543. within the active region.
  544. When set to `start-level', some commands will be performed in all
  545. headlines within the active region, provided that these headlines
  546. are of the same level than the first one.
  547. When set to a string, those commands will be performed on the
  548. matching headlines within the active region. Such string must be
  549. a tags/property/todo match as it is used in the agenda tags view.
  550. The list of commands is: `org-schedule', `org-deadline',
  551. `org-todo', `org-archive-subtree', `org-archive-set-tag' and
  552. `org-archive-to-archive-sibling'. The archiving commands skip
  553. already archived entries."
  554. :type '(choice (const :tag "Don't loop" nil)
  555. (const :tag "All headlines in active region" t)
  556. (const :tag "In active region, headlines at the same level than the first one" start-level)
  557. (string :tag "Tags/Property/Todo matcher"))
  558. :version "24.1"
  559. :group 'org-todo
  560. :group 'org-archive)
  561. (defgroup org-startup nil
  562. "Options concerning startup of Org-mode."
  563. :tag "Org Startup"
  564. :group 'org)
  565. (defcustom org-startup-folded t
  566. "Non-nil means entering Org-mode will switch to OVERVIEW.
  567. This can also be configured on a per-file basis by adding one of
  568. the following lines anywhere in the buffer:
  569. #+STARTUP: fold (or `overview', this is equivalent)
  570. #+STARTUP: nofold (or `showall', this is equivalent)
  571. #+STARTUP: content
  572. #+STARTUP: showeverything
  573. By default, this option is ignored when Org opens agenda files
  574. for the first time. If you want the agenda to honor the startup
  575. option, set `org-agenda-inhibit-startup' to nil."
  576. :group 'org-startup
  577. :type '(choice
  578. (const :tag "nofold: show all" nil)
  579. (const :tag "fold: overview" t)
  580. (const :tag "content: all headlines" content)
  581. (const :tag "show everything, even drawers" showeverything)))
  582. (defcustom org-startup-truncated t
  583. "Non-nil means entering Org-mode will set `truncate-lines'.
  584. This is useful since some lines containing links can be very long and
  585. uninteresting. Also tables look terrible when wrapped."
  586. :group 'org-startup
  587. :type 'boolean)
  588. (defcustom org-startup-indented nil
  589. "Non-nil means turn on `org-indent-mode' on startup.
  590. This can also be configured on a per-file basis by adding one of
  591. the following lines anywhere in the buffer:
  592. #+STARTUP: indent
  593. #+STARTUP: noindent"
  594. :group 'org-structure
  595. :type '(choice
  596. (const :tag "Not" nil)
  597. (const :tag "Globally (slow on startup in large files)" t)))
  598. (defcustom org-use-sub-superscripts t
  599. "Non-nil means interpret \"_\" and \"^\" for display.
  600. If you want to control how Org exports those characters, see
  601. `org-export-with-sub-superscripts'. `org-use-sub-superscripts'
  602. used to be an alias for `org-export-with-sub-superscripts' in
  603. Org <8.0, it is not anymore.
  604. When this option is turned on, you can use TeX-like syntax for
  605. sub- and superscripts within the buffer. Several characters after
  606. \"_\" or \"^\" will be considered as a single item - so grouping
  607. with {} is normally not needed. For example, the following things
  608. will be parsed as single sub- or superscripts:
  609. 10^24 or 10^tau several digits will be considered 1 item.
  610. 10^-12 or 10^-tau a leading sign with digits or a word
  611. x^2-y^3 will be read as x^2 - y^3, because items are
  612. terminated by almost any nonword/nondigit char.
  613. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  614. Still, ambiguity is possible. So when in doubt, use {} to enclose
  615. the sub/superscript. If you set this variable to the symbol `{}',
  616. the braces are *required* in order to trigger interpretations as
  617. sub/superscript. This can be helpful in documents that need \"_\"
  618. frequently in plain text."
  619. :group 'org-startup
  620. :version "24.4"
  621. :package-version '(Org . "8.0")
  622. :type '(choice
  623. (const :tag "Always interpret" t)
  624. (const :tag "Only with braces" {})
  625. (const :tag "Never interpret" nil)))
  626. (defcustom org-startup-with-beamer-mode nil
  627. "Non-nil means turn on `org-beamer-mode' on startup.
  628. This can also be configured on a per-file basis by adding one of
  629. the following lines anywhere in the buffer:
  630. #+STARTUP: beamer"
  631. :group 'org-startup
  632. :version "24.1"
  633. :type 'boolean)
  634. (defcustom org-startup-align-all-tables nil
  635. "Non-nil means align all tables when visiting a file.
  636. This is useful when the column width in tables is forced with <N> cookies
  637. in table fields. Such tables will look correct only after the first re-align.
  638. This can also be configured on a per-file basis by adding one of
  639. the following lines anywhere in the buffer:
  640. #+STARTUP: align
  641. #+STARTUP: noalign"
  642. :group 'org-startup
  643. :type 'boolean)
  644. (defcustom org-startup-with-inline-images nil
  645. "Non-nil means show inline images when loading a new Org file.
  646. This can also be configured on a per-file basis by adding one of
  647. the following lines anywhere in the buffer:
  648. #+STARTUP: inlineimages
  649. #+STARTUP: noinlineimages"
  650. :group 'org-startup
  651. :version "24.1"
  652. :type 'boolean)
  653. (defcustom org-startup-with-latex-preview nil
  654. "Non-nil means preview LaTeX fragments when loading a new Org file.
  655. This can also be configured on a per-file basis by adding one of
  656. the following lines anywhere in the buffer:
  657. #+STARTUP: latexpreview
  658. #+STARTUP: nolatexpreview"
  659. :group 'org-startup
  660. :version "24.4"
  661. :package-version '(Org . "8.0")
  662. :type 'boolean)
  663. (defcustom org-insert-mode-line-in-empty-file nil
  664. "Non-nil means insert the first line setting Org-mode in empty files.
  665. When the function `org-mode' is called interactively in an empty file, this
  666. normally means that the file name does not automatically trigger Org-mode.
  667. To ensure that the file will always be in Org-mode in the future, a
  668. line enforcing Org-mode will be inserted into the buffer, if this option
  669. has been set."
  670. :group 'org-startup
  671. :type 'boolean)
  672. (defcustom org-replace-disputed-keys nil
  673. "Non-nil means use alternative key bindings for some keys.
  674. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  675. These keys are also used by other packages like shift-selection-mode'
  676. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  677. If you want to use Org-mode together with one of these other modes,
  678. or more generally if you would like to move some Org-mode commands to
  679. other keys, set this variable and configure the keys with the variable
  680. `org-disputed-keys'.
  681. This option is only relevant at load-time of Org-mode, and must be set
  682. *before* org.el is loaded. Changing it requires a restart of Emacs to
  683. become effective."
  684. :group 'org-startup
  685. :type 'boolean)
  686. (defcustom org-use-extra-keys nil
  687. "Non-nil means use extra key sequence definitions for certain commands.
  688. This happens automatically if you run XEmacs or if `window-system'
  689. is nil. This variable lets you do the same manually. You must
  690. set it before loading org.
  691. Example: on Carbon Emacs 22 running graphically, with an external
  692. keyboard on a Powerbook, the default way of setting M-left might
  693. not work for either Alt or ESC. Setting this variable will make
  694. it work for ESC."
  695. :group 'org-startup
  696. :type 'boolean)
  697. (org-defvaralias 'org-CUA-compatible 'org-replace-disputed-keys)
  698. (defcustom org-disputed-keys
  699. '(([(shift up)] . [(meta p)])
  700. ([(shift down)] . [(meta n)])
  701. ([(shift left)] . [(meta -)])
  702. ([(shift right)] . [(meta +)])
  703. ([(control shift right)] . [(meta shift +)])
  704. ([(control shift left)] . [(meta shift -)]))
  705. "Keys for which Org-mode and other modes compete.
  706. This is an alist, cars are the default keys, second element specifies
  707. the alternative to use when `org-replace-disputed-keys' is t.
  708. Keys can be specified in any syntax supported by `define-key'.
  709. The value of this option takes effect only at Org-mode's startup,
  710. therefore you'll have to restart Emacs to apply it after changing."
  711. :group 'org-startup
  712. :type 'alist)
  713. (defun org-key (key)
  714. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  715. Or return the original if not disputed.
  716. Also apply the translations defined in `org-xemacs-key-equivalents'."
  717. (when org-replace-disputed-keys
  718. (let* ((nkey (key-description key))
  719. (x (org-find-if (lambda (x)
  720. (equal (key-description (car x)) nkey))
  721. org-disputed-keys)))
  722. (setq key (if x (cdr x) key))))
  723. (when (featurep 'xemacs)
  724. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  725. key)
  726. (defun org-find-if (predicate seq)
  727. (catch 'exit
  728. (while seq
  729. (if (funcall predicate (car seq))
  730. (throw 'exit (car seq))
  731. (pop seq)))))
  732. (defun org-defkey (keymap key def)
  733. "Define a key, possibly translated, as returned by `org-key'."
  734. (define-key keymap (org-key key) def))
  735. (defcustom org-ellipsis nil
  736. "The ellipsis to use in the Org-mode outline.
  737. When nil, just use the standard three dots.
  738. When a string, use that string instead.
  739. When a face, use the standard 3 dots, but with the specified face.
  740. The change affects only Org-mode (which will then use its own display table).
  741. Changing this requires executing `M-x org-mode RET' in a buffer to become
  742. effective."
  743. :group 'org-startup
  744. :type '(choice (const :tag "Default" nil)
  745. (face :tag "Face" :value org-warning)
  746. (string :tag "String" :value "...#")))
  747. (defvar org-display-table nil
  748. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  749. (defgroup org-keywords nil
  750. "Keywords in Org-mode."
  751. :tag "Org Keywords"
  752. :group 'org)
  753. (defcustom org-deadline-string "DEADLINE:"
  754. "String to mark deadline entries.
  755. A deadline is this string, followed by a time stamp. Should be a word,
  756. terminated by a colon. You can insert a schedule keyword and
  757. a timestamp with \\[org-deadline].
  758. Changes become only effective after restarting Emacs."
  759. :group 'org-keywords
  760. :type 'string)
  761. (defcustom org-scheduled-string "SCHEDULED:"
  762. "String to mark scheduled TODO entries.
  763. A schedule is this string, followed by a time stamp. Should be a word,
  764. terminated by a colon. You can insert a schedule keyword and
  765. a timestamp with \\[org-schedule].
  766. Changes become only effective after restarting Emacs."
  767. :group 'org-keywords
  768. :type 'string)
  769. (defcustom org-closed-string "CLOSED:"
  770. "String used as the prefix for timestamps logging closing a TODO entry."
  771. :group 'org-keywords
  772. :type 'string)
  773. (defcustom org-clock-string "CLOCK:"
  774. "String used as prefix for timestamps clocking work hours on an item."
  775. :group 'org-keywords
  776. :type 'string)
  777. (defcustom org-closed-keep-when-no-todo nil
  778. "Remove CLOSED: time-stamp when switching back to a non-todo state?"
  779. :group 'org-todo
  780. :group 'org-keywords
  781. :version "24.4"
  782. :package-version '(Org . "8.0")
  783. :type 'boolean)
  784. (defconst org-planning-or-clock-line-re (concat "^[ \t]*\\("
  785. org-scheduled-string "\\|"
  786. org-deadline-string "\\|"
  787. org-closed-string "\\|"
  788. org-clock-string "\\)")
  789. "Matches a line with planning or clock info.")
  790. (defcustom org-comment-string "COMMENT"
  791. "Entries starting with this keyword will never be exported.
  792. An entry can be toggled between COMMENT and normal with
  793. \\[org-toggle-comment].
  794. Changes become only effective after restarting Emacs."
  795. :group 'org-keywords
  796. :type 'string)
  797. (defcustom org-quote-string "QUOTE"
  798. "Entries starting with this keyword will be exported in fixed-width font.
  799. Quoting applies only to the text in the entry following the headline, and does
  800. not extend beyond the next headline, even if that is lower level.
  801. An entry can be toggled between QUOTE and normal with
  802. \\[org-toggle-fixed-width-section]."
  803. :group 'org-keywords
  804. :type 'string)
  805. (defconst org-repeat-re
  806. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)"
  807. "Regular expression for specifying repeated events.
  808. After a match, group 1 contains the repeat expression.")
  809. (defgroup org-structure nil
  810. "Options concerning the general structure of Org-mode files."
  811. :tag "Org Structure"
  812. :group 'org)
  813. (defgroup org-reveal-location nil
  814. "Options about how to make context of a location visible."
  815. :tag "Org Reveal Location"
  816. :group 'org-structure)
  817. (defconst org-context-choice
  818. '(choice
  819. (const :tag "Always" t)
  820. (const :tag "Never" nil)
  821. (repeat :greedy t :tag "Individual contexts"
  822. (cons
  823. (choice :tag "Context"
  824. (const agenda)
  825. (const org-goto)
  826. (const occur-tree)
  827. (const tags-tree)
  828. (const link-search)
  829. (const mark-goto)
  830. (const bookmark-jump)
  831. (const isearch)
  832. (const default))
  833. (boolean))))
  834. "Contexts for the reveal options.")
  835. (defcustom org-show-hierarchy-above '((default . t))
  836. "Non-nil means show full hierarchy when revealing a location.
  837. Org-mode often shows locations in an org-mode file which might have
  838. been invisible before. When this is set, the hierarchy of headings
  839. above the exposed location is shown.
  840. Turning this off for example for sparse trees makes them very compact.
  841. Instead of t, this can also be an alist specifying this option for different
  842. contexts. Valid contexts are
  843. agenda when exposing an entry from the agenda
  844. org-goto when using the command `org-goto' on key C-c C-j
  845. occur-tree when using the command `org-occur' on key C-c /
  846. tags-tree when constructing a sparse tree based on tags matches
  847. link-search when exposing search matches associated with a link
  848. mark-goto when exposing the jump goal of a mark
  849. bookmark-jump when exposing a bookmark location
  850. isearch when exiting from an incremental search
  851. default default for all contexts not set explicitly"
  852. :group 'org-reveal-location
  853. :type org-context-choice)
  854. (defcustom org-show-following-heading '((default . nil))
  855. "Non-nil means show following heading when revealing a location.
  856. Org-mode often shows locations in an org-mode file which might have
  857. been invisible before. When this is set, the heading following the
  858. match is shown.
  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. (defcustom org-show-siblings '((default . nil) (isearch t) (bookmark-jump t))
  867. "Non-nil means show all sibling heading when revealing a location.
  868. Org-mode often shows locations in an org-mode file which might have
  869. been invisible before. When this is set, the sibling of the current entry
  870. heading are all made visible. If `org-show-hierarchy-above' is t,
  871. the same happens on each level of the hierarchy above the current entry.
  872. By default this is on for the isearch context, off for all other contexts.
  873. Turning this off for example for sparse trees makes them very compact,
  874. but makes it harder to edit the location of the match. In such a case,
  875. use the command \\[org-reveal] to show more context.
  876. Instead of t, this can also be an alist specifying this option for different
  877. contexts. See `org-show-hierarchy-above' for valid contexts."
  878. :group 'org-reveal-location
  879. :type org-context-choice
  880. :version "24.4"
  881. :package-version '(Org . "8.0"))
  882. (defcustom org-show-entry-below '((default . nil))
  883. "Non-nil means show the entry below a headline when revealing a location.
  884. Org-mode often shows locations in an org-mode file which might have
  885. been invisible before. When this is set, the text below the headline that is
  886. exposed is also shown.
  887. By default this is off for all contexts.
  888. Instead of t, this can also be an alist specifying this option for different
  889. contexts. See `org-show-hierarchy-above' for valid contexts."
  890. :group 'org-reveal-location
  891. :type org-context-choice)
  892. (defcustom org-indirect-buffer-display 'other-window
  893. "How should indirect tree buffers be displayed?
  894. This applies to indirect buffers created with the commands
  895. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  896. Valid values are:
  897. current-window Display in the current window
  898. other-window Just display in another window.
  899. dedicated-frame Create one new frame, and re-use it each time.
  900. new-frame Make a new frame each time. Note that in this case
  901. previously-made indirect buffers are kept, and you need to
  902. kill these buffers yourself."
  903. :group 'org-structure
  904. :group 'org-agenda-windows
  905. :type '(choice
  906. (const :tag "In current window" current-window)
  907. (const :tag "In current frame, other window" other-window)
  908. (const :tag "Each time a new frame" new-frame)
  909. (const :tag "One dedicated frame" dedicated-frame)))
  910. (defcustom org-use-speed-commands nil
  911. "Non-nil means activate single letter commands at beginning of a headline.
  912. This may also be a function to test for appropriate locations where speed
  913. commands should be active."
  914. :group 'org-structure
  915. :type '(choice
  916. (const :tag "Never" nil)
  917. (const :tag "At beginning of headline stars" t)
  918. (function)))
  919. (defcustom org-speed-commands-user nil
  920. "Alist of additional speed commands.
  921. This list will be checked before `org-speed-commands-default'
  922. when the variable `org-use-speed-commands' is non-nil
  923. and when the cursor is at the beginning of a headline.
  924. The car if each entry is a string with a single letter, which must
  925. be assigned to `self-insert-command' in the global map.
  926. The cdr is either a command to be called interactively, a function
  927. to be called, or a form to be evaluated.
  928. An entry that is just a list with a single string will be interpreted
  929. as a descriptive headline that will be added when listing the speed
  930. commands in the Help buffer using the `?' speed command."
  931. :group 'org-structure
  932. :type '(repeat :value ("k" . ignore)
  933. (choice :value ("k" . ignore)
  934. (list :tag "Descriptive Headline" (string :tag "Headline"))
  935. (cons :tag "Letter and Command"
  936. (string :tag "Command letter")
  937. (choice
  938. (function)
  939. (sexp))))))
  940. (defcustom org-bookmark-names-plist
  941. '(:last-capture "org-capture-last-stored"
  942. :last-refile "org-refile-last-stored"
  943. :last-capture-marker "org-capture-last-stored-marker")
  944. "Names for bookmarks automatically set by some Org commands.
  945. This can provide strings as names for a number of bookmarks Org sets
  946. automatically. The following keys are currently implemented:
  947. :last-capture
  948. :last-capture-marker
  949. :last-refile
  950. When a key does not show up in the property list, the corresponding bookmark
  951. is not set."
  952. :group 'org-structure
  953. :type 'plist)
  954. (defgroup org-cycle nil
  955. "Options concerning visibility cycling in Org-mode."
  956. :tag "Org Cycle"
  957. :group 'org-structure)
  958. (defcustom org-cycle-skip-children-state-if-no-children t
  959. "Non-nil means skip CHILDREN state in entries that don't have any."
  960. :group 'org-cycle
  961. :type 'boolean)
  962. (defcustom org-cycle-max-level nil
  963. "Maximum level which should still be subject to visibility cycling.
  964. Levels higher than this will, for cycling, be treated as text, not a headline.
  965. When `org-odd-levels-only' is set, a value of N in this variable actually
  966. means 2N-1 stars as the limiting headline.
  967. When nil, cycle all levels.
  968. Note that the limiting level of cycling is also influenced by
  969. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  970. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  971. than its value."
  972. :group 'org-cycle
  973. :type '(choice
  974. (const :tag "No limit" nil)
  975. (integer :tag "Maximum level")))
  976. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK" "RESULTS")
  977. "Names of drawers. Drawers are not opened by cycling on the headline above.
  978. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  979. this:
  980. :DRAWERNAME:
  981. .....
  982. :END:
  983. The drawer \"PROPERTIES\" is special for capturing properties through
  984. the property API.
  985. Drawers can be defined on the per-file basis with a line like:
  986. #+DRAWERS: HIDDEN STATE PROPERTIES"
  987. :group 'org-structure
  988. :group 'org-cycle
  989. :type '(repeat (string :tag "Drawer Name")))
  990. (defcustom org-hide-block-startup nil
  991. "Non-nil means entering Org-mode will fold all blocks.
  992. This can also be set in on a per-file basis with
  993. #+STARTUP: hideblocks
  994. #+STARTUP: showblocks"
  995. :group 'org-startup
  996. :group 'org-cycle
  997. :type 'boolean)
  998. (defcustom org-cycle-global-at-bob nil
  999. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  1000. This makes it possible to do global cycling without having to use S-TAB or
  1001. \\[universal-argument] TAB. For this special case to work, the first line
  1002. of the buffer must not be a headline -- it may be empty or some other text.
  1003. When used in this way, `org-cycle-hook' is disabled temporarily to make
  1004. sure the cursor stays at the beginning of the buffer. When this option is
  1005. nil, don't do anything special at the beginning of the buffer."
  1006. :group 'org-cycle
  1007. :type 'boolean)
  1008. (defcustom org-cycle-level-after-item/entry-creation t
  1009. "Non-nil means cycle entry level or item indentation in new empty entries.
  1010. When the cursor is at the end of an empty headline, i.e., with only stars
  1011. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  1012. and then all possible ancestor states, before returning to the original state.
  1013. This makes data entry extremely fast: M-RET to create a new headline,
  1014. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  1015. When the cursor is at the end of an empty plain list item, one TAB will
  1016. make it a subitem, two or more tabs will back up to make this an item
  1017. higher up in the item hierarchy."
  1018. :group 'org-cycle
  1019. :type 'boolean)
  1020. (defcustom org-cycle-emulate-tab t
  1021. "Where should `org-cycle' emulate TAB.
  1022. nil Never
  1023. white Only in completely white lines
  1024. whitestart Only at the beginning of lines, before the first non-white char
  1025. t Everywhere except in headlines
  1026. exc-hl-bol Everywhere except at the start of a headline
  1027. If TAB is used in a place where it does not emulate TAB, the current subtree
  1028. visibility is cycled."
  1029. :group 'org-cycle
  1030. :type '(choice (const :tag "Never" nil)
  1031. (const :tag "Only in completely white lines" white)
  1032. (const :tag "Before first char in a line" whitestart)
  1033. (const :tag "Everywhere except in headlines" t)
  1034. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)))
  1035. (defcustom org-cycle-separator-lines 2
  1036. "Number of empty lines needed to keep an empty line between collapsed trees.
  1037. If you leave an empty line between the end of a subtree and the following
  1038. headline, this empty line is hidden when the subtree is folded.
  1039. Org-mode will leave (exactly) one empty line visible if the number of
  1040. empty lines is equal or larger to the number given in this variable.
  1041. So the default 2 means at least 2 empty lines after the end of a subtree
  1042. are needed to produce free space between a collapsed subtree and the
  1043. following headline.
  1044. If the number is negative, and the number of empty lines is at least -N,
  1045. all empty lines are shown.
  1046. Special case: when 0, never leave empty lines in collapsed view."
  1047. :group 'org-cycle
  1048. :type 'integer)
  1049. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  1050. (defcustom org-pre-cycle-hook nil
  1051. "Hook that is run before visibility cycling is happening.
  1052. The function(s) in this hook must accept a single argument which indicates
  1053. the new state that will be set right after running this hook. The
  1054. argument is a symbol. Before a global state change, it can have the values
  1055. `overview', `content', or `all'. Before a local state change, it can have
  1056. the values `folded', `children', or `subtree'."
  1057. :group 'org-cycle
  1058. :type 'hook)
  1059. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  1060. org-cycle-hide-drawers
  1061. org-cycle-hide-inline-tasks
  1062. org-cycle-show-empty-lines
  1063. org-optimize-window-after-visibility-change)
  1064. "Hook that is run after `org-cycle' has changed the buffer visibility.
  1065. The function(s) in this hook must accept a single argument which indicates
  1066. the new state that was set by the most recent `org-cycle' command. The
  1067. argument is a symbol. After a global state change, it can have the values
  1068. `overview', `contents', or `all'. After a local state change, it can have
  1069. the values `folded', `children', or `subtree'."
  1070. :group 'org-cycle
  1071. :type 'hook)
  1072. (defgroup org-edit-structure nil
  1073. "Options concerning structure editing in Org-mode."
  1074. :tag "Org Edit Structure"
  1075. :group 'org-structure)
  1076. (defcustom org-odd-levels-only nil
  1077. "Non-nil means skip even levels and only use odd levels for the outline.
  1078. This has the effect that two stars are being added/taken away in
  1079. promotion/demotion commands. It also influences how levels are
  1080. handled by the exporters.
  1081. Changing it requires restart of `font-lock-mode' to become effective
  1082. for fontification also in regions already fontified.
  1083. You may also set this on a per-file basis by adding one of the following
  1084. lines to the buffer:
  1085. #+STARTUP: odd
  1086. #+STARTUP: oddeven"
  1087. :group 'org-edit-structure
  1088. :group 'org-appearance
  1089. :type 'boolean)
  1090. (defcustom org-adapt-indentation t
  1091. "Non-nil means adapt indentation to outline node level.
  1092. When this variable is set, Org assumes that you write outlines by
  1093. indenting text in each node to align with the headline (after the stars).
  1094. The following issues are influenced by this variable:
  1095. - When this is set and the *entire* text in an entry is indented, the
  1096. indentation is increased by one space in a demotion command, and
  1097. decreased by one in a promotion command. If any line in the entry
  1098. body starts with text at column 0, indentation is not changed at all.
  1099. - Property drawers and planning information is inserted indented when
  1100. this variable is set. When nil, they will not be indented.
  1101. - TAB indents a line relative to context. The lines below a headline
  1102. will be indented when this variable is set.
  1103. Note that this is all about true indentation, by adding and removing
  1104. space characters. See also `org-indent.el' which does level-dependent
  1105. indentation in a virtual way, i.e. at display time in Emacs."
  1106. :group 'org-edit-structure
  1107. :type 'boolean)
  1108. (defcustom org-special-ctrl-a/e nil
  1109. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  1110. When t, `C-a' will bring back the cursor to the beginning of the
  1111. headline text, i.e. after the stars and after a possible TODO
  1112. keyword. In an item, this will be the position after bullet and
  1113. check-box, if any. When the cursor is already at that position,
  1114. another `C-a' will bring it to the beginning of the line.
  1115. `C-e' will jump to the end of the headline, ignoring the presence
  1116. of tags in the headline. A second `C-e' will then jump to the
  1117. true end of the line, after any tags. This also means that, when
  1118. this variable is non-nil, `C-e' also will never jump beyond the
  1119. end of the heading of a folded section, i.e. not after the
  1120. ellipses.
  1121. When set to the symbol `reversed', the first `C-a' or `C-e' works
  1122. normally, going to the true line boundary first. Only a directly
  1123. following, identical keypress will bring the cursor to the
  1124. special positions.
  1125. This may also be a cons cell where the behavior for `C-a' and
  1126. `C-e' is set separately."
  1127. :group 'org-edit-structure
  1128. :type '(choice
  1129. (const :tag "off" nil)
  1130. (const :tag "on: after stars/bullet and before tags first" t)
  1131. (const :tag "reversed: true line boundary first" reversed)
  1132. (cons :tag "Set C-a and C-e separately"
  1133. (choice :tag "Special C-a"
  1134. (const :tag "off" nil)
  1135. (const :tag "on: after stars/bullet first" t)
  1136. (const :tag "reversed: before stars/bullet first" reversed))
  1137. (choice :tag "Special C-e"
  1138. (const :tag "off" nil)
  1139. (const :tag "on: before tags first" t)
  1140. (const :tag "reversed: after tags first" reversed)))))
  1141. (org-defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e)
  1142. (defcustom org-special-ctrl-k nil
  1143. "Non-nil means `C-k' will behave specially in headlines.
  1144. When nil, `C-k' will call the default `kill-line' command.
  1145. When t, the following will happen while the cursor is in the headline:
  1146. - When the cursor is at the beginning of a headline, kill the entire
  1147. line and possible the folded subtree below the line.
  1148. - When in the middle of the headline text, kill the headline up to the tags.
  1149. - When after the headline text, kill the tags."
  1150. :group 'org-edit-structure
  1151. :type 'boolean)
  1152. (defcustom org-ctrl-k-protect-subtree nil
  1153. "Non-nil means, do not delete a hidden subtree with C-k.
  1154. When set to the symbol `error', simply throw an error when C-k is
  1155. used to kill (part-of) a headline that has hidden text behind it.
  1156. Any other non-nil value will result in a query to the user, if it is
  1157. OK to kill that hidden subtree. When nil, kill without remorse."
  1158. :group 'org-edit-structure
  1159. :version "24.1"
  1160. :type '(choice
  1161. (const :tag "Do not protect hidden subtrees" nil)
  1162. (const :tag "Protect hidden subtrees with a security query" t)
  1163. (const :tag "Never kill a hidden subtree with C-k" error)))
  1164. (defcustom org-special-ctrl-o t
  1165. "Non-nil means, make `C-o' insert a row in tables."
  1166. :group 'org-edit-structure
  1167. :type 'boolean)
  1168. (defcustom org-catch-invisible-edits nil
  1169. "Check if in invisible region before inserting or deleting a character.
  1170. Valid values are:
  1171. nil Do not check, so just do invisible edits.
  1172. error Throw an error and do nothing.
  1173. show Make point visible, and do the requested edit.
  1174. show-and-error Make point visible, then throw an error and abort the edit.
  1175. smart Make point visible, and do insertion/deletion if it is
  1176. adjacent to visible text and the change feels predictable.
  1177. Never delete a previously invisible character or add in the
  1178. middle or right after an invisible region. Basically, this
  1179. allows insertion and backward-delete right before ellipses.
  1180. FIXME: maybe in this case we should not even show?"
  1181. :group 'org-edit-structure
  1182. :version "24.1"
  1183. :type '(choice
  1184. (const :tag "Do not check" nil)
  1185. (const :tag "Throw error when trying to edit" error)
  1186. (const :tag "Unhide, but do not do the edit" show-and-error)
  1187. (const :tag "Show invisible part and do the edit" show)
  1188. (const :tag "Be smart and do the right thing" smart)))
  1189. (defcustom org-yank-folded-subtrees t
  1190. "Non-nil means when yanking subtrees, fold them.
  1191. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  1192. it starts with a heading and all other headings in it are either children
  1193. or siblings, then fold all the subtrees. However, do this only if no
  1194. text after the yank would be swallowed into a folded tree by this action."
  1195. :group 'org-edit-structure
  1196. :type 'boolean)
  1197. (defcustom org-yank-adjusted-subtrees nil
  1198. "Non-nil means when yanking subtrees, adjust the level.
  1199. With this setting, `org-paste-subtree' is used to insert the subtree, see
  1200. this function for details."
  1201. :group 'org-edit-structure
  1202. :type 'boolean)
  1203. (defcustom org-M-RET-may-split-line '((default . t))
  1204. "Non-nil means M-RET will split the line at the cursor position.
  1205. When nil, it will go to the end of the line before making a
  1206. new line.
  1207. You may also set this option in a different way for different
  1208. contexts. Valid contexts are:
  1209. headline when creating a new headline
  1210. item when creating a new item
  1211. table in a table field
  1212. default the value to be used for all contexts not explicitly
  1213. customized"
  1214. :group 'org-structure
  1215. :group 'org-table
  1216. :type '(choice
  1217. (const :tag "Always" t)
  1218. (const :tag "Never" nil)
  1219. (repeat :greedy t :tag "Individual contexts"
  1220. (cons
  1221. (choice :tag "Context"
  1222. (const headline)
  1223. (const item)
  1224. (const table)
  1225. (const default))
  1226. (boolean)))))
  1227. (defcustom org-insert-heading-respect-content nil
  1228. "Non-nil means insert new headings after the current subtree.
  1229. When nil, the new heading is created directly after the current line.
  1230. The commands \\[org-insert-heading-respect-content] and \\[org-insert-todo-heading-respect-content] turn
  1231. this variable on for the duration of the command."
  1232. :group 'org-structure
  1233. :type 'boolean)
  1234. (defcustom org-blank-before-new-entry '((heading . auto)
  1235. (plain-list-item . auto))
  1236. "Should `org-insert-heading' leave a blank line before new heading/item?
  1237. The value is an alist, with `heading' and `plain-list-item' as CAR,
  1238. and a boolean flag as CDR. The cdr may also be the symbol `auto', in
  1239. which case Org will look at the surrounding headings/items and try to
  1240. make an intelligent decision whether to insert a blank line or not.
  1241. For plain lists, if `org-list-empty-line-terminates-plain-lists' is set,
  1242. the setting here is ignored and no empty line is inserted to avoid breaking
  1243. the list structure."
  1244. :group 'org-edit-structure
  1245. :type '(list
  1246. (cons (const heading)
  1247. (choice (const :tag "Never" nil)
  1248. (const :tag "Always" t)
  1249. (const :tag "Auto" auto)))
  1250. (cons (const plain-list-item)
  1251. (choice (const :tag "Never" nil)
  1252. (const :tag "Always" t)
  1253. (const :tag "Auto" auto)))))
  1254. (defcustom org-insert-heading-hook nil
  1255. "Hook being run after inserting a new heading."
  1256. :group 'org-edit-structure
  1257. :type 'hook)
  1258. (defcustom org-enable-fixed-width-editor t
  1259. "Non-nil means lines starting with \":\" are treated as fixed-width.
  1260. This currently only means they are never auto-wrapped.
  1261. When nil, such lines will be treated like ordinary lines.
  1262. See also the QUOTE keyword."
  1263. :group 'org-edit-structure
  1264. :type 'boolean)
  1265. (defcustom org-goto-auto-isearch t
  1266. "Non-nil means typing characters in `org-goto' starts incremental search.
  1267. When nil, you can use these keybindings to navigate the buffer:
  1268. q Quit the org-goto interface
  1269. n Go to the next visible heading
  1270. p Go to the previous visible heading
  1271. f Go one heading forward on same level
  1272. b Go one heading backward on same level
  1273. u Go one heading up"
  1274. :group 'org-edit-structure
  1275. :type 'boolean)
  1276. (defgroup org-sparse-trees nil
  1277. "Options concerning sparse trees in Org-mode."
  1278. :tag "Org Sparse Trees"
  1279. :group 'org-structure)
  1280. (defcustom org-highlight-sparse-tree-matches t
  1281. "Non-nil means highlight all matches that define a sparse tree.
  1282. The highlights will automatically disappear the next time the buffer is
  1283. changed by an edit command."
  1284. :group 'org-sparse-trees
  1285. :type 'boolean)
  1286. (defcustom org-remove-highlights-with-change t
  1287. "Non-nil means any change to the buffer will remove temporary highlights.
  1288. Such highlights are created by `org-occur' and `org-clock-display'.
  1289. When nil, `C-c C-c' needs to be used to get rid of the highlights.
  1290. The highlights created by `org-preview-latex-fragment' always need
  1291. `C-c C-c' to be removed."
  1292. :group 'org-sparse-trees
  1293. :group 'org-time
  1294. :type 'boolean)
  1295. (defcustom org-occur-hook '(org-first-headline-recenter)
  1296. "Hook that is run after `org-occur' has constructed a sparse tree.
  1297. This can be used to recenter the window to show as much of the structure
  1298. as possible."
  1299. :group 'org-sparse-trees
  1300. :type 'hook)
  1301. (defgroup org-imenu-and-speedbar nil
  1302. "Options concerning imenu and speedbar in Org-mode."
  1303. :tag "Org Imenu and Speedbar"
  1304. :group 'org-structure)
  1305. (defcustom org-imenu-depth 2
  1306. "The maximum level for Imenu access to Org-mode headlines.
  1307. This also applied for speedbar access."
  1308. :group 'org-imenu-and-speedbar
  1309. :type 'integer)
  1310. (defgroup org-table nil
  1311. "Options concerning tables in Org-mode."
  1312. :tag "Org Table"
  1313. :group 'org)
  1314. (defcustom org-enable-table-editor 'optimized
  1315. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1316. When nil, such lines will be treated like ordinary lines.
  1317. When equal to the symbol `optimized', the table editor will be optimized to
  1318. do the following:
  1319. - Automatic overwrite mode in front of whitespace in table fields.
  1320. This makes the structure of the table stay in tact as long as the edited
  1321. field does not exceed the column width.
  1322. - Minimize the number of realigns. Normally, the table is aligned each time
  1323. TAB or RET are pressed to move to another field. With optimization this
  1324. happens only if changes to a field might have changed the column width.
  1325. Optimization requires replacing the functions `self-insert-command',
  1326. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1327. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1328. very good at guessing when a re-align will be necessary, but you can always
  1329. force one with \\[org-ctrl-c-ctrl-c].
  1330. If you would like to use the optimized version in Org-mode, but the
  1331. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1332. This variable can be used to turn on and off the table editor during a session,
  1333. but in order to toggle optimization, a restart is required.
  1334. See also the variable `org-table-auto-blank-field'."
  1335. :group 'org-table
  1336. :type '(choice
  1337. (const :tag "off" nil)
  1338. (const :tag "on" t)
  1339. (const :tag "on, optimized" optimized)))
  1340. (defcustom org-self-insert-cluster-for-undo (or (featurep 'xemacs)
  1341. (version<= emacs-version "24.1"))
  1342. "Non-nil means cluster self-insert commands for undo when possible.
  1343. If this is set, then, like in the Emacs command loop, 20 consecutive
  1344. characters will be undone together.
  1345. This is configurable, because there is some impact on typing performance."
  1346. :group 'org-table
  1347. :type 'boolean)
  1348. (defcustom org-table-tab-recognizes-table.el t
  1349. "Non-nil means TAB will automatically notice a table.el table.
  1350. When it sees such a table, it moves point into it and - if necessary -
  1351. calls `table-recognize-table'."
  1352. :group 'org-table-editing
  1353. :type 'boolean)
  1354. (defgroup org-link nil
  1355. "Options concerning links in Org-mode."
  1356. :tag "Org Link"
  1357. :group 'org)
  1358. (defvar org-link-abbrev-alist-local nil
  1359. "Buffer-local version of `org-link-abbrev-alist', which see.
  1360. The value of this is taken from the #+LINK lines.")
  1361. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1362. (defcustom org-link-abbrev-alist nil
  1363. "Alist of link abbreviations.
  1364. The car of each element is a string, to be replaced at the start of a link.
  1365. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1366. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1367. [[linkkey:tag][description]]
  1368. The 'linkkey' must be a word word, starting with a letter, followed
  1369. by letters, numbers, '-' or '_'.
  1370. If REPLACE is a string, the tag will simply be appended to create the link.
  1371. If the string contains \"%s\", the tag will be inserted there. If the string
  1372. contains \"%h\", it will cause a url-encoded version of the tag to be inserted
  1373. at that point (see the function `url-hexify-string'). If the string contains
  1374. the specifier \"%(my-function)\", then the custom function `my-function' will
  1375. be invoked: this function takes the tag as its only argument and must return
  1376. a string.
  1377. REPLACE may also be a function that will be called with the tag as the
  1378. only argument to create the link, which should be returned as a string.
  1379. See the manual for examples."
  1380. :group 'org-link
  1381. :type '(repeat
  1382. (cons
  1383. (string :tag "Protocol")
  1384. (choice
  1385. (string :tag "Format")
  1386. (function)))))
  1387. (defcustom org-descriptive-links t
  1388. "Non-nil means Org will display descriptive links.
  1389. E.g. [[http://orgmode.org][Org website]] will be displayed as
  1390. \"Org Website\", hiding the link itself and just displaying its
  1391. description. When set to `nil', Org will display the full links
  1392. literally.
  1393. You can interactively set the value of this variable by calling
  1394. `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
  1395. :group 'org-link
  1396. :type 'boolean)
  1397. (defcustom org-link-file-path-type 'adaptive
  1398. "How the path name in file links should be stored.
  1399. Valid values are:
  1400. relative Relative to the current directory, i.e. the directory of the file
  1401. into which the link is being inserted.
  1402. absolute Absolute path, if possible with ~ for home directory.
  1403. noabbrev Absolute path, no abbreviation of home directory.
  1404. adaptive Use relative path for files in the current directory and sub-
  1405. directories of it. For other files, use an absolute path."
  1406. :group 'org-link
  1407. :type '(choice
  1408. (const relative)
  1409. (const absolute)
  1410. (const noabbrev)
  1411. (const adaptive)))
  1412. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  1413. "Types of links that should be activated in Org-mode files.
  1414. This is a list of symbols, each leading to the activation of a certain link
  1415. type. In principle, it does not hurt to turn on most link types - there may
  1416. be a small gain when turning off unused link types. The types are:
  1417. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1418. angle Links in angular brackets that may contain whitespace like
  1419. <bbdb:Carsten Dominik>.
  1420. plain Plain links in normal text, no whitespace, like http://google.com.
  1421. radio Text that is matched by a radio target, see manual for details.
  1422. tag Tag settings in a headline (link to tag search).
  1423. date Time stamps (link to calendar).
  1424. footnote Footnote labels.
  1425. Changing this variable requires a restart of Emacs to become effective."
  1426. :group 'org-link
  1427. :type '(set :greedy t
  1428. (const :tag "Double bracket links" bracket)
  1429. (const :tag "Angular bracket links" angle)
  1430. (const :tag "Plain text links" plain)
  1431. (const :tag "Radio target matches" radio)
  1432. (const :tag "Tags" tag)
  1433. (const :tag "Timestamps" date)
  1434. (const :tag "Footnotes" footnote)))
  1435. (defcustom org-make-link-description-function nil
  1436. "Function to use for generating link descriptions from links.
  1437. When nil, the link location will be used. This function must take
  1438. two parameters: the first one is the link, the second one is the
  1439. description generated by `org-insert-link'. The function should
  1440. return the description to use."
  1441. :group 'org-link
  1442. :type '(choice (const nil) (function)))
  1443. (defgroup org-link-store nil
  1444. "Options concerning storing links in Org-mode."
  1445. :tag "Org Store Link"
  1446. :group 'org-link)
  1447. (defcustom org-url-hexify-p t
  1448. "When non-nil, hexify URL when creating a link."
  1449. :type 'boolean
  1450. :version "24.3"
  1451. :group 'org-link-store)
  1452. (defcustom org-email-link-description-format "Email %c: %.30s"
  1453. "Format of the description part of a link to an email or usenet message.
  1454. The following %-escapes will be replaced by corresponding information:
  1455. %F full \"From\" field
  1456. %f name, taken from \"From\" field, address if no name
  1457. %T full \"To\" field
  1458. %t first name in \"To\" field, address if no name
  1459. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1460. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1461. %s subject
  1462. %d date
  1463. %m message-id.
  1464. You may use normal field width specification between the % and the letter.
  1465. This is for example useful to limit the length of the subject.
  1466. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1467. :group 'org-link-store
  1468. :type 'string)
  1469. (defcustom org-from-is-user-regexp
  1470. (let (r1 r2)
  1471. (when (and user-mail-address (not (string= user-mail-address "")))
  1472. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1473. (when (and user-full-name (not (string= user-full-name "")))
  1474. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1475. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1476. "Regexp matched against the \"From:\" header of an email or usenet message.
  1477. It should match if the message is from the user him/herself."
  1478. :group 'org-link-store
  1479. :type 'regexp)
  1480. (defcustom org-context-in-file-links t
  1481. "Non-nil means file links from `org-store-link' contain context.
  1482. A search string will be added to the file name with :: as separator and
  1483. used to find the context when the link is activated by the command
  1484. `org-open-at-point'. When this option is t, the entire active region
  1485. will be placed in the search string of the file link. If set to a
  1486. positive integer, only the first n lines of context will be stored.
  1487. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1488. negates this setting for the duration of the command."
  1489. :group 'org-link-store
  1490. :type '(choice boolean integer))
  1491. (defcustom org-keep-stored-link-after-insertion nil
  1492. "Non-nil means keep link in list for entire session.
  1493. The command `org-store-link' adds a link pointing to the current
  1494. location to an internal list. These links accumulate during a session.
  1495. The command `org-insert-link' can be used to insert links into any
  1496. Org-mode file (offering completion for all stored links). When this
  1497. option is nil, every link which has been inserted once using \\[org-insert-link]
  1498. will be removed from the list, to make completing the unused links
  1499. more efficient."
  1500. :group 'org-link-store
  1501. :type 'boolean)
  1502. (defgroup org-link-follow nil
  1503. "Options concerning following links in Org-mode."
  1504. :tag "Org Follow Link"
  1505. :group 'org-link)
  1506. (defcustom org-link-translation-function nil
  1507. "Function to translate links with different syntax to Org syntax.
  1508. This can be used to translate links created for example by the Planner
  1509. or emacs-wiki packages to Org syntax.
  1510. The function must accept two parameters, a TYPE containing the link
  1511. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1512. which is everything after the link protocol. It should return a cons
  1513. with possibly modified values of type and path.
  1514. Org contains a function for this, so if you set this variable to
  1515. `org-translate-link-from-planner', you should be able follow many
  1516. links created by planner."
  1517. :group 'org-link-follow
  1518. :type '(choice (const nil) (function)))
  1519. (defcustom org-follow-link-hook nil
  1520. "Hook that is run after a link has been followed."
  1521. :group 'org-link-follow
  1522. :type 'hook)
  1523. (defcustom org-tab-follows-link nil
  1524. "Non-nil means on links TAB will follow the link.
  1525. Needs to be set before org.el is loaded.
  1526. This really should not be used, it does not make sense, and the
  1527. implementation is bad."
  1528. :group 'org-link-follow
  1529. :type 'boolean)
  1530. (defcustom org-return-follows-link nil
  1531. "Non-nil means on links RET will follow the link.
  1532. In tables, the special behavior of RET has precedence."
  1533. :group 'org-link-follow
  1534. :type 'boolean)
  1535. (defcustom org-mouse-1-follows-link
  1536. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1537. "Non-nil means mouse-1 on a link will follow the link.
  1538. A longer mouse click will still set point. Does not work on XEmacs.
  1539. Needs to be set before org.el is loaded."
  1540. :group 'org-link-follow
  1541. :version "24.4"
  1542. :package-version '(Org . "8.3")
  1543. :type '(choice
  1544. (const :tag "A double click follows the link" double)
  1545. (const :tag "Unconditionally follow the link with mouse-1" t)
  1546. (integer :tag "mouse-1 click does not follow the link if longer than N ms" 450)))
  1547. (defcustom org-mark-ring-length 4
  1548. "Number of different positions to be recorded in the ring.
  1549. Changing this requires a restart of Emacs to work correctly."
  1550. :group 'org-link-follow
  1551. :type 'integer)
  1552. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1553. "Non-nil means internal links in Org files must exactly match a headline.
  1554. When nil, the link search tries to match a phrase with all words
  1555. in the search text."
  1556. :group 'org-link-follow
  1557. :version "24.1"
  1558. :type '(choice
  1559. (const :tag "Use fuzzy text search" nil)
  1560. (const :tag "Match only exact headline" t)
  1561. (const :tag "Match exact headline or query to create it"
  1562. query-to-create)))
  1563. (defcustom org-link-frame-setup
  1564. '((vm . vm-visit-folder-other-frame)
  1565. (vm-imap . vm-visit-imap-folder-other-frame)
  1566. (gnus . org-gnus-no-new-news)
  1567. (file . find-file-other-window)
  1568. (wl . wl-other-frame))
  1569. "Setup the frame configuration for following links.
  1570. When following a link with Emacs, it may often be useful to display
  1571. this link in another window or frame. This variable can be used to
  1572. set this up for the different types of links.
  1573. For VM, use any of
  1574. `vm-visit-folder'
  1575. `vm-visit-folder-other-window'
  1576. `vm-visit-folder-other-frame'
  1577. For Gnus, use any of
  1578. `gnus'
  1579. `gnus-other-frame'
  1580. `org-gnus-no-new-news'
  1581. For FILE, use any of
  1582. `find-file'
  1583. `find-file-other-window'
  1584. `find-file-other-frame'
  1585. For Wanderlust use any of
  1586. `wl'
  1587. `wl-other-frame'
  1588. For the calendar, use the variable `calendar-setup'.
  1589. For BBDB, it is currently only possible to display the matches in
  1590. another window."
  1591. :group 'org-link-follow
  1592. :type '(list
  1593. (cons (const vm)
  1594. (choice
  1595. (const vm-visit-folder)
  1596. (const vm-visit-folder-other-window)
  1597. (const vm-visit-folder-other-frame)))
  1598. (cons (const vm-imap)
  1599. (choice
  1600. (const vm-visit-imap-folder)
  1601. (const vm-visit-imap-folder-other-window)
  1602. (const vm-visit-imap-folder-other-frame)))
  1603. (cons (const gnus)
  1604. (choice
  1605. (const gnus)
  1606. (const gnus-other-frame)
  1607. (const org-gnus-no-new-news)))
  1608. (cons (const file)
  1609. (choice
  1610. (const find-file)
  1611. (const find-file-other-window)
  1612. (const find-file-other-frame)))
  1613. (cons (const wl)
  1614. (choice
  1615. (const wl)
  1616. (const wl-other-frame)))))
  1617. (defcustom org-display-internal-link-with-indirect-buffer nil
  1618. "Non-nil means use indirect buffer to display infile links.
  1619. Activating internal links (from one location in a file to another location
  1620. in the same file) normally just jumps to the location. When the link is
  1621. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1622. is displayed in
  1623. another window. When this option is set, the other window actually displays
  1624. an indirect buffer clone of the current buffer, to avoid any visibility
  1625. changes to the current buffer."
  1626. :group 'org-link-follow
  1627. :type 'boolean)
  1628. (defcustom org-open-non-existing-files nil
  1629. "Non-nil means `org-open-file' will open non-existing files.
  1630. When nil, an error will be generated.
  1631. This variable applies only to external applications because they
  1632. might choke on non-existing files. If the link is to a file that
  1633. will be opened in Emacs, the variable is ignored."
  1634. :group 'org-link-follow
  1635. :type 'boolean)
  1636. (defcustom org-open-directory-means-index-dot-org nil
  1637. "Non-nil means a link to a directory really means to index.org.
  1638. When nil, following a directory link will run dired or open a finder/explorer
  1639. window on that directory."
  1640. :group 'org-link-follow
  1641. :type 'boolean)
  1642. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1643. "Function and arguments to call for following mailto links.
  1644. This is a list with the first element being a Lisp function, and the
  1645. remaining elements being arguments to the function. In string arguments,
  1646. %a will be replaced by the address, and %s will be replaced by the subject
  1647. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1648. :group 'org-link-follow
  1649. :type '(choice
  1650. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1651. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1652. (const :tag "message-mail" (message-mail "%a" "%s"))
  1653. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1654. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1655. "Non-nil means ask for confirmation before executing shell links.
  1656. Shell links can be dangerous: just think about a link
  1657. [[shell:rm -rf ~/*][Google Search]]
  1658. This link would show up in your Org-mode document as \"Google Search\",
  1659. but really it would remove your entire home directory.
  1660. Therefore we advise against setting this variable to nil.
  1661. Just change it to `y-or-n-p' if you want to confirm with a
  1662. single keystroke rather than having to type \"yes\"."
  1663. :group 'org-link-follow
  1664. :type '(choice
  1665. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1666. (const :tag "with y-or-n (faster)" y-or-n-p)
  1667. (const :tag "no confirmation (dangerous)" nil)))
  1668. (put 'org-confirm-shell-link-function
  1669. 'safe-local-variable
  1670. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1671. (defcustom org-confirm-shell-link-not-regexp ""
  1672. "A regexp to skip confirmation for shell links."
  1673. :group 'org-link-follow
  1674. :version "24.1"
  1675. :type 'regexp)
  1676. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1677. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1678. Elisp links can be dangerous: just think about a link
  1679. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1680. This link would show up in your Org-mode document as \"Google Search\",
  1681. but really it would remove your entire home directory.
  1682. Therefore we advise against setting this variable to nil.
  1683. Just change it to `y-or-n-p' if you want to confirm with a
  1684. single keystroke rather than having to type \"yes\"."
  1685. :group 'org-link-follow
  1686. :type '(choice
  1687. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1688. (const :tag "with y-or-n (faster)" y-or-n-p)
  1689. (const :tag "no confirmation (dangerous)" nil)))
  1690. (put 'org-confirm-shell-link-function
  1691. 'safe-local-variable
  1692. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1693. (defcustom org-confirm-elisp-link-not-regexp ""
  1694. "A regexp to skip confirmation for Elisp links."
  1695. :group 'org-link-follow
  1696. :version "24.1"
  1697. :type 'regexp)
  1698. (defconst org-file-apps-defaults-gnu
  1699. '((remote . emacs)
  1700. (system . mailcap)
  1701. (t . mailcap))
  1702. "Default file applications on a UNIX or GNU/Linux system.
  1703. See `org-file-apps'.")
  1704. (defconst org-file-apps-defaults-macosx
  1705. '((remote . emacs)
  1706. (t . "open %s")
  1707. (system . "open %s")
  1708. ("ps.gz" . "gv %s")
  1709. ("eps.gz" . "gv %s")
  1710. ("dvi" . "xdvi %s")
  1711. ("fig" . "xfig %s"))
  1712. "Default file applications on a MacOS X system.
  1713. The system \"open\" is known as a default, but we use X11 applications
  1714. for some files for which the OS does not have a good default.
  1715. See `org-file-apps'.")
  1716. (defconst org-file-apps-defaults-windowsnt
  1717. (list
  1718. '(remote . emacs)
  1719. (cons t
  1720. (list (if (featurep 'xemacs)
  1721. 'mswindows-shell-execute
  1722. 'w32-shell-execute)
  1723. "open" 'file))
  1724. (cons 'system
  1725. (list (if (featurep 'xemacs)
  1726. 'mswindows-shell-execute
  1727. 'w32-shell-execute)
  1728. "open" 'file)))
  1729. "Default file applications on a Windows NT system.
  1730. The system \"open\" is used for most files.
  1731. See `org-file-apps'.")
  1732. (defcustom org-file-apps
  1733. '((auto-mode . emacs)
  1734. ("\\.mm\\'" . default)
  1735. ("\\.x?html?\\'" . default)
  1736. ("\\.pdf\\'" . default))
  1737. "External applications for opening `file:path' items in a document.
  1738. Org-mode uses system defaults for different file types, but
  1739. you can use this variable to set the application for a given file
  1740. extension. The entries in this list are cons cells where the car identifies
  1741. files and the cdr the corresponding command. Possible values for the
  1742. file identifier are
  1743. \"string\" A string as a file identifier can be interpreted in different
  1744. ways, depending on its contents:
  1745. - Alphanumeric characters only:
  1746. Match links with this file extension.
  1747. Example: (\"pdf\" . \"evince %s\")
  1748. to open PDFs with evince.
  1749. - Regular expression: Match links where the
  1750. filename matches the regexp. If you want to
  1751. use groups here, use shy groups.
  1752. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1753. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1754. to open *.html and *.xhtml with firefox.
  1755. - Regular expression which contains (non-shy) groups:
  1756. Match links where the whole link, including \"::\", and
  1757. anything after that, matches the regexp.
  1758. In a custom command string, %1, %2, etc. are replaced with
  1759. the parts of the link that were matched by the groups.
  1760. For backwards compatibility, if a command string is given
  1761. that does not use any of the group matches, this case is
  1762. handled identically to the second one (i.e. match against
  1763. file name only).
  1764. In a custom lisp form, you can access the group matches with
  1765. (match-string n link).
  1766. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1767. to open [[file:document.pdf::5]] with evince at page 5.
  1768. `directory' Matches a directory
  1769. `remote' Matches a remote file, accessible through tramp or efs.
  1770. Remote files most likely should be visited through Emacs
  1771. because external applications cannot handle such paths.
  1772. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1773. so all files Emacs knows how to handle. Using this with
  1774. command `emacs' will open most files in Emacs. Beware that this
  1775. will also open html files inside Emacs, unless you add
  1776. (\"html\" . default) to the list as well.
  1777. t Default for files not matched by any of the other options.
  1778. `system' The system command to open files, like `open' on Windows
  1779. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1780. that will be selected if you call `C-c C-o' with a double
  1781. \\[universal-argument] \\[universal-argument] prefix.
  1782. Possible values for the command are:
  1783. `emacs' The file will be visited by the current Emacs process.
  1784. `default' Use the default application for this file type, which is the
  1785. association for t in the list, most likely in the system-specific
  1786. part.
  1787. This can be used to overrule an unwanted setting in the
  1788. system-specific variable.
  1789. `system' Use the system command for opening files, like \"open\".
  1790. This command is specified by the entry whose car is `system'.
  1791. Most likely, the system-specific version of this variable
  1792. does define this command, but you can overrule/replace it
  1793. here.
  1794. string A command to be executed by a shell; %s will be replaced
  1795. by the path to the file.
  1796. sexp A Lisp form which will be evaluated. The file path will
  1797. be available in the Lisp variable `file'.
  1798. For more examples, see the system specific constants
  1799. `org-file-apps-defaults-macosx'
  1800. `org-file-apps-defaults-windowsnt'
  1801. `org-file-apps-defaults-gnu'."
  1802. :group 'org-link-follow
  1803. :type '(repeat
  1804. (cons (choice :value ""
  1805. (string :tag "Extension")
  1806. (const :tag "System command to open files" system)
  1807. (const :tag "Default for unrecognized files" t)
  1808. (const :tag "Remote file" remote)
  1809. (const :tag "Links to a directory" directory)
  1810. (const :tag "Any files that have Emacs modes"
  1811. auto-mode))
  1812. (choice :value ""
  1813. (const :tag "Visit with Emacs" emacs)
  1814. (const :tag "Use default" default)
  1815. (const :tag "Use the system command" system)
  1816. (string :tag "Command")
  1817. (sexp :tag "Lisp form")))))
  1818. (defcustom org-doi-server-url "http://dx.doi.org/"
  1819. "The URL of the DOI server."
  1820. :type 'string
  1821. :version "24.3"
  1822. :group 'org-link-follow)
  1823. (defgroup org-refile nil
  1824. "Options concerning refiling entries in Org-mode."
  1825. :tag "Org Refile"
  1826. :group 'org)
  1827. (defcustom org-directory "~/org"
  1828. "Directory with org files.
  1829. This is just a default location to look for Org files. There is no need
  1830. at all to put your files into this directory. It is only used in the
  1831. following situations:
  1832. 1. When a capture template specifies a target file that is not an
  1833. absolute path. The path will then be interpreted relative to
  1834. `org-directory'
  1835. 2. When a capture note is filed away in an interactive way (when exiting the
  1836. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1837. with `org-directory' as the default path."
  1838. :group 'org-refile
  1839. :group 'org-capture
  1840. :type 'directory)
  1841. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1842. "Default target for storing notes.
  1843. Used as a fall back file for org-capture.el, for templates that
  1844. do not specify a target file."
  1845. :group 'org-refile
  1846. :group 'org-capture
  1847. :type '(choice
  1848. (const :tag "Default from remember-data-file" nil)
  1849. file))
  1850. (defcustom org-goto-interface 'outline
  1851. "The default interface to be used for `org-goto'.
  1852. Allowed values are:
  1853. outline The interface shows an outline of the relevant file
  1854. and the correct heading is found by moving through
  1855. the outline or by searching with incremental search.
  1856. outline-path-completion Headlines in the current buffer are offered via
  1857. completion. This is the interface also used by
  1858. the refile command."
  1859. :group 'org-refile
  1860. :type '(choice
  1861. (const :tag "Outline" outline)
  1862. (const :tag "Outline-path-completion" outline-path-completion)))
  1863. (defcustom org-goto-max-level 5
  1864. "Maximum target level when running `org-goto' with refile interface."
  1865. :group 'org-refile
  1866. :type 'integer)
  1867. (defcustom org-reverse-note-order nil
  1868. "Non-nil means store new notes at the beginning of a file or entry.
  1869. When nil, new notes will be filed to the end of a file or entry.
  1870. This can also be a list with cons cells of regular expressions that
  1871. are matched against file names, and values."
  1872. :group 'org-capture
  1873. :group 'org-refile
  1874. :type '(choice
  1875. (const :tag "Reverse always" t)
  1876. (const :tag "Reverse never" nil)
  1877. (repeat :tag "By file name regexp"
  1878. (cons regexp boolean))))
  1879. (defcustom org-log-refile nil
  1880. "Information to record when a task is refiled.
  1881. Possible values are:
  1882. nil Don't add anything
  1883. time Add a time stamp to the task
  1884. note Prompt for a note and add it with template `org-log-note-headings'
  1885. This option can also be set with on a per-file-basis with
  1886. #+STARTUP: nologrefile
  1887. #+STARTUP: logrefile
  1888. #+STARTUP: lognoterefile
  1889. You can have local logging settings for a subtree by setting the LOGGING
  1890. property to one or more of these keywords.
  1891. When bulk-refiling from the agenda, the value `note' is forbidden and
  1892. will temporarily be changed to `time'."
  1893. :group 'org-refile
  1894. :group 'org-progress
  1895. :version "24.1"
  1896. :type '(choice
  1897. (const :tag "No logging" nil)
  1898. (const :tag "Record timestamp" time)
  1899. (const :tag "Record timestamp with note." note)))
  1900. (defcustom org-refile-targets nil
  1901. "Targets for refiling entries with \\[org-refile].
  1902. This is a list of cons cells. Each cell contains:
  1903. - a specification of the files to be considered, either a list of files,
  1904. or a symbol whose function or variable value will be used to retrieve
  1905. a file name or a list of file names. If you use `org-agenda-files' for
  1906. that, all agenda files will be scanned for targets. Nil means consider
  1907. headings in the current buffer.
  1908. - A specification of how to find candidate refile targets. This may be
  1909. any of:
  1910. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1911. This tag has to be present in all target headlines, inheritance will
  1912. not be considered.
  1913. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1914. todo keyword.
  1915. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1916. headlines that are refiling targets.
  1917. - a cons cell (:level . N). Any headline of level N is considered a target.
  1918. Note that, when `org-odd-levels-only' is set, level corresponds to
  1919. order in hierarchy, not to the number of stars.
  1920. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1921. Note that, when `org-odd-levels-only' is set, level corresponds to
  1922. order in hierarchy, not to the number of stars.
  1923. Each element of this list generates a set of possible targets.
  1924. The union of these sets is presented (with completion) to
  1925. the user by `org-refile'.
  1926. You can set the variable `org-refile-target-verify-function' to a function
  1927. to verify each headline found by the simple criteria above.
  1928. When this variable is nil, all top-level headlines in the current buffer
  1929. are used, equivalent to the value `((nil . (:level . 1))'."
  1930. :group 'org-refile
  1931. :type '(repeat
  1932. (cons
  1933. (choice :value org-agenda-files
  1934. (const :tag "All agenda files" org-agenda-files)
  1935. (const :tag "Current buffer" nil)
  1936. (function) (variable) (file))
  1937. (choice :tag "Identify target headline by"
  1938. (cons :tag "Specific tag" (const :value :tag) (string))
  1939. (cons :tag "TODO keyword" (const :value :todo) (string))
  1940. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1941. (cons :tag "Level number" (const :value :level) (integer))
  1942. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1943. (defcustom org-refile-target-verify-function nil
  1944. "Function to verify if the headline at point should be a refile target.
  1945. The function will be called without arguments, with point at the
  1946. beginning of the headline. It should return t and leave point
  1947. where it is if the headline is a valid target for refiling.
  1948. If the target should not be selected, the function must return nil.
  1949. In addition to this, it may move point to a place from where the search
  1950. should be continued. For example, the function may decide that the entire
  1951. subtree of the current entry should be excluded and move point to the end
  1952. of the subtree."
  1953. :group 'org-refile
  1954. :type '(choice
  1955. (const nil)
  1956. (function)))
  1957. (defcustom org-refile-use-cache nil
  1958. "Non-nil means cache refile targets to speed up the process.
  1959. The cache for a particular file will be updated automatically when
  1960. the buffer has been killed, or when any of the marker used for flagging
  1961. refile targets no longer points at a live buffer.
  1962. If you have added new entries to a buffer that might themselves be targets,
  1963. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  1964. find that easier, `C-u C-u C-u C-c C-w'."
  1965. :group 'org-refile
  1966. :version "24.1"
  1967. :type 'boolean)
  1968. (defcustom org-refile-use-outline-path nil
  1969. "Non-nil means provide refile targets as paths.
  1970. So a level 3 headline will be available as level1/level2/level3.
  1971. When the value is `file', also include the file name (without directory)
  1972. into the path. In this case, you can also stop the completion after
  1973. the file name, to get entries inserted as top level in the file.
  1974. When `full-file-path', include the full file path."
  1975. :group 'org-refile
  1976. :type '(choice
  1977. (const :tag "Not" nil)
  1978. (const :tag "Yes" t)
  1979. (const :tag "Start with file name" file)
  1980. (const :tag "Start with full file path" full-file-path)))
  1981. (defcustom org-outline-path-complete-in-steps t
  1982. "Non-nil means complete the outline path in hierarchical steps.
  1983. When Org-mode uses the refile interface to select an outline path
  1984. \(see variable `org-refile-use-outline-path'), the completion of
  1985. the path can be done is a single go, or if can be done in steps down
  1986. the headline hierarchy. Going in steps is probably the best if you
  1987. do not use a special completion package like `ido' or `icicles'.
  1988. However, when using these packages, going in one step can be very
  1989. fast, while still showing the whole path to the entry."
  1990. :group 'org-refile
  1991. :type 'boolean)
  1992. (defcustom org-refile-allow-creating-parent-nodes nil
  1993. "Non-nil means allow to create new nodes as refile targets.
  1994. New nodes are then created by adding \"/new node name\" to the completion
  1995. of an existing node. When the value of this variable is `confirm',
  1996. new node creation must be confirmed by the user (recommended).
  1997. When nil, the completion must match an existing entry.
  1998. Note that, if the new heading is not seen by the criteria
  1999. listed in `org-refile-targets', multiple instances of the same
  2000. heading would be created by trying again to file under the new
  2001. heading."
  2002. :group 'org-refile
  2003. :type '(choice
  2004. (const :tag "Never" nil)
  2005. (const :tag "Always" t)
  2006. (const :tag "Prompt for confirmation" confirm)))
  2007. (defcustom org-refile-active-region-within-subtree nil
  2008. "Non-nil means also refile active region within a subtree.
  2009. By default `org-refile' doesn't allow refiling regions if they
  2010. don't contain a set of subtrees, but it might be convenient to
  2011. do so sometimes: in that case, the first line of the region is
  2012. converted to a headline before refiling."
  2013. :group 'org-refile
  2014. :version "24.1"
  2015. :type 'boolean)
  2016. (defgroup org-todo nil
  2017. "Options concerning TODO items in Org-mode."
  2018. :tag "Org TODO"
  2019. :group 'org)
  2020. (defgroup org-progress nil
  2021. "Options concerning Progress logging in Org-mode."
  2022. :tag "Org Progress"
  2023. :group 'org-time)
  2024. (defvar org-todo-interpretation-widgets
  2025. '((:tag "Sequence (cycling hits every state)" sequence)
  2026. (:tag "Type (cycling directly to DONE)" type))
  2027. "The available interpretation symbols for customizing `org-todo-keywords'.
  2028. Interested libraries should add to this list.")
  2029. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  2030. "List of TODO entry keyword sequences and their interpretation.
  2031. \\<org-mode-map>This is a list of sequences.
  2032. Each sequence starts with a symbol, either `sequence' or `type',
  2033. indicating if the keywords should be interpreted as a sequence of
  2034. action steps, or as different types of TODO items. The first
  2035. keywords are states requiring action - these states will select a headline
  2036. for inclusion into the global TODO list Org-mode produces. If one of
  2037. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  2038. signify that no further action is necessary. If \"|\" is not found,
  2039. the last keyword is treated as the only DONE state of the sequence.
  2040. The command \\[org-todo] cycles an entry through these states, and one
  2041. additional state where no keyword is present. For details about this
  2042. cycling, see the manual.
  2043. TODO keywords and interpretation can also be set on a per-file basis with
  2044. the special #+SEQ_TODO and #+TYP_TODO lines.
  2045. Each keyword can optionally specify a character for fast state selection
  2046. \(in combination with the variable `org-use-fast-todo-selection')
  2047. and specifiers for state change logging, using the same syntax that
  2048. is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says that
  2049. the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  2050. indicates to record a time stamp each time this state is selected.
  2051. Each keyword may also specify if a timestamp or a note should be
  2052. recorded when entering or leaving the state, by adding additional
  2053. characters in the parenthesis after the keyword. This looks like this:
  2054. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  2055. record only the time of the state change. With X and Y being either
  2056. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  2057. Y when leaving the state if and only if the *target* state does not
  2058. define X. You may omit any of the fast-selection key or X or /Y,
  2059. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  2060. For backward compatibility, this variable may also be just a list
  2061. of keywords. In this case the interpretation (sequence or type) will be
  2062. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  2063. :group 'org-todo
  2064. :group 'org-keywords
  2065. :type '(choice
  2066. (repeat :tag "Old syntax, just keywords"
  2067. (string :tag "Keyword"))
  2068. (repeat :tag "New syntax"
  2069. (cons
  2070. (choice
  2071. :tag "Interpretation"
  2072. ;;Quick and dirty way to see
  2073. ;;`org-todo-interpretations'. This takes the
  2074. ;;place of item arguments
  2075. :convert-widget
  2076. (lambda (widget)
  2077. (widget-put widget
  2078. :args (mapcar
  2079. #'(lambda (x)
  2080. (widget-convert
  2081. (cons 'const x)))
  2082. org-todo-interpretation-widgets))
  2083. widget))
  2084. (repeat
  2085. (string :tag "Keyword"))))))
  2086. (defvar org-todo-keywords-1 nil
  2087. "All TODO and DONE keywords active in a buffer.")
  2088. (make-variable-buffer-local 'org-todo-keywords-1)
  2089. (defvar org-todo-keywords-for-agenda nil)
  2090. (defvar org-done-keywords-for-agenda nil)
  2091. (defvar org-drawers-for-agenda nil)
  2092. (defvar org-todo-keyword-alist-for-agenda nil)
  2093. (defvar org-tag-alist-for-agenda nil
  2094. "Alist of all tags from all agenda files.")
  2095. (defvar org-tag-groups-alist-for-agenda nil
  2096. "Alist of all groups tags from all current agenda files.")
  2097. (defvar org-tag-groups-alist nil)
  2098. (make-variable-buffer-local 'org-tag-groups-alist)
  2099. (defvar org-agenda-contributing-files nil)
  2100. (defvar org-not-done-keywords nil)
  2101. (make-variable-buffer-local 'org-not-done-keywords)
  2102. (defvar org-done-keywords nil)
  2103. (make-variable-buffer-local 'org-done-keywords)
  2104. (defvar org-todo-heads nil)
  2105. (make-variable-buffer-local 'org-todo-heads)
  2106. (defvar org-todo-sets nil)
  2107. (make-variable-buffer-local 'org-todo-sets)
  2108. (defvar org-todo-log-states nil)
  2109. (make-variable-buffer-local 'org-todo-log-states)
  2110. (defvar org-todo-kwd-alist nil)
  2111. (make-variable-buffer-local 'org-todo-kwd-alist)
  2112. (defvar org-todo-key-alist nil)
  2113. (make-variable-buffer-local 'org-todo-key-alist)
  2114. (defvar org-todo-key-trigger nil)
  2115. (make-variable-buffer-local 'org-todo-key-trigger)
  2116. (defcustom org-todo-interpretation 'sequence
  2117. "Controls how TODO keywords are interpreted.
  2118. This variable is in principle obsolete and is only used for
  2119. backward compatibility, if the interpretation of todo keywords is
  2120. not given already in `org-todo-keywords'. See that variable for
  2121. more information."
  2122. :group 'org-todo
  2123. :group 'org-keywords
  2124. :type '(choice (const sequence)
  2125. (const type)))
  2126. (defcustom org-use-fast-todo-selection t
  2127. "Non-nil means use the fast todo selection scheme with C-c C-t.
  2128. This variable describes if and under what circumstances the cycling
  2129. mechanism for TODO keywords will be replaced by a single-key, direct
  2130. selection scheme.
  2131. When nil, fast selection is never used.
  2132. When the symbol `prefix', it will be used when `org-todo' is called
  2133. with a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and
  2134. `C-u t' in an agenda buffer.
  2135. When t, fast selection is used by default. In this case, the prefix
  2136. argument forces cycling instead.
  2137. In all cases, the special interface is only used if access keys have
  2138. actually been assigned by the user, i.e. if keywords in the configuration
  2139. are followed by a letter in parenthesis, like TODO(t)."
  2140. :group 'org-todo
  2141. :type '(choice
  2142. (const :tag "Never" nil)
  2143. (const :tag "By default" t)
  2144. (const :tag "Only with C-u C-c C-t" prefix)))
  2145. (defcustom org-provide-todo-statistics t
  2146. "Non-nil means update todo statistics after insert and toggle.
  2147. ALL-HEADLINES means update todo statistics by including headlines
  2148. with no TODO keyword as well, counting them as not done.
  2149. A list of TODO keywords means the same, but skip keywords that are
  2150. not in this list.
  2151. When this is set, todo statistics is updated in the parent of the
  2152. current entry each time a todo state is changed."
  2153. :group 'org-todo
  2154. :type '(choice
  2155. (const :tag "Yes, only for TODO entries" t)
  2156. (const :tag "Yes, including all entries" all-headlines)
  2157. (repeat :tag "Yes, for TODOs in this list"
  2158. (string :tag "TODO keyword"))
  2159. (other :tag "No TODO statistics" nil)))
  2160. (defcustom org-hierarchical-todo-statistics t
  2161. "Non-nil means TODO statistics covers just direct children.
  2162. When nil, all entries in the subtree are considered.
  2163. This has only an effect if `org-provide-todo-statistics' is set.
  2164. To set this to nil for only a single subtree, use a COOKIE_DATA
  2165. property and include the word \"recursive\" into the value."
  2166. :group 'org-todo
  2167. :type 'boolean)
  2168. (defcustom org-after-todo-state-change-hook nil
  2169. "Hook which is run after the state of a TODO item was changed.
  2170. The new state (a string with a TODO keyword, or nil) is available in the
  2171. Lisp variable `org-state'."
  2172. :group 'org-todo
  2173. :type 'hook)
  2174. (defvar org-blocker-hook nil
  2175. "Hook for functions that are allowed to block a state change.
  2176. Functions in this hook should not modify the buffer.
  2177. Each function gets as its single argument a property list,
  2178. see `org-trigger-hook' for more information about this list.
  2179. If any of the functions in this hook returns nil, the state change
  2180. is blocked.")
  2181. (defvar org-trigger-hook nil
  2182. "Hook for functions that are triggered by a state change.
  2183. Each function gets as its single argument a property list with at
  2184. least the following elements:
  2185. (:type type-of-change :position pos-at-entry-start
  2186. :from old-state :to new-state)
  2187. Depending on the type, more properties may be present.
  2188. This mechanism is currently implemented for:
  2189. TODO state changes
  2190. ------------------
  2191. :type todo-state-change
  2192. :from previous state (keyword as a string), or nil, or a symbol
  2193. 'todo' or 'done', to indicate the general type of state.
  2194. :to new state, like in :from")
  2195. (defcustom org-enforce-todo-dependencies nil
  2196. "Non-nil means undone TODO entries will block switching the parent to DONE.
  2197. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  2198. be blocked if any prior sibling is not yet done.
  2199. Finally, if the parent is blocked because of ordered siblings of its own,
  2200. the child will also be blocked."
  2201. :set (lambda (var val)
  2202. (set var val)
  2203. (if val
  2204. (add-hook 'org-blocker-hook
  2205. 'org-block-todo-from-children-or-siblings-or-parent)
  2206. (remove-hook 'org-blocker-hook
  2207. 'org-block-todo-from-children-or-siblings-or-parent)))
  2208. :group 'org-todo
  2209. :type 'boolean)
  2210. (defcustom org-enforce-todo-checkbox-dependencies nil
  2211. "Non-nil means unchecked boxes will block switching the parent to DONE.
  2212. When this is nil, checkboxes have no influence on switching TODO states.
  2213. When non-nil, you first need to check off all check boxes before the TODO
  2214. entry can be switched to DONE.
  2215. This variable needs to be set before org.el is loaded, and you need to
  2216. restart Emacs after a change to make the change effective. The only way
  2217. to change is while Emacs is running is through the customize interface."
  2218. :set (lambda (var val)
  2219. (set var val)
  2220. (if val
  2221. (add-hook 'org-blocker-hook
  2222. 'org-block-todo-from-checkboxes)
  2223. (remove-hook 'org-blocker-hook
  2224. 'org-block-todo-from-checkboxes)))
  2225. :group 'org-todo
  2226. :type 'boolean)
  2227. (defcustom org-treat-insert-todo-heading-as-state-change nil
  2228. "Non-nil means inserting a TODO heading is treated as state change.
  2229. So when the command \\[org-insert-todo-heading] is used, state change
  2230. logging will apply if appropriate. When nil, the new TODO item will
  2231. be inserted directly, and no logging will take place."
  2232. :group 'org-todo
  2233. :type 'boolean)
  2234. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  2235. "Non-nil means switching TODO states with S-cursor counts as state change.
  2236. This is the default behavior. However, setting this to nil allows a
  2237. convenient way to select a TODO state and bypass any logging associated
  2238. with that."
  2239. :group 'org-todo
  2240. :type 'boolean)
  2241. (defcustom org-todo-state-tags-triggers nil
  2242. "Tag changes that should be triggered by TODO state changes.
  2243. This is a list. Each entry is
  2244. (state-change (tag . flag) .......)
  2245. State-change can be a string with a state, and empty string to indicate the
  2246. state that has no TODO keyword, or it can be one of the symbols `todo'
  2247. or `done', meaning any not-done or done state, respectively."
  2248. :group 'org-todo
  2249. :group 'org-tags
  2250. :type '(repeat
  2251. (cons (choice :tag "When changing to"
  2252. (const :tag "Not-done state" todo)
  2253. (const :tag "Done state" done)
  2254. (string :tag "State"))
  2255. (repeat
  2256. (cons :tag "Tag action"
  2257. (string :tag "Tag")
  2258. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  2259. (defcustom org-log-done nil
  2260. "Information to record when a task moves to the DONE state.
  2261. Possible values are:
  2262. nil Don't add anything, just change the keyword
  2263. time Add a time stamp to the task
  2264. note Prompt for a note and add it with template `org-log-note-headings'
  2265. This option can also be set with on a per-file-basis with
  2266. #+STARTUP: nologdone
  2267. #+STARTUP: logdone
  2268. #+STARTUP: lognotedone
  2269. You can have local logging settings for a subtree by setting the LOGGING
  2270. property to one or more of these keywords."
  2271. :group 'org-todo
  2272. :group 'org-progress
  2273. :type '(choice
  2274. (const :tag "No logging" nil)
  2275. (const :tag "Record CLOSED timestamp" time)
  2276. (const :tag "Record CLOSED timestamp with note." note)))
  2277. ;; Normalize old uses of org-log-done.
  2278. (cond
  2279. ((eq org-log-done t) (setq org-log-done 'time))
  2280. ((and (listp org-log-done) (memq 'done org-log-done))
  2281. (setq org-log-done 'note)))
  2282. (defcustom org-log-reschedule nil
  2283. "Information to record when the scheduling date of a tasks is modified.
  2284. Possible values are:
  2285. nil Don't add anything, just change the date
  2286. time Add a time stamp to the task
  2287. note Prompt for a note and add it with template `org-log-note-headings'
  2288. This option can also be set with on a per-file-basis with
  2289. #+STARTUP: nologreschedule
  2290. #+STARTUP: logreschedule
  2291. #+STARTUP: lognotereschedule"
  2292. :group 'org-todo
  2293. :group 'org-progress
  2294. :type '(choice
  2295. (const :tag "No logging" nil)
  2296. (const :tag "Record timestamp" time)
  2297. (const :tag "Record timestamp with note." note)))
  2298. (defcustom org-log-redeadline nil
  2299. "Information to record when the deadline date of a tasks is modified.
  2300. Possible values are:
  2301. nil Don't add anything, just change the date
  2302. time Add a time stamp to the task
  2303. note Prompt for a note and add it with template `org-log-note-headings'
  2304. This option can also be set with on a per-file-basis with
  2305. #+STARTUP: nologredeadline
  2306. #+STARTUP: logredeadline
  2307. #+STARTUP: lognoteredeadline
  2308. You can have local logging settings for a subtree by setting the LOGGING
  2309. property to one or more of these keywords."
  2310. :group 'org-todo
  2311. :group 'org-progress
  2312. :type '(choice
  2313. (const :tag "No logging" nil)
  2314. (const :tag "Record timestamp" time)
  2315. (const :tag "Record timestamp with note." note)))
  2316. (defcustom org-log-note-clock-out nil
  2317. "Non-nil means record a note when clocking out of an item.
  2318. This can also be configured on a per-file basis by adding one of
  2319. the following lines anywhere in the buffer:
  2320. #+STARTUP: lognoteclock-out
  2321. #+STARTUP: nolognoteclock-out"
  2322. :group 'org-todo
  2323. :group 'org-progress
  2324. :type 'boolean)
  2325. (defcustom org-log-done-with-time t
  2326. "Non-nil means the CLOSED time stamp will contain date and time.
  2327. When nil, only the date will be recorded."
  2328. :group 'org-progress
  2329. :type 'boolean)
  2330. (defcustom org-log-note-headings
  2331. '((done . "CLOSING NOTE %t")
  2332. (state . "State %-12s from %-12S %t")
  2333. (note . "Note taken on %t")
  2334. (reschedule . "Rescheduled from %S on %t")
  2335. (delschedule . "Not scheduled, was %S on %t")
  2336. (redeadline . "New deadline from %S on %t")
  2337. (deldeadline . "Removed deadline, was %S on %t")
  2338. (refile . "Refiled on %t")
  2339. (clock-out . ""))
  2340. "Headings for notes added to entries.
  2341. The value is an alist, with the car being a symbol indicating the note
  2342. context, and the cdr is the heading to be used. The heading may also be the
  2343. empty string.
  2344. %t in the heading will be replaced by a time stamp.
  2345. %T will be an active time stamp instead the default inactive one
  2346. %d will be replaced by a short-format time stamp.
  2347. %D will be replaced by an active short-format time stamp.
  2348. %s will be replaced by the new TODO state, in double quotes.
  2349. %S will be replaced by the old TODO state, in double quotes.
  2350. %u will be replaced by the user name.
  2351. %U will be replaced by the full user name.
  2352. In fact, it is not a good idea to change the `state' entry, because
  2353. agenda log mode depends on the format of these entries."
  2354. :group 'org-todo
  2355. :group 'org-progress
  2356. :type '(list :greedy t
  2357. (cons (const :tag "Heading when closing an item" done) string)
  2358. (cons (const :tag
  2359. "Heading when changing todo state (todo sequence only)"
  2360. state) string)
  2361. (cons (const :tag "Heading when just taking a note" note) string)
  2362. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2363. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2364. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2365. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2366. (cons (const :tag "Heading when refiling" refile) string)
  2367. (cons (const :tag "Heading when clocking out" clock-out) string)))
  2368. (unless (assq 'note org-log-note-headings)
  2369. (push '(note . "%t") org-log-note-headings))
  2370. (defcustom org-log-into-drawer nil
  2371. "Non-nil means insert state change notes and time stamps into a drawer.
  2372. When nil, state changes notes will be inserted after the headline and
  2373. any scheduling and clock lines, but not inside a drawer.
  2374. The value of this variable should be the name of the drawer to use.
  2375. LOGBOOK is proposed as the default drawer for this purpose, you can
  2376. also set this to a string to define the drawer of your choice.
  2377. A value of t is also allowed, representing \"LOGBOOK\".
  2378. A value of t or nil can also be set with on a per-file-basis with
  2379. #+STARTUP: logdrawer
  2380. #+STARTUP: nologdrawer
  2381. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2382. will be ignored.
  2383. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2384. a subtree."
  2385. :group 'org-todo
  2386. :group 'org-progress
  2387. :type '(choice
  2388. (const :tag "Not into a drawer" nil)
  2389. (const :tag "LOGBOOK" t)
  2390. (string :tag "Other")))
  2391. (org-defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer)
  2392. (defun org-log-into-drawer ()
  2393. "Return the value of `org-log-into-drawer', but let properties overrule.
  2394. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  2395. used instead of the default value."
  2396. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit t)))
  2397. (cond
  2398. ((not p) org-log-into-drawer)
  2399. ((equal p "nil") nil)
  2400. ((equal p "t") "LOGBOOK")
  2401. (t p))))
  2402. (defcustom org-log-state-notes-insert-after-drawers nil
  2403. "Non-nil means insert state change notes after any drawers in entry.
  2404. Only the drawers that *immediately* follow the headline and the
  2405. deadline/scheduled line are skipped.
  2406. When nil, insert notes right after the heading and perhaps the line
  2407. with deadline/scheduling if present.
  2408. This variable will have no effect if `org-log-into-drawer' is
  2409. set."
  2410. :group 'org-todo
  2411. :group 'org-progress
  2412. :type 'boolean)
  2413. (defcustom org-log-states-order-reversed t
  2414. "Non-nil means the latest state note will be directly after heading.
  2415. When nil, the state change notes will be ordered according to time.
  2416. This option can also be set with on a per-file-basis with
  2417. #+STARTUP: logstatesreversed
  2418. #+STARTUP: nologstatesreversed"
  2419. :group 'org-todo
  2420. :group 'org-progress
  2421. :type 'boolean)
  2422. (defcustom org-todo-repeat-to-state nil
  2423. "The TODO state to which a repeater should return the repeating task.
  2424. By default this is the first task in a TODO sequence, or the previous state
  2425. in a TODO_TYP set. But you can specify another task here.
  2426. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2427. :group 'org-todo
  2428. :version "24.1"
  2429. :type '(choice (const :tag "Head of sequence" nil)
  2430. (string :tag "Specific state")))
  2431. (defcustom org-log-repeat 'time
  2432. "Non-nil means record moving through the DONE state when triggering repeat.
  2433. An auto-repeating task is immediately switched back to TODO when
  2434. marked DONE. If you are not logging state changes (by adding \"@\"
  2435. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2436. record a closing note, there will be no record of the task moving
  2437. through DONE. This variable forces taking a note anyway.
  2438. nil Don't force a record
  2439. time Record a time stamp
  2440. note Prompt for a note and add it with template `org-log-note-headings'
  2441. This option can also be set with on a per-file-basis with
  2442. #+STARTUP: nologrepeat
  2443. #+STARTUP: logrepeat
  2444. #+STARTUP: lognoterepeat
  2445. You can have local logging settings for a subtree by setting the LOGGING
  2446. property to one or more of these keywords."
  2447. :group 'org-todo
  2448. :group 'org-progress
  2449. :type '(choice
  2450. (const :tag "Don't force a record" nil)
  2451. (const :tag "Force recording the DONE state" time)
  2452. (const :tag "Force recording a note with the DONE state" note)))
  2453. (defgroup org-priorities nil
  2454. "Priorities in Org-mode."
  2455. :tag "Org Priorities"
  2456. :group 'org-todo)
  2457. (defcustom org-enable-priority-commands t
  2458. "Non-nil means priority commands are active.
  2459. When nil, these commands will be disabled, so that you never accidentally
  2460. set a priority."
  2461. :group 'org-priorities
  2462. :type 'boolean)
  2463. (defcustom org-highest-priority ?A
  2464. "The highest priority of TODO items. A character like ?A, ?B etc.
  2465. Must have a smaller ASCII number than `org-lowest-priority'."
  2466. :group 'org-priorities
  2467. :type 'character)
  2468. (defcustom org-lowest-priority ?C
  2469. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2470. Must have a larger ASCII number than `org-highest-priority'."
  2471. :group 'org-priorities
  2472. :type 'character)
  2473. (defcustom org-default-priority ?B
  2474. "The default priority of TODO items.
  2475. This is the priority an item gets if no explicit priority is given.
  2476. When starting to cycle on an empty priority the first step in the cycle
  2477. depends on `org-priority-start-cycle-with-default'. The resulting first
  2478. step priority must not exceed the range from `org-highest-priority' to
  2479. `org-lowest-priority' which means that `org-default-priority' has to be
  2480. in this range exclusive or inclusive the range boundaries. Else the
  2481. first step refuses to set the default and the second will fall back
  2482. to (depending on the command used) the highest or lowest priority."
  2483. :group 'org-priorities
  2484. :type 'character)
  2485. (defcustom org-priority-start-cycle-with-default t
  2486. "Non-nil means start with default priority when starting to cycle.
  2487. When this is nil, the first step in the cycle will be (depending on the
  2488. command used) one higher or lower than the default priority.
  2489. See also `org-default-priority'."
  2490. :group 'org-priorities
  2491. :type 'boolean)
  2492. (defcustom org-get-priority-function nil
  2493. "Function to extract the priority from a string.
  2494. The string is normally the headline. If this is nil Org computes the
  2495. priority from the priority cookie like [#A] in the headline. It returns
  2496. an integer, increasing by 1000 for each priority level.
  2497. The user can set a different function here, which should take a string
  2498. as an argument and return the numeric priority."
  2499. :group 'org-priorities
  2500. :version "24.1"
  2501. :type '(choice
  2502. (const nil)
  2503. (function)))
  2504. (defgroup org-time nil
  2505. "Options concerning time stamps and deadlines in Org-mode."
  2506. :tag "Org Time"
  2507. :group 'org)
  2508. (defcustom org-insert-labeled-timestamps-at-point nil
  2509. "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
  2510. When nil, these labeled time stamps are forces into the second line of an
  2511. entry, just after the headline. When scheduling from the global TODO list,
  2512. the time stamp will always be forced into the second line."
  2513. :group 'org-time
  2514. :type 'boolean)
  2515. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  2516. "Formats for `format-time-string' which are used for time stamps.
  2517. It is not recommended to change this constant.")
  2518. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2519. "Number of minutes to round time stamps to.
  2520. These are two values, the first applies when first creating a time stamp.
  2521. The second applies when changing it with the commands `S-up' and `S-down'.
  2522. When changing the time stamp, this means that it will change in steps
  2523. of N minutes, as given by the second value.
  2524. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2525. numbers should be factors of 60, so for example 5, 10, 15.
  2526. When this is larger than 1, you can still force an exact time stamp by using
  2527. a double prefix argument to a time stamp command like `C-c .' or `C-c !',
  2528. and by using a prefix arg to `S-up/down' to specify the exact number
  2529. of minutes to shift."
  2530. :group 'org-time
  2531. :get #'(lambda (var) ; Make sure both elements are there
  2532. (if (integerp (default-value var))
  2533. (list (default-value var) 5)
  2534. (default-value var)))
  2535. :type '(list
  2536. (integer :tag "when inserting times")
  2537. (integer :tag "when modifying times")))
  2538. ;; Normalize old customizations of this variable.
  2539. (when (integerp org-time-stamp-rounding-minutes)
  2540. (setq org-time-stamp-rounding-minutes
  2541. (list org-time-stamp-rounding-minutes
  2542. org-time-stamp-rounding-minutes)))
  2543. (defcustom org-display-custom-times nil
  2544. "Non-nil means overlay custom formats over all time stamps.
  2545. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2546. To turn this on on a per-file basis, insert anywhere in the file:
  2547. #+STARTUP: customtime"
  2548. :group 'org-time
  2549. :set 'set-default
  2550. :type 'sexp)
  2551. (make-variable-buffer-local 'org-display-custom-times)
  2552. (defcustom org-time-stamp-custom-formats
  2553. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2554. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2555. These are overlaid over the default ISO format if the variable
  2556. `org-display-custom-times' is set. Time like %H:%M should be at the
  2557. end of the second format. The custom formats are also honored by export
  2558. commands, if custom time display is turned on at the time of export."
  2559. :group 'org-time
  2560. :type 'sexp)
  2561. (defun org-time-stamp-format (&optional long inactive)
  2562. "Get the right format for a time string."
  2563. (let ((f (if long (cdr org-time-stamp-formats)
  2564. (car org-time-stamp-formats))))
  2565. (if inactive
  2566. (concat "[" (substring f 1 -1) "]")
  2567. f)))
  2568. (defcustom org-time-clocksum-format
  2569. '(:days "%dd " :hours "%d" :require-hours t :minutes ":%02d" :require-minutes t)
  2570. "The format string used when creating CLOCKSUM lines.
  2571. This is also used when Org mode generates a time duration.
  2572. The value can be a single format string containing two
  2573. %-sequences, which will be filled with the number of hours and
  2574. minutes in that order.
  2575. Alternatively, the value can be a plist associating any of the
  2576. keys :years, :months, :weeks, :days, :hours or :minutes with
  2577. format strings. The time duration is formatted using only the
  2578. time components that are needed and concatenating the results.
  2579. If a time unit in absent, it falls back to the next smallest
  2580. unit.
  2581. The keys :require-years, :require-months, :require-days,
  2582. :require-weeks, :require-hours, :require-minutes are also
  2583. meaningful. A non-nil value for these keys indicates that the
  2584. corresponding time component should always be included, even if
  2585. its value is 0.
  2586. For example,
  2587. \(:days \"%dd\" :hours \"%d\" :require-hours t :minutes \":%02d\"
  2588. :require-minutes t)
  2589. means durations longer than a day will be expressed in days,
  2590. hours and minutes, and durations less than a day will always be
  2591. expressed in hours and minutes (even for durations less than an
  2592. hour).
  2593. The value
  2594. \(:days \"%dd\" :minutes \"%dm\")
  2595. means durations longer than a day will be expressed in days and
  2596. minutes, and durations less than a day will be expressed entirely
  2597. in minutes (even for durations longer than an hour)."
  2598. :group 'org-time
  2599. :group 'org-clock
  2600. :version "24.4"
  2601. :package-version '(Org . "8.0")
  2602. :type '(choice (string :tag "Format string")
  2603. (set :tag "Plist"
  2604. (group :inline t (const :tag "Years" :years)
  2605. (string :tag "Format string"))
  2606. (group :inline t
  2607. (const :tag "Always show years" :require-years)
  2608. (const t))
  2609. (group :inline t (const :tag "Months" :months)
  2610. (string :tag "Format string"))
  2611. (group :inline t
  2612. (const :tag "Always show months" :require-months)
  2613. (const t))
  2614. (group :inline t (const :tag "Weeks" :weeks)
  2615. (string :tag "Format string"))
  2616. (group :inline t
  2617. (const :tag "Always show weeks" :require-weeks)
  2618. (const t))
  2619. (group :inline t (const :tag "Days" :days)
  2620. (string :tag "Format string"))
  2621. (group :inline t
  2622. (const :tag "Always show days" :require-days)
  2623. (const t))
  2624. (group :inline t (const :tag "Hours" :hours)
  2625. (string :tag "Format string"))
  2626. (group :inline t
  2627. (const :tag "Always show hours" :require-hours)
  2628. (const t))
  2629. (group :inline t (const :tag "Minutes" :minutes)
  2630. (string :tag "Format string"))
  2631. (group :inline t
  2632. (const :tag "Always show minutes" :require-minutes)
  2633. (const t)))))
  2634. (defcustom org-time-clocksum-use-fractional nil
  2635. "When non-nil, \\[org-clock-display] uses fractional times.
  2636. See `org-time-clocksum-format' for more on time clock formats."
  2637. :group 'org-time
  2638. :group 'org-clock
  2639. :version "24.3"
  2640. :type 'boolean)
  2641. (defcustom org-time-clocksum-use-effort-durations nil
  2642. "When non-nil, \\[org-clock-display] uses effort durations.
  2643. E.g. by default, one day is considered to be a 8 hours effort,
  2644. so a task that has been clocked for 16 hours will be displayed
  2645. as during 2 days in the clock display or in the clocktable.
  2646. See `org-effort-durations' on how to set effort durations
  2647. and `org-time-clocksum-format' for more on time clock formats."
  2648. :group 'org-time
  2649. :group 'org-clock
  2650. :version "24.4"
  2651. :package-version '(Org . "8.0")
  2652. :type 'boolean)
  2653. (defcustom org-time-clocksum-fractional-format "%.2f"
  2654. "The format string used when creating CLOCKSUM lines,
  2655. or when Org mode generates a time duration, if
  2656. `org-time-clocksum-use-fractional' is enabled.
  2657. The value can be a single format string containing one
  2658. %-sequence, which will be filled with the number of hours as
  2659. a float.
  2660. Alternatively, the value can be a plist associating any of the
  2661. keys :years, :months, :weeks, :days, :hours or :minutes with
  2662. a format string. The time duration is formatted using the
  2663. largest time unit which gives a non-zero integer part. If all
  2664. specified formats have zero integer part, the smallest time unit
  2665. is used."
  2666. :group 'org-time
  2667. :type '(choice (string :tag "Format string")
  2668. (set (group :inline t (const :tag "Years" :years)
  2669. (string :tag "Format string"))
  2670. (group :inline t (const :tag "Months" :months)
  2671. (string :tag "Format string"))
  2672. (group :inline t (const :tag "Weeks" :weeks)
  2673. (string :tag "Format string"))
  2674. (group :inline t (const :tag "Days" :days)
  2675. (string :tag "Format string"))
  2676. (group :inline t (const :tag "Hours" :hours)
  2677. (string :tag "Format string"))
  2678. (group :inline t (const :tag "Minutes" :minutes)
  2679. (string :tag "Format string")))))
  2680. (defcustom org-deadline-warning-days 14
  2681. "Number of days before expiration during which a deadline becomes active.
  2682. This variable governs the display in sparse trees and in the agenda.
  2683. When 0 or negative, it means use this number (the absolute value of it)
  2684. even if a deadline has a different individual lead time specified.
  2685. Custom commands can set this variable in the options section."
  2686. :group 'org-time
  2687. :group 'org-agenda-daily/weekly
  2688. :type 'integer)
  2689. (defcustom org-scheduled-delay-days 0
  2690. "Number of days before a scheduled item becomes active.
  2691. This variable governs the display in sparse trees and in the agenda.
  2692. The default value (i.e. 0) means: don't delay scheduled item.
  2693. When negative, it means use this number (the absolute value of it)
  2694. even if a scheduled item has a different individual delay time
  2695. specified.
  2696. Custom commands can set this variable in the options section."
  2697. :group 'org-time
  2698. :group 'org-agenda-daily/weekly
  2699. :version "24.4"
  2700. :package-version '(Org . "8.0")
  2701. :type 'integer)
  2702. (defcustom org-read-date-prefer-future t
  2703. "Non-nil means assume future for incomplete date input from user.
  2704. This affects the following situations:
  2705. 1. The user gives a month but not a year.
  2706. For example, if it is April and you enter \"feb 2\", this will be read
  2707. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2708. 2. The user gives a day, but no month.
  2709. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2710. read this as the third of *next* month. However, if you enter \"17\",
  2711. it will be considered as *this* month.
  2712. If you set this variable to the symbol `time', then also the following
  2713. will work:
  2714. 3. If the user gives a time.
  2715. If the time is before now, it will be interpreted as tomorrow.
  2716. Currently none of this works for ISO week specifications.
  2717. When this option is nil, the current day, month and year will always be
  2718. used as defaults.
  2719. See also `org-agenda-jump-prefer-future'."
  2720. :group 'org-time
  2721. :type '(choice
  2722. (const :tag "Never" nil)
  2723. (const :tag "Check month and day" t)
  2724. (const :tag "Check month, day, and time" time)))
  2725. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2726. "Should the agenda jump command prefer the future for incomplete dates?
  2727. The default is to do the same as configured in `org-read-date-prefer-future'.
  2728. But you can also set a deviating value here.
  2729. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2730. :group 'org-agenda
  2731. :group 'org-time
  2732. :version "24.1"
  2733. :type '(choice
  2734. (const :tag "Use org-read-date-prefer-future"
  2735. org-read-date-prefer-future)
  2736. (const :tag "Never" nil)
  2737. (const :tag "Always" t)))
  2738. (defcustom org-read-date-force-compatible-dates t
  2739. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2740. Depending on the system Emacs is running on, certain dates cannot
  2741. be represented with the type used internally to represent time.
  2742. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2743. correctly. Some systems allow for earlier dates, some for later,
  2744. some for both. One way to find out it to insert any date into an
  2745. Org buffer, putting the cursor on the year and hitting S-up and
  2746. S-down to test the range.
  2747. When this variable is set to t, the date/time prompt will not let
  2748. you specify dates outside the 1970-2037 range, so it is certain that
  2749. these dates will work in whatever version of Emacs you are
  2750. running, and also that you can move a file from one Emacs implementation
  2751. to another. WHenever Org is forcing the year for you, it will display
  2752. a message and beep.
  2753. When this variable is nil, Org will check if the date is
  2754. representable in the specific Emacs implementation you are using.
  2755. If not, it will force a year, usually the current year, and beep
  2756. to remind you. Currently this setting is not recommended because
  2757. the likelihood that you will open your Org files in an Emacs that
  2758. has limited date range is not negligible.
  2759. A workaround for this problem is to use diary sexp dates for time
  2760. stamps outside of this range."
  2761. :group 'org-time
  2762. :version "24.1"
  2763. :type 'boolean)
  2764. (defcustom org-read-date-display-live t
  2765. "Non-nil means display current interpretation of date prompt live.
  2766. This display will be in an overlay, in the minibuffer."
  2767. :group 'org-time
  2768. :type 'boolean)
  2769. (defcustom org-read-date-popup-calendar t
  2770. "Non-nil means pop up a calendar when prompting for a date.
  2771. In the calendar, the date can be selected with mouse-1. However, the
  2772. minibuffer will also be active, and you can simply enter the date as well.
  2773. When nil, only the minibuffer will be available."
  2774. :group 'org-time
  2775. :type 'boolean)
  2776. (org-defvaralias 'org-popup-calendar-for-date-prompt
  2777. 'org-read-date-popup-calendar)
  2778. (make-obsolete-variable
  2779. 'org-read-date-minibuffer-setup-hook
  2780. "Set `org-read-date-minibuffer-local-map' instead." "24.4")
  2781. (defcustom org-read-date-minibuffer-setup-hook nil
  2782. "Hook to be used to set up keys for the date/time interface.
  2783. Add key definitions to `minibuffer-local-map', which will be a
  2784. temporary copy.
  2785. WARNING: This option is obsolete, you should use
  2786. `org-read-date-minibuffer-local-map' to set up keys."
  2787. :group 'org-time
  2788. :type 'hook)
  2789. (defcustom org-extend-today-until 0
  2790. "The hour when your day really ends. Must be an integer.
  2791. This has influence for the following applications:
  2792. - When switching the agenda to \"today\". It it is still earlier than
  2793. the time given here, the day recognized as TODAY is actually yesterday.
  2794. - When a date is read from the user and it is still before the time given
  2795. here, the current date and time will be assumed to be yesterday, 23:59.
  2796. Also, timestamps inserted in capture templates follow this rule.
  2797. IMPORTANT: This is a feature whose implementation is and likely will
  2798. remain incomplete. Really, it is only here because past midnight seems to
  2799. be the favorite working time of John Wiegley :-)"
  2800. :group 'org-time
  2801. :type 'integer)
  2802. (defcustom org-use-effective-time nil
  2803. "If non-nil, consider `org-extend-today-until' when creating timestamps.
  2804. For example, if `org-extend-today-until' is 8, and it's 4am, then the
  2805. \"effective time\" of any timestamps between midnight and 8am will be
  2806. 23:59 of the previous day."
  2807. :group 'org-time
  2808. :version "24.1"
  2809. :type 'boolean)
  2810. (defcustom org-use-last-clock-out-time-as-effective-time nil
  2811. "When non-nil, use the last clock out time for `org-todo'.
  2812. Note that this option has precedence over the combined use of
  2813. `org-use-effective-time' and `org-extend-today-until'."
  2814. :group 'org-time
  2815. :version "24.4"
  2816. :package-version '(Org . "8.0")
  2817. :type 'boolean)
  2818. (defcustom org-edit-timestamp-down-means-later nil
  2819. "Non-nil means S-down will increase the time in a time stamp.
  2820. When nil, S-up will increase."
  2821. :group 'org-time
  2822. :type 'boolean)
  2823. (defcustom org-calendar-follow-timestamp-change t
  2824. "Non-nil means make the calendar window follow timestamp changes.
  2825. When a timestamp is modified and the calendar window is visible, it will be
  2826. moved to the new date."
  2827. :group 'org-time
  2828. :type 'boolean)
  2829. (defgroup org-tags nil
  2830. "Options concerning tags in Org-mode."
  2831. :tag "Org Tags"
  2832. :group 'org)
  2833. (defcustom org-tag-alist nil
  2834. "List of tags allowed in Org-mode files.
  2835. When this list is nil, Org-mode will base TAG input on what is already in the
  2836. buffer.
  2837. The value of this variable is an alist, the car of each entry must be a
  2838. keyword as a string, the cdr may be a character that is used to select
  2839. that tag through the fast-tag-selection interface.
  2840. See the manual for details."
  2841. :group 'org-tags
  2842. :type '(repeat
  2843. (choice
  2844. (cons (string :tag "Tag name")
  2845. (character :tag "Access char"))
  2846. (list :tag "Start radio group"
  2847. (const :startgroup)
  2848. (option (string :tag "Group description")))
  2849. (list :tag "Group tags delimiter"
  2850. (const :grouptags))
  2851. (list :tag "End radio group"
  2852. (const :endgroup)
  2853. (option (string :tag "Group description")))
  2854. (const :tag "New line" (:newline)))))
  2855. (defcustom org-tag-persistent-alist nil
  2856. "List of tags that will always appear in all Org-mode files.
  2857. This is in addition to any in buffer settings or customizations
  2858. of `org-tag-alist'.
  2859. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2860. The value of this variable is an alist, the car of each entry must be a
  2861. keyword as a string, the cdr may be a character that is used to select
  2862. that tag through the fast-tag-selection interface.
  2863. See the manual for details.
  2864. To disable these tags on a per-file basis, insert anywhere in the file:
  2865. #+STARTUP: noptag"
  2866. :group 'org-tags
  2867. :type '(repeat
  2868. (choice
  2869. (cons (string :tag "Tag name")
  2870. (character :tag "Access char"))
  2871. (const :tag "Start radio group" (:startgroup))
  2872. (const :tag "Group tags delimiter" (:grouptags))
  2873. (const :tag "End radio group" (:endgroup))
  2874. (const :tag "New line" (:newline)))))
  2875. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2876. "If non-nil, always offer completion for all tags of all agenda files.
  2877. Instead of customizing this variable directly, you might want to
  2878. set it locally for capture buffers, because there no list of
  2879. tags in that file can be created dynamically (there are none).
  2880. (add-hook 'org-capture-mode-hook
  2881. (lambda ()
  2882. (set (make-local-variable
  2883. 'org-complete-tags-always-offer-all-agenda-tags)
  2884. t)))"
  2885. :group 'org-tags
  2886. :version "24.1"
  2887. :type 'boolean)
  2888. (defvar org-file-tags nil
  2889. "List of tags that can be inherited by all entries in the file.
  2890. The tags will be inherited if the variable `org-use-tag-inheritance'
  2891. says they should be.
  2892. This variable is populated from #+FILETAGS lines.")
  2893. (defcustom org-use-fast-tag-selection 'auto
  2894. "Non-nil means use fast tag selection scheme.
  2895. This is a special interface to select and deselect tags with single keys.
  2896. When nil, fast selection is never used.
  2897. When the symbol `auto', fast selection is used if and only if selection
  2898. characters for tags have been configured, either through the variable
  2899. `org-tag-alist' or through a #+TAGS line in the buffer.
  2900. When t, fast selection is always used and selection keys are assigned
  2901. automatically if necessary."
  2902. :group 'org-tags
  2903. :type '(choice
  2904. (const :tag "Always" t)
  2905. (const :tag "Never" nil)
  2906. (const :tag "When selection characters are configured" auto)))
  2907. (defcustom org-fast-tag-selection-single-key nil
  2908. "Non-nil means fast tag selection exits after first change.
  2909. When nil, you have to press RET to exit it.
  2910. During fast tag selection, you can toggle this flag with `C-c'.
  2911. This variable can also have the value `expert'. In this case, the window
  2912. displaying the tags menu is not even shown, until you press C-c again."
  2913. :group 'org-tags
  2914. :type '(choice
  2915. (const :tag "No" nil)
  2916. (const :tag "Yes" t)
  2917. (const :tag "Expert" expert)))
  2918. (defvar org-fast-tag-selection-include-todo nil
  2919. "Non-nil means fast tags selection interface will also offer TODO states.
  2920. This is an undocumented feature, you should not rely on it.")
  2921. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2922. "The column to which tags should be indented in a headline.
  2923. If this number is positive, it specifies the column. If it is negative,
  2924. it means that the tags should be flushright to that column. For example,
  2925. -80 works well for a normal 80 character screen.
  2926. When 0, place tags directly after headline text, with only one space in
  2927. between."
  2928. :group 'org-tags
  2929. :type 'integer)
  2930. (defcustom org-auto-align-tags t
  2931. "Non-nil keeps tags aligned when modifying headlines.
  2932. Some operations (i.e. demoting) change the length of a headline and
  2933. therefore shift the tags around. With this option turned on, after
  2934. each such operation the tags are again aligned to `org-tags-column'."
  2935. :group 'org-tags
  2936. :type 'boolean)
  2937. (defcustom org-use-tag-inheritance t
  2938. "Non-nil means tags in levels apply also for sublevels.
  2939. When nil, only the tags directly given in a specific line apply there.
  2940. This may also be a list of tags that should be inherited, or a regexp that
  2941. matches tags that should be inherited. Additional control is possible
  2942. with the variable `org-tags-exclude-from-inheritance' which gives an
  2943. explicit list of tags to be excluded from inheritance, even if the value of
  2944. `org-use-tag-inheritance' would select it for inheritance.
  2945. If this option is t, a match early-on in a tree can lead to a large
  2946. number of matches in the subtree when constructing the agenda or creating
  2947. a sparse tree. If you only want to see the first match in a tree during
  2948. a search, check out the variable `org-tags-match-list-sublevels'."
  2949. :group 'org-tags
  2950. :type '(choice
  2951. (const :tag "Not" nil)
  2952. (const :tag "Always" t)
  2953. (repeat :tag "Specific tags" (string :tag "Tag"))
  2954. (regexp :tag "Tags matched by regexp")))
  2955. (defcustom org-tags-exclude-from-inheritance nil
  2956. "List of tags that should never be inherited.
  2957. This is a way to exclude a few tags from inheritance. For way to do
  2958. the opposite, to actively allow inheritance for selected tags,
  2959. see the variable `org-use-tag-inheritance'."
  2960. :group 'org-tags
  2961. :type '(repeat (string :tag "Tag")))
  2962. (defun org-tag-inherit-p (tag)
  2963. "Check if TAG is one that should be inherited."
  2964. (cond
  2965. ((member tag org-tags-exclude-from-inheritance) nil)
  2966. ((eq org-use-tag-inheritance t) t)
  2967. ((not org-use-tag-inheritance) nil)
  2968. ((stringp org-use-tag-inheritance)
  2969. (string-match org-use-tag-inheritance tag))
  2970. ((listp org-use-tag-inheritance)
  2971. (member tag org-use-tag-inheritance))
  2972. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2973. (defcustom org-tags-match-list-sublevels t
  2974. "Non-nil means list also sublevels of headlines matching a search.
  2975. This variable applies to tags/property searches, and also to stuck
  2976. projects because this search is based on a tags match as well.
  2977. When set to the symbol `indented', sublevels are indented with
  2978. leading dots.
  2979. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2980. the sublevels of a headline matching a tag search often also match
  2981. the same search. Listing all of them can create very long lists.
  2982. Setting this variable to nil causes subtrees of a match to be skipped.
  2983. This variable is semi-obsolete and probably should always be true. It
  2984. is better to limit inheritance to certain tags using the variables
  2985. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2986. :group 'org-tags
  2987. :type '(choice
  2988. (const :tag "No, don't list them" nil)
  2989. (const :tag "Yes, do list them" t)
  2990. (const :tag "List them, indented with leading dots" indented)))
  2991. (defcustom org-tags-sort-function nil
  2992. "When set, tags are sorted using this function as a comparator."
  2993. :group 'org-tags
  2994. :type '(choice
  2995. (const :tag "No sorting" nil)
  2996. (const :tag "Alphabetical" string<)
  2997. (const :tag "Reverse alphabetical" string>)
  2998. (function :tag "Custom function" nil)))
  2999. (defvar org-tags-history nil
  3000. "History of minibuffer reads for tags.")
  3001. (defvar org-last-tags-completion-table nil
  3002. "The last used completion table for tags.")
  3003. (defvar org-after-tags-change-hook nil
  3004. "Hook that is run after the tags in a line have changed.")
  3005. (defgroup org-properties nil
  3006. "Options concerning properties in Org-mode."
  3007. :tag "Org Properties"
  3008. :group 'org)
  3009. (defcustom org-property-format "%-10s %s"
  3010. "How property key/value pairs should be formatted by `indent-line'.
  3011. When `indent-line' hits a property definition, it will format the line
  3012. according to this format, mainly to make sure that the values are
  3013. lined-up with respect to each other."
  3014. :group 'org-properties
  3015. :type 'string)
  3016. (defcustom org-properties-postprocess-alist nil
  3017. "Alist of properties and functions to adjust inserted values.
  3018. Elements of this alist must be of the form
  3019. ([string] [function])
  3020. where [string] must be a property name and [function] must be a
  3021. lambda expression: this lambda expression must take one argument,
  3022. the value to adjust, and return the new value as a string.
  3023. For example, this element will allow the property \"Remaining\"
  3024. to be updated wrt the relation between the \"Effort\" property
  3025. and the clock summary:
  3026. ((\"Remaining\" (lambda(value)
  3027. (let ((clocksum (org-clock-sum-current-item))
  3028. (effort (org-duration-string-to-minutes
  3029. (org-entry-get (point) \"Effort\"))))
  3030. (org-minutes-to-clocksum-string (- effort clocksum))))))"
  3031. :group 'org-properties
  3032. :version "24.1"
  3033. :type '(alist :key-type (string :tag "Property")
  3034. :value-type (function :tag "Function")))
  3035. (defcustom org-use-property-inheritance nil
  3036. "Non-nil means properties apply also for sublevels.
  3037. This setting is chiefly used during property searches. Turning it on can
  3038. cause significant overhead when doing a search, which is why it is not
  3039. on by default.
  3040. When nil, only the properties directly given in the current entry count.
  3041. When t, every property is inherited. The value may also be a list of
  3042. properties that should have inheritance, or a regular expression matching
  3043. properties that should be inherited.
  3044. However, note that some special properties use inheritance under special
  3045. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  3046. and the properties ending in \"_ALL\" when they are used as descriptor
  3047. for valid values of a property.
  3048. Note for programmers:
  3049. When querying an entry with `org-entry-get', you can control if inheritance
  3050. should be used. By default, `org-entry-get' looks only at the local
  3051. properties. You can request inheritance by setting the inherit argument
  3052. to t (to force inheritance) or to `selective' (to respect the setting
  3053. in this variable)."
  3054. :group 'org-properties
  3055. :type '(choice
  3056. (const :tag "Not" nil)
  3057. (const :tag "Always" t)
  3058. (repeat :tag "Specific properties" (string :tag "Property"))
  3059. (regexp :tag "Properties matched by regexp")))
  3060. (defun org-property-inherit-p (property)
  3061. "Check if PROPERTY is one that should be inherited."
  3062. (cond
  3063. ((eq org-use-property-inheritance t) t)
  3064. ((not org-use-property-inheritance) nil)
  3065. ((stringp org-use-property-inheritance)
  3066. (string-match org-use-property-inheritance property))
  3067. ((listp org-use-property-inheritance)
  3068. (member property org-use-property-inheritance))
  3069. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  3070. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  3071. "The default column format, if no other format has been defined.
  3072. This variable can be set on the per-file basis by inserting a line
  3073. #+COLUMNS: %25ITEM ....."
  3074. :group 'org-properties
  3075. :type 'string)
  3076. (defcustom org-columns-ellipses ".."
  3077. "The ellipses to be used when a field in column view is truncated.
  3078. When this is the empty string, as many characters as possible are shown,
  3079. but then there will be no visual indication that the field has been truncated.
  3080. When this is a string of length N, the last N characters of a truncated
  3081. field are replaced by this string. If the column is narrower than the
  3082. ellipses string, only part of the ellipses string will be shown."
  3083. :group 'org-properties
  3084. :type 'string)
  3085. (defcustom org-columns-modify-value-for-display-function nil
  3086. "Function that modifies values for display in column view.
  3087. For example, it can be used to cut out a certain part from a time stamp.
  3088. The function must take 2 arguments:
  3089. column-title The title of the column (*not* the property name)
  3090. value The value that should be modified.
  3091. The function should return the value that should be displayed,
  3092. or nil if the normal value should be used."
  3093. :group 'org-properties
  3094. :type '(choice (const nil) (function)))
  3095. (defcustom org-effort-property "Effort"
  3096. "The property that is being used to keep track of effort estimates.
  3097. Effort estimates given in this property need to have the format H:MM."
  3098. :group 'org-properties
  3099. :group 'org-progress
  3100. :type '(string :tag "Property"))
  3101. (defconst org-global-properties-fixed
  3102. '(("VISIBILITY_ALL" . "folded children content all")
  3103. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  3104. "List of property/value pairs that can be inherited by any entry.
  3105. These are fixed values, for the preset properties. The user variable
  3106. that can be used to add to this list is `org-global-properties'.
  3107. The entries in this list are cons cells where the car is a property
  3108. name and cdr is a string with the value. If the value represents
  3109. multiple items like an \"_ALL\" property, separate the items by
  3110. spaces.")
  3111. (defcustom org-global-properties nil
  3112. "List of property/value pairs that can be inherited by any entry.
  3113. This list will be combined with the constant `org-global-properties-fixed'.
  3114. The entries in this list are cons cells where the car is a property
  3115. name and cdr is a string with the value.
  3116. You can set buffer-local values for the same purpose in the variable
  3117. `org-file-properties' this by adding lines like
  3118. #+PROPERTY: NAME VALUE"
  3119. :group 'org-properties
  3120. :type '(repeat
  3121. (cons (string :tag "Property")
  3122. (string :tag "Value"))))
  3123. (defvar org-file-properties nil
  3124. "List of property/value pairs that can be inherited by any entry.
  3125. Valid for the current buffer.
  3126. This variable is populated from #+PROPERTY lines.")
  3127. (make-variable-buffer-local 'org-file-properties)
  3128. (defgroup org-agenda nil
  3129. "Options concerning agenda views in Org-mode."
  3130. :tag "Org Agenda"
  3131. :group 'org)
  3132. (defvar org-category nil
  3133. "Variable used by org files to set a category for agenda display.
  3134. Such files should use a file variable to set it, for example
  3135. # -*- mode: org; org-category: \"ELisp\"
  3136. or contain a special line
  3137. #+CATEGORY: ELisp
  3138. If the file does not specify a category, then file's base name
  3139. is used instead.")
  3140. (make-variable-buffer-local 'org-category)
  3141. (put 'org-category 'safe-local-variable #'(lambda (x) (or (symbolp x) (stringp x))))
  3142. (defcustom org-agenda-files nil
  3143. "The files to be used for agenda display.
  3144. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  3145. \\[org-remove-file]. You can also use customize to edit the list.
  3146. If an entry is a directory, all files in that directory that are matched by
  3147. `org-agenda-file-regexp' will be part of the file list.
  3148. If the value of the variable is not a list but a single file name, then
  3149. the list of agenda files is actually stored and maintained in that file, one
  3150. agenda file per line. In this file paths can be given relative to
  3151. `org-directory'. Tilde expansion and environment variable substitution
  3152. are also made."
  3153. :group 'org-agenda
  3154. :type '(choice
  3155. (repeat :tag "List of files and directories" file)
  3156. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  3157. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  3158. "Regular expression to match files for `org-agenda-files'.
  3159. If any element in the list in that variable contains a directory instead
  3160. of a normal file, all files in that directory that are matched by this
  3161. regular expression will be included."
  3162. :group 'org-agenda
  3163. :type 'regexp)
  3164. (defcustom org-agenda-text-search-extra-files nil
  3165. "List of extra files to be searched by text search commands.
  3166. These files will be searched in addition to the agenda files by the
  3167. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  3168. Note that these files will only be searched for text search commands,
  3169. not for the other agenda views like todo lists, tag searches or the weekly
  3170. agenda. This variable is intended to list notes and possibly archive files
  3171. that should also be searched by these two commands.
  3172. In fact, if the first element in the list is the symbol `agenda-archives',
  3173. then all archive files of all agenda files will be added to the search
  3174. scope."
  3175. :group 'org-agenda
  3176. :type '(set :greedy t
  3177. (const :tag "Agenda Archives" agenda-archives)
  3178. (repeat :inline t (file))))
  3179. (org-defvaralias 'org-agenda-multi-occur-extra-files
  3180. 'org-agenda-text-search-extra-files)
  3181. (defcustom org-agenda-skip-unavailable-files nil
  3182. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  3183. A nil value means to remove them, after a query, from the list."
  3184. :group 'org-agenda
  3185. :type 'boolean)
  3186. (defcustom org-calendar-to-agenda-key [?c]
  3187. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  3188. The command `org-calendar-goto-agenda' will be bound to this key. The
  3189. default is the character `c' because then `c' can be used to switch back and
  3190. forth between agenda and calendar."
  3191. :group 'org-agenda
  3192. :type 'sexp)
  3193. (defcustom org-calendar-insert-diary-entry-key [?i]
  3194. "The key to be installed in `calendar-mode-map' for adding diary entries.
  3195. This option is irrelevant until `org-agenda-diary-file' has been configured
  3196. to point to an Org-mode file. When that is the case, the command
  3197. `org-agenda-diary-entry' will be bound to the key given here, by default
  3198. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  3199. if you want to continue doing this, you need to change this to a different
  3200. key."
  3201. :group 'org-agenda
  3202. :type 'sexp)
  3203. (defcustom org-agenda-diary-file 'diary-file
  3204. "File to which to add new entries with the `i' key in agenda and calendar.
  3205. When this is the symbol `diary-file', the functionality in the Emacs
  3206. calendar will be used to add entries to the `diary-file'. But when this
  3207. points to a file, `org-agenda-diary-entry' will be used instead."
  3208. :group 'org-agenda
  3209. :type '(choice
  3210. (const :tag "The standard Emacs diary file" diary-file)
  3211. (file :tag "Special Org file diary entries")))
  3212. (eval-after-load "calendar"
  3213. '(progn
  3214. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  3215. 'org-calendar-goto-agenda)
  3216. (add-hook 'calendar-mode-hook
  3217. (lambda ()
  3218. (unless (eq org-agenda-diary-file 'diary-file)
  3219. (define-key calendar-mode-map
  3220. org-calendar-insert-diary-entry-key
  3221. 'org-agenda-diary-entry))))))
  3222. (defgroup org-latex nil
  3223. "Options for embedding LaTeX code into Org-mode."
  3224. :tag "Org LaTeX"
  3225. :group 'org)
  3226. (defcustom org-format-latex-options
  3227. '(:foreground default :background default :scale 1.0
  3228. :html-foreground "Black" :html-background "Transparent"
  3229. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  3230. "Options for creating images from LaTeX fragments.
  3231. This is a property list with the following properties:
  3232. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  3233. `default' means use the foreground of the default face.
  3234. `auto' means use the foreground from the text face.
  3235. :background the background color, or \"Transparent\".
  3236. `default' means use the background of the default face.
  3237. `auto' means use the background from the text face.
  3238. :scale a scaling factor for the size of the images, to get more pixels
  3239. :html-foreground, :html-background, :html-scale
  3240. the same numbers for HTML export.
  3241. :matchers a list indicating which matchers should be used to
  3242. find LaTeX fragments. Valid members of this list are:
  3243. \"begin\" find environments
  3244. \"$1\" find single characters surrounded by $.$
  3245. \"$\" find math expressions surrounded by $...$
  3246. \"$$\" find math expressions surrounded by $$....$$
  3247. \"\\(\" find math expressions surrounded by \\(...\\)
  3248. \"\\=\\[\" find math expressions surrounded by \\=\\[...\\]"
  3249. :group 'org-latex
  3250. :type 'plist)
  3251. (defcustom org-format-latex-signal-error t
  3252. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  3253. When nil, just push out a message."
  3254. :group 'org-latex
  3255. :version "24.1"
  3256. :type 'boolean)
  3257. (defcustom org-latex-to-mathml-jar-file nil
  3258. "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
  3259. Use this to specify additional executable file say a jar file.
  3260. When using MathToWeb as the converter, specify the full-path to
  3261. your mathtoweb.jar file."
  3262. :group 'org-latex
  3263. :version "24.1"
  3264. :type '(choice
  3265. (const :tag "None" nil)
  3266. (file :tag "JAR file" :must-match t)))
  3267. (defcustom org-latex-to-mathml-convert-command nil
  3268. "Command to convert LaTeX fragments to MathML.
  3269. Replace format-specifiers in the command as noted below and use
  3270. `shell-command' to convert LaTeX to MathML.
  3271. %j: Executable file in fully expanded form as specified by
  3272. `org-latex-to-mathml-jar-file'.
  3273. %I: Input LaTeX file in fully expanded form
  3274. %o: Output MathML file
  3275. This command is used by `org-create-math-formula'.
  3276. When using MathToWeb as the converter, set this to
  3277. \"java -jar %j -unicode -force -df %o %I\"."
  3278. :group 'org-latex
  3279. :version "24.1"
  3280. :type '(choice
  3281. (const :tag "None" nil)
  3282. (string :tag "\nShell command")))
  3283. (defcustom org-latex-create-formula-image-program 'dvipng
  3284. "Program to convert LaTeX fragments with.
  3285. dvipng Process the LaTeX fragments to dvi file, then convert
  3286. dvi files to png files using dvipng.
  3287. This will also include processing of non-math environments.
  3288. imagemagick Convert the LaTeX fragments to pdf files and use imagemagick
  3289. to convert pdf files to png files"
  3290. :group 'org-latex
  3291. :version "24.1"
  3292. :type '(choice
  3293. (const :tag "dvipng" dvipng)
  3294. (const :tag "imagemagick" imagemagick)))
  3295. (defcustom org-latex-preview-ltxpng-directory "ltxpng/"
  3296. "Path to store latex preview images.
  3297. A relative path here creates many directories relative to the
  3298. processed org files paths. An absolute path puts all preview
  3299. images at the same place."
  3300. :group 'org-latex
  3301. :version "24.3"
  3302. :type 'string)
  3303. (defun org-format-latex-mathml-available-p ()
  3304. "Return t if `org-latex-to-mathml-convert-command' is usable."
  3305. (save-match-data
  3306. (when (and (boundp 'org-latex-to-mathml-convert-command)
  3307. org-latex-to-mathml-convert-command)
  3308. (let ((executable (car (split-string
  3309. org-latex-to-mathml-convert-command))))
  3310. (when (executable-find executable)
  3311. (if (string-match
  3312. "%j" org-latex-to-mathml-convert-command)
  3313. (file-readable-p org-latex-to-mathml-jar-file)
  3314. t))))))
  3315. (defcustom org-format-latex-header "\\documentclass{article}
  3316. \\usepackage[usenames]{color}
  3317. \[PACKAGES]
  3318. \[DEFAULT-PACKAGES]
  3319. \\pagestyle{empty} % do not remove
  3320. % The settings below are copied from fullpage.sty
  3321. \\setlength{\\textwidth}{\\paperwidth}
  3322. \\addtolength{\\textwidth}{-3cm}
  3323. \\setlength{\\oddsidemargin}{1.5cm}
  3324. \\addtolength{\\oddsidemargin}{-2.54cm}
  3325. \\setlength{\\evensidemargin}{\\oddsidemargin}
  3326. \\setlength{\\textheight}{\\paperheight}
  3327. \\addtolength{\\textheight}{-\\headheight}
  3328. \\addtolength{\\textheight}{-\\headsep}
  3329. \\addtolength{\\textheight}{-\\footskip}
  3330. \\addtolength{\\textheight}{-3cm}
  3331. \\setlength{\\topmargin}{1.5cm}
  3332. \\addtolength{\\topmargin}{-2.54cm}"
  3333. "The document header used for processing LaTeX fragments.
  3334. It is imperative that this header make sure that no page number
  3335. appears on the page. The package defined in the variables
  3336. `org-latex-default-packages-alist' and `org-latex-packages-alist'
  3337. will either replace the placeholder \"[PACKAGES]\" in this
  3338. header, or they will be appended."
  3339. :group 'org-latex
  3340. :type 'string)
  3341. (defun org-set-packages-alist (var val)
  3342. "Set the packages alist and make sure it has 3 elements per entry."
  3343. (set var (mapcar (lambda (x)
  3344. (if (and (consp x) (= (length x) 2))
  3345. (list (car x) (nth 1 x) t)
  3346. x))
  3347. val)))
  3348. (defun org-get-packages-alist (var)
  3349. "Get the packages alist and make sure it has 3 elements per entry."
  3350. (mapcar (lambda (x)
  3351. (if (and (consp x) (= (length x) 2))
  3352. (list (car x) (nth 1 x) t)
  3353. x))
  3354. (default-value var)))
  3355. (defcustom org-latex-default-packages-alist
  3356. '(("AUTO" "inputenc" t)
  3357. ("T1" "fontenc" t)
  3358. ("" "fixltx2e" nil)
  3359. ("" "graphicx" t)
  3360. ("" "longtable" nil)
  3361. ("" "float" nil)
  3362. ("" "wrapfig" nil)
  3363. ("" "rotating" nil)
  3364. ("normalem" "ulem" t)
  3365. ("" "amsmath" t)
  3366. ("" "textcomp" t)
  3367. ("" "marvosym" t)
  3368. ("" "wasysym" t)
  3369. ("" "amssymb" t)
  3370. ("" "hyperref" nil)
  3371. "\\tolerance=1000")
  3372. "Alist of default packages to be inserted in the header.
  3373. Change this only if one of the packages here causes an
  3374. incompatibility with another package you are using.
  3375. The packages in this list are needed by one part or another of
  3376. Org mode to function properly:
  3377. - inputenc, fontenc: for basic font and character selection
  3378. - fixltx2e: Important patches of LaTeX itself
  3379. - graphicx: for including images
  3380. - longtable: For multipage tables
  3381. - float, wrapfig: for figure placement
  3382. - rotating: for sideways figures and tables
  3383. - ulem: for underline and strike-through
  3384. - amsmath: for subscript and superscript and math environments
  3385. - textcomp, marvosymb, wasysym, amssymb: for various symbols used
  3386. for interpreting the entities in `org-entities'. You can skip
  3387. some of these packages if you don't use any of their symbols.
  3388. - hyperref: for cross references
  3389. Therefore you should not modify this variable unless you know
  3390. what you are doing. The one reason to change it anyway is that
  3391. you might be loading some other package that conflicts with one
  3392. of the default packages. Each element is either a cell or
  3393. a string.
  3394. A cell is of the format:
  3395. \( \"options\" \"package\" SNIPPET-FLAG).
  3396. If SNIPPET-FLAG is non-nil, the package also needs to be included
  3397. when compiling LaTeX snippets into images for inclusion into
  3398. non-LaTeX output.
  3399. A string will be inserted as-is in the header of the document."
  3400. :group 'org-latex
  3401. :group 'org-export-latex
  3402. :set 'org-set-packages-alist
  3403. :get 'org-get-packages-alist
  3404. :version "24.1"
  3405. :type '(repeat
  3406. (choice
  3407. (list :tag "options/package pair"
  3408. (string :tag "options")
  3409. (string :tag "package")
  3410. (boolean :tag "Snippet"))
  3411. (string :tag "A line of LaTeX"))))
  3412. (defcustom org-latex-packages-alist nil
  3413. "Alist of packages to be inserted in every LaTeX header.
  3414. These will be inserted after `org-latex-default-packages-alist'.
  3415. Each element is either a cell or a string.
  3416. A cell is of the format:
  3417. \(\"options\" \"package\" SNIPPET-FLAG)
  3418. SNIPPET-FLAG, when non-nil, indicates that this package is also
  3419. needed when turning LaTeX snippets into images for inclusion into
  3420. non-LaTeX output.
  3421. A string will be inserted as-is in the header of the document.
  3422. Make sure that you only list packages here which:
  3423. - you want in every file;
  3424. - do not conflict with the setup in `org-format-latex-header';
  3425. - do not conflict with the default packages in
  3426. `org-latex-default-packages-alist'."
  3427. :group 'org-latex
  3428. :group 'org-export-latex
  3429. :set 'org-set-packages-alist
  3430. :get 'org-get-packages-alist
  3431. :type '(repeat
  3432. (choice
  3433. (list :tag "options/package pair"
  3434. (string :tag "options")
  3435. (string :tag "package")
  3436. (boolean :tag "Snippet"))
  3437. (string :tag "A line of LaTeX"))))
  3438. (defgroup org-appearance nil
  3439. "Settings for Org-mode appearance."
  3440. :tag "Org Appearance"
  3441. :group 'org)
  3442. (defcustom org-level-color-stars-only nil
  3443. "Non-nil means fontify only the stars in each headline.
  3444. When nil, the entire headline is fontified.
  3445. Changing it requires restart of `font-lock-mode' to become effective
  3446. also in regions already fontified."
  3447. :group 'org-appearance
  3448. :type 'boolean)
  3449. (defcustom org-hide-leading-stars nil
  3450. "Non-nil means hide the first N-1 stars in a headline.
  3451. This works by using the face `org-hide' for these stars. This
  3452. face is white for a light background, and black for a dark
  3453. background. You may have to customize the face `org-hide' to
  3454. make this work.
  3455. Changing it requires restart of `font-lock-mode' to become effective
  3456. also in regions already fontified.
  3457. You may also set this on a per-file basis by adding one of the following
  3458. lines to the buffer:
  3459. #+STARTUP: hidestars
  3460. #+STARTUP: showstars"
  3461. :group 'org-appearance
  3462. :type 'boolean)
  3463. (defcustom org-hidden-keywords nil
  3464. "List of symbols corresponding to keywords to be hidden the org buffer.
  3465. For example, a value '(title) for this list will make the document's title
  3466. appear in the buffer without the initial #+TITLE: keyword."
  3467. :group 'org-appearance
  3468. :version "24.1"
  3469. :type '(set (const :tag "#+AUTHOR" author)
  3470. (const :tag "#+DATE" date)
  3471. (const :tag "#+EMAIL" email)
  3472. (const :tag "#+TITLE" title)))
  3473. (defcustom org-custom-properties nil
  3474. "List of properties (as strings) with a special meaning.
  3475. The default use of these custom properties is to let the user
  3476. hide them with `org-toggle-custom-properties-visibility'."
  3477. :group 'org-properties
  3478. :group 'org-appearance
  3479. :version "24.3"
  3480. :type '(repeat (string :tag "Property Name")))
  3481. (defcustom org-fontify-done-headline nil
  3482. "Non-nil means change the face of a headline if it is marked DONE.
  3483. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3484. When this is non-nil, the headline after the keyword is set to the
  3485. `org-headline-done' as an additional indication."
  3486. :group 'org-appearance
  3487. :type 'boolean)
  3488. (defcustom org-fontify-emphasized-text t
  3489. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3490. Changing this variable requires a restart of Emacs to take effect."
  3491. :group 'org-appearance
  3492. :type 'boolean)
  3493. (defcustom org-fontify-whole-heading-line nil
  3494. "Non-nil means fontify the whole line for headings.
  3495. This is useful when setting a background color for the
  3496. org-level-* faces."
  3497. :group 'org-appearance
  3498. :type 'boolean)
  3499. (defcustom org-highlight-latex-and-related nil
  3500. "Non-nil means highlight LaTeX related syntax in the buffer.
  3501. When non nil, the value should be a list containing any of the
  3502. following symbols:
  3503. `latex' Highlight LaTeX snippets and environments.
  3504. `script' Highlight subscript and superscript.
  3505. `entities' Highlight entities."
  3506. :group 'org-appearance
  3507. :version "24.4"
  3508. :package-version '(Org . "8.0")
  3509. :type '(choice
  3510. (const :tag "No highlighting" nil)
  3511. (set :greedy t :tag "Highlight"
  3512. (const :tag "LaTeX snippets and environments" latex)
  3513. (const :tag "Subscript and superscript" script)
  3514. (const :tag "Entities" entities))))
  3515. (defcustom org-hide-emphasis-markers nil
  3516. "Non-nil mean font-lock should hide the emphasis marker characters."
  3517. :group 'org-appearance
  3518. :type 'boolean)
  3519. (defcustom org-pretty-entities nil
  3520. "Non-nil means show entities as UTF8 characters.
  3521. When nil, the \\name form remains in the buffer."
  3522. :group 'org-appearance
  3523. :version "24.1"
  3524. :type 'boolean)
  3525. (defcustom org-pretty-entities-include-sub-superscripts t
  3526. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  3527. :group 'org-appearance
  3528. :version "24.1"
  3529. :type 'boolean)
  3530. (defvar org-emph-re nil
  3531. "Regular expression for matching emphasis.
  3532. After a match, the match groups contain these elements:
  3533. 0 The match of the full regular expression, including the characters
  3534. before and after the proper match
  3535. 1 The character before the proper match, or empty at beginning of line
  3536. 2 The proper match, including the leading and trailing markers
  3537. 3 The leading marker like * or /, indicating the type of highlighting
  3538. 4 The text between the emphasis markers, not including the markers
  3539. 5 The character after the match, empty at the end of a line")
  3540. (defvar org-verbatim-re nil
  3541. "Regular expression for matching verbatim text.")
  3542. (defvar org-emphasis-regexp-components) ; defined just below
  3543. (defvar org-emphasis-alist) ; defined just below
  3544. (defun org-set-emph-re (var val)
  3545. "Set variable and compute the emphasis regular expression."
  3546. (set var val)
  3547. (when (and (boundp 'org-emphasis-alist)
  3548. (boundp 'org-emphasis-regexp-components)
  3549. org-emphasis-alist org-emphasis-regexp-components)
  3550. (let* ((e org-emphasis-regexp-components)
  3551. (pre (car e))
  3552. (post (nth 1 e))
  3553. (border (nth 2 e))
  3554. (body (nth 3 e))
  3555. (nl (nth 4 e))
  3556. (body1 (concat body "*?"))
  3557. (markers (mapconcat 'car org-emphasis-alist ""))
  3558. (vmarkers (mapconcat
  3559. (lambda (x) (if (eq (nth 2 x) 'verbatim) (car x) ""))
  3560. org-emphasis-alist "")))
  3561. ;; make sure special characters appear at the right position in the class
  3562. (if (string-match "\\^" markers)
  3563. (setq markers (concat (replace-match "" t t markers) "^")))
  3564. (if (string-match "-" markers)
  3565. (setq markers (concat (replace-match "" t t markers) "-")))
  3566. (if (string-match "\\^" vmarkers)
  3567. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3568. (if (string-match "-" vmarkers)
  3569. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3570. (if (> nl 0)
  3571. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3572. (int-to-string nl) "\\}")))
  3573. ;; Make the regexp
  3574. (setq org-emph-re
  3575. (concat "\\([" pre "]\\|^\\)"
  3576. "\\("
  3577. "\\([" markers "]\\)"
  3578. "\\("
  3579. "[^" border "]\\|"
  3580. "[^" border "]"
  3581. body1
  3582. "[^" border "]"
  3583. "\\)"
  3584. "\\3\\)"
  3585. "\\([" post "]\\|$\\)"))
  3586. (setq org-verbatim-re
  3587. (concat "\\([" pre "]\\|^\\)"
  3588. "\\("
  3589. "\\([" vmarkers "]\\)"
  3590. "\\("
  3591. "[^" border "]\\|"
  3592. "[^" border "]"
  3593. body1
  3594. "[^" border "]"
  3595. "\\)"
  3596. "\\3\\)"
  3597. "\\([" post "]\\|$\\)")))))
  3598. ;; This used to be a defcustom (Org <8.0) but allowing the users to
  3599. ;; set this option proved cumbersome. See this message/thread:
  3600. ;; http://article.gmane.org/gmane.emacs.orgmode/68681
  3601. (defvar org-emphasis-regexp-components
  3602. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  3603. "Components used to build the regular expression for emphasis.
  3604. This is a list with five entries. Terminology: In an emphasis string
  3605. like \" *strong word* \", we call the initial space PREMATCH, the final
  3606. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3607. and \"trong wor\" is the body. The different components in this variable
  3608. specify what is allowed/forbidden in each part:
  3609. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3610. post Chars allowed as postmatch. End of line will be allowed too.
  3611. border The chars *forbidden* as border characters.
  3612. body-regexp A regexp like \".\" to match a body character. Don't use
  3613. non-shy groups here, and don't allow newline here.
  3614. newline The maximum number of newlines allowed in an emphasis exp.
  3615. You need to reload Org or to restart Emacs after customizing this.")
  3616. (defcustom org-emphasis-alist
  3617. `(("*" bold)
  3618. ("/" italic)
  3619. ("_" underline)
  3620. ("=" org-verbatim verbatim)
  3621. ("~" org-code verbatim)
  3622. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))))
  3623. "Alist of characters and faces to emphasize text.
  3624. Text starting and ending with a special character will be emphasized,
  3625. for example *bold*, _underlined_ and /italic/. This variable sets the
  3626. marker characters and the face to be used by font-lock for highlighting
  3627. in Org-mode Emacs buffers.
  3628. You need to reload Org or to restart Emacs after customizing this."
  3629. :group 'org-appearance
  3630. :set 'org-set-emph-re
  3631. :version "24.4"
  3632. :package-version '(Org . "8.0")
  3633. :type '(repeat
  3634. (list
  3635. (string :tag "Marker character")
  3636. (choice
  3637. (face :tag "Font-lock-face")
  3638. (plist :tag "Face property list"))
  3639. (option (const verbatim)))))
  3640. (defvar org-protecting-blocks
  3641. '("src" "example" "latex" "ascii" "html" "ditaa" "dot" "r" "R")
  3642. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3643. This is needed for font-lock setup.")
  3644. ;;; Miscellaneous options
  3645. (defgroup org-completion nil
  3646. "Completion in Org-mode."
  3647. :tag "Org Completion"
  3648. :group 'org)
  3649. (defcustom org-completion-use-ido nil
  3650. "Non-nil means use ido completion wherever possible.
  3651. Note that `ido-mode' must be active for this variable to be relevant.
  3652. If you decide to turn this variable on, you might well want to turn off
  3653. `org-outline-path-complete-in-steps'.
  3654. See also `org-completion-use-iswitchb'."
  3655. :group 'org-completion
  3656. :type 'boolean)
  3657. (defcustom org-completion-use-iswitchb nil
  3658. "Non-nil means use iswitchb completion wherever possible.
  3659. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3660. If you decide to turn this variable on, you might well want to turn off
  3661. `org-outline-path-complete-in-steps'.
  3662. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3663. :group 'org-completion
  3664. :type 'boolean)
  3665. (defcustom org-completion-fallback-command 'hippie-expand
  3666. "The expansion command called by \\[pcomplete] in normal context.
  3667. Normal means, no org-mode-specific context."
  3668. :group 'org-completion
  3669. :type 'function)
  3670. ;;; Functions and variables from their packages
  3671. ;; Declared here to avoid compiler warnings
  3672. ;; XEmacs only
  3673. (defvar outline-mode-menu-heading)
  3674. (defvar outline-mode-menu-show)
  3675. (defvar outline-mode-menu-hide)
  3676. (defvar zmacs-regions) ; XEmacs regions
  3677. ;; Emacs only
  3678. (defvar mark-active)
  3679. ;; Various packages
  3680. (declare-function calendar-iso-to-absolute "cal-iso" (date))
  3681. (declare-function calendar-forward-day "cal-move" (arg))
  3682. (declare-function calendar-goto-date "cal-move" (date))
  3683. (declare-function calendar-goto-today "cal-move" ())
  3684. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3685. (defvar calc-embedded-close-formula)
  3686. (defvar calc-embedded-open-formula)
  3687. (declare-function cdlatex-tab "ext:cdlatex" ())
  3688. (declare-function cdlatex-compute-tables "ext:cdlatex" ())
  3689. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3690. (defvar font-lock-unfontify-region-function)
  3691. (declare-function iswitchb-read-buffer "iswitchb"
  3692. (prompt &optional default require-match start matches-set))
  3693. (defvar iswitchb-temp-buflist)
  3694. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3695. (defvar org-agenda-tags-todo-honor-ignore-options)
  3696. (declare-function org-agenda-skip "org-agenda" ())
  3697. (declare-function
  3698. org-agenda-format-item "org-agenda"
  3699. (extra txt &optional level category tags dotime noprefix remove-re habitp))
  3700. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3701. (declare-function org-agenda-change-all-lines "org-agenda"
  3702. (newhead hdmarker &optional fixface just-this))
  3703. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3704. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3705. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3706. (beg end))
  3707. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3708. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3709. "org-agenda" (&optional end))
  3710. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3711. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3712. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3713. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3714. (declare-function org-indent-mode "org-indent" (&optional arg))
  3715. (declare-function parse-time-string "parse-time" (string))
  3716. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3717. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3718. (defvar remember-data-file)
  3719. (defvar texmathp-why)
  3720. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3721. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3722. (defvar org-latex-regexps)
  3723. ;;; Autoload and prepare some org modules
  3724. ;; Some table stuff that needs to be defined here, because it is used
  3725. ;; by the functions setting up org-mode or checking for table context.
  3726. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  3727. "Detect an org-type or table-type table.")
  3728. (defconst org-table-line-regexp "^[ \t]*|"
  3729. "Detect an org-type table line.")
  3730. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  3731. "Detect an org-type table line.")
  3732. (defconst org-table-hline-regexp "^[ \t]*|-"
  3733. "Detect an org-type table hline.")
  3734. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  3735. "Detect a table-type table hline.")
  3736. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  3737. "Detect the first line outside a table when searching from within it.
  3738. This works for both table types.")
  3739. (defconst org-TBLFM-regexp "^[ \t]*#\\+TBLFM: "
  3740. "Detect a #+TBLFM line.")
  3741. ;;;###autoload
  3742. (defun turn-on-orgtbl ()
  3743. "Unconditionally turn on `orgtbl-mode'."
  3744. (require 'org-table)
  3745. (orgtbl-mode 1))
  3746. (defun org-at-table-p (&optional table-type)
  3747. "Return t if the cursor is inside an org-type table.
  3748. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  3749. (if org-enable-table-editor
  3750. (save-excursion
  3751. (beginning-of-line 1)
  3752. (looking-at (if table-type org-table-any-line-regexp
  3753. org-table-line-regexp)))
  3754. nil))
  3755. (defsubst org-table-p () (org-at-table-p))
  3756. (defun org-at-table.el-p ()
  3757. "Return t if and only if we are at a table.el table."
  3758. (and (org-at-table-p 'any)
  3759. (save-excursion
  3760. (goto-char (org-table-begin 'any))
  3761. (looking-at org-table1-hline-regexp))))
  3762. (defun org-table-recognize-table.el ()
  3763. "If there is a table.el table nearby, recognize it and move into it."
  3764. (if org-table-tab-recognizes-table.el
  3765. (if (org-at-table.el-p)
  3766. (progn
  3767. (beginning-of-line 1)
  3768. (if (looking-at org-table-dataline-regexp)
  3769. nil
  3770. (if (looking-at org-table1-hline-regexp)
  3771. (progn
  3772. (beginning-of-line 2)
  3773. (if (looking-at org-table-any-border-regexp)
  3774. (beginning-of-line -1)))))
  3775. (if (re-search-forward "|" (org-table-end t) t)
  3776. (progn
  3777. (require 'table)
  3778. (if (table--at-cell-p (point))
  3779. t
  3780. (message "recognizing table.el table...")
  3781. (table-recognize-table)
  3782. (message "recognizing table.el table...done")))
  3783. (error "This should not happen"))
  3784. t)
  3785. nil)
  3786. nil))
  3787. (defun org-at-table-hline-p ()
  3788. "Return t if the cursor is inside a hline in a table."
  3789. (if org-enable-table-editor
  3790. (save-excursion
  3791. (beginning-of-line 1)
  3792. (looking-at org-table-hline-regexp))
  3793. nil))
  3794. (defun org-table-map-tables (function &optional quietly)
  3795. "Apply FUNCTION to the start of all tables in the buffer."
  3796. (save-excursion
  3797. (save-restriction
  3798. (widen)
  3799. (goto-char (point-min))
  3800. (while (re-search-forward org-table-any-line-regexp nil t)
  3801. (unless quietly
  3802. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
  3803. (beginning-of-line 1)
  3804. (when (and (looking-at org-table-line-regexp)
  3805. ;; Exclude tables in src/example/verbatim/clocktable blocks
  3806. (not (org-in-block-p '("src" "example" "verbatim" "clocktable"))))
  3807. (save-excursion (funcall function))
  3808. (or (looking-at org-table-line-regexp)
  3809. (forward-char 1)))
  3810. (re-search-forward org-table-any-border-regexp nil 1))))
  3811. (unless quietly (message "Mapping tables: done")))
  3812. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock" (beg end))
  3813. (declare-function org-clock-update-mode-line "org-clock" ())
  3814. (declare-function org-resolve-clocks "org-clock"
  3815. (&optional also-non-dangling-p prompt last-valid))
  3816. (defun org-at-TBLFM-p (&optional pos)
  3817. "Return t when point (or POS) is in #+TBLFM line."
  3818. (save-excursion
  3819. (let ((pos pos)))
  3820. (goto-char (or pos (point)))
  3821. (beginning-of-line 1)
  3822. (looking-at org-TBLFM-regexp)))
  3823. (defvar org-clock-start-time)
  3824. (defvar org-clock-marker (make-marker)
  3825. "Marker recording the last clock-in.")
  3826. (defvar org-clock-hd-marker (make-marker)
  3827. "Marker recording the last clock-in, but the headline position.")
  3828. (defvar org-clock-heading ""
  3829. "The heading of the current clock entry.")
  3830. (defun org-clock-is-active ()
  3831. "Return the buffer where the clock is currently running.
  3832. Return nil if no clock is running."
  3833. (marker-buffer org-clock-marker))
  3834. (defun org-check-running-clock ()
  3835. "Check if the current buffer contains the running clock.
  3836. If yes, offer to stop it and to save the buffer with the changes."
  3837. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3838. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3839. (buffer-name))))
  3840. (org-clock-out)
  3841. (when (y-or-n-p "Save changed buffer?")
  3842. (save-buffer))))
  3843. (defun org-clocktable-try-shift (dir n)
  3844. "Check if this line starts a clock table, if yes, shift the time block."
  3845. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  3846. (org-clocktable-shift dir n)))
  3847. ;;;###autoload
  3848. (defun org-clock-persistence-insinuate ()
  3849. "Set up hooks for clock persistence."
  3850. (require 'org-clock)
  3851. (add-hook 'org-mode-hook 'org-clock-load)
  3852. (add-hook 'kill-emacs-hook 'org-clock-save))
  3853. (defgroup org-archive nil
  3854. "Options concerning archiving in Org-mode."
  3855. :tag "Org Archive"
  3856. :group 'org-structure)
  3857. (defcustom org-archive-location "%s_archive::"
  3858. "The location where subtrees should be archived.
  3859. The value of this variable is a string, consisting of two parts,
  3860. separated by a double-colon. The first part is a filename and
  3861. the second part is a headline.
  3862. When the filename is omitted, archiving happens in the same file.
  3863. %s in the filename will be replaced by the current file
  3864. name (without the directory part). Archiving to a different file
  3865. is useful to keep archived entries from contributing to the
  3866. Org-mode Agenda.
  3867. The archived entries will be filed as subtrees of the specified
  3868. headline. When the headline is omitted, the subtrees are simply
  3869. filed away at the end of the file, as top-level entries. Also in
  3870. the heading you can use %s to represent the file name, this can be
  3871. useful when using the same archive for a number of different files.
  3872. Here are a few examples:
  3873. \"%s_archive::\"
  3874. If the current file is Projects.org, archive in file
  3875. Projects.org_archive, as top-level trees. This is the default.
  3876. \"::* Archived Tasks\"
  3877. Archive in the current file, under the top-level headline
  3878. \"* Archived Tasks\".
  3879. \"~/org/archive.org::\"
  3880. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3881. \"~/org/archive.org::* From %s\"
  3882. Archive in file ~/org/archive.org (absolute path), under headlines
  3883. \"From FILENAME\" where file name is the current file name.
  3884. \"~/org/datetree.org::datetree/* Finished Tasks\"
  3885. The \"datetree/\" string is special, signifying to archive
  3886. items to the datetree. Items are placed in either the CLOSED
  3887. date of the item, or the current date if there is no CLOSED date.
  3888. The heading will be a subentry to the current date. There doesn't
  3889. need to be a heading, but there always needs to be a slash after
  3890. datetree. For example, to store archived items directly in the
  3891. datetree, use \"~/org/datetree.org::datetree/\".
  3892. \"basement::** Finished Tasks\"
  3893. Archive in file ./basement (relative path), as level 3 trees
  3894. below the level 2 heading \"** Finished Tasks\".
  3895. You may set this option on a per-file basis by adding to the buffer a
  3896. line like
  3897. #+ARCHIVE: basement::** Finished Tasks
  3898. You may also define it locally for a subtree by setting an ARCHIVE property
  3899. in the entry. If such a property is found in an entry, or anywhere up
  3900. the hierarchy, it will be used."
  3901. :group 'org-archive
  3902. :type 'string)
  3903. (defcustom org-archive-tag "ARCHIVE"
  3904. "The tag that marks a subtree as archived.
  3905. An archived subtree does not open during visibility cycling, and does
  3906. not contribute to the agenda listings.
  3907. After changing this, font-lock must be restarted in the relevant buffers to
  3908. get the proper fontification."
  3909. :group 'org-archive
  3910. :group 'org-keywords
  3911. :type 'string)
  3912. (defcustom org-agenda-skip-archived-trees t
  3913. "Non-nil means the agenda will skip any items located in archived trees.
  3914. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3915. variable is no longer recommended, you should leave it at the value t.
  3916. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3917. :group 'org-archive
  3918. :group 'org-agenda-skip
  3919. :type 'boolean)
  3920. (defcustom org-columns-skip-archived-trees t
  3921. "Non-nil means ignore archived trees when creating column view."
  3922. :group 'org-archive
  3923. :group 'org-properties
  3924. :type 'boolean)
  3925. (defcustom org-cycle-open-archived-trees nil
  3926. "Non-nil means `org-cycle' will open archived trees.
  3927. An archived tree is a tree marked with the tag ARCHIVE.
  3928. When nil, archived trees will stay folded. You can still open them with
  3929. normal outline commands like `show-all', but not with the cycling commands."
  3930. :group 'org-archive
  3931. :group 'org-cycle
  3932. :type 'boolean)
  3933. (defcustom org-sparse-tree-open-archived-trees nil
  3934. "Non-nil means sparse tree construction shows matches in archived trees.
  3935. When nil, matches in these trees are highlighted, but the trees are kept in
  3936. collapsed state."
  3937. :group 'org-archive
  3938. :group 'org-sparse-trees
  3939. :type 'boolean)
  3940. (defcustom org-sparse-tree-default-date-type 'scheduled-or-deadline
  3941. "The default date type when building a sparse tree.
  3942. When this is nil, a date is a scheduled or a deadline timestamp.
  3943. Otherwise, these types are allowed:
  3944. all: all timestamps
  3945. active: only active timestamps (<...>)
  3946. inactive: only inactive timestamps (<...)
  3947. scheduled: only scheduled timestamps
  3948. deadline: only deadline timestamps"
  3949. :type '(choice (const :tag "Scheduled or deadline" scheduled-or-deadline)
  3950. (const :tag "All timestamps" all)
  3951. (const :tag "Only active timestamps" active)
  3952. (const :tag "Only inactive timestamps" inactive)
  3953. (const :tag "Only scheduled timestamps" scheduled)
  3954. (const :tag "Only deadline timestamps" deadline)
  3955. (const :tag "Only closed timestamps" closed))
  3956. :version "24.3"
  3957. :group 'org-sparse-trees)
  3958. (defun org-cycle-hide-archived-subtrees (state)
  3959. "Re-hide all archived subtrees after a visibility state change."
  3960. (when (and (not org-cycle-open-archived-trees)
  3961. (not (memq state '(overview folded))))
  3962. (save-excursion
  3963. (let* ((globalp (memq state '(contents all)))
  3964. (beg (if globalp (point-min) (point)))
  3965. (end (if globalp (point-max) (org-end-of-subtree t))))
  3966. (org-hide-archived-subtrees beg end)
  3967. (goto-char beg)
  3968. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3969. (message "%s" (substitute-command-keys
  3970. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3971. (defun org-force-cycle-archived ()
  3972. "Cycle subtree even if it is archived."
  3973. (interactive)
  3974. (setq this-command 'org-cycle)
  3975. (let ((org-cycle-open-archived-trees t))
  3976. (call-interactively 'org-cycle)))
  3977. (defun org-hide-archived-subtrees (beg end)
  3978. "Re-hide all archived subtrees after a visibility state change."
  3979. (save-excursion
  3980. (let* ((re (concat ":" org-archive-tag ":")))
  3981. (goto-char beg)
  3982. (while (re-search-forward re end t)
  3983. (when (org-at-heading-p)
  3984. (org-flag-subtree t)
  3985. (org-end-of-subtree t))))))
  3986. (declare-function outline-end-of-heading "outline" ())
  3987. (declare-function outline-flag-region "outline" (from to flag))
  3988. (defun org-flag-subtree (flag)
  3989. (save-excursion
  3990. (org-back-to-heading t)
  3991. (outline-end-of-heading)
  3992. (outline-flag-region (point)
  3993. (progn (org-end-of-subtree t) (point))
  3994. flag)))
  3995. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3996. ;; Declare Column View Code
  3997. (declare-function org-columns-number-to-string "org-colview" (n fmt &optional printf))
  3998. (declare-function org-columns-get-format-and-top-level "org-colview" ())
  3999. (declare-function org-columns-compute "org-colview" (property))
  4000. ;; Declare ID code
  4001. (declare-function org-id-store-link "org-id")
  4002. (declare-function org-id-locations-load "org-id")
  4003. (declare-function org-id-locations-save "org-id")
  4004. (defvar org-id-track-globally)
  4005. ;;; Variables for pre-computed regular expressions, all buffer local
  4006. (defvar org-drawer-regexp "^[ \t]*:PROPERTIES:[ \t]*$"
  4007. "Matches first line of a hidden block.")
  4008. (make-variable-buffer-local 'org-drawer-regexp)
  4009. (defvar org-todo-regexp nil
  4010. "Matches any of the TODO state keywords.")
  4011. (make-variable-buffer-local 'org-todo-regexp)
  4012. (defvar org-not-done-regexp nil
  4013. "Matches any of the TODO state keywords except the last one.")
  4014. (make-variable-buffer-local 'org-not-done-regexp)
  4015. (defvar org-not-done-heading-regexp nil
  4016. "Matches a TODO headline that is not done.")
  4017. (make-variable-buffer-local 'org-not-done-regexp)
  4018. (defvar org-todo-line-regexp nil
  4019. "Matches a headline and puts TODO state into group 2 if present.")
  4020. (make-variable-buffer-local 'org-todo-line-regexp)
  4021. (defvar org-complex-heading-regexp nil
  4022. "Matches a headline and puts everything into groups:
  4023. group 1: the stars
  4024. group 2: The todo keyword, maybe
  4025. group 3: Priority cookie
  4026. group 4: True headline
  4027. group 5: Tags")
  4028. (make-variable-buffer-local 'org-complex-heading-regexp)
  4029. (defvar org-complex-heading-regexp-format nil
  4030. "Printf format to make regexp to match an exact headline.
  4031. This regexp will match the headline of any node which has the
  4032. exact headline text that is put into the format, but may have any
  4033. TODO state, priority and tags.")
  4034. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  4035. (defvar org-todo-line-tags-regexp nil
  4036. "Matches a headline and puts TODO state into group 2 if present.
  4037. Also put tags into group 4 if tags are present.")
  4038. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  4039. (defvar org-ds-keyword-length 12
  4040. "Maximum length of the DEADLINE and SCHEDULED keywords.")
  4041. (make-variable-buffer-local 'org-ds-keyword-length)
  4042. (defvar org-deadline-regexp nil
  4043. "Matches the DEADLINE keyword.")
  4044. (make-variable-buffer-local 'org-deadline-regexp)
  4045. (defvar org-deadline-time-regexp nil
  4046. "Matches the DEADLINE keyword together with a time stamp.")
  4047. (make-variable-buffer-local 'org-deadline-time-regexp)
  4048. (defvar org-deadline-time-hour-regexp nil
  4049. "Matches the DEADLINE keyword together with a time-and-hour stamp.")
  4050. (make-variable-buffer-local 'org-deadline-time-hour-regexp)
  4051. (defvar org-deadline-line-regexp nil
  4052. "Matches the DEADLINE keyword and the rest of the line.")
  4053. (make-variable-buffer-local 'org-deadline-line-regexp)
  4054. (defvar org-scheduled-regexp nil
  4055. "Matches the SCHEDULED keyword.")
  4056. (make-variable-buffer-local 'org-scheduled-regexp)
  4057. (defvar org-scheduled-time-regexp nil
  4058. "Matches the SCHEDULED keyword together with a time stamp.")
  4059. (make-variable-buffer-local 'org-scheduled-time-regexp)
  4060. (defvar org-scheduled-time-hour-regexp nil
  4061. "Matches the SCHEDULED keyword together with a time-and-hour stamp.")
  4062. (make-variable-buffer-local 'org-scheduled-time-hour-regexp)
  4063. (defvar org-closed-time-regexp nil
  4064. "Matches the CLOSED keyword together with a time stamp.")
  4065. (make-variable-buffer-local 'org-closed-time-regexp)
  4066. (defvar org-keyword-time-regexp nil
  4067. "Matches any of the 4 keywords, together with the time stamp.")
  4068. (make-variable-buffer-local 'org-keyword-time-regexp)
  4069. (defvar org-keyword-time-not-clock-regexp nil
  4070. "Matches any of the 3 keywords, together with the time stamp.")
  4071. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  4072. (defvar org-maybe-keyword-time-regexp nil
  4073. "Matches a timestamp, possibly preceded by a keyword.")
  4074. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  4075. (defvar org-all-time-keywords nil
  4076. "List of time keywords.")
  4077. (make-variable-buffer-local 'org-all-time-keywords)
  4078. (defconst org-plain-time-of-day-regexp
  4079. (concat
  4080. "\\(\\<[012]?[0-9]"
  4081. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4082. "\\(--?"
  4083. "\\(\\<[012]?[0-9]"
  4084. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4085. "\\)?")
  4086. "Regular expression to match a plain time or time range.
  4087. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4088. groups carry important information:
  4089. 0 the full match
  4090. 1 the first time, range or not
  4091. 8 the second time, if it is a range.")
  4092. (defconst org-plain-time-extension-regexp
  4093. (concat
  4094. "\\(\\<[012]?[0-9]"
  4095. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4096. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  4097. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  4098. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4099. groups carry important information:
  4100. 0 the full match
  4101. 7 hours of duration
  4102. 9 minutes of duration")
  4103. (defconst org-stamp-time-of-day-regexp
  4104. (concat
  4105. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  4106. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  4107. "\\(--?"
  4108. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  4109. "Regular expression to match a timestamp time or time range.
  4110. After a match, the following groups carry important information:
  4111. 0 the full match
  4112. 1 date plus weekday, for back referencing to make sure both times are on the same day
  4113. 2 the first time, range or not
  4114. 4 the second time, if it is a range.")
  4115. (defconst org-startup-options
  4116. '(("fold" org-startup-folded t)
  4117. ("overview" org-startup-folded t)
  4118. ("nofold" org-startup-folded nil)
  4119. ("showall" org-startup-folded nil)
  4120. ("showeverything" org-startup-folded showeverything)
  4121. ("content" org-startup-folded content)
  4122. ("indent" org-startup-indented t)
  4123. ("noindent" org-startup-indented nil)
  4124. ("hidestars" org-hide-leading-stars t)
  4125. ("showstars" org-hide-leading-stars nil)
  4126. ("odd" org-odd-levels-only t)
  4127. ("oddeven" org-odd-levels-only nil)
  4128. ("align" org-startup-align-all-tables t)
  4129. ("noalign" org-startup-align-all-tables nil)
  4130. ("inlineimages" org-startup-with-inline-images t)
  4131. ("noinlineimages" org-startup-with-inline-images nil)
  4132. ("latexpreview" org-startup-with-latex-preview t)
  4133. ("nolatexpreview" org-startup-with-latex-preview nil)
  4134. ("customtime" org-display-custom-times t)
  4135. ("logdone" org-log-done time)
  4136. ("lognotedone" org-log-done note)
  4137. ("nologdone" org-log-done nil)
  4138. ("lognoteclock-out" org-log-note-clock-out t)
  4139. ("nolognoteclock-out" org-log-note-clock-out nil)
  4140. ("logrepeat" org-log-repeat state)
  4141. ("lognoterepeat" org-log-repeat note)
  4142. ("logdrawer" org-log-into-drawer t)
  4143. ("nologdrawer" org-log-into-drawer nil)
  4144. ("logstatesreversed" org-log-states-order-reversed t)
  4145. ("nologstatesreversed" org-log-states-order-reversed nil)
  4146. ("nologrepeat" org-log-repeat nil)
  4147. ("logreschedule" org-log-reschedule time)
  4148. ("lognotereschedule" org-log-reschedule note)
  4149. ("nologreschedule" org-log-reschedule nil)
  4150. ("logredeadline" org-log-redeadline time)
  4151. ("lognoteredeadline" org-log-redeadline note)
  4152. ("nologredeadline" org-log-redeadline nil)
  4153. ("logrefile" org-log-refile time)
  4154. ("lognoterefile" org-log-refile note)
  4155. ("nologrefile" org-log-refile nil)
  4156. ("fninline" org-footnote-define-inline t)
  4157. ("nofninline" org-footnote-define-inline nil)
  4158. ("fnlocal" org-footnote-section nil)
  4159. ("fnauto" org-footnote-auto-label t)
  4160. ("fnprompt" org-footnote-auto-label nil)
  4161. ("fnconfirm" org-footnote-auto-label confirm)
  4162. ("fnplain" org-footnote-auto-label plain)
  4163. ("fnadjust" org-footnote-auto-adjust t)
  4164. ("nofnadjust" org-footnote-auto-adjust nil)
  4165. ("constcgs" constants-unit-system cgs)
  4166. ("constSI" constants-unit-system SI)
  4167. ("noptag" org-tag-persistent-alist nil)
  4168. ("hideblocks" org-hide-block-startup t)
  4169. ("nohideblocks" org-hide-block-startup nil)
  4170. ("beamer" org-startup-with-beamer-mode t)
  4171. ("entitiespretty" org-pretty-entities t)
  4172. ("entitiesplain" org-pretty-entities nil))
  4173. "Variable associated with STARTUP options for org-mode.
  4174. Each element is a list of three items: the startup options (as written
  4175. in the #+STARTUP line), the corresponding variable, and the value to set
  4176. this variable to if the option is found. An optional forth element PUSH
  4177. means to push this value onto the list in the variable.")
  4178. (defun org-update-property-plist (key val props)
  4179. "Update PROPS with KEY and VAL."
  4180. (let* ((appending (string= "+" (substring key (- (length key) 1))))
  4181. (key (if appending (substring key 0 (- (length key) 1)) key))
  4182. (remainder (org-remove-if (lambda (p) (string= (car p) key)) props))
  4183. (previous (cdr (assoc key props))))
  4184. (if appending
  4185. (cons (cons key (if previous (concat previous " " val) val)) remainder)
  4186. (cons (cons key val) remainder))))
  4187. (defconst org-block-regexp
  4188. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  4189. "Regular expression for hiding blocks.")
  4190. (defconst org-heading-keyword-regexp-format
  4191. "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  4192. "Printf format for a regexp matching a headline with some keyword.
  4193. This regexp will match the headline of any node which has the
  4194. exact keyword that is put into the format. The keyword isn't in
  4195. any group by default, but the stars and the body are.")
  4196. (defconst org-heading-keyword-maybe-regexp-format
  4197. "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
  4198. "Printf format for a regexp matching a headline, possibly with some keyword.
  4199. This regexp can match any headline with the specified keyword, or
  4200. without a keyword. The keyword isn't in any group by default,
  4201. but the stars and the body are.")
  4202. (defcustom org-group-tags t
  4203. "When non-nil (the default), use group tags.
  4204. This can be turned on/off through `org-toggle-tags-groups'."
  4205. :group 'org-tags
  4206. :group 'org-startup
  4207. :type 'boolean)
  4208. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4209. (defun org-toggle-tags-groups ()
  4210. "Toggle support for group tags.
  4211. Support for group tags is controlled by the option
  4212. `org-group-tags', which is non-nil by default."
  4213. (interactive)
  4214. (setq org-group-tags (not org-group-tags))
  4215. (cond ((and (derived-mode-p 'org-agenda-mode)
  4216. org-group-tags)
  4217. (org-agenda-redo))
  4218. ((derived-mode-p 'org-mode)
  4219. (let ((org-inhibit-startup t)) (org-mode))))
  4220. (message "Groups tags support has been turned %s"
  4221. (if org-group-tags "on" "off")))
  4222. (defun org-set-regexps-and-options-for-tags ()
  4223. "Precompute variables used for tags."
  4224. (when (derived-mode-p 'org-mode)
  4225. (org-set-local 'org-file-tags nil)
  4226. (let ((re (org-make-options-regexp '("FILETAGS" "TAGS")))
  4227. (splitre "[ \t]+")
  4228. (start 0)
  4229. tags ftags key value)
  4230. (save-excursion
  4231. (save-restriction
  4232. (widen)
  4233. (goto-char (point-min))
  4234. (while (re-search-forward re nil t)
  4235. (setq key (upcase (org-match-string-no-properties 1))
  4236. value (org-match-string-no-properties 2))
  4237. (if (stringp value) (setq value (org-trim value)))
  4238. (cond
  4239. ((equal key "TAGS")
  4240. (setq tags (append tags (if tags '("\\n") nil)
  4241. (org-split-string value splitre))))
  4242. ((equal key "FILETAGS")
  4243. (when (string-match "\\S-" value)
  4244. (setq ftags
  4245. (append
  4246. ftags
  4247. (apply 'append
  4248. (mapcar (lambda (x) (org-split-string x ":"))
  4249. (org-split-string value)))))))))))
  4250. ;; Process the file tags.
  4251. (and ftags (org-set-local 'org-file-tags
  4252. (mapcar 'org-add-prop-inherited ftags)))
  4253. (org-set-local 'org-tag-groups-alist nil)
  4254. ;; Process the tags.
  4255. (when (and (not tags) org-tag-alist)
  4256. (setq tags
  4257. (mapcar
  4258. (lambda (tg) (cond ((eq (car tg) :startgroup) "{")
  4259. ((eq (car tg) :endgroup) "}")
  4260. ((eq (car tg) :grouptags) ":")
  4261. ((eq (car tg) :newline) "\n")
  4262. (t (concat (car tg)
  4263. (if (characterp (cdr tg))
  4264. (format "(%s)" (char-to-string (cdr tg))) "")))))
  4265. org-tag-alist)))
  4266. (let (e tgs g)
  4267. (while (setq e (pop tags))
  4268. (cond
  4269. ((equal e "{")
  4270. (progn (push '(:startgroup) tgs)
  4271. (when (equal (nth 1 tags) ":")
  4272. (push (list (replace-regexp-in-string
  4273. "(.+)$" "" (nth 0 tags)))
  4274. org-tag-groups-alist)
  4275. (setq g 0))))
  4276. ((equal e ":") (push '(:grouptags) tgs))
  4277. ((equal e "}") (push '(:endgroup) tgs) (if g (setq g nil)))
  4278. ((equal e "\\n") (push '(:newline) tgs))
  4279. ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e)
  4280. (push (cons (match-string 1 e)
  4281. (string-to-char (match-string 2 e))) tgs)
  4282. (if (and g (> g 0))
  4283. (setcar org-tag-groups-alist
  4284. (append (car org-tag-groups-alist)
  4285. (list (match-string 1 e)))))
  4286. (if g (setq g (1+ g))))
  4287. (t (push (list e) tgs)
  4288. (if (and g (> g 0))
  4289. (setcar org-tag-groups-alist
  4290. (append (car org-tag-groups-alist) (list e))))
  4291. (if g (setq g (1+ g))))))
  4292. (org-set-local 'org-tag-alist nil)
  4293. (while (setq e (pop tgs))
  4294. (or (and (stringp (car e))
  4295. (assoc (car e) org-tag-alist))
  4296. (push e org-tag-alist)))
  4297. ;; Return a list with tag variables
  4298. (list org-file-tags org-tag-alist org-tag-groups-alist)))))
  4299. (defvar org-ota nil)
  4300. (defun org-set-regexps-and-options ()
  4301. "Precompute regular expressions used in the current buffer."
  4302. (when (derived-mode-p 'org-mode)
  4303. (org-set-local 'org-todo-kwd-alist nil)
  4304. (org-set-local 'org-todo-key-alist nil)
  4305. (org-set-local 'org-todo-key-trigger nil)
  4306. (org-set-local 'org-todo-keywords-1 nil)
  4307. (org-set-local 'org-done-keywords nil)
  4308. (org-set-local 'org-todo-heads nil)
  4309. (org-set-local 'org-todo-sets nil)
  4310. (org-set-local 'org-todo-log-states nil)
  4311. (org-set-local 'org-file-properties nil)
  4312. (let ((re (org-make-options-regexp
  4313. '("CATEGORY" "TODO" "COLUMNS" "STARTUP" "ARCHIVE"
  4314. "LINK" "PRIORITIES" "CONSTANTS" "PROPERTY" "DRAWERS"
  4315. "SETUPFILE" "OPTIONS")
  4316. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  4317. (splitre "[ \t]+")
  4318. (scripts org-use-sub-superscripts)
  4319. kwds kws0 kwsa key log value cat arch const links hw dws
  4320. tail sep kws1 prio props drawers ext-setup-or-nil setup-contents
  4321. (start 0))
  4322. (save-excursion
  4323. (save-restriction
  4324. (widen)
  4325. (goto-char (point-min))
  4326. (while
  4327. (or (and
  4328. ext-setup-or-nil
  4329. (not org-ota)
  4330. (let (ret)
  4331. (with-temp-buffer
  4332. (insert ext-setup-or-nil)
  4333. (let ((major-mode 'org-mode) org-ota)
  4334. (setq ret (save-match-data
  4335. (org-set-regexps-and-options-for-tags)))))
  4336. ;; Append setupfile tags to existing tags
  4337. (setq org-ota t)
  4338. (setq org-file-tags
  4339. (delq nil (append org-file-tags (nth 0 ret)))
  4340. org-tag-alist
  4341. (delq nil (append org-tag-alist (nth 1 ret)))
  4342. org-tag-groups-alist
  4343. (delq nil (append org-tag-groups-alist (nth 2 ret))))))
  4344. (and ext-setup-or-nil
  4345. (string-match re ext-setup-or-nil start)
  4346. (setq start (match-end 0)))
  4347. (and (setq ext-setup-or-nil nil start 0)
  4348. (re-search-forward re nil t)))
  4349. (setq key (upcase (match-string 1 ext-setup-or-nil))
  4350. value (org-match-string-no-properties 2 ext-setup-or-nil))
  4351. (if (stringp value) (setq value (org-trim value)))
  4352. (cond
  4353. ((equal key "CATEGORY")
  4354. (setq cat value))
  4355. ((member key '("SEQ_TODO" "TODO"))
  4356. (push (cons 'sequence (org-split-string value splitre)) kwds))
  4357. ((equal key "TYP_TODO")
  4358. (push (cons 'type (org-split-string value splitre)) kwds))
  4359. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  4360. ;; general TODO-like setup
  4361. (push (cons (intern (downcase (match-string 1 key)))
  4362. (org-split-string value splitre)) kwds))
  4363. ((equal key "COLUMNS")
  4364. (org-set-local 'org-columns-default-format value))
  4365. ((equal key "LINK")
  4366. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  4367. (push (cons (match-string 1 value)
  4368. (org-trim (match-string 2 value)))
  4369. links)))
  4370. ((equal key "PRIORITIES")
  4371. (setq prio (org-split-string value " +")))
  4372. ((equal key "PROPERTY")
  4373. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4374. (setq props (org-update-property-plist (match-string 1 value)
  4375. (match-string 2 value)
  4376. props))))
  4377. ((equal key "DRAWERS")
  4378. (setq drawers (delete-dups (append org-drawers (org-split-string value splitre)))))
  4379. ((equal key "CONSTANTS")
  4380. (org-table-set-constants))
  4381. ((equal key "STARTUP")
  4382. (let ((opts (org-split-string value splitre))
  4383. l var val)
  4384. (while (setq l (pop opts))
  4385. (when (setq l (assoc l org-startup-options))
  4386. (setq var (nth 1 l) val (nth 2 l))
  4387. (if (not (nth 3 l))
  4388. (set (make-local-variable var) val)
  4389. (if (not (listp (symbol-value var)))
  4390. (set (make-local-variable var) nil))
  4391. (set (make-local-variable var) (symbol-value var))
  4392. (add-to-list var val))))))
  4393. ((equal key "ARCHIVE")
  4394. (setq arch value)
  4395. (remove-text-properties 0 (length arch)
  4396. '(face t fontified t) arch))
  4397. ((equal key "OPTIONS")
  4398. (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value)
  4399. (setq scripts (read (match-string 2 value)))))
  4400. ((and (equal key "SETUPFILE")
  4401. ;; Prevent checking in Gnus messages
  4402. (not buffer-read-only))
  4403. (setq setup-contents (org-file-contents
  4404. (expand-file-name
  4405. (org-remove-double-quotes value))
  4406. 'noerror))
  4407. (if (not ext-setup-or-nil)
  4408. (setq ext-setup-or-nil setup-contents start 0)
  4409. (setq ext-setup-or-nil
  4410. (concat (substring ext-setup-or-nil 0 start)
  4411. "\n" setup-contents "\n"
  4412. (substring ext-setup-or-nil start)))))))
  4413. ;; search for property blocks
  4414. (goto-char (point-min))
  4415. (while (re-search-forward org-block-regexp nil t)
  4416. (when (equal "PROPERTY" (upcase (match-string 1)))
  4417. (setq value (replace-regexp-in-string
  4418. "[\n\r]" " " (match-string 4)))
  4419. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4420. (setq props (org-update-property-plist (match-string 1 value)
  4421. (match-string 2 value)
  4422. props)))))))
  4423. (org-set-local 'org-use-sub-superscripts scripts)
  4424. (when cat
  4425. (org-set-local 'org-category (intern cat))
  4426. (push (cons "CATEGORY" cat) props))
  4427. (when prio
  4428. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  4429. (setq prio (mapcar 'string-to-char prio))
  4430. (org-set-local 'org-highest-priority (nth 0 prio))
  4431. (org-set-local 'org-lowest-priority (nth 1 prio))
  4432. (org-set-local 'org-default-priority (nth 2 prio)))
  4433. (and props (org-set-local 'org-file-properties (nreverse props)))
  4434. (and drawers (org-set-local 'org-drawers drawers))
  4435. (and arch (org-set-local 'org-archive-location arch))
  4436. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  4437. ;; Process the TODO keywords
  4438. (unless kwds
  4439. ;; Use the global values as if they had been given locally.
  4440. (setq kwds (default-value 'org-todo-keywords))
  4441. (if (stringp (car kwds))
  4442. (setq kwds (list (cons org-todo-interpretation
  4443. (default-value 'org-todo-keywords)))))
  4444. (setq kwds (reverse kwds)))
  4445. (setq kwds (nreverse kwds))
  4446. (let (inter kws kw)
  4447. (while (setq kws (pop kwds))
  4448. (let ((kws (or
  4449. (run-hook-with-args-until-success
  4450. 'org-todo-setup-filter-hook kws)
  4451. kws)))
  4452. (setq inter (pop kws) sep (member "|" kws)
  4453. kws0 (delete "|" (copy-sequence kws))
  4454. kwsa nil
  4455. kws1 (mapcar
  4456. (lambda (x)
  4457. ;; 1 2
  4458. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  4459. (progn
  4460. (setq kw (match-string 1 x)
  4461. key (and (match-end 2) (match-string 2 x))
  4462. log (org-extract-log-state-settings x))
  4463. (push (cons kw (and key (string-to-char key))) kwsa)
  4464. (and log (push log org-todo-log-states))
  4465. kw)
  4466. (error "Invalid TODO keyword %s" x)))
  4467. kws0)
  4468. kwsa (if kwsa (append '((:startgroup))
  4469. (nreverse kwsa)
  4470. '((:endgroup))))
  4471. hw (car kws1)
  4472. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  4473. tail (list inter hw (car dws) (org-last dws))))
  4474. (add-to-list 'org-todo-heads hw 'append)
  4475. (push kws1 org-todo-sets)
  4476. (setq org-done-keywords (append org-done-keywords dws nil))
  4477. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  4478. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  4479. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  4480. (setq org-todo-sets (nreverse org-todo-sets)
  4481. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4482. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  4483. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  4484. ;; Compute the regular expressions and other local variables.
  4485. ;; Using `org-outline-regexp-bol' would complicate them much,
  4486. ;; because of the fixed white space at the end of that string.
  4487. (if (not org-done-keywords)
  4488. (setq org-done-keywords (and org-todo-keywords-1
  4489. (list (org-last org-todo-keywords-1)))))
  4490. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  4491. (length org-scheduled-string)
  4492. (length org-clock-string)
  4493. (length org-closed-string)))
  4494. org-drawer-regexp
  4495. (concat "^[ \t]*:\\("
  4496. (mapconcat 'regexp-quote org-drawers "\\|")
  4497. "\\):[ \t]*$")
  4498. org-not-done-keywords
  4499. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  4500. org-todo-regexp
  4501. (concat "\\("
  4502. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4503. "\\)")
  4504. org-not-done-regexp
  4505. (concat "\\("
  4506. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  4507. "\\)")
  4508. org-not-done-heading-regexp
  4509. (format org-heading-keyword-regexp-format org-not-done-regexp)
  4510. org-todo-line-regexp
  4511. (format org-heading-keyword-maybe-regexp-format org-todo-regexp)
  4512. org-complex-heading-regexp
  4513. (concat "^\\(\\*+\\)"
  4514. "\\(?: +" org-todo-regexp "\\)?"
  4515. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4516. "\\(?: +\\(.*?\\)\\)??"
  4517. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
  4518. "[ \t]*$")
  4519. org-complex-heading-regexp-format
  4520. (concat "^\\(\\*+\\)"
  4521. "\\(?: +" org-todo-regexp "\\)?"
  4522. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4523. "\\(?: +"
  4524. ;; Stats cookies can be stuck to body.
  4525. "\\(?:\\[[0-9%%/]+\\] *\\)*"
  4526. "\\(%s\\)"
  4527. "\\(?: *\\[[0-9%%/]+\\]\\)*"
  4528. "\\)"
  4529. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
  4530. "[ \t]*$")
  4531. org-todo-line-tags-regexp
  4532. (concat "^\\(\\*+\\)"
  4533. "\\(?: +" org-todo-regexp "\\)?"
  4534. "\\(?: +\\(.*?\\)\\)??"
  4535. (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
  4536. "[ \t]*$")
  4537. org-deadline-regexp (concat "\\<" org-deadline-string)
  4538. org-deadline-time-regexp
  4539. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  4540. org-deadline-time-hour-regexp
  4541. (concat "\\<" org-deadline-string
  4542. " *<\\([^>]+[0-9]\\{1,2\\}:[0-9]\\{2\\}[0-9-+:hdwmy \t.]*\\)>")
  4543. org-deadline-line-regexp
  4544. (concat "\\<\\(" org-deadline-string "\\).*")
  4545. org-scheduled-regexp
  4546. (concat "\\<" org-scheduled-string)
  4547. org-scheduled-time-regexp
  4548. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  4549. org-scheduled-time-hour-regexp
  4550. (concat "\\<" org-scheduled-string
  4551. " *<\\([^>]+[0-9]\\{1,2\\}:[0-9]\\{2\\}[0-9-+:hdwmy \t.]*\\)>")
  4552. org-closed-time-regexp
  4553. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  4554. org-keyword-time-regexp
  4555. (concat "\\<\\(" org-scheduled-string
  4556. "\\|" org-deadline-string
  4557. "\\|" org-closed-string
  4558. "\\|" org-clock-string "\\)"
  4559. " *[[<]\\([^]>]+\\)[]>]")
  4560. org-keyword-time-not-clock-regexp
  4561. (concat "\\<\\(" org-scheduled-string
  4562. "\\|" org-deadline-string
  4563. "\\|" org-closed-string
  4564. "\\)"
  4565. " *[[<]\\([^]>]+\\)[]>]")
  4566. org-maybe-keyword-time-regexp
  4567. (concat "\\(\\<\\(" org-scheduled-string
  4568. "\\|" org-deadline-string
  4569. "\\|" org-closed-string
  4570. "\\|" org-clock-string "\\)\\)?"
  4571. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4572. org-all-time-keywords
  4573. (mapcar (lambda (w) (substring w 0 -1))
  4574. (list org-scheduled-string org-deadline-string
  4575. org-clock-string org-closed-string)))
  4576. (setq org-ota nil)
  4577. (org-compute-latex-and-related-regexp))))
  4578. (defun org-file-contents (file &optional noerror)
  4579. "Return the contents of FILE, as a string."
  4580. (if (or (not file) (not (file-readable-p file)))
  4581. (if (not noerror)
  4582. (error "Cannot read file \"%s\"" file)
  4583. (message "Cannot read file \"%s\"" file)
  4584. "")
  4585. (with-temp-buffer
  4586. (insert-file-contents file)
  4587. (buffer-string))))
  4588. (defun org-extract-log-state-settings (x)
  4589. "Extract the log state setting from a TODO keyword string.
  4590. This will extract info from a string like \"WAIT(w@/!)\"."
  4591. (let (kw key log1 log2)
  4592. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4593. (setq kw (match-string 1 x)
  4594. key (and (match-end 2) (match-string 2 x))
  4595. log1 (and (match-end 3) (match-string 3 x))
  4596. log2 (and (match-end 4) (match-string 4 x)))
  4597. (and (or log1 log2)
  4598. (list kw
  4599. (and log1 (if (equal log1 "!") 'time 'note))
  4600. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4601. (defun org-remove-keyword-keys (list)
  4602. "Remove a pair of parenthesis at the end of each string in LIST."
  4603. (mapcar (lambda (x)
  4604. (if (string-match "(.*)$" x)
  4605. (substring x 0 (match-beginning 0))
  4606. x))
  4607. list))
  4608. (defun org-assign-fast-keys (alist)
  4609. "Assign fast keys to a keyword-key alist.
  4610. Respect keys that are already there."
  4611. (let (new e (alt ?0))
  4612. (while (setq e (pop alist))
  4613. (if (or (memq (car e) '(:newline :grouptags :endgroup :startgroup))
  4614. (cdr e)) ;; Key already assigned.
  4615. (push e new)
  4616. (let ((clist (string-to-list (downcase (car e))))
  4617. (used (append new alist)))
  4618. (when (= (car clist) ?@)
  4619. (pop clist))
  4620. (while (and clist (rassoc (car clist) used))
  4621. (pop clist))
  4622. (unless clist
  4623. (while (rassoc alt used)
  4624. (incf alt)))
  4625. (push (cons (car e) (or (car clist) alt)) new))))
  4626. (nreverse new)))
  4627. ;;; Some variables used in various places
  4628. (defvar org-window-configuration nil
  4629. "Used in various places to store a window configuration.")
  4630. (defvar org-selected-window nil
  4631. "Used in various places to store a window configuration.")
  4632. (defvar org-finish-function nil
  4633. "Function to be called when `C-c C-c' is used.
  4634. This is for getting out of special buffers like capture.")
  4635. ;; FIXME: Occasionally check by commenting these, to make sure
  4636. ;; no other functions uses these, forgetting to let-bind them.
  4637. (org-no-warnings (defvar entry)) ;; unprefixed, from calendar.el
  4638. (defvar org-last-state)
  4639. (org-no-warnings (defvar date)) ;; unprefixed, from calendar.el
  4640. ;; Defined somewhere in this file, but used before definition.
  4641. (defvar org-entities) ;; defined in org-entities.el
  4642. (defvar org-struct-menu)
  4643. (defvar org-org-menu)
  4644. (defvar org-tbl-menu)
  4645. ;;;; Define the Org-mode
  4646. ;; We use a before-change function to check if a table might need
  4647. ;; an update.
  4648. (defvar org-table-may-need-update t
  4649. "Indicates that a table might need an update.
  4650. This variable is set by `org-before-change-function'.
  4651. `org-table-align' sets it back to nil.")
  4652. (defun org-before-change-function (beg end)
  4653. "Every change indicates that a table might need an update."
  4654. (setq org-table-may-need-update t))
  4655. (defvar org-mode-map)
  4656. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4657. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4658. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4659. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4660. (defvar org-table-buffer-is-an nil)
  4661. (defvar bidi-paragraph-direction)
  4662. (defvar buffer-face-mode-face)
  4663. (require 'outline)
  4664. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4665. (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"))
  4666. (require 'noutline "noutline" 'noerror) ;; stock XEmacs does not have it
  4667. ;; Other stuff we need.
  4668. (require 'time-date)
  4669. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  4670. (require 'easymenu)
  4671. (require 'overlay)
  4672. ;; (require 'org-macs) moved higher up in the file before it is first used
  4673. (require 'org-entities)
  4674. ;; (require 'org-compat) moved higher up in the file before it is first used
  4675. (require 'org-faces)
  4676. (require 'org-list)
  4677. (require 'org-pcomplete)
  4678. (require 'org-src)
  4679. (require 'org-footnote)
  4680. (require 'org-macro)
  4681. ;; babel
  4682. (require 'ob)
  4683. ;;;###autoload
  4684. (define-derived-mode org-mode outline-mode "Org"
  4685. "Outline-based notes management and organizer, alias
  4686. \"Carsten's outline-mode for keeping track of everything.\"
  4687. Org-mode develops organizational tasks around a NOTES file which
  4688. contains information about projects as plain text. Org-mode is
  4689. implemented on top of outline-mode, which is ideal to keep the content
  4690. of large files well structured. It supports ToDo items, deadlines and
  4691. time stamps, which magically appear in the diary listing of the Emacs
  4692. calendar. Tables are easily created with a built-in table editor.
  4693. Plain text URL-like links connect to websites, emails (VM), Usenet
  4694. messages (Gnus), BBDB entries, and any files related to the project.
  4695. For printing and sharing of notes, an Org-mode file (or a part of it)
  4696. can be exported as a structured ASCII or HTML file.
  4697. The following commands are available:
  4698. \\{org-mode-map}"
  4699. ;; Get rid of Outline menus, they are not needed
  4700. ;; Need to do this here because define-derived-mode sets up
  4701. ;; the keymap so late. Still, it is a waste to call this each time
  4702. ;; we switch another buffer into org-mode.
  4703. (if (featurep 'xemacs)
  4704. (when (boundp 'outline-mode-menu-heading)
  4705. ;; Assume this is Greg's port, it uses easymenu
  4706. (easy-menu-remove outline-mode-menu-heading)
  4707. (easy-menu-remove outline-mode-menu-show)
  4708. (easy-menu-remove outline-mode-menu-hide))
  4709. (define-key org-mode-map [menu-bar headings] 'undefined)
  4710. (define-key org-mode-map [menu-bar hide] 'undefined)
  4711. (define-key org-mode-map [menu-bar show] 'undefined))
  4712. (org-load-modules-maybe)
  4713. (easy-menu-add org-org-menu)
  4714. (easy-menu-add org-tbl-menu)
  4715. (org-install-agenda-files-menu)
  4716. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4717. (add-to-invisibility-spec '(org-cwidth))
  4718. (add-to-invisibility-spec '(org-hide-block . t))
  4719. (when (featurep 'xemacs)
  4720. (org-set-local 'line-move-ignore-invisible t))
  4721. (org-set-local 'outline-regexp org-outline-regexp)
  4722. (org-set-local 'outline-level 'org-outline-level)
  4723. (setq bidi-paragraph-direction 'left-to-right)
  4724. (when (and org-ellipsis
  4725. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4726. (fboundp 'make-glyph-code))
  4727. (unless org-display-table
  4728. (setq org-display-table (make-display-table)))
  4729. (set-display-table-slot
  4730. org-display-table 4
  4731. (vconcat (mapcar
  4732. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4733. org-ellipsis)))
  4734. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4735. (setq buffer-display-table org-display-table))
  4736. (org-set-regexps-and-options-for-tags)
  4737. (org-set-regexps-and-options)
  4738. (org-set-font-lock-defaults)
  4739. (when (and org-tag-faces (not org-tags-special-faces-re))
  4740. ;; tag faces set outside customize.... force initialization.
  4741. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4742. ;; Calc embedded
  4743. (org-set-local 'calc-embedded-open-mode "# ")
  4744. ;; Modify a few syntax entries
  4745. (modify-syntax-entry ?@ "w")
  4746. (modify-syntax-entry ?\" "\"")
  4747. (modify-syntax-entry ?\\ "_")
  4748. (modify-syntax-entry ?~ "_")
  4749. (if org-startup-truncated (setq truncate-lines t))
  4750. (when org-startup-indented (require 'org-indent) (org-indent-mode 1))
  4751. (org-set-local 'font-lock-unfontify-region-function
  4752. 'org-unfontify-region)
  4753. ;; Activate before-change-function
  4754. (org-set-local 'org-table-may-need-update t)
  4755. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4756. 'local)
  4757. ;; Check for running clock before killing a buffer
  4758. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4759. ;; Initialize macros templates.
  4760. (org-macro-initialize-templates)
  4761. ;; Initialize radio targets.
  4762. (org-update-radio-target-regexp)
  4763. ;; Indentation.
  4764. (org-set-local 'indent-line-function 'org-indent-line)
  4765. (org-set-local 'indent-region-function 'org-indent-region)
  4766. ;; Filling and auto-filling.
  4767. (org-setup-filling)
  4768. ;; Comments.
  4769. (org-setup-comments-handling)
  4770. ;; Beginning/end of defun
  4771. (org-set-local 'beginning-of-defun-function 'org-backward-element)
  4772. (org-set-local 'end-of-defun-function
  4773. (lambda ()
  4774. (if (not (org-at-heading-p))
  4775. (org-forward-element)
  4776. (org-forward-element)
  4777. (forward-char -1))))
  4778. ;; Next error for sparse trees
  4779. (org-set-local 'next-error-function 'org-occur-next-match)
  4780. ;; Make sure dependence stuff works reliably, even for users who set it
  4781. ;; too late :-(
  4782. (if org-enforce-todo-dependencies
  4783. (add-hook 'org-blocker-hook
  4784. 'org-block-todo-from-children-or-siblings-or-parent)
  4785. (remove-hook 'org-blocker-hook
  4786. 'org-block-todo-from-children-or-siblings-or-parent))
  4787. (if org-enforce-todo-checkbox-dependencies
  4788. (add-hook 'org-blocker-hook
  4789. 'org-block-todo-from-checkboxes)
  4790. (remove-hook 'org-blocker-hook
  4791. 'org-block-todo-from-checkboxes))
  4792. ;; Align options lines
  4793. (org-set-local
  4794. 'align-mode-rules-list
  4795. '((org-in-buffer-settings
  4796. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4797. (modes . '(org-mode)))))
  4798. ;; Imenu
  4799. (org-set-local 'imenu-create-index-function
  4800. 'org-imenu-get-tree)
  4801. ;; Make isearch reveal context
  4802. (if (or (featurep 'xemacs)
  4803. (not (boundp 'outline-isearch-open-invisible-function)))
  4804. ;; Emacs 21 and XEmacs make use of the hook
  4805. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4806. ;; Emacs 22 deals with this through a special variable
  4807. (org-set-local 'outline-isearch-open-invisible-function
  4808. (lambda (&rest ignore) (org-show-context 'isearch))))
  4809. ;; Setup the pcomplete hooks
  4810. (set (make-local-variable 'pcomplete-command-completion-function)
  4811. 'org-pcomplete-initial)
  4812. (set (make-local-variable 'pcomplete-command-name-function)
  4813. 'org-command-at-point)
  4814. (set (make-local-variable 'pcomplete-default-completion-function)
  4815. 'ignore)
  4816. (set (make-local-variable 'pcomplete-parse-arguments-function)
  4817. 'org-parse-arguments)
  4818. (set (make-local-variable 'pcomplete-termination-string) "")
  4819. (when (>= emacs-major-version 23)
  4820. (set (make-local-variable 'buffer-face-mode-face) 'org-default))
  4821. ;; If empty file that did not turn on org-mode automatically, make it to.
  4822. (if (and org-insert-mode-line-in-empty-file
  4823. (org-called-interactively-p 'any)
  4824. (= (point-min) (point-max)))
  4825. (insert "# -*- mode: org -*-\n\n"))
  4826. (unless org-inhibit-startup
  4827. (org-unmodified
  4828. (and org-startup-with-beamer-mode (org-beamer-mode))
  4829. (when org-startup-align-all-tables
  4830. (org-table-map-tables 'org-table-align 'quietly))
  4831. (when org-startup-with-inline-images
  4832. (org-display-inline-images))
  4833. (when org-startup-with-latex-preview
  4834. (org-preview-latex-fragment))
  4835. (unless org-inhibit-startup-visibility-stuff
  4836. (org-set-startup-visibility))))
  4837. ;; Try to set org-hide correctly
  4838. (let ((foreground (org-find-invisible-foreground)))
  4839. (if foreground
  4840. (set-face-foreground 'org-hide foreground))))
  4841. ;; Update `customize-package-emacs-version-alist'
  4842. (add-to-list 'customize-package-emacs-version-alist
  4843. '(Org ("6.21b" . "23.1") ("6.33x" . "23.2")
  4844. ("7.8.11" . "24.1") ("7.9.4" . "24.3")
  4845. ("8.2.6" . "24.4")))
  4846. (defvar org-mode-transpose-word-syntax-table
  4847. (let ((st (make-syntax-table)))
  4848. (mapc (lambda(c) (modify-syntax-entry
  4849. (string-to-char (car c)) "w p" st))
  4850. org-emphasis-alist)
  4851. st))
  4852. (when (fboundp 'abbrev-table-put)
  4853. (abbrev-table-put org-mode-abbrev-table
  4854. :parents (list text-mode-abbrev-table)))
  4855. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4856. (defun org-find-invisible-foreground ()
  4857. (let ((candidates (remove
  4858. "unspecified-bg"
  4859. (nconc
  4860. (list (face-background 'default)
  4861. (face-background 'org-default))
  4862. (mapcar
  4863. (lambda (alist)
  4864. (when (boundp alist)
  4865. (cdr (assoc 'background-color (symbol-value alist)))))
  4866. '(default-frame-alist initial-frame-alist window-system-default-frame-alist))
  4867. (list (face-foreground 'org-hide))))))
  4868. (car (remove nil candidates))))
  4869. (defun org-current-time (&optional rounding-minutes past)
  4870. "Current time, possibly rounded to ROUNDING-MINUTES.
  4871. When ROUNDING-MINUTES is not an integer, fall back on the car of
  4872. `org-time-stamp-rounding-minutes'. When PAST is non-nil, ensure
  4873. the rounding returns a past time."
  4874. (let ((r (or (and (integerp rounding-minutes) rounding-minutes)
  4875. (car org-time-stamp-rounding-minutes)))
  4876. (time (decode-time)) res)
  4877. (if (< r 1)
  4878. (current-time)
  4879. (setq res
  4880. (apply 'encode-time
  4881. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4882. (nthcdr 2 time))))
  4883. (if (and past (< (org-float-time (time-subtract (current-time) res)) 0))
  4884. (seconds-to-time (- (org-float-time res) (* r 60)))
  4885. res))))
  4886. (defun org-today ()
  4887. "Return today date, considering `org-extend-today-until'."
  4888. (time-to-days
  4889. (time-subtract (current-time)
  4890. (list 0 (* 3600 org-extend-today-until) 0))))
  4891. ;;;; Font-Lock stuff, including the activators
  4892. (defvar org-mouse-map (make-sparse-keymap))
  4893. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4894. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4895. (when org-mouse-1-follows-link
  4896. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4897. (when org-tab-follows-link
  4898. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4899. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4900. (require 'font-lock)
  4901. (defconst org-non-link-chars "]\t\n\r<>")
  4902. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  4903. "shell" "elisp" "doi" "message"))
  4904. (defvar org-link-types-re nil
  4905. "Matches a link that has a url-like prefix like \"http:\"")
  4906. (defvar org-link-re-with-space nil
  4907. "Matches a link with spaces, optional angular brackets around it.")
  4908. (defvar org-link-re-with-space2 nil
  4909. "Matches a link with spaces, optional angular brackets around it.")
  4910. (defvar org-link-re-with-space3 nil
  4911. "Matches a link with spaces, only for internal part in bracket links.")
  4912. (defvar org-angle-link-re nil
  4913. "Matches link with angular brackets, spaces are allowed.")
  4914. (defvar org-plain-link-re nil
  4915. "Matches plain link, without spaces.")
  4916. (defvar org-bracket-link-regexp nil
  4917. "Matches a link in double brackets.")
  4918. (defvar org-bracket-link-analytic-regexp nil
  4919. "Regular expression used to analyze links.
  4920. Here is what the match groups contain after a match:
  4921. 1: http:
  4922. 2: http
  4923. 3: path
  4924. 4: [desc]
  4925. 5: desc")
  4926. (defvar org-bracket-link-analytic-regexp++ nil
  4927. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4928. (defvar org-any-link-re nil
  4929. "Regular expression matching any link.")
  4930. (defconst org-match-sexp-depth 3
  4931. "Number of stacked braces for sub/superscript matching.")
  4932. (defun org-create-multibrace-regexp (left right n)
  4933. "Create a regular expression which will match a balanced sexp.
  4934. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4935. as single character strings.
  4936. The regexp returned will match the entire expression including the
  4937. delimiters. It will also define a single group which contains the
  4938. match except for the outermost delimiters. The maximum depth of
  4939. stacked delimiters is N. Escaping delimiters is not possible."
  4940. (let* ((nothing (concat "[^" left right "]*?"))
  4941. (or "\\|")
  4942. (re nothing)
  4943. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4944. (while (> n 1)
  4945. (setq n (1- n)
  4946. re (concat re or next)
  4947. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4948. (concat left "\\(" re "\\)" right)))
  4949. (defvar org-match-substring-regexp
  4950. (concat
  4951. "\\(\\S-\\)\\([_^]\\)\\("
  4952. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4953. "\\|"
  4954. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4955. "\\|"
  4956. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  4957. "The regular expression matching a sub- or superscript.")
  4958. (defvar org-match-substring-with-braces-regexp
  4959. (concat
  4960. "\\(\\S-\\)\\([_^]\\)\\("
  4961. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4962. "\\)")
  4963. "The regular expression matching a sub- or superscript, forcing braces.")
  4964. (defun org-make-link-regexps ()
  4965. "Update the link regular expressions.
  4966. This should be called after the variable `org-link-types' has changed."
  4967. (let ((types-re (regexp-opt org-link-types t)))
  4968. (setq org-link-types-re
  4969. (concat "\\`" types-re ":")
  4970. org-link-re-with-space
  4971. (concat "<?" types-re ":"
  4972. "\\([^" org-non-link-chars " ]"
  4973. "[^" org-non-link-chars "]*"
  4974. "[^" org-non-link-chars " ]\\)>?")
  4975. org-link-re-with-space2
  4976. (concat "<?" types-re ":"
  4977. "\\([^" org-non-link-chars " ]"
  4978. "[^\t\n\r]*"
  4979. "[^" org-non-link-chars " ]\\)>?")
  4980. org-link-re-with-space3
  4981. (concat "<?" types-re ":"
  4982. "\\([^" org-non-link-chars " ]"
  4983. "[^\t\n\r]*\\)")
  4984. org-angle-link-re
  4985. (concat "<" types-re ":"
  4986. "\\([^" org-non-link-chars " ]"
  4987. "[^" org-non-link-chars "]*"
  4988. "\\)>")
  4989. org-plain-link-re
  4990. (concat
  4991. "\\<" types-re ":"
  4992. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  4993. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4994. org-bracket-link-regexp
  4995. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4996. org-bracket-link-analytic-regexp
  4997. (concat
  4998. "\\[\\["
  4999. "\\(" types-re ":\\)?"
  5000. "\\([^]]+\\)"
  5001. "\\]"
  5002. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  5003. "\\]")
  5004. org-bracket-link-analytic-regexp++
  5005. (concat
  5006. "\\[\\["
  5007. "\\(" (regexp-opt (cons "coderef" org-link-types) t) ":\\)?"
  5008. "\\([^]]+\\)"
  5009. "\\]"
  5010. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  5011. "\\]")
  5012. org-any-link-re
  5013. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  5014. org-angle-link-re "\\)\\|\\("
  5015. org-plain-link-re "\\)"))))
  5016. (org-make-link-regexps)
  5017. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
  5018. "Regular expression for fast time stamp matching.")
  5019. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
  5020. "Regular expression for fast time stamp matching.")
  5021. (defconst org-ts-regexp0
  5022. "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  5023. "Regular expression matching time strings for analysis.
  5024. This one does not require the space after the date, so it can be used
  5025. on a string that terminates immediately after the date.")
  5026. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  5027. "Regular expression matching time strings for analysis.")
  5028. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  5029. "Regular expression matching time stamps, with groups.")
  5030. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  5031. "Regular expression matching time stamps (also [..]), with groups.")
  5032. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  5033. "Regular expression matching a time stamp range.")
  5034. (defconst org-tr-regexp-both
  5035. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  5036. "Regular expression matching a time stamp range.")
  5037. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  5038. org-ts-regexp "\\)?")
  5039. "Regular expression matching a time stamp or time stamp range.")
  5040. (defconst org-tsr-regexp-both
  5041. (concat org-ts-regexp-both "\\(--?-?"
  5042. org-ts-regexp-both "\\)?")
  5043. "Regular expression matching a time stamp or time stamp range.
  5044. The time stamps may be either active or inactive.")
  5045. (defvar org-emph-face nil)
  5046. (defun org-do-emphasis-faces (limit)
  5047. "Run through the buffer and add overlays to emphasized strings."
  5048. (let (rtn a)
  5049. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  5050. (let* ((border (char-after (match-beginning 3)))
  5051. (bre (regexp-quote (char-to-string border))))
  5052. (if (and (not (= border (char-after (match-beginning 4))))
  5053. (not (save-match-data
  5054. (string-match (concat bre ".*" bre)
  5055. (replace-regexp-in-string
  5056. "\n" " "
  5057. (substring (match-string 2) 1 -1))))))
  5058. (progn
  5059. (setq rtn t)
  5060. (setq a (assoc (match-string 3) org-emphasis-alist))
  5061. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  5062. 'face
  5063. (nth 1 a))
  5064. (and (nth 2 a)
  5065. (org-remove-flyspell-overlays-in
  5066. (match-beginning 0) (match-end 0)))
  5067. (add-text-properties (match-beginning 2) (match-end 2)
  5068. '(font-lock-multiline t org-emphasis t))
  5069. (when org-hide-emphasis-markers
  5070. (add-text-properties (match-end 4) (match-beginning 5)
  5071. '(invisible org-link))
  5072. (add-text-properties (match-beginning 3) (match-end 3)
  5073. '(invisible org-link))))))
  5074. (goto-char (1+ (match-beginning 0))))
  5075. rtn))
  5076. (defun org-emphasize (&optional char)
  5077. "Insert or change an emphasis, i.e. a font like bold or italic.
  5078. If there is an active region, change that region to a new emphasis.
  5079. If there is no region, just insert the marker characters and position
  5080. the cursor between them.
  5081. CHAR should be the marker character. If it is a space, it means to
  5082. remove the emphasis of the selected region.
  5083. If CHAR is not given (for example in an interactive call) it will be
  5084. prompted for."
  5085. (interactive)
  5086. (let ((erc org-emphasis-regexp-components)
  5087. (prompt "")
  5088. (string "") beg end move c s)
  5089. (if (org-region-active-p)
  5090. (setq beg (region-beginning) end (region-end)
  5091. string (buffer-substring beg end))
  5092. (setq move t))
  5093. (unless char
  5094. (message "Emphasis marker or tag: [%s]"
  5095. (mapconcat (lambda(e) (car e)) org-emphasis-alist ""))
  5096. (setq char (read-char-exclusive)))
  5097. (if (equal char ?\ )
  5098. (setq s "" move nil)
  5099. (unless (assoc (char-to-string char) org-emphasis-alist)
  5100. (user-error "No such emphasis marker: \"%c\"" char))
  5101. (setq s (char-to-string char)))
  5102. (while (and (> (length string) 1)
  5103. (equal (substring string 0 1) (substring string -1))
  5104. (assoc (substring string 0 1) org-emphasis-alist))
  5105. (setq string (substring string 1 -1)))
  5106. (setq string (concat s string s))
  5107. (if beg (delete-region beg end))
  5108. (unless (or (bolp)
  5109. (string-match (concat "[" (nth 0 erc) "\n]")
  5110. (char-to-string (char-before (point)))))
  5111. (insert " "))
  5112. (unless (or (eobp)
  5113. (string-match (concat "[" (nth 1 erc) "\n]")
  5114. (char-to-string (char-after (point)))))
  5115. (insert " ") (backward-char 1))
  5116. (insert string)
  5117. (and move (backward-char 1))))
  5118. (defconst org-nonsticky-props
  5119. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text htmlize-link))
  5120. (defsubst org-rear-nonsticky-at (pos)
  5121. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  5122. (defun org-activate-plain-links (limit)
  5123. "Run through the buffer and add overlays to links."
  5124. (let (f hl)
  5125. (when (and (re-search-forward (concat org-plain-link-re) limit t)
  5126. (not (org-in-src-block-p)))
  5127. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5128. (setq f (get-text-property (match-beginning 0) 'face))
  5129. (setq hl (org-match-string-no-properties 0))
  5130. (if (or (eq f 'org-tag)
  5131. (and (listp f) (memq 'org-tag f)))
  5132. nil
  5133. (add-text-properties (match-beginning 0) (match-end 0)
  5134. (list 'mouse-face 'highlight
  5135. 'face 'org-link
  5136. 'htmlize-link `(:uri ,hl)
  5137. 'keymap org-mouse-map))
  5138. (org-rear-nonsticky-at (match-end 0)))
  5139. t)))
  5140. (defun org-activate-code (limit)
  5141. (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  5142. (progn
  5143. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5144. (remove-text-properties (match-beginning 0) (match-end 0)
  5145. '(display t invisible t intangible t))
  5146. t)))
  5147. (defcustom org-src-fontify-natively nil
  5148. "When non-nil, fontify code in code blocks."
  5149. :type 'boolean
  5150. :version "24.1"
  5151. :group 'org-appearance
  5152. :group 'org-babel)
  5153. (defcustom org-allow-promoting-top-level-subtree nil
  5154. "When non-nil, allow promoting a top level subtree.
  5155. The leading star of the top level headline will be replaced
  5156. by a #."
  5157. :type 'boolean
  5158. :version "24.1"
  5159. :group 'org-appearance)
  5160. (defun org-fontify-meta-lines-and-blocks (limit)
  5161. (condition-case nil
  5162. (org-fontify-meta-lines-and-blocks-1 limit)
  5163. (error (message "org-mode fontification error"))))
  5164. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  5165. "Fontify #+ lines and blocks."
  5166. (let ((case-fold-search t))
  5167. (if (re-search-forward
  5168. "^\\([ \t]*#\\(\\(\\+[a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  5169. limit t)
  5170. (let ((beg (match-beginning 0))
  5171. (block-start (match-end 0))
  5172. (block-end nil)
  5173. (lang (match-string 7))
  5174. (beg1 (line-beginning-position 2))
  5175. (dc1 (downcase (match-string 2)))
  5176. (dc3 (downcase (match-string 3)))
  5177. end end1 quoting block-type ovl)
  5178. (cond
  5179. ((member dc1 '("+html:" "+ascii:" "+latex:"))
  5180. ;; a single line of backend-specific content
  5181. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5182. (remove-text-properties (match-beginning 0) (match-end 0)
  5183. '(display t invisible t intangible t))
  5184. (add-text-properties (match-beginning 1) (match-end 3)
  5185. '(font-lock-fontified t face org-meta-line))
  5186. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  5187. '(font-lock-fontified t face org-block))
  5188. ; for backend-specific code
  5189. t)
  5190. ((and (match-end 4) (equal dc3 "+begin"))
  5191. ;; Truly a block
  5192. (setq block-type (downcase (match-string 5))
  5193. quoting (member block-type org-protecting-blocks))
  5194. (when (re-search-forward
  5195. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  5196. nil t) ;; on purpose, we look further than LIMIT
  5197. (setq end (min (point-max) (match-end 0))
  5198. end1 (min (point-max) (1- (match-beginning 0))))
  5199. (setq block-end (match-beginning 0))
  5200. (when quoting
  5201. (remove-text-properties beg end
  5202. '(display t invisible t intangible t)))
  5203. (add-text-properties
  5204. beg end
  5205. '(font-lock-fontified t font-lock-multiline t))
  5206. (add-text-properties beg beg1 '(face org-meta-line))
  5207. (add-text-properties end1 (min (point-max) (1+ end))
  5208. '(face org-meta-line)) ; for end_src
  5209. (cond
  5210. ((and lang (not (string= lang "")) org-src-fontify-natively)
  5211. (org-src-font-lock-fontify-block lang block-start block-end)
  5212. ;; remove old background overlays
  5213. (mapc (lambda (ov)
  5214. (if (eq (overlay-get ov 'face) 'org-block-background)
  5215. (delete-overlay ov)))
  5216. (overlays-at (/ (+ beg1 block-end) 2)))
  5217. ;; add a background overlay
  5218. (setq ovl (make-overlay beg1 block-end))
  5219. (overlay-put ovl 'face 'org-block-background)
  5220. (overlay-put ovl 'evaporate t)) ;; make it go away when empty
  5221. (quoting
  5222. (add-text-properties beg1 (min (point-max) (1+ end1))
  5223. '(face org-block))) ; end of source block
  5224. ((not org-fontify-quote-and-verse-blocks))
  5225. ((string= block-type "quote")
  5226. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-quote)))
  5227. ((string= block-type "verse")
  5228. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-verse))))
  5229. (add-text-properties beg beg1 '(face org-block-begin-line))
  5230. (add-text-properties (min (point-max) (1+ end)) (min (point-max) (1+ end1))
  5231. '(face org-block-end-line))
  5232. t))
  5233. ((member dc1 '("+title:" "+author:" "+email:" "+date:"))
  5234. (add-text-properties
  5235. beg (match-end 3)
  5236. (if (member (intern (substring dc1 1 -1)) org-hidden-keywords)
  5237. '(font-lock-fontified t invisible t)
  5238. '(font-lock-fontified t face org-document-info-keyword)))
  5239. (add-text-properties
  5240. (match-beginning 6) (min (point-max) (1+ (match-end 6)))
  5241. (if (string-equal dc1 "+title:")
  5242. '(font-lock-fontified t face org-document-title)
  5243. '(font-lock-fontified t face org-document-info))))
  5244. ((or (equal dc1 "+results")
  5245. (member dc1 '("+begin:" "+end:" "+caption:" "+label:"
  5246. "+orgtbl:" "+tblfm:" "+tblname:" "+results:"
  5247. "+call:" "+header:" "+headers:" "+name:"))
  5248. (and (match-end 4) (equal dc3 "+attr")))
  5249. (add-text-properties
  5250. beg (match-end 0)
  5251. '(font-lock-fontified t face org-meta-line))
  5252. t)
  5253. ((member dc3 '(" " ""))
  5254. (add-text-properties
  5255. beg (match-end 0)
  5256. '(font-lock-fontified t face font-lock-comment-face)))
  5257. ((not (member (char-after beg) '(?\ ?\t)))
  5258. ;; just any other in-buffer setting, but not indented
  5259. (add-text-properties
  5260. beg (match-end 0)
  5261. '(font-lock-fontified t face org-meta-line))
  5262. t)
  5263. (t nil))))))
  5264. (defun org-activate-angle-links (limit)
  5265. "Run through the buffer and add overlays to links."
  5266. (if (and (re-search-forward org-angle-link-re limit t)
  5267. (not (org-in-src-block-p)))
  5268. (progn
  5269. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5270. (add-text-properties (match-beginning 0) (match-end 0)
  5271. (list 'mouse-face 'highlight
  5272. 'keymap org-mouse-map))
  5273. (org-rear-nonsticky-at (match-end 0))
  5274. t)))
  5275. (defun org-activate-footnote-links (limit)
  5276. "Run through the buffer and add overlays to footnotes."
  5277. (let ((fn (org-footnote-next-reference-or-definition limit)))
  5278. (when fn
  5279. (let ((beg (nth 1 fn)) (end (nth 2 fn)))
  5280. (org-remove-flyspell-overlays-in beg end)
  5281. (add-text-properties beg end
  5282. (list 'mouse-face 'highlight
  5283. 'keymap org-mouse-map
  5284. 'help-echo
  5285. (if (= (point-at-bol) beg)
  5286. "Footnote definition"
  5287. "Footnote reference")
  5288. 'font-lock-fontified t
  5289. 'font-lock-multiline t
  5290. 'face 'org-footnote))))))
  5291. (defun org-activate-bracket-links (limit)
  5292. "Run through the buffer and add overlays to bracketed links."
  5293. (if (and (re-search-forward org-bracket-link-regexp limit t)
  5294. (not (org-in-src-block-p)))
  5295. (let* ((hl (org-match-string-no-properties 1))
  5296. (help (concat "LINK: " (save-match-data (org-link-unescape hl))))
  5297. (ip (org-maybe-intangible
  5298. (list 'invisible 'org-link
  5299. 'keymap org-mouse-map 'mouse-face 'highlight
  5300. 'font-lock-multiline t 'help-echo help
  5301. 'htmlize-link `(:uri ,hl))))
  5302. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  5303. 'font-lock-multiline t 'help-echo help
  5304. 'htmlize-link `(:uri ,hl))))
  5305. ;; We need to remove the invisible property here. Table narrowing
  5306. ;; may have made some of this invisible.
  5307. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5308. (remove-text-properties (match-beginning 0) (match-end 0)
  5309. '(invisible nil))
  5310. (if (match-end 3)
  5311. (progn
  5312. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  5313. (org-rear-nonsticky-at (match-beginning 3))
  5314. (add-text-properties (match-beginning 3) (match-end 3) vp)
  5315. (org-rear-nonsticky-at (match-end 3))
  5316. (add-text-properties (match-end 3) (match-end 0) ip)
  5317. (org-rear-nonsticky-at (match-end 0)))
  5318. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  5319. (org-rear-nonsticky-at (match-beginning 1))
  5320. (add-text-properties (match-beginning 1) (match-end 1) vp)
  5321. (org-rear-nonsticky-at (match-end 1))
  5322. (add-text-properties (match-end 1) (match-end 0) ip)
  5323. (org-rear-nonsticky-at (match-end 0)))
  5324. t)))
  5325. (defun org-activate-dates (limit)
  5326. "Run through the buffer and add overlays to dates."
  5327. (if (and (re-search-forward org-tsr-regexp-both limit t)
  5328. (not (equal (char-before (match-beginning 0)) 91)))
  5329. (progn
  5330. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5331. (add-text-properties (match-beginning 0) (match-end 0)
  5332. (list 'mouse-face 'highlight
  5333. 'keymap org-mouse-map))
  5334. (org-rear-nonsticky-at (match-end 0))
  5335. (when org-display-custom-times
  5336. (if (match-end 3)
  5337. (org-display-custom-time (match-beginning 3) (match-end 3)))
  5338. (org-display-custom-time (match-beginning 1) (match-end 1)))
  5339. t)))
  5340. (defvar org-target-link-regexp nil
  5341. "Regular expression matching radio targets in plain text.")
  5342. (make-variable-buffer-local 'org-target-link-regexp)
  5343. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  5344. "Regular expression matching a link target.")
  5345. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  5346. "Regular expression matching a radio target.")
  5347. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  5348. "Regular expression matching any target.")
  5349. (defun org-activate-target-links (limit)
  5350. "Run through the buffer and add overlays to target matches."
  5351. (when org-target-link-regexp
  5352. (let ((case-fold-search t))
  5353. (if (re-search-forward org-target-link-regexp limit t)
  5354. (progn
  5355. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5356. (add-text-properties (match-beginning 0) (match-end 0)
  5357. (list 'mouse-face 'highlight
  5358. 'keymap org-mouse-map
  5359. 'help-echo "Radio target link"
  5360. 'org-linked-text t))
  5361. (org-rear-nonsticky-at (match-end 0))
  5362. t)))))
  5363. (defun org-update-radio-target-regexp ()
  5364. "Find all radio targets in this file and update the regular expression."
  5365. (interactive)
  5366. (when (memq 'radio org-activate-links)
  5367. (setq org-target-link-regexp
  5368. (org-make-target-link-regexp (org-all-targets 'radio)))
  5369. (org-restart-font-lock)))
  5370. (defun org-hide-wide-columns (limit)
  5371. (let (s e)
  5372. (setq s (text-property-any (point) (or limit (point-max))
  5373. 'org-cwidth t))
  5374. (when s
  5375. (setq e (next-single-property-change s 'org-cwidth))
  5376. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  5377. (goto-char e)
  5378. t)))
  5379. (defvar org-latex-and-related-regexp nil
  5380. "Regular expression for highlighting LaTeX, entities and sub/superscript.")
  5381. (defvar org-match-substring-regexp)
  5382. (defvar org-match-substring-with-braces-regexp)
  5383. (defun org-compute-latex-and-related-regexp ()
  5384. "Compute regular expression for LaTeX, entities and sub/superscript.
  5385. Result depends on variable `org-highlight-latex-and-related'."
  5386. (org-set-local
  5387. 'org-latex-and-related-regexp
  5388. (let* ((re-sub
  5389. (cond ((not (memq 'script org-highlight-latex-and-related)) nil)
  5390. ((eq org-use-sub-superscripts '{})
  5391. (list org-match-substring-with-braces-regexp))
  5392. (org-use-sub-superscripts (list org-match-substring-regexp))))
  5393. (re-latex
  5394. (when (memq 'latex org-highlight-latex-and-related)
  5395. (let ((matchers (plist-get org-format-latex-options :matchers)))
  5396. (delq nil
  5397. (mapcar (lambda (x)
  5398. (and (member (car x) matchers) (nth 1 x)))
  5399. org-latex-regexps)))))
  5400. (re-entities
  5401. (when (memq 'entities org-highlight-latex-and-related)
  5402. (list "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))))
  5403. (mapconcat 'identity (append re-latex re-entities re-sub) "\\|"))))
  5404. (defun org-do-latex-and-related (limit)
  5405. "Highlight LaTeX snippets and environments, entities and sub/superscript.
  5406. LIMIT bounds the search for syntax to highlight. Stop at first
  5407. highlighted object, if any. Return t if some highlighting was
  5408. done, nil otherwise."
  5409. (when (org-string-nw-p org-latex-and-related-regexp)
  5410. (catch 'found
  5411. (while (re-search-forward org-latex-and-related-regexp limit t)
  5412. (unless (memq (car-safe (get-text-property (1+ (match-beginning 0))
  5413. 'face))
  5414. '(org-code org-verbatim underline))
  5415. (let ((offset (if (memq (char-after (1+ (match-beginning 0)))
  5416. '(?_ ?^))
  5417. 1
  5418. 0)))
  5419. (font-lock-prepend-text-property
  5420. (+ offset (match-beginning 0)) (match-end 0)
  5421. 'face 'org-latex-and-related)
  5422. (add-text-properties (+ offset (match-beginning 0)) (match-end 0)
  5423. '(font-lock-multiline t)))
  5424. (throw 'found t)))
  5425. nil)))
  5426. (defun org-restart-font-lock ()
  5427. "Restart `font-lock-mode', to force refontification."
  5428. (when (and (boundp 'font-lock-mode) font-lock-mode)
  5429. (font-lock-mode -1)
  5430. (font-lock-mode 1)))
  5431. (defun org-all-targets (&optional radio)
  5432. "Return a list of all targets in this file.
  5433. When optional argument RADIO is non-nil, only find radio
  5434. targets."
  5435. (let ((re (if radio org-radio-target-regexp org-target-regexp)) rtn)
  5436. (save-excursion
  5437. (goto-char (point-min))
  5438. (while (re-search-forward re nil t)
  5439. ;; Make sure point is really within the object.
  5440. (backward-char)
  5441. (let ((obj (org-element-context)))
  5442. (when (memq (org-element-type obj) '(radio-target target))
  5443. (add-to-list 'rtn (downcase (org-element-property :value obj))))))
  5444. rtn)))
  5445. (defun org-make-target-link-regexp (targets)
  5446. "Make regular expression matching all strings in TARGETS.
  5447. The regular expression finds the targets also if there is a line break
  5448. between words."
  5449. (and targets
  5450. (concat
  5451. "\\_<\\("
  5452. (mapconcat
  5453. (lambda (x)
  5454. (setq x (regexp-quote x))
  5455. (while (string-match " +" x)
  5456. (setq x (replace-match "\\s-+" t t x)))
  5457. x)
  5458. targets
  5459. "\\|")
  5460. "\\)\\_>")))
  5461. (defun org-activate-tags (limit)
  5462. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  5463. (progn
  5464. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5465. (add-text-properties (match-beginning 1) (match-end 1)
  5466. (list 'mouse-face 'highlight
  5467. 'keymap org-mouse-map))
  5468. (org-rear-nonsticky-at (match-end 1))
  5469. t)))
  5470. (defun org-outline-level ()
  5471. "Compute the outline level of the heading at point.
  5472. If this is called at a normal headline, the level is the number of stars.
  5473. Use `org-reduced-level' to remove the effect of `org-odd-levels'."
  5474. (save-excursion
  5475. (if (not (condition-case nil
  5476. (org-back-to-heading t)
  5477. (error nil)))
  5478. 0
  5479. (looking-at org-outline-regexp)
  5480. (1- (- (match-end 0) (match-beginning 0))))))
  5481. (defvar org-font-lock-keywords nil)
  5482. (defsubst org-re-property (property &optional literal allow-null)
  5483. "Return a regexp matching a PROPERTY line.
  5484. When optional argument LITERAL is non-nil, do not quote PROPERTY.
  5485. This is useful when PROPERTY is a regexp. When ALLOW-NULL is
  5486. non-nil, match properties even without a value.
  5487. Match group 3 is set to the value when it exists. If there is no
  5488. value and ALLOW-NULL is non-nil, it is set to the empty string."
  5489. (concat
  5490. "^\\(?4:[ \t]*\\)"
  5491. (format "\\(?1::\\(?2:%s\\):\\)"
  5492. (if literal property (regexp-quote property)))
  5493. (if allow-null
  5494. "\\(?:\\(?3:$\\)\\|[ \t]+\\(?3:.*?\\)\\)\\(?5:[ \t]*\\)$"
  5495. "[ \t]+\\(?3:[^ \r\t\n]+.*?\\)\\(?5:[ \t]*\\)$")))
  5496. (defconst org-property-re
  5497. (org-re-property ".*?" 'literal t)
  5498. "Regular expression matching a property line.
  5499. There are four matching groups:
  5500. 1: :PROPKEY: including the leading and trailing colon,
  5501. 2: PROPKEY without the leading and trailing colon,
  5502. 3: PROPVAL without leading or trailing spaces,
  5503. 4: the indentation of the current line,
  5504. 5: trailing whitespace.")
  5505. (defvar org-font-lock-hook nil
  5506. "Functions to be called for special font lock stuff.")
  5507. (defvar org-font-lock-set-keywords-hook nil
  5508. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5509. This is called after `org-font-lock-extra-keywords' is defined, but before
  5510. it is installed to be used by font lock. This can be useful if something
  5511. needs to be inserted at a specific position in the font-lock sequence.")
  5512. (defun org-font-lock-hook (limit)
  5513. "Run `org-font-lock-hook' within LIMIT."
  5514. (run-hook-with-args 'org-font-lock-hook limit))
  5515. (defun org-set-font-lock-defaults ()
  5516. "Set font lock defaults for the current buffer."
  5517. (let* ((em org-fontify-emphasized-text)
  5518. (lk org-activate-links)
  5519. (org-font-lock-extra-keywords
  5520. (list
  5521. ;; Call the hook
  5522. '(org-font-lock-hook)
  5523. ;; Headlines
  5524. `(,(if org-fontify-whole-heading-line
  5525. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5526. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5527. (1 (org-get-level-face 1))
  5528. (2 (org-get-level-face 2))
  5529. (3 (org-get-level-face 3)))
  5530. ;; Table lines
  5531. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5532. (1 'org-table t))
  5533. ;; Table internals
  5534. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5535. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5536. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5537. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5538. ;; Drawers
  5539. (list org-drawer-regexp '(0 'org-special-keyword t))
  5540. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  5541. ;; Properties
  5542. (list org-property-re
  5543. '(1 'org-special-keyword t)
  5544. '(3 'org-property-value t))
  5545. ;; Links
  5546. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5547. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5548. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  5549. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5550. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  5551. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5552. (if (memq 'footnote lk) '(org-activate-footnote-links))
  5553. ;; Targets.
  5554. (list org-any-target-regexp '(0 'org-target t))
  5555. ;; Diary sexps.
  5556. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5557. ;; Macro
  5558. '("{{{.+}}}" (0 'org-macro t))
  5559. '(org-hide-wide-columns (0 nil append))
  5560. ;; TODO keyword
  5561. (list (format org-heading-keyword-regexp-format
  5562. org-todo-regexp)
  5563. '(2 (org-get-todo-face 2) t))
  5564. ;; DONE
  5565. (if org-fontify-done-headline
  5566. (list (format org-heading-keyword-regexp-format
  5567. (concat
  5568. "\\(?:"
  5569. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5570. "\\)"))
  5571. '(2 'org-headline-done t))
  5572. nil)
  5573. ;; Priorities
  5574. '(org-font-lock-add-priority-faces)
  5575. ;; Tags
  5576. '(org-font-lock-add-tag-faces)
  5577. ;; Tags groups
  5578. (if (and org-group-tags org-tag-groups-alist)
  5579. (list (concat org-outline-regexp-bol ".+\\(:"
  5580. (regexp-opt (mapcar 'car org-tag-groups-alist))
  5581. ":\\).*$")
  5582. '(1 'org-tag-group prepend)))
  5583. ;; Special keywords
  5584. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5585. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5586. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5587. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5588. ;; Emphasis
  5589. (if em
  5590. (if (featurep 'xemacs)
  5591. '(org-do-emphasis-faces (0 nil append))
  5592. '(org-do-emphasis-faces)))
  5593. ;; Checkboxes
  5594. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5595. 1 'org-checkbox prepend)
  5596. (if (cdr (assq 'checkbox org-list-automatic-rules))
  5597. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5598. (0 (org-get-checkbox-statistics-face) t)))
  5599. ;; Description list items
  5600. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5601. 1 'org-list-dt prepend)
  5602. ;; ARCHIVEd headings
  5603. (list (concat
  5604. org-outline-regexp-bol
  5605. "\\(.*:" org-archive-tag ":.*\\)")
  5606. '(1 'org-archived prepend))
  5607. ;; Specials
  5608. '(org-do-latex-and-related)
  5609. '(org-fontify-entities)
  5610. '(org-raise-scripts)
  5611. ;; Code
  5612. '(org-activate-code (1 'org-code t))
  5613. ;; COMMENT
  5614. (list (format org-heading-keyword-regexp-format
  5615. (concat "\\("
  5616. org-comment-string "\\|" org-quote-string
  5617. "\\)"))
  5618. '(2 'org-special-keyword t))
  5619. ;; Blocks and meta lines
  5620. '(org-fontify-meta-lines-and-blocks))))
  5621. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5622. (run-hooks 'org-font-lock-set-keywords-hook)
  5623. ;; Now set the full font-lock-keywords
  5624. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5625. (org-set-local 'font-lock-defaults
  5626. '(org-font-lock-keywords t nil nil backward-paragraph))
  5627. (kill-local-variable 'font-lock-keywords) nil))
  5628. (defun org-toggle-pretty-entities ()
  5629. "Toggle the composition display of entities as UTF8 characters."
  5630. (interactive)
  5631. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5632. (org-restart-font-lock)
  5633. (if org-pretty-entities
  5634. (message "Entities are now displayed as UTF8 characters")
  5635. (save-restriction
  5636. (widen)
  5637. (org-decompose-region (point-min) (point-max))
  5638. (message "Entities are now displayed as plain text"))))
  5639. (defvar org-custom-properties-overlays nil
  5640. "List of overlays used for custom properties.")
  5641. (make-variable-buffer-local 'org-custom-properties-overlays)
  5642. (defun org-toggle-custom-properties-visibility ()
  5643. "Display or hide properties in `org-custom-properties'."
  5644. (interactive)
  5645. (if org-custom-properties-overlays
  5646. (progn (mapc 'delete-overlay org-custom-properties-overlays)
  5647. (setq org-custom-properties-overlays nil))
  5648. (unless (not org-custom-properties)
  5649. (save-excursion
  5650. (save-restriction
  5651. (widen)
  5652. (goto-char (point-min))
  5653. (while (re-search-forward org-property-re nil t)
  5654. (mapc (lambda(p)
  5655. (when (equal p (substring (match-string 1) 1 -1))
  5656. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5657. (overlay-put o 'invisible t)
  5658. (overlay-put o 'org-custom-property t)
  5659. (push o org-custom-properties-overlays))))
  5660. org-custom-properties)))))))
  5661. (defun org-fontify-entities (limit)
  5662. "Find an entity to fontify."
  5663. (let (ee)
  5664. (when org-pretty-entities
  5665. (catch 'match
  5666. (while (re-search-forward
  5667. "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
  5668. limit t)
  5669. (if (and (not (org-in-indented-comment-line))
  5670. (setq ee (org-entity-get (match-string 1)))
  5671. (= (length (nth 6 ee)) 1))
  5672. (let*
  5673. ((end (if (equal (match-string 2) "{}")
  5674. (match-end 2)
  5675. (match-end 1))))
  5676. (add-text-properties
  5677. (match-beginning 0) end
  5678. (list 'font-lock-fontified t))
  5679. (compose-region (match-beginning 0) end
  5680. (nth 6 ee) nil)
  5681. (backward-char 1)
  5682. (throw 'match t))))
  5683. nil))))
  5684. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5685. "Fontify string S like in Org-mode."
  5686. (with-temp-buffer
  5687. (insert s)
  5688. (let ((org-odd-levels-only odd-levels))
  5689. (org-mode)
  5690. (font-lock-ensure)
  5691. (buffer-string))))
  5692. (defvar org-m nil)
  5693. (defvar org-l nil)
  5694. (defvar org-f nil)
  5695. (defun org-get-level-face (n)
  5696. "Get the right face for match N in font-lock matching of headlines."
  5697. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5698. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5699. (if org-cycle-level-faces
  5700. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5701. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5702. (cond
  5703. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5704. ((eq n 2) org-f)
  5705. (t (if org-level-color-stars-only nil org-f))))
  5706. (defun org-get-todo-face (kwd)
  5707. "Get the right face for a TODO keyword KWD.
  5708. If KWD is a number, get the corresponding match group."
  5709. (if (numberp kwd) (setq kwd (match-string kwd)))
  5710. (or (org-face-from-face-or-color
  5711. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5712. (and (member kwd org-done-keywords) 'org-done)
  5713. 'org-todo))
  5714. (defun org-face-from-face-or-color (context inherit face-or-color)
  5715. "Create a face list that inherits INHERIT, but sets the foreground color.
  5716. When FACE-OR-COLOR is not a string, just return it."
  5717. (if (stringp face-or-color)
  5718. (list :inherit inherit
  5719. (cdr (assoc context org-faces-easy-properties))
  5720. face-or-color)
  5721. face-or-color))
  5722. (defun org-font-lock-add-tag-faces (limit)
  5723. "Add the special tag faces."
  5724. (when (and org-tag-faces org-tags-special-faces-re)
  5725. (while (re-search-forward org-tags-special-faces-re limit t)
  5726. (add-text-properties (match-beginning 1) (match-end 1)
  5727. (list 'face (org-get-tag-face 1)
  5728. 'font-lock-fontified t))
  5729. (backward-char 1))))
  5730. (defun org-font-lock-add-priority-faces (limit)
  5731. "Add the special priority faces."
  5732. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  5733. (when (save-match-data (org-at-heading-p))
  5734. (add-text-properties
  5735. (match-beginning 0) (match-end 0)
  5736. (list 'face (or (org-face-from-face-or-color
  5737. 'priority 'org-priority
  5738. (cdr (assoc (char-after (match-beginning 1))
  5739. org-priority-faces)))
  5740. 'org-priority)
  5741. 'font-lock-fontified t)))))
  5742. (defun org-get-tag-face (kwd)
  5743. "Get the right face for a TODO keyword KWD.
  5744. If KWD is a number, get the corresponding match group."
  5745. (if (numberp kwd) (setq kwd (match-string kwd)))
  5746. (or (org-face-from-face-or-color
  5747. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  5748. 'org-tag))
  5749. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5750. "Remove fontification and activation overlays from links."
  5751. (font-lock-default-unfontify-region beg end)
  5752. (let* ((buffer-undo-list t)
  5753. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5754. (inhibit-modification-hooks t)
  5755. deactivate-mark buffer-file-name buffer-file-truename)
  5756. (org-decompose-region beg end)
  5757. (remove-text-properties beg end
  5758. '(mouse-face t keymap t org-linked-text t
  5759. invisible t intangible t
  5760. org-no-flyspell t org-emphasis t))
  5761. (org-remove-font-lock-display-properties beg end)))
  5762. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5763. ((raise 0.3) (height 0.7))
  5764. ((raise -0.5))
  5765. ((raise 0.5)))
  5766. "Display properties for showing superscripts and subscripts.")
  5767. (defun org-remove-font-lock-display-properties (beg end)
  5768. "Remove specific display properties that have been added by font lock.
  5769. The will remove the raise properties that are used to show superscripts
  5770. and subscripts."
  5771. (let (next prop)
  5772. (while (< beg end)
  5773. (setq next (next-single-property-change beg 'display nil end)
  5774. prop (get-text-property beg 'display))
  5775. (if (member prop org-script-display)
  5776. (put-text-property beg next 'display nil))
  5777. (setq beg next))))
  5778. (defun org-raise-scripts (limit)
  5779. "Add raise properties to sub/superscripts."
  5780. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5781. (if (re-search-forward
  5782. (if (eq org-use-sub-superscripts t)
  5783. org-match-substring-regexp
  5784. org-match-substring-with-braces-regexp)
  5785. limit t)
  5786. (let* ((pos (point)) table-p comment-p
  5787. (mpos (match-beginning 3))
  5788. (emph-p (get-text-property mpos 'org-emphasis))
  5789. (link-p (get-text-property mpos 'mouse-face))
  5790. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5791. (goto-char (point-at-bol))
  5792. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5793. comment-p (org-looking-at-p "^[ \t]*#[ +]"))
  5794. (goto-char pos)
  5795. ;; Handle a_b^c
  5796. (if (member (char-after) '(?_ ?^)) (goto-char (1- pos)))
  5797. (if (or comment-p emph-p link-p keyw-p)
  5798. t
  5799. (put-text-property (match-beginning 3) (match-end 0)
  5800. 'display
  5801. (if (equal (char-after (match-beginning 2)) ?^)
  5802. (nth (if table-p 3 1) org-script-display)
  5803. (nth (if table-p 2 0) org-script-display)))
  5804. (add-text-properties (match-beginning 2) (match-end 2)
  5805. (list 'invisible t
  5806. 'org-dwidth t 'org-dwidth-n 1))
  5807. (if (and (eq (char-after (match-beginning 3)) ?{)
  5808. (eq (char-before (match-end 3)) ?}))
  5809. (progn
  5810. (add-text-properties
  5811. (match-beginning 3) (1+ (match-beginning 3))
  5812. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5813. (add-text-properties
  5814. (1- (match-end 3)) (match-end 3)
  5815. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5816. t)))))
  5817. ;;;; Visibility cycling, including org-goto and indirect buffer
  5818. ;;; Cycling
  5819. (defvar org-cycle-global-status nil)
  5820. (make-variable-buffer-local 'org-cycle-global-status)
  5821. (put 'org-cycle-global-status 'org-state t)
  5822. (defvar org-cycle-subtree-status nil)
  5823. (make-variable-buffer-local 'org-cycle-subtree-status)
  5824. (put 'org-cycle-subtree-status 'org-state t)
  5825. (defvar org-inlinetask-min-level)
  5826. (defun org-unlogged-message (&rest args)
  5827. "Display a message, but avoid logging it in the *Messages* buffer."
  5828. (let ((message-log-max nil))
  5829. (apply 'message args)))
  5830. ;;;###autoload
  5831. (defun org-cycle (&optional arg)
  5832. "TAB-action and visibility cycling for Org-mode.
  5833. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5834. is outline visibility cycling, but it also invokes other actions
  5835. in special contexts.
  5836. - When this function is called with a prefix argument, rotate the entire
  5837. buffer through 3 states (global cycling)
  5838. 1. OVERVIEW: Show only top-level headlines.
  5839. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5840. 3. SHOW ALL: Show everything.
  5841. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5842. determined by the variable `org-startup-folded', and by any VISIBILITY
  5843. properties in the buffer.
  5844. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5845. including any drawers.
  5846. - When inside a table, re-align the table and move to the next field.
  5847. - When point is at the beginning of a headline, rotate the subtree started
  5848. by this line through 3 different states (local cycling)
  5849. 1. FOLDED: Only the main headline is shown.
  5850. 2. CHILDREN: The main headline and the direct children are shown.
  5851. From this state, you can move to one of the children
  5852. and zoom in further.
  5853. 3. SUBTREE: Show the entire subtree, including body text.
  5854. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5855. - When point is at the beginning of an empty headline and the variable
  5856. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5857. of the headline by demoting and promoting it to likely levels. This
  5858. speeds up creation document structure by pressing TAB once or several
  5859. times right after creating a new headline.
  5860. - When there is a numeric prefix, go up to a heading with level ARG, do
  5861. a `show-subtree' and return to the previous cursor position. If ARG
  5862. is negative, go up that many levels.
  5863. - When point is not at the beginning of a headline, execute the global
  5864. binding for TAB, which is re-indenting the line. See the option
  5865. `org-cycle-emulate-tab' for details.
  5866. - Special case: if point is at the beginning of the buffer and there is
  5867. no headline in line 1, this function will act as if called with prefix arg
  5868. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5869. But only if also the variable `org-cycle-global-at-bob' is t."
  5870. (interactive "P")
  5871. (org-load-modules-maybe)
  5872. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5873. (and org-cycle-level-after-item/entry-creation
  5874. (or (org-cycle-level)
  5875. (org-cycle-item-indentation))))
  5876. (let* ((limit-level
  5877. (or org-cycle-max-level
  5878. (and (boundp 'org-inlinetask-min-level)
  5879. org-inlinetask-min-level
  5880. (1- org-inlinetask-min-level))))
  5881. (nstars (and limit-level
  5882. (if org-odd-levels-only
  5883. (and limit-level (1- (* limit-level 2)))
  5884. limit-level)))
  5885. (org-outline-regexp
  5886. (if (not (derived-mode-p 'org-mode))
  5887. outline-regexp
  5888. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5889. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5890. (not (looking-at org-outline-regexp))))
  5891. (org-cycle-hook
  5892. (if bob-special
  5893. (delq 'org-optimize-window-after-visibility-change
  5894. (copy-sequence org-cycle-hook))
  5895. org-cycle-hook))
  5896. (pos (point)))
  5897. (if (or bob-special (equal arg '(4)))
  5898. ;; special case: use global cycling
  5899. (setq arg t))
  5900. (cond
  5901. ((equal arg '(16))
  5902. (setq last-command 'dummy)
  5903. (org-set-startup-visibility)
  5904. (org-unlogged-message "Startup visibility, plus VISIBILITY properties"))
  5905. ((equal arg '(64))
  5906. (show-all)
  5907. (org-unlogged-message "Entire buffer visible, including drawers"))
  5908. ;; Try cdlatex TAB completion
  5909. ((org-try-cdlatex-tab))
  5910. ;; Table: enter it or move to the next field.
  5911. ((org-at-table-p 'any)
  5912. (if (org-at-table.el-p)
  5913. (message "Use C-c ' to edit table.el tables")
  5914. (if arg (org-table-edit-field t)
  5915. (org-table-justify-field-maybe)
  5916. (call-interactively 'org-table-next-field))))
  5917. ((run-hook-with-args-until-success
  5918. 'org-tab-after-check-for-table-hook))
  5919. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5920. ((eq arg t) (org-cycle-internal-global))
  5921. ;; Drawers: delegate to `org-flag-drawer'.
  5922. ((and org-drawers org-drawer-regexp
  5923. (save-excursion
  5924. (beginning-of-line 1)
  5925. (looking-at org-drawer-regexp)))
  5926. (org-flag-drawer ; toggle block visibility
  5927. (not (get-char-property (match-end 0) 'invisible))))
  5928. ;; Show-subtree, ARG levels up from here.
  5929. ((integerp arg)
  5930. (save-excursion
  5931. (org-back-to-heading)
  5932. (outline-up-heading (if (< arg 0) (- arg)
  5933. (- (funcall outline-level) arg)))
  5934. (org-show-subtree)))
  5935. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5936. ((and (featurep 'org-inlinetask)
  5937. (org-inlinetask-at-task-p)
  5938. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5939. (org-inlinetask-toggle-visibility))
  5940. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5941. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5942. (save-excursion (beginning-of-line 1)
  5943. (looking-at org-outline-regexp)))
  5944. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5945. (org-cycle-internal-local))
  5946. ;; From there: TAB emulation and template completion.
  5947. (buffer-read-only (org-back-to-heading))
  5948. ((run-hook-with-args-until-success
  5949. 'org-tab-after-check-for-cycling-hook))
  5950. ((org-try-structure-completion))
  5951. ((run-hook-with-args-until-success
  5952. 'org-tab-before-tab-emulation-hook))
  5953. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5954. (or (not (bolp))
  5955. (not (looking-at org-outline-regexp))))
  5956. (call-interactively (global-key-binding "\t")))
  5957. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5958. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5959. (or (and (eq org-cycle-emulate-tab 'white)
  5960. (= (match-end 0) (point-at-eol)))
  5961. (and (eq org-cycle-emulate-tab 'whitestart)
  5962. (>= (match-end 0) pos))))
  5963. t
  5964. (eq org-cycle-emulate-tab t))
  5965. (call-interactively (global-key-binding "\t")))
  5966. (t (save-excursion
  5967. (org-back-to-heading)
  5968. (org-cycle)))))))
  5969. (defun org-cycle-internal-global ()
  5970. "Do the global cycling action."
  5971. ;; Hack to avoid display of messages for .org attachments in Gnus
  5972. (let ((ga (string-match "\\*fontification" (buffer-name))))
  5973. (cond
  5974. ((and (eq last-command this-command)
  5975. (eq org-cycle-global-status 'overview))
  5976. ;; We just created the overview - now do table of contents
  5977. ;; This can be slow in very large buffers, so indicate action
  5978. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  5979. (unless ga (org-unlogged-message "CONTENTS..."))
  5980. (org-content)
  5981. (unless ga (org-unlogged-message "CONTENTS...done"))
  5982. (setq org-cycle-global-status 'contents)
  5983. (run-hook-with-args 'org-cycle-hook 'contents))
  5984. ((and (eq last-command this-command)
  5985. (eq org-cycle-global-status 'contents))
  5986. ;; We just showed the table of contents - now show everything
  5987. (run-hook-with-args 'org-pre-cycle-hook 'all)
  5988. (show-all)
  5989. (unless ga (org-unlogged-message "SHOW ALL"))
  5990. (setq org-cycle-global-status 'all)
  5991. (run-hook-with-args 'org-cycle-hook 'all))
  5992. (t
  5993. ;; Default action: go to overview
  5994. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  5995. (org-overview)
  5996. (unless ga (org-unlogged-message "OVERVIEW"))
  5997. (setq org-cycle-global-status 'overview)
  5998. (run-hook-with-args 'org-cycle-hook 'overview)))))
  5999. (defvar org-called-with-limited-levels nil
  6000. "Non-nil when `org-with-limited-levels' is currently active.")
  6001. (defun org-cycle-internal-local ()
  6002. "Do the local cycling action."
  6003. (let ((goal-column 0) eoh eol eos has-children children-skipped struct)
  6004. ;; First, determine end of headline (EOH), end of subtree or item
  6005. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  6006. (save-excursion
  6007. (if (org-at-item-p)
  6008. (progn
  6009. (beginning-of-line)
  6010. (setq struct (org-list-struct))
  6011. (setq eoh (point-at-eol))
  6012. (setq eos (org-list-get-item-end-before-blank (point) struct))
  6013. (setq has-children (org-list-has-child-p (point) struct)))
  6014. (org-back-to-heading)
  6015. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  6016. (setq eos (save-excursion (org-end-of-subtree t t)
  6017. (when (bolp) (backward-char)) (point)))
  6018. (setq has-children
  6019. (or (save-excursion
  6020. (let ((level (funcall outline-level)))
  6021. (outline-next-heading)
  6022. (and (org-at-heading-p t)
  6023. (> (funcall outline-level) level))))
  6024. (save-excursion
  6025. (org-list-search-forward (org-item-beginning-re) eos t)))))
  6026. ;; Determine end invisible part of buffer (EOL)
  6027. (beginning-of-line 2)
  6028. ;; XEmacs doesn't have `next-single-char-property-change'
  6029. (if (featurep 'xemacs)
  6030. (while (and (not (eobp)) ;; this is like `next-line'
  6031. (get-char-property (1- (point)) 'invisible))
  6032. (beginning-of-line 2))
  6033. (while (and (not (eobp)) ;; this is like `next-line'
  6034. (get-char-property (1- (point)) 'invisible))
  6035. (goto-char (next-single-char-property-change (point) 'invisible))
  6036. (and (eolp) (beginning-of-line 2))))
  6037. (setq eol (point)))
  6038. ;; Find out what to do next and set `this-command'
  6039. (cond
  6040. ((= eos eoh)
  6041. ;; Nothing is hidden behind this heading
  6042. (unless (org-before-first-heading-p)
  6043. (run-hook-with-args 'org-pre-cycle-hook 'empty))
  6044. (org-unlogged-message "EMPTY ENTRY")
  6045. (setq org-cycle-subtree-status nil)
  6046. (save-excursion
  6047. (goto-char eos)
  6048. (outline-next-heading)
  6049. (if (outline-invisible-p) (org-flag-heading nil))))
  6050. ((and (or (>= eol eos)
  6051. (not (string-match "\\S-" (buffer-substring eol eos))))
  6052. (or has-children
  6053. (not (setq children-skipped
  6054. org-cycle-skip-children-state-if-no-children))))
  6055. ;; Entire subtree is hidden in one line: children view
  6056. (unless (org-before-first-heading-p)
  6057. (run-hook-with-args 'org-pre-cycle-hook 'children))
  6058. (if (org-at-item-p)
  6059. (org-list-set-item-visibility (point-at-bol) struct 'children)
  6060. (org-show-entry)
  6061. (org-with-limited-levels (show-children))
  6062. ;; FIXME: This slows down the func way too much.
  6063. ;; How keep drawers hidden in subtree anyway?
  6064. ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
  6065. ;; (org-cycle-hide-drawers 'subtree))
  6066. ;; Fold every list in subtree to top-level items.
  6067. (when (eq org-cycle-include-plain-lists 'integrate)
  6068. (save-excursion
  6069. (org-back-to-heading)
  6070. (while (org-list-search-forward (org-item-beginning-re) eos t)
  6071. (beginning-of-line 1)
  6072. (let* ((struct (org-list-struct))
  6073. (prevs (org-list-prevs-alist struct))
  6074. (end (org-list-get-bottom-point struct)))
  6075. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  6076. (org-list-get-all-items (point) struct prevs))
  6077. (goto-char (if (< end eos) end eos)))))))
  6078. (org-unlogged-message "CHILDREN")
  6079. (save-excursion
  6080. (goto-char eos)
  6081. (outline-next-heading)
  6082. (if (outline-invisible-p) (org-flag-heading nil)))
  6083. (setq org-cycle-subtree-status 'children)
  6084. (unless (org-before-first-heading-p)
  6085. (run-hook-with-args 'org-cycle-hook 'children)))
  6086. ((or children-skipped
  6087. (and (eq last-command this-command)
  6088. (eq org-cycle-subtree-status 'children)))
  6089. ;; We just showed the children, or no children are there,
  6090. ;; now show everything.
  6091. (unless (org-before-first-heading-p)
  6092. (run-hook-with-args 'org-pre-cycle-hook 'subtree))
  6093. (outline-flag-region eoh eos nil)
  6094. (org-unlogged-message
  6095. (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  6096. (setq org-cycle-subtree-status 'subtree)
  6097. (unless (org-before-first-heading-p)
  6098. (run-hook-with-args 'org-cycle-hook 'subtree)))
  6099. (t
  6100. ;; Default action: hide the subtree.
  6101. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  6102. (outline-flag-region eoh eos t)
  6103. (org-unlogged-message "FOLDED")
  6104. (setq org-cycle-subtree-status 'folded)
  6105. (unless (org-before-first-heading-p)
  6106. (run-hook-with-args 'org-cycle-hook 'folded))))))
  6107. ;;;###autoload
  6108. (defun org-global-cycle (&optional arg)
  6109. "Cycle the global visibility. For details see `org-cycle'.
  6110. With \\[universal-argument] prefix arg, switch to startup visibility.
  6111. With a numeric prefix, show all headlines up to that level."
  6112. (interactive "P")
  6113. (let ((org-cycle-include-plain-lists
  6114. (if (derived-mode-p 'org-mode) org-cycle-include-plain-lists nil)))
  6115. (cond
  6116. ((integerp arg)
  6117. (show-all)
  6118. (hide-sublevels arg)
  6119. (setq org-cycle-global-status 'contents))
  6120. ((equal arg '(4))
  6121. (org-set-startup-visibility)
  6122. (org-unlogged-message "Startup visibility, plus VISIBILITY properties."))
  6123. (t
  6124. (org-cycle '(4))))))
  6125. (defun org-set-startup-visibility ()
  6126. "Set the visibility required by startup options and properties."
  6127. (cond
  6128. ((eq org-startup-folded t)
  6129. (org-overview))
  6130. ((eq org-startup-folded 'content)
  6131. (org-content))
  6132. ((or (eq org-startup-folded 'showeverything)
  6133. (eq org-startup-folded nil))
  6134. (show-all)))
  6135. (unless (eq org-startup-folded 'showeverything)
  6136. (if org-hide-block-startup (org-hide-block-all))
  6137. (org-set-visibility-according-to-property 'no-cleanup)
  6138. (org-cycle-hide-archived-subtrees 'all)
  6139. (org-cycle-hide-drawers 'all)
  6140. (org-cycle-show-empty-lines t)))
  6141. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  6142. "Switch subtree visibilities according to :VISIBILITY: property."
  6143. (interactive)
  6144. (let (org-show-entry-below state)
  6145. (save-excursion
  6146. (goto-char (point-min))
  6147. (while (re-search-forward
  6148. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  6149. nil t)
  6150. (setq state (match-string 1))
  6151. (save-excursion
  6152. (org-back-to-heading t)
  6153. (hide-subtree)
  6154. (org-reveal)
  6155. (cond
  6156. ((equal state '("fold" "folded"))
  6157. (hide-subtree))
  6158. ((equal state "children")
  6159. (org-show-hidden-entry)
  6160. (show-children))
  6161. ((equal state "content")
  6162. (save-excursion
  6163. (save-restriction
  6164. (org-narrow-to-subtree)
  6165. (org-content))))
  6166. ((member state '("all" "showall"))
  6167. (show-subtree)))))
  6168. (unless no-cleanup
  6169. (org-cycle-hide-archived-subtrees 'all)
  6170. (org-cycle-hide-drawers 'all)
  6171. (org-cycle-show-empty-lines 'all)))))
  6172. ;; This function uses outline-regexp instead of the more fundamental
  6173. ;; org-outline-regexp so that org-cycle-global works outside of Org
  6174. ;; buffers, where outline-regexp is needed.
  6175. (defun org-overview ()
  6176. "Switch to overview mode, showing only top-level headlines.
  6177. This shows all headlines with a level equal or greater than the level
  6178. of the first headline in the buffer. This is important, because if the
  6179. first headline is not level one, then (hide-sublevels 1) gives confusing
  6180. results."
  6181. (interactive)
  6182. (save-excursion
  6183. (let ((level
  6184. (save-excursion
  6185. (goto-char (point-min))
  6186. (if (re-search-forward (concat "^" outline-regexp) nil t)
  6187. (progn
  6188. (goto-char (match-beginning 0))
  6189. (funcall outline-level))))))
  6190. (and level (hide-sublevels level)))))
  6191. (defun org-content (&optional arg)
  6192. "Show all headlines in the buffer, like a table of contents.
  6193. With numerical argument N, show content up to level N."
  6194. (interactive "P")
  6195. (org-overview)
  6196. (save-excursion
  6197. ;; Visit all headings and show their offspring
  6198. (and (integerp arg) (org-overview))
  6199. (goto-char (point-max))
  6200. (catch 'exit
  6201. (while (and (progn (condition-case nil
  6202. (outline-previous-visible-heading 1)
  6203. (error (goto-char (point-min))))
  6204. t)
  6205. (looking-at org-outline-regexp))
  6206. (if (integerp arg)
  6207. (show-children (1- arg))
  6208. (show-branches))
  6209. (if (bobp) (throw 'exit nil))))))
  6210. (defun org-optimize-window-after-visibility-change (state)
  6211. "Adjust the window after a change in outline visibility.
  6212. This function is the default value of the hook `org-cycle-hook'."
  6213. (when (get-buffer-window (current-buffer))
  6214. (cond
  6215. ((eq state 'content) nil)
  6216. ((eq state 'all) nil)
  6217. ((eq state 'folded) nil)
  6218. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  6219. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  6220. (defun org-remove-empty-overlays-at (pos)
  6221. "Remove outline overlays that do not contain non-white stuff."
  6222. (mapc
  6223. (lambda (o)
  6224. (and (eq 'outline (overlay-get o 'invisible))
  6225. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  6226. (overlay-end o))))
  6227. (delete-overlay o)))
  6228. (overlays-at pos)))
  6229. (defun org-clean-visibility-after-subtree-move ()
  6230. "Fix visibility issues after moving a subtree."
  6231. ;; First, find a reasonable region to look at:
  6232. ;; Start two siblings above, end three below
  6233. (let* ((beg (save-excursion
  6234. (and (org-get-last-sibling)
  6235. (org-get-last-sibling))
  6236. (point)))
  6237. (end (save-excursion
  6238. (and (org-get-next-sibling)
  6239. (org-get-next-sibling)
  6240. (org-get-next-sibling))
  6241. (if (org-at-heading-p)
  6242. (point-at-eol)
  6243. (point))))
  6244. (level (looking-at "\\*+"))
  6245. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  6246. (save-excursion
  6247. (save-restriction
  6248. (narrow-to-region beg end)
  6249. (when re
  6250. ;; Properly fold already folded siblings
  6251. (goto-char (point-min))
  6252. (while (re-search-forward re nil t)
  6253. (if (and (not (outline-invisible-p))
  6254. (save-excursion
  6255. (goto-char (point-at-eol)) (outline-invisible-p)))
  6256. (hide-entry))))
  6257. (org-cycle-show-empty-lines 'overview)
  6258. (org-cycle-hide-drawers 'overview)))))
  6259. (defun org-cycle-show-empty-lines (state)
  6260. "Show empty lines above all visible headlines.
  6261. The region to be covered depends on STATE when called through
  6262. `org-cycle-hook'. Lisp program can use t for STATE to get the
  6263. entire buffer covered. Note that an empty line is only shown if there
  6264. are at least `org-cycle-separator-lines' empty lines before the headline."
  6265. (when (not (= org-cycle-separator-lines 0))
  6266. (save-excursion
  6267. (let* ((n (abs org-cycle-separator-lines))
  6268. (re (cond
  6269. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  6270. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  6271. (t (let ((ns (number-to-string (- n 2))))
  6272. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  6273. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  6274. beg end b e)
  6275. (cond
  6276. ((memq state '(overview contents t))
  6277. (setq beg (point-min) end (point-max)))
  6278. ((memq state '(children folded))
  6279. (setq beg (point) end (progn (org-end-of-subtree t t)
  6280. (beginning-of-line 2)
  6281. (point)))))
  6282. (when beg
  6283. (goto-char beg)
  6284. (while (re-search-forward re end t)
  6285. (unless (get-char-property (match-end 1) 'invisible)
  6286. (setq e (match-end 1))
  6287. (if (< org-cycle-separator-lines 0)
  6288. (setq b (save-excursion
  6289. (goto-char (match-beginning 0))
  6290. (org-back-over-empty-lines)
  6291. (if (save-excursion
  6292. (goto-char (max (point-min) (1- (point))))
  6293. (org-at-heading-p))
  6294. (1- (point))
  6295. (point))))
  6296. (setq b (match-beginning 1)))
  6297. (outline-flag-region b e nil)))))))
  6298. ;; Never hide empty lines at the end of the file.
  6299. (save-excursion
  6300. (goto-char (point-max))
  6301. (outline-previous-heading)
  6302. (outline-end-of-heading)
  6303. (if (and (looking-at "[ \t\n]+")
  6304. (= (match-end 0) (point-max)))
  6305. (outline-flag-region (point) (match-end 0) nil))))
  6306. (defun org-show-empty-lines-in-parent ()
  6307. "Move to the parent and re-show empty lines before visible headlines."
  6308. (save-excursion
  6309. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  6310. (org-cycle-show-empty-lines context))))
  6311. (defun org-files-list ()
  6312. "Return `org-agenda-files' list, plus all open org-mode files.
  6313. This is useful for operations that need to scan all of a user's
  6314. open and agenda-wise Org files."
  6315. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  6316. (dolist (buf (buffer-list))
  6317. (with-current-buffer buf
  6318. (if (and (derived-mode-p 'org-mode) (buffer-file-name))
  6319. (let ((file (expand-file-name (buffer-file-name))))
  6320. (unless (member file files)
  6321. (push file files))))))
  6322. files))
  6323. (defsubst org-entry-beginning-position ()
  6324. "Return the beginning position of the current entry."
  6325. (save-excursion (outline-back-to-heading t) (point)))
  6326. (defsubst org-entry-end-position ()
  6327. "Return the end position of the current entry."
  6328. (save-excursion (outline-next-heading) (point)))
  6329. (defun org-cycle-hide-drawers (state)
  6330. "Re-hide all drawers after a visibility state change."
  6331. (when (and (derived-mode-p 'org-mode)
  6332. (not (memq state '(overview folded contents))))
  6333. (save-excursion
  6334. (let* ((globalp (memq state '(contents all)))
  6335. (beg (if globalp (point-min) (point)))
  6336. (end (if globalp (point-max)
  6337. (if (eq state 'children)
  6338. (save-excursion (outline-next-heading) (point))
  6339. (org-end-of-subtree t)))))
  6340. (goto-char beg)
  6341. (while (re-search-forward org-drawer-regexp end t)
  6342. (org-flag-drawer t))))))
  6343. (defun org-cycle-hide-inline-tasks (state)
  6344. "Re-hide inline tasks when switching to 'contents or 'children
  6345. visibility state."
  6346. (case state
  6347. (contents
  6348. (when (org-bound-and-true-p org-inlinetask-min-level)
  6349. (hide-sublevels (1- org-inlinetask-min-level))))
  6350. (children
  6351. (when (featurep 'org-inlinetask)
  6352. (save-excursion
  6353. (while (and (outline-next-heading)
  6354. (org-inlinetask-at-task-p))
  6355. (org-inlinetask-toggle-visibility)
  6356. (org-inlinetask-goto-end)))))))
  6357. (defun org-flag-drawer (flag)
  6358. "When FLAG is non-nil, hide the drawer we are within.
  6359. Otherwise make it visible."
  6360. (save-excursion
  6361. (beginning-of-line 1)
  6362. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  6363. (let ((b (match-end 0)))
  6364. (if (re-search-forward
  6365. "^[ \t]*:END:"
  6366. (save-excursion (outline-next-heading) (point)) t)
  6367. (outline-flag-region b (point-at-eol) flag)
  6368. (user-error ":END: line missing at position %s" b))))))
  6369. (defun org-subtree-end-visible-p ()
  6370. "Is the end of the current subtree visible?"
  6371. (pos-visible-in-window-p
  6372. (save-excursion (org-end-of-subtree t) (point))))
  6373. (defun org-first-headline-recenter ()
  6374. "Move cursor to the first headline and recenter the headline."
  6375. (goto-char (point-min))
  6376. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  6377. (set-window-start (selected-window) (point-at-bol))))
  6378. ;;; Saving and restoring visibility
  6379. (defun org-outline-overlay-data (&optional use-markers)
  6380. "Return a list of the locations of all outline overlays.
  6381. These are overlays with the `invisible' property value `outline'.
  6382. The return value is a list of cons cells, with start and stop
  6383. positions for each overlay.
  6384. If USE-MARKERS is set, return the positions as markers."
  6385. (let (beg end)
  6386. (save-excursion
  6387. (save-restriction
  6388. (widen)
  6389. (delq nil
  6390. (mapcar (lambda (o)
  6391. (when (eq (overlay-get o 'invisible) 'outline)
  6392. (setq beg (overlay-start o)
  6393. end (overlay-end o))
  6394. (and beg end (> end beg)
  6395. (if use-markers
  6396. (cons (move-marker (make-marker) beg)
  6397. (move-marker (make-marker) end))
  6398. (cons beg end)))))
  6399. (overlays-in (point-min) (point-max))))))))
  6400. (defun org-set-outline-overlay-data (data)
  6401. "Create visibility overlays for all positions in DATA.
  6402. DATA should have been made by `org-outline-overlay-data'."
  6403. (let (o)
  6404. (save-excursion
  6405. (save-restriction
  6406. (widen)
  6407. (show-all)
  6408. (mapc (lambda (c)
  6409. (outline-flag-region (car c) (cdr c) t))
  6410. data)))))
  6411. ;;; Folding of blocks
  6412. (defvar org-hide-block-overlays nil
  6413. "Overlays hiding blocks.")
  6414. (make-variable-buffer-local 'org-hide-block-overlays)
  6415. (defun org-block-map (function &optional start end)
  6416. "Call FUNCTION at the head of all source blocks in the current buffer.
  6417. Optional arguments START and END can be used to limit the range."
  6418. (let ((start (or start (point-min)))
  6419. (end (or end (point-max))))
  6420. (save-excursion
  6421. (goto-char start)
  6422. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  6423. (save-excursion
  6424. (save-match-data
  6425. (goto-char (match-beginning 0))
  6426. (funcall function)))))))
  6427. (defun org-hide-block-toggle-all ()
  6428. "Toggle the visibility of all blocks in the current buffer."
  6429. (org-block-map #'org-hide-block-toggle))
  6430. (defun org-hide-block-all ()
  6431. "Fold all blocks in the current buffer."
  6432. (interactive)
  6433. (org-show-block-all)
  6434. (org-block-map #'org-hide-block-toggle-maybe))
  6435. (defun org-show-block-all ()
  6436. "Unfold all blocks in the current buffer."
  6437. (interactive)
  6438. (mapc 'delete-overlay org-hide-block-overlays)
  6439. (setq org-hide-block-overlays nil))
  6440. (defun org-hide-block-toggle-maybe ()
  6441. "Toggle visibility of block at point."
  6442. (interactive)
  6443. (let ((case-fold-search t))
  6444. (if (save-excursion
  6445. (beginning-of-line 1)
  6446. (looking-at org-block-regexp))
  6447. (progn (org-hide-block-toggle)
  6448. t) ;; to signal that we took action
  6449. nil))) ;; to signal that we did not
  6450. (defun org-hide-block-toggle (&optional force)
  6451. "Toggle the visibility of the current block."
  6452. (interactive)
  6453. (save-excursion
  6454. (beginning-of-line)
  6455. (if (re-search-forward org-block-regexp nil t)
  6456. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  6457. (end (match-end 0)) ;; end of entire body
  6458. ov)
  6459. (if (memq t (mapcar (lambda (overlay)
  6460. (eq (overlay-get overlay 'invisible)
  6461. 'org-hide-block))
  6462. (overlays-at start)))
  6463. (if (or (not force) (eq force 'off))
  6464. (mapc (lambda (ov)
  6465. (when (member ov org-hide-block-overlays)
  6466. (setq org-hide-block-overlays
  6467. (delq ov org-hide-block-overlays)))
  6468. (when (eq (overlay-get ov 'invisible)
  6469. 'org-hide-block)
  6470. (delete-overlay ov)))
  6471. (overlays-at start)))
  6472. (setq ov (make-overlay start end))
  6473. (overlay-put ov 'invisible 'org-hide-block)
  6474. ;; make the block accessible to isearch
  6475. (overlay-put
  6476. ov 'isearch-open-invisible
  6477. (lambda (ov)
  6478. (when (member ov org-hide-block-overlays)
  6479. (setq org-hide-block-overlays
  6480. (delq ov org-hide-block-overlays)))
  6481. (when (eq (overlay-get ov 'invisible)
  6482. 'org-hide-block)
  6483. (delete-overlay ov))))
  6484. (push ov org-hide-block-overlays)))
  6485. (user-error "Not looking at a source block"))))
  6486. ;; org-tab-after-check-for-cycling-hook
  6487. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  6488. ;; Remove overlays when changing major mode
  6489. (add-hook 'org-mode-hook
  6490. (lambda () (org-add-hook 'change-major-mode-hook
  6491. 'org-show-block-all 'append 'local)))
  6492. ;;; Org-goto
  6493. (defvar org-goto-window-configuration nil)
  6494. (defvar org-goto-marker nil)
  6495. (defvar org-goto-map)
  6496. (defun org-goto-map ()
  6497. "Set the keymap `org-goto'."
  6498. (setq org-goto-map
  6499. (let ((map (make-sparse-keymap)))
  6500. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command
  6501. mouse-drag-region universal-argument org-occur))
  6502. cmd)
  6503. (while (setq cmd (pop cmds))
  6504. (substitute-key-definition cmd cmd map global-map)))
  6505. (suppress-keymap map)
  6506. (org-defkey map "\C-m" 'org-goto-ret)
  6507. (org-defkey map [(return)] 'org-goto-ret)
  6508. (org-defkey map [(left)] 'org-goto-left)
  6509. (org-defkey map [(right)] 'org-goto-right)
  6510. (org-defkey map [(control ?g)] 'org-goto-quit)
  6511. (org-defkey map "\C-i" 'org-cycle)
  6512. (org-defkey map [(tab)] 'org-cycle)
  6513. (org-defkey map [(down)] 'outline-next-visible-heading)
  6514. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6515. (if org-goto-auto-isearch
  6516. (if (fboundp 'define-key-after)
  6517. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6518. nil)
  6519. (org-defkey map "q" 'org-goto-quit)
  6520. (org-defkey map "n" 'outline-next-visible-heading)
  6521. (org-defkey map "p" 'outline-previous-visible-heading)
  6522. (org-defkey map "f" 'outline-forward-same-level)
  6523. (org-defkey map "b" 'outline-backward-same-level)
  6524. (org-defkey map "u" 'outline-up-heading))
  6525. (org-defkey map "/" 'org-occur)
  6526. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6527. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6528. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6529. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6530. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6531. map)))
  6532. (defconst org-goto-help
  6533. "Browse buffer copy, to find location or copy text.%s
  6534. RET=jump to location C-g=quit and return to previous location
  6535. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6536. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6537. (defun org-goto (&optional alternative-interface)
  6538. "Look up a different location in the current file, keeping current visibility.
  6539. When you want look-up or go to a different location in a
  6540. document, the fastest way is often to fold the entire buffer and
  6541. then dive into the tree. This method has the disadvantage, that
  6542. the previous location will be folded, which may not be what you
  6543. want.
  6544. This command works around this by showing a copy of the current
  6545. buffer in an indirect buffer, in overview mode. You can dive
  6546. into the tree in that copy, use org-occur and incremental search
  6547. to find a location. When pressing RET or `Q', the command
  6548. returns to the original buffer in which the visibility is still
  6549. unchanged. After RET it will also jump to the location selected
  6550. in the indirect buffer and expose the headline hierarchy above.
  6551. With a prefix argument, use the alternative interface: e.g. if
  6552. `org-goto-interface' is 'outline use 'outline-path-completion."
  6553. (interactive "P")
  6554. (org-goto-map)
  6555. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6556. (org-refile-use-outline-path t)
  6557. (org-refile-target-verify-function nil)
  6558. (interface
  6559. (if (not alternative-interface)
  6560. org-goto-interface
  6561. (if (eq org-goto-interface 'outline)
  6562. 'outline-path-completion
  6563. 'outline)))
  6564. (org-goto-start-pos (point))
  6565. (selected-point
  6566. (if (eq interface 'outline)
  6567. (car (org-get-location (current-buffer) org-goto-help))
  6568. (let ((pa (org-refile-get-location "Goto" nil nil t)))
  6569. (org-refile-check-position pa)
  6570. (nth 3 pa)))))
  6571. (if selected-point
  6572. (progn
  6573. (org-mark-ring-push org-goto-start-pos)
  6574. (goto-char selected-point)
  6575. (if (or (outline-invisible-p) (org-invisible-p2))
  6576. (org-show-context 'org-goto)))
  6577. (message "Quit"))))
  6578. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6579. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6580. (defvar org-goto-local-auto-isearch-map) ; defined below
  6581. (defun org-get-location (buf help)
  6582. "Let the user select a location in the Org-mode buffer BUF.
  6583. This function uses a recursive edit. It returns the selected position
  6584. or nil."
  6585. (org-no-popups
  6586. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6587. (isearch-hide-immediately nil)
  6588. (isearch-search-fun-function
  6589. (lambda () 'org-goto-local-search-headings))
  6590. (org-goto-selected-point org-goto-exit-command))
  6591. (save-excursion
  6592. (save-window-excursion
  6593. (delete-other-windows)
  6594. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6595. (org-pop-to-buffer-same-window
  6596. (condition-case nil
  6597. (make-indirect-buffer (current-buffer) "*org-goto*")
  6598. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6599. (with-output-to-temp-buffer "*Org Help*"
  6600. (princ (format help (if org-goto-auto-isearch
  6601. " Just type for auto-isearch."
  6602. " n/p/f/b/u to navigate, q to quit."))))
  6603. (org-fit-window-to-buffer (get-buffer-window "*Org Help*"))
  6604. (setq buffer-read-only nil)
  6605. (let ((org-startup-truncated t)
  6606. (org-startup-folded nil)
  6607. (org-startup-align-all-tables nil))
  6608. (org-mode)
  6609. (org-overview))
  6610. (setq buffer-read-only t)
  6611. (if (and (boundp 'org-goto-start-pos)
  6612. (integer-or-marker-p org-goto-start-pos))
  6613. (let ((org-show-hierarchy-above t)
  6614. (org-show-siblings t)
  6615. (org-show-following-heading t))
  6616. (goto-char org-goto-start-pos)
  6617. (and (outline-invisible-p) (org-show-context)))
  6618. (goto-char (point-min)))
  6619. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6620. (message "Select location and press RET")
  6621. (use-local-map org-goto-map)
  6622. (recursive-edit)))
  6623. (kill-buffer "*org-goto*")
  6624. (cons org-goto-selected-point org-goto-exit-command))))
  6625. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6626. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6627. ;; `isearch-other-control-char' was removed in Emacs 24.4.
  6628. (if (fboundp 'isearch-other-control-char)
  6629. (progn
  6630. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6631. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char))
  6632. (define-key org-goto-local-auto-isearch-map "\C-i" nil)
  6633. (define-key org-goto-local-auto-isearch-map "\C-m" nil)
  6634. (define-key org-goto-local-auto-isearch-map [return] nil))
  6635. (defun org-goto-local-search-headings (string bound noerror)
  6636. "Search and make sure that any matches are in headlines."
  6637. (catch 'return
  6638. (while (if isearch-forward
  6639. (search-forward string bound noerror)
  6640. (search-backward string bound noerror))
  6641. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  6642. (and (member :headline context)
  6643. (not (member :tags context))))
  6644. (throw 'return (point))))))
  6645. (defun org-goto-local-auto-isearch ()
  6646. "Start isearch."
  6647. (interactive)
  6648. (goto-char (point-min))
  6649. (let ((keys (this-command-keys)))
  6650. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6651. (isearch-mode t)
  6652. (isearch-process-search-char (string-to-char keys)))))
  6653. (defun org-goto-ret (&optional arg)
  6654. "Finish `org-goto' by going to the new location."
  6655. (interactive "P")
  6656. (setq org-goto-selected-point (point)
  6657. org-goto-exit-command 'return)
  6658. (throw 'exit nil))
  6659. (defun org-goto-left ()
  6660. "Finish `org-goto' by going to the new location."
  6661. (interactive)
  6662. (if (org-at-heading-p)
  6663. (progn
  6664. (beginning-of-line 1)
  6665. (setq org-goto-selected-point (point)
  6666. org-goto-exit-command 'left)
  6667. (throw 'exit nil))
  6668. (user-error "Not on a heading")))
  6669. (defun org-goto-right ()
  6670. "Finish `org-goto' by going to the new location."
  6671. (interactive)
  6672. (if (org-at-heading-p)
  6673. (progn
  6674. (setq org-goto-selected-point (point)
  6675. org-goto-exit-command 'right)
  6676. (throw 'exit nil))
  6677. (user-error "Not on a heading")))
  6678. (defun org-goto-quit ()
  6679. "Finish `org-goto' without cursor motion."
  6680. (interactive)
  6681. (setq org-goto-selected-point nil)
  6682. (setq org-goto-exit-command 'quit)
  6683. (throw 'exit nil))
  6684. ;;; Indirect buffer display of subtrees
  6685. (defvar org-indirect-dedicated-frame nil
  6686. "This is the frame being used for indirect tree display.")
  6687. (defvar org-last-indirect-buffer nil)
  6688. (defun org-tree-to-indirect-buffer (&optional arg)
  6689. "Create indirect buffer and narrow it to current subtree.
  6690. With a numerical prefix ARG, go up to this level and then take that tree.
  6691. If ARG is negative, go up that many levels.
  6692. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6693. indirect buffer previously made with this command, to avoid proliferation of
  6694. indirect buffers. However, when you call the command with a \
  6695. \\[universal-argument] prefix, or
  6696. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6697. is kept so that you can work with several indirect buffers at the same time.
  6698. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6699. \\[universal-argument] prefix also
  6700. requests that a new frame be made for the new buffer, so that the dedicated
  6701. frame is not changed."
  6702. (interactive "P")
  6703. (let ((cbuf (current-buffer))
  6704. (cwin (selected-window))
  6705. (pos (point))
  6706. beg end level heading ibuf)
  6707. (save-excursion
  6708. (org-back-to-heading t)
  6709. (when (numberp arg)
  6710. (setq level (org-outline-level))
  6711. (if (< arg 0) (setq arg (+ level arg)))
  6712. (while (> (setq level (org-outline-level)) arg)
  6713. (org-up-heading-safe)))
  6714. (setq beg (point)
  6715. heading (org-get-heading))
  6716. (org-end-of-subtree t t)
  6717. (if (org-at-heading-p) (backward-char 1))
  6718. (setq end (point)))
  6719. (if (and (buffer-live-p org-last-indirect-buffer)
  6720. (not (eq org-indirect-buffer-display 'new-frame))
  6721. (not arg))
  6722. (kill-buffer org-last-indirect-buffer))
  6723. (setq ibuf (org-get-indirect-buffer cbuf)
  6724. org-last-indirect-buffer ibuf)
  6725. (cond
  6726. ((or (eq org-indirect-buffer-display 'new-frame)
  6727. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6728. (select-frame (make-frame))
  6729. (delete-other-windows)
  6730. (org-pop-to-buffer-same-window ibuf)
  6731. (org-set-frame-title heading))
  6732. ((eq org-indirect-buffer-display 'dedicated-frame)
  6733. (raise-frame
  6734. (select-frame (or (and org-indirect-dedicated-frame
  6735. (frame-live-p org-indirect-dedicated-frame)
  6736. org-indirect-dedicated-frame)
  6737. (setq org-indirect-dedicated-frame (make-frame)))))
  6738. (delete-other-windows)
  6739. (org-pop-to-buffer-same-window ibuf)
  6740. (org-set-frame-title (concat "Indirect: " heading)))
  6741. ((eq org-indirect-buffer-display 'current-window)
  6742. (org-pop-to-buffer-same-window ibuf))
  6743. ((eq org-indirect-buffer-display 'other-window)
  6744. (pop-to-buffer ibuf))
  6745. (t (error "Invalid value")))
  6746. (if (featurep 'xemacs)
  6747. (save-excursion (org-mode) (turn-on-font-lock)))
  6748. (narrow-to-region beg end)
  6749. (show-all)
  6750. (goto-char pos)
  6751. (run-hook-with-args 'org-cycle-hook 'all)
  6752. (and (window-live-p cwin) (select-window cwin))))
  6753. (defun org-get-indirect-buffer (&optional buffer)
  6754. (setq buffer (or buffer (current-buffer)))
  6755. (let ((n 1) (base (buffer-name buffer)) bname)
  6756. (while (buffer-live-p
  6757. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  6758. (setq n (1+ n)))
  6759. (condition-case nil
  6760. (make-indirect-buffer buffer bname 'clone)
  6761. (error (make-indirect-buffer buffer bname)))))
  6762. (defun org-set-frame-title (title)
  6763. "Set the title of the current frame to the string TITLE."
  6764. ;; FIXME: how to name a single frame in XEmacs???
  6765. (unless (featurep 'xemacs)
  6766. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6767. ;;;; Structure editing
  6768. ;;; Inserting headlines
  6769. (defun org-previous-line-empty-p (&optional next)
  6770. "Is the previous line a blank line?
  6771. When NEXT is non-nil, check the next line instead."
  6772. (save-excursion
  6773. (and (not (bobp))
  6774. (or (beginning-of-line (if next 2 0)) t)
  6775. (save-match-data
  6776. (looking-at "[ \t]*$")))))
  6777. (defun org-insert-heading (&optional arg invisible-ok)
  6778. "Insert a new heading or an item with the same depth at point.
  6779. If point is at the beginning of a heading or a list item, insert
  6780. a new heading or a new item above the current one. If point is
  6781. at the beginning of a normal line, turn the line into a heading.
  6782. If point is in the middle of a headline or a list item, split the
  6783. headline or the item and create a new headline/item with the text
  6784. in the current line after point \(see `org-M-RET-may-split-line'
  6785. on how to modify this behavior).
  6786. With one universal prefirx argument, set the user option
  6787. `org-insert-heading-respect-content' to t for the duration of
  6788. the command. This modifies the behavior described above in this
  6789. ways: on list items and at the beginning of normal lines, force
  6790. the insertion of a heading after the current subtree.
  6791. With two universal prefix arguments, insert the heading at the
  6792. end of the grandparent subtree. For example, if point is within
  6793. a 2nd-level heading, then it will insert a 2nd-level heading at
  6794. the end of the 1st-level parent heading.
  6795. When INVISIBLE-OK is set, stop at invisible headlines when going
  6796. back. This is important for non-interactive uses of the
  6797. command."
  6798. (interactive "P")
  6799. (if (org-called-interactively-p 'any) (org-reveal))
  6800. (let ((itemp (org-in-item-p))
  6801. (may-split (org-get-alist-option org-M-RET-may-split-line 'headline))
  6802. (respect-content (or org-insert-heading-respect-content
  6803. (equal arg '(4))))
  6804. (initial-content "")
  6805. (adjust-empty-lines t))
  6806. (cond
  6807. ((or (= (buffer-size) 0)
  6808. (and (not (save-excursion
  6809. (and (ignore-errors (org-back-to-heading invisible-ok))
  6810. (org-at-heading-p))))
  6811. (or arg (not itemp))))
  6812. ;; At beginning of buffer or so high up that only a heading
  6813. ;; makes sense.
  6814. (cond ((and (bolp) (not respect-content)) (insert "* "))
  6815. ((not respect-content)
  6816. (unless may-split (end-of-line))
  6817. (insert "\n* "))
  6818. ((re-search-forward org-outline-regexp-bol nil t)
  6819. (beginning-of-line)
  6820. (insert "* \n")
  6821. (backward-char))
  6822. (t (goto-char (point-max))
  6823. (insert "\n* ")))
  6824. (run-hooks 'org-insert-heading-hook))
  6825. ((and itemp (not (member arg '((4) (16)))) (org-insert-item)))
  6826. (t
  6827. ;; Maybe move at the end of the subtree
  6828. (when (equal arg '(16))
  6829. (org-up-heading-safe)
  6830. (org-end-of-subtree t))
  6831. ;; Insert a heading
  6832. (save-restriction
  6833. (widen)
  6834. (let* ((level nil)
  6835. (on-heading (org-at-heading-p))
  6836. (empty-line-p (if on-heading
  6837. (org-previous-line-empty-p)
  6838. ;; We will decide later
  6839. nil))
  6840. ;; Get a level string to fall back on
  6841. (fix-level
  6842. (if (org-before-first-heading-p) "*"
  6843. (save-excursion
  6844. (org-back-to-heading t)
  6845. (if (org-previous-line-empty-p) (setq empty-line-p t))
  6846. (looking-at org-outline-regexp)
  6847. (make-string (1- (length (match-string 0))) ?*))))
  6848. (stars
  6849. (save-excursion
  6850. (condition-case nil
  6851. (progn
  6852. (org-back-to-heading invisible-ok)
  6853. (when (and (not on-heading)
  6854. (featurep 'org-inlinetask)
  6855. (integerp org-inlinetask-min-level)
  6856. (>= (length (match-string 0))
  6857. org-inlinetask-min-level))
  6858. ;; Find a heading level before the inline task
  6859. (while (and (setq level (org-up-heading-safe))
  6860. (>= level org-inlinetask-min-level)))
  6861. (if (org-at-heading-p)
  6862. (org-back-to-heading invisible-ok)
  6863. (error "This should not happen")))
  6864. (unless (and (save-excursion
  6865. (save-match-data
  6866. (org-backward-heading-same-level
  6867. 1 invisible-ok))
  6868. (= (point) (match-beginning 0)))
  6869. (not (org-previous-line-empty-p t)))
  6870. (setq empty-line-p (or empty-line-p
  6871. (org-previous-line-empty-p))))
  6872. (match-string 0))
  6873. (error (or fix-level "* ")))))
  6874. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6875. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6876. pos hide-previous previous-pos)
  6877. ;; If we insert after content, move there and clean up whitespace
  6878. (when (and respect-content
  6879. (not (org-looking-at-p org-outline-regexp-bol)))
  6880. (if (not (org-before-first-heading-p))
  6881. (org-end-of-subtree nil t)
  6882. (re-search-forward org-outline-regexp-bol)
  6883. (beginning-of-line 0))
  6884. (skip-chars-backward " \r\n")
  6885. (and (not (looking-back "^\*+" (line-beginning-position)))
  6886. (looking-at "[ \t]+") (replace-match ""))
  6887. (unless (eobp) (forward-char 1))
  6888. (when (looking-at "^\\*")
  6889. (unless (bobp) (backward-char 1))
  6890. (insert "\n")))
  6891. ;; If we are splitting, grab the text that should be moved to the new headline
  6892. (when may-split
  6893. (if (org-on-heading-p)
  6894. ;; This is a heading, we split intelligently (keeping tags)
  6895. (let ((pos (point)))
  6896. (goto-char (point-at-bol))
  6897. (unless (looking-at org-complex-heading-regexp)
  6898. (error "This should not happen"))
  6899. (when (and (match-beginning 4)
  6900. (> pos (match-beginning 4))
  6901. (< pos (match-end 4)))
  6902. (setq initial-content (buffer-substring pos (match-end 4)))
  6903. (goto-char pos)
  6904. (delete-region (point) (match-end 4))
  6905. (if (looking-at "[ \t]*$")
  6906. (replace-match "")
  6907. (insert (make-string (length initial-content) ?\ )))
  6908. (setq initial-content (org-trim initial-content)))
  6909. (goto-char pos))
  6910. ;; a normal line
  6911. (setq initial-content
  6912. (org-trim (buffer-substring (point) (point-at-eol))))
  6913. (delete-region (point) (point-at-eol))))
  6914. ;; If we are at the beginning of the line, insert before it. Else after
  6915. (cond
  6916. ((and (bolp) (looking-at "[ \t]*$")))
  6917. ((and (bolp) (not (looking-at "[ \t]*$")))
  6918. (open-line 1))
  6919. (t
  6920. (goto-char (point-at-eol))
  6921. (insert "\n")))
  6922. ;; Insert the new heading
  6923. (insert stars)
  6924. (just-one-space)
  6925. (insert initial-content)
  6926. (when adjust-empty-lines
  6927. (if (or (not blank)
  6928. (and blank (not (org-previous-line-empty-p))))
  6929. (org-N-empty-lines-before-current (if blank 1 0))))
  6930. (run-hooks 'org-insert-heading-hook)))))))
  6931. (defun org-N-empty-lines-before-current (N)
  6932. "Make the number of empty lines before current exactly N.
  6933. So this will delete or add empty lines."
  6934. (save-excursion
  6935. (beginning-of-line)
  6936. (let ((p (point)))
  6937. (skip-chars-backward " \r\t\n")
  6938. (unless (bolp) (forward-line))
  6939. (delete-region (point) p))
  6940. (when (> N 0) (insert (make-string N ?\n)))))
  6941. (defun org-get-heading (&optional no-tags no-todo)
  6942. "Return the heading of the current entry, without the stars.
  6943. When NO-TAGS is non-nil, don't include tags.
  6944. When NO-TODO is non-nil, don't include TODO keywords."
  6945. (save-excursion
  6946. (org-back-to-heading t)
  6947. (cond
  6948. ((and no-tags no-todo)
  6949. (looking-at org-complex-heading-regexp)
  6950. (match-string 4))
  6951. (no-tags
  6952. (looking-at (concat org-outline-regexp
  6953. "\\(.*?\\)"
  6954. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  6955. (match-string 1))
  6956. (no-todo
  6957. (looking-at org-todo-line-regexp)
  6958. (match-string 3))
  6959. (t (looking-at org-heading-regexp)
  6960. (match-string 2)))))
  6961. (defvar orgstruct-mode) ; defined below
  6962. (defun org-heading-components ()
  6963. "Return the components of the current heading.
  6964. This is a list with the following elements:
  6965. - the level as an integer
  6966. - the reduced level, different if `org-odd-levels-only' is set.
  6967. - the TODO keyword, or nil
  6968. - the priority character, like ?A, or nil if no priority is given
  6969. - the headline text itself, or the tags string if no headline text
  6970. - the tags string, or nil."
  6971. (save-excursion
  6972. (org-back-to-heading t)
  6973. (if (let (case-fold-search)
  6974. (looking-at
  6975. (if orgstruct-mode
  6976. org-heading-regexp
  6977. org-complex-heading-regexp)))
  6978. (if orgstruct-mode
  6979. (list (length (match-string 1))
  6980. (org-reduced-level (length (match-string 1)))
  6981. nil
  6982. nil
  6983. (match-string 2)
  6984. nil)
  6985. (list (length (match-string 1))
  6986. (org-reduced-level (length (match-string 1)))
  6987. (org-match-string-no-properties 2)
  6988. (and (match-end 3) (aref (match-string 3) 2))
  6989. (org-match-string-no-properties 4)
  6990. (org-match-string-no-properties 5))))))
  6991. (defun org-get-entry ()
  6992. "Get the entry text, after heading, entire subtree."
  6993. (save-excursion
  6994. (org-back-to-heading t)
  6995. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  6996. (defun org-insert-heading-after-current ()
  6997. "Insert a new heading with same level as current, after current subtree."
  6998. (interactive)
  6999. (org-back-to-heading)
  7000. (org-insert-heading)
  7001. (org-move-subtree-down)
  7002. (end-of-line 1))
  7003. (defun org-insert-heading-respect-content (&optional invisible-ok)
  7004. "Insert heading with `org-insert-heading-respect-content' set to t."
  7005. (interactive)
  7006. (org-insert-heading '(4) invisible-ok))
  7007. (defun org-insert-todo-heading-respect-content (&optional force-state)
  7008. "Insert TODO heading with `org-insert-heading-respect-content' set to t."
  7009. (interactive)
  7010. (org-insert-todo-heading force-state '(4)))
  7011. (defun org-insert-todo-heading (arg &optional force-heading)
  7012. "Insert a new heading with the same level and TODO state as current heading.
  7013. If the heading has no TODO state, or if the state is DONE, use the first
  7014. state (TODO by default). Also with one prefix arg, force first state. With
  7015. two prefix args, force inserting at the end of the parent subtree."
  7016. (interactive "P")
  7017. (when (or force-heading (not (org-insert-item 'checkbox)))
  7018. (org-insert-heading (or (and (equal arg '(16)) '(16))
  7019. force-heading))
  7020. (save-excursion
  7021. (org-back-to-heading)
  7022. (outline-previous-heading)
  7023. (looking-at org-todo-line-regexp))
  7024. (let*
  7025. ((new-mark-x
  7026. (if (or (equal arg '(4))
  7027. (not (match-beginning 2))
  7028. (member (match-string 2) org-done-keywords))
  7029. (car org-todo-keywords-1)
  7030. (match-string 2)))
  7031. (new-mark
  7032. (or
  7033. (run-hook-with-args-until-success
  7034. 'org-todo-get-default-hook new-mark-x nil)
  7035. new-mark-x)))
  7036. (beginning-of-line 1)
  7037. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  7038. (if org-treat-insert-todo-heading-as-state-change
  7039. (org-todo new-mark)
  7040. (insert new-mark " "))))
  7041. (when org-provide-todo-statistics
  7042. (org-update-parent-todo-statistics))))
  7043. (defun org-insert-subheading (arg)
  7044. "Insert a new subheading and demote it.
  7045. Works for outline headings and for plain lists alike."
  7046. (interactive "P")
  7047. (org-insert-heading arg)
  7048. (cond
  7049. ((org-at-heading-p) (org-do-demote))
  7050. ((org-at-item-p) (org-indent-item))))
  7051. (defun org-insert-todo-subheading (arg)
  7052. "Insert a new subheading with TODO keyword or checkbox and demote it.
  7053. Works for outline headings and for plain lists alike."
  7054. (interactive "P")
  7055. (org-insert-todo-heading arg)
  7056. (cond
  7057. ((org-at-heading-p) (org-do-demote))
  7058. ((org-at-item-p) (org-indent-item))))
  7059. ;;; Promotion and Demotion
  7060. (defvar org-after-demote-entry-hook nil
  7061. "Hook run after an entry has been demoted.
  7062. The cursor will be at the beginning of the entry.
  7063. When a subtree is being demoted, the hook will be called for each node.")
  7064. (defvar org-after-promote-entry-hook nil
  7065. "Hook run after an entry has been promoted.
  7066. The cursor will be at the beginning of the entry.
  7067. When a subtree is being promoted, the hook will be called for each node.")
  7068. (defun org-promote-subtree ()
  7069. "Promote the entire subtree.
  7070. See also `org-promote'."
  7071. (interactive)
  7072. (save-excursion
  7073. (org-with-limited-levels (org-map-tree 'org-promote)))
  7074. (org-fix-position-after-promote))
  7075. (defun org-demote-subtree ()
  7076. "Demote the entire subtree. See `org-demote'.
  7077. See also `org-promote'."
  7078. (interactive)
  7079. (save-excursion
  7080. (org-with-limited-levels (org-map-tree 'org-demote)))
  7081. (org-fix-position-after-promote))
  7082. (defun org-do-promote ()
  7083. "Promote the current heading higher up the tree.
  7084. If the region is active in `transient-mark-mode', promote all headings
  7085. in the region."
  7086. (interactive)
  7087. (save-excursion
  7088. (if (org-region-active-p)
  7089. (org-map-region 'org-promote (region-beginning) (region-end))
  7090. (org-promote)))
  7091. (org-fix-position-after-promote))
  7092. (defun org-do-demote ()
  7093. "Demote the current heading lower down the tree.
  7094. If the region is active in `transient-mark-mode', demote all headings
  7095. in the region."
  7096. (interactive)
  7097. (save-excursion
  7098. (if (org-region-active-p)
  7099. (org-map-region 'org-demote (region-beginning) (region-end))
  7100. (org-demote)))
  7101. (org-fix-position-after-promote))
  7102. (defun org-fix-position-after-promote ()
  7103. "Make sure that after pro/demotion cursor position is right."
  7104. (let ((pos (point)))
  7105. (when (save-excursion
  7106. (beginning-of-line 1)
  7107. (looking-at org-todo-line-regexp)
  7108. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  7109. (cond ((eobp) (insert " "))
  7110. ((eolp) (insert " "))
  7111. ((equal (char-after) ?\ ) (forward-char 1))))))
  7112. (defun org-current-level ()
  7113. "Return the level of the current entry, or nil if before the first headline.
  7114. The level is the number of stars at the beginning of the headline."
  7115. (save-excursion
  7116. (org-with-limited-levels
  7117. (if (ignore-errors (org-back-to-heading t))
  7118. (funcall outline-level)))))
  7119. (defun org-get-previous-line-level ()
  7120. "Return the outline depth of the last headline before the current line.
  7121. Returns 0 for the first headline in the buffer, and nil if before the
  7122. first headline."
  7123. (and (org-current-level)
  7124. (or (and (/= (line-beginning-position) (point-min))
  7125. (save-excursion (beginning-of-line 0) (org-current-level)))
  7126. 0)))
  7127. (defun org-reduced-level (l)
  7128. "Compute the effective level of a heading.
  7129. This takes into account the setting of `org-odd-levels-only'."
  7130. (cond
  7131. ((zerop l) 0)
  7132. (org-odd-levels-only (1+ (floor (/ l 2))))
  7133. (t l)))
  7134. (defun org-level-increment ()
  7135. "Return the number of stars that will be added or removed at a
  7136. time to headlines when structure editing, based on the value of
  7137. `org-odd-levels-only'."
  7138. (if org-odd-levels-only 2 1))
  7139. (defun org-get-valid-level (level &optional change)
  7140. "Rectify a level change under the influence of `org-odd-levels-only'
  7141. LEVEL is a current level, CHANGE is by how much the level should be
  7142. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  7143. even level numbers will become the next higher odd number."
  7144. (if org-odd-levels-only
  7145. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  7146. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  7147. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  7148. (max 1 (+ level (or change 0)))))
  7149. (if (boundp 'define-obsolete-function-alias)
  7150. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  7151. (define-obsolete-function-alias 'org-get-legal-level
  7152. 'org-get-valid-level)
  7153. (define-obsolete-function-alias 'org-get-legal-level
  7154. 'org-get-valid-level "23.1")))
  7155. (defun org-promote ()
  7156. "Promote the current heading higher up the tree.
  7157. If the region is active in `transient-mark-mode', promote all headings
  7158. in the region."
  7159. (org-back-to-heading t)
  7160. (let* ((level (save-match-data (funcall outline-level)))
  7161. (after-change-functions (remove 'flyspell-after-change-function
  7162. after-change-functions))
  7163. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  7164. (diff (abs (- level (length up-head) -1))))
  7165. (cond ((and (= level 1) org-called-with-limited-levels
  7166. org-allow-promoting-top-level-subtree)
  7167. (replace-match "# " nil t))
  7168. ((= level 1)
  7169. (user-error "Cannot promote to level 0. UNDO to recover if necessary"))
  7170. (t (replace-match up-head nil t)))
  7171. ;; Fixup tag positioning
  7172. (unless (= level 1)
  7173. (and org-auto-align-tags (org-set-tags nil 'ignore-column))
  7174. (if org-adapt-indentation (org-fixup-indentation (- diff))))
  7175. (run-hooks 'org-after-promote-entry-hook)))
  7176. (defun org-demote ()
  7177. "Demote the current heading lower down the tree.
  7178. If the region is active in `transient-mark-mode', demote all headings
  7179. in the region."
  7180. (org-back-to-heading t)
  7181. (let* ((level (save-match-data (funcall outline-level)))
  7182. (after-change-functions (remove 'flyspell-after-change-function
  7183. after-change-functions))
  7184. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  7185. (diff (abs (- level (length down-head) -1))))
  7186. (replace-match down-head nil t)
  7187. ;; Fixup tag positioning
  7188. (and org-auto-align-tags (org-set-tags nil 'ignore-column))
  7189. (if org-adapt-indentation (org-fixup-indentation diff))
  7190. (run-hooks 'org-after-demote-entry-hook)))
  7191. (defun org-cycle-level ()
  7192. "Cycle the level of an empty headline through possible states.
  7193. This goes first to child, then to parent, level, then up the hierarchy.
  7194. After top level, it switches back to sibling level."
  7195. (interactive)
  7196. (let ((org-adapt-indentation nil))
  7197. (when (org-point-at-end-of-empty-headline)
  7198. (setq this-command 'org-cycle-level) ; Only needed for caching
  7199. (let ((cur-level (org-current-level))
  7200. (prev-level (org-get-previous-line-level)))
  7201. (cond
  7202. ;; If first headline in file, promote to top-level.
  7203. ((= prev-level 0)
  7204. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7205. do (org-do-promote)))
  7206. ;; If same level as prev, demote one.
  7207. ((= prev-level cur-level)
  7208. (org-do-demote))
  7209. ;; If parent is top-level, promote to top level if not already.
  7210. ((= prev-level 1)
  7211. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7212. do (org-do-promote)))
  7213. ;; If top-level, return to prev-level.
  7214. ((= cur-level 1)
  7215. (loop repeat (/ (- prev-level 1) (org-level-increment))
  7216. do (org-do-demote)))
  7217. ;; If less than prev-level, promote one.
  7218. ((< cur-level prev-level)
  7219. (org-do-promote))
  7220. ;; If deeper than prev-level, promote until higher than
  7221. ;; prev-level.
  7222. ((> cur-level prev-level)
  7223. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  7224. do (org-do-promote))))
  7225. t))))
  7226. (defun org-map-tree (fun)
  7227. "Call FUN for every heading underneath the current one."
  7228. (org-back-to-heading)
  7229. (let ((level (funcall outline-level)))
  7230. (save-excursion
  7231. (funcall fun)
  7232. (while (and (progn
  7233. (outline-next-heading)
  7234. (> (funcall outline-level) level))
  7235. (not (eobp)))
  7236. (funcall fun)))))
  7237. (defun org-map-region (fun beg end)
  7238. "Call FUN for every heading between BEG and END."
  7239. (let ((org-ignore-region t))
  7240. (save-excursion
  7241. (setq end (copy-marker end))
  7242. (goto-char beg)
  7243. (if (and (re-search-forward org-outline-regexp-bol nil t)
  7244. (< (point) end))
  7245. (funcall fun))
  7246. (while (and (progn
  7247. (outline-next-heading)
  7248. (< (point) end))
  7249. (not (eobp)))
  7250. (funcall fun)))))
  7251. (defvar org-property-end-re) ; silence byte-compiler
  7252. (defun org-fixup-indentation (diff)
  7253. "Change the indentation in the current entry by DIFF.
  7254. However, if any line in the current entry has no indentation, or if it
  7255. would end up with no indentation after the change, nothing at all is done."
  7256. (save-excursion
  7257. (let ((end (save-excursion (outline-next-heading)
  7258. (point-marker)))
  7259. (prohibit (if (> diff 0)
  7260. "^\\S-"
  7261. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  7262. col)
  7263. (unless (save-excursion (end-of-line 1)
  7264. (re-search-forward prohibit end t))
  7265. (while (and (< (point) end)
  7266. (re-search-forward "^[ \t]+" end t))
  7267. (goto-char (match-end 0))
  7268. (setq col (current-column))
  7269. (if (< diff 0) (replace-match ""))
  7270. (org-indent-to-column (+ diff col))))
  7271. (move-marker end nil))))
  7272. (defun org-convert-to-odd-levels ()
  7273. "Convert an org-mode file with all levels allowed to one with odd levels.
  7274. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  7275. level 5 etc."
  7276. (interactive)
  7277. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  7278. (let ((outline-level 'org-outline-level)
  7279. (org-odd-levels-only nil) n)
  7280. (save-excursion
  7281. (goto-char (point-min))
  7282. (while (re-search-forward "^\\*\\*+ " nil t)
  7283. (setq n (- (length (match-string 0)) 2))
  7284. (while (>= (setq n (1- n)) 0)
  7285. (org-demote))
  7286. (end-of-line 1))))))
  7287. (defun org-convert-to-oddeven-levels ()
  7288. "Convert an org-mode file with only odd levels to one with odd/even levels.
  7289. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  7290. file contains a section with an even level, conversion would
  7291. destroy the structure of the file. An error is signaled in this
  7292. case."
  7293. (interactive)
  7294. (goto-char (point-min))
  7295. ;; First check if there are no even levels
  7296. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  7297. (org-show-context t)
  7298. (error "Not all levels are odd in this file. Conversion not possible"))
  7299. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  7300. (let ((outline-regexp org-outline-regexp)
  7301. (outline-level 'org-outline-level)
  7302. (org-odd-levels-only nil) n)
  7303. (save-excursion
  7304. (goto-char (point-min))
  7305. (while (re-search-forward "^\\*\\*+ " nil t)
  7306. (setq n (/ (1- (length (match-string 0))) 2))
  7307. (while (>= (setq n (1- n)) 0)
  7308. (org-promote))
  7309. (end-of-line 1))))))
  7310. (defun org-tr-level (n)
  7311. "Make N odd if required."
  7312. (if org-odd-levels-only (1+ (/ n 2)) n))
  7313. ;;; Vertical tree motion, cutting and pasting of subtrees
  7314. (defun org-move-subtree-up (&optional arg)
  7315. "Move the current subtree up past ARG headlines of the same level."
  7316. (interactive "p")
  7317. (org-move-subtree-down (- (prefix-numeric-value arg))))
  7318. (defun org-move-subtree-down (&optional arg)
  7319. "Move the current subtree down past ARG headlines of the same level."
  7320. (interactive "p")
  7321. (setq arg (prefix-numeric-value arg))
  7322. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  7323. 'org-get-last-sibling))
  7324. (ins-point (make-marker))
  7325. (cnt (abs arg))
  7326. (col (current-column))
  7327. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  7328. ;; Select the tree
  7329. (org-back-to-heading)
  7330. (setq beg0 (point))
  7331. (save-excursion
  7332. (setq ne-beg (org-back-over-empty-lines))
  7333. (setq beg (point)))
  7334. (save-match-data
  7335. (save-excursion (outline-end-of-heading)
  7336. (setq folded (outline-invisible-p)))
  7337. (progn (org-end-of-subtree nil t)
  7338. (unless (eobp) (backward-char))))
  7339. (outline-next-heading)
  7340. (setq ne-end (org-back-over-empty-lines))
  7341. (setq end (point))
  7342. (goto-char beg0)
  7343. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  7344. ;; include less whitespace
  7345. (save-excursion
  7346. (goto-char beg)
  7347. (forward-line (- ne-beg ne-end))
  7348. (setq beg (point))))
  7349. ;; Find insertion point, with error handling
  7350. (while (> cnt 0)
  7351. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  7352. (progn (goto-char beg0)
  7353. (user-error "Cannot move past superior level or buffer limit")))
  7354. (setq cnt (1- cnt)))
  7355. (if (> arg 0)
  7356. ;; Moving forward - still need to move over subtree
  7357. (progn (org-end-of-subtree t t)
  7358. (save-excursion
  7359. (org-back-over-empty-lines)
  7360. (or (bolp) (newline)))))
  7361. (setq ne-ins (org-back-over-empty-lines))
  7362. (move-marker ins-point (point))
  7363. (setq txt (buffer-substring beg end))
  7364. (org-save-markers-in-region beg end)
  7365. (delete-region beg end)
  7366. (org-remove-empty-overlays-at beg)
  7367. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  7368. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  7369. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  7370. (let ((bbb (point)))
  7371. (insert-before-markers txt)
  7372. (org-reinstall-markers-in-region bbb)
  7373. (move-marker ins-point bbb))
  7374. (or (bolp) (insert "\n"))
  7375. (setq ins-end (point))
  7376. (goto-char ins-point)
  7377. (org-skip-whitespace)
  7378. (when (and (< arg 0)
  7379. (org-first-sibling-p)
  7380. (> ne-ins ne-beg))
  7381. ;; Move whitespace back to beginning
  7382. (save-excursion
  7383. (goto-char ins-end)
  7384. (let ((kill-whole-line t))
  7385. (kill-line (- ne-ins ne-beg)) (point)))
  7386. (insert (make-string (- ne-ins ne-beg) ?\n)))
  7387. (move-marker ins-point nil)
  7388. (if folded
  7389. (hide-subtree)
  7390. (org-show-entry)
  7391. (show-children)
  7392. (org-cycle-hide-drawers 'children))
  7393. (org-clean-visibility-after-subtree-move)
  7394. ;; move back to the initial column we were at
  7395. (move-to-column col)))
  7396. (defvar org-subtree-clip ""
  7397. "Clipboard for cut and paste of subtrees.
  7398. This is actually only a copy of the kill, because we use the normal kill
  7399. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  7400. (defvar org-subtree-clip-folded nil
  7401. "Was the last copied subtree folded?
  7402. This is used to fold the tree back after pasting.")
  7403. (defun org-cut-subtree (&optional n)
  7404. "Cut the current subtree into the clipboard.
  7405. With prefix arg N, cut this many sequential subtrees.
  7406. This is a short-hand for marking the subtree and then cutting it."
  7407. (interactive "p")
  7408. (org-copy-subtree n 'cut))
  7409. (defun org-copy-subtree (&optional n cut force-store-markers nosubtrees)
  7410. "Copy the current subtree it in the clipboard.
  7411. With prefix arg N, copy this many sequential subtrees.
  7412. This is a short-hand for marking the subtree and then copying it.
  7413. If CUT is non-nil, actually cut the subtree.
  7414. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  7415. of some markers in the region, even if CUT is non-nil. This is
  7416. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  7417. (interactive "p")
  7418. (let (beg end folded (beg0 (point)))
  7419. (if (org-called-interactively-p 'any)
  7420. (org-back-to-heading nil) ; take what looks like a subtree
  7421. (org-back-to-heading t)) ; take what is really there
  7422. (setq beg (point))
  7423. (skip-chars-forward " \t\r\n")
  7424. (save-match-data
  7425. (if nosubtrees
  7426. (outline-next-heading)
  7427. (save-excursion (outline-end-of-heading)
  7428. (setq folded (outline-invisible-p)))
  7429. (condition-case nil
  7430. (org-forward-heading-same-level (1- n) t)
  7431. (error nil))
  7432. (org-end-of-subtree t t)))
  7433. (setq end (point))
  7434. (goto-char beg0)
  7435. (when (> end beg)
  7436. (setq org-subtree-clip-folded folded)
  7437. (when (or cut force-store-markers)
  7438. (org-save-markers-in-region beg end))
  7439. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  7440. (setq org-subtree-clip (current-kill 0))
  7441. (message "%s: Subtree(s) with %d characters"
  7442. (if cut "Cut" "Copied")
  7443. (length org-subtree-clip)))))
  7444. (defun org-paste-subtree (&optional level tree for-yank)
  7445. "Paste the clipboard as a subtree, with modification of headline level.
  7446. The entire subtree is promoted or demoted in order to match a new headline
  7447. level.
  7448. If the cursor is at the beginning of a headline, the same level as
  7449. that headline is used to paste the tree.
  7450. If not, the new level is derived from the *visible* headings
  7451. before and after the insertion point, and taken to be the inferior headline
  7452. level of the two. So if the previous visible heading is level 3 and the
  7453. next is level 4 (or vice versa), level 4 will be used for insertion.
  7454. This makes sure that the subtree remains an independent subtree and does
  7455. not swallow low level entries.
  7456. You can also force a different level, either by using a numeric prefix
  7457. argument, or by inserting the heading marker by hand. For example, if the
  7458. cursor is after \"*****\", then the tree will be shifted to level 5.
  7459. If optional TREE is given, use this text instead of the kill ring.
  7460. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  7461. move back over whitespace before inserting, and move point to the end of
  7462. the inserted text when done."
  7463. (interactive "P")
  7464. (setq tree (or tree (and kill-ring (current-kill 0))))
  7465. (unless (org-kill-is-subtree-p tree)
  7466. (user-error "%s"
  7467. (substitute-command-keys
  7468. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  7469. (org-with-limited-levels
  7470. (let* ((visp (not (outline-invisible-p)))
  7471. (txt tree)
  7472. (^re_ "\\(\\*+\\)[ \t]*")
  7473. (old-level (if (string-match org-outline-regexp-bol txt)
  7474. (- (match-end 0) (match-beginning 0) 1)
  7475. -1))
  7476. (force-level (cond (level (prefix-numeric-value level))
  7477. ((and (looking-at "[ \t]*$")
  7478. (string-match
  7479. "^\\*+$" (buffer-substring
  7480. (point-at-bol) (point))))
  7481. (- (match-end 0) (match-beginning 0)))
  7482. ((and (bolp)
  7483. (looking-at org-outline-regexp))
  7484. (- (match-end 0) (point) 1))))
  7485. (previous-level (save-excursion
  7486. (condition-case nil
  7487. (progn
  7488. (outline-previous-visible-heading 1)
  7489. (if (looking-at ^re_)
  7490. (- (match-end 0) (match-beginning 0) 1)
  7491. 1))
  7492. (error 1))))
  7493. (next-level (save-excursion
  7494. (condition-case nil
  7495. (progn
  7496. (or (looking-at org-outline-regexp)
  7497. (outline-next-visible-heading 1))
  7498. (if (looking-at ^re_)
  7499. (- (match-end 0) (match-beginning 0) 1)
  7500. 1))
  7501. (error 1))))
  7502. (new-level (or force-level (max previous-level next-level)))
  7503. (shift (if (or (= old-level -1)
  7504. (= new-level -1)
  7505. (= old-level new-level))
  7506. 0
  7507. (- new-level old-level)))
  7508. (delta (if (> shift 0) -1 1))
  7509. (func (if (> shift 0) 'org-demote 'org-promote))
  7510. (org-odd-levels-only nil)
  7511. beg end newend)
  7512. ;; Remove the forced level indicator
  7513. (if force-level
  7514. (delete-region (point-at-bol) (point)))
  7515. ;; Paste
  7516. (beginning-of-line (if (bolp) 1 2))
  7517. (setq beg (point))
  7518. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  7519. (insert-before-markers txt)
  7520. (unless (string-match "\n\\'" txt) (insert "\n"))
  7521. (setq newend (point))
  7522. (org-reinstall-markers-in-region beg)
  7523. (setq end (point))
  7524. (goto-char beg)
  7525. (skip-chars-forward " \t\n\r")
  7526. (setq beg (point))
  7527. (if (and (outline-invisible-p) visp)
  7528. (save-excursion (outline-show-heading)))
  7529. ;; Shift if necessary
  7530. (unless (= shift 0)
  7531. (save-restriction
  7532. (narrow-to-region beg end)
  7533. (while (not (= shift 0))
  7534. (org-map-region func (point-min) (point-max))
  7535. (setq shift (+ delta shift)))
  7536. (goto-char (point-min))
  7537. (setq newend (point-max))))
  7538. (when (or (org-called-interactively-p 'interactive) for-yank)
  7539. (message "Clipboard pasted as level %d subtree" new-level))
  7540. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  7541. kill-ring
  7542. (eq org-subtree-clip (current-kill 0))
  7543. org-subtree-clip-folded)
  7544. ;; The tree was folded before it was killed/copied
  7545. (hide-subtree))
  7546. (and for-yank (goto-char newend)))))
  7547. (defun org-kill-is-subtree-p (&optional txt)
  7548. "Check if the current kill is an outline subtree, or a set of trees.
  7549. Returns nil if kill does not start with a headline, or if the first
  7550. headline level is not the largest headline level in the tree.
  7551. So this will actually accept several entries of equal levels as well,
  7552. which is OK for `org-paste-subtree'.
  7553. If optional TXT is given, check this string instead of the current kill."
  7554. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  7555. (re (org-get-limited-outline-regexp))
  7556. (^re (concat "^" re))
  7557. (start-level (and kill
  7558. (string-match
  7559. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7560. kill)
  7561. (- (match-end 2) (match-beginning 2) 1)))
  7562. (start (1+ (or (match-beginning 2) -1))))
  7563. (if (not start-level)
  7564. (progn
  7565. nil) ;; does not even start with a heading
  7566. (catch 'exit
  7567. (while (setq start (string-match ^re kill (1+ start)))
  7568. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7569. (throw 'exit nil)))
  7570. t))))
  7571. (defvar org-markers-to-move nil
  7572. "Markers that should be moved with a cut-and-paste operation.
  7573. Those markers are stored together with their positions relative to
  7574. the start of the region.")
  7575. (defun org-save-markers-in-region (beg end)
  7576. "Check markers in region.
  7577. If these markers are between BEG and END, record their position relative
  7578. to BEG, so that after moving the block of text, we can put the markers back
  7579. into place.
  7580. This function gets called just before an entry or tree gets cut from the
  7581. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7582. called immediately, to move the markers with the entries."
  7583. (setq org-markers-to-move nil)
  7584. (when (featurep 'org-clock)
  7585. (org-clock-save-markers-for-cut-and-paste beg end))
  7586. (when (featurep 'org-agenda)
  7587. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7588. (defun org-check-and-save-marker (marker beg end)
  7589. "Check if MARKER is between BEG and END.
  7590. If yes, remember the marker and the distance to BEG."
  7591. (when (and (marker-buffer marker)
  7592. (equal (marker-buffer marker) (current-buffer)))
  7593. (if (and (>= marker beg) (< marker end))
  7594. (push (cons marker (- marker beg)) org-markers-to-move))))
  7595. (defun org-reinstall-markers-in-region (beg)
  7596. "Move all remembered markers to their position relative to BEG."
  7597. (mapc (lambda (x)
  7598. (move-marker (car x) (+ beg (cdr x))))
  7599. org-markers-to-move)
  7600. (setq org-markers-to-move nil))
  7601. (defun org-narrow-to-subtree ()
  7602. "Narrow buffer to the current subtree."
  7603. (interactive)
  7604. (save-excursion
  7605. (save-match-data
  7606. (org-with-limited-levels
  7607. (narrow-to-region
  7608. (progn (org-back-to-heading t) (point))
  7609. (progn (org-end-of-subtree t t)
  7610. (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7611. (point)))))))
  7612. (defun org-narrow-to-block ()
  7613. "Narrow buffer to the current block."
  7614. (interactive)
  7615. (let* ((case-fold-search t)
  7616. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7617. "^[ \t]*#\\+end_.*")))
  7618. (if blockp
  7619. (narrow-to-region (car blockp) (cdr blockp))
  7620. (user-error "Not in a block"))))
  7621. (eval-when-compile
  7622. (defvar org-property-drawer-re))
  7623. (defvar org-property-start-re) ;; defined below
  7624. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7625. "Clone the task (subtree) at point N times.
  7626. The clones will be inserted as siblings.
  7627. In interactive use, the user will be prompted for the number of
  7628. clones to be produced. If the entry has a timestamp, the user
  7629. will also be prompted for a time shift, which may be a repeater
  7630. as used in time stamps, for example `+3d'. To disable this,
  7631. you can call the function with a universal prefix argument.
  7632. When a valid repeater is given and the entry contains any time
  7633. stamps, the clones will become a sequence in time, with time
  7634. stamps in the subtree shifted for each clone produced. If SHIFT
  7635. is nil or the empty string, time stamps will be left alone. The
  7636. ID property of the original subtree is removed.
  7637. If the original subtree did contain time stamps with a repeater,
  7638. the following will happen:
  7639. - the repeater will be removed in each clone
  7640. - an additional clone will be produced, with the current, unshifted
  7641. date(s) in the entry.
  7642. - the original entry will be placed *after* all the clones, with
  7643. repeater intact.
  7644. - the start days in the repeater in the original entry will be shifted
  7645. to past the last clone.
  7646. In this way you can spell out a number of instances of a repeating task,
  7647. and still retain the repeater to cover future instances of the task."
  7648. (interactive "nNumber of clones to produce: ")
  7649. (let ((shift
  7650. (or shift
  7651. (if (and (not (equal current-prefix-arg '(4)))
  7652. (save-excursion
  7653. (re-search-forward org-ts-regexp-both
  7654. (save-excursion
  7655. (org-end-of-subtree t)
  7656. (point)) t)))
  7657. (read-from-minibuffer
  7658. "Date shift per clone (e.g. +1w, empty to copy unchanged): ")
  7659. ""))) ;; No time shift
  7660. (n-no-remove -1)
  7661. (drawer-re org-drawer-regexp)
  7662. beg end template task idprop
  7663. shift-n shift-what doshift nmin nmax)
  7664. (if (not (and (integerp n) (> n 0)))
  7665. (error "Invalid number of replications %s" n))
  7666. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7667. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
  7668. shift)))
  7669. (error "Invalid shift specification %s" shift))
  7670. (when doshift
  7671. (setq shift-n (string-to-number (match-string 1 shift))
  7672. shift-what (cdr (assoc (match-string 2 shift)
  7673. '(("d" . day) ("w" . week)
  7674. ("m" . month) ("y" . year))))))
  7675. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7676. (setq nmin 1 nmax n)
  7677. (org-back-to-heading t)
  7678. (setq beg (point))
  7679. (setq idprop (org-entry-get nil "ID"))
  7680. (org-end-of-subtree t t)
  7681. (or (bolp) (insert "\n"))
  7682. (setq end (point))
  7683. (setq template (buffer-substring beg end))
  7684. (when (and doshift
  7685. (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
  7686. (delete-region beg end)
  7687. (setq end beg)
  7688. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7689. (goto-char end)
  7690. (loop for n from nmin to nmax do
  7691. ;; prepare clone
  7692. (with-temp-buffer
  7693. (insert template)
  7694. (org-mode)
  7695. (goto-char (point-min))
  7696. (org-show-subtree)
  7697. (and idprop (if org-clone-delete-id
  7698. (org-entry-delete nil "ID")
  7699. (org-id-get-create t)))
  7700. (unless (= n 0)
  7701. (while (re-search-forward "^[ \t]*CLOCK:.*$" nil t)
  7702. (kill-whole-line))
  7703. (goto-char (point-min))
  7704. (while (re-search-forward drawer-re nil t)
  7705. (mapc (lambda (d)
  7706. (org-remove-empty-drawer-at d (point))) org-drawers)))
  7707. (goto-char (point-min))
  7708. (when doshift
  7709. (while (re-search-forward org-ts-regexp-both nil t)
  7710. (org-timestamp-change (* n shift-n) shift-what))
  7711. (unless (= n n-no-remove)
  7712. (goto-char (point-min))
  7713. (while (re-search-forward org-ts-regexp nil t)
  7714. (save-excursion
  7715. (goto-char (match-beginning 0))
  7716. (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
  7717. (delete-region (match-beginning 1) (match-end 1)))))))
  7718. (setq task (buffer-string)))
  7719. (insert task))
  7720. (goto-char beg)))
  7721. ;;; Outline Sorting
  7722. (defun org-sort (with-case)
  7723. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7724. Optional argument WITH-CASE means sort case-sensitively."
  7725. (interactive "P")
  7726. (cond
  7727. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7728. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7729. (t
  7730. (org-call-with-arg 'org-sort-entries with-case))))
  7731. (defun org-sort-remove-invisible (s)
  7732. "Remove invisible links from string S."
  7733. (remove-text-properties 0 (length s) org-rm-props s)
  7734. (while (string-match org-bracket-link-regexp s)
  7735. (setq s (replace-match (if (match-end 2)
  7736. (match-string 3 s)
  7737. (match-string 1 s)) t t s)))
  7738. (let ((st (format " %s " s)))
  7739. (while (string-match org-emph-re st)
  7740. (setq st (replace-match (format " %s " (match-string 4 st)) t t st)))
  7741. (setq s (substring st 1 -1)))
  7742. s)
  7743. (defvar org-priority-regexp) ; defined later in the file
  7744. (defvar org-after-sorting-entries-or-items-hook nil
  7745. "Hook that is run after a bunch of entries or items have been sorted.
  7746. When children are sorted, the cursor is in the parent line when this
  7747. hook gets called. When a region or a plain list is sorted, the cursor
  7748. will be in the first entry of the sorted region/list.")
  7749. (defun org-sort-entries
  7750. (&optional with-case sorting-type getkey-func compare-func property)
  7751. "Sort entries on a certain level of an outline tree.
  7752. If there is an active region, the entries in the region are sorted.
  7753. Else, if the cursor is before the first entry, sort the top-level items.
  7754. Else, the children of the entry at point are sorted.
  7755. Sorting can be alphabetically, numerically, by date/time as given by
  7756. a time stamp, by a property, by priority order, or by a custom function.
  7757. The command prompts for the sorting type unless it has been given to the
  7758. function through the SORTING-TYPE argument, which needs to be a character,
  7759. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?o ?O ?r ?R ?f ?F). Here is the
  7760. precise meaning of each character:
  7761. n Numerically, by converting the beginning of the entry/item to a number.
  7762. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7763. o By order of TODO keywords.
  7764. t By date/time, either the first active time stamp in the entry, or, if
  7765. none exist, by the first inactive one.
  7766. s By the scheduled date/time.
  7767. d By deadline date/time.
  7768. c By creation time, which is assumed to be the first inactive time stamp
  7769. at the beginning of a line.
  7770. p By priority according to the cookie.
  7771. r By the value of a property.
  7772. Capital letters will reverse the sort order.
  7773. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7774. called with point at the beginning of the record. It must return either
  7775. a string or a number that should serve as the sorting key for that record.
  7776. Comparing entries ignores case by default. However, with an optional argument
  7777. WITH-CASE, the sorting considers case as well.
  7778. Sorting is done against the visible part of the headlines, it ignores hidden
  7779. links."
  7780. (interactive "P")
  7781. (let ((case-func (if with-case 'identity 'downcase))
  7782. (cmstr
  7783. ;; The clock marker is lost when using `sort-subr', let's
  7784. ;; store the clocking string.
  7785. (when (equal (marker-buffer org-clock-marker) (current-buffer))
  7786. (save-excursion
  7787. (goto-char org-clock-marker)
  7788. (buffer-substring-no-properties (line-beginning-position)
  7789. (point)))))
  7790. start beg end stars re re2
  7791. txt what tmp)
  7792. ;; Find beginning and end of region to sort
  7793. (cond
  7794. ((org-region-active-p)
  7795. ;; we will sort the region
  7796. (setq end (region-end)
  7797. what "region")
  7798. (goto-char (region-beginning))
  7799. (if (not (org-at-heading-p)) (outline-next-heading))
  7800. (setq start (point)))
  7801. ((or (org-at-heading-p)
  7802. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  7803. ;; we will sort the children of the current headline
  7804. (org-back-to-heading)
  7805. (setq start (point)
  7806. end (progn (org-end-of-subtree t t)
  7807. (or (bolp) (insert "\n"))
  7808. (when (>= (org-back-over-empty-lines) 1)
  7809. (forward-line 1))
  7810. (point))
  7811. what "children")
  7812. (goto-char start)
  7813. (show-subtree)
  7814. (outline-next-heading))
  7815. (t
  7816. ;; we will sort the top-level entries in this file
  7817. (goto-char (point-min))
  7818. (or (org-at-heading-p) (outline-next-heading))
  7819. (setq start (point))
  7820. (goto-char (point-max))
  7821. (beginning-of-line 1)
  7822. (when (looking-at ".*?\\S-")
  7823. ;; File ends in a non-white line
  7824. (end-of-line 1)
  7825. (insert "\n"))
  7826. (setq end (point-max))
  7827. (setq what "top-level")
  7828. (goto-char start)
  7829. (show-all)))
  7830. (setq beg (point))
  7831. (when (>= beg end) (goto-char start) (user-error "Nothing to sort"))
  7832. (looking-at "\\(\\*+\\)")
  7833. (setq stars (match-string 1)
  7834. re (concat "^" (regexp-quote stars) " +")
  7835. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  7836. txt (buffer-substring beg end))
  7837. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  7838. (if (and (not (equal stars "*")) (string-match re2 txt))
  7839. (user-error "Region to sort contains a level above the first entry"))
  7840. (unless sorting-type
  7841. (message
  7842. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  7843. [t]ime [s]cheduled [d]eadline [c]reated
  7844. A/N/P/R/O/F/T/S/D/C means reversed:"
  7845. what)
  7846. (setq sorting-type (read-char-exclusive))
  7847. (unless getkey-func
  7848. (and (= (downcase sorting-type) ?f)
  7849. (setq getkey-func
  7850. (org-icompleting-read "Sort using function: "
  7851. obarray 'fboundp t nil nil))
  7852. (setq getkey-func (intern getkey-func))))
  7853. (and (= (downcase sorting-type) ?r)
  7854. (not property)
  7855. (setq property
  7856. (org-icompleting-read "Property: "
  7857. (mapcar 'list (org-buffer-property-keys t))
  7858. nil t))))
  7859. (message "Sorting entries...")
  7860. (save-restriction
  7861. (narrow-to-region start end)
  7862. (let ((dcst (downcase sorting-type))
  7863. (case-fold-search nil)
  7864. (now (current-time)))
  7865. (sort-subr
  7866. (/= dcst sorting-type)
  7867. ;; This function moves to the beginning character of the "record" to
  7868. ;; be sorted.
  7869. (lambda nil
  7870. (if (re-search-forward re nil t)
  7871. (goto-char (match-beginning 0))
  7872. (goto-char (point-max))))
  7873. ;; This function moves to the last character of the "record" being
  7874. ;; sorted.
  7875. (lambda nil
  7876. (save-match-data
  7877. (condition-case nil
  7878. (outline-forward-same-level 1)
  7879. (error
  7880. (goto-char (point-max))))))
  7881. ;; This function returns the value that gets sorted against.
  7882. (lambda nil
  7883. (cond
  7884. ((= dcst ?n)
  7885. (if (looking-at org-complex-heading-regexp)
  7886. (string-to-number (org-sort-remove-invisible (match-string 4)))
  7887. nil))
  7888. ((= dcst ?a)
  7889. (if (looking-at org-complex-heading-regexp)
  7890. (funcall case-func (org-sort-remove-invisible (match-string 4)))
  7891. nil))
  7892. ((= dcst ?t)
  7893. (let ((end (save-excursion (outline-next-heading) (point))))
  7894. (if (or (re-search-forward org-ts-regexp end t)
  7895. (re-search-forward org-ts-regexp-both end t))
  7896. (org-time-string-to-seconds (match-string 0))
  7897. (org-float-time now))))
  7898. ((= dcst ?c)
  7899. (let ((end (save-excursion (outline-next-heading) (point))))
  7900. (if (re-search-forward
  7901. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  7902. end t)
  7903. (org-time-string-to-seconds (match-string 0))
  7904. (org-float-time now))))
  7905. ((= dcst ?s)
  7906. (let ((end (save-excursion (outline-next-heading) (point))))
  7907. (if (re-search-forward org-scheduled-time-regexp end t)
  7908. (org-time-string-to-seconds (match-string 1))
  7909. (org-float-time now))))
  7910. ((= dcst ?d)
  7911. (let ((end (save-excursion (outline-next-heading) (point))))
  7912. (if (re-search-forward org-deadline-time-regexp end t)
  7913. (org-time-string-to-seconds (match-string 1))
  7914. (org-float-time now))))
  7915. ((= dcst ?p)
  7916. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  7917. (string-to-char (match-string 2))
  7918. org-default-priority))
  7919. ((= dcst ?r)
  7920. (or (org-entry-get nil property) ""))
  7921. ((= dcst ?o)
  7922. (if (looking-at org-complex-heading-regexp)
  7923. (- 9999 (length (member (match-string 2)
  7924. org-todo-keywords-1)))))
  7925. ((= dcst ?f)
  7926. (if getkey-func
  7927. (progn
  7928. (setq tmp (funcall getkey-func))
  7929. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  7930. tmp)
  7931. (error "Invalid key function `%s'" getkey-func)))
  7932. (t (error "Invalid sorting type `%c'" sorting-type))))
  7933. nil
  7934. (cond
  7935. ((= dcst ?a) 'string<)
  7936. ((= dcst ?f) compare-func)
  7937. ((member dcst '(?p ?t ?s ?d ?c)) '<)))))
  7938. (run-hooks 'org-after-sorting-entries-or-items-hook)
  7939. ;; Reset the clock marker if needed
  7940. (when cmstr
  7941. (save-excursion
  7942. (goto-char start)
  7943. (search-forward cmstr nil t)
  7944. (move-marker org-clock-marker (point))))
  7945. (message "Sorting entries...done")))
  7946. (defun org-do-sort (table what &optional with-case sorting-type)
  7947. "Sort TABLE of WHAT according to SORTING-TYPE.
  7948. The user will be prompted for the SORTING-TYPE if the call to this
  7949. function does not specify it. WHAT is only for the prompt, to indicate
  7950. what is being sorted. The sorting key will be extracted from
  7951. the car of the elements of the table.
  7952. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  7953. (unless sorting-type
  7954. (message
  7955. "Sort %s: [a]lphabetic, [n]umeric, [t]ime. A/N/T means reversed:"
  7956. what)
  7957. (setq sorting-type (read-char-exclusive)))
  7958. (let ((dcst (downcase sorting-type))
  7959. extractfun comparefun)
  7960. ;; Define the appropriate functions
  7961. (cond
  7962. ((= dcst ?n)
  7963. (setq extractfun 'string-to-number
  7964. comparefun (if (= dcst sorting-type) '< '>)))
  7965. ((= dcst ?a)
  7966. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  7967. (lambda(x) (downcase (org-sort-remove-invisible x))))
  7968. comparefun (if (= dcst sorting-type)
  7969. 'string<
  7970. (lambda (a b) (and (not (string< a b))
  7971. (not (string= a b)))))))
  7972. ((= dcst ?t)
  7973. (setq extractfun
  7974. (lambda (x)
  7975. (if (or (string-match org-ts-regexp x)
  7976. (string-match org-ts-regexp-both x))
  7977. (org-float-time
  7978. (org-time-string-to-time (match-string 0 x)))
  7979. 0))
  7980. comparefun (if (= dcst sorting-type) '< '>)))
  7981. (t (error "Invalid sorting type `%c'" sorting-type)))
  7982. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  7983. table)
  7984. (lambda (a b) (funcall comparefun (car a) (car b))))))
  7985. ;;; The orgstruct minor mode
  7986. ;; Define a minor mode which can be used in other modes in order to
  7987. ;; integrate the org-mode structure editing commands.
  7988. ;; This is really a hack, because the org-mode structure commands use
  7989. ;; keys which normally belong to the major mode. Here is how it
  7990. ;; works: The minor mode defines all the keys necessary to operate the
  7991. ;; structure commands, but wraps the commands into a function which
  7992. ;; tests if the cursor is currently at a headline or a plain list
  7993. ;; item. If that is the case, the structure command is used,
  7994. ;; temporarily setting many Org-mode variables like regular
  7995. ;; expressions for filling etc. However, when any of those keys is
  7996. ;; used at a different location, function uses `key-binding' to look
  7997. ;; up if the key has an associated command in another currently active
  7998. ;; keymap (minor modes, major mode, global), and executes that
  7999. ;; command. There might be problems if any of the keys is otherwise
  8000. ;; used as a prefix key.
  8001. (defcustom orgstruct-heading-prefix-regexp ""
  8002. "Regexp that matches the custom prefix of Org headlines in
  8003. orgstruct(++)-mode."
  8004. :group 'org
  8005. :version "24.4"
  8006. :package-version '(Org . "8.3")
  8007. :type 'regexp)
  8008. ;;;###autoload(put 'orgstruct-heading-prefix-regexp 'safe-local-variable 'stringp)
  8009. (defcustom orgstruct-setup-hook nil
  8010. "Hook run after orgstruct-mode-map is filled."
  8011. :group 'org
  8012. :version "24.4"
  8013. :package-version '(Org . "8.0")
  8014. :type 'hook)
  8015. (defvar orgstruct-initialized nil)
  8016. (defvar org-local-vars nil
  8017. "List of local variables, for use by `orgstruct-mode'.")
  8018. ;;;###autoload
  8019. (define-minor-mode orgstruct-mode
  8020. "Toggle the minor mode `orgstruct-mode'.
  8021. This mode is for using Org-mode structure commands in other
  8022. modes. The following keys behave as if Org-mode were active, if
  8023. the cursor is on a headline, or on a plain list item (both as
  8024. defined by Org-mode)."
  8025. nil " OrgStruct" (make-sparse-keymap)
  8026. (funcall (if orgstruct-mode
  8027. 'add-to-invisibility-spec
  8028. 'remove-from-invisibility-spec)
  8029. '(outline . t))
  8030. (when orgstruct-mode
  8031. (org-load-modules-maybe)
  8032. (unless orgstruct-initialized
  8033. (orgstruct-setup)
  8034. (setq orgstruct-initialized t))))
  8035. ;;;###autoload
  8036. (defun turn-on-orgstruct ()
  8037. "Unconditionally turn on `orgstruct-mode'."
  8038. (orgstruct-mode 1))
  8039. (defvar org-fb-vars nil)
  8040. (make-variable-buffer-local 'org-fb-vars)
  8041. (defun orgstruct++-mode (&optional arg)
  8042. "Toggle `orgstruct-mode', the enhanced version of it.
  8043. In addition to setting orgstruct-mode, this also exports all
  8044. indentation and autofilling variables from org-mode into the
  8045. buffer. It will also recognize item context in multiline items."
  8046. (interactive "P")
  8047. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  8048. (if (< arg 1)
  8049. (progn (orgstruct-mode -1)
  8050. (mapc (lambda(v)
  8051. (org-set-local (car v)
  8052. (if (eq (car-safe (cadr v)) 'quote) (cadadr v) (cadr v))))
  8053. org-fb-vars))
  8054. (orgstruct-mode 1)
  8055. (setq org-fb-vars nil)
  8056. (unless org-local-vars
  8057. (setq org-local-vars (org-get-local-variables)))
  8058. (let (var val)
  8059. (mapc
  8060. (lambda (x)
  8061. (when (string-match
  8062. "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|fill-prefix\\|indent-\\)"
  8063. (symbol-name (car x)))
  8064. (setq var (car x) val (nth 1 x))
  8065. (push (list var `(quote ,(eval var))) org-fb-vars)
  8066. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  8067. org-local-vars)
  8068. (org-set-local 'orgstruct-is-++ t))))
  8069. (defvar orgstruct-is-++ nil
  8070. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  8071. (make-variable-buffer-local 'orgstruct-is-++)
  8072. ;;;###autoload
  8073. (defun turn-on-orgstruct++ ()
  8074. "Unconditionally turn on `orgstruct++-mode'."
  8075. (orgstruct++-mode 1))
  8076. (defun orgstruct-error ()
  8077. "Error when there is no default binding for a structure key."
  8078. (interactive)
  8079. (funcall (if (fboundp 'user-error)
  8080. 'user-error
  8081. 'error)
  8082. "This key has no function outside structure elements"))
  8083. (defun orgstruct-setup ()
  8084. "Setup orgstruct keymap."
  8085. (dolist (cell '((org-demote . t)
  8086. (org-metaleft . t)
  8087. (org-metaright . t)
  8088. (org-promote . t)
  8089. (org-shiftmetaleft . t)
  8090. (org-shiftmetaright . t)
  8091. org-backward-element
  8092. org-backward-heading-same-level
  8093. org-ctrl-c-ret
  8094. org-ctrl-c-minus
  8095. org-ctrl-c-star
  8096. org-cycle
  8097. org-forward-heading-same-level
  8098. org-insert-heading
  8099. org-insert-heading-respect-content
  8100. org-kill-note-or-show-branches
  8101. org-mark-subtree
  8102. org-meta-return
  8103. org-metadown
  8104. org-metaup
  8105. org-narrow-to-subtree
  8106. org-promote-subtree
  8107. org-reveal
  8108. org-shiftdown
  8109. org-shiftleft
  8110. org-shiftmetadown
  8111. org-shiftmetaup
  8112. org-shiftright
  8113. org-shifttab
  8114. org-shifttab
  8115. org-shiftup
  8116. org-show-subtree
  8117. org-sort
  8118. org-up-element
  8119. outline-demote
  8120. outline-next-visible-heading
  8121. outline-previous-visible-heading
  8122. outline-promote
  8123. outline-up-heading
  8124. show-children))
  8125. (let ((f (or (car-safe cell) cell))
  8126. (disable-when-heading-prefix (cdr-safe cell)))
  8127. (when (fboundp f)
  8128. (let ((new-bindings))
  8129. (dolist (binding (nconc (where-is-internal f org-mode-map)
  8130. (where-is-internal f outline-mode-map)))
  8131. (push binding new-bindings)
  8132. ;; TODO use local-function-key-map
  8133. (dolist (rep '(("<tab>" . "TAB")
  8134. ("<return>" . "RET")
  8135. ("<escape>" . "ESC")
  8136. ("<delete>" . "DEL")))
  8137. (setq binding (read-kbd-macro
  8138. (let ((case-fold-search))
  8139. (replace-regexp-in-string
  8140. (regexp-quote (cdr rep))
  8141. (car rep)
  8142. (key-description binding)))))
  8143. (pushnew binding new-bindings :test 'equal)))
  8144. (dolist (binding new-bindings)
  8145. (let ((key (lookup-key orgstruct-mode-map binding)))
  8146. (when (or (not key) (numberp key))
  8147. (condition-case nil
  8148. (org-defkey orgstruct-mode-map
  8149. binding
  8150. (orgstruct-make-binding f binding disable-when-heading-prefix))
  8151. (error nil)))))))))
  8152. (run-hooks 'orgstruct-setup-hook))
  8153. (defun orgstruct-make-binding (fun key disable-when-heading-prefix)
  8154. "Create a function for binding in the structure minor mode.
  8155. FUN is the command to call inside a table. KEY is the key that
  8156. should be checked in for a command to execute outside of tables.
  8157. Non-nil `disable-when-heading-prefix' means to disable the command
  8158. if `orgstruct-heading-prefix-regexp' is not empty."
  8159. (let ((name (concat "orgstruct-hijacker-" (symbol-name fun))))
  8160. (let ((nname name)
  8161. (i 0))
  8162. (while (fboundp (intern nname))
  8163. (setq nname (format "%s-%d" name (setq i (1+ i)))))
  8164. (setq name (intern nname)))
  8165. (eval
  8166. (let ((bindings '((org-heading-regexp
  8167. (concat "^"
  8168. orgstruct-heading-prefix-regexp
  8169. "\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ ]*$"))
  8170. (org-outline-regexp
  8171. (concat orgstruct-heading-prefix-regexp "\\*+ "))
  8172. (org-outline-regexp-bol
  8173. (concat "^" org-outline-regexp))
  8174. (outline-regexp org-outline-regexp)
  8175. (outline-heading-end-regexp "\n")
  8176. (outline-level 'org-outline-level)
  8177. (outline-heading-alist))))
  8178. `(defun ,name (arg)
  8179. ,(concat "In Structure, run `" (symbol-name fun) "'.\n"
  8180. "Outside of structure, run the binding of `"
  8181. (key-description key) "'."
  8182. (when disable-when-heading-prefix
  8183. (concat
  8184. "\nIf `orgstruct-heading-prefix-regexp' is not empty, this command will always fall\n"
  8185. "back to the default binding due to limitations of Org's implementation of\n"
  8186. "`" (symbol-name fun) "'.")))
  8187. (interactive "p")
  8188. (let* ((disable
  8189. ,(and disable-when-heading-prefix
  8190. '(not (string= orgstruct-heading-prefix-regexp ""))))
  8191. (fallback
  8192. (or disable
  8193. (not
  8194. (let* ,bindings
  8195. (org-context-p 'headline 'item
  8196. ,(when (memq fun
  8197. '(org-insert-heading
  8198. org-insert-heading-respect-content
  8199. org-meta-return))
  8200. '(when orgstruct-is-++
  8201. 'item-body))))))))
  8202. (if fallback
  8203. (let* ((orgstruct-mode)
  8204. (binding
  8205. (let ((key ,key))
  8206. (catch 'exit
  8207. (dolist
  8208. (rep
  8209. '(nil
  8210. ("<\\([^>]*\\)tab>" . "\\1TAB")
  8211. ("<\\([^>]*\\)return>" . "\\1RET")
  8212. ("<\\([^>]*\\)escape>" . "\\1ESC")
  8213. ("<\\([^>]*\\)delete>" . "\\1DEL"))
  8214. nil)
  8215. (when rep
  8216. (setq key (read-kbd-macro
  8217. (let ((case-fold-search))
  8218. (replace-regexp-in-string
  8219. (car rep)
  8220. (cdr rep)
  8221. (key-description key))))))
  8222. (when (key-binding key)
  8223. (throw 'exit (key-binding key))))))))
  8224. (if (keymapp binding)
  8225. (org-set-transient-map binding)
  8226. (let ((func (or binding
  8227. (unless disable
  8228. 'orgstruct-error))))
  8229. (when func
  8230. (call-interactively func)))))
  8231. (org-run-like-in-org-mode
  8232. (lambda ()
  8233. (interactive)
  8234. (let* ,bindings
  8235. (call-interactively ',fun)))))))))
  8236. name))
  8237. (defun org-contextualize-keys (alist contexts)
  8238. "Return valid elements in ALIST depending on CONTEXTS.
  8239. `org-agenda-custom-commands' or `org-capture-templates' are the
  8240. values used for ALIST, and `org-agenda-custom-commands-contexts'
  8241. or `org-capture-templates-contexts' are the associated contexts
  8242. definitions."
  8243. (let ((contexts
  8244. ;; normalize contexts
  8245. (mapcar
  8246. (lambda(c) (cond ((listp (cadr c))
  8247. (list (car c) (car c) (cadr c)))
  8248. ((string= "" (cadr c))
  8249. (list (car c) (car c) (caddr c)))
  8250. (t c))) contexts))
  8251. (a alist) c r s)
  8252. ;; loop over all commands or templates
  8253. (while (setq c (pop a))
  8254. (let (vrules repl)
  8255. (cond
  8256. ((not (assoc (car c) contexts))
  8257. (push c r))
  8258. ((and (assoc (car c) contexts)
  8259. (setq vrules (org-contextualize-validate-key
  8260. (car c) contexts)))
  8261. (mapc (lambda (vr)
  8262. (when (not (equal (car vr) (cadr vr)))
  8263. (setq repl vr))) vrules)
  8264. (if (not repl) (push c r)
  8265. (push (cadr repl) s)
  8266. (push
  8267. (cons (car c)
  8268. (cdr (or (assoc (cadr repl) alist)
  8269. (error "Undefined key `%s' as contextual replacement for `%s'"
  8270. (cadr repl) (car c)))))
  8271. r))))))
  8272. ;; Return limited ALIST, possibly with keys modified, and deduplicated
  8273. (delq
  8274. nil
  8275. (delete-dups
  8276. (mapcar (lambda (x)
  8277. (let ((tpl (car x)))
  8278. (when (not (delq
  8279. nil
  8280. (mapcar (lambda(y)
  8281. (equal y tpl)) s))) x)))
  8282. (reverse r))))))
  8283. (defun org-contextualize-validate-key (key contexts)
  8284. "Check CONTEXTS for agenda or capture KEY."
  8285. (let (r rr res)
  8286. (while (setq r (pop contexts))
  8287. (mapc
  8288. (lambda (rr)
  8289. (when
  8290. (and (equal key (car r))
  8291. (if (functionp rr) (funcall rr)
  8292. (or (and (eq (car rr) 'in-file)
  8293. (buffer-file-name)
  8294. (string-match (cdr rr) (buffer-file-name)))
  8295. (and (eq (car rr) 'in-mode)
  8296. (string-match (cdr rr) (symbol-name major-mode)))
  8297. (and (eq (car rr) 'in-buffer)
  8298. (string-match (cdr rr) (buffer-name)))
  8299. (when (and (eq (car rr) 'not-in-file)
  8300. (buffer-file-name))
  8301. (not (string-match (cdr rr) (buffer-file-name))))
  8302. (when (eq (car rr) 'not-in-mode)
  8303. (not (string-match (cdr rr) (symbol-name major-mode))))
  8304. (when (eq (car rr) 'not-in-buffer)
  8305. (not (string-match (cdr rr) (buffer-name)))))))
  8306. (push r res)))
  8307. (car (last r))))
  8308. (delete-dups (delq nil res))))
  8309. (defun org-context-p (&rest contexts)
  8310. "Check if local context is any of CONTEXTS.
  8311. Possible values in the list of contexts are `table', `headline', and `item'."
  8312. (let ((pos (point)))
  8313. (goto-char (point-at-bol))
  8314. (prog1 (or (and (memq 'table contexts)
  8315. (looking-at "[ \t]*|"))
  8316. (and (memq 'headline contexts)
  8317. (looking-at org-outline-regexp))
  8318. (and (memq 'item contexts)
  8319. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  8320. (and (memq 'item-body contexts)
  8321. (org-in-item-p)))
  8322. (goto-char pos))))
  8323. (defun org-get-local-variables ()
  8324. "Return a list of all local variables in an Org mode buffer."
  8325. (let (varlist)
  8326. (with-current-buffer (get-buffer-create "*Org tmp*")
  8327. (erase-buffer)
  8328. (org-mode)
  8329. (setq varlist (buffer-local-variables)))
  8330. (kill-buffer "*Org tmp*")
  8331. (delq nil
  8332. (mapcar
  8333. (lambda (x)
  8334. (setq x
  8335. (if (symbolp x)
  8336. (list x)
  8337. (list (car x) (cdr x))))
  8338. (if (and (not (get (car x) 'org-state))
  8339. (string-match
  8340. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
  8341. (symbol-name (car x))))
  8342. x nil))
  8343. varlist))))
  8344. (defun org-clone-local-variables (from-buffer &optional regexp)
  8345. "Clone local variables from FROM-BUFFER.
  8346. Optional argument REGEXP selects variables to clone."
  8347. (mapc
  8348. (lambda (pair)
  8349. (and (symbolp (car pair))
  8350. (or (null regexp)
  8351. (string-match regexp (symbol-name (car pair))))
  8352. (set (make-local-variable (car pair))
  8353. (cdr pair))))
  8354. (buffer-local-variables from-buffer)))
  8355. ;;;###autoload
  8356. (defun org-run-like-in-org-mode (cmd)
  8357. "Run a command, pretending that the current buffer is in Org-mode.
  8358. This will temporarily bind local variables that are typically bound in
  8359. Org-mode to the values they have in Org-mode, and then interactively
  8360. call CMD."
  8361. (org-load-modules-maybe)
  8362. (unless org-local-vars
  8363. (setq org-local-vars (org-get-local-variables)))
  8364. (let (binds)
  8365. (dolist (var org-local-vars)
  8366. (when (or (not (boundp (car var)))
  8367. (eq (symbol-value (car var))
  8368. (default-value (car var))))
  8369. (push (list (car var) `(quote ,(cadr var))) binds)))
  8370. (eval `(let ,binds
  8371. (call-interactively (quote ,cmd))))))
  8372. ;;;; Archiving
  8373. (defun org-get-category (&optional pos force-refresh)
  8374. "Get the category applying to position POS."
  8375. (save-match-data
  8376. (if force-refresh (org-refresh-category-properties))
  8377. (let ((pos (or pos (point))))
  8378. (or (get-text-property pos 'org-category)
  8379. (progn (org-refresh-category-properties)
  8380. (get-text-property pos 'org-category))))))
  8381. (defun org-refresh-category-properties ()
  8382. "Refresh category text properties in the buffer."
  8383. (let ((case-fold-search t)
  8384. (inhibit-read-only t)
  8385. (def-cat (cond
  8386. ((null org-category)
  8387. (if buffer-file-name
  8388. (file-name-sans-extension
  8389. (file-name-nondirectory buffer-file-name))
  8390. "???"))
  8391. ((symbolp org-category) (symbol-name org-category))
  8392. (t org-category)))
  8393. beg end cat pos optionp)
  8394. (org-with-silent-modifications
  8395. (save-excursion
  8396. (save-restriction
  8397. (widen)
  8398. (goto-char (point-min))
  8399. (put-text-property (point) (point-max) 'org-category def-cat)
  8400. (while (re-search-forward
  8401. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  8402. (setq pos (match-end 0)
  8403. optionp (equal (char-after (match-beginning 0)) ?#)
  8404. cat (org-trim (match-string 2)))
  8405. (if optionp
  8406. (setq beg (point-at-bol) end (point-max))
  8407. (org-back-to-heading t)
  8408. (setq beg (point) end (org-end-of-subtree t t)))
  8409. (put-text-property beg end 'org-category cat)
  8410. (put-text-property beg end 'org-category-position beg)
  8411. (goto-char pos)))))))
  8412. (defun org-refresh-properties (dprop tprop)
  8413. "Refresh buffer text properties.
  8414. DPROP is the drawer property and TPROP is the corresponding text
  8415. property to set."
  8416. (let ((case-fold-search t)
  8417. (inhibit-read-only t) p)
  8418. (org-with-silent-modifications
  8419. (save-excursion
  8420. (save-restriction
  8421. (widen)
  8422. (goto-char (point-min))
  8423. (while (re-search-forward (concat "^[ \t]*:" dprop ": +\\(.*\\)[ \t]*$") nil t)
  8424. (setq p (org-match-string-no-properties 1))
  8425. (save-excursion
  8426. (org-back-to-heading t)
  8427. (put-text-property
  8428. (point-at-bol) (or (outline-next-heading) (point-max)) tprop p))))))))
  8429. ;;;; Link Stuff
  8430. ;;; Link abbreviations
  8431. (defun org-link-expand-abbrev (link)
  8432. "Apply replacements as defined in `org-link-abbrev-alist'."
  8433. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  8434. (let* ((key (match-string 1 link))
  8435. (as (or (assoc key org-link-abbrev-alist-local)
  8436. (assoc key org-link-abbrev-alist)))
  8437. (tag (and (match-end 2) (match-string 3 link)))
  8438. rpl)
  8439. (if (not as)
  8440. link
  8441. (setq rpl (cdr as))
  8442. (cond
  8443. ((symbolp rpl) (funcall rpl tag))
  8444. ((string-match "%(\\([^)]+\\))" rpl)
  8445. (replace-match
  8446. (save-match-data
  8447. (funcall (intern-soft (match-string 1 rpl)) tag)) t t rpl))
  8448. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  8449. ((string-match "%h" rpl)
  8450. (replace-match (url-hexify-string (or tag "")) t t rpl))
  8451. (t (concat rpl tag)))))
  8452. link))
  8453. ;;; Storing and inserting links
  8454. (defvar org-insert-link-history nil
  8455. "Minibuffer history for links inserted with `org-insert-link'.")
  8456. (defvar org-stored-links nil
  8457. "Contains the links stored with `org-store-link'.")
  8458. (defvar org-store-link-plist nil
  8459. "Plist with info about the most recently link created with `org-store-link'.")
  8460. (defvar org-link-protocols nil
  8461. "Link protocols added to Org-mode using `org-add-link-type'.")
  8462. (defvar org-store-link-functions nil
  8463. "List of functions that are called to create and store a link.
  8464. Each function will be called in turn until one returns a non-nil
  8465. value. Each function should check if it is responsible for creating
  8466. this link (for example by looking at the major mode).
  8467. If not, it must exit and return nil.
  8468. If yes, it should return a non-nil value after a calling
  8469. `org-store-link-props' with a list of properties and values.
  8470. Special properties are:
  8471. :type The link prefix, like \"http\". This must be given.
  8472. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  8473. This is obligatory as well.
  8474. :description Optional default description for the second pair
  8475. of brackets in an Org-mode link. The user can still change
  8476. this when inserting this link into an Org-mode buffer.
  8477. In addition to these, any additional properties can be specified
  8478. and then used in capture templates.")
  8479. (defun org-add-link-type (type &optional follow export)
  8480. "Add TYPE to the list of `org-link-types'.
  8481. Re-compute all regular expressions depending on `org-link-types'
  8482. FOLLOW and EXPORT are two functions.
  8483. FOLLOW should take the link path as the single argument and do whatever
  8484. is necessary to follow the link, for example find a file or display
  8485. a mail message.
  8486. EXPORT should format the link path for export to one of the export formats.
  8487. It should be a function accepting three arguments:
  8488. path the path of the link, the text after the prefix (like \"http:\")
  8489. desc the description of the link, if any, or a description added by
  8490. org-export-normalize-links if there is none
  8491. format the export format, a symbol like `html' or `latex' or `ascii'..
  8492. The function may use the FORMAT information to return different values
  8493. depending on the format. The return value will be put literally into
  8494. the exported file. If the return value is nil, this means Org should
  8495. do what it normally does with links which do not have EXPORT defined.
  8496. Org-mode has a built-in default for exporting links. If you are happy with
  8497. this default, there is no need to define an export function for the link
  8498. type. For a simple example of an export function, see `org-bbdb.el'."
  8499. (add-to-list 'org-link-types type t)
  8500. (org-make-link-regexps)
  8501. (if (assoc type org-link-protocols)
  8502. (setcdr (assoc type org-link-protocols) (list follow export))
  8503. (push (list type follow export) org-link-protocols)))
  8504. (defvar org-agenda-buffer-name) ; Defined in org-agenda.el
  8505. (defvar org-id-link-to-org-use-id) ; Defined in org-id.el
  8506. ;;;###autoload
  8507. (defun org-store-link (arg)
  8508. "\\<org-mode-map>Store an org-link to the current location.
  8509. This link is added to `org-stored-links' and can later be inserted
  8510. into an org-buffer with \\[org-insert-link].
  8511. For some link types, a prefix arg is interpreted.
  8512. For links to Usenet articles, arg negates `org-gnus-prefer-web-links'.
  8513. For file links, arg negates `org-context-in-file-links'.
  8514. A double prefix arg force skipping storing functions that are not
  8515. part of Org's core.
  8516. A triple prefix arg force storing a link for each line in the
  8517. active region."
  8518. (interactive "P")
  8519. (org-load-modules-maybe)
  8520. (if (and (equal arg '(64)) (org-region-active-p))
  8521. (save-excursion
  8522. (let ((end (region-end)))
  8523. (goto-char (region-beginning))
  8524. (set-mark (point))
  8525. (while (< (point-at-eol) end)
  8526. (move-end-of-line 1) (activate-mark)
  8527. (let (current-prefix-arg)
  8528. (call-interactively 'org-store-link))
  8529. (move-beginning-of-line 2)
  8530. (set-mark (point)))))
  8531. (org-with-limited-levels
  8532. (setq org-store-link-plist nil)
  8533. (let (link cpltxt desc description search
  8534. txt custom-id agenda-link sfuns sfunsn)
  8535. (cond
  8536. ;; Store a link using an external link type
  8537. ((and (not (equal arg '(16)))
  8538. (setq sfuns
  8539. (delq
  8540. nil (mapcar (lambda (f)
  8541. (let (fs) (if (funcall f) (push f fs))))
  8542. org-store-link-functions))
  8543. sfunsn (mapcar (lambda (fu) (symbol-name (car fu))) sfuns))
  8544. (or (and (cdr sfuns)
  8545. (funcall (intern
  8546. (completing-read
  8547. "Which function for creating the link? "
  8548. sfunsn nil t (car sfunsn)))))
  8549. (funcall (caar sfuns)))
  8550. (setq link (plist-get org-store-link-plist :link)
  8551. desc (or (plist-get org-store-link-plist
  8552. :description) link))))
  8553. ;; Store a link from a source code buffer
  8554. ((org-src-edit-buffer-p)
  8555. (let (label gc)
  8556. (while (or (not label)
  8557. (save-excursion
  8558. (save-restriction
  8559. (widen)
  8560. (goto-char (point-min))
  8561. (re-search-forward
  8562. (regexp-quote (format org-coderef-label-format label))
  8563. nil t))))
  8564. (when label (message "Label exists already") (sit-for 2))
  8565. (setq label (read-string "Code line label: " label)))
  8566. (end-of-line 1)
  8567. (setq link (format org-coderef-label-format label))
  8568. (setq gc (- 79 (length link)))
  8569. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  8570. (insert link)
  8571. (setq link (concat "(" label ")") desc nil)))
  8572. ;; We are in the agenda, link to referenced location
  8573. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  8574. (let ((m (or (get-text-property (point) 'org-hd-marker)
  8575. (get-text-property (point) 'org-marker))))
  8576. (when m
  8577. (org-with-point-at m
  8578. (setq agenda-link
  8579. (if (org-called-interactively-p 'any)
  8580. (call-interactively 'org-store-link)
  8581. (org-store-link nil)))))))
  8582. ((eq major-mode 'calendar-mode)
  8583. (let ((cd (calendar-cursor-to-date)))
  8584. (setq link
  8585. (format-time-string
  8586. (car org-time-stamp-formats)
  8587. (apply 'encode-time
  8588. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  8589. nil nil nil))))
  8590. (org-store-link-props :type "calendar" :date cd)))
  8591. ((eq major-mode 'help-mode)
  8592. (setq link (concat "help:" (save-excursion
  8593. (goto-char (point-min))
  8594. (looking-at "^[^ ]+")
  8595. (match-string 0))))
  8596. (org-store-link-props :type "help"))
  8597. ((eq major-mode 'w3-mode)
  8598. (setq cpltxt (if (and (buffer-name)
  8599. (not (string-match "Untitled" (buffer-name))))
  8600. (buffer-name)
  8601. (url-view-url t))
  8602. link (url-view-url t))
  8603. (org-store-link-props :type "w3" :url (url-view-url t)))
  8604. ((eq major-mode 'image-mode)
  8605. (setq cpltxt (concat "file:"
  8606. (abbreviate-file-name buffer-file-name))
  8607. link cpltxt)
  8608. (org-store-link-props :type "image" :file buffer-file-name))
  8609. ;; In dired, store a link to the file of the current line
  8610. ((eq major-mode 'dired-mode)
  8611. (let ((file (dired-get-filename nil t)))
  8612. (setq file (if file
  8613. (abbreviate-file-name
  8614. (expand-file-name (dired-get-filename nil t)))
  8615. ;; otherwise, no file so use current directory.
  8616. default-directory))
  8617. (setq cpltxt (concat "file:" file)
  8618. link cpltxt)))
  8619. ((setq search (run-hook-with-args-until-success
  8620. 'org-create-file-search-functions))
  8621. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  8622. "::" search))
  8623. (setq cpltxt (or description link)))
  8624. ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
  8625. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  8626. (cond
  8627. ;; Store a link using the target at point
  8628. ((org-in-regexp "[^<]<<\\([^<>]+\\)>>[^>]" 1)
  8629. (setq cpltxt
  8630. (concat "file:"
  8631. (abbreviate-file-name
  8632. (buffer-file-name (buffer-base-buffer)))
  8633. "::" (match-string 1))
  8634. link cpltxt))
  8635. ((and (featurep 'org-id)
  8636. (or (eq org-id-link-to-org-use-id t)
  8637. (and (org-called-interactively-p 'any)
  8638. (or (eq org-id-link-to-org-use-id 'create-if-interactive)
  8639. (and (eq org-id-link-to-org-use-id
  8640. 'create-if-interactive-and-no-custom-id)
  8641. (not custom-id))))
  8642. (and org-id-link-to-org-use-id (org-entry-get nil "ID"))))
  8643. ;; Store a link using the ID at point
  8644. (setq link (condition-case nil
  8645. (prog1 (org-id-store-link)
  8646. (setq desc (or (plist-get org-store-link-plist
  8647. :description)
  8648. "")))
  8649. (error
  8650. ;; Probably before first headline, link only to file
  8651. (concat "file:"
  8652. (abbreviate-file-name
  8653. (buffer-file-name (buffer-base-buffer))))))))
  8654. (t
  8655. ;; Just link to current headline
  8656. (setq cpltxt (concat "file:"
  8657. (abbreviate-file-name
  8658. (buffer-file-name (buffer-base-buffer)))))
  8659. ;; Add a context search string
  8660. (when (org-xor org-context-in-file-links arg)
  8661. (let* ((ee (org-element-at-point))
  8662. (et (org-element-type ee))
  8663. (ev (plist-get (cadr ee) :value))
  8664. (ek (plist-get (cadr ee) :key))
  8665. (eok (and (stringp ek) (string-match "name" ek))))
  8666. (setq txt (cond
  8667. ((org-at-heading-p) nil)
  8668. ((and (eq et 'keyword) eok) ev)
  8669. ((org-region-active-p)
  8670. (buffer-substring (region-beginning) (region-end)))))
  8671. (when (or (null txt) (string-match "\\S-" txt))
  8672. (setq cpltxt
  8673. (concat cpltxt "::"
  8674. (condition-case nil
  8675. (org-make-org-heading-search-string txt)
  8676. (error "")))
  8677. desc (or (and (eq et 'keyword) eok ev)
  8678. (nth 4 (ignore-errors (org-heading-components)))
  8679. "NONE")))))
  8680. (if (string-match "::\\'" cpltxt)
  8681. (setq cpltxt (substring cpltxt 0 -2)))
  8682. (setq link cpltxt))))
  8683. ((buffer-file-name (buffer-base-buffer))
  8684. ;; Just link to this file here.
  8685. (setq cpltxt (concat "file:"
  8686. (abbreviate-file-name
  8687. (buffer-file-name (buffer-base-buffer)))))
  8688. ;; Add a context string.
  8689. (when (org-xor org-context-in-file-links arg)
  8690. (setq txt (if (org-region-active-p)
  8691. (buffer-substring (region-beginning) (region-end))
  8692. (buffer-substring (point-at-bol) (point-at-eol))))
  8693. ;; Only use search option if there is some text.
  8694. (when (string-match "\\S-" txt)
  8695. (setq cpltxt
  8696. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  8697. desc "NONE")))
  8698. (setq link cpltxt))
  8699. ((org-called-interactively-p 'interactive)
  8700. (user-error "No method for storing a link from this buffer"))
  8701. (t (setq link nil)))
  8702. ;; We're done setting link and desc, clean up
  8703. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  8704. (setq link (or link cpltxt)
  8705. desc (or desc cpltxt))
  8706. (cond ((equal desc "NONE") (setq desc nil))
  8707. ((and desc (string-match org-bracket-link-analytic-regexp desc))
  8708. (let ((d0 (match-string 3 desc))
  8709. (p0 (match-string 5 desc)))
  8710. (setq desc
  8711. (replace-regexp-in-string
  8712. org-bracket-link-regexp
  8713. (concat (or p0 d0)
  8714. (if (equal (length (match-string 0 desc))
  8715. (length desc)) "*" "")) desc)))))
  8716. ;; Return the link
  8717. (if (not (and (or (org-called-interactively-p 'any)
  8718. executing-kbd-macro) link))
  8719. (or agenda-link (and link (org-make-link-string link desc)))
  8720. (push (list link desc) org-stored-links)
  8721. (message "Stored: %s" (or desc link))
  8722. (when custom-id
  8723. (setq link (concat "file:" (abbreviate-file-name
  8724. (buffer-file-name)) "::#" custom-id))
  8725. (push (list link desc) org-stored-links))
  8726. (car org-stored-links))))))
  8727. (defun org-store-link-props (&rest plist)
  8728. "Store link properties, extract names and addresses."
  8729. (let (x adr)
  8730. (when (setq x (plist-get plist :from))
  8731. (setq adr (mail-extract-address-components x))
  8732. (setq plist (plist-put plist :fromname (car adr)))
  8733. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  8734. (when (setq x (plist-get plist :to))
  8735. (setq adr (mail-extract-address-components x))
  8736. (setq plist (plist-put plist :toname (car adr)))
  8737. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  8738. (let ((from (plist-get plist :from))
  8739. (to (plist-get plist :to)))
  8740. (when (and from to org-from-is-user-regexp)
  8741. (setq plist
  8742. (plist-put plist :fromto
  8743. (if (string-match org-from-is-user-regexp from)
  8744. (concat "to %t")
  8745. (concat "from %f"))))))
  8746. (setq org-store-link-plist plist))
  8747. (defun org-add-link-props (&rest plist)
  8748. "Add these properties to the link property list."
  8749. (let (key value)
  8750. (while plist
  8751. (setq key (pop plist) value (pop plist))
  8752. (setq org-store-link-plist
  8753. (plist-put org-store-link-plist key value)))))
  8754. (defun org-email-link-description (&optional fmt)
  8755. "Return the description part of an email link.
  8756. This takes information from `org-store-link-plist' and formats it
  8757. according to FMT (default from `org-email-link-description-format')."
  8758. (setq fmt (or fmt org-email-link-description-format))
  8759. (let* ((p org-store-link-plist)
  8760. (to (plist-get p :toaddress))
  8761. (from (plist-get p :fromaddress))
  8762. (table
  8763. (list
  8764. (cons "%c" (plist-get p :fromto))
  8765. (cons "%F" (plist-get p :from))
  8766. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  8767. (cons "%T" (plist-get p :to))
  8768. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  8769. (cons "%s" (plist-get p :subject))
  8770. (cons "%d" (plist-get p :date))
  8771. (cons "%m" (plist-get p :message-id)))))
  8772. (when (string-match "%c" fmt)
  8773. ;; Check if the user wrote this message
  8774. (if (and org-from-is-user-regexp from to
  8775. (save-match-data (string-match org-from-is-user-regexp from)))
  8776. (setq fmt (replace-match "to %t" t t fmt))
  8777. (setq fmt (replace-match "from %f" t t fmt))))
  8778. (org-replace-escapes fmt table)))
  8779. (defun org-make-org-heading-search-string (&optional string)
  8780. "Make search string for the current headline or STRING."
  8781. (let ((s (or string
  8782. (and (derived-mode-p 'org-mode)
  8783. (save-excursion
  8784. (org-back-to-heading t)
  8785. (org-element-property :raw-value (org-element-at-point))))))
  8786. (lines org-context-in-file-links))
  8787. (or string (setq s (concat "*" s))) ; Add * for headlines
  8788. (setq s (replace-regexp-in-string "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" "" s))
  8789. (when (and string (integerp lines) (> lines 0))
  8790. (let ((slines (org-split-string s "\n")))
  8791. (when (< lines (length slines))
  8792. (setq s (mapconcat
  8793. 'identity
  8794. (reverse (nthcdr (- (length slines) lines)
  8795. (reverse slines))) "\n")))))
  8796. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  8797. (defun org-make-link-string (link &optional description)
  8798. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8799. (unless (string-match "\\S-" link)
  8800. (error "Empty link"))
  8801. (when (and description
  8802. (stringp description)
  8803. (not (string-match "\\S-" description)))
  8804. (setq description nil))
  8805. (when (stringp description)
  8806. ;; Remove brackets from the description, they are fatal.
  8807. (while (string-match "\\[" description)
  8808. (setq description (replace-match "{" t t description)))
  8809. (while (string-match "\\]" description)
  8810. (setq description (replace-match "}" t t description))))
  8811. (when (equal link description)
  8812. ;; No description needed, it is identical
  8813. (setq description nil))
  8814. (when (and (not description)
  8815. (not (string-match (org-image-file-name-regexp) link))
  8816. (not (equal link (org-link-escape link))))
  8817. (setq description (org-extract-attributes link)))
  8818. (setq link
  8819. (cond ((string-match (org-image-file-name-regexp) link) link)
  8820. ((string-match org-link-types-re link)
  8821. (concat (match-string 1 link)
  8822. (org-link-escape (substring link (match-end 1)))))
  8823. (t (org-link-escape link))))
  8824. (concat "[[" link "]"
  8825. (if description (concat "[" description "]") "")
  8826. "]"))
  8827. (defconst org-link-escape-chars
  8828. '(?\ ?\[ ?\] ?\; ?\= ?\+)
  8829. "List of characters that should be escaped in link.
  8830. This is the list that is used for internal purposes.")
  8831. (defconst org-link-escape-chars-browser
  8832. '(?\ ?\")
  8833. "List of escapes for characters that are problematic in links.
  8834. This is the list that is used before handing over to the browser.")
  8835. (defun org-link-escape (text &optional table merge)
  8836. "Return percent escaped representation of TEXT.
  8837. TEXT is a string with the text to escape.
  8838. Optional argument TABLE is a list with characters that should be
  8839. escaped. When nil, `org-link-escape-chars' is used.
  8840. If optional argument MERGE is set, merge TABLE into
  8841. `org-link-escape-chars'."
  8842. (cond
  8843. ((and table merge)
  8844. (mapc (lambda (defchr)
  8845. (unless (member defchr table)
  8846. (setq table (cons defchr table)))) org-link-escape-chars))
  8847. ((null table)
  8848. (setq table org-link-escape-chars)))
  8849. (mapconcat
  8850. (lambda (char)
  8851. (if (or (member char table)
  8852. (and (or (< char 32) (= char 37) (> char 126))
  8853. org-url-hexify-p))
  8854. (mapconcat (lambda (sequence-element)
  8855. (format "%%%.2X" sequence-element))
  8856. (or (encode-coding-char char 'utf-8)
  8857. (error "Unable to percent escape character: %s"
  8858. (char-to-string char))) "")
  8859. (char-to-string char))) text ""))
  8860. (defun org-link-unescape (str)
  8861. "Unhex hexified Unicode strings as returned from the JavaScript function
  8862. encodeURIComponent. E.g. `%C3%B6' is the german o-Umlaut."
  8863. (unless (and (null str) (string= "" str))
  8864. (let ((pos 0) (case-fold-search t) unhexed)
  8865. (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
  8866. (setq unhexed (org-link-unescape-compound (match-string 0 str)))
  8867. (setq str (replace-match unhexed t t str))
  8868. (setq pos (+ pos (length unhexed))))))
  8869. str)
  8870. (defun org-link-unescape-compound (hex)
  8871. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German o-Umlaut.
  8872. Note: this function also decodes single byte encodings like
  8873. `%E1' (a-acute) if not followed by another `%[A-F0-9]{2}' group."
  8874. (save-match-data
  8875. (let* ((bytes (cdr (split-string hex "%")))
  8876. (ret "")
  8877. (eat 0)
  8878. (sum 0))
  8879. (while bytes
  8880. (let* ((val (string-to-number (pop bytes) 16))
  8881. (shift-xor
  8882. (if (= 0 eat)
  8883. (cond
  8884. ((>= val 252) (cons 6 252))
  8885. ((>= val 248) (cons 5 248))
  8886. ((>= val 240) (cons 4 240))
  8887. ((>= val 224) (cons 3 224))
  8888. ((>= val 192) (cons 2 192))
  8889. (t (cons 0 0)))
  8890. (cons 6 128))))
  8891. (if (>= val 192) (setq eat (car shift-xor)))
  8892. (setq val (logxor val (cdr shift-xor)))
  8893. (setq sum (+ (lsh sum (car shift-xor)) val))
  8894. (if (> eat 0) (setq eat (- eat 1)))
  8895. (cond
  8896. ((= 0 eat) ;multi byte
  8897. (setq ret (concat ret (org-char-to-string sum)))
  8898. (setq sum 0))
  8899. ((not bytes) ; single byte(s)
  8900. (setq ret (org-link-unescape-single-byte-sequence hex))))
  8901. )) ;; end (while bytes
  8902. ret )))
  8903. (defun org-link-unescape-single-byte-sequence (hex)
  8904. "Unhexify hex-encoded single byte character sequences."
  8905. (mapconcat (lambda (byte)
  8906. (char-to-string (string-to-number byte 16)))
  8907. (cdr (split-string hex "%")) ""))
  8908. (defun org-xor (a b)
  8909. "Exclusive or."
  8910. (if a (not b) b))
  8911. (defun org-fixup-message-id-for-http (s)
  8912. "Replace special characters in a message id, so it can be used in an http query."
  8913. (when (string-match "%" s)
  8914. (setq s (mapconcat (lambda (c)
  8915. (if (eq c ?%)
  8916. "%25"
  8917. (char-to-string c)))
  8918. s "")))
  8919. (while (string-match "<" s)
  8920. (setq s (replace-match "%3C" t t s)))
  8921. (while (string-match ">" s)
  8922. (setq s (replace-match "%3E" t t s)))
  8923. (while (string-match "@" s)
  8924. (setq s (replace-match "%40" t t s)))
  8925. s)
  8926. (defun org-link-prettify (link)
  8927. "Return a human-readable representation of LINK.
  8928. The car of LINK must be a raw link the cdr of LINK must be either
  8929. a link description or nil."
  8930. (let ((desc (or (cadr link) "<no description>")))
  8931. (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
  8932. "<" (car link) ">")))
  8933. ;;;###autoload
  8934. (defun org-insert-link-global ()
  8935. "Insert a link like Org-mode does.
  8936. This command can be called in any mode to insert a link in Org-mode syntax."
  8937. (interactive)
  8938. (org-load-modules-maybe)
  8939. (org-run-like-in-org-mode 'org-insert-link))
  8940. (defun org-insert-all-links (&optional keep)
  8941. "Insert all links in `org-stored-links'."
  8942. (interactive "P")
  8943. (let ((links (copy-sequence org-stored-links)) l)
  8944. (while (setq l (if keep (pop links) (pop org-stored-links)))
  8945. (insert "- ")
  8946. (org-insert-link nil (car l) (or (cadr l) "<no description>"))
  8947. (insert "\n"))))
  8948. (defun org-link-fontify-links-to-this-file ()
  8949. "Fontify links to the current file in `org-stored-links'."
  8950. (let ((f (buffer-file-name)) a b)
  8951. (setq a (mapcar (lambda(l)
  8952. (let ((ll (car l)))
  8953. (when (and (string-match "^file:\\(.+\\)::" ll)
  8954. (equal f (expand-file-name (match-string 1 ll))))
  8955. ll)))
  8956. org-stored-links))
  8957. (when (featurep 'org-id)
  8958. (setq b (mapcar (lambda(l)
  8959. (let ((ll (car l)))
  8960. (when (and (string-match "^id:\\(.+\\)$" ll)
  8961. (equal f (expand-file-name
  8962. (or (org-id-find-id-file
  8963. (match-string 1 ll)) ""))))
  8964. ll)))
  8965. org-stored-links)))
  8966. (mapcar (lambda(l)
  8967. (put-text-property 0 (length l) 'face 'font-lock-comment-face l))
  8968. (delq nil (append a b)))))
  8969. (defvar org-link-links-in-this-file nil)
  8970. (defun org-insert-link (&optional complete-file link-location default-description)
  8971. "Insert a link. At the prompt, enter the link.
  8972. Completion can be used to insert any of the link protocol prefixes like
  8973. http or ftp in use.
  8974. The history can be used to select a link previously stored with
  8975. `org-store-link'. When the empty string is entered (i.e. if you just
  8976. press RET at the prompt), the link defaults to the most recently
  8977. stored link. As SPC triggers completion in the minibuffer, you need to
  8978. use M-SPC or C-q SPC to force the insertion of a space character.
  8979. You will also be prompted for a description, and if one is given, it will
  8980. be displayed in the buffer instead of the link.
  8981. If there is already a link at point, this command will allow you to edit link
  8982. and description parts.
  8983. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  8984. be selected using completion. The path to the file will be relative to the
  8985. current directory if the file is in the current directory or a subdirectory.
  8986. Otherwise, the link will be the absolute path as completed in the minibuffer
  8987. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  8988. option `org-link-file-path-type'.
  8989. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  8990. the current directory or below.
  8991. With three \\[universal-argument] prefixes, negate the meaning of
  8992. `org-keep-stored-link-after-insertion'.
  8993. If `org-make-link-description-function' is non-nil, this function will be
  8994. called with the link target, and the result will be the default
  8995. link description.
  8996. If the LINK-LOCATION parameter is non-nil, this value will be
  8997. used as the link location instead of reading one interactively.
  8998. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  8999. be used as the default description."
  9000. (interactive "P")
  9001. (let* ((wcf (current-window-configuration))
  9002. (origbuf (current-buffer))
  9003. (region (if (org-region-active-p)
  9004. (buffer-substring (region-beginning) (region-end))))
  9005. (remove (and region (list (region-beginning) (region-end))))
  9006. (desc region)
  9007. tmphist ; byte-compile incorrectly complains about this
  9008. (link link-location)
  9009. (abbrevs org-link-abbrev-alist-local)
  9010. entry file all-prefixes auto-desc)
  9011. (cond
  9012. (link-location) ; specified by arg, just use it.
  9013. ((org-in-regexp org-bracket-link-regexp 1)
  9014. ;; We do have a link at point, and we are going to edit it.
  9015. (setq remove (list (match-beginning 0) (match-end 0)))
  9016. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  9017. (setq link (read-string "Link: "
  9018. (org-link-unescape
  9019. (org-match-string-no-properties 1)))))
  9020. ((or (org-in-regexp org-angle-link-re)
  9021. (org-in-regexp org-plain-link-re))
  9022. ;; Convert to bracket link
  9023. (setq remove (list (match-beginning 0) (match-end 0))
  9024. link (read-string "Link: "
  9025. (org-remove-angle-brackets (match-string 0)))))
  9026. ((member complete-file '((4) (16)))
  9027. ;; Completing read for file names.
  9028. (setq link (org-file-complete-link complete-file)))
  9029. (t
  9030. ;; Read link, with completion for stored links.
  9031. (org-link-fontify-links-to-this-file)
  9032. (org-switch-to-buffer-other-window "*Org Links*")
  9033. (with-current-buffer "*Org Links*"
  9034. (erase-buffer)
  9035. (insert "Insert a link.
  9036. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  9037. (when org-stored-links
  9038. (insert "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  9039. (insert (mapconcat 'org-link-prettify
  9040. (reverse org-stored-links) "\n")))
  9041. (goto-char (point-min)))
  9042. (let ((cw (selected-window)))
  9043. (select-window (get-buffer-window "*Org Links*" 'visible))
  9044. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  9045. (unless (pos-visible-in-window-p (point-max))
  9046. (org-fit-window-to-buffer))
  9047. (and (window-live-p cw) (select-window cw)))
  9048. ;; Fake a link history, containing the stored links.
  9049. (setq tmphist (append (mapcar 'car org-stored-links)
  9050. org-insert-link-history))
  9051. (setq all-prefixes (append (mapcar 'car abbrevs)
  9052. (mapcar 'car org-link-abbrev-alist)
  9053. org-link-types))
  9054. (unwind-protect
  9055. (progn
  9056. (setq link
  9057. (org-completing-read
  9058. "Link: "
  9059. (append
  9060. (mapcar (lambda (x) (concat x ":"))
  9061. all-prefixes)
  9062. (mapcar 'car org-stored-links))
  9063. nil nil nil
  9064. 'tmphist
  9065. (caar org-stored-links)))
  9066. (if (not (string-match "\\S-" link))
  9067. (user-error "No link selected"))
  9068. (mapc (lambda(l)
  9069. (when (equal link (cadr l)) (setq link (car l) auto-desc t)))
  9070. org-stored-links)
  9071. (if (or (member link all-prefixes)
  9072. (and (equal ":" (substring link -1))
  9073. (member (substring link 0 -1) all-prefixes)
  9074. (setq link (substring link 0 -1))))
  9075. (setq link (with-current-buffer origbuf
  9076. (org-link-try-special-completion link)))))
  9077. (set-window-configuration wcf)
  9078. (kill-buffer "*Org Links*"))
  9079. (setq entry (assoc link org-stored-links))
  9080. (or entry (push link org-insert-link-history))
  9081. (setq desc (or desc (nth 1 entry)))))
  9082. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  9083. (not org-keep-stored-link-after-insertion))
  9084. (setq org-stored-links (delq (assoc link org-stored-links)
  9085. org-stored-links)))
  9086. (if (and (string-match org-plain-link-re link)
  9087. (not (string-match org-ts-regexp link)))
  9088. ;; URL-like link, normalize the use of angular brackets.
  9089. (setq link (org-remove-angle-brackets link)))
  9090. ;; Check if we are linking to the current file with a search
  9091. ;; option If yes, simplify the link by using only the search
  9092. ;; option.
  9093. (when (and buffer-file-name
  9094. (string-match "^file:\\(.+?\\)::\\(.+\\)" link))
  9095. (let* ((path (match-string 1 link))
  9096. (case-fold-search nil)
  9097. (search (match-string 2 link)))
  9098. (save-match-data
  9099. (if (equal (file-truename buffer-file-name) (file-truename path))
  9100. ;; We are linking to this same file, with a search option
  9101. (setq link search)))))
  9102. ;; Check if we can/should use a relative path. If yes, simplify the link
  9103. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  9104. (let* ((type (match-string 1 link))
  9105. (path (match-string 2 link))
  9106. (origpath path)
  9107. (case-fold-search nil))
  9108. (cond
  9109. ((or (eq org-link-file-path-type 'absolute)
  9110. (equal complete-file '(16)))
  9111. (setq path (abbreviate-file-name (expand-file-name path))))
  9112. ((eq org-link-file-path-type 'noabbrev)
  9113. (setq path (expand-file-name path)))
  9114. ((eq org-link-file-path-type 'relative)
  9115. (setq path (file-relative-name path)))
  9116. (t
  9117. (save-match-data
  9118. (if (string-match (concat "^" (regexp-quote
  9119. (expand-file-name
  9120. (file-name-as-directory
  9121. default-directory))))
  9122. (expand-file-name path))
  9123. ;; We are linking a file with relative path name.
  9124. (setq path (substring (expand-file-name path)
  9125. (match-end 0)))
  9126. (setq path (abbreviate-file-name (expand-file-name path)))))))
  9127. (setq link (concat type path))
  9128. (if (equal desc origpath)
  9129. (setq desc path))))
  9130. (if org-make-link-description-function
  9131. (setq desc
  9132. (or (condition-case nil
  9133. (funcall org-make-link-description-function link desc)
  9134. (error (progn (message "Can't get link description from `%s'"
  9135. (symbol-name org-make-link-description-function))
  9136. (sit-for 2) nil)))
  9137. (read-string "Description: " default-description)))
  9138. (if default-description (setq desc default-description)
  9139. (setq desc (or (and auto-desc desc)
  9140. (read-string "Description: " desc)))))
  9141. (unless (string-match "\\S-" desc) (setq desc nil))
  9142. (if remove (apply 'delete-region remove))
  9143. (insert (org-make-link-string link desc))))
  9144. (defun org-link-try-special-completion (type)
  9145. "If there is completion support for link type TYPE, offer it."
  9146. (let ((fun (intern (concat "org-" type "-complete-link"))))
  9147. (if (functionp fun)
  9148. (funcall fun)
  9149. (read-string "Link (no completion support): " (concat type ":")))))
  9150. (defun org-file-complete-link (&optional arg)
  9151. "Create a file link using completion."
  9152. (let (file link)
  9153. (setq file (org-iread-file-name "File: "))
  9154. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  9155. (pwd1 (file-name-as-directory (abbreviate-file-name
  9156. (expand-file-name ".")))))
  9157. (cond
  9158. ((equal arg '(16))
  9159. (setq link (concat
  9160. "file:"
  9161. (abbreviate-file-name (expand-file-name file)))))
  9162. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  9163. (setq link (concat "file:" (match-string 1 file))))
  9164. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  9165. (expand-file-name file))
  9166. (setq link (concat
  9167. "file:" (match-string 1 (expand-file-name file)))))
  9168. (t (setq link (concat "file:" file)))))
  9169. link))
  9170. (defun org-iread-file-name (&rest args)
  9171. "Read-file-name using `ido-mode' speedup if available.
  9172. ARGS are arguments that may be passed to `ido-read-file-name' or `read-file-name'.
  9173. See `read-file-name' for a description of parameters."
  9174. (org-without-partial-completion
  9175. (if (and org-completion-use-ido
  9176. (fboundp 'ido-read-file-name)
  9177. (boundp 'ido-mode) ido-mode
  9178. (listp (second args)))
  9179. (let ((ido-enter-matching-directory nil))
  9180. (apply 'ido-read-file-name args))
  9181. (apply 'read-file-name args))))
  9182. (defun org-completing-read (&rest args)
  9183. "Completing-read with SPACE being a normal character."
  9184. (let ((enable-recursive-minibuffers t)
  9185. (minibuffer-local-completion-map
  9186. (copy-keymap minibuffer-local-completion-map)))
  9187. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  9188. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  9189. (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
  9190. (apply 'org-icompleting-read args)))
  9191. (defun org-completing-read-no-i (&rest args)
  9192. (let (org-completion-use-ido org-completion-use-iswitchb)
  9193. (apply 'org-completing-read args)))
  9194. (defun org-iswitchb-completing-read (prompt choices &rest args)
  9195. "Use iswitch as a completing-read replacement to choose from choices.
  9196. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  9197. from."
  9198. (let* ((iswitchb-use-virtual-buffers nil)
  9199. (iswitchb-make-buflist-hook
  9200. (lambda ()
  9201. (setq iswitchb-temp-buflist choices))))
  9202. (iswitchb-read-buffer prompt)))
  9203. (defun org-icompleting-read (&rest args)
  9204. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  9205. (org-without-partial-completion
  9206. (if (and org-completion-use-ido
  9207. (fboundp 'ido-completing-read)
  9208. (boundp 'ido-mode) ido-mode
  9209. (listp (second args)))
  9210. (let ((ido-enter-matching-directory nil))
  9211. (apply 'ido-completing-read (concat (car args))
  9212. (if (consp (car (nth 1 args)))
  9213. (mapcar 'car (nth 1 args))
  9214. (nth 1 args))
  9215. (cddr args)))
  9216. (if (and org-completion-use-iswitchb
  9217. (boundp 'iswitchb-mode) iswitchb-mode
  9218. (listp (second args)))
  9219. (apply 'org-iswitchb-completing-read (concat (car args))
  9220. (if (consp (car (nth 1 args)))
  9221. (mapcar 'car (nth 1 args))
  9222. (nth 1 args))
  9223. (cddr args))
  9224. (apply 'completing-read args)))))
  9225. (defun org-extract-attributes (s)
  9226. "Extract the attributes cookie from a string and set as text property."
  9227. (let (a attr (start 0) key value)
  9228. (save-match-data
  9229. (when (string-match "{{\\([^}]+\\)}}$" s)
  9230. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  9231. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  9232. (setq key (match-string 1 a) value (match-string 2 a)
  9233. start (match-end 0)
  9234. attr (plist-put attr (intern key) value))))
  9235. (org-add-props s nil 'org-attr attr))
  9236. s))
  9237. ;;; Opening/following a link
  9238. (defvar org-link-search-failed nil)
  9239. (defvar org-open-link-functions nil
  9240. "Hook for functions finding a plain text link.
  9241. These functions must take a single argument, the link content.
  9242. They will be called for links that look like [[link text][description]]
  9243. when LINK TEXT does not have a protocol like \"http:\" and does not look
  9244. like a filename (e.g. \"./blue.png\").
  9245. These functions will be called *before* Org attempts to resolve the
  9246. link by doing text searches in the current buffer - so if you want a
  9247. link \"[[target]]\" to still find \"<<target>>\", your function should
  9248. handle this as a special case.
  9249. When the function does handle the link, it must return a non-nil value.
  9250. If it decides that it is not responsible for this link, it must return
  9251. nil to indicate that that Org-mode can continue with other options
  9252. like exact and fuzzy text search.")
  9253. (defun org-next-link (&optional search-backward)
  9254. "Move forward to the next link.
  9255. If the link is in hidden text, expose it."
  9256. (interactive "P")
  9257. (when (and org-link-search-failed (eq this-command last-command))
  9258. (goto-char (point-min))
  9259. (message "Link search wrapped back to beginning of buffer"))
  9260. (setq org-link-search-failed nil)
  9261. (let* ((pos (point))
  9262. (ct (org-context))
  9263. (a (assoc :link ct))
  9264. (srch-fun (if search-backward 're-search-backward 're-search-forward)))
  9265. (cond (a (goto-char (nth (if search-backward 1 2) a)))
  9266. ((looking-at org-any-link-re)
  9267. ;; Don't stay stuck at link without an org-link face
  9268. (forward-char (if search-backward -1 1))))
  9269. (if (funcall srch-fun org-any-link-re nil t)
  9270. (progn
  9271. (goto-char (match-beginning 0))
  9272. (if (outline-invisible-p) (org-show-context)))
  9273. (goto-char pos)
  9274. (setq org-link-search-failed t)
  9275. (message "No further link found"))))
  9276. (defun org-previous-link ()
  9277. "Move backward to the previous link.
  9278. If the link is in hidden text, expose it."
  9279. (interactive)
  9280. (funcall 'org-next-link t))
  9281. (defun org-translate-link (s)
  9282. "Translate a link string if a translation function has been defined."
  9283. (if (and org-link-translation-function
  9284. (fboundp org-link-translation-function)
  9285. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  9286. (progn
  9287. (setq s (funcall org-link-translation-function
  9288. (match-string 1 s) (match-string 2 s)))
  9289. (concat (car s) ":" (cdr s)))
  9290. s))
  9291. (defun org-translate-link-from-planner (type path)
  9292. "Translate a link from Emacs Planner syntax so that Org can follow it.
  9293. This is still an experimental function, your mileage may vary."
  9294. (cond
  9295. ((member type '("http" "https" "news" "ftp"))
  9296. ;; standard Internet links are the same.
  9297. nil)
  9298. ((and (equal type "irc") (string-match "^//" path))
  9299. ;; Planner has two / at the beginning of an irc link, we have 1.
  9300. ;; We should have zero, actually....
  9301. (setq path (substring path 1)))
  9302. ((and (equal type "lisp") (string-match "^/" path))
  9303. ;; Planner has a slash, we do not.
  9304. (setq type "elisp" path (substring path 1)))
  9305. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  9306. ;; A typical message link. Planner has the id after the final slash,
  9307. ;; we separate it with a hash mark
  9308. (setq path (concat (match-string 1 path) "#"
  9309. (org-remove-angle-brackets (match-string 2 path))))))
  9310. (cons type path))
  9311. (defun org-find-file-at-mouse (ev)
  9312. "Open file link or URL at mouse."
  9313. (interactive "e")
  9314. (mouse-set-point ev)
  9315. (org-open-at-point 'in-emacs))
  9316. (defun org-open-at-mouse (ev)
  9317. "Open file link or URL at mouse.
  9318. See the docstring of `org-open-file' for details."
  9319. (interactive "e")
  9320. (mouse-set-point ev)
  9321. (if (eq major-mode 'org-agenda-mode)
  9322. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  9323. (org-open-at-point))
  9324. (defvar org-window-config-before-follow-link nil
  9325. "The window configuration before following a link.
  9326. This is saved in case the need arises to restore it.")
  9327. (defvar org-open-link-marker (make-marker)
  9328. "Marker pointing to the location where `org-open-at-point' was called.")
  9329. ;;;###autoload
  9330. (defun org-open-at-point-global ()
  9331. "Follow a link like Org-mode does.
  9332. This command can be called in any mode to follow a link that has
  9333. Org-mode syntax."
  9334. (interactive)
  9335. (org-run-like-in-org-mode 'org-open-at-point))
  9336. ;;;###autoload
  9337. (defun org-open-link-from-string (s &optional arg reference-buffer)
  9338. "Open a link in the string S, as if it was in Org-mode."
  9339. (interactive "sLink: \nP")
  9340. (let ((reference-buffer (or reference-buffer (current-buffer))))
  9341. (with-temp-buffer
  9342. (let ((org-inhibit-startup (not reference-buffer)))
  9343. (org-mode)
  9344. (insert s)
  9345. (goto-char (point-min))
  9346. (when reference-buffer
  9347. (setq org-link-abbrev-alist-local
  9348. (with-current-buffer reference-buffer
  9349. org-link-abbrev-alist-local)))
  9350. (org-open-at-point arg reference-buffer)))))
  9351. (defvar org-open-at-point-functions nil
  9352. "Hook that is run when following a link at point.
  9353. Functions in this hook must return t if they identify and follow
  9354. a link at point. If they don't find anything interesting at point,
  9355. they must return nil.")
  9356. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  9357. (defvar clean-buffer-list-kill-buffer-names) ; Defined in midnight.el
  9358. (defun org-open-at-point (&optional arg reference-buffer)
  9359. "Open link at or after point.
  9360. If there is no link at point, this function will search forward up to
  9361. the end of the current line.
  9362. Normally, files will be opened by an appropriate application. If the
  9363. optional prefix argument ARG is non-nil, Emacs will visit the file.
  9364. With a double prefix argument, try to open outside of Emacs, in the
  9365. application the system uses for this file type."
  9366. (interactive "P")
  9367. ;; if in a code block, then open the block's results
  9368. (unless (call-interactively #'org-babel-open-src-block-result)
  9369. (org-load-modules-maybe)
  9370. (move-marker org-open-link-marker (point))
  9371. (setq org-window-config-before-follow-link (current-window-configuration))
  9372. (org-remove-occur-highlights nil nil t)
  9373. (cond
  9374. ((and (org-at-heading-p)
  9375. (not (org-at-timestamp-p t))
  9376. (not (org-in-regexp
  9377. (concat org-plain-link-re "\\|"
  9378. org-bracket-link-regexp "\\|"
  9379. org-angle-link-re "\\|"
  9380. "[ \t]:[^ \t\n]+:[ \t]*$")))
  9381. (not (get-text-property (point) 'org-linked-text)))
  9382. (or (let* ((lkall (org-offer-links-in-entry (current-buffer) (point) arg))
  9383. (lk0 (car lkall))
  9384. (lk (if (stringp lk0) (list lk0) lk0))
  9385. (lkend (cdr lkall)))
  9386. (mapcar (lambda(l)
  9387. (search-forward l nil lkend)
  9388. (goto-char (match-beginning 0))
  9389. (org-open-at-point))
  9390. lk))
  9391. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  9392. ((run-hook-with-args-until-success 'org-open-at-point-functions))
  9393. ((and (org-at-timestamp-p t)
  9394. (not (org-in-regexp org-bracket-link-regexp)))
  9395. (org-follow-timestamp-link))
  9396. ((and (or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  9397. (not (org-in-regexp org-any-link-re)))
  9398. (org-footnote-action))
  9399. (t
  9400. (let (type path link line search (pos (point)))
  9401. (catch 'match
  9402. (save-excursion
  9403. (or (org-in-regexp org-plain-link-re)
  9404. (skip-chars-forward "^]\n\r"))
  9405. (when (org-in-regexp org-bracket-link-regexp 1)
  9406. (setq link (org-extract-attributes
  9407. (org-link-unescape (org-match-string-no-properties 1))))
  9408. (while (string-match " *\n *" link)
  9409. (setq link (replace-match " " t t link)))
  9410. (setq link (org-link-expand-abbrev link))
  9411. (cond
  9412. ((or (file-name-absolute-p link)
  9413. (string-match "^\\.\\.?/" link))
  9414. (setq type "file" path link))
  9415. ((string-match org-link-re-with-space3 link)
  9416. (setq type (match-string 1 link) path (match-string 2 link)))
  9417. ((string-match "^help:+\\(.+\\)" link)
  9418. (setq type "help" path (match-string 1 link)))
  9419. (t (setq type "thisfile" path link)))
  9420. (throw 'match t)))
  9421. (when (get-text-property (point) 'org-linked-text)
  9422. (setq type "thisfile"
  9423. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  9424. (1+ (point)) (point))
  9425. path (buffer-substring
  9426. (or (previous-single-property-change pos 'org-linked-text)
  9427. (point-min))
  9428. (or (next-single-property-change pos 'org-linked-text)
  9429. (point-max)))
  9430. ;; Ensure we will search for a <<<radio>>> link, not
  9431. ;; a simple reference like <<ref>>
  9432. path (concat "<" path))
  9433. (throw 'match t))
  9434. (save-excursion
  9435. (when (or (org-in-regexp org-angle-link-re)
  9436. (let ((match (org-in-regexp org-plain-link-re)))
  9437. ;; Check a plain link is not within a bracket link
  9438. (and match
  9439. (save-excursion
  9440. (save-match-data
  9441. (progn
  9442. (goto-char (car match))
  9443. (not (org-in-regexp org-bracket-link-regexp)))))))
  9444. (let ((line_ending (save-excursion (end-of-line) (point))))
  9445. ;; We are in a line before a plain or bracket link
  9446. (or (re-search-forward org-plain-link-re line_ending t)
  9447. (re-search-forward org-bracket-link-regexp line_ending t))))
  9448. (setq type (match-string 1)
  9449. path (org-link-unescape (match-string 2)))
  9450. (throw 'match t)))
  9451. (save-excursion
  9452. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
  9453. (setq type "tags"
  9454. path (match-string 1))
  9455. (while (string-match ":" path)
  9456. (setq path (replace-match "+" t t path)))
  9457. (throw 'match t)))
  9458. (when (org-in-regexp "<\\([^><\n]+\\)>")
  9459. (setq type "tree-match"
  9460. path (match-string 1))
  9461. (throw 'match t)))
  9462. (unless path
  9463. (user-error "No link found"))
  9464. ;; switch back to reference buffer
  9465. ;; needed when if called in a temporary buffer through
  9466. ;; org-open-link-from-string
  9467. (with-current-buffer (or reference-buffer (current-buffer))
  9468. ;; Remove any trailing spaces in path
  9469. (if (string-match " +\\'" path)
  9470. (setq path (replace-match "" t t path)))
  9471. (if (and org-link-translation-function
  9472. (fboundp org-link-translation-function))
  9473. ;; Check if we need to translate the link
  9474. (let ((tmp (funcall org-link-translation-function type path)))
  9475. (setq type (car tmp) path (cdr tmp))))
  9476. (cond
  9477. ((assoc type org-link-protocols)
  9478. (funcall (nth 1 (assoc type org-link-protocols)) path))
  9479. ((equal type "help")
  9480. (let ((f-or-v (intern path)))
  9481. (cond ((fboundp f-or-v)
  9482. (describe-function f-or-v))
  9483. ((boundp f-or-v)
  9484. (describe-variable f-or-v))
  9485. (t (error "Not a known function or variable")))))
  9486. ((equal type "mailto")
  9487. (let ((cmd (car org-link-mailto-program))
  9488. (args (cdr org-link-mailto-program)) args1
  9489. (address path) (subject "") a)
  9490. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  9491. (setq address (match-string 1 path)
  9492. subject (org-link-escape (match-string 2 path))))
  9493. (while args
  9494. (cond
  9495. ((not (stringp (car args))) (push (pop args) args1))
  9496. (t (setq a (pop args))
  9497. (if (string-match "%a" a)
  9498. (setq a (replace-match address t t a)))
  9499. (if (string-match "%s" a)
  9500. (setq a (replace-match subject t t a)))
  9501. (push a args1))))
  9502. (apply cmd (nreverse args1))))
  9503. ((member type '("http" "https" "ftp" "news"))
  9504. (browse-url
  9505. (concat type ":"
  9506. (if (org-string-match-p
  9507. (concat "[[:nonascii:]"
  9508. org-link-escape-chars-browser "]")
  9509. path)
  9510. (org-link-escape path org-link-escape-chars-browser)
  9511. path))))
  9512. ((string= type "doi")
  9513. (browse-url
  9514. (concat org-doi-server-url
  9515. (if (org-string-match-p
  9516. (concat "[[:nonascii:]"
  9517. org-link-escape-chars-browser "]")
  9518. path)
  9519. (org-link-escape path org-link-escape-chars-browser)
  9520. path))))
  9521. ((member type '("message"))
  9522. (browse-url (concat type ":" path)))
  9523. ((string= type "tags")
  9524. (org-tags-view arg path))
  9525. ((string= type "tree-match")
  9526. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  9527. ((string= type "file")
  9528. (if (string-match "::\\([0-9]+\\)\\'" path)
  9529. (setq line (string-to-number (match-string 1 path))
  9530. path (substring path 0 (match-beginning 0)))
  9531. (if (string-match "::\\(.+\\)\\'" path)
  9532. (setq search (match-string 1 path)
  9533. path (substring path 0 (match-beginning 0)))))
  9534. (if (string-match "[*?{]" (file-name-nondirectory path))
  9535. (dired path)
  9536. (org-open-file path arg line search)))
  9537. ((string= type "shell")
  9538. (let ((buf (generate-new-buffer "*Org Shell Output"))
  9539. (cmd path))
  9540. (if (or (and (not (string= org-confirm-shell-link-not-regexp ""))
  9541. (string-match org-confirm-shell-link-not-regexp cmd))
  9542. (not org-confirm-shell-link-function)
  9543. (funcall org-confirm-shell-link-function
  9544. (format "Execute \"%s\" in shell? "
  9545. (org-add-props cmd nil
  9546. 'face 'org-warning))))
  9547. (progn
  9548. (message "Executing %s" cmd)
  9549. (shell-command cmd buf)
  9550. (if (featurep 'midnight)
  9551. (setq clean-buffer-list-kill-buffer-names
  9552. (cons buf clean-buffer-list-kill-buffer-names))))
  9553. (error "Abort"))))
  9554. ((string= type "elisp")
  9555. (let ((cmd path))
  9556. (if (or (and (not (string= org-confirm-elisp-link-not-regexp ""))
  9557. (string-match org-confirm-elisp-link-not-regexp cmd))
  9558. (not org-confirm-elisp-link-function)
  9559. (funcall org-confirm-elisp-link-function
  9560. (format "Execute \"%s\" as elisp? "
  9561. (org-add-props cmd nil
  9562. 'face 'org-warning))))
  9563. (message "%s => %s" cmd
  9564. (if (equal (string-to-char cmd) ?\()
  9565. (eval (read cmd))
  9566. (call-interactively (read cmd))))
  9567. (error "Abort"))))
  9568. ((and (string= type "thisfile")
  9569. (or (run-hook-with-args-until-success
  9570. 'org-open-link-functions path)
  9571. (and link
  9572. (string-match "^id:" link)
  9573. (or (featurep 'org-id) (require 'org-id))
  9574. (progn
  9575. (funcall (nth 1 (assoc "id" org-link-protocols))
  9576. (substring path 3))
  9577. t)))))
  9578. ((string= type "thisfile")
  9579. (if arg
  9580. (switch-to-buffer-other-window
  9581. (org-get-buffer-for-internal-link (current-buffer)))
  9582. (org-mark-ring-push))
  9583. (let ((cmd `(org-link-search
  9584. ,path
  9585. ,(cond ((equal arg '(4)) ''occur)
  9586. ((equal arg '(16)) ''org-occur))
  9587. ,pos)))
  9588. (condition-case nil (let ((org-link-search-inhibit-query t))
  9589. (eval cmd))
  9590. (error (progn (widen) (eval cmd))))))
  9591. (t (browse-url-at-point)))))))
  9592. (move-marker org-open-link-marker nil)
  9593. (run-hook-with-args 'org-follow-link-hook)))
  9594. (defsubst org-uniquify (list)
  9595. "Non-destructively remove duplicate elements from LIST."
  9596. (let ((res (copy-sequence list))) (delete-dups res)))
  9597. (defun org-offer-links-in-entry (buffer marker &optional nth zero)
  9598. "Offer links in the current entry and return the selected link.
  9599. If there is only one link, return it.
  9600. If NTH is an integer, return the NTH link found.
  9601. If ZERO is a string, check also this string for a link, and if
  9602. there is one, return it."
  9603. (with-current-buffer buffer
  9604. (save-excursion
  9605. (save-restriction
  9606. (widen)
  9607. (goto-char marker)
  9608. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  9609. "\\(" org-angle-link-re "\\)\\|"
  9610. "\\(" org-plain-link-re "\\)"))
  9611. (cnt ?0)
  9612. (in-emacs (if (integerp nth) nil nth))
  9613. have-zero end links link c)
  9614. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  9615. (push (match-string 0 zero) links)
  9616. (setq cnt (1- cnt) have-zero t))
  9617. (save-excursion
  9618. (org-back-to-heading t)
  9619. (setq end (save-excursion (outline-next-heading) (point)))
  9620. (while (re-search-forward re end t)
  9621. (push (match-string 0) links))
  9622. (setq links (org-uniquify (reverse links))))
  9623. (cond
  9624. ((null links)
  9625. (message "No links"))
  9626. ((equal (length links) 1)
  9627. (setq link (car links)))
  9628. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  9629. (setq link (nth (if have-zero nth (1- nth)) links)))
  9630. (t ; we have to select a link
  9631. (save-excursion
  9632. (save-window-excursion
  9633. (delete-other-windows)
  9634. (with-output-to-temp-buffer "*Select Link*"
  9635. (mapc (lambda (l)
  9636. (if (not (string-match org-bracket-link-regexp l))
  9637. (princ (format "[%c] %s\n" (incf cnt)
  9638. (org-remove-angle-brackets l)))
  9639. (if (match-end 3)
  9640. (princ (format "[%c] %s (%s)\n" (incf cnt)
  9641. (match-string 3 l) (match-string 1 l)))
  9642. (princ (format "[%c] %s\n" (incf cnt)
  9643. (match-string 1 l))))))
  9644. links))
  9645. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  9646. (message "Select link to open, RET to open all:")
  9647. (setq c (read-char-exclusive))
  9648. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  9649. (when (equal c ?q) (error "Abort"))
  9650. (if (equal c ?\C-m)
  9651. (setq link links)
  9652. (setq nth (- c ?0))
  9653. (if have-zero (setq nth (1+ nth)))
  9654. (unless (and (integerp nth) (>= (length links) nth))
  9655. (user-error "Invalid link selection"))
  9656. (setq link (nth (1- nth) links)))))
  9657. (cons link end))))))
  9658. ;; Add special file links that specify the way of opening
  9659. (org-add-link-type "file+sys" 'org-open-file-with-system)
  9660. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  9661. (defun org-open-file-with-system (path)
  9662. "Open file at PATH using the system way of opening it."
  9663. (org-open-file path 'system))
  9664. (defun org-open-file-with-emacs (path)
  9665. "Open file at PATH in Emacs."
  9666. (org-open-file path 'emacs))
  9667. ;;; File search
  9668. (defvar org-create-file-search-functions nil
  9669. "List of functions to construct the right search string for a file link.
  9670. These functions are called in turn with point at the location to
  9671. which the link should point.
  9672. A function in the hook should first test if it would like to
  9673. handle this file type, for example by checking the `major-mode'
  9674. or the file extension. If it decides not to handle this file, it
  9675. should just return nil to give other functions a chance. If it
  9676. does handle the file, it must return the search string to be used
  9677. when following the link. The search string will be part of the
  9678. file link, given after a double colon, and `org-open-at-point'
  9679. will automatically search for it. If special measures must be
  9680. taken to make the search successful, another function should be
  9681. added to the companion hook `org-execute-file-search-functions',
  9682. which see.
  9683. A function in this hook may also use `setq' to set the variable
  9684. `description' to provide a suggestion for the descriptive text to
  9685. be used for this link when it gets inserted into an Org-mode
  9686. buffer with \\[org-insert-link].")
  9687. (defvar org-execute-file-search-functions nil
  9688. "List of functions to execute a file search triggered by a link.
  9689. Functions added to this hook must accept a single argument, the
  9690. search string that was part of the file link, the part after the
  9691. double colon. The function must first check if it would like to
  9692. handle this search, for example by checking the `major-mode' or
  9693. the file extension. If it decides not to handle this search, it
  9694. should just return nil to give other functions a chance. If it
  9695. does handle the search, it must return a non-nil value to keep
  9696. other functions from trying.
  9697. Each function can access the current prefix argument through the
  9698. variable `current-prefix-arg'. Note that a single prefix is used
  9699. to force opening a link in Emacs, so it may be good to only use a
  9700. numeric or double prefix to guide the search function.
  9701. In case this is needed, a function in this hook can also restore
  9702. the window configuration before `org-open-at-point' was called using:
  9703. (set-window-configuration org-window-config-before-follow-link)")
  9704. (defun org-link-search (s &optional type avoid-pos stealth)
  9705. "Search for a link search option.
  9706. If S is surrounded by forward slashes, it is interpreted as a
  9707. regular expression. In org-mode files, this will create an `org-occur'
  9708. sparse tree. In ordinary files, `occur' will be used to list matches.
  9709. If the current buffer is in `dired-mode', grep will be used to search
  9710. in all files. If AVOID-POS is given, ignore matches near that position.
  9711. When optional argument STEALTH is non-nil, do not modify
  9712. visibility around point, thus ignoring
  9713. `org-show-hierarchy-above', `org-show-following-heading' and
  9714. `org-show-siblings' variables."
  9715. (let ((case-fold-search t)
  9716. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  9717. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  9718. (append '(("") (" ") ("\t") ("\n"))
  9719. org-emphasis-alist)
  9720. "\\|") "\\)"))
  9721. (pos (point))
  9722. (pre nil) (post nil)
  9723. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  9724. (cond
  9725. ;; First check if there are any special search functions
  9726. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  9727. ;; Now try the builtin stuff
  9728. ((and (equal (string-to-char s0) ?#)
  9729. (> (length s0) 1)
  9730. (save-excursion
  9731. (goto-char (point-min))
  9732. (and
  9733. (re-search-forward
  9734. (concat "^[ \t]*:CUSTOM_ID:[ \t]+"
  9735. (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  9736. (setq type 'dedicated
  9737. pos (match-beginning 0))))
  9738. ;; There is an exact target for this
  9739. (goto-char pos)
  9740. (org-back-to-heading t)))
  9741. ((save-excursion
  9742. (goto-char (point-min))
  9743. (and
  9744. (re-search-forward
  9745. (concat "<<" (regexp-quote s0) ">>") nil t)
  9746. (setq type 'dedicated
  9747. pos (match-beginning 0))))
  9748. ;; There is an exact target for this
  9749. (goto-char pos))
  9750. ((save-excursion
  9751. (goto-char (point-min))
  9752. (and
  9753. (re-search-forward
  9754. (format "^[ \t]*#\\+NAME: %s" (regexp-quote s0)) nil t)
  9755. (setq type 'dedicated pos (match-beginning 0))))
  9756. ;; Found an element with a matching #+name affiliated keyword.
  9757. (goto-char pos))
  9758. ((and (string-match "^(\\(.*\\))$" s0)
  9759. (save-excursion
  9760. (goto-char (point-min))
  9761. (and
  9762. (re-search-forward
  9763. (concat "[^[]" (regexp-quote
  9764. (format org-coderef-label-format
  9765. (match-string 1 s0))))
  9766. nil t)
  9767. (setq type 'dedicated
  9768. pos (1+ (match-beginning 0))))))
  9769. ;; There is a coderef target for this
  9770. (goto-char pos))
  9771. ((string-match "^/\\(.*\\)/$" s)
  9772. ;; A regular expression
  9773. (cond
  9774. ((derived-mode-p 'org-mode)
  9775. (org-occur (match-string 1 s)))
  9776. (t (org-do-occur (match-string 1 s)))))
  9777. ((and (derived-mode-p 'org-mode) org-link-search-must-match-exact-headline)
  9778. (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  9779. (goto-char (point-min))
  9780. (cond
  9781. ((let (case-fold-search)
  9782. (re-search-forward (format org-complex-heading-regexp-format
  9783. (regexp-quote s))
  9784. nil t))
  9785. ;; OK, found a match
  9786. (setq type 'dedicated)
  9787. (goto-char (match-beginning 0)))
  9788. ((and (not org-link-search-inhibit-query)
  9789. (eq org-link-search-must-match-exact-headline 'query-to-create)
  9790. (y-or-n-p "No match - create this as a new heading? "))
  9791. (goto-char (point-max))
  9792. (or (bolp) (newline))
  9793. (insert "* " s "\n")
  9794. (beginning-of-line 0))
  9795. (t
  9796. (goto-char pos)
  9797. (error "No match"))))
  9798. (t
  9799. ;; A normal search string
  9800. (when (equal (string-to-char s) ?*)
  9801. ;; Anchor on headlines, post may include tags.
  9802. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  9803. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
  9804. s (substring s 1)))
  9805. (remove-text-properties
  9806. 0 (length s)
  9807. '(face nil mouse-face nil keymap nil fontified nil) s)
  9808. ;; Make a series of regular expressions to find a match
  9809. (setq words (org-split-string s "[ \n\r\t]+")
  9810. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  9811. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  9812. "\\)" markers)
  9813. re2a_ (concat "\\(" (mapconcat 'downcase words
  9814. "[ \t\r\n]+") "\\)[ \t\r\n]")
  9815. re2a (concat "[ \t\r\n]" re2a_)
  9816. re4_ (concat "\\(" (mapconcat 'downcase words
  9817. "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  9818. re4 (concat "[^a-zA-Z_]" re4_)
  9819. re1 (concat pre re2 post)
  9820. re3 (concat pre (if pre re4_ re4) post)
  9821. re5 (concat pre ".*" re4)
  9822. re2 (concat pre re2)
  9823. re2a (concat pre (if pre re2a_ re2a))
  9824. re4 (concat pre (if pre re4_ re4))
  9825. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  9826. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  9827. re5 "\\)"))
  9828. (cond
  9829. ((eq type 'org-occur) (org-occur reall))
  9830. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  9831. (t (goto-char (point-min))
  9832. (setq type 'fuzzy)
  9833. (if (or (and (org-search-not-self 1 re0 nil t)
  9834. (setq type 'dedicated))
  9835. (org-search-not-self 1 re1 nil t)
  9836. (org-search-not-self 1 re2 nil t)
  9837. (org-search-not-self 1 re2a nil t)
  9838. (org-search-not-self 1 re3 nil t)
  9839. (org-search-not-self 1 re4 nil t)
  9840. (org-search-not-self 1 re5 nil t))
  9841. (goto-char (match-beginning 1))
  9842. (goto-char pos)
  9843. (error "No match"))))))
  9844. (and (derived-mode-p 'org-mode)
  9845. (not stealth)
  9846. (org-show-context 'link-search))
  9847. type))
  9848. (defun org-search-not-self (group &rest args)
  9849. "Execute `re-search-forward', but only accept matches that do not
  9850. enclose the position of `org-open-link-marker'."
  9851. (let ((m org-open-link-marker))
  9852. (catch 'exit
  9853. (while (apply 're-search-forward args)
  9854. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  9855. (goto-char (match-end group))
  9856. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  9857. (> (match-beginning 0) (marker-position m))
  9858. (< (match-end 0) (marker-position m)))
  9859. (save-match-data
  9860. (or (not (org-in-regexp
  9861. org-bracket-link-analytic-regexp 1))
  9862. (not (match-end 4)) ; no description
  9863. (and (<= (match-beginning 4) (point))
  9864. (>= (match-end 4) (point))))))
  9865. (throw 'exit (point))))))))
  9866. (defun org-get-buffer-for-internal-link (buffer)
  9867. "Return a buffer to be used for displaying the link target of internal links."
  9868. (cond
  9869. ((not org-display-internal-link-with-indirect-buffer)
  9870. buffer)
  9871. ((string-match "(Clone)$" (buffer-name buffer))
  9872. (message "Buffer is already a clone, not making another one")
  9873. ;; we also do not modify visibility in this case
  9874. buffer)
  9875. (t ; make a new indirect buffer for displaying the link
  9876. (let* ((bn (buffer-name buffer))
  9877. (ibn (concat bn "(Clone)"))
  9878. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  9879. (with-current-buffer ib (org-overview))
  9880. ib))))
  9881. (defun org-do-occur (regexp &optional cleanup)
  9882. "Call the Emacs command `occur'.
  9883. If CLEANUP is non-nil, remove the printout of the regular expression
  9884. in the *Occur* buffer. This is useful if the regex is long and not useful
  9885. to read."
  9886. (occur regexp)
  9887. (when cleanup
  9888. (let ((cwin (selected-window)) win beg end)
  9889. (when (setq win (get-buffer-window "*Occur*"))
  9890. (select-window win))
  9891. (goto-char (point-min))
  9892. (when (re-search-forward "match[a-z]+" nil t)
  9893. (setq beg (match-end 0))
  9894. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  9895. (setq end (1- (match-beginning 0)))))
  9896. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  9897. (goto-char (point-min))
  9898. (select-window cwin))))
  9899. ;;; The mark ring for links jumps
  9900. (defvar org-mark-ring nil
  9901. "Mark ring for positions before jumps in Org-mode.")
  9902. (defvar org-mark-ring-last-goto nil
  9903. "Last position in the mark ring used to go back.")
  9904. ;; Fill and close the ring
  9905. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  9906. (loop for i from 1 to org-mark-ring-length do
  9907. (push (make-marker) org-mark-ring))
  9908. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  9909. org-mark-ring)
  9910. (defun org-mark-ring-push (&optional pos buffer)
  9911. "Put the current position or POS into the mark ring and rotate it."
  9912. (interactive)
  9913. (setq pos (or pos (point)))
  9914. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  9915. (move-marker (car org-mark-ring)
  9916. (or pos (point))
  9917. (or buffer (current-buffer)))
  9918. (message "%s"
  9919. (substitute-command-keys
  9920. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  9921. (defun org-mark-ring-goto (&optional n)
  9922. "Jump to the previous position in the mark ring.
  9923. With prefix arg N, jump back that many stored positions. When
  9924. called several times in succession, walk through the entire ring.
  9925. Org-mode commands jumping to a different position in the current file,
  9926. or to another Org-mode file, automatically push the old position
  9927. onto the ring."
  9928. (interactive "p")
  9929. (let (p m)
  9930. (if (eq last-command this-command)
  9931. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  9932. (setq p org-mark-ring))
  9933. (setq org-mark-ring-last-goto p)
  9934. (setq m (car p))
  9935. (org-pop-to-buffer-same-window (marker-buffer m))
  9936. (goto-char m)
  9937. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  9938. (defun org-remove-angle-brackets (s)
  9939. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  9940. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  9941. s)
  9942. (defun org-add-angle-brackets (s)
  9943. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  9944. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  9945. s)
  9946. (defun org-remove-double-quotes (s)
  9947. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  9948. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  9949. s)
  9950. ;;; Following specific links
  9951. (defun org-follow-timestamp-link ()
  9952. "Open an agenda view for the time-stamp date/range at point."
  9953. (cond
  9954. ((org-at-date-range-p t)
  9955. (let ((org-agenda-start-on-weekday)
  9956. (t1 (match-string 1))
  9957. (t2 (match-string 2)) tt1 tt2)
  9958. (setq tt1 (time-to-days (org-time-string-to-time t1))
  9959. tt2 (time-to-days (org-time-string-to-time t2)))
  9960. (let ((org-agenda-buffer-tmp-name
  9961. (format "*Org Agenda(a:%s)"
  9962. (concat (substring t1 0 10) "--" (substring t2 0 10)))))
  9963. (org-agenda-list nil tt1 (1+ (- tt2 tt1))))))
  9964. ((org-at-timestamp-p t)
  9965. (let ((org-agenda-buffer-tmp-name
  9966. (format "*Org Agenda(a:%s)" (substring (match-string 1) 0 10))))
  9967. (org-agenda-list nil (time-to-days (org-time-string-to-time
  9968. (substring (match-string 1) 0 10)))
  9969. 1)))
  9970. (t (error "This should not happen"))))
  9971. ;;; Following file links
  9972. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  9973. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  9974. (declare-function mailcap-mime-info
  9975. "mailcap" (string &optional request no-decode))
  9976. (defvar org-wait nil)
  9977. (defun org-open-file (path &optional in-emacs line search)
  9978. "Open the file at PATH.
  9979. First, this expands any special file name abbreviations. Then the
  9980. configuration variable `org-file-apps' is checked if it contains an
  9981. entry for this file type, and if yes, the corresponding command is launched.
  9982. If no application is found, Emacs simply visits the file.
  9983. With optional prefix argument IN-EMACS, Emacs will visit the file.
  9984. With a double \\[universal-argument] \\[universal-argument] \
  9985. prefix arg, Org tries to avoid opening in Emacs
  9986. and to use an external application to visit the file.
  9987. Optional LINE specifies a line to go to, optional SEARCH a string
  9988. to search for. If LINE or SEARCH is given, the file will be
  9989. opened in Emacs, unless an entry from org-file-apps that makes
  9990. use of groups in a regexp matches.
  9991. If you want to change the way frames are used when following a
  9992. link, please customize `org-link-frame-setup'.
  9993. If the file does not exist, an error is thrown."
  9994. (let* ((file (if (equal path "")
  9995. buffer-file-name
  9996. (substitute-in-file-name (expand-file-name path))))
  9997. (file-apps (append org-file-apps (org-default-apps)))
  9998. (apps (org-remove-if
  9999. 'org-file-apps-entry-match-against-dlink-p file-apps))
  10000. (apps-dlink (org-remove-if-not
  10001. 'org-file-apps-entry-match-against-dlink-p file-apps))
  10002. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  10003. (dirp (if remp nil (file-directory-p file)))
  10004. (file (if (and dirp org-open-directory-means-index-dot-org)
  10005. (concat (file-name-as-directory file) "index.org")
  10006. file))
  10007. (a-m-a-p (assq 'auto-mode apps))
  10008. (dfile (downcase file))
  10009. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  10010. (link (cond ((and (eq line nil)
  10011. (eq search nil))
  10012. file)
  10013. (line
  10014. (concat file "::" (number-to-string line)))
  10015. (search
  10016. (concat file "::" search))))
  10017. (dlink (downcase link))
  10018. (old-buffer (current-buffer))
  10019. (old-pos (point))
  10020. (old-mode major-mode)
  10021. ext cmd link-match-data)
  10022. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  10023. (setq ext (match-string 1 dfile))
  10024. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  10025. (setq ext (match-string 1 dfile))))
  10026. (cond
  10027. ((member in-emacs '((16) system))
  10028. (setq cmd (cdr (assoc 'system apps))))
  10029. (in-emacs (setq cmd 'emacs))
  10030. (t
  10031. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  10032. (and dirp (cdr (assoc 'directory apps)))
  10033. ; first, try matching against apps-dlink
  10034. ; if we get a match here, store the match data for later
  10035. (let ((match (assoc-default dlink apps-dlink
  10036. 'string-match)))
  10037. (if match
  10038. (progn (setq link-match-data (match-data))
  10039. match)
  10040. (progn (setq in-emacs (or in-emacs line search))
  10041. nil))) ; if we have no match in apps-dlink,
  10042. ; always open the file in emacs if line or search
  10043. ; is given (for backwards compatibility)
  10044. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  10045. 'string-match)
  10046. (cdr (assoc ext apps))
  10047. (cdr (assoc t apps))))))
  10048. (when (eq cmd 'system)
  10049. (setq cmd (cdr (assoc 'system apps))))
  10050. (when (eq cmd 'default)
  10051. (setq cmd (cdr (assoc t apps))))
  10052. (when (eq cmd 'mailcap)
  10053. (require 'mailcap)
  10054. (mailcap-parse-mailcaps)
  10055. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  10056. (command (mailcap-mime-info mime-type)))
  10057. (if (stringp command)
  10058. (setq cmd command)
  10059. (setq cmd 'emacs))))
  10060. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  10061. (not (file-exists-p file))
  10062. (not org-open-non-existing-files))
  10063. (user-error "No such file: %s" file))
  10064. (cond
  10065. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  10066. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  10067. (while (string-match "['\"]%s['\"]" cmd)
  10068. (setq cmd (replace-match "%s" t t cmd)))
  10069. (while (string-match "%s" cmd)
  10070. (setq cmd (replace-match
  10071. (save-match-data
  10072. (shell-quote-argument
  10073. (convert-standard-filename file)))
  10074. t t cmd)))
  10075. ;; Replace "%1", "%2" etc. in command with group matches from regex
  10076. (save-match-data
  10077. (let ((match-index 1)
  10078. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  10079. (set-match-data link-match-data)
  10080. (while (<= match-index number-of-groups)
  10081. (let ((regex (concat "%" (number-to-string match-index)))
  10082. (replace-with (match-string match-index dlink)))
  10083. (while (string-match regex cmd)
  10084. (setq cmd (replace-match replace-with t t cmd))))
  10085. (setq match-index (+ match-index 1)))))
  10086. (save-window-excursion
  10087. (message "Running %s...done" cmd)
  10088. (start-process-shell-command cmd nil cmd)
  10089. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))))
  10090. ((or (stringp cmd)
  10091. (eq cmd 'emacs))
  10092. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  10093. (widen)
  10094. (if line (org-goto-line line)
  10095. (if search (org-link-search search))))
  10096. ((consp cmd)
  10097. (let ((file (convert-standard-filename file)))
  10098. (save-match-data
  10099. (set-match-data link-match-data)
  10100. (eval cmd))))
  10101. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  10102. (and (derived-mode-p 'org-mode) (eq old-mode 'org-mode)
  10103. (or (not (equal old-buffer (current-buffer)))
  10104. (not (equal old-pos (point))))
  10105. (org-mark-ring-push old-pos old-buffer))))
  10106. (defun org-file-apps-entry-match-against-dlink-p (entry)
  10107. "This function returns non-nil if `entry' uses a regular
  10108. expression which should be matched against the whole link by
  10109. org-open-file.
  10110. It assumes that is the case when the entry uses a regular
  10111. expression which has at least one grouping construct and the
  10112. action is either a lisp form or a command string containing
  10113. '%1', i.e. using at least one subexpression match as a
  10114. parameter."
  10115. (let ((selector (car entry))
  10116. (action (cdr entry)))
  10117. (if (stringp selector)
  10118. (and (> (regexp-opt-depth selector) 0)
  10119. (or (and (stringp action)
  10120. (string-match "%[0-9]" action))
  10121. (consp action)))
  10122. nil)))
  10123. (defun org-default-apps ()
  10124. "Return the default applications for this operating system."
  10125. (cond
  10126. ((eq system-type 'darwin)
  10127. org-file-apps-defaults-macosx)
  10128. ((eq system-type 'windows-nt)
  10129. org-file-apps-defaults-windowsnt)
  10130. (t org-file-apps-defaults-gnu)))
  10131. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  10132. "Convert extensions to regular expressions in the cars of LIST.
  10133. Also, weed out any non-string entries, because the return value is used
  10134. only for regexp matching.
  10135. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  10136. point to the symbol `emacs', indicating that the file should
  10137. be opened in Emacs."
  10138. (append
  10139. (delq nil
  10140. (mapcar (lambda (x)
  10141. (if (not (stringp (car x)))
  10142. nil
  10143. (if (string-match "\\W" (car x))
  10144. x
  10145. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  10146. list))
  10147. (if add-auto-mode
  10148. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  10149. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  10150. (defun org-file-remote-p (file)
  10151. "Test whether FILE specifies a location on a remote system.
  10152. Return non-nil if the location is indeed remote.
  10153. For example, the filename \"/user@host:/foo\" specifies a location
  10154. on the system \"/user@host:\"."
  10155. (cond ((fboundp 'file-remote-p)
  10156. (file-remote-p file))
  10157. ((fboundp 'tramp-handle-file-remote-p)
  10158. (tramp-handle-file-remote-p file))
  10159. ((and (boundp 'ange-ftp-name-format)
  10160. (string-match (car ange-ftp-name-format) file))
  10161. t)))
  10162. ;;;; Refiling
  10163. (defun org-get-org-file ()
  10164. "Read a filename, with default directory `org-directory'."
  10165. (let ((default (or org-default-notes-file remember-data-file)))
  10166. (read-file-name (format "File name [%s]: " default)
  10167. (file-name-as-directory org-directory)
  10168. default)))
  10169. (defun org-notes-order-reversed-p ()
  10170. "Check if the current file should receive notes in reversed order."
  10171. (cond
  10172. ((not org-reverse-note-order) nil)
  10173. ((eq t org-reverse-note-order) t)
  10174. ((not (listp org-reverse-note-order)) nil)
  10175. (t (catch 'exit
  10176. (let ((all org-reverse-note-order)
  10177. entry)
  10178. (while (setq entry (pop all))
  10179. (if (string-match (car entry) buffer-file-name)
  10180. (throw 'exit (cdr entry))))
  10181. nil)))))
  10182. (defvar org-refile-target-table nil
  10183. "The list of refile targets, created by `org-refile'.")
  10184. (defvar org-agenda-new-buffers nil
  10185. "Buffers created to visit agenda files.")
  10186. (defvar org-refile-cache nil
  10187. "Cache for refile targets.")
  10188. (defvar org-refile-markers nil
  10189. "All the markers used for caching refile locations.")
  10190. (defun org-refile-marker (pos)
  10191. "Get a new refile marker, but only if caching is in use."
  10192. (if (not org-refile-use-cache)
  10193. pos
  10194. (let ((m (make-marker)))
  10195. (move-marker m pos)
  10196. (push m org-refile-markers)
  10197. m)))
  10198. (defun org-refile-cache-clear ()
  10199. "Clear the refile cache and disable all the markers."
  10200. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  10201. (setq org-refile-markers nil)
  10202. (setq org-refile-cache nil)
  10203. (message "Refile cache has been cleared"))
  10204. (defun org-refile-cache-check-set (set)
  10205. "Check if all the markers in the cache still have live buffers."
  10206. (let (marker)
  10207. (catch 'exit
  10208. (while (and set (setq marker (nth 3 (pop set))))
  10209. ;; If `org-refile-use-outline-path' is 'file, marker may be nil
  10210. (when (and marker (null (marker-buffer marker)))
  10211. (message "Please regenerate the refile cache with `C-0 C-c C-w'")
  10212. (sit-for 3)
  10213. (throw 'exit nil)))
  10214. t)))
  10215. (defun org-refile-cache-put (set &rest identifiers)
  10216. "Push the refile targets SET into the cache, under IDENTIFIERS."
  10217. (let* ((key (sha1 (prin1-to-string identifiers)))
  10218. (entry (assoc key org-refile-cache)))
  10219. (if entry
  10220. (setcdr entry set)
  10221. (push (cons key set) org-refile-cache))))
  10222. (defun org-refile-cache-get (&rest identifiers)
  10223. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  10224. (cond
  10225. ((not org-refile-cache) nil)
  10226. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  10227. (t
  10228. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  10229. org-refile-cache))))
  10230. (and set (org-refile-cache-check-set set) set)))))
  10231. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  10232. "Produce a table with refile targets."
  10233. (let ((case-fold-search nil)
  10234. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  10235. (entries (or org-refile-targets '((nil . (:level . 1)))))
  10236. targets tgs txt re files f desc descre fast-path-p level pos0)
  10237. (message "Getting targets...")
  10238. (with-current-buffer (or default-buffer (current-buffer))
  10239. (while (setq entry (pop entries))
  10240. (setq files (car entry) desc (cdr entry))
  10241. (setq fast-path-p nil)
  10242. (cond
  10243. ((null files) (setq files (list (current-buffer))))
  10244. ((eq files 'org-agenda-files)
  10245. (setq files (org-agenda-files 'unrestricted)))
  10246. ((and (symbolp files) (fboundp files))
  10247. (setq files (funcall files)))
  10248. ((and (symbolp files) (boundp files))
  10249. (setq files (symbol-value files))))
  10250. (if (stringp files) (setq files (list files)))
  10251. (cond
  10252. ((eq (car desc) :tag)
  10253. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  10254. ((eq (car desc) :todo)
  10255. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  10256. ((eq (car desc) :regexp)
  10257. (setq descre (cdr desc)))
  10258. ((eq (car desc) :level)
  10259. (setq descre (concat "^\\*\\{" (number-to-string
  10260. (if org-odd-levels-only
  10261. (1- (* 2 (cdr desc)))
  10262. (cdr desc)))
  10263. "\\}[ \t]")))
  10264. ((eq (car desc) :maxlevel)
  10265. (setq fast-path-p t)
  10266. (setq descre (concat "^\\*\\{1," (number-to-string
  10267. (if org-odd-levels-only
  10268. (1- (* 2 (cdr desc)))
  10269. (cdr desc)))
  10270. "\\}[ \t]")))
  10271. (t (error "Bad refiling target description %s" desc)))
  10272. (while (setq f (pop files))
  10273. (with-current-buffer
  10274. (if (bufferp f) f (org-get-agenda-file-buffer f))
  10275. (or
  10276. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  10277. (progn
  10278. (if (bufferp f) (setq f (buffer-file-name
  10279. (buffer-base-buffer f))))
  10280. (setq f (and f (expand-file-name f)))
  10281. (if (eq org-refile-use-outline-path 'file)
  10282. (push (list (file-name-nondirectory f) f nil nil) tgs))
  10283. (save-excursion
  10284. (save-restriction
  10285. (widen)
  10286. (goto-char (point-min))
  10287. (while (re-search-forward descre nil t)
  10288. (goto-char (setq pos0 (point-at-bol)))
  10289. (catch 'next
  10290. (when org-refile-target-verify-function
  10291. (save-match-data
  10292. (or (funcall org-refile-target-verify-function)
  10293. (throw 'next t))))
  10294. (when (and (looking-at org-complex-heading-regexp)
  10295. (not (member (match-string 4) excluded-entries))
  10296. (match-string 4))
  10297. (setq level (org-reduced-level
  10298. (- (match-end 1) (match-beginning 1)))
  10299. txt (org-link-display-format (match-string 4))
  10300. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  10301. re (format org-complex-heading-regexp-format
  10302. (regexp-quote (match-string 4))))
  10303. (when org-refile-use-outline-path
  10304. (setq txt (mapconcat
  10305. 'org-protect-slash
  10306. (append
  10307. (if (eq org-refile-use-outline-path
  10308. 'file)
  10309. (list (file-name-nondirectory
  10310. (buffer-file-name
  10311. (buffer-base-buffer))))
  10312. (if (eq org-refile-use-outline-path
  10313. 'full-file-path)
  10314. (list (buffer-file-name
  10315. (buffer-base-buffer)))))
  10316. (org-get-outline-path fast-path-p
  10317. level txt)
  10318. (list txt))
  10319. "/")))
  10320. (push (list txt f re (org-refile-marker (point)))
  10321. tgs)))
  10322. (when (= (point) pos0)
  10323. ;; verification function has not moved point
  10324. (goto-char (point-at-eol))))))))
  10325. (when org-refile-use-cache
  10326. (org-refile-cache-put tgs (buffer-file-name) descre))
  10327. (setq targets (append tgs targets))))))
  10328. (message "Getting targets...done")
  10329. (nreverse targets)))
  10330. (defun org-protect-slash (s)
  10331. (while (string-match "/" s)
  10332. (setq s (replace-match "\\" t t s)))
  10333. s)
  10334. (defvar org-olpa (make-vector 20 nil))
  10335. (defun org-get-outline-path (&optional fastp level heading)
  10336. "Return the outline path to the current entry, as a list.
  10337. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  10338. routine which makes outline path derivations for an entire file,
  10339. avoiding backtracing. Refile target collection makes use of that."
  10340. (if fastp
  10341. (progn
  10342. (if (> level 19)
  10343. (error "Outline path failure, more than 19 levels"))
  10344. (loop for i from level upto 19 do
  10345. (aset org-olpa i nil))
  10346. (prog1
  10347. (delq nil (append org-olpa nil))
  10348. (aset org-olpa level heading)))
  10349. (let (rtn case-fold-search)
  10350. (save-excursion
  10351. (save-restriction
  10352. (widen)
  10353. (while (org-up-heading-safe)
  10354. (when (looking-at org-complex-heading-regexp)
  10355. (push (org-trim
  10356. (replace-regexp-in-string
  10357. ;; Remove statistical/checkboxes cookies
  10358. "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" ""
  10359. (org-match-string-no-properties 4)))
  10360. rtn)))
  10361. rtn)))))
  10362. (defun org-format-outline-path (path &optional width prefix separator)
  10363. "Format the outline path PATH for display.
  10364. WIDTH is the maximum number of characters that is available.
  10365. PREFIX is a prefix to be included in the returned string,
  10366. such as the file name.
  10367. SEPARATOR is inserted between the different parts of the path,
  10368. the default is \"/\"."
  10369. (setq width (or width 79))
  10370. (if prefix (setq width (- width (length prefix))))
  10371. (if (not path)
  10372. (or prefix "")
  10373. (let* ((nsteps (length path))
  10374. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  10375. (maxwidth (if (<= total-width width)
  10376. 10000 ;; everything fits
  10377. ;; we need to shorten the level headings
  10378. (/ (- width nsteps) nsteps)))
  10379. (org-odd-levels-only nil)
  10380. (n 0)
  10381. (total (1+ (length prefix))))
  10382. (setq maxwidth (max maxwidth 10))
  10383. (concat prefix
  10384. (if prefix (or separator "/"))
  10385. (mapconcat
  10386. (lambda (h)
  10387. (setq n (1+ n))
  10388. (if (and (= n nsteps) (< maxwidth 10000))
  10389. (setq maxwidth (- total-width total)))
  10390. (if (< (length h) maxwidth)
  10391. (progn (setq total (+ total (length h) 1)) h)
  10392. (setq h (substring h 0 (- maxwidth 2))
  10393. total (+ total maxwidth 1))
  10394. (if (string-match "[ \t]+\\'" h)
  10395. (setq h (substring h 0 (match-beginning 0))))
  10396. (setq h (concat h "..")))
  10397. (org-add-props h nil 'face
  10398. (nth (% (1- n) org-n-level-faces)
  10399. org-level-faces))
  10400. h)
  10401. path (or separator "/"))))))
  10402. (defun org-display-outline-path (&optional file current separator just-return-string)
  10403. "Display the current outline path in the echo area.
  10404. If FILE is non-nil, prepend the output with the file name.
  10405. If CURRENT is non-nil, append the current heading to the output.
  10406. SEPARATOR is passed through to `org-format-outline-path'. It separates
  10407. the different parts of the path and defaults to \"/\".
  10408. If JUST-RETURN-STRING is non-nil, return a string, don't display a message."
  10409. (interactive "P")
  10410. (let* (case-fold-search
  10411. (bfn (buffer-file-name (buffer-base-buffer)))
  10412. (path (and (derived-mode-p 'org-mode) (org-get-outline-path)))
  10413. res)
  10414. (if current (setq path (append path
  10415. (save-excursion
  10416. (org-back-to-heading t)
  10417. (if (looking-at org-complex-heading-regexp)
  10418. (list (match-string 4)))))))
  10419. (setq res
  10420. (org-format-outline-path
  10421. path
  10422. (1- (frame-width))
  10423. (and file bfn (concat (file-name-nondirectory bfn) separator))
  10424. separator))
  10425. (if just-return-string
  10426. (org-no-properties res)
  10427. (org-unlogged-message "%s" res))))
  10428. (defvar org-refile-history nil
  10429. "History for refiling operations.")
  10430. (defvar org-after-refile-insert-hook nil
  10431. "Hook run after `org-refile' has inserted its stuff at the new location.
  10432. Note that this is still *before* the stuff will be removed from
  10433. the *old* location.")
  10434. (defvar org-capture-last-stored-marker)
  10435. (defvar org-refile-keep nil
  10436. "Non-nil means `org-refile' will copy instead of refile.")
  10437. (defun org-copy ()
  10438. "Like `org-refile', but copy."
  10439. (interactive)
  10440. (let ((org-refile-keep t))
  10441. (funcall 'org-refile nil nil nil "Copy")))
  10442. (defun org-refile (&optional goto default-buffer rfloc msg)
  10443. "Move the entry or entries at point to another heading.
  10444. The list of target headings is compiled using the information in
  10445. `org-refile-targets', which see.
  10446. At the target location, the entry is filed as a subitem of the target
  10447. heading. Depending on `org-reverse-note-order', the new subitem will
  10448. either be the first or the last subitem.
  10449. If there is an active region, all entries in that region will be moved.
  10450. However, the region must fulfill the requirement that the first heading
  10451. is the first one sets the top-level of the moved text - at most siblings
  10452. below it are allowed.
  10453. With prefix arg GOTO, the command will only visit the target location
  10454. and not actually move anything.
  10455. With a double prefix arg \\[universal-argument] \\[universal-argument], \
  10456. go to the location where the last refiling operation has put the subtree.
  10457. With a numeric prefix argument of `2', refile to the running clock.
  10458. With a numeric prefix argument of `3', emulate `org-refile-keep'
  10459. being set to `t' and copy to the target location, don't move it.
  10460. Beware that keeping refiled entries may result in duplicated ID
  10461. properties.
  10462. RFLOC can be a refile location obtained in a different way.
  10463. MSG is a string to replace \"Refile\" in the default prompt with
  10464. another verb. E.g. `org-copy' sets this parameter to \"Copy\".
  10465. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  10466. If you are using target caching (see `org-refile-use-cache'),
  10467. you have to clear the target cache in order to find new targets.
  10468. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  10469. prefix argument (`C-u C-u C-u C-c C-w')."
  10470. (interactive "P")
  10471. (if (member goto '(0 (64)))
  10472. (org-refile-cache-clear)
  10473. (let* ((actionmsg (cond (msg msg)
  10474. ((equal goto 3) "Refile (and keep)")
  10475. (t "Refile")))
  10476. (cbuf (current-buffer))
  10477. (regionp (org-region-active-p))
  10478. (region-start (and regionp (region-beginning)))
  10479. (region-end (and regionp (region-end)))
  10480. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  10481. (org-refile-keep (if (equal goto 3) t org-refile-keep))
  10482. pos it nbuf file re level reversed)
  10483. (setq last-command nil)
  10484. (when regionp
  10485. (goto-char region-start)
  10486. (or (bolp) (goto-char (point-at-bol)))
  10487. (setq region-start (point))
  10488. (unless (or (org-kill-is-subtree-p
  10489. (buffer-substring region-start region-end))
  10490. (prog1 org-refile-active-region-within-subtree
  10491. (let ((s (point-at-eol)))
  10492. (org-toggle-heading)
  10493. (setq region-end (+ (- (point-at-eol) s) region-end)))))
  10494. (user-error "The region is not a (sequence of) subtree(s)")))
  10495. (if (equal goto '(16))
  10496. (org-refile-goto-last-stored)
  10497. (when (or
  10498. (and (equal goto 2)
  10499. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  10500. (prog1
  10501. (setq it (list (or org-clock-heading "running clock")
  10502. (buffer-file-name
  10503. (marker-buffer org-clock-hd-marker))
  10504. ""
  10505. (marker-position org-clock-hd-marker)))
  10506. (setq goto nil)))
  10507. (setq it (or rfloc
  10508. (let (heading-text)
  10509. (save-excursion
  10510. (unless (and goto (listp goto))
  10511. (org-back-to-heading t)
  10512. (setq heading-text
  10513. (nth 4 (org-heading-components))))
  10514. (org-refile-get-location
  10515. (cond ((and goto (listp goto)) "Goto")
  10516. (regionp (concat actionmsg " region to"))
  10517. (t (concat actionmsg " subtree \""
  10518. heading-text "\" to")))
  10519. default-buffer
  10520. (and (not (equal '(4) goto))
  10521. org-refile-allow-creating-parent-nodes)
  10522. goto))))))
  10523. (setq file (nth 1 it)
  10524. re (nth 2 it)
  10525. pos (nth 3 it))
  10526. (if (and (not goto)
  10527. pos
  10528. (equal (buffer-file-name) file)
  10529. (if regionp
  10530. (and (>= pos region-start)
  10531. (<= pos region-end))
  10532. (and (>= pos (point))
  10533. (< pos (save-excursion
  10534. (org-end-of-subtree t t))))))
  10535. (error "Cannot refile to position inside the tree or region"))
  10536. (setq nbuf (or (find-buffer-visiting file)
  10537. (find-file-noselect file)))
  10538. (if (and goto (not (equal goto 3)))
  10539. (progn
  10540. (org-pop-to-buffer-same-window nbuf)
  10541. (goto-char pos)
  10542. (org-show-context 'org-goto))
  10543. (if regionp
  10544. (progn
  10545. (org-kill-new (buffer-substring region-start region-end))
  10546. (org-save-markers-in-region region-start region-end))
  10547. (org-copy-subtree 1 nil t))
  10548. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  10549. (find-file-noselect file)))
  10550. (setq reversed (org-notes-order-reversed-p))
  10551. (save-excursion
  10552. (save-restriction
  10553. (widen)
  10554. (if pos
  10555. (progn
  10556. (goto-char pos)
  10557. (looking-at org-outline-regexp)
  10558. (setq level (org-get-valid-level (funcall outline-level) 1))
  10559. (goto-char
  10560. (if reversed
  10561. (or (outline-next-heading) (point-max))
  10562. (or (save-excursion (org-get-next-sibling))
  10563. (org-end-of-subtree t t)
  10564. (point-max)))))
  10565. (setq level 1)
  10566. (if (not reversed)
  10567. (goto-char (point-max))
  10568. (goto-char (point-min))
  10569. (or (outline-next-heading) (goto-char (point-max)))))
  10570. (if (not (bolp)) (newline))
  10571. (org-paste-subtree level)
  10572. (when org-log-refile
  10573. (org-add-log-setup 'refile nil nil 'findpos org-log-refile)
  10574. (unless (eq org-log-refile 'note)
  10575. (save-excursion (org-add-log-note))))
  10576. (and org-auto-align-tags
  10577. (let ((org-loop-over-headlines-in-active-region nil))
  10578. (org-set-tags nil t)))
  10579. (let ((bookmark-name (plist-get org-bookmark-names-plist
  10580. :last-refile)))
  10581. (when bookmark-name
  10582. (with-demoted-errors
  10583. (bookmark-set bookmark-name))))
  10584. ;; If we are refiling for capture, make sure that the
  10585. ;; last-capture pointers point here
  10586. (when (org-bound-and-true-p org-refile-for-capture)
  10587. (let ((bookmark-name (plist-get org-bookmark-names-plist
  10588. :last-capture-marker)))
  10589. (when bookmark-name
  10590. (with-demoted-errors
  10591. (bookmark-set bookmark-name))))
  10592. (move-marker org-capture-last-stored-marker (point)))
  10593. (if (fboundp 'deactivate-mark) (deactivate-mark))
  10594. (run-hooks 'org-after-refile-insert-hook))))
  10595. (unless org-refile-keep
  10596. (if regionp
  10597. (delete-region (point) (+ (point) (- region-end region-start)))
  10598. (delete-region
  10599. (and (org-back-to-heading t) (point))
  10600. (min (1+ (buffer-size)) (org-end-of-subtree t t) (point)))))
  10601. (when (featurep 'org-inlinetask)
  10602. (org-inlinetask-remove-END-maybe))
  10603. (setq org-markers-to-move nil)
  10604. (message (concat actionmsg " to \"%s\" in file %s: done") (car it) file)))))))
  10605. (defun org-refile-goto-last-stored ()
  10606. "Go to the location where the last refile was stored."
  10607. (interactive)
  10608. (bookmark-jump "org-refile-last-stored")
  10609. (message "This is the location of the last refile"))
  10610. (defun org-refile--get-location (refloc tbl)
  10611. "When user refile to REFLOC, find the associated target in TBL.
  10612. Also check `org-refile-target-table'."
  10613. (car (delq
  10614. nil
  10615. (mapcar
  10616. (lambda (r) (or (assoc r tbl)
  10617. (assoc r org-refile-target-table)))
  10618. (list (replace-regexp-in-string "/$" "" refloc)
  10619. (replace-regexp-in-string "\\([^/]\\)$" "\\1/" refloc))))))
  10620. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  10621. no-exclude)
  10622. "Prompt the user for a refile location, using PROMPT.
  10623. PROMPT should not be suffixed with a colon and a space, because
  10624. this function appends the default value from
  10625. `org-refile-history' automatically, if that is not empty.
  10626. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  10627. this is used for the GOTO interface."
  10628. (let ((org-refile-targets org-refile-targets)
  10629. (org-refile-use-outline-path org-refile-use-outline-path)
  10630. excluded-entries)
  10631. (when (and (derived-mode-p 'org-mode)
  10632. (not org-refile-use-cache)
  10633. (not no-exclude))
  10634. (org-map-tree
  10635. (lambda()
  10636. (setq excluded-entries
  10637. (append excluded-entries (list (org-get-heading t t)))))))
  10638. (setq org-refile-target-table
  10639. (org-refile-get-targets default-buffer excluded-entries)))
  10640. (unless org-refile-target-table
  10641. (user-error "No refile targets"))
  10642. (let* ((cbuf (current-buffer))
  10643. (partial-completion-mode nil)
  10644. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  10645. (cfunc (if (and org-refile-use-outline-path
  10646. org-outline-path-complete-in-steps)
  10647. 'org-olpath-completing-read
  10648. 'org-icompleting-read))
  10649. (extra (if org-refile-use-outline-path "/" ""))
  10650. (cbnex (concat (buffer-name) extra))
  10651. (filename (and cfn (expand-file-name cfn)))
  10652. (tbl (mapcar
  10653. (lambda (x)
  10654. (if (and (not (member org-refile-use-outline-path
  10655. '(file full-file-path)))
  10656. (not (equal filename (nth 1 x))))
  10657. (cons (concat (car x) extra " ("
  10658. (file-name-nondirectory (nth 1 x)) ")")
  10659. (cdr x))
  10660. (cons (concat (car x) extra) (cdr x))))
  10661. org-refile-target-table))
  10662. (completion-ignore-case t)
  10663. cdef
  10664. (prompt (concat prompt
  10665. (or (and (car org-refile-history)
  10666. (concat " (default " (car org-refile-history) ")"))
  10667. (and (assoc cbnex tbl) (setq cdef cbnex)
  10668. (concat " (default " cbnex ")"))) ": "))
  10669. pa answ parent-target child parent old-hist)
  10670. (setq old-hist org-refile-history)
  10671. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  10672. nil 'org-refile-history (or cdef (car org-refile-history))))
  10673. (if (setq pa (org-refile--get-location answ tbl))
  10674. (progn
  10675. (org-refile-check-position pa)
  10676. (when (or (not org-refile-history)
  10677. (not (eq old-hist org-refile-history))
  10678. (not (equal (car pa) (car org-refile-history))))
  10679. (setq org-refile-history
  10680. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  10681. org-refile-history
  10682. (cdr org-refile-history))))
  10683. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  10684. (pop org-refile-history)))
  10685. pa)
  10686. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  10687. (progn
  10688. (setq parent (match-string 1 answ)
  10689. child (match-string 2 answ))
  10690. (setq parent-target (org-refile--get-location parent tbl))
  10691. (when (and parent-target
  10692. (or (eq new-nodes t)
  10693. (and (eq new-nodes 'confirm)
  10694. (y-or-n-p (format "Create new node \"%s\"? "
  10695. child)))))
  10696. (org-refile-new-child parent-target child)))
  10697. (user-error "Invalid target location")))))
  10698. (declare-function org-string-nw-p "org-macs" (s))
  10699. (defun org-refile-check-position (refile-pointer)
  10700. "Check if the refile pointer matches the headline to which it points."
  10701. (let* ((file (nth 1 refile-pointer))
  10702. (re (nth 2 refile-pointer))
  10703. (pos (nth 3 refile-pointer))
  10704. buffer)
  10705. (if (and (not (markerp pos)) (not file))
  10706. (user-error "Please save the buffer to a file before refiling")
  10707. (when (org-string-nw-p re)
  10708. (setq buffer (if (markerp pos)
  10709. (marker-buffer pos)
  10710. (or (find-buffer-visiting file)
  10711. (find-file-noselect file))))
  10712. (with-current-buffer buffer
  10713. (save-excursion
  10714. (save-restriction
  10715. (widen)
  10716. (goto-char pos)
  10717. (beginning-of-line 1)
  10718. (unless (org-looking-at-p re)
  10719. (user-error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling")))))))))
  10720. (defun org-refile-new-child (parent-target child)
  10721. "Use refile target PARENT-TARGET to add new CHILD below it."
  10722. (unless parent-target
  10723. (error "Cannot find parent for new node"))
  10724. (let ((file (nth 1 parent-target))
  10725. (pos (nth 3 parent-target))
  10726. level)
  10727. (with-current-buffer (or (find-buffer-visiting file)
  10728. (find-file-noselect file))
  10729. (save-excursion
  10730. (save-restriction
  10731. (widen)
  10732. (if pos
  10733. (goto-char pos)
  10734. (goto-char (point-max))
  10735. (if (not (bolp)) (newline)))
  10736. (when (looking-at org-outline-regexp)
  10737. (setq level (funcall outline-level))
  10738. (org-end-of-subtree t t))
  10739. (org-back-over-empty-lines)
  10740. (insert "\n" (make-string
  10741. (if pos (org-get-valid-level level 1) 1) ?*)
  10742. " " child "\n")
  10743. (beginning-of-line 0)
  10744. (list (concat (car parent-target) "/" child) file "" (point)))))))
  10745. (defun org-olpath-completing-read (prompt collection &rest args)
  10746. "Read an outline path like a file name."
  10747. (let ((thetable collection)
  10748. (org-completion-use-ido nil) ; does not work with ido.
  10749. (org-completion-use-iswitchb nil)) ; or iswitchb
  10750. (apply
  10751. 'org-icompleting-read prompt
  10752. (lambda (string predicate &optional flag)
  10753. (let (rtn r f (l (length string)))
  10754. (cond
  10755. ((eq flag nil)
  10756. ;; try completion
  10757. (try-completion string thetable))
  10758. ((eq flag t)
  10759. ;; all-completions
  10760. (setq rtn (all-completions string thetable predicate))
  10761. (mapcar
  10762. (lambda (x)
  10763. (setq r (substring x l))
  10764. (if (string-match " ([^)]*)$" x)
  10765. (setq f (match-string 0 x))
  10766. (setq f ""))
  10767. (if (string-match "/" r)
  10768. (concat string (substring r 0 (match-end 0)) f)
  10769. x))
  10770. rtn))
  10771. ((eq flag 'lambda)
  10772. ;; exact match?
  10773. (assoc string thetable)))))
  10774. args)))
  10775. ;;;; Dynamic blocks
  10776. (defun org-find-dblock (name)
  10777. "Find the first dynamic block with name NAME in the buffer.
  10778. If not found, stay at current position and return nil."
  10779. (let ((case-fold-search t) pos)
  10780. (save-excursion
  10781. (goto-char (point-min))
  10782. (setq pos (and (re-search-forward
  10783. (concat "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+" name "\\>") nil t)
  10784. (match-beginning 0))))
  10785. (if pos (goto-char pos))
  10786. pos))
  10787. (defconst org-dblock-start-re
  10788. "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  10789. "Matches the start line of a dynamic block, with parameters.")
  10790. (defconst org-dblock-end-re "^[ \t]*#\\+\\(?:END\\|end\\)\\([: \t\r\n]\\|$\\)"
  10791. "Matches the end of a dynamic block.")
  10792. (defun org-create-dblock (plist)
  10793. "Create a dynamic block section, with parameters taken from PLIST.
  10794. PLIST must contain a :name entry which is used as name of the block."
  10795. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  10796. (end-of-line 1)
  10797. (newline))
  10798. (let ((col (current-column))
  10799. (name (plist-get plist :name)))
  10800. (insert "#+BEGIN: " name)
  10801. (while plist
  10802. (if (eq (car plist) :name)
  10803. (setq plist (cddr plist))
  10804. (insert " " (prin1-to-string (pop plist)))))
  10805. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  10806. (beginning-of-line -2)))
  10807. (defun org-prepare-dblock ()
  10808. "Prepare dynamic block for refresh.
  10809. This empties the block, puts the cursor at the insert position and returns
  10810. the property list including an extra property :name with the block name."
  10811. (unless (looking-at org-dblock-start-re)
  10812. (user-error "Not at a dynamic block"))
  10813. (let* ((begdel (1+ (match-end 0)))
  10814. (name (org-no-properties (match-string 1)))
  10815. (params (append (list :name name)
  10816. (read (concat "(" (match-string 3) ")")))))
  10817. (save-excursion
  10818. (beginning-of-line 1)
  10819. (skip-chars-forward " \t")
  10820. (setq params (plist-put params :indentation-column (current-column))))
  10821. (unless (re-search-forward org-dblock-end-re nil t)
  10822. (error "Dynamic block not terminated"))
  10823. (setq params
  10824. (append params
  10825. (list :content (buffer-substring
  10826. begdel (match-beginning 0)))))
  10827. (delete-region begdel (match-beginning 0))
  10828. (goto-char begdel)
  10829. (open-line 1)
  10830. params))
  10831. (defun org-map-dblocks (&optional command)
  10832. "Apply COMMAND to all dynamic blocks in the current buffer.
  10833. If COMMAND is not given, use `org-update-dblock'."
  10834. (let ((cmd (or command 'org-update-dblock)))
  10835. (save-excursion
  10836. (goto-char (point-min))
  10837. (while (re-search-forward org-dblock-start-re nil t)
  10838. (goto-char (match-beginning 0))
  10839. (save-excursion
  10840. (condition-case nil
  10841. (funcall cmd)
  10842. (error (message "Error during update of dynamic block"))))
  10843. (unless (re-search-forward org-dblock-end-re nil t)
  10844. (error "Dynamic block not terminated"))))))
  10845. (defun org-dblock-update (&optional arg)
  10846. "User command for updating dynamic blocks.
  10847. Update the dynamic block at point. With prefix ARG, update all dynamic
  10848. blocks in the buffer."
  10849. (interactive "P")
  10850. (if arg
  10851. (org-update-all-dblocks)
  10852. (or (looking-at org-dblock-start-re)
  10853. (org-beginning-of-dblock))
  10854. (org-update-dblock)))
  10855. (defun org-update-dblock ()
  10856. "Update the dynamic block at point.
  10857. This means to empty the block, parse for parameters and then call
  10858. the correct writing function."
  10859. (interactive)
  10860. (save-excursion
  10861. (let* ((win (selected-window))
  10862. (pos (point))
  10863. (line (org-current-line))
  10864. (params (org-prepare-dblock))
  10865. (name (plist-get params :name))
  10866. (indent (plist-get params :indentation-column))
  10867. (cmd (intern (concat "org-dblock-write:" name))))
  10868. (message "Updating dynamic block `%s' at line %d..." name line)
  10869. (funcall cmd params)
  10870. (message "Updating dynamic block `%s' at line %d...done" name line)
  10871. (goto-char pos)
  10872. (when (and indent (> indent 0))
  10873. (setq indent (make-string indent ?\ ))
  10874. (save-excursion
  10875. (select-window win)
  10876. (org-beginning-of-dblock)
  10877. (forward-line 1)
  10878. (while (not (looking-at org-dblock-end-re))
  10879. (insert indent)
  10880. (beginning-of-line 2))
  10881. (when (looking-at org-dblock-end-re)
  10882. (and (looking-at "[ \t]+")
  10883. (replace-match ""))
  10884. (insert indent)))))))
  10885. (defun org-beginning-of-dblock ()
  10886. "Find the beginning of the dynamic block at point.
  10887. Error if there is no such block at point."
  10888. (let ((pos (point))
  10889. beg)
  10890. (end-of-line 1)
  10891. (if (and (re-search-backward org-dblock-start-re nil t)
  10892. (setq beg (match-beginning 0))
  10893. (re-search-forward org-dblock-end-re nil t)
  10894. (> (match-end 0) pos))
  10895. (goto-char beg)
  10896. (goto-char pos)
  10897. (error "Not in a dynamic block"))))
  10898. (defun org-update-all-dblocks ()
  10899. "Update all dynamic blocks in the buffer.
  10900. This function can be used in a hook."
  10901. (interactive)
  10902. (when (derived-mode-p 'org-mode)
  10903. (org-map-dblocks 'org-update-dblock)))
  10904. ;;;; Completion
  10905. (declare-function org-export-backend-name "org-export" (cl-x))
  10906. (declare-function org-export-backend-options "org-export" (cl-x))
  10907. (defun org-get-export-keywords ()
  10908. "Return a list of all currently understood export keywords.
  10909. Export keywords include options, block names, attributes and
  10910. keywords relative to each registered export back-end."
  10911. (let (keywords)
  10912. (dolist (backend
  10913. (org-bound-and-true-p org-export--registered-backends)
  10914. (delq nil keywords))
  10915. ;; Back-end name (for keywords, like #+LATEX:)
  10916. (push (upcase (symbol-name (org-export-backend-name backend))) keywords)
  10917. (dolist (option-entry (org-export-backend-options backend))
  10918. ;; Back-end options.
  10919. (push (nth 1 option-entry) keywords)))))
  10920. (defconst org-options-keywords
  10921. '("ARCHIVE:" "AUTHOR:" "BIND:" "CATEGORY:" "COLUMNS:" "CREATOR:" "DATE:"
  10922. "DESCRIPTION:" "DRAWERS:" "EMAIL:" "EXCLUDE_TAGS:" "FILETAGS:" "INCLUDE:"
  10923. "INDEX:" "KEYWORDS:" "LANGUAGE:" "MACRO:" "OPTIONS:" "PROPERTY:"
  10924. "PRIORITIES:" "SELECT_TAGS:" "SEQ_TODO:" "SETUPFILE:" "STARTUP:" "TAGS:"
  10925. "TITLE:" "TODO:" "TYP_TODO:" "SELECT_TAGS:" "EXCLUDE_TAGS:"))
  10926. (defcustom org-structure-template-alist
  10927. '(("s" "#+BEGIN_SRC ?\n\n#+END_SRC" "<src lang=\"?\">\n\n</src>")
  10928. ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE" "<example>\n?\n</example>")
  10929. ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE" "<quote>\n?\n</quote>")
  10930. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE" "<verse>\n?\n</verse>")
  10931. ("V" "#+BEGIN_VERBATIM\n?\n#+END_VERBATIM" "<verbatim>\n?\n</verbatim>")
  10932. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER" "<center>\n?\n</center>")
  10933. ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX"
  10934. "<literal style=\"latex\">\n?\n</literal>")
  10935. ("L" "#+LaTeX: " "<literal style=\"latex\">?</literal>")
  10936. ("h" "#+BEGIN_HTML\n?\n#+END_HTML"
  10937. "<literal style=\"html\">\n?\n</literal>")
  10938. ("H" "#+HTML: " "<literal style=\"html\">?</literal>")
  10939. ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII" "")
  10940. ("A" "#+ASCII: " "")
  10941. ("i" "#+INDEX: ?" "#+INDEX: ?")
  10942. ("I" "#+INCLUDE: %file ?"
  10943. "<include file=%file markup=\"?\">"))
  10944. "Structure completion elements.
  10945. This is a list of abbreviation keys and values. The value gets inserted
  10946. if you type `<' followed by the key and then press the completion key,
  10947. usually `M-TAB'. %file will be replaced by a file name after prompting
  10948. for the file using completion. The cursor will be placed at the position
  10949. of the `?' in the template.
  10950. There are two templates for each key, the first uses the original Org syntax,
  10951. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  10952. the default when the /org-mtags.el/ module has been loaded. See also the
  10953. variable `org-mtags-prefer-muse-templates'."
  10954. :group 'org-completion
  10955. :type '(repeat
  10956. (list
  10957. (string :tag "Key")
  10958. (string :tag "Template")
  10959. (string :tag "Muse Template"))))
  10960. (defun org-try-structure-completion ()
  10961. "Try to complete a structure template before point.
  10962. This looks for strings like \"<e\" on an otherwise empty line and
  10963. expands them."
  10964. (let ((l (buffer-substring (point-at-bol) (point)))
  10965. a)
  10966. (when (and (looking-at "[ \t]*$")
  10967. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  10968. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  10969. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  10970. (match-beginning 1)) a)
  10971. t)))
  10972. (defun org-complete-expand-structure-template (start cell)
  10973. "Expand a structure template."
  10974. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  10975. (rpl (nth (if musep 2 1) cell))
  10976. (ind ""))
  10977. (delete-region start (point))
  10978. (when (string-match "\\`#\\+" rpl)
  10979. (cond
  10980. ((bolp))
  10981. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  10982. (setq ind (buffer-substring (point-at-bol) (point))))
  10983. (t (newline))))
  10984. (setq start (point))
  10985. (if (string-match "%file" rpl)
  10986. (setq rpl (replace-match
  10987. (concat
  10988. "\""
  10989. (save-match-data
  10990. (abbreviate-file-name (read-file-name "Include file: ")))
  10991. "\"")
  10992. t t rpl)))
  10993. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  10994. (concat "\n" ind)))
  10995. (insert rpl)
  10996. (if (re-search-backward "\\?" start t) (delete-char 1))))
  10997. ;;;; TODO, DEADLINE, Comments
  10998. (defun org-toggle-comment ()
  10999. "Change the COMMENT state of an entry."
  11000. (interactive)
  11001. (save-excursion
  11002. (org-back-to-heading)
  11003. (let (case-fold-search)
  11004. (cond
  11005. ((looking-at (format org-heading-keyword-regexp-format
  11006. org-comment-string))
  11007. (goto-char (match-end 1))
  11008. (looking-at (concat " +" org-comment-string))
  11009. (replace-match "" t t)
  11010. (when (eolp) (insert " ")))
  11011. ((looking-at org-outline-regexp)
  11012. (goto-char (match-end 0))
  11013. (insert org-comment-string " "))))))
  11014. (defvar org-last-todo-state-is-todo nil
  11015. "This is non-nil when the last TODO state change led to a TODO state.
  11016. If the last change removed the TODO tag or switched to DONE, then
  11017. this is nil.")
  11018. (defvar org-setting-tags nil) ; dynamically skipped
  11019. (defvar org-todo-setup-filter-hook nil
  11020. "Hook for functions that pre-filter todo specs.
  11021. Each function takes a todo spec and returns either nil or the spec
  11022. transformed into canonical form." )
  11023. (defvar org-todo-get-default-hook nil
  11024. "Hook for functions that get a default item for todo.
  11025. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  11026. nil or a string to be used for the todo mark." )
  11027. (defvar org-agenda-headline-snapshot-before-repeat)
  11028. (defun org-current-effective-time ()
  11029. "Return current time adjusted for `org-extend-today-until' variable."
  11030. (let* ((ct (org-current-time))
  11031. (dct (decode-time ct))
  11032. (ct1
  11033. (cond
  11034. (org-use-last-clock-out-time-as-effective-time
  11035. (or (org-clock-get-last-clock-out-time) ct))
  11036. ((and org-use-effective-time (< (nth 2 dct) org-extend-today-until))
  11037. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct)))
  11038. (t ct))))
  11039. ct1))
  11040. (defun org-todo-yesterday (&optional arg)
  11041. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  11042. (interactive "P")
  11043. (if (eq major-mode 'org-agenda-mode)
  11044. (apply 'org-agenda-todo-yesterday arg)
  11045. (let* ((hour (third (decode-time
  11046. (org-current-time))))
  11047. (org-extend-today-until (1+ hour)))
  11048. (org-todo arg))))
  11049. (defvar org-block-entry-blocking ""
  11050. "First entry preventing the TODO state change.")
  11051. (defun org-todo (&optional arg)
  11052. "Change the TODO state of an item.
  11053. The state of an item is given by a keyword at the start of the heading,
  11054. like
  11055. *** TODO Write paper
  11056. *** DONE Call mom
  11057. The different keywords are specified in the variable `org-todo-keywords'.
  11058. By default the available states are \"TODO\" and \"DONE\".
  11059. So for this example: when the item starts with TODO, it is changed to DONE.
  11060. When it starts with DONE, the DONE is removed. And when neither TODO nor
  11061. DONE are present, add TODO at the beginning of the heading.
  11062. With \\[universal-argument] prefix arg, use completion to determine the new \
  11063. state.
  11064. With numeric prefix arg, switch to that state.
  11065. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  11066. keywords (nextset).
  11067. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  11068. With a numeric prefix arg of 0, inhibit note taking for the change.
  11069. For calling through lisp, arg is also interpreted in the following way:
  11070. 'none -> empty state
  11071. \"\"(empty string) -> switch to empty state
  11072. 'done -> switch to DONE
  11073. 'nextset -> switch to the next set of keywords
  11074. 'previousset -> switch to the previous set of keywords
  11075. \"WAITING\" -> switch to the specified keyword, but only if it
  11076. really is a member of `org-todo-keywords'."
  11077. (interactive "P")
  11078. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11079. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11080. 'region-start-level 'region))
  11081. org-loop-over-headlines-in-active-region)
  11082. (org-map-entries
  11083. `(org-todo ,arg)
  11084. org-loop-over-headlines-in-active-region
  11085. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11086. (if (equal arg '(16)) (setq arg 'nextset))
  11087. (let ((org-blocker-hook org-blocker-hook)
  11088. commentp
  11089. case-fold-search)
  11090. (when (equal arg '(64))
  11091. (setq arg nil org-blocker-hook nil))
  11092. (when (and org-blocker-hook
  11093. (or org-inhibit-blocking
  11094. (org-entry-get nil "NOBLOCKING")))
  11095. (setq org-blocker-hook nil))
  11096. (save-excursion
  11097. (catch 'exit
  11098. (org-back-to-heading t)
  11099. (when (looking-at (concat "^\\*+ " org-comment-string))
  11100. (org-toggle-comment)
  11101. (setq commentp t))
  11102. (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  11103. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  11104. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  11105. (let* ((match-data (match-data))
  11106. (startpos (point-at-bol))
  11107. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  11108. (org-log-done org-log-done)
  11109. (org-log-repeat org-log-repeat)
  11110. (org-todo-log-states org-todo-log-states)
  11111. (org-inhibit-logging
  11112. (if (equal arg 0)
  11113. (progn (setq arg nil) 'note) org-inhibit-logging))
  11114. (this (match-string 1))
  11115. (hl-pos (match-beginning 0))
  11116. (head (org-get-todo-sequence-head this))
  11117. (ass (assoc head org-todo-kwd-alist))
  11118. (interpret (nth 1 ass))
  11119. (done-word (nth 3 ass))
  11120. (final-done-word (nth 4 ass))
  11121. (org-last-state (or this ""))
  11122. (completion-ignore-case t)
  11123. (member (member this org-todo-keywords-1))
  11124. (tail (cdr member))
  11125. (org-state (cond
  11126. ((and org-todo-key-trigger
  11127. (or (and (equal arg '(4))
  11128. (eq org-use-fast-todo-selection 'prefix))
  11129. (and (not arg) org-use-fast-todo-selection
  11130. (not (eq org-use-fast-todo-selection
  11131. 'prefix)))))
  11132. ;; Use fast selection
  11133. (org-fast-todo-selection))
  11134. ((and (equal arg '(4))
  11135. (or (not org-use-fast-todo-selection)
  11136. (not org-todo-key-trigger)))
  11137. ;; Read a state with completion
  11138. (org-icompleting-read
  11139. "State: " (mapcar 'list org-todo-keywords-1)
  11140. nil t))
  11141. ((eq arg 'right)
  11142. (if this
  11143. (if tail (car tail) nil)
  11144. (car org-todo-keywords-1)))
  11145. ((eq arg 'left)
  11146. (if (equal member org-todo-keywords-1)
  11147. nil
  11148. (if this
  11149. (nth (- (length org-todo-keywords-1)
  11150. (length tail) 2)
  11151. org-todo-keywords-1)
  11152. (org-last org-todo-keywords-1))))
  11153. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  11154. (setq arg nil))) ; hack to fall back to cycling
  11155. (arg
  11156. ;; user or caller requests a specific state
  11157. (cond
  11158. ((equal arg "") nil)
  11159. ((eq arg 'none) nil)
  11160. ((eq arg 'done) (or done-word (car org-done-keywords)))
  11161. ((eq arg 'nextset)
  11162. (or (car (cdr (member head org-todo-heads)))
  11163. (car org-todo-heads)))
  11164. ((eq arg 'previousset)
  11165. (let ((org-todo-heads (reverse org-todo-heads)))
  11166. (or (car (cdr (member head org-todo-heads)))
  11167. (car org-todo-heads))))
  11168. ((car (member arg org-todo-keywords-1)))
  11169. ((stringp arg)
  11170. (user-error "State `%s' not valid in this file" arg))
  11171. ((nth (1- (prefix-numeric-value arg))
  11172. org-todo-keywords-1))))
  11173. ((null member) (or head (car org-todo-keywords-1)))
  11174. ((equal this final-done-word) nil) ;; -> make empty
  11175. ((null tail) nil) ;; -> first entry
  11176. ((memq interpret '(type priority))
  11177. (if (eq this-command last-command)
  11178. (car tail)
  11179. (if (> (length tail) 0)
  11180. (or done-word (car org-done-keywords))
  11181. nil)))
  11182. (t
  11183. (car tail))))
  11184. (org-state (or
  11185. (run-hook-with-args-until-success
  11186. 'org-todo-get-default-hook org-state org-last-state)
  11187. org-state))
  11188. (next (if org-state (concat " " org-state " ") " "))
  11189. (change-plist (list :type 'todo-state-change :from this :to org-state
  11190. :position startpos))
  11191. dolog now-done-p)
  11192. (when org-blocker-hook
  11193. (setq org-last-todo-state-is-todo
  11194. (not (member this org-done-keywords)))
  11195. (unless (save-excursion
  11196. (save-match-data
  11197. (org-with-wide-buffer
  11198. (run-hook-with-args-until-failure
  11199. 'org-blocker-hook change-plist))))
  11200. (if (org-called-interactively-p 'interactive)
  11201. (user-error "TODO state change from %s to %s blocked (by \"%s\")"
  11202. this org-state org-block-entry-blocking)
  11203. ;; fail silently
  11204. (message "TODO state change from %s to %s blocked (by \"%s\")"
  11205. this org-state org-block-entry-blocking)
  11206. (throw 'exit nil))))
  11207. (store-match-data match-data)
  11208. (replace-match next t t)
  11209. (unless (pos-visible-in-window-p hl-pos)
  11210. (message "TODO state changed to %s" (org-trim next)))
  11211. (unless head
  11212. (setq head (org-get-todo-sequence-head org-state)
  11213. ass (assoc head org-todo-kwd-alist)
  11214. interpret (nth 1 ass)
  11215. done-word (nth 3 ass)
  11216. final-done-word (nth 4 ass)))
  11217. (when (memq arg '(nextset previousset))
  11218. (message "Keyword-Set %d/%d: %s"
  11219. (- (length org-todo-sets) -1
  11220. (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
  11221. (length org-todo-sets)
  11222. (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
  11223. (setq org-last-todo-state-is-todo
  11224. (not (member org-state org-done-keywords)))
  11225. (setq now-done-p (and (member org-state org-done-keywords)
  11226. (not (member this org-done-keywords))))
  11227. (and logging (org-local-logging logging))
  11228. (when (and (or org-todo-log-states org-log-done)
  11229. (not (eq org-inhibit-logging t))
  11230. (not (memq arg '(nextset previousset))))
  11231. ;; we need to look at recording a time and note
  11232. (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
  11233. (nth 2 (assoc this org-todo-log-states))))
  11234. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  11235. (setq dolog 'time))
  11236. (when (or (and (not org-state) (not org-closed-keep-when-no-todo))
  11237. (and org-state
  11238. (member org-state org-not-done-keywords)
  11239. (not (member this org-not-done-keywords))))
  11240. ;; This is now a todo state and was not one before
  11241. ;; If there was a CLOSED time stamp, get rid of it.
  11242. (org-add-planning-info nil nil 'closed))
  11243. (when (and now-done-p org-log-done)
  11244. ;; It is now done, and it was not done before
  11245. (org-add-planning-info 'closed (org-current-effective-time))
  11246. (if (and (not dolog) (eq 'note org-log-done))
  11247. (org-add-log-setup 'done org-state this 'findpos 'note)))
  11248. (when (and org-state dolog)
  11249. ;; This is a non-nil state, and we need to log it
  11250. (org-add-log-setup 'state org-state this 'findpos dolog)))
  11251. ;; Fixup tag positioning
  11252. (org-todo-trigger-tag-changes org-state)
  11253. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  11254. (when org-provide-todo-statistics
  11255. (org-update-parent-todo-statistics))
  11256. (run-hooks 'org-after-todo-state-change-hook)
  11257. (if (and arg (not (member org-state org-done-keywords)))
  11258. (setq head (org-get-todo-sequence-head org-state)))
  11259. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  11260. ;; Do we need to trigger a repeat?
  11261. (when now-done-p
  11262. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  11263. ;; This is for the agenda, take a snapshot of the headline.
  11264. (save-match-data
  11265. (setq org-agenda-headline-snapshot-before-repeat
  11266. (org-get-heading))))
  11267. (org-auto-repeat-maybe org-state))
  11268. ;; Fixup cursor location if close to the keyword
  11269. (if (and (outline-on-heading-p)
  11270. (not (bolp))
  11271. (save-excursion (beginning-of-line 1)
  11272. (looking-at org-todo-line-regexp))
  11273. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  11274. (progn
  11275. (goto-char (or (match-end 2) (match-end 1)))
  11276. (and (looking-at " ") (just-one-space))))
  11277. (when org-trigger-hook
  11278. (save-excursion
  11279. (run-hook-with-args 'org-trigger-hook change-plist)))
  11280. (when commentp (org-toggle-comment))))))))
  11281. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  11282. "Block turning an entry into a TODO, using the hierarchy.
  11283. This checks whether the current task should be blocked from state
  11284. changes. Such blocking occurs when:
  11285. 1. The task has children which are not all in a completed state.
  11286. 2. A task has a parent with the property :ORDERED:, and there
  11287. are siblings prior to the current task with incomplete
  11288. status.
  11289. 3. The parent of the task is blocked because it has siblings that should
  11290. be done first, or is child of a block grandparent TODO entry."
  11291. (if (not org-enforce-todo-dependencies)
  11292. t ; if locally turned off don't block
  11293. (catch 'dont-block
  11294. ;; If this is not a todo state change, or if this entry is already DONE,
  11295. ;; do not block
  11296. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11297. (member (plist-get change-plist :from)
  11298. (cons 'done org-done-keywords))
  11299. (member (plist-get change-plist :to)
  11300. (cons 'todo org-not-done-keywords))
  11301. (not (plist-get change-plist :to)))
  11302. (throw 'dont-block t))
  11303. ;; If this task has children, and any are undone, it's blocked
  11304. (save-excursion
  11305. (org-back-to-heading t)
  11306. (let ((this-level (funcall outline-level)))
  11307. (outline-next-heading)
  11308. (let ((child-level (funcall outline-level)))
  11309. (while (and (not (eobp))
  11310. (> child-level this-level))
  11311. ;; this todo has children, check whether they are all
  11312. ;; completed
  11313. (if (and (not (org-entry-is-done-p))
  11314. (org-entry-is-todo-p))
  11315. (progn (setq org-block-entry-blocking (org-get-heading))
  11316. (throw 'dont-block nil)))
  11317. (outline-next-heading)
  11318. (setq child-level (funcall outline-level))))))
  11319. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  11320. ;; any previous siblings are undone, it's blocked
  11321. (save-excursion
  11322. (org-back-to-heading t)
  11323. (let* ((pos (point))
  11324. (parent-pos (and (org-up-heading-safe) (point))))
  11325. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11326. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11327. (forward-line 1)
  11328. (re-search-forward org-not-done-heading-regexp pos t))
  11329. (setq org-block-entry-blocking (match-string 0))
  11330. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  11331. ;; Search further up the hierarchy, to see if an ancestor is blocked
  11332. (while t
  11333. (goto-char parent-pos)
  11334. (if (not (looking-at org-not-done-heading-regexp))
  11335. (throw 'dont-block t)) ; do not block, parent is not a TODO
  11336. (setq pos (point))
  11337. (setq parent-pos (and (org-up-heading-safe) (point)))
  11338. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11339. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11340. (forward-line 1)
  11341. (re-search-forward org-not-done-heading-regexp pos t)
  11342. (setq org-block-entry-blocking (org-get-heading)))
  11343. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  11344. (defcustom org-track-ordered-property-with-tag nil
  11345. "Should the ORDERED property also be shown as a tag?
  11346. The ORDERED property decides if an entry should require subtasks to be
  11347. completed in sequence. Since a property is not very visible, setting
  11348. this option means that toggling the ORDERED property with the command
  11349. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  11350. not relevant for the behavior, but it makes things more visible.
  11351. Note that toggling the tag with tags commands will not change the property
  11352. and therefore not influence behavior!
  11353. This can be t, meaning the tag ORDERED should be used, It can also be a
  11354. string to select a different tag for this task."
  11355. :group 'org-todo
  11356. :type '(choice
  11357. (const :tag "No tracking" nil)
  11358. (const :tag "Track with ORDERED tag" t)
  11359. (string :tag "Use other tag")))
  11360. (defun org-toggle-ordered-property ()
  11361. "Toggle the ORDERED property of the current entry.
  11362. For better visibility, you can track the value of this property with a tag.
  11363. See variable `org-track-ordered-property-with-tag'."
  11364. (interactive)
  11365. (let* ((t1 org-track-ordered-property-with-tag)
  11366. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  11367. (save-excursion
  11368. (org-back-to-heading)
  11369. (if (org-entry-get nil "ORDERED")
  11370. (progn
  11371. (org-delete-property "ORDERED" "PROPERTIES")
  11372. (and tag (org-toggle-tag tag 'off))
  11373. (message "Subtasks can be completed in arbitrary order"))
  11374. (org-entry-put nil "ORDERED" "t")
  11375. (and tag (org-toggle-tag tag 'on))
  11376. (message "Subtasks must be completed in sequence")))))
  11377. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  11378. (defun org-block-todo-from-checkboxes (change-plist)
  11379. "Block turning an entry into a TODO, using checkboxes.
  11380. This checks whether the current task should be blocked from state
  11381. changes because there are unchecked boxes in this entry."
  11382. (if (not org-enforce-todo-checkbox-dependencies)
  11383. t ; if locally turned off don't block
  11384. (catch 'dont-block
  11385. ;; If this is not a todo state change, or if this entry is already DONE,
  11386. ;; do not block
  11387. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11388. (member (plist-get change-plist :from)
  11389. (cons 'done org-done-keywords))
  11390. (member (plist-get change-plist :to)
  11391. (cons 'todo org-not-done-keywords))
  11392. (not (plist-get change-plist :to)))
  11393. (throw 'dont-block t))
  11394. ;; If this task has checkboxes that are not checked, it's blocked
  11395. (save-excursion
  11396. (org-back-to-heading t)
  11397. (let ((beg (point)) end)
  11398. (outline-next-heading)
  11399. (setq end (point))
  11400. (goto-char beg)
  11401. (if (org-list-search-forward
  11402. (concat (org-item-beginning-re)
  11403. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  11404. "\\[[- ]\\]")
  11405. end t)
  11406. (progn
  11407. (if (boundp 'org-blocked-by-checkboxes)
  11408. (setq org-blocked-by-checkboxes t))
  11409. (throw 'dont-block nil)))))
  11410. t))) ; do not block
  11411. (defun org-entry-blocked-p ()
  11412. "Is the current entry blocked?"
  11413. (org-with-silent-modifications
  11414. (if (org-entry-get nil "NOBLOCKING")
  11415. nil ;; Never block this entry
  11416. (not (run-hook-with-args-until-failure
  11417. 'org-blocker-hook
  11418. (list :type 'todo-state-change
  11419. :position (point)
  11420. :from 'todo
  11421. :to 'done))))))
  11422. (defun org-update-statistics-cookies (all)
  11423. "Update the statistics cookie, either from TODO or from checkboxes.
  11424. This should be called with the cursor in a line with a statistics cookie."
  11425. (interactive "P")
  11426. (if all
  11427. (progn
  11428. (org-update-checkbox-count 'all)
  11429. (org-map-entries 'org-update-parent-todo-statistics))
  11430. (if (not (org-at-heading-p))
  11431. (org-update-checkbox-count)
  11432. (let ((pos (point-marker))
  11433. end l1 l2)
  11434. (ignore-errors (org-back-to-heading t))
  11435. (if (not (org-at-heading-p))
  11436. (org-update-checkbox-count)
  11437. (setq l1 (org-outline-level))
  11438. (setq end (save-excursion
  11439. (outline-next-heading)
  11440. (if (org-at-heading-p) (setq l2 (org-outline-level)))
  11441. (point)))
  11442. (if (and (save-excursion
  11443. (re-search-forward
  11444. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  11445. (not (save-excursion (re-search-forward
  11446. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  11447. (org-update-checkbox-count)
  11448. (if (and l2 (> l2 l1))
  11449. (progn
  11450. (goto-char end)
  11451. (org-update-parent-todo-statistics))
  11452. (goto-char pos)
  11453. (beginning-of-line 1)
  11454. (while (re-search-forward
  11455. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  11456. (point-at-eol) t)
  11457. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  11458. (goto-char pos)
  11459. (move-marker pos nil)))))
  11460. (defvar org-entry-property-inherited-from) ;; defined below
  11461. (defun org-update-parent-todo-statistics ()
  11462. "Update any statistics cookie in the parent of the current headline.
  11463. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  11464. the entire subtree and this will travel up the hierarchy and update
  11465. statistics everywhere."
  11466. (let* ((prop (save-excursion (org-up-heading-safe)
  11467. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  11468. (recursive (or (not org-hierarchical-todo-statistics)
  11469. (and prop (string-match "\\<recursive\\>" prop))))
  11470. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  11471. 0))
  11472. (first t)
  11473. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  11474. level ltoggle l1 new ndel
  11475. (cnt-all 0) (cnt-done 0) is-percent kwd
  11476. checkbox-beg ov ovs ove cookie-present)
  11477. (catch 'exit
  11478. (save-excursion
  11479. (beginning-of-line 1)
  11480. (setq ltoggle (funcall outline-level))
  11481. ;; Three situations are to consider:
  11482. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  11483. ;; to the top-level ancestor on the headline;
  11484. ;; 2. If parent has "recursive" property, repeat up to the
  11485. ;; headline setting that property, taking inheritance into
  11486. ;; account;
  11487. ;; 3. Else, move up to direct parent and proceed only once.
  11488. (while (and (setq level (org-up-heading-safe))
  11489. (or recursive first)
  11490. (>= (point) lim))
  11491. (setq first nil cookie-present nil)
  11492. (unless (and level
  11493. (not (string-match
  11494. "\\<checkbox\\>"
  11495. (downcase (or (org-entry-get nil "COOKIE_DATA")
  11496. "")))))
  11497. (throw 'exit nil))
  11498. (while (re-search-forward box-re (point-at-eol) t)
  11499. (setq cnt-all 0 cnt-done 0 cookie-present t)
  11500. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  11501. (save-match-data
  11502. (unless (outline-next-heading) (throw 'exit nil))
  11503. (while (and (looking-at org-complex-heading-regexp)
  11504. (> (setq l1 (length (match-string 1))) level))
  11505. (setq kwd (and (or recursive (= l1 ltoggle))
  11506. (match-string 2)))
  11507. (if (or (eq org-provide-todo-statistics 'all-headlines)
  11508. (and (listp org-provide-todo-statistics)
  11509. (or (member kwd org-provide-todo-statistics)
  11510. (member kwd org-done-keywords))))
  11511. (setq cnt-all (1+ cnt-all))
  11512. (if (eq org-provide-todo-statistics t)
  11513. (and kwd (setq cnt-all (1+ cnt-all)))))
  11514. (and (member kwd org-done-keywords)
  11515. (setq cnt-done (1+ cnt-done)))
  11516. (outline-next-heading)))
  11517. (setq new
  11518. (if is-percent
  11519. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  11520. (format "[%d/%d]" cnt-done cnt-all))
  11521. ndel (- (match-end 0) checkbox-beg))
  11522. ;; handle overlays when updating cookie from column view
  11523. (when (setq ov (car (overlays-at checkbox-beg)))
  11524. (setq ovs (overlay-start ov) ove (overlay-end ov))
  11525. (delete-overlay ov))
  11526. (goto-char checkbox-beg)
  11527. (insert new)
  11528. (delete-region (point) (+ (point) ndel))
  11529. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  11530. (when ov (move-overlay ov ovs ove)))
  11531. (when cookie-present
  11532. (run-hook-with-args 'org-after-todo-statistics-hook
  11533. cnt-done (- cnt-all cnt-done))))))
  11534. (run-hooks 'org-todo-statistics-hook)))
  11535. (defvar org-after-todo-statistics-hook nil
  11536. "Hook that is called after a TODO statistics cookie has been updated.
  11537. Each function is called with two arguments: the number of not-done entries
  11538. and the number of done entries.
  11539. For example, the following function, when added to this hook, will switch
  11540. an entry to DONE when all children are done, and back to TODO when new
  11541. entries are set to a TODO status. Note that this hook is only called
  11542. when there is a statistics cookie in the headline!
  11543. (defun org-summary-todo (n-done n-not-done)
  11544. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  11545. (let (org-log-done org-log-states) ; turn off logging
  11546. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  11547. ")
  11548. (defvar org-todo-statistics-hook nil
  11549. "Hook that is run whenever Org thinks TODO statistics should be updated.
  11550. This hook runs even if there is no statistics cookie present, in which case
  11551. `org-after-todo-statistics-hook' would not run.")
  11552. (defun org-todo-trigger-tag-changes (state)
  11553. "Apply the changes defined in `org-todo-state-tags-triggers'."
  11554. (let ((l org-todo-state-tags-triggers)
  11555. changes)
  11556. (when (or (not state) (equal state ""))
  11557. (setq changes (append changes (cdr (assoc "" l)))))
  11558. (when (and (stringp state) (> (length state) 0))
  11559. (setq changes (append changes (cdr (assoc state l)))))
  11560. (when (member state org-not-done-keywords)
  11561. (setq changes (append changes (cdr (assoc 'todo l)))))
  11562. (when (member state org-done-keywords)
  11563. (setq changes (append changes (cdr (assoc 'done l)))))
  11564. (dolist (c changes)
  11565. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  11566. (defun org-local-logging (value)
  11567. "Get logging settings from a property VALUE."
  11568. (let* (words w a)
  11569. ;; directly set the variables, they are already local.
  11570. (setq org-log-done nil
  11571. org-log-repeat nil
  11572. org-todo-log-states nil)
  11573. (setq words (org-split-string value))
  11574. (while (setq w (pop words))
  11575. (cond
  11576. ((setq a (assoc w org-startup-options))
  11577. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  11578. (set (nth 1 a) (nth 2 a))))
  11579. ((setq a (org-extract-log-state-settings w))
  11580. (and (member (car a) org-todo-keywords-1)
  11581. (push a org-todo-log-states)))))))
  11582. (defun org-get-todo-sequence-head (kwd)
  11583. "Return the head of the TODO sequence to which KWD belongs.
  11584. If KWD is not set, check if there is a text property remembering the
  11585. right sequence."
  11586. (let (p)
  11587. (cond
  11588. ((not kwd)
  11589. (or (get-text-property (point-at-bol) 'org-todo-head)
  11590. (progn
  11591. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  11592. nil (point-at-eol)))
  11593. (get-text-property p 'org-todo-head))))
  11594. ((not (member kwd org-todo-keywords-1))
  11595. (car org-todo-keywords-1))
  11596. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  11597. (defun org-fast-todo-selection ()
  11598. "Fast TODO keyword selection with single keys.
  11599. Returns the new TODO keyword, or nil if no state change should occur."
  11600. (let* ((fulltable org-todo-key-alist)
  11601. (done-keywords org-done-keywords) ;; needed for the faces.
  11602. (maxlen (apply 'max (mapcar
  11603. (lambda (x)
  11604. (if (stringp (car x)) (string-width (car x)) 0))
  11605. fulltable)))
  11606. (expert nil)
  11607. (fwidth (+ maxlen 3 1 3))
  11608. (ncol (/ (- (window-width) 4) fwidth))
  11609. tg cnt e c tbl
  11610. groups ingroup)
  11611. (save-excursion
  11612. (save-window-excursion
  11613. (if expert
  11614. (set-buffer (get-buffer-create " *Org todo*"))
  11615. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  11616. (erase-buffer)
  11617. (org-set-local 'org-done-keywords done-keywords)
  11618. (setq tbl fulltable cnt 0)
  11619. (while (setq e (pop tbl))
  11620. (cond
  11621. ((equal e '(:startgroup))
  11622. (push '() groups) (setq ingroup t)
  11623. (when (not (= cnt 0))
  11624. (setq cnt 0)
  11625. (insert "\n"))
  11626. (insert "{ "))
  11627. ((equal e '(:endgroup))
  11628. (setq ingroup nil cnt 0)
  11629. (insert "}\n"))
  11630. ((equal e '(:newline))
  11631. (when (not (= cnt 0))
  11632. (setq cnt 0)
  11633. (insert "\n")
  11634. (setq e (car tbl))
  11635. (while (equal (car tbl) '(:newline))
  11636. (insert "\n")
  11637. (setq tbl (cdr tbl)))))
  11638. (t
  11639. (setq tg (car e) c (cdr e))
  11640. (if ingroup (push tg (car groups)))
  11641. (setq tg (org-add-props tg nil 'face
  11642. (org-get-todo-face tg)))
  11643. (if (and (= cnt 0) (not ingroup)) (insert " "))
  11644. (insert "[" c "] " tg (make-string
  11645. (- fwidth 4 (length tg)) ?\ ))
  11646. (when (= (setq cnt (1+ cnt)) ncol)
  11647. (insert "\n")
  11648. (if ingroup (insert " "))
  11649. (setq cnt 0)))))
  11650. (insert "\n")
  11651. (goto-char (point-min))
  11652. (if (not expert) (org-fit-window-to-buffer))
  11653. (message "[a-z..]:Set [SPC]:clear")
  11654. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11655. (cond
  11656. ((or (= c ?\C-g)
  11657. (and (= c ?q) (not (rassoc c fulltable))))
  11658. (setq quit-flag t))
  11659. ((= c ?\ ) nil)
  11660. ((setq e (rassoc c fulltable) tg (car e))
  11661. tg)
  11662. (t (setq quit-flag t)))))))
  11663. (defun org-entry-is-todo-p ()
  11664. (member (org-get-todo-state) org-not-done-keywords))
  11665. (defun org-entry-is-done-p ()
  11666. (member (org-get-todo-state) org-done-keywords))
  11667. (defun org-get-todo-state ()
  11668. "Return the TODO keyword of the current subtree."
  11669. (save-excursion
  11670. (org-back-to-heading t)
  11671. (and (looking-at org-todo-line-regexp)
  11672. (match-end 2)
  11673. (match-string 2))))
  11674. (defun org-at-date-range-p (&optional inactive-ok)
  11675. "Is the cursor inside a date range?"
  11676. (interactive)
  11677. (save-excursion
  11678. (catch 'exit
  11679. (let ((pos (point)))
  11680. (skip-chars-backward "^[<\r\n")
  11681. (skip-chars-backward "<[")
  11682. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11683. (>= (match-end 0) pos)
  11684. (throw 'exit t))
  11685. (skip-chars-backward "^<[\r\n")
  11686. (skip-chars-backward "<[")
  11687. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11688. (>= (match-end 0) pos)
  11689. (throw 'exit t)))
  11690. nil)))
  11691. (defun org-get-repeat (&optional tagline)
  11692. "Check if there is a deadline/schedule with repeater in this entry."
  11693. (save-match-data
  11694. (save-excursion
  11695. (org-back-to-heading t)
  11696. (and (re-search-forward (if tagline
  11697. (concat tagline "\\s-*" org-repeat-re)
  11698. org-repeat-re)
  11699. (org-entry-end-position) t)
  11700. (match-string-no-properties 1)))))
  11701. (defvar org-last-changed-timestamp)
  11702. (defvar org-last-inserted-timestamp)
  11703. (defvar org-log-post-message)
  11704. (defvar org-log-note-purpose)
  11705. (defvar org-log-note-how)
  11706. (defvar org-log-note-extra)
  11707. (defun org-auto-repeat-maybe (done-word)
  11708. "Check if the current headline contains a repeated deadline/schedule.
  11709. If yes, set TODO state back to what it was and change the base date
  11710. of repeating deadline/scheduled time stamps to new date.
  11711. This function is run automatically after each state change to a DONE state."
  11712. ;; last-state is dynamically scoped into this function
  11713. (let* ((repeat (org-get-repeat))
  11714. (aa (assoc org-last-state org-todo-kwd-alist))
  11715. (interpret (nth 1 aa))
  11716. (head (nth 2 aa))
  11717. (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
  11718. (msg "Entry repeats: ")
  11719. (org-log-done nil)
  11720. (org-todo-log-states nil)
  11721. re type n what ts time to-state)
  11722. (when repeat
  11723. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  11724. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  11725. org-todo-repeat-to-state))
  11726. (unless (and to-state (member to-state org-todo-keywords-1))
  11727. (setq to-state (if (eq interpret 'type) org-last-state head)))
  11728. (org-todo to-state)
  11729. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  11730. (org-entry-put nil "LAST_REPEAT" (format-time-string
  11731. (org-time-stamp-format t t))))
  11732. (when org-log-repeat
  11733. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  11734. (memq 'org-add-log-note post-command-hook))
  11735. ;; OK, we are already setup for some record
  11736. (if (eq org-log-repeat 'note)
  11737. ;; make sure we take a note, not only a time stamp
  11738. (setq org-log-note-how 'note))
  11739. ;; Set up for taking a record
  11740. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  11741. org-last-state
  11742. 'findpos org-log-repeat)))
  11743. (org-back-to-heading t)
  11744. (org-add-planning-info nil nil 'closed)
  11745. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11746. org-deadline-time-regexp "\\)\\|\\("
  11747. org-ts-regexp "\\)"))
  11748. (while (re-search-forward
  11749. re (save-excursion (outline-next-heading) (point)) t)
  11750. (setq type (if (match-end 1) org-scheduled-string
  11751. (if (match-end 3) org-deadline-string "Plain:"))
  11752. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  11753. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts)
  11754. (setq n (string-to-number (match-string 2 ts))
  11755. what (match-string 3 ts))
  11756. (if (equal what "w") (setq n (* n 7) what "d"))
  11757. (if (and (equal what "h") (not (string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts)))
  11758. (user-error "Cannot repeat in Repeat in %d hour(s) because no hour has been set" n))
  11759. ;; Preparation, see if we need to modify the start date for the change
  11760. (when (match-end 1)
  11761. (setq time (save-match-data (org-time-string-to-time ts)))
  11762. (cond
  11763. ((equal (match-string 1 ts) ".")
  11764. ;; Shift starting date to today
  11765. (org-timestamp-change
  11766. (- (org-today) (time-to-days time))
  11767. 'day))
  11768. ((equal (match-string 1 ts) "+")
  11769. (let ((nshiftmax 10) (nshift 0))
  11770. (while (or (= nshift 0)
  11771. (<= (time-to-days time)
  11772. (time-to-days (current-time))))
  11773. (when (= (incf nshift) nshiftmax)
  11774. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  11775. (error "Abort")))
  11776. (org-timestamp-change n (cdr (assoc what whata)))
  11777. (org-at-timestamp-p t)
  11778. (setq ts (match-string 1))
  11779. (setq time (save-match-data (org-time-string-to-time ts)))))
  11780. (org-timestamp-change (- n) (cdr (assoc what whata)))
  11781. ;; rematch, so that we have everything in place for the real shift
  11782. (org-at-timestamp-p t)
  11783. (setq ts (match-string 1))
  11784. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))))
  11785. (save-excursion (org-timestamp-change n (cdr (assoc what whata)) nil t))
  11786. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  11787. (setq org-log-post-message msg)
  11788. (message "%s" msg))))
  11789. (defun org-show-todo-tree (arg)
  11790. "Make a compact tree which shows all headlines marked with TODO.
  11791. The tree will show the lines where the regexp matches, and all higher
  11792. headlines above the match.
  11793. With a \\[universal-argument] prefix, prompt for a regexp to match.
  11794. With a numeric prefix N, construct a sparse tree for the Nth element
  11795. of `org-todo-keywords-1'."
  11796. (interactive "P")
  11797. (let ((case-fold-search nil)
  11798. (kwd-re
  11799. (cond ((null arg) org-not-done-regexp)
  11800. ((equal arg '(4))
  11801. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  11802. (mapcar 'list org-todo-keywords-1))))
  11803. (concat "\\("
  11804. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  11805. "\\)\\>")))
  11806. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  11807. (regexp-quote (nth (1- (prefix-numeric-value arg))
  11808. org-todo-keywords-1)))
  11809. (t (user-error "Invalid prefix argument: %s" arg)))))
  11810. (message "%d TODO entries found"
  11811. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  11812. (defun org-deadline (arg &optional time)
  11813. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  11814. With one universal prefix argument, remove any deadline from the item.
  11815. With two universal prefix arguments, prompt for a warning delay.
  11816. With argument TIME, set the deadline at the corresponding date. TIME
  11817. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11818. (interactive "P")
  11819. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11820. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11821. 'region-start-level 'region))
  11822. org-loop-over-headlines-in-active-region)
  11823. (org-map-entries
  11824. `(org-deadline ',arg ,time)
  11825. org-loop-over-headlines-in-active-region
  11826. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11827. (let* ((old-date (org-entry-get nil "DEADLINE"))
  11828. (old-date-time (if old-date (org-time-string-to-time old-date)))
  11829. (repeater (and old-date
  11830. (string-match
  11831. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11832. old-date)
  11833. (match-string 1 old-date))))
  11834. (cond
  11835. ((equal arg '(4))
  11836. (when (and old-date org-log-redeadline)
  11837. (org-add-log-setup 'deldeadline nil old-date 'findpos
  11838. org-log-redeadline))
  11839. (org-remove-timestamp-with-keyword org-deadline-string)
  11840. (message "Item no longer has a deadline."))
  11841. ((equal arg '(16))
  11842. (save-excursion
  11843. (org-back-to-heading t)
  11844. (if (re-search-forward
  11845. org-deadline-time-regexp
  11846. (save-excursion (outline-next-heading) (point)) t)
  11847. (let* ((rpl0 (match-string 1))
  11848. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  11849. (replace-match
  11850. (concat org-deadline-string
  11851. " <" rpl
  11852. (format " -%dd"
  11853. (abs
  11854. (- (time-to-days
  11855. (save-match-data
  11856. (org-read-date nil t nil "Warn starting from" old-date-time)))
  11857. (time-to-days old-date-time))))
  11858. ">") t t))
  11859. (user-error "No deadline information to update"))))
  11860. (t
  11861. (org-add-planning-info 'deadline time 'closed)
  11862. (when (and old-date org-log-redeadline
  11863. (not (equal old-date
  11864. (substring org-last-inserted-timestamp 1 -1))))
  11865. (org-add-log-setup 'redeadline nil old-date 'findpos
  11866. org-log-redeadline))
  11867. (when repeater
  11868. (save-excursion
  11869. (org-back-to-heading t)
  11870. (when (re-search-forward (concat org-deadline-string " "
  11871. org-last-inserted-timestamp)
  11872. (save-excursion
  11873. (outline-next-heading) (point)) t)
  11874. (goto-char (1- (match-end 0)))
  11875. (insert " " repeater)
  11876. (setq org-last-inserted-timestamp
  11877. (concat (substring org-last-inserted-timestamp 0 -1)
  11878. " " repeater
  11879. (substring org-last-inserted-timestamp -1))))))
  11880. (message "Deadline on %s" org-last-inserted-timestamp))))))
  11881. (defun org-schedule (arg &optional time)
  11882. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  11883. With one universal prefix argument, remove any scheduling date from the item.
  11884. With two universal prefix arguments, prompt for a delay cookie.
  11885. With argument TIME, scheduled at the corresponding date. TIME can
  11886. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11887. (interactive "P")
  11888. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11889. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11890. 'region-start-level 'region))
  11891. org-loop-over-headlines-in-active-region)
  11892. (org-map-entries
  11893. `(org-schedule ',arg ,time)
  11894. org-loop-over-headlines-in-active-region
  11895. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11896. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  11897. (old-date-time (if old-date (org-time-string-to-time old-date)))
  11898. (repeater (and old-date
  11899. (string-match
  11900. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11901. old-date)
  11902. (match-string 1 old-date))))
  11903. (cond
  11904. ((equal arg '(4))
  11905. (progn
  11906. (when (and old-date org-log-reschedule)
  11907. (org-add-log-setup 'delschedule nil old-date 'findpos
  11908. org-log-reschedule))
  11909. (org-remove-timestamp-with-keyword org-scheduled-string)
  11910. (message "Item is no longer scheduled.")))
  11911. ((equal arg '(16))
  11912. (save-excursion
  11913. (org-back-to-heading t)
  11914. (if (re-search-forward
  11915. org-scheduled-time-regexp
  11916. (save-excursion (outline-next-heading) (point)) t)
  11917. (let* ((rpl0 (match-string 1))
  11918. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  11919. (replace-match
  11920. (concat org-scheduled-string
  11921. " <" rpl
  11922. (format " -%dd"
  11923. (abs
  11924. (- (time-to-days
  11925. (save-match-data
  11926. (org-read-date nil t nil "Delay until" old-date-time)))
  11927. (time-to-days old-date-time))))
  11928. ">") t t))
  11929. (user-error "No scheduled information to update"))))
  11930. (t
  11931. (org-add-planning-info 'scheduled time 'closed)
  11932. (when (and old-date org-log-reschedule
  11933. (not (equal old-date
  11934. (substring org-last-inserted-timestamp 1 -1))))
  11935. (org-add-log-setup 'reschedule nil old-date 'findpos
  11936. org-log-reschedule))
  11937. (when repeater
  11938. (save-excursion
  11939. (org-back-to-heading t)
  11940. (when (re-search-forward (concat org-scheduled-string " "
  11941. org-last-inserted-timestamp)
  11942. (save-excursion
  11943. (outline-next-heading) (point)) t)
  11944. (goto-char (1- (match-end 0)))
  11945. (insert " " repeater)
  11946. (setq org-last-inserted-timestamp
  11947. (concat (substring org-last-inserted-timestamp 0 -1)
  11948. " " repeater
  11949. (substring org-last-inserted-timestamp -1))))))
  11950. (message "Scheduled to %s" org-last-inserted-timestamp))))))
  11951. (defun org-get-scheduled-time (pom &optional inherit)
  11952. "Get the scheduled time as a time tuple, of a format suitable
  11953. for calling org-schedule with, or if there is no scheduling,
  11954. returns nil."
  11955. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  11956. (when time
  11957. (apply 'encode-time (org-parse-time-string time)))))
  11958. (defun org-get-deadline-time (pom &optional inherit)
  11959. "Get the deadline as a time tuple, of a format suitable for
  11960. calling org-deadline with, or if there is no scheduling, returns
  11961. nil."
  11962. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  11963. (when time
  11964. (apply 'encode-time (org-parse-time-string time)))))
  11965. (defun org-remove-timestamp-with-keyword (keyword)
  11966. "Remove all time stamps with KEYWORD in the current entry."
  11967. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  11968. beg)
  11969. (save-excursion
  11970. (org-back-to-heading t)
  11971. (setq beg (point))
  11972. (outline-next-heading)
  11973. (while (re-search-backward re beg t)
  11974. (replace-match "")
  11975. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  11976. (equal (char-before) ?\ ))
  11977. (backward-delete-char 1)
  11978. (if (string-match "^[ \t]*$" (buffer-substring
  11979. (point-at-bol) (point-at-eol)))
  11980. (delete-region (point-at-bol)
  11981. (min (point-max) (1+ (point-at-eol))))))))))
  11982. (defvar org-time-was-given) ; dynamically scoped parameter
  11983. (defvar org-end-time-was-given) ; dynamically scoped parameter
  11984. (defun org-add-planning-info (what &optional time &rest remove)
  11985. "Insert new timestamp with keyword in the line directly after the headline.
  11986. WHAT indicates what kind of time stamp to add. TIME indicates the time to use.
  11987. If non is given, the user is prompted for a date.
  11988. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  11989. be removed."
  11990. (interactive)
  11991. (let (org-time-was-given org-end-time-was-given ts
  11992. end default-time default-input)
  11993. (catch 'exit
  11994. (when (and (memq what '(scheduled deadline))
  11995. (or (not time)
  11996. (and (stringp time)
  11997. (string-match "^[-+]+[0-9]" time))))
  11998. ;; Try to get a default date/time from existing timestamp
  11999. (save-excursion
  12000. (org-back-to-heading t)
  12001. (setq end (save-excursion (outline-next-heading) (point)))
  12002. (when (re-search-forward (if (eq what 'scheduled)
  12003. org-scheduled-time-regexp
  12004. org-deadline-time-regexp)
  12005. end t)
  12006. (setq ts (match-string 1)
  12007. default-time
  12008. (apply 'encode-time (org-parse-time-string ts))
  12009. default-input (and ts (org-get-compact-tod ts))))))
  12010. (when what
  12011. (setq time
  12012. (if (stringp time)
  12013. ;; This is a string (relative or absolute), set proper date
  12014. (apply 'encode-time
  12015. (org-read-date-analyze
  12016. time default-time (decode-time default-time)))
  12017. ;; If necessary, get the time from the user
  12018. (or time (org-read-date nil 'to-time nil nil
  12019. default-time default-input)))))
  12020. (when (and org-insert-labeled-timestamps-at-point
  12021. (member what '(scheduled deadline)))
  12022. (insert
  12023. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  12024. (org-insert-time-stamp time org-time-was-given
  12025. nil nil nil (list org-end-time-was-given))
  12026. (setq what nil))
  12027. (save-excursion
  12028. (save-restriction
  12029. (let (col list elt ts buffer-invisibility-spec)
  12030. (org-back-to-heading t)
  12031. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
  12032. (goto-char (match-end 1))
  12033. (setq col (current-column))
  12034. (goto-char (match-end 0))
  12035. (if (eobp) (insert "\n") (forward-char 1))
  12036. (when (and (not what)
  12037. (not (looking-at
  12038. (concat "[ \t]*"
  12039. org-keyword-time-not-clock-regexp))))
  12040. ;; Nothing to add, nothing to remove...... :-)
  12041. (throw 'exit nil))
  12042. (if (and (not (looking-at org-outline-regexp))
  12043. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  12044. "[^\r\n]*"))
  12045. (not (equal (match-string 1) org-clock-string)))
  12046. (narrow-to-region (match-beginning 0) (match-end 0))
  12047. (insert-before-markers "\n")
  12048. (backward-char 1)
  12049. (narrow-to-region (point) (point))
  12050. (and org-adapt-indentation (org-indent-to-column col)))
  12051. ;; Check if we have to remove something.
  12052. (setq list (cons what remove))
  12053. (while list
  12054. (setq elt (pop list))
  12055. (when (or (and (eq elt 'scheduled)
  12056. (re-search-forward org-scheduled-time-regexp nil t))
  12057. (and (eq elt 'deadline)
  12058. (re-search-forward org-deadline-time-regexp nil t))
  12059. (and (eq elt 'closed)
  12060. (re-search-forward org-closed-time-regexp nil t)))
  12061. (replace-match "")
  12062. (if (looking-at "--+<[^>]+>") (replace-match ""))))
  12063. (and (looking-at "[ \t]+") (replace-match ""))
  12064. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  12065. (when what
  12066. (insert
  12067. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  12068. (cond ((eq what 'scheduled) org-scheduled-string)
  12069. ((eq what 'deadline) org-deadline-string)
  12070. ((eq what 'closed) org-closed-string))
  12071. " ")
  12072. (setq ts (org-insert-time-stamp
  12073. time
  12074. (or org-time-was-given
  12075. (and (eq what 'closed) org-log-done-with-time))
  12076. (eq what 'closed)
  12077. nil nil (list org-end-time-was-given)))
  12078. (insert
  12079. (if (not (or (bolp) (eq (char-before) ?\ )
  12080. (memq (char-after) '(32 10))
  12081. (eobp))) " " ""))
  12082. (end-of-line 1))
  12083. (goto-char (point-min))
  12084. (widen)
  12085. (if (and (looking-at "[ \t]*\n")
  12086. (equal (char-before) ?\n))
  12087. (delete-region (1- (point)) (point-at-eol)))
  12088. ts))))))
  12089. (defvar org-log-note-marker (make-marker))
  12090. (defvar org-log-note-purpose nil)
  12091. (defvar org-log-note-state nil)
  12092. (defvar org-log-note-previous-state nil)
  12093. (defvar org-log-note-how nil)
  12094. (defvar org-log-note-extra nil)
  12095. (defvar org-log-note-window-configuration nil)
  12096. (defvar org-log-note-return-to (make-marker))
  12097. (defvar org-log-note-effective-time nil
  12098. "Remembered current time so that dynamically scoped
  12099. `org-extend-today-until' affects tha timestamps in state change
  12100. log")
  12101. (defvar org-log-post-message nil
  12102. "Message to be displayed after a log note has been stored.
  12103. The auto-repeater uses this.")
  12104. (defun org-add-note ()
  12105. "Add a note to the current entry.
  12106. This is done in the same way as adding a state change note."
  12107. (interactive)
  12108. (org-add-log-setup 'note nil nil 'findpos nil))
  12109. (defvar org-property-end-re)
  12110. (defun org-add-log-setup (&optional purpose state prev-state
  12111. findpos how extra)
  12112. "Set up the post command hook to take a note.
  12113. If this is about to TODO state change, the new state is expected in STATE.
  12114. When FINDPOS is non-nil, find the correct position for the note in
  12115. the current entry. If not, assume that it can be inserted at point.
  12116. HOW is an indicator what kind of note should be created.
  12117. EXTRA is additional text that will be inserted into the notes buffer."
  12118. (let* ((org-log-into-drawer (org-log-into-drawer))
  12119. (drawer (cond ((stringp org-log-into-drawer)
  12120. org-log-into-drawer)
  12121. (org-log-into-drawer "LOGBOOK"))))
  12122. (save-restriction
  12123. (save-excursion
  12124. (when findpos
  12125. (org-back-to-heading t)
  12126. (narrow-to-region (point) (save-excursion
  12127. (outline-next-heading) (point)))
  12128. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"
  12129. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  12130. "[^\r\n]*\\)?"))
  12131. (goto-char (match-end 0))
  12132. (cond
  12133. (drawer
  12134. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  12135. nil t)
  12136. (progn
  12137. (goto-char (match-end 0))
  12138. (or org-log-states-order-reversed
  12139. (and (re-search-forward org-property-end-re nil t)
  12140. (goto-char (1- (match-beginning 0))))))
  12141. (insert "\n:" drawer ":\n:END:")
  12142. (beginning-of-line 0)
  12143. (org-indent-line)
  12144. (beginning-of-line 2)
  12145. (org-indent-line)
  12146. (end-of-line 0)))
  12147. ((and org-log-state-notes-insert-after-drawers
  12148. (save-excursion
  12149. (forward-line) (looking-at org-drawer-regexp)))
  12150. (forward-line)
  12151. (while (looking-at org-drawer-regexp)
  12152. (goto-char (match-end 0))
  12153. (re-search-forward org-property-end-re (point-max) t)
  12154. (forward-line))
  12155. (forward-line -1)))
  12156. (unless org-log-states-order-reversed
  12157. (and (= (char-after) ?\n) (forward-char 1))
  12158. (org-skip-over-state-notes)
  12159. (skip-chars-backward " \t\n\r")))
  12160. (move-marker org-log-note-marker (point))
  12161. (setq org-log-note-purpose purpose
  12162. org-log-note-state state
  12163. org-log-note-previous-state prev-state
  12164. org-log-note-how how
  12165. org-log-note-extra extra
  12166. org-log-note-effective-time (org-current-effective-time))
  12167. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  12168. (defun org-skip-over-state-notes ()
  12169. "Skip past the list of State notes in an entry."
  12170. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  12171. (when (ignore-errors (goto-char (org-in-item-p)))
  12172. (let* ((struct (org-list-struct))
  12173. (prevs (org-list-prevs-alist struct)))
  12174. (while (looking-at "[ \t]*- State")
  12175. (goto-char (or (org-list-get-next-item (point) struct prevs)
  12176. (org-list-get-item-end (point) struct)))))))
  12177. (defun org-add-log-note (&optional purpose)
  12178. "Pop up a window for taking a note, and add this note later at point."
  12179. (remove-hook 'post-command-hook 'org-add-log-note)
  12180. (setq org-log-note-window-configuration (current-window-configuration))
  12181. (delete-other-windows)
  12182. (move-marker org-log-note-return-to (point))
  12183. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  12184. (goto-char org-log-note-marker)
  12185. (org-switch-to-buffer-other-window "*Org Note*")
  12186. (erase-buffer)
  12187. (if (memq org-log-note-how '(time state))
  12188. (let (current-prefix-arg) (org-store-log-note))
  12189. (let ((org-inhibit-startup t)) (org-mode))
  12190. (insert (format "# Insert note for %s.
  12191. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  12192. (cond
  12193. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  12194. ((eq org-log-note-purpose 'done) "closed todo item")
  12195. ((eq org-log-note-purpose 'state)
  12196. (format "state change from \"%s\" to \"%s\""
  12197. (or org-log-note-previous-state "")
  12198. (or org-log-note-state "")))
  12199. ((eq org-log-note-purpose 'reschedule)
  12200. "rescheduling")
  12201. ((eq org-log-note-purpose 'delschedule)
  12202. "no longer scheduled")
  12203. ((eq org-log-note-purpose 'redeadline)
  12204. "changing deadline")
  12205. ((eq org-log-note-purpose 'deldeadline)
  12206. "removing deadline")
  12207. ((eq org-log-note-purpose 'refile)
  12208. "refiling")
  12209. ((eq org-log-note-purpose 'note)
  12210. "this entry")
  12211. (t (error "This should not happen")))))
  12212. (if org-log-note-extra (insert org-log-note-extra))
  12213. (org-set-local 'org-finish-function 'org-store-log-note)
  12214. (run-hooks 'org-log-buffer-setup-hook)))
  12215. (defvar org-note-abort nil) ; dynamically scoped
  12216. (defun org-store-log-note ()
  12217. "Finish taking a log note, and insert it to where it belongs."
  12218. (let ((txt (buffer-string)))
  12219. (kill-buffer (current-buffer))
  12220. (let ((note (cdr (assq org-log-note-purpose org-log-note-headings)))
  12221. lines ind bul)
  12222. (while (string-match "\\`# .*\n[ \t\n]*" txt)
  12223. (setq txt (replace-match "" t t txt)))
  12224. (if (string-match "\\s-+\\'" txt)
  12225. (setq txt (replace-match "" t t txt)))
  12226. (setq lines (org-split-string txt "\n"))
  12227. (when (and note (string-match "\\S-" note))
  12228. (setq note
  12229. (org-replace-escapes
  12230. note
  12231. (list (cons "%u" (user-login-name))
  12232. (cons "%U" user-full-name)
  12233. (cons "%t" (format-time-string
  12234. (org-time-stamp-format 'long 'inactive)
  12235. org-log-note-effective-time))
  12236. (cons "%T" (format-time-string
  12237. (org-time-stamp-format 'long nil)
  12238. org-log-note-effective-time))
  12239. (cons "%d" (format-time-string
  12240. (org-time-stamp-format nil 'inactive)
  12241. org-log-note-effective-time))
  12242. (cons "%D" (format-time-string
  12243. (org-time-stamp-format nil nil)
  12244. org-log-note-effective-time))
  12245. (cons "%s" (if org-log-note-state
  12246. (concat "\"" org-log-note-state "\"")
  12247. ""))
  12248. (cons "%S" (if org-log-note-previous-state
  12249. (concat "\"" org-log-note-previous-state "\"")
  12250. "\"\"")))))
  12251. (if lines (setq note (concat note " \\\\")))
  12252. (push note lines))
  12253. (when (or current-prefix-arg org-note-abort)
  12254. (when org-log-into-drawer
  12255. (org-remove-empty-drawer-at
  12256. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  12257. org-log-note-marker))
  12258. (setq lines nil))
  12259. (when lines
  12260. (with-current-buffer (marker-buffer org-log-note-marker)
  12261. (save-excursion
  12262. (goto-char org-log-note-marker)
  12263. (move-marker org-log-note-marker nil)
  12264. (end-of-line 1)
  12265. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  12266. (setq ind (save-excursion
  12267. (if (ignore-errors (goto-char (org-in-item-p)))
  12268. (let ((struct (org-list-struct)))
  12269. (org-list-get-ind
  12270. (org-list-get-top-point struct) struct))
  12271. (skip-chars-backward " \r\t\n")
  12272. (cond
  12273. ((and (org-at-heading-p)
  12274. org-adapt-indentation)
  12275. (1+ (org-current-level)))
  12276. ((org-at-heading-p) 0)
  12277. (t (org-get-indentation))))))
  12278. (setq bul (org-list-bullet-string "-"))
  12279. (org-indent-line-to ind)
  12280. (insert bul (pop lines))
  12281. (let ((ind-body (+ (length bul) ind)))
  12282. (while lines
  12283. (insert "\n")
  12284. (org-indent-line-to ind-body)
  12285. (insert (pop lines))))
  12286. (message "Note stored")
  12287. (org-back-to-heading t)
  12288. (org-cycle-hide-drawers 'children))
  12289. ;; Fix `buffer-undo-list' when `org-store-log-note' is called
  12290. ;; from within `org-add-log-note' because `buffer-undo-list'
  12291. ;; is then modified outside of `org-with-remote-undo'.
  12292. (when (eq this-command 'org-agenda-todo)
  12293. (setcdr buffer-undo-list (cddr buffer-undo-list)))))))
  12294. ;; Don't add undo information when called from `org-agenda-todo'
  12295. (let ((buffer-undo-list (eq this-command 'org-agenda-todo)))
  12296. (set-window-configuration org-log-note-window-configuration)
  12297. (with-current-buffer (marker-buffer org-log-note-return-to)
  12298. (goto-char org-log-note-return-to))
  12299. (move-marker org-log-note-return-to nil)
  12300. (and org-log-post-message (message "%s" org-log-post-message))))
  12301. (defun org-remove-empty-drawer-at (drawer pos)
  12302. "Remove an empty drawer DRAWER at position POS.
  12303. POS may also be a marker."
  12304. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  12305. (save-excursion
  12306. (save-restriction
  12307. (widen)
  12308. (goto-char pos)
  12309. (if (org-in-regexp
  12310. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  12311. (replace-match ""))))))
  12312. (defvar org-ts-type nil)
  12313. (defun org-sparse-tree (&optional arg type)
  12314. "Create a sparse tree, prompt for the details.
  12315. This command can create sparse trees. You first need to select the type
  12316. of match used to create the tree:
  12317. t Show all TODO entries.
  12318. T Show entries with a specific TODO keyword.
  12319. m Show entries selected by a tags/property match.
  12320. p Enter a property name and its value (both with completion on existing
  12321. names/values) and show entries with that property.
  12322. r Show entries matching a regular expression (`/' can be used as well).
  12323. b Show deadlines and scheduled items before a date.
  12324. a Show deadlines and scheduled items after a date.
  12325. d Show deadlines due within `org-deadline-warning-days'.
  12326. D Show deadlines and scheduled items between a date range."
  12327. (interactive "P")
  12328. (setq type (or type org-sparse-tree-default-date-type))
  12329. (setq org-ts-type type)
  12330. (message "Sparse tree: [/]regexp [t]odo [T]odo-kwd [m]atch [p]roperty
  12331. [d]eadlines [b]efore-date [a]fter-date [D]ates range
  12332. [c]ycle through date types: %s"
  12333. (case type
  12334. (all "all timestamps")
  12335. (scheduled "only scheduled")
  12336. (deadline "only deadline")
  12337. (active "only active timestamps")
  12338. (inactive "only inactive timestamps")
  12339. (scheduled-or-deadline "scheduled/deadline")
  12340. (closed "with a closed time-stamp")
  12341. (otherwise "scheduled/deadline")))
  12342. (let ((answer (read-char-exclusive)))
  12343. (case answer
  12344. (?c
  12345. (org-sparse-tree
  12346. arg
  12347. (cadr (memq type '(scheduled-or-deadline all scheduled deadline active
  12348. inactive closed)))))
  12349. (?d (call-interactively #'org-check-deadlines))
  12350. (?b (call-interactively #'org-check-before-date))
  12351. (?a (call-interactively #'org-check-after-date))
  12352. (?D (call-interactively #'org-check-dates-range))
  12353. (?t (call-interactively #'org-show-todo-tree))
  12354. (?T (org-show-todo-tree '(4)))
  12355. (?m (call-interactively #'org-match-sparse-tree))
  12356. ((?p ?P)
  12357. (let* ((kwd (org-icompleting-read
  12358. "Property: " (mapcar #'list (org-buffer-property-keys))))
  12359. (value (org-icompleting-read
  12360. "Value: " (mapcar #'list (org-property-values kwd)))))
  12361. (unless (string-match "\\`{.*}\\'" value)
  12362. (setq value (concat "\"" value "\"")))
  12363. (org-match-sparse-tree arg (concat kwd "=" value))))
  12364. ((?r ?R ?/) (call-interactively #'org-occur))
  12365. (otherwise (user-error "No such sparse tree command \"%c\"" answer)))))
  12366. (defvar org-occur-highlights nil
  12367. "List of overlays used for occur matches.")
  12368. (make-variable-buffer-local 'org-occur-highlights)
  12369. (defvar org-occur-parameters nil
  12370. "Parameters of the active org-occur calls.
  12371. This is a list, each call to org-occur pushes as cons cell,
  12372. containing the regular expression and the callback, onto the list.
  12373. The list can contain several entries if `org-occur' has been called
  12374. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  12375. will only contain one set of parameters. When the highlights are
  12376. removed (for example with `C-c C-c', or with the next edit (depending
  12377. on `org-remove-highlights-with-change'), this variable is emptied
  12378. as well.")
  12379. (make-variable-buffer-local 'org-occur-parameters)
  12380. (defun org-occur (regexp &optional keep-previous callback)
  12381. "Make a compact tree which shows all matches of REGEXP.
  12382. The tree will show the lines where the regexp matches, and all higher
  12383. headlines above the match. It will also show the heading after the match,
  12384. to make sure editing the matching entry is easy.
  12385. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  12386. call to `org-occur' will be kept, to allow stacking of calls to this
  12387. command.
  12388. If CALLBACK is non-nil, it is a function which is called to confirm
  12389. that the match should indeed be shown."
  12390. (interactive "sRegexp: \nP")
  12391. (when (equal regexp "")
  12392. (user-error "Regexp cannot be empty"))
  12393. (unless keep-previous
  12394. (org-remove-occur-highlights nil nil t))
  12395. (push (cons regexp callback) org-occur-parameters)
  12396. (let ((cnt 0))
  12397. (save-excursion
  12398. (goto-char (point-min))
  12399. (if (or (not keep-previous) ; do not want to keep
  12400. (not org-occur-highlights)) ; no previous matches
  12401. ;; hide everything
  12402. (org-overview))
  12403. (while (re-search-forward regexp nil t)
  12404. (when (or (not callback)
  12405. (save-match-data (funcall callback)))
  12406. (setq cnt (1+ cnt))
  12407. (when org-highlight-sparse-tree-matches
  12408. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  12409. (org-show-context 'occur-tree))))
  12410. (when org-remove-highlights-with-change
  12411. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  12412. nil 'local))
  12413. (unless org-sparse-tree-open-archived-trees
  12414. (org-hide-archived-subtrees (point-min) (point-max)))
  12415. (run-hooks 'org-occur-hook)
  12416. (if (org-called-interactively-p 'interactive)
  12417. (message "%d match(es) for regexp %s" cnt regexp))
  12418. cnt))
  12419. (defun org-occur-next-match (&optional n reset)
  12420. "Function for `next-error-function' to find sparse tree matches.
  12421. N is the number of matches to move, when negative move backwards.
  12422. RESET is entirely ignored - this function always goes back to the
  12423. starting point when no match is found."
  12424. (let* ((limit (if (< n 0) (point-min) (point-max)))
  12425. (search-func (if (< n 0)
  12426. 'previous-single-char-property-change
  12427. 'next-single-char-property-change))
  12428. (n (abs n))
  12429. (pos (point))
  12430. p1)
  12431. (catch 'exit
  12432. (while (setq p1 (funcall search-func (point) 'org-type))
  12433. (when (equal p1 limit)
  12434. (goto-char pos)
  12435. (error "No more matches"))
  12436. (when (equal (get-char-property p1 'org-type) 'org-occur)
  12437. (setq n (1- n))
  12438. (when (= n 0)
  12439. (goto-char p1)
  12440. (throw 'exit (point))))
  12441. (goto-char p1))
  12442. (goto-char p1)
  12443. (error "No more matches"))))
  12444. (defun org-show-context (&optional key)
  12445. "Make sure point and context are visible.
  12446. How much context is shown depends upon the variables
  12447. `org-show-hierarchy-above', `org-show-following-heading',
  12448. `org-show-entry-below' and `org-show-siblings'."
  12449. (let ((heading-p (org-at-heading-p t))
  12450. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  12451. (following-p (org-get-alist-option org-show-following-heading key))
  12452. (entry-p (org-get-alist-option org-show-entry-below key))
  12453. (siblings-p (org-get-alist-option org-show-siblings key)))
  12454. ;; Show heading or entry text
  12455. (if (and heading-p (not entry-p))
  12456. (org-flag-heading nil) ; only show the heading
  12457. (and (or entry-p (outline-invisible-p) (org-invisible-p2))
  12458. (org-show-hidden-entry))) ; show entire entry
  12459. (when following-p
  12460. ;; Show next sibling, or heading below text
  12461. (save-excursion
  12462. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  12463. (org-flag-heading nil))))
  12464. (when siblings-p (org-show-siblings))
  12465. (when hierarchy-p
  12466. ;; show all higher headings, possibly with siblings
  12467. (save-excursion
  12468. (while (and (condition-case nil
  12469. (progn (org-up-heading-all 1) t)
  12470. (error nil))
  12471. (not (bobp)))
  12472. (org-flag-heading nil)
  12473. (when siblings-p (org-show-siblings)))))))
  12474. (defvar org-reveal-start-hook nil
  12475. "Hook run before revealing a location.")
  12476. (defun org-reveal (&optional siblings)
  12477. "Show current entry, hierarchy above it, and the following headline.
  12478. This can be used to show a consistent set of context around locations
  12479. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  12480. not t for the search context.
  12481. With optional argument SIBLINGS, on each level of the hierarchy all
  12482. siblings are shown. This repairs the tree structure to what it would
  12483. look like when opened with hierarchical calls to `org-cycle'.
  12484. With double optional argument \\[universal-argument] \\[universal-argument], \
  12485. go to the parent and show the
  12486. entire tree."
  12487. (interactive "P")
  12488. (run-hooks 'org-reveal-start-hook)
  12489. (let ((org-show-hierarchy-above t)
  12490. (org-show-following-heading t)
  12491. (org-show-siblings (if siblings t org-show-siblings)))
  12492. (org-show-context nil))
  12493. (when (equal siblings '(16))
  12494. (save-excursion
  12495. (when (org-up-heading-safe)
  12496. (org-show-subtree)
  12497. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  12498. (defun org-highlight-new-match (beg end)
  12499. "Highlight from BEG to END and mark the highlight is an occur headline."
  12500. (let ((ov (make-overlay beg end)))
  12501. (overlay-put ov 'face 'secondary-selection)
  12502. (overlay-put ov 'org-type 'org-occur)
  12503. (push ov org-occur-highlights)))
  12504. (defun org-remove-occur-highlights (&optional beg end noremove)
  12505. "Remove the occur highlights from the buffer.
  12506. BEG and END are ignored. If NOREMOVE is nil, remove this function
  12507. from the `before-change-functions' in the current buffer."
  12508. (interactive)
  12509. (unless org-inhibit-highlight-removal
  12510. (mapc 'delete-overlay org-occur-highlights)
  12511. (setq org-occur-highlights nil)
  12512. (setq org-occur-parameters nil)
  12513. (unless noremove
  12514. (remove-hook 'before-change-functions
  12515. 'org-remove-occur-highlights 'local))))
  12516. ;;;; Priorities
  12517. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  12518. "Regular expression matching the priority indicator.")
  12519. (defvar org-remove-priority-next-time nil)
  12520. (defun org-priority-up ()
  12521. "Increase the priority of the current item."
  12522. (interactive)
  12523. (org-priority 'up))
  12524. (defun org-priority-down ()
  12525. "Decrease the priority of the current item."
  12526. (interactive)
  12527. (org-priority 'down))
  12528. (defun org-priority (&optional action show)
  12529. "Change the priority of an item.
  12530. ACTION can be `set', `up', `down', or a character."
  12531. (interactive "P")
  12532. (if (equal action '(4))
  12533. (org-show-priority)
  12534. (unless org-enable-priority-commands
  12535. (user-error "Priority commands are disabled"))
  12536. (setq action (or action 'set))
  12537. (let (current new news have remove)
  12538. (save-excursion
  12539. (org-back-to-heading t)
  12540. (if (looking-at org-priority-regexp)
  12541. (setq current (string-to-char (match-string 2))
  12542. have t))
  12543. (cond
  12544. ((eq action 'remove)
  12545. (setq remove t new ?\ ))
  12546. ((or (eq action 'set)
  12547. (if (featurep 'xemacs) (characterp action) (integerp action)))
  12548. (if (not (eq action 'set))
  12549. (setq new action)
  12550. (message "Priority %c-%c, SPC to remove: "
  12551. org-highest-priority org-lowest-priority)
  12552. (save-match-data
  12553. (setq new (read-char-exclusive))))
  12554. (if (and (= (upcase org-highest-priority) org-highest-priority)
  12555. (= (upcase org-lowest-priority) org-lowest-priority))
  12556. (setq new (upcase new)))
  12557. (cond ((equal new ?\ ) (setq remove t))
  12558. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  12559. (user-error "Priority must be between `%c' and `%c'"
  12560. org-highest-priority org-lowest-priority))))
  12561. ((eq action 'up)
  12562. (setq new (if have
  12563. (1- current) ; normal cycling
  12564. ;; last priority was empty
  12565. (if (eq last-command this-command)
  12566. org-lowest-priority ; wrap around empty to lowest
  12567. ;; default
  12568. (if org-priority-start-cycle-with-default
  12569. org-default-priority
  12570. (1- org-default-priority))))))
  12571. ((eq action 'down)
  12572. (setq new (if have
  12573. (1+ current) ; normal cycling
  12574. ;; last priority was empty
  12575. (if (eq last-command this-command)
  12576. org-highest-priority ; wrap around empty to highest
  12577. ;; default
  12578. (if org-priority-start-cycle-with-default
  12579. org-default-priority
  12580. (1+ org-default-priority))))))
  12581. (t (user-error "Invalid action")))
  12582. (if (or (< (upcase new) org-highest-priority)
  12583. (> (upcase new) org-lowest-priority))
  12584. (if (and (memq action '(up down))
  12585. (not have) (not (eq last-command this-command)))
  12586. ;; `new' is from default priority
  12587. (error
  12588. "The default can not be set, see `org-default-priority' why")
  12589. ;; normal cycling: `new' is beyond highest/lowest priority
  12590. ;; and is wrapped around to the empty priority
  12591. (setq remove t)))
  12592. (setq news (format "%c" new))
  12593. (if have
  12594. (if remove
  12595. (replace-match "" t t nil 1)
  12596. (replace-match news t t nil 2))
  12597. (if remove
  12598. (user-error "No priority cookie found in line")
  12599. (let ((case-fold-search nil))
  12600. (looking-at org-todo-line-regexp))
  12601. (if (match-end 2)
  12602. (progn
  12603. (goto-char (match-end 2))
  12604. (insert " [#" news "]"))
  12605. (goto-char (match-beginning 3))
  12606. (insert "[#" news "] "))))
  12607. (org-preserve-lc (org-set-tags nil 'align)))
  12608. (if remove
  12609. (message "Priority removed")
  12610. (message "Priority of current item set to %s" news)))))
  12611. (defun org-show-priority ()
  12612. "Show the priority of the current item.
  12613. This priority is composed of the main priority given with the [#A] cookies,
  12614. and by additional input from the age of a schedules or deadline entry."
  12615. (interactive)
  12616. (let ((pri (if (eq major-mode 'org-agenda-mode)
  12617. (org-get-at-bol 'priority)
  12618. (save-excursion
  12619. (save-match-data
  12620. (beginning-of-line)
  12621. (and (looking-at org-heading-regexp)
  12622. (org-get-priority (match-string 0))))))))
  12623. (message "Priority is %d" (if pri pri -1000))))
  12624. (defun org-get-priority (s)
  12625. "Find priority cookie and return priority."
  12626. (save-match-data
  12627. (if (functionp org-get-priority-function)
  12628. (funcall org-get-priority-function)
  12629. (if (not (string-match org-priority-regexp s))
  12630. (* 1000 (- org-lowest-priority org-default-priority))
  12631. (* 1000 (- org-lowest-priority
  12632. (string-to-char (match-string 2 s))))))))
  12633. ;;;; Tags
  12634. (defvar org-agenda-archives-mode)
  12635. (defvar org-map-continue-from nil
  12636. "Position from where mapping should continue.
  12637. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  12638. (defvar org-scanner-tags nil
  12639. "The current tag list while the tags scanner is running.")
  12640. (defvar org-trust-scanner-tags nil
  12641. "Should `org-get-tags-at' use the tags for the scanner.
  12642. This is for internal dynamical scoping only.
  12643. When this is non-nil, the function `org-get-tags-at' will return the value
  12644. of `org-scanner-tags' instead of building the list by itself. This
  12645. can lead to large speed-ups when the tags scanner is used in a file with
  12646. many entries, and when the list of tags is retrieved, for example to
  12647. obtain a list of properties. Building the tags list for each entry in such
  12648. a file becomes an N^2 operation - but with this variable set, it scales
  12649. as N.")
  12650. (defun org-scan-tags (action matcher todo-only &optional start-level)
  12651. "Scan headline tags with inheritance and produce output ACTION.
  12652. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  12653. or `agenda' to produce an entry list for an agenda view. It can also be
  12654. a Lisp form or a function that should be called at each matched headline, in
  12655. this case the return value is a list of all return values from these calls.
  12656. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  12657. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  12658. only lines with a not-done TODO keyword are included in the output.
  12659. This should be the same variable that was scoped into
  12660. and set by `org-make-tags-matcher' when it constructed MATCHER.
  12661. START-LEVEL can be a string with asterisks, reducing the scope to
  12662. headlines matching this string."
  12663. (require 'org-agenda)
  12664. (let* ((re (concat "^"
  12665. (if start-level
  12666. ;; Get the correct level to match
  12667. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  12668. org-outline-regexp)
  12669. " *\\(\\<\\("
  12670. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  12671. (org-re "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  12672. (props (list 'face 'default
  12673. 'done-face 'org-agenda-done
  12674. 'undone-face 'default
  12675. 'mouse-face 'highlight
  12676. 'org-not-done-regexp org-not-done-regexp
  12677. 'org-todo-regexp org-todo-regexp
  12678. 'org-complex-heading-regexp org-complex-heading-regexp
  12679. 'help-echo
  12680. (format "mouse-2 or RET jump to org file %s"
  12681. (abbreviate-file-name
  12682. (or (buffer-file-name (buffer-base-buffer))
  12683. (buffer-name (buffer-base-buffer)))))))
  12684. (org-map-continue-from nil)
  12685. lspos tags tags-list
  12686. (tags-alist (list (cons 0 org-file-tags)))
  12687. (llast 0) rtn rtn1 level category i txt
  12688. todo marker entry priority)
  12689. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  12690. (setq action (list 'lambda nil action)))
  12691. (save-excursion
  12692. (goto-char (point-min))
  12693. (when (eq action 'sparse-tree)
  12694. (org-overview)
  12695. (org-remove-occur-highlights))
  12696. (while (let (case-fold-search)
  12697. (re-search-forward re nil t))
  12698. (setq org-map-continue-from nil)
  12699. (catch :skip
  12700. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  12701. tags (if (match-end 4) (org-match-string-no-properties 4)))
  12702. (goto-char (setq lspos (match-beginning 0)))
  12703. (setq level (org-reduced-level (org-outline-level))
  12704. category (org-get-category))
  12705. (setq i llast llast level)
  12706. ;; remove tag lists from same and sublevels
  12707. (while (>= i level)
  12708. (when (setq entry (assoc i tags-alist))
  12709. (setq tags-alist (delete entry tags-alist)))
  12710. (setq i (1- i)))
  12711. ;; add the next tags
  12712. (when tags
  12713. (setq tags (org-split-string tags ":")
  12714. tags-alist
  12715. (cons (cons level tags) tags-alist)))
  12716. ;; compile tags for current headline
  12717. (setq tags-list
  12718. (if org-use-tag-inheritance
  12719. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  12720. tags)
  12721. org-scanner-tags tags-list)
  12722. (when org-use-tag-inheritance
  12723. (setcdr (car tags-alist)
  12724. (mapcar (lambda (x)
  12725. (setq x (copy-sequence x))
  12726. (org-add-prop-inherited x))
  12727. (cdar tags-alist))))
  12728. (when (and tags org-use-tag-inheritance
  12729. (or (not (eq t org-use-tag-inheritance))
  12730. org-tags-exclude-from-inheritance))
  12731. ;; selective inheritance, remove uninherited ones
  12732. (setcdr (car tags-alist)
  12733. (org-remove-uninherited-tags (cdar tags-alist))))
  12734. (when (and
  12735. ;; eval matcher only when the todo condition is OK
  12736. (and (or (not todo-only) (member todo org-not-done-keywords))
  12737. (let ((case-fold-search t) (org-trust-scanner-tags t))
  12738. (eval matcher)))
  12739. ;; Call the skipper, but return t if it does not skip,
  12740. ;; so that the `and' form continues evaluating
  12741. (progn
  12742. (unless (eq action 'sparse-tree) (org-agenda-skip))
  12743. t)
  12744. ;; Check if timestamps are deselecting this entry
  12745. (or (not todo-only)
  12746. (and (member todo org-not-done-keywords)
  12747. (or (not org-agenda-tags-todo-honor-ignore-options)
  12748. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item))))))
  12749. ;; select this headline
  12750. (cond
  12751. ((eq action 'sparse-tree)
  12752. (and org-highlight-sparse-tree-matches
  12753. (org-get-heading) (match-end 0)
  12754. (org-highlight-new-match
  12755. (match-beginning 1) (match-end 1)))
  12756. (org-show-context 'tags-tree))
  12757. ((eq action 'agenda)
  12758. (setq txt (org-agenda-format-item
  12759. ""
  12760. (concat
  12761. (if (eq org-tags-match-list-sublevels 'indented)
  12762. (make-string (1- level) ?.) "")
  12763. (org-get-heading))
  12764. level category
  12765. tags-list)
  12766. priority (org-get-priority txt))
  12767. (goto-char lspos)
  12768. (setq marker (org-agenda-new-marker))
  12769. (org-add-props txt props
  12770. 'org-marker marker 'org-hd-marker marker 'org-category category
  12771. 'todo-state todo
  12772. 'priority priority 'type "tagsmatch")
  12773. (push txt rtn))
  12774. ((functionp action)
  12775. (setq org-map-continue-from nil)
  12776. (save-excursion
  12777. (setq rtn1 (funcall action))
  12778. (push rtn1 rtn)))
  12779. (t (user-error "Invalid action")))
  12780. ;; if we are to skip sublevels, jump to end of subtree
  12781. (unless org-tags-match-list-sublevels
  12782. (org-end-of-subtree t)
  12783. (backward-char 1))))
  12784. ;; Get the correct position from where to continue
  12785. (if org-map-continue-from
  12786. (goto-char org-map-continue-from)
  12787. (and (= (point) lspos) (end-of-line 1)))))
  12788. (when (and (eq action 'sparse-tree)
  12789. (not org-sparse-tree-open-archived-trees))
  12790. (org-hide-archived-subtrees (point-min) (point-max)))
  12791. (nreverse rtn)))
  12792. (defun org-remove-uninherited-tags (tags)
  12793. "Remove all tags that are not inherited from the list TAGS."
  12794. (cond
  12795. ((eq org-use-tag-inheritance t)
  12796. (if org-tags-exclude-from-inheritance
  12797. (org-delete-all org-tags-exclude-from-inheritance tags)
  12798. tags))
  12799. ((not org-use-tag-inheritance) nil)
  12800. ((stringp org-use-tag-inheritance)
  12801. (delq nil (mapcar
  12802. (lambda (x)
  12803. (if (and (string-match org-use-tag-inheritance x)
  12804. (not (member x org-tags-exclude-from-inheritance)))
  12805. x nil))
  12806. tags)))
  12807. ((listp org-use-tag-inheritance)
  12808. (delq nil (mapcar
  12809. (lambda (x)
  12810. (if (member x org-use-tag-inheritance) x nil))
  12811. tags)))))
  12812. (defun org-match-sparse-tree (&optional todo-only match)
  12813. "Create a sparse tree according to tags string MATCH.
  12814. MATCH can contain positive and negative selection of tags, like
  12815. \"+WORK+URGENT-WITHBOSS\".
  12816. If optional argument TODO-ONLY is non-nil, only select lines that are
  12817. also TODO lines."
  12818. (interactive "P")
  12819. (org-agenda-prepare-buffers (list (current-buffer)))
  12820. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  12821. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  12822. (defvar org-cached-props nil)
  12823. (defun org-cached-entry-get (pom property)
  12824. (if (or (eq t org-use-property-inheritance)
  12825. (and (stringp org-use-property-inheritance)
  12826. (string-match org-use-property-inheritance property))
  12827. (and (listp org-use-property-inheritance)
  12828. (member property org-use-property-inheritance)))
  12829. ;; Caching is not possible, check it directly
  12830. (org-entry-get pom property 'inherit)
  12831. ;; Get all properties, so that we can do complicated checks easily
  12832. (cdr (assoc property (or org-cached-props
  12833. (setq org-cached-props
  12834. (org-entry-properties pom)))))))
  12835. (defun org-global-tags-completion-table (&optional files)
  12836. "Return the list of all tags in all agenda buffer/files.
  12837. Optional FILES argument is a list of files which can be used
  12838. instead of the agenda files."
  12839. (save-excursion
  12840. (org-uniquify
  12841. (delq nil
  12842. (apply 'append
  12843. (mapcar
  12844. (lambda (file)
  12845. (set-buffer (find-file-noselect file))
  12846. (append (org-get-buffer-tags)
  12847. (mapcar (lambda (x) (if (stringp (car-safe x))
  12848. (list (car-safe x)) nil))
  12849. org-tag-alist)))
  12850. (if (and files (car files))
  12851. files
  12852. (org-agenda-files))))))))
  12853. (defun org-make-tags-matcher (match)
  12854. "Create the TAGS/TODO matcher form for the selection string MATCH.
  12855. The variable `todo-only' is scoped dynamically into this function.
  12856. It will be set to t if the matcher restricts matching to TODO entries,
  12857. otherwise will not be touched.
  12858. Returns a cons of the selection string MATCH and the constructed
  12859. lisp form implementing the matcher. The matcher is to be evaluated
  12860. at an Org entry, with point on the headline, and returns t if the
  12861. entry matches the selection string MATCH. The returned lisp form
  12862. references two variables with information about the entry, which
  12863. must be bound around the form's evaluation: todo, the TODO keyword
  12864. at the entry (or nil of none); and tags-list, the list of all tags
  12865. at the entry including inherited ones. Additionally, the category
  12866. of the entry (if any) must be specified as the text property
  12867. 'org-category on the headline.
  12868. See also `org-scan-tags'.
  12869. "
  12870. (declare (special todo-only))
  12871. (unless (boundp 'todo-only)
  12872. (error "`org-make-tags-matcher' expects todo-only to be scoped in"))
  12873. (unless match
  12874. ;; Get a new match request, with completion against the global
  12875. ;; tags table and the local tags in current buffer
  12876. (let ((org-last-tags-completion-table
  12877. (org-uniquify
  12878. (delq nil (append (org-get-buffer-tags)
  12879. (org-global-tags-completion-table))))))
  12880. (setq match (org-completing-read-no-i
  12881. "Match: " 'org-tags-completion-function nil nil nil
  12882. 'org-tags-history))))
  12883. ;; Parse the string and create a lisp form
  12884. (let ((match0 match)
  12885. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  12886. minus tag mm
  12887. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  12888. orterms term orlist re-p str-p level-p level-op time-p
  12889. prop-p pn pv po gv rest (start 0) (ss 0))
  12890. ;; Expand group tags
  12891. (setq match (org-tags-expand match))
  12892. ;; Check if there is a TODO part of this match, which would be the
  12893. ;; part after a "/". TO make sure that this slash is not part of
  12894. ;; a property value to be matched against, we also check that there
  12895. ;; is no " after that slash.
  12896. ;; First, find the last slash
  12897. (while (string-match "/+" match ss)
  12898. (setq start (match-beginning 0) ss (match-end 0)))
  12899. (if (and (string-match "/+" match start)
  12900. (not (save-match-data (string-match "\"" match start))))
  12901. ;; match contains also a todo-matching request
  12902. (progn
  12903. (setq tagsmatch (substring match 0 (match-beginning 0))
  12904. todomatch (substring match (match-end 0)))
  12905. (if (string-match "^!" todomatch)
  12906. (setq todo-only t todomatch (substring todomatch 1)))
  12907. (if (string-match "^\\s-*$" todomatch)
  12908. (setq todomatch nil)))
  12909. ;; only matching tags
  12910. (setq tagsmatch match todomatch nil))
  12911. ;; Make the tags matcher
  12912. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  12913. (setq tagsmatcher t)
  12914. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  12915. (while (setq term (pop orterms))
  12916. (while (and (equal (substring term -1) "\\") orterms)
  12917. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  12918. (while (string-match re term)
  12919. (setq rest (substring term (match-end 0))
  12920. minus (and (match-end 1)
  12921. (equal (match-string 1 term) "-"))
  12922. tag (save-match-data (replace-regexp-in-string
  12923. "\\\\-" "-"
  12924. (match-string 2 term)))
  12925. re-p (equal (string-to-char tag) ?{)
  12926. level-p (match-end 4)
  12927. prop-p (match-end 5)
  12928. mm (cond
  12929. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  12930. (level-p
  12931. (setq level-op (org-op-to-function (match-string 3 term)))
  12932. `(,level-op level ,(string-to-number
  12933. (match-string 4 term))))
  12934. (prop-p
  12935. (setq pn (match-string 5 term)
  12936. po (match-string 6 term)
  12937. pv (match-string 7 term)
  12938. re-p (equal (string-to-char pv) ?{)
  12939. str-p (equal (string-to-char pv) ?\")
  12940. time-p (save-match-data
  12941. (string-match "^\"[[<].*[]>]\"$" pv))
  12942. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  12943. (if time-p (setq pv (org-matcher-time pv)))
  12944. (setq po (org-op-to-function po (if time-p 'time str-p)))
  12945. (cond
  12946. ((equal pn "CATEGORY")
  12947. (setq gv '(get-text-property (point) 'org-category)))
  12948. ((equal pn "TODO")
  12949. (setq gv 'todo))
  12950. (t
  12951. (setq gv `(org-cached-entry-get nil ,pn))))
  12952. (if re-p
  12953. (if (eq po 'org<>)
  12954. `(not (string-match ,pv (or ,gv "")))
  12955. `(string-match ,pv (or ,gv "")))
  12956. (if str-p
  12957. `(,po (or ,gv "") ,pv)
  12958. `(,po (string-to-number (or ,gv ""))
  12959. ,(string-to-number pv) ))))
  12960. (t `(member ,tag tags-list)))
  12961. mm (if minus (list 'not mm) mm)
  12962. term rest)
  12963. (push mm tagsmatcher))
  12964. (push (if (> (length tagsmatcher) 1)
  12965. (cons 'and tagsmatcher)
  12966. (car tagsmatcher))
  12967. orlist)
  12968. (setq tagsmatcher nil))
  12969. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  12970. (setq tagsmatcher
  12971. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  12972. ;; Make the todo matcher
  12973. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  12974. (setq todomatcher t)
  12975. (setq orterms (org-split-string todomatch "|") orlist nil)
  12976. (while (setq term (pop orterms))
  12977. (while (string-match re term)
  12978. (setq minus (and (match-end 1)
  12979. (equal (match-string 1 term) "-"))
  12980. kwd (match-string 2 term)
  12981. re-p (equal (string-to-char kwd) ?{)
  12982. term (substring term (match-end 0))
  12983. mm (if re-p
  12984. `(string-match ,(substring kwd 1 -1) todo)
  12985. (list 'equal 'todo kwd))
  12986. mm (if minus (list 'not mm) mm))
  12987. (push mm todomatcher))
  12988. (push (if (> (length todomatcher) 1)
  12989. (cons 'and todomatcher)
  12990. (car todomatcher))
  12991. orlist)
  12992. (setq todomatcher nil))
  12993. (setq todomatcher (if (> (length orlist) 1)
  12994. (cons 'or orlist) (car orlist))))
  12995. ;; Return the string and lisp forms of the matcher
  12996. (setq matcher (if todomatcher
  12997. (list 'and tagsmatcher todomatcher)
  12998. tagsmatcher))
  12999. (when todo-only
  13000. (setq matcher (list 'and '(member todo org-not-done-keywords)
  13001. matcher)))
  13002. (cons match0 matcher)))
  13003. (defun org-tags-expand (match &optional single-as-list downcased)
  13004. "Expand group tags in MATCH.
  13005. This replaces every group tag in MATCH with a regexp tag search.
  13006. For example, a group tag \"Work\" defined as { Work : Lab Conf }
  13007. will be replaced like this:
  13008. Work => {\\(?:Work\\|Lab\\|Conf\\)}
  13009. +Work => +{\\(?:Work\\|Lab\\|Conf\\)}
  13010. -Work => -{\\(?:Work\\|Lab\\|Conf\\)}
  13011. Replacing by a regexp preserves the structure of the match.
  13012. E.g., this expansion
  13013. Work|Home => {\\(?:Work\\|Lab\\|Conf\\}|Home
  13014. will match anything tagged with \"Lab\" and \"Home\", or tagged
  13015. with \"Conf\" and \"Home\" or tagged with \"Work\" and \"home\".
  13016. When the optional argument SINGLE-AS-LIST is non-nil, MATCH is
  13017. assumed to be a single group tag, and the function will return
  13018. the list of tags in this group.
  13019. When DOWNCASE is non-nil, expand downcased TAGS."
  13020. (if org-group-tags
  13021. (let* ((case-fold-search t)
  13022. (stable org-mode-syntax-table)
  13023. (tal (or org-tag-groups-alist-for-agenda
  13024. org-tag-groups-alist))
  13025. (tal (if downcased
  13026. (mapcar (lambda(tg) (mapcar 'downcase tg)) tal) tal))
  13027. (tml (mapcar 'car tal))
  13028. (rtnmatch match) rpl)
  13029. ;; @ and _ are allowed as word-components in tags
  13030. (modify-syntax-entry ?@ "w" stable)
  13031. (modify-syntax-entry ?_ "w" stable)
  13032. (while (and tml
  13033. (with-syntax-table stable
  13034. (string-match
  13035. (concat "\\(?1:[+-]?\\)\\(?2:\\<"
  13036. (regexp-opt tml) "\\>\\)") rtnmatch)))
  13037. (let* ((dir (match-string 1 rtnmatch))
  13038. (tag (match-string 2 rtnmatch))
  13039. (tag (if downcased (downcase tag) tag)))
  13040. (setq tml (delete tag tml))
  13041. (when (not (get-text-property 0 'grouptag (match-string 2 rtnmatch)))
  13042. (setq rpl (append (org-uniquify rpl) (assoc tag tal)))
  13043. (setq rpl (concat dir "{\\<" (regexp-opt rpl) "\\>}"))
  13044. (if (stringp rpl) (org-add-props rpl '(grouptag t)))
  13045. (setq rtnmatch (replace-match rpl t t rtnmatch)))))
  13046. (if single-as-list
  13047. (or (reverse rpl) (list rtnmatch))
  13048. rtnmatch))
  13049. (if single-as-list (list (if downcased (downcase match) match))
  13050. match)))
  13051. (defun org-op-to-function (op &optional stringp)
  13052. "Turn an operator into the appropriate function."
  13053. (setq op
  13054. (cond
  13055. ((equal op "<" ) '(< string< org-time<))
  13056. ((equal op ">" ) '(> org-string> org-time>))
  13057. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  13058. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  13059. ((member op '("=" "==")) '(= string= org-time=))
  13060. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  13061. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  13062. (defun org<> (a b) (not (= a b)))
  13063. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  13064. (defun org-string>= (a b) (not (string< a b)))
  13065. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  13066. (defun org-string<> (a b) (not (string= a b)))
  13067. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  13068. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  13069. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  13070. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  13071. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  13072. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  13073. (defun org-2ft (s)
  13074. "Convert S to a floating point time.
  13075. If S is already a number, just return it. If it is a string, parse
  13076. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  13077. (cond
  13078. ((numberp s) s)
  13079. ((stringp s)
  13080. (condition-case nil
  13081. (float-time (apply 'encode-time (org-parse-time-string s)))
  13082. (error 0.)))
  13083. (t 0.)))
  13084. (defun org-time-today ()
  13085. "Time in seconds today at 0:00.
  13086. Returns the float number of seconds since the beginning of the
  13087. epoch to the beginning of today (00:00)."
  13088. (float-time (apply 'encode-time
  13089. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  13090. (defun org-matcher-time (s)
  13091. "Interpret a time comparison value."
  13092. (save-match-data
  13093. (cond
  13094. ((string= s "<now>") (float-time))
  13095. ((string= s "<today>") (org-time-today))
  13096. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  13097. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  13098. ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
  13099. (+ (org-time-today)
  13100. (* (string-to-number (match-string 1 s))
  13101. (cdr (assoc (match-string 2 s)
  13102. '(("d" . 86400.0) ("w" . 604800.0)
  13103. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  13104. (t (org-2ft s)))))
  13105. (defun org-match-any-p (re list)
  13106. "Does re match any element of list?"
  13107. (setq list (mapcar (lambda (x) (string-match re x)) list))
  13108. (delq nil list))
  13109. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  13110. (defvar org-tags-overlay (make-overlay 1 1))
  13111. (org-detach-overlay org-tags-overlay)
  13112. (defun org-get-local-tags-at (&optional pos)
  13113. "Get a list of tags defined in the current headline."
  13114. (org-get-tags-at pos 'local))
  13115. (defun org-get-local-tags ()
  13116. "Get a list of tags defined in the current headline."
  13117. (org-get-tags-at nil 'local))
  13118. (defun org-get-tags-at (&optional pos local)
  13119. "Get a list of all headline tags applicable at POS.
  13120. POS defaults to point. If tags are inherited, the list contains
  13121. the targets in the same sequence as the headlines appear, i.e.
  13122. the tags of the current headline come last.
  13123. When LOCAL is non-nil, only return tags from the current headline,
  13124. ignore inherited ones."
  13125. (interactive)
  13126. (if (and org-trust-scanner-tags
  13127. (or (not pos) (equal pos (point)))
  13128. (not local))
  13129. org-scanner-tags
  13130. (let (tags ltags lastpos parent)
  13131. (save-excursion
  13132. (save-restriction
  13133. (widen)
  13134. (goto-char (or pos (point)))
  13135. (save-match-data
  13136. (catch 'done
  13137. (condition-case nil
  13138. (progn
  13139. (org-back-to-heading t)
  13140. (while (not (equal lastpos (point)))
  13141. (setq lastpos (point))
  13142. (when (looking-at
  13143. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  13144. (setq ltags (org-split-string
  13145. (org-match-string-no-properties 1) ":"))
  13146. (when parent
  13147. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  13148. (setq tags (append
  13149. (if parent
  13150. (org-remove-uninherited-tags ltags)
  13151. ltags)
  13152. tags)))
  13153. (or org-use-tag-inheritance (throw 'done t))
  13154. (if local (throw 'done t))
  13155. (or (org-up-heading-safe) (error nil))
  13156. (setq parent t)))
  13157. (error nil)))))
  13158. (if local
  13159. tags
  13160. (reverse (delete-dups
  13161. (reverse (append
  13162. (org-remove-uninherited-tags
  13163. org-file-tags) tags)))))))))
  13164. (defun org-add-prop-inherited (s)
  13165. (add-text-properties 0 (length s) '(inherited t) s)
  13166. s)
  13167. (defun org-toggle-tag (tag &optional onoff)
  13168. "Toggle the tag TAG for the current line.
  13169. If ONOFF is `on' or `off', don't toggle but set to this state."
  13170. (let (res current)
  13171. (save-excursion
  13172. (org-back-to-heading t)
  13173. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  13174. (point-at-eol) t)
  13175. (progn
  13176. (setq current (match-string 1))
  13177. (replace-match ""))
  13178. (setq current ""))
  13179. (setq current (nreverse (org-split-string current ":")))
  13180. (cond
  13181. ((eq onoff 'on)
  13182. (setq res t)
  13183. (or (member tag current) (push tag current)))
  13184. ((eq onoff 'off)
  13185. (or (not (member tag current)) (setq current (delete tag current))))
  13186. (t (if (member tag current)
  13187. (setq current (delete tag current))
  13188. (setq res t)
  13189. (push tag current))))
  13190. (end-of-line 1)
  13191. (if current
  13192. (progn
  13193. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  13194. (org-set-tags nil t))
  13195. (delete-horizontal-space))
  13196. (run-hooks 'org-after-tags-change-hook))
  13197. res))
  13198. (defun org-align-tags-here (to-col)
  13199. ;; Assumes that this is a headline
  13200. "Align tags on the current headline to TO-COL."
  13201. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  13202. (beginning-of-line 1)
  13203. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13204. (< pos (match-beginning 2)))
  13205. (progn
  13206. (setq tags-l (- (match-end 2) (match-beginning 2)))
  13207. (goto-char (match-beginning 1))
  13208. (insert " ")
  13209. (delete-region (point) (1+ (match-beginning 2)))
  13210. (setq ncol (max (current-column)
  13211. (1+ col)
  13212. (if (> to-col 0)
  13213. to-col
  13214. (- (abs to-col) tags-l))))
  13215. (setq p (point))
  13216. (insert (make-string (- ncol (current-column)) ?\ ))
  13217. (setq ncol (current-column))
  13218. (when indent-tabs-mode (tabify p (point-at-eol)))
  13219. (org-move-to-column (min ncol col)))
  13220. (goto-char pos))))
  13221. (defun org-set-tags-command (&optional arg just-align)
  13222. "Call the set-tags command for the current entry."
  13223. (interactive "P")
  13224. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  13225. (org-set-tags arg just-align)
  13226. (save-excursion
  13227. (unless (and (org-region-active-p)
  13228. org-loop-over-headlines-in-active-region)
  13229. (org-back-to-heading t))
  13230. (org-set-tags arg just-align))))
  13231. (defun org-set-tags-to (data)
  13232. "Set the tags of the current entry to DATA, replacing the current tags.
  13233. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  13234. If DATA is nil or the empty string, any tags will be removed."
  13235. (interactive "sTags: ")
  13236. (setq data
  13237. (cond
  13238. ((eq data nil) "")
  13239. ((equal data "") "")
  13240. ((stringp data)
  13241. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  13242. ":"))
  13243. ((listp data)
  13244. (concat ":" (mapconcat 'identity data ":") ":"))))
  13245. (when data
  13246. (save-excursion
  13247. (org-back-to-heading t)
  13248. (when (looking-at org-complex-heading-regexp)
  13249. (if (match-end 5)
  13250. (progn
  13251. (goto-char (match-beginning 5))
  13252. (insert data)
  13253. (delete-region (point) (point-at-eol))
  13254. (org-set-tags nil 'align))
  13255. (goto-char (point-at-eol))
  13256. (insert " " data)
  13257. (org-set-tags nil 'align)))
  13258. (beginning-of-line 1)
  13259. (if (looking-at ".*?\\([ \t]+\\)$")
  13260. (delete-region (match-beginning 1) (match-end 1))))))
  13261. (defun org-align-all-tags ()
  13262. "Align the tags i all headings."
  13263. (interactive)
  13264. (save-excursion
  13265. (or (ignore-errors (org-back-to-heading t))
  13266. (outline-next-heading))
  13267. (if (org-at-heading-p)
  13268. (org-set-tags t)
  13269. (message "No headings"))))
  13270. (defvar org-indent-indentation-per-level)
  13271. (defun org-set-tags (&optional arg just-align)
  13272. "Set the tags for the current headline.
  13273. With prefix ARG, realign all tags in headings in the current buffer.
  13274. When JUST-ALIGN is non-nil, only align tags."
  13275. (interactive "P")
  13276. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  13277. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  13278. 'region-start-level 'region))
  13279. org-loop-over-headlines-in-active-region)
  13280. (org-map-entries
  13281. ;; We don't use ARG and JUST-ALIGN here because these args
  13282. ;; are not useful when looping over headlines.
  13283. `(org-set-tags)
  13284. org-loop-over-headlines-in-active-region
  13285. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  13286. (let* ((re org-outline-regexp-bol)
  13287. (current (unless arg (org-get-tags-string)))
  13288. (col (current-column))
  13289. (org-setting-tags t)
  13290. table current-tags inherited-tags ; computed below when needed
  13291. tags p0 c0 c1 rpl di tc level)
  13292. (if arg
  13293. (save-excursion
  13294. (goto-char (point-min))
  13295. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  13296. (while (re-search-forward re nil t)
  13297. (org-set-tags nil t)
  13298. (end-of-line 1)))
  13299. (message "All tags realigned to column %d" org-tags-column))
  13300. (if just-align
  13301. (setq tags current)
  13302. ;; Get a new set of tags from the user
  13303. (save-excursion
  13304. (setq table (append org-tag-persistent-alist
  13305. (or org-tag-alist (org-get-buffer-tags))
  13306. (and
  13307. org-complete-tags-always-offer-all-agenda-tags
  13308. (org-global-tags-completion-table
  13309. (org-agenda-files))))
  13310. org-last-tags-completion-table table
  13311. current-tags (org-split-string current ":")
  13312. inherited-tags (nreverse
  13313. (nthcdr (length current-tags)
  13314. (nreverse (org-get-tags-at))))
  13315. tags
  13316. (if (or (eq t org-use-fast-tag-selection)
  13317. (and org-use-fast-tag-selection
  13318. (delq nil (mapcar 'cdr table))))
  13319. (org-fast-tag-selection
  13320. current-tags inherited-tags table
  13321. (if org-fast-tag-selection-include-todo
  13322. org-todo-key-alist))
  13323. (let ((org-add-colon-after-tag-completion (< 1 (length table))))
  13324. (org-trim
  13325. (org-icompleting-read "Tags: "
  13326. 'org-tags-completion-function
  13327. nil nil current 'org-tags-history))))))
  13328. (while (string-match "[-+&]+" tags)
  13329. ;; No boolean logic, just a list
  13330. (setq tags (replace-match ":" t t tags))))
  13331. (setq tags (replace-regexp-in-string "[,]" ":" tags))
  13332. (if org-tags-sort-function
  13333. (setq tags (mapconcat 'identity
  13334. (sort (org-split-string
  13335. tags (org-re "[^[:alnum:]_@#%]+"))
  13336. org-tags-sort-function) ":")))
  13337. (if (string-match "\\`[\t ]*\\'" tags)
  13338. (setq tags "")
  13339. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  13340. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  13341. ;; Insert new tags at the correct column
  13342. (beginning-of-line 1)
  13343. (setq level (or (and (looking-at org-outline-regexp)
  13344. (- (match-end 0) (point) 1))
  13345. 1))
  13346. (cond
  13347. ((and (equal current "") (equal tags "")))
  13348. ((re-search-forward
  13349. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  13350. (point-at-eol) t)
  13351. (if (equal tags "")
  13352. (setq rpl "")
  13353. (goto-char (match-beginning 0))
  13354. (setq c0 (current-column)
  13355. ;; compute offset for the case of org-indent-mode active
  13356. di (if (org-bound-and-true-p org-indent-mode)
  13357. (* (1- org-indent-indentation-per-level) (1- level))
  13358. 0)
  13359. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  13360. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  13361. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
  13362. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  13363. (replace-match rpl t t)
  13364. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  13365. tags)
  13366. (t (error "Tags alignment failed")))
  13367. (org-move-to-column col)
  13368. (unless just-align
  13369. (run-hooks 'org-after-tags-change-hook))))))
  13370. (defun org-change-tag-in-region (beg end tag off)
  13371. "Add or remove TAG for each entry in the region.
  13372. This works in the agenda, and also in an org-mode buffer."
  13373. (interactive
  13374. (list (region-beginning) (region-end)
  13375. (let ((org-last-tags-completion-table
  13376. (if (derived-mode-p 'org-mode)
  13377. (org-uniquify
  13378. (delq nil (append (org-get-buffer-tags)
  13379. (org-global-tags-completion-table))))
  13380. (org-global-tags-completion-table))))
  13381. (org-icompleting-read
  13382. "Tag: " 'org-tags-completion-function nil nil nil
  13383. 'org-tags-history))
  13384. (progn
  13385. (message "[s]et or [r]emove? ")
  13386. (equal (read-char-exclusive) ?r))))
  13387. (if (fboundp 'deactivate-mark) (deactivate-mark))
  13388. (let ((agendap (equal major-mode 'org-agenda-mode))
  13389. l1 l2 m buf pos newhead (cnt 0))
  13390. (goto-char end)
  13391. (setq l2 (1- (org-current-line)))
  13392. (goto-char beg)
  13393. (setq l1 (org-current-line))
  13394. (loop for l from l1 to l2 do
  13395. (org-goto-line l)
  13396. (setq m (get-text-property (point) 'org-hd-marker))
  13397. (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
  13398. (and agendap m))
  13399. (setq buf (if agendap (marker-buffer m) (current-buffer))
  13400. pos (if agendap m (point)))
  13401. (with-current-buffer buf
  13402. (save-excursion
  13403. (save-restriction
  13404. (goto-char pos)
  13405. (setq cnt (1+ cnt))
  13406. (org-toggle-tag tag (if off 'off 'on))
  13407. (setq newhead (org-get-heading)))))
  13408. (and agendap (org-agenda-change-all-lines newhead m))))
  13409. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  13410. (defun org-tags-completion-function (string predicate &optional flag)
  13411. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  13412. (confirm (lambda (x) (stringp (car x)))))
  13413. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  13414. (setq s1 (match-string 1 string)
  13415. s2 (match-string 2 string))
  13416. (setq s1 "" s2 string))
  13417. (cond
  13418. ((eq flag nil)
  13419. ;; try completion
  13420. (setq rtn (try-completion s2 ctable confirm))
  13421. (if (stringp rtn)
  13422. (setq rtn
  13423. (concat s1 s2 (substring rtn (length s2))
  13424. (if (and org-add-colon-after-tag-completion
  13425. (assoc rtn ctable))
  13426. ":" ""))))
  13427. rtn)
  13428. ((eq flag t)
  13429. ;; all-completions
  13430. (all-completions s2 ctable confirm))
  13431. ((eq flag 'lambda)
  13432. ;; exact match?
  13433. (assoc s2 ctable)))))
  13434. (defun org-fast-tag-insert (kwd tags face &optional end)
  13435. "Insert KDW, and the TAGS, the latter with face FACE.
  13436. Also insert END."
  13437. (insert (format "%-12s" (concat kwd ":"))
  13438. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  13439. (or end "")))
  13440. (defun org-fast-tag-show-exit (flag)
  13441. (save-excursion
  13442. (org-goto-line 3)
  13443. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  13444. (replace-match ""))
  13445. (when flag
  13446. (end-of-line 1)
  13447. (org-move-to-column (- (window-width) 19) t)
  13448. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  13449. (defun org-set-current-tags-overlay (current prefix)
  13450. "Add an overlay to CURRENT tag with PREFIX."
  13451. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  13452. (if (featurep 'xemacs)
  13453. (org-overlay-display org-tags-overlay (concat prefix s)
  13454. 'secondary-selection)
  13455. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  13456. (org-overlay-display org-tags-overlay (concat prefix s)))))
  13457. (defvar org-last-tag-selection-key nil)
  13458. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  13459. "Fast tag selection with single keys.
  13460. CURRENT is the current list of tags in the headline, INHERITED is the
  13461. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  13462. possibly with grouping information. TODO-TABLE is a similar table with
  13463. TODO keywords, should these have keys assigned to them.
  13464. If the keys are nil, a-z are automatically assigned.
  13465. Returns the new tags string, or nil to not change the current settings."
  13466. (let* ((fulltable (append table todo-table))
  13467. (maxlen (apply 'max (mapcar
  13468. (lambda (x)
  13469. (if (stringp (car x)) (string-width (car x)) 0))
  13470. fulltable)))
  13471. (buf (current-buffer))
  13472. (expert (eq org-fast-tag-selection-single-key 'expert))
  13473. (buffer-tags nil)
  13474. (fwidth (+ maxlen 3 1 3))
  13475. (ncol (/ (- (window-width) 4) fwidth))
  13476. (i-face 'org-done)
  13477. (c-face 'org-todo)
  13478. tg cnt e c char c1 c2 ntable tbl rtn
  13479. ov-start ov-end ov-prefix
  13480. (exit-after-next org-fast-tag-selection-single-key)
  13481. (done-keywords org-done-keywords)
  13482. groups ingroup)
  13483. (save-excursion
  13484. (beginning-of-line 1)
  13485. (if (looking-at
  13486. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13487. (setq ov-start (match-beginning 1)
  13488. ov-end (match-end 1)
  13489. ov-prefix "")
  13490. (setq ov-start (1- (point-at-eol))
  13491. ov-end (1+ ov-start))
  13492. (skip-chars-forward "^\n\r")
  13493. (setq ov-prefix
  13494. (concat
  13495. (buffer-substring (1- (point)) (point))
  13496. (if (> (current-column) org-tags-column)
  13497. " "
  13498. (make-string (- org-tags-column (current-column)) ?\ ))))))
  13499. (move-overlay org-tags-overlay ov-start ov-end)
  13500. (save-window-excursion
  13501. (if expert
  13502. (set-buffer (get-buffer-create " *Org tags*"))
  13503. (delete-other-windows)
  13504. (set-window-buffer (split-window-vertically) (get-buffer-create " *Org tags*"))
  13505. (org-switch-to-buffer-other-window " *Org tags*"))
  13506. (erase-buffer)
  13507. (org-set-local 'org-done-keywords done-keywords)
  13508. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  13509. (org-fast-tag-insert "Current" current c-face "\n\n")
  13510. (org-fast-tag-show-exit exit-after-next)
  13511. (org-set-current-tags-overlay current ov-prefix)
  13512. (setq tbl fulltable char ?a cnt 0)
  13513. (while (setq e (pop tbl))
  13514. (cond
  13515. ((equal (car e) :startgroup)
  13516. (push '() groups) (setq ingroup t)
  13517. (when (not (= cnt 0))
  13518. (setq cnt 0)
  13519. (insert "\n"))
  13520. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  13521. ((equal (car e) :endgroup)
  13522. (setq ingroup nil cnt 0)
  13523. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  13524. ((equal e '(:newline))
  13525. (when (not (= cnt 0))
  13526. (setq cnt 0)
  13527. (insert "\n")
  13528. (setq e (car tbl))
  13529. (while (equal (car tbl) '(:newline))
  13530. (insert "\n")
  13531. (setq tbl (cdr tbl)))))
  13532. ((equal e '(:grouptags)) nil)
  13533. (t
  13534. (setq tg (copy-sequence (car e)) c2 nil)
  13535. (if (cdr e)
  13536. (setq c (cdr e))
  13537. ;; automatically assign a character.
  13538. (setq c1 (string-to-char
  13539. (downcase (substring
  13540. tg (if (= (string-to-char tg) ?@) 1 0)))))
  13541. (if (or (rassoc c1 ntable) (rassoc c1 table))
  13542. (while (or (rassoc char ntable) (rassoc char table))
  13543. (setq char (1+ char)))
  13544. (setq c2 c1))
  13545. (setq c (or c2 char)))
  13546. (if ingroup (push tg (car groups)))
  13547. (setq tg (org-add-props tg nil 'face
  13548. (cond
  13549. ((not (assoc tg table))
  13550. (org-get-todo-face tg))
  13551. ((member tg current) c-face)
  13552. ((member tg inherited) i-face))))
  13553. (if (equal (caar tbl) :grouptags)
  13554. (org-add-props tg nil 'face 'org-tag-group))
  13555. (if (and (= cnt 0) (not ingroup)) (insert " "))
  13556. (insert "[" c "] " tg (make-string
  13557. (- fwidth 4 (length tg)) ?\ ))
  13558. (push (cons tg c) ntable)
  13559. (when (= (setq cnt (1+ cnt)) ncol)
  13560. (insert "\n")
  13561. (if ingroup (insert " "))
  13562. (setq cnt 0)))))
  13563. (setq ntable (nreverse ntable))
  13564. (insert "\n")
  13565. (goto-char (point-min))
  13566. (if (not expert) (org-fit-window-to-buffer))
  13567. (setq rtn
  13568. (catch 'exit
  13569. (while t
  13570. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  13571. (if (not groups) "no " "")
  13572. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  13573. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  13574. (setq org-last-tag-selection-key c)
  13575. (cond
  13576. ((= c ?\r) (throw 'exit t))
  13577. ((= c ?!)
  13578. (setq groups (not groups))
  13579. (goto-char (point-min))
  13580. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  13581. ((= c ?\C-c)
  13582. (if (not expert)
  13583. (org-fast-tag-show-exit
  13584. (setq exit-after-next (not exit-after-next)))
  13585. (setq expert nil)
  13586. (delete-other-windows)
  13587. (set-window-buffer (split-window-vertically) " *Org tags*")
  13588. (org-switch-to-buffer-other-window " *Org tags*")
  13589. (org-fit-window-to-buffer)))
  13590. ((or (= c ?\C-g)
  13591. (and (= c ?q) (not (rassoc c ntable))))
  13592. (org-detach-overlay org-tags-overlay)
  13593. (setq quit-flag t))
  13594. ((= c ?\ )
  13595. (setq current nil)
  13596. (if exit-after-next (setq exit-after-next 'now)))
  13597. ((= c ?\t)
  13598. (condition-case nil
  13599. (setq tg (org-icompleting-read
  13600. "Tag: "
  13601. (or buffer-tags
  13602. (with-current-buffer buf
  13603. (org-get-buffer-tags)))))
  13604. (quit (setq tg "")))
  13605. (when (string-match "\\S-" tg)
  13606. (add-to-list 'buffer-tags (list tg))
  13607. (if (member tg current)
  13608. (setq current (delete tg current))
  13609. (push tg current)))
  13610. (if exit-after-next (setq exit-after-next 'now)))
  13611. ((setq e (rassoc c todo-table) tg (car e))
  13612. (with-current-buffer buf
  13613. (save-excursion (org-todo tg)))
  13614. (if exit-after-next (setq exit-after-next 'now)))
  13615. ((setq e (rassoc c ntable) tg (car e))
  13616. (if (member tg current)
  13617. (setq current (delete tg current))
  13618. (loop for g in groups do
  13619. (if (member tg g)
  13620. (mapc (lambda (x)
  13621. (setq current (delete x current)))
  13622. g)))
  13623. (push tg current))
  13624. (if exit-after-next (setq exit-after-next 'now))))
  13625. ;; Create a sorted list
  13626. (setq current
  13627. (sort current
  13628. (lambda (a b)
  13629. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  13630. (if (eq exit-after-next 'now) (throw 'exit t))
  13631. (goto-char (point-min))
  13632. (beginning-of-line 2)
  13633. (delete-region (point) (point-at-eol))
  13634. (org-fast-tag-insert "Current" current c-face)
  13635. (org-set-current-tags-overlay current ov-prefix)
  13636. (while (re-search-forward
  13637. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  13638. (setq tg (match-string 1))
  13639. (add-text-properties
  13640. (match-beginning 1) (match-end 1)
  13641. (list 'face
  13642. (cond
  13643. ((member tg current) c-face)
  13644. ((member tg inherited) i-face)
  13645. (t (get-text-property (match-beginning 1) 'face))))))
  13646. (goto-char (point-min)))))
  13647. (org-detach-overlay org-tags-overlay)
  13648. (if rtn
  13649. (mapconcat 'identity current ":")
  13650. nil))))
  13651. (defun org-get-tags-string ()
  13652. "Get the TAGS string in the current headline."
  13653. (unless (org-at-heading-p t)
  13654. (user-error "Not on a heading"))
  13655. (save-excursion
  13656. (beginning-of-line 1)
  13657. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13658. (org-match-string-no-properties 1)
  13659. "")))
  13660. (defun org-get-tags ()
  13661. "Get the list of tags specified in the current headline."
  13662. (org-split-string (org-get-tags-string) ":"))
  13663. (defun org-get-buffer-tags ()
  13664. "Get a table of all tags used in the buffer, for completion."
  13665. (let (tags)
  13666. (save-excursion
  13667. (goto-char (point-min))
  13668. (while (re-search-forward
  13669. (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
  13670. (when (equal (char-after (point-at-bol 0)) ?*)
  13671. (mapc (lambda (x) (add-to-list 'tags x))
  13672. (org-split-string (org-match-string-no-properties 1) ":")))))
  13673. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  13674. (mapcar 'list tags)))
  13675. ;;;; The mapping API
  13676. (defun org-map-entries (func &optional match scope &rest skip)
  13677. "Call FUNC at each headline selected by MATCH in SCOPE.
  13678. FUNC is a function or a lisp form. The function will be called without
  13679. arguments, with the cursor positioned at the beginning of the headline.
  13680. The return values of all calls to the function will be collected and
  13681. returned as a list.
  13682. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  13683. does not need to preserve point. After evaluation, the cursor will be
  13684. moved to the end of the line (presumably of the headline of the
  13685. processed entry) and search continues from there. Under some
  13686. circumstances, this may not produce the wanted results. For example,
  13687. if you have removed (e.g. archived) the current (sub)tree it could
  13688. mean that the next entry will be skipped entirely. In such cases, you
  13689. can specify the position from where search should continue by making
  13690. FUNC set the variable `org-map-continue-from' to the desired buffer
  13691. position.
  13692. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  13693. Only headlines that are matched by this query will be considered during
  13694. the iteration. When MATCH is nil or t, all headlines will be
  13695. visited by the iteration.
  13696. SCOPE determines the scope of this command. It can be any of:
  13697. nil The current buffer, respecting the restriction if any
  13698. tree The subtree started with the entry at point
  13699. region The entries within the active region, if any
  13700. region-start-level
  13701. The entries within the active region, but only those at
  13702. the same level than the first one.
  13703. file The current buffer, without restriction
  13704. file-with-archives
  13705. The current buffer, and any archives associated with it
  13706. agenda All agenda files
  13707. agenda-with-archives
  13708. All agenda files with any archive files associated with them
  13709. \(file1 file2 ...)
  13710. If this is a list, all files in the list will be scanned
  13711. The remaining args are treated as settings for the skipping facilities of
  13712. the scanner. The following items can be given here:
  13713. archive skip trees with the archive tag
  13714. comment skip trees with the COMMENT keyword
  13715. function or Emacs Lisp form:
  13716. will be used as value for `org-agenda-skip-function', so
  13717. whenever the function returns a position, FUNC will not be
  13718. called for that entry and search will continue from the
  13719. position returned
  13720. If your function needs to retrieve the tags including inherited tags
  13721. at the *current* entry, you can use the value of the variable
  13722. `org-scanner-tags' which will be much faster than getting the value
  13723. with `org-get-tags-at'. If your function gets properties with
  13724. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  13725. to t around the call to `org-entry-properties' to get the same speedup.
  13726. Note that if your function moves around to retrieve tags and properties at
  13727. a *different* entry, you cannot use these techniques."
  13728. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  13729. (not (org-region-active-p)))
  13730. (let* ((org-agenda-archives-mode nil) ; just to make sure
  13731. (org-agenda-skip-archived-trees (memq 'archive skip))
  13732. (org-agenda-skip-comment-trees (memq 'comment skip))
  13733. (org-agenda-skip-function
  13734. (car (org-delete-all '(comment archive) skip)))
  13735. (org-tags-match-list-sublevels t)
  13736. (start-level (eq scope 'region-start-level))
  13737. matcher file res
  13738. org-todo-keywords-for-agenda
  13739. org-done-keywords-for-agenda
  13740. org-todo-keyword-alist-for-agenda
  13741. org-drawers-for-agenda
  13742. org-tag-alist-for-agenda
  13743. todo-only)
  13744. (cond
  13745. ((eq match t) (setq matcher t))
  13746. ((eq match nil) (setq matcher t))
  13747. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  13748. (save-excursion
  13749. (save-restriction
  13750. (cond ((eq scope 'tree)
  13751. (org-back-to-heading t)
  13752. (org-narrow-to-subtree)
  13753. (setq scope nil))
  13754. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  13755. (org-region-active-p))
  13756. ;; If needed, set start-level to a string like "2"
  13757. (when start-level
  13758. (save-excursion
  13759. (goto-char (region-beginning))
  13760. (unless (org-at-heading-p) (outline-next-heading))
  13761. (setq start-level (org-current-level))))
  13762. (narrow-to-region (region-beginning)
  13763. (save-excursion
  13764. (goto-char (region-end))
  13765. (unless (and (bolp) (org-at-heading-p))
  13766. (outline-next-heading))
  13767. (point)))
  13768. (setq scope nil)))
  13769. (if (not scope)
  13770. (progn
  13771. (org-agenda-prepare-buffers
  13772. (list (buffer-file-name (current-buffer))))
  13773. (setq res (org-scan-tags func matcher todo-only start-level)))
  13774. ;; Get the right scope
  13775. (cond
  13776. ((and scope (listp scope) (symbolp (car scope)))
  13777. (setq scope (eval scope)))
  13778. ((eq scope 'agenda)
  13779. (setq scope (org-agenda-files t)))
  13780. ((eq scope 'agenda-with-archives)
  13781. (setq scope (org-agenda-files t))
  13782. (setq scope (org-add-archive-files scope)))
  13783. ((eq scope 'file)
  13784. (setq scope (list (buffer-file-name))))
  13785. ((eq scope 'file-with-archives)
  13786. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  13787. (org-agenda-prepare-buffers scope)
  13788. (while (setq file (pop scope))
  13789. (with-current-buffer (org-find-base-buffer-visiting file)
  13790. (save-excursion
  13791. (save-restriction
  13792. (widen)
  13793. (goto-char (point-min))
  13794. (setq res (append res (org-scan-tags func matcher todo-only))))))))))
  13795. res)))
  13796. ;;;; Properties
  13797. ;;; Setting and retrieving properties
  13798. (defconst org-special-properties
  13799. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  13800. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM" "CLOCKSUM_T")
  13801. "The special properties valid in Org-mode.
  13802. These are properties that are not defined in the property drawer,
  13803. but in some other way.")
  13804. (defconst org-default-properties
  13805. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  13806. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  13807. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  13808. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  13809. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  13810. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  13811. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  13812. "Some properties that are used by Org-mode for various purposes.
  13813. Being in this list makes sure that they are offered for completion.")
  13814. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  13815. "Regular expression matching the first line of a property drawer.")
  13816. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  13817. "Regular expression matching the last line of a property drawer.")
  13818. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  13819. "Regular expression matching the first line of a property drawer.")
  13820. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  13821. "Regular expression matching the first line of a property drawer.")
  13822. (defconst org-property-drawer-re
  13823. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  13824. org-property-end-re "\\)\n?")
  13825. "Matches an entire property drawer.")
  13826. (defconst org-clock-drawer-re
  13827. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  13828. org-property-end-re "\\)\n?")
  13829. "Matches an entire clock drawer.")
  13830. (defun org-property-action ()
  13831. "Do an action on properties."
  13832. (interactive)
  13833. (let (c)
  13834. (org-at-property-p)
  13835. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  13836. (setq c (read-char-exclusive))
  13837. (cond
  13838. ((equal c ?s)
  13839. (call-interactively 'org-set-property))
  13840. ((equal c ?d)
  13841. (call-interactively 'org-delete-property))
  13842. ((equal c ?D)
  13843. (call-interactively 'org-delete-property-globally))
  13844. ((equal c ?c)
  13845. (call-interactively 'org-compute-property-at-point))
  13846. (t (user-error "No such property action %c" c)))))
  13847. (defun org-inc-effort ()
  13848. "Increment the value of the effort property in the current entry."
  13849. (interactive)
  13850. (org-set-effort nil t))
  13851. (defvar org-clock-effort) ;; Defined in org-clock.el
  13852. (defvar org-clock-current-task) ;; Defined in org-clock.el
  13853. (defun org-set-effort (&optional value increment)
  13854. "Set the effort property of the current entry.
  13855. With numerical prefix arg, use the nth allowed value, 0 stands for the
  13856. 10th allowed value.
  13857. When INCREMENT is non-nil, set the property to the next allowed value."
  13858. (interactive "P")
  13859. (if (equal value 0) (setq value 10))
  13860. (let* ((completion-ignore-case t)
  13861. (prop org-effort-property)
  13862. (cur (org-entry-get nil prop))
  13863. (allowed (org-property-get-allowed-values nil prop 'table))
  13864. (existing (mapcar 'list (org-property-values prop)))
  13865. (heading (nth 4 (org-heading-components)))
  13866. rpl
  13867. (val (cond
  13868. ((stringp value) value)
  13869. ((and allowed (integerp value))
  13870. (or (car (nth (1- value) allowed))
  13871. (car (org-last allowed))))
  13872. ((and allowed increment)
  13873. (or (caadr (member (list cur) allowed))
  13874. (user-error "Allowed effort values are not set")))
  13875. (allowed
  13876. (message "Select 1-9,0, [RET%s]: %s"
  13877. (if cur (concat "=" cur) "")
  13878. (mapconcat 'car allowed " "))
  13879. (setq rpl (read-char-exclusive))
  13880. (if (equal rpl ?\r)
  13881. cur
  13882. (setq rpl (- rpl ?0))
  13883. (if (equal rpl 0) (setq rpl 10))
  13884. (if (and (> rpl 0) (<= rpl (length allowed)))
  13885. (car (nth (1- rpl) allowed))
  13886. (org-completing-read "Effort: " allowed nil))))
  13887. (t
  13888. (let (org-completion-use-ido org-completion-use-iswitchb)
  13889. (org-completing-read
  13890. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  13891. (concat "[" cur "]") "")
  13892. ": ")
  13893. existing nil nil "" nil cur))))))
  13894. (unless (equal (org-entry-get nil prop) val)
  13895. (org-entry-put nil prop val))
  13896. (save-excursion
  13897. (org-back-to-heading t)
  13898. (put-text-property (point-at-bol) (point-at-eol) 'org-effort val))
  13899. (when (string= heading org-clock-current-task)
  13900. (setq org-clock-effort (get-text-property (point-at-bol) 'org-effort))
  13901. (org-clock-update-mode-line))
  13902. (message "%s is now %s" prop val)))
  13903. (defun org-at-property-p ()
  13904. "Is cursor inside a property drawer?"
  13905. (save-excursion
  13906. (when (equal 'node-property (car (org-element-at-point)))
  13907. (beginning-of-line 1)
  13908. (looking-at org-property-re))))
  13909. (defun org-get-property-block (&optional beg end force)
  13910. "Return the (beg . end) range of the body of the property drawer.
  13911. BEG and END are the beginning and end of the current subtree, or of
  13912. the part before the first headline. If they are not given, they will
  13913. be found. If the drawer does not exist and FORCE is non-nil, create
  13914. the drawer."
  13915. (catch 'exit
  13916. (save-excursion
  13917. (let* ((beg (or beg (and (org-before-first-heading-p) (point-min))
  13918. (progn (org-back-to-heading t) (point))))
  13919. (end (or end (and (not (outline-next-heading)) (point-max))
  13920. (point))))
  13921. (goto-char beg)
  13922. (if (re-search-forward org-property-start-re end t)
  13923. (setq beg (1+ (match-end 0)))
  13924. (if force
  13925. (save-excursion
  13926. (org-insert-property-drawer)
  13927. (setq end (progn (outline-next-heading) (point))))
  13928. (throw 'exit nil))
  13929. (goto-char beg)
  13930. (if (re-search-forward org-property-start-re end t)
  13931. (setq beg (1+ (match-end 0)))))
  13932. (if (re-search-forward org-property-end-re end t)
  13933. (setq end (match-beginning 0))
  13934. (or force (throw 'exit nil))
  13935. (goto-char beg)
  13936. (setq end beg)
  13937. (org-indent-line)
  13938. (insert ":END:\n"))
  13939. (cons beg end)))))
  13940. (defun org-entry-properties (&optional pom which specific)
  13941. "Get all properties of the entry at point-or-marker POM.
  13942. This includes the TODO keyword, the tags, time strings for deadline,
  13943. scheduled, and clocking, and any additional properties defined in the
  13944. entry. The return value is an alist, keys may occur multiple times
  13945. if the property key was used several times.
  13946. POM may also be nil, in which case the current entry is used.
  13947. If WHICH is nil or `all', get all properties. If WHICH is
  13948. `special' or `standard', only get that subclass. If WHICH
  13949. is a string only get exactly this property. SPECIFIC can be a string, the
  13950. specific property we are interested in. Specifying it can speed
  13951. things up because then unnecessary parsing is avoided."
  13952. (setq which (or which 'all))
  13953. (org-with-wide-buffer
  13954. (org-with-point-at pom
  13955. (let ((clockstr (substring org-clock-string 0 -1))
  13956. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  13957. (case-fold-search nil)
  13958. beg end range props sum-props key key1 value string clocksum clocksumt)
  13959. (when (and (derived-mode-p 'org-mode)
  13960. (ignore-errors (org-back-to-heading t)))
  13961. (setq beg (point))
  13962. (setq sum-props (get-text-property (point) 'org-summaries))
  13963. (setq clocksum (get-text-property (point) :org-clock-minutes)
  13964. clocksumt (get-text-property (point) :org-clock-minutes-today))
  13965. (outline-next-heading)
  13966. (setq end (point))
  13967. (when (memq which '(all special))
  13968. ;; Get the special properties, like TODO and tags
  13969. (goto-char beg)
  13970. (when (and (or (not specific) (string= specific "TODO"))
  13971. (looking-at org-todo-line-regexp) (match-end 2))
  13972. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  13973. (when (and (or (not specific) (string= specific "PRIORITY"))
  13974. (looking-at org-priority-regexp))
  13975. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  13976. (when (or (not specific) (string= specific "FILE"))
  13977. (push (cons "FILE" buffer-file-name) props))
  13978. (when (and (or (not specific) (string= specific "TAGS"))
  13979. (setq value (org-get-tags-string))
  13980. (string-match "\\S-" value))
  13981. (push (cons "TAGS" value) props))
  13982. (when (and (or (not specific) (string= specific "ALLTAGS"))
  13983. (setq value (org-get-tags-at)))
  13984. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  13985. ":"))
  13986. props))
  13987. (when (or (not specific) (string= specific "BLOCKED"))
  13988. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  13989. (when (or (not specific)
  13990. (member specific
  13991. '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
  13992. "TIMESTAMP" "TIMESTAMP_IA")))
  13993. (catch 'match
  13994. (while (and (re-search-forward org-maybe-keyword-time-regexp end t)
  13995. (not (text-property-any 0 (length (match-string 0))
  13996. 'face 'font-lock-comment-face
  13997. (match-string 0))))
  13998. (setq key (if (match-end 1)
  13999. (substring (org-match-string-no-properties 1)
  14000. 0 -1))
  14001. string (if (equal key clockstr)
  14002. (org-trim
  14003. (buffer-substring-no-properties
  14004. (match-beginning 3) (goto-char
  14005. (point-at-eol))))
  14006. (substring (org-match-string-no-properties 3)
  14007. 1 -1)))
  14008. ;; Get the correct property name from the key. This is
  14009. ;; necessary if the user has configured time keywords.
  14010. (setq key1 (concat key ":"))
  14011. (cond
  14012. ((not key)
  14013. (setq key
  14014. (if (= (char-after (match-beginning 3)) ?\[)
  14015. "TIMESTAMP_IA" "TIMESTAMP")))
  14016. ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
  14017. ((equal key1 org-deadline-string) (setq key "DEADLINE"))
  14018. ((equal key1 org-closed-string) (setq key "CLOSED"))
  14019. ((equal key1 org-clock-string) (setq key "CLOCK")))
  14020. (if (and specific (equal key specific) (not (equal key "CLOCK")))
  14021. (progn
  14022. (push (cons key string) props)
  14023. ;; no need to search further if match is found
  14024. (throw 'match t))
  14025. (when (or (equal key "CLOCK") (not (assoc key props)))
  14026. (push (cons key string) props)))))))
  14027. (when (memq which '(all standard))
  14028. ;; Get the standard properties, like :PROP: ...
  14029. (setq range (org-get-property-block beg end))
  14030. (when range
  14031. (goto-char (car range))
  14032. (while (re-search-forward org-property-re
  14033. (cdr range) t)
  14034. (setq key (org-match-string-no-properties 2)
  14035. value (org-trim (or (org-match-string-no-properties 3) "")))
  14036. (unless (member key excluded)
  14037. (push (cons key (or value "")) props)))))
  14038. (if clocksum
  14039. (push (cons "CLOCKSUM"
  14040. (org-columns-number-to-string (/ (float clocksum) 60.)
  14041. 'add_times))
  14042. props))
  14043. (if clocksumt
  14044. (push (cons "CLOCKSUM_T"
  14045. (org-columns-number-to-string (/ (float clocksumt) 60.)
  14046. 'add_times))
  14047. props))
  14048. (unless (assoc "CATEGORY" props)
  14049. (push (cons "CATEGORY" (org-get-category)) props))
  14050. (append sum-props (nreverse props)))))))
  14051. (defun org-entry-get (pom property &optional inherit literal-nil)
  14052. "Get value of PROPERTY for entry or content at point-or-marker POM.
  14053. If INHERIT is non-nil and the entry does not have the property,
  14054. then also check higher levels of the hierarchy.
  14055. If INHERIT is the symbol `selective', use inheritance only if the setting
  14056. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  14057. If the property is present but empty, the return value is the empty string.
  14058. If the property is not present at all, nil is returned.
  14059. Return the value as a string.
  14060. If LITERAL-NIL is set, return the string value \"nil\" as a string,
  14061. do not interpret it as the list atom nil. This is used for inheritance
  14062. when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
  14063. (org-with-point-at pom
  14064. (if (and inherit (if (eq inherit 'selective)
  14065. (org-property-inherit-p property)
  14066. t))
  14067. (org-entry-get-with-inheritance property literal-nil)
  14068. (if (member property org-special-properties)
  14069. ;; We need a special property. Use `org-entry-properties'
  14070. ;; to retrieve it, but specify the wanted property
  14071. (cdr (assoc property (org-entry-properties nil 'special property)))
  14072. (org-with-wide-buffer
  14073. (let ((range (org-get-property-block)))
  14074. (when (and range (not (eq (car range) (cdr range)))
  14075. (save-excursion
  14076. (goto-char (car range))
  14077. (re-search-forward
  14078. (concat (org-re-property property) "\\|"
  14079. (org-re-property (concat property "+")))
  14080. (cdr range) t)))
  14081. (let* ((props
  14082. (list (or (assoc property org-file-properties)
  14083. (assoc property org-global-properties)
  14084. (assoc property org-global-properties-fixed))))
  14085. (ap (lambda (key)
  14086. (when (re-search-forward
  14087. (org-re-property key) (cdr range) t)
  14088. (setq props
  14089. (org-update-property-plist
  14090. key
  14091. (if (match-end 3)
  14092. (org-match-string-no-properties 3) "")
  14093. props)))))
  14094. val)
  14095. (goto-char (car range))
  14096. (funcall ap property)
  14097. (goto-char (car range))
  14098. (while (funcall ap (concat property "+")))
  14099. (setq val (cdr (assoc property props)))
  14100. (when val (if literal-nil val (org-not-nil val)))))))))))
  14101. (defun org-property-or-variable-value (var &optional inherit)
  14102. "Check if there is a property fixing the value of VAR.
  14103. If yes, return this value. If not, return the current value of the variable."
  14104. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  14105. (if (and prop (stringp prop) (string-match "\\S-" prop))
  14106. (read prop)
  14107. (symbol-value var))))
  14108. (defun org-entry-delete (pom property &optional delete-empty-drawer)
  14109. "Delete the property PROPERTY from entry at point-or-marker POM.
  14110. When optional argument DELETE-EMPTY-DRAWER is a string, it defines
  14111. an empty drawer to delete."
  14112. (org-with-point-at pom
  14113. (if (member property org-special-properties)
  14114. nil ; cannot delete these properties.
  14115. (let ((range (org-get-property-block)))
  14116. (if (and range
  14117. (goto-char (car range))
  14118. (re-search-forward
  14119. (org-re-property property nil t)
  14120. (cdr range) t))
  14121. (progn
  14122. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  14123. (and delete-empty-drawer
  14124. (org-remove-empty-drawer-at
  14125. delete-empty-drawer (car range)))
  14126. t)
  14127. nil)))))
  14128. ;; Multi-values properties are properties that contain multiple values
  14129. ;; These values are assumed to be single words, separated by whitespace.
  14130. (defun org-entry-add-to-multivalued-property (pom property value)
  14131. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  14132. (let* ((old (org-entry-get pom property))
  14133. (values (and old (org-split-string old "[ \t]"))))
  14134. (setq value (org-entry-protect-space value))
  14135. (unless (member value values)
  14136. (setq values (append values (list value)))
  14137. (org-entry-put pom property
  14138. (mapconcat 'identity values " ")))))
  14139. (defun org-entry-remove-from-multivalued-property (pom property value)
  14140. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  14141. (let* ((old (org-entry-get pom property))
  14142. (values (and old (org-split-string old "[ \t]"))))
  14143. (setq value (org-entry-protect-space value))
  14144. (when (member value values)
  14145. (setq values (delete value values))
  14146. (org-entry-put pom property
  14147. (mapconcat 'identity values " ")))))
  14148. (defun org-entry-member-in-multivalued-property (pom property value)
  14149. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  14150. (let* ((old (org-entry-get pom property))
  14151. (values (and old (org-split-string old "[ \t]"))))
  14152. (setq value (org-entry-protect-space value))
  14153. (member value values)))
  14154. (defun org-entry-get-multivalued-property (pom property)
  14155. "Return a list of values in a multivalued property."
  14156. (let* ((value (org-entry-get pom property))
  14157. (values (and value (org-split-string value "[ \t]"))))
  14158. (mapcar 'org-entry-restore-space values)))
  14159. (defun org-entry-put-multivalued-property (pom property &rest values)
  14160. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  14161. VALUES should be a list of strings. Spaces will be protected."
  14162. (org-entry-put pom property
  14163. (mapconcat 'org-entry-protect-space values " "))
  14164. (let* ((value (org-entry-get pom property))
  14165. (values (and value (org-split-string value "[ \t]"))))
  14166. (mapcar 'org-entry-restore-space values)))
  14167. (defun org-entry-protect-space (s)
  14168. "Protect spaces and newline in string S."
  14169. (while (string-match " " s)
  14170. (setq s (replace-match "%20" t t s)))
  14171. (while (string-match "\n" s)
  14172. (setq s (replace-match "%0A" t t s)))
  14173. s)
  14174. (defun org-entry-restore-space (s)
  14175. "Restore spaces and newline in string S."
  14176. (while (string-match "%20" s)
  14177. (setq s (replace-match " " t t s)))
  14178. (while (string-match "%0A" s)
  14179. (setq s (replace-match "\n" t t s)))
  14180. s)
  14181. (defvar org-entry-property-inherited-from (make-marker)
  14182. "Marker pointing to the entry from where a property was inherited.
  14183. Each call to `org-entry-get-with-inheritance' will set this marker to the
  14184. location of the entry where the inheritance search matched. If there was
  14185. no match, the marker will point nowhere.
  14186. Note that also `org-entry-get' calls this function, if the INHERIT flag
  14187. is set.")
  14188. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  14189. "Get PROPERTY of entry or content at point, search higher levels if needed.
  14190. The search will stop at the first ancestor which has the property defined.
  14191. If the value found is \"nil\", return nil to show that the property
  14192. should be considered as undefined (this is the meaning of nil here).
  14193. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  14194. (move-marker org-entry-property-inherited-from nil)
  14195. (let (tmp)
  14196. (save-excursion
  14197. (save-restriction
  14198. (widen)
  14199. (catch 'ex
  14200. (while t
  14201. (when (setq tmp (org-entry-get nil property nil literal-nil))
  14202. (or (ignore-errors (org-back-to-heading t))
  14203. (goto-char (point-min)))
  14204. (move-marker org-entry-property-inherited-from (point))
  14205. (throw 'ex tmp))
  14206. (or (ignore-errors (org-up-heading-safe))
  14207. (throw 'ex nil))))))
  14208. (setq tmp (or tmp
  14209. (cdr (assoc property org-file-properties))
  14210. (cdr (assoc property org-global-properties))
  14211. (cdr (assoc property org-global-properties-fixed))))
  14212. (if literal-nil tmp (org-not-nil tmp))))
  14213. (defvar org-property-changed-functions nil
  14214. "Hook called when the value of a property has changed.
  14215. Each hook function should accept two arguments, the name of the property
  14216. and the new value.")
  14217. (defun org-entry-put (pom property value)
  14218. "Set PROPERTY to VALUE for entry at point-or-marker POM.
  14219. If the value is `nil', it is converted to the empty string.
  14220. If it is not a string, an error is raised."
  14221. (cond ((null value) (setq value ""))
  14222. ((not (stringp value))
  14223. (error "Properties values should be strings.")))
  14224. (org-with-point-at pom
  14225. (org-back-to-heading t)
  14226. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  14227. range)
  14228. (cond
  14229. ((equal property "TODO")
  14230. (when (and (string-match "\\S-" value)
  14231. (not (member value org-todo-keywords-1)))
  14232. (user-error "\"%s\" is not a valid TODO state" value))
  14233. (if (or (not value)
  14234. (not (string-match "\\S-" value)))
  14235. (setq value 'none))
  14236. (org-todo value)
  14237. (org-set-tags nil 'align))
  14238. ((equal property "PRIORITY")
  14239. (org-priority (if (and value (string-match "\\S-" value))
  14240. (string-to-char value) ?\ ))
  14241. (org-set-tags nil 'align))
  14242. ((equal property "CLOCKSUM")
  14243. (if (not (re-search-forward
  14244. (concat org-clock-string ".*\\]--\\(\\[[^]]+\\]\\)") nil t))
  14245. (error "Cannot find a clock log")
  14246. (goto-char (- (match-end 1) 2))
  14247. (cond
  14248. ((eq value 'earlier) (org-timestamp-down))
  14249. ((eq value 'later) (org-timestamp-up)))
  14250. (org-clock-sum-current-item)))
  14251. ((equal property "SCHEDULED")
  14252. (if (re-search-forward org-scheduled-time-regexp end t)
  14253. (cond
  14254. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14255. ((eq value 'later) (org-timestamp-change 1 'day))
  14256. (t (call-interactively 'org-schedule)))
  14257. (call-interactively 'org-schedule)))
  14258. ((equal property "DEADLINE")
  14259. (if (re-search-forward org-deadline-time-regexp end t)
  14260. (cond
  14261. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14262. ((eq value 'later) (org-timestamp-change 1 'day))
  14263. (t (call-interactively 'org-deadline)))
  14264. (call-interactively 'org-deadline)))
  14265. ((member property org-special-properties)
  14266. (error "The %s property can not yet be set with `org-entry-put'"
  14267. property))
  14268. (t ; a non-special property
  14269. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  14270. (setq range (org-get-property-block beg end 'force))
  14271. (goto-char (car range))
  14272. (if (re-search-forward
  14273. (org-re-property property nil t) (cdr range) t)
  14274. (progn
  14275. (delete-region (match-beginning 0) (match-end 0))
  14276. (goto-char (match-beginning 0)))
  14277. (goto-char (cdr range))
  14278. (insert "\n")
  14279. (backward-char 1)
  14280. (org-indent-line))
  14281. (insert ":" property ":")
  14282. (and value (insert " " value))
  14283. (org-indent-line)))))
  14284. (run-hook-with-args 'org-property-changed-functions property value)))
  14285. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  14286. "Get all property keys in the current buffer.
  14287. With INCLUDE-SPECIALS, also list the special properties that reflect things
  14288. like tags and TODO state.
  14289. With INCLUDE-DEFAULTS, also include properties that has special meaning
  14290. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
  14291. and others.
  14292. With INCLUDE-COLUMNS, also include property names given in COLUMN
  14293. formats in the current buffer."
  14294. (let (rtn range cfmt s p)
  14295. (save-excursion
  14296. (save-restriction
  14297. (widen)
  14298. (goto-char (point-min))
  14299. (while (re-search-forward org-property-start-re nil t)
  14300. (setq range (org-get-property-block))
  14301. (goto-char (car range))
  14302. (while (re-search-forward org-property-re
  14303. (cdr range) t)
  14304. (add-to-list 'rtn (org-match-string-no-properties 2)))
  14305. (outline-next-heading))))
  14306. (when include-specials
  14307. (setq rtn (append org-special-properties rtn)))
  14308. (when include-defaults
  14309. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  14310. (add-to-list 'rtn org-effort-property))
  14311. (when include-columns
  14312. (save-excursion
  14313. (save-restriction
  14314. (widen)
  14315. (goto-char (point-min))
  14316. (while (re-search-forward
  14317. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  14318. nil t)
  14319. (setq cfmt (match-string 2) s 0)
  14320. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  14321. cfmt s)
  14322. (setq s (match-end 0)
  14323. p (match-string 1 cfmt))
  14324. (unless (or (equal p "ITEM")
  14325. (member p org-special-properties))
  14326. (add-to-list 'rtn (match-string 1 cfmt))))))))
  14327. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  14328. (defun org-property-values (key)
  14329. "Return a list of all values of property KEY in the current buffer."
  14330. (save-excursion
  14331. (save-restriction
  14332. (widen)
  14333. (goto-char (point-min))
  14334. (let ((re (org-re-property key))
  14335. values)
  14336. (while (re-search-forward re nil t)
  14337. (add-to-list 'values (org-trim (match-string 3))))
  14338. (delete "" values)))))
  14339. (defun org-insert-property-drawer ()
  14340. "Insert a property drawer into the current entry."
  14341. (org-back-to-heading t)
  14342. (looking-at org-outline-regexp)
  14343. (let ((indent (if org-adapt-indentation
  14344. (- (match-end 0) (match-beginning 0))
  14345. 0))
  14346. (beg (point))
  14347. (re (concat "^[ \t]*" org-keyword-time-regexp))
  14348. end hiddenp)
  14349. (outline-next-heading)
  14350. (setq end (point))
  14351. (goto-char beg)
  14352. (while (re-search-forward re end t))
  14353. (setq hiddenp (outline-invisible-p))
  14354. (end-of-line 1)
  14355. (and (equal (char-after) ?\n) (forward-char 1))
  14356. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  14357. (if (member (match-string 1) '("CLOCK:" ":END:"))
  14358. ;; just skip this line
  14359. (beginning-of-line 2)
  14360. ;; Drawer start, find the end
  14361. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  14362. (beginning-of-line 1)))
  14363. (org-skip-over-state-notes)
  14364. (skip-chars-backward " \t\n\r")
  14365. (if (and (eq (char-before) ?*) (not (eq (char-after) ?\n)))
  14366. (forward-char 1))
  14367. (goto-char (point-at-eol))
  14368. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  14369. (beginning-of-line 0)
  14370. (org-indent-to-column indent)
  14371. (beginning-of-line 2)
  14372. (org-indent-to-column indent)
  14373. (beginning-of-line 0)
  14374. (if hiddenp
  14375. (save-excursion
  14376. (org-back-to-heading t)
  14377. (hide-entry))
  14378. (org-flag-drawer t))))
  14379. (defun org-insert-drawer (&optional arg drawer)
  14380. "Insert a drawer at point.
  14381. Optional argument DRAWER, when non-nil, is a string representing
  14382. drawer's name. Otherwise, the user is prompted for a name.
  14383. If a region is active, insert the drawer around that region
  14384. instead.
  14385. Point is left between drawer's boundaries."
  14386. (interactive "P")
  14387. (let* ((logbook (if (stringp org-log-into-drawer) org-log-into-drawer
  14388. "LOGBOOK"))
  14389. ;; SYSTEM-DRAWERS is a list of drawer names that are used
  14390. ;; internally by Org. They are meant to be inserted
  14391. ;; automatically.
  14392. (system-drawers `("CLOCK" ,logbook "PROPERTIES"))
  14393. ;; Remove system drawers from list. Note: For some reason,
  14394. ;; `org-completing-read' ignores the predicate while
  14395. ;; `completing-read' handles it fine.
  14396. (drawer (if arg "PROPERTIES"
  14397. (or drawer
  14398. (completing-read
  14399. "Drawer: " org-drawers
  14400. (lambda (d) (not (member d system-drawers))))))))
  14401. (cond
  14402. ;; With C-u, fall back on `org-insert-property-drawer'
  14403. (arg (org-insert-property-drawer))
  14404. ;; With an active region, insert a drawer at point.
  14405. ((not (org-region-active-p))
  14406. (progn
  14407. (unless (bolp) (insert "\n"))
  14408. (insert (format ":%s:\n\n:END:\n" drawer))
  14409. (forward-line -2)))
  14410. ;; Otherwise, insert the drawer at point
  14411. (t
  14412. (let ((rbeg (region-beginning))
  14413. (rend (copy-marker (region-end))))
  14414. (unwind-protect
  14415. (progn
  14416. (goto-char rbeg)
  14417. (beginning-of-line)
  14418. (when (save-excursion
  14419. (re-search-forward org-outline-regexp-bol rend t))
  14420. (user-error "Drawers cannot contain headlines"))
  14421. ;; Position point at the beginning of the first
  14422. ;; non-blank line in region. Insert drawer's opening
  14423. ;; there, then indent it.
  14424. (org-skip-whitespace)
  14425. (beginning-of-line)
  14426. (insert ":" drawer ":\n")
  14427. (forward-line -1)
  14428. (indent-for-tab-command)
  14429. ;; Move point to the beginning of the first blank line
  14430. ;; after the last non-blank line in region. Insert
  14431. ;; drawer's closing, then indent it.
  14432. (goto-char rend)
  14433. (skip-chars-backward " \r\t\n")
  14434. (insert "\n:END:")
  14435. (deactivate-mark t)
  14436. (indent-for-tab-command)
  14437. (unless (eolp) (insert "\n")))
  14438. ;; Clear marker, whatever the outcome of insertion is.
  14439. (set-marker rend nil)))))))
  14440. (defvar org-property-set-functions-alist nil
  14441. "Property set function alist.
  14442. Each entry should have the following format:
  14443. (PROPERTY . READ-FUNCTION)
  14444. The read function will be called with the same argument as
  14445. `org-completing-read'.")
  14446. (defun org-set-property-function (property)
  14447. "Get the function that should be used to set PROPERTY.
  14448. This is computed according to `org-property-set-functions-alist'."
  14449. (or (cdr (assoc property org-property-set-functions-alist))
  14450. 'org-completing-read))
  14451. (defun org-read-property-value (property)
  14452. "Read PROPERTY value from user."
  14453. (let* ((completion-ignore-case t)
  14454. (allowed (org-property-get-allowed-values nil property 'table))
  14455. (cur (org-entry-get nil property))
  14456. (prompt (concat property " value"
  14457. (if (and cur (string-match "\\S-" cur))
  14458. (concat " [" cur "]") "") ": "))
  14459. (set-function (org-set-property-function property))
  14460. (val (if allowed
  14461. (funcall set-function prompt allowed nil
  14462. (not (get-text-property 0 'org-unrestricted
  14463. (caar allowed))))
  14464. (let (org-completion-use-ido org-completion-use-iswitchb)
  14465. (funcall set-function prompt
  14466. (mapcar 'list (org-property-values property))
  14467. nil nil "" nil cur)))))
  14468. (org-trim val)))
  14469. (defvar org-last-set-property nil)
  14470. (defvar org-last-set-property-value nil)
  14471. (defun org-read-property-name ()
  14472. "Read a property name."
  14473. (let* ((completion-ignore-case t)
  14474. (keys (org-buffer-property-keys nil t t))
  14475. (default-prop (or (save-excursion
  14476. (save-match-data
  14477. (beginning-of-line)
  14478. (and (looking-at "^\\s-*:\\([^:\n]+\\):")
  14479. (null (string= (match-string 1) "END"))
  14480. (match-string 1))))
  14481. org-last-set-property))
  14482. (property (org-icompleting-read
  14483. (concat "Property"
  14484. (if default-prop (concat " [" default-prop "]") "")
  14485. ": ")
  14486. (mapcar 'list keys)
  14487. nil nil nil nil
  14488. default-prop)))
  14489. (if (member property keys)
  14490. property
  14491. (or (cdr (assoc (downcase property)
  14492. (mapcar (lambda (x) (cons (downcase x) x))
  14493. keys)))
  14494. property))))
  14495. (defun org-set-property-and-value (use-last)
  14496. "Allow to set [PROPERTY]: [value] direction from prompt.
  14497. When use-default, don't even ask, just use the last
  14498. \"[PROPERTY]: [value]\" string from the history."
  14499. (interactive "P")
  14500. (let* ((completion-ignore-case t)
  14501. (pv (or (and use-last org-last-set-property-value)
  14502. (org-completing-read
  14503. "Enter a \"[Property]: [value]\" pair: "
  14504. nil nil nil nil nil
  14505. org-last-set-property-value)))
  14506. prop val)
  14507. (when (string-match "^[ \t]*\\([^:]+\\):[ \t]*\\(.*\\)[ \t]*$" pv)
  14508. (setq prop (match-string 1 pv)
  14509. val (match-string 2 pv))
  14510. (org-set-property prop val))))
  14511. (defun org-set-property (property value)
  14512. "In the current entry, set PROPERTY to VALUE.
  14513. When called interactively, this will prompt for a property name, offering
  14514. completion on existing and default properties. And then it will prompt
  14515. for a value, offering completion either on allowed values (via an inherited
  14516. xxx_ALL property) or on existing values in other instances of this property
  14517. in the current file."
  14518. (interactive (list nil nil))
  14519. (let* ((property (or property (org-read-property-name)))
  14520. (value (or value (org-read-property-value property)))
  14521. (fn (cdr (assoc property org-properties-postprocess-alist))))
  14522. (setq org-last-set-property property)
  14523. (setq org-last-set-property-value (concat property ": " value))
  14524. ;; Possibly postprocess the inserted value:
  14525. (when fn (setq value (funcall fn value)))
  14526. (unless (equal (org-entry-get nil property) value)
  14527. (org-entry-put nil property value))))
  14528. (defun org-delete-property (property &optional delete-empty-drawer)
  14529. "In the current entry, delete PROPERTY.
  14530. When optional argument DELETE-EMPTY-DRAWER is a string, it defines
  14531. an empty drawer to delete."
  14532. (interactive
  14533. (let* ((completion-ignore-case t)
  14534. (cat (org-entry-get (point) "CATEGORY"))
  14535. (props0 (org-entry-properties nil 'standard))
  14536. (props (if cat props0
  14537. (delete `("CATEGORY" . ,(org-get-category)) props0)))
  14538. (prop (if (< 1 (length props))
  14539. (org-icompleting-read "Property: " props nil t)
  14540. (caar props))))
  14541. (list prop)))
  14542. (if (not property)
  14543. (message "No property to delete in this entry")
  14544. (org-entry-delete nil property delete-empty-drawer)
  14545. (message "Property \"%s\" deleted" property)))
  14546. (defun org-delete-property-globally (property)
  14547. "Remove PROPERTY globally, from all entries."
  14548. (interactive
  14549. (let* ((completion-ignore-case t)
  14550. (prop (org-icompleting-read
  14551. "Globally remove property: "
  14552. (mapcar 'list (org-buffer-property-keys)))))
  14553. (list prop)))
  14554. (save-excursion
  14555. (save-restriction
  14556. (widen)
  14557. (goto-char (point-min))
  14558. (let ((cnt 0))
  14559. (while (re-search-forward
  14560. (org-re-property property)
  14561. nil t)
  14562. (setq cnt (1+ cnt))
  14563. (delete-region (match-beginning 0) (1+ (point-at-eol))))
  14564. (message "Property \"%s\" removed from %d entries" property cnt)))))
  14565. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  14566. (defun org-compute-property-at-point ()
  14567. "Compute the property at point.
  14568. This looks for an enclosing column format, extracts the operator and
  14569. then applies it to the property in the column format's scope."
  14570. (interactive)
  14571. (unless (org-at-property-p)
  14572. (user-error "Not at a property"))
  14573. (let ((prop (org-match-string-no-properties 2)))
  14574. (org-columns-get-format-and-top-level)
  14575. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  14576. (user-error "No operator defined for property %s" prop))
  14577. (org-columns-compute prop)))
  14578. (defvar org-property-allowed-value-functions nil
  14579. "Hook for functions supplying allowed values for a specific property.
  14580. The functions must take a single argument, the name of the property, and
  14581. return a flat list of allowed values. If \":ETC\" is one of
  14582. the values, this means that these values are intended as defaults for
  14583. completion, but that other values should be allowed too.
  14584. The functions must return nil if they are not responsible for this
  14585. property.")
  14586. (defun org-property-get-allowed-values (pom property &optional table)
  14587. "Get allowed values for the property PROPERTY.
  14588. When TABLE is non-nil, return an alist that can directly be used for
  14589. completion."
  14590. (let (vals)
  14591. (cond
  14592. ((equal property "TODO")
  14593. (setq vals (org-with-point-at pom
  14594. (append org-todo-keywords-1 '("")))))
  14595. ((equal property "PRIORITY")
  14596. (let ((n org-lowest-priority))
  14597. (while (>= n org-highest-priority)
  14598. (push (char-to-string n) vals)
  14599. (setq n (1- n)))))
  14600. ((member property org-special-properties))
  14601. ((setq vals (run-hook-with-args-until-success
  14602. 'org-property-allowed-value-functions property)))
  14603. (t
  14604. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  14605. (when (and vals (string-match "\\S-" vals))
  14606. (setq vals (car (read-from-string (concat "(" vals ")"))))
  14607. (setq vals (mapcar (lambda (x)
  14608. (cond ((stringp x) x)
  14609. ((numberp x) (number-to-string x))
  14610. ((symbolp x) (symbol-name x))
  14611. (t "???")))
  14612. vals)))))
  14613. (when (member ":ETC" vals)
  14614. (setq vals (remove ":ETC" vals))
  14615. (org-add-props (car vals) '(org-unrestricted t)))
  14616. (if table (mapcar 'list vals) vals)))
  14617. (defun org-property-previous-allowed-value (&optional previous)
  14618. "Switch to the next allowed value for this property."
  14619. (interactive)
  14620. (org-property-next-allowed-value t))
  14621. (defun org-property-next-allowed-value (&optional previous)
  14622. "Switch to the next allowed value for this property."
  14623. (interactive)
  14624. (unless (org-at-property-p)
  14625. (user-error "Not at a property"))
  14626. (let* ((prop (car (save-match-data (org-split-string (match-string 1) ":"))))
  14627. (key (match-string 2))
  14628. (value (match-string 3))
  14629. (allowed (or (org-property-get-allowed-values (point) key)
  14630. (and (member value '("[ ]" "[-]" "[X]"))
  14631. '("[ ]" "[X]"))))
  14632. (heading (save-match-data (nth 4 (org-heading-components))))
  14633. nval)
  14634. (unless allowed
  14635. (user-error "Allowed values for this property have not been defined"))
  14636. (if previous (setq allowed (reverse allowed)))
  14637. (if (member value allowed)
  14638. (setq nval (car (cdr (member value allowed)))))
  14639. (setq nval (or nval (car allowed)))
  14640. (if (equal nval value)
  14641. (user-error "Only one allowed value for this property"))
  14642. (org-at-property-p)
  14643. (replace-match (concat " :" key ": " nval) t t)
  14644. (org-indent-line)
  14645. (beginning-of-line 1)
  14646. (skip-chars-forward " \t")
  14647. (when (equal prop org-effort-property)
  14648. (save-excursion
  14649. (org-back-to-heading t)
  14650. (put-text-property (point-at-bol) (point-at-eol) 'org-effort nval))
  14651. (when (string= org-clock-current-task heading)
  14652. (setq org-clock-effort nval)
  14653. (org-clock-update-mode-line)))
  14654. (run-hook-with-args 'org-property-changed-functions key nval)))
  14655. (defun org-find-olp (path &optional this-buffer)
  14656. "Return a marker pointing to the entry at outline path OLP.
  14657. If anything goes wrong, throw an error.
  14658. You can wrap this call to catch the error like this:
  14659. (condition-case msg
  14660. (org-mobile-locate-entry (match-string 4))
  14661. (error (nth 1 msg)))
  14662. The return value will then be either a string with the error message,
  14663. or a marker if everything is OK.
  14664. If THIS-BUFFER is set, the outline path does not contain a file,
  14665. only headings."
  14666. (let* ((file (if this-buffer buffer-file-name (pop path)))
  14667. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  14668. (level 1)
  14669. (lmin 1)
  14670. (lmax 1)
  14671. limit re end found pos heading cnt flevel)
  14672. (unless buffer (error "File not found :%s" file))
  14673. (with-current-buffer buffer
  14674. (save-excursion
  14675. (save-restriction
  14676. (widen)
  14677. (setq limit (point-max))
  14678. (goto-char (point-min))
  14679. (while (setq heading (pop path))
  14680. (setq re (format org-complex-heading-regexp-format
  14681. (regexp-quote heading)))
  14682. (setq cnt 0 pos (point))
  14683. (while (re-search-forward re end t)
  14684. (setq level (- (match-end 1) (match-beginning 1)))
  14685. (if (and (>= level lmin) (<= level lmax))
  14686. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  14687. (when (= cnt 0) (error "Heading not found on level %d: %s"
  14688. lmax heading))
  14689. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  14690. lmax heading))
  14691. (goto-char found)
  14692. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  14693. (setq end (save-excursion (org-end-of-subtree t t))))
  14694. (when (org-at-heading-p)
  14695. (point-marker)))))))
  14696. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  14697. "Find node HEADING in BUFFER.
  14698. Return a marker to the heading if it was found, or nil if not.
  14699. If POS-ONLY is set, return just the position instead of a marker.
  14700. The heading text must match exact, but it may have a TODO keyword,
  14701. a priority cookie and tags in the standard locations."
  14702. (with-current-buffer (or buffer (current-buffer))
  14703. (save-excursion
  14704. (save-restriction
  14705. (widen)
  14706. (goto-char (point-min))
  14707. (let (case-fold-search)
  14708. (if (re-search-forward
  14709. (format org-complex-heading-regexp-format
  14710. (regexp-quote heading)) nil t)
  14711. (if pos-only
  14712. (match-beginning 0)
  14713. (move-marker (make-marker) (match-beginning 0)))))))))
  14714. (defun org-find-exact-heading-in-directory (heading &optional dir)
  14715. "Find Org node headline HEADING in all .org files in directory DIR.
  14716. When the target headline is found, return a marker to this location."
  14717. (let ((files (directory-files (or dir default-directory)
  14718. nil "\\`[^.#].*\\.org\\'"))
  14719. file visiting m buffer)
  14720. (catch 'found
  14721. (while (setq file (pop files))
  14722. (message "trying %s" file)
  14723. (setq visiting (org-find-base-buffer-visiting file))
  14724. (setq buffer (or visiting (find-file-noselect file)))
  14725. (setq m (org-find-exact-headline-in-buffer
  14726. heading buffer))
  14727. (when (and (not m) (not visiting)) (kill-buffer buffer))
  14728. (and m (throw 'found m))))))
  14729. (defun org-find-entry-with-id (ident)
  14730. "Locate the entry that contains the ID property with exact value IDENT.
  14731. IDENT can be a string, a symbol or a number, this function will search for
  14732. the string representation of it.
  14733. Return the position where this entry starts, or nil if there is no such entry."
  14734. (interactive "sID: ")
  14735. (let ((id (cond
  14736. ((stringp ident) ident)
  14737. ((symbol-name ident) (symbol-name ident))
  14738. ((numberp ident) (number-to-string ident))
  14739. (t (error "IDENT %s must be a string, symbol or number" ident))))
  14740. (case-fold-search nil))
  14741. (save-excursion
  14742. (save-restriction
  14743. (widen)
  14744. (goto-char (point-min))
  14745. (when (re-search-forward
  14746. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  14747. nil t)
  14748. (org-back-to-heading t)
  14749. (point))))))
  14750. ;;;; Timestamps
  14751. (defvar org-last-changed-timestamp nil)
  14752. (defvar org-last-inserted-timestamp nil
  14753. "The last time stamp inserted with `org-insert-time-stamp'.")
  14754. (defvar org-ts-what) ; dynamically scoped parameter
  14755. (defun org-time-stamp (arg &optional inactive)
  14756. "Prompt for a date/time and insert a time stamp.
  14757. If the user specifies a time like HH:MM or if this command is
  14758. called with at least one prefix argument, the time stamp contains
  14759. the date and the time. Otherwise, only the date is be included.
  14760. All parts of a date not specified by the user is filled in from
  14761. the current date/time. So if you just press return without
  14762. typing anything, the time stamp will represent the current
  14763. date/time.
  14764. If there is already a timestamp at the cursor, it will be
  14765. modified.
  14766. With two universal prefix arguments, insert an active timestamp
  14767. with the current time without prompting the user.
  14768. When called from lisp, the timestamp is inactive if INACTIVE is
  14769. non-nil."
  14770. (interactive "P")
  14771. (let* ((ts nil)
  14772. (default-time
  14773. ;; Default time is either today, or, when entering a range,
  14774. ;; the range start.
  14775. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  14776. (save-excursion
  14777. (re-search-backward
  14778. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  14779. (- (point) 20) t)))
  14780. (apply 'encode-time (org-parse-time-string (match-string 1)))
  14781. (current-time)))
  14782. (default-input (and ts (org-get-compact-tod ts)))
  14783. (repeater (save-excursion
  14784. (save-match-data
  14785. (beginning-of-line)
  14786. (when (re-search-forward
  14787. "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  14788. (save-excursion (progn (end-of-line) (point))) t)
  14789. (match-string 0)))))
  14790. org-time-was-given org-end-time-was-given time)
  14791. (cond
  14792. ((and (org-at-timestamp-p t)
  14793. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  14794. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  14795. (insert "--")
  14796. (setq time (let ((this-command this-command))
  14797. (org-read-date arg 'totime nil nil
  14798. default-time default-input inactive)))
  14799. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  14800. ((org-at-timestamp-p t)
  14801. (setq time (let ((this-command this-command))
  14802. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14803. (when (org-at-timestamp-p t) ; just to get the match data
  14804. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  14805. (replace-match "")
  14806. (setq org-last-changed-timestamp
  14807. (org-insert-time-stamp
  14808. time (or org-time-was-given arg)
  14809. inactive nil nil (list org-end-time-was-given)))
  14810. (when repeater (goto-char (1- (point))) (insert " " repeater)
  14811. (setq org-last-changed-timestamp
  14812. (concat (substring org-last-inserted-timestamp 0 -1)
  14813. " " repeater ">"))))
  14814. (message "Timestamp updated"))
  14815. ((equal arg '(16))
  14816. (org-insert-time-stamp (current-time) t inactive))
  14817. (t
  14818. (setq time (let ((this-command this-command))
  14819. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14820. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  14821. nil nil (list org-end-time-was-given))))))
  14822. ;; FIXME: can we use this for something else, like computing time differences?
  14823. (defun org-get-compact-tod (s)
  14824. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  14825. (let* ((t1 (match-string 1 s))
  14826. (h1 (string-to-number (match-string 2 s)))
  14827. (m1 (string-to-number (match-string 3 s)))
  14828. (t2 (and (match-end 4) (match-string 5 s)))
  14829. (h2 (and t2 (string-to-number (match-string 6 s))))
  14830. (m2 (and t2 (string-to-number (match-string 7 s))))
  14831. dh dm)
  14832. (if (not t2)
  14833. t1
  14834. (setq dh (- h2 h1) dm (- m2 m1))
  14835. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  14836. (concat t1 "+" (number-to-string dh)
  14837. (and (/= 0 dm) (format ":%02d" dm)))))))
  14838. (defun org-time-stamp-inactive (&optional arg)
  14839. "Insert an inactive time stamp.
  14840. An inactive time stamp is enclosed in square brackets instead of angle
  14841. brackets. It is inactive in the sense that it does not trigger agenda entries,
  14842. does not link to the calendar and cannot be changed with the S-cursor keys.
  14843. So these are more for recording a certain time/date."
  14844. (interactive "P")
  14845. (org-time-stamp arg 'inactive))
  14846. (defvar org-date-ovl (make-overlay 1 1))
  14847. (overlay-put org-date-ovl 'face 'org-date-selected)
  14848. (org-detach-overlay org-date-ovl)
  14849. (defvar org-ans1) ; dynamically scoped parameter
  14850. (defvar org-ans2) ; dynamically scoped parameter
  14851. (defvar org-plain-time-of-day-regexp) ; defined below
  14852. (defvar org-overriding-default-time nil) ; dynamically scoped
  14853. (defvar org-read-date-overlay nil)
  14854. (defvar org-dcst nil) ; dynamically scoped
  14855. (defvar org-read-date-history nil)
  14856. (defvar org-read-date-final-answer nil)
  14857. (defvar org-read-date-analyze-futurep nil)
  14858. (defvar org-read-date-analyze-forced-year nil)
  14859. (defvar org-read-date-inactive)
  14860. (defvar org-read-date-minibuffer-local-map
  14861. (let* ((org-replace-disputed-keys nil)
  14862. (map (make-sparse-keymap)))
  14863. (set-keymap-parent map minibuffer-local-map)
  14864. (org-defkey map (kbd ".")
  14865. (lambda () (interactive)
  14866. ;; Are we at the beginning of the prompt?
  14867. (if (looking-back "^[^:]+: ")
  14868. (org-eval-in-calendar '(calendar-goto-today))
  14869. (insert "."))))
  14870. (org-defkey map (kbd "C-.")
  14871. (lambda () (interactive)
  14872. (org-eval-in-calendar '(calendar-goto-today))))
  14873. (org-defkey map [(meta shift left)]
  14874. (lambda () (interactive)
  14875. (org-eval-in-calendar '(calendar-backward-month 1))))
  14876. (org-defkey map [(meta shift right)]
  14877. (lambda () (interactive)
  14878. (org-eval-in-calendar '(calendar-forward-month 1))))
  14879. (org-defkey map [(meta shift up)]
  14880. (lambda () (interactive)
  14881. (org-eval-in-calendar '(calendar-backward-year 1))))
  14882. (org-defkey map [(meta shift down)]
  14883. (lambda () (interactive)
  14884. (org-eval-in-calendar '(calendar-forward-year 1))))
  14885. (org-defkey map [?\e (shift left)]
  14886. (lambda () (interactive)
  14887. (org-eval-in-calendar '(calendar-backward-month 1))))
  14888. (org-defkey map [?\e (shift right)]
  14889. (lambda () (interactive)
  14890. (org-eval-in-calendar '(calendar-forward-month 1))))
  14891. (org-defkey map [?\e (shift up)]
  14892. (lambda () (interactive)
  14893. (org-eval-in-calendar '(calendar-backward-year 1))))
  14894. (org-defkey map [?\e (shift down)]
  14895. (lambda () (interactive)
  14896. (org-eval-in-calendar '(calendar-forward-year 1))))
  14897. (org-defkey map [(shift up)]
  14898. (lambda () (interactive)
  14899. (org-eval-in-calendar '(calendar-backward-week 1))))
  14900. (org-defkey map [(shift down)]
  14901. (lambda () (interactive)
  14902. (org-eval-in-calendar '(calendar-forward-week 1))))
  14903. (org-defkey map [(shift left)]
  14904. (lambda () (interactive)
  14905. (org-eval-in-calendar '(calendar-backward-day 1))))
  14906. (org-defkey map [(shift right)]
  14907. (lambda () (interactive)
  14908. (org-eval-in-calendar '(calendar-forward-day 1))))
  14909. (org-defkey map "!"
  14910. (lambda () (interactive)
  14911. (org-eval-in-calendar '(diary-view-entries))
  14912. (message "")))
  14913. (org-defkey map ">"
  14914. (lambda () (interactive)
  14915. (org-eval-in-calendar '(calendar-scroll-left 1))))
  14916. (org-defkey map "<"
  14917. (lambda () (interactive)
  14918. (org-eval-in-calendar '(calendar-scroll-right 1))))
  14919. (org-defkey map "\C-v"
  14920. (lambda () (interactive)
  14921. (org-eval-in-calendar
  14922. '(calendar-scroll-left-three-months 1))))
  14923. (org-defkey map "\M-v"
  14924. (lambda () (interactive)
  14925. (org-eval-in-calendar
  14926. '(calendar-scroll-right-three-months 1))))
  14927. map)
  14928. "Keymap for minibuffer commands when using `org-read-date'.")
  14929. (defvar org-def)
  14930. (defvar org-defdecode)
  14931. (defvar org-with-time)
  14932. (defun org-read-date (&optional org-with-time to-time from-string prompt
  14933. default-time default-input inactive)
  14934. "Read a date, possibly a time, and make things smooth for the user.
  14935. The prompt will suggest to enter an ISO date, but you can also enter anything
  14936. which will at least partially be understood by `parse-time-string'.
  14937. Unrecognized parts of the date will default to the current day, month, year,
  14938. hour and minute. If this command is called to replace a timestamp at point,
  14939. or to enter the second timestamp of a range, the default time is taken
  14940. from the existing stamp. Furthermore, the command prefers the future,
  14941. so if you are giving a date where the year is not given, and the day-month
  14942. combination is already past in the current year, it will assume you
  14943. mean next year. For details, see the manual. A few examples:
  14944. 3-2-5 --> 2003-02-05
  14945. feb 15 --> currentyear-02-15
  14946. 2/15 --> currentyear-02-15
  14947. sep 12 9 --> 2009-09-12
  14948. 12:45 --> today 12:45
  14949. 22 sept 0:34 --> currentyear-09-22 0:34
  14950. 12 --> currentyear-currentmonth-12
  14951. Fri --> nearest Friday after today
  14952. -Tue --> last Tuesday
  14953. etc.
  14954. Furthermore you can specify a relative date by giving, as the *first* thing
  14955. in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
  14956. change in days weeks, months, years.
  14957. With a single plus or minus, the date is relative to today. With a double
  14958. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  14959. +4d --> four days from today
  14960. +4 --> same as above
  14961. +2w --> two weeks from today
  14962. ++5 --> five days from default date
  14963. The function understands only English month and weekday abbreviations.
  14964. While prompting, a calendar is popped up - you can also select the
  14965. date with the mouse (button 1). The calendar shows a period of three
  14966. months. To scroll it to other months, use the keys `>' and `<'.
  14967. If you don't like the calendar, turn it off with
  14968. \(setq org-read-date-popup-calendar nil)
  14969. With optional argument TO-TIME, the date will immediately be converted
  14970. to an internal time.
  14971. With an optional argument ORG-WITH-TIME, the prompt will suggest to
  14972. also insert a time. Note that when ORG-WITH-TIME is not set, you can
  14973. still enter a time, and this function will inform the calling routine
  14974. about this change. The calling routine may then choose to change the
  14975. format used to insert the time stamp into the buffer to include the time.
  14976. With optional argument FROM-STRING, read from this string instead from
  14977. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  14978. the time/date that is used for everything that is not specified by the
  14979. user."
  14980. (require 'parse-time)
  14981. (let* ((org-time-stamp-rounding-minutes
  14982. (if (equal org-with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  14983. (org-dcst org-display-custom-times)
  14984. (ct (org-current-time))
  14985. (org-def (or org-overriding-default-time default-time ct))
  14986. (org-defdecode (decode-time org-def))
  14987. (dummy (progn
  14988. (when (< (nth 2 org-defdecode) org-extend-today-until)
  14989. (setcar (nthcdr 2 org-defdecode) -1)
  14990. (setcar (nthcdr 1 org-defdecode) 59)
  14991. (setq org-def (apply 'encode-time org-defdecode)
  14992. org-defdecode (decode-time org-def)))))
  14993. (mouse-autoselect-window nil) ; Don't let the mouse jump
  14994. (calendar-frame-setup nil)
  14995. (calendar-setup nil)
  14996. (calendar-move-hook nil)
  14997. (calendar-view-diary-initially-flag nil)
  14998. (calendar-view-holidays-initially-flag nil)
  14999. (timestr (format-time-string
  15000. (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") org-def))
  15001. (prompt (concat (if prompt (concat prompt " ") "")
  15002. (format "Date+time [%s]: " timestr)))
  15003. ans (org-ans0 "") org-ans1 org-ans2 final)
  15004. (cond
  15005. (from-string (setq ans from-string))
  15006. (org-read-date-popup-calendar
  15007. (save-excursion
  15008. (save-window-excursion
  15009. (calendar)
  15010. (org-eval-in-calendar '(setq cursor-type nil) t)
  15011. (unwind-protect
  15012. (progn
  15013. (calendar-forward-day (- (time-to-days org-def)
  15014. (calendar-absolute-from-gregorian
  15015. (calendar-current-date))))
  15016. (org-eval-in-calendar nil t)
  15017. (let* ((old-map (current-local-map))
  15018. (map (copy-keymap calendar-mode-map))
  15019. (minibuffer-local-map
  15020. (copy-keymap org-read-date-minibuffer-local-map)))
  15021. (org-defkey map (kbd "RET") 'org-calendar-select)
  15022. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  15023. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  15024. (unwind-protect
  15025. (progn
  15026. (use-local-map map)
  15027. (setq org-read-date-inactive inactive)
  15028. (add-hook 'post-command-hook 'org-read-date-display)
  15029. (setq org-ans0 (read-string prompt default-input
  15030. 'org-read-date-history nil))
  15031. ;; org-ans0: from prompt
  15032. ;; org-ans1: from mouse click
  15033. ;; org-ans2: from calendar motion
  15034. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  15035. (remove-hook 'post-command-hook 'org-read-date-display)
  15036. (use-local-map old-map)
  15037. (when org-read-date-overlay
  15038. (delete-overlay org-read-date-overlay)
  15039. (setq org-read-date-overlay nil)))))
  15040. (bury-buffer "*Calendar*")))))
  15041. (t ; Naked prompt only
  15042. (unwind-protect
  15043. (setq ans (read-string prompt default-input
  15044. 'org-read-date-history timestr))
  15045. (when org-read-date-overlay
  15046. (delete-overlay org-read-date-overlay)
  15047. (setq org-read-date-overlay nil)))))
  15048. (setq final (org-read-date-analyze ans org-def org-defdecode))
  15049. (when org-read-date-analyze-forced-year
  15050. (message "Year was forced into %s"
  15051. (if org-read-date-force-compatible-dates
  15052. "compatible range (1970-2037)"
  15053. "range representable on this machine"))
  15054. (ding))
  15055. ;; One round trip to get rid of 34th of August and stuff like that....
  15056. (setq final (decode-time (apply 'encode-time final)))
  15057. (setq org-read-date-final-answer ans)
  15058. (if to-time
  15059. (apply 'encode-time final)
  15060. (if (and (boundp 'org-time-was-given) org-time-was-given)
  15061. (format "%04d-%02d-%02d %02d:%02d"
  15062. (nth 5 final) (nth 4 final) (nth 3 final)
  15063. (nth 2 final) (nth 1 final))
  15064. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  15065. (defun org-read-date-display ()
  15066. "Display the current date prompt interpretation in the minibuffer."
  15067. (when org-read-date-display-live
  15068. (when org-read-date-overlay
  15069. (delete-overlay org-read-date-overlay))
  15070. (when (minibufferp (current-buffer))
  15071. (save-excursion
  15072. (end-of-line 1)
  15073. (while (not (equal (buffer-substring
  15074. (max (point-min) (- (point) 4)) (point))
  15075. " "))
  15076. (insert " ")))
  15077. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  15078. " " (or org-ans1 org-ans2)))
  15079. (org-end-time-was-given nil)
  15080. (f (org-read-date-analyze ans org-def org-defdecode))
  15081. (fmts (if org-dcst
  15082. org-time-stamp-custom-formats
  15083. org-time-stamp-formats))
  15084. (fmt (if (or org-with-time
  15085. (and (boundp 'org-time-was-given) org-time-was-given))
  15086. (cdr fmts)
  15087. (car fmts)))
  15088. (txt (format-time-string fmt (apply 'encode-time f)))
  15089. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  15090. (txt (concat "=> " txt)))
  15091. (when (and org-end-time-was-given
  15092. (string-match org-plain-time-of-day-regexp txt))
  15093. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  15094. org-end-time-was-given
  15095. (substring txt (match-end 0)))))
  15096. (when org-read-date-analyze-futurep
  15097. (setq txt (concat txt " (=>F)")))
  15098. (setq org-read-date-overlay
  15099. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  15100. (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
  15101. (defun org-read-date-analyze (ans org-def org-defdecode)
  15102. "Analyze the combined answer of the date prompt."
  15103. ;; FIXME: cleanup and comment
  15104. ;; Pass `current-time' result to `decode-time' (instead of calling
  15105. ;; without arguments) so that only `current-time' has to be
  15106. ;; overriden in tests.
  15107. (let ((nowdecode (decode-time (current-time)))
  15108. delta deltan deltaw deltadef year month day
  15109. hour minute second wday pm h2 m2 tl wday1
  15110. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  15111. (setq org-read-date-analyze-futurep nil
  15112. org-read-date-analyze-forced-year nil)
  15113. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  15114. (setq ans "+0"))
  15115. (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
  15116. (setq ans (replace-match "" t t ans)
  15117. deltan (car delta)
  15118. deltaw (nth 1 delta)
  15119. deltadef (nth 2 delta)))
  15120. ;; Check if there is an iso week date in there. If yes, store the
  15121. ;; info and postpone interpreting it until the rest of the parsing
  15122. ;; is done.
  15123. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  15124. (setq iso-year (if (match-end 1)
  15125. (org-small-year-to-year
  15126. (string-to-number (match-string 1 ans))))
  15127. iso-weekday (if (match-end 3)
  15128. (string-to-number (match-string 3 ans)))
  15129. iso-week (string-to-number (match-string 2 ans)))
  15130. (setq ans (replace-match "" t t ans)))
  15131. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  15132. (when (string-match
  15133. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  15134. (setq year (if (match-end 2)
  15135. (string-to-number (match-string 2 ans))
  15136. (progn (setq kill-year t)
  15137. (string-to-number (format-time-string "%Y"))))
  15138. month (string-to-number (match-string 3 ans))
  15139. day (string-to-number (match-string 4 ans)))
  15140. (if (< year 100) (setq year (+ 2000 year)))
  15141. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15142. t nil ans)))
  15143. ;; Help matching dotted european dates
  15144. (when (string-match
  15145. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\.\\( ?[1-9][0-9]\\{3\\}\\)?" ans)
  15146. (setq year (if (match-end 3) (string-to-number (match-string 3 ans))
  15147. (setq kill-year t)
  15148. (string-to-number (format-time-string "%Y")))
  15149. day (string-to-number (match-string 1 ans))
  15150. month (string-to-number (match-string 2 ans))
  15151. ans (replace-match (format "%04d-%02d-%02d" year month day)
  15152. t nil ans)))
  15153. ;; Help matching american dates, like 5/30 or 5/30/7
  15154. (when (string-match
  15155. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  15156. (setq year (if (match-end 4)
  15157. (string-to-number (match-string 4 ans))
  15158. (progn (setq kill-year t)
  15159. (string-to-number (format-time-string "%Y"))))
  15160. month (string-to-number (match-string 1 ans))
  15161. day (string-to-number (match-string 2 ans)))
  15162. (if (< year 100) (setq year (+ 2000 year)))
  15163. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15164. t nil ans)))
  15165. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  15166. ;; If there is a time with am/pm, and *no* time without it, we convert
  15167. ;; so that matching will be successful.
  15168. (loop for i from 1 to 2 do ; twice, for end time as well
  15169. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  15170. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  15171. (setq hour (string-to-number (match-string 1 ans))
  15172. minute (if (match-end 3)
  15173. (string-to-number (match-string 3 ans))
  15174. 0)
  15175. pm (equal ?p
  15176. (string-to-char (downcase (match-string 4 ans)))))
  15177. (if (and (= hour 12) (not pm))
  15178. (setq hour 0)
  15179. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  15180. (setq ans (replace-match (format "%02d:%02d" hour minute)
  15181. t t ans))))
  15182. ;; Check if a time range is given as a duration
  15183. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  15184. (setq hour (string-to-number (match-string 1 ans))
  15185. h2 (+ hour (string-to-number (match-string 3 ans)))
  15186. minute (string-to-number (match-string 2 ans))
  15187. m2 (+ minute (if (match-end 5) (string-to-number
  15188. (match-string 5 ans))0)))
  15189. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  15190. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  15191. t t ans)))
  15192. ;; Check if there is a time range
  15193. (when (boundp 'org-end-time-was-given)
  15194. (setq org-time-was-given nil)
  15195. (when (and (string-match org-plain-time-of-day-regexp ans)
  15196. (match-end 8))
  15197. (setq org-end-time-was-given (match-string 8 ans))
  15198. (setq ans (concat (substring ans 0 (match-beginning 7))
  15199. (substring ans (match-end 7))))))
  15200. (setq tl (parse-time-string ans)
  15201. day (or (nth 3 tl) (nth 3 org-defdecode))
  15202. month (or (nth 4 tl)
  15203. (if (and org-read-date-prefer-future
  15204. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  15205. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  15206. (nth 4 org-defdecode)))
  15207. year (or (and (not kill-year) (nth 5 tl))
  15208. (if (and org-read-date-prefer-future
  15209. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  15210. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  15211. (nth 5 org-defdecode)))
  15212. hour (or (nth 2 tl) (nth 2 org-defdecode))
  15213. minute (or (nth 1 tl) (nth 1 org-defdecode))
  15214. second (or (nth 0 tl) 0)
  15215. wday (nth 6 tl))
  15216. (when (and (eq org-read-date-prefer-future 'time)
  15217. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  15218. (equal day (nth 3 nowdecode))
  15219. (equal month (nth 4 nowdecode))
  15220. (equal year (nth 5 nowdecode))
  15221. (nth 2 tl)
  15222. (or (< (nth 2 tl) (nth 2 nowdecode))
  15223. (and (= (nth 2 tl) (nth 2 nowdecode))
  15224. (nth 1 tl)
  15225. (< (nth 1 tl) (nth 1 nowdecode)))))
  15226. (setq day (1+ day)
  15227. futurep t))
  15228. ;; Special date definitions below
  15229. (cond
  15230. (iso-week
  15231. ;; There was an iso week
  15232. (require 'cal-iso)
  15233. (setq futurep nil)
  15234. (setq year (or iso-year year)
  15235. day (or iso-weekday wday 1)
  15236. wday nil ; to make sure that the trigger below does not match
  15237. iso-date (calendar-gregorian-from-absolute
  15238. (calendar-iso-to-absolute
  15239. (list iso-week day year))))
  15240. ; FIXME: Should we also push ISO weeks into the future?
  15241. ; (when (and org-read-date-prefer-future
  15242. ; (not iso-year)
  15243. ; (< (calendar-absolute-from-gregorian iso-date)
  15244. ; (time-to-days (current-time))))
  15245. ; (setq year (1+ year)
  15246. ; iso-date (calendar-gregorian-from-absolute
  15247. ; (calendar-iso-to-absolute
  15248. ; (list iso-week day year)))))
  15249. (setq month (car iso-date)
  15250. year (nth 2 iso-date)
  15251. day (nth 1 iso-date)))
  15252. (deltan
  15253. (setq futurep nil)
  15254. (unless deltadef
  15255. ;; Pass `current-time' result to `decode-time' (instead of
  15256. ;; calling without arguments) so that only `current-time' has
  15257. ;; to be overriden in tests.
  15258. (let ((now (decode-time (current-time))))
  15259. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  15260. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  15261. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  15262. ((equal deltaw "m") (setq month (+ month deltan)))
  15263. ((equal deltaw "y") (setq year (+ year deltan)))))
  15264. ((and wday (not (nth 3 tl)))
  15265. ;; Weekday was given, but no day, so pick that day in the week
  15266. ;; on or after the derived date.
  15267. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  15268. (unless (equal wday wday1)
  15269. (setq day (+ day (% (- wday wday1 -7) 7))))))
  15270. (if (and (boundp 'org-time-was-given)
  15271. (nth 2 tl))
  15272. (setq org-time-was-given t))
  15273. (if (< year 100) (setq year (+ 2000 year)))
  15274. ;; Check of the date is representable
  15275. (if org-read-date-force-compatible-dates
  15276. (progn
  15277. (if (< year 1970)
  15278. (setq year 1970 org-read-date-analyze-forced-year t))
  15279. (if (> year 2037)
  15280. (setq year 2037 org-read-date-analyze-forced-year t)))
  15281. (condition-case nil
  15282. (ignore (encode-time second minute hour day month year))
  15283. (error
  15284. (setq year (nth 5 org-defdecode))
  15285. (setq org-read-date-analyze-forced-year t))))
  15286. (setq org-read-date-analyze-futurep futurep)
  15287. (list second minute hour day month year)))
  15288. (defvar parse-time-weekdays)
  15289. (defun org-read-date-get-relative (s today default)
  15290. "Check string S for special relative date string.
  15291. TODAY and DEFAULT are internal times, for today and for a default.
  15292. Return shift list (N what def-flag)
  15293. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  15294. N is the number of WHATs to shift.
  15295. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  15296. the DEFAULT date rather than TODAY."
  15297. (require 'parse-time)
  15298. (when (and
  15299. (string-match
  15300. (concat
  15301. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  15302. "\\([0-9]+\\)?"
  15303. "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  15304. "\\([ \t]\\|$\\)") s)
  15305. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  15306. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  15307. (string-to-char (substring (match-string 1 s) -1))
  15308. ?+))
  15309. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  15310. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  15311. (what (if (match-end 3) (match-string 3 s) "d"))
  15312. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  15313. (date (if rel default today))
  15314. (wday (nth 6 (decode-time date)))
  15315. delta)
  15316. (if wday1
  15317. (progn
  15318. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  15319. (if (= delta 0) (setq delta 7))
  15320. (if (= dir ?-)
  15321. (progn
  15322. (setq delta (- delta 7))
  15323. (if (= delta 0) (setq delta -7))))
  15324. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  15325. (list delta "d" rel))
  15326. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  15327. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  15328. "Turn a user-specified date into the internal representation.
  15329. The internal representation needed by the calendar is (month day year).
  15330. This is a wrapper to handle the brain-dead convention in calendar that
  15331. user function argument order change dependent on argument order."
  15332. (if (boundp 'calendar-date-style)
  15333. (cond
  15334. ((eq calendar-date-style 'american)
  15335. (list arg1 arg2 arg3))
  15336. ((eq calendar-date-style 'european)
  15337. (list arg2 arg1 arg3))
  15338. ((eq calendar-date-style 'iso)
  15339. (list arg2 arg3 arg1)))
  15340. (org-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  15341. (if (org-bound-and-true-p european-calendar-style)
  15342. (list arg2 arg1 arg3)
  15343. (list arg1 arg2 arg3)))))
  15344. (defun org-eval-in-calendar (form &optional keepdate)
  15345. "Eval FORM in the calendar window and return to current window.
  15346. When KEEPDATE is non-nil, update `org-ans2' from the cursor date,
  15347. otherwise stick to the current value of `org-ans2'."
  15348. (let ((sf (selected-frame))
  15349. (sw (selected-window)))
  15350. (select-window (get-buffer-window "*Calendar*" t))
  15351. (eval form)
  15352. (when (and (not keepdate) (calendar-cursor-to-date))
  15353. (let* ((date (calendar-cursor-to-date))
  15354. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15355. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  15356. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  15357. (select-window sw)
  15358. (org-select-frame-set-input-focus sf)))
  15359. (defun org-calendar-select ()
  15360. "Return to `org-read-date' with the date currently selected.
  15361. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15362. (interactive)
  15363. (when (calendar-cursor-to-date)
  15364. (let* ((date (calendar-cursor-to-date))
  15365. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15366. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15367. (if (active-minibuffer-window) (exit-minibuffer))))
  15368. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  15369. "Insert a date stamp for the date given by the internal TIME.
  15370. WITH-HM means use the stamp format that includes the time of the day.
  15371. INACTIVE means use square brackets instead of angular ones, so that the
  15372. stamp will not contribute to the agenda.
  15373. PRE and POST are optional strings to be inserted before and after the
  15374. stamp.
  15375. The command returns the inserted time stamp."
  15376. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  15377. stamp)
  15378. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  15379. (insert-before-markers (or pre ""))
  15380. (when (listp extra)
  15381. (setq extra (car extra))
  15382. (if (and (stringp extra)
  15383. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  15384. (setq extra (format "-%02d:%02d"
  15385. (string-to-number (match-string 1 extra))
  15386. (string-to-number (match-string 2 extra))))
  15387. (setq extra nil)))
  15388. (when extra
  15389. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  15390. (insert-before-markers (setq stamp (format-time-string fmt time)))
  15391. (insert-before-markers (or post ""))
  15392. (setq org-last-inserted-timestamp stamp)))
  15393. (defun org-toggle-time-stamp-overlays ()
  15394. "Toggle the use of custom time stamp formats."
  15395. (interactive)
  15396. (setq org-display-custom-times (not org-display-custom-times))
  15397. (unless org-display-custom-times
  15398. (let ((p (point-min)) (bmp (buffer-modified-p)))
  15399. (while (setq p (next-single-property-change p 'display))
  15400. (if (and (get-text-property p 'display)
  15401. (eq (get-text-property p 'face) 'org-date))
  15402. (remove-text-properties
  15403. p (setq p (next-single-property-change p 'display))
  15404. '(display t))))
  15405. (set-buffer-modified-p bmp)))
  15406. (if (featurep 'xemacs)
  15407. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  15408. (org-restart-font-lock)
  15409. (setq org-table-may-need-update t)
  15410. (if org-display-custom-times
  15411. (message "Time stamps are overlaid with custom format")
  15412. (message "Time stamp overlays removed")))
  15413. (defun org-display-custom-time (beg end)
  15414. "Overlay modified time stamp format over timestamp between BEG and END."
  15415. (let* ((ts (buffer-substring beg end))
  15416. t1 w1 with-hm tf time str w2 (off 0))
  15417. (save-match-data
  15418. (setq t1 (org-parse-time-string ts t))
  15419. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
  15420. (setq off (- (match-end 0) (match-beginning 0)))))
  15421. (setq end (- end off))
  15422. (setq w1 (- end beg)
  15423. with-hm (and (nth 1 t1) (nth 2 t1))
  15424. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  15425. time (org-fix-decoded-time t1)
  15426. str (org-add-props
  15427. (format-time-string
  15428. (substring tf 1 -1) (apply 'encode-time time))
  15429. nil 'mouse-face 'highlight)
  15430. w2 (length str))
  15431. (if (not (= w2 w1))
  15432. (add-text-properties (1+ beg) (+ 2 beg)
  15433. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  15434. (if (featurep 'xemacs)
  15435. (progn
  15436. (put-text-property beg end 'invisible t)
  15437. (put-text-property beg end 'end-glyph (make-glyph str)))
  15438. (put-text-property beg end 'display str))))
  15439. (defun org-translate-time (string)
  15440. "Translate all timestamps in STRING to custom format.
  15441. But do this only if the variable `org-display-custom-times' is set."
  15442. (when org-display-custom-times
  15443. (save-match-data
  15444. (let* ((start 0)
  15445. (re org-ts-regexp-both)
  15446. t1 with-hm inactive tf time str beg end)
  15447. (while (setq start (string-match re string start))
  15448. (setq beg (match-beginning 0)
  15449. end (match-end 0)
  15450. t1 (save-match-data
  15451. (org-parse-time-string (substring string beg end) t))
  15452. with-hm (and (nth 1 t1) (nth 2 t1))
  15453. inactive (equal (substring string beg (1+ beg)) "[")
  15454. tf (funcall (if with-hm 'cdr 'car)
  15455. org-time-stamp-custom-formats)
  15456. time (org-fix-decoded-time t1)
  15457. str (format-time-string
  15458. (concat
  15459. (if inactive "[" "<") (substring tf 1 -1)
  15460. (if inactive "]" ">"))
  15461. (apply 'encode-time time))
  15462. string (replace-match str t t string)
  15463. start (+ start (length str)))))))
  15464. string)
  15465. (defun org-fix-decoded-time (time)
  15466. "Set 0 instead of nil for the first 6 elements of time.
  15467. Don't touch the rest."
  15468. (let ((n 0))
  15469. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  15470. (define-obsolete-function-alias 'org-days-to-time 'org-time-stamp-to-now "24.4")
  15471. (defun org-time-stamp-to-now (timestamp-string &optional seconds)
  15472. "Difference between TIMESTAMP-STRING and now in days.
  15473. If SECONDS is non-nil, return the difference in seconds."
  15474. (let ((fdiff (if seconds 'org-float-time 'time-to-days)))
  15475. (- (funcall fdiff (org-time-string-to-time timestamp-string))
  15476. (funcall fdiff (current-time)))))
  15477. (defun org-deadline-close (timestamp-string &optional ndays)
  15478. "Is the time in TIMESTAMP-STRING close to the current date?"
  15479. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  15480. (and (< (org-time-stamp-to-now timestamp-string) ndays)
  15481. (not (org-entry-is-done-p))))
  15482. (defun org-get-wdays (ts &optional delay zero-delay)
  15483. "Get the deadline lead time appropriate for timestring TS.
  15484. When DELAY is non-nil, get the delay time for scheduled items
  15485. instead of the deadline lead time. When ZERO-DELAY is non-nil
  15486. and `org-scheduled-delay-days' is 0, enforce 0 as the delay,
  15487. don't try to find the delay cookie in the scheduled timestamp."
  15488. (let ((tv (if delay org-scheduled-delay-days
  15489. org-deadline-warning-days)))
  15490. (cond
  15491. ((or (and delay (< tv 0))
  15492. (and delay zero-delay (<= tv 0))
  15493. (and (not delay) (<= tv 0)))
  15494. ;; Enforce this value no matter what
  15495. (- tv))
  15496. ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
  15497. ;; lead time is specified.
  15498. (floor (* (string-to-number (match-string 1 ts))
  15499. (cdr (assoc (match-string 2 ts)
  15500. '(("d" . 1) ("w" . 7)
  15501. ("m" . 30.4) ("y" . 365.25)
  15502. ("h" . 0.041667)))))))
  15503. ;; go for the default.
  15504. (t tv))))
  15505. (defun org-calendar-select-mouse (ev)
  15506. "Return to `org-read-date' with the date currently selected.
  15507. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15508. (interactive "e")
  15509. (mouse-set-point ev)
  15510. (when (calendar-cursor-to-date)
  15511. (let* ((date (calendar-cursor-to-date))
  15512. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15513. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15514. (if (active-minibuffer-window) (exit-minibuffer))))
  15515. (defun org-check-deadlines (ndays)
  15516. "Check if there are any deadlines due or past due.
  15517. A deadline is considered due if it happens within `org-deadline-warning-days'
  15518. days from today's date. If the deadline appears in an entry marked DONE,
  15519. it is not shown. The prefix arg NDAYS can be used to test that many
  15520. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  15521. (interactive "P")
  15522. (let* ((org-warn-days
  15523. (cond
  15524. ((equal ndays '(4)) 100000)
  15525. (ndays (prefix-numeric-value ndays))
  15526. (t (abs org-deadline-warning-days))))
  15527. (case-fold-search nil)
  15528. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15529. (callback
  15530. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  15531. (message "%d deadlines past-due or due within %d days"
  15532. (org-occur regexp nil callback)
  15533. org-warn-days)))
  15534. (defsubst org-re-timestamp (type)
  15535. "Return a regexp for timestamp TYPE.
  15536. Allowed values for TYPE are:
  15537. all: all timestamps
  15538. active: only active timestamps (<...>)
  15539. inactive: only inactive timestamps ([...])
  15540. scheduled: only scheduled timestamps
  15541. deadline: only deadline timestamps
  15542. closed: only closed time-stamps
  15543. When TYPE is nil, fall back on returning a regexp that matches
  15544. both scheduled and deadline timestamps."
  15545. (cond ((eq type 'all) "\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}\\(?: +[^]+0-9> \n -]+\\)?\\(?: +[0-9]\\{1,2\\}:[0-9]\\{2\\}\\)?\\)")
  15546. ((eq type 'active) org-ts-regexp)
  15547. ((eq type 'inactive) "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^ \n>]*?\\)\\]")
  15548. ((eq type 'scheduled) (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>"))
  15549. ((eq type 'deadline) (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15550. ((eq type 'closed) (concat org-closed-string " \\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^ \n>]*?\\)\\]"))
  15551. ((eq type 'scheduled-or-deadline)
  15552. (concat "\\<\\(?:" org-deadline-string "\\|" org-scheduled-string "\\) *<\\([^>]+\\)>"))))
  15553. (defun org-check-before-date (date)
  15554. "Check if there are deadlines or scheduled entries before DATE."
  15555. (interactive (list (org-read-date)))
  15556. (let ((case-fold-search nil)
  15557. (regexp (org-re-timestamp org-ts-type))
  15558. (callback
  15559. (lambda () (time-less-p
  15560. (org-time-string-to-time (match-string 1))
  15561. (org-time-string-to-time date)))))
  15562. (message "%d entries before %s"
  15563. (org-occur regexp nil callback) date)))
  15564. (defun org-check-after-date (date)
  15565. "Check if there are deadlines or scheduled entries after DATE."
  15566. (interactive (list (org-read-date)))
  15567. (let ((case-fold-search nil)
  15568. (regexp (org-re-timestamp org-ts-type))
  15569. (callback
  15570. (lambda () (not
  15571. (time-less-p
  15572. (org-time-string-to-time (match-string 1))
  15573. (org-time-string-to-time date))))))
  15574. (message "%d entries after %s"
  15575. (org-occur regexp nil callback) date)))
  15576. (defun org-check-dates-range (start-date end-date)
  15577. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  15578. (interactive (list (org-read-date nil nil nil "Range starts")
  15579. (org-read-date nil nil nil "Range end")))
  15580. (let ((case-fold-search nil)
  15581. (regexp (org-re-timestamp org-ts-type))
  15582. (callback
  15583. (lambda ()
  15584. (let ((match (match-string 1)))
  15585. (and
  15586. (not (time-less-p
  15587. (org-time-string-to-time match)
  15588. (org-time-string-to-time start-date)))
  15589. (time-less-p
  15590. (org-time-string-to-time match)
  15591. (org-time-string-to-time end-date)))))))
  15592. (message "%d entries between %s and %s"
  15593. (org-occur regexp nil callback) start-date end-date)))
  15594. (defun org-evaluate-time-range (&optional to-buffer)
  15595. "Evaluate a time range by computing the difference between start and end.
  15596. Normally the result is just printed in the echo area, but with prefix arg
  15597. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  15598. If the time range is actually in a table, the result is inserted into the
  15599. next column.
  15600. For time difference computation, a year is assumed to be exactly 365
  15601. days in order to avoid rounding problems."
  15602. (interactive "P")
  15603. (or
  15604. (org-clock-update-time-maybe)
  15605. (save-excursion
  15606. (unless (org-at-date-range-p t)
  15607. (goto-char (point-at-bol))
  15608. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  15609. (if (not (org-at-date-range-p t))
  15610. (user-error "Not at a time-stamp range, and none found in current line")))
  15611. (let* ((ts1 (match-string 1))
  15612. (ts2 (match-string 2))
  15613. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  15614. (match-end (match-end 0))
  15615. (time1 (org-time-string-to-time ts1))
  15616. (time2 (org-time-string-to-time ts2))
  15617. (t1 (org-float-time time1))
  15618. (t2 (org-float-time time2))
  15619. (diff (abs (- t2 t1)))
  15620. (negative (< (- t2 t1) 0))
  15621. ;; (ys (floor (* 365 24 60 60)))
  15622. (ds (* 24 60 60))
  15623. (hs (* 60 60))
  15624. (fy "%dy %dd %02d:%02d")
  15625. (fy1 "%dy %dd")
  15626. (fd "%dd %02d:%02d")
  15627. (fd1 "%dd")
  15628. (fh "%02d:%02d")
  15629. y d h m align)
  15630. (if havetime
  15631. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15632. y 0
  15633. d (floor (/ diff ds)) diff (mod diff ds)
  15634. h (floor (/ diff hs)) diff (mod diff hs)
  15635. m (floor (/ diff 60)))
  15636. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15637. y 0
  15638. d (floor (+ (/ diff ds) 0.5))
  15639. h 0 m 0))
  15640. (if (not to-buffer)
  15641. (message "%s" (org-make-tdiff-string y d h m))
  15642. (if (org-at-table-p)
  15643. (progn
  15644. (goto-char match-end)
  15645. (setq align t)
  15646. (and (looking-at " *|") (goto-char (match-end 0))))
  15647. (goto-char match-end))
  15648. (if (looking-at
  15649. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  15650. (replace-match ""))
  15651. (if negative (insert " -"))
  15652. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  15653. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  15654. (insert " " (format fh h m))))
  15655. (if align (org-table-align))
  15656. (message "Time difference inserted")))))
  15657. (defun org-make-tdiff-string (y d h m)
  15658. (let ((fmt "")
  15659. (l nil))
  15660. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  15661. l (push y l)))
  15662. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  15663. l (push d l)))
  15664. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  15665. l (push h l)))
  15666. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  15667. l (push m l)))
  15668. (apply 'format fmt (nreverse l))))
  15669. (defun org-time-string-to-time (s &optional buffer pos)
  15670. "Convert a timestamp string into internal time."
  15671. (condition-case errdata
  15672. (apply 'encode-time (org-parse-time-string s))
  15673. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15674. s (if (not (and buffer pos))
  15675. ""
  15676. (format " at %d in buffer `%s'" pos buffer))
  15677. (cdr errdata)))))
  15678. (defun org-time-string-to-seconds (s)
  15679. "Convert a timestamp string to a number of seconds."
  15680. (org-float-time (org-time-string-to-time s)))
  15681. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  15682. "Convert a time stamp to an absolute day number.
  15683. If there is a specifier for a cyclic time stamp, get the closest
  15684. date to DAYNR.
  15685. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  15686. The variable `date' is bound by the calendar when this is called."
  15687. (cond
  15688. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  15689. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  15690. daynr
  15691. (+ daynr 1000)))
  15692. ((and daynr (string-match "\\+[0-9]+[hdwmy]" s))
  15693. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  15694. (time-to-days (current-time))) (match-string 0 s)
  15695. prefer show-all))
  15696. (t (time-to-days
  15697. (condition-case errdata
  15698. (apply 'encode-time (org-parse-time-string s))
  15699. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15700. s (if (not (and buffer pos))
  15701. ""
  15702. (format " at %d in buffer `%s'" pos buffer))
  15703. (cdr errdata))))))))
  15704. (defun org-days-to-iso-week (days)
  15705. "Return the iso week number."
  15706. (require 'cal-iso)
  15707. (car (calendar-iso-from-absolute days)))
  15708. (defun org-small-year-to-year (year)
  15709. "Convert 2-digit years into 4-digit years.
  15710. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2037.
  15711. The year 2000 cannot be abbreviated. Any year larger than 99
  15712. is returned unchanged."
  15713. (if (< year 38)
  15714. (setq year (+ 2000 year))
  15715. (if (< year 100)
  15716. (setq year (+ 1900 year))))
  15717. year)
  15718. (defun org-time-from-absolute (d)
  15719. "Return the time corresponding to date D.
  15720. D may be an absolute day number, or a calendar-type list (month day year)."
  15721. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  15722. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  15723. (defun org-calendar-holiday ()
  15724. "List of holidays, for Diary display in Org-mode."
  15725. (require 'holidays)
  15726. (let ((hl (funcall
  15727. (if (fboundp 'calendar-check-holidays)
  15728. 'calendar-check-holidays 'check-calendar-holidays) date)))
  15729. (if hl (mapconcat 'identity hl "; "))))
  15730. (defun org-diary-sexp-entry (sexp entry date)
  15731. "Process a SEXP diary ENTRY for DATE."
  15732. (require 'diary-lib)
  15733. (let ((result (if calendar-debug-sexp
  15734. (let ((stack-trace-on-error t))
  15735. (eval (car (read-from-string sexp))))
  15736. (condition-case nil
  15737. (eval (car (read-from-string sexp)))
  15738. (error
  15739. (beep)
  15740. (message "Bad sexp at line %d in %s: %s"
  15741. (org-current-line)
  15742. (buffer-file-name) sexp)
  15743. (sleep-for 2))))))
  15744. (cond ((stringp result) (split-string result "; "))
  15745. ((and (consp result)
  15746. (not (consp (cdr result)))
  15747. (stringp (cdr result))) (cdr result))
  15748. ((and (consp result)
  15749. (stringp (car result))) result)
  15750. (result entry))))
  15751. (defun org-diary-to-ical-string (frombuf)
  15752. "Get iCalendar entries from diary entries in buffer FROMBUF.
  15753. This uses the icalendar.el library."
  15754. (let* ((tmpdir (if (featurep 'xemacs)
  15755. (temp-directory)
  15756. temporary-file-directory))
  15757. (tmpfile (make-temp-name
  15758. (expand-file-name "orgics" tmpdir)))
  15759. buf rtn b e)
  15760. (with-current-buffer frombuf
  15761. (icalendar-export-region (point-min) (point-max) tmpfile)
  15762. (setq buf (find-buffer-visiting tmpfile))
  15763. (set-buffer buf)
  15764. (goto-char (point-min))
  15765. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  15766. (setq b (match-beginning 0)))
  15767. (goto-char (point-max))
  15768. (if (re-search-backward "^END:VEVENT" nil t)
  15769. (setq e (match-end 0)))
  15770. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  15771. (kill-buffer buf)
  15772. (delete-file tmpfile)
  15773. rtn))
  15774. (defun org-closest-date (start current change prefer show-all)
  15775. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  15776. When PREFER is `past', return a date that is either CURRENT or past.
  15777. When PREFER is `future', return a date that is either CURRENT or future.
  15778. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  15779. ;; Make the proper lists from the dates
  15780. (catch 'exit
  15781. (let ((a1 '(("h" . hour)
  15782. ("d" . day)
  15783. ("w" . week)
  15784. ("m" . month)
  15785. ("y" . year)))
  15786. (shour (nth 2 (org-parse-time-string start)))
  15787. dn dw sday cday n1 n2 n0
  15788. d m y y1 y2 date1 date2 nmonths nm ny m2)
  15789. (setq start (org-date-to-gregorian start)
  15790. current (org-date-to-gregorian
  15791. (if show-all
  15792. current
  15793. (time-to-days (current-time))))
  15794. sday (calendar-absolute-from-gregorian start)
  15795. cday (calendar-absolute-from-gregorian current))
  15796. (if (<= cday sday) (throw 'exit sday))
  15797. (if (string-match "\\(\\+[0-9]+\\)\\([hdwmy]\\)" change)
  15798. (setq dn (string-to-number (match-string 1 change))
  15799. dw (cdr (assoc (match-string 2 change) a1)))
  15800. (user-error "Invalid change specifier: %s" change))
  15801. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  15802. (cond
  15803. ((eq dw 'hour)
  15804. (let ((missing-hours
  15805. (mod (+ (- (* 24 (- cday sday)) shour) org-extend-today-until)
  15806. dn)))
  15807. (setq n1 (if (zerop missing-hours) cday
  15808. (- cday (1+ (floor (/ missing-hours 24)))))
  15809. n2 (+ cday (floor (/ (- dn missing-hours) 24))))))
  15810. ((eq dw 'day)
  15811. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  15812. n2 (+ n1 dn)))
  15813. ((eq dw 'year)
  15814. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  15815. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  15816. (setq date1 (list m d y1)
  15817. n1 (calendar-absolute-from-gregorian date1)
  15818. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  15819. n2 (calendar-absolute-from-gregorian date2)))
  15820. ((eq dw 'month)
  15821. ;; approx number of month between the two dates
  15822. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  15823. ;; How often does dn fit in there?
  15824. (setq d (nth 1 start) m (car start) y (nth 2 start)
  15825. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  15826. m (+ m nm)
  15827. ny (floor (/ m 12))
  15828. y (+ y ny)
  15829. m (- m (* ny 12)))
  15830. (while (> m 12) (setq m (- m 12) y (1+ y)))
  15831. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  15832. (setq m2 (+ m dn) y2 y)
  15833. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15834. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  15835. (while (<= n2 cday)
  15836. (setq n1 n2 m m2 y y2)
  15837. (setq m2 (+ m dn) y2 y)
  15838. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15839. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  15840. ;; Make sure n1 is the earlier date
  15841. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  15842. (if show-all
  15843. (cond
  15844. ((eq prefer 'past) (if (= cday n2) n2 n1))
  15845. ((eq prefer 'future) (if (= cday n1) n1 n2))
  15846. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  15847. (cond
  15848. ((eq prefer 'past) (if (= cday n2) n2 n1))
  15849. ((eq prefer 'future) (if (= cday n1) n1 n2))
  15850. (t (if (= cday n1) n1 n2)))))))
  15851. (defun org-date-to-gregorian (date)
  15852. "Turn any specification of DATE into a Gregorian date for the calendar."
  15853. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  15854. ((and (listp date) (= (length date) 3)) date)
  15855. ((stringp date)
  15856. (setq date (org-parse-time-string date))
  15857. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  15858. ((listp date)
  15859. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  15860. (defun org-parse-time-string (s &optional nodefault)
  15861. "Parse the standard Org-mode time string.
  15862. This should be a lot faster than the normal `parse-time-string'.
  15863. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  15864. hour and minute fields will be nil if not given."
  15865. (cond ((string-match org-ts-regexp0 s)
  15866. (list 0
  15867. (if (or (match-beginning 8) (not nodefault))
  15868. (string-to-number (or (match-string 8 s) "0")))
  15869. (if (or (match-beginning 7) (not nodefault))
  15870. (string-to-number (or (match-string 7 s) "0")))
  15871. (string-to-number (match-string 4 s))
  15872. (string-to-number (match-string 3 s))
  15873. (string-to-number (match-string 2 s))
  15874. nil nil nil))
  15875. ((string-match "^<[^>]+>$" s)
  15876. (decode-time (seconds-to-time (org-matcher-time s))))
  15877. (t (error "Not a standard Org-mode time string: %s" s))))
  15878. (defun org-timestamp-up (&optional arg)
  15879. "Increase the date item at the cursor by one.
  15880. If the cursor is on the year, change the year. If it is on the month,
  15881. the day or the time, change that.
  15882. With prefix ARG, change by that many units."
  15883. (interactive "p")
  15884. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  15885. (defun org-timestamp-down (&optional arg)
  15886. "Decrease the date item at the cursor by one.
  15887. If the cursor is on the year, change the year. If it is on the month,
  15888. the day or the time, change that.
  15889. With prefix ARG, change by that many units."
  15890. (interactive "p")
  15891. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  15892. (defun org-timestamp-up-day (&optional arg)
  15893. "Increase the date in the time stamp by one day.
  15894. With prefix ARG, change that many days."
  15895. (interactive "p")
  15896. (if (and (not (org-at-timestamp-p t))
  15897. (org-at-heading-p))
  15898. (org-todo 'up)
  15899. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  15900. (defun org-timestamp-down-day (&optional arg)
  15901. "Decrease the date in the time stamp by one day.
  15902. With prefix ARG, change that many days."
  15903. (interactive "p")
  15904. (if (and (not (org-at-timestamp-p t))
  15905. (org-at-heading-p))
  15906. (org-todo 'down)
  15907. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  15908. (defun org-at-timestamp-p (&optional inactive-ok)
  15909. "Determine if the cursor is in or at a timestamp."
  15910. (interactive)
  15911. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  15912. (pos (point))
  15913. (ans (or (looking-at tsr)
  15914. (save-excursion
  15915. (skip-chars-backward "^[<\n\r\t")
  15916. (if (> (point) (point-min)) (backward-char 1))
  15917. (and (looking-at tsr)
  15918. (> (- (match-end 0) pos) -1))))))
  15919. (and ans
  15920. (boundp 'org-ts-what)
  15921. (setq org-ts-what
  15922. (cond
  15923. ((= pos (match-beginning 0)) 'bracket)
  15924. ;; Point is considered to be "on the bracket" whether
  15925. ;; it's really on it or right after it.
  15926. ((= pos (1- (match-end 0))) 'bracket)
  15927. ((= pos (match-end 0)) 'after)
  15928. ((org-pos-in-match-range pos 2) 'year)
  15929. ((org-pos-in-match-range pos 3) 'month)
  15930. ((org-pos-in-match-range pos 7) 'hour)
  15931. ((org-pos-in-match-range pos 8) 'minute)
  15932. ((or (org-pos-in-match-range pos 4)
  15933. (org-pos-in-match-range pos 5)) 'day)
  15934. ((and (> pos (or (match-end 8) (match-end 5)))
  15935. (< pos (match-end 0)))
  15936. (- pos (or (match-end 8) (match-end 5))))
  15937. (t 'day))))
  15938. ans))
  15939. (defun org-toggle-timestamp-type ()
  15940. "Toggle the type (<active> or [inactive]) of a time stamp."
  15941. (interactive)
  15942. (when (org-at-timestamp-p t)
  15943. (let ((beg (match-beginning 0)) (end (match-end 0))
  15944. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  15945. (save-excursion
  15946. (goto-char beg)
  15947. (while (re-search-forward "[][<>]" end t)
  15948. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  15949. t t)))
  15950. (message "Timestamp is now %sactive"
  15951. (if (equal (char-after beg) ?<) "" "in")))))
  15952. (defun org-at-clock-log-p nil
  15953. "Is the cursor on the clock log line?"
  15954. (save-excursion
  15955. (move-beginning-of-line 1)
  15956. (looking-at "^[ \t]*CLOCK:")))
  15957. (defvar org-clock-history) ; defined in org-clock.el
  15958. (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
  15959. (defun org-timestamp-change (n &optional what updown suppress-tmp-delay)
  15960. "Change the date in the time stamp at point.
  15961. The date will be changed by N times WHAT. WHAT can be `day', `month',
  15962. `year', `minute', `second'. If WHAT is not given, the cursor position
  15963. in the timestamp determines what will be changed.
  15964. When SUPPRESS-TMP-DELAY is non-nil, suppress delays like \"--2d\"."
  15965. (let ((origin (point)) origin-cat
  15966. with-hm inactive
  15967. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  15968. org-ts-what
  15969. extra rem
  15970. ts time time0 fixnext clrgx)
  15971. (if (not (org-at-timestamp-p t))
  15972. (user-error "Not at a timestamp"))
  15973. (if (and (not what) (eq org-ts-what 'bracket))
  15974. (org-toggle-timestamp-type)
  15975. ;; Point isn't on brackets. Remember the part of the time-stamp
  15976. ;; the point was in. Indeed, size of time-stamps may change,
  15977. ;; but point must be kept in the same category nonetheless.
  15978. (setq origin-cat org-ts-what)
  15979. (if (and (not what) (not (eq org-ts-what 'day))
  15980. org-display-custom-times
  15981. (get-text-property (point) 'display)
  15982. (not (get-text-property (1- (point)) 'display)))
  15983. (setq org-ts-what 'day))
  15984. (setq org-ts-what (or what org-ts-what)
  15985. inactive (= (char-after (match-beginning 0)) ?\[)
  15986. ts (match-string 0))
  15987. (replace-match "")
  15988. (when (string-match
  15989. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?-?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
  15990. ts)
  15991. (setq extra (match-string 1 ts))
  15992. (if suppress-tmp-delay
  15993. (setq extra (replace-regexp-in-string " --[0-9]+[hdwmy]" "" extra))))
  15994. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  15995. (setq with-hm t))
  15996. (setq time0 (org-parse-time-string ts))
  15997. (when (and updown
  15998. (eq org-ts-what 'minute)
  15999. (not current-prefix-arg))
  16000. ;; This looks like s-up and s-down. Change by one rounding step.
  16001. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  16002. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  16003. (setcar (cdr time0) (+ (nth 1 time0)
  16004. (if (> n 0) (- rem) (- dm rem))))))
  16005. (setq time
  16006. (encode-time (or (car time0) 0)
  16007. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  16008. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  16009. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  16010. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  16011. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  16012. (nthcdr 6 time0)))
  16013. (when (and (member org-ts-what '(hour minute))
  16014. extra
  16015. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  16016. (setq extra (org-modify-ts-extra
  16017. extra
  16018. (if (eq org-ts-what 'hour) 2 5)
  16019. n dm)))
  16020. (when (integerp org-ts-what)
  16021. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  16022. (if (eq what 'calendar)
  16023. (let ((cal-date (org-get-date-from-calendar)))
  16024. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  16025. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  16026. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  16027. (setcar time0 (or (car time0) 0))
  16028. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  16029. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  16030. (setq time (apply 'encode-time time0))))
  16031. ;; Insert the new time-stamp, and ensure point stays in the same
  16032. ;; category as before (i.e. not after the last position in that
  16033. ;; category).
  16034. (let ((pos (point)))
  16035. ;; Stay before inserted string. `save-excursion' is of no use.
  16036. (setq org-last-changed-timestamp
  16037. (org-insert-time-stamp time with-hm inactive nil nil extra))
  16038. (goto-char pos))
  16039. (save-match-data
  16040. (looking-at org-ts-regexp3)
  16041. (goto-char (cond
  16042. ;; `day' category ends before `hour' if any, or at
  16043. ;; the end of the day name.
  16044. ((eq origin-cat 'day)
  16045. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  16046. ((eq origin-cat 'hour) (min (match-end 7) origin))
  16047. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  16048. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  16049. ;; `year' and `month' have both fixed size: point
  16050. ;; couldn't have moved into another part.
  16051. (t origin))))
  16052. ;; Update clock if on a CLOCK line.
  16053. (org-clock-update-time-maybe)
  16054. ;; Maybe adjust the closest clock in `org-clock-history'
  16055. (when org-clock-adjust-closest
  16056. (if (not (and (org-at-clock-log-p)
  16057. (< 1 (length (delq nil (mapcar 'marker-position
  16058. org-clock-history))))))
  16059. (message "No clock to adjust")
  16060. (cond ((save-excursion ; fix previous clock?
  16061. (re-search-backward org-ts-regexp0 nil t)
  16062. (org-looking-back (concat org-clock-string " \\[")))
  16063. (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
  16064. ((save-excursion ; fix next clock?
  16065. (re-search-backward org-ts-regexp0 nil t)
  16066. (looking-at (concat org-ts-regexp0 "\\] =>")))
  16067. (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
  16068. (save-window-excursion
  16069. ;; Find closest clock to point, adjust the previous/next one in history
  16070. (let* ((p (save-excursion (org-back-to-heading t)))
  16071. (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
  16072. (clfixnth
  16073. (+ fixnext (- (length cl) (or (length (member (apply #'min cl) cl)) 100))))
  16074. (clfixpos (if (> 0 clfixnth) nil (nth clfixnth org-clock-history))))
  16075. (if (not clfixpos)
  16076. (message "No clock to adjust")
  16077. (save-excursion
  16078. (org-goto-marker-or-bmk clfixpos)
  16079. (org-show-subtree)
  16080. (when (re-search-forward clrgx nil t)
  16081. (goto-char (match-beginning 1))
  16082. (let (org-clock-adjust-closest)
  16083. (org-timestamp-change n org-ts-what updown))
  16084. (message "Clock adjusted in %s for heading: %s"
  16085. (file-name-nondirectory (buffer-file-name))
  16086. (org-get-heading t t)))))))))
  16087. ;; Try to recenter the calendar window, if any.
  16088. (if (and org-calendar-follow-timestamp-change
  16089. (get-buffer-window "*Calendar*" t)
  16090. (memq org-ts-what '(day month year)))
  16091. (org-recenter-calendar (time-to-days time))))))
  16092. (defun org-modify-ts-extra (s pos n dm)
  16093. "Change the different parts of the lead-time and repeat fields in timestamp."
  16094. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  16095. ng h m new rem)
  16096. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  16097. (cond
  16098. ((or (org-pos-in-match-range pos 2)
  16099. (org-pos-in-match-range pos 3))
  16100. (setq m (string-to-number (match-string 3 s))
  16101. h (string-to-number (match-string 2 s)))
  16102. (if (org-pos-in-match-range pos 2)
  16103. (setq h (+ h n))
  16104. (setq n (* dm (org-no-warnings (signum n))))
  16105. (when (not (= 0 (setq rem (% m dm))))
  16106. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  16107. (setq m (+ m n)))
  16108. (if (< m 0) (setq m (+ m 60) h (1- h)))
  16109. (if (> m 59) (setq m (- m 60) h (1+ h)))
  16110. (setq h (min 24 (max 0 h)))
  16111. (setq ng 1 new (format "-%02d:%02d" h m)))
  16112. ((org-pos-in-match-range pos 6)
  16113. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  16114. ((org-pos-in-match-range pos 5)
  16115. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  16116. ((org-pos-in-match-range pos 9)
  16117. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  16118. ((org-pos-in-match-range pos 8)
  16119. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  16120. (when ng
  16121. (setq s (concat
  16122. (substring s 0 (match-beginning ng))
  16123. new
  16124. (substring s (match-end ng))))))
  16125. s))
  16126. (defun org-recenter-calendar (date)
  16127. "If the calendar is visible, recenter it to DATE."
  16128. (let ((cwin (get-buffer-window "*Calendar*" t)))
  16129. (when cwin
  16130. (let ((calendar-move-hook nil))
  16131. (with-selected-window cwin
  16132. (calendar-goto-date (if (listp date) date
  16133. (calendar-gregorian-from-absolute date))))))))
  16134. (defun org-goto-calendar (&optional arg)
  16135. "Go to the Emacs calendar at the current date.
  16136. If there is a time stamp in the current line, go to that date.
  16137. A prefix ARG can be used to force the current date."
  16138. (interactive "P")
  16139. (let ((tsr org-ts-regexp) diff
  16140. (calendar-move-hook nil)
  16141. (calendar-view-holidays-initially-flag nil)
  16142. (calendar-view-diary-initially-flag nil))
  16143. (if (or (org-at-timestamp-p)
  16144. (save-excursion
  16145. (beginning-of-line 1)
  16146. (looking-at (concat ".*" tsr))))
  16147. (let ((d1 (time-to-days (current-time)))
  16148. (d2 (time-to-days
  16149. (org-time-string-to-time (match-string 1)))))
  16150. (setq diff (- d2 d1))))
  16151. (calendar)
  16152. (calendar-goto-today)
  16153. (if (and diff (not arg)) (calendar-forward-day diff))))
  16154. (defun org-get-date-from-calendar ()
  16155. "Return a list (month day year) of date at point in calendar."
  16156. (with-current-buffer "*Calendar*"
  16157. (save-match-data
  16158. (calendar-cursor-to-date))))
  16159. (defun org-date-from-calendar ()
  16160. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  16161. If there is already a time stamp at the cursor position, update it."
  16162. (interactive)
  16163. (if (org-at-timestamp-p t)
  16164. (org-timestamp-change 0 'calendar)
  16165. (let ((cal-date (org-get-date-from-calendar)))
  16166. (org-insert-time-stamp
  16167. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  16168. (defcustom org-effort-durations
  16169. `(("h" . 60)
  16170. ("d" . ,(* 60 8))
  16171. ("w" . ,(* 60 8 5))
  16172. ("m" . ,(* 60 8 5 4))
  16173. ("y" . ,(* 60 8 5 40)))
  16174. "Conversion factor to minutes for an effort modifier.
  16175. Each entry has the form (MODIFIER . MINUTES).
  16176. In an effort string, a number followed by MODIFIER is multiplied
  16177. by the specified number of MINUTES to obtain an effort in
  16178. minutes.
  16179. For example, if the value of this variable is ((\"hours\" . 60)), then an
  16180. effort string \"2hours\" is equivalent to 120 minutes."
  16181. :group 'org-agenda
  16182. :version "24.1"
  16183. :type '(alist :key-type (string :tag "Modifier")
  16184. :value-type (number :tag "Minutes")))
  16185. (defun org-minutes-to-clocksum-string (m)
  16186. "Format number of minutes as a clocksum string.
  16187. The format is determined by `org-time-clocksum-format',
  16188. `org-time-clocksum-use-fractional' and
  16189. `org-time-clocksum-fractional-format' and
  16190. `org-time-clocksum-use-effort-durations'."
  16191. (let ((clocksum "")
  16192. (m (round m)) ; Don't allow fractions of minutes
  16193. h d w mo y fmt n)
  16194. (setq h (if org-time-clocksum-use-effort-durations
  16195. (cdr (assoc "h" org-effort-durations)) 60)
  16196. d (if org-time-clocksum-use-effort-durations
  16197. (/ (cdr (assoc "d" org-effort-durations)) h) 24)
  16198. w (if org-time-clocksum-use-effort-durations
  16199. (/ (cdr (assoc "w" org-effort-durations)) (* d h)) 7)
  16200. mo (if org-time-clocksum-use-effort-durations
  16201. (/ (cdr (assoc "m" org-effort-durations)) (* d h)) 30)
  16202. y (if org-time-clocksum-use-effort-durations
  16203. (/ (cdr (assoc "y" org-effort-durations)) (* d h)) 365))
  16204. ;; fractional format
  16205. (if org-time-clocksum-use-fractional
  16206. (cond
  16207. ;; single format string
  16208. ((stringp org-time-clocksum-fractional-format)
  16209. (format org-time-clocksum-fractional-format (/ m (float h))))
  16210. ;; choice of fractional formats for different time units
  16211. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16212. (> (/ (truncate m) (* y d h)) 0))
  16213. (format fmt (/ m (* y d (float h)))))
  16214. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16215. (> (/ (truncate m) (* mo d h)) 0))
  16216. (format fmt (/ m (* mo d (float h)))))
  16217. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16218. (> (/ (truncate m) (* w d h)) 0))
  16219. (format fmt (/ m (* w d (float h)))))
  16220. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16221. (> (/ (truncate m) (* d h)) 0))
  16222. (format fmt (/ m (* d (float h)))))
  16223. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16224. (> (/ (truncate m) h) 0))
  16225. (format fmt (/ m (float h))))
  16226. ((setq fmt (plist-get org-time-clocksum-fractional-format :minutes))
  16227. (format fmt m))
  16228. ;; fall back to smallest time unit with a format
  16229. ((setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16230. (format fmt (/ m (float h))))
  16231. ((setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16232. (format fmt (/ m (* d (float h)))))
  16233. ((setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16234. (format fmt (/ m (* w d (float h)))))
  16235. ((setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16236. (format fmt (/ m (* mo d (float h)))))
  16237. ((setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16238. (format fmt (/ m (* y d (float h))))))
  16239. ;; standard (non-fractional) format, with single format string
  16240. (if (stringp org-time-clocksum-format)
  16241. (format org-time-clocksum-format (setq n (/ m h)) (- m (* h n)))
  16242. ;; separate formats components
  16243. (and (setq fmt (plist-get org-time-clocksum-format :years))
  16244. (or (> (setq n (/ (truncate m) (* y d h))) 0)
  16245. (plist-get org-time-clocksum-format :require-years))
  16246. (setq clocksum (concat clocksum (format fmt n))
  16247. m (- m (* n y d h))))
  16248. (and (setq fmt (plist-get org-time-clocksum-format :months))
  16249. (or (> (setq n (/ (truncate m) (* mo d h))) 0)
  16250. (plist-get org-time-clocksum-format :require-months))
  16251. (setq clocksum (concat clocksum (format fmt n))
  16252. m (- m (* n mo d h))))
  16253. (and (setq fmt (plist-get org-time-clocksum-format :weeks))
  16254. (or (> (setq n (/ (truncate m) (* w d h))) 0)
  16255. (plist-get org-time-clocksum-format :require-weeks))
  16256. (setq clocksum (concat clocksum (format fmt n))
  16257. m (- m (* n w d h))))
  16258. (and (setq fmt (plist-get org-time-clocksum-format :days))
  16259. (or (> (setq n (/ (truncate m) (* d h))) 0)
  16260. (plist-get org-time-clocksum-format :require-days))
  16261. (setq clocksum (concat clocksum (format fmt n))
  16262. m (- m (* n d h))))
  16263. (and (setq fmt (plist-get org-time-clocksum-format :hours))
  16264. (or (> (setq n (/ (truncate m) h)) 0)
  16265. (plist-get org-time-clocksum-format :require-hours))
  16266. (setq clocksum (concat clocksum (format fmt n))
  16267. m (- m (* n h))))
  16268. (and (setq fmt (plist-get org-time-clocksum-format :minutes))
  16269. (or (> m 0) (plist-get org-time-clocksum-format :require-minutes))
  16270. (setq clocksum (concat clocksum (format fmt m))))
  16271. ;; return formatted time duration
  16272. clocksum))))
  16273. (defalias 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string)
  16274. (make-obsolete 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string
  16275. "Org mode version 8.0")
  16276. (defun org-hours-to-clocksum-string (n)
  16277. (org-minutes-to-clocksum-string (* n 60)))
  16278. (defun org-hh:mm-string-to-minutes (s)
  16279. "Convert a string H:MM to a number of minutes.
  16280. If the string is just a number, interpret it as minutes.
  16281. In fact, the first hh:mm or number in the string will be taken,
  16282. there can be extra stuff in the string.
  16283. If no number is found, the return value is 0."
  16284. (cond
  16285. ((integerp s) s)
  16286. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  16287. (+ (* (string-to-number (match-string 1 s)) 60)
  16288. (string-to-number (match-string 2 s))))
  16289. ((string-match "\\([0-9]+\\)" s)
  16290. (string-to-number (match-string 1 s)))
  16291. (t 0)))
  16292. (defcustom org-image-actual-width t
  16293. "Should we use the actual width of images when inlining them?
  16294. When set to `t', always use the image width.
  16295. When set to a number, use imagemagick (when available) to set
  16296. the image's width to this value.
  16297. When set to a number in a list, try to get the width from any
  16298. #+ATTR.* keyword if it matches a width specification like
  16299. #+ATTR_HTML: :width 300px
  16300. and fall back on that number if none is found.
  16301. When set to nil, try to get the width from an #+ATTR.* keyword
  16302. and fall back on the original width if none is found.
  16303. This requires Emacs >= 24.1, build with imagemagick support."
  16304. :group 'org-appearance
  16305. :version "24.4"
  16306. :package-version '(Org . "8.0")
  16307. :type '(choice
  16308. (const :tag "Use the image width" t)
  16309. (integer :tag "Use a number of pixels")
  16310. (list :tag "Use #+ATTR* or a number of pixels" (integer))
  16311. (const :tag "Use #+ATTR* or don't resize" nil)))
  16312. (defcustom org-agenda-inhibit-startup nil
  16313. "Inhibit startup when preparing agenda buffers.
  16314. When this variable is `t', the initialization of the Org agenda
  16315. buffers is inhibited: e.g. the visibility state is not set, the
  16316. tables are not re-aligned, etc."
  16317. :type 'boolean
  16318. :version "24.3"
  16319. :group 'org-agenda)
  16320. (defcustom org-agenda-ignore-drawer-properties nil
  16321. "Avoid updating text properties when building the agenda.
  16322. Properties are used to prepare buffers for effort estimates, appointments,
  16323. and subtree-local categories.
  16324. If you don't use these in the agenda, you can add them to this list and
  16325. agenda building will be a bit faster.
  16326. The value is a list, with zero or more of the symbols `effort', `appt',
  16327. or `category'."
  16328. :type '(set :greedy t
  16329. (const effort)
  16330. (const appt)
  16331. (const category))
  16332. :version "24.3"
  16333. :group 'org-agenda)
  16334. (defun org-duration-string-to-minutes (s &optional output-to-string)
  16335. "Convert a duration string S to minutes.
  16336. A bare number is interpreted as minutes, modifiers can be set by
  16337. customizing `org-effort-durations' (which see).
  16338. Entries containing a colon are interpreted as H:MM by
  16339. `org-hh:mm-string-to-minutes'."
  16340. (let ((result 0)
  16341. (re (concat "\\([0-9.]+\\) *\\("
  16342. (regexp-opt (mapcar 'car org-effort-durations))
  16343. "\\)")))
  16344. (while (string-match re s)
  16345. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  16346. (string-to-number (match-string 1 s))))
  16347. (setq s (replace-match "" nil t s)))
  16348. (setq result (floor result))
  16349. (incf result (org-hh:mm-string-to-minutes s))
  16350. (if output-to-string (number-to-string result) result)))
  16351. ;;;; Files
  16352. (defun org-save-all-org-buffers ()
  16353. "Save all Org-mode buffers without user confirmation."
  16354. (interactive)
  16355. (message "Saving all Org-mode buffers...")
  16356. (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
  16357. (when (featurep 'org-id) (org-id-locations-save))
  16358. (message "Saving all Org-mode buffers... done"))
  16359. (defun org-revert-all-org-buffers ()
  16360. "Revert all Org-mode buffers.
  16361. Prompt for confirmation when there are unsaved changes.
  16362. Be sure you know what you are doing before letting this function
  16363. overwrite your changes.
  16364. This function is useful in a setup where one tracks org files
  16365. with a version control system, to revert on one machine after pulling
  16366. changes from another. I believe the procedure must be like this:
  16367. 1. M-x org-save-all-org-buffers
  16368. 2. Pull changes from the other machine, resolve conflicts
  16369. 3. M-x org-revert-all-org-buffers"
  16370. (interactive)
  16371. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  16372. (user-error "Abort"))
  16373. (save-excursion
  16374. (save-window-excursion
  16375. (mapc
  16376. (lambda (b)
  16377. (when (and (with-current-buffer b (derived-mode-p 'org-mode))
  16378. (with-current-buffer b buffer-file-name))
  16379. (org-pop-to-buffer-same-window b)
  16380. (revert-buffer t 'no-confirm)))
  16381. (buffer-list))
  16382. (when (and (featurep 'org-id) org-id-track-globally)
  16383. (org-id-locations-load)))))
  16384. ;;;; Agenda files
  16385. ;;;###autoload
  16386. (defun org-switchb (&optional arg)
  16387. "Switch between Org buffers.
  16388. With one prefix argument, restrict available buffers to files.
  16389. With two prefix arguments, restrict available buffers to agenda files.
  16390. Defaults to `iswitchb' for buffer name completion.
  16391. Set `org-completion-use-ido' to make it use ido instead."
  16392. (interactive "P")
  16393. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  16394. ((equal arg '(16)) (org-buffer-list 'agenda))
  16395. (t (org-buffer-list))))
  16396. (org-completion-use-iswitchb org-completion-use-iswitchb)
  16397. (org-completion-use-ido org-completion-use-ido))
  16398. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  16399. (setq org-completion-use-iswitchb t))
  16400. (org-pop-to-buffer-same-window
  16401. (org-icompleting-read "Org buffer: "
  16402. (mapcar 'list (mapcar 'buffer-name blist))
  16403. nil t))))
  16404. ;;; Define some older names previously used for this functionality
  16405. ;;;###autoload
  16406. (defalias 'org-ido-switchb 'org-switchb)
  16407. ;;;###autoload
  16408. (defalias 'org-iswitchb 'org-switchb)
  16409. (defun org-buffer-list (&optional predicate exclude-tmp)
  16410. "Return a list of Org buffers.
  16411. PREDICATE can be `export', `files' or `agenda'.
  16412. export restrict the list to Export buffers.
  16413. files restrict the list to buffers visiting Org files.
  16414. agenda restrict the list to buffers visiting agenda files.
  16415. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  16416. (let* ((bfn nil)
  16417. (agenda-files (and (eq predicate 'agenda)
  16418. (mapcar 'file-truename (org-agenda-files t))))
  16419. (filter
  16420. (cond
  16421. ((eq predicate 'files)
  16422. (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
  16423. ((eq predicate 'export)
  16424. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  16425. ((eq predicate 'agenda)
  16426. (lambda (b)
  16427. (with-current-buffer b
  16428. (and (derived-mode-p 'org-mode)
  16429. (setq bfn (buffer-file-name b))
  16430. (member (file-truename bfn) agenda-files)))))
  16431. (t (lambda (b) (with-current-buffer b
  16432. (or (derived-mode-p 'org-mode)
  16433. (string-match "\*Org .*Export"
  16434. (buffer-name b)))))))))
  16435. (delq nil
  16436. (mapcar
  16437. (lambda(b)
  16438. (if (and (funcall filter b)
  16439. (or (not exclude-tmp)
  16440. (not (string-match "tmp" (buffer-name b)))))
  16441. b
  16442. nil))
  16443. (buffer-list)))))
  16444. (defun org-agenda-files (&optional unrestricted archives)
  16445. "Get the list of agenda files.
  16446. Optional UNRESTRICTED means return the full list even if a restriction
  16447. is currently in place.
  16448. When ARCHIVES is t, include all archive files that are really being
  16449. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  16450. `org-agenda-archives-mode' is t."
  16451. (let ((files
  16452. (cond
  16453. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  16454. ((stringp org-agenda-files) (org-read-agenda-file-list))
  16455. ((listp org-agenda-files) org-agenda-files)
  16456. (t (error "Invalid value of `org-agenda-files'")))))
  16457. (setq files (apply 'append
  16458. (mapcar (lambda (f)
  16459. (if (file-directory-p f)
  16460. (directory-files
  16461. f t org-agenda-file-regexp)
  16462. (list f)))
  16463. files)))
  16464. (when org-agenda-skip-unavailable-files
  16465. (setq files (delq nil
  16466. (mapcar (function
  16467. (lambda (file)
  16468. (and (file-readable-p file) file)))
  16469. files))))
  16470. (when (or (eq archives t)
  16471. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  16472. (setq files (org-add-archive-files files)))
  16473. files))
  16474. (defun org-agenda-file-p (&optional file)
  16475. "Return non-nil, if FILE is an agenda file.
  16476. If FILE is omitted, use the file associated with the current
  16477. buffer."
  16478. (member (or file (buffer-file-name))
  16479. (org-agenda-files t)))
  16480. (defun org-edit-agenda-file-list ()
  16481. "Edit the list of agenda files.
  16482. Depending on setup, this either uses customize to edit the variable
  16483. `org-agenda-files', or it visits the file that is holding the list. In the
  16484. latter case, the buffer is set up in a way that saving it automatically kills
  16485. the buffer and restores the previous window configuration."
  16486. (interactive)
  16487. (if (stringp org-agenda-files)
  16488. (let ((cw (current-window-configuration)))
  16489. (find-file org-agenda-files)
  16490. (org-set-local 'org-window-configuration cw)
  16491. (org-add-hook 'after-save-hook
  16492. (lambda ()
  16493. (set-window-configuration
  16494. (prog1 org-window-configuration
  16495. (kill-buffer (current-buffer))))
  16496. (org-install-agenda-files-menu)
  16497. (message "New agenda file list installed"))
  16498. nil 'local)
  16499. (message "%s" (substitute-command-keys
  16500. "Edit list and finish with \\[save-buffer]")))
  16501. (customize-variable 'org-agenda-files)))
  16502. (defun org-store-new-agenda-file-list (list)
  16503. "Set new value for the agenda file list and save it correctly."
  16504. (if (stringp org-agenda-files)
  16505. (let ((fe (org-read-agenda-file-list t)) b u)
  16506. (while (setq b (find-buffer-visiting org-agenda-files))
  16507. (kill-buffer b))
  16508. (with-temp-file org-agenda-files
  16509. (insert
  16510. (mapconcat
  16511. (lambda (f) ;; Keep un-expanded entries.
  16512. (if (setq u (assoc f fe))
  16513. (cdr u)
  16514. f))
  16515. list "\n")
  16516. "\n")))
  16517. (let ((org-mode-hook nil) (org-inhibit-startup t)
  16518. (org-insert-mode-line-in-empty-file nil))
  16519. (setq org-agenda-files list)
  16520. (customize-save-variable 'org-agenda-files org-agenda-files))))
  16521. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  16522. "Read the list of agenda files from a file.
  16523. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  16524. filenames, used by `org-store-new-agenda-file-list' to write back
  16525. un-expanded file names."
  16526. (when (file-directory-p org-agenda-files)
  16527. (error "`org-agenda-files' cannot be a single directory"))
  16528. (when (stringp org-agenda-files)
  16529. (with-temp-buffer
  16530. (insert-file-contents org-agenda-files)
  16531. (mapcar
  16532. (lambda (f)
  16533. (let ((e (expand-file-name (substitute-in-file-name f)
  16534. org-directory)))
  16535. (if pair-with-expansion
  16536. (cons e f)
  16537. e)))
  16538. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  16539. ;;;###autoload
  16540. (defun org-cycle-agenda-files ()
  16541. "Cycle through the files in `org-agenda-files'.
  16542. If the current buffer visits an agenda file, find the next one in the list.
  16543. If the current buffer does not, find the first agenda file."
  16544. (interactive)
  16545. (let* ((fs (org-agenda-files t))
  16546. (files (append fs (list (car fs))))
  16547. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  16548. file)
  16549. (unless files (user-error "No agenda files"))
  16550. (catch 'exit
  16551. (while (setq file (pop files))
  16552. (if (equal (file-truename file) tcf)
  16553. (when (car files)
  16554. (find-file (car files))
  16555. (throw 'exit t))))
  16556. (find-file (car fs)))
  16557. (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  16558. (defun org-agenda-file-to-front (&optional to-end)
  16559. "Move/add the current file to the top of the agenda file list.
  16560. If the file is not present in the list, it is added to the front. If it is
  16561. present, it is moved there. With optional argument TO-END, add/move to the
  16562. end of the list."
  16563. (interactive "P")
  16564. (let ((org-agenda-skip-unavailable-files nil)
  16565. (file-alist (mapcar (lambda (x)
  16566. (cons (file-truename x) x))
  16567. (org-agenda-files t)))
  16568. (ctf (file-truename
  16569. (or buffer-file-name
  16570. (user-error "Please save the current buffer to a file"))))
  16571. x had)
  16572. (setq x (assoc ctf file-alist) had x)
  16573. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  16574. (if to-end
  16575. (setq file-alist (append (delq x file-alist) (list x)))
  16576. (setq file-alist (cons x (delq x file-alist))))
  16577. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  16578. (org-install-agenda-files-menu)
  16579. (message "File %s to %s of agenda file list"
  16580. (if had "moved" "added") (if to-end "end" "front"))))
  16581. (defun org-remove-file (&optional file)
  16582. "Remove current file from the list of files in variable `org-agenda-files'.
  16583. These are the files which are being checked for agenda entries.
  16584. Optional argument FILE means use this file instead of the current."
  16585. (interactive)
  16586. (let* ((org-agenda-skip-unavailable-files nil)
  16587. (file (or file buffer-file-name
  16588. (user-error "Current buffer does not visit a file")))
  16589. (true-file (file-truename file))
  16590. (afile (abbreviate-file-name file))
  16591. (files (delq nil (mapcar
  16592. (lambda (x)
  16593. (if (equal true-file
  16594. (file-truename x))
  16595. nil x))
  16596. (org-agenda-files t)))))
  16597. (if (not (= (length files) (length (org-agenda-files t))))
  16598. (progn
  16599. (org-store-new-agenda-file-list files)
  16600. (org-install-agenda-files-menu)
  16601. (message "Removed from Org Agenda list: %s" afile))
  16602. (message "File was not in list: %s (not removed)" afile))))
  16603. (defun org-file-menu-entry (file)
  16604. (vector file (list 'find-file file) t))
  16605. (defun org-check-agenda-file (file)
  16606. "Make sure FILE exists. If not, ask user what to do."
  16607. (when (not (file-exists-p file))
  16608. (message "Non-existent agenda file %s. [R]emove from list or [A]bort?"
  16609. (abbreviate-file-name file))
  16610. (let ((r (downcase (read-char-exclusive))))
  16611. (cond
  16612. ((equal r ?r)
  16613. (org-remove-file file)
  16614. (throw 'nextfile t))
  16615. (t (error "Abort"))))))
  16616. (defun org-get-agenda-file-buffer (file)
  16617. "Get a buffer visiting FILE. If the buffer needs to be created, add
  16618. it to the list of buffers which might be released later."
  16619. (let ((buf (org-find-base-buffer-visiting file)))
  16620. (if buf
  16621. buf ; just return it
  16622. ;; Make a new buffer and remember it
  16623. (setq buf (find-file-noselect file))
  16624. (if buf (push buf org-agenda-new-buffers))
  16625. buf)))
  16626. (defun org-release-buffers (blist)
  16627. "Release all buffers in list, asking the user for confirmation when needed.
  16628. When a buffer is unmodified, it is just killed. When modified, it is saved
  16629. \(if the user agrees) and then killed."
  16630. (let (buf file)
  16631. (while (setq buf (pop blist))
  16632. (setq file (buffer-file-name buf))
  16633. (when (and (buffer-modified-p buf)
  16634. file
  16635. (y-or-n-p (format "Save file %s? " file)))
  16636. (with-current-buffer buf (save-buffer)))
  16637. (kill-buffer buf))))
  16638. (defun org-agenda-prepare-buffers (files)
  16639. "Create buffers for all agenda files, protect archived trees and comments."
  16640. (interactive)
  16641. (let ((pa '(:org-archived t))
  16642. (pc '(:org-comment t))
  16643. (pall '(:org-archived t :org-comment t))
  16644. (inhibit-read-only t)
  16645. (org-inhibit-startup org-agenda-inhibit-startup)
  16646. (rea (concat ":" org-archive-tag ":"))
  16647. file re pos)
  16648. (setq org-tag-alist-for-agenda nil
  16649. org-tag-groups-alist-for-agenda nil)
  16650. (save-excursion
  16651. (save-restriction
  16652. (while (setq file (pop files))
  16653. (catch 'nextfile
  16654. (if (bufferp file)
  16655. (set-buffer file)
  16656. (org-check-agenda-file file)
  16657. (set-buffer (org-get-agenda-file-buffer file)))
  16658. (widen)
  16659. (org-set-regexps-and-options-for-tags)
  16660. (setq pos (point))
  16661. (goto-char (point-min))
  16662. (let ((case-fold-search t))
  16663. (when (search-forward "#+setupfile" nil t)
  16664. ;; Don't set all regexps and options systematically as
  16665. ;; this is only run for setting agenda tags from setup
  16666. ;; file
  16667. (org-set-regexps-and-options)))
  16668. (or (memq 'category org-agenda-ignore-drawer-properties)
  16669. (org-refresh-category-properties))
  16670. (or (memq 'effort org-agenda-ignore-drawer-properties)
  16671. (org-refresh-properties org-effort-property 'org-effort))
  16672. (or (memq 'appt org-agenda-ignore-drawer-properties)
  16673. (org-refresh-properties "APPT_WARNTIME" 'org-appt-warntime))
  16674. (setq org-todo-keywords-for-agenda
  16675. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  16676. (setq org-done-keywords-for-agenda
  16677. (append org-done-keywords-for-agenda org-done-keywords))
  16678. (setq org-todo-keyword-alist-for-agenda
  16679. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  16680. (setq org-drawers-for-agenda
  16681. (append org-drawers-for-agenda org-drawers))
  16682. (setq org-tag-alist-for-agenda
  16683. (org-uniquify
  16684. (append org-tag-alist-for-agenda
  16685. org-tag-alist
  16686. org-tag-persistent-alist)))
  16687. (if org-group-tags
  16688. (setq org-tag-groups-alist-for-agenda
  16689. (org-uniquify-alist
  16690. (append org-tag-groups-alist-for-agenda org-tag-groups-alist))))
  16691. (org-with-silent-modifications
  16692. (save-excursion
  16693. (remove-text-properties (point-min) (point-max) pall)
  16694. (when org-agenda-skip-archived-trees
  16695. (goto-char (point-min))
  16696. (while (re-search-forward rea nil t)
  16697. (if (org-at-heading-p t)
  16698. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  16699. (goto-char (point-min))
  16700. (setq re (format org-heading-keyword-regexp-format
  16701. org-comment-string))
  16702. (while (re-search-forward re nil t)
  16703. (add-text-properties
  16704. (match-beginning 0) (org-end-of-subtree t) pc))))
  16705. (goto-char pos)))))
  16706. (setq org-todo-keywords-for-agenda
  16707. (org-uniquify org-todo-keywords-for-agenda))
  16708. (setq org-todo-keyword-alist-for-agenda
  16709. (org-uniquify org-todo-keyword-alist-for-agenda))))
  16710. ;;;; CDLaTeX minor mode
  16711. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  16712. "Keymap for the minor `org-cdlatex-mode'.")
  16713. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  16714. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  16715. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  16716. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  16717. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  16718. (defvar org-cdlatex-texmathp-advice-is-done nil
  16719. "Flag remembering if we have applied the advice to texmathp already.")
  16720. (define-minor-mode org-cdlatex-mode
  16721. "Toggle the minor `org-cdlatex-mode'.
  16722. This mode supports entering LaTeX environment and math in LaTeX fragments
  16723. in Org-mode.
  16724. \\{org-cdlatex-mode-map}"
  16725. nil " OCDL" nil
  16726. (when org-cdlatex-mode
  16727. (require 'cdlatex)
  16728. (run-hooks 'cdlatex-mode-hook)
  16729. (cdlatex-compute-tables))
  16730. (unless org-cdlatex-texmathp-advice-is-done
  16731. (setq org-cdlatex-texmathp-advice-is-done t)
  16732. (defadvice texmathp (around org-math-always-on activate)
  16733. "Always return t in org-mode buffers.
  16734. This is because we want to insert math symbols without dollars even outside
  16735. the LaTeX math segments. If Orgmode thinks that point is actually inside
  16736. an embedded LaTeX fragment, let texmathp do its job.
  16737. \\[org-cdlatex-mode-map]"
  16738. (interactive)
  16739. (let (p)
  16740. (cond
  16741. ((not (derived-mode-p 'org-mode)) ad-do-it)
  16742. ((eq this-command 'cdlatex-math-symbol)
  16743. (setq ad-return-value t
  16744. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  16745. (t
  16746. (let ((p (org-inside-LaTeX-fragment-p)))
  16747. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  16748. (setq ad-return-value t
  16749. texmathp-why '("Org-mode embedded math" . 0))
  16750. (if p ad-do-it)))))))))
  16751. (defun turn-on-org-cdlatex ()
  16752. "Unconditionally turn on `org-cdlatex-mode'."
  16753. (org-cdlatex-mode 1))
  16754. (defun org-try-cdlatex-tab ()
  16755. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  16756. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  16757. - inside a LaTeX fragment, or
  16758. - after the first word in a line, where an abbreviation expansion could
  16759. insert a LaTeX environment."
  16760. (when org-cdlatex-mode
  16761. (cond
  16762. ;; Before any word on the line: No expansion possible.
  16763. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  16764. ;; Just after first word on the line: Expand it. Make sure it
  16765. ;; cannot happen on headlines, though.
  16766. ((save-excursion
  16767. (skip-chars-backward "a-zA-Z0-9*")
  16768. (skip-chars-backward " \t")
  16769. (and (bolp) (not (org-at-heading-p))))
  16770. (cdlatex-tab) t)
  16771. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  16772. (defun org-cdlatex-underscore-caret (&optional arg)
  16773. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  16774. Revert to the normal definition outside of these fragments."
  16775. (interactive "P")
  16776. (if (org-inside-LaTeX-fragment-p)
  16777. (call-interactively 'cdlatex-sub-superscript)
  16778. (let (org-cdlatex-mode)
  16779. (call-interactively (key-binding (vector last-input-event))))))
  16780. (defun org-cdlatex-math-modify (&optional arg)
  16781. "Execute `cdlatex-math-modify' in LaTeX fragments.
  16782. Revert to the normal definition outside of these fragments."
  16783. (interactive "P")
  16784. (if (org-inside-LaTeX-fragment-p)
  16785. (call-interactively 'cdlatex-math-modify)
  16786. (let (org-cdlatex-mode)
  16787. (call-interactively (key-binding (vector last-input-event))))))
  16788. ;;;; LaTeX fragments
  16789. (defvar org-latex-regexps
  16790. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  16791. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  16792. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  16793. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  16794. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  16795. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  16796. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  16797. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  16798. "Regular expressions for matching embedded LaTeX.")
  16799. (defun org-inside-LaTeX-fragment-p ()
  16800. "Test if point is inside a LaTeX fragment.
  16801. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  16802. sequence appearing also before point.
  16803. Even though the matchers for math are configurable, this function assumes
  16804. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  16805. delimiters are skipped when they have been removed by customization.
  16806. The return value is nil, or a cons cell with the delimiter and the
  16807. position of this delimiter.
  16808. This function does a reasonably good job, but can locally be fooled by
  16809. for example currency specifications. For example it will assume being in
  16810. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  16811. fragments that are properly closed, but during editing, we have to live
  16812. with the uncertainty caused by missing closing delimiters. This function
  16813. looks only before point, not after."
  16814. (catch 'exit
  16815. (let ((pos (point))
  16816. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  16817. (lim (progn
  16818. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  16819. (point)))
  16820. dd-on str (start 0) m re)
  16821. (goto-char pos)
  16822. (when dodollar
  16823. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  16824. re (nth 1 (assoc "$" org-latex-regexps)))
  16825. (while (string-match re str start)
  16826. (cond
  16827. ((= (match-end 0) (length str))
  16828. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  16829. ((= (match-end 0) (- (length str) 5))
  16830. (throw 'exit nil))
  16831. (t (setq start (match-end 0))))))
  16832. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  16833. (goto-char pos)
  16834. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  16835. (and (match-beginning 2) (throw 'exit nil))
  16836. ;; count $$
  16837. (while (re-search-backward "\\$\\$" lim t)
  16838. (setq dd-on (not dd-on)))
  16839. (goto-char pos)
  16840. (if dd-on (cons "$$" m))))))
  16841. (defun org-inside-latex-macro-p ()
  16842. "Is point inside a LaTeX macro or its arguments?"
  16843. (save-match-data
  16844. (org-in-regexp
  16845. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  16846. (defvar org-latex-fragment-image-overlays nil
  16847. "List of overlays carrying the images of latex fragments.")
  16848. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  16849. (defun org-remove-latex-fragment-image-overlays ()
  16850. "Remove all overlays with LaTeX fragment images in current buffer."
  16851. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  16852. (setq org-latex-fragment-image-overlays nil))
  16853. (defun org-preview-latex-fragment (&optional subtree)
  16854. "Preview the LaTeX fragment at point, or all locally or globally.
  16855. If the cursor is in a LaTeX fragment, create the image and overlay
  16856. it over the source code. If there is no fragment at point, display
  16857. all fragments in the current text, from one headline to the next. With
  16858. prefix SUBTREE, display all fragments in the current subtree. With a
  16859. double prefix arg \\[universal-argument] \\[universal-argument], or when \
  16860. the cursor is before the first headline,
  16861. display all fragments in the buffer.
  16862. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  16863. (interactive "P")
  16864. (unless buffer-file-name
  16865. (user-error "Can't preview LaTeX fragment in a non-file buffer"))
  16866. (when (display-graphic-p)
  16867. (org-remove-latex-fragment-image-overlays)
  16868. (save-excursion
  16869. (save-restriction
  16870. (let (beg end at msg)
  16871. (cond
  16872. ((or (equal subtree '(16))
  16873. (not (save-excursion
  16874. (re-search-backward org-outline-regexp-bol nil t))))
  16875. (setq beg (point-min) end (point-max)
  16876. msg "Creating images for buffer...%s"))
  16877. ((equal subtree '(4))
  16878. (org-back-to-heading)
  16879. (setq beg (point) end (org-end-of-subtree t)
  16880. msg "Creating images for subtree...%s"))
  16881. (t
  16882. (if (setq at (org-inside-LaTeX-fragment-p))
  16883. (goto-char (max (point-min) (- (cdr at) 2)))
  16884. (org-back-to-heading))
  16885. (setq beg (point) end (progn (outline-next-heading) (point))
  16886. msg (if at "Creating image...%s"
  16887. "Creating images for entry...%s"))))
  16888. (message msg "")
  16889. (narrow-to-region beg end)
  16890. (goto-char beg)
  16891. (org-format-latex
  16892. (concat org-latex-preview-ltxpng-directory (file-name-sans-extension
  16893. (file-name-nondirectory
  16894. buffer-file-name)))
  16895. default-directory 'overlays msg at 'forbuffer
  16896. org-latex-create-formula-image-program)
  16897. (message msg "done. Use `C-c C-c' to remove images."))))))
  16898. (defun org-format-latex (prefix &optional dir overlays msg at
  16899. forbuffer processing-type)
  16900. "Replace LaTeX fragments with links to an image, and produce images.
  16901. Some of the options can be changed using the variable
  16902. `org-format-latex-options'."
  16903. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  16904. (let* ((prefixnodir (file-name-nondirectory prefix))
  16905. (absprefix (expand-file-name prefix dir))
  16906. (todir (file-name-directory absprefix))
  16907. (opt org-format-latex-options)
  16908. (optnew org-format-latex-options)
  16909. (matchers (plist-get opt :matchers))
  16910. (re-list org-latex-regexps)
  16911. (cnt 0) txt hash link beg end re e checkdir
  16912. string
  16913. m n block-type block linkfile movefile ov)
  16914. ;; Check the different regular expressions
  16915. (while (setq e (pop re-list))
  16916. (setq m (car e) re (nth 1 e) n (nth 2 e) block-type (nth 3 e)
  16917. block (if block-type "\n\n" ""))
  16918. (when (member m matchers)
  16919. (goto-char (point-min))
  16920. (while (re-search-forward re nil t)
  16921. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  16922. (or (not overlays)
  16923. (not (eq (get-char-property (match-beginning n)
  16924. 'org-overlay-type)
  16925. 'org-latex-overlay))))
  16926. (cond
  16927. ((eq processing-type 'verbatim))
  16928. ((eq processing-type 'mathjax)
  16929. ;; Prepare for MathJax processing.
  16930. (setq string (match-string n))
  16931. (when (member m '("$" "$1"))
  16932. (save-excursion
  16933. (delete-region (match-beginning n) (match-end n))
  16934. (goto-char (match-beginning n))
  16935. (insert (concat "\\(" (substring string 1 -1) "\\)")))))
  16936. ((or (eq processing-type 'dvipng)
  16937. (eq processing-type 'imagemagick))
  16938. ;; Process to an image.
  16939. (setq txt (match-string n)
  16940. beg (match-beginning n) end (match-end n)
  16941. cnt (1+ cnt))
  16942. (let ((face (face-at-point))
  16943. (fg (plist-get opt :foreground))
  16944. (bg (plist-get opt :background))
  16945. ;; Ensure full list is printed.
  16946. print-length print-level)
  16947. (when forbuffer
  16948. ;; Get the colors from the face at point.
  16949. (goto-char beg)
  16950. (when (eq fg 'auto)
  16951. (setq fg (face-attribute face :foreground nil 'default)))
  16952. (when (eq bg 'auto)
  16953. (setq bg (face-attribute face :background nil 'default)))
  16954. (setq optnew (copy-sequence opt))
  16955. (plist-put optnew :foreground fg)
  16956. (plist-put optnew :background bg))
  16957. (setq hash (sha1 (prin1-to-string
  16958. (list org-format-latex-header
  16959. org-latex-default-packages-alist
  16960. org-latex-packages-alist
  16961. org-format-latex-options
  16962. forbuffer txt fg bg)))
  16963. linkfile (format "%s_%s.png" prefix hash)
  16964. movefile (format "%s_%s.png" absprefix hash)))
  16965. (setq link (concat block "[[file:" linkfile "]]" block))
  16966. (if msg (message msg cnt))
  16967. (goto-char beg)
  16968. (unless checkdir ; Ensure the directory exists.
  16969. (setq checkdir t)
  16970. (or (file-directory-p todir) (make-directory todir t)))
  16971. (unless (file-exists-p movefile)
  16972. (org-create-formula-image
  16973. txt movefile optnew forbuffer processing-type))
  16974. (if overlays
  16975. (progn
  16976. (mapc (lambda (o)
  16977. (if (eq (overlay-get o 'org-overlay-type)
  16978. 'org-latex-overlay)
  16979. (delete-overlay o)))
  16980. (overlays-in beg end))
  16981. (setq ov (make-overlay beg end))
  16982. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  16983. (if (featurep 'xemacs)
  16984. (progn
  16985. (overlay-put ov 'invisible t)
  16986. (overlay-put
  16987. ov 'end-glyph
  16988. (make-glyph (vector 'png :file movefile))))
  16989. (overlay-put
  16990. ov 'display
  16991. (list 'image :type 'png :file movefile :ascent 'center)))
  16992. (push ov org-latex-fragment-image-overlays)
  16993. (goto-char end))
  16994. (delete-region beg end)
  16995. (insert (org-add-props link
  16996. (list 'org-latex-src
  16997. (replace-regexp-in-string
  16998. "\"" "" txt)
  16999. 'org-latex-src-embed-type
  17000. (if block-type 'paragraph 'character))))))
  17001. ((eq processing-type 'mathml)
  17002. ;; Process to MathML
  17003. (unless (save-match-data (org-format-latex-mathml-available-p))
  17004. (user-error "LaTeX to MathML converter not configured"))
  17005. (setq txt (match-string n)
  17006. beg (match-beginning n) end (match-end n)
  17007. cnt (1+ cnt))
  17008. (if msg (message msg cnt))
  17009. (goto-char beg)
  17010. (delete-region beg end)
  17011. (insert (org-format-latex-as-mathml
  17012. txt block-type prefix dir)))
  17013. (t
  17014. (error "Unknown conversion type %s for LaTeX fragments"
  17015. processing-type)))))))))
  17016. (defun org-create-math-formula (latex-frag &optional mathml-file)
  17017. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  17018. Use `org-latex-to-mathml-convert-command'. If the conversion is
  17019. sucessful, return the portion between \"<math...> </math>\"
  17020. elements otherwise return nil. When MATHML-FILE is specified,
  17021. write the results in to that file. When invoked as an
  17022. interactive command, prompt for LATEX-FRAG, with initial value
  17023. set to the current active region and echo the results for user
  17024. inspection."
  17025. (interactive (list (let ((frag (when (org-region-active-p)
  17026. (buffer-substring-no-properties
  17027. (region-beginning) (region-end)))))
  17028. (read-string "LaTeX Fragment: " frag nil frag))))
  17029. (unless latex-frag (error "Invalid LaTeX fragment"))
  17030. (let* ((tmp-in-file (file-relative-name
  17031. (make-temp-name (expand-file-name "ltxmathml-in"))))
  17032. (ignore (write-region latex-frag nil tmp-in-file))
  17033. (tmp-out-file (file-relative-name
  17034. (make-temp-name (expand-file-name "ltxmathml-out"))))
  17035. (cmd (format-spec
  17036. org-latex-to-mathml-convert-command
  17037. `((?j . ,(shell-quote-argument
  17038. (expand-file-name org-latex-to-mathml-jar-file)))
  17039. (?I . ,(shell-quote-argument tmp-in-file))
  17040. (?o . ,(shell-quote-argument tmp-out-file)))))
  17041. mathml shell-command-output)
  17042. (when (org-called-interactively-p 'any)
  17043. (unless (org-format-latex-mathml-available-p)
  17044. (user-error "LaTeX to MathML converter not configured")))
  17045. (message "Running %s" cmd)
  17046. (setq shell-command-output (shell-command-to-string cmd))
  17047. (setq mathml
  17048. (when (file-readable-p tmp-out-file)
  17049. (with-current-buffer (find-file-noselect tmp-out-file t)
  17050. (goto-char (point-min))
  17051. (when (re-search-forward
  17052. (concat
  17053. (regexp-quote
  17054. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
  17055. "\\(.\\|\n\\)*"
  17056. (regexp-quote "</math>")) nil t)
  17057. (prog1 (match-string 0) (kill-buffer))))))
  17058. (cond
  17059. (mathml
  17060. (setq mathml
  17061. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  17062. (when mathml-file
  17063. (write-region mathml nil mathml-file))
  17064. (when (org-called-interactively-p 'any)
  17065. (message mathml)))
  17066. ((message "LaTeX to MathML conversion failed")
  17067. (message shell-command-output)))
  17068. (delete-file tmp-in-file)
  17069. (when (file-exists-p tmp-out-file)
  17070. (delete-file tmp-out-file))
  17071. mathml))
  17072. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  17073. prefix &optional dir)
  17074. "Use `org-create-math-formula' but check local cache first."
  17075. (let* ((absprefix (expand-file-name prefix dir))
  17076. (print-length nil) (print-level nil)
  17077. (formula-id (concat
  17078. "formula-"
  17079. (sha1
  17080. (prin1-to-string
  17081. (list latex-frag
  17082. org-latex-to-mathml-convert-command)))))
  17083. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  17084. (formula-cache-dir (file-name-directory formula-cache)))
  17085. (unless (file-directory-p formula-cache-dir)
  17086. (make-directory formula-cache-dir t))
  17087. (unless (file-exists-p formula-cache)
  17088. (org-create-math-formula latex-frag formula-cache))
  17089. (if (file-exists-p formula-cache)
  17090. ;; Successful conversion. Return the link to MathML file.
  17091. (org-add-props
  17092. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  17093. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  17094. 'org-latex-src-embed-type (if latex-frag-type
  17095. 'paragraph 'character)))
  17096. ;; Failed conversion. Return the LaTeX fragment verbatim
  17097. latex-frag)))
  17098. (defun org-create-formula-image (string tofile options buffer &optional type)
  17099. "Create an image from LaTeX source using dvipng or convert.
  17100. This function calls either `org-create-formula-image-with-dvipng'
  17101. or `org-create-formula-image-with-imagemagick' depending on the
  17102. value of `org-latex-create-formula-image-program' or on the value
  17103. of the optional TYPE variable.
  17104. Note: ultimately these two function should be combined as they
  17105. share a good deal of logic."
  17106. (org-check-external-command
  17107. "latex" "needed to convert LaTeX fragments to images")
  17108. (funcall
  17109. (case (or type org-latex-create-formula-image-program)
  17110. ('dvipng
  17111. (org-check-external-command
  17112. "dvipng" "needed to convert LaTeX fragments to images")
  17113. #'org-create-formula-image-with-dvipng)
  17114. ('imagemagick
  17115. (org-check-external-command
  17116. "convert" "you need to install imagemagick")
  17117. #'org-create-formula-image-with-imagemagick)
  17118. (t (error
  17119. "Invalid value of `org-latex-create-formula-image-program'")))
  17120. string tofile options buffer))
  17121. (declare-function org-export-get-backend "ox" (name))
  17122. (declare-function org-export--get-global-options "ox" (&optional backend))
  17123. (declare-function org-export--get-inbuffer-options "ox" (&optional backend))
  17124. (declare-function org-latex-guess-inputenc "ox-latex" (header))
  17125. (declare-function org-latex-guess-babel-language "ox-latex" (header info))
  17126. (defun org-create-formula--latex-header ()
  17127. "Return LaTeX header appropriate for previewing a LaTeX snippet."
  17128. (let ((info (org-combine-plists (org-export--get-global-options
  17129. (org-export-get-backend 'latex))
  17130. (org-export--get-inbuffer-options
  17131. (org-export-get-backend 'latex)))))
  17132. (org-latex-guess-babel-language
  17133. (org-latex-guess-inputenc
  17134. (org-splice-latex-header
  17135. org-format-latex-header
  17136. org-latex-default-packages-alist
  17137. org-latex-packages-alist t
  17138. (plist-get info :latex-header)))
  17139. info)))
  17140. ;; This function borrows from Ganesh Swami's latex2png.el
  17141. (defun org-create-formula-image-with-dvipng (string tofile options buffer)
  17142. "This calls dvipng."
  17143. (require 'ox-latex)
  17144. (let* ((tmpdir (if (featurep 'xemacs)
  17145. (temp-directory)
  17146. temporary-file-directory))
  17147. (texfilebase (make-temp-name
  17148. (expand-file-name "orgtex" tmpdir)))
  17149. (texfile (concat texfilebase ".tex"))
  17150. (dvifile (concat texfilebase ".dvi"))
  17151. (pngfile (concat texfilebase ".png"))
  17152. (fnh (if (featurep 'xemacs)
  17153. (font-height (face-font 'default))
  17154. (face-attribute 'default :height nil)))
  17155. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  17156. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  17157. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  17158. "Black"))
  17159. (bg (or (plist-get options (if buffer :background :html-background))
  17160. "Transparent")))
  17161. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground))
  17162. (unless (string= fg "Transparent") (setq fg (org-dvipng-color-format fg))))
  17163. (if (eq bg 'default) (setq bg (org-dvipng-color :background))
  17164. (unless (string= bg "Transparent") (setq bg (org-dvipng-color-format bg))))
  17165. (let ((latex-header (org-create-formula--latex-header)))
  17166. (with-temp-file texfile
  17167. (insert latex-header)
  17168. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")))
  17169. (let ((dir default-directory))
  17170. (condition-case nil
  17171. (progn
  17172. (cd tmpdir)
  17173. (call-process "latex" nil nil nil texfile))
  17174. (error nil))
  17175. (cd dir))
  17176. (if (not (file-exists-p dvifile))
  17177. (progn (message "Failed to create dvi file from %s" texfile) nil)
  17178. (condition-case nil
  17179. (if (featurep 'xemacs)
  17180. (call-process "dvipng" nil nil nil
  17181. "-fg" fg "-bg" bg
  17182. "-T" "tight"
  17183. "-o" pngfile
  17184. dvifile)
  17185. (call-process "dvipng" nil nil nil
  17186. "-fg" fg "-bg" bg
  17187. "-D" dpi
  17188. ;;"-x" scale "-y" scale
  17189. "-T" "tight"
  17190. "-o" pngfile
  17191. dvifile))
  17192. (error nil))
  17193. (if (not (file-exists-p pngfile))
  17194. (if org-format-latex-signal-error
  17195. (error "Failed to create png file from %s" texfile)
  17196. (message "Failed to create png file from %s" texfile)
  17197. nil)
  17198. ;; Use the requested file name and clean up
  17199. (copy-file pngfile tofile 'replace)
  17200. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png" ".out") do
  17201. (if (file-exists-p (concat texfilebase e))
  17202. (delete-file (concat texfilebase e))))
  17203. pngfile))))
  17204. (declare-function org-latex-compile "ox-latex" (texfile &optional snippet))
  17205. (defun org-create-formula-image-with-imagemagick (string tofile options buffer)
  17206. "This calls convert, which is included into imagemagick."
  17207. (require 'ox-latex)
  17208. (let* ((tmpdir (if (featurep 'xemacs)
  17209. (temp-directory)
  17210. temporary-file-directory))
  17211. (texfilebase (make-temp-name
  17212. (expand-file-name "orgtex" tmpdir)))
  17213. (texfile (concat texfilebase ".tex"))
  17214. (pdffile (concat texfilebase ".pdf"))
  17215. (pngfile (concat texfilebase ".png"))
  17216. (fnh (if (featurep 'xemacs)
  17217. (font-height (face-font 'default))
  17218. (face-attribute 'default :height nil)))
  17219. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  17220. (dpi (number-to-string (* scale (floor (if buffer fnh 120.)))))
  17221. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  17222. "black"))
  17223. (bg (or (plist-get options (if buffer :background :html-background))
  17224. "white")))
  17225. (if (eq fg 'default) (setq fg (org-latex-color :foreground))
  17226. (setq fg (org-latex-color-format fg)))
  17227. (if (eq bg 'default) (setq bg (org-latex-color :background))
  17228. (setq bg (org-latex-color-format
  17229. (if (string= bg "Transparent") "white" bg))))
  17230. (let ((latex-header (org-create-formula--latex-header)))
  17231. (with-temp-file texfile
  17232. (insert latex-header)
  17233. (insert "\n\\begin{document}\n"
  17234. "\\definecolor{fg}{rgb}{" fg "}\n"
  17235. "\\definecolor{bg}{rgb}{" bg "}\n"
  17236. "\n\\pagecolor{bg}\n"
  17237. "\n{\\color{fg}\n"
  17238. string
  17239. "\n}\n"
  17240. "\n\\end{document}\n")))
  17241. (org-latex-compile texfile t)
  17242. (if (not (file-exists-p pdffile))
  17243. (progn (message "Failed to create pdf file from %s" texfile) nil)
  17244. (condition-case nil
  17245. (if (featurep 'xemacs)
  17246. (call-process "convert" nil nil nil
  17247. "-density" "96"
  17248. "-trim"
  17249. "-antialias"
  17250. pdffile
  17251. "-quality" "100"
  17252. ;; "-sharpen" "0x1.0"
  17253. pngfile)
  17254. (call-process "convert" nil nil nil
  17255. "-density" dpi
  17256. "-trim"
  17257. "-antialias"
  17258. pdffile
  17259. "-quality" "100"
  17260. ;; "-sharpen" "0x1.0"
  17261. pngfile))
  17262. (error nil))
  17263. (if (not (file-exists-p pngfile))
  17264. (if org-format-latex-signal-error
  17265. (error "Failed to create png file from %s" texfile)
  17266. (message "Failed to create png file from %s" texfile)
  17267. nil)
  17268. ;; Use the requested file name and clean up
  17269. (copy-file pngfile tofile 'replace)
  17270. (loop for e in '(".pdf" ".tex" ".aux" ".log" ".png") do
  17271. (if (file-exists-p (concat texfilebase e))
  17272. (delete-file (concat texfilebase e))))
  17273. pngfile))))
  17274. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  17275. "Fill a LaTeX header template TPL.
  17276. In the template, the following place holders will be recognized:
  17277. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  17278. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  17279. [PACKAGES] \\usepackage statements for PKG
  17280. [NO-PACKAGES] do not include PKG
  17281. [EXTRA] the string EXTRA
  17282. [NO-EXTRA] do not include EXTRA
  17283. For backward compatibility, if both the positive and the negative place
  17284. holder is missing, the positive one (without the \"NO-\") will be
  17285. assumed to be present at the end of the template.
  17286. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  17287. EXTRA is a string.
  17288. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  17289. (let (rpl (end ""))
  17290. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  17291. (setq rpl (if (or (match-end 1) (not def-pkg))
  17292. "" (org-latex-packages-to-string def-pkg snippets-p t))
  17293. tpl (replace-match rpl t t tpl))
  17294. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  17295. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  17296. (setq rpl (if (or (match-end 1) (not pkg))
  17297. "" (org-latex-packages-to-string pkg snippets-p t))
  17298. tpl (replace-match rpl t t tpl))
  17299. (if pkg (setq end
  17300. (concat end "\n"
  17301. (org-latex-packages-to-string pkg snippets-p)))))
  17302. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  17303. (setq rpl (if (or (match-end 1) (not extra))
  17304. "" (concat extra "\n"))
  17305. tpl (replace-match rpl t t tpl))
  17306. (if (and extra (string-match "\\S-" extra))
  17307. (setq end (concat end "\n" extra))))
  17308. (if (string-match "\\S-" end)
  17309. (concat tpl "\n" end)
  17310. tpl)))
  17311. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  17312. "Turn an alist of packages into a string with the \\usepackage macros."
  17313. (setq pkg (mapconcat (lambda(p)
  17314. (cond
  17315. ((stringp p) p)
  17316. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  17317. (format "%% Package %s omitted" (cadr p)))
  17318. ((equal "" (car p))
  17319. (format "\\usepackage{%s}" (cadr p)))
  17320. (t
  17321. (format "\\usepackage[%s]{%s}"
  17322. (car p) (cadr p)))))
  17323. pkg
  17324. "\n"))
  17325. (if newline (concat pkg "\n") pkg))
  17326. (defun org-dvipng-color (attr)
  17327. "Return a RGB color specification for dvipng."
  17328. (apply 'format "rgb %s %s %s"
  17329. (mapcar 'org-normalize-color
  17330. (if (featurep 'xemacs)
  17331. (color-rgb-components
  17332. (face-property 'default
  17333. (cond ((eq attr :foreground) 'foreground)
  17334. ((eq attr :background) 'background))))
  17335. (color-values (face-attribute 'default attr nil))))))
  17336. (defun org-dvipng-color-format (color-name)
  17337. "Convert COLOR-NAME to a RGB color value for dvipng."
  17338. (apply 'format "rgb %s %s %s"
  17339. (mapcar 'org-normalize-color
  17340. (color-values color-name))))
  17341. (defun org-latex-color (attr)
  17342. "Return a RGB color for the LaTeX color package."
  17343. (apply 'format "%s,%s,%s"
  17344. (mapcar 'org-normalize-color
  17345. (if (featurep 'xemacs)
  17346. (color-rgb-components
  17347. (face-property 'default
  17348. (cond ((eq attr :foreground) 'foreground)
  17349. ((eq attr :background) 'background))))
  17350. (color-values (face-attribute 'default attr nil))))))
  17351. (defun org-latex-color-format (color-name)
  17352. "Convert COLOR-NAME to a RGB color value."
  17353. (apply 'format "%s,%s,%s"
  17354. (mapcar 'org-normalize-color
  17355. (color-values color-name))))
  17356. (defun org-normalize-color (value)
  17357. "Return string to be used as color value for an RGB component."
  17358. (format "%g" (/ value 65535.0)))
  17359. ;; Image display
  17360. (defvar org-inline-image-overlays nil)
  17361. (make-variable-buffer-local 'org-inline-image-overlays)
  17362. (defun org-toggle-inline-images (&optional include-linked)
  17363. "Toggle the display of inline images.
  17364. INCLUDE-LINKED is passed to `org-display-inline-images'."
  17365. (interactive "P")
  17366. (if org-inline-image-overlays
  17367. (progn
  17368. (org-remove-inline-images)
  17369. (message "Inline image display turned off"))
  17370. (org-display-inline-images include-linked)
  17371. (if (and (org-called-interactively-p)
  17372. org-inline-image-overlays)
  17373. (message "%d images displayed inline"
  17374. (length org-inline-image-overlays))
  17375. (message "No images to display inline"))))
  17376. (defun org-redisplay-inline-images ()
  17377. "Refresh the display of inline images."
  17378. (interactive)
  17379. (if (not org-inline-image-overlays)
  17380. (org-toggle-inline-images)
  17381. (org-toggle-inline-images)
  17382. (org-toggle-inline-images)))
  17383. (defun org-display-inline-images (&optional include-linked refresh beg end)
  17384. "Display inline images.
  17385. Normally only links without a description part are inlined, because this
  17386. is how it will work for export. When INCLUDE-LINKED is set, also links
  17387. with a description part will be inlined. This can be nice for a quick
  17388. look at those images, but it does not reflect what exported files will look
  17389. like.
  17390. When REFRESH is set, refresh existing images between BEG and END.
  17391. This will create new image displays only if necessary.
  17392. BEG and END default to the buffer boundaries."
  17393. (interactive "P")
  17394. (when (display-graphic-p)
  17395. (unless refresh
  17396. (org-remove-inline-images)
  17397. (if (fboundp 'clear-image-cache) (clear-image-cache)))
  17398. (save-excursion
  17399. (save-restriction
  17400. (widen)
  17401. (setq beg (or beg (point-min)) end (or end (point-max)))
  17402. (goto-char beg)
  17403. (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
  17404. (substring (org-image-file-name-regexp) 0 -2)
  17405. "\\)\\]" (if include-linked "" "\\]")))
  17406. (case-fold-search t)
  17407. old file ov img type attrwidth width)
  17408. (while (re-search-forward re end t)
  17409. (setq old (get-char-property-and-overlay (match-beginning 1)
  17410. 'org-image-overlay)
  17411. file (expand-file-name
  17412. (concat (or (match-string 3) "") (match-string 4))))
  17413. (when (image-type-available-p 'imagemagick)
  17414. (setq attrwidth (if (or (listp org-image-actual-width)
  17415. (null org-image-actual-width))
  17416. (save-excursion
  17417. (save-match-data
  17418. (when (re-search-backward
  17419. "#\\+attr.*:width[ \t]+\\([^ ]+\\)"
  17420. (save-excursion
  17421. (re-search-backward "^[ \t]*$\\|\\`" nil t)) t)
  17422. (string-to-number (match-string 1))))))
  17423. width (cond ((eq org-image-actual-width t) nil)
  17424. ((null org-image-actual-width) attrwidth)
  17425. ((numberp org-image-actual-width)
  17426. org-image-actual-width)
  17427. ((listp org-image-actual-width)
  17428. (or attrwidth (car org-image-actual-width))))
  17429. type (if width 'imagemagick)))
  17430. (when (file-exists-p file)
  17431. (if (and (car-safe old) refresh)
  17432. (image-refresh (overlay-get (cdr old) 'display))
  17433. (setq img (save-match-data (create-image file type nil :width width)))
  17434. (when img
  17435. (setq ov (make-overlay (match-beginning 0) (match-end 0)))
  17436. (overlay-put ov 'display img)
  17437. (overlay-put ov 'face 'default)
  17438. (overlay-put ov 'org-image-overlay t)
  17439. (overlay-put ov 'modification-hooks
  17440. (list 'org-display-inline-remove-overlay))
  17441. (push ov org-inline-image-overlays))))))))))
  17442. (define-obsolete-function-alias
  17443. 'org-display-inline-modification-hook 'org-display-inline-remove-overlay "24.3")
  17444. (defun org-display-inline-remove-overlay (ov after beg end &optional len)
  17445. "Remove inline-display overlay if a corresponding region is modified."
  17446. (let ((inhibit-modification-hooks t))
  17447. (when (and ov after)
  17448. (delete ov org-inline-image-overlays)
  17449. (delete-overlay ov))))
  17450. (defun org-remove-inline-images ()
  17451. "Remove inline display of images."
  17452. (interactive)
  17453. (mapc 'delete-overlay org-inline-image-overlays)
  17454. (setq org-inline-image-overlays nil))
  17455. ;;;; Key bindings
  17456. ;; Outline functions from `outline-mode-prefix-map'
  17457. ;; that can be remapped in Org:
  17458. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  17459. (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
  17460. (define-key org-mode-map [remap outline-forward-same-level]
  17461. 'org-forward-heading-same-level)
  17462. (define-key org-mode-map [remap outline-backward-same-level]
  17463. 'org-backward-heading-same-level)
  17464. (define-key org-mode-map [remap show-branches]
  17465. 'org-kill-note-or-show-branches)
  17466. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  17467. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  17468. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  17469. ;; Outline functions from `outline-mode-prefix-map' that can not
  17470. ;; be remapped in Org:
  17471. ;;
  17472. ;; - the column "key binding" shows whether the Outline function is still
  17473. ;; available in Org mode on the same key that it has been bound to in
  17474. ;; Outline mode:
  17475. ;; - "overridden": key used for a different functionality in Org mode
  17476. ;; - else: key still bound to the same Outline function in Org mode
  17477. ;;
  17478. ;; | Outline function | key binding | Org replacement |
  17479. ;; |------------------------------------+-------------+-----------------------|
  17480. ;; | `outline-next-visible-heading' | `C-c C-n' | still same function |
  17481. ;; | `outline-previous-visible-heading' | `C-c C-p' | still same function |
  17482. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  17483. ;; | `outline-move-subtree-up' | overridden | better: org-shiftup |
  17484. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  17485. ;; | `show-entry' | overridden | no replacement |
  17486. ;; | `show-children' | `C-c C-i' | visibility cycling |
  17487. ;; | `show-branches' | `C-c C-k' | still same function |
  17488. ;; | `show-subtree' | overridden | visibility cycling |
  17489. ;; | `show-all' | overridden | no replacement |
  17490. ;; | `hide-subtree' | overridden | visibility cycling |
  17491. ;; | `hide-body' | overridden | no replacement |
  17492. ;; | `hide-entry' | overridden | visibility cycling |
  17493. ;; | `hide-leaves' | overridden | no replacement |
  17494. ;; | `hide-sublevels' | overridden | no replacement |
  17495. ;; | `hide-other' | overridden | no replacement |
  17496. ;; Make `C-c C-x' a prefix key
  17497. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  17498. ;; TAB key with modifiers
  17499. (org-defkey org-mode-map "\C-i" 'org-cycle)
  17500. (org-defkey org-mode-map [(tab)] 'org-cycle)
  17501. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  17502. (org-defkey org-mode-map "\M-\t" 'pcomplete)
  17503. ;; The following line is necessary under Suse GNU/Linux
  17504. (unless (featurep 'xemacs)
  17505. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  17506. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  17507. (define-key org-mode-map [backtab] 'org-shifttab)
  17508. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  17509. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  17510. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  17511. ;; Cursor keys with modifiers
  17512. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  17513. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  17514. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  17515. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  17516. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  17517. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  17518. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  17519. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  17520. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  17521. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  17522. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  17523. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  17524. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  17525. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  17526. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  17527. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  17528. ;; Babel keys
  17529. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  17530. (mapc (lambda (pair)
  17531. (define-key org-babel-map (car pair) (cdr pair)))
  17532. org-babel-key-bindings)
  17533. ;;; Extra keys for tty access.
  17534. ;; We only set them when really needed because otherwise the
  17535. ;; menus don't show the simple keys
  17536. (when (or org-use-extra-keys
  17537. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  17538. (not window-system))
  17539. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  17540. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  17541. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  17542. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  17543. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  17544. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  17545. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  17546. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  17547. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  17548. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  17549. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  17550. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  17551. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  17552. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  17553. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  17554. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  17555. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  17556. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  17557. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  17558. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  17559. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  17560. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  17561. (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
  17562. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  17563. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  17564. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  17565. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  17566. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  17567. ;; All the other keys
  17568. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  17569. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  17570. (if (boundp 'narrow-map)
  17571. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  17572. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  17573. (if (boundp 'narrow-map)
  17574. (org-defkey narrow-map "b" 'org-narrow-to-block)
  17575. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  17576. (if (boundp 'narrow-map)
  17577. (org-defkey narrow-map "e" 'org-narrow-to-element)
  17578. (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
  17579. (org-defkey org-mode-map "\C-\M-t" 'org-transpose-element)
  17580. (org-defkey org-mode-map "\M-}" 'org-forward-element)
  17581. (org-defkey org-mode-map "\M-{" 'org-backward-element)
  17582. (org-defkey org-mode-map "\C-c\C-^" 'org-up-element)
  17583. (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
  17584. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
  17585. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
  17586. (org-defkey org-mode-map "\C-c\M-f" 'org-next-block)
  17587. (org-defkey org-mode-map "\C-c\M-b" 'org-previous-block)
  17588. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  17589. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-archive-subtree)
  17590. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  17591. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  17592. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  17593. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  17594. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  17595. (org-defkey org-mode-map "\C-c\C-xq" 'org-toggle-tags-groups)
  17596. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  17597. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  17598. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  17599. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  17600. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  17601. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  17602. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  17603. (org-defkey org-mode-map "\C-c\M-w" 'org-copy)
  17604. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  17605. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  17606. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  17607. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  17608. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  17609. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  17610. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  17611. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  17612. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  17613. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  17614. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  17615. (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
  17616. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  17617. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  17618. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  17619. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  17620. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  17621. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  17622. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  17623. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  17624. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  17625. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  17626. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  17627. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  17628. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  17629. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  17630. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  17631. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  17632. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  17633. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  17634. (org-defkey org-mode-map "\C-c^" 'org-sort)
  17635. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  17636. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  17637. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  17638. (org-defkey org-mode-map [remap open-line] 'org-open-line)
  17639. (org-defkey org-mode-map [remap forward-paragraph] 'org-forward-paragraph)
  17640. (org-defkey org-mode-map [remap backward-paragraph] 'org-backward-paragraph)
  17641. (org-defkey org-mode-map "\C-m" 'org-return)
  17642. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  17643. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  17644. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  17645. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  17646. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  17647. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  17648. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  17649. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  17650. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  17651. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  17652. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  17653. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  17654. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  17655. (org-defkey org-mode-map "\C-c\C-e" 'org-export-dispatch)
  17656. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  17657. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  17658. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  17659. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  17660. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  17661. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  17662. (org-defkey org-mode-map "\M-h" 'org-mark-element)
  17663. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  17664. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  17665. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  17666. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  17667. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  17668. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  17669. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  17670. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
  17671. (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
  17672. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  17673. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  17674. (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
  17675. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  17676. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  17677. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  17678. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  17679. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  17680. (org-defkey org-mode-map "\C-c\C-x\C-\M-v" 'org-redisplay-inline-images)
  17681. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  17682. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  17683. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  17684. (org-defkey org-mode-map "\C-c\C-xP" 'org-set-property-and-value)
  17685. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  17686. (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
  17687. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  17688. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  17689. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  17690. (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
  17691. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  17692. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  17693. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  17694. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  17695. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  17696. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  17697. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  17698. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  17699. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  17700. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  17701. (when (featurep 'xemacs)
  17702. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  17703. (defconst org-speed-commands-default
  17704. '(
  17705. ("Outline Navigation")
  17706. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  17707. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  17708. ("f" . (org-speed-move-safe 'org-forward-heading-same-level))
  17709. ("b" . (org-speed-move-safe 'org-backward-heading-same-level))
  17710. ("F" . org-next-block)
  17711. ("B" . org-previous-block)
  17712. ("u" . (org-speed-move-safe 'outline-up-heading))
  17713. ("j" . org-goto)
  17714. ("g" . (org-refile t))
  17715. ("Outline Visibility")
  17716. ("c" . org-cycle)
  17717. ("C" . org-shifttab)
  17718. (" " . org-display-outline-path)
  17719. ("s" . org-narrow-to-subtree)
  17720. ("=" . org-columns)
  17721. ("Outline Structure Editing")
  17722. ("U" . org-shiftmetaup)
  17723. ("D" . org-shiftmetadown)
  17724. ("r" . org-metaright)
  17725. ("l" . org-metaleft)
  17726. ("R" . org-shiftmetaright)
  17727. ("L" . org-shiftmetaleft)
  17728. ("i" . (progn (forward-char 1) (call-interactively
  17729. 'org-insert-heading-respect-content)))
  17730. ("^" . org-sort)
  17731. ("w" . org-refile)
  17732. ("a" . org-archive-subtree-default-with-confirmation)
  17733. ("@" . org-mark-subtree)
  17734. ("#" . org-toggle-comment)
  17735. ("Clock Commands")
  17736. ("I" . org-clock-in)
  17737. ("O" . org-clock-out)
  17738. ("Meta Data Editing")
  17739. ("t" . org-todo)
  17740. ("," . (org-priority))
  17741. ("0" . (org-priority ?\ ))
  17742. ("1" . (org-priority ?A))
  17743. ("2" . (org-priority ?B))
  17744. ("3" . (org-priority ?C))
  17745. (":" . org-set-tags-command)
  17746. ("e" . org-set-effort)
  17747. ("E" . org-inc-effort)
  17748. ("W" . (lambda(m) (interactive "sMinutes before warning: ")
  17749. (org-entry-put (point) "APPT_WARNTIME" m)))
  17750. ("Agenda Views etc")
  17751. ("v" . org-agenda)
  17752. ("/" . org-sparse-tree)
  17753. ("Misc")
  17754. ("o" . org-open-at-point)
  17755. ("?" . org-speed-command-help)
  17756. ("<" . (org-agenda-set-restriction-lock 'subtree))
  17757. (">" . (org-agenda-remove-restriction-lock))
  17758. )
  17759. "The default speed commands.")
  17760. (defun org-print-speed-command (e)
  17761. (if (> (length (car e)) 1)
  17762. (progn
  17763. (princ "\n")
  17764. (princ (car e))
  17765. (princ "\n")
  17766. (princ (make-string (length (car e)) ?-))
  17767. (princ "\n"))
  17768. (princ (car e))
  17769. (princ " ")
  17770. (if (symbolp (cdr e))
  17771. (princ (symbol-name (cdr e)))
  17772. (prin1 (cdr e)))
  17773. (princ "\n")))
  17774. (defun org-speed-command-help ()
  17775. "Show the available speed commands."
  17776. (interactive)
  17777. (if (not org-use-speed-commands)
  17778. (user-error "Speed commands are not activated, customize `org-use-speed-commands'")
  17779. (with-output-to-temp-buffer "*Help*"
  17780. (princ "User-defined Speed commands\n===========================\n")
  17781. (mapc 'org-print-speed-command org-speed-commands-user)
  17782. (princ "\n")
  17783. (princ "Built-in Speed commands\n=======================\n")
  17784. (mapc 'org-print-speed-command org-speed-commands-default))
  17785. (with-current-buffer "*Help*"
  17786. (setq truncate-lines t))))
  17787. (defun org-speed-move-safe (cmd)
  17788. "Execute CMD, but make sure that the cursor always ends up in a headline.
  17789. If not, return to the original position and throw an error."
  17790. (interactive)
  17791. (let ((pos (point)))
  17792. (call-interactively cmd)
  17793. (unless (and (bolp) (org-at-heading-p))
  17794. (goto-char pos)
  17795. (error "Boundary reached while executing %s" cmd))))
  17796. (defvar org-self-insert-command-undo-counter 0)
  17797. (defvar org-table-auto-blank-field) ; defined in org-table.el
  17798. (defvar org-speed-command nil)
  17799. (define-obsolete-function-alias
  17800. 'org-speed-command-default-hook 'org-speed-command-activate "24.3")
  17801. (defun org-speed-command-activate (keys)
  17802. "Hook for activating single-letter speed commands.
  17803. `org-speed-commands-default' specifies a minimal command set.
  17804. Use `org-speed-commands-user' for further customization."
  17805. (when (or (and (bolp) (looking-at org-outline-regexp))
  17806. (and (functionp org-use-speed-commands)
  17807. (funcall org-use-speed-commands)))
  17808. (cdr (assoc keys (append org-speed-commands-user
  17809. org-speed-commands-default)))))
  17810. (define-obsolete-function-alias
  17811. 'org-babel-speed-command-hook 'org-babel-speed-command-activate "24.3")
  17812. (defun org-babel-speed-command-activate (keys)
  17813. "Hook for activating single-letter code block commands."
  17814. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  17815. (cdr (assoc keys org-babel-key-bindings))))
  17816. (defcustom org-speed-command-hook
  17817. '(org-speed-command-default-hook org-babel-speed-command-hook)
  17818. "Hook for activating speed commands at strategic locations.
  17819. Hook functions are called in sequence until a valid handler is
  17820. found.
  17821. Each hook takes a single argument, a user-pressed command key
  17822. which is also a `self-insert-command' from the global map.
  17823. Within the hook, examine the cursor position and the command key
  17824. and return nil or a valid handler as appropriate. Handler could
  17825. be one of an interactive command, a function, or a form.
  17826. Set `org-use-speed-commands' to non-nil value to enable this
  17827. hook. The default setting is `org-speed-command-activate'."
  17828. :group 'org-structure
  17829. :version "24.1"
  17830. :type 'hook)
  17831. (defun org-self-insert-command (N)
  17832. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  17833. If the cursor is in a table looking at whitespace, the whitespace is
  17834. overwritten, and the table is not marked as requiring realignment."
  17835. (interactive "p")
  17836. (org-check-before-invisible-edit 'insert)
  17837. (cond
  17838. ((and org-use-speed-commands
  17839. (setq org-speed-command
  17840. (run-hook-with-args-until-success
  17841. 'org-speed-command-hook (this-command-keys))))
  17842. (cond
  17843. ((commandp org-speed-command)
  17844. (setq this-command org-speed-command)
  17845. (call-interactively org-speed-command))
  17846. ((functionp org-speed-command)
  17847. (funcall org-speed-command))
  17848. ((and org-speed-command (listp org-speed-command))
  17849. (eval org-speed-command))
  17850. (t (let (org-use-speed-commands)
  17851. (call-interactively 'org-self-insert-command)))))
  17852. ((and
  17853. (org-table-p)
  17854. (progn
  17855. ;; check if we blank the field, and if that triggers align
  17856. (and (featurep 'org-table) org-table-auto-blank-field
  17857. (member last-command
  17858. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  17859. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  17860. ;; got extra space, this field does not determine column width
  17861. (let (org-table-may-need-update) (org-table-blank-field))
  17862. ;; no extra space, this field may determine column width
  17863. (org-table-blank-field)))
  17864. t)
  17865. (eq N 1)
  17866. (looking-at "[^|\n]* |"))
  17867. (let (org-table-may-need-update)
  17868. (goto-char (1- (match-end 0)))
  17869. (backward-delete-char 1)
  17870. (goto-char (match-beginning 0))
  17871. (self-insert-command N)))
  17872. (t
  17873. (setq org-table-may-need-update t)
  17874. (self-insert-command N)
  17875. (org-fix-tags-on-the-fly)
  17876. (if org-self-insert-cluster-for-undo
  17877. (if (not (eq last-command 'org-self-insert-command))
  17878. (setq org-self-insert-command-undo-counter 1)
  17879. (if (>= org-self-insert-command-undo-counter 20)
  17880. (setq org-self-insert-command-undo-counter 1)
  17881. (and (> org-self-insert-command-undo-counter 0)
  17882. buffer-undo-list (listp buffer-undo-list)
  17883. (not (cadr buffer-undo-list)) ; remove nil entry
  17884. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  17885. (setq org-self-insert-command-undo-counter
  17886. (1+ org-self-insert-command-undo-counter))))))))
  17887. (defun org-check-before-invisible-edit (kind)
  17888. "Check is editing if kind KIND would be dangerous with invisible text around.
  17889. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  17890. ;; First, try to get out of here as quickly as possible, to reduce overhead
  17891. (if (and org-catch-invisible-edits
  17892. (or (not (boundp 'visible-mode)) (not visible-mode))
  17893. (or (get-char-property (point) 'invisible)
  17894. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  17895. ;; OK, we need to take a closer look
  17896. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  17897. (invisible-before-point (if (bobp) nil (get-char-property
  17898. (1- (point)) 'invisible)))
  17899. (border-and-ok-direction
  17900. (or
  17901. ;; Check if we are acting predictably before invisible text
  17902. (and invisible-at-point (not invisible-before-point)
  17903. (memq kind '(insert delete-backward)))
  17904. ;; Check if we are acting predictably after invisible text
  17905. ;; This works not well, and I have turned it off. It seems
  17906. ;; better to always show and stop after invisible text.
  17907. ;; (and (not invisible-at-point) invisible-before-point
  17908. ;; (memq kind '(insert delete)))
  17909. )))
  17910. (when (or (memq invisible-at-point '(outline org-hide-block t))
  17911. (memq invisible-before-point '(outline org-hide-block t)))
  17912. (if (eq org-catch-invisible-edits 'error)
  17913. (user-error "Editing in invisible areas is prohibited, make them visible first"))
  17914. (if (and org-custom-properties-overlays
  17915. (y-or-n-p "Display invisible properties in this buffer? "))
  17916. (org-toggle-custom-properties-visibility)
  17917. ;; Make the area visible
  17918. (save-excursion
  17919. (if invisible-before-point
  17920. (goto-char (previous-single-char-property-change
  17921. (point) 'invisible)))
  17922. (show-subtree))
  17923. (cond
  17924. ((eq org-catch-invisible-edits 'show)
  17925. ;; That's it, we do the edit after showing
  17926. (message
  17927. "Unfolding invisible region around point before editing")
  17928. (sit-for 1))
  17929. ((and (eq org-catch-invisible-edits 'smart)
  17930. border-and-ok-direction)
  17931. (message "Unfolding invisible region around point before editing"))
  17932. (t
  17933. ;; Don't do the edit, make the user repeat it in full visibility
  17934. (user-error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
  17935. (defun org-fix-tags-on-the-fly ()
  17936. (when (and (equal (char-after (point-at-bol)) ?*)
  17937. (org-at-heading-p))
  17938. (org-align-tags-here org-tags-column)))
  17939. (defun org-delete-backward-char (N)
  17940. "Like `delete-backward-char', insert whitespace at field end in tables.
  17941. When deleting backwards, in tables this function will insert whitespace in
  17942. front of the next \"|\" separator, to keep the table aligned. The table will
  17943. still be marked for re-alignment if the field did fill the entire column,
  17944. because, in this case the deletion might narrow the column."
  17945. (interactive "p")
  17946. (save-match-data
  17947. (org-check-before-invisible-edit 'delete-backward)
  17948. (if (and (org-table-p)
  17949. (eq N 1)
  17950. (string-match "|" (buffer-substring (point-at-bol) (point)))
  17951. (looking-at ".*?|"))
  17952. (let ((pos (point))
  17953. (noalign (looking-at "[^|\n\r]* |"))
  17954. (c org-table-may-need-update))
  17955. (backward-delete-char N)
  17956. (if (not overwrite-mode)
  17957. (progn
  17958. (skip-chars-forward "^|")
  17959. (insert " ")
  17960. (goto-char (1- pos))))
  17961. ;; noalign: if there were two spaces at the end, this field
  17962. ;; does not determine the width of the column.
  17963. (if noalign (setq org-table-may-need-update c)))
  17964. (backward-delete-char N)
  17965. (org-fix-tags-on-the-fly))))
  17966. (defun org-delete-char (N)
  17967. "Like `delete-char', but insert whitespace at field end in tables.
  17968. When deleting characters, in tables this function will insert whitespace in
  17969. front of the next \"|\" separator, to keep the table aligned. The table will
  17970. still be marked for re-alignment if the field did fill the entire column,
  17971. because, in this case the deletion might narrow the column."
  17972. (interactive "p")
  17973. (save-match-data
  17974. (org-check-before-invisible-edit 'delete)
  17975. (if (and (org-table-p)
  17976. (not (bolp))
  17977. (not (= (char-after) ?|))
  17978. (eq N 1))
  17979. (if (looking-at ".*?|")
  17980. (let ((pos (point))
  17981. (noalign (looking-at "[^|\n\r]* |"))
  17982. (c org-table-may-need-update))
  17983. (replace-match
  17984. (concat (substring (match-string 0) 1 -1) " |") nil t)
  17985. (goto-char pos)
  17986. ;; noalign: if there were two spaces at the end, this field
  17987. ;; does not determine the width of the column.
  17988. (if noalign (setq org-table-may-need-update c)))
  17989. (delete-char N))
  17990. (delete-char N)
  17991. (org-fix-tags-on-the-fly))))
  17992. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  17993. (put 'org-self-insert-command 'delete-selection
  17994. (lambda ()
  17995. (not (run-hook-with-args-until-success
  17996. 'self-insert-uses-region-functions))))
  17997. (put 'orgtbl-self-insert-command 'delete-selection
  17998. (lambda ()
  17999. (not (run-hook-with-args-until-success
  18000. 'self-insert-uses-region-functions))))
  18001. (put 'org-delete-char 'delete-selection 'supersede)
  18002. (put 'org-delete-backward-char 'delete-selection 'supersede)
  18003. (put 'org-yank 'delete-selection 'yank)
  18004. ;; Make `flyspell-mode' delay after some commands
  18005. (put 'org-self-insert-command 'flyspell-delayed t)
  18006. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  18007. (put 'org-delete-char 'flyspell-delayed t)
  18008. (put 'org-delete-backward-char 'flyspell-delayed t)
  18009. ;; Make pabbrev-mode expand after org-mode commands
  18010. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  18011. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  18012. (defun org-remap (map &rest commands)
  18013. "In MAP, remap the functions given in COMMANDS.
  18014. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  18015. (let (new old)
  18016. (while commands
  18017. (setq old (pop commands) new (pop commands))
  18018. (if (fboundp 'command-remapping)
  18019. (org-defkey map (vector 'remap old) new)
  18020. (substitute-key-definition old new map global-map)))))
  18021. (defun org-transpose-words ()
  18022. "Transpose words for Org.
  18023. This uses the `org-mode-transpose-word-syntax-table' syntax
  18024. table, which interprets characters in `org-emphasis-alist' as
  18025. word constituents."
  18026. (interactive)
  18027. (with-syntax-table org-mode-transpose-word-syntax-table
  18028. (call-interactively 'transpose-words)))
  18029. (org-remap org-mode-map 'transpose-words 'org-transpose-words)
  18030. (when (eq org-enable-table-editor 'optimized)
  18031. ;; If the user wants maximum table support, we need to hijack
  18032. ;; some standard editing functions
  18033. (org-remap org-mode-map
  18034. 'self-insert-command 'org-self-insert-command
  18035. 'delete-char 'org-delete-char
  18036. 'delete-backward-char 'org-delete-backward-char)
  18037. (org-defkey org-mode-map "|" 'org-force-self-insert))
  18038. (defvar org-ctrl-c-ctrl-c-hook nil
  18039. "Hook for functions attaching themselves to `C-c C-c'.
  18040. This can be used to add additional functionality to the C-c C-c
  18041. key which executes context-dependent commands. This hook is run
  18042. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  18043. run after the last test.
  18044. Each function will be called with no arguments. The function
  18045. must check if the context is appropriate for it to act. If yes,
  18046. it should do its thing and then return a non-nil value. If the
  18047. context is wrong, just do nothing and return nil.")
  18048. (defvar org-ctrl-c-ctrl-c-final-hook nil
  18049. "Hook for functions attaching themselves to `C-c C-c'.
  18050. This can be used to add additional functionality to the C-c C-c
  18051. key which executes context-dependent commands. This hook is run
  18052. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  18053. before the first test.
  18054. Each function will be called with no arguments. The function
  18055. must check if the context is appropriate for it to act. If yes,
  18056. it should do its thing and then return a non-nil value. If the
  18057. context is wrong, just do nothing and return nil.")
  18058. (defvar org-tab-first-hook nil
  18059. "Hook for functions to attach themselves to TAB.
  18060. See `org-ctrl-c-ctrl-c-hook' for more information.
  18061. This hook runs as the first action when TAB is pressed, even before
  18062. `org-cycle' messes around with the `outline-regexp' to cater for
  18063. inline tasks and plain list item folding.
  18064. If any function in this hook returns t, any other actions that
  18065. would have been caused by TAB (such as table field motion or visibility
  18066. cycling) will not occur.")
  18067. (defvar org-tab-after-check-for-table-hook nil
  18068. "Hook for functions to attach themselves to TAB.
  18069. See `org-ctrl-c-ctrl-c-hook' for more information.
  18070. This hook runs after it has been established that the cursor is not in a
  18071. table, but before checking if the cursor is in a headline or if global cycling
  18072. should be done.
  18073. If any function in this hook returns t, not other actions like visibility
  18074. cycling will be done.")
  18075. (defvar org-tab-after-check-for-cycling-hook nil
  18076. "Hook for functions to attach themselves to TAB.
  18077. See `org-ctrl-c-ctrl-c-hook' for more information.
  18078. This hook runs after it has been established that not table field motion and
  18079. not visibility should be done because of current context. This is probably
  18080. the place where a package like yasnippets can hook in.")
  18081. (defvar org-tab-before-tab-emulation-hook nil
  18082. "Hook for functions to attach themselves to TAB.
  18083. See `org-ctrl-c-ctrl-c-hook' for more information.
  18084. This hook runs after every other options for TAB have been exhausted, but
  18085. before indentation and \t insertion takes place.")
  18086. (defvar org-metaleft-hook nil
  18087. "Hook for functions attaching themselves to `M-left'.
  18088. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18089. (defvar org-metaright-hook nil
  18090. "Hook for functions attaching themselves to `M-right'.
  18091. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18092. (defvar org-metaup-hook nil
  18093. "Hook for functions attaching themselves to `M-up'.
  18094. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18095. (defvar org-metadown-hook nil
  18096. "Hook for functions attaching themselves to `M-down'.
  18097. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18098. (defvar org-shiftmetaleft-hook nil
  18099. "Hook for functions attaching themselves to `M-S-left'.
  18100. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18101. (defvar org-shiftmetaright-hook nil
  18102. "Hook for functions attaching themselves to `M-S-right'.
  18103. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18104. (defvar org-shiftmetaup-hook nil
  18105. "Hook for functions attaching themselves to `M-S-up'.
  18106. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18107. (defvar org-shiftmetadown-hook nil
  18108. "Hook for functions attaching themselves to `M-S-down'.
  18109. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18110. (defvar org-metareturn-hook nil
  18111. "Hook for functions attaching themselves to `M-RET'.
  18112. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18113. (defvar org-shiftup-hook nil
  18114. "Hook for functions attaching themselves to `S-up'.
  18115. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18116. (defvar org-shiftup-final-hook nil
  18117. "Hook for functions attaching themselves to `S-up'.
  18118. This one runs after all other options except shift-select have been excluded.
  18119. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18120. (defvar org-shiftdown-hook nil
  18121. "Hook for functions attaching themselves to `S-down'.
  18122. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18123. (defvar org-shiftdown-final-hook nil
  18124. "Hook for functions attaching themselves to `S-down'.
  18125. This one runs after all other options except shift-select have been excluded.
  18126. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18127. (defvar org-shiftleft-hook nil
  18128. "Hook for functions attaching themselves to `S-left'.
  18129. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18130. (defvar org-shiftleft-final-hook nil
  18131. "Hook for functions attaching themselves to `S-left'.
  18132. This one runs after all other options except shift-select have been excluded.
  18133. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18134. (defvar org-shiftright-hook nil
  18135. "Hook for functions attaching themselves to `S-right'.
  18136. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18137. (defvar org-shiftright-final-hook nil
  18138. "Hook for functions attaching themselves to `S-right'.
  18139. This one runs after all other options except shift-select have been excluded.
  18140. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18141. (defun org-modifier-cursor-error ()
  18142. "Throw an error, a modified cursor command was applied in wrong context."
  18143. (user-error "This command is active in special context like tables, headlines or items"))
  18144. (defun org-shiftselect-error ()
  18145. "Throw an error because Shift-Cursor command was applied in wrong context."
  18146. (if (and (boundp 'shift-select-mode) shift-select-mode)
  18147. (user-error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  18148. (user-error "This command works only in special context like headlines or timestamps")))
  18149. (defun org-call-for-shift-select (cmd)
  18150. (let ((this-command-keys-shift-translated t))
  18151. (call-interactively cmd)))
  18152. (defun org-shifttab (&optional arg)
  18153. "Global visibility cycling or move to previous table field.
  18154. Call `org-table-previous-field' within a table.
  18155. When ARG is nil, cycle globally through visibility states.
  18156. When ARG is a numeric prefix, show contents of this level."
  18157. (interactive "P")
  18158. (cond
  18159. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  18160. ((integerp arg)
  18161. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  18162. (message "Content view to level: %d" arg)
  18163. (org-content (prefix-numeric-value arg2))
  18164. (org-cycle-show-empty-lines t)
  18165. (setq org-cycle-global-status 'overview)))
  18166. (t (call-interactively 'org-global-cycle))))
  18167. (defun org-shiftmetaleft ()
  18168. "Promote subtree or delete table column.
  18169. Calls `org-promote-subtree', `org-outdent-item-tree', or
  18170. `org-table-delete-column', depending on context. See the
  18171. individual commands for more information."
  18172. (interactive)
  18173. (cond
  18174. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  18175. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  18176. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  18177. ((if (not (org-region-active-p)) (org-at-item-p)
  18178. (save-excursion (goto-char (region-beginning))
  18179. (org-at-item-p)))
  18180. (call-interactively 'org-outdent-item-tree))
  18181. (t (org-modifier-cursor-error))))
  18182. (defun org-shiftmetaright ()
  18183. "Demote subtree or insert table column.
  18184. Calls `org-demote-subtree', `org-indent-item-tree', or
  18185. `org-table-insert-column', depending on context. See the
  18186. individual commands for more information."
  18187. (interactive)
  18188. (cond
  18189. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  18190. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  18191. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  18192. ((if (not (org-region-active-p)) (org-at-item-p)
  18193. (save-excursion (goto-char (region-beginning))
  18194. (org-at-item-p)))
  18195. (call-interactively 'org-indent-item-tree))
  18196. (t (org-modifier-cursor-error))))
  18197. (defun org-shiftmetaup (&optional arg)
  18198. "Move subtree up or kill table row.
  18199. Calls `org-move-subtree-up' or `org-table-kill-row' or
  18200. `org-move-item-up' or `org-timestamp-up', depending on context.
  18201. See the individual commands for more information."
  18202. (interactive "P")
  18203. (cond
  18204. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  18205. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  18206. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  18207. ((org-at-item-p) (call-interactively 'org-move-item-up))
  18208. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18209. (call-interactively 'org-timestamp-up)))
  18210. (t (call-interactively 'org-drag-line-backward))))
  18211. (defun org-shiftmetadown (&optional arg)
  18212. "Move subtree down or insert table row.
  18213. Calls `org-move-subtree-down' or `org-table-insert-row' or
  18214. `org-move-item-down' or `org-timestamp-up', depending on context.
  18215. See the individual commands for more information."
  18216. (interactive "P")
  18217. (cond
  18218. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  18219. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  18220. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  18221. ((org-at-item-p) (call-interactively 'org-move-item-down))
  18222. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18223. (call-interactively 'org-timestamp-down)))
  18224. (t (call-interactively 'org-drag-line-forward))))
  18225. (defsubst org-hidden-tree-error ()
  18226. (user-error
  18227. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  18228. (defun org-metaleft (&optional arg)
  18229. "Promote heading or move table column to left.
  18230. Calls `org-do-promote' or `org-table-move-column', depending on context.
  18231. With no specific context, calls the Emacs default `backward-word'.
  18232. See the individual commands for more information."
  18233. (interactive "P")
  18234. (cond
  18235. ((run-hook-with-args-until-success 'org-metaleft-hook))
  18236. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  18237. ((org-with-limited-levels
  18238. (or (org-at-heading-p)
  18239. (and (org-region-active-p)
  18240. (save-excursion
  18241. (goto-char (region-beginning))
  18242. (org-at-heading-p)))))
  18243. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18244. (call-interactively 'org-do-promote))
  18245. ;; At an inline task.
  18246. ((org-at-heading-p)
  18247. (call-interactively 'org-inlinetask-promote))
  18248. ((or (org-at-item-p)
  18249. (and (org-region-active-p)
  18250. (save-excursion
  18251. (goto-char (region-beginning))
  18252. (org-at-item-p))))
  18253. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18254. (call-interactively 'org-outdent-item))
  18255. (t (call-interactively 'backward-word))))
  18256. (defun org-metaright (&optional arg)
  18257. "Demote a subtree, a list item or move table column to right.
  18258. In front of a drawer or a block keyword, indent it correctly.
  18259. With no specific context, calls the Emacs default `forward-word'.
  18260. See the individual commands for more information."
  18261. (interactive "P")
  18262. (cond
  18263. ((run-hook-with-args-until-success 'org-metaright-hook))
  18264. ((org-at-table-p) (call-interactively 'org-table-move-column))
  18265. ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
  18266. ((org-at-block-p) (call-interactively 'org-indent-block))
  18267. ((org-with-limited-levels
  18268. (or (org-at-heading-p)
  18269. (and (org-region-active-p)
  18270. (save-excursion
  18271. (goto-char (region-beginning))
  18272. (org-at-heading-p)))))
  18273. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18274. (call-interactively 'org-do-demote))
  18275. ;; At an inline task.
  18276. ((org-at-heading-p)
  18277. (call-interactively 'org-inlinetask-demote))
  18278. ((or (org-at-item-p)
  18279. (and (org-region-active-p)
  18280. (save-excursion
  18281. (goto-char (region-beginning))
  18282. (org-at-item-p))))
  18283. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18284. (call-interactively 'org-indent-item))
  18285. (t (call-interactively 'forward-word))))
  18286. (defun org-check-for-hidden (what)
  18287. "Check if there are hidden headlines/items in the current visual line.
  18288. WHAT can be either `headlines' or `items'. If the current line is
  18289. an outline or item heading and it has a folded subtree below it,
  18290. this function returns t, nil otherwise."
  18291. (let ((re (cond
  18292. ((eq what 'headlines) org-outline-regexp-bol)
  18293. ((eq what 'items) (org-item-beginning-re))
  18294. (t (error "This should not happen"))))
  18295. beg end)
  18296. (save-excursion
  18297. (catch 'exit
  18298. (unless (org-region-active-p)
  18299. (setq beg (point-at-bol))
  18300. (beginning-of-line 2)
  18301. (while (and (not (eobp)) ;; this is like `next-line'
  18302. (get-char-property (1- (point)) 'invisible))
  18303. (beginning-of-line 2))
  18304. (setq end (point))
  18305. (goto-char beg)
  18306. (goto-char (point-at-eol))
  18307. (setq end (max end (point)))
  18308. (while (re-search-forward re end t)
  18309. (if (get-char-property (match-beginning 0) 'invisible)
  18310. (throw 'exit t))))
  18311. nil))))
  18312. (defun org-metaup (&optional arg)
  18313. "Move subtree up or move table row up.
  18314. Calls `org-move-subtree-up' or `org-table-move-row' or
  18315. `org-move-item-up', depending on context. See the individual commands
  18316. for more information."
  18317. (interactive "P")
  18318. (cond
  18319. ((run-hook-with-args-until-success 'org-metaup-hook))
  18320. ((org-region-active-p)
  18321. (let* ((a (min (region-beginning) (region-end)))
  18322. (b (1- (max (region-beginning) (region-end))))
  18323. (c (save-excursion (goto-char a)
  18324. (move-beginning-of-line 0)))
  18325. (d (save-excursion (goto-char a)
  18326. (move-end-of-line 0) (point))))
  18327. (transpose-regions a b c d)
  18328. (goto-char c)))
  18329. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  18330. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  18331. ((org-at-item-p) (call-interactively 'org-move-item-up))
  18332. (t (org-drag-element-backward))))
  18333. (defun org-metadown (&optional arg)
  18334. "Move subtree down or move table row down.
  18335. Calls `org-move-subtree-down' or `org-table-move-row' or
  18336. `org-move-item-down', depending on context. See the individual
  18337. commands for more information."
  18338. (interactive "P")
  18339. (cond
  18340. ((run-hook-with-args-until-success 'org-metadown-hook))
  18341. ((org-region-active-p)
  18342. (let* ((a (min (region-beginning) (region-end)))
  18343. (b (max (region-beginning) (region-end)))
  18344. (c (save-excursion (goto-char b)
  18345. (move-beginning-of-line 1)))
  18346. (d (save-excursion (goto-char b)
  18347. (move-end-of-line 1) (1+ (point)))))
  18348. (transpose-regions a b c d)
  18349. (goto-char d)))
  18350. ((org-at-table-p) (call-interactively 'org-table-move-row))
  18351. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  18352. ((org-at-item-p) (call-interactively 'org-move-item-down))
  18353. (t (org-drag-element-forward))))
  18354. (defun org-shiftup (&optional arg)
  18355. "Increase item in timestamp or increase priority of current headline.
  18356. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  18357. depending on context. See the individual commands for more information."
  18358. (interactive "P")
  18359. (cond
  18360. ((run-hook-with-args-until-success 'org-shiftup-hook))
  18361. ((and org-support-shift-select (org-region-active-p))
  18362. (org-call-for-shift-select 'previous-line))
  18363. ((org-at-timestamp-p t)
  18364. (call-interactively (if org-edit-timestamp-down-means-later
  18365. 'org-timestamp-down 'org-timestamp-up)))
  18366. ((and (not (eq org-support-shift-select 'always))
  18367. org-enable-priority-commands
  18368. (org-at-heading-p))
  18369. (call-interactively 'org-priority-up))
  18370. ((and (not org-support-shift-select) (org-at-item-p))
  18371. (call-interactively 'org-previous-item))
  18372. ((org-clocktable-try-shift 'up arg))
  18373. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  18374. (org-support-shift-select
  18375. (org-call-for-shift-select 'previous-line))
  18376. (t (org-shiftselect-error))))
  18377. (defun org-shiftdown (&optional arg)
  18378. "Decrease item in timestamp or decrease priority of current headline.
  18379. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  18380. depending on context. See the individual commands for more information."
  18381. (interactive "P")
  18382. (cond
  18383. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  18384. ((and org-support-shift-select (org-region-active-p))
  18385. (org-call-for-shift-select 'next-line))
  18386. ((org-at-timestamp-p t)
  18387. (call-interactively (if org-edit-timestamp-down-means-later
  18388. 'org-timestamp-up 'org-timestamp-down)))
  18389. ((and (not (eq org-support-shift-select 'always))
  18390. org-enable-priority-commands
  18391. (org-at-heading-p))
  18392. (call-interactively 'org-priority-down))
  18393. ((and (not org-support-shift-select) (org-at-item-p))
  18394. (call-interactively 'org-next-item))
  18395. ((org-clocktable-try-shift 'down arg))
  18396. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  18397. (org-support-shift-select
  18398. (org-call-for-shift-select 'next-line))
  18399. (t (org-shiftselect-error))))
  18400. (defun org-shiftright (&optional arg)
  18401. "Cycle the thing at point or in the current line, depending on context.
  18402. Depending on context, this does one of the following:
  18403. - switch a timestamp at point one day into the future
  18404. - on a headline, switch to the next TODO keyword.
  18405. - on an item, switch entire list to the next bullet type
  18406. - on a property line, switch to the next allowed value
  18407. - on a clocktable definition line, move time block into the future"
  18408. (interactive "P")
  18409. (cond
  18410. ((run-hook-with-args-until-success 'org-shiftright-hook))
  18411. ((and org-support-shift-select (org-region-active-p))
  18412. (org-call-for-shift-select 'forward-char))
  18413. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  18414. ((and (not (eq org-support-shift-select 'always))
  18415. (org-at-heading-p))
  18416. (let ((org-inhibit-logging
  18417. (not org-treat-S-cursor-todo-selection-as-state-change))
  18418. (org-inhibit-blocking
  18419. (not org-treat-S-cursor-todo-selection-as-state-change)))
  18420. (org-call-with-arg 'org-todo 'right)))
  18421. ((or (and org-support-shift-select
  18422. (not (eq org-support-shift-select 'always))
  18423. (org-at-item-bullet-p))
  18424. (and (not org-support-shift-select) (org-at-item-p)))
  18425. (org-call-with-arg 'org-cycle-list-bullet nil))
  18426. ((and (not (eq org-support-shift-select 'always))
  18427. (org-at-property-p))
  18428. (call-interactively 'org-property-next-allowed-value))
  18429. ((org-clocktable-try-shift 'right arg))
  18430. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  18431. (org-support-shift-select
  18432. (org-call-for-shift-select 'forward-char))
  18433. (t (org-shiftselect-error))))
  18434. (defun org-shiftleft (&optional arg)
  18435. "Cycle the thing at point or in the current line, depending on context.
  18436. Depending on context, this does one of the following:
  18437. - switch a timestamp at point one day into the past
  18438. - on a headline, switch to the previous TODO keyword.
  18439. - on an item, switch entire list to the previous bullet type
  18440. - on a property line, switch to the previous allowed value
  18441. - on a clocktable definition line, move time block into the past"
  18442. (interactive "P")
  18443. (cond
  18444. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  18445. ((and org-support-shift-select (org-region-active-p))
  18446. (org-call-for-shift-select 'backward-char))
  18447. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  18448. ((and (not (eq org-support-shift-select 'always))
  18449. (org-at-heading-p))
  18450. (let ((org-inhibit-logging
  18451. (not org-treat-S-cursor-todo-selection-as-state-change))
  18452. (org-inhibit-blocking
  18453. (not org-treat-S-cursor-todo-selection-as-state-change)))
  18454. (org-call-with-arg 'org-todo 'left)))
  18455. ((or (and org-support-shift-select
  18456. (not (eq org-support-shift-select 'always))
  18457. (org-at-item-bullet-p))
  18458. (and (not org-support-shift-select) (org-at-item-p)))
  18459. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  18460. ((and (not (eq org-support-shift-select 'always))
  18461. (org-at-property-p))
  18462. (call-interactively 'org-property-previous-allowed-value))
  18463. ((org-clocktable-try-shift 'left arg))
  18464. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  18465. (org-support-shift-select
  18466. (org-call-for-shift-select 'backward-char))
  18467. (t (org-shiftselect-error))))
  18468. (defun org-shiftcontrolright ()
  18469. "Switch to next TODO set."
  18470. (interactive)
  18471. (cond
  18472. ((and org-support-shift-select (org-region-active-p))
  18473. (org-call-for-shift-select 'forward-word))
  18474. ((and (not (eq org-support-shift-select 'always))
  18475. (org-at-heading-p))
  18476. (org-call-with-arg 'org-todo 'nextset))
  18477. (org-support-shift-select
  18478. (org-call-for-shift-select 'forward-word))
  18479. (t (org-shiftselect-error))))
  18480. (defun org-shiftcontrolleft ()
  18481. "Switch to previous TODO set."
  18482. (interactive)
  18483. (cond
  18484. ((and org-support-shift-select (org-region-active-p))
  18485. (org-call-for-shift-select 'backward-word))
  18486. ((and (not (eq org-support-shift-select 'always))
  18487. (org-at-heading-p))
  18488. (org-call-with-arg 'org-todo 'previousset))
  18489. (org-support-shift-select
  18490. (org-call-for-shift-select 'backward-word))
  18491. (t (org-shiftselect-error))))
  18492. (defun org-shiftcontrolup (&optional n)
  18493. "Change timestamps synchronously up in CLOCK log lines.
  18494. Optional argument N tells to change by that many units."
  18495. (interactive "P")
  18496. (if (and (org-at-clock-log-p) (org-at-timestamp-p t))
  18497. (let (org-support-shift-select)
  18498. (org-clock-timestamps-up n))
  18499. (user-error "Not at a clock log")))
  18500. (defun org-shiftcontroldown (&optional n)
  18501. "Change timestamps synchronously down in CLOCK log lines.
  18502. Optional argument N tells to change by that many units."
  18503. (interactive "P")
  18504. (if (and (org-at-clock-log-p) (org-at-timestamp-p t))
  18505. (let (org-support-shift-select)
  18506. (org-clock-timestamps-down n))
  18507. (user-error "Not at a clock log")))
  18508. (defun org-ctrl-c-ret ()
  18509. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  18510. (interactive)
  18511. (cond
  18512. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  18513. (t (call-interactively 'org-insert-heading))))
  18514. (defun org-find-visible ()
  18515. (let ((s (point)))
  18516. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  18517. (get-char-property s 'invisible)))
  18518. s))
  18519. (defun org-find-invisible ()
  18520. (let ((s (point)))
  18521. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  18522. (not (get-char-property s 'invisible))))
  18523. s))
  18524. (defun org-copy-visible (beg end)
  18525. "Copy the visible parts of the region."
  18526. (interactive "r")
  18527. (let (snippets s)
  18528. (save-excursion
  18529. (save-restriction
  18530. (narrow-to-region beg end)
  18531. (setq s (goto-char (point-min)))
  18532. (while (not (= (point) (point-max)))
  18533. (goto-char (org-find-invisible))
  18534. (push (buffer-substring s (point)) snippets)
  18535. (setq s (goto-char (org-find-visible))))))
  18536. (kill-new (apply 'concat (nreverse snippets)))))
  18537. (defun org-copy-special ()
  18538. "Copy region in table or copy current subtree.
  18539. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  18540. See the individual commands for more information."
  18541. (interactive)
  18542. (call-interactively
  18543. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  18544. (defun org-cut-special ()
  18545. "Cut region in table or cut current subtree.
  18546. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  18547. See the individual commands for more information."
  18548. (interactive)
  18549. (call-interactively
  18550. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  18551. (defun org-paste-special (arg)
  18552. "Paste rectangular region into table, or past subtree relative to level.
  18553. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  18554. See the individual commands for more information."
  18555. (interactive "P")
  18556. (if (org-at-table-p)
  18557. (org-table-paste-rectangle)
  18558. (org-paste-subtree arg)))
  18559. (defsubst org-in-fixed-width-region-p ()
  18560. "Is point in a fixed-width region?"
  18561. (save-match-data
  18562. (eq 'fixed-width (org-element-type (org-element-at-point)))))
  18563. (defun org-edit-special (&optional arg)
  18564. "Call a special editor for the element at point.
  18565. When at a table, call the formula editor with `org-table-edit-formulas'.
  18566. When in a source code block, call `org-edit-src-code'.
  18567. When in a fixed-width region, call `org-edit-fixed-width-region'.
  18568. When at an #+INCLUDE keyword, visit the included file.
  18569. On a link, call `ffap' to visit the link at point.
  18570. Otherwise, return a user error."
  18571. (interactive "P")
  18572. (let ((element (org-element-at-point)))
  18573. (assert (not buffer-read-only) nil
  18574. "Buffer is read-only: %s" (buffer-name))
  18575. (case (org-element-type element)
  18576. (src-block
  18577. (if (not arg) (org-edit-src-code)
  18578. (let* ((info (org-babel-get-src-block-info))
  18579. (lang (nth 0 info))
  18580. (params (nth 2 info))
  18581. (session (cdr (assq :session params))))
  18582. (if (not session) (org-edit-src-code)
  18583. ;; At a src-block with a session and function called with
  18584. ;; an ARG: switch to the buffer related to the inferior
  18585. ;; process.
  18586. (switch-to-buffer
  18587. (funcall (intern (concat "org-babel-prep-session:" lang))
  18588. session params))))))
  18589. (keyword
  18590. (if (member (org-element-property :key element) '("INCLUDE" "SETUPFILE"))
  18591. (find-file
  18592. (org-remove-double-quotes
  18593. (car (org-split-string (org-element-property :value element)))))
  18594. (user-error "No special environment to edit here")))
  18595. (table
  18596. (if (eq (org-element-property :type element) 'table.el)
  18597. (org-edit-src-code)
  18598. (call-interactively 'org-table-edit-formulas)))
  18599. ;; Only Org tables contain `table-row' type elements.
  18600. (table-row (call-interactively 'org-table-edit-formulas))
  18601. ((example-block export-block) (org-edit-src-code))
  18602. (fixed-width (org-edit-fixed-width-region))
  18603. (otherwise
  18604. ;; No notable element at point. Though, we may be at a link,
  18605. ;; which is an object. Thus, scan deeper.
  18606. (if (eq (org-element-type (org-element-context element)) 'link)
  18607. (call-interactively 'ffap)
  18608. (user-error "No special environment to edit here"))))))
  18609. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  18610. (defun org-ctrl-c-ctrl-c (&optional arg)
  18611. "Set tags in headline, or update according to changed information at point.
  18612. This command does many different things, depending on context:
  18613. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  18614. this is what we do.
  18615. - If the cursor is on a statistics cookie, update it.
  18616. - If the cursor is in a headline, prompt for tags and insert them
  18617. into the current line, aligned to `org-tags-column'. When called
  18618. with prefix arg, realign all tags in the current buffer.
  18619. - If the cursor is in one of the special #+KEYWORD lines, this
  18620. triggers scanning the buffer for these lines and updating the
  18621. information.
  18622. - If the cursor is inside a table, realign the table. This command
  18623. works even if the automatic table editor has been turned off.
  18624. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  18625. the entire table.
  18626. - If the cursor is at a footnote reference or definition, jump to
  18627. the corresponding definition or references, respectively.
  18628. - If the cursor is a the beginning of a dynamic block, update it.
  18629. - If the current buffer is a capture buffer, close note and file it.
  18630. - If the cursor is on a <<<target>>>, update radio targets and
  18631. corresponding links in this buffer.
  18632. - If the cursor is on a numbered item in a plain list, renumber the
  18633. ordered list.
  18634. - If the cursor is on a checkbox, toggle it.
  18635. - If the cursor is on a code block, evaluate it. The variable
  18636. `org-confirm-babel-evaluate' can be used to control prompting
  18637. before code block evaluation, by default every code block
  18638. evaluation requires confirmation. Code block evaluation can be
  18639. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  18640. (interactive "P")
  18641. (cond
  18642. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  18643. org-occur-highlights
  18644. org-latex-fragment-image-overlays)
  18645. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  18646. (org-remove-occur-highlights)
  18647. (org-remove-latex-fragment-image-overlays)
  18648. (message "Temporary highlights/overlays removed from current buffer"))
  18649. ((and (local-variable-p 'org-finish-function (current-buffer))
  18650. (fboundp org-finish-function))
  18651. (funcall org-finish-function))
  18652. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  18653. (t
  18654. (let* ((context (org-element-context)) (type (org-element-type context)))
  18655. ;; Test if point is within a blank line.
  18656. (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
  18657. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  18658. (user-error "C-c C-c can do nothing useful at this location"))
  18659. (case type
  18660. ;; When at a link, act according to the parent instead.
  18661. (link (setq context (org-element-property :parent context))
  18662. (setq type (org-element-type context)))
  18663. ;; Unsupported object types: refer to the first supported
  18664. ;; element or object containing it.
  18665. ((bold code entity export-snippet inline-babel-call inline-src-block
  18666. italic latex-fragment line-break macro strike-through subscript
  18667. superscript underline verbatim)
  18668. (while (and (setq context (org-element-property :parent context))
  18669. (not (memq (setq type (org-element-type context))
  18670. '(radio-target paragraph verse-block
  18671. table-cell)))))))
  18672. ;; For convenience: at the first line of a paragraph on the
  18673. ;; same line as an item, apply function on that item instead.
  18674. (when (eq type 'paragraph)
  18675. (let ((parent (org-element-property :parent context)))
  18676. (when (and (eq (org-element-type parent) 'item)
  18677. (= (point-at-bol) (org-element-property :begin parent)))
  18678. (setq context parent type 'item))))
  18679. ;; Act according to type of element or object at point.
  18680. (case type
  18681. (clock (org-clock-update-time-maybe))
  18682. (dynamic-block
  18683. (save-excursion
  18684. (goto-char (org-element-property :post-affiliated context))
  18685. (org-update-dblock)))
  18686. (footnote-definition
  18687. (goto-char (org-element-property :post-affiliated context))
  18688. (call-interactively 'org-footnote-action))
  18689. (footnote-reference (call-interactively 'org-footnote-action))
  18690. ((headline inlinetask)
  18691. (save-excursion (goto-char (org-element-property :begin context))
  18692. (call-interactively 'org-set-tags)))
  18693. (item
  18694. ;; At an item: a double C-u set checkbox to "[-]"
  18695. ;; unconditionally, whereas a single one will toggle its
  18696. ;; presence. Without a universal argument, if the item
  18697. ;; has a checkbox, toggle it. Otherwise repair the list.
  18698. (let* ((box (org-element-property :checkbox context))
  18699. (struct (org-element-property :structure context))
  18700. (old-struct (copy-tree struct))
  18701. (parents (org-list-parents-alist struct))
  18702. (prevs (org-list-prevs-alist struct))
  18703. (orderedp (org-not-nil (org-entry-get nil "ORDERED"))))
  18704. (org-list-set-checkbox
  18705. (org-element-property :begin context) struct
  18706. (cond ((equal arg '(16)) "[-]")
  18707. ((and (not box) (equal arg '(4))) "[ ]")
  18708. ((or (not box) (equal arg '(4))) nil)
  18709. ((eq box 'on) "[ ]")
  18710. (t "[X]")))
  18711. ;; Mimic `org-list-write-struct' but with grabbing
  18712. ;; a return value from `org-list-struct-fix-box'.
  18713. (org-list-struct-fix-ind struct parents 2)
  18714. (org-list-struct-fix-item-end struct)
  18715. (org-list-struct-fix-bul struct prevs)
  18716. (org-list-struct-fix-ind struct parents)
  18717. (let ((block-item
  18718. (org-list-struct-fix-box struct parents prevs orderedp)))
  18719. (if (and box (equal struct old-struct))
  18720. (if (equal arg '(16))
  18721. (message "Checkboxes already reset")
  18722. (user-error "Cannot toggle this checkbox: %s"
  18723. (if (eq box 'on)
  18724. "all subitems checked"
  18725. "unchecked subitems")))
  18726. (org-list-struct-apply-struct struct old-struct)
  18727. (org-update-checkbox-count-maybe))
  18728. (when block-item
  18729. (message "Checkboxes were removed due to empty box at line %d"
  18730. (org-current-line block-item))))))
  18731. (keyword
  18732. (let ((org-inhibit-startup-visibility-stuff t)
  18733. (org-startup-align-all-tables nil))
  18734. (when (boundp 'org-table-coordinate-overlays)
  18735. (mapc 'delete-overlay org-table-coordinate-overlays)
  18736. (setq org-table-coordinate-overlays nil))
  18737. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  18738. (message "Local setup has been refreshed"))
  18739. (plain-list
  18740. ;; At a plain list, with a double C-u argument, set
  18741. ;; checkboxes of each item to "[-]", whereas a single one
  18742. ;; will toggle their presence according to the state of the
  18743. ;; first item in the list. Without an argument, repair the
  18744. ;; list.
  18745. (let* ((begin (org-element-property :contents-begin context))
  18746. (beginm (move-marker (make-marker) begin))
  18747. (struct (org-element-property :structure context))
  18748. (old-struct (copy-tree struct))
  18749. (first-box (save-excursion
  18750. (goto-char begin)
  18751. (looking-at org-list-full-item-re)
  18752. (match-string-no-properties 3)))
  18753. (new-box (cond ((equal arg '(16)) "[-]")
  18754. ((equal arg '(4)) (unless first-box "[ ]"))
  18755. ((equal first-box "[X]") "[ ]")
  18756. (t "[X]"))))
  18757. (cond
  18758. (arg
  18759. (mapc (lambda (pos) (org-list-set-checkbox pos struct new-box))
  18760. (org-list-get-all-items
  18761. begin struct (org-list-prevs-alist struct))))
  18762. ((and first-box (eq (point) begin))
  18763. ;; For convenience, when point is at bol on the first
  18764. ;; item of the list and no argument is provided, simply
  18765. ;; toggle checkbox of that item, if any.
  18766. (org-list-set-checkbox begin struct new-box)))
  18767. (org-list-write-struct
  18768. struct (org-list-parents-alist struct) old-struct)
  18769. (org-update-checkbox-count-maybe)
  18770. (save-excursion (goto-char beginm) (org-list-send-list 'maybe))))
  18771. ((property-drawer node-property)
  18772. (call-interactively 'org-property-action))
  18773. ((radio-target target)
  18774. (call-interactively 'org-update-radio-target-regexp))
  18775. (statistics-cookie
  18776. (call-interactively 'org-update-statistics-cookies))
  18777. ((table table-cell table-row)
  18778. ;; At a table, recalculate every field and align it. Also
  18779. ;; send the table if necessary. If the table has
  18780. ;; a `table.el' type, just give up. At a table row or
  18781. ;; cell, maybe recalculate line but always align table.
  18782. (if (eq (org-element-property :type context) 'table.el)
  18783. (message "Use C-c ' to edit table.el tables")
  18784. (let ((org-enable-table-editor t))
  18785. (if (or (eq type 'table)
  18786. ;; Check if point is at a TBLFM line.
  18787. (and (eq type 'table-row)
  18788. (= (point) (org-element-property :end context))))
  18789. (save-excursion
  18790. (if (org-at-TBLFM-p)
  18791. (progn (require 'org-table)
  18792. (org-table-calc-current-TBLFM))
  18793. (goto-char (org-element-property :contents-begin context))
  18794. (org-call-with-arg 'org-table-recalculate (or arg t))
  18795. (orgtbl-send-table 'maybe)))
  18796. (org-table-maybe-eval-formula)
  18797. (cond (arg (call-interactively 'org-table-recalculate))
  18798. ((org-table-maybe-recalculate-line))
  18799. (t (org-table-align)))))))
  18800. (timestamp (org-timestamp-change 0 'day))
  18801. (otherwise
  18802. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  18803. (user-error
  18804. "C-c C-c can do nothing useful at this location")))))))))
  18805. (defun org-mode-restart ()
  18806. (interactive)
  18807. (let ((indent-status (org-bound-and-true-p org-indent-mode)))
  18808. (funcall major-mode)
  18809. (hack-local-variables)
  18810. (when (and indent-status (not (org-bound-and-true-p org-indent-mode)))
  18811. (org-indent-mode -1)))
  18812. (message "%s restarted" major-mode))
  18813. (defun org-kill-note-or-show-branches ()
  18814. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  18815. (interactive)
  18816. (if (not org-finish-function)
  18817. (progn
  18818. (hide-subtree)
  18819. (call-interactively 'show-branches))
  18820. (let ((org-note-abort t))
  18821. (funcall org-finish-function))))
  18822. (defun org-open-line (n)
  18823. "Insert a new row in tables, call `open-line' elsewhere.
  18824. If `org-special-ctrl-o' is nil, just call `open-line' everywhere."
  18825. (interactive "*p")
  18826. (cond
  18827. ((not org-special-ctrl-o)
  18828. (open-line n))
  18829. ((org-at-table-p)
  18830. (org-table-insert-row))
  18831. (t
  18832. (open-line n))))
  18833. (defun org-return (&optional indent)
  18834. "Goto next table row or insert a newline.
  18835. Calls `org-table-next-row' or `newline', depending on context.
  18836. See the individual commands for more information."
  18837. (interactive)
  18838. (let (org-ts-what)
  18839. (cond
  18840. ((or (bobp) (org-in-src-block-p))
  18841. (if indent (newline-and-indent) (newline)))
  18842. ((org-at-table-p)
  18843. (org-table-justify-field-maybe)
  18844. (call-interactively 'org-table-next-row))
  18845. ;; when `newline-and-indent' is called within a list, make sure
  18846. ;; text moved stays inside the item.
  18847. ((and (org-in-item-p) indent)
  18848. (if (and (org-at-item-p) (>= (point) (match-end 0)))
  18849. (progn
  18850. (save-match-data (newline))
  18851. (org-indent-line-to (length (match-string 0))))
  18852. (let ((ind (org-get-indentation)))
  18853. (newline)
  18854. (if (org-looking-back org-list-end-re)
  18855. (org-indent-line)
  18856. (org-indent-line-to ind)))))
  18857. ((and org-return-follows-link
  18858. (org-at-timestamp-p t)
  18859. (not (eq org-ts-what 'after)))
  18860. (org-follow-timestamp-link))
  18861. ((and org-return-follows-link
  18862. (let ((tprop (get-text-property (point) 'face)))
  18863. (or (eq tprop 'org-link)
  18864. (and (listp tprop) (memq 'org-link tprop)))))
  18865. (call-interactively 'org-open-at-point))
  18866. ((and (org-at-heading-p)
  18867. (looking-at
  18868. (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
  18869. (org-show-entry)
  18870. (end-of-line 1)
  18871. (newline))
  18872. (t (if indent (newline-and-indent) (newline))))))
  18873. (defun org-return-indent ()
  18874. "Goto next table row or insert a newline and indent.
  18875. Calls `org-table-next-row' or `newline-and-indent', depending on
  18876. context. See the individual commands for more information."
  18877. (interactive)
  18878. (org-return t))
  18879. (defun org-ctrl-c-star ()
  18880. "Compute table, or change heading status of lines.
  18881. Calls `org-table-recalculate' or `org-toggle-heading',
  18882. depending on context."
  18883. (interactive)
  18884. (cond
  18885. ((org-at-table-p)
  18886. (call-interactively 'org-table-recalculate))
  18887. (t
  18888. ;; Convert all lines in region to list items
  18889. (call-interactively 'org-toggle-heading))))
  18890. (defun org-ctrl-c-minus ()
  18891. "Insert separator line in table or modify bullet status of line.
  18892. Also turns a plain line or a region of lines into list items.
  18893. Calls `org-table-insert-hline', `org-toggle-item', or
  18894. `org-cycle-list-bullet', depending on context."
  18895. (interactive)
  18896. (cond
  18897. ((org-at-table-p)
  18898. (call-interactively 'org-table-insert-hline))
  18899. ((org-region-active-p)
  18900. (call-interactively 'org-toggle-item))
  18901. ((org-in-item-p)
  18902. (call-interactively 'org-cycle-list-bullet))
  18903. (t
  18904. (call-interactively 'org-toggle-item))))
  18905. (defun org-toggle-item (arg)
  18906. "Convert headings or normal lines to items, items to normal lines.
  18907. If there is no active region, only the current line is considered.
  18908. If the first non blank line in the region is a headline, convert
  18909. all headlines to items, shifting text accordingly.
  18910. If it is an item, convert all items to normal lines.
  18911. If it is normal text, change region into a list of items.
  18912. With a prefix argument ARG, change the region in a single item."
  18913. (interactive "P")
  18914. (let ((shift-text
  18915. (function
  18916. ;; Shift text in current section to IND, from point to END.
  18917. ;; The function leaves point to END line.
  18918. (lambda (ind end)
  18919. (let ((min-i 1000) (end (copy-marker end)))
  18920. ;; First determine the minimum indentation (MIN-I) of
  18921. ;; the text.
  18922. (save-excursion
  18923. (catch 'exit
  18924. (while (< (point) end)
  18925. (let ((i (org-get-indentation)))
  18926. (cond
  18927. ;; Skip blank lines and inline tasks.
  18928. ((looking-at "^[ \t]*$"))
  18929. ((looking-at org-outline-regexp-bol))
  18930. ;; We can't find less than 0 indentation.
  18931. ((zerop i) (throw 'exit (setq min-i 0)))
  18932. ((< i min-i) (setq min-i i))))
  18933. (forward-line))))
  18934. ;; Then indent each line so that a line indented to
  18935. ;; MIN-I becomes indented to IND. Ignore blank lines
  18936. ;; and inline tasks in the process.
  18937. (let ((delta (- ind min-i)))
  18938. (while (< (point) end)
  18939. (unless (or (looking-at "^[ \t]*$")
  18940. (looking-at org-outline-regexp-bol))
  18941. (org-indent-line-to (+ (org-get-indentation) delta)))
  18942. (forward-line)))))))
  18943. (skip-blanks
  18944. (function
  18945. ;; Return beginning of first non-blank line, starting from
  18946. ;; line at POS.
  18947. (lambda (pos)
  18948. (save-excursion
  18949. (goto-char pos)
  18950. (skip-chars-forward " \r\t\n")
  18951. (point-at-bol)))))
  18952. beg end)
  18953. ;; Determine boundaries of changes.
  18954. (if (org-region-active-p)
  18955. (setq beg (funcall skip-blanks (region-beginning))
  18956. end (copy-marker (region-end)))
  18957. (setq beg (funcall skip-blanks (point-at-bol))
  18958. end (copy-marker (point-at-eol))))
  18959. ;; Depending on the starting line, choose an action on the text
  18960. ;; between BEG and END.
  18961. (org-with-limited-levels
  18962. (save-excursion
  18963. (goto-char beg)
  18964. (cond
  18965. ;; Case 1. Start at an item: de-itemize. Note that it only
  18966. ;; happens when a region is active: `org-ctrl-c-minus'
  18967. ;; would call `org-cycle-list-bullet' otherwise.
  18968. ((org-at-item-p)
  18969. (while (< (point) end)
  18970. (when (org-at-item-p)
  18971. (skip-chars-forward " \t")
  18972. (delete-region (point) (match-end 0)))
  18973. (forward-line)))
  18974. ;; Case 2. Start at an heading: convert to items.
  18975. ((org-at-heading-p)
  18976. (let* ((bul (org-list-bullet-string "-"))
  18977. (bul-len (length bul))
  18978. ;; Indentation of the first heading. It should be
  18979. ;; relative to the indentation of its parent, if any.
  18980. (start-ind (save-excursion
  18981. (cond
  18982. ((not org-adapt-indentation) 0)
  18983. ((not (outline-previous-heading)) 0)
  18984. (t (length (match-string 0))))))
  18985. ;; Level of first heading. Further headings will be
  18986. ;; compared to it to determine hierarchy in the list.
  18987. (ref-level (org-reduced-level (org-outline-level))))
  18988. (while (< (point) end)
  18989. (let* ((level (org-reduced-level (org-outline-level)))
  18990. (delta (max 0 (- level ref-level))))
  18991. ;; If current headline is less indented than the first
  18992. ;; one, set it as reference, in order to preserve
  18993. ;; subtrees.
  18994. (when (< level ref-level) (setq ref-level level))
  18995. (replace-match bul t t)
  18996. (org-indent-line-to (+ start-ind (* delta bul-len)))
  18997. ;; Ensure all text down to END (or SECTION-END) belongs
  18998. ;; to the newly created item.
  18999. (let ((section-end (save-excursion
  19000. (or (outline-next-heading) (point)))))
  19001. (forward-line)
  19002. (funcall shift-text
  19003. (+ start-ind (* (1+ delta) bul-len))
  19004. (min end section-end)))))))
  19005. ;; Case 3. Normal line with ARG: make the first line of region
  19006. ;; an item, and shift indentation of others lines to
  19007. ;; set them as item's body.
  19008. (arg (let* ((bul (org-list-bullet-string "-"))
  19009. (bul-len (length bul))
  19010. (ref-ind (org-get-indentation)))
  19011. (skip-chars-forward " \t")
  19012. (insert bul)
  19013. (forward-line)
  19014. (while (< (point) end)
  19015. ;; Ensure that lines less indented than first one
  19016. ;; still get included in item body.
  19017. (funcall shift-text
  19018. (+ ref-ind bul-len)
  19019. (min end (save-excursion (or (outline-next-heading)
  19020. (point)))))
  19021. (forward-line))))
  19022. ;; Case 4. Normal line without ARG: turn each non-item line
  19023. ;; into an item.
  19024. (t
  19025. (while (< (point) end)
  19026. (unless (or (org-at-heading-p) (org-at-item-p))
  19027. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  19028. (replace-match
  19029. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  19030. (forward-line))))))))
  19031. (defun org-toggle-heading (&optional nstars)
  19032. "Convert headings to normal text, or items or text to headings.
  19033. If there is no active region, only convert the current line.
  19034. With a \\[universal-argument] prefix, convert the whole list at
  19035. point into heading.
  19036. In a region:
  19037. - If the first non blank line is a headline, remove the stars
  19038. from all headlines in the region.
  19039. - If it is a normal line, turn each and every normal line (i.e.,
  19040. not an heading or an item) in the region into headings. If you
  19041. want to convert only the first line of this region, use one
  19042. universal prefix argument.
  19043. - If it is a plain list item, turn all plain list items into headings.
  19044. When converting a line into a heading, the number of stars is chosen
  19045. such that the lines become children of the current entry. However,
  19046. when a numeric prefix argument is given, its value determines the
  19047. number of stars to add."
  19048. (interactive "P")
  19049. (let ((skip-blanks
  19050. (function
  19051. ;; Return beginning of first non-blank line, starting from
  19052. ;; line at POS.
  19053. (lambda (pos)
  19054. (save-excursion
  19055. (goto-char pos)
  19056. (while (org-at-comment-p) (forward-line))
  19057. (skip-chars-forward " \r\t\n")
  19058. (point-at-bol)))))
  19059. beg end toggled)
  19060. ;; Determine boundaries of changes. If a universal prefix has
  19061. ;; been given, put the list in a region. If region ends at a bol,
  19062. ;; do not consider the last line to be in the region.
  19063. (when (and current-prefix-arg (org-at-item-p))
  19064. (if (listp current-prefix-arg) (setq current-prefix-arg 1))
  19065. (org-mark-element))
  19066. (if (org-region-active-p)
  19067. (setq beg (funcall skip-blanks (region-beginning))
  19068. end (copy-marker (save-excursion
  19069. (goto-char (region-end))
  19070. (if (bolp) (point) (point-at-eol)))))
  19071. (setq beg (funcall skip-blanks (point-at-bol))
  19072. end (copy-marker (point-at-eol))))
  19073. ;; Ensure inline tasks don't count as headings.
  19074. (org-with-limited-levels
  19075. (save-excursion
  19076. (goto-char beg)
  19077. (cond
  19078. ;; Case 1. Started at an heading: de-star headings.
  19079. ((org-at-heading-p)
  19080. (while (< (point) end)
  19081. (when (org-at-heading-p t)
  19082. (looking-at org-outline-regexp) (replace-match "")
  19083. (setq toggled t))
  19084. (forward-line)))
  19085. ;; Case 2. Started at an item: change items into headlines.
  19086. ;; One star will be added by `org-list-to-subtree'.
  19087. ((org-at-item-p)
  19088. (let* ((stars (make-string
  19089. ;; subtract the star that will be added again by
  19090. ;; `org-list-to-subtree'
  19091. (if (numberp nstars) (1- nstars)
  19092. (or (org-current-level) 0))
  19093. ?*))
  19094. (add-stars
  19095. (cond (nstars "") ; stars from prefix only
  19096. ((equal stars "") "") ; before first heading
  19097. (org-odd-levels-only "*") ; inside heading, odd
  19098. (t "")))) ; inside heading, oddeven
  19099. (while (< (point) end)
  19100. (when (org-at-item-p)
  19101. ;; Pay attention to cases when region ends before list.
  19102. (let* ((struct (org-list-struct))
  19103. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  19104. (save-restriction
  19105. (narrow-to-region (point) list-end)
  19106. (insert
  19107. (org-list-to-subtree
  19108. (org-list-parse-list t)
  19109. '(:istart (concat stars add-stars (funcall get-stars depth))
  19110. :icount (concat stars add-stars (funcall get-stars depth)))))))
  19111. (setq toggled t))
  19112. (forward-line))))
  19113. ;; Case 3. Started at normal text: make every line an heading,
  19114. ;; skipping headlines and items.
  19115. (t (let* ((stars
  19116. (make-string
  19117. (if (numberp nstars) nstars (or (org-current-level) 0)) ?*))
  19118. (add-stars
  19119. (cond (nstars "") ; stars from prefix only
  19120. ((equal stars "") "*") ; before first heading
  19121. (org-odd-levels-only "**") ; inside heading, odd
  19122. (t "*"))) ; inside heading, oddeven
  19123. (rpl (concat stars add-stars " "))
  19124. (lend (if (listp nstars) (save-excursion (end-of-line) (point)))))
  19125. (while (< (point) (if (equal nstars '(4)) lend end))
  19126. (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
  19127. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  19128. (replace-match (concat rpl (match-string 2))) (setq toggled t))
  19129. (forward-line)))))))
  19130. (unless toggled (message "Cannot toggle heading from here"))))
  19131. (defun org-meta-return (&optional arg)
  19132. "Insert a new heading or wrap a region in a table.
  19133. Calls `org-insert-heading' or `org-table-wrap-region', depending
  19134. on context. See the individual commands for more information."
  19135. (interactive "P")
  19136. (org-check-before-invisible-edit 'insert)
  19137. (or (run-hook-with-args-until-success 'org-metareturn-hook)
  19138. (let* ((element (org-element-at-point))
  19139. (type (org-element-type element)))
  19140. (when (eq type 'table-row)
  19141. (setq element (org-element-property :parent element))
  19142. (setq type 'table))
  19143. (if (and (eq type 'table)
  19144. (eq (org-element-property :type element) 'org)
  19145. (>= (point) (org-element-property :contents-begin element))
  19146. (< (point) (org-element-property :contents-end element)))
  19147. (call-interactively 'org-table-wrap-region)
  19148. (call-interactively 'org-insert-heading)))))
  19149. ;;; Menu entries
  19150. (defsubst org-in-subtree-not-table-p ()
  19151. "Are we in a subtree and not in a table?"
  19152. (and (not (org-before-first-heading-p))
  19153. (not (org-at-table-p))))
  19154. ;; Define the Org-mode menus
  19155. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  19156. '("Tbl"
  19157. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  19158. ["Next Field" org-cycle (org-at-table-p)]
  19159. ["Previous Field" org-shifttab (org-at-table-p)]
  19160. ["Next Row" org-return (org-at-table-p)]
  19161. "--"
  19162. ["Blank Field" org-table-blank-field (org-at-table-p)]
  19163. ["Edit Field" org-table-edit-field (org-at-table-p)]
  19164. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  19165. "--"
  19166. ("Column"
  19167. ["Move Column Left" org-metaleft (org-at-table-p)]
  19168. ["Move Column Right" org-metaright (org-at-table-p)]
  19169. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  19170. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  19171. ("Row"
  19172. ["Move Row Up" org-metaup (org-at-table-p)]
  19173. ["Move Row Down" org-metadown (org-at-table-p)]
  19174. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  19175. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  19176. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  19177. "--"
  19178. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  19179. ("Rectangle"
  19180. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  19181. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  19182. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  19183. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  19184. "--"
  19185. ("Calculate"
  19186. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  19187. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  19188. ["Edit Formulas" org-edit-special (org-at-table-p)]
  19189. "--"
  19190. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  19191. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  19192. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  19193. "--"
  19194. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  19195. "--"
  19196. ["Sum Column/Rectangle" org-table-sum
  19197. (or (org-at-table-p) (org-region-active-p))]
  19198. ["Which Column?" org-table-current-column (org-at-table-p)])
  19199. ["Debug Formulas"
  19200. org-table-toggle-formula-debugger
  19201. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  19202. ["Show Col/Row Numbers"
  19203. org-table-toggle-coordinate-overlays
  19204. :style toggle
  19205. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  19206. "--"
  19207. ["Create" org-table-create (and (not (org-at-table-p))
  19208. org-enable-table-editor)]
  19209. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  19210. ["Import from File" org-table-import (not (org-at-table-p))]
  19211. ["Export to File" org-table-export (org-at-table-p)]
  19212. "--"
  19213. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  19214. (easy-menu-define org-org-menu org-mode-map "Org menu"
  19215. '("Org"
  19216. ("Show/Hide"
  19217. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  19218. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  19219. ["Sparse Tree..." org-sparse-tree t]
  19220. ["Reveal Context" org-reveal t]
  19221. ["Show All" show-all t]
  19222. "--"
  19223. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  19224. "--"
  19225. ["New Heading" org-insert-heading t]
  19226. ("Navigate Headings"
  19227. ["Up" outline-up-heading t]
  19228. ["Next" outline-next-visible-heading t]
  19229. ["Previous" outline-previous-visible-heading t]
  19230. ["Next Same Level" outline-forward-same-level t]
  19231. ["Previous Same Level" outline-backward-same-level t]
  19232. "--"
  19233. ["Jump" org-goto t])
  19234. ("Edit Structure"
  19235. ["Refile Subtree" org-refile (org-in-subtree-not-table-p)]
  19236. "--"
  19237. ["Move Subtree Up" org-shiftmetaup (org-in-subtree-not-table-p)]
  19238. ["Move Subtree Down" org-shiftmetadown (org-in-subtree-not-table-p)]
  19239. "--"
  19240. ["Copy Subtree" org-copy-special (org-in-subtree-not-table-p)]
  19241. ["Cut Subtree" org-cut-special (org-in-subtree-not-table-p)]
  19242. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  19243. "--"
  19244. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  19245. "--"
  19246. ["Copy visible text" org-copy-visible t]
  19247. "--"
  19248. ["Promote Heading" org-metaleft (org-in-subtree-not-table-p)]
  19249. ["Promote Subtree" org-shiftmetaleft (org-in-subtree-not-table-p)]
  19250. ["Demote Heading" org-metaright (org-in-subtree-not-table-p)]
  19251. ["Demote Subtree" org-shiftmetaright (org-in-subtree-not-table-p)]
  19252. "--"
  19253. ["Sort Region/Children" org-sort t]
  19254. "--"
  19255. ["Convert to odd levels" org-convert-to-odd-levels t]
  19256. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  19257. ("Editing"
  19258. ["Emphasis..." org-emphasize t]
  19259. ["Edit Source Example" org-edit-special t]
  19260. "--"
  19261. ["Footnote new/jump" org-footnote-action t]
  19262. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  19263. ("Archive"
  19264. ["Archive (default method)" org-archive-subtree-default (org-in-subtree-not-table-p)]
  19265. "--"
  19266. ["Move Subtree to Archive file" org-archive-subtree (org-in-subtree-not-table-p)]
  19267. ["Toggle ARCHIVE tag" org-toggle-archive-tag (org-in-subtree-not-table-p)]
  19268. ["Move subtree to Archive sibling" org-archive-to-archive-sibling (org-in-subtree-not-table-p)]
  19269. )
  19270. "--"
  19271. ("Hyperlinks"
  19272. ["Store Link (Global)" org-store-link t]
  19273. ["Find existing link to here" org-occur-link-in-agenda-files t]
  19274. ["Insert Link" org-insert-link t]
  19275. ["Follow Link" org-open-at-point t]
  19276. "--"
  19277. ["Next link" org-next-link t]
  19278. ["Previous link" org-previous-link t]
  19279. "--"
  19280. ["Descriptive Links"
  19281. org-toggle-link-display
  19282. :style radio
  19283. :selected org-descriptive-links
  19284. ]
  19285. ["Literal Links"
  19286. org-toggle-link-display
  19287. :style radio
  19288. :selected (not org-descriptive-links)])
  19289. "--"
  19290. ("TODO Lists"
  19291. ["TODO/DONE/-" org-todo t]
  19292. ("Select keyword"
  19293. ["Next keyword" org-shiftright (org-at-heading-p)]
  19294. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  19295. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  19296. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  19297. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  19298. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  19299. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  19300. "--"
  19301. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  19302. :selected org-enforce-todo-dependencies :style toggle :active t]
  19303. "Settings for tree at point"
  19304. ["Do Children sequentially" org-toggle-ordered-property :style radio
  19305. :selected (org-entry-get nil "ORDERED")
  19306. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  19307. ["Do Children parallel" org-toggle-ordered-property :style radio
  19308. :selected (not (org-entry-get nil "ORDERED"))
  19309. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  19310. "--"
  19311. ["Set Priority" org-priority t]
  19312. ["Priority Up" org-shiftup t]
  19313. ["Priority Down" org-shiftdown t]
  19314. "--"
  19315. ["Get news from all feeds" org-feed-update-all t]
  19316. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  19317. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  19318. ("TAGS and Properties"
  19319. ["Set Tags" org-set-tags-command (not (org-before-first-heading-p))]
  19320. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  19321. "--"
  19322. ["Set property" org-set-property (not (org-before-first-heading-p))]
  19323. ["Column view of properties" org-columns t]
  19324. ["Insert Column View DBlock" org-insert-columns-dblock t])
  19325. ("Dates and Scheduling"
  19326. ["Timestamp" org-time-stamp (not (org-before-first-heading-p))]
  19327. ["Timestamp (inactive)" org-time-stamp-inactive (not (org-before-first-heading-p))]
  19328. ("Change Date"
  19329. ["1 Day Later" org-shiftright (org-at-timestamp-p)]
  19330. ["1 Day Earlier" org-shiftleft (org-at-timestamp-p)]
  19331. ["1 ... Later" org-shiftup (org-at-timestamp-p)]
  19332. ["1 ... Earlier" org-shiftdown (org-at-timestamp-p)])
  19333. ["Compute Time Range" org-evaluate-time-range t]
  19334. ["Schedule Item" org-schedule (not (org-before-first-heading-p))]
  19335. ["Deadline" org-deadline (not (org-before-first-heading-p))]
  19336. "--"
  19337. ["Custom time format" org-toggle-time-stamp-overlays
  19338. :style radio :selected org-display-custom-times]
  19339. "--"
  19340. ["Goto Calendar" org-goto-calendar t]
  19341. ["Date from Calendar" org-date-from-calendar t]
  19342. "--"
  19343. ["Start/Restart Timer" org-timer-start t]
  19344. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  19345. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  19346. ["Insert Timer String" org-timer t]
  19347. ["Insert Timer Item" org-timer-item t])
  19348. ("Logging work"
  19349. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  19350. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  19351. ["Clock out" org-clock-out t]
  19352. ["Clock cancel" org-clock-cancel t]
  19353. "--"
  19354. ["Mark as default task" org-clock-mark-default-task t]
  19355. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  19356. ["Goto running clock" org-clock-goto t]
  19357. "--"
  19358. ["Display times" org-clock-display t]
  19359. ["Create clock table" org-clock-report t]
  19360. "--"
  19361. ["Record DONE time"
  19362. (progn (setq org-log-done (not org-log-done))
  19363. (message "Switching to %s will %s record a timestamp"
  19364. (car org-done-keywords)
  19365. (if org-log-done "automatically" "not")))
  19366. :style toggle :selected org-log-done])
  19367. "--"
  19368. ["Agenda Command..." org-agenda t]
  19369. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  19370. ("File List for Agenda")
  19371. ("Special views current file"
  19372. ["TODO Tree" org-show-todo-tree t]
  19373. ["Check Deadlines" org-check-deadlines t]
  19374. ["Timeline" org-timeline t]
  19375. ["Tags/Property tree" org-match-sparse-tree t])
  19376. "--"
  19377. ["Export/Publish..." org-export-dispatch t]
  19378. ("LaTeX"
  19379. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  19380. :selected org-cdlatex-mode]
  19381. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  19382. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  19383. ["Modify math symbol" org-cdlatex-math-modify
  19384. (org-inside-LaTeX-fragment-p)]
  19385. ["Insert citation" org-reftex-citation t]
  19386. "--"
  19387. ["Template for BEAMER" (org-beamer-insert-options-template) t])
  19388. "--"
  19389. ("MobileOrg"
  19390. ["Push Files and Views" org-mobile-push t]
  19391. ["Get Captured and Flagged" org-mobile-pull t]
  19392. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  19393. "--"
  19394. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  19395. "--"
  19396. ("Documentation"
  19397. ["Show Version" org-version t]
  19398. ["Info Documentation" org-info t])
  19399. ("Customize"
  19400. ["Browse Org Group" org-customize t]
  19401. "--"
  19402. ["Expand This Menu" org-create-customize-menu
  19403. (fboundp 'customize-menu-create)])
  19404. ["Send bug report" org-submit-bug-report t]
  19405. "--"
  19406. ("Refresh/Reload"
  19407. ["Refresh setup current buffer" org-mode-restart t]
  19408. ["Reload Org (after update)" org-reload t]
  19409. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x !"])
  19410. ))
  19411. (defun org-info (&optional node)
  19412. "Read documentation for Org-mode in the info system.
  19413. With optional NODE, go directly to that node."
  19414. (interactive)
  19415. (info (format "(org)%s" (or node ""))))
  19416. ;;;###autoload
  19417. (defun org-submit-bug-report ()
  19418. "Submit a bug report on Org-mode via mail.
  19419. Don't hesitate to report any problems or inaccurate documentation.
  19420. If you don't have setup sending mail from (X)Emacs, please copy the
  19421. output buffer into your mail program, as it gives us important
  19422. information about your Org-mode version and configuration."
  19423. (interactive)
  19424. (require 'reporter)
  19425. (org-load-modules-maybe)
  19426. (org-require-autoloaded-modules)
  19427. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  19428. (reporter-submit-bug-report
  19429. "emacs-orgmode@gnu.org"
  19430. (org-version nil 'full)
  19431. (let (list)
  19432. (save-window-excursion
  19433. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  19434. (delete-other-windows)
  19435. (erase-buffer)
  19436. (insert "You are about to submit a bug report to the Org-mode mailing list.
  19437. We would like to add your full Org-mode and Outline configuration to the
  19438. bug report. This greatly simplifies the work of the maintainer and
  19439. other experts on the mailing list.
  19440. HOWEVER, some variables you have customized may contain private
  19441. information. The names of customers, colleagues, or friends, might
  19442. appear in the form of file names, tags, todo states, or search strings.
  19443. If you answer yes to the prompt, you might want to check and remove
  19444. such private information before sending the email.")
  19445. (add-text-properties (point-min) (point-max) '(face org-warning))
  19446. (when (yes-or-no-p "Include your Org-mode configuration ")
  19447. (mapatoms
  19448. (lambda (v)
  19449. (and (boundp v)
  19450. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  19451. (or (and (symbol-value v)
  19452. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  19453. (and
  19454. (get v 'custom-type) (get v 'standard-value)
  19455. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  19456. (push v list)))))
  19457. (kill-buffer (get-buffer "*Warn about privacy*"))
  19458. list))
  19459. nil nil
  19460. "Remember to cover the basics, that is, what you expected to happen and
  19461. what in fact did happen. You don't know how to make a good report? See
  19462. http://orgmode.org/manual/Feedback.html#Feedback
  19463. Your bug report will be posted to the Org-mode mailing list.
  19464. ------------------------------------------------------------------------")
  19465. (save-excursion
  19466. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  19467. (replace-match "\\1Bug: \\3 [\\2]")))))
  19468. (defun org-install-agenda-files-menu ()
  19469. (let ((bl (buffer-list)))
  19470. (save-excursion
  19471. (while bl
  19472. (set-buffer (pop bl))
  19473. (if (derived-mode-p 'org-mode) (setq bl nil)))
  19474. (when (derived-mode-p 'org-mode)
  19475. (easy-menu-change
  19476. '("Org") "File List for Agenda"
  19477. (append
  19478. (list
  19479. ["Edit File List" (org-edit-agenda-file-list) t]
  19480. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  19481. ["Remove Current File from List" org-remove-file t]
  19482. ["Cycle through agenda files" org-cycle-agenda-files t]
  19483. ["Occur in all agenda files" org-occur-in-agenda-files t]
  19484. "--")
  19485. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  19486. ;;;; Documentation
  19487. (defun org-require-autoloaded-modules ()
  19488. (interactive)
  19489. (mapc 'require
  19490. '(org-agenda org-archive org-attach org-clock org-colview org-id
  19491. org-table org-timer)))
  19492. ;;;###autoload
  19493. (defun org-reload (&optional uncompiled)
  19494. "Reload all org lisp files.
  19495. With prefix arg UNCOMPILED, load the uncompiled versions."
  19496. (interactive "P")
  19497. (require 'loadhist)
  19498. (let* ((org-dir (org-find-library-dir "org"))
  19499. (contrib-dir (or (org-find-library-dir "org-contribdir") org-dir))
  19500. (feature-re "^\\(org\\|ob\\|ox\\)\\(-.*\\)?")
  19501. (remove-re (mapconcat 'identity
  19502. (mapcar (lambda (f) (concat "^" f "$"))
  19503. (list (if (featurep 'xemacs)
  19504. "org-colview"
  19505. "org-colview-xemacs")
  19506. "org" "org-loaddefs" "org-version"))
  19507. "\\|"))
  19508. (feats (delete-dups
  19509. (mapcar 'file-name-sans-extension
  19510. (mapcar 'file-name-nondirectory
  19511. (delq nil
  19512. (mapcar 'feature-file
  19513. features))))))
  19514. (lfeat (append
  19515. (sort
  19516. (setq feats
  19517. (delq nil (mapcar
  19518. (lambda (f)
  19519. (if (and (string-match feature-re f)
  19520. (not (string-match remove-re f)))
  19521. f nil))
  19522. feats)))
  19523. 'string-lessp)
  19524. (list "org-version" "org")))
  19525. (load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  19526. (load-suffixes (if uncompiled (reverse load-suffixes) load-suffixes))
  19527. load-uncore load-misses)
  19528. (setq load-misses
  19529. (delq 't
  19530. (mapcar (lambda (f)
  19531. (or (org-load-noerror-mustsuffix (concat org-dir f))
  19532. (and (string= org-dir contrib-dir)
  19533. (org-load-noerror-mustsuffix (concat contrib-dir f)))
  19534. (and (org-load-noerror-mustsuffix (concat (org-find-library-dir f) f))
  19535. (add-to-list 'load-uncore f 'append)
  19536. 't)
  19537. f))
  19538. lfeat)))
  19539. (if load-uncore
  19540. (message "The following feature%s found in load-path, please check if that's correct:\n%s"
  19541. (if (> (length load-uncore) 1) "s were" " was") load-uncore))
  19542. (if load-misses
  19543. (message "Some error occurred while reloading Org feature%s\n%s\nPlease check *Messages*!\n%s"
  19544. (if (> (length load-misses) 1) "s" "") load-misses (org-version nil 'full))
  19545. (message "Successfully reloaded Org\n%s" (org-version nil 'full)))))
  19546. ;;;###autoload
  19547. (defun org-customize ()
  19548. "Call the customize function with org as argument."
  19549. (interactive)
  19550. (org-load-modules-maybe)
  19551. (org-require-autoloaded-modules)
  19552. (customize-browse 'org))
  19553. (defun org-create-customize-menu ()
  19554. "Create a full customization menu for Org-mode, insert it into the menu."
  19555. (interactive)
  19556. (org-load-modules-maybe)
  19557. (org-require-autoloaded-modules)
  19558. (if (fboundp 'customize-menu-create)
  19559. (progn
  19560. (easy-menu-change
  19561. '("Org") "Customize"
  19562. `(["Browse Org group" org-customize t]
  19563. "--"
  19564. ,(customize-menu-create 'org)
  19565. ["Set" Custom-set t]
  19566. ["Save" Custom-save t]
  19567. ["Reset to Current" Custom-reset-current t]
  19568. ["Reset to Saved" Custom-reset-saved t]
  19569. ["Reset to Standard Settings" Custom-reset-standard t]))
  19570. (message "\"Org\"-menu now contains full customization menu"))
  19571. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  19572. ;;;; Miscellaneous stuff
  19573. ;;; Generally useful functions
  19574. (defun org-get-at-bol (property)
  19575. "Get text property PROPERTY at beginning of line."
  19576. (get-text-property (point-at-bol) property))
  19577. (defun org-find-text-property-in-string (prop s)
  19578. "Return the first non-nil value of property PROP in string S."
  19579. (or (get-text-property 0 prop s)
  19580. (get-text-property (or (next-single-property-change 0 prop s) 0)
  19581. prop s)))
  19582. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  19583. "Display the given MESSAGE as a warning."
  19584. (if (fboundp 'display-warning)
  19585. (display-warning 'org message
  19586. (if (featurep 'xemacs) 'warning :warning))
  19587. (let ((buf (get-buffer-create "*Org warnings*")))
  19588. (with-current-buffer buf
  19589. (goto-char (point-max))
  19590. (insert "Warning (Org): " message)
  19591. (unless (bolp)
  19592. (newline)))
  19593. (display-buffer buf)
  19594. (sit-for 0))))
  19595. (defun org-eval (form)
  19596. "Eval FORM and return result."
  19597. (condition-case error
  19598. (eval form)
  19599. (error (format "%%![Error: %s]" error))))
  19600. (defun org-in-clocktable-p ()
  19601. "Check if the cursor is in a clocktable."
  19602. (let ((pos (point)) start)
  19603. (save-excursion
  19604. (end-of-line 1)
  19605. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  19606. (setq start (match-beginning 0))
  19607. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  19608. (>= (match-end 0) pos)
  19609. start))))
  19610. (defun org-in-commented-line ()
  19611. "Is point in a line starting with `#'?"
  19612. (equal (char-after (point-at-bol)) ?#))
  19613. (defun org-in-indented-comment-line ()
  19614. "Is point in a line starting with `#' after some white space?"
  19615. (save-excursion
  19616. (save-match-data
  19617. (goto-char (point-at-bol))
  19618. (looking-at "[ \t]*#"))))
  19619. (defun org-in-verbatim-emphasis ()
  19620. (save-match-data
  19621. (and (org-in-regexp org-emph-re 2)
  19622. (>= (point) (match-beginning 3))
  19623. (<= (point) (match-end 4))
  19624. (member (match-string 3) '("=" "~")))))
  19625. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  19626. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  19627. (if (and marker (marker-buffer marker)
  19628. (buffer-live-p (marker-buffer marker)))
  19629. (progn
  19630. (org-pop-to-buffer-same-window (marker-buffer marker))
  19631. (if (or (> marker (point-max)) (< marker (point-min)))
  19632. (widen))
  19633. (goto-char marker)
  19634. (org-show-context 'org-goto))
  19635. (if bookmark
  19636. (bookmark-jump bookmark)
  19637. (error "Cannot find location"))))
  19638. (defun org-quote-csv-field (s)
  19639. "Quote field for inclusion in CSV material."
  19640. (if (string-match "[\",]" s)
  19641. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  19642. s))
  19643. (defun org-force-self-insert (N)
  19644. "Needed to enforce self-insert under remapping."
  19645. (interactive "p")
  19646. (self-insert-command N))
  19647. (defun org-string-width (s)
  19648. "Compute width of string, ignoring invisible characters.
  19649. This ignores character with invisibility property `org-link', and also
  19650. characters with property `org-cwidth', because these will become invisible
  19651. upon the next fontification round."
  19652. (let (b l)
  19653. (when (or (eq t buffer-invisibility-spec)
  19654. (assq 'org-link buffer-invisibility-spec))
  19655. (while (setq b (text-property-any 0 (length s)
  19656. 'invisible 'org-link s))
  19657. (setq s (concat (substring s 0 b)
  19658. (substring s (or (next-single-property-change
  19659. b 'invisible s) (length s)))))))
  19660. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  19661. (setq s (concat (substring s 0 b)
  19662. (substring s (or (next-single-property-change
  19663. b 'org-cwidth s) (length s))))))
  19664. (setq l (string-width s) b -1)
  19665. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  19666. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  19667. l))
  19668. (defun org-shorten-string (s maxlength)
  19669. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  19670. If the string is shorter or has length MAXLENGTH, just return the
  19671. original string. If it is longer, the functions finds a space in the
  19672. string, breaks this string off at that locations and adds three dots
  19673. as ellipsis. Including the ellipsis, the string will not be longer
  19674. than MAXLENGTH. If finding a good breaking point in the string does
  19675. not work, the string is just chopped off in the middle of a word
  19676. if necessary."
  19677. (if (<= (length s) maxlength)
  19678. s
  19679. (let* ((n (max (- maxlength 4) 1))
  19680. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  19681. (if (string-match re s)
  19682. (concat (match-string 1 s) "...")
  19683. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  19684. (defun org-get-indentation (&optional line)
  19685. "Get the indentation of the current line, interpreting tabs.
  19686. When LINE is given, assume it represents a line and compute its indentation."
  19687. (if line
  19688. (if (string-match "^ *" (org-remove-tabs line))
  19689. (match-end 0))
  19690. (save-excursion
  19691. (beginning-of-line 1)
  19692. (skip-chars-forward " \t")
  19693. (current-column))))
  19694. (defun org-get-string-indentation (s)
  19695. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  19696. (let ((n -1) (i 0) (w tab-width) c)
  19697. (catch 'exit
  19698. (while (< (setq n (1+ n)) (length s))
  19699. (setq c (aref s n))
  19700. (cond ((= c ?\ ) (setq i (1+ i)))
  19701. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  19702. (t (throw 'exit t)))))
  19703. i))
  19704. (defun org-remove-tabs (s &optional width)
  19705. "Replace tabulators in S with spaces.
  19706. Assumes that s is a single line, starting in column 0."
  19707. (setq width (or width tab-width))
  19708. (while (string-match "\t" s)
  19709. (setq s (replace-match
  19710. (make-string
  19711. (- (* width (/ (+ (match-beginning 0) width) width))
  19712. (match-beginning 0)) ?\ )
  19713. t t s)))
  19714. s)
  19715. (defun org-fix-indentation (line ind)
  19716. "Fix indentation in LINE.
  19717. IND is a cons cell with target and minimum indentation.
  19718. If the current indentation in LINE is smaller than the minimum,
  19719. leave it alone. If it is larger than ind, set it to the target."
  19720. (let* ((l (org-remove-tabs line))
  19721. (i (org-get-indentation l))
  19722. (i1 (car ind)) (i2 (cdr ind)))
  19723. (if (>= i i2) (setq l (substring line i2)))
  19724. (if (> i1 0)
  19725. (concat (make-string i1 ?\ ) l)
  19726. l)))
  19727. (defun org-remove-indentation (code &optional n)
  19728. "Remove the maximum common indentation from the lines in CODE.
  19729. N may optionally be the number of spaces to remove."
  19730. (with-temp-buffer
  19731. (insert code)
  19732. (org-do-remove-indentation n)
  19733. (buffer-string)))
  19734. (defun org-do-remove-indentation (&optional n)
  19735. "Remove the maximum common indentation from the buffer."
  19736. (untabify (point-min) (point-max))
  19737. (let ((min 10000) re)
  19738. (if n
  19739. (setq min n)
  19740. (goto-char (point-min))
  19741. (while (re-search-forward "^ *[^ \n]" nil t)
  19742. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  19743. (unless (or (= min 0) (= min 10000))
  19744. (setq re (format "^ \\{%d\\}" min))
  19745. (goto-char (point-min))
  19746. (while (re-search-forward re nil t)
  19747. (replace-match "")
  19748. (end-of-line 1))
  19749. min)))
  19750. (defun org-fill-template (template alist)
  19751. "Find each %key of ALIST in TEMPLATE and replace it."
  19752. (let ((case-fold-search nil)
  19753. entry key value)
  19754. (setq alist (sort (copy-sequence alist)
  19755. (lambda (a b) (< (length (car a)) (length (car b))))))
  19756. (while (setq entry (pop alist))
  19757. (setq template
  19758. (replace-regexp-in-string
  19759. (concat "%" (regexp-quote (car entry)))
  19760. (or (cdr entry) "") template t t)))
  19761. template))
  19762. (defun org-base-buffer (buffer)
  19763. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  19764. (if (not buffer)
  19765. buffer
  19766. (or (buffer-base-buffer buffer)
  19767. buffer)))
  19768. (defun org-trim (s)
  19769. "Remove whitespace at beginning and end of string."
  19770. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  19771. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  19772. s)
  19773. (defun org-wrap (string &optional width lines)
  19774. "Wrap string to either a number of lines, or a width in characters.
  19775. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  19776. that costs. If there is a word longer than WIDTH, the text is actually
  19777. wrapped to the length of that word.
  19778. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  19779. many lines, whatever width that takes.
  19780. The return value is a list of lines, without newlines at the end."
  19781. (let* ((words (org-split-string string "[ \t\n]+"))
  19782. (maxword (apply 'max (mapcar 'org-string-width words)))
  19783. w ll)
  19784. (cond (width
  19785. (org-do-wrap words (max maxword width)))
  19786. (lines
  19787. (setq w maxword)
  19788. (setq ll (org-do-wrap words maxword))
  19789. (if (<= (length ll) lines)
  19790. ll
  19791. (setq ll words)
  19792. (while (> (length ll) lines)
  19793. (setq w (1+ w))
  19794. (setq ll (org-do-wrap words w)))
  19795. ll))
  19796. (t (error "Cannot wrap this")))))
  19797. (defun org-do-wrap (words width)
  19798. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  19799. (let (lines line)
  19800. (while words
  19801. (setq line (pop words))
  19802. (while (and words (< (+ (length line) (length (car words))) width))
  19803. (setq line (concat line " " (pop words))))
  19804. (setq lines (push line lines)))
  19805. (nreverse lines)))
  19806. (defun org-split-string (string &optional separators)
  19807. "Splits STRING into substrings at SEPARATORS.
  19808. No empty strings are returned if there are matches at the beginning
  19809. and end of string."
  19810. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  19811. (start 0)
  19812. notfirst
  19813. (list nil))
  19814. (while (and (string-match rexp string
  19815. (if (and notfirst
  19816. (= start (match-beginning 0))
  19817. (< start (length string)))
  19818. (1+ start) start))
  19819. (< (match-beginning 0) (length string)))
  19820. (setq notfirst t)
  19821. (or (eq (match-beginning 0) 0)
  19822. (and (eq (match-beginning 0) (match-end 0))
  19823. (eq (match-beginning 0) start))
  19824. (setq list
  19825. (cons (substring string start (match-beginning 0))
  19826. list)))
  19827. (setq start (match-end 0)))
  19828. (or (eq start (length string))
  19829. (setq list
  19830. (cons (substring string start)
  19831. list)))
  19832. (nreverse list)))
  19833. (defun org-quote-vert (s)
  19834. "Replace \"|\" with \"\\vert\"."
  19835. (while (string-match "|" s)
  19836. (setq s (replace-match "\\vert" t t s)))
  19837. s)
  19838. (defun org-uuidgen-p (s)
  19839. "Is S an ID created by UUIDGEN?"
  19840. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  19841. (defun org-in-src-block-p (&optional inside)
  19842. "Whether point is in a code source block.
  19843. When INSIDE is non-nil, don't consider we are within a src block
  19844. when point is at #+BEGIN_SRC or #+END_SRC."
  19845. (let ((case-fold-search t) ov)
  19846. (or (and (setq ov (overlays-at (point)))
  19847. (memq 'org-block-background
  19848. (overlay-properties (car ov))))
  19849. (and (not inside)
  19850. (save-match-data
  19851. (save-excursion
  19852. (beginning-of-line)
  19853. (looking-at ".*#\\+\\(begin\\|end\\)_src")))))))
  19854. (defun org-context ()
  19855. "Return a list of contexts of the current cursor position.
  19856. If several contexts apply, all are returned.
  19857. Each context entry is a list with a symbol naming the context, and
  19858. two positions indicating start and end of the context. Possible
  19859. contexts are:
  19860. :headline anywhere in a headline
  19861. :headline-stars on the leading stars in a headline
  19862. :todo-keyword on a TODO keyword (including DONE) in a headline
  19863. :tags on the TAGS in a headline
  19864. :priority on the priority cookie in a headline
  19865. :item on the first line of a plain list item
  19866. :item-bullet on the bullet/number of a plain list item
  19867. :checkbox on the checkbox in a plain list item
  19868. :table in an org-mode table
  19869. :table-special on a special filed in a table
  19870. :table-table in a table.el table
  19871. :clocktable in a clocktable
  19872. :src-block in a source block
  19873. :link on a hyperlink
  19874. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT, QUOTE.
  19875. :target on a <<target>>
  19876. :radio-target on a <<<radio-target>>>
  19877. :latex-fragment on a LaTeX fragment
  19878. :latex-preview on a LaTeX fragment with overlaid preview image
  19879. This function expects the position to be visible because it uses font-lock
  19880. faces as a help to recognize the following contexts: :table-special, :link,
  19881. and :keyword."
  19882. (let* ((f (get-text-property (point) 'face))
  19883. (faces (if (listp f) f (list f)))
  19884. (case-fold-search t)
  19885. (p (point)) clist o)
  19886. ;; First the large context
  19887. (cond
  19888. ((org-at-heading-p t)
  19889. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  19890. (when (progn
  19891. (beginning-of-line 1)
  19892. (looking-at org-todo-line-tags-regexp))
  19893. (push (org-point-in-group p 1 :headline-stars) clist)
  19894. (push (org-point-in-group p 2 :todo-keyword) clist)
  19895. (push (org-point-in-group p 4 :tags) clist))
  19896. (goto-char p)
  19897. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  19898. (if (looking-at "\\[#[A-Z0-9]\\]")
  19899. (push (org-point-in-group p 0 :priority) clist)))
  19900. ((org-at-item-p)
  19901. (push (org-point-in-group p 2 :item-bullet) clist)
  19902. (push (list :item (point-at-bol)
  19903. (save-excursion (org-end-of-item) (point)))
  19904. clist)
  19905. (and (org-at-item-checkbox-p)
  19906. (push (org-point-in-group p 0 :checkbox) clist)))
  19907. ((org-at-table-p)
  19908. (push (list :table (org-table-begin) (org-table-end)) clist)
  19909. (if (memq 'org-formula faces)
  19910. (push (list :table-special
  19911. (previous-single-property-change p 'face)
  19912. (next-single-property-change p 'face)) clist)))
  19913. ((org-at-table-p 'any)
  19914. (push (list :table-table) clist)))
  19915. (goto-char p)
  19916. (let ((case-fold-search t))
  19917. ;; New the "medium" contexts: clocktables, source blocks
  19918. (cond ((org-in-clocktable-p)
  19919. (push (list :clocktable
  19920. (and (or (looking-at "#\\+BEGIN: clocktable")
  19921. (search-backward "#+BEGIN: clocktable" nil t))
  19922. (match-beginning 0))
  19923. (and (re-search-forward "#\\+END:?" nil t)
  19924. (match-end 0))) clist))
  19925. ((org-in-src-block-p)
  19926. (push (list :src-block
  19927. (and (or (looking-at "#\\+BEGIN_SRC")
  19928. (search-backward "#+BEGIN_SRC" nil t))
  19929. (match-beginning 0))
  19930. (and (search-forward "#+END_SRC" nil t)
  19931. (match-beginning 0))) clist))))
  19932. (goto-char p)
  19933. ;; Now the small context
  19934. (cond
  19935. ((org-at-timestamp-p)
  19936. (push (org-point-in-group p 0 :timestamp) clist))
  19937. ((memq 'org-link faces)
  19938. (push (list :link
  19939. (previous-single-property-change p 'face)
  19940. (next-single-property-change p 'face)) clist))
  19941. ((memq 'org-special-keyword faces)
  19942. (push (list :keyword
  19943. (previous-single-property-change p 'face)
  19944. (next-single-property-change p 'face)) clist))
  19945. ((org-at-target-p)
  19946. (push (org-point-in-group p 0 :target) clist)
  19947. (goto-char (1- (match-beginning 0)))
  19948. (if (looking-at org-radio-target-regexp)
  19949. (push (org-point-in-group p 0 :radio-target) clist))
  19950. (goto-char p))
  19951. ((setq o (car (delq nil
  19952. (mapcar
  19953. (lambda (x)
  19954. (if (memq x org-latex-fragment-image-overlays) x))
  19955. (overlays-at (point))))))
  19956. (push (list :latex-fragment
  19957. (overlay-start o) (overlay-end o)) clist)
  19958. (push (list :latex-preview
  19959. (overlay-start o) (overlay-end o)) clist))
  19960. ((org-inside-LaTeX-fragment-p)
  19961. ;; FIXME: positions wrong.
  19962. (push (list :latex-fragment (point) (point)) clist)))
  19963. (setq clist (nreverse (delq nil clist)))
  19964. clist))
  19965. ;; FIXME: Compare with at-regexp-p Do we need both?
  19966. (defun org-in-regexp (re &optional nlines visually)
  19967. "Check if point is inside a match of regexp.
  19968. Normally only the current line is checked, but you can include NLINES extra
  19969. lines both before and after point into the search.
  19970. If VISUALLY is set, require that the cursor is not after the match but
  19971. really on, so that the block visually is on the match."
  19972. (catch 'exit
  19973. (let ((pos (point))
  19974. (eol (point-at-eol (+ 1 (or nlines 0))))
  19975. (inc (if visually 1 0)))
  19976. (save-excursion
  19977. (beginning-of-line (- 1 (or nlines 0)))
  19978. (while (re-search-forward re eol t)
  19979. (if (and (<= (match-beginning 0) pos)
  19980. (>= (+ inc (match-end 0)) pos))
  19981. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  19982. (defun org-at-regexp-p (regexp)
  19983. "Is point inside a match of REGEXP in the current line?"
  19984. (catch 'exit
  19985. (save-excursion
  19986. (let ((pos (point)) (end (point-at-eol)))
  19987. (beginning-of-line 1)
  19988. (while (re-search-forward regexp end t)
  19989. (if (and (<= (match-beginning 0) pos)
  19990. (>= (match-end 0) pos))
  19991. (throw 'exit t)))
  19992. nil))))
  19993. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  19994. "Non-nil when point is between matches of START-RE and END-RE.
  19995. Also return a non-nil value when point is on one of the matches.
  19996. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  19997. buffer positions. Default values are the positions of headlines
  19998. surrounding the point.
  19999. The functions returns a cons cell whose car (resp. cdr) is the
  20000. position before START-RE (resp. after END-RE)."
  20001. (save-match-data
  20002. (let ((pos (point))
  20003. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  20004. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  20005. beg end)
  20006. (save-excursion
  20007. ;; Point is on a block when on START-RE or if START-RE can be
  20008. ;; found before it...
  20009. (and (or (org-at-regexp-p start-re)
  20010. (re-search-backward start-re limit-up t))
  20011. (setq beg (match-beginning 0))
  20012. ;; ... and END-RE after it...
  20013. (goto-char (match-end 0))
  20014. (re-search-forward end-re limit-down t)
  20015. (> (setq end (match-end 0)) pos)
  20016. ;; ... without another START-RE in-between.
  20017. (goto-char (match-beginning 0))
  20018. (not (re-search-backward start-re (1+ beg) t))
  20019. ;; Return value.
  20020. (cons beg end))))))
  20021. (defun org-in-block-p (names)
  20022. "Non-nil when point belongs to a block whose name belongs to NAMES.
  20023. NAMES is a list of strings containing names of blocks.
  20024. Return first block name matched, or nil. Beware that in case of
  20025. nested blocks, the returned name may not belong to the closest
  20026. block from point."
  20027. (save-match-data
  20028. (catch 'exit
  20029. (let ((case-fold-search t)
  20030. (lim-up (save-excursion (outline-previous-heading)))
  20031. (lim-down (save-excursion (outline-next-heading))))
  20032. (mapc (lambda (name)
  20033. (let ((n (regexp-quote name)))
  20034. (when (org-between-regexps-p
  20035. (concat "^[ \t]*#\\+begin_" n)
  20036. (concat "^[ \t]*#\\+end_" n)
  20037. lim-up lim-down)
  20038. (throw 'exit n))))
  20039. names))
  20040. nil)))
  20041. (defun org-in-drawer-p ()
  20042. "Is point within a drawer?"
  20043. (save-match-data
  20044. (let ((case-fold-search t)
  20045. (lim-up (save-excursion (outline-previous-heading)))
  20046. (lim-down (save-excursion (outline-next-heading))))
  20047. (org-between-regexps-p
  20048. (concat "^[ \t]*:" (regexp-opt org-drawers) ":")
  20049. "^[ \t]*:end:.*$"
  20050. lim-up lim-down))))
  20051. (defun org-occur-in-agenda-files (regexp &optional nlines)
  20052. "Call `multi-occur' with buffers for all agenda files."
  20053. (interactive "sOrg-files matching: \np")
  20054. (let* ((files (org-agenda-files))
  20055. (tnames (mapcar 'file-truename files))
  20056. (extra org-agenda-text-search-extra-files)
  20057. f)
  20058. (when (eq (car extra) 'agenda-archives)
  20059. (setq extra (cdr extra))
  20060. (setq files (org-add-archive-files files)))
  20061. (while (setq f (pop extra))
  20062. (unless (member (file-truename f) tnames)
  20063. (add-to-list 'files f 'append)
  20064. (add-to-list 'tnames (file-truename f) 'append)))
  20065. (multi-occur
  20066. (mapcar (lambda (x)
  20067. (with-current-buffer
  20068. (or (get-file-buffer x) (find-file-noselect x))
  20069. (widen)
  20070. (current-buffer)))
  20071. files)
  20072. regexp)))
  20073. (if (boundp 'occur-mode-find-occurrence-hook)
  20074. ;; Emacs 23
  20075. (add-hook 'occur-mode-find-occurrence-hook
  20076. (lambda ()
  20077. (when (derived-mode-p 'org-mode)
  20078. (org-reveal))))
  20079. ;; Emacs 22
  20080. (defadvice occur-mode-goto-occurrence
  20081. (after org-occur-reveal activate)
  20082. (and (derived-mode-p 'org-mode) (org-reveal)))
  20083. (defadvice occur-mode-goto-occurrence-other-window
  20084. (after org-occur-reveal activate)
  20085. (and (derived-mode-p 'org-mode) (org-reveal)))
  20086. (defadvice occur-mode-display-occurrence
  20087. (after org-occur-reveal activate)
  20088. (when (derived-mode-p 'org-mode)
  20089. (let ((pos (occur-mode-find-occurrence)))
  20090. (with-current-buffer (marker-buffer pos)
  20091. (save-excursion
  20092. (goto-char pos)
  20093. (org-reveal)))))))
  20094. (defun org-occur-link-in-agenda-files ()
  20095. "Create a link and search for it in the agendas.
  20096. The link is not stored in `org-stored-links', it is just created
  20097. for the search purpose."
  20098. (interactive)
  20099. (let ((link (condition-case nil
  20100. (org-store-link nil)
  20101. (error "Unable to create a link to here"))))
  20102. (org-occur-in-agenda-files (regexp-quote link))))
  20103. (defun org-reverse-string (string)
  20104. "Return the reverse of STRING."
  20105. (apply 'string (reverse (string-to-list string))))
  20106. ;; defsubst org-uniquify must be defined before first use
  20107. (defun org-uniquify-alist (alist)
  20108. "Merge elements of ALIST with the same key.
  20109. For example, in this alist:
  20110. \(org-uniquify-alist '((a 1) (b 2) (a 3)))
  20111. => '((a 1 3) (b 2))
  20112. merge (a 1) and (a 3) into (a 1 3).
  20113. The function returns the new ALIST."
  20114. (let (rtn)
  20115. (mapc
  20116. (lambda (e)
  20117. (let (n)
  20118. (if (not (assoc (car e) rtn))
  20119. (push e rtn)
  20120. (setq n (cons (car e) (append (cdr (assoc (car e) rtn)) (cdr e))))
  20121. (setq rtn (assq-delete-all (car e) rtn))
  20122. (push n rtn))))
  20123. alist)
  20124. rtn))
  20125. (defun org-delete-all (elts list)
  20126. "Remove all elements in ELTS from LIST."
  20127. (while elts
  20128. (setq list (delete (pop elts) list)))
  20129. list)
  20130. (defun org-count (cl-item cl-seq)
  20131. "Count the number of occurrences of ITEM in SEQ.
  20132. Taken from `count' in cl-seq.el with all keyword arguments removed."
  20133. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  20134. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  20135. (while (< cl-start cl-end)
  20136. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  20137. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  20138. (setq cl-start (1+ cl-start)))
  20139. cl-count))
  20140. (defun org-remove-if (predicate seq)
  20141. "Remove everything from SEQ that fulfills PREDICATE."
  20142. (let (res e)
  20143. (while seq
  20144. (setq e (pop seq))
  20145. (if (not (funcall predicate e)) (push e res)))
  20146. (nreverse res)))
  20147. (defun org-remove-if-not (predicate seq)
  20148. "Remove everything from SEQ that does not fulfill PREDICATE."
  20149. (let (res e)
  20150. (while seq
  20151. (setq e (pop seq))
  20152. (if (funcall predicate e) (push e res)))
  20153. (nreverse res)))
  20154. (defun org-reduce (cl-func cl-seq &rest cl-keys)
  20155. "Reduce two-argument FUNCTION across SEQ.
  20156. Taken from `reduce' in cl-seq.el with all keyword arguments but
  20157. \":initial-value\" removed."
  20158. (let ((cl-accum (cond ((memq :initial-value cl-keys)
  20159. (cadr (memq :initial-value cl-keys)))
  20160. (cl-seq (pop cl-seq))
  20161. (t (funcall cl-func)))))
  20162. (while cl-seq
  20163. (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
  20164. cl-accum))
  20165. (defun org-every (pred seq)
  20166. "Return true if PREDICATE is true of every element of SEQ.
  20167. Adapted from `every' in cl.el."
  20168. (catch 'org-every
  20169. (mapc (lambda (e) (unless (funcall pred e) (throw 'org-every nil))) seq)
  20170. t))
  20171. (defun org-some (pred seq)
  20172. "Return true if PREDICATE is true of any element of SEQ.
  20173. Adapted from `some' in cl.el."
  20174. (catch 'org-some
  20175. (mapc (lambda (e) (when (funcall pred e) (throw 'org-some t))) seq)
  20176. nil))
  20177. (defun org-back-over-empty-lines ()
  20178. "Move backwards over whitespace, to the beginning of the first empty line.
  20179. Returns the number of empty lines passed."
  20180. (let ((pos (point)))
  20181. (if (cdr (assoc 'heading org-blank-before-new-entry))
  20182. (skip-chars-backward " \t\n\r")
  20183. (unless (eobp)
  20184. (forward-line -1)))
  20185. (beginning-of-line 2)
  20186. (goto-char (min (point) pos))
  20187. (count-lines (point) pos)))
  20188. (defun org-skip-whitespace ()
  20189. (skip-chars-forward " \t\n\r"))
  20190. (defun org-point-in-group (point group &optional context)
  20191. "Check if POINT is in match-group GROUP.
  20192. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  20193. match. If the match group does not exist or point is not inside it,
  20194. return nil."
  20195. (and (match-beginning group)
  20196. (>= point (match-beginning group))
  20197. (<= point (match-end group))
  20198. (if context
  20199. (list context (match-beginning group) (match-end group))
  20200. t)))
  20201. (defun org-switch-to-buffer-other-window (&rest args)
  20202. "Switch to buffer in a second window on the current frame.
  20203. In particular, do not allow pop-up frames.
  20204. Returns the newly created buffer."
  20205. (org-no-popups
  20206. (apply 'switch-to-buffer-other-window args)))
  20207. (defun org-combine-plists (&rest plists)
  20208. "Create a single property list from all plists in PLISTS.
  20209. The process starts by copying the first list, and then setting properties
  20210. from the other lists. Settings in the last list are the most significant
  20211. ones and overrule settings in the other lists."
  20212. (let ((rtn (copy-sequence (pop plists)))
  20213. p v ls)
  20214. (while plists
  20215. (setq ls (pop plists))
  20216. (while ls
  20217. (setq p (pop ls) v (pop ls))
  20218. (setq rtn (plist-put rtn p v))))
  20219. rtn))
  20220. (defun org-replace-escapes (string table)
  20221. "Replace %-escapes in STRING with values in TABLE.
  20222. TABLE is an association list with keys like \"%a\" and string values.
  20223. The sequences in STRING may contain normal field width and padding information,
  20224. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  20225. so values can contain further %-escapes if they are define later in TABLE."
  20226. (let ((tbl (copy-alist table))
  20227. (case-fold-search nil)
  20228. (pchg 0)
  20229. e re rpl)
  20230. (while (setq e (pop tbl))
  20231. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  20232. (when (and (cdr e) (string-match re (cdr e)))
  20233. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  20234. (safe "SREF"))
  20235. (add-text-properties 0 3 (list 'sref sref) safe)
  20236. (setcdr e (replace-match safe t t (cdr e)))))
  20237. (while (string-match re string)
  20238. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  20239. (cdr e)))
  20240. (setq string (replace-match rpl t t string))))
  20241. (while (setq pchg (next-property-change pchg string))
  20242. (let ((sref (get-text-property pchg 'sref string)))
  20243. (when (and sref (string-match "SREF" string pchg))
  20244. (setq string (replace-match sref t t string)))))
  20245. string))
  20246. (defun org-sublist (list start end)
  20247. "Return a section of LIST, from START to END.
  20248. Counting starts at 1."
  20249. (let (rtn (c start))
  20250. (setq list (nthcdr (1- start) list))
  20251. (while (and list (<= c end))
  20252. (push (pop list) rtn)
  20253. (setq c (1+ c)))
  20254. (nreverse rtn)))
  20255. (defun org-find-base-buffer-visiting (file)
  20256. "Like `find-buffer-visiting' but always return the base buffer and
  20257. not an indirect buffer."
  20258. (let ((buf (or (get-file-buffer file)
  20259. (find-buffer-visiting file))))
  20260. (if buf
  20261. (or (buffer-base-buffer buf) buf)
  20262. nil)))
  20263. (defun org-image-file-name-regexp (&optional extensions)
  20264. "Return regexp matching the file names of images.
  20265. If EXTENSIONS is given, only match these."
  20266. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  20267. (image-file-name-regexp)
  20268. (let ((image-file-name-extensions
  20269. (or extensions
  20270. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  20271. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  20272. (concat "\\."
  20273. (regexp-opt (nconc (mapcar 'upcase
  20274. image-file-name-extensions)
  20275. image-file-name-extensions)
  20276. t)
  20277. "\\'"))))
  20278. (defun org-file-image-p (file &optional extensions)
  20279. "Return non-nil if FILE is an image."
  20280. (save-match-data
  20281. (string-match (org-image-file-name-regexp extensions) file)))
  20282. (defun org-get-cursor-date (&optional with-time)
  20283. "Return the date at cursor in as a time.
  20284. This works in the calendar and in the agenda, anywhere else it just
  20285. returns the current time.
  20286. If WITH-TIME is non-nil, returns the time of the event at point (in
  20287. the agenda) or the current time of the day."
  20288. (let (date day defd tp tm hod mod)
  20289. (when with-time
  20290. (setq tp (get-text-property (point) 'time))
  20291. (when (and tp (string-match "\\([0-9][0-9]\\):\\([0-9][0-9]\\)" tp))
  20292. (setq hod (string-to-number (match-string 1 tp))
  20293. mod (string-to-number (match-string 2 tp))))
  20294. (or tp (let ((now (decode-time)))
  20295. (setq hod (nth 2 now)
  20296. mod (nth 1 now)))))
  20297. (cond
  20298. ((eq major-mode 'calendar-mode)
  20299. (setq date (calendar-cursor-to-date)
  20300. defd (encode-time 0 (or mod 0) (or hod 0)
  20301. (nth 1 date) (nth 0 date) (nth 2 date))))
  20302. ((eq major-mode 'org-agenda-mode)
  20303. (setq day (get-text-property (point) 'day))
  20304. (if day
  20305. (setq date (calendar-gregorian-from-absolute day)
  20306. defd (encode-time 0 (or mod 0) (or hod 0)
  20307. (nth 1 date) (nth 0 date) (nth 2 date))))))
  20308. (or defd (current-time))))
  20309. (defun org-mark-subtree (&optional up)
  20310. "Mark the current subtree.
  20311. This puts point at the start of the current subtree, and mark at
  20312. the end. If a numeric prefix UP is given, move up into the
  20313. hierarchy of headlines by UP levels before marking the subtree."
  20314. (interactive "P")
  20315. (org-with-limited-levels
  20316. (cond ((org-at-heading-p) (beginning-of-line))
  20317. ((org-before-first-heading-p) (user-error "Not in a subtree"))
  20318. (t (outline-previous-visible-heading 1))))
  20319. (when up (while (and (> up 0) (org-up-heading-safe)) (decf up)))
  20320. (if (org-called-interactively-p 'any)
  20321. (call-interactively 'org-mark-element)
  20322. (org-mark-element)))
  20323. ;;; Indentation
  20324. (defun org-indent-line ()
  20325. "Indent line depending on context."
  20326. (interactive)
  20327. (let* ((pos (point))
  20328. (itemp (org-at-item-p))
  20329. (case-fold-search t)
  20330. (org-drawer-regexp (or org-drawer-regexp "\000"))
  20331. (inline-task-p (and (featurep 'org-inlinetask)
  20332. (org-inlinetask-in-task-p)))
  20333. (inline-re (and inline-task-p
  20334. (org-inlinetask-outline-regexp)))
  20335. column)
  20336. (if (and orgstruct-is-++ (eq pos (point)))
  20337. (let ((indent-line-function (cadadr (assoc 'indent-line-function org-fb-vars))))
  20338. (indent-according-to-mode))
  20339. (beginning-of-line 1)
  20340. (cond
  20341. ;; Headings
  20342. ((looking-at org-outline-regexp) (setq column 0))
  20343. ;; Footnote definition
  20344. ((looking-at org-footnote-definition-re) (setq column 0))
  20345. ;; Literal examples
  20346. ((looking-at "[ \t]*:\\( \\|$\\)")
  20347. (setq column (org-get-indentation))) ; do nothing
  20348. ;; Lists
  20349. ((ignore-errors (goto-char (org-in-item-p)))
  20350. (setq column (if itemp
  20351. (org-get-indentation)
  20352. (org-list-item-body-column (point))))
  20353. (goto-char pos))
  20354. ;; Drawers
  20355. ((and (looking-at "[ \t]*:END:")
  20356. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  20357. (save-excursion
  20358. (goto-char (1- (match-beginning 1)))
  20359. (setq column (current-column))))
  20360. ;; Special blocks
  20361. ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
  20362. (save-excursion
  20363. (re-search-backward
  20364. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  20365. (setq column (org-get-indentation (match-string 0))))
  20366. ((and (not (looking-at "[ \t]*#\\+begin_"))
  20367. (org-between-regexps-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
  20368. (save-excursion
  20369. (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
  20370. (setq column
  20371. (cond ((equal (downcase (match-string 1)) "src")
  20372. ;; src blocks: let `org-edit-src-exit' handle them
  20373. (org-get-indentation))
  20374. ((equal (downcase (match-string 1)) "example")
  20375. (max (org-get-indentation)
  20376. (org-get-indentation (match-string 0))))
  20377. (t
  20378. (org-get-indentation (match-string 0))))))
  20379. ;; This line has nothing special, look at the previous relevant
  20380. ;; line to compute indentation
  20381. (t
  20382. (beginning-of-line 0)
  20383. (while (and (not (bobp))
  20384. (not (looking-at org-table-line-regexp))
  20385. (not (looking-at org-drawer-regexp))
  20386. ;; When point started in an inline task, do not move
  20387. ;; above task starting line.
  20388. (not (and inline-task-p (looking-at inline-re)))
  20389. ;; Skip drawers, blocks, empty lines, verbatim,
  20390. ;; comments, tables, footnotes definitions, lists,
  20391. ;; inline tasks.
  20392. (or (and (looking-at "[ \t]*:END:")
  20393. (re-search-backward org-drawer-regexp nil t))
  20394. (and (looking-at "[ \t]*#\\+end_")
  20395. (re-search-backward "[ \t]*#\\+begin_"nil t))
  20396. (looking-at "[ \t]*[\n:#|]")
  20397. (looking-at org-footnote-definition-re)
  20398. (and (not inline-task-p)
  20399. (featurep 'org-inlinetask)
  20400. (org-inlinetask-in-task-p)
  20401. (or (org-inlinetask-goto-beginning) t))))
  20402. (beginning-of-line 0))
  20403. (cond
  20404. ;; There was a list item above.
  20405. ((ignore-errors (goto-char (org-in-item-p)))
  20406. (goto-char (org-list-get-top-point (org-list-struct)))
  20407. (setq column (org-get-indentation)))
  20408. ;; There was an heading above.
  20409. ((looking-at "\\*+[ \t]+")
  20410. (if (not org-adapt-indentation)
  20411. (setq column 0)
  20412. (goto-char (match-end 0))
  20413. (setq column (current-column))))
  20414. ;; A drawer had started and is unfinished
  20415. ((looking-at org-drawer-regexp)
  20416. (goto-char (1- (match-beginning 1)))
  20417. (setq column (current-column)))
  20418. ;; Else, nothing noticeable found: get indentation and go on.
  20419. (t (setq column (org-get-indentation))))))
  20420. ;; Now apply indentation and move cursor accordingly
  20421. (goto-char pos)
  20422. (if (<= (current-column) (current-indentation))
  20423. (org-indent-line-to column)
  20424. (save-excursion (org-indent-line-to column)))
  20425. ;; Special polishing for properties, see `org-property-format'
  20426. (setq column (current-column))
  20427. (beginning-of-line 1)
  20428. (if (looking-at org-property-re)
  20429. (replace-match (concat (match-string 4)
  20430. (format org-property-format
  20431. (match-string 1) (match-string 3)))
  20432. t t))
  20433. (org-move-to-column column))))
  20434. (defun org-indent-drawer ()
  20435. "Indent the drawer at point."
  20436. (interactive)
  20437. (let ((p (point))
  20438. (e (and (save-excursion (re-search-forward ":END:" nil t))
  20439. (match-end 0)))
  20440. (folded
  20441. (save-excursion
  20442. (end-of-line)
  20443. (when (overlays-at (point))
  20444. (member 'invisible (overlay-properties
  20445. (car (overlays-at (point)))))))))
  20446. (when folded (org-cycle))
  20447. (indent-for-tab-command)
  20448. (while (and (move-beginning-of-line 2) (< (point) e))
  20449. (indent-for-tab-command))
  20450. (goto-char p)
  20451. (when folded (org-cycle)))
  20452. (message "Drawer at point indented"))
  20453. (defun org-indent-block ()
  20454. "Indent the block at point."
  20455. (interactive)
  20456. (let ((p (point))
  20457. (case-fold-search t)
  20458. (e (and (save-excursion (re-search-forward "#\\+end_?\\(?:[a-z]+\\)?" nil t))
  20459. (match-end 0)))
  20460. (folded
  20461. (save-excursion
  20462. (end-of-line)
  20463. (when (overlays-at (point))
  20464. (member 'invisible (overlay-properties
  20465. (car (overlays-at (point)))))))))
  20466. (when folded (org-cycle))
  20467. (indent-for-tab-command)
  20468. (while (and (move-beginning-of-line 2) (< (point) e))
  20469. (indent-for-tab-command))
  20470. (goto-char p)
  20471. (when folded (org-cycle)))
  20472. (message "Block at point indented"))
  20473. (defun org-indent-region (start end)
  20474. "Indent region."
  20475. (interactive "r")
  20476. (save-excursion
  20477. (let ((line-end (org-current-line end)))
  20478. (goto-char start)
  20479. (while (< (org-current-line) line-end)
  20480. (cond ((org-in-src-block-p t) (org-src-native-tab-command-maybe))
  20481. (t (call-interactively 'org-indent-line)))
  20482. (move-beginning-of-line 2)))))
  20483. ;;; Filling
  20484. ;; We use our own fill-paragraph and auto-fill functions.
  20485. ;; `org-fill-paragraph' relies on adaptive filling and context
  20486. ;; checking. Appropriate `fill-prefix' is computed with
  20487. ;; `org-adaptive-fill-function'.
  20488. ;; `org-auto-fill-function' takes care of auto-filling. It calls
  20489. ;; `do-auto-fill' only on valid areas with `fill-prefix' shadowed with
  20490. ;; `org-adaptive-fill-function' value. Internally,
  20491. ;; `org-comment-line-break-function' breaks the line.
  20492. ;; `org-setup-filling' installs filling and auto-filling related
  20493. ;; variables during `org-mode' initialization.
  20494. (defvar org-element-paragraph-separate) ; org-element.el
  20495. (defun org-setup-filling ()
  20496. (require 'org-element)
  20497. ;; Prevent auto-fill from inserting unwanted new items.
  20498. (when (boundp 'fill-nobreak-predicate)
  20499. (org-set-local
  20500. 'fill-nobreak-predicate
  20501. (org-uniquify
  20502. (append fill-nobreak-predicate
  20503. '(org-fill-line-break-nobreak-p
  20504. org-fill-paragraph-with-timestamp-nobreak-p)))))
  20505. (let ((paragraph-ending (substring org-element-paragraph-separate 1)))
  20506. (org-set-local 'paragraph-start paragraph-ending)
  20507. (org-set-local 'paragraph-separate paragraph-ending))
  20508. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  20509. (org-set-local 'auto-fill-inhibit-regexp nil)
  20510. (org-set-local 'adaptive-fill-function 'org-adaptive-fill-function)
  20511. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  20512. (org-set-local 'comment-line-break-function 'org-comment-line-break-function))
  20513. (defun org-fill-line-break-nobreak-p ()
  20514. "Non-nil when a new line at point would create an Org line break."
  20515. (save-excursion
  20516. (skip-chars-backward "[ \t]")
  20517. (skip-chars-backward "\\\\")
  20518. (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
  20519. (defun org-fill-paragraph-with-timestamp-nobreak-p ()
  20520. "Non-nil when a new line at point would split a timestamp."
  20521. (and (org-at-timestamp-p t)
  20522. (not (looking-at org-ts-regexp-both))))
  20523. (declare-function message-in-body-p "message" ())
  20524. (defvar orgtbl-line-start-regexp) ; From org-table.el
  20525. (defun org-adaptive-fill-function ()
  20526. "Compute a fill prefix for the current line.
  20527. Return fill prefix, as a string, or nil if current line isn't
  20528. meant to be filled. For convenience, if `adaptive-fill-regexp'
  20529. matches in paragraphs or comments, use it."
  20530. (catch 'exit
  20531. (when (derived-mode-p 'message-mode)
  20532. (save-excursion
  20533. (beginning-of-line)
  20534. (cond ((or (not (message-in-body-p))
  20535. (looking-at orgtbl-line-start-regexp))
  20536. (throw 'exit nil))
  20537. ((looking-at message-cite-prefix-regexp)
  20538. (throw 'exit (match-string-no-properties 0)))
  20539. ((looking-at org-outline-regexp)
  20540. (throw 'exit (make-string (length (match-string 0)) ? ))))))
  20541. (org-with-wide-buffer
  20542. (let* ((p (line-beginning-position))
  20543. (element (save-excursion
  20544. (beginning-of-line)
  20545. (or (ignore-errors (org-element-at-point))
  20546. (user-error "An element cannot be parsed line %d"
  20547. (line-number-at-pos (point))))))
  20548. (type (org-element-type element))
  20549. (post-affiliated (org-element-property :post-affiliated element)))
  20550. (unless (and post-affiliated (< p post-affiliated))
  20551. (case type
  20552. (comment
  20553. (save-excursion
  20554. (beginning-of-line)
  20555. (looking-at "[ \t]*")
  20556. (concat (match-string 0) "# ")))
  20557. (footnote-definition "")
  20558. ((item plain-list)
  20559. (make-string (org-list-item-body-column
  20560. (or post-affiliated
  20561. (org-element-property :begin element)))
  20562. ? ))
  20563. (paragraph
  20564. ;; Fill prefix is usually the same as the current line,
  20565. ;; unless the paragraph is at the beginning of an item.
  20566. (let ((parent (org-element-property :parent element)))
  20567. (save-excursion
  20568. (beginning-of-line)
  20569. (cond ((eq (org-element-type parent) 'item)
  20570. (make-string (org-list-item-body-column
  20571. (org-element-property :begin parent))
  20572. ? ))
  20573. ((and adaptive-fill-regexp
  20574. ;; Locally disable
  20575. ;; `adaptive-fill-function' to let
  20576. ;; `fill-context-prefix' handle
  20577. ;; `adaptive-fill-regexp' variable.
  20578. (let (adaptive-fill-function)
  20579. (fill-context-prefix
  20580. post-affiliated
  20581. (org-element-property :end element)))))
  20582. ((looking-at "[ \t]+") (match-string 0))
  20583. (t "")))))
  20584. (comment-block
  20585. ;; Only fill contents if P is within block boundaries.
  20586. (let* ((cbeg (save-excursion (goto-char post-affiliated)
  20587. (forward-line)
  20588. (point)))
  20589. (cend (save-excursion
  20590. (goto-char (org-element-property :end element))
  20591. (skip-chars-backward " \r\t\n")
  20592. (line-beginning-position))))
  20593. (when (and (>= p cbeg) (< p cend))
  20594. (if (save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  20595. (match-string 0)
  20596. ""))))))))))
  20597. (declare-function message-goto-body "message" ())
  20598. (defvar message-cite-prefix-regexp) ; From message.el
  20599. (defun org-fill-paragraph (&optional justify)
  20600. "Fill element at point, when applicable.
  20601. This function only applies to comment blocks, comments, example
  20602. blocks and paragraphs. Also, as a special case, re-align table
  20603. when point is at one.
  20604. If JUSTIFY is non-nil (interactively, with prefix argument),
  20605. justify as well. If `sentence-end-double-space' is non-nil, then
  20606. period followed by one space does not end a sentence, so don't
  20607. break a line there. The variable `fill-column' controls the
  20608. width for filling.
  20609. For convenience, when point is at a plain list, an item or
  20610. a footnote definition, try to fill the first paragraph within."
  20611. (interactive)
  20612. (if (and (derived-mode-p 'message-mode)
  20613. (or (not (message-in-body-p))
  20614. (save-excursion (move-beginning-of-line 1)
  20615. (looking-at message-cite-prefix-regexp))))
  20616. ;; First ensure filling is correct in message-mode.
  20617. (let ((fill-paragraph-function
  20618. (cadadr (assoc 'fill-paragraph-function org-fb-vars)))
  20619. (fill-prefix (cadadr (assoc 'fill-prefix org-fb-vars)))
  20620. (paragraph-start (cadadr (assoc 'paragraph-start org-fb-vars)))
  20621. (paragraph-separate
  20622. (cadadr (assoc 'paragraph-separate org-fb-vars))))
  20623. (fill-paragraph nil))
  20624. (with-syntax-table org-mode-transpose-word-syntax-table
  20625. ;; Move to end of line in order to get the first paragraph
  20626. ;; within a plain list or a footnote definition.
  20627. (let ((element (save-excursion
  20628. (end-of-line)
  20629. (or (ignore-errors (org-element-at-point))
  20630. (user-error "An element cannot be parsed line %d"
  20631. (line-number-at-pos (point)))))))
  20632. ;; First check if point is in a blank line at the beginning of
  20633. ;; the buffer. In that case, ignore filling.
  20634. (case (org-element-type element)
  20635. ;; Use major mode filling function is src blocks.
  20636. (src-block (org-babel-do-key-sequence-in-edit-buffer (kbd "M-q")))
  20637. ;; Align Org tables, leave table.el tables as-is.
  20638. (table-row (org-table-align) t)
  20639. (table
  20640. (when (eq (org-element-property :type element) 'org)
  20641. (save-excursion
  20642. (goto-char (org-element-property :post-affiliated element))
  20643. (org-table-align)))
  20644. t)
  20645. (paragraph
  20646. ;; Paragraphs may contain `line-break' type objects.
  20647. (let ((beg (max (point-min)
  20648. (org-element-property :contents-begin element)))
  20649. (end (min (point-max)
  20650. (org-element-property :contents-end element))))
  20651. ;; Do nothing if point is at an affiliated keyword.
  20652. (if (< (line-end-position) beg) t
  20653. (when (derived-mode-p 'message-mode)
  20654. ;; In `message-mode', do not fill following citation
  20655. ;; in current paragraph nor text before message body.
  20656. (let ((body-start (save-excursion (message-goto-body))))
  20657. (when body-start (setq beg (max body-start beg))))
  20658. (when (save-excursion
  20659. (re-search-forward
  20660. (concat "^" message-cite-prefix-regexp) end t))
  20661. (setq end (match-beginning 0))))
  20662. ;; Fill paragraph, taking line breaks into account.
  20663. ;; For that, slice the paragraph using line breaks as
  20664. ;; separators, and fill the parts in reverse order to
  20665. ;; avoid messing with markers.
  20666. (save-excursion
  20667. (goto-char end)
  20668. (mapc
  20669. (lambda (pos)
  20670. (fill-region-as-paragraph pos (point) justify)
  20671. (goto-char pos))
  20672. ;; Find the list of ending positions for line breaks
  20673. ;; in the current paragraph. Add paragraph
  20674. ;; beginning to include first slice.
  20675. (nreverse
  20676. (cons beg
  20677. (org-element-map
  20678. (org-element--parse-objects
  20679. beg end nil (org-element-restriction 'paragraph))
  20680. 'line-break
  20681. (lambda (lb) (org-element-property :end lb)))))))
  20682. t)))
  20683. ;; Contents of `comment-block' type elements should be
  20684. ;; filled as plain text, but only if point is within block
  20685. ;; markers.
  20686. (comment-block
  20687. (let* ((case-fold-search t)
  20688. (beg (save-excursion
  20689. (goto-char (org-element-property :begin element))
  20690. (re-search-forward "^[ \t]*#\\+begin_comment" nil t)
  20691. (forward-line)
  20692. (point)))
  20693. (end (save-excursion
  20694. (goto-char (org-element-property :end element))
  20695. (re-search-backward "^[ \t]*#\\+end_comment" nil t)
  20696. (line-beginning-position))))
  20697. (if (or (< (point) beg) (> (point) end)) t
  20698. (fill-region-as-paragraph
  20699. (save-excursion (end-of-line)
  20700. (re-search-backward "^[ \t]*$" beg 'move)
  20701. (line-beginning-position))
  20702. (save-excursion (beginning-of-line)
  20703. (re-search-forward "^[ \t]*$" end 'move)
  20704. (line-beginning-position))
  20705. justify))))
  20706. ;; Fill comments.
  20707. (comment
  20708. (let ((begin (org-element-property :post-affiliated element))
  20709. (end (org-element-property :end element)))
  20710. (when (and (>= (point) begin) (<= (point) end))
  20711. (let ((begin (save-excursion
  20712. (end-of-line)
  20713. (if (re-search-backward "^[ \t]*#[ \t]*$" begin t)
  20714. (progn (forward-line) (point))
  20715. begin)))
  20716. (end (save-excursion
  20717. (end-of-line)
  20718. (if (re-search-forward "^[ \t]*#[ \t]*$" end 'move)
  20719. (1- (line-beginning-position))
  20720. (skip-chars-backward " \r\t\n")
  20721. (line-end-position)))))
  20722. ;; Do not fill comments when at a blank line.
  20723. (when (> end begin)
  20724. (let ((fill-prefix
  20725. (save-excursion
  20726. (beginning-of-line)
  20727. (looking-at "[ \t]*#")
  20728. (let ((comment-prefix (match-string 0)))
  20729. (goto-char (match-end 0))
  20730. (if (looking-at adaptive-fill-regexp)
  20731. (concat comment-prefix (match-string 0))
  20732. (concat comment-prefix " "))))))
  20733. (save-excursion
  20734. (fill-region-as-paragraph begin end justify))))))
  20735. t))
  20736. ;; Ignore every other element.
  20737. (otherwise t))))))
  20738. (defun org-auto-fill-function ()
  20739. "Auto-fill function."
  20740. ;; Check if auto-filling is meaningful.
  20741. (let ((fc (current-fill-column)))
  20742. (when (and fc (> (current-column) fc))
  20743. (let* ((fill-prefix (org-adaptive-fill-function))
  20744. ;; Enforce empty fill prefix, if required. Otherwise, it
  20745. ;; will be computed again.
  20746. (adaptive-fill-mode (not (equal fill-prefix ""))))
  20747. (when fill-prefix (do-auto-fill))))))
  20748. (defun org-comment-line-break-function (&optional soft)
  20749. "Break line at point and indent, continuing comment if within one.
  20750. The inserted newline is marked hard if variable
  20751. `use-hard-newlines' is true, unless optional argument SOFT is
  20752. non-nil."
  20753. (if soft (insert-and-inherit ?\n) (newline 1))
  20754. (save-excursion (forward-char -1) (delete-horizontal-space))
  20755. (delete-horizontal-space)
  20756. (indent-to-left-margin)
  20757. (insert-before-markers-and-inherit fill-prefix))
  20758. ;;; Comments
  20759. ;; Org comments syntax is quite complex. It requires the entire line
  20760. ;; to be just a comment. Also, even with the right syntax at the
  20761. ;; beginning of line, some some elements (i.e. verse-block or
  20762. ;; example-block) don't accept comments. Usual Emacs comment commands
  20763. ;; cannot cope with those requirements. Therefore, Org replaces them.
  20764. ;; Org still relies on `comment-dwim', but cannot trust
  20765. ;; `comment-only-p'. So, `comment-region-function' and
  20766. ;; `uncomment-region-function' both point
  20767. ;; to`org-comment-or-uncomment-region'. Eventually,
  20768. ;; `org-insert-comment' takes care of insertion of comments at the
  20769. ;; beginning of line.
  20770. ;; `org-setup-comments-handling' install comments related variables
  20771. ;; during `org-mode' initialization.
  20772. (defun org-setup-comments-handling ()
  20773. (interactive)
  20774. (org-set-local 'comment-use-syntax nil)
  20775. (org-set-local 'comment-start "# ")
  20776. (org-set-local 'comment-start-skip "^\\s-*#\\(?: \\|$\\)")
  20777. (org-set-local 'comment-insert-comment-function 'org-insert-comment)
  20778. (org-set-local 'comment-region-function 'org-comment-or-uncomment-region)
  20779. (org-set-local 'uncomment-region-function 'org-comment-or-uncomment-region))
  20780. (defun org-insert-comment ()
  20781. "Insert an empty comment above current line.
  20782. If the line is empty, insert comment at its beginning."
  20783. (beginning-of-line)
  20784. (if (looking-at "\\s-*$") (replace-match "") (open-line 1))
  20785. (org-indent-line)
  20786. (insert "# "))
  20787. (defvar comment-empty-lines) ; From newcomment.el.
  20788. (defun org-comment-or-uncomment-region (beg end &rest ignore)
  20789. "Comment or uncomment each non-blank line in the region.
  20790. Uncomment each non-blank line between BEG and END if it only
  20791. contains commented lines. Otherwise, comment them."
  20792. (save-restriction
  20793. ;; Restrict region
  20794. (narrow-to-region (save-excursion (goto-char beg)
  20795. (skip-chars-forward " \r\t\n" end)
  20796. (line-beginning-position))
  20797. (save-excursion (goto-char end)
  20798. (skip-chars-backward " \r\t\n" beg)
  20799. (line-end-position)))
  20800. (let ((uncommentp
  20801. ;; UNCOMMENTP is non-nil when every non blank line between
  20802. ;; BEG and END is a comment.
  20803. (save-excursion
  20804. (goto-char (point-min))
  20805. (while (and (not (eobp))
  20806. (let ((element (org-element-at-point)))
  20807. (and (eq (org-element-type element) 'comment)
  20808. (goto-char (min (point-max)
  20809. (org-element-property
  20810. :end element)))))))
  20811. (eobp))))
  20812. (if uncommentp
  20813. ;; Only blank lines and comments in region: uncomment it.
  20814. (save-excursion
  20815. (goto-char (point-min))
  20816. (while (not (eobp))
  20817. (when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
  20818. (replace-match "" nil nil nil 1))
  20819. (forward-line)))
  20820. ;; Comment each line in region.
  20821. (let ((min-indent (point-max)))
  20822. ;; First find the minimum indentation across all lines.
  20823. (save-excursion
  20824. (goto-char (point-min))
  20825. (while (and (not (eobp)) (not (zerop min-indent)))
  20826. (unless (looking-at "[ \t]*$")
  20827. (setq min-indent (min min-indent (current-indentation))))
  20828. (forward-line)))
  20829. ;; Then loop over all lines.
  20830. (save-excursion
  20831. (goto-char (point-min))
  20832. (while (not (eobp))
  20833. (unless (and (not comment-empty-lines) (looking-at "[ \t]*$"))
  20834. ;; Don't get fooled by invisible text (e.g. link path)
  20835. ;; when moving to column MIN-INDENT.
  20836. (let ((buffer-invisibility-spec nil))
  20837. (org-move-to-column min-indent t))
  20838. (insert comment-start))
  20839. (forward-line))))))))
  20840. ;;; Planning
  20841. ;; This section contains tools to operate on timestamp objects, as
  20842. ;; returned by, e.g. `org-element-context'.
  20843. (defun org-timestamp-has-time-p (timestamp)
  20844. "Non-nil when TIMESTAMP has a time specified."
  20845. (org-element-property :hour-start timestamp))
  20846. (defun org-timestamp-format (timestamp format &optional end utc)
  20847. "Format a TIMESTAMP element into a string.
  20848. FORMAT is a format specifier to be passed to
  20849. `format-time-string'.
  20850. When optional argument END is non-nil, use end of date-range or
  20851. time-range, if possible.
  20852. When optional argument UTC is non-nil, time will be expressed as
  20853. Universal Time."
  20854. (format-time-string
  20855. format
  20856. (apply 'encode-time
  20857. (cons 0
  20858. (mapcar
  20859. (lambda (prop) (or (org-element-property prop timestamp) 0))
  20860. (if end '(:minute-end :hour-end :day-end :month-end :year-end)
  20861. '(:minute-start :hour-start :day-start :month-start
  20862. :year-start)))))
  20863. utc))
  20864. (defun org-timestamp-split-range (timestamp &optional end)
  20865. "Extract a timestamp object from a date or time range.
  20866. TIMESTAMP is a timestamp object. END, when non-nil, means extract
  20867. the end of the range. Otherwise, extract its start.
  20868. Return a new timestamp object sharing the same parent as
  20869. TIMESTAMP."
  20870. (let ((type (org-element-property :type timestamp)))
  20871. (if (memq type '(active inactive diary)) timestamp
  20872. (let ((split-ts (list 'timestamp (copy-sequence (nth 1 timestamp)))))
  20873. ;; Set new type.
  20874. (org-element-put-property
  20875. split-ts :type (if (eq type 'active-range) 'active 'inactive))
  20876. ;; Copy start properties over end properties if END is
  20877. ;; non-nil. Otherwise, copy end properties over `start' ones.
  20878. (let ((p-alist '((:minute-start . :minute-end)
  20879. (:hour-start . :hour-end)
  20880. (:day-start . :day-end)
  20881. (:month-start . :month-end)
  20882. (:year-start . :year-end))))
  20883. (dolist (p-cell p-alist)
  20884. (org-element-put-property
  20885. split-ts
  20886. (funcall (if end 'car 'cdr) p-cell)
  20887. (org-element-property
  20888. (funcall (if end 'cdr 'car) p-cell) split-ts)))
  20889. ;; Eventually refresh `:raw-value'.
  20890. (org-element-put-property split-ts :raw-value nil)
  20891. (org-element-put-property
  20892. split-ts :raw-value (org-element-interpret-data split-ts)))))))
  20893. (defun org-timestamp-translate (timestamp &optional boundary)
  20894. "Apply `org-translate-time' on a TIMESTAMP object.
  20895. When optional argument BOUNDARY is non-nil, it is either the
  20896. symbol `start' or `end'. In this case, only translate the
  20897. starting or ending part of TIMESTAMP if it is a date or time
  20898. range. Otherwise, translate both parts."
  20899. (if (and (not boundary)
  20900. (memq (org-element-property :type timestamp)
  20901. '(active-range inactive-range)))
  20902. (concat
  20903. (org-translate-time
  20904. (org-element-property :raw-value
  20905. (org-timestamp-split-range timestamp)))
  20906. "--"
  20907. (org-translate-time
  20908. (org-element-property :raw-value
  20909. (org-timestamp-split-range timestamp t))))
  20910. (org-translate-time
  20911. (org-element-property
  20912. :raw-value
  20913. (if (not boundary) timestamp
  20914. (org-timestamp-split-range timestamp (eq boundary 'end)))))))
  20915. ;;; Other stuff.
  20916. (defun org-toggle-fixed-width-section (arg)
  20917. "Toggle the fixed-width export.
  20918. If there is no active region, the QUOTE keyword at the current headline is
  20919. inserted or removed. When present, it causes the text between this headline
  20920. and the next to be exported as fixed-width text, and unmodified.
  20921. If there is an active region, this command adds or removes a colon as the
  20922. first character of this line. If the first character of a line is a colon,
  20923. this line is also exported in fixed-width font."
  20924. (interactive "P")
  20925. (let* ((cc 0)
  20926. (regionp (org-region-active-p))
  20927. (beg (if regionp (region-beginning) (point)))
  20928. (end (if regionp (region-end)))
  20929. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  20930. (case-fold-search nil)
  20931. (re "[ \t]*\\(:\\(?: \\|$\\)\\)")
  20932. off)
  20933. (if regionp
  20934. (save-excursion
  20935. (goto-char beg)
  20936. (setq cc (current-column))
  20937. (beginning-of-line 1)
  20938. (setq off (looking-at re))
  20939. (while (> nlines 0)
  20940. (setq nlines (1- nlines))
  20941. (beginning-of-line 1)
  20942. (cond
  20943. (arg
  20944. (org-move-to-column cc t)
  20945. (insert ": \n")
  20946. (forward-line -1))
  20947. ((and off (looking-at re))
  20948. (replace-match "" t t nil 1))
  20949. ((not off) (org-move-to-column cc t) (insert ": ")))
  20950. (forward-line 1)))
  20951. (save-excursion
  20952. (org-back-to-heading)
  20953. (cond
  20954. ((looking-at (format org-heading-keyword-regexp-format
  20955. org-quote-string))
  20956. (goto-char (match-end 1))
  20957. (looking-at (concat " +" org-quote-string))
  20958. (replace-match "" t t)
  20959. (when (eolp) (insert " ")))
  20960. ((looking-at org-outline-regexp)
  20961. (goto-char (match-end 0))
  20962. (insert org-quote-string " ")))))))
  20963. (defun org-reftex-citation ()
  20964. "Use reftex-citation to insert a citation into the buffer.
  20965. This looks for a line like
  20966. #+BIBLIOGRAPHY: foo plain option:-d
  20967. and derives from it that foo.bib is the bibliography file relevant
  20968. for this document. It then installs the necessary environment for RefTeX
  20969. to work in this buffer and calls `reftex-citation' to insert a citation
  20970. into the buffer.
  20971. Export of such citations to both LaTeX and HTML is handled by the contributed
  20972. package ox-bibtex by Taru Karttunen."
  20973. (interactive)
  20974. (let ((reftex-docstruct-symbol 'rds)
  20975. (reftex-cite-format "\\cite{%l}")
  20976. rds bib)
  20977. (save-excursion
  20978. (save-restriction
  20979. (widen)
  20980. (let ((case-fold-search t)
  20981. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  20982. (if (not (save-excursion
  20983. (or (re-search-forward re nil t)
  20984. (re-search-backward re nil t))))
  20985. (error "No bibliography defined in file")
  20986. (setq bib (concat (match-string 1) ".bib")
  20987. rds (list (list 'bib bib)))))))
  20988. (call-interactively 'reftex-citation)))
  20989. ;;;; Functions extending outline functionality
  20990. (defun org-beginning-of-line (&optional arg)
  20991. "Go to the beginning of the current line. If that is invisible, continue
  20992. to a visible line beginning. This makes the function of C-a more intuitive.
  20993. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  20994. first attempt, and only move to after the tags when the cursor is already
  20995. beyond the end of the headline."
  20996. (interactive "P")
  20997. (let ((pos (point))
  20998. (special (if (consp org-special-ctrl-a/e)
  20999. (car org-special-ctrl-a/e)
  21000. org-special-ctrl-a/e))
  21001. deactivate-mark refpos)
  21002. (if (org-bound-and-true-p visual-line-mode)
  21003. (beginning-of-visual-line 1)
  21004. (beginning-of-line 1))
  21005. (if (and arg (fboundp 'move-beginning-of-line))
  21006. (call-interactively 'move-beginning-of-line)
  21007. (if (bobp)
  21008. nil
  21009. (backward-char 1)
  21010. (if (org-truely-invisible-p)
  21011. (while (and (not (bobp)) (org-truely-invisible-p))
  21012. (backward-char 1)
  21013. (beginning-of-line 1))
  21014. (forward-char 1))))
  21015. (when special
  21016. (cond
  21017. ((and (looking-at org-complex-heading-regexp)
  21018. (= (char-after (match-end 1)) ?\ ))
  21019. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  21020. (point-at-eol)))
  21021. (goto-char
  21022. (if (eq special t)
  21023. (cond ((> pos refpos) refpos)
  21024. ((= pos (point)) refpos)
  21025. (t (point)))
  21026. (cond ((> pos (point)) (point))
  21027. ((not (eq last-command this-command)) (point))
  21028. (t refpos)))))
  21029. ((org-at-item-p)
  21030. ;; Being at an item and not looking at an the item means point
  21031. ;; was previously moved to beginning of a visual line, which
  21032. ;; doesn't contain the item. Therefore, do nothing special,
  21033. ;; just stay here.
  21034. (when (looking-at org-list-full-item-re)
  21035. ;; Set special position at first white space character after
  21036. ;; bullet, and check-box, if any.
  21037. (let ((after-bullet
  21038. (let ((box (match-end 3)))
  21039. (if (not box) (match-end 1)
  21040. (let ((after (char-after box)))
  21041. (if (and after (= after ? )) (1+ box) box))))))
  21042. ;; Special case: Move point to special position when
  21043. ;; currently after it or at beginning of line.
  21044. (if (eq special t)
  21045. (when (or (> pos after-bullet) (= (point) pos))
  21046. (goto-char after-bullet))
  21047. ;; Reversed case: Move point to special position when
  21048. ;; point was already at beginning of line and command is
  21049. ;; repeated.
  21050. (when (and (= (point) pos) (eq last-command this-command))
  21051. (goto-char after-bullet))))))))
  21052. (org-no-warnings
  21053. (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  21054. (setq disable-point-adjustment
  21055. (or (not (invisible-p (point)))
  21056. (not (invisible-p (max (point-min) (1- (point))))))))
  21057. (defun org-end-of-line (&optional arg)
  21058. "Go to the end of the line.
  21059. If this is a headline, and `org-special-ctrl-a/e' is set, ignore
  21060. tags on the first attempt, and only move to after the tags when
  21061. the cursor is already beyond the end of the headline."
  21062. (interactive "P")
  21063. (let ((special (if (consp org-special-ctrl-a/e) (cdr org-special-ctrl-a/e)
  21064. org-special-ctrl-a/e))
  21065. (move-fun (cond ((org-bound-and-true-p visual-line-mode)
  21066. 'end-of-visual-line)
  21067. ((fboundp 'move-end-of-line) 'move-end-of-line)
  21068. (t 'end-of-line)))
  21069. deactivate-mark)
  21070. (if (or (not special) arg) (call-interactively move-fun)
  21071. (let* ((element (save-excursion (beginning-of-line)
  21072. (org-element-at-point)))
  21073. (type (org-element-type element)))
  21074. (cond
  21075. ((memq type '(headline inlinetask))
  21076. (let ((pos (point)))
  21077. (beginning-of-line 1)
  21078. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  21079. (if (eq special t)
  21080. (if (or (< pos (match-beginning 1)) (= pos (match-end 0)))
  21081. (goto-char (match-beginning 1))
  21082. (goto-char (match-end 0)))
  21083. (if (or (< pos (match-end 0))
  21084. (not (eq this-command last-command)))
  21085. (goto-char (match-end 0))
  21086. (goto-char (match-beginning 1))))
  21087. (call-interactively move-fun))))
  21088. ((org-element-property :hiddenp element)
  21089. ;; If element is hidden, `move-end-of-line' would put point
  21090. ;; after it. Use `end-of-line' to stay on current line.
  21091. (call-interactively 'end-of-line))
  21092. (t (call-interactively move-fun)))))
  21093. (org-no-warnings (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  21094. (setq disable-point-adjustment
  21095. (or (not (invisible-p (point)))
  21096. (not (invisible-p (max (point-min) (1- (point))))))))
  21097. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  21098. (define-key org-mode-map "\C-e" 'org-end-of-line)
  21099. (defun org-backward-sentence (&optional arg)
  21100. "Go to beginning of sentence, or beginning of table field.
  21101. This will call `backward-sentence' or `org-table-beginning-of-field',
  21102. depending on context."
  21103. (interactive "P")
  21104. (cond
  21105. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  21106. (t (call-interactively 'backward-sentence))))
  21107. (defun org-forward-sentence (&optional arg)
  21108. "Go to end of sentence, or end of table field.
  21109. This will call `forward-sentence' or `org-table-end-of-field',
  21110. depending on context."
  21111. (interactive "P")
  21112. (cond
  21113. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  21114. (t (call-interactively 'forward-sentence))))
  21115. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  21116. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  21117. (defun org-kill-line (&optional arg)
  21118. "Kill line, to tags or end of line."
  21119. (interactive "P")
  21120. (cond
  21121. ((or (not org-special-ctrl-k)
  21122. (bolp)
  21123. (not (org-at-heading-p)))
  21124. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  21125. org-ctrl-k-protect-subtree)
  21126. (if (or (eq org-ctrl-k-protect-subtree 'error)
  21127. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  21128. (user-error "C-k aborted as it would kill a hidden subtree")))
  21129. (call-interactively
  21130. (if (org-bound-and-true-p visual-line-mode) 'kill-visual-line 'kill-line)))
  21131. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  21132. (kill-region (point) (match-beginning 1))
  21133. (org-set-tags nil t))
  21134. (t (kill-region (point) (point-at-eol)))))
  21135. (define-key org-mode-map "\C-k" 'org-kill-line)
  21136. (defun org-yank (&optional arg)
  21137. "Yank. If the kill is a subtree, treat it specially.
  21138. This command will look at the current kill and check if is a single
  21139. subtree, or a series of subtrees[1]. If it passes the test, and if the
  21140. cursor is at the beginning of a line or after the stars of a currently
  21141. empty headline, then the yank is handled specially. How exactly depends
  21142. on the value of the following variables, both set by default.
  21143. org-yank-folded-subtrees
  21144. When set, the subtree(s) will be folded after insertion, but only
  21145. if doing so would now swallow text after the yanked text.
  21146. org-yank-adjusted-subtrees
  21147. When set, the subtree will be promoted or demoted in order to
  21148. fit into the local outline tree structure, which means that the level
  21149. will be adjusted so that it becomes the smaller one of the two
  21150. *visible* surrounding headings.
  21151. Any prefix to this command will cause `yank' to be called directly with
  21152. no special treatment. In particular, a simple \\[universal-argument] prefix \
  21153. will just
  21154. plainly yank the text as it is.
  21155. \[1] The test checks if the first non-white line is a heading
  21156. and if there are no other headings with fewer stars."
  21157. (interactive "P")
  21158. (org-yank-generic 'yank arg))
  21159. (defun org-yank-generic (command arg)
  21160. "Perform some yank-like command.
  21161. This function implements the behavior described in the `org-yank'
  21162. documentation. However, it has been generalized to work for any
  21163. interactive command with similar behavior."
  21164. ;; pretend to be command COMMAND
  21165. (setq this-command command)
  21166. (if arg
  21167. (call-interactively command)
  21168. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  21169. (and (org-kill-is-subtree-p)
  21170. (or (bolp)
  21171. (and (looking-at "[ \t]*$")
  21172. (string-match
  21173. "\\`\\*+\\'"
  21174. (buffer-substring (point-at-bol) (point)))))))
  21175. swallowp)
  21176. (cond
  21177. ((and subtreep org-yank-folded-subtrees)
  21178. (let ((beg (point))
  21179. end)
  21180. (if (and subtreep org-yank-adjusted-subtrees)
  21181. (org-paste-subtree nil nil 'for-yank)
  21182. (call-interactively command))
  21183. (setq end (point))
  21184. (goto-char beg)
  21185. (when (and (bolp) subtreep
  21186. (not (setq swallowp
  21187. (org-yank-folding-would-swallow-text beg end))))
  21188. (org-with-limited-levels
  21189. (or (looking-at org-outline-regexp)
  21190. (re-search-forward org-outline-regexp-bol end t))
  21191. (while (and (< (point) end) (looking-at org-outline-regexp))
  21192. (hide-subtree)
  21193. (org-cycle-show-empty-lines 'folded)
  21194. (condition-case nil
  21195. (outline-forward-same-level 1)
  21196. (error (goto-char end))))))
  21197. (when swallowp
  21198. (message
  21199. "Inserted text not folded because that would swallow text"))
  21200. (goto-char end)
  21201. (skip-chars-forward " \t\n\r")
  21202. (beginning-of-line 1)
  21203. (push-mark beg 'nomsg)))
  21204. ((and subtreep org-yank-adjusted-subtrees)
  21205. (let ((beg (point-at-bol)))
  21206. (org-paste-subtree nil nil 'for-yank)
  21207. (push-mark beg 'nomsg)))
  21208. (t
  21209. (call-interactively command))))))
  21210. (defun org-yank-folding-would-swallow-text (beg end)
  21211. "Would hide-subtree at BEG swallow any text after END?"
  21212. (let (level)
  21213. (org-with-limited-levels
  21214. (save-excursion
  21215. (goto-char beg)
  21216. (when (or (looking-at org-outline-regexp)
  21217. (re-search-forward org-outline-regexp-bol end t))
  21218. (setq level (org-outline-level)))
  21219. (goto-char end)
  21220. (skip-chars-forward " \t\r\n\v\f")
  21221. (if (or (eobp)
  21222. (and (bolp) (looking-at org-outline-regexp)
  21223. (<= (org-outline-level) level)))
  21224. nil ; Nothing would be swallowed
  21225. t))))) ; something would swallow
  21226. (define-key org-mode-map "\C-y" 'org-yank)
  21227. (defun org-truely-invisible-p ()
  21228. "Check if point is at a character currently not visible.
  21229. This version does not only check the character property, but also
  21230. `visible-mode'."
  21231. ;; Early versions of noutline don't have `outline-invisible-p'.
  21232. (if (org-bound-and-true-p visible-mode)
  21233. nil
  21234. (outline-invisible-p)))
  21235. (defun org-invisible-p2 ()
  21236. "Check if point is at a character currently not visible."
  21237. (save-excursion
  21238. (if (and (eolp) (not (bobp))) (backward-char 1))
  21239. ;; Early versions of noutline don't have `outline-invisible-p'.
  21240. (outline-invisible-p)))
  21241. (defun org-back-to-heading (&optional invisible-ok)
  21242. "Call `outline-back-to-heading', but provide a better error message."
  21243. (condition-case nil
  21244. (outline-back-to-heading invisible-ok)
  21245. (error (error "Before first headline at position %d in buffer %s"
  21246. (point) (current-buffer)))))
  21247. (defun org-before-first-heading-p ()
  21248. "Before first heading?"
  21249. (save-excursion
  21250. (end-of-line)
  21251. (null (re-search-backward org-outline-regexp-bol nil t))))
  21252. (defun org-at-heading-p (&optional ignored)
  21253. (outline-on-heading-p t))
  21254. ;; Compatibility alias with Org versions < 7.8.03
  21255. (defalias 'org-on-heading-p 'org-at-heading-p)
  21256. (defun org-at-comment-p nil
  21257. "Is cursor in a line starting with a # character?"
  21258. (save-excursion
  21259. (beginning-of-line)
  21260. (looking-at "^#")))
  21261. (defun org-at-drawer-p nil
  21262. "Is cursor at a drawer keyword?"
  21263. (save-excursion
  21264. (move-beginning-of-line 1)
  21265. (looking-at org-drawer-regexp)))
  21266. (defun org-at-block-p nil
  21267. "Is cursor at a block keyword?"
  21268. (save-excursion
  21269. (move-beginning-of-line 1)
  21270. (looking-at org-block-regexp)))
  21271. (defun org-point-at-end-of-empty-headline ()
  21272. "If point is at the end of an empty headline, return t, else nil.
  21273. If the heading only contains a TODO keyword, it is still still considered
  21274. empty."
  21275. (and (looking-at "[ \t]*$")
  21276. (when org-todo-line-regexp
  21277. (save-excursion
  21278. (beginning-of-line 1)
  21279. (let ((case-fold-search nil))
  21280. (looking-at org-todo-line-regexp)
  21281. (string= (match-string 3) ""))))))
  21282. (defun org-at-heading-or-item-p ()
  21283. (or (org-at-heading-p) (org-at-item-p)))
  21284. (defun org-at-target-p ()
  21285. (or (org-in-regexp org-radio-target-regexp)
  21286. (org-in-regexp org-target-regexp)))
  21287. ;; Compatibility alias with Org versions < 7.8.03
  21288. (defalias 'org-on-target-p 'org-at-target-p)
  21289. (defun org-up-heading-all (arg)
  21290. "Move to the heading line of which the present line is a subheading.
  21291. This function considers both visible and invisible heading lines.
  21292. With argument, move up ARG levels."
  21293. (if (fboundp 'outline-up-heading-all)
  21294. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  21295. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  21296. (defun org-up-heading-safe ()
  21297. "Move to the heading line of which the present line is a subheading.
  21298. This version will not throw an error. It will return the level of the
  21299. headline found, or nil if no higher level is found.
  21300. Also, this function will be a lot faster than `outline-up-heading',
  21301. because it relies on stars being the outline starters. This can really
  21302. make a significant difference in outlines with very many siblings."
  21303. (let (start-level re)
  21304. (org-back-to-heading t)
  21305. (setq start-level (funcall outline-level))
  21306. (if (equal start-level 1)
  21307. nil
  21308. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  21309. (if (re-search-backward re nil t)
  21310. (funcall outline-level)))))
  21311. (defun org-first-sibling-p ()
  21312. "Is this heading the first child of its parents?"
  21313. (interactive)
  21314. (let ((re org-outline-regexp-bol)
  21315. level l)
  21316. (unless (org-at-heading-p t)
  21317. (user-error "Not at a heading"))
  21318. (setq level (funcall outline-level))
  21319. (save-excursion
  21320. (if (not (re-search-backward re nil t))
  21321. t
  21322. (setq l (funcall outline-level))
  21323. (< l level)))))
  21324. (defun org-goto-sibling (&optional previous)
  21325. "Goto the next sibling, even if it is invisible.
  21326. When PREVIOUS is set, go to the previous sibling instead. Returns t
  21327. when a sibling was found. When none is found, return nil and don't
  21328. move point."
  21329. (let ((fun (if previous 're-search-backward 're-search-forward))
  21330. (pos (point))
  21331. (re org-outline-regexp-bol)
  21332. level l)
  21333. (when (condition-case nil (org-back-to-heading t) (error nil))
  21334. (setq level (funcall outline-level))
  21335. (catch 'exit
  21336. (or previous (forward-char 1))
  21337. (while (funcall fun re nil t)
  21338. (setq l (funcall outline-level))
  21339. (when (< l level) (goto-char pos) (throw 'exit nil))
  21340. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  21341. (goto-char pos)
  21342. nil))))
  21343. (defun org-show-siblings ()
  21344. "Show all siblings of the current headline."
  21345. (save-excursion
  21346. (while (org-goto-sibling) (org-flag-heading nil)))
  21347. (save-excursion
  21348. (while (org-goto-sibling 'previous)
  21349. (org-flag-heading nil))))
  21350. (defun org-goto-first-child ()
  21351. "Goto the first child, even if it is invisible.
  21352. Return t when a child was found. Otherwise don't move point and
  21353. return nil."
  21354. (let (level (pos (point)) (re org-outline-regexp-bol))
  21355. (when (condition-case nil (org-back-to-heading t) (error nil))
  21356. (setq level (outline-level))
  21357. (forward-char 1)
  21358. (if (and (re-search-forward re nil t) (> (outline-level) level))
  21359. (progn (goto-char (match-beginning 0)) t)
  21360. (goto-char pos) nil))))
  21361. (defun org-show-hidden-entry ()
  21362. "Show an entry where even the heading is hidden."
  21363. (save-excursion
  21364. (org-show-entry)))
  21365. (defun org-flag-heading (flag &optional entry)
  21366. "Flag the current heading. FLAG non-nil means make invisible.
  21367. When ENTRY is non-nil, show the entire entry."
  21368. (save-excursion
  21369. (org-back-to-heading t)
  21370. ;; Check if we should show the entire entry
  21371. (if entry
  21372. (progn
  21373. (org-show-entry)
  21374. (save-excursion
  21375. (and (outline-next-heading)
  21376. (org-flag-heading nil))))
  21377. (outline-flag-region (max (point-min) (1- (point)))
  21378. (save-excursion (outline-end-of-heading) (point))
  21379. flag))))
  21380. (defun org-get-next-sibling ()
  21381. "Move to next heading of the same level, and return point.
  21382. If there is no such heading, return nil.
  21383. This is like outline-next-sibling, but invisible headings are ok."
  21384. (let ((level (funcall outline-level)))
  21385. (outline-next-heading)
  21386. (while (and (not (eobp)) (> (funcall outline-level) level))
  21387. (outline-next-heading))
  21388. (if (or (eobp) (< (funcall outline-level) level))
  21389. nil
  21390. (point))))
  21391. (defun org-get-last-sibling ()
  21392. "Move to previous heading of the same level, and return point.
  21393. If there is no such heading, return nil."
  21394. (let ((opoint (point))
  21395. (level (funcall outline-level)))
  21396. (outline-previous-heading)
  21397. (when (and (/= (point) opoint) (outline-on-heading-p t))
  21398. (while (and (> (funcall outline-level) level)
  21399. (not (bobp)))
  21400. (outline-previous-heading))
  21401. (if (< (funcall outline-level) level)
  21402. nil
  21403. (point)))))
  21404. (defun org-end-of-subtree (&optional invisible-ok to-heading)
  21405. "Goto to the end of a subtree."
  21406. ;; This contains an exact copy of the original function, but it uses
  21407. ;; `org-back-to-heading', to make it work also in invisible
  21408. ;; trees. And is uses an invisible-ok argument.
  21409. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  21410. ;; Furthermore, when used inside Org, finding the end of a large subtree
  21411. ;; with many children and grandchildren etc, this can be much faster
  21412. ;; than the outline version.
  21413. (org-back-to-heading invisible-ok)
  21414. (let ((first t)
  21415. (level (funcall outline-level)))
  21416. (if (and (derived-mode-p 'org-mode) (< level 1000))
  21417. ;; A true heading (not a plain list item), in Org-mode
  21418. ;; This means we can easily find the end by looking
  21419. ;; only for the right number of stars. Using a regexp to do
  21420. ;; this is so much faster than using a Lisp loop.
  21421. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  21422. (forward-char 1)
  21423. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  21424. ;; something else, do it the slow way
  21425. (while (and (not (eobp))
  21426. (or first (> (funcall outline-level) level)))
  21427. (setq first nil)
  21428. (outline-next-heading)))
  21429. (unless to-heading
  21430. (if (memq (preceding-char) '(?\n ?\^M))
  21431. (progn
  21432. ;; Go to end of line before heading
  21433. (forward-char -1)
  21434. (if (memq (preceding-char) '(?\n ?\^M))
  21435. ;; leave blank line before heading
  21436. (forward-char -1))))))
  21437. (point))
  21438. (defun org-end-of-meta-data-and-drawers ()
  21439. "Jump to the first text after meta data and drawers in the current entry.
  21440. This will move over empty lines, lines with planning time stamps,
  21441. clocking lines, and drawers."
  21442. (org-back-to-heading t)
  21443. (let ((end (save-excursion (outline-next-heading) (point)))
  21444. (re (concat "\\(" org-drawer-regexp "\\)"
  21445. "\\|" "[ \t]*" org-keyword-time-regexp)))
  21446. (forward-line 1)
  21447. (while (re-search-forward re end t)
  21448. (if (not (match-end 1))
  21449. ;; empty or planning line
  21450. (forward-line 1)
  21451. ;; a drawer, find the end
  21452. (re-search-forward "^[ \t]*:END:" end 'move)
  21453. (forward-line 1)))
  21454. (and (re-search-forward "[^\n]" nil t) (backward-char 1))
  21455. (point)))
  21456. (defun org-forward-heading-same-level (arg &optional invisible-ok)
  21457. "Move forward to the ARG'th subheading at same level as this one.
  21458. Stop at the first and last subheadings of a superior heading.
  21459. Normally this only looks at visible headings, but when INVISIBLE-OK is
  21460. non-nil it will also look at invisible ones."
  21461. (interactive "p")
  21462. (if (not (ignore-errors (org-back-to-heading invisible-ok)))
  21463. (if (and arg (< arg 0))
  21464. (goto-char (point-min))
  21465. (outline-next-heading))
  21466. (org-at-heading-p)
  21467. (let ((level (- (match-end 0) (match-beginning 0) 1))
  21468. (f (if (and arg (< arg 0))
  21469. 're-search-backward
  21470. 're-search-forward))
  21471. (count (if arg (abs arg) 1))
  21472. (result (point)))
  21473. (while (and (prog1 (> count 0)
  21474. (forward-char (if (and arg (< arg 0)) -1 1)))
  21475. (funcall f org-outline-regexp-bol nil 'move))
  21476. (let ((l (- (match-end 0) (match-beginning 0) 1)))
  21477. (cond ((< l level) (setq count 0))
  21478. ((and (= l level)
  21479. (or invisible-ok
  21480. (progn
  21481. (goto-char (line-beginning-position))
  21482. (not (outline-invisible-p)))))
  21483. (setq count (1- count))
  21484. (when (eq l level)
  21485. (setq result (point)))))))
  21486. (goto-char result))
  21487. (beginning-of-line 1)))
  21488. (defun org-backward-heading-same-level (arg &optional invisible-ok)
  21489. "Move backward to the ARG'th subheading at same level as this one.
  21490. Stop at the first and last subheadings of a superior heading."
  21491. (interactive "p")
  21492. (org-forward-heading-same-level (if arg (- arg) -1) invisible-ok))
  21493. (defun org-next-block (arg &optional backward block-regexp)
  21494. "Jump to the next block.
  21495. With a prefix argument ARG, jump forward ARG many source blocks.
  21496. When BACKWARD is non-nil, jump to the previous block.
  21497. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  21498. (interactive "p")
  21499. (let ((re (or block-regexp org-block-regexp))
  21500. (re-search-fn (or (and backward 're-search-backward)
  21501. 're-search-forward)))
  21502. (if (looking-at re) (forward-char 1))
  21503. (condition-case nil
  21504. (funcall re-search-fn re nil nil arg)
  21505. (error (error "No %s code blocks" (if backward "previous" "further" ))))
  21506. (goto-char (match-beginning 0)) (org-show-context)))
  21507. (defun org-previous-block (arg &optional block-regexp)
  21508. "Jump to the previous block.
  21509. With a prefix argument ARG, jump backward ARG many source blocks.
  21510. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  21511. (interactive "p")
  21512. (org-next-block arg t block-regexp))
  21513. (defun org-forward-paragraph ()
  21514. "Move forward to beginning of next paragraph or equivalent.
  21515. The function moves point to the beginning of the next visible
  21516. structural element, which can be a paragraph, a table, a list
  21517. item, etc. It also provides some special moves for convenience:
  21518. - On an affiliated keyword, jump to the beginning of the
  21519. relative element.
  21520. - On an item or a footnote definition, move to the second
  21521. element inside, if any.
  21522. - On a table or a property drawer, jump after it.
  21523. - On a verse or source block, stop after blank lines."
  21524. (interactive)
  21525. (when (eobp) (user-error "Cannot move further down"))
  21526. (let* ((deactivate-mark nil)
  21527. (element (org-element-at-point))
  21528. (type (org-element-type element))
  21529. (post-affiliated (org-element-property :post-affiliated element))
  21530. (contents-begin (org-element-property :contents-begin element))
  21531. (contents-end (org-element-property :contents-end element))
  21532. (end (let ((end (org-element-property :end element)) (parent element))
  21533. (while (and (setq parent (org-element-property :parent parent))
  21534. (= (org-element-property :contents-end parent) end))
  21535. (setq end (org-element-property :end parent)))
  21536. end)))
  21537. (cond ((not element)
  21538. (skip-chars-forward " \r\t\n")
  21539. (or (eobp) (beginning-of-line)))
  21540. ;; On affiliated keywords, move to element's beginning.
  21541. ((and post-affiliated (< (point) post-affiliated))
  21542. (goto-char post-affiliated))
  21543. ;; At a table row, move to the end of the table. Similarly,
  21544. ;; at a node property, move to the end of the property
  21545. ;; drawer.
  21546. ((memq type '(node-property table-row))
  21547. (goto-char (org-element-property
  21548. :end (org-element-property :parent element))))
  21549. ((memq type '(property-drawer table)) (goto-char end))
  21550. ;; Consider blank lines as separators in verse and source
  21551. ;; blocks to ease editing.
  21552. ((memq type '(src-block verse-block))
  21553. (when (eq type 'src-block)
  21554. (setq contents-end
  21555. (save-excursion (goto-char end)
  21556. (skip-chars-backward " \r\t\n")
  21557. (line-beginning-position))))
  21558. (beginning-of-line)
  21559. (when (looking-at "[ \t]*$") (skip-chars-forward " \r\t\n"))
  21560. (if (not (re-search-forward "^[ \t]*$" contents-end t))
  21561. (goto-char end)
  21562. (skip-chars-forward " \r\t\n")
  21563. (if (= (point) contents-end) (goto-char end)
  21564. (beginning-of-line))))
  21565. ;; With no contents, just skip element.
  21566. ((not contents-begin) (goto-char end))
  21567. ;; If contents are invisible, skip the element altogether.
  21568. ((outline-invisible-p (line-end-position))
  21569. (case type
  21570. (headline
  21571. (org-with-limited-levels (outline-next-visible-heading 1)))
  21572. ;; At a plain list, make sure we move to the next item
  21573. ;; instead of skipping the whole list.
  21574. (plain-list (forward-char)
  21575. (org-forward-paragraph))
  21576. (otherwise (goto-char end))))
  21577. ((>= (point) contents-end) (goto-char end))
  21578. ((>= (point) contents-begin)
  21579. ;; This can only happen on paragraphs and plain lists.
  21580. (case type
  21581. (paragraph (goto-char end))
  21582. ;; At a plain list, try to move to second element in
  21583. ;; first item, if possible.
  21584. (plain-list (end-of-line)
  21585. (org-forward-paragraph))))
  21586. ;; When contents start on the middle of a line (e.g. in
  21587. ;; items and footnote definitions), try to reach first
  21588. ;; element starting after current line.
  21589. ((> (line-end-position) contents-begin)
  21590. (end-of-line)
  21591. (org-forward-paragraph))
  21592. (t (goto-char contents-begin)))))
  21593. (defun org-backward-paragraph ()
  21594. "Move backward to start of previous paragraph or equivalent.
  21595. The function moves point to the beginning of the current
  21596. structural element, which can be a paragraph, a table, a list
  21597. item, etc., or to the beginning of the previous visible one if
  21598. point is already there. It also provides some special moves for
  21599. convenience:
  21600. - On an affiliated keyword, jump to the first one.
  21601. - On a table or a property drawer, move to its beginning.
  21602. - On a verse or source block, stop before blank lines."
  21603. (interactive)
  21604. (when (bobp) (user-error "Cannot move further up"))
  21605. (let* ((deactivate-mark nil)
  21606. (element (org-element-at-point))
  21607. (type (org-element-type element))
  21608. (contents-begin (org-element-property :contents-begin element))
  21609. (contents-end (org-element-property :contents-end element))
  21610. (post-affiliated (org-element-property :post-affiliated element))
  21611. (begin (org-element-property :begin element)))
  21612. (cond
  21613. ((not element) (goto-char (point-min)))
  21614. ((= (point) begin)
  21615. (backward-char)
  21616. (org-backward-paragraph))
  21617. ((and post-affiliated (<= (point) post-affiliated)) (goto-char begin))
  21618. ((memq type '(node-property table-row))
  21619. (goto-char (org-element-property
  21620. :post-affiliated (org-element-property :parent element))))
  21621. ((memq type '(property-drawer table)) (goto-char begin))
  21622. ((memq type '(src-block verse-block))
  21623. (when (eq type 'src-block)
  21624. (setq contents-begin
  21625. (save-excursion (goto-char begin) (forward-line) (point))))
  21626. (if (= (point) contents-begin) (goto-char post-affiliated)
  21627. ;; Inside a verse block, see blank lines as paragraph
  21628. ;; separators.
  21629. (let ((origin (point)))
  21630. (skip-chars-backward " \r\t\n" contents-begin)
  21631. (when (re-search-backward "^[ \t]*$" contents-begin 'move)
  21632. (skip-chars-forward " \r\t\n" origin)
  21633. (if (= (point) origin) (goto-char contents-begin)
  21634. (beginning-of-line))))))
  21635. ((not contents-begin) (goto-char (or post-affiliated begin)))
  21636. ((eq type 'paragraph)
  21637. (goto-char contents-begin)
  21638. ;; When at first paragraph in an item or a footnote definition,
  21639. ;; move directly to beginning of line.
  21640. (let ((parent-contents
  21641. (org-element-property
  21642. :contents-begin (org-element-property :parent element))))
  21643. (when (and parent-contents (= parent-contents contents-begin))
  21644. (beginning-of-line))))
  21645. ;; At the end of a greater element, move to the beginning of the
  21646. ;; last element within.
  21647. ((>= (point) contents-end)
  21648. (goto-char (1- contents-end))
  21649. (org-backward-paragraph))
  21650. (t (goto-char (or post-affiliated begin))))
  21651. ;; Ensure we never leave point invisible.
  21652. (when (outline-invisible-p (point)) (beginning-of-visual-line))))
  21653. (defun org-forward-element ()
  21654. "Move forward by one element.
  21655. Move to the next element at the same level, when possible."
  21656. (interactive)
  21657. (cond ((eobp) (user-error "Cannot move further down"))
  21658. ((org-with-limited-levels (org-at-heading-p))
  21659. (let ((origin (point)))
  21660. (goto-char (org-end-of-subtree nil t))
  21661. (unless (org-with-limited-levels (org-at-heading-p))
  21662. (goto-char origin)
  21663. (user-error "Cannot move further down"))))
  21664. (t
  21665. (let* ((elem (org-element-at-point))
  21666. (end (org-element-property :end elem))
  21667. (parent (org-element-property :parent elem)))
  21668. (cond ((and parent (= (org-element-property :contents-end parent) end))
  21669. (goto-char (org-element-property :end parent)))
  21670. ((integer-or-marker-p end) (goto-char end))
  21671. (t (message "No element at point")))))))
  21672. (defun org-backward-element ()
  21673. "Move backward by one element.
  21674. Move to the previous element at the same level, when possible."
  21675. (interactive)
  21676. (cond ((bobp) (user-error "Cannot move further up"))
  21677. ((org-with-limited-levels (org-at-heading-p))
  21678. ;; At a headline, move to the previous one, if any, or stay
  21679. ;; here.
  21680. (let ((origin (point)))
  21681. (org-with-limited-levels (org-backward-heading-same-level 1))
  21682. ;; When current headline has no sibling above, move to its
  21683. ;; parent.
  21684. (when (= (point) origin)
  21685. (or (org-with-limited-levels (org-up-heading-safe))
  21686. (progn (goto-char origin)
  21687. (user-error "Cannot move further up"))))))
  21688. (t
  21689. (let* ((trail (org-element-at-point 'keep-trail))
  21690. (elem (car trail))
  21691. (prev-elem (nth 1 trail))
  21692. (beg (org-element-property :begin elem)))
  21693. (cond
  21694. ;; Move to beginning of current element if point isn't
  21695. ;; there already.
  21696. ((null beg) (message "No element at point"))
  21697. ((/= (point) beg) (goto-char beg))
  21698. (prev-elem (goto-char (org-element-property :begin prev-elem)))
  21699. ((org-before-first-heading-p) (goto-char (point-min)))
  21700. (t (org-back-to-heading)))))))
  21701. (defun org-up-element ()
  21702. "Move to upper element."
  21703. (interactive)
  21704. (if (org-with-limited-levels (org-at-heading-p))
  21705. (unless (org-up-heading-safe) (user-error "No surrounding element"))
  21706. (let* ((elem (org-element-at-point))
  21707. (parent (org-element-property :parent elem)))
  21708. (if parent (goto-char (org-element-property :begin parent))
  21709. (if (org-with-limited-levels (org-before-first-heading-p))
  21710. (user-error "No surrounding element")
  21711. (org-with-limited-levels (org-back-to-heading)))))))
  21712. (defvar org-element-greater-elements)
  21713. (defun org-down-element ()
  21714. "Move to inner element."
  21715. (interactive)
  21716. (let ((element (org-element-at-point)))
  21717. (cond
  21718. ((memq (org-element-type element) '(plain-list table))
  21719. (goto-char (org-element-property :contents-begin element))
  21720. (forward-char))
  21721. ((memq (org-element-type element) org-element-greater-elements)
  21722. ;; If contents are hidden, first disclose them.
  21723. (when (org-element-property :hiddenp element) (org-cycle))
  21724. (goto-char (or (org-element-property :contents-begin element)
  21725. (user-error "No content for this element"))))
  21726. (t (user-error "No inner element")))))
  21727. (defun org-drag-element-backward ()
  21728. "Move backward element at point."
  21729. (interactive)
  21730. (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
  21731. (let* ((trail (org-element-at-point 'keep-trail))
  21732. (elem (car trail))
  21733. (prev-elem (nth 1 trail)))
  21734. ;; Error out if no previous element or previous element is
  21735. ;; a parent of the current one.
  21736. (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
  21737. (user-error "Cannot drag element backward")
  21738. (let ((pos (point)))
  21739. (org-element-swap-A-B prev-elem elem)
  21740. (goto-char (+ (org-element-property :begin prev-elem)
  21741. (- pos (org-element-property :begin elem)))))))))
  21742. (defun org-drag-element-forward ()
  21743. "Move forward element at point."
  21744. (interactive)
  21745. (let* ((pos (point))
  21746. (elem (org-element-at-point)))
  21747. (when (= (point-max) (org-element-property :end elem))
  21748. (user-error "Cannot drag element forward"))
  21749. (goto-char (org-element-property :end elem))
  21750. (let ((next-elem (org-element-at-point)))
  21751. (when (or (org-element-nested-p elem next-elem)
  21752. (and (eq (org-element-type next-elem) 'headline)
  21753. (not (eq (org-element-type elem) 'headline))))
  21754. (goto-char pos)
  21755. (user-error "Cannot drag element forward"))
  21756. ;; Compute new position of point: it's shifted by NEXT-ELEM
  21757. ;; body's length (without final blanks) and by the length of
  21758. ;; blanks between ELEM and NEXT-ELEM.
  21759. (let ((size-next (- (save-excursion
  21760. (goto-char (org-element-property :end next-elem))
  21761. (skip-chars-backward " \r\t\n")
  21762. (forward-line)
  21763. ;; Small correction if buffer doesn't end
  21764. ;; with a newline character.
  21765. (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
  21766. (org-element-property :begin next-elem)))
  21767. (size-blank (- (org-element-property :end elem)
  21768. (save-excursion
  21769. (goto-char (org-element-property :end elem))
  21770. (skip-chars-backward " \r\t\n")
  21771. (forward-line)
  21772. (point)))))
  21773. (org-element-swap-A-B elem next-elem)
  21774. (goto-char (+ pos size-next size-blank))))))
  21775. (defun org-drag-line-forward (arg)
  21776. "Drag the line at point ARG lines forward."
  21777. (interactive "p")
  21778. (dotimes (n (abs arg))
  21779. (let ((c (current-column)))
  21780. (if (< 0 arg)
  21781. (progn
  21782. (beginning-of-line 2)
  21783. (transpose-lines 1)
  21784. (beginning-of-line 0))
  21785. (transpose-lines 1)
  21786. (beginning-of-line -1))
  21787. (org-move-to-column c))))
  21788. (defun org-drag-line-backward (arg)
  21789. "Drag the line at point ARG lines backward."
  21790. (interactive "p")
  21791. (org-drag-line-forward (- arg)))
  21792. (defun org-mark-element ()
  21793. "Put point at beginning of this element, mark at end.
  21794. Interactively, if this command is repeated or (in Transient Mark
  21795. mode) if the mark is active, it marks the next element after the
  21796. ones already marked."
  21797. (interactive)
  21798. (let (deactivate-mark)
  21799. (if (and (org-called-interactively-p 'any)
  21800. (or (and (eq last-command this-command) (mark t))
  21801. (and transient-mark-mode mark-active)))
  21802. (set-mark
  21803. (save-excursion
  21804. (goto-char (mark))
  21805. (goto-char (org-element-property :end (org-element-at-point)))))
  21806. (let ((element (org-element-at-point)))
  21807. (end-of-line)
  21808. (push-mark (org-element-property :end element) t t)
  21809. (goto-char (org-element-property :begin element))))))
  21810. (defun org-narrow-to-element ()
  21811. "Narrow buffer to current element."
  21812. (interactive)
  21813. (let ((elem (org-element-at-point)))
  21814. (cond
  21815. ((eq (car elem) 'headline)
  21816. (narrow-to-region
  21817. (org-element-property :begin elem)
  21818. (org-element-property :end elem)))
  21819. ((memq (car elem) org-element-greater-elements)
  21820. (narrow-to-region
  21821. (org-element-property :contents-begin elem)
  21822. (org-element-property :contents-end elem)))
  21823. (t
  21824. (narrow-to-region
  21825. (org-element-property :begin elem)
  21826. (org-element-property :end elem))))))
  21827. (defun org-transpose-element ()
  21828. "Transpose current and previous elements, keeping blank lines between.
  21829. Point is moved after both elements."
  21830. (interactive)
  21831. (org-skip-whitespace)
  21832. (let ((end (org-element-property :end (org-element-at-point))))
  21833. (org-drag-element-backward)
  21834. (goto-char end)))
  21835. (defun org-unindent-buffer ()
  21836. "Un-indent the visible part of the buffer.
  21837. Relative indentation (between items, inside blocks, etc.) isn't
  21838. modified."
  21839. (interactive)
  21840. (unless (eq major-mode 'org-mode)
  21841. (user-error "Cannot un-indent a buffer not in Org mode"))
  21842. (let* ((parse-tree (org-element-parse-buffer 'greater-element))
  21843. unindent-tree ; For byte-compiler.
  21844. (unindent-tree
  21845. (function
  21846. (lambda (contents)
  21847. (mapc
  21848. (lambda (element)
  21849. (if (memq (org-element-type element) '(headline section))
  21850. (funcall unindent-tree (org-element-contents element))
  21851. (save-excursion
  21852. (save-restriction
  21853. (narrow-to-region
  21854. (org-element-property :begin element)
  21855. (org-element-property :end element))
  21856. (org-do-remove-indentation)))))
  21857. (reverse contents))))))
  21858. (funcall unindent-tree (org-element-contents parse-tree))))
  21859. (defun org-show-subtree ()
  21860. "Show everything after this heading at deeper levels."
  21861. (interactive)
  21862. (outline-flag-region
  21863. (point)
  21864. (save-excursion
  21865. (org-end-of-subtree t t))
  21866. nil))
  21867. (defun org-show-entry ()
  21868. "Show the body directly following this heading.
  21869. Show the heading too, if it is currently invisible."
  21870. (interactive)
  21871. (save-excursion
  21872. (condition-case nil
  21873. (progn
  21874. (org-back-to-heading t)
  21875. (outline-flag-region
  21876. (max (point-min) (1- (point)))
  21877. (save-excursion
  21878. (if (re-search-forward
  21879. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  21880. (match-beginning 1)
  21881. (point-max)))
  21882. nil)
  21883. (org-cycle-hide-drawers 'children))
  21884. (error nil))))
  21885. (defun org-make-options-regexp (kwds &optional extra)
  21886. "Make a regular expression for keyword lines."
  21887. (concat
  21888. "^#\\+\\("
  21889. (mapconcat 'regexp-quote kwds "\\|")
  21890. (if extra (concat "\\|" extra))
  21891. "\\):[ \t]*\\(.*\\)"))
  21892. ;; Make isearch reveal the necessary context
  21893. (defun org-isearch-end ()
  21894. "Reveal context after isearch exits."
  21895. (when isearch-success ; only if search was successful
  21896. (if (featurep 'xemacs)
  21897. ;; Under XEmacs, the hook is run in the correct place,
  21898. ;; we directly show the context.
  21899. (org-show-context 'isearch)
  21900. ;; In Emacs the hook runs *before* restoring the overlays.
  21901. ;; So we have to use a one-time post-command-hook to do this.
  21902. ;; (Emacs 22 has a special variable, see function `org-mode')
  21903. (unless (and (boundp 'isearch-mode-end-hook-quit)
  21904. isearch-mode-end-hook-quit)
  21905. ;; Only when the isearch was not quitted.
  21906. (org-add-hook 'post-command-hook 'org-isearch-post-command
  21907. 'append 'local)))))
  21908. (defun org-isearch-post-command ()
  21909. "Remove self from hook, and show context."
  21910. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  21911. (org-show-context 'isearch))
  21912. ;;;; Integration with and fixes for other packages
  21913. ;;; Imenu support
  21914. (defvar org-imenu-markers nil
  21915. "All markers currently used by Imenu.")
  21916. (make-variable-buffer-local 'org-imenu-markers)
  21917. (defun org-imenu-new-marker (&optional pos)
  21918. "Return a new marker for use by Imenu, and remember the marker."
  21919. (let ((m (make-marker)))
  21920. (move-marker m (or pos (point)))
  21921. (push m org-imenu-markers)
  21922. m))
  21923. (defun org-imenu-get-tree ()
  21924. "Produce the index for Imenu."
  21925. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  21926. (setq org-imenu-markers nil)
  21927. (let* ((n org-imenu-depth)
  21928. (re (concat "^" (org-get-limited-outline-regexp)))
  21929. (subs (make-vector (1+ n) nil))
  21930. (last-level 0)
  21931. m level head0 head)
  21932. (save-excursion
  21933. (save-restriction
  21934. (widen)
  21935. (goto-char (point-max))
  21936. (while (re-search-backward re nil t)
  21937. (setq level (org-reduced-level (funcall outline-level)))
  21938. (when (and (<= level n)
  21939. (looking-at org-complex-heading-regexp)
  21940. (setq head0 (org-match-string-no-properties 4)))
  21941. (setq head (org-link-display-format head0)
  21942. m (org-imenu-new-marker))
  21943. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  21944. (if (>= level last-level)
  21945. (push (cons head m) (aref subs level))
  21946. (push (cons head (aref subs (1+ level))) (aref subs level))
  21947. (loop for i from (1+ level) to n do (aset subs i nil)))
  21948. (setq last-level level)))))
  21949. (aref subs 1)))
  21950. (eval-after-load "imenu"
  21951. '(progn
  21952. (add-hook 'imenu-after-jump-hook
  21953. (lambda ()
  21954. (if (derived-mode-p 'org-mode)
  21955. (org-show-context 'org-goto))))))
  21956. (defun org-link-display-format (link)
  21957. "Replace a link with its the description.
  21958. If there is no description, use the link target."
  21959. (save-match-data
  21960. (if (string-match org-bracket-link-analytic-regexp link)
  21961. (replace-match (if (match-end 5)
  21962. (match-string 5 link)
  21963. (concat (match-string 1 link)
  21964. (match-string 3 link)))
  21965. nil t link)
  21966. link)))
  21967. (defun org-toggle-link-display ()
  21968. "Toggle the literal or descriptive display of links."
  21969. (interactive)
  21970. (if org-descriptive-links
  21971. (progn (org-remove-from-invisibility-spec '(org-link))
  21972. (org-restart-font-lock)
  21973. (setq org-descriptive-links nil))
  21974. (progn (add-to-invisibility-spec '(org-link))
  21975. (org-restart-font-lock)
  21976. (setq org-descriptive-links t))))
  21977. ;; Speedbar support
  21978. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  21979. "Overlay marking the agenda restriction line in speedbar.")
  21980. (overlay-put org-speedbar-restriction-lock-overlay
  21981. 'face 'org-agenda-restriction-lock)
  21982. (overlay-put org-speedbar-restriction-lock-overlay
  21983. 'help-echo "Agendas are currently limited to this item.")
  21984. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  21985. (defun org-speedbar-set-agenda-restriction ()
  21986. "Restrict future agenda commands to the location at point in speedbar.
  21987. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  21988. (interactive)
  21989. (require 'org-agenda)
  21990. (let (p m tp np dir txt)
  21991. (cond
  21992. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  21993. 'org-imenu t))
  21994. (setq m (get-text-property p 'org-imenu-marker))
  21995. (with-current-buffer (marker-buffer m)
  21996. (goto-char m)
  21997. (org-agenda-set-restriction-lock 'subtree)))
  21998. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  21999. 'speedbar-function 'speedbar-find-file))
  22000. (setq tp (previous-single-property-change
  22001. (1+ p) 'speedbar-function)
  22002. np (next-single-property-change
  22003. tp 'speedbar-function)
  22004. dir (speedbar-line-directory)
  22005. txt (buffer-substring-no-properties (or tp (point-min))
  22006. (or np (point-max))))
  22007. (with-current-buffer (find-file-noselect
  22008. (let ((default-directory dir))
  22009. (expand-file-name txt)))
  22010. (unless (derived-mode-p 'org-mode)
  22011. (user-error "Cannot restrict to non-Org-mode file"))
  22012. (org-agenda-set-restriction-lock 'file)))
  22013. (t (user-error "Don't know how to restrict Org-mode's agenda")))
  22014. (move-overlay org-speedbar-restriction-lock-overlay
  22015. (point-at-bol) (point-at-eol))
  22016. (setq current-prefix-arg nil)
  22017. (org-agenda-maybe-redo)))
  22018. (defvar speedbar-file-key-map)
  22019. (declare-function speedbar-add-supported-extension "speedbar" (extension))
  22020. (eval-after-load "speedbar"
  22021. '(progn
  22022. (speedbar-add-supported-extension ".org")
  22023. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  22024. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  22025. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  22026. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  22027. (add-hook 'speedbar-visiting-tag-hook
  22028. (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
  22029. ;;; Fixes and Hacks for problems with other packages
  22030. ;; Make flyspell not check words in links, to not mess up our keymap
  22031. (defvar org-element-affiliated-keywords) ; From org-element.el
  22032. (defvar org-element-block-name-alist) ; From org-element.el
  22033. (defun org-mode-flyspell-verify ()
  22034. "Don't let flyspell put overlays at active buttons, or on
  22035. {todo,all-time,additional-option-like}-keywords."
  22036. (require 'org-element) ; For `org-element-affiliated-keywords'
  22037. (let ((pos (max (1- (point)) (point-min)))
  22038. (word (thing-at-point 'word)))
  22039. (and (not (get-text-property pos 'keymap))
  22040. (not (get-text-property pos 'org-no-flyspell))
  22041. (not (member word org-todo-keywords-1))
  22042. (not (member word org-all-time-keywords))
  22043. (not (member word org-options-keywords))
  22044. (not (member word (mapcar 'car org-startup-options)))
  22045. (not (member-ignore-case word org-element-affiliated-keywords))
  22046. (not (member-ignore-case word (org-get-export-keywords)))
  22047. (not (member-ignore-case
  22048. word (mapcar 'car org-element-block-name-alist)))
  22049. (not (member-ignore-case word '("BEGIN" "END" "ATTR")))
  22050. (not (org-in-src-block-p)))))
  22051. (defun org-remove-flyspell-overlays-in (beg end)
  22052. "Remove flyspell overlays in region."
  22053. (and (org-bound-and-true-p flyspell-mode)
  22054. (fboundp 'flyspell-delete-region-overlays)
  22055. (flyspell-delete-region-overlays beg end))
  22056. (add-text-properties beg end '(org-no-flyspell t)))
  22057. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  22058. (eval-after-load "bookmark"
  22059. '(if (boundp 'bookmark-after-jump-hook)
  22060. ;; We can use the hook
  22061. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  22062. ;; Hook not available, use advice
  22063. (defadvice bookmark-jump (after org-make-visible activate)
  22064. "Make the position visible."
  22065. (org-bookmark-jump-unhide))))
  22066. ;; Make sure saveplace shows the location if it was hidden
  22067. (eval-after-load "saveplace"
  22068. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  22069. "Make the position visible."
  22070. (org-bookmark-jump-unhide)))
  22071. ;; Make sure ecb shows the location if it was hidden
  22072. (eval-after-load "ecb"
  22073. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  22074. "Make hierarchy visible when jumping into location from ECB tree buffer."
  22075. (if (derived-mode-p 'org-mode)
  22076. (org-show-context))))
  22077. (defun org-bookmark-jump-unhide ()
  22078. "Unhide the current position, to show the bookmark location."
  22079. (and (derived-mode-p 'org-mode)
  22080. (or (outline-invisible-p)
  22081. (save-excursion (goto-char (max (point-min) (1- (point))))
  22082. (outline-invisible-p)))
  22083. (org-show-context 'bookmark-jump)))
  22084. ;; Make session.el ignore our circular variable
  22085. (defvar session-globals-exclude)
  22086. (eval-after-load "session"
  22087. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  22088. ;;;; Finish up
  22089. (provide 'org)
  22090. (run-hooks 'org-load-hook)
  22091. ;;; org.el ends here